vmx.c 401 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256
  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/asm.h>
  39. #include <asm/cpu.h>
  40. #include <asm/io.h>
  41. #include <asm/desc.h>
  42. #include <asm/vmx.h>
  43. #include <asm/virtext.h>
  44. #include <asm/mce.h>
  45. #include <asm/fpu/internal.h>
  46. #include <asm/perf_event.h>
  47. #include <asm/debugreg.h>
  48. #include <asm/kexec.h>
  49. #include <asm/apic.h>
  50. #include <asm/irq_remapping.h>
  51. #include <asm/mmu_context.h>
  52. #include <asm/spec-ctrl.h>
  53. #include <asm/mshyperv.h>
  54. #include "trace.h"
  55. #include "pmu.h"
  56. #include "vmx_evmcs.h"
  57. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  58. #define __ex_clear(x, reg) \
  59. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  60. MODULE_AUTHOR("Qumranet");
  61. MODULE_LICENSE("GPL");
  62. static const struct x86_cpu_id vmx_cpu_id[] = {
  63. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  64. {}
  65. };
  66. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  67. static bool __read_mostly enable_vpid = 1;
  68. module_param_named(vpid, enable_vpid, bool, 0444);
  69. static bool __read_mostly enable_vnmi = 1;
  70. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  71. static bool __read_mostly flexpriority_enabled = 1;
  72. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  73. static bool __read_mostly enable_ept = 1;
  74. module_param_named(ept, enable_ept, bool, S_IRUGO);
  75. static bool __read_mostly enable_unrestricted_guest = 1;
  76. module_param_named(unrestricted_guest,
  77. enable_unrestricted_guest, bool, S_IRUGO);
  78. static bool __read_mostly enable_ept_ad_bits = 1;
  79. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  80. static bool __read_mostly emulate_invalid_guest_state = true;
  81. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  82. static bool __read_mostly fasteoi = 1;
  83. module_param(fasteoi, bool, S_IRUGO);
  84. static bool __read_mostly enable_apicv = 1;
  85. module_param(enable_apicv, bool, S_IRUGO);
  86. static bool __read_mostly enable_shadow_vmcs = 1;
  87. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  88. /*
  89. * If nested=1, nested virtualization is supported, i.e., guests may use
  90. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  91. * use VMX instructions.
  92. */
  93. static bool __read_mostly nested = 0;
  94. module_param(nested, bool, S_IRUGO);
  95. static u64 __read_mostly host_xss;
  96. static bool __read_mostly enable_pml = 1;
  97. module_param_named(pml, enable_pml, bool, S_IRUGO);
  98. #define MSR_TYPE_R 1
  99. #define MSR_TYPE_W 2
  100. #define MSR_TYPE_RW 3
  101. #define MSR_BITMAP_MODE_X2APIC 1
  102. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  103. #define MSR_BITMAP_MODE_LM 4
  104. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  105. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  106. static int __read_mostly cpu_preemption_timer_multi;
  107. static bool __read_mostly enable_preemption_timer = 1;
  108. #ifdef CONFIG_X86_64
  109. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  110. #endif
  111. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  112. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
  113. #define KVM_VM_CR0_ALWAYS_ON \
  114. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
  115. X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
  116. #define KVM_CR4_GUEST_OWNED_BITS \
  117. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  118. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  119. #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
  120. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  121. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  122. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  123. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  124. /*
  125. * Hyper-V requires all of these, so mark them as supported even though
  126. * they are just treated the same as all-context.
  127. */
  128. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  129. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  130. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  131. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  132. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  133. /*
  134. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  135. * ple_gap: upper bound on the amount of time between two successive
  136. * executions of PAUSE in a loop. Also indicate if ple enabled.
  137. * According to test, this time is usually smaller than 128 cycles.
  138. * ple_window: upper bound on the amount of time a guest is allowed to execute
  139. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  140. * less than 2^12 cycles
  141. * Time is measured based on a counter that runs at the same rate as the TSC,
  142. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  143. */
  144. static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
  145. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  146. module_param(ple_window, uint, 0444);
  147. /* Default doubles per-vcpu window every exit. */
  148. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  149. module_param(ple_window_grow, uint, 0444);
  150. /* Default resets per-vcpu window every exit to ple_window. */
  151. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  152. module_param(ple_window_shrink, uint, 0444);
  153. /* Default is to compute the maximum so we can never overflow. */
  154. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  155. module_param(ple_window_max, uint, 0444);
  156. extern const ulong vmx_return;
  157. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
  158. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
  159. static DEFINE_MUTEX(vmx_l1d_flush_mutex);
  160. /* Storage for pre module init parameter parsing */
  161. static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
  162. static const struct {
  163. const char *option;
  164. bool for_parse;
  165. } vmentry_l1d_param[] = {
  166. [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
  167. [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
  168. [VMENTER_L1D_FLUSH_COND] = {"cond", true},
  169. [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
  170. [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
  171. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
  172. };
  173. #define L1D_CACHE_ORDER 4
  174. static void *vmx_l1d_flush_pages;
  175. static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
  176. {
  177. struct page *page;
  178. unsigned int i;
  179. if (!enable_ept) {
  180. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
  181. return 0;
  182. }
  183. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
  184. u64 msr;
  185. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
  186. if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
  187. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
  188. return 0;
  189. }
  190. }
  191. /* If set to auto use the default l1tf mitigation method */
  192. if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
  193. switch (l1tf_mitigation) {
  194. case L1TF_MITIGATION_OFF:
  195. l1tf = VMENTER_L1D_FLUSH_NEVER;
  196. break;
  197. case L1TF_MITIGATION_FLUSH_NOWARN:
  198. case L1TF_MITIGATION_FLUSH:
  199. case L1TF_MITIGATION_FLUSH_NOSMT:
  200. l1tf = VMENTER_L1D_FLUSH_COND;
  201. break;
  202. case L1TF_MITIGATION_FULL:
  203. case L1TF_MITIGATION_FULL_FORCE:
  204. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  205. break;
  206. }
  207. } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
  208. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  209. }
  210. if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
  211. !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  212. page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
  213. if (!page)
  214. return -ENOMEM;
  215. vmx_l1d_flush_pages = page_address(page);
  216. /*
  217. * Initialize each page with a different pattern in
  218. * order to protect against KSM in the nested
  219. * virtualization case.
  220. */
  221. for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
  222. memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
  223. PAGE_SIZE);
  224. }
  225. }
  226. l1tf_vmx_mitigation = l1tf;
  227. if (l1tf != VMENTER_L1D_FLUSH_NEVER)
  228. static_branch_enable(&vmx_l1d_should_flush);
  229. else
  230. static_branch_disable(&vmx_l1d_should_flush);
  231. if (l1tf == VMENTER_L1D_FLUSH_COND)
  232. static_branch_enable(&vmx_l1d_flush_cond);
  233. else
  234. static_branch_disable(&vmx_l1d_flush_cond);
  235. return 0;
  236. }
  237. static int vmentry_l1d_flush_parse(const char *s)
  238. {
  239. unsigned int i;
  240. if (s) {
  241. for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
  242. if (vmentry_l1d_param[i].for_parse &&
  243. sysfs_streq(s, vmentry_l1d_param[i].option))
  244. return i;
  245. }
  246. }
  247. return -EINVAL;
  248. }
  249. static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
  250. {
  251. int l1tf, ret;
  252. l1tf = vmentry_l1d_flush_parse(s);
  253. if (l1tf < 0)
  254. return l1tf;
  255. if (!boot_cpu_has(X86_BUG_L1TF))
  256. return 0;
  257. /*
  258. * Has vmx_init() run already? If not then this is the pre init
  259. * parameter parsing. In that case just store the value and let
  260. * vmx_init() do the proper setup after enable_ept has been
  261. * established.
  262. */
  263. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
  264. vmentry_l1d_flush_param = l1tf;
  265. return 0;
  266. }
  267. mutex_lock(&vmx_l1d_flush_mutex);
  268. ret = vmx_setup_l1d_flush(l1tf);
  269. mutex_unlock(&vmx_l1d_flush_mutex);
  270. return ret;
  271. }
  272. static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
  273. {
  274. if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
  275. return sprintf(s, "???\n");
  276. return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
  277. }
  278. static const struct kernel_param_ops vmentry_l1d_flush_ops = {
  279. .set = vmentry_l1d_flush_set,
  280. .get = vmentry_l1d_flush_get,
  281. };
  282. module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
  283. enum ept_pointers_status {
  284. EPT_POINTERS_CHECK = 0,
  285. EPT_POINTERS_MATCH = 1,
  286. EPT_POINTERS_MISMATCH = 2
  287. };
  288. struct kvm_vmx {
  289. struct kvm kvm;
  290. unsigned int tss_addr;
  291. bool ept_identity_pagetable_done;
  292. gpa_t ept_identity_map_addr;
  293. enum ept_pointers_status ept_pointers_match;
  294. spinlock_t ept_pointer_lock;
  295. };
  296. #define NR_AUTOLOAD_MSRS 8
  297. struct vmcs_hdr {
  298. u32 revision_id:31;
  299. u32 shadow_vmcs:1;
  300. };
  301. struct vmcs {
  302. struct vmcs_hdr hdr;
  303. u32 abort;
  304. char data[0];
  305. };
  306. /*
  307. * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
  308. * and whose values change infrequently, but are not constant. I.e. this is
  309. * used as a write-through cache of the corresponding VMCS fields.
  310. */
  311. struct vmcs_host_state {
  312. unsigned long cr3; /* May not match real cr3 */
  313. unsigned long cr4; /* May not match real cr4 */
  314. unsigned long gs_base;
  315. unsigned long fs_base;
  316. u16 fs_sel, gs_sel, ldt_sel;
  317. #ifdef CONFIG_X86_64
  318. u16 ds_sel, es_sel;
  319. #endif
  320. };
  321. /*
  322. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  323. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  324. * loaded on this CPU (so we can clear them if the CPU goes down).
  325. */
  326. struct loaded_vmcs {
  327. struct vmcs *vmcs;
  328. struct vmcs *shadow_vmcs;
  329. int cpu;
  330. bool launched;
  331. bool nmi_known_unmasked;
  332. /* Support for vnmi-less CPUs */
  333. int soft_vnmi_blocked;
  334. ktime_t entry_time;
  335. s64 vnmi_blocked_time;
  336. unsigned long *msr_bitmap;
  337. struct list_head loaded_vmcss_on_cpu_link;
  338. struct vmcs_host_state host_state;
  339. };
  340. struct shared_msr_entry {
  341. unsigned index;
  342. u64 data;
  343. u64 mask;
  344. };
  345. /*
  346. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  347. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  348. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  349. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  350. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  351. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  352. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  353. * underlying hardware which will be used to run L2.
  354. * This structure is packed to ensure that its layout is identical across
  355. * machines (necessary for live migration).
  356. *
  357. * IMPORTANT: Changing the layout of existing fields in this structure
  358. * will break save/restore compatibility with older kvm releases. When
  359. * adding new fields, either use space in the reserved padding* arrays
  360. * or add the new fields to the end of the structure.
  361. */
  362. typedef u64 natural_width;
  363. struct __packed vmcs12 {
  364. /* According to the Intel spec, a VMCS region must start with the
  365. * following two fields. Then follow implementation-specific data.
  366. */
  367. struct vmcs_hdr hdr;
  368. u32 abort;
  369. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  370. u32 padding[7]; /* room for future expansion */
  371. u64 io_bitmap_a;
  372. u64 io_bitmap_b;
  373. u64 msr_bitmap;
  374. u64 vm_exit_msr_store_addr;
  375. u64 vm_exit_msr_load_addr;
  376. u64 vm_entry_msr_load_addr;
  377. u64 tsc_offset;
  378. u64 virtual_apic_page_addr;
  379. u64 apic_access_addr;
  380. u64 posted_intr_desc_addr;
  381. u64 ept_pointer;
  382. u64 eoi_exit_bitmap0;
  383. u64 eoi_exit_bitmap1;
  384. u64 eoi_exit_bitmap2;
  385. u64 eoi_exit_bitmap3;
  386. u64 xss_exit_bitmap;
  387. u64 guest_physical_address;
  388. u64 vmcs_link_pointer;
  389. u64 guest_ia32_debugctl;
  390. u64 guest_ia32_pat;
  391. u64 guest_ia32_efer;
  392. u64 guest_ia32_perf_global_ctrl;
  393. u64 guest_pdptr0;
  394. u64 guest_pdptr1;
  395. u64 guest_pdptr2;
  396. u64 guest_pdptr3;
  397. u64 guest_bndcfgs;
  398. u64 host_ia32_pat;
  399. u64 host_ia32_efer;
  400. u64 host_ia32_perf_global_ctrl;
  401. u64 vmread_bitmap;
  402. u64 vmwrite_bitmap;
  403. u64 vm_function_control;
  404. u64 eptp_list_address;
  405. u64 pml_address;
  406. u64 padding64[3]; /* room for future expansion */
  407. /*
  408. * To allow migration of L1 (complete with its L2 guests) between
  409. * machines of different natural widths (32 or 64 bit), we cannot have
  410. * unsigned long fields with no explict size. We use u64 (aliased
  411. * natural_width) instead. Luckily, x86 is little-endian.
  412. */
  413. natural_width cr0_guest_host_mask;
  414. natural_width cr4_guest_host_mask;
  415. natural_width cr0_read_shadow;
  416. natural_width cr4_read_shadow;
  417. natural_width cr3_target_value0;
  418. natural_width cr3_target_value1;
  419. natural_width cr3_target_value2;
  420. natural_width cr3_target_value3;
  421. natural_width exit_qualification;
  422. natural_width guest_linear_address;
  423. natural_width guest_cr0;
  424. natural_width guest_cr3;
  425. natural_width guest_cr4;
  426. natural_width guest_es_base;
  427. natural_width guest_cs_base;
  428. natural_width guest_ss_base;
  429. natural_width guest_ds_base;
  430. natural_width guest_fs_base;
  431. natural_width guest_gs_base;
  432. natural_width guest_ldtr_base;
  433. natural_width guest_tr_base;
  434. natural_width guest_gdtr_base;
  435. natural_width guest_idtr_base;
  436. natural_width guest_dr7;
  437. natural_width guest_rsp;
  438. natural_width guest_rip;
  439. natural_width guest_rflags;
  440. natural_width guest_pending_dbg_exceptions;
  441. natural_width guest_sysenter_esp;
  442. natural_width guest_sysenter_eip;
  443. natural_width host_cr0;
  444. natural_width host_cr3;
  445. natural_width host_cr4;
  446. natural_width host_fs_base;
  447. natural_width host_gs_base;
  448. natural_width host_tr_base;
  449. natural_width host_gdtr_base;
  450. natural_width host_idtr_base;
  451. natural_width host_ia32_sysenter_esp;
  452. natural_width host_ia32_sysenter_eip;
  453. natural_width host_rsp;
  454. natural_width host_rip;
  455. natural_width paddingl[8]; /* room for future expansion */
  456. u32 pin_based_vm_exec_control;
  457. u32 cpu_based_vm_exec_control;
  458. u32 exception_bitmap;
  459. u32 page_fault_error_code_mask;
  460. u32 page_fault_error_code_match;
  461. u32 cr3_target_count;
  462. u32 vm_exit_controls;
  463. u32 vm_exit_msr_store_count;
  464. u32 vm_exit_msr_load_count;
  465. u32 vm_entry_controls;
  466. u32 vm_entry_msr_load_count;
  467. u32 vm_entry_intr_info_field;
  468. u32 vm_entry_exception_error_code;
  469. u32 vm_entry_instruction_len;
  470. u32 tpr_threshold;
  471. u32 secondary_vm_exec_control;
  472. u32 vm_instruction_error;
  473. u32 vm_exit_reason;
  474. u32 vm_exit_intr_info;
  475. u32 vm_exit_intr_error_code;
  476. u32 idt_vectoring_info_field;
  477. u32 idt_vectoring_error_code;
  478. u32 vm_exit_instruction_len;
  479. u32 vmx_instruction_info;
  480. u32 guest_es_limit;
  481. u32 guest_cs_limit;
  482. u32 guest_ss_limit;
  483. u32 guest_ds_limit;
  484. u32 guest_fs_limit;
  485. u32 guest_gs_limit;
  486. u32 guest_ldtr_limit;
  487. u32 guest_tr_limit;
  488. u32 guest_gdtr_limit;
  489. u32 guest_idtr_limit;
  490. u32 guest_es_ar_bytes;
  491. u32 guest_cs_ar_bytes;
  492. u32 guest_ss_ar_bytes;
  493. u32 guest_ds_ar_bytes;
  494. u32 guest_fs_ar_bytes;
  495. u32 guest_gs_ar_bytes;
  496. u32 guest_ldtr_ar_bytes;
  497. u32 guest_tr_ar_bytes;
  498. u32 guest_interruptibility_info;
  499. u32 guest_activity_state;
  500. u32 guest_sysenter_cs;
  501. u32 host_ia32_sysenter_cs;
  502. u32 vmx_preemption_timer_value;
  503. u32 padding32[7]; /* room for future expansion */
  504. u16 virtual_processor_id;
  505. u16 posted_intr_nv;
  506. u16 guest_es_selector;
  507. u16 guest_cs_selector;
  508. u16 guest_ss_selector;
  509. u16 guest_ds_selector;
  510. u16 guest_fs_selector;
  511. u16 guest_gs_selector;
  512. u16 guest_ldtr_selector;
  513. u16 guest_tr_selector;
  514. u16 guest_intr_status;
  515. u16 host_es_selector;
  516. u16 host_cs_selector;
  517. u16 host_ss_selector;
  518. u16 host_ds_selector;
  519. u16 host_fs_selector;
  520. u16 host_gs_selector;
  521. u16 host_tr_selector;
  522. u16 guest_pml_index;
  523. };
  524. /*
  525. * For save/restore compatibility, the vmcs12 field offsets must not change.
  526. */
  527. #define CHECK_OFFSET(field, loc) \
  528. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  529. "Offset of " #field " in struct vmcs12 has changed.")
  530. static inline void vmx_check_vmcs12_offsets(void) {
  531. CHECK_OFFSET(hdr, 0);
  532. CHECK_OFFSET(abort, 4);
  533. CHECK_OFFSET(launch_state, 8);
  534. CHECK_OFFSET(io_bitmap_a, 40);
  535. CHECK_OFFSET(io_bitmap_b, 48);
  536. CHECK_OFFSET(msr_bitmap, 56);
  537. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  538. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  539. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  540. CHECK_OFFSET(tsc_offset, 88);
  541. CHECK_OFFSET(virtual_apic_page_addr, 96);
  542. CHECK_OFFSET(apic_access_addr, 104);
  543. CHECK_OFFSET(posted_intr_desc_addr, 112);
  544. CHECK_OFFSET(ept_pointer, 120);
  545. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  546. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  547. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  548. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  549. CHECK_OFFSET(xss_exit_bitmap, 160);
  550. CHECK_OFFSET(guest_physical_address, 168);
  551. CHECK_OFFSET(vmcs_link_pointer, 176);
  552. CHECK_OFFSET(guest_ia32_debugctl, 184);
  553. CHECK_OFFSET(guest_ia32_pat, 192);
  554. CHECK_OFFSET(guest_ia32_efer, 200);
  555. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  556. CHECK_OFFSET(guest_pdptr0, 216);
  557. CHECK_OFFSET(guest_pdptr1, 224);
  558. CHECK_OFFSET(guest_pdptr2, 232);
  559. CHECK_OFFSET(guest_pdptr3, 240);
  560. CHECK_OFFSET(guest_bndcfgs, 248);
  561. CHECK_OFFSET(host_ia32_pat, 256);
  562. CHECK_OFFSET(host_ia32_efer, 264);
  563. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  564. CHECK_OFFSET(vmread_bitmap, 280);
  565. CHECK_OFFSET(vmwrite_bitmap, 288);
  566. CHECK_OFFSET(vm_function_control, 296);
  567. CHECK_OFFSET(eptp_list_address, 304);
  568. CHECK_OFFSET(pml_address, 312);
  569. CHECK_OFFSET(cr0_guest_host_mask, 344);
  570. CHECK_OFFSET(cr4_guest_host_mask, 352);
  571. CHECK_OFFSET(cr0_read_shadow, 360);
  572. CHECK_OFFSET(cr4_read_shadow, 368);
  573. CHECK_OFFSET(cr3_target_value0, 376);
  574. CHECK_OFFSET(cr3_target_value1, 384);
  575. CHECK_OFFSET(cr3_target_value2, 392);
  576. CHECK_OFFSET(cr3_target_value3, 400);
  577. CHECK_OFFSET(exit_qualification, 408);
  578. CHECK_OFFSET(guest_linear_address, 416);
  579. CHECK_OFFSET(guest_cr0, 424);
  580. CHECK_OFFSET(guest_cr3, 432);
  581. CHECK_OFFSET(guest_cr4, 440);
  582. CHECK_OFFSET(guest_es_base, 448);
  583. CHECK_OFFSET(guest_cs_base, 456);
  584. CHECK_OFFSET(guest_ss_base, 464);
  585. CHECK_OFFSET(guest_ds_base, 472);
  586. CHECK_OFFSET(guest_fs_base, 480);
  587. CHECK_OFFSET(guest_gs_base, 488);
  588. CHECK_OFFSET(guest_ldtr_base, 496);
  589. CHECK_OFFSET(guest_tr_base, 504);
  590. CHECK_OFFSET(guest_gdtr_base, 512);
  591. CHECK_OFFSET(guest_idtr_base, 520);
  592. CHECK_OFFSET(guest_dr7, 528);
  593. CHECK_OFFSET(guest_rsp, 536);
  594. CHECK_OFFSET(guest_rip, 544);
  595. CHECK_OFFSET(guest_rflags, 552);
  596. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  597. CHECK_OFFSET(guest_sysenter_esp, 568);
  598. CHECK_OFFSET(guest_sysenter_eip, 576);
  599. CHECK_OFFSET(host_cr0, 584);
  600. CHECK_OFFSET(host_cr3, 592);
  601. CHECK_OFFSET(host_cr4, 600);
  602. CHECK_OFFSET(host_fs_base, 608);
  603. CHECK_OFFSET(host_gs_base, 616);
  604. CHECK_OFFSET(host_tr_base, 624);
  605. CHECK_OFFSET(host_gdtr_base, 632);
  606. CHECK_OFFSET(host_idtr_base, 640);
  607. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  608. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  609. CHECK_OFFSET(host_rsp, 664);
  610. CHECK_OFFSET(host_rip, 672);
  611. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  612. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  613. CHECK_OFFSET(exception_bitmap, 752);
  614. CHECK_OFFSET(page_fault_error_code_mask, 756);
  615. CHECK_OFFSET(page_fault_error_code_match, 760);
  616. CHECK_OFFSET(cr3_target_count, 764);
  617. CHECK_OFFSET(vm_exit_controls, 768);
  618. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  619. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  620. CHECK_OFFSET(vm_entry_controls, 780);
  621. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  622. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  623. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  624. CHECK_OFFSET(vm_entry_instruction_len, 796);
  625. CHECK_OFFSET(tpr_threshold, 800);
  626. CHECK_OFFSET(secondary_vm_exec_control, 804);
  627. CHECK_OFFSET(vm_instruction_error, 808);
  628. CHECK_OFFSET(vm_exit_reason, 812);
  629. CHECK_OFFSET(vm_exit_intr_info, 816);
  630. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  631. CHECK_OFFSET(idt_vectoring_info_field, 824);
  632. CHECK_OFFSET(idt_vectoring_error_code, 828);
  633. CHECK_OFFSET(vm_exit_instruction_len, 832);
  634. CHECK_OFFSET(vmx_instruction_info, 836);
  635. CHECK_OFFSET(guest_es_limit, 840);
  636. CHECK_OFFSET(guest_cs_limit, 844);
  637. CHECK_OFFSET(guest_ss_limit, 848);
  638. CHECK_OFFSET(guest_ds_limit, 852);
  639. CHECK_OFFSET(guest_fs_limit, 856);
  640. CHECK_OFFSET(guest_gs_limit, 860);
  641. CHECK_OFFSET(guest_ldtr_limit, 864);
  642. CHECK_OFFSET(guest_tr_limit, 868);
  643. CHECK_OFFSET(guest_gdtr_limit, 872);
  644. CHECK_OFFSET(guest_idtr_limit, 876);
  645. CHECK_OFFSET(guest_es_ar_bytes, 880);
  646. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  647. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  648. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  649. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  650. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  651. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  652. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  653. CHECK_OFFSET(guest_interruptibility_info, 912);
  654. CHECK_OFFSET(guest_activity_state, 916);
  655. CHECK_OFFSET(guest_sysenter_cs, 920);
  656. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  657. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  658. CHECK_OFFSET(virtual_processor_id, 960);
  659. CHECK_OFFSET(posted_intr_nv, 962);
  660. CHECK_OFFSET(guest_es_selector, 964);
  661. CHECK_OFFSET(guest_cs_selector, 966);
  662. CHECK_OFFSET(guest_ss_selector, 968);
  663. CHECK_OFFSET(guest_ds_selector, 970);
  664. CHECK_OFFSET(guest_fs_selector, 972);
  665. CHECK_OFFSET(guest_gs_selector, 974);
  666. CHECK_OFFSET(guest_ldtr_selector, 976);
  667. CHECK_OFFSET(guest_tr_selector, 978);
  668. CHECK_OFFSET(guest_intr_status, 980);
  669. CHECK_OFFSET(host_es_selector, 982);
  670. CHECK_OFFSET(host_cs_selector, 984);
  671. CHECK_OFFSET(host_ss_selector, 986);
  672. CHECK_OFFSET(host_ds_selector, 988);
  673. CHECK_OFFSET(host_fs_selector, 990);
  674. CHECK_OFFSET(host_gs_selector, 992);
  675. CHECK_OFFSET(host_tr_selector, 994);
  676. CHECK_OFFSET(guest_pml_index, 996);
  677. }
  678. /*
  679. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  680. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  681. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  682. *
  683. * IMPORTANT: Changing this value will break save/restore compatibility with
  684. * older kvm releases.
  685. */
  686. #define VMCS12_REVISION 0x11e57ed0
  687. /*
  688. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  689. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  690. * current implementation, 4K are reserved to avoid future complications.
  691. */
  692. #define VMCS12_SIZE 0x1000
  693. /*
  694. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  695. * supported VMCS12 field encoding.
  696. */
  697. #define VMCS12_MAX_FIELD_INDEX 0x17
  698. struct nested_vmx_msrs {
  699. /*
  700. * We only store the "true" versions of the VMX capability MSRs. We
  701. * generate the "non-true" versions by setting the must-be-1 bits
  702. * according to the SDM.
  703. */
  704. u32 procbased_ctls_low;
  705. u32 procbased_ctls_high;
  706. u32 secondary_ctls_low;
  707. u32 secondary_ctls_high;
  708. u32 pinbased_ctls_low;
  709. u32 pinbased_ctls_high;
  710. u32 exit_ctls_low;
  711. u32 exit_ctls_high;
  712. u32 entry_ctls_low;
  713. u32 entry_ctls_high;
  714. u32 misc_low;
  715. u32 misc_high;
  716. u32 ept_caps;
  717. u32 vpid_caps;
  718. u64 basic;
  719. u64 cr0_fixed0;
  720. u64 cr0_fixed1;
  721. u64 cr4_fixed0;
  722. u64 cr4_fixed1;
  723. u64 vmcs_enum;
  724. u64 vmfunc_controls;
  725. };
  726. /*
  727. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  728. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  729. */
  730. struct nested_vmx {
  731. /* Has the level1 guest done vmxon? */
  732. bool vmxon;
  733. gpa_t vmxon_ptr;
  734. bool pml_full;
  735. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  736. gpa_t current_vmptr;
  737. /*
  738. * Cache of the guest's VMCS, existing outside of guest memory.
  739. * Loaded from guest memory during VMPTRLD. Flushed to guest
  740. * memory during VMCLEAR and VMPTRLD.
  741. */
  742. struct vmcs12 *cached_vmcs12;
  743. /*
  744. * Cache of the guest's shadow VMCS, existing outside of guest
  745. * memory. Loaded from guest memory during VM entry. Flushed
  746. * to guest memory during VM exit.
  747. */
  748. struct vmcs12 *cached_shadow_vmcs12;
  749. /*
  750. * Indicates if the shadow vmcs must be updated with the
  751. * data hold by vmcs12
  752. */
  753. bool sync_shadow_vmcs;
  754. bool dirty_vmcs12;
  755. bool change_vmcs01_virtual_apic_mode;
  756. /* L2 must run next, and mustn't decide to exit to L1. */
  757. bool nested_run_pending;
  758. struct loaded_vmcs vmcs02;
  759. /*
  760. * Guest pages referred to in the vmcs02 with host-physical
  761. * pointers, so we must keep them pinned while L2 runs.
  762. */
  763. struct page *apic_access_page;
  764. struct page *virtual_apic_page;
  765. struct page *pi_desc_page;
  766. struct pi_desc *pi_desc;
  767. bool pi_pending;
  768. u16 posted_intr_nv;
  769. struct hrtimer preemption_timer;
  770. bool preemption_timer_expired;
  771. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  772. u64 vmcs01_debugctl;
  773. u16 vpid02;
  774. u16 last_vpid;
  775. struct nested_vmx_msrs msrs;
  776. /* SMM related state */
  777. struct {
  778. /* in VMX operation on SMM entry? */
  779. bool vmxon;
  780. /* in guest mode on SMM entry? */
  781. bool guest_mode;
  782. } smm;
  783. };
  784. #define POSTED_INTR_ON 0
  785. #define POSTED_INTR_SN 1
  786. /* Posted-Interrupt Descriptor */
  787. struct pi_desc {
  788. u32 pir[8]; /* Posted interrupt requested */
  789. union {
  790. struct {
  791. /* bit 256 - Outstanding Notification */
  792. u16 on : 1,
  793. /* bit 257 - Suppress Notification */
  794. sn : 1,
  795. /* bit 271:258 - Reserved */
  796. rsvd_1 : 14;
  797. /* bit 279:272 - Notification Vector */
  798. u8 nv;
  799. /* bit 287:280 - Reserved */
  800. u8 rsvd_2;
  801. /* bit 319:288 - Notification Destination */
  802. u32 ndst;
  803. };
  804. u64 control;
  805. };
  806. u32 rsvd[6];
  807. } __aligned(64);
  808. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  809. {
  810. return test_and_set_bit(POSTED_INTR_ON,
  811. (unsigned long *)&pi_desc->control);
  812. }
  813. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  814. {
  815. return test_and_clear_bit(POSTED_INTR_ON,
  816. (unsigned long *)&pi_desc->control);
  817. }
  818. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  819. {
  820. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  821. }
  822. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  823. {
  824. return clear_bit(POSTED_INTR_SN,
  825. (unsigned long *)&pi_desc->control);
  826. }
  827. static inline void pi_set_sn(struct pi_desc *pi_desc)
  828. {
  829. return set_bit(POSTED_INTR_SN,
  830. (unsigned long *)&pi_desc->control);
  831. }
  832. static inline void pi_clear_on(struct pi_desc *pi_desc)
  833. {
  834. clear_bit(POSTED_INTR_ON,
  835. (unsigned long *)&pi_desc->control);
  836. }
  837. static inline int pi_test_on(struct pi_desc *pi_desc)
  838. {
  839. return test_bit(POSTED_INTR_ON,
  840. (unsigned long *)&pi_desc->control);
  841. }
  842. static inline int pi_test_sn(struct pi_desc *pi_desc)
  843. {
  844. return test_bit(POSTED_INTR_SN,
  845. (unsigned long *)&pi_desc->control);
  846. }
  847. struct vmx_msrs {
  848. unsigned int nr;
  849. struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
  850. };
  851. struct vcpu_vmx {
  852. struct kvm_vcpu vcpu;
  853. unsigned long host_rsp;
  854. u8 fail;
  855. u8 msr_bitmap_mode;
  856. u32 exit_intr_info;
  857. u32 idt_vectoring_info;
  858. ulong rflags;
  859. struct shared_msr_entry *guest_msrs;
  860. int nmsrs;
  861. int save_nmsrs;
  862. unsigned long host_idt_base;
  863. #ifdef CONFIG_X86_64
  864. u64 msr_host_kernel_gs_base;
  865. u64 msr_guest_kernel_gs_base;
  866. #endif
  867. u64 arch_capabilities;
  868. u64 spec_ctrl;
  869. u32 vm_entry_controls_shadow;
  870. u32 vm_exit_controls_shadow;
  871. u32 secondary_exec_control;
  872. /*
  873. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  874. * non-nested (L1) guest, it always points to vmcs01. For a nested
  875. * guest (L2), it points to a different VMCS. loaded_cpu_state points
  876. * to the VMCS whose state is loaded into the CPU registers that only
  877. * need to be switched when transitioning to/from the kernel; a NULL
  878. * value indicates that host state is loaded.
  879. */
  880. struct loaded_vmcs vmcs01;
  881. struct loaded_vmcs *loaded_vmcs;
  882. struct loaded_vmcs *loaded_cpu_state;
  883. bool __launched; /* temporary, used in vmx_vcpu_run */
  884. struct msr_autoload {
  885. struct vmx_msrs guest;
  886. struct vmx_msrs host;
  887. } msr_autoload;
  888. struct {
  889. int vm86_active;
  890. ulong save_rflags;
  891. struct kvm_segment segs[8];
  892. } rmode;
  893. struct {
  894. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  895. struct kvm_save_segment {
  896. u16 selector;
  897. unsigned long base;
  898. u32 limit;
  899. u32 ar;
  900. } seg[8];
  901. } segment_cache;
  902. int vpid;
  903. bool emulation_required;
  904. u32 exit_reason;
  905. /* Posted interrupt descriptor */
  906. struct pi_desc pi_desc;
  907. /* Support for a guest hypervisor (nested VMX) */
  908. struct nested_vmx nested;
  909. /* Dynamic PLE window. */
  910. int ple_window;
  911. bool ple_window_dirty;
  912. /* Support for PML */
  913. #define PML_ENTITY_NUM 512
  914. struct page *pml_pg;
  915. /* apic deadline value in host tsc */
  916. u64 hv_deadline_tsc;
  917. u64 current_tsc_ratio;
  918. u32 host_pkru;
  919. unsigned long host_debugctlmsr;
  920. /*
  921. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  922. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  923. * in msr_ia32_feature_control_valid_bits.
  924. */
  925. u64 msr_ia32_feature_control;
  926. u64 msr_ia32_feature_control_valid_bits;
  927. u64 ept_pointer;
  928. };
  929. enum segment_cache_field {
  930. SEG_FIELD_SEL = 0,
  931. SEG_FIELD_BASE = 1,
  932. SEG_FIELD_LIMIT = 2,
  933. SEG_FIELD_AR = 3,
  934. SEG_FIELD_NR = 4
  935. };
  936. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  937. {
  938. return container_of(kvm, struct kvm_vmx, kvm);
  939. }
  940. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  941. {
  942. return container_of(vcpu, struct vcpu_vmx, vcpu);
  943. }
  944. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  945. {
  946. return &(to_vmx(vcpu)->pi_desc);
  947. }
  948. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  949. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  950. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  951. #define FIELD64(number, name) \
  952. FIELD(number, name), \
  953. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  954. static u16 shadow_read_only_fields[] = {
  955. #define SHADOW_FIELD_RO(x) x,
  956. #include "vmx_shadow_fields.h"
  957. };
  958. static int max_shadow_read_only_fields =
  959. ARRAY_SIZE(shadow_read_only_fields);
  960. static u16 shadow_read_write_fields[] = {
  961. #define SHADOW_FIELD_RW(x) x,
  962. #include "vmx_shadow_fields.h"
  963. };
  964. static int max_shadow_read_write_fields =
  965. ARRAY_SIZE(shadow_read_write_fields);
  966. static const unsigned short vmcs_field_to_offset_table[] = {
  967. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  968. FIELD(POSTED_INTR_NV, posted_intr_nv),
  969. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  970. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  971. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  972. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  973. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  974. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  975. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  976. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  977. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  978. FIELD(GUEST_PML_INDEX, guest_pml_index),
  979. FIELD(HOST_ES_SELECTOR, host_es_selector),
  980. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  981. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  982. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  983. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  984. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  985. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  986. FIELD64(IO_BITMAP_A, io_bitmap_a),
  987. FIELD64(IO_BITMAP_B, io_bitmap_b),
  988. FIELD64(MSR_BITMAP, msr_bitmap),
  989. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  990. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  991. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  992. FIELD64(PML_ADDRESS, pml_address),
  993. FIELD64(TSC_OFFSET, tsc_offset),
  994. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  995. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  996. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  997. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  998. FIELD64(EPT_POINTER, ept_pointer),
  999. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  1000. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  1001. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  1002. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  1003. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  1004. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  1005. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  1006. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  1007. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  1008. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  1009. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  1010. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  1011. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  1012. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  1013. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  1014. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  1015. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  1016. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  1017. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  1018. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  1019. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  1020. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  1021. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  1022. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  1023. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  1024. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  1025. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  1026. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  1027. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  1028. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  1029. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  1030. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  1031. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  1032. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  1033. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  1034. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  1035. FIELD(TPR_THRESHOLD, tpr_threshold),
  1036. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  1037. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  1038. FIELD(VM_EXIT_REASON, vm_exit_reason),
  1039. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  1040. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  1041. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  1042. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  1043. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  1044. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  1045. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  1046. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  1047. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  1048. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  1049. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  1050. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  1051. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  1052. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  1053. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  1054. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  1055. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  1056. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  1057. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  1058. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  1059. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  1060. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  1061. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  1062. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  1063. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  1064. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  1065. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  1066. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  1067. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  1068. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  1069. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  1070. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  1071. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  1072. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  1073. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  1074. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  1075. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  1076. FIELD(EXIT_QUALIFICATION, exit_qualification),
  1077. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  1078. FIELD(GUEST_CR0, guest_cr0),
  1079. FIELD(GUEST_CR3, guest_cr3),
  1080. FIELD(GUEST_CR4, guest_cr4),
  1081. FIELD(GUEST_ES_BASE, guest_es_base),
  1082. FIELD(GUEST_CS_BASE, guest_cs_base),
  1083. FIELD(GUEST_SS_BASE, guest_ss_base),
  1084. FIELD(GUEST_DS_BASE, guest_ds_base),
  1085. FIELD(GUEST_FS_BASE, guest_fs_base),
  1086. FIELD(GUEST_GS_BASE, guest_gs_base),
  1087. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  1088. FIELD(GUEST_TR_BASE, guest_tr_base),
  1089. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  1090. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  1091. FIELD(GUEST_DR7, guest_dr7),
  1092. FIELD(GUEST_RSP, guest_rsp),
  1093. FIELD(GUEST_RIP, guest_rip),
  1094. FIELD(GUEST_RFLAGS, guest_rflags),
  1095. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  1096. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  1097. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  1098. FIELD(HOST_CR0, host_cr0),
  1099. FIELD(HOST_CR3, host_cr3),
  1100. FIELD(HOST_CR4, host_cr4),
  1101. FIELD(HOST_FS_BASE, host_fs_base),
  1102. FIELD(HOST_GS_BASE, host_gs_base),
  1103. FIELD(HOST_TR_BASE, host_tr_base),
  1104. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  1105. FIELD(HOST_IDTR_BASE, host_idtr_base),
  1106. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  1107. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  1108. FIELD(HOST_RSP, host_rsp),
  1109. FIELD(HOST_RIP, host_rip),
  1110. };
  1111. static inline short vmcs_field_to_offset(unsigned long field)
  1112. {
  1113. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  1114. unsigned short offset;
  1115. unsigned index;
  1116. if (field >> 15)
  1117. return -ENOENT;
  1118. index = ROL16(field, 6);
  1119. if (index >= size)
  1120. return -ENOENT;
  1121. index = array_index_nospec(index, size);
  1122. offset = vmcs_field_to_offset_table[index];
  1123. if (offset == 0)
  1124. return -ENOENT;
  1125. return offset;
  1126. }
  1127. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  1128. {
  1129. return to_vmx(vcpu)->nested.cached_vmcs12;
  1130. }
  1131. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  1132. {
  1133. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  1134. }
  1135. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  1136. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  1137. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  1138. static bool vmx_xsaves_supported(void);
  1139. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1140. struct kvm_segment *var, int seg);
  1141. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1142. struct kvm_segment *var, int seg);
  1143. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  1144. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  1145. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  1146. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  1147. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  1148. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  1149. u16 error_code);
  1150. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  1151. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  1152. u32 msr, int type);
  1153. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  1154. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  1155. /*
  1156. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  1157. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  1158. */
  1159. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1160. /*
  1161. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1162. * can find which vCPU should be waken up.
  1163. */
  1164. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1165. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1166. enum {
  1167. VMX_VMREAD_BITMAP,
  1168. VMX_VMWRITE_BITMAP,
  1169. VMX_BITMAP_NR
  1170. };
  1171. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1172. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1173. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1174. static bool cpu_has_load_ia32_efer;
  1175. static bool cpu_has_load_perf_global_ctrl;
  1176. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1177. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1178. static struct vmcs_config {
  1179. int size;
  1180. int order;
  1181. u32 basic_cap;
  1182. u32 revision_id;
  1183. u32 pin_based_exec_ctrl;
  1184. u32 cpu_based_exec_ctrl;
  1185. u32 cpu_based_2nd_exec_ctrl;
  1186. u32 vmexit_ctrl;
  1187. u32 vmentry_ctrl;
  1188. struct nested_vmx_msrs nested;
  1189. } vmcs_config;
  1190. static struct vmx_capability {
  1191. u32 ept;
  1192. u32 vpid;
  1193. } vmx_capability;
  1194. #define VMX_SEGMENT_FIELD(seg) \
  1195. [VCPU_SREG_##seg] = { \
  1196. .selector = GUEST_##seg##_SELECTOR, \
  1197. .base = GUEST_##seg##_BASE, \
  1198. .limit = GUEST_##seg##_LIMIT, \
  1199. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1200. }
  1201. static const struct kvm_vmx_segment_field {
  1202. unsigned selector;
  1203. unsigned base;
  1204. unsigned limit;
  1205. unsigned ar_bytes;
  1206. } kvm_vmx_segment_fields[] = {
  1207. VMX_SEGMENT_FIELD(CS),
  1208. VMX_SEGMENT_FIELD(DS),
  1209. VMX_SEGMENT_FIELD(ES),
  1210. VMX_SEGMENT_FIELD(FS),
  1211. VMX_SEGMENT_FIELD(GS),
  1212. VMX_SEGMENT_FIELD(SS),
  1213. VMX_SEGMENT_FIELD(TR),
  1214. VMX_SEGMENT_FIELD(LDTR),
  1215. };
  1216. static u64 host_efer;
  1217. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1218. /*
  1219. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1220. * away by decrementing the array size.
  1221. */
  1222. static const u32 vmx_msr_index[] = {
  1223. #ifdef CONFIG_X86_64
  1224. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1225. #endif
  1226. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1227. };
  1228. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1229. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1230. #define KVM_EVMCS_VERSION 1
  1231. #if IS_ENABLED(CONFIG_HYPERV)
  1232. static bool __read_mostly enlightened_vmcs = true;
  1233. module_param(enlightened_vmcs, bool, 0444);
  1234. static inline void evmcs_write64(unsigned long field, u64 value)
  1235. {
  1236. u16 clean_field;
  1237. int offset = get_evmcs_offset(field, &clean_field);
  1238. if (offset < 0)
  1239. return;
  1240. *(u64 *)((char *)current_evmcs + offset) = value;
  1241. current_evmcs->hv_clean_fields &= ~clean_field;
  1242. }
  1243. static inline void evmcs_write32(unsigned long field, u32 value)
  1244. {
  1245. u16 clean_field;
  1246. int offset = get_evmcs_offset(field, &clean_field);
  1247. if (offset < 0)
  1248. return;
  1249. *(u32 *)((char *)current_evmcs + offset) = value;
  1250. current_evmcs->hv_clean_fields &= ~clean_field;
  1251. }
  1252. static inline void evmcs_write16(unsigned long field, u16 value)
  1253. {
  1254. u16 clean_field;
  1255. int offset = get_evmcs_offset(field, &clean_field);
  1256. if (offset < 0)
  1257. return;
  1258. *(u16 *)((char *)current_evmcs + offset) = value;
  1259. current_evmcs->hv_clean_fields &= ~clean_field;
  1260. }
  1261. static inline u64 evmcs_read64(unsigned long field)
  1262. {
  1263. int offset = get_evmcs_offset(field, NULL);
  1264. if (offset < 0)
  1265. return 0;
  1266. return *(u64 *)((char *)current_evmcs + offset);
  1267. }
  1268. static inline u32 evmcs_read32(unsigned long field)
  1269. {
  1270. int offset = get_evmcs_offset(field, NULL);
  1271. if (offset < 0)
  1272. return 0;
  1273. return *(u32 *)((char *)current_evmcs + offset);
  1274. }
  1275. static inline u16 evmcs_read16(unsigned long field)
  1276. {
  1277. int offset = get_evmcs_offset(field, NULL);
  1278. if (offset < 0)
  1279. return 0;
  1280. return *(u16 *)((char *)current_evmcs + offset);
  1281. }
  1282. static inline void evmcs_touch_msr_bitmap(void)
  1283. {
  1284. if (unlikely(!current_evmcs))
  1285. return;
  1286. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1287. current_evmcs->hv_clean_fields &=
  1288. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1289. }
  1290. static void evmcs_load(u64 phys_addr)
  1291. {
  1292. struct hv_vp_assist_page *vp_ap =
  1293. hv_get_vp_assist_page(smp_processor_id());
  1294. vp_ap->current_nested_vmcs = phys_addr;
  1295. vp_ap->enlighten_vmentry = 1;
  1296. }
  1297. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1298. {
  1299. /*
  1300. * Enlightened VMCSv1 doesn't support these:
  1301. *
  1302. * POSTED_INTR_NV = 0x00000002,
  1303. * GUEST_INTR_STATUS = 0x00000810,
  1304. * APIC_ACCESS_ADDR = 0x00002014,
  1305. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1306. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1307. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1308. * EOI_EXIT_BITMAP2 = 0x00002020,
  1309. * EOI_EXIT_BITMAP3 = 0x00002022,
  1310. */
  1311. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  1312. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1313. ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1314. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1315. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1316. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1317. ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1318. /*
  1319. * GUEST_PML_INDEX = 0x00000812,
  1320. * PML_ADDRESS = 0x0000200e,
  1321. */
  1322. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
  1323. /* VM_FUNCTION_CONTROL = 0x00002018, */
  1324. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
  1325. /*
  1326. * EPTP_LIST_ADDRESS = 0x00002024,
  1327. * VMREAD_BITMAP = 0x00002026,
  1328. * VMWRITE_BITMAP = 0x00002028,
  1329. */
  1330. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
  1331. /*
  1332. * TSC_MULTIPLIER = 0x00002032,
  1333. */
  1334. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
  1335. /*
  1336. * PLE_GAP = 0x00004020,
  1337. * PLE_WINDOW = 0x00004022,
  1338. */
  1339. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1340. /*
  1341. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1342. */
  1343. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  1344. /*
  1345. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1346. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1347. */
  1348. vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
  1349. vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  1350. /*
  1351. * Currently unsupported in KVM:
  1352. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1353. */
  1354. }
  1355. /* check_ept_pointer() should be under protection of ept_pointer_lock. */
  1356. static void check_ept_pointer_match(struct kvm *kvm)
  1357. {
  1358. struct kvm_vcpu *vcpu;
  1359. u64 tmp_eptp = INVALID_PAGE;
  1360. int i;
  1361. kvm_for_each_vcpu(i, vcpu, kvm) {
  1362. if (!VALID_PAGE(tmp_eptp)) {
  1363. tmp_eptp = to_vmx(vcpu)->ept_pointer;
  1364. } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
  1365. to_kvm_vmx(kvm)->ept_pointers_match
  1366. = EPT_POINTERS_MISMATCH;
  1367. return;
  1368. }
  1369. }
  1370. to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
  1371. }
  1372. static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
  1373. {
  1374. int ret;
  1375. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1376. if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
  1377. check_ept_pointer_match(kvm);
  1378. if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
  1379. ret = -ENOTSUPP;
  1380. goto out;
  1381. }
  1382. ret = hyperv_flush_guest_mapping(
  1383. to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
  1384. out:
  1385. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1386. return ret;
  1387. }
  1388. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1389. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1390. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1391. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1392. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1393. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1394. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1395. static inline void evmcs_load(u64 phys_addr) {}
  1396. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1397. static inline void evmcs_touch_msr_bitmap(void) {}
  1398. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1399. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1400. {
  1401. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1402. INTR_INFO_VALID_MASK)) ==
  1403. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1404. }
  1405. static inline bool is_debug(u32 intr_info)
  1406. {
  1407. return is_exception_n(intr_info, DB_VECTOR);
  1408. }
  1409. static inline bool is_breakpoint(u32 intr_info)
  1410. {
  1411. return is_exception_n(intr_info, BP_VECTOR);
  1412. }
  1413. static inline bool is_page_fault(u32 intr_info)
  1414. {
  1415. return is_exception_n(intr_info, PF_VECTOR);
  1416. }
  1417. static inline bool is_no_device(u32 intr_info)
  1418. {
  1419. return is_exception_n(intr_info, NM_VECTOR);
  1420. }
  1421. static inline bool is_invalid_opcode(u32 intr_info)
  1422. {
  1423. return is_exception_n(intr_info, UD_VECTOR);
  1424. }
  1425. static inline bool is_gp_fault(u32 intr_info)
  1426. {
  1427. return is_exception_n(intr_info, GP_VECTOR);
  1428. }
  1429. static inline bool is_external_interrupt(u32 intr_info)
  1430. {
  1431. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1432. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  1433. }
  1434. static inline bool is_machine_check(u32 intr_info)
  1435. {
  1436. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1437. INTR_INFO_VALID_MASK)) ==
  1438. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1439. }
  1440. /* Undocumented: icebp/int1 */
  1441. static inline bool is_icebp(u32 intr_info)
  1442. {
  1443. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1444. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1445. }
  1446. static inline bool cpu_has_vmx_msr_bitmap(void)
  1447. {
  1448. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1449. }
  1450. static inline bool cpu_has_vmx_tpr_shadow(void)
  1451. {
  1452. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1453. }
  1454. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1455. {
  1456. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1457. }
  1458. static inline bool cpu_has_secondary_exec_ctrls(void)
  1459. {
  1460. return vmcs_config.cpu_based_exec_ctrl &
  1461. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1462. }
  1463. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1464. {
  1465. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1466. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1467. }
  1468. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1469. {
  1470. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1471. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1472. }
  1473. static inline bool cpu_has_vmx_apic_register_virt(void)
  1474. {
  1475. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1476. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1477. }
  1478. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1479. {
  1480. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1481. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1482. }
  1483. static inline bool cpu_has_vmx_encls_vmexit(void)
  1484. {
  1485. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1486. SECONDARY_EXEC_ENCLS_EXITING;
  1487. }
  1488. /*
  1489. * Comment's format: document - errata name - stepping - processor name.
  1490. * Refer from
  1491. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1492. */
  1493. static u32 vmx_preemption_cpu_tfms[] = {
  1494. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1495. 0x000206E6,
  1496. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1497. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1498. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1499. 0x00020652,
  1500. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1501. 0x00020655,
  1502. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1503. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1504. /*
  1505. * 320767.pdf - AAP86 - B1 -
  1506. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1507. */
  1508. 0x000106E5,
  1509. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1510. 0x000106A0,
  1511. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1512. 0x000106A1,
  1513. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1514. 0x000106A4,
  1515. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1516. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1517. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1518. 0x000106A5,
  1519. };
  1520. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1521. {
  1522. u32 eax = cpuid_eax(0x00000001), i;
  1523. /* Clear the reserved bits */
  1524. eax &= ~(0x3U << 14 | 0xfU << 28);
  1525. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1526. if (eax == vmx_preemption_cpu_tfms[i])
  1527. return true;
  1528. return false;
  1529. }
  1530. static inline bool cpu_has_vmx_preemption_timer(void)
  1531. {
  1532. return vmcs_config.pin_based_exec_ctrl &
  1533. PIN_BASED_VMX_PREEMPTION_TIMER;
  1534. }
  1535. static inline bool cpu_has_vmx_posted_intr(void)
  1536. {
  1537. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1538. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1539. }
  1540. static inline bool cpu_has_vmx_apicv(void)
  1541. {
  1542. return cpu_has_vmx_apic_register_virt() &&
  1543. cpu_has_vmx_virtual_intr_delivery() &&
  1544. cpu_has_vmx_posted_intr();
  1545. }
  1546. static inline bool cpu_has_vmx_flexpriority(void)
  1547. {
  1548. return cpu_has_vmx_tpr_shadow() &&
  1549. cpu_has_vmx_virtualize_apic_accesses();
  1550. }
  1551. static inline bool cpu_has_vmx_ept_execute_only(void)
  1552. {
  1553. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1554. }
  1555. static inline bool cpu_has_vmx_ept_2m_page(void)
  1556. {
  1557. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1558. }
  1559. static inline bool cpu_has_vmx_ept_1g_page(void)
  1560. {
  1561. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1562. }
  1563. static inline bool cpu_has_vmx_ept_4levels(void)
  1564. {
  1565. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1566. }
  1567. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1568. {
  1569. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1570. }
  1571. static inline bool cpu_has_vmx_ept_5levels(void)
  1572. {
  1573. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1574. }
  1575. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1576. {
  1577. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1578. }
  1579. static inline bool cpu_has_vmx_invept_context(void)
  1580. {
  1581. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1582. }
  1583. static inline bool cpu_has_vmx_invept_global(void)
  1584. {
  1585. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1586. }
  1587. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1588. {
  1589. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1590. }
  1591. static inline bool cpu_has_vmx_invvpid_single(void)
  1592. {
  1593. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1594. }
  1595. static inline bool cpu_has_vmx_invvpid_global(void)
  1596. {
  1597. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1598. }
  1599. static inline bool cpu_has_vmx_invvpid(void)
  1600. {
  1601. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1602. }
  1603. static inline bool cpu_has_vmx_ept(void)
  1604. {
  1605. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1606. SECONDARY_EXEC_ENABLE_EPT;
  1607. }
  1608. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1609. {
  1610. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1611. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1612. }
  1613. static inline bool cpu_has_vmx_ple(void)
  1614. {
  1615. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1616. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1617. }
  1618. static inline bool cpu_has_vmx_basic_inout(void)
  1619. {
  1620. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1621. }
  1622. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1623. {
  1624. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1625. }
  1626. static inline bool cpu_has_vmx_vpid(void)
  1627. {
  1628. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1629. SECONDARY_EXEC_ENABLE_VPID;
  1630. }
  1631. static inline bool cpu_has_vmx_rdtscp(void)
  1632. {
  1633. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1634. SECONDARY_EXEC_RDTSCP;
  1635. }
  1636. static inline bool cpu_has_vmx_invpcid(void)
  1637. {
  1638. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1639. SECONDARY_EXEC_ENABLE_INVPCID;
  1640. }
  1641. static inline bool cpu_has_virtual_nmis(void)
  1642. {
  1643. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1644. }
  1645. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1646. {
  1647. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1648. SECONDARY_EXEC_WBINVD_EXITING;
  1649. }
  1650. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1651. {
  1652. u64 vmx_msr;
  1653. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1654. /* check if the cpu supports writing r/o exit information fields */
  1655. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1656. return false;
  1657. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1658. SECONDARY_EXEC_SHADOW_VMCS;
  1659. }
  1660. static inline bool cpu_has_vmx_pml(void)
  1661. {
  1662. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1663. }
  1664. static inline bool cpu_has_vmx_tsc_scaling(void)
  1665. {
  1666. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1667. SECONDARY_EXEC_TSC_SCALING;
  1668. }
  1669. static inline bool cpu_has_vmx_vmfunc(void)
  1670. {
  1671. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1672. SECONDARY_EXEC_ENABLE_VMFUNC;
  1673. }
  1674. static bool vmx_umip_emulated(void)
  1675. {
  1676. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1677. SECONDARY_EXEC_DESC;
  1678. }
  1679. static inline bool report_flexpriority(void)
  1680. {
  1681. return flexpriority_enabled;
  1682. }
  1683. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1684. {
  1685. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1686. }
  1687. /*
  1688. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1689. * to modify any valid field of the VMCS, or are the VM-exit
  1690. * information fields read-only?
  1691. */
  1692. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1693. {
  1694. return to_vmx(vcpu)->nested.msrs.misc_low &
  1695. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1696. }
  1697. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1698. {
  1699. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1700. }
  1701. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1702. {
  1703. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1704. CPU_BASED_MONITOR_TRAP_FLAG;
  1705. }
  1706. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  1707. {
  1708. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  1709. SECONDARY_EXEC_SHADOW_VMCS;
  1710. }
  1711. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1712. {
  1713. return vmcs12->cpu_based_vm_exec_control & bit;
  1714. }
  1715. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1716. {
  1717. return (vmcs12->cpu_based_vm_exec_control &
  1718. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1719. (vmcs12->secondary_vm_exec_control & bit);
  1720. }
  1721. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1722. {
  1723. return vmcs12->pin_based_vm_exec_control &
  1724. PIN_BASED_VMX_PREEMPTION_TIMER;
  1725. }
  1726. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1727. {
  1728. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1729. }
  1730. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1731. {
  1732. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1733. }
  1734. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1735. {
  1736. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1737. }
  1738. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1739. {
  1740. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1741. }
  1742. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1743. {
  1744. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1745. }
  1746. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1747. {
  1748. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1749. }
  1750. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1751. {
  1752. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1753. }
  1754. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1755. {
  1756. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1757. }
  1758. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1759. {
  1760. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1761. }
  1762. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1763. {
  1764. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1765. }
  1766. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1767. {
  1768. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1769. }
  1770. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1771. {
  1772. return nested_cpu_has_vmfunc(vmcs12) &&
  1773. (vmcs12->vm_function_control &
  1774. VMX_VMFUNC_EPTP_SWITCHING);
  1775. }
  1776. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  1777. {
  1778. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  1779. }
  1780. static inline bool is_nmi(u32 intr_info)
  1781. {
  1782. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1783. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1784. }
  1785. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1786. u32 exit_intr_info,
  1787. unsigned long exit_qualification);
  1788. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1789. struct vmcs12 *vmcs12,
  1790. u32 reason, unsigned long qualification);
  1791. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1792. {
  1793. int i;
  1794. for (i = 0; i < vmx->nmsrs; ++i)
  1795. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1796. return i;
  1797. return -1;
  1798. }
  1799. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1800. {
  1801. struct {
  1802. u64 vpid : 16;
  1803. u64 rsvd : 48;
  1804. u64 gva;
  1805. } operand = { vpid, 0, gva };
  1806. bool error;
  1807. asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
  1808. : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
  1809. : "memory");
  1810. BUG_ON(error);
  1811. }
  1812. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1813. {
  1814. struct {
  1815. u64 eptp, gpa;
  1816. } operand = {eptp, gpa};
  1817. bool error;
  1818. asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
  1819. : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
  1820. : "memory");
  1821. BUG_ON(error);
  1822. }
  1823. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1824. {
  1825. int i;
  1826. i = __find_msr_index(vmx, msr);
  1827. if (i >= 0)
  1828. return &vmx->guest_msrs[i];
  1829. return NULL;
  1830. }
  1831. static void vmcs_clear(struct vmcs *vmcs)
  1832. {
  1833. u64 phys_addr = __pa(vmcs);
  1834. bool error;
  1835. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
  1836. : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
  1837. : "memory");
  1838. if (unlikely(error))
  1839. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1840. vmcs, phys_addr);
  1841. }
  1842. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1843. {
  1844. vmcs_clear(loaded_vmcs->vmcs);
  1845. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1846. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1847. loaded_vmcs->cpu = -1;
  1848. loaded_vmcs->launched = 0;
  1849. }
  1850. static void vmcs_load(struct vmcs *vmcs)
  1851. {
  1852. u64 phys_addr = __pa(vmcs);
  1853. bool error;
  1854. if (static_branch_unlikely(&enable_evmcs))
  1855. return evmcs_load(phys_addr);
  1856. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
  1857. : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
  1858. : "memory");
  1859. if (unlikely(error))
  1860. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1861. vmcs, phys_addr);
  1862. }
  1863. #ifdef CONFIG_KEXEC_CORE
  1864. /*
  1865. * This bitmap is used to indicate whether the vmclear
  1866. * operation is enabled on all cpus. All disabled by
  1867. * default.
  1868. */
  1869. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1870. static inline void crash_enable_local_vmclear(int cpu)
  1871. {
  1872. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1873. }
  1874. static inline void crash_disable_local_vmclear(int cpu)
  1875. {
  1876. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1877. }
  1878. static inline int crash_local_vmclear_enabled(int cpu)
  1879. {
  1880. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1881. }
  1882. static void crash_vmclear_local_loaded_vmcss(void)
  1883. {
  1884. int cpu = raw_smp_processor_id();
  1885. struct loaded_vmcs *v;
  1886. if (!crash_local_vmclear_enabled(cpu))
  1887. return;
  1888. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1889. loaded_vmcss_on_cpu_link)
  1890. vmcs_clear(v->vmcs);
  1891. }
  1892. #else
  1893. static inline void crash_enable_local_vmclear(int cpu) { }
  1894. static inline void crash_disable_local_vmclear(int cpu) { }
  1895. #endif /* CONFIG_KEXEC_CORE */
  1896. static void __loaded_vmcs_clear(void *arg)
  1897. {
  1898. struct loaded_vmcs *loaded_vmcs = arg;
  1899. int cpu = raw_smp_processor_id();
  1900. if (loaded_vmcs->cpu != cpu)
  1901. return; /* vcpu migration can race with cpu offline */
  1902. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1903. per_cpu(current_vmcs, cpu) = NULL;
  1904. crash_disable_local_vmclear(cpu);
  1905. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1906. /*
  1907. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1908. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1909. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1910. * then adds the vmcs into percpu list before it is deleted.
  1911. */
  1912. smp_wmb();
  1913. loaded_vmcs_init(loaded_vmcs);
  1914. crash_enable_local_vmclear(cpu);
  1915. }
  1916. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1917. {
  1918. int cpu = loaded_vmcs->cpu;
  1919. if (cpu != -1)
  1920. smp_call_function_single(cpu,
  1921. __loaded_vmcs_clear, loaded_vmcs, 1);
  1922. }
  1923. static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
  1924. {
  1925. if (vpid == 0)
  1926. return true;
  1927. if (cpu_has_vmx_invvpid_individual_addr()) {
  1928. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
  1929. return true;
  1930. }
  1931. return false;
  1932. }
  1933. static inline void vpid_sync_vcpu_single(int vpid)
  1934. {
  1935. if (vpid == 0)
  1936. return;
  1937. if (cpu_has_vmx_invvpid_single())
  1938. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1939. }
  1940. static inline void vpid_sync_vcpu_global(void)
  1941. {
  1942. if (cpu_has_vmx_invvpid_global())
  1943. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1944. }
  1945. static inline void vpid_sync_context(int vpid)
  1946. {
  1947. if (cpu_has_vmx_invvpid_single())
  1948. vpid_sync_vcpu_single(vpid);
  1949. else
  1950. vpid_sync_vcpu_global();
  1951. }
  1952. static inline void ept_sync_global(void)
  1953. {
  1954. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1955. }
  1956. static inline void ept_sync_context(u64 eptp)
  1957. {
  1958. if (cpu_has_vmx_invept_context())
  1959. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1960. else
  1961. ept_sync_global();
  1962. }
  1963. static __always_inline void vmcs_check16(unsigned long field)
  1964. {
  1965. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1966. "16-bit accessor invalid for 64-bit field");
  1967. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1968. "16-bit accessor invalid for 64-bit high field");
  1969. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1970. "16-bit accessor invalid for 32-bit high field");
  1971. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1972. "16-bit accessor invalid for natural width field");
  1973. }
  1974. static __always_inline void vmcs_check32(unsigned long field)
  1975. {
  1976. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1977. "32-bit accessor invalid for 16-bit field");
  1978. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1979. "32-bit accessor invalid for natural width field");
  1980. }
  1981. static __always_inline void vmcs_check64(unsigned long field)
  1982. {
  1983. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1984. "64-bit accessor invalid for 16-bit field");
  1985. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1986. "64-bit accessor invalid for 64-bit high field");
  1987. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1988. "64-bit accessor invalid for 32-bit field");
  1989. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1990. "64-bit accessor invalid for natural width field");
  1991. }
  1992. static __always_inline void vmcs_checkl(unsigned long field)
  1993. {
  1994. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1995. "Natural width accessor invalid for 16-bit field");
  1996. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1997. "Natural width accessor invalid for 64-bit field");
  1998. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1999. "Natural width accessor invalid for 64-bit high field");
  2000. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  2001. "Natural width accessor invalid for 32-bit field");
  2002. }
  2003. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  2004. {
  2005. unsigned long value;
  2006. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  2007. : "=a"(value) : "d"(field) : "cc");
  2008. return value;
  2009. }
  2010. static __always_inline u16 vmcs_read16(unsigned long field)
  2011. {
  2012. vmcs_check16(field);
  2013. if (static_branch_unlikely(&enable_evmcs))
  2014. return evmcs_read16(field);
  2015. return __vmcs_readl(field);
  2016. }
  2017. static __always_inline u32 vmcs_read32(unsigned long field)
  2018. {
  2019. vmcs_check32(field);
  2020. if (static_branch_unlikely(&enable_evmcs))
  2021. return evmcs_read32(field);
  2022. return __vmcs_readl(field);
  2023. }
  2024. static __always_inline u64 vmcs_read64(unsigned long field)
  2025. {
  2026. vmcs_check64(field);
  2027. if (static_branch_unlikely(&enable_evmcs))
  2028. return evmcs_read64(field);
  2029. #ifdef CONFIG_X86_64
  2030. return __vmcs_readl(field);
  2031. #else
  2032. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  2033. #endif
  2034. }
  2035. static __always_inline unsigned long vmcs_readl(unsigned long field)
  2036. {
  2037. vmcs_checkl(field);
  2038. if (static_branch_unlikely(&enable_evmcs))
  2039. return evmcs_read64(field);
  2040. return __vmcs_readl(field);
  2041. }
  2042. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  2043. {
  2044. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  2045. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  2046. dump_stack();
  2047. }
  2048. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  2049. {
  2050. bool error;
  2051. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
  2052. : CC_OUT(na) (error) : "a"(value), "d"(field));
  2053. if (unlikely(error))
  2054. vmwrite_error(field, value);
  2055. }
  2056. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  2057. {
  2058. vmcs_check16(field);
  2059. if (static_branch_unlikely(&enable_evmcs))
  2060. return evmcs_write16(field, value);
  2061. __vmcs_writel(field, value);
  2062. }
  2063. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  2064. {
  2065. vmcs_check32(field);
  2066. if (static_branch_unlikely(&enable_evmcs))
  2067. return evmcs_write32(field, value);
  2068. __vmcs_writel(field, value);
  2069. }
  2070. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  2071. {
  2072. vmcs_check64(field);
  2073. if (static_branch_unlikely(&enable_evmcs))
  2074. return evmcs_write64(field, value);
  2075. __vmcs_writel(field, value);
  2076. #ifndef CONFIG_X86_64
  2077. asm volatile ("");
  2078. __vmcs_writel(field+1, value >> 32);
  2079. #endif
  2080. }
  2081. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  2082. {
  2083. vmcs_checkl(field);
  2084. if (static_branch_unlikely(&enable_evmcs))
  2085. return evmcs_write64(field, value);
  2086. __vmcs_writel(field, value);
  2087. }
  2088. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  2089. {
  2090. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2091. "vmcs_clear_bits does not support 64-bit fields");
  2092. if (static_branch_unlikely(&enable_evmcs))
  2093. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  2094. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  2095. }
  2096. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  2097. {
  2098. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2099. "vmcs_set_bits does not support 64-bit fields");
  2100. if (static_branch_unlikely(&enable_evmcs))
  2101. return evmcs_write32(field, evmcs_read32(field) | mask);
  2102. __vmcs_writel(field, __vmcs_readl(field) | mask);
  2103. }
  2104. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  2105. {
  2106. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  2107. }
  2108. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  2109. {
  2110. vmcs_write32(VM_ENTRY_CONTROLS, val);
  2111. vmx->vm_entry_controls_shadow = val;
  2112. }
  2113. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  2114. {
  2115. if (vmx->vm_entry_controls_shadow != val)
  2116. vm_entry_controls_init(vmx, val);
  2117. }
  2118. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  2119. {
  2120. return vmx->vm_entry_controls_shadow;
  2121. }
  2122. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2123. {
  2124. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  2125. }
  2126. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2127. {
  2128. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  2129. }
  2130. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  2131. {
  2132. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  2133. }
  2134. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  2135. {
  2136. vmcs_write32(VM_EXIT_CONTROLS, val);
  2137. vmx->vm_exit_controls_shadow = val;
  2138. }
  2139. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  2140. {
  2141. if (vmx->vm_exit_controls_shadow != val)
  2142. vm_exit_controls_init(vmx, val);
  2143. }
  2144. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  2145. {
  2146. return vmx->vm_exit_controls_shadow;
  2147. }
  2148. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2149. {
  2150. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  2151. }
  2152. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2153. {
  2154. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  2155. }
  2156. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  2157. {
  2158. vmx->segment_cache.bitmask = 0;
  2159. }
  2160. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  2161. unsigned field)
  2162. {
  2163. bool ret;
  2164. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  2165. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  2166. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  2167. vmx->segment_cache.bitmask = 0;
  2168. }
  2169. ret = vmx->segment_cache.bitmask & mask;
  2170. vmx->segment_cache.bitmask |= mask;
  2171. return ret;
  2172. }
  2173. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  2174. {
  2175. u16 *p = &vmx->segment_cache.seg[seg].selector;
  2176. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  2177. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  2178. return *p;
  2179. }
  2180. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  2181. {
  2182. ulong *p = &vmx->segment_cache.seg[seg].base;
  2183. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  2184. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  2185. return *p;
  2186. }
  2187. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  2188. {
  2189. u32 *p = &vmx->segment_cache.seg[seg].limit;
  2190. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  2191. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  2192. return *p;
  2193. }
  2194. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  2195. {
  2196. u32 *p = &vmx->segment_cache.seg[seg].ar;
  2197. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  2198. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  2199. return *p;
  2200. }
  2201. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  2202. {
  2203. u32 eb;
  2204. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  2205. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  2206. /*
  2207. * Guest access to VMware backdoor ports could legitimately
  2208. * trigger #GP because of TSS I/O permission bitmap.
  2209. * We intercept those #GP and allow access to them anyway
  2210. * as VMware does.
  2211. */
  2212. if (enable_vmware_backdoor)
  2213. eb |= (1u << GP_VECTOR);
  2214. if ((vcpu->guest_debug &
  2215. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  2216. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  2217. eb |= 1u << BP_VECTOR;
  2218. if (to_vmx(vcpu)->rmode.vm86_active)
  2219. eb = ~0;
  2220. if (enable_ept)
  2221. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2222. /* When we are running a nested L2 guest and L1 specified for it a
  2223. * certain exception bitmap, we must trap the same exceptions and pass
  2224. * them to L1. When running L2, we will only handle the exceptions
  2225. * specified above if L1 did not want them.
  2226. */
  2227. if (is_guest_mode(vcpu))
  2228. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2229. vmcs_write32(EXCEPTION_BITMAP, eb);
  2230. }
  2231. /*
  2232. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2233. */
  2234. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2235. {
  2236. unsigned long *msr_bitmap;
  2237. int f = sizeof(unsigned long);
  2238. if (!cpu_has_vmx_msr_bitmap())
  2239. return true;
  2240. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2241. if (msr <= 0x1fff) {
  2242. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2243. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2244. msr &= 0x1fff;
  2245. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2246. }
  2247. return true;
  2248. }
  2249. /*
  2250. * Check if MSR is intercepted for L01 MSR bitmap.
  2251. */
  2252. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2253. {
  2254. unsigned long *msr_bitmap;
  2255. int f = sizeof(unsigned long);
  2256. if (!cpu_has_vmx_msr_bitmap())
  2257. return true;
  2258. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2259. if (msr <= 0x1fff) {
  2260. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2261. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2262. msr &= 0x1fff;
  2263. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2264. }
  2265. return true;
  2266. }
  2267. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2268. unsigned long entry, unsigned long exit)
  2269. {
  2270. vm_entry_controls_clearbit(vmx, entry);
  2271. vm_exit_controls_clearbit(vmx, exit);
  2272. }
  2273. static int find_msr(struct vmx_msrs *m, unsigned int msr)
  2274. {
  2275. unsigned int i;
  2276. for (i = 0; i < m->nr; ++i) {
  2277. if (m->val[i].index == msr)
  2278. return i;
  2279. }
  2280. return -ENOENT;
  2281. }
  2282. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2283. {
  2284. int i;
  2285. struct msr_autoload *m = &vmx->msr_autoload;
  2286. switch (msr) {
  2287. case MSR_EFER:
  2288. if (cpu_has_load_ia32_efer) {
  2289. clear_atomic_switch_msr_special(vmx,
  2290. VM_ENTRY_LOAD_IA32_EFER,
  2291. VM_EXIT_LOAD_IA32_EFER);
  2292. return;
  2293. }
  2294. break;
  2295. case MSR_CORE_PERF_GLOBAL_CTRL:
  2296. if (cpu_has_load_perf_global_ctrl) {
  2297. clear_atomic_switch_msr_special(vmx,
  2298. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2299. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2300. return;
  2301. }
  2302. break;
  2303. }
  2304. i = find_msr(&m->guest, msr);
  2305. if (i < 0)
  2306. goto skip_guest;
  2307. --m->guest.nr;
  2308. m->guest.val[i] = m->guest.val[m->guest.nr];
  2309. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2310. skip_guest:
  2311. i = find_msr(&m->host, msr);
  2312. if (i < 0)
  2313. return;
  2314. --m->host.nr;
  2315. m->host.val[i] = m->host.val[m->host.nr];
  2316. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2317. }
  2318. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2319. unsigned long entry, unsigned long exit,
  2320. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2321. u64 guest_val, u64 host_val)
  2322. {
  2323. vmcs_write64(guest_val_vmcs, guest_val);
  2324. vmcs_write64(host_val_vmcs, host_val);
  2325. vm_entry_controls_setbit(vmx, entry);
  2326. vm_exit_controls_setbit(vmx, exit);
  2327. }
  2328. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2329. u64 guest_val, u64 host_val, bool entry_only)
  2330. {
  2331. int i, j = 0;
  2332. struct msr_autoload *m = &vmx->msr_autoload;
  2333. switch (msr) {
  2334. case MSR_EFER:
  2335. if (cpu_has_load_ia32_efer) {
  2336. add_atomic_switch_msr_special(vmx,
  2337. VM_ENTRY_LOAD_IA32_EFER,
  2338. VM_EXIT_LOAD_IA32_EFER,
  2339. GUEST_IA32_EFER,
  2340. HOST_IA32_EFER,
  2341. guest_val, host_val);
  2342. return;
  2343. }
  2344. break;
  2345. case MSR_CORE_PERF_GLOBAL_CTRL:
  2346. if (cpu_has_load_perf_global_ctrl) {
  2347. add_atomic_switch_msr_special(vmx,
  2348. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2349. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2350. GUEST_IA32_PERF_GLOBAL_CTRL,
  2351. HOST_IA32_PERF_GLOBAL_CTRL,
  2352. guest_val, host_val);
  2353. return;
  2354. }
  2355. break;
  2356. case MSR_IA32_PEBS_ENABLE:
  2357. /* PEBS needs a quiescent period after being disabled (to write
  2358. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2359. * provide that period, so a CPU could write host's record into
  2360. * guest's memory.
  2361. */
  2362. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2363. }
  2364. i = find_msr(&m->guest, msr);
  2365. if (!entry_only)
  2366. j = find_msr(&m->host, msr);
  2367. if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
  2368. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2369. "Can't add msr %x\n", msr);
  2370. return;
  2371. }
  2372. if (i < 0) {
  2373. i = m->guest.nr++;
  2374. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2375. }
  2376. m->guest.val[i].index = msr;
  2377. m->guest.val[i].value = guest_val;
  2378. if (entry_only)
  2379. return;
  2380. if (j < 0) {
  2381. j = m->host.nr++;
  2382. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2383. }
  2384. m->host.val[j].index = msr;
  2385. m->host.val[j].value = host_val;
  2386. }
  2387. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2388. {
  2389. u64 guest_efer = vmx->vcpu.arch.efer;
  2390. u64 ignore_bits = 0;
  2391. if (!enable_ept) {
  2392. /*
  2393. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  2394. * host CPUID is more efficient than testing guest CPUID
  2395. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  2396. */
  2397. if (boot_cpu_has(X86_FEATURE_SMEP))
  2398. guest_efer |= EFER_NX;
  2399. else if (!(guest_efer & EFER_NX))
  2400. ignore_bits |= EFER_NX;
  2401. }
  2402. /*
  2403. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2404. */
  2405. ignore_bits |= EFER_SCE;
  2406. #ifdef CONFIG_X86_64
  2407. ignore_bits |= EFER_LMA | EFER_LME;
  2408. /* SCE is meaningful only in long mode on Intel */
  2409. if (guest_efer & EFER_LMA)
  2410. ignore_bits &= ~(u64)EFER_SCE;
  2411. #endif
  2412. clear_atomic_switch_msr(vmx, MSR_EFER);
  2413. /*
  2414. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2415. * On CPUs that support "load IA32_EFER", always switch EFER
  2416. * atomically, since it's faster than switching it manually.
  2417. */
  2418. if (cpu_has_load_ia32_efer ||
  2419. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2420. if (!(guest_efer & EFER_LMA))
  2421. guest_efer &= ~EFER_LME;
  2422. if (guest_efer != host_efer)
  2423. add_atomic_switch_msr(vmx, MSR_EFER,
  2424. guest_efer, host_efer, false);
  2425. return false;
  2426. } else {
  2427. guest_efer &= ~ignore_bits;
  2428. guest_efer |= host_efer & ignore_bits;
  2429. vmx->guest_msrs[efer_offset].data = guest_efer;
  2430. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2431. return true;
  2432. }
  2433. }
  2434. #ifdef CONFIG_X86_32
  2435. /*
  2436. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2437. * VMCS rather than the segment table. KVM uses this helper to figure
  2438. * out the current bases to poke them into the VMCS before entry.
  2439. */
  2440. static unsigned long segment_base(u16 selector)
  2441. {
  2442. struct desc_struct *table;
  2443. unsigned long v;
  2444. if (!(selector & ~SEGMENT_RPL_MASK))
  2445. return 0;
  2446. table = get_current_gdt_ro();
  2447. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2448. u16 ldt_selector = kvm_read_ldt();
  2449. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2450. return 0;
  2451. table = (struct desc_struct *)segment_base(ldt_selector);
  2452. }
  2453. v = get_desc_base(&table[selector >> 3]);
  2454. return v;
  2455. }
  2456. #endif
  2457. static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
  2458. {
  2459. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2460. struct vmcs_host_state *host_state;
  2461. #ifdef CONFIG_X86_64
  2462. int cpu = raw_smp_processor_id();
  2463. #endif
  2464. unsigned long fs_base, gs_base;
  2465. u16 fs_sel, gs_sel;
  2466. int i;
  2467. if (vmx->loaded_cpu_state)
  2468. return;
  2469. vmx->loaded_cpu_state = vmx->loaded_vmcs;
  2470. host_state = &vmx->loaded_cpu_state->host_state;
  2471. /*
  2472. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2473. * allow segment selectors with cpl > 0 or ti == 1.
  2474. */
  2475. host_state->ldt_sel = kvm_read_ldt();
  2476. #ifdef CONFIG_X86_64
  2477. savesegment(ds, host_state->ds_sel);
  2478. savesegment(es, host_state->es_sel);
  2479. gs_base = cpu_kernelmode_gs_base(cpu);
  2480. if (likely(is_64bit_mm(current->mm))) {
  2481. save_fsgs_for_kvm();
  2482. fs_sel = current->thread.fsindex;
  2483. gs_sel = current->thread.gsindex;
  2484. fs_base = current->thread.fsbase;
  2485. vmx->msr_host_kernel_gs_base = current->thread.gsbase;
  2486. } else {
  2487. savesegment(fs, fs_sel);
  2488. savesegment(gs, gs_sel);
  2489. fs_base = read_msr(MSR_FS_BASE);
  2490. vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2491. }
  2492. if (is_long_mode(&vmx->vcpu))
  2493. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2494. #else
  2495. savesegment(fs, fs_sel);
  2496. savesegment(gs, gs_sel);
  2497. fs_base = segment_base(fs_sel);
  2498. gs_base = segment_base(gs_sel);
  2499. #endif
  2500. if (unlikely(fs_sel != host_state->fs_sel)) {
  2501. if (!(fs_sel & 7))
  2502. vmcs_write16(HOST_FS_SELECTOR, fs_sel);
  2503. else
  2504. vmcs_write16(HOST_FS_SELECTOR, 0);
  2505. host_state->fs_sel = fs_sel;
  2506. }
  2507. if (unlikely(gs_sel != host_state->gs_sel)) {
  2508. if (!(gs_sel & 7))
  2509. vmcs_write16(HOST_GS_SELECTOR, gs_sel);
  2510. else
  2511. vmcs_write16(HOST_GS_SELECTOR, 0);
  2512. host_state->gs_sel = gs_sel;
  2513. }
  2514. if (unlikely(fs_base != host_state->fs_base)) {
  2515. vmcs_writel(HOST_FS_BASE, fs_base);
  2516. host_state->fs_base = fs_base;
  2517. }
  2518. if (unlikely(gs_base != host_state->gs_base)) {
  2519. vmcs_writel(HOST_GS_BASE, gs_base);
  2520. host_state->gs_base = gs_base;
  2521. }
  2522. for (i = 0; i < vmx->save_nmsrs; ++i)
  2523. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2524. vmx->guest_msrs[i].data,
  2525. vmx->guest_msrs[i].mask);
  2526. }
  2527. static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
  2528. {
  2529. struct vmcs_host_state *host_state;
  2530. if (!vmx->loaded_cpu_state)
  2531. return;
  2532. WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
  2533. host_state = &vmx->loaded_cpu_state->host_state;
  2534. ++vmx->vcpu.stat.host_state_reload;
  2535. vmx->loaded_cpu_state = NULL;
  2536. #ifdef CONFIG_X86_64
  2537. if (is_long_mode(&vmx->vcpu))
  2538. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2539. #endif
  2540. if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
  2541. kvm_load_ldt(host_state->ldt_sel);
  2542. #ifdef CONFIG_X86_64
  2543. load_gs_index(host_state->gs_sel);
  2544. #else
  2545. loadsegment(gs, host_state->gs_sel);
  2546. #endif
  2547. }
  2548. if (host_state->fs_sel & 7)
  2549. loadsegment(fs, host_state->fs_sel);
  2550. #ifdef CONFIG_X86_64
  2551. if (unlikely(host_state->ds_sel | host_state->es_sel)) {
  2552. loadsegment(ds, host_state->ds_sel);
  2553. loadsegment(es, host_state->es_sel);
  2554. }
  2555. #endif
  2556. invalidate_tss_limit();
  2557. #ifdef CONFIG_X86_64
  2558. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2559. #endif
  2560. load_fixmap_gdt(raw_smp_processor_id());
  2561. }
  2562. #ifdef CONFIG_X86_64
  2563. static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
  2564. {
  2565. if (is_long_mode(&vmx->vcpu)) {
  2566. preempt_disable();
  2567. if (vmx->loaded_cpu_state)
  2568. rdmsrl(MSR_KERNEL_GS_BASE,
  2569. vmx->msr_guest_kernel_gs_base);
  2570. preempt_enable();
  2571. }
  2572. return vmx->msr_guest_kernel_gs_base;
  2573. }
  2574. static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
  2575. {
  2576. if (is_long_mode(&vmx->vcpu)) {
  2577. preempt_disable();
  2578. if (vmx->loaded_cpu_state)
  2579. wrmsrl(MSR_KERNEL_GS_BASE, data);
  2580. preempt_enable();
  2581. }
  2582. vmx->msr_guest_kernel_gs_base = data;
  2583. }
  2584. #endif
  2585. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2586. {
  2587. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2588. struct pi_desc old, new;
  2589. unsigned int dest;
  2590. /*
  2591. * In case of hot-plug or hot-unplug, we may have to undo
  2592. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2593. * always keep PI.NDST up to date for simplicity: it makes the
  2594. * code easier, and CPU migration is not a fast path.
  2595. */
  2596. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2597. return;
  2598. /*
  2599. * First handle the simple case where no cmpxchg is necessary; just
  2600. * allow posting non-urgent interrupts.
  2601. *
  2602. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2603. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2604. * expects the VCPU to be on the blocked_vcpu_list that matches
  2605. * PI.NDST.
  2606. */
  2607. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2608. vcpu->cpu == cpu) {
  2609. pi_clear_sn(pi_desc);
  2610. return;
  2611. }
  2612. /* The full case. */
  2613. do {
  2614. old.control = new.control = pi_desc->control;
  2615. dest = cpu_physical_id(cpu);
  2616. if (x2apic_enabled())
  2617. new.ndst = dest;
  2618. else
  2619. new.ndst = (dest << 8) & 0xFF00;
  2620. new.sn = 0;
  2621. } while (cmpxchg64(&pi_desc->control, old.control,
  2622. new.control) != old.control);
  2623. }
  2624. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2625. {
  2626. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2627. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2628. }
  2629. /*
  2630. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2631. * vcpu mutex is already taken.
  2632. */
  2633. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2634. {
  2635. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2636. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2637. if (!already_loaded) {
  2638. loaded_vmcs_clear(vmx->loaded_vmcs);
  2639. local_irq_disable();
  2640. crash_disable_local_vmclear(cpu);
  2641. /*
  2642. * Read loaded_vmcs->cpu should be before fetching
  2643. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2644. * See the comments in __loaded_vmcs_clear().
  2645. */
  2646. smp_rmb();
  2647. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2648. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2649. crash_enable_local_vmclear(cpu);
  2650. local_irq_enable();
  2651. }
  2652. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2653. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2654. vmcs_load(vmx->loaded_vmcs->vmcs);
  2655. indirect_branch_prediction_barrier();
  2656. }
  2657. if (!already_loaded) {
  2658. void *gdt = get_current_gdt_ro();
  2659. unsigned long sysenter_esp;
  2660. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2661. /*
  2662. * Linux uses per-cpu TSS and GDT, so set these when switching
  2663. * processors. See 22.2.4.
  2664. */
  2665. vmcs_writel(HOST_TR_BASE,
  2666. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2667. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2668. /*
  2669. * VM exits change the host TR limit to 0x67 after a VM
  2670. * exit. This is okay, since 0x67 covers everything except
  2671. * the IO bitmap and have have code to handle the IO bitmap
  2672. * being lost after a VM exit.
  2673. */
  2674. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2675. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2676. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2677. vmx->loaded_vmcs->cpu = cpu;
  2678. }
  2679. /* Setup TSC multiplier */
  2680. if (kvm_has_tsc_control &&
  2681. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2682. decache_tsc_multiplier(vmx);
  2683. vmx_vcpu_pi_load(vcpu, cpu);
  2684. vmx->host_pkru = read_pkru();
  2685. vmx->host_debugctlmsr = get_debugctlmsr();
  2686. }
  2687. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2688. {
  2689. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2690. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2691. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2692. !kvm_vcpu_apicv_active(vcpu))
  2693. return;
  2694. /* Set SN when the vCPU is preempted */
  2695. if (vcpu->preempted)
  2696. pi_set_sn(pi_desc);
  2697. }
  2698. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2699. {
  2700. vmx_vcpu_pi_put(vcpu);
  2701. vmx_prepare_switch_to_host(to_vmx(vcpu));
  2702. }
  2703. static bool emulation_required(struct kvm_vcpu *vcpu)
  2704. {
  2705. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2706. }
  2707. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2708. /*
  2709. * Return the cr0 value that a nested guest would read. This is a combination
  2710. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2711. * its hypervisor (cr0_read_shadow).
  2712. */
  2713. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2714. {
  2715. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2716. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2717. }
  2718. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2719. {
  2720. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2721. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2722. }
  2723. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2724. {
  2725. unsigned long rflags, save_rflags;
  2726. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2727. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2728. rflags = vmcs_readl(GUEST_RFLAGS);
  2729. if (to_vmx(vcpu)->rmode.vm86_active) {
  2730. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2731. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2732. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2733. }
  2734. to_vmx(vcpu)->rflags = rflags;
  2735. }
  2736. return to_vmx(vcpu)->rflags;
  2737. }
  2738. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2739. {
  2740. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2741. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2742. to_vmx(vcpu)->rflags = rflags;
  2743. if (to_vmx(vcpu)->rmode.vm86_active) {
  2744. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2745. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2746. }
  2747. vmcs_writel(GUEST_RFLAGS, rflags);
  2748. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2749. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2750. }
  2751. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2752. {
  2753. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2754. int ret = 0;
  2755. if (interruptibility & GUEST_INTR_STATE_STI)
  2756. ret |= KVM_X86_SHADOW_INT_STI;
  2757. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2758. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2759. return ret;
  2760. }
  2761. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2762. {
  2763. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2764. u32 interruptibility = interruptibility_old;
  2765. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2766. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2767. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2768. else if (mask & KVM_X86_SHADOW_INT_STI)
  2769. interruptibility |= GUEST_INTR_STATE_STI;
  2770. if ((interruptibility != interruptibility_old))
  2771. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2772. }
  2773. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2774. {
  2775. unsigned long rip;
  2776. rip = kvm_rip_read(vcpu);
  2777. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2778. kvm_rip_write(vcpu, rip);
  2779. /* skipping an emulated instruction also counts */
  2780. vmx_set_interrupt_shadow(vcpu, 0);
  2781. }
  2782. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2783. unsigned long exit_qual)
  2784. {
  2785. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2786. unsigned int nr = vcpu->arch.exception.nr;
  2787. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2788. if (vcpu->arch.exception.has_error_code) {
  2789. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2790. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2791. }
  2792. if (kvm_exception_is_soft(nr))
  2793. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2794. else
  2795. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2796. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2797. vmx_get_nmi_mask(vcpu))
  2798. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2799. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2800. }
  2801. /*
  2802. * KVM wants to inject page-faults which it got to the guest. This function
  2803. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2804. */
  2805. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2806. {
  2807. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2808. unsigned int nr = vcpu->arch.exception.nr;
  2809. if (nr == PF_VECTOR) {
  2810. if (vcpu->arch.exception.nested_apf) {
  2811. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2812. return 1;
  2813. }
  2814. /*
  2815. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2816. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2817. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2818. * can be written only when inject_pending_event runs. This should be
  2819. * conditional on a new capability---if the capability is disabled,
  2820. * kvm_multiple_exception would write the ancillary information to
  2821. * CR2 or DR6, for backwards ABI-compatibility.
  2822. */
  2823. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2824. vcpu->arch.exception.error_code)) {
  2825. *exit_qual = vcpu->arch.cr2;
  2826. return 1;
  2827. }
  2828. } else {
  2829. if (vmcs12->exception_bitmap & (1u << nr)) {
  2830. if (nr == DB_VECTOR)
  2831. *exit_qual = vcpu->arch.dr6;
  2832. else
  2833. *exit_qual = 0;
  2834. return 1;
  2835. }
  2836. }
  2837. return 0;
  2838. }
  2839. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2840. {
  2841. /*
  2842. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2843. * explicitly skip the instruction because if the HLT state is set,
  2844. * then the instruction is already executing and RIP has already been
  2845. * advanced.
  2846. */
  2847. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2848. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2849. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2850. }
  2851. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2852. {
  2853. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2854. unsigned nr = vcpu->arch.exception.nr;
  2855. bool has_error_code = vcpu->arch.exception.has_error_code;
  2856. u32 error_code = vcpu->arch.exception.error_code;
  2857. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2858. if (has_error_code) {
  2859. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2860. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2861. }
  2862. if (vmx->rmode.vm86_active) {
  2863. int inc_eip = 0;
  2864. if (kvm_exception_is_soft(nr))
  2865. inc_eip = vcpu->arch.event_exit_inst_len;
  2866. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2867. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2868. return;
  2869. }
  2870. WARN_ON_ONCE(vmx->emulation_required);
  2871. if (kvm_exception_is_soft(nr)) {
  2872. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2873. vmx->vcpu.arch.event_exit_inst_len);
  2874. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2875. } else
  2876. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2877. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2878. vmx_clear_hlt(vcpu);
  2879. }
  2880. static bool vmx_rdtscp_supported(void)
  2881. {
  2882. return cpu_has_vmx_rdtscp();
  2883. }
  2884. static bool vmx_invpcid_supported(void)
  2885. {
  2886. return cpu_has_vmx_invpcid();
  2887. }
  2888. /*
  2889. * Swap MSR entry in host/guest MSR entry array.
  2890. */
  2891. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2892. {
  2893. struct shared_msr_entry tmp;
  2894. tmp = vmx->guest_msrs[to];
  2895. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2896. vmx->guest_msrs[from] = tmp;
  2897. }
  2898. /*
  2899. * Set up the vmcs to automatically save and restore system
  2900. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2901. * mode, as fiddling with msrs is very expensive.
  2902. */
  2903. static void setup_msrs(struct vcpu_vmx *vmx)
  2904. {
  2905. int save_nmsrs, index;
  2906. save_nmsrs = 0;
  2907. #ifdef CONFIG_X86_64
  2908. if (is_long_mode(&vmx->vcpu)) {
  2909. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2910. if (index >= 0)
  2911. move_msr_up(vmx, index, save_nmsrs++);
  2912. index = __find_msr_index(vmx, MSR_LSTAR);
  2913. if (index >= 0)
  2914. move_msr_up(vmx, index, save_nmsrs++);
  2915. index = __find_msr_index(vmx, MSR_CSTAR);
  2916. if (index >= 0)
  2917. move_msr_up(vmx, index, save_nmsrs++);
  2918. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2919. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2920. move_msr_up(vmx, index, save_nmsrs++);
  2921. /*
  2922. * MSR_STAR is only needed on long mode guests, and only
  2923. * if efer.sce is enabled.
  2924. */
  2925. index = __find_msr_index(vmx, MSR_STAR);
  2926. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2927. move_msr_up(vmx, index, save_nmsrs++);
  2928. }
  2929. #endif
  2930. index = __find_msr_index(vmx, MSR_EFER);
  2931. if (index >= 0 && update_transition_efer(vmx, index))
  2932. move_msr_up(vmx, index, save_nmsrs++);
  2933. vmx->save_nmsrs = save_nmsrs;
  2934. if (cpu_has_vmx_msr_bitmap())
  2935. vmx_update_msr_bitmap(&vmx->vcpu);
  2936. }
  2937. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2938. {
  2939. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2940. if (is_guest_mode(vcpu) &&
  2941. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2942. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2943. return vcpu->arch.tsc_offset;
  2944. }
  2945. /*
  2946. * writes 'offset' into guest's timestamp counter offset register
  2947. */
  2948. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2949. {
  2950. if (is_guest_mode(vcpu)) {
  2951. /*
  2952. * We're here if L1 chose not to trap WRMSR to TSC. According
  2953. * to the spec, this should set L1's TSC; The offset that L1
  2954. * set for L2 remains unchanged, and still needs to be added
  2955. * to the newly set TSC to get L2's TSC.
  2956. */
  2957. struct vmcs12 *vmcs12;
  2958. /* recalculate vmcs02.TSC_OFFSET: */
  2959. vmcs12 = get_vmcs12(vcpu);
  2960. vmcs_write64(TSC_OFFSET, offset +
  2961. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2962. vmcs12->tsc_offset : 0));
  2963. } else {
  2964. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2965. vmcs_read64(TSC_OFFSET), offset);
  2966. vmcs_write64(TSC_OFFSET, offset);
  2967. }
  2968. }
  2969. /*
  2970. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2971. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2972. * all guests if the "nested" module option is off, and can also be disabled
  2973. * for a single guest by disabling its VMX cpuid bit.
  2974. */
  2975. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2976. {
  2977. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2978. }
  2979. /*
  2980. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2981. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2982. * The same values should also be used to verify that vmcs12 control fields are
  2983. * valid during nested entry from L1 to L2.
  2984. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2985. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2986. * bit in the high half is on if the corresponding bit in the control field
  2987. * may be on. See also vmx_control_verify().
  2988. */
  2989. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  2990. {
  2991. if (!nested) {
  2992. memset(msrs, 0, sizeof(*msrs));
  2993. return;
  2994. }
  2995. /*
  2996. * Note that as a general rule, the high half of the MSRs (bits in
  2997. * the control fields which may be 1) should be initialized by the
  2998. * intersection of the underlying hardware's MSR (i.e., features which
  2999. * can be supported) and the list of features we want to expose -
  3000. * because they are known to be properly supported in our code.
  3001. * Also, usually, the low half of the MSRs (bits which must be 1) can
  3002. * be set to 0, meaning that L1 may turn off any of these bits. The
  3003. * reason is that if one of these bits is necessary, it will appear
  3004. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  3005. * fields of vmcs01 and vmcs02, will turn these bits off - and
  3006. * nested_vmx_exit_reflected() will not pass related exits to L1.
  3007. * These rules have exceptions below.
  3008. */
  3009. /* pin-based controls */
  3010. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  3011. msrs->pinbased_ctls_low,
  3012. msrs->pinbased_ctls_high);
  3013. msrs->pinbased_ctls_low |=
  3014. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3015. msrs->pinbased_ctls_high &=
  3016. PIN_BASED_EXT_INTR_MASK |
  3017. PIN_BASED_NMI_EXITING |
  3018. PIN_BASED_VIRTUAL_NMIS |
  3019. (apicv ? PIN_BASED_POSTED_INTR : 0);
  3020. msrs->pinbased_ctls_high |=
  3021. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3022. PIN_BASED_VMX_PREEMPTION_TIMER;
  3023. /* exit controls */
  3024. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  3025. msrs->exit_ctls_low,
  3026. msrs->exit_ctls_high);
  3027. msrs->exit_ctls_low =
  3028. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3029. msrs->exit_ctls_high &=
  3030. #ifdef CONFIG_X86_64
  3031. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  3032. #endif
  3033. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  3034. msrs->exit_ctls_high |=
  3035. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  3036. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  3037. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  3038. if (kvm_mpx_supported())
  3039. msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  3040. /* We support free control of debug control saving. */
  3041. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  3042. /* entry controls */
  3043. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  3044. msrs->entry_ctls_low,
  3045. msrs->entry_ctls_high);
  3046. msrs->entry_ctls_low =
  3047. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3048. msrs->entry_ctls_high &=
  3049. #ifdef CONFIG_X86_64
  3050. VM_ENTRY_IA32E_MODE |
  3051. #endif
  3052. VM_ENTRY_LOAD_IA32_PAT;
  3053. msrs->entry_ctls_high |=
  3054. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  3055. if (kvm_mpx_supported())
  3056. msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  3057. /* We support free control of debug control loading. */
  3058. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3059. /* cpu-based controls */
  3060. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  3061. msrs->procbased_ctls_low,
  3062. msrs->procbased_ctls_high);
  3063. msrs->procbased_ctls_low =
  3064. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3065. msrs->procbased_ctls_high &=
  3066. CPU_BASED_VIRTUAL_INTR_PENDING |
  3067. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  3068. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  3069. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  3070. CPU_BASED_CR3_STORE_EXITING |
  3071. #ifdef CONFIG_X86_64
  3072. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  3073. #endif
  3074. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  3075. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  3076. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  3077. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  3078. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3079. /*
  3080. * We can allow some features even when not supported by the
  3081. * hardware. For example, L1 can specify an MSR bitmap - and we
  3082. * can use it to avoid exits to L1 - even when L0 runs L2
  3083. * without MSR bitmaps.
  3084. */
  3085. msrs->procbased_ctls_high |=
  3086. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3087. CPU_BASED_USE_MSR_BITMAPS;
  3088. /* We support free control of CR3 access interception. */
  3089. msrs->procbased_ctls_low &=
  3090. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  3091. /*
  3092. * secondary cpu-based controls. Do not include those that
  3093. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  3094. */
  3095. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  3096. msrs->secondary_ctls_low,
  3097. msrs->secondary_ctls_high);
  3098. msrs->secondary_ctls_low = 0;
  3099. msrs->secondary_ctls_high &=
  3100. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3101. SECONDARY_EXEC_DESC |
  3102. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3103. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3104. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3105. SECONDARY_EXEC_WBINVD_EXITING;
  3106. /*
  3107. * We can emulate "VMCS shadowing," even if the hardware
  3108. * doesn't support it.
  3109. */
  3110. msrs->secondary_ctls_high |=
  3111. SECONDARY_EXEC_SHADOW_VMCS;
  3112. if (enable_ept) {
  3113. /* nested EPT: emulate EPT also to L1 */
  3114. msrs->secondary_ctls_high |=
  3115. SECONDARY_EXEC_ENABLE_EPT;
  3116. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  3117. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  3118. if (cpu_has_vmx_ept_execute_only())
  3119. msrs->ept_caps |=
  3120. VMX_EPT_EXECUTE_ONLY_BIT;
  3121. msrs->ept_caps &= vmx_capability.ept;
  3122. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  3123. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  3124. VMX_EPT_1GB_PAGE_BIT;
  3125. if (enable_ept_ad_bits) {
  3126. msrs->secondary_ctls_high |=
  3127. SECONDARY_EXEC_ENABLE_PML;
  3128. msrs->ept_caps |= VMX_EPT_AD_BIT;
  3129. }
  3130. }
  3131. if (cpu_has_vmx_vmfunc()) {
  3132. msrs->secondary_ctls_high |=
  3133. SECONDARY_EXEC_ENABLE_VMFUNC;
  3134. /*
  3135. * Advertise EPTP switching unconditionally
  3136. * since we emulate it
  3137. */
  3138. if (enable_ept)
  3139. msrs->vmfunc_controls =
  3140. VMX_VMFUNC_EPTP_SWITCHING;
  3141. }
  3142. /*
  3143. * Old versions of KVM use the single-context version without
  3144. * checking for support, so declare that it is supported even
  3145. * though it is treated as global context. The alternative is
  3146. * not failing the single-context invvpid, and it is worse.
  3147. */
  3148. if (enable_vpid) {
  3149. msrs->secondary_ctls_high |=
  3150. SECONDARY_EXEC_ENABLE_VPID;
  3151. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  3152. VMX_VPID_EXTENT_SUPPORTED_MASK;
  3153. }
  3154. if (enable_unrestricted_guest)
  3155. msrs->secondary_ctls_high |=
  3156. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3157. /* miscellaneous data */
  3158. rdmsr(MSR_IA32_VMX_MISC,
  3159. msrs->misc_low,
  3160. msrs->misc_high);
  3161. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  3162. msrs->misc_low |=
  3163. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  3164. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  3165. VMX_MISC_ACTIVITY_HLT;
  3166. msrs->misc_high = 0;
  3167. /*
  3168. * This MSR reports some information about VMX support. We
  3169. * should return information about the VMX we emulate for the
  3170. * guest, and the VMCS structure we give it - not about the
  3171. * VMX support of the underlying hardware.
  3172. */
  3173. msrs->basic =
  3174. VMCS12_REVISION |
  3175. VMX_BASIC_TRUE_CTLS |
  3176. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  3177. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  3178. if (cpu_has_vmx_basic_inout())
  3179. msrs->basic |= VMX_BASIC_INOUT;
  3180. /*
  3181. * These MSRs specify bits which the guest must keep fixed on
  3182. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  3183. * We picked the standard core2 setting.
  3184. */
  3185. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  3186. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  3187. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  3188. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  3189. /* These MSRs specify bits which the guest must keep fixed off. */
  3190. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  3191. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  3192. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  3193. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  3194. }
  3195. /*
  3196. * if fixed0[i] == 1: val[i] must be 1
  3197. * if fixed1[i] == 0: val[i] must be 0
  3198. */
  3199. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  3200. {
  3201. return ((val & fixed1) | fixed0) == val;
  3202. }
  3203. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  3204. {
  3205. return fixed_bits_valid(control, low, high);
  3206. }
  3207. static inline u64 vmx_control_msr(u32 low, u32 high)
  3208. {
  3209. return low | ((u64)high << 32);
  3210. }
  3211. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  3212. {
  3213. superset &= mask;
  3214. subset &= mask;
  3215. return (superset | subset) == superset;
  3216. }
  3217. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  3218. {
  3219. const u64 feature_and_reserved =
  3220. /* feature (except bit 48; see below) */
  3221. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  3222. /* reserved */
  3223. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  3224. u64 vmx_basic = vmx->nested.msrs.basic;
  3225. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  3226. return -EINVAL;
  3227. /*
  3228. * KVM does not emulate a version of VMX that constrains physical
  3229. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  3230. */
  3231. if (data & BIT_ULL(48))
  3232. return -EINVAL;
  3233. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  3234. vmx_basic_vmcs_revision_id(data))
  3235. return -EINVAL;
  3236. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  3237. return -EINVAL;
  3238. vmx->nested.msrs.basic = data;
  3239. return 0;
  3240. }
  3241. static int
  3242. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3243. {
  3244. u64 supported;
  3245. u32 *lowp, *highp;
  3246. switch (msr_index) {
  3247. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3248. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  3249. highp = &vmx->nested.msrs.pinbased_ctls_high;
  3250. break;
  3251. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3252. lowp = &vmx->nested.msrs.procbased_ctls_low;
  3253. highp = &vmx->nested.msrs.procbased_ctls_high;
  3254. break;
  3255. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3256. lowp = &vmx->nested.msrs.exit_ctls_low;
  3257. highp = &vmx->nested.msrs.exit_ctls_high;
  3258. break;
  3259. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3260. lowp = &vmx->nested.msrs.entry_ctls_low;
  3261. highp = &vmx->nested.msrs.entry_ctls_high;
  3262. break;
  3263. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3264. lowp = &vmx->nested.msrs.secondary_ctls_low;
  3265. highp = &vmx->nested.msrs.secondary_ctls_high;
  3266. break;
  3267. default:
  3268. BUG();
  3269. }
  3270. supported = vmx_control_msr(*lowp, *highp);
  3271. /* Check must-be-1 bits are still 1. */
  3272. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3273. return -EINVAL;
  3274. /* Check must-be-0 bits are still 0. */
  3275. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3276. return -EINVAL;
  3277. *lowp = data;
  3278. *highp = data >> 32;
  3279. return 0;
  3280. }
  3281. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3282. {
  3283. const u64 feature_and_reserved_bits =
  3284. /* feature */
  3285. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3286. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3287. /* reserved */
  3288. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3289. u64 vmx_misc;
  3290. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3291. vmx->nested.msrs.misc_high);
  3292. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3293. return -EINVAL;
  3294. if ((vmx->nested.msrs.pinbased_ctls_high &
  3295. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3296. vmx_misc_preemption_timer_rate(data) !=
  3297. vmx_misc_preemption_timer_rate(vmx_misc))
  3298. return -EINVAL;
  3299. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3300. return -EINVAL;
  3301. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3302. return -EINVAL;
  3303. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3304. return -EINVAL;
  3305. vmx->nested.msrs.misc_low = data;
  3306. vmx->nested.msrs.misc_high = data >> 32;
  3307. /*
  3308. * If L1 has read-only VM-exit information fields, use the
  3309. * less permissive vmx_vmwrite_bitmap to specify write
  3310. * permissions for the shadow VMCS.
  3311. */
  3312. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3313. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3314. return 0;
  3315. }
  3316. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3317. {
  3318. u64 vmx_ept_vpid_cap;
  3319. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3320. vmx->nested.msrs.vpid_caps);
  3321. /* Every bit is either reserved or a feature bit. */
  3322. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3323. return -EINVAL;
  3324. vmx->nested.msrs.ept_caps = data;
  3325. vmx->nested.msrs.vpid_caps = data >> 32;
  3326. return 0;
  3327. }
  3328. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3329. {
  3330. u64 *msr;
  3331. switch (msr_index) {
  3332. case MSR_IA32_VMX_CR0_FIXED0:
  3333. msr = &vmx->nested.msrs.cr0_fixed0;
  3334. break;
  3335. case MSR_IA32_VMX_CR4_FIXED0:
  3336. msr = &vmx->nested.msrs.cr4_fixed0;
  3337. break;
  3338. default:
  3339. BUG();
  3340. }
  3341. /*
  3342. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3343. * must be 1 in the restored value.
  3344. */
  3345. if (!is_bitwise_subset(data, *msr, -1ULL))
  3346. return -EINVAL;
  3347. *msr = data;
  3348. return 0;
  3349. }
  3350. /*
  3351. * Called when userspace is restoring VMX MSRs.
  3352. *
  3353. * Returns 0 on success, non-0 otherwise.
  3354. */
  3355. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3356. {
  3357. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3358. /*
  3359. * Don't allow changes to the VMX capability MSRs while the vCPU
  3360. * is in VMX operation.
  3361. */
  3362. if (vmx->nested.vmxon)
  3363. return -EBUSY;
  3364. switch (msr_index) {
  3365. case MSR_IA32_VMX_BASIC:
  3366. return vmx_restore_vmx_basic(vmx, data);
  3367. case MSR_IA32_VMX_PINBASED_CTLS:
  3368. case MSR_IA32_VMX_PROCBASED_CTLS:
  3369. case MSR_IA32_VMX_EXIT_CTLS:
  3370. case MSR_IA32_VMX_ENTRY_CTLS:
  3371. /*
  3372. * The "non-true" VMX capability MSRs are generated from the
  3373. * "true" MSRs, so we do not support restoring them directly.
  3374. *
  3375. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3376. * should restore the "true" MSRs with the must-be-1 bits
  3377. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3378. * DEFAULT SETTINGS".
  3379. */
  3380. return -EINVAL;
  3381. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3382. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3383. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3384. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3385. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3386. return vmx_restore_control_msr(vmx, msr_index, data);
  3387. case MSR_IA32_VMX_MISC:
  3388. return vmx_restore_vmx_misc(vmx, data);
  3389. case MSR_IA32_VMX_CR0_FIXED0:
  3390. case MSR_IA32_VMX_CR4_FIXED0:
  3391. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3392. case MSR_IA32_VMX_CR0_FIXED1:
  3393. case MSR_IA32_VMX_CR4_FIXED1:
  3394. /*
  3395. * These MSRs are generated based on the vCPU's CPUID, so we
  3396. * do not support restoring them directly.
  3397. */
  3398. return -EINVAL;
  3399. case MSR_IA32_VMX_EPT_VPID_CAP:
  3400. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3401. case MSR_IA32_VMX_VMCS_ENUM:
  3402. vmx->nested.msrs.vmcs_enum = data;
  3403. return 0;
  3404. default:
  3405. /*
  3406. * The rest of the VMX capability MSRs do not support restore.
  3407. */
  3408. return -EINVAL;
  3409. }
  3410. }
  3411. /* Returns 0 on success, non-0 otherwise. */
  3412. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3413. {
  3414. switch (msr_index) {
  3415. case MSR_IA32_VMX_BASIC:
  3416. *pdata = msrs->basic;
  3417. break;
  3418. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3419. case MSR_IA32_VMX_PINBASED_CTLS:
  3420. *pdata = vmx_control_msr(
  3421. msrs->pinbased_ctls_low,
  3422. msrs->pinbased_ctls_high);
  3423. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3424. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3425. break;
  3426. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3427. case MSR_IA32_VMX_PROCBASED_CTLS:
  3428. *pdata = vmx_control_msr(
  3429. msrs->procbased_ctls_low,
  3430. msrs->procbased_ctls_high);
  3431. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3432. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3433. break;
  3434. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3435. case MSR_IA32_VMX_EXIT_CTLS:
  3436. *pdata = vmx_control_msr(
  3437. msrs->exit_ctls_low,
  3438. msrs->exit_ctls_high);
  3439. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3440. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3441. break;
  3442. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3443. case MSR_IA32_VMX_ENTRY_CTLS:
  3444. *pdata = vmx_control_msr(
  3445. msrs->entry_ctls_low,
  3446. msrs->entry_ctls_high);
  3447. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3448. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3449. break;
  3450. case MSR_IA32_VMX_MISC:
  3451. *pdata = vmx_control_msr(
  3452. msrs->misc_low,
  3453. msrs->misc_high);
  3454. break;
  3455. case MSR_IA32_VMX_CR0_FIXED0:
  3456. *pdata = msrs->cr0_fixed0;
  3457. break;
  3458. case MSR_IA32_VMX_CR0_FIXED1:
  3459. *pdata = msrs->cr0_fixed1;
  3460. break;
  3461. case MSR_IA32_VMX_CR4_FIXED0:
  3462. *pdata = msrs->cr4_fixed0;
  3463. break;
  3464. case MSR_IA32_VMX_CR4_FIXED1:
  3465. *pdata = msrs->cr4_fixed1;
  3466. break;
  3467. case MSR_IA32_VMX_VMCS_ENUM:
  3468. *pdata = msrs->vmcs_enum;
  3469. break;
  3470. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3471. *pdata = vmx_control_msr(
  3472. msrs->secondary_ctls_low,
  3473. msrs->secondary_ctls_high);
  3474. break;
  3475. case MSR_IA32_VMX_EPT_VPID_CAP:
  3476. *pdata = msrs->ept_caps |
  3477. ((u64)msrs->vpid_caps << 32);
  3478. break;
  3479. case MSR_IA32_VMX_VMFUNC:
  3480. *pdata = msrs->vmfunc_controls;
  3481. break;
  3482. default:
  3483. return 1;
  3484. }
  3485. return 0;
  3486. }
  3487. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3488. uint64_t val)
  3489. {
  3490. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3491. return !(val & ~valid_bits);
  3492. }
  3493. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3494. {
  3495. switch (msr->index) {
  3496. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3497. if (!nested)
  3498. return 1;
  3499. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3500. default:
  3501. return 1;
  3502. }
  3503. return 0;
  3504. }
  3505. /*
  3506. * Reads an msr value (of 'msr_index') into 'pdata'.
  3507. * Returns 0 on success, non-0 otherwise.
  3508. * Assumes vcpu_load() was already called.
  3509. */
  3510. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3511. {
  3512. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3513. struct shared_msr_entry *msr;
  3514. switch (msr_info->index) {
  3515. #ifdef CONFIG_X86_64
  3516. case MSR_FS_BASE:
  3517. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3518. break;
  3519. case MSR_GS_BASE:
  3520. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3521. break;
  3522. case MSR_KERNEL_GS_BASE:
  3523. msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
  3524. break;
  3525. #endif
  3526. case MSR_EFER:
  3527. return kvm_get_msr_common(vcpu, msr_info);
  3528. case MSR_IA32_SPEC_CTRL:
  3529. if (!msr_info->host_initiated &&
  3530. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3531. return 1;
  3532. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3533. break;
  3534. case MSR_IA32_ARCH_CAPABILITIES:
  3535. if (!msr_info->host_initiated &&
  3536. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  3537. return 1;
  3538. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  3539. break;
  3540. case MSR_IA32_SYSENTER_CS:
  3541. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3542. break;
  3543. case MSR_IA32_SYSENTER_EIP:
  3544. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3545. break;
  3546. case MSR_IA32_SYSENTER_ESP:
  3547. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3548. break;
  3549. case MSR_IA32_BNDCFGS:
  3550. if (!kvm_mpx_supported() ||
  3551. (!msr_info->host_initiated &&
  3552. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3553. return 1;
  3554. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3555. break;
  3556. case MSR_IA32_MCG_EXT_CTL:
  3557. if (!msr_info->host_initiated &&
  3558. !(vmx->msr_ia32_feature_control &
  3559. FEATURE_CONTROL_LMCE))
  3560. return 1;
  3561. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3562. break;
  3563. case MSR_IA32_FEATURE_CONTROL:
  3564. msr_info->data = vmx->msr_ia32_feature_control;
  3565. break;
  3566. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3567. if (!nested_vmx_allowed(vcpu))
  3568. return 1;
  3569. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3570. &msr_info->data);
  3571. case MSR_IA32_XSS:
  3572. if (!vmx_xsaves_supported())
  3573. return 1;
  3574. msr_info->data = vcpu->arch.ia32_xss;
  3575. break;
  3576. case MSR_TSC_AUX:
  3577. if (!msr_info->host_initiated &&
  3578. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3579. return 1;
  3580. /* Otherwise falls through */
  3581. default:
  3582. msr = find_msr_entry(vmx, msr_info->index);
  3583. if (msr) {
  3584. msr_info->data = msr->data;
  3585. break;
  3586. }
  3587. return kvm_get_msr_common(vcpu, msr_info);
  3588. }
  3589. return 0;
  3590. }
  3591. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3592. /*
  3593. * Writes msr value into into the appropriate "register".
  3594. * Returns 0 on success, non-0 otherwise.
  3595. * Assumes vcpu_load() was already called.
  3596. */
  3597. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3598. {
  3599. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3600. struct shared_msr_entry *msr;
  3601. int ret = 0;
  3602. u32 msr_index = msr_info->index;
  3603. u64 data = msr_info->data;
  3604. switch (msr_index) {
  3605. case MSR_EFER:
  3606. ret = kvm_set_msr_common(vcpu, msr_info);
  3607. break;
  3608. #ifdef CONFIG_X86_64
  3609. case MSR_FS_BASE:
  3610. vmx_segment_cache_clear(vmx);
  3611. vmcs_writel(GUEST_FS_BASE, data);
  3612. break;
  3613. case MSR_GS_BASE:
  3614. vmx_segment_cache_clear(vmx);
  3615. vmcs_writel(GUEST_GS_BASE, data);
  3616. break;
  3617. case MSR_KERNEL_GS_BASE:
  3618. vmx_write_guest_kernel_gs_base(vmx, data);
  3619. break;
  3620. #endif
  3621. case MSR_IA32_SYSENTER_CS:
  3622. vmcs_write32(GUEST_SYSENTER_CS, data);
  3623. break;
  3624. case MSR_IA32_SYSENTER_EIP:
  3625. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3626. break;
  3627. case MSR_IA32_SYSENTER_ESP:
  3628. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3629. break;
  3630. case MSR_IA32_BNDCFGS:
  3631. if (!kvm_mpx_supported() ||
  3632. (!msr_info->host_initiated &&
  3633. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3634. return 1;
  3635. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3636. (data & MSR_IA32_BNDCFGS_RSVD))
  3637. return 1;
  3638. vmcs_write64(GUEST_BNDCFGS, data);
  3639. break;
  3640. case MSR_IA32_SPEC_CTRL:
  3641. if (!msr_info->host_initiated &&
  3642. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3643. return 1;
  3644. /* The STIBP bit doesn't fault even if it's not advertised */
  3645. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3646. return 1;
  3647. vmx->spec_ctrl = data;
  3648. if (!data)
  3649. break;
  3650. /*
  3651. * For non-nested:
  3652. * When it's written (to non-zero) for the first time, pass
  3653. * it through.
  3654. *
  3655. * For nested:
  3656. * The handling of the MSR bitmap for L2 guests is done in
  3657. * nested_vmx_merge_msr_bitmap. We should not touch the
  3658. * vmcs02.msr_bitmap here since it gets completely overwritten
  3659. * in the merging. We update the vmcs01 here for L1 as well
  3660. * since it will end up touching the MSR anyway now.
  3661. */
  3662. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3663. MSR_IA32_SPEC_CTRL,
  3664. MSR_TYPE_RW);
  3665. break;
  3666. case MSR_IA32_PRED_CMD:
  3667. if (!msr_info->host_initiated &&
  3668. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3669. return 1;
  3670. if (data & ~PRED_CMD_IBPB)
  3671. return 1;
  3672. if (!data)
  3673. break;
  3674. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3675. /*
  3676. * For non-nested:
  3677. * When it's written (to non-zero) for the first time, pass
  3678. * it through.
  3679. *
  3680. * For nested:
  3681. * The handling of the MSR bitmap for L2 guests is done in
  3682. * nested_vmx_merge_msr_bitmap. We should not touch the
  3683. * vmcs02.msr_bitmap here since it gets completely overwritten
  3684. * in the merging.
  3685. */
  3686. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3687. MSR_TYPE_W);
  3688. break;
  3689. case MSR_IA32_ARCH_CAPABILITIES:
  3690. if (!msr_info->host_initiated)
  3691. return 1;
  3692. vmx->arch_capabilities = data;
  3693. break;
  3694. case MSR_IA32_CR_PAT:
  3695. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3696. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3697. return 1;
  3698. vmcs_write64(GUEST_IA32_PAT, data);
  3699. vcpu->arch.pat = data;
  3700. break;
  3701. }
  3702. ret = kvm_set_msr_common(vcpu, msr_info);
  3703. break;
  3704. case MSR_IA32_TSC_ADJUST:
  3705. ret = kvm_set_msr_common(vcpu, msr_info);
  3706. break;
  3707. case MSR_IA32_MCG_EXT_CTL:
  3708. if ((!msr_info->host_initiated &&
  3709. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3710. FEATURE_CONTROL_LMCE)) ||
  3711. (data & ~MCG_EXT_CTL_LMCE_EN))
  3712. return 1;
  3713. vcpu->arch.mcg_ext_ctl = data;
  3714. break;
  3715. case MSR_IA32_FEATURE_CONTROL:
  3716. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3717. (to_vmx(vcpu)->msr_ia32_feature_control &
  3718. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3719. return 1;
  3720. vmx->msr_ia32_feature_control = data;
  3721. if (msr_info->host_initiated && data == 0)
  3722. vmx_leave_nested(vcpu);
  3723. break;
  3724. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3725. if (!msr_info->host_initiated)
  3726. return 1; /* they are read-only */
  3727. if (!nested_vmx_allowed(vcpu))
  3728. return 1;
  3729. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3730. case MSR_IA32_XSS:
  3731. if (!vmx_xsaves_supported())
  3732. return 1;
  3733. /*
  3734. * The only supported bit as of Skylake is bit 8, but
  3735. * it is not supported on KVM.
  3736. */
  3737. if (data != 0)
  3738. return 1;
  3739. vcpu->arch.ia32_xss = data;
  3740. if (vcpu->arch.ia32_xss != host_xss)
  3741. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3742. vcpu->arch.ia32_xss, host_xss, false);
  3743. else
  3744. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3745. break;
  3746. case MSR_TSC_AUX:
  3747. if (!msr_info->host_initiated &&
  3748. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3749. return 1;
  3750. /* Check reserved bit, higher 32 bits should be zero */
  3751. if ((data >> 32) != 0)
  3752. return 1;
  3753. /* Otherwise falls through */
  3754. default:
  3755. msr = find_msr_entry(vmx, msr_index);
  3756. if (msr) {
  3757. u64 old_msr_data = msr->data;
  3758. msr->data = data;
  3759. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3760. preempt_disable();
  3761. ret = kvm_set_shared_msr(msr->index, msr->data,
  3762. msr->mask);
  3763. preempt_enable();
  3764. if (ret)
  3765. msr->data = old_msr_data;
  3766. }
  3767. break;
  3768. }
  3769. ret = kvm_set_msr_common(vcpu, msr_info);
  3770. }
  3771. return ret;
  3772. }
  3773. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3774. {
  3775. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3776. switch (reg) {
  3777. case VCPU_REGS_RSP:
  3778. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3779. break;
  3780. case VCPU_REGS_RIP:
  3781. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3782. break;
  3783. case VCPU_EXREG_PDPTR:
  3784. if (enable_ept)
  3785. ept_save_pdptrs(vcpu);
  3786. break;
  3787. default:
  3788. break;
  3789. }
  3790. }
  3791. static __init int cpu_has_kvm_support(void)
  3792. {
  3793. return cpu_has_vmx();
  3794. }
  3795. static __init int vmx_disabled_by_bios(void)
  3796. {
  3797. u64 msr;
  3798. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3799. if (msr & FEATURE_CONTROL_LOCKED) {
  3800. /* launched w/ TXT and VMX disabled */
  3801. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3802. && tboot_enabled())
  3803. return 1;
  3804. /* launched w/o TXT and VMX only enabled w/ TXT */
  3805. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3806. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3807. && !tboot_enabled()) {
  3808. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3809. "activate TXT before enabling KVM\n");
  3810. return 1;
  3811. }
  3812. /* launched w/o TXT and VMX disabled */
  3813. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3814. && !tboot_enabled())
  3815. return 1;
  3816. }
  3817. return 0;
  3818. }
  3819. static void kvm_cpu_vmxon(u64 addr)
  3820. {
  3821. cr4_set_bits(X86_CR4_VMXE);
  3822. intel_pt_handle_vmx(1);
  3823. asm volatile (ASM_VMX_VMXON_RAX
  3824. : : "a"(&addr), "m"(addr)
  3825. : "memory", "cc");
  3826. }
  3827. static int hardware_enable(void)
  3828. {
  3829. int cpu = raw_smp_processor_id();
  3830. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3831. u64 old, test_bits;
  3832. if (cr4_read_shadow() & X86_CR4_VMXE)
  3833. return -EBUSY;
  3834. /*
  3835. * This can happen if we hot-added a CPU but failed to allocate
  3836. * VP assist page for it.
  3837. */
  3838. if (static_branch_unlikely(&enable_evmcs) &&
  3839. !hv_get_vp_assist_page(cpu))
  3840. return -EFAULT;
  3841. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3842. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3843. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3844. /*
  3845. * Now we can enable the vmclear operation in kdump
  3846. * since the loaded_vmcss_on_cpu list on this cpu
  3847. * has been initialized.
  3848. *
  3849. * Though the cpu is not in VMX operation now, there
  3850. * is no problem to enable the vmclear operation
  3851. * for the loaded_vmcss_on_cpu list is empty!
  3852. */
  3853. crash_enable_local_vmclear(cpu);
  3854. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3855. test_bits = FEATURE_CONTROL_LOCKED;
  3856. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3857. if (tboot_enabled())
  3858. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3859. if ((old & test_bits) != test_bits) {
  3860. /* enable and lock */
  3861. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3862. }
  3863. kvm_cpu_vmxon(phys_addr);
  3864. if (enable_ept)
  3865. ept_sync_global();
  3866. return 0;
  3867. }
  3868. static void vmclear_local_loaded_vmcss(void)
  3869. {
  3870. int cpu = raw_smp_processor_id();
  3871. struct loaded_vmcs *v, *n;
  3872. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3873. loaded_vmcss_on_cpu_link)
  3874. __loaded_vmcs_clear(v);
  3875. }
  3876. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3877. * tricks.
  3878. */
  3879. static void kvm_cpu_vmxoff(void)
  3880. {
  3881. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3882. intel_pt_handle_vmx(0);
  3883. cr4_clear_bits(X86_CR4_VMXE);
  3884. }
  3885. static void hardware_disable(void)
  3886. {
  3887. vmclear_local_loaded_vmcss();
  3888. kvm_cpu_vmxoff();
  3889. }
  3890. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3891. u32 msr, u32 *result)
  3892. {
  3893. u32 vmx_msr_low, vmx_msr_high;
  3894. u32 ctl = ctl_min | ctl_opt;
  3895. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3896. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3897. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3898. /* Ensure minimum (required) set of control bits are supported. */
  3899. if (ctl_min & ~ctl)
  3900. return -EIO;
  3901. *result = ctl;
  3902. return 0;
  3903. }
  3904. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3905. {
  3906. u32 vmx_msr_low, vmx_msr_high;
  3907. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3908. return vmx_msr_high & ctl;
  3909. }
  3910. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3911. {
  3912. u32 vmx_msr_low, vmx_msr_high;
  3913. u32 min, opt, min2, opt2;
  3914. u32 _pin_based_exec_control = 0;
  3915. u32 _cpu_based_exec_control = 0;
  3916. u32 _cpu_based_2nd_exec_control = 0;
  3917. u32 _vmexit_control = 0;
  3918. u32 _vmentry_control = 0;
  3919. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3920. min = CPU_BASED_HLT_EXITING |
  3921. #ifdef CONFIG_X86_64
  3922. CPU_BASED_CR8_LOAD_EXITING |
  3923. CPU_BASED_CR8_STORE_EXITING |
  3924. #endif
  3925. CPU_BASED_CR3_LOAD_EXITING |
  3926. CPU_BASED_CR3_STORE_EXITING |
  3927. CPU_BASED_UNCOND_IO_EXITING |
  3928. CPU_BASED_MOV_DR_EXITING |
  3929. CPU_BASED_USE_TSC_OFFSETING |
  3930. CPU_BASED_MWAIT_EXITING |
  3931. CPU_BASED_MONITOR_EXITING |
  3932. CPU_BASED_INVLPG_EXITING |
  3933. CPU_BASED_RDPMC_EXITING;
  3934. opt = CPU_BASED_TPR_SHADOW |
  3935. CPU_BASED_USE_MSR_BITMAPS |
  3936. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3937. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3938. &_cpu_based_exec_control) < 0)
  3939. return -EIO;
  3940. #ifdef CONFIG_X86_64
  3941. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3942. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3943. ~CPU_BASED_CR8_STORE_EXITING;
  3944. #endif
  3945. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3946. min2 = 0;
  3947. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3948. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3949. SECONDARY_EXEC_WBINVD_EXITING |
  3950. SECONDARY_EXEC_ENABLE_VPID |
  3951. SECONDARY_EXEC_ENABLE_EPT |
  3952. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3953. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3954. SECONDARY_EXEC_DESC |
  3955. SECONDARY_EXEC_RDTSCP |
  3956. SECONDARY_EXEC_ENABLE_INVPCID |
  3957. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3958. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3959. SECONDARY_EXEC_SHADOW_VMCS |
  3960. SECONDARY_EXEC_XSAVES |
  3961. SECONDARY_EXEC_RDSEED_EXITING |
  3962. SECONDARY_EXEC_RDRAND_EXITING |
  3963. SECONDARY_EXEC_ENABLE_PML |
  3964. SECONDARY_EXEC_TSC_SCALING |
  3965. SECONDARY_EXEC_ENABLE_VMFUNC |
  3966. SECONDARY_EXEC_ENCLS_EXITING;
  3967. if (adjust_vmx_controls(min2, opt2,
  3968. MSR_IA32_VMX_PROCBASED_CTLS2,
  3969. &_cpu_based_2nd_exec_control) < 0)
  3970. return -EIO;
  3971. }
  3972. #ifndef CONFIG_X86_64
  3973. if (!(_cpu_based_2nd_exec_control &
  3974. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3975. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3976. #endif
  3977. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3978. _cpu_based_2nd_exec_control &= ~(
  3979. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3980. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3981. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3982. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3983. &vmx_capability.ept, &vmx_capability.vpid);
  3984. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3985. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3986. enabled */
  3987. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3988. CPU_BASED_CR3_STORE_EXITING |
  3989. CPU_BASED_INVLPG_EXITING);
  3990. } else if (vmx_capability.ept) {
  3991. vmx_capability.ept = 0;
  3992. pr_warn_once("EPT CAP should not exist if not support "
  3993. "1-setting enable EPT VM-execution control\n");
  3994. }
  3995. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3996. vmx_capability.vpid) {
  3997. vmx_capability.vpid = 0;
  3998. pr_warn_once("VPID CAP should not exist if not support "
  3999. "1-setting enable VPID VM-execution control\n");
  4000. }
  4001. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  4002. #ifdef CONFIG_X86_64
  4003. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  4004. #endif
  4005. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  4006. VM_EXIT_CLEAR_BNDCFGS;
  4007. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  4008. &_vmexit_control) < 0)
  4009. return -EIO;
  4010. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  4011. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  4012. PIN_BASED_VMX_PREEMPTION_TIMER;
  4013. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  4014. &_pin_based_exec_control) < 0)
  4015. return -EIO;
  4016. if (cpu_has_broken_vmx_preemption_timer())
  4017. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  4018. if (!(_cpu_based_2nd_exec_control &
  4019. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  4020. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  4021. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  4022. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  4023. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  4024. &_vmentry_control) < 0)
  4025. return -EIO;
  4026. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  4027. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  4028. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  4029. return -EIO;
  4030. #ifdef CONFIG_X86_64
  4031. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  4032. if (vmx_msr_high & (1u<<16))
  4033. return -EIO;
  4034. #endif
  4035. /* Require Write-Back (WB) memory type for VMCS accesses. */
  4036. if (((vmx_msr_high >> 18) & 15) != 6)
  4037. return -EIO;
  4038. vmcs_conf->size = vmx_msr_high & 0x1fff;
  4039. vmcs_conf->order = get_order(vmcs_conf->size);
  4040. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  4041. vmcs_conf->revision_id = vmx_msr_low;
  4042. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  4043. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  4044. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  4045. vmcs_conf->vmexit_ctrl = _vmexit_control;
  4046. vmcs_conf->vmentry_ctrl = _vmentry_control;
  4047. if (static_branch_unlikely(&enable_evmcs))
  4048. evmcs_sanitize_exec_ctrls(vmcs_conf);
  4049. cpu_has_load_ia32_efer =
  4050. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4051. VM_ENTRY_LOAD_IA32_EFER)
  4052. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4053. VM_EXIT_LOAD_IA32_EFER);
  4054. cpu_has_load_perf_global_ctrl =
  4055. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4056. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  4057. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4058. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  4059. /*
  4060. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  4061. * but due to errata below it can't be used. Workaround is to use
  4062. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  4063. *
  4064. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  4065. *
  4066. * AAK155 (model 26)
  4067. * AAP115 (model 30)
  4068. * AAT100 (model 37)
  4069. * BC86,AAY89,BD102 (model 44)
  4070. * BA97 (model 46)
  4071. *
  4072. */
  4073. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  4074. switch (boot_cpu_data.x86_model) {
  4075. case 26:
  4076. case 30:
  4077. case 37:
  4078. case 44:
  4079. case 46:
  4080. cpu_has_load_perf_global_ctrl = false;
  4081. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  4082. "does not work properly. Using workaround\n");
  4083. break;
  4084. default:
  4085. break;
  4086. }
  4087. }
  4088. if (boot_cpu_has(X86_FEATURE_XSAVES))
  4089. rdmsrl(MSR_IA32_XSS, host_xss);
  4090. return 0;
  4091. }
  4092. static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
  4093. {
  4094. int node = cpu_to_node(cpu);
  4095. struct page *pages;
  4096. struct vmcs *vmcs;
  4097. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  4098. if (!pages)
  4099. return NULL;
  4100. vmcs = page_address(pages);
  4101. memset(vmcs, 0, vmcs_config.size);
  4102. /* KVM supports Enlightened VMCS v1 only */
  4103. if (static_branch_unlikely(&enable_evmcs))
  4104. vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
  4105. else
  4106. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4107. if (shadow)
  4108. vmcs->hdr.shadow_vmcs = 1;
  4109. return vmcs;
  4110. }
  4111. static void free_vmcs(struct vmcs *vmcs)
  4112. {
  4113. free_pages((unsigned long)vmcs, vmcs_config.order);
  4114. }
  4115. /*
  4116. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  4117. */
  4118. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4119. {
  4120. if (!loaded_vmcs->vmcs)
  4121. return;
  4122. loaded_vmcs_clear(loaded_vmcs);
  4123. free_vmcs(loaded_vmcs->vmcs);
  4124. loaded_vmcs->vmcs = NULL;
  4125. if (loaded_vmcs->msr_bitmap)
  4126. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  4127. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  4128. }
  4129. static struct vmcs *alloc_vmcs(bool shadow)
  4130. {
  4131. return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
  4132. }
  4133. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4134. {
  4135. loaded_vmcs->vmcs = alloc_vmcs(false);
  4136. if (!loaded_vmcs->vmcs)
  4137. return -ENOMEM;
  4138. loaded_vmcs->shadow_vmcs = NULL;
  4139. loaded_vmcs_init(loaded_vmcs);
  4140. if (cpu_has_vmx_msr_bitmap()) {
  4141. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  4142. if (!loaded_vmcs->msr_bitmap)
  4143. goto out_vmcs;
  4144. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  4145. if (IS_ENABLED(CONFIG_HYPERV) &&
  4146. static_branch_unlikely(&enable_evmcs) &&
  4147. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  4148. struct hv_enlightened_vmcs *evmcs =
  4149. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  4150. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  4151. }
  4152. }
  4153. memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
  4154. return 0;
  4155. out_vmcs:
  4156. free_loaded_vmcs(loaded_vmcs);
  4157. return -ENOMEM;
  4158. }
  4159. static void free_kvm_area(void)
  4160. {
  4161. int cpu;
  4162. for_each_possible_cpu(cpu) {
  4163. free_vmcs(per_cpu(vmxarea, cpu));
  4164. per_cpu(vmxarea, cpu) = NULL;
  4165. }
  4166. }
  4167. enum vmcs_field_width {
  4168. VMCS_FIELD_WIDTH_U16 = 0,
  4169. VMCS_FIELD_WIDTH_U64 = 1,
  4170. VMCS_FIELD_WIDTH_U32 = 2,
  4171. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  4172. };
  4173. static inline int vmcs_field_width(unsigned long field)
  4174. {
  4175. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4176. return VMCS_FIELD_WIDTH_U32;
  4177. return (field >> 13) & 0x3 ;
  4178. }
  4179. static inline int vmcs_field_readonly(unsigned long field)
  4180. {
  4181. return (((field >> 10) & 0x3) == 1);
  4182. }
  4183. static void init_vmcs_shadow_fields(void)
  4184. {
  4185. int i, j;
  4186. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  4187. u16 field = shadow_read_only_fields[i];
  4188. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4189. (i + 1 == max_shadow_read_only_fields ||
  4190. shadow_read_only_fields[i + 1] != field + 1))
  4191. pr_err("Missing field from shadow_read_only_field %x\n",
  4192. field + 1);
  4193. clear_bit(field, vmx_vmread_bitmap);
  4194. #ifdef CONFIG_X86_64
  4195. if (field & 1)
  4196. continue;
  4197. #endif
  4198. if (j < i)
  4199. shadow_read_only_fields[j] = field;
  4200. j++;
  4201. }
  4202. max_shadow_read_only_fields = j;
  4203. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  4204. u16 field = shadow_read_write_fields[i];
  4205. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4206. (i + 1 == max_shadow_read_write_fields ||
  4207. shadow_read_write_fields[i + 1] != field + 1))
  4208. pr_err("Missing field from shadow_read_write_field %x\n",
  4209. field + 1);
  4210. /*
  4211. * PML and the preemption timer can be emulated, but the
  4212. * processor cannot vmwrite to fields that don't exist
  4213. * on bare metal.
  4214. */
  4215. switch (field) {
  4216. case GUEST_PML_INDEX:
  4217. if (!cpu_has_vmx_pml())
  4218. continue;
  4219. break;
  4220. case VMX_PREEMPTION_TIMER_VALUE:
  4221. if (!cpu_has_vmx_preemption_timer())
  4222. continue;
  4223. break;
  4224. case GUEST_INTR_STATUS:
  4225. if (!cpu_has_vmx_apicv())
  4226. continue;
  4227. break;
  4228. default:
  4229. break;
  4230. }
  4231. clear_bit(field, vmx_vmwrite_bitmap);
  4232. clear_bit(field, vmx_vmread_bitmap);
  4233. #ifdef CONFIG_X86_64
  4234. if (field & 1)
  4235. continue;
  4236. #endif
  4237. if (j < i)
  4238. shadow_read_write_fields[j] = field;
  4239. j++;
  4240. }
  4241. max_shadow_read_write_fields = j;
  4242. }
  4243. static __init int alloc_kvm_area(void)
  4244. {
  4245. int cpu;
  4246. for_each_possible_cpu(cpu) {
  4247. struct vmcs *vmcs;
  4248. vmcs = alloc_vmcs_cpu(false, cpu);
  4249. if (!vmcs) {
  4250. free_kvm_area();
  4251. return -ENOMEM;
  4252. }
  4253. /*
  4254. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  4255. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  4256. * revision_id reported by MSR_IA32_VMX_BASIC.
  4257. *
  4258. * However, even though not explictly documented by
  4259. * TLFS, VMXArea passed as VMXON argument should
  4260. * still be marked with revision_id reported by
  4261. * physical CPU.
  4262. */
  4263. if (static_branch_unlikely(&enable_evmcs))
  4264. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4265. per_cpu(vmxarea, cpu) = vmcs;
  4266. }
  4267. return 0;
  4268. }
  4269. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4270. struct kvm_segment *save)
  4271. {
  4272. if (!emulate_invalid_guest_state) {
  4273. /*
  4274. * CS and SS RPL should be equal during guest entry according
  4275. * to VMX spec, but in reality it is not always so. Since vcpu
  4276. * is in the middle of the transition from real mode to
  4277. * protected mode it is safe to assume that RPL 0 is a good
  4278. * default value.
  4279. */
  4280. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4281. save->selector &= ~SEGMENT_RPL_MASK;
  4282. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4283. save->s = 1;
  4284. }
  4285. vmx_set_segment(vcpu, save, seg);
  4286. }
  4287. static void enter_pmode(struct kvm_vcpu *vcpu)
  4288. {
  4289. unsigned long flags;
  4290. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4291. /*
  4292. * Update real mode segment cache. It may be not up-to-date if sement
  4293. * register was written while vcpu was in a guest mode.
  4294. */
  4295. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4296. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4297. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4298. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4299. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4300. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4301. vmx->rmode.vm86_active = 0;
  4302. vmx_segment_cache_clear(vmx);
  4303. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4304. flags = vmcs_readl(GUEST_RFLAGS);
  4305. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4306. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4307. vmcs_writel(GUEST_RFLAGS, flags);
  4308. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4309. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4310. update_exception_bitmap(vcpu);
  4311. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4312. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4313. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4314. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4315. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4316. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4317. }
  4318. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4319. {
  4320. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4321. struct kvm_segment var = *save;
  4322. var.dpl = 0x3;
  4323. if (seg == VCPU_SREG_CS)
  4324. var.type = 0x3;
  4325. if (!emulate_invalid_guest_state) {
  4326. var.selector = var.base >> 4;
  4327. var.base = var.base & 0xffff0;
  4328. var.limit = 0xffff;
  4329. var.g = 0;
  4330. var.db = 0;
  4331. var.present = 1;
  4332. var.s = 1;
  4333. var.l = 0;
  4334. var.unusable = 0;
  4335. var.type = 0x3;
  4336. var.avl = 0;
  4337. if (save->base & 0xf)
  4338. printk_once(KERN_WARNING "kvm: segment base is not "
  4339. "paragraph aligned when entering "
  4340. "protected mode (seg=%d)", seg);
  4341. }
  4342. vmcs_write16(sf->selector, var.selector);
  4343. vmcs_writel(sf->base, var.base);
  4344. vmcs_write32(sf->limit, var.limit);
  4345. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4346. }
  4347. static void enter_rmode(struct kvm_vcpu *vcpu)
  4348. {
  4349. unsigned long flags;
  4350. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4351. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4352. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4353. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4354. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4355. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4356. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4357. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4358. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4359. vmx->rmode.vm86_active = 1;
  4360. /*
  4361. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4362. * vcpu. Warn the user that an update is overdue.
  4363. */
  4364. if (!kvm_vmx->tss_addr)
  4365. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4366. "called before entering vcpu\n");
  4367. vmx_segment_cache_clear(vmx);
  4368. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4369. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4370. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4371. flags = vmcs_readl(GUEST_RFLAGS);
  4372. vmx->rmode.save_rflags = flags;
  4373. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4374. vmcs_writel(GUEST_RFLAGS, flags);
  4375. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4376. update_exception_bitmap(vcpu);
  4377. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4378. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4379. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4380. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4381. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4382. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4383. kvm_mmu_reset_context(vcpu);
  4384. }
  4385. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4386. {
  4387. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4388. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4389. if (!msr)
  4390. return;
  4391. /*
  4392. * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
  4393. * 64-bit mode as a 64-bit kernel may frequently access the
  4394. * MSR. This means we need to manually save/restore the MSR
  4395. * when switching between guest and host state, but only if
  4396. * the guest is in 64-bit mode. Sync our cached value if the
  4397. * guest is transitioning to 32-bit mode and the CPU contains
  4398. * guest state, i.e. the cache is stale.
  4399. */
  4400. #ifdef CONFIG_X86_64
  4401. if (!(efer & EFER_LMA))
  4402. (void)vmx_read_guest_kernel_gs_base(vmx);
  4403. #endif
  4404. vcpu->arch.efer = efer;
  4405. if (efer & EFER_LMA) {
  4406. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4407. msr->data = efer;
  4408. } else {
  4409. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4410. msr->data = efer & ~EFER_LME;
  4411. }
  4412. setup_msrs(vmx);
  4413. }
  4414. #ifdef CONFIG_X86_64
  4415. static void enter_lmode(struct kvm_vcpu *vcpu)
  4416. {
  4417. u32 guest_tr_ar;
  4418. vmx_segment_cache_clear(to_vmx(vcpu));
  4419. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4420. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4421. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4422. __func__);
  4423. vmcs_write32(GUEST_TR_AR_BYTES,
  4424. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4425. | VMX_AR_TYPE_BUSY_64_TSS);
  4426. }
  4427. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4428. }
  4429. static void exit_lmode(struct kvm_vcpu *vcpu)
  4430. {
  4431. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4432. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4433. }
  4434. #endif
  4435. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4436. bool invalidate_gpa)
  4437. {
  4438. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4439. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  4440. return;
  4441. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  4442. } else {
  4443. vpid_sync_context(vpid);
  4444. }
  4445. }
  4446. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4447. {
  4448. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4449. }
  4450. static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
  4451. {
  4452. int vpid = to_vmx(vcpu)->vpid;
  4453. if (!vpid_sync_vcpu_addr(vpid, addr))
  4454. vpid_sync_context(vpid);
  4455. /*
  4456. * If VPIDs are not supported or enabled, then the above is a no-op.
  4457. * But we don't really need a TLB flush in that case anyway, because
  4458. * each VM entry/exit includes an implicit flush when VPID is 0.
  4459. */
  4460. }
  4461. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4462. {
  4463. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4464. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4465. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4466. }
  4467. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4468. {
  4469. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4470. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4471. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4472. }
  4473. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4474. {
  4475. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4476. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4477. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4478. }
  4479. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4480. {
  4481. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4482. if (!test_bit(VCPU_EXREG_PDPTR,
  4483. (unsigned long *)&vcpu->arch.regs_dirty))
  4484. return;
  4485. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4486. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4487. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4488. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4489. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4490. }
  4491. }
  4492. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4493. {
  4494. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4495. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4496. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4497. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4498. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4499. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4500. }
  4501. __set_bit(VCPU_EXREG_PDPTR,
  4502. (unsigned long *)&vcpu->arch.regs_avail);
  4503. __set_bit(VCPU_EXREG_PDPTR,
  4504. (unsigned long *)&vcpu->arch.regs_dirty);
  4505. }
  4506. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4507. {
  4508. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4509. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4510. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4511. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4512. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4513. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4514. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4515. return fixed_bits_valid(val, fixed0, fixed1);
  4516. }
  4517. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4518. {
  4519. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4520. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4521. return fixed_bits_valid(val, fixed0, fixed1);
  4522. }
  4523. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4524. {
  4525. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4526. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4527. return fixed_bits_valid(val, fixed0, fixed1);
  4528. }
  4529. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4530. #define nested_guest_cr4_valid nested_cr4_valid
  4531. #define nested_host_cr4_valid nested_cr4_valid
  4532. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4533. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4534. unsigned long cr0,
  4535. struct kvm_vcpu *vcpu)
  4536. {
  4537. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4538. vmx_decache_cr3(vcpu);
  4539. if (!(cr0 & X86_CR0_PG)) {
  4540. /* From paging/starting to nonpaging */
  4541. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4542. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4543. (CPU_BASED_CR3_LOAD_EXITING |
  4544. CPU_BASED_CR3_STORE_EXITING));
  4545. vcpu->arch.cr0 = cr0;
  4546. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4547. } else if (!is_paging(vcpu)) {
  4548. /* From nonpaging to paging */
  4549. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4550. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4551. ~(CPU_BASED_CR3_LOAD_EXITING |
  4552. CPU_BASED_CR3_STORE_EXITING));
  4553. vcpu->arch.cr0 = cr0;
  4554. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4555. }
  4556. if (!(cr0 & X86_CR0_WP))
  4557. *hw_cr0 &= ~X86_CR0_WP;
  4558. }
  4559. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4560. {
  4561. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4562. unsigned long hw_cr0;
  4563. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  4564. if (enable_unrestricted_guest)
  4565. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4566. else {
  4567. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4568. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4569. enter_pmode(vcpu);
  4570. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4571. enter_rmode(vcpu);
  4572. }
  4573. #ifdef CONFIG_X86_64
  4574. if (vcpu->arch.efer & EFER_LME) {
  4575. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4576. enter_lmode(vcpu);
  4577. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4578. exit_lmode(vcpu);
  4579. }
  4580. #endif
  4581. if (enable_ept && !enable_unrestricted_guest)
  4582. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4583. vmcs_writel(CR0_READ_SHADOW, cr0);
  4584. vmcs_writel(GUEST_CR0, hw_cr0);
  4585. vcpu->arch.cr0 = cr0;
  4586. /* depends on vcpu->arch.cr0 to be set to a new value */
  4587. vmx->emulation_required = emulation_required(vcpu);
  4588. }
  4589. static int get_ept_level(struct kvm_vcpu *vcpu)
  4590. {
  4591. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4592. return 5;
  4593. return 4;
  4594. }
  4595. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4596. {
  4597. u64 eptp = VMX_EPTP_MT_WB;
  4598. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4599. if (enable_ept_ad_bits &&
  4600. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4601. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4602. eptp |= (root_hpa & PAGE_MASK);
  4603. return eptp;
  4604. }
  4605. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4606. {
  4607. struct kvm *kvm = vcpu->kvm;
  4608. unsigned long guest_cr3;
  4609. u64 eptp;
  4610. guest_cr3 = cr3;
  4611. if (enable_ept) {
  4612. eptp = construct_eptp(vcpu, cr3);
  4613. vmcs_write64(EPT_POINTER, eptp);
  4614. if (kvm_x86_ops->tlb_remote_flush) {
  4615. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4616. to_vmx(vcpu)->ept_pointer = eptp;
  4617. to_kvm_vmx(kvm)->ept_pointers_match
  4618. = EPT_POINTERS_CHECK;
  4619. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4620. }
  4621. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4622. is_guest_mode(vcpu))
  4623. guest_cr3 = kvm_read_cr3(vcpu);
  4624. else
  4625. guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
  4626. ept_load_pdptrs(vcpu);
  4627. }
  4628. vmcs_writel(GUEST_CR3, guest_cr3);
  4629. }
  4630. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4631. {
  4632. /*
  4633. * Pass through host's Machine Check Enable value to hw_cr4, which
  4634. * is in force while we are in guest mode. Do not let guests control
  4635. * this bit, even if host CR4.MCE == 0.
  4636. */
  4637. unsigned long hw_cr4;
  4638. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4639. if (enable_unrestricted_guest)
  4640. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4641. else if (to_vmx(vcpu)->rmode.vm86_active)
  4642. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4643. else
  4644. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4645. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4646. if (cr4 & X86_CR4_UMIP) {
  4647. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4648. SECONDARY_EXEC_DESC);
  4649. hw_cr4 &= ~X86_CR4_UMIP;
  4650. } else if (!is_guest_mode(vcpu) ||
  4651. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4652. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4653. SECONDARY_EXEC_DESC);
  4654. }
  4655. if (cr4 & X86_CR4_VMXE) {
  4656. /*
  4657. * To use VMXON (and later other VMX instructions), a guest
  4658. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4659. * So basically the check on whether to allow nested VMX
  4660. * is here.
  4661. */
  4662. if (!nested_vmx_allowed(vcpu))
  4663. return 1;
  4664. }
  4665. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4666. return 1;
  4667. vcpu->arch.cr4 = cr4;
  4668. if (!enable_unrestricted_guest) {
  4669. if (enable_ept) {
  4670. if (!is_paging(vcpu)) {
  4671. hw_cr4 &= ~X86_CR4_PAE;
  4672. hw_cr4 |= X86_CR4_PSE;
  4673. } else if (!(cr4 & X86_CR4_PAE)) {
  4674. hw_cr4 &= ~X86_CR4_PAE;
  4675. }
  4676. }
  4677. /*
  4678. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4679. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4680. * to be manually disabled when guest switches to non-paging
  4681. * mode.
  4682. *
  4683. * If !enable_unrestricted_guest, the CPU is always running
  4684. * with CR0.PG=1 and CR4 needs to be modified.
  4685. * If enable_unrestricted_guest, the CPU automatically
  4686. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4687. */
  4688. if (!is_paging(vcpu))
  4689. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4690. }
  4691. vmcs_writel(CR4_READ_SHADOW, cr4);
  4692. vmcs_writel(GUEST_CR4, hw_cr4);
  4693. return 0;
  4694. }
  4695. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4696. struct kvm_segment *var, int seg)
  4697. {
  4698. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4699. u32 ar;
  4700. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4701. *var = vmx->rmode.segs[seg];
  4702. if (seg == VCPU_SREG_TR
  4703. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4704. return;
  4705. var->base = vmx_read_guest_seg_base(vmx, seg);
  4706. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4707. return;
  4708. }
  4709. var->base = vmx_read_guest_seg_base(vmx, seg);
  4710. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4711. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4712. ar = vmx_read_guest_seg_ar(vmx, seg);
  4713. var->unusable = (ar >> 16) & 1;
  4714. var->type = ar & 15;
  4715. var->s = (ar >> 4) & 1;
  4716. var->dpl = (ar >> 5) & 3;
  4717. /*
  4718. * Some userspaces do not preserve unusable property. Since usable
  4719. * segment has to be present according to VMX spec we can use present
  4720. * property to amend userspace bug by making unusable segment always
  4721. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4722. * segment as unusable.
  4723. */
  4724. var->present = !var->unusable;
  4725. var->avl = (ar >> 12) & 1;
  4726. var->l = (ar >> 13) & 1;
  4727. var->db = (ar >> 14) & 1;
  4728. var->g = (ar >> 15) & 1;
  4729. }
  4730. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4731. {
  4732. struct kvm_segment s;
  4733. if (to_vmx(vcpu)->rmode.vm86_active) {
  4734. vmx_get_segment(vcpu, &s, seg);
  4735. return s.base;
  4736. }
  4737. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4738. }
  4739. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4740. {
  4741. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4742. if (unlikely(vmx->rmode.vm86_active))
  4743. return 0;
  4744. else {
  4745. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4746. return VMX_AR_DPL(ar);
  4747. }
  4748. }
  4749. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4750. {
  4751. u32 ar;
  4752. if (var->unusable || !var->present)
  4753. ar = 1 << 16;
  4754. else {
  4755. ar = var->type & 15;
  4756. ar |= (var->s & 1) << 4;
  4757. ar |= (var->dpl & 3) << 5;
  4758. ar |= (var->present & 1) << 7;
  4759. ar |= (var->avl & 1) << 12;
  4760. ar |= (var->l & 1) << 13;
  4761. ar |= (var->db & 1) << 14;
  4762. ar |= (var->g & 1) << 15;
  4763. }
  4764. return ar;
  4765. }
  4766. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4767. struct kvm_segment *var, int seg)
  4768. {
  4769. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4770. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4771. vmx_segment_cache_clear(vmx);
  4772. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4773. vmx->rmode.segs[seg] = *var;
  4774. if (seg == VCPU_SREG_TR)
  4775. vmcs_write16(sf->selector, var->selector);
  4776. else if (var->s)
  4777. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4778. goto out;
  4779. }
  4780. vmcs_writel(sf->base, var->base);
  4781. vmcs_write32(sf->limit, var->limit);
  4782. vmcs_write16(sf->selector, var->selector);
  4783. /*
  4784. * Fix the "Accessed" bit in AR field of segment registers for older
  4785. * qemu binaries.
  4786. * IA32 arch specifies that at the time of processor reset the
  4787. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4788. * is setting it to 0 in the userland code. This causes invalid guest
  4789. * state vmexit when "unrestricted guest" mode is turned on.
  4790. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4791. * tree. Newer qemu binaries with that qemu fix would not need this
  4792. * kvm hack.
  4793. */
  4794. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4795. var->type |= 0x1; /* Accessed */
  4796. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4797. out:
  4798. vmx->emulation_required = emulation_required(vcpu);
  4799. }
  4800. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4801. {
  4802. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4803. *db = (ar >> 14) & 1;
  4804. *l = (ar >> 13) & 1;
  4805. }
  4806. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4807. {
  4808. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4809. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4810. }
  4811. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4812. {
  4813. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4814. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4815. }
  4816. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4817. {
  4818. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4819. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4820. }
  4821. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4822. {
  4823. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4824. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4825. }
  4826. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4827. {
  4828. struct kvm_segment var;
  4829. u32 ar;
  4830. vmx_get_segment(vcpu, &var, seg);
  4831. var.dpl = 0x3;
  4832. if (seg == VCPU_SREG_CS)
  4833. var.type = 0x3;
  4834. ar = vmx_segment_access_rights(&var);
  4835. if (var.base != (var.selector << 4))
  4836. return false;
  4837. if (var.limit != 0xffff)
  4838. return false;
  4839. if (ar != 0xf3)
  4840. return false;
  4841. return true;
  4842. }
  4843. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4844. {
  4845. struct kvm_segment cs;
  4846. unsigned int cs_rpl;
  4847. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4848. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4849. if (cs.unusable)
  4850. return false;
  4851. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4852. return false;
  4853. if (!cs.s)
  4854. return false;
  4855. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4856. if (cs.dpl > cs_rpl)
  4857. return false;
  4858. } else {
  4859. if (cs.dpl != cs_rpl)
  4860. return false;
  4861. }
  4862. if (!cs.present)
  4863. return false;
  4864. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4865. return true;
  4866. }
  4867. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4868. {
  4869. struct kvm_segment ss;
  4870. unsigned int ss_rpl;
  4871. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4872. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4873. if (ss.unusable)
  4874. return true;
  4875. if (ss.type != 3 && ss.type != 7)
  4876. return false;
  4877. if (!ss.s)
  4878. return false;
  4879. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4880. return false;
  4881. if (!ss.present)
  4882. return false;
  4883. return true;
  4884. }
  4885. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4886. {
  4887. struct kvm_segment var;
  4888. unsigned int rpl;
  4889. vmx_get_segment(vcpu, &var, seg);
  4890. rpl = var.selector & SEGMENT_RPL_MASK;
  4891. if (var.unusable)
  4892. return true;
  4893. if (!var.s)
  4894. return false;
  4895. if (!var.present)
  4896. return false;
  4897. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4898. if (var.dpl < rpl) /* DPL < RPL */
  4899. return false;
  4900. }
  4901. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4902. * rights flags
  4903. */
  4904. return true;
  4905. }
  4906. static bool tr_valid(struct kvm_vcpu *vcpu)
  4907. {
  4908. struct kvm_segment tr;
  4909. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4910. if (tr.unusable)
  4911. return false;
  4912. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4913. return false;
  4914. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4915. return false;
  4916. if (!tr.present)
  4917. return false;
  4918. return true;
  4919. }
  4920. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4921. {
  4922. struct kvm_segment ldtr;
  4923. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4924. if (ldtr.unusable)
  4925. return true;
  4926. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4927. return false;
  4928. if (ldtr.type != 2)
  4929. return false;
  4930. if (!ldtr.present)
  4931. return false;
  4932. return true;
  4933. }
  4934. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4935. {
  4936. struct kvm_segment cs, ss;
  4937. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4938. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4939. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4940. (ss.selector & SEGMENT_RPL_MASK));
  4941. }
  4942. /*
  4943. * Check if guest state is valid. Returns true if valid, false if
  4944. * not.
  4945. * We assume that registers are always usable
  4946. */
  4947. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4948. {
  4949. if (enable_unrestricted_guest)
  4950. return true;
  4951. /* real mode guest state checks */
  4952. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4953. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4954. return false;
  4955. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4956. return false;
  4957. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4958. return false;
  4959. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4960. return false;
  4961. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4962. return false;
  4963. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4964. return false;
  4965. } else {
  4966. /* protected mode guest state checks */
  4967. if (!cs_ss_rpl_check(vcpu))
  4968. return false;
  4969. if (!code_segment_valid(vcpu))
  4970. return false;
  4971. if (!stack_segment_valid(vcpu))
  4972. return false;
  4973. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4974. return false;
  4975. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4976. return false;
  4977. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4978. return false;
  4979. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4980. return false;
  4981. if (!tr_valid(vcpu))
  4982. return false;
  4983. if (!ldtr_valid(vcpu))
  4984. return false;
  4985. }
  4986. /* TODO:
  4987. * - Add checks on RIP
  4988. * - Add checks on RFLAGS
  4989. */
  4990. return true;
  4991. }
  4992. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4993. {
  4994. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4995. }
  4996. static int init_rmode_tss(struct kvm *kvm)
  4997. {
  4998. gfn_t fn;
  4999. u16 data = 0;
  5000. int idx, r;
  5001. idx = srcu_read_lock(&kvm->srcu);
  5002. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  5003. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5004. if (r < 0)
  5005. goto out;
  5006. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  5007. r = kvm_write_guest_page(kvm, fn++, &data,
  5008. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  5009. if (r < 0)
  5010. goto out;
  5011. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  5012. if (r < 0)
  5013. goto out;
  5014. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5015. if (r < 0)
  5016. goto out;
  5017. data = ~0;
  5018. r = kvm_write_guest_page(kvm, fn, &data,
  5019. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  5020. sizeof(u8));
  5021. out:
  5022. srcu_read_unlock(&kvm->srcu, idx);
  5023. return r;
  5024. }
  5025. static int init_rmode_identity_map(struct kvm *kvm)
  5026. {
  5027. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  5028. int i, idx, r = 0;
  5029. kvm_pfn_t identity_map_pfn;
  5030. u32 tmp;
  5031. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  5032. mutex_lock(&kvm->slots_lock);
  5033. if (likely(kvm_vmx->ept_identity_pagetable_done))
  5034. goto out2;
  5035. if (!kvm_vmx->ept_identity_map_addr)
  5036. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5037. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  5038. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  5039. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  5040. if (r < 0)
  5041. goto out2;
  5042. idx = srcu_read_lock(&kvm->srcu);
  5043. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  5044. if (r < 0)
  5045. goto out;
  5046. /* Set up identity-mapping pagetable for EPT in real mode */
  5047. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  5048. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  5049. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  5050. r = kvm_write_guest_page(kvm, identity_map_pfn,
  5051. &tmp, i * sizeof(tmp), sizeof(tmp));
  5052. if (r < 0)
  5053. goto out;
  5054. }
  5055. kvm_vmx->ept_identity_pagetable_done = true;
  5056. out:
  5057. srcu_read_unlock(&kvm->srcu, idx);
  5058. out2:
  5059. mutex_unlock(&kvm->slots_lock);
  5060. return r;
  5061. }
  5062. static void seg_setup(int seg)
  5063. {
  5064. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  5065. unsigned int ar;
  5066. vmcs_write16(sf->selector, 0);
  5067. vmcs_writel(sf->base, 0);
  5068. vmcs_write32(sf->limit, 0xffff);
  5069. ar = 0x93;
  5070. if (seg == VCPU_SREG_CS)
  5071. ar |= 0x08; /* code segment */
  5072. vmcs_write32(sf->ar_bytes, ar);
  5073. }
  5074. static int alloc_apic_access_page(struct kvm *kvm)
  5075. {
  5076. struct page *page;
  5077. int r = 0;
  5078. mutex_lock(&kvm->slots_lock);
  5079. if (kvm->arch.apic_access_page_done)
  5080. goto out;
  5081. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  5082. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  5083. if (r)
  5084. goto out;
  5085. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5086. if (is_error_page(page)) {
  5087. r = -EFAULT;
  5088. goto out;
  5089. }
  5090. /*
  5091. * Do not pin the page in memory, so that memory hot-unplug
  5092. * is able to migrate it.
  5093. */
  5094. put_page(page);
  5095. kvm->arch.apic_access_page_done = true;
  5096. out:
  5097. mutex_unlock(&kvm->slots_lock);
  5098. return r;
  5099. }
  5100. static int allocate_vpid(void)
  5101. {
  5102. int vpid;
  5103. if (!enable_vpid)
  5104. return 0;
  5105. spin_lock(&vmx_vpid_lock);
  5106. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  5107. if (vpid < VMX_NR_VPIDS)
  5108. __set_bit(vpid, vmx_vpid_bitmap);
  5109. else
  5110. vpid = 0;
  5111. spin_unlock(&vmx_vpid_lock);
  5112. return vpid;
  5113. }
  5114. static void free_vpid(int vpid)
  5115. {
  5116. if (!enable_vpid || vpid == 0)
  5117. return;
  5118. spin_lock(&vmx_vpid_lock);
  5119. __clear_bit(vpid, vmx_vpid_bitmap);
  5120. spin_unlock(&vmx_vpid_lock);
  5121. }
  5122. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  5123. u32 msr, int type)
  5124. {
  5125. int f = sizeof(unsigned long);
  5126. if (!cpu_has_vmx_msr_bitmap())
  5127. return;
  5128. if (static_branch_unlikely(&enable_evmcs))
  5129. evmcs_touch_msr_bitmap();
  5130. /*
  5131. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5132. * have the write-low and read-high bitmap offsets the wrong way round.
  5133. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5134. */
  5135. if (msr <= 0x1fff) {
  5136. if (type & MSR_TYPE_R)
  5137. /* read-low */
  5138. __clear_bit(msr, msr_bitmap + 0x000 / f);
  5139. if (type & MSR_TYPE_W)
  5140. /* write-low */
  5141. __clear_bit(msr, msr_bitmap + 0x800 / f);
  5142. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5143. msr &= 0x1fff;
  5144. if (type & MSR_TYPE_R)
  5145. /* read-high */
  5146. __clear_bit(msr, msr_bitmap + 0x400 / f);
  5147. if (type & MSR_TYPE_W)
  5148. /* write-high */
  5149. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  5150. }
  5151. }
  5152. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  5153. u32 msr, int type)
  5154. {
  5155. int f = sizeof(unsigned long);
  5156. if (!cpu_has_vmx_msr_bitmap())
  5157. return;
  5158. if (static_branch_unlikely(&enable_evmcs))
  5159. evmcs_touch_msr_bitmap();
  5160. /*
  5161. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5162. * have the write-low and read-high bitmap offsets the wrong way round.
  5163. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5164. */
  5165. if (msr <= 0x1fff) {
  5166. if (type & MSR_TYPE_R)
  5167. /* read-low */
  5168. __set_bit(msr, msr_bitmap + 0x000 / f);
  5169. if (type & MSR_TYPE_W)
  5170. /* write-low */
  5171. __set_bit(msr, msr_bitmap + 0x800 / f);
  5172. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5173. msr &= 0x1fff;
  5174. if (type & MSR_TYPE_R)
  5175. /* read-high */
  5176. __set_bit(msr, msr_bitmap + 0x400 / f);
  5177. if (type & MSR_TYPE_W)
  5178. /* write-high */
  5179. __set_bit(msr, msr_bitmap + 0xc00 / f);
  5180. }
  5181. }
  5182. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  5183. u32 msr, int type, bool value)
  5184. {
  5185. if (value)
  5186. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  5187. else
  5188. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  5189. }
  5190. /*
  5191. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  5192. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  5193. */
  5194. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  5195. unsigned long *msr_bitmap_nested,
  5196. u32 msr, int type)
  5197. {
  5198. int f = sizeof(unsigned long);
  5199. /*
  5200. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5201. * have the write-low and read-high bitmap offsets the wrong way round.
  5202. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5203. */
  5204. if (msr <= 0x1fff) {
  5205. if (type & MSR_TYPE_R &&
  5206. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  5207. /* read-low */
  5208. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  5209. if (type & MSR_TYPE_W &&
  5210. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  5211. /* write-low */
  5212. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  5213. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5214. msr &= 0x1fff;
  5215. if (type & MSR_TYPE_R &&
  5216. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  5217. /* read-high */
  5218. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  5219. if (type & MSR_TYPE_W &&
  5220. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  5221. /* write-high */
  5222. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  5223. }
  5224. }
  5225. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  5226. {
  5227. u8 mode = 0;
  5228. if (cpu_has_secondary_exec_ctrls() &&
  5229. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  5230. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  5231. mode |= MSR_BITMAP_MODE_X2APIC;
  5232. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  5233. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  5234. }
  5235. if (is_long_mode(vcpu))
  5236. mode |= MSR_BITMAP_MODE_LM;
  5237. return mode;
  5238. }
  5239. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  5240. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  5241. u8 mode)
  5242. {
  5243. int msr;
  5244. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  5245. unsigned word = msr / BITS_PER_LONG;
  5246. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  5247. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  5248. }
  5249. if (mode & MSR_BITMAP_MODE_X2APIC) {
  5250. /*
  5251. * TPR reads and writes can be virtualized even if virtual interrupt
  5252. * delivery is not in use.
  5253. */
  5254. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  5255. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  5256. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  5257. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  5258. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  5259. }
  5260. }
  5261. }
  5262. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  5263. {
  5264. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5265. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  5266. u8 mode = vmx_msr_bitmap_mode(vcpu);
  5267. u8 changed = mode ^ vmx->msr_bitmap_mode;
  5268. if (!changed)
  5269. return;
  5270. vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
  5271. !(mode & MSR_BITMAP_MODE_LM));
  5272. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  5273. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  5274. vmx->msr_bitmap_mode = mode;
  5275. }
  5276. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  5277. {
  5278. return enable_apicv;
  5279. }
  5280. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  5281. {
  5282. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5283. gfn_t gfn;
  5284. /*
  5285. * Don't need to mark the APIC access page dirty; it is never
  5286. * written to by the CPU during APIC virtualization.
  5287. */
  5288. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  5289. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  5290. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5291. }
  5292. if (nested_cpu_has_posted_intr(vmcs12)) {
  5293. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5294. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5295. }
  5296. }
  5297. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5298. {
  5299. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5300. int max_irr;
  5301. void *vapic_page;
  5302. u16 status;
  5303. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5304. return;
  5305. vmx->nested.pi_pending = false;
  5306. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5307. return;
  5308. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5309. if (max_irr != 256) {
  5310. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5311. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5312. vapic_page, &max_irr);
  5313. kunmap(vmx->nested.virtual_apic_page);
  5314. status = vmcs_read16(GUEST_INTR_STATUS);
  5315. if ((u8)max_irr > ((u8)status & 0xff)) {
  5316. status &= ~0xff;
  5317. status |= (u8)max_irr;
  5318. vmcs_write16(GUEST_INTR_STATUS, status);
  5319. }
  5320. }
  5321. nested_mark_vmcs12_pages_dirty(vcpu);
  5322. }
  5323. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5324. bool nested)
  5325. {
  5326. #ifdef CONFIG_SMP
  5327. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5328. if (vcpu->mode == IN_GUEST_MODE) {
  5329. /*
  5330. * The vector of interrupt to be delivered to vcpu had
  5331. * been set in PIR before this function.
  5332. *
  5333. * Following cases will be reached in this block, and
  5334. * we always send a notification event in all cases as
  5335. * explained below.
  5336. *
  5337. * Case 1: vcpu keeps in non-root mode. Sending a
  5338. * notification event posts the interrupt to vcpu.
  5339. *
  5340. * Case 2: vcpu exits to root mode and is still
  5341. * runnable. PIR will be synced to vIRR before the
  5342. * next vcpu entry. Sending a notification event in
  5343. * this case has no effect, as vcpu is not in root
  5344. * mode.
  5345. *
  5346. * Case 3: vcpu exits to root mode and is blocked.
  5347. * vcpu_block() has already synced PIR to vIRR and
  5348. * never blocks vcpu if vIRR is not cleared. Therefore,
  5349. * a blocked vcpu here does not wait for any requested
  5350. * interrupts in PIR, and sending a notification event
  5351. * which has no effect is safe here.
  5352. */
  5353. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5354. return true;
  5355. }
  5356. #endif
  5357. return false;
  5358. }
  5359. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5360. int vector)
  5361. {
  5362. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5363. if (is_guest_mode(vcpu) &&
  5364. vector == vmx->nested.posted_intr_nv) {
  5365. /*
  5366. * If a posted intr is not recognized by hardware,
  5367. * we will accomplish it in the next vmentry.
  5368. */
  5369. vmx->nested.pi_pending = true;
  5370. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5371. /* the PIR and ON have been set by L1. */
  5372. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5373. kvm_vcpu_kick(vcpu);
  5374. return 0;
  5375. }
  5376. return -1;
  5377. }
  5378. /*
  5379. * Send interrupt to vcpu via posted interrupt way.
  5380. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5381. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5382. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5383. * interrupt from PIR in next vmentry.
  5384. */
  5385. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5386. {
  5387. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5388. int r;
  5389. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5390. if (!r)
  5391. return;
  5392. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5393. return;
  5394. /* If a previous notification has sent the IPI, nothing to do. */
  5395. if (pi_test_and_set_on(&vmx->pi_desc))
  5396. return;
  5397. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5398. kvm_vcpu_kick(vcpu);
  5399. }
  5400. /*
  5401. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5402. * will not change in the lifetime of the guest.
  5403. * Note that host-state that does change is set elsewhere. E.g., host-state
  5404. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5405. */
  5406. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5407. {
  5408. u32 low32, high32;
  5409. unsigned long tmpl;
  5410. struct desc_ptr dt;
  5411. unsigned long cr0, cr3, cr4;
  5412. cr0 = read_cr0();
  5413. WARN_ON(cr0 & X86_CR0_TS);
  5414. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5415. /*
  5416. * Save the most likely value for this task's CR3 in the VMCS.
  5417. * We can't use __get_current_cr3_fast() because we're not atomic.
  5418. */
  5419. cr3 = __read_cr3();
  5420. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5421. vmx->loaded_vmcs->host_state.cr3 = cr3;
  5422. /* Save the most likely value for this task's CR4 in the VMCS. */
  5423. cr4 = cr4_read_shadow();
  5424. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5425. vmx->loaded_vmcs->host_state.cr4 = cr4;
  5426. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5427. #ifdef CONFIG_X86_64
  5428. /*
  5429. * Load null selectors, so we can avoid reloading them in
  5430. * vmx_prepare_switch_to_host(), in case userspace uses
  5431. * the null selectors too (the expected case).
  5432. */
  5433. vmcs_write16(HOST_DS_SELECTOR, 0);
  5434. vmcs_write16(HOST_ES_SELECTOR, 0);
  5435. #else
  5436. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5437. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5438. #endif
  5439. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5440. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5441. store_idt(&dt);
  5442. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5443. vmx->host_idt_base = dt.address;
  5444. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5445. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5446. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5447. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5448. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5449. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5450. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5451. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5452. }
  5453. }
  5454. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5455. {
  5456. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5457. if (enable_ept)
  5458. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5459. if (is_guest_mode(&vmx->vcpu))
  5460. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5461. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5462. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5463. }
  5464. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5465. {
  5466. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5467. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5468. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5469. if (!enable_vnmi)
  5470. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5471. /* Enable the preemption timer dynamically */
  5472. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5473. return pin_based_exec_ctrl;
  5474. }
  5475. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5476. {
  5477. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5478. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5479. if (cpu_has_secondary_exec_ctrls()) {
  5480. if (kvm_vcpu_apicv_active(vcpu))
  5481. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5482. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5483. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5484. else
  5485. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5486. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5487. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5488. }
  5489. if (cpu_has_vmx_msr_bitmap())
  5490. vmx_update_msr_bitmap(vcpu);
  5491. }
  5492. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5493. {
  5494. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5495. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5496. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5497. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5498. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5499. #ifdef CONFIG_X86_64
  5500. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5501. CPU_BASED_CR8_LOAD_EXITING;
  5502. #endif
  5503. }
  5504. if (!enable_ept)
  5505. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5506. CPU_BASED_CR3_LOAD_EXITING |
  5507. CPU_BASED_INVLPG_EXITING;
  5508. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5509. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5510. CPU_BASED_MONITOR_EXITING);
  5511. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5512. exec_control &= ~CPU_BASED_HLT_EXITING;
  5513. return exec_control;
  5514. }
  5515. static bool vmx_rdrand_supported(void)
  5516. {
  5517. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5518. SECONDARY_EXEC_RDRAND_EXITING;
  5519. }
  5520. static bool vmx_rdseed_supported(void)
  5521. {
  5522. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5523. SECONDARY_EXEC_RDSEED_EXITING;
  5524. }
  5525. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5526. {
  5527. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5528. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5529. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5530. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5531. if (vmx->vpid == 0)
  5532. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5533. if (!enable_ept) {
  5534. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5535. enable_unrestricted_guest = 0;
  5536. }
  5537. if (!enable_unrestricted_guest)
  5538. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5539. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5540. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5541. if (!kvm_vcpu_apicv_active(vcpu))
  5542. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5543. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5544. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5545. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5546. * in vmx_set_cr4. */
  5547. exec_control &= ~SECONDARY_EXEC_DESC;
  5548. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5549. (handle_vmptrld).
  5550. We can NOT enable shadow_vmcs here because we don't have yet
  5551. a current VMCS12
  5552. */
  5553. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5554. if (!enable_pml)
  5555. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5556. if (vmx_xsaves_supported()) {
  5557. /* Exposing XSAVES only when XSAVE is exposed */
  5558. bool xsaves_enabled =
  5559. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5560. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5561. if (!xsaves_enabled)
  5562. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5563. if (nested) {
  5564. if (xsaves_enabled)
  5565. vmx->nested.msrs.secondary_ctls_high |=
  5566. SECONDARY_EXEC_XSAVES;
  5567. else
  5568. vmx->nested.msrs.secondary_ctls_high &=
  5569. ~SECONDARY_EXEC_XSAVES;
  5570. }
  5571. }
  5572. if (vmx_rdtscp_supported()) {
  5573. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5574. if (!rdtscp_enabled)
  5575. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5576. if (nested) {
  5577. if (rdtscp_enabled)
  5578. vmx->nested.msrs.secondary_ctls_high |=
  5579. SECONDARY_EXEC_RDTSCP;
  5580. else
  5581. vmx->nested.msrs.secondary_ctls_high &=
  5582. ~SECONDARY_EXEC_RDTSCP;
  5583. }
  5584. }
  5585. if (vmx_invpcid_supported()) {
  5586. /* Exposing INVPCID only when PCID is exposed */
  5587. bool invpcid_enabled =
  5588. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5589. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5590. if (!invpcid_enabled) {
  5591. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5592. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5593. }
  5594. if (nested) {
  5595. if (invpcid_enabled)
  5596. vmx->nested.msrs.secondary_ctls_high |=
  5597. SECONDARY_EXEC_ENABLE_INVPCID;
  5598. else
  5599. vmx->nested.msrs.secondary_ctls_high &=
  5600. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5601. }
  5602. }
  5603. if (vmx_rdrand_supported()) {
  5604. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5605. if (rdrand_enabled)
  5606. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5607. if (nested) {
  5608. if (rdrand_enabled)
  5609. vmx->nested.msrs.secondary_ctls_high |=
  5610. SECONDARY_EXEC_RDRAND_EXITING;
  5611. else
  5612. vmx->nested.msrs.secondary_ctls_high &=
  5613. ~SECONDARY_EXEC_RDRAND_EXITING;
  5614. }
  5615. }
  5616. if (vmx_rdseed_supported()) {
  5617. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5618. if (rdseed_enabled)
  5619. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5620. if (nested) {
  5621. if (rdseed_enabled)
  5622. vmx->nested.msrs.secondary_ctls_high |=
  5623. SECONDARY_EXEC_RDSEED_EXITING;
  5624. else
  5625. vmx->nested.msrs.secondary_ctls_high &=
  5626. ~SECONDARY_EXEC_RDSEED_EXITING;
  5627. }
  5628. }
  5629. vmx->secondary_exec_control = exec_control;
  5630. }
  5631. static void ept_set_mmio_spte_mask(void)
  5632. {
  5633. /*
  5634. * EPT Misconfigurations can be generated if the value of bits 2:0
  5635. * of an EPT paging-structure entry is 110b (write/execute).
  5636. */
  5637. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5638. VMX_EPT_MISCONFIG_WX_VALUE);
  5639. }
  5640. #define VMX_XSS_EXIT_BITMAP 0
  5641. /*
  5642. * Sets up the vmcs for emulated real mode.
  5643. */
  5644. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5645. {
  5646. int i;
  5647. if (enable_shadow_vmcs) {
  5648. /*
  5649. * At vCPU creation, "VMWRITE to any supported field
  5650. * in the VMCS" is supported, so use the more
  5651. * permissive vmx_vmread_bitmap to specify both read
  5652. * and write permissions for the shadow VMCS.
  5653. */
  5654. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5655. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5656. }
  5657. if (cpu_has_vmx_msr_bitmap())
  5658. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5659. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5660. /* Control */
  5661. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5662. vmx->hv_deadline_tsc = -1;
  5663. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5664. if (cpu_has_secondary_exec_ctrls()) {
  5665. vmx_compute_secondary_exec_control(vmx);
  5666. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5667. vmx->secondary_exec_control);
  5668. }
  5669. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5670. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5671. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5672. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5673. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5674. vmcs_write16(GUEST_INTR_STATUS, 0);
  5675. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5676. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5677. }
  5678. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5679. vmcs_write32(PLE_GAP, ple_gap);
  5680. vmx->ple_window = ple_window;
  5681. vmx->ple_window_dirty = true;
  5682. }
  5683. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5684. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5685. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5686. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5687. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5688. vmx_set_constant_host_state(vmx);
  5689. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5690. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5691. if (cpu_has_vmx_vmfunc())
  5692. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5693. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5694. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5695. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  5696. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5697. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  5698. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5699. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5700. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5701. u32 index = vmx_msr_index[i];
  5702. u32 data_low, data_high;
  5703. int j = vmx->nmsrs;
  5704. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5705. continue;
  5706. if (wrmsr_safe(index, data_low, data_high) < 0)
  5707. continue;
  5708. vmx->guest_msrs[j].index = i;
  5709. vmx->guest_msrs[j].data = 0;
  5710. vmx->guest_msrs[j].mask = -1ull;
  5711. ++vmx->nmsrs;
  5712. }
  5713. vmx->arch_capabilities = kvm_get_arch_capabilities();
  5714. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5715. /* 22.2.1, 20.8.1 */
  5716. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5717. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5718. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5719. set_cr4_guest_host_mask(vmx);
  5720. if (vmx_xsaves_supported())
  5721. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5722. if (enable_pml) {
  5723. ASSERT(vmx->pml_pg);
  5724. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5725. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5726. }
  5727. if (cpu_has_vmx_encls_vmexit())
  5728. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  5729. }
  5730. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5731. {
  5732. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5733. struct msr_data apic_base_msr;
  5734. u64 cr0;
  5735. vmx->rmode.vm86_active = 0;
  5736. vmx->spec_ctrl = 0;
  5737. vcpu->arch.microcode_version = 0x100000000ULL;
  5738. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5739. kvm_set_cr8(vcpu, 0);
  5740. if (!init_event) {
  5741. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5742. MSR_IA32_APICBASE_ENABLE;
  5743. if (kvm_vcpu_is_reset_bsp(vcpu))
  5744. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5745. apic_base_msr.host_initiated = true;
  5746. kvm_set_apic_base(vcpu, &apic_base_msr);
  5747. }
  5748. vmx_segment_cache_clear(vmx);
  5749. seg_setup(VCPU_SREG_CS);
  5750. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5751. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5752. seg_setup(VCPU_SREG_DS);
  5753. seg_setup(VCPU_SREG_ES);
  5754. seg_setup(VCPU_SREG_FS);
  5755. seg_setup(VCPU_SREG_GS);
  5756. seg_setup(VCPU_SREG_SS);
  5757. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5758. vmcs_writel(GUEST_TR_BASE, 0);
  5759. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5760. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5761. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5762. vmcs_writel(GUEST_LDTR_BASE, 0);
  5763. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5764. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5765. if (!init_event) {
  5766. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5767. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5768. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5769. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5770. }
  5771. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5772. kvm_rip_write(vcpu, 0xfff0);
  5773. vmcs_writel(GUEST_GDTR_BASE, 0);
  5774. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5775. vmcs_writel(GUEST_IDTR_BASE, 0);
  5776. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5777. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5778. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5779. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5780. if (kvm_mpx_supported())
  5781. vmcs_write64(GUEST_BNDCFGS, 0);
  5782. setup_msrs(vmx);
  5783. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5784. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5785. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5786. if (cpu_need_tpr_shadow(vcpu))
  5787. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5788. __pa(vcpu->arch.apic->regs));
  5789. vmcs_write32(TPR_THRESHOLD, 0);
  5790. }
  5791. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5792. if (vmx->vpid != 0)
  5793. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5794. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5795. vmx->vcpu.arch.cr0 = cr0;
  5796. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5797. vmx_set_cr4(vcpu, 0);
  5798. vmx_set_efer(vcpu, 0);
  5799. update_exception_bitmap(vcpu);
  5800. vpid_sync_context(vmx->vpid);
  5801. if (init_event)
  5802. vmx_clear_hlt(vcpu);
  5803. }
  5804. /*
  5805. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5806. * For most existing hypervisors, this will always return true.
  5807. */
  5808. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5809. {
  5810. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5811. PIN_BASED_EXT_INTR_MASK;
  5812. }
  5813. /*
  5814. * In nested virtualization, check if L1 has set
  5815. * VM_EXIT_ACK_INTR_ON_EXIT
  5816. */
  5817. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5818. {
  5819. return get_vmcs12(vcpu)->vm_exit_controls &
  5820. VM_EXIT_ACK_INTR_ON_EXIT;
  5821. }
  5822. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5823. {
  5824. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5825. }
  5826. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5827. {
  5828. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5829. CPU_BASED_VIRTUAL_INTR_PENDING);
  5830. }
  5831. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5832. {
  5833. if (!enable_vnmi ||
  5834. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5835. enable_irq_window(vcpu);
  5836. return;
  5837. }
  5838. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5839. CPU_BASED_VIRTUAL_NMI_PENDING);
  5840. }
  5841. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5842. {
  5843. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5844. uint32_t intr;
  5845. int irq = vcpu->arch.interrupt.nr;
  5846. trace_kvm_inj_virq(irq);
  5847. ++vcpu->stat.irq_injections;
  5848. if (vmx->rmode.vm86_active) {
  5849. int inc_eip = 0;
  5850. if (vcpu->arch.interrupt.soft)
  5851. inc_eip = vcpu->arch.event_exit_inst_len;
  5852. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5853. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5854. return;
  5855. }
  5856. intr = irq | INTR_INFO_VALID_MASK;
  5857. if (vcpu->arch.interrupt.soft) {
  5858. intr |= INTR_TYPE_SOFT_INTR;
  5859. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5860. vmx->vcpu.arch.event_exit_inst_len);
  5861. } else
  5862. intr |= INTR_TYPE_EXT_INTR;
  5863. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5864. vmx_clear_hlt(vcpu);
  5865. }
  5866. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5867. {
  5868. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5869. if (!enable_vnmi) {
  5870. /*
  5871. * Tracking the NMI-blocked state in software is built upon
  5872. * finding the next open IRQ window. This, in turn, depends on
  5873. * well-behaving guests: They have to keep IRQs disabled at
  5874. * least as long as the NMI handler runs. Otherwise we may
  5875. * cause NMI nesting, maybe breaking the guest. But as this is
  5876. * highly unlikely, we can live with the residual risk.
  5877. */
  5878. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5879. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5880. }
  5881. ++vcpu->stat.nmi_injections;
  5882. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5883. if (vmx->rmode.vm86_active) {
  5884. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5885. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5886. return;
  5887. }
  5888. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5889. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5890. vmx_clear_hlt(vcpu);
  5891. }
  5892. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5893. {
  5894. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5895. bool masked;
  5896. if (!enable_vnmi)
  5897. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5898. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5899. return false;
  5900. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5901. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5902. return masked;
  5903. }
  5904. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5905. {
  5906. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5907. if (!enable_vnmi) {
  5908. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5909. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5910. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5911. }
  5912. } else {
  5913. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5914. if (masked)
  5915. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5916. GUEST_INTR_STATE_NMI);
  5917. else
  5918. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5919. GUEST_INTR_STATE_NMI);
  5920. }
  5921. }
  5922. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5923. {
  5924. if (to_vmx(vcpu)->nested.nested_run_pending)
  5925. return 0;
  5926. if (!enable_vnmi &&
  5927. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5928. return 0;
  5929. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5930. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5931. | GUEST_INTR_STATE_NMI));
  5932. }
  5933. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5934. {
  5935. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5936. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5937. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5938. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5939. }
  5940. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5941. {
  5942. int ret;
  5943. if (enable_unrestricted_guest)
  5944. return 0;
  5945. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5946. PAGE_SIZE * 3);
  5947. if (ret)
  5948. return ret;
  5949. to_kvm_vmx(kvm)->tss_addr = addr;
  5950. return init_rmode_tss(kvm);
  5951. }
  5952. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5953. {
  5954. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5955. return 0;
  5956. }
  5957. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5958. {
  5959. switch (vec) {
  5960. case BP_VECTOR:
  5961. /*
  5962. * Update instruction length as we may reinject the exception
  5963. * from user space while in guest debugging mode.
  5964. */
  5965. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5966. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5967. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5968. return false;
  5969. /* fall through */
  5970. case DB_VECTOR:
  5971. if (vcpu->guest_debug &
  5972. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5973. return false;
  5974. /* fall through */
  5975. case DE_VECTOR:
  5976. case OF_VECTOR:
  5977. case BR_VECTOR:
  5978. case UD_VECTOR:
  5979. case DF_VECTOR:
  5980. case SS_VECTOR:
  5981. case GP_VECTOR:
  5982. case MF_VECTOR:
  5983. return true;
  5984. break;
  5985. }
  5986. return false;
  5987. }
  5988. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5989. int vec, u32 err_code)
  5990. {
  5991. /*
  5992. * Instruction with address size override prefix opcode 0x67
  5993. * Cause the #SS fault with 0 error code in VM86 mode.
  5994. */
  5995. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5996. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5997. if (vcpu->arch.halt_request) {
  5998. vcpu->arch.halt_request = 0;
  5999. return kvm_vcpu_halt(vcpu);
  6000. }
  6001. return 1;
  6002. }
  6003. return 0;
  6004. }
  6005. /*
  6006. * Forward all other exceptions that are valid in real mode.
  6007. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  6008. * the required debugging infrastructure rework.
  6009. */
  6010. kvm_queue_exception(vcpu, vec);
  6011. return 1;
  6012. }
  6013. /*
  6014. * Trigger machine check on the host. We assume all the MSRs are already set up
  6015. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  6016. * We pass a fake environment to the machine check handler because we want
  6017. * the guest to be always treated like user space, no matter what context
  6018. * it used internally.
  6019. */
  6020. static void kvm_machine_check(void)
  6021. {
  6022. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  6023. struct pt_regs regs = {
  6024. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  6025. .flags = X86_EFLAGS_IF,
  6026. };
  6027. do_machine_check(&regs, 0);
  6028. #endif
  6029. }
  6030. static int handle_machine_check(struct kvm_vcpu *vcpu)
  6031. {
  6032. /* already handled by vcpu_run */
  6033. return 1;
  6034. }
  6035. static int handle_exception(struct kvm_vcpu *vcpu)
  6036. {
  6037. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6038. struct kvm_run *kvm_run = vcpu->run;
  6039. u32 intr_info, ex_no, error_code;
  6040. unsigned long cr2, rip, dr6;
  6041. u32 vect_info;
  6042. enum emulation_result er;
  6043. vect_info = vmx->idt_vectoring_info;
  6044. intr_info = vmx->exit_intr_info;
  6045. if (is_machine_check(intr_info))
  6046. return handle_machine_check(vcpu);
  6047. if (is_nmi(intr_info))
  6048. return 1; /* already handled by vmx_vcpu_run() */
  6049. if (is_invalid_opcode(intr_info))
  6050. return handle_ud(vcpu);
  6051. error_code = 0;
  6052. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  6053. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  6054. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  6055. WARN_ON_ONCE(!enable_vmware_backdoor);
  6056. er = emulate_instruction(vcpu,
  6057. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  6058. if (er == EMULATE_USER_EXIT)
  6059. return 0;
  6060. else if (er != EMULATE_DONE)
  6061. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  6062. return 1;
  6063. }
  6064. /*
  6065. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  6066. * MMIO, it is better to report an internal error.
  6067. * See the comments in vmx_handle_exit.
  6068. */
  6069. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  6070. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  6071. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6072. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  6073. vcpu->run->internal.ndata = 3;
  6074. vcpu->run->internal.data[0] = vect_info;
  6075. vcpu->run->internal.data[1] = intr_info;
  6076. vcpu->run->internal.data[2] = error_code;
  6077. return 0;
  6078. }
  6079. if (is_page_fault(intr_info)) {
  6080. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  6081. /* EPT won't cause page fault directly */
  6082. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  6083. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  6084. }
  6085. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  6086. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  6087. return handle_rmode_exception(vcpu, ex_no, error_code);
  6088. switch (ex_no) {
  6089. case AC_VECTOR:
  6090. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  6091. return 1;
  6092. case DB_VECTOR:
  6093. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  6094. if (!(vcpu->guest_debug &
  6095. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  6096. vcpu->arch.dr6 &= ~15;
  6097. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  6098. if (is_icebp(intr_info))
  6099. skip_emulated_instruction(vcpu);
  6100. kvm_queue_exception(vcpu, DB_VECTOR);
  6101. return 1;
  6102. }
  6103. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  6104. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  6105. /* fall through */
  6106. case BP_VECTOR:
  6107. /*
  6108. * Update instruction length as we may reinject #BP from
  6109. * user space while in guest debugging mode. Reading it for
  6110. * #DB as well causes no harm, it is not used in that case.
  6111. */
  6112. vmx->vcpu.arch.event_exit_inst_len =
  6113. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  6114. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  6115. rip = kvm_rip_read(vcpu);
  6116. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  6117. kvm_run->debug.arch.exception = ex_no;
  6118. break;
  6119. default:
  6120. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  6121. kvm_run->ex.exception = ex_no;
  6122. kvm_run->ex.error_code = error_code;
  6123. break;
  6124. }
  6125. return 0;
  6126. }
  6127. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  6128. {
  6129. ++vcpu->stat.irq_exits;
  6130. return 1;
  6131. }
  6132. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  6133. {
  6134. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  6135. vcpu->mmio_needed = 0;
  6136. return 0;
  6137. }
  6138. static int handle_io(struct kvm_vcpu *vcpu)
  6139. {
  6140. unsigned long exit_qualification;
  6141. int size, in, string;
  6142. unsigned port;
  6143. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6144. string = (exit_qualification & 16) != 0;
  6145. ++vcpu->stat.io_exits;
  6146. if (string)
  6147. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6148. port = exit_qualification >> 16;
  6149. size = (exit_qualification & 7) + 1;
  6150. in = (exit_qualification & 8) != 0;
  6151. return kvm_fast_pio(vcpu, size, port, in);
  6152. }
  6153. static void
  6154. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  6155. {
  6156. /*
  6157. * Patch in the VMCALL instruction:
  6158. */
  6159. hypercall[0] = 0x0f;
  6160. hypercall[1] = 0x01;
  6161. hypercall[2] = 0xc1;
  6162. }
  6163. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  6164. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  6165. {
  6166. if (is_guest_mode(vcpu)) {
  6167. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6168. unsigned long orig_val = val;
  6169. /*
  6170. * We get here when L2 changed cr0 in a way that did not change
  6171. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  6172. * but did change L0 shadowed bits. So we first calculate the
  6173. * effective cr0 value that L1 would like to write into the
  6174. * hardware. It consists of the L2-owned bits from the new
  6175. * value combined with the L1-owned bits from L1's guest_cr0.
  6176. */
  6177. val = (val & ~vmcs12->cr0_guest_host_mask) |
  6178. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  6179. if (!nested_guest_cr0_valid(vcpu, val))
  6180. return 1;
  6181. if (kvm_set_cr0(vcpu, val))
  6182. return 1;
  6183. vmcs_writel(CR0_READ_SHADOW, orig_val);
  6184. return 0;
  6185. } else {
  6186. if (to_vmx(vcpu)->nested.vmxon &&
  6187. !nested_host_cr0_valid(vcpu, val))
  6188. return 1;
  6189. return kvm_set_cr0(vcpu, val);
  6190. }
  6191. }
  6192. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  6193. {
  6194. if (is_guest_mode(vcpu)) {
  6195. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6196. unsigned long orig_val = val;
  6197. /* analogously to handle_set_cr0 */
  6198. val = (val & ~vmcs12->cr4_guest_host_mask) |
  6199. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  6200. if (kvm_set_cr4(vcpu, val))
  6201. return 1;
  6202. vmcs_writel(CR4_READ_SHADOW, orig_val);
  6203. return 0;
  6204. } else
  6205. return kvm_set_cr4(vcpu, val);
  6206. }
  6207. static int handle_desc(struct kvm_vcpu *vcpu)
  6208. {
  6209. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  6210. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6211. }
  6212. static int handle_cr(struct kvm_vcpu *vcpu)
  6213. {
  6214. unsigned long exit_qualification, val;
  6215. int cr;
  6216. int reg;
  6217. int err;
  6218. int ret;
  6219. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6220. cr = exit_qualification & 15;
  6221. reg = (exit_qualification >> 8) & 15;
  6222. switch ((exit_qualification >> 4) & 3) {
  6223. case 0: /* mov to cr */
  6224. val = kvm_register_readl(vcpu, reg);
  6225. trace_kvm_cr_write(cr, val);
  6226. switch (cr) {
  6227. case 0:
  6228. err = handle_set_cr0(vcpu, val);
  6229. return kvm_complete_insn_gp(vcpu, err);
  6230. case 3:
  6231. WARN_ON_ONCE(enable_unrestricted_guest);
  6232. err = kvm_set_cr3(vcpu, val);
  6233. return kvm_complete_insn_gp(vcpu, err);
  6234. case 4:
  6235. err = handle_set_cr4(vcpu, val);
  6236. return kvm_complete_insn_gp(vcpu, err);
  6237. case 8: {
  6238. u8 cr8_prev = kvm_get_cr8(vcpu);
  6239. u8 cr8 = (u8)val;
  6240. err = kvm_set_cr8(vcpu, cr8);
  6241. ret = kvm_complete_insn_gp(vcpu, err);
  6242. if (lapic_in_kernel(vcpu))
  6243. return ret;
  6244. if (cr8_prev <= cr8)
  6245. return ret;
  6246. /*
  6247. * TODO: we might be squashing a
  6248. * KVM_GUESTDBG_SINGLESTEP-triggered
  6249. * KVM_EXIT_DEBUG here.
  6250. */
  6251. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  6252. return 0;
  6253. }
  6254. }
  6255. break;
  6256. case 2: /* clts */
  6257. WARN_ONCE(1, "Guest should always own CR0.TS");
  6258. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  6259. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  6260. return kvm_skip_emulated_instruction(vcpu);
  6261. case 1: /*mov from cr*/
  6262. switch (cr) {
  6263. case 3:
  6264. WARN_ON_ONCE(enable_unrestricted_guest);
  6265. val = kvm_read_cr3(vcpu);
  6266. kvm_register_write(vcpu, reg, val);
  6267. trace_kvm_cr_read(cr, val);
  6268. return kvm_skip_emulated_instruction(vcpu);
  6269. case 8:
  6270. val = kvm_get_cr8(vcpu);
  6271. kvm_register_write(vcpu, reg, val);
  6272. trace_kvm_cr_read(cr, val);
  6273. return kvm_skip_emulated_instruction(vcpu);
  6274. }
  6275. break;
  6276. case 3: /* lmsw */
  6277. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  6278. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  6279. kvm_lmsw(vcpu, val);
  6280. return kvm_skip_emulated_instruction(vcpu);
  6281. default:
  6282. break;
  6283. }
  6284. vcpu->run->exit_reason = 0;
  6285. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  6286. (int)(exit_qualification >> 4) & 3, cr);
  6287. return 0;
  6288. }
  6289. static int handle_dr(struct kvm_vcpu *vcpu)
  6290. {
  6291. unsigned long exit_qualification;
  6292. int dr, dr7, reg;
  6293. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6294. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  6295. /* First, if DR does not exist, trigger UD */
  6296. if (!kvm_require_dr(vcpu, dr))
  6297. return 1;
  6298. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  6299. if (!kvm_require_cpl(vcpu, 0))
  6300. return 1;
  6301. dr7 = vmcs_readl(GUEST_DR7);
  6302. if (dr7 & DR7_GD) {
  6303. /*
  6304. * As the vm-exit takes precedence over the debug trap, we
  6305. * need to emulate the latter, either for the host or the
  6306. * guest debugging itself.
  6307. */
  6308. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6309. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6310. vcpu->run->debug.arch.dr7 = dr7;
  6311. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6312. vcpu->run->debug.arch.exception = DB_VECTOR;
  6313. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6314. return 0;
  6315. } else {
  6316. vcpu->arch.dr6 &= ~15;
  6317. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6318. kvm_queue_exception(vcpu, DB_VECTOR);
  6319. return 1;
  6320. }
  6321. }
  6322. if (vcpu->guest_debug == 0) {
  6323. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6324. CPU_BASED_MOV_DR_EXITING);
  6325. /*
  6326. * No more DR vmexits; force a reload of the debug registers
  6327. * and reenter on this instruction. The next vmexit will
  6328. * retrieve the full state of the debug registers.
  6329. */
  6330. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6331. return 1;
  6332. }
  6333. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6334. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6335. unsigned long val;
  6336. if (kvm_get_dr(vcpu, dr, &val))
  6337. return 1;
  6338. kvm_register_write(vcpu, reg, val);
  6339. } else
  6340. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6341. return 1;
  6342. return kvm_skip_emulated_instruction(vcpu);
  6343. }
  6344. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6345. {
  6346. return vcpu->arch.dr6;
  6347. }
  6348. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6349. {
  6350. }
  6351. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6352. {
  6353. get_debugreg(vcpu->arch.db[0], 0);
  6354. get_debugreg(vcpu->arch.db[1], 1);
  6355. get_debugreg(vcpu->arch.db[2], 2);
  6356. get_debugreg(vcpu->arch.db[3], 3);
  6357. get_debugreg(vcpu->arch.dr6, 6);
  6358. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6359. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6360. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6361. }
  6362. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6363. {
  6364. vmcs_writel(GUEST_DR7, val);
  6365. }
  6366. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6367. {
  6368. return kvm_emulate_cpuid(vcpu);
  6369. }
  6370. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6371. {
  6372. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6373. struct msr_data msr_info;
  6374. msr_info.index = ecx;
  6375. msr_info.host_initiated = false;
  6376. if (vmx_get_msr(vcpu, &msr_info)) {
  6377. trace_kvm_msr_read_ex(ecx);
  6378. kvm_inject_gp(vcpu, 0);
  6379. return 1;
  6380. }
  6381. trace_kvm_msr_read(ecx, msr_info.data);
  6382. /* FIXME: handling of bits 32:63 of rax, rdx */
  6383. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6384. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6385. return kvm_skip_emulated_instruction(vcpu);
  6386. }
  6387. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6388. {
  6389. struct msr_data msr;
  6390. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6391. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6392. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6393. msr.data = data;
  6394. msr.index = ecx;
  6395. msr.host_initiated = false;
  6396. if (kvm_set_msr(vcpu, &msr) != 0) {
  6397. trace_kvm_msr_write_ex(ecx, data);
  6398. kvm_inject_gp(vcpu, 0);
  6399. return 1;
  6400. }
  6401. trace_kvm_msr_write(ecx, data);
  6402. return kvm_skip_emulated_instruction(vcpu);
  6403. }
  6404. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6405. {
  6406. kvm_apic_update_ppr(vcpu);
  6407. return 1;
  6408. }
  6409. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6410. {
  6411. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6412. CPU_BASED_VIRTUAL_INTR_PENDING);
  6413. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6414. ++vcpu->stat.irq_window_exits;
  6415. return 1;
  6416. }
  6417. static int handle_halt(struct kvm_vcpu *vcpu)
  6418. {
  6419. return kvm_emulate_halt(vcpu);
  6420. }
  6421. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6422. {
  6423. return kvm_emulate_hypercall(vcpu);
  6424. }
  6425. static int handle_invd(struct kvm_vcpu *vcpu)
  6426. {
  6427. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6428. }
  6429. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6430. {
  6431. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6432. kvm_mmu_invlpg(vcpu, exit_qualification);
  6433. return kvm_skip_emulated_instruction(vcpu);
  6434. }
  6435. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6436. {
  6437. int err;
  6438. err = kvm_rdpmc(vcpu);
  6439. return kvm_complete_insn_gp(vcpu, err);
  6440. }
  6441. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6442. {
  6443. return kvm_emulate_wbinvd(vcpu);
  6444. }
  6445. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6446. {
  6447. u64 new_bv = kvm_read_edx_eax(vcpu);
  6448. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6449. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6450. return kvm_skip_emulated_instruction(vcpu);
  6451. return 1;
  6452. }
  6453. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6454. {
  6455. kvm_skip_emulated_instruction(vcpu);
  6456. WARN(1, "this should never happen\n");
  6457. return 1;
  6458. }
  6459. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6460. {
  6461. kvm_skip_emulated_instruction(vcpu);
  6462. WARN(1, "this should never happen\n");
  6463. return 1;
  6464. }
  6465. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6466. {
  6467. if (likely(fasteoi)) {
  6468. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6469. int access_type, offset;
  6470. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6471. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6472. /*
  6473. * Sane guest uses MOV to write EOI, with written value
  6474. * not cared. So make a short-circuit here by avoiding
  6475. * heavy instruction emulation.
  6476. */
  6477. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6478. (offset == APIC_EOI)) {
  6479. kvm_lapic_set_eoi(vcpu);
  6480. return kvm_skip_emulated_instruction(vcpu);
  6481. }
  6482. }
  6483. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6484. }
  6485. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6486. {
  6487. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6488. int vector = exit_qualification & 0xff;
  6489. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6490. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6491. return 1;
  6492. }
  6493. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6494. {
  6495. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6496. u32 offset = exit_qualification & 0xfff;
  6497. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6498. kvm_apic_write_nodecode(vcpu, offset);
  6499. return 1;
  6500. }
  6501. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6502. {
  6503. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6504. unsigned long exit_qualification;
  6505. bool has_error_code = false;
  6506. u32 error_code = 0;
  6507. u16 tss_selector;
  6508. int reason, type, idt_v, idt_index;
  6509. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6510. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6511. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6512. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6513. reason = (u32)exit_qualification >> 30;
  6514. if (reason == TASK_SWITCH_GATE && idt_v) {
  6515. switch (type) {
  6516. case INTR_TYPE_NMI_INTR:
  6517. vcpu->arch.nmi_injected = false;
  6518. vmx_set_nmi_mask(vcpu, true);
  6519. break;
  6520. case INTR_TYPE_EXT_INTR:
  6521. case INTR_TYPE_SOFT_INTR:
  6522. kvm_clear_interrupt_queue(vcpu);
  6523. break;
  6524. case INTR_TYPE_HARD_EXCEPTION:
  6525. if (vmx->idt_vectoring_info &
  6526. VECTORING_INFO_DELIVER_CODE_MASK) {
  6527. has_error_code = true;
  6528. error_code =
  6529. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6530. }
  6531. /* fall through */
  6532. case INTR_TYPE_SOFT_EXCEPTION:
  6533. kvm_clear_exception_queue(vcpu);
  6534. break;
  6535. default:
  6536. break;
  6537. }
  6538. }
  6539. tss_selector = exit_qualification;
  6540. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6541. type != INTR_TYPE_EXT_INTR &&
  6542. type != INTR_TYPE_NMI_INTR))
  6543. skip_emulated_instruction(vcpu);
  6544. if (kvm_task_switch(vcpu, tss_selector,
  6545. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6546. has_error_code, error_code) == EMULATE_FAIL) {
  6547. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6548. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6549. vcpu->run->internal.ndata = 0;
  6550. return 0;
  6551. }
  6552. /*
  6553. * TODO: What about debug traps on tss switch?
  6554. * Are we supposed to inject them and update dr6?
  6555. */
  6556. return 1;
  6557. }
  6558. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6559. {
  6560. unsigned long exit_qualification;
  6561. gpa_t gpa;
  6562. u64 error_code;
  6563. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6564. /*
  6565. * EPT violation happened while executing iret from NMI,
  6566. * "blocked by NMI" bit has to be set before next VM entry.
  6567. * There are errata that may cause this bit to not be set:
  6568. * AAK134, BY25.
  6569. */
  6570. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6571. enable_vnmi &&
  6572. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6573. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6574. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6575. trace_kvm_page_fault(gpa, exit_qualification);
  6576. /* Is it a read fault? */
  6577. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6578. ? PFERR_USER_MASK : 0;
  6579. /* Is it a write fault? */
  6580. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6581. ? PFERR_WRITE_MASK : 0;
  6582. /* Is it a fetch fault? */
  6583. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6584. ? PFERR_FETCH_MASK : 0;
  6585. /* ept page table entry is present? */
  6586. error_code |= (exit_qualification &
  6587. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6588. EPT_VIOLATION_EXECUTABLE))
  6589. ? PFERR_PRESENT_MASK : 0;
  6590. error_code |= (exit_qualification & 0x100) != 0 ?
  6591. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6592. vcpu->arch.exit_qualification = exit_qualification;
  6593. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6594. }
  6595. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6596. {
  6597. gpa_t gpa;
  6598. /*
  6599. * A nested guest cannot optimize MMIO vmexits, because we have an
  6600. * nGPA here instead of the required GPA.
  6601. */
  6602. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6603. if (!is_guest_mode(vcpu) &&
  6604. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6605. trace_kvm_fast_mmio(gpa);
  6606. /*
  6607. * Doing kvm_skip_emulated_instruction() depends on undefined
  6608. * behavior: Intel's manual doesn't mandate
  6609. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6610. * occurs and while on real hardware it was observed to be set,
  6611. * other hypervisors (namely Hyper-V) don't set it, we end up
  6612. * advancing IP with some random value. Disable fast mmio when
  6613. * running nested and keep it for real hardware in hope that
  6614. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6615. */
  6616. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6617. return kvm_skip_emulated_instruction(vcpu);
  6618. else
  6619. return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
  6620. NULL, 0) == EMULATE_DONE;
  6621. }
  6622. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6623. }
  6624. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6625. {
  6626. WARN_ON_ONCE(!enable_vnmi);
  6627. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6628. CPU_BASED_VIRTUAL_NMI_PENDING);
  6629. ++vcpu->stat.nmi_window_exits;
  6630. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6631. return 1;
  6632. }
  6633. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6634. {
  6635. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6636. enum emulation_result err = EMULATE_DONE;
  6637. int ret = 1;
  6638. u32 cpu_exec_ctrl;
  6639. bool intr_window_requested;
  6640. unsigned count = 130;
  6641. /*
  6642. * We should never reach the point where we are emulating L2
  6643. * due to invalid guest state as that means we incorrectly
  6644. * allowed a nested VMEntry with an invalid vmcs12.
  6645. */
  6646. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6647. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6648. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6649. while (vmx->emulation_required && count-- != 0) {
  6650. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6651. return handle_interrupt_window(&vmx->vcpu);
  6652. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6653. return 1;
  6654. err = emulate_instruction(vcpu, 0);
  6655. if (err == EMULATE_USER_EXIT) {
  6656. ++vcpu->stat.mmio_exits;
  6657. ret = 0;
  6658. goto out;
  6659. }
  6660. if (err != EMULATE_DONE)
  6661. goto emulation_error;
  6662. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6663. vcpu->arch.exception.pending)
  6664. goto emulation_error;
  6665. if (vcpu->arch.halt_request) {
  6666. vcpu->arch.halt_request = 0;
  6667. ret = kvm_vcpu_halt(vcpu);
  6668. goto out;
  6669. }
  6670. if (signal_pending(current))
  6671. goto out;
  6672. if (need_resched())
  6673. schedule();
  6674. }
  6675. out:
  6676. return ret;
  6677. emulation_error:
  6678. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6679. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6680. vcpu->run->internal.ndata = 0;
  6681. return 0;
  6682. }
  6683. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6684. {
  6685. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6686. int old = vmx->ple_window;
  6687. vmx->ple_window = __grow_ple_window(old, ple_window,
  6688. ple_window_grow,
  6689. ple_window_max);
  6690. if (vmx->ple_window != old)
  6691. vmx->ple_window_dirty = true;
  6692. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6693. }
  6694. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6695. {
  6696. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6697. int old = vmx->ple_window;
  6698. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6699. ple_window_shrink,
  6700. ple_window);
  6701. if (vmx->ple_window != old)
  6702. vmx->ple_window_dirty = true;
  6703. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6704. }
  6705. /*
  6706. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6707. */
  6708. static void wakeup_handler(void)
  6709. {
  6710. struct kvm_vcpu *vcpu;
  6711. int cpu = smp_processor_id();
  6712. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6713. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6714. blocked_vcpu_list) {
  6715. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6716. if (pi_test_on(pi_desc) == 1)
  6717. kvm_vcpu_kick(vcpu);
  6718. }
  6719. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6720. }
  6721. static void vmx_enable_tdp(void)
  6722. {
  6723. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6724. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6725. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6726. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6727. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6728. VMX_EPT_RWX_MASK, 0ull);
  6729. ept_set_mmio_spte_mask();
  6730. kvm_enable_tdp();
  6731. }
  6732. static __init int hardware_setup(void)
  6733. {
  6734. unsigned long host_bndcfgs;
  6735. int r = -ENOMEM, i;
  6736. rdmsrl_safe(MSR_EFER, &host_efer);
  6737. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6738. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6739. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6740. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6741. if (!vmx_bitmap[i])
  6742. goto out;
  6743. }
  6744. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6745. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6746. if (setup_vmcs_config(&vmcs_config) < 0) {
  6747. r = -EIO;
  6748. goto out;
  6749. }
  6750. if (boot_cpu_has(X86_FEATURE_NX))
  6751. kvm_enable_efer_bits(EFER_NX);
  6752. if (boot_cpu_has(X86_FEATURE_MPX)) {
  6753. rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
  6754. WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
  6755. }
  6756. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6757. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6758. enable_vpid = 0;
  6759. if (!cpu_has_vmx_ept() ||
  6760. !cpu_has_vmx_ept_4levels() ||
  6761. !cpu_has_vmx_ept_mt_wb() ||
  6762. !cpu_has_vmx_invept_global())
  6763. enable_ept = 0;
  6764. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6765. enable_ept_ad_bits = 0;
  6766. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6767. enable_unrestricted_guest = 0;
  6768. if (!cpu_has_vmx_flexpriority())
  6769. flexpriority_enabled = 0;
  6770. if (!cpu_has_virtual_nmis())
  6771. enable_vnmi = 0;
  6772. /*
  6773. * set_apic_access_page_addr() is used to reload apic access
  6774. * page upon invalidation. No need to do anything if not
  6775. * using the APIC_ACCESS_ADDR VMCS field.
  6776. */
  6777. if (!flexpriority_enabled)
  6778. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6779. if (!cpu_has_vmx_tpr_shadow())
  6780. kvm_x86_ops->update_cr8_intercept = NULL;
  6781. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6782. kvm_disable_largepages();
  6783. #if IS_ENABLED(CONFIG_HYPERV)
  6784. if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
  6785. && enable_ept)
  6786. kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
  6787. #endif
  6788. if (!cpu_has_vmx_ple()) {
  6789. ple_gap = 0;
  6790. ple_window = 0;
  6791. ple_window_grow = 0;
  6792. ple_window_max = 0;
  6793. ple_window_shrink = 0;
  6794. }
  6795. if (!cpu_has_vmx_apicv()) {
  6796. enable_apicv = 0;
  6797. kvm_x86_ops->sync_pir_to_irr = NULL;
  6798. }
  6799. if (cpu_has_vmx_tsc_scaling()) {
  6800. kvm_has_tsc_control = true;
  6801. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6802. kvm_tsc_scaling_ratio_frac_bits = 48;
  6803. }
  6804. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6805. if (enable_ept)
  6806. vmx_enable_tdp();
  6807. else
  6808. kvm_disable_tdp();
  6809. if (!nested) {
  6810. kvm_x86_ops->get_nested_state = NULL;
  6811. kvm_x86_ops->set_nested_state = NULL;
  6812. }
  6813. /*
  6814. * Only enable PML when hardware supports PML feature, and both EPT
  6815. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6816. */
  6817. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6818. enable_pml = 0;
  6819. if (!enable_pml) {
  6820. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6821. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6822. kvm_x86_ops->flush_log_dirty = NULL;
  6823. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6824. }
  6825. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6826. u64 vmx_msr;
  6827. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6828. cpu_preemption_timer_multi =
  6829. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6830. } else {
  6831. kvm_x86_ops->set_hv_timer = NULL;
  6832. kvm_x86_ops->cancel_hv_timer = NULL;
  6833. }
  6834. if (!cpu_has_vmx_shadow_vmcs())
  6835. enable_shadow_vmcs = 0;
  6836. if (enable_shadow_vmcs)
  6837. init_vmcs_shadow_fields();
  6838. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6839. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6840. kvm_mce_cap_supported |= MCG_LMCE_P;
  6841. return alloc_kvm_area();
  6842. out:
  6843. for (i = 0; i < VMX_BITMAP_NR; i++)
  6844. free_page((unsigned long)vmx_bitmap[i]);
  6845. return r;
  6846. }
  6847. static __exit void hardware_unsetup(void)
  6848. {
  6849. int i;
  6850. for (i = 0; i < VMX_BITMAP_NR; i++)
  6851. free_page((unsigned long)vmx_bitmap[i]);
  6852. free_kvm_area();
  6853. }
  6854. /*
  6855. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6856. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6857. */
  6858. static int handle_pause(struct kvm_vcpu *vcpu)
  6859. {
  6860. if (!kvm_pause_in_guest(vcpu->kvm))
  6861. grow_ple_window(vcpu);
  6862. /*
  6863. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6864. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6865. * never set PAUSE_EXITING and just set PLE if supported,
  6866. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6867. */
  6868. kvm_vcpu_on_spin(vcpu, true);
  6869. return kvm_skip_emulated_instruction(vcpu);
  6870. }
  6871. static int handle_nop(struct kvm_vcpu *vcpu)
  6872. {
  6873. return kvm_skip_emulated_instruction(vcpu);
  6874. }
  6875. static int handle_mwait(struct kvm_vcpu *vcpu)
  6876. {
  6877. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6878. return handle_nop(vcpu);
  6879. }
  6880. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6881. {
  6882. kvm_queue_exception(vcpu, UD_VECTOR);
  6883. return 1;
  6884. }
  6885. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6886. {
  6887. return 1;
  6888. }
  6889. static int handle_monitor(struct kvm_vcpu *vcpu)
  6890. {
  6891. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6892. return handle_nop(vcpu);
  6893. }
  6894. /*
  6895. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6896. * set the success or error code of an emulated VMX instruction, as specified
  6897. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6898. */
  6899. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6900. {
  6901. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6902. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6903. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6904. }
  6905. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6906. {
  6907. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6908. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6909. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6910. | X86_EFLAGS_CF);
  6911. }
  6912. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6913. u32 vm_instruction_error)
  6914. {
  6915. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6916. /*
  6917. * failValid writes the error number to the current VMCS, which
  6918. * can't be done there isn't a current VMCS.
  6919. */
  6920. nested_vmx_failInvalid(vcpu);
  6921. return;
  6922. }
  6923. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6924. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6925. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6926. | X86_EFLAGS_ZF);
  6927. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6928. /*
  6929. * We don't need to force a shadow sync because
  6930. * VM_INSTRUCTION_ERROR is not shadowed
  6931. */
  6932. }
  6933. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6934. {
  6935. /* TODO: not to reset guest simply here. */
  6936. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6937. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6938. }
  6939. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6940. {
  6941. struct vcpu_vmx *vmx =
  6942. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6943. vmx->nested.preemption_timer_expired = true;
  6944. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6945. kvm_vcpu_kick(&vmx->vcpu);
  6946. return HRTIMER_NORESTART;
  6947. }
  6948. /*
  6949. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6950. * exit caused by such an instruction (run by a guest hypervisor).
  6951. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6952. * #UD or #GP.
  6953. */
  6954. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6955. unsigned long exit_qualification,
  6956. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6957. {
  6958. gva_t off;
  6959. bool exn;
  6960. struct kvm_segment s;
  6961. /*
  6962. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6963. * Execution", on an exit, vmx_instruction_info holds most of the
  6964. * addressing components of the operand. Only the displacement part
  6965. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6966. * For how an actual address is calculated from all these components,
  6967. * refer to Vol. 1, "Operand Addressing".
  6968. */
  6969. int scaling = vmx_instruction_info & 3;
  6970. int addr_size = (vmx_instruction_info >> 7) & 7;
  6971. bool is_reg = vmx_instruction_info & (1u << 10);
  6972. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6973. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6974. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6975. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6976. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6977. if (is_reg) {
  6978. kvm_queue_exception(vcpu, UD_VECTOR);
  6979. return 1;
  6980. }
  6981. /* Addr = segment_base + offset */
  6982. /* offset = base + [index * scale] + displacement */
  6983. off = exit_qualification; /* holds the displacement */
  6984. if (base_is_valid)
  6985. off += kvm_register_read(vcpu, base_reg);
  6986. if (index_is_valid)
  6987. off += kvm_register_read(vcpu, index_reg)<<scaling;
  6988. vmx_get_segment(vcpu, &s, seg_reg);
  6989. *ret = s.base + off;
  6990. if (addr_size == 1) /* 32 bit */
  6991. *ret &= 0xffffffff;
  6992. /* Checks for #GP/#SS exceptions. */
  6993. exn = false;
  6994. if (is_long_mode(vcpu)) {
  6995. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  6996. * non-canonical form. This is the only check on the memory
  6997. * destination for long mode!
  6998. */
  6999. exn = is_noncanonical_address(*ret, vcpu);
  7000. } else if (is_protmode(vcpu)) {
  7001. /* Protected mode: apply checks for segment validity in the
  7002. * following order:
  7003. * - segment type check (#GP(0) may be thrown)
  7004. * - usability check (#GP(0)/#SS(0))
  7005. * - limit check (#GP(0)/#SS(0))
  7006. */
  7007. if (wr)
  7008. /* #GP(0) if the destination operand is located in a
  7009. * read-only data segment or any code segment.
  7010. */
  7011. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  7012. else
  7013. /* #GP(0) if the source operand is located in an
  7014. * execute-only code segment
  7015. */
  7016. exn = ((s.type & 0xa) == 8);
  7017. if (exn) {
  7018. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  7019. return 1;
  7020. }
  7021. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  7022. */
  7023. exn = (s.unusable != 0);
  7024. /* Protected mode: #GP(0)/#SS(0) if the memory
  7025. * operand is outside the segment limit.
  7026. */
  7027. exn = exn || (off + sizeof(u64) > s.limit);
  7028. }
  7029. if (exn) {
  7030. kvm_queue_exception_e(vcpu,
  7031. seg_reg == VCPU_SREG_SS ?
  7032. SS_VECTOR : GP_VECTOR,
  7033. 0);
  7034. return 1;
  7035. }
  7036. return 0;
  7037. }
  7038. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  7039. {
  7040. gva_t gva;
  7041. struct x86_exception e;
  7042. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7043. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  7044. return 1;
  7045. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  7046. kvm_inject_page_fault(vcpu, &e);
  7047. return 1;
  7048. }
  7049. return 0;
  7050. }
  7051. /*
  7052. * Allocate a shadow VMCS and associate it with the currently loaded
  7053. * VMCS, unless such a shadow VMCS already exists. The newly allocated
  7054. * VMCS is also VMCLEARed, so that it is ready for use.
  7055. */
  7056. static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
  7057. {
  7058. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7059. struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
  7060. /*
  7061. * We should allocate a shadow vmcs for vmcs01 only when L1
  7062. * executes VMXON and free it when L1 executes VMXOFF.
  7063. * As it is invalid to execute VMXON twice, we shouldn't reach
  7064. * here when vmcs01 already have an allocated shadow vmcs.
  7065. */
  7066. WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
  7067. if (!loaded_vmcs->shadow_vmcs) {
  7068. loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
  7069. if (loaded_vmcs->shadow_vmcs)
  7070. vmcs_clear(loaded_vmcs->shadow_vmcs);
  7071. }
  7072. return loaded_vmcs->shadow_vmcs;
  7073. }
  7074. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  7075. {
  7076. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7077. int r;
  7078. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  7079. if (r < 0)
  7080. goto out_vmcs02;
  7081. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  7082. if (!vmx->nested.cached_vmcs12)
  7083. goto out_cached_vmcs12;
  7084. vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  7085. if (!vmx->nested.cached_shadow_vmcs12)
  7086. goto out_cached_shadow_vmcs12;
  7087. if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
  7088. goto out_shadow_vmcs;
  7089. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  7090. HRTIMER_MODE_REL_PINNED);
  7091. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  7092. vmx->nested.vpid02 = allocate_vpid();
  7093. vmx->nested.vmxon = true;
  7094. return 0;
  7095. out_shadow_vmcs:
  7096. kfree(vmx->nested.cached_shadow_vmcs12);
  7097. out_cached_shadow_vmcs12:
  7098. kfree(vmx->nested.cached_vmcs12);
  7099. out_cached_vmcs12:
  7100. free_loaded_vmcs(&vmx->nested.vmcs02);
  7101. out_vmcs02:
  7102. return -ENOMEM;
  7103. }
  7104. /*
  7105. * Emulate the VMXON instruction.
  7106. * Currently, we just remember that VMX is active, and do not save or even
  7107. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  7108. * do not currently need to store anything in that guest-allocated memory
  7109. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  7110. * argument is different from the VMXON pointer (which the spec says they do).
  7111. */
  7112. static int handle_vmon(struct kvm_vcpu *vcpu)
  7113. {
  7114. int ret;
  7115. gpa_t vmptr;
  7116. struct page *page;
  7117. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7118. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  7119. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  7120. /*
  7121. * The Intel VMX Instruction Reference lists a bunch of bits that are
  7122. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  7123. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  7124. * Otherwise, we should fail with #UD. But most faulting conditions
  7125. * have already been checked by hardware, prior to the VM-exit for
  7126. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  7127. * that bit set to 1 in non-root mode.
  7128. */
  7129. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  7130. kvm_queue_exception(vcpu, UD_VECTOR);
  7131. return 1;
  7132. }
  7133. /* CPL=0 must be checked manually. */
  7134. if (vmx_get_cpl(vcpu)) {
  7135. kvm_inject_gp(vcpu, 0);
  7136. return 1;
  7137. }
  7138. if (vmx->nested.vmxon) {
  7139. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  7140. return kvm_skip_emulated_instruction(vcpu);
  7141. }
  7142. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  7143. != VMXON_NEEDED_FEATURES) {
  7144. kvm_inject_gp(vcpu, 0);
  7145. return 1;
  7146. }
  7147. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7148. return 1;
  7149. /*
  7150. * SDM 3: 24.11.5
  7151. * The first 4 bytes of VMXON region contain the supported
  7152. * VMCS revision identifier
  7153. *
  7154. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  7155. * which replaces physical address width with 32
  7156. */
  7157. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7158. nested_vmx_failInvalid(vcpu);
  7159. return kvm_skip_emulated_instruction(vcpu);
  7160. }
  7161. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7162. if (is_error_page(page)) {
  7163. nested_vmx_failInvalid(vcpu);
  7164. return kvm_skip_emulated_instruction(vcpu);
  7165. }
  7166. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  7167. kunmap(page);
  7168. kvm_release_page_clean(page);
  7169. nested_vmx_failInvalid(vcpu);
  7170. return kvm_skip_emulated_instruction(vcpu);
  7171. }
  7172. kunmap(page);
  7173. kvm_release_page_clean(page);
  7174. vmx->nested.vmxon_ptr = vmptr;
  7175. ret = enter_vmx_operation(vcpu);
  7176. if (ret)
  7177. return ret;
  7178. nested_vmx_succeed(vcpu);
  7179. return kvm_skip_emulated_instruction(vcpu);
  7180. }
  7181. /*
  7182. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  7183. * for running VMX instructions (except VMXON, whose prerequisites are
  7184. * slightly different). It also specifies what exception to inject otherwise.
  7185. * Note that many of these exceptions have priority over VM exits, so they
  7186. * don't have to be checked again here.
  7187. */
  7188. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  7189. {
  7190. if (!to_vmx(vcpu)->nested.vmxon) {
  7191. kvm_queue_exception(vcpu, UD_VECTOR);
  7192. return 0;
  7193. }
  7194. if (vmx_get_cpl(vcpu)) {
  7195. kvm_inject_gp(vcpu, 0);
  7196. return 0;
  7197. }
  7198. return 1;
  7199. }
  7200. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  7201. {
  7202. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  7203. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7204. }
  7205. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  7206. {
  7207. if (vmx->nested.current_vmptr == -1ull)
  7208. return;
  7209. if (enable_shadow_vmcs) {
  7210. /* copy to memory all shadowed fields in case
  7211. they were modified */
  7212. copy_shadow_to_vmcs12(vmx);
  7213. vmx->nested.sync_shadow_vmcs = false;
  7214. vmx_disable_shadow_vmcs(vmx);
  7215. }
  7216. vmx->nested.posted_intr_nv = -1;
  7217. /* Flush VMCS12 to guest memory */
  7218. kvm_vcpu_write_guest_page(&vmx->vcpu,
  7219. vmx->nested.current_vmptr >> PAGE_SHIFT,
  7220. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  7221. vmx->nested.current_vmptr = -1ull;
  7222. }
  7223. /*
  7224. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  7225. * just stops using VMX.
  7226. */
  7227. static void free_nested(struct vcpu_vmx *vmx)
  7228. {
  7229. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  7230. return;
  7231. vmx->nested.vmxon = false;
  7232. vmx->nested.smm.vmxon = false;
  7233. free_vpid(vmx->nested.vpid02);
  7234. vmx->nested.posted_intr_nv = -1;
  7235. vmx->nested.current_vmptr = -1ull;
  7236. if (enable_shadow_vmcs) {
  7237. vmx_disable_shadow_vmcs(vmx);
  7238. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  7239. free_vmcs(vmx->vmcs01.shadow_vmcs);
  7240. vmx->vmcs01.shadow_vmcs = NULL;
  7241. }
  7242. kfree(vmx->nested.cached_vmcs12);
  7243. kfree(vmx->nested.cached_shadow_vmcs12);
  7244. /* Unpin physical memory we referred to in the vmcs02 */
  7245. if (vmx->nested.apic_access_page) {
  7246. kvm_release_page_dirty(vmx->nested.apic_access_page);
  7247. vmx->nested.apic_access_page = NULL;
  7248. }
  7249. if (vmx->nested.virtual_apic_page) {
  7250. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  7251. vmx->nested.virtual_apic_page = NULL;
  7252. }
  7253. if (vmx->nested.pi_desc_page) {
  7254. kunmap(vmx->nested.pi_desc_page);
  7255. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  7256. vmx->nested.pi_desc_page = NULL;
  7257. vmx->nested.pi_desc = NULL;
  7258. }
  7259. free_loaded_vmcs(&vmx->nested.vmcs02);
  7260. }
  7261. /* Emulate the VMXOFF instruction */
  7262. static int handle_vmoff(struct kvm_vcpu *vcpu)
  7263. {
  7264. if (!nested_vmx_check_permission(vcpu))
  7265. return 1;
  7266. free_nested(to_vmx(vcpu));
  7267. nested_vmx_succeed(vcpu);
  7268. return kvm_skip_emulated_instruction(vcpu);
  7269. }
  7270. /* Emulate the VMCLEAR instruction */
  7271. static int handle_vmclear(struct kvm_vcpu *vcpu)
  7272. {
  7273. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7274. u32 zero = 0;
  7275. gpa_t vmptr;
  7276. if (!nested_vmx_check_permission(vcpu))
  7277. return 1;
  7278. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7279. return 1;
  7280. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7281. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  7282. return kvm_skip_emulated_instruction(vcpu);
  7283. }
  7284. if (vmptr == vmx->nested.vmxon_ptr) {
  7285. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  7286. return kvm_skip_emulated_instruction(vcpu);
  7287. }
  7288. if (vmptr == vmx->nested.current_vmptr)
  7289. nested_release_vmcs12(vmx);
  7290. kvm_vcpu_write_guest(vcpu,
  7291. vmptr + offsetof(struct vmcs12, launch_state),
  7292. &zero, sizeof(zero));
  7293. nested_vmx_succeed(vcpu);
  7294. return kvm_skip_emulated_instruction(vcpu);
  7295. }
  7296. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  7297. /* Emulate the VMLAUNCH instruction */
  7298. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  7299. {
  7300. return nested_vmx_run(vcpu, true);
  7301. }
  7302. /* Emulate the VMRESUME instruction */
  7303. static int handle_vmresume(struct kvm_vcpu *vcpu)
  7304. {
  7305. return nested_vmx_run(vcpu, false);
  7306. }
  7307. /*
  7308. * Read a vmcs12 field. Since these can have varying lengths and we return
  7309. * one type, we chose the biggest type (u64) and zero-extend the return value
  7310. * to that size. Note that the caller, handle_vmread, might need to use only
  7311. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  7312. * 64-bit fields are to be returned).
  7313. */
  7314. static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
  7315. unsigned long field, u64 *ret)
  7316. {
  7317. short offset = vmcs_field_to_offset(field);
  7318. char *p;
  7319. if (offset < 0)
  7320. return offset;
  7321. p = (char *)vmcs12 + offset;
  7322. switch (vmcs_field_width(field)) {
  7323. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7324. *ret = *((natural_width *)p);
  7325. return 0;
  7326. case VMCS_FIELD_WIDTH_U16:
  7327. *ret = *((u16 *)p);
  7328. return 0;
  7329. case VMCS_FIELD_WIDTH_U32:
  7330. *ret = *((u32 *)p);
  7331. return 0;
  7332. case VMCS_FIELD_WIDTH_U64:
  7333. *ret = *((u64 *)p);
  7334. return 0;
  7335. default:
  7336. WARN_ON(1);
  7337. return -ENOENT;
  7338. }
  7339. }
  7340. static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
  7341. unsigned long field, u64 field_value){
  7342. short offset = vmcs_field_to_offset(field);
  7343. char *p = (char *)vmcs12 + offset;
  7344. if (offset < 0)
  7345. return offset;
  7346. switch (vmcs_field_width(field)) {
  7347. case VMCS_FIELD_WIDTH_U16:
  7348. *(u16 *)p = field_value;
  7349. return 0;
  7350. case VMCS_FIELD_WIDTH_U32:
  7351. *(u32 *)p = field_value;
  7352. return 0;
  7353. case VMCS_FIELD_WIDTH_U64:
  7354. *(u64 *)p = field_value;
  7355. return 0;
  7356. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7357. *(natural_width *)p = field_value;
  7358. return 0;
  7359. default:
  7360. WARN_ON(1);
  7361. return -ENOENT;
  7362. }
  7363. }
  7364. /*
  7365. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7366. * they have been modified by the L1 guest. Note that the "read-only"
  7367. * VM-exit information fields are actually writable if the vCPU is
  7368. * configured to support "VMWRITE to any supported field in the VMCS."
  7369. */
  7370. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7371. {
  7372. const u16 *fields[] = {
  7373. shadow_read_write_fields,
  7374. shadow_read_only_fields
  7375. };
  7376. const int max_fields[] = {
  7377. max_shadow_read_write_fields,
  7378. max_shadow_read_only_fields
  7379. };
  7380. int i, q;
  7381. unsigned long field;
  7382. u64 field_value;
  7383. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7384. preempt_disable();
  7385. vmcs_load(shadow_vmcs);
  7386. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7387. for (i = 0; i < max_fields[q]; i++) {
  7388. field = fields[q][i];
  7389. field_value = __vmcs_readl(field);
  7390. vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
  7391. }
  7392. /*
  7393. * Skip the VM-exit information fields if they are read-only.
  7394. */
  7395. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7396. break;
  7397. }
  7398. vmcs_clear(shadow_vmcs);
  7399. vmcs_load(vmx->loaded_vmcs->vmcs);
  7400. preempt_enable();
  7401. }
  7402. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7403. {
  7404. const u16 *fields[] = {
  7405. shadow_read_write_fields,
  7406. shadow_read_only_fields
  7407. };
  7408. const int max_fields[] = {
  7409. max_shadow_read_write_fields,
  7410. max_shadow_read_only_fields
  7411. };
  7412. int i, q;
  7413. unsigned long field;
  7414. u64 field_value = 0;
  7415. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7416. vmcs_load(shadow_vmcs);
  7417. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7418. for (i = 0; i < max_fields[q]; i++) {
  7419. field = fields[q][i];
  7420. vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
  7421. __vmcs_writel(field, field_value);
  7422. }
  7423. }
  7424. vmcs_clear(shadow_vmcs);
  7425. vmcs_load(vmx->loaded_vmcs->vmcs);
  7426. }
  7427. /*
  7428. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  7429. * used before) all generate the same failure when it is missing.
  7430. */
  7431. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  7432. {
  7433. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7434. if (vmx->nested.current_vmptr == -1ull) {
  7435. nested_vmx_failInvalid(vcpu);
  7436. return 0;
  7437. }
  7438. return 1;
  7439. }
  7440. static int handle_vmread(struct kvm_vcpu *vcpu)
  7441. {
  7442. unsigned long field;
  7443. u64 field_value;
  7444. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7445. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7446. gva_t gva = 0;
  7447. struct vmcs12 *vmcs12;
  7448. if (!nested_vmx_check_permission(vcpu))
  7449. return 1;
  7450. if (!nested_vmx_check_vmcs12(vcpu))
  7451. return kvm_skip_emulated_instruction(vcpu);
  7452. if (!is_guest_mode(vcpu))
  7453. vmcs12 = get_vmcs12(vcpu);
  7454. else {
  7455. /*
  7456. * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
  7457. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7458. */
  7459. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7460. nested_vmx_failInvalid(vcpu);
  7461. return kvm_skip_emulated_instruction(vcpu);
  7462. }
  7463. vmcs12 = get_shadow_vmcs12(vcpu);
  7464. }
  7465. /* Decode instruction info and find the field to read */
  7466. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7467. /* Read the field, zero-extended to a u64 field_value */
  7468. if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
  7469. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7470. return kvm_skip_emulated_instruction(vcpu);
  7471. }
  7472. /*
  7473. * Now copy part of this value to register or memory, as requested.
  7474. * Note that the number of bits actually copied is 32 or 64 depending
  7475. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7476. */
  7477. if (vmx_instruction_info & (1u << 10)) {
  7478. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7479. field_value);
  7480. } else {
  7481. if (get_vmx_mem_address(vcpu, exit_qualification,
  7482. vmx_instruction_info, true, &gva))
  7483. return 1;
  7484. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7485. kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7486. (is_long_mode(vcpu) ? 8 : 4), NULL);
  7487. }
  7488. nested_vmx_succeed(vcpu);
  7489. return kvm_skip_emulated_instruction(vcpu);
  7490. }
  7491. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7492. {
  7493. unsigned long field;
  7494. gva_t gva;
  7495. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7496. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7497. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7498. /* The value to write might be 32 or 64 bits, depending on L1's long
  7499. * mode, and eventually we need to write that into a field of several
  7500. * possible lengths. The code below first zero-extends the value to 64
  7501. * bit (field_value), and then copies only the appropriate number of
  7502. * bits into the vmcs12 field.
  7503. */
  7504. u64 field_value = 0;
  7505. struct x86_exception e;
  7506. struct vmcs12 *vmcs12;
  7507. if (!nested_vmx_check_permission(vcpu))
  7508. return 1;
  7509. if (!nested_vmx_check_vmcs12(vcpu))
  7510. return kvm_skip_emulated_instruction(vcpu);
  7511. if (vmx_instruction_info & (1u << 10))
  7512. field_value = kvm_register_readl(vcpu,
  7513. (((vmx_instruction_info) >> 3) & 0xf));
  7514. else {
  7515. if (get_vmx_mem_address(vcpu, exit_qualification,
  7516. vmx_instruction_info, false, &gva))
  7517. return 1;
  7518. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7519. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7520. kvm_inject_page_fault(vcpu, &e);
  7521. return 1;
  7522. }
  7523. }
  7524. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7525. /*
  7526. * If the vCPU supports "VMWRITE to any supported field in the
  7527. * VMCS," then the "read-only" fields are actually read/write.
  7528. */
  7529. if (vmcs_field_readonly(field) &&
  7530. !nested_cpu_has_vmwrite_any_field(vcpu)) {
  7531. nested_vmx_failValid(vcpu,
  7532. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7533. return kvm_skip_emulated_instruction(vcpu);
  7534. }
  7535. if (!is_guest_mode(vcpu))
  7536. vmcs12 = get_vmcs12(vcpu);
  7537. else {
  7538. /*
  7539. * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
  7540. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7541. */
  7542. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7543. nested_vmx_failInvalid(vcpu);
  7544. return kvm_skip_emulated_instruction(vcpu);
  7545. }
  7546. vmcs12 = get_shadow_vmcs12(vcpu);
  7547. }
  7548. if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
  7549. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7550. return kvm_skip_emulated_instruction(vcpu);
  7551. }
  7552. /*
  7553. * Do not track vmcs12 dirty-state if in guest-mode
  7554. * as we actually dirty shadow vmcs12 instead of vmcs12.
  7555. */
  7556. if (!is_guest_mode(vcpu)) {
  7557. switch (field) {
  7558. #define SHADOW_FIELD_RW(x) case x:
  7559. #include "vmx_shadow_fields.h"
  7560. /*
  7561. * The fields that can be updated by L1 without a vmexit are
  7562. * always updated in the vmcs02, the others go down the slow
  7563. * path of prepare_vmcs02.
  7564. */
  7565. break;
  7566. default:
  7567. vmx->nested.dirty_vmcs12 = true;
  7568. break;
  7569. }
  7570. }
  7571. nested_vmx_succeed(vcpu);
  7572. return kvm_skip_emulated_instruction(vcpu);
  7573. }
  7574. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7575. {
  7576. vmx->nested.current_vmptr = vmptr;
  7577. if (enable_shadow_vmcs) {
  7578. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7579. SECONDARY_EXEC_SHADOW_VMCS);
  7580. vmcs_write64(VMCS_LINK_POINTER,
  7581. __pa(vmx->vmcs01.shadow_vmcs));
  7582. vmx->nested.sync_shadow_vmcs = true;
  7583. }
  7584. vmx->nested.dirty_vmcs12 = true;
  7585. }
  7586. /* Emulate the VMPTRLD instruction */
  7587. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7588. {
  7589. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7590. gpa_t vmptr;
  7591. if (!nested_vmx_check_permission(vcpu))
  7592. return 1;
  7593. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7594. return 1;
  7595. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7596. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  7597. return kvm_skip_emulated_instruction(vcpu);
  7598. }
  7599. if (vmptr == vmx->nested.vmxon_ptr) {
  7600. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  7601. return kvm_skip_emulated_instruction(vcpu);
  7602. }
  7603. if (vmx->nested.current_vmptr != vmptr) {
  7604. struct vmcs12 *new_vmcs12;
  7605. struct page *page;
  7606. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7607. if (is_error_page(page)) {
  7608. nested_vmx_failInvalid(vcpu);
  7609. return kvm_skip_emulated_instruction(vcpu);
  7610. }
  7611. new_vmcs12 = kmap(page);
  7612. if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  7613. (new_vmcs12->hdr.shadow_vmcs &&
  7614. !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
  7615. kunmap(page);
  7616. kvm_release_page_clean(page);
  7617. nested_vmx_failValid(vcpu,
  7618. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7619. return kvm_skip_emulated_instruction(vcpu);
  7620. }
  7621. nested_release_vmcs12(vmx);
  7622. /*
  7623. * Load VMCS12 from guest memory since it is not already
  7624. * cached.
  7625. */
  7626. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7627. kunmap(page);
  7628. kvm_release_page_clean(page);
  7629. set_current_vmptr(vmx, vmptr);
  7630. }
  7631. nested_vmx_succeed(vcpu);
  7632. return kvm_skip_emulated_instruction(vcpu);
  7633. }
  7634. /* Emulate the VMPTRST instruction */
  7635. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  7636. {
  7637. unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
  7638. u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7639. gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
  7640. struct x86_exception e;
  7641. gva_t gva;
  7642. if (!nested_vmx_check_permission(vcpu))
  7643. return 1;
  7644. if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
  7645. return 1;
  7646. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  7647. if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
  7648. sizeof(gpa_t), &e)) {
  7649. kvm_inject_page_fault(vcpu, &e);
  7650. return 1;
  7651. }
  7652. nested_vmx_succeed(vcpu);
  7653. return kvm_skip_emulated_instruction(vcpu);
  7654. }
  7655. /* Emulate the INVEPT instruction */
  7656. static int handle_invept(struct kvm_vcpu *vcpu)
  7657. {
  7658. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7659. u32 vmx_instruction_info, types;
  7660. unsigned long type;
  7661. gva_t gva;
  7662. struct x86_exception e;
  7663. struct {
  7664. u64 eptp, gpa;
  7665. } operand;
  7666. if (!(vmx->nested.msrs.secondary_ctls_high &
  7667. SECONDARY_EXEC_ENABLE_EPT) ||
  7668. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  7669. kvm_queue_exception(vcpu, UD_VECTOR);
  7670. return 1;
  7671. }
  7672. if (!nested_vmx_check_permission(vcpu))
  7673. return 1;
  7674. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7675. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7676. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  7677. if (type >= 32 || !(types & (1 << type))) {
  7678. nested_vmx_failValid(vcpu,
  7679. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7680. return kvm_skip_emulated_instruction(vcpu);
  7681. }
  7682. /* According to the Intel VMX instruction reference, the memory
  7683. * operand is read even if it isn't needed (e.g., for type==global)
  7684. */
  7685. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7686. vmx_instruction_info, false, &gva))
  7687. return 1;
  7688. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7689. kvm_inject_page_fault(vcpu, &e);
  7690. return 1;
  7691. }
  7692. switch (type) {
  7693. case VMX_EPT_EXTENT_GLOBAL:
  7694. /*
  7695. * TODO: track mappings and invalidate
  7696. * single context requests appropriately
  7697. */
  7698. case VMX_EPT_EXTENT_CONTEXT:
  7699. kvm_mmu_sync_roots(vcpu);
  7700. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7701. nested_vmx_succeed(vcpu);
  7702. break;
  7703. default:
  7704. BUG_ON(1);
  7705. break;
  7706. }
  7707. return kvm_skip_emulated_instruction(vcpu);
  7708. }
  7709. static int handle_invvpid(struct kvm_vcpu *vcpu)
  7710. {
  7711. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7712. u32 vmx_instruction_info;
  7713. unsigned long type, types;
  7714. gva_t gva;
  7715. struct x86_exception e;
  7716. struct {
  7717. u64 vpid;
  7718. u64 gla;
  7719. } operand;
  7720. if (!(vmx->nested.msrs.secondary_ctls_high &
  7721. SECONDARY_EXEC_ENABLE_VPID) ||
  7722. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  7723. kvm_queue_exception(vcpu, UD_VECTOR);
  7724. return 1;
  7725. }
  7726. if (!nested_vmx_check_permission(vcpu))
  7727. return 1;
  7728. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7729. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7730. types = (vmx->nested.msrs.vpid_caps &
  7731. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  7732. if (type >= 32 || !(types & (1 << type))) {
  7733. nested_vmx_failValid(vcpu,
  7734. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7735. return kvm_skip_emulated_instruction(vcpu);
  7736. }
  7737. /* according to the intel vmx instruction reference, the memory
  7738. * operand is read even if it isn't needed (e.g., for type==global)
  7739. */
  7740. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7741. vmx_instruction_info, false, &gva))
  7742. return 1;
  7743. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7744. kvm_inject_page_fault(vcpu, &e);
  7745. return 1;
  7746. }
  7747. if (operand.vpid >> 16) {
  7748. nested_vmx_failValid(vcpu,
  7749. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7750. return kvm_skip_emulated_instruction(vcpu);
  7751. }
  7752. switch (type) {
  7753. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  7754. if (!operand.vpid ||
  7755. is_noncanonical_address(operand.gla, vcpu)) {
  7756. nested_vmx_failValid(vcpu,
  7757. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7758. return kvm_skip_emulated_instruction(vcpu);
  7759. }
  7760. if (cpu_has_vmx_invvpid_individual_addr() &&
  7761. vmx->nested.vpid02) {
  7762. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  7763. vmx->nested.vpid02, operand.gla);
  7764. } else
  7765. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7766. break;
  7767. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  7768. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  7769. if (!operand.vpid) {
  7770. nested_vmx_failValid(vcpu,
  7771. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7772. return kvm_skip_emulated_instruction(vcpu);
  7773. }
  7774. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7775. break;
  7776. case VMX_VPID_EXTENT_ALL_CONTEXT:
  7777. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7778. break;
  7779. default:
  7780. WARN_ON_ONCE(1);
  7781. return kvm_skip_emulated_instruction(vcpu);
  7782. }
  7783. nested_vmx_succeed(vcpu);
  7784. return kvm_skip_emulated_instruction(vcpu);
  7785. }
  7786. static int handle_invpcid(struct kvm_vcpu *vcpu)
  7787. {
  7788. u32 vmx_instruction_info;
  7789. unsigned long type;
  7790. bool pcid_enabled;
  7791. gva_t gva;
  7792. struct x86_exception e;
  7793. unsigned i;
  7794. unsigned long roots_to_free = 0;
  7795. struct {
  7796. u64 pcid;
  7797. u64 gla;
  7798. } operand;
  7799. if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
  7800. kvm_queue_exception(vcpu, UD_VECTOR);
  7801. return 1;
  7802. }
  7803. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7804. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7805. if (type > 3) {
  7806. kvm_inject_gp(vcpu, 0);
  7807. return 1;
  7808. }
  7809. /* According to the Intel instruction reference, the memory operand
  7810. * is read even if it isn't needed (e.g., for type==all)
  7811. */
  7812. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7813. vmx_instruction_info, false, &gva))
  7814. return 1;
  7815. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7816. kvm_inject_page_fault(vcpu, &e);
  7817. return 1;
  7818. }
  7819. if (operand.pcid >> 12 != 0) {
  7820. kvm_inject_gp(vcpu, 0);
  7821. return 1;
  7822. }
  7823. pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
  7824. switch (type) {
  7825. case INVPCID_TYPE_INDIV_ADDR:
  7826. if ((!pcid_enabled && (operand.pcid != 0)) ||
  7827. is_noncanonical_address(operand.gla, vcpu)) {
  7828. kvm_inject_gp(vcpu, 0);
  7829. return 1;
  7830. }
  7831. kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
  7832. return kvm_skip_emulated_instruction(vcpu);
  7833. case INVPCID_TYPE_SINGLE_CTXT:
  7834. if (!pcid_enabled && (operand.pcid != 0)) {
  7835. kvm_inject_gp(vcpu, 0);
  7836. return 1;
  7837. }
  7838. if (kvm_get_active_pcid(vcpu) == operand.pcid) {
  7839. kvm_mmu_sync_roots(vcpu);
  7840. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7841. }
  7842. for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
  7843. if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
  7844. == operand.pcid)
  7845. roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
  7846. kvm_mmu_free_roots(vcpu, roots_to_free);
  7847. /*
  7848. * If neither the current cr3 nor any of the prev_roots use the
  7849. * given PCID, then nothing needs to be done here because a
  7850. * resync will happen anyway before switching to any other CR3.
  7851. */
  7852. return kvm_skip_emulated_instruction(vcpu);
  7853. case INVPCID_TYPE_ALL_NON_GLOBAL:
  7854. /*
  7855. * Currently, KVM doesn't mark global entries in the shadow
  7856. * page tables, so a non-global flush just degenerates to a
  7857. * global flush. If needed, we could optimize this later by
  7858. * keeping track of global entries in shadow page tables.
  7859. */
  7860. /* fall-through */
  7861. case INVPCID_TYPE_ALL_INCL_GLOBAL:
  7862. kvm_mmu_unload(vcpu);
  7863. return kvm_skip_emulated_instruction(vcpu);
  7864. default:
  7865. BUG(); /* We have already checked above that type <= 3 */
  7866. }
  7867. }
  7868. static int handle_pml_full(struct kvm_vcpu *vcpu)
  7869. {
  7870. unsigned long exit_qualification;
  7871. trace_kvm_pml_full(vcpu->vcpu_id);
  7872. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7873. /*
  7874. * PML buffer FULL happened while executing iret from NMI,
  7875. * "blocked by NMI" bit has to be set before next VM entry.
  7876. */
  7877. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7878. enable_vnmi &&
  7879. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  7880. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7881. GUEST_INTR_STATE_NMI);
  7882. /*
  7883. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  7884. * here.., and there's no userspace involvement needed for PML.
  7885. */
  7886. return 1;
  7887. }
  7888. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  7889. {
  7890. kvm_lapic_expired_hv_timer(vcpu);
  7891. return 1;
  7892. }
  7893. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  7894. {
  7895. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7896. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7897. /* Check for memory type validity */
  7898. switch (address & VMX_EPTP_MT_MASK) {
  7899. case VMX_EPTP_MT_UC:
  7900. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  7901. return false;
  7902. break;
  7903. case VMX_EPTP_MT_WB:
  7904. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  7905. return false;
  7906. break;
  7907. default:
  7908. return false;
  7909. }
  7910. /* only 4 levels page-walk length are valid */
  7911. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  7912. return false;
  7913. /* Reserved bits should not be set */
  7914. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  7915. return false;
  7916. /* AD, if set, should be supported */
  7917. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  7918. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  7919. return false;
  7920. }
  7921. return true;
  7922. }
  7923. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  7924. struct vmcs12 *vmcs12)
  7925. {
  7926. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  7927. u64 address;
  7928. bool accessed_dirty;
  7929. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  7930. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  7931. !nested_cpu_has_ept(vmcs12))
  7932. return 1;
  7933. if (index >= VMFUNC_EPTP_ENTRIES)
  7934. return 1;
  7935. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7936. &address, index * 8, 8))
  7937. return 1;
  7938. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7939. /*
  7940. * If the (L2) guest does a vmfunc to the currently
  7941. * active ept pointer, we don't have to do anything else
  7942. */
  7943. if (vmcs12->ept_pointer != address) {
  7944. if (!valid_ept_address(vcpu, address))
  7945. return 1;
  7946. kvm_mmu_unload(vcpu);
  7947. mmu->ept_ad = accessed_dirty;
  7948. mmu->base_role.ad_disabled = !accessed_dirty;
  7949. vmcs12->ept_pointer = address;
  7950. /*
  7951. * TODO: Check what's the correct approach in case
  7952. * mmu reload fails. Currently, we just let the next
  7953. * reload potentially fail
  7954. */
  7955. kvm_mmu_reload(vcpu);
  7956. }
  7957. return 0;
  7958. }
  7959. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7960. {
  7961. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7962. struct vmcs12 *vmcs12;
  7963. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  7964. /*
  7965. * VMFUNC is only supported for nested guests, but we always enable the
  7966. * secondary control for simplicity; for non-nested mode, fake that we
  7967. * didn't by injecting #UD.
  7968. */
  7969. if (!is_guest_mode(vcpu)) {
  7970. kvm_queue_exception(vcpu, UD_VECTOR);
  7971. return 1;
  7972. }
  7973. vmcs12 = get_vmcs12(vcpu);
  7974. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  7975. goto fail;
  7976. switch (function) {
  7977. case 0:
  7978. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  7979. goto fail;
  7980. break;
  7981. default:
  7982. goto fail;
  7983. }
  7984. return kvm_skip_emulated_instruction(vcpu);
  7985. fail:
  7986. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  7987. vmcs_read32(VM_EXIT_INTR_INFO),
  7988. vmcs_readl(EXIT_QUALIFICATION));
  7989. return 1;
  7990. }
  7991. static int handle_encls(struct kvm_vcpu *vcpu)
  7992. {
  7993. /*
  7994. * SGX virtualization is not yet supported. There is no software
  7995. * enable bit for SGX, so we have to trap ENCLS and inject a #UD
  7996. * to prevent the guest from executing ENCLS.
  7997. */
  7998. kvm_queue_exception(vcpu, UD_VECTOR);
  7999. return 1;
  8000. }
  8001. /*
  8002. * The exit handlers return 1 if the exit was handled fully and guest execution
  8003. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  8004. * to be done to userspace and return 0.
  8005. */
  8006. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  8007. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  8008. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  8009. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  8010. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  8011. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  8012. [EXIT_REASON_CR_ACCESS] = handle_cr,
  8013. [EXIT_REASON_DR_ACCESS] = handle_dr,
  8014. [EXIT_REASON_CPUID] = handle_cpuid,
  8015. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  8016. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  8017. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  8018. [EXIT_REASON_HLT] = handle_halt,
  8019. [EXIT_REASON_INVD] = handle_invd,
  8020. [EXIT_REASON_INVLPG] = handle_invlpg,
  8021. [EXIT_REASON_RDPMC] = handle_rdpmc,
  8022. [EXIT_REASON_VMCALL] = handle_vmcall,
  8023. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  8024. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  8025. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  8026. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  8027. [EXIT_REASON_VMREAD] = handle_vmread,
  8028. [EXIT_REASON_VMRESUME] = handle_vmresume,
  8029. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  8030. [EXIT_REASON_VMOFF] = handle_vmoff,
  8031. [EXIT_REASON_VMON] = handle_vmon,
  8032. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  8033. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  8034. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  8035. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  8036. [EXIT_REASON_WBINVD] = handle_wbinvd,
  8037. [EXIT_REASON_XSETBV] = handle_xsetbv,
  8038. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  8039. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  8040. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  8041. [EXIT_REASON_LDTR_TR] = handle_desc,
  8042. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  8043. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  8044. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  8045. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  8046. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  8047. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  8048. [EXIT_REASON_INVEPT] = handle_invept,
  8049. [EXIT_REASON_INVVPID] = handle_invvpid,
  8050. [EXIT_REASON_RDRAND] = handle_invalid_op,
  8051. [EXIT_REASON_RDSEED] = handle_invalid_op,
  8052. [EXIT_REASON_XSAVES] = handle_xsaves,
  8053. [EXIT_REASON_XRSTORS] = handle_xrstors,
  8054. [EXIT_REASON_PML_FULL] = handle_pml_full,
  8055. [EXIT_REASON_INVPCID] = handle_invpcid,
  8056. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  8057. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  8058. [EXIT_REASON_ENCLS] = handle_encls,
  8059. };
  8060. static const int kvm_vmx_max_exit_handlers =
  8061. ARRAY_SIZE(kvm_vmx_exit_handlers);
  8062. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  8063. struct vmcs12 *vmcs12)
  8064. {
  8065. unsigned long exit_qualification;
  8066. gpa_t bitmap, last_bitmap;
  8067. unsigned int port;
  8068. int size;
  8069. u8 b;
  8070. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  8071. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  8072. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8073. port = exit_qualification >> 16;
  8074. size = (exit_qualification & 7) + 1;
  8075. last_bitmap = (gpa_t)-1;
  8076. b = -1;
  8077. while (size > 0) {
  8078. if (port < 0x8000)
  8079. bitmap = vmcs12->io_bitmap_a;
  8080. else if (port < 0x10000)
  8081. bitmap = vmcs12->io_bitmap_b;
  8082. else
  8083. return true;
  8084. bitmap += (port & 0x7fff) / 8;
  8085. if (last_bitmap != bitmap)
  8086. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  8087. return true;
  8088. if (b & (1 << (port & 7)))
  8089. return true;
  8090. port++;
  8091. size--;
  8092. last_bitmap = bitmap;
  8093. }
  8094. return false;
  8095. }
  8096. /*
  8097. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  8098. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  8099. * disinterest in the current event (read or write a specific MSR) by using an
  8100. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  8101. */
  8102. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  8103. struct vmcs12 *vmcs12, u32 exit_reason)
  8104. {
  8105. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  8106. gpa_t bitmap;
  8107. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8108. return true;
  8109. /*
  8110. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  8111. * for the four combinations of read/write and low/high MSR numbers.
  8112. * First we need to figure out which of the four to use:
  8113. */
  8114. bitmap = vmcs12->msr_bitmap;
  8115. if (exit_reason == EXIT_REASON_MSR_WRITE)
  8116. bitmap += 2048;
  8117. if (msr_index >= 0xc0000000) {
  8118. msr_index -= 0xc0000000;
  8119. bitmap += 1024;
  8120. }
  8121. /* Then read the msr_index'th bit from this bitmap: */
  8122. if (msr_index < 1024*8) {
  8123. unsigned char b;
  8124. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  8125. return true;
  8126. return 1 & (b >> (msr_index & 7));
  8127. } else
  8128. return true; /* let L1 handle the wrong parameter */
  8129. }
  8130. /*
  8131. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  8132. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  8133. * intercept (via guest_host_mask etc.) the current event.
  8134. */
  8135. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  8136. struct vmcs12 *vmcs12)
  8137. {
  8138. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8139. int cr = exit_qualification & 15;
  8140. int reg;
  8141. unsigned long val;
  8142. switch ((exit_qualification >> 4) & 3) {
  8143. case 0: /* mov to cr */
  8144. reg = (exit_qualification >> 8) & 15;
  8145. val = kvm_register_readl(vcpu, reg);
  8146. switch (cr) {
  8147. case 0:
  8148. if (vmcs12->cr0_guest_host_mask &
  8149. (val ^ vmcs12->cr0_read_shadow))
  8150. return true;
  8151. break;
  8152. case 3:
  8153. if ((vmcs12->cr3_target_count >= 1 &&
  8154. vmcs12->cr3_target_value0 == val) ||
  8155. (vmcs12->cr3_target_count >= 2 &&
  8156. vmcs12->cr3_target_value1 == val) ||
  8157. (vmcs12->cr3_target_count >= 3 &&
  8158. vmcs12->cr3_target_value2 == val) ||
  8159. (vmcs12->cr3_target_count >= 4 &&
  8160. vmcs12->cr3_target_value3 == val))
  8161. return false;
  8162. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  8163. return true;
  8164. break;
  8165. case 4:
  8166. if (vmcs12->cr4_guest_host_mask &
  8167. (vmcs12->cr4_read_shadow ^ val))
  8168. return true;
  8169. break;
  8170. case 8:
  8171. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  8172. return true;
  8173. break;
  8174. }
  8175. break;
  8176. case 2: /* clts */
  8177. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  8178. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  8179. return true;
  8180. break;
  8181. case 1: /* mov from cr */
  8182. switch (cr) {
  8183. case 3:
  8184. if (vmcs12->cpu_based_vm_exec_control &
  8185. CPU_BASED_CR3_STORE_EXITING)
  8186. return true;
  8187. break;
  8188. case 8:
  8189. if (vmcs12->cpu_based_vm_exec_control &
  8190. CPU_BASED_CR8_STORE_EXITING)
  8191. return true;
  8192. break;
  8193. }
  8194. break;
  8195. case 3: /* lmsw */
  8196. /*
  8197. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  8198. * cr0. Other attempted changes are ignored, with no exit.
  8199. */
  8200. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  8201. if (vmcs12->cr0_guest_host_mask & 0xe &
  8202. (val ^ vmcs12->cr0_read_shadow))
  8203. return true;
  8204. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  8205. !(vmcs12->cr0_read_shadow & 0x1) &&
  8206. (val & 0x1))
  8207. return true;
  8208. break;
  8209. }
  8210. return false;
  8211. }
  8212. static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
  8213. struct vmcs12 *vmcs12, gpa_t bitmap)
  8214. {
  8215. u32 vmx_instruction_info;
  8216. unsigned long field;
  8217. u8 b;
  8218. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  8219. return true;
  8220. /* Decode instruction info and find the field to access */
  8221. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8222. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  8223. /* Out-of-range fields always cause a VM exit from L2 to L1 */
  8224. if (field >> 15)
  8225. return true;
  8226. if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
  8227. return true;
  8228. return 1 & (b >> (field & 7));
  8229. }
  8230. /*
  8231. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  8232. * should handle it ourselves in L0 (and then continue L2). Only call this
  8233. * when in is_guest_mode (L2).
  8234. */
  8235. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  8236. {
  8237. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8238. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8239. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8240. if (vmx->nested.nested_run_pending)
  8241. return false;
  8242. if (unlikely(vmx->fail)) {
  8243. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  8244. vmcs_read32(VM_INSTRUCTION_ERROR));
  8245. return true;
  8246. }
  8247. /*
  8248. * The host physical addresses of some pages of guest memory
  8249. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  8250. * Page). The CPU may write to these pages via their host
  8251. * physical address while L2 is running, bypassing any
  8252. * address-translation-based dirty tracking (e.g. EPT write
  8253. * protection).
  8254. *
  8255. * Mark them dirty on every exit from L2 to prevent them from
  8256. * getting out of sync with dirty tracking.
  8257. */
  8258. nested_mark_vmcs12_pages_dirty(vcpu);
  8259. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  8260. vmcs_readl(EXIT_QUALIFICATION),
  8261. vmx->idt_vectoring_info,
  8262. intr_info,
  8263. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8264. KVM_ISA_VMX);
  8265. switch (exit_reason) {
  8266. case EXIT_REASON_EXCEPTION_NMI:
  8267. if (is_nmi(intr_info))
  8268. return false;
  8269. else if (is_page_fault(intr_info))
  8270. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  8271. else if (is_no_device(intr_info) &&
  8272. !(vmcs12->guest_cr0 & X86_CR0_TS))
  8273. return false;
  8274. else if (is_debug(intr_info) &&
  8275. vcpu->guest_debug &
  8276. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  8277. return false;
  8278. else if (is_breakpoint(intr_info) &&
  8279. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  8280. return false;
  8281. return vmcs12->exception_bitmap &
  8282. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  8283. case EXIT_REASON_EXTERNAL_INTERRUPT:
  8284. return false;
  8285. case EXIT_REASON_TRIPLE_FAULT:
  8286. return true;
  8287. case EXIT_REASON_PENDING_INTERRUPT:
  8288. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  8289. case EXIT_REASON_NMI_WINDOW:
  8290. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  8291. case EXIT_REASON_TASK_SWITCH:
  8292. return true;
  8293. case EXIT_REASON_CPUID:
  8294. return true;
  8295. case EXIT_REASON_HLT:
  8296. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  8297. case EXIT_REASON_INVD:
  8298. return true;
  8299. case EXIT_REASON_INVLPG:
  8300. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8301. case EXIT_REASON_RDPMC:
  8302. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  8303. case EXIT_REASON_RDRAND:
  8304. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  8305. case EXIT_REASON_RDSEED:
  8306. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  8307. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  8308. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  8309. case EXIT_REASON_VMREAD:
  8310. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8311. vmcs12->vmread_bitmap);
  8312. case EXIT_REASON_VMWRITE:
  8313. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8314. vmcs12->vmwrite_bitmap);
  8315. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  8316. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  8317. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
  8318. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  8319. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  8320. /*
  8321. * VMX instructions trap unconditionally. This allows L1 to
  8322. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  8323. */
  8324. return true;
  8325. case EXIT_REASON_CR_ACCESS:
  8326. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  8327. case EXIT_REASON_DR_ACCESS:
  8328. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  8329. case EXIT_REASON_IO_INSTRUCTION:
  8330. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  8331. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  8332. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  8333. case EXIT_REASON_MSR_READ:
  8334. case EXIT_REASON_MSR_WRITE:
  8335. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  8336. case EXIT_REASON_INVALID_STATE:
  8337. return true;
  8338. case EXIT_REASON_MWAIT_INSTRUCTION:
  8339. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  8340. case EXIT_REASON_MONITOR_TRAP_FLAG:
  8341. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  8342. case EXIT_REASON_MONITOR_INSTRUCTION:
  8343. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  8344. case EXIT_REASON_PAUSE_INSTRUCTION:
  8345. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  8346. nested_cpu_has2(vmcs12,
  8347. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  8348. case EXIT_REASON_MCE_DURING_VMENTRY:
  8349. return false;
  8350. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  8351. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  8352. case EXIT_REASON_APIC_ACCESS:
  8353. case EXIT_REASON_APIC_WRITE:
  8354. case EXIT_REASON_EOI_INDUCED:
  8355. /*
  8356. * The controls for "virtualize APIC accesses," "APIC-
  8357. * register virtualization," and "virtual-interrupt
  8358. * delivery" only come from vmcs12.
  8359. */
  8360. return true;
  8361. case EXIT_REASON_EPT_VIOLATION:
  8362. /*
  8363. * L0 always deals with the EPT violation. If nested EPT is
  8364. * used, and the nested mmu code discovers that the address is
  8365. * missing in the guest EPT table (EPT12), the EPT violation
  8366. * will be injected with nested_ept_inject_page_fault()
  8367. */
  8368. return false;
  8369. case EXIT_REASON_EPT_MISCONFIG:
  8370. /*
  8371. * L2 never uses directly L1's EPT, but rather L0's own EPT
  8372. * table (shadow on EPT) or a merged EPT table that L0 built
  8373. * (EPT on EPT). So any problems with the structure of the
  8374. * table is L0's fault.
  8375. */
  8376. return false;
  8377. case EXIT_REASON_INVPCID:
  8378. return
  8379. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  8380. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8381. case EXIT_REASON_WBINVD:
  8382. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  8383. case EXIT_REASON_XSETBV:
  8384. return true;
  8385. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  8386. /*
  8387. * This should never happen, since it is not possible to
  8388. * set XSS to a non-zero value---neither in L1 nor in L2.
  8389. * If if it were, XSS would have to be checked against
  8390. * the XSS exit bitmap in vmcs12.
  8391. */
  8392. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  8393. case EXIT_REASON_PREEMPTION_TIMER:
  8394. return false;
  8395. case EXIT_REASON_PML_FULL:
  8396. /* We emulate PML support to L1. */
  8397. return false;
  8398. case EXIT_REASON_VMFUNC:
  8399. /* VM functions are emulated through L2->L0 vmexits. */
  8400. return false;
  8401. case EXIT_REASON_ENCLS:
  8402. /* SGX is never exposed to L1 */
  8403. return false;
  8404. default:
  8405. return true;
  8406. }
  8407. }
  8408. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  8409. {
  8410. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8411. /*
  8412. * At this point, the exit interruption info in exit_intr_info
  8413. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  8414. * we need to query the in-kernel LAPIC.
  8415. */
  8416. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  8417. if ((exit_intr_info &
  8418. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8419. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  8420. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8421. vmcs12->vm_exit_intr_error_code =
  8422. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8423. }
  8424. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  8425. vmcs_readl(EXIT_QUALIFICATION));
  8426. return 1;
  8427. }
  8428. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  8429. {
  8430. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  8431. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  8432. }
  8433. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  8434. {
  8435. if (vmx->pml_pg) {
  8436. __free_page(vmx->pml_pg);
  8437. vmx->pml_pg = NULL;
  8438. }
  8439. }
  8440. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  8441. {
  8442. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8443. u64 *pml_buf;
  8444. u16 pml_idx;
  8445. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  8446. /* Do nothing if PML buffer is empty */
  8447. if (pml_idx == (PML_ENTITY_NUM - 1))
  8448. return;
  8449. /* PML index always points to next available PML buffer entity */
  8450. if (pml_idx >= PML_ENTITY_NUM)
  8451. pml_idx = 0;
  8452. else
  8453. pml_idx++;
  8454. pml_buf = page_address(vmx->pml_pg);
  8455. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  8456. u64 gpa;
  8457. gpa = pml_buf[pml_idx];
  8458. WARN_ON(gpa & (PAGE_SIZE - 1));
  8459. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  8460. }
  8461. /* reset PML index */
  8462. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  8463. }
  8464. /*
  8465. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  8466. * Called before reporting dirty_bitmap to userspace.
  8467. */
  8468. static void kvm_flush_pml_buffers(struct kvm *kvm)
  8469. {
  8470. int i;
  8471. struct kvm_vcpu *vcpu;
  8472. /*
  8473. * We only need to kick vcpu out of guest mode here, as PML buffer
  8474. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8475. * vcpus running in guest are possible to have unflushed GPAs in PML
  8476. * buffer.
  8477. */
  8478. kvm_for_each_vcpu(i, vcpu, kvm)
  8479. kvm_vcpu_kick(vcpu);
  8480. }
  8481. static void vmx_dump_sel(char *name, uint32_t sel)
  8482. {
  8483. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8484. name, vmcs_read16(sel),
  8485. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8486. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8487. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8488. }
  8489. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8490. {
  8491. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8492. name, vmcs_read32(limit),
  8493. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8494. }
  8495. static void dump_vmcs(void)
  8496. {
  8497. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8498. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8499. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8500. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8501. u32 secondary_exec_control = 0;
  8502. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8503. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8504. int i, n;
  8505. if (cpu_has_secondary_exec_ctrls())
  8506. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8507. pr_err("*** Guest State ***\n");
  8508. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8509. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8510. vmcs_readl(CR0_GUEST_HOST_MASK));
  8511. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8512. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8513. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8514. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8515. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8516. {
  8517. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8518. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8519. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8520. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8521. }
  8522. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8523. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8524. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8525. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8526. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8527. vmcs_readl(GUEST_SYSENTER_ESP),
  8528. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8529. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8530. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8531. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8532. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8533. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8534. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8535. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8536. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8537. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8538. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8539. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8540. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8541. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8542. efer, vmcs_read64(GUEST_IA32_PAT));
  8543. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8544. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8545. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8546. if (cpu_has_load_perf_global_ctrl &&
  8547. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8548. pr_err("PerfGlobCtl = 0x%016llx\n",
  8549. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8550. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8551. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8552. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8553. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8554. vmcs_read32(GUEST_ACTIVITY_STATE));
  8555. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8556. pr_err("InterruptStatus = %04x\n",
  8557. vmcs_read16(GUEST_INTR_STATUS));
  8558. pr_err("*** Host State ***\n");
  8559. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8560. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8561. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8562. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8563. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8564. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8565. vmcs_read16(HOST_TR_SELECTOR));
  8566. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8567. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8568. vmcs_readl(HOST_TR_BASE));
  8569. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8570. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8571. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8572. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8573. vmcs_readl(HOST_CR4));
  8574. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8575. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8576. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8577. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8578. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8579. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8580. vmcs_read64(HOST_IA32_EFER),
  8581. vmcs_read64(HOST_IA32_PAT));
  8582. if (cpu_has_load_perf_global_ctrl &&
  8583. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8584. pr_err("PerfGlobCtl = 0x%016llx\n",
  8585. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8586. pr_err("*** Control State ***\n");
  8587. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8588. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8589. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8590. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8591. vmcs_read32(EXCEPTION_BITMAP),
  8592. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8593. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8594. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8595. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8596. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  8597. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  8598. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  8599. vmcs_read32(VM_EXIT_INTR_INFO),
  8600. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8601. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  8602. pr_err(" reason=%08x qualification=%016lx\n",
  8603. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  8604. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  8605. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  8606. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  8607. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  8608. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  8609. pr_err("TSC Multiplier = 0x%016llx\n",
  8610. vmcs_read64(TSC_MULTIPLIER));
  8611. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  8612. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  8613. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  8614. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  8615. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  8616. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  8617. n = vmcs_read32(CR3_TARGET_COUNT);
  8618. for (i = 0; i + 1 < n; i += 4)
  8619. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  8620. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  8621. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  8622. if (i < n)
  8623. pr_err("CR3 target%u=%016lx\n",
  8624. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  8625. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  8626. pr_err("PLE Gap=%08x Window=%08x\n",
  8627. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  8628. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  8629. pr_err("Virtual processor ID = 0x%04x\n",
  8630. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  8631. }
  8632. /*
  8633. * The guest has exited. See if we can fix it or if we need userspace
  8634. * assistance.
  8635. */
  8636. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  8637. {
  8638. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8639. u32 exit_reason = vmx->exit_reason;
  8640. u32 vectoring_info = vmx->idt_vectoring_info;
  8641. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  8642. /*
  8643. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  8644. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  8645. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  8646. * mode as if vcpus is in root mode, the PML buffer must has been
  8647. * flushed already.
  8648. */
  8649. if (enable_pml)
  8650. vmx_flush_pml_buffer(vcpu);
  8651. /* If guest state is invalid, start emulating */
  8652. if (vmx->emulation_required)
  8653. return handle_invalid_guest_state(vcpu);
  8654. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  8655. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  8656. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  8657. dump_vmcs();
  8658. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8659. vcpu->run->fail_entry.hardware_entry_failure_reason
  8660. = exit_reason;
  8661. return 0;
  8662. }
  8663. if (unlikely(vmx->fail)) {
  8664. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8665. vcpu->run->fail_entry.hardware_entry_failure_reason
  8666. = vmcs_read32(VM_INSTRUCTION_ERROR);
  8667. return 0;
  8668. }
  8669. /*
  8670. * Note:
  8671. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  8672. * delivery event since it indicates guest is accessing MMIO.
  8673. * The vm-exit can be triggered again after return to guest that
  8674. * will cause infinite loop.
  8675. */
  8676. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8677. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  8678. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  8679. exit_reason != EXIT_REASON_PML_FULL &&
  8680. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  8681. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  8682. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  8683. vcpu->run->internal.ndata = 3;
  8684. vcpu->run->internal.data[0] = vectoring_info;
  8685. vcpu->run->internal.data[1] = exit_reason;
  8686. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  8687. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  8688. vcpu->run->internal.ndata++;
  8689. vcpu->run->internal.data[3] =
  8690. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  8691. }
  8692. return 0;
  8693. }
  8694. if (unlikely(!enable_vnmi &&
  8695. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  8696. if (vmx_interrupt_allowed(vcpu)) {
  8697. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8698. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  8699. vcpu->arch.nmi_pending) {
  8700. /*
  8701. * This CPU don't support us in finding the end of an
  8702. * NMI-blocked window if the guest runs with IRQs
  8703. * disabled. So we pull the trigger after 1 s of
  8704. * futile waiting, but inform the user about this.
  8705. */
  8706. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  8707. "state on VCPU %d after 1 s timeout\n",
  8708. __func__, vcpu->vcpu_id);
  8709. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8710. }
  8711. }
  8712. if (exit_reason < kvm_vmx_max_exit_handlers
  8713. && kvm_vmx_exit_handlers[exit_reason])
  8714. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  8715. else {
  8716. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  8717. exit_reason);
  8718. kvm_queue_exception(vcpu, UD_VECTOR);
  8719. return 1;
  8720. }
  8721. }
  8722. /*
  8723. * Software based L1D cache flush which is used when microcode providing
  8724. * the cache control MSR is not loaded.
  8725. *
  8726. * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
  8727. * flush it is required to read in 64 KiB because the replacement algorithm
  8728. * is not exactly LRU. This could be sized at runtime via topology
  8729. * information but as all relevant affected CPUs have 32KiB L1D cache size
  8730. * there is no point in doing so.
  8731. */
  8732. #define L1D_CACHE_ORDER 4
  8733. static void *vmx_l1d_flush_pages;
  8734. static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
  8735. {
  8736. int size = PAGE_SIZE << L1D_CACHE_ORDER;
  8737. /*
  8738. * This code is only executed when the the flush mode is 'cond' or
  8739. * 'always'
  8740. */
  8741. if (static_branch_likely(&vmx_l1d_flush_cond)) {
  8742. bool flush_l1d;
  8743. /*
  8744. * Clear the per-vcpu flush bit, it gets set again
  8745. * either from vcpu_run() or from one of the unsafe
  8746. * VMEXIT handlers.
  8747. */
  8748. flush_l1d = vcpu->arch.l1tf_flush_l1d;
  8749. vcpu->arch.l1tf_flush_l1d = false;
  8750. /*
  8751. * Clear the per-cpu flush bit, it gets set again from
  8752. * the interrupt handlers.
  8753. */
  8754. flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
  8755. kvm_clear_cpu_l1tf_flush_l1d();
  8756. if (!flush_l1d)
  8757. return;
  8758. }
  8759. vcpu->stat.l1d_flush++;
  8760. if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  8761. wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
  8762. return;
  8763. }
  8764. asm volatile(
  8765. /* First ensure the pages are in the TLB */
  8766. "xorl %%eax, %%eax\n"
  8767. ".Lpopulate_tlb:\n\t"
  8768. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  8769. "addl $4096, %%eax\n\t"
  8770. "cmpl %%eax, %[size]\n\t"
  8771. "jne .Lpopulate_tlb\n\t"
  8772. "xorl %%eax, %%eax\n\t"
  8773. "cpuid\n\t"
  8774. /* Now fill the cache */
  8775. "xorl %%eax, %%eax\n"
  8776. ".Lfill_cache:\n"
  8777. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  8778. "addl $64, %%eax\n\t"
  8779. "cmpl %%eax, %[size]\n\t"
  8780. "jne .Lfill_cache\n\t"
  8781. "lfence\n"
  8782. :: [flush_pages] "r" (vmx_l1d_flush_pages),
  8783. [size] "r" (size)
  8784. : "eax", "ebx", "ecx", "edx");
  8785. }
  8786. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  8787. {
  8788. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8789. if (is_guest_mode(vcpu) &&
  8790. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8791. return;
  8792. if (irr == -1 || tpr < irr) {
  8793. vmcs_write32(TPR_THRESHOLD, 0);
  8794. return;
  8795. }
  8796. vmcs_write32(TPR_THRESHOLD, irr);
  8797. }
  8798. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  8799. {
  8800. u32 sec_exec_control;
  8801. if (!lapic_in_kernel(vcpu))
  8802. return;
  8803. /* Postpone execution until vmcs01 is the current VMCS. */
  8804. if (is_guest_mode(vcpu)) {
  8805. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  8806. return;
  8807. }
  8808. if (!cpu_need_tpr_shadow(vcpu))
  8809. return;
  8810. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8811. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8812. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  8813. switch (kvm_get_apic_mode(vcpu)) {
  8814. case LAPIC_MODE_INVALID:
  8815. WARN_ONCE(true, "Invalid local APIC state");
  8816. case LAPIC_MODE_DISABLED:
  8817. break;
  8818. case LAPIC_MODE_XAPIC:
  8819. if (flexpriority_enabled) {
  8820. sec_exec_control |=
  8821. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8822. vmx_flush_tlb(vcpu, true);
  8823. }
  8824. break;
  8825. case LAPIC_MODE_X2APIC:
  8826. if (cpu_has_vmx_virtualize_x2apic_mode())
  8827. sec_exec_control |=
  8828. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  8829. break;
  8830. }
  8831. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  8832. vmx_update_msr_bitmap(vcpu);
  8833. }
  8834. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  8835. {
  8836. if (!is_guest_mode(vcpu)) {
  8837. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8838. vmx_flush_tlb(vcpu, true);
  8839. }
  8840. }
  8841. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  8842. {
  8843. u16 status;
  8844. u8 old;
  8845. if (max_isr == -1)
  8846. max_isr = 0;
  8847. status = vmcs_read16(GUEST_INTR_STATUS);
  8848. old = status >> 8;
  8849. if (max_isr != old) {
  8850. status &= 0xff;
  8851. status |= max_isr << 8;
  8852. vmcs_write16(GUEST_INTR_STATUS, status);
  8853. }
  8854. }
  8855. static void vmx_set_rvi(int vector)
  8856. {
  8857. u16 status;
  8858. u8 old;
  8859. if (vector == -1)
  8860. vector = 0;
  8861. status = vmcs_read16(GUEST_INTR_STATUS);
  8862. old = (u8)status & 0xff;
  8863. if ((u8)vector != old) {
  8864. status &= ~0xff;
  8865. status |= (u8)vector;
  8866. vmcs_write16(GUEST_INTR_STATUS, status);
  8867. }
  8868. }
  8869. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  8870. {
  8871. /*
  8872. * When running L2, updating RVI is only relevant when
  8873. * vmcs12 virtual-interrupt-delivery enabled.
  8874. * However, it can be enabled only when L1 also
  8875. * intercepts external-interrupts and in that case
  8876. * we should not update vmcs02 RVI but instead intercept
  8877. * interrupt. Therefore, do nothing when running L2.
  8878. */
  8879. if (!is_guest_mode(vcpu))
  8880. vmx_set_rvi(max_irr);
  8881. }
  8882. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  8883. {
  8884. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8885. int max_irr;
  8886. bool max_irr_updated;
  8887. WARN_ON(!vcpu->arch.apicv_active);
  8888. if (pi_test_on(&vmx->pi_desc)) {
  8889. pi_clear_on(&vmx->pi_desc);
  8890. /*
  8891. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  8892. * But on x86 this is just a compiler barrier anyway.
  8893. */
  8894. smp_mb__after_atomic();
  8895. max_irr_updated =
  8896. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  8897. /*
  8898. * If we are running L2 and L1 has a new pending interrupt
  8899. * which can be injected, we should re-evaluate
  8900. * what should be done with this new L1 interrupt.
  8901. * If L1 intercepts external-interrupts, we should
  8902. * exit from L2 to L1. Otherwise, interrupt should be
  8903. * delivered directly to L2.
  8904. */
  8905. if (is_guest_mode(vcpu) && max_irr_updated) {
  8906. if (nested_exit_on_intr(vcpu))
  8907. kvm_vcpu_exiting_guest_mode(vcpu);
  8908. else
  8909. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8910. }
  8911. } else {
  8912. max_irr = kvm_lapic_find_highest_irr(vcpu);
  8913. }
  8914. vmx_hwapic_irr_update(vcpu, max_irr);
  8915. return max_irr;
  8916. }
  8917. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  8918. {
  8919. if (!kvm_vcpu_apicv_active(vcpu))
  8920. return;
  8921. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  8922. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  8923. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  8924. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  8925. }
  8926. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  8927. {
  8928. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8929. pi_clear_on(&vmx->pi_desc);
  8930. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  8931. }
  8932. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  8933. {
  8934. u32 exit_intr_info = 0;
  8935. u16 basic_exit_reason = (u16)vmx->exit_reason;
  8936. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  8937. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  8938. return;
  8939. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8940. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8941. vmx->exit_intr_info = exit_intr_info;
  8942. /* if exit due to PF check for async PF */
  8943. if (is_page_fault(exit_intr_info))
  8944. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  8945. /* Handle machine checks before interrupts are enabled */
  8946. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  8947. is_machine_check(exit_intr_info))
  8948. kvm_machine_check();
  8949. /* We need to handle NMIs before interrupts are enabled */
  8950. if (is_nmi(exit_intr_info)) {
  8951. kvm_before_interrupt(&vmx->vcpu);
  8952. asm("int $2");
  8953. kvm_after_interrupt(&vmx->vcpu);
  8954. }
  8955. }
  8956. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  8957. {
  8958. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8959. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  8960. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  8961. unsigned int vector;
  8962. unsigned long entry;
  8963. gate_desc *desc;
  8964. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8965. #ifdef CONFIG_X86_64
  8966. unsigned long tmp;
  8967. #endif
  8968. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8969. desc = (gate_desc *)vmx->host_idt_base + vector;
  8970. entry = gate_offset(desc);
  8971. asm volatile(
  8972. #ifdef CONFIG_X86_64
  8973. "mov %%" _ASM_SP ", %[sp]\n\t"
  8974. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  8975. "push $%c[ss]\n\t"
  8976. "push %[sp]\n\t"
  8977. #endif
  8978. "pushf\n\t"
  8979. __ASM_SIZE(push) " $%c[cs]\n\t"
  8980. CALL_NOSPEC
  8981. :
  8982. #ifdef CONFIG_X86_64
  8983. [sp]"=&r"(tmp),
  8984. #endif
  8985. ASM_CALL_CONSTRAINT
  8986. :
  8987. THUNK_TARGET(entry),
  8988. [ss]"i"(__KERNEL_DS),
  8989. [cs]"i"(__KERNEL_CS)
  8990. );
  8991. }
  8992. }
  8993. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  8994. static bool vmx_has_emulated_msr(int index)
  8995. {
  8996. switch (index) {
  8997. case MSR_IA32_SMBASE:
  8998. /*
  8999. * We cannot do SMM unless we can run the guest in big
  9000. * real mode.
  9001. */
  9002. return enable_unrestricted_guest || emulate_invalid_guest_state;
  9003. case MSR_AMD64_VIRT_SPEC_CTRL:
  9004. /* This is AMD only. */
  9005. return false;
  9006. default:
  9007. return true;
  9008. }
  9009. }
  9010. static bool vmx_mpx_supported(void)
  9011. {
  9012. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  9013. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  9014. }
  9015. static bool vmx_xsaves_supported(void)
  9016. {
  9017. return vmcs_config.cpu_based_2nd_exec_ctrl &
  9018. SECONDARY_EXEC_XSAVES;
  9019. }
  9020. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  9021. {
  9022. u32 exit_intr_info;
  9023. bool unblock_nmi;
  9024. u8 vector;
  9025. bool idtv_info_valid;
  9026. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9027. if (enable_vnmi) {
  9028. if (vmx->loaded_vmcs->nmi_known_unmasked)
  9029. return;
  9030. /*
  9031. * Can't use vmx->exit_intr_info since we're not sure what
  9032. * the exit reason is.
  9033. */
  9034. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9035. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  9036. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9037. /*
  9038. * SDM 3: 27.7.1.2 (September 2008)
  9039. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  9040. * a guest IRET fault.
  9041. * SDM 3: 23.2.2 (September 2008)
  9042. * Bit 12 is undefined in any of the following cases:
  9043. * If the VM exit sets the valid bit in the IDT-vectoring
  9044. * information field.
  9045. * If the VM exit is due to a double fault.
  9046. */
  9047. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  9048. vector != DF_VECTOR && !idtv_info_valid)
  9049. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  9050. GUEST_INTR_STATE_NMI);
  9051. else
  9052. vmx->loaded_vmcs->nmi_known_unmasked =
  9053. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  9054. & GUEST_INTR_STATE_NMI);
  9055. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  9056. vmx->loaded_vmcs->vnmi_blocked_time +=
  9057. ktime_to_ns(ktime_sub(ktime_get(),
  9058. vmx->loaded_vmcs->entry_time));
  9059. }
  9060. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  9061. u32 idt_vectoring_info,
  9062. int instr_len_field,
  9063. int error_code_field)
  9064. {
  9065. u8 vector;
  9066. int type;
  9067. bool idtv_info_valid;
  9068. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9069. vcpu->arch.nmi_injected = false;
  9070. kvm_clear_exception_queue(vcpu);
  9071. kvm_clear_interrupt_queue(vcpu);
  9072. if (!idtv_info_valid)
  9073. return;
  9074. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9075. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  9076. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  9077. switch (type) {
  9078. case INTR_TYPE_NMI_INTR:
  9079. vcpu->arch.nmi_injected = true;
  9080. /*
  9081. * SDM 3: 27.7.1.2 (September 2008)
  9082. * Clear bit "block by NMI" before VM entry if a NMI
  9083. * delivery faulted.
  9084. */
  9085. vmx_set_nmi_mask(vcpu, false);
  9086. break;
  9087. case INTR_TYPE_SOFT_EXCEPTION:
  9088. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9089. /* fall through */
  9090. case INTR_TYPE_HARD_EXCEPTION:
  9091. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  9092. u32 err = vmcs_read32(error_code_field);
  9093. kvm_requeue_exception_e(vcpu, vector, err);
  9094. } else
  9095. kvm_requeue_exception(vcpu, vector);
  9096. break;
  9097. case INTR_TYPE_SOFT_INTR:
  9098. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9099. /* fall through */
  9100. case INTR_TYPE_EXT_INTR:
  9101. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  9102. break;
  9103. default:
  9104. break;
  9105. }
  9106. }
  9107. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  9108. {
  9109. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  9110. VM_EXIT_INSTRUCTION_LEN,
  9111. IDT_VECTORING_ERROR_CODE);
  9112. }
  9113. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  9114. {
  9115. __vmx_complete_interrupts(vcpu,
  9116. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9117. VM_ENTRY_INSTRUCTION_LEN,
  9118. VM_ENTRY_EXCEPTION_ERROR_CODE);
  9119. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9120. }
  9121. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  9122. {
  9123. int i, nr_msrs;
  9124. struct perf_guest_switch_msr *msrs;
  9125. msrs = perf_guest_get_msrs(&nr_msrs);
  9126. if (!msrs)
  9127. return;
  9128. for (i = 0; i < nr_msrs; i++)
  9129. if (msrs[i].host == msrs[i].guest)
  9130. clear_atomic_switch_msr(vmx, msrs[i].msr);
  9131. else
  9132. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  9133. msrs[i].host, false);
  9134. }
  9135. static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
  9136. {
  9137. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9138. u64 tscl;
  9139. u32 delta_tsc;
  9140. if (vmx->hv_deadline_tsc == -1)
  9141. return;
  9142. tscl = rdtsc();
  9143. if (vmx->hv_deadline_tsc > tscl)
  9144. /* sure to be 32 bit only because checked on set_hv_timer */
  9145. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  9146. cpu_preemption_timer_multi);
  9147. else
  9148. delta_tsc = 0;
  9149. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
  9150. }
  9151. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  9152. {
  9153. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9154. unsigned long cr3, cr4, evmcs_rsp;
  9155. /* Record the guest's net vcpu time for enforced NMI injections. */
  9156. if (unlikely(!enable_vnmi &&
  9157. vmx->loaded_vmcs->soft_vnmi_blocked))
  9158. vmx->loaded_vmcs->entry_time = ktime_get();
  9159. /* Don't enter VMX if guest state is invalid, let the exit handler
  9160. start emulation until we arrive back to a valid state */
  9161. if (vmx->emulation_required)
  9162. return;
  9163. if (vmx->ple_window_dirty) {
  9164. vmx->ple_window_dirty = false;
  9165. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  9166. }
  9167. if (vmx->nested.sync_shadow_vmcs) {
  9168. copy_vmcs12_to_shadow(vmx);
  9169. vmx->nested.sync_shadow_vmcs = false;
  9170. }
  9171. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  9172. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  9173. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  9174. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  9175. cr3 = __get_current_cr3_fast();
  9176. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  9177. vmcs_writel(HOST_CR3, cr3);
  9178. vmx->loaded_vmcs->host_state.cr3 = cr3;
  9179. }
  9180. cr4 = cr4_read_shadow();
  9181. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  9182. vmcs_writel(HOST_CR4, cr4);
  9183. vmx->loaded_vmcs->host_state.cr4 = cr4;
  9184. }
  9185. /* When single-stepping over STI and MOV SS, we must clear the
  9186. * corresponding interruptibility bits in the guest state. Otherwise
  9187. * vmentry fails as it then expects bit 14 (BS) in pending debug
  9188. * exceptions being set, but that's not correct for the guest debugging
  9189. * case. */
  9190. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  9191. vmx_set_interrupt_shadow(vcpu, 0);
  9192. if (static_cpu_has(X86_FEATURE_PKU) &&
  9193. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  9194. vcpu->arch.pkru != vmx->host_pkru)
  9195. __write_pkru(vcpu->arch.pkru);
  9196. atomic_switch_perf_msrs(vmx);
  9197. vmx_arm_hv_timer(vcpu);
  9198. /*
  9199. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  9200. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  9201. * is no need to worry about the conditional branch over the wrmsr
  9202. * being speculatively taken.
  9203. */
  9204. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  9205. vmx->__launched = vmx->loaded_vmcs->launched;
  9206. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  9207. (unsigned long)&current_evmcs->host_rsp : 0;
  9208. if (static_branch_unlikely(&vmx_l1d_should_flush))
  9209. vmx_l1d_flush(vcpu);
  9210. asm(
  9211. /* Store host registers */
  9212. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  9213. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  9214. "push %%" _ASM_CX " \n\t"
  9215. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9216. "je 1f \n\t"
  9217. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9218. /* Avoid VMWRITE when Enlightened VMCS is in use */
  9219. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  9220. "jz 2f \n\t"
  9221. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  9222. "jmp 1f \n\t"
  9223. "2: \n\t"
  9224. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  9225. "1: \n\t"
  9226. /* Reload cr2 if changed */
  9227. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  9228. "mov %%cr2, %%" _ASM_DX " \n\t"
  9229. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  9230. "je 3f \n\t"
  9231. "mov %%" _ASM_AX", %%cr2 \n\t"
  9232. "3: \n\t"
  9233. /* Check if vmlaunch of vmresume is needed */
  9234. "cmpl $0, %c[launched](%0) \n\t"
  9235. /* Load guest registers. Don't clobber flags. */
  9236. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  9237. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  9238. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  9239. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  9240. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  9241. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  9242. #ifdef CONFIG_X86_64
  9243. "mov %c[r8](%0), %%r8 \n\t"
  9244. "mov %c[r9](%0), %%r9 \n\t"
  9245. "mov %c[r10](%0), %%r10 \n\t"
  9246. "mov %c[r11](%0), %%r11 \n\t"
  9247. "mov %c[r12](%0), %%r12 \n\t"
  9248. "mov %c[r13](%0), %%r13 \n\t"
  9249. "mov %c[r14](%0), %%r14 \n\t"
  9250. "mov %c[r15](%0), %%r15 \n\t"
  9251. #endif
  9252. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  9253. /* Enter guest mode */
  9254. "jne 1f \n\t"
  9255. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  9256. "jmp 2f \n\t"
  9257. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  9258. "2: "
  9259. /* Save guest registers, load host registers, keep flags */
  9260. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  9261. "pop %0 \n\t"
  9262. "setbe %c[fail](%0)\n\t"
  9263. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  9264. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  9265. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  9266. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  9267. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  9268. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  9269. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  9270. #ifdef CONFIG_X86_64
  9271. "mov %%r8, %c[r8](%0) \n\t"
  9272. "mov %%r9, %c[r9](%0) \n\t"
  9273. "mov %%r10, %c[r10](%0) \n\t"
  9274. "mov %%r11, %c[r11](%0) \n\t"
  9275. "mov %%r12, %c[r12](%0) \n\t"
  9276. "mov %%r13, %c[r13](%0) \n\t"
  9277. "mov %%r14, %c[r14](%0) \n\t"
  9278. "mov %%r15, %c[r15](%0) \n\t"
  9279. "xor %%r8d, %%r8d \n\t"
  9280. "xor %%r9d, %%r9d \n\t"
  9281. "xor %%r10d, %%r10d \n\t"
  9282. "xor %%r11d, %%r11d \n\t"
  9283. "xor %%r12d, %%r12d \n\t"
  9284. "xor %%r13d, %%r13d \n\t"
  9285. "xor %%r14d, %%r14d \n\t"
  9286. "xor %%r15d, %%r15d \n\t"
  9287. #endif
  9288. "mov %%cr2, %%" _ASM_AX " \n\t"
  9289. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  9290. "xor %%eax, %%eax \n\t"
  9291. "xor %%ebx, %%ebx \n\t"
  9292. "xor %%esi, %%esi \n\t"
  9293. "xor %%edi, %%edi \n\t"
  9294. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  9295. ".pushsection .rodata \n\t"
  9296. ".global vmx_return \n\t"
  9297. "vmx_return: " _ASM_PTR " 2b \n\t"
  9298. ".popsection"
  9299. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  9300. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  9301. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  9302. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  9303. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  9304. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  9305. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  9306. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  9307. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  9308. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  9309. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  9310. #ifdef CONFIG_X86_64
  9311. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  9312. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  9313. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  9314. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  9315. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  9316. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  9317. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  9318. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  9319. #endif
  9320. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  9321. [wordsize]"i"(sizeof(ulong))
  9322. : "cc", "memory"
  9323. #ifdef CONFIG_X86_64
  9324. , "rax", "rbx", "rdi"
  9325. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  9326. #else
  9327. , "eax", "ebx", "edi"
  9328. #endif
  9329. );
  9330. /*
  9331. * We do not use IBRS in the kernel. If this vCPU has used the
  9332. * SPEC_CTRL MSR it may have left it on; save the value and
  9333. * turn it off. This is much more efficient than blindly adding
  9334. * it to the atomic save/restore list. Especially as the former
  9335. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  9336. *
  9337. * For non-nested case:
  9338. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  9339. * save it.
  9340. *
  9341. * For nested case:
  9342. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  9343. * save it.
  9344. */
  9345. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  9346. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  9347. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  9348. /* Eliminate branch target predictions from guest mode */
  9349. vmexit_fill_RSB();
  9350. /* All fields are clean at this point */
  9351. if (static_branch_unlikely(&enable_evmcs))
  9352. current_evmcs->hv_clean_fields |=
  9353. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9354. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  9355. if (vmx->host_debugctlmsr)
  9356. update_debugctlmsr(vmx->host_debugctlmsr);
  9357. #ifndef CONFIG_X86_64
  9358. /*
  9359. * The sysexit path does not restore ds/es, so we must set them to
  9360. * a reasonable value ourselves.
  9361. *
  9362. * We can't defer this to vmx_prepare_switch_to_host() since that
  9363. * function may be executed in interrupt context, which saves and
  9364. * restore segments around it, nullifying its effect.
  9365. */
  9366. loadsegment(ds, __USER_DS);
  9367. loadsegment(es, __USER_DS);
  9368. #endif
  9369. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  9370. | (1 << VCPU_EXREG_RFLAGS)
  9371. | (1 << VCPU_EXREG_PDPTR)
  9372. | (1 << VCPU_EXREG_SEGMENTS)
  9373. | (1 << VCPU_EXREG_CR3));
  9374. vcpu->arch.regs_dirty = 0;
  9375. /*
  9376. * eager fpu is enabled if PKEY is supported and CR4 is switched
  9377. * back on host, so it is safe to read guest PKRU from current
  9378. * XSAVE.
  9379. */
  9380. if (static_cpu_has(X86_FEATURE_PKU) &&
  9381. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  9382. vcpu->arch.pkru = __read_pkru();
  9383. if (vcpu->arch.pkru != vmx->host_pkru)
  9384. __write_pkru(vmx->host_pkru);
  9385. }
  9386. vmx->nested.nested_run_pending = 0;
  9387. vmx->idt_vectoring_info = 0;
  9388. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  9389. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9390. return;
  9391. vmx->loaded_vmcs->launched = 1;
  9392. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  9393. vmx_complete_atomic_exit(vmx);
  9394. vmx_recover_nmi_blocking(vmx);
  9395. vmx_complete_interrupts(vmx);
  9396. }
  9397. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  9398. static struct kvm *vmx_vm_alloc(void)
  9399. {
  9400. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  9401. return &kvm_vmx->kvm;
  9402. }
  9403. static void vmx_vm_free(struct kvm *kvm)
  9404. {
  9405. vfree(to_kvm_vmx(kvm));
  9406. }
  9407. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  9408. {
  9409. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9410. int cpu;
  9411. if (vmx->loaded_vmcs == vmcs)
  9412. return;
  9413. cpu = get_cpu();
  9414. vmx_vcpu_put(vcpu);
  9415. vmx->loaded_vmcs = vmcs;
  9416. vmx_vcpu_load(vcpu, cpu);
  9417. put_cpu();
  9418. }
  9419. /*
  9420. * Ensure that the current vmcs of the logical processor is the
  9421. * vmcs01 of the vcpu before calling free_nested().
  9422. */
  9423. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  9424. {
  9425. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9426. vcpu_load(vcpu);
  9427. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9428. free_nested(vmx);
  9429. vcpu_put(vcpu);
  9430. }
  9431. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  9432. {
  9433. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9434. if (enable_pml)
  9435. vmx_destroy_pml_buffer(vmx);
  9436. free_vpid(vmx->vpid);
  9437. leave_guest_mode(vcpu);
  9438. vmx_free_vcpu_nested(vcpu);
  9439. free_loaded_vmcs(vmx->loaded_vmcs);
  9440. kfree(vmx->guest_msrs);
  9441. kvm_vcpu_uninit(vcpu);
  9442. kmem_cache_free(kvm_vcpu_cache, vmx);
  9443. }
  9444. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  9445. {
  9446. int err;
  9447. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  9448. unsigned long *msr_bitmap;
  9449. int cpu;
  9450. if (!vmx)
  9451. return ERR_PTR(-ENOMEM);
  9452. vmx->vpid = allocate_vpid();
  9453. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  9454. if (err)
  9455. goto free_vcpu;
  9456. err = -ENOMEM;
  9457. /*
  9458. * If PML is turned on, failure on enabling PML just results in failure
  9459. * of creating the vcpu, therefore we can simplify PML logic (by
  9460. * avoiding dealing with cases, such as enabling PML partially on vcpus
  9461. * for the guest, etc.
  9462. */
  9463. if (enable_pml) {
  9464. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  9465. if (!vmx->pml_pg)
  9466. goto uninit_vcpu;
  9467. }
  9468. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  9469. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  9470. > PAGE_SIZE);
  9471. if (!vmx->guest_msrs)
  9472. goto free_pml;
  9473. err = alloc_loaded_vmcs(&vmx->vmcs01);
  9474. if (err < 0)
  9475. goto free_msrs;
  9476. msr_bitmap = vmx->vmcs01.msr_bitmap;
  9477. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  9478. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  9479. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  9480. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  9481. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  9482. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  9483. vmx->msr_bitmap_mode = 0;
  9484. vmx->loaded_vmcs = &vmx->vmcs01;
  9485. cpu = get_cpu();
  9486. vmx_vcpu_load(&vmx->vcpu, cpu);
  9487. vmx->vcpu.cpu = cpu;
  9488. vmx_vcpu_setup(vmx);
  9489. vmx_vcpu_put(&vmx->vcpu);
  9490. put_cpu();
  9491. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  9492. err = alloc_apic_access_page(kvm);
  9493. if (err)
  9494. goto free_vmcs;
  9495. }
  9496. if (enable_ept && !enable_unrestricted_guest) {
  9497. err = init_rmode_identity_map(kvm);
  9498. if (err)
  9499. goto free_vmcs;
  9500. }
  9501. if (nested)
  9502. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  9503. kvm_vcpu_apicv_active(&vmx->vcpu));
  9504. vmx->nested.posted_intr_nv = -1;
  9505. vmx->nested.current_vmptr = -1ull;
  9506. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  9507. /*
  9508. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  9509. * or POSTED_INTR_WAKEUP_VECTOR.
  9510. */
  9511. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  9512. vmx->pi_desc.sn = 1;
  9513. return &vmx->vcpu;
  9514. free_vmcs:
  9515. free_loaded_vmcs(vmx->loaded_vmcs);
  9516. free_msrs:
  9517. kfree(vmx->guest_msrs);
  9518. free_pml:
  9519. vmx_destroy_pml_buffer(vmx);
  9520. uninit_vcpu:
  9521. kvm_vcpu_uninit(&vmx->vcpu);
  9522. free_vcpu:
  9523. free_vpid(vmx->vpid);
  9524. kmem_cache_free(kvm_vcpu_cache, vmx);
  9525. return ERR_PTR(err);
  9526. }
  9527. #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
  9528. #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
  9529. static int vmx_vm_init(struct kvm *kvm)
  9530. {
  9531. spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
  9532. if (!ple_gap)
  9533. kvm->arch.pause_in_guest = true;
  9534. if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
  9535. switch (l1tf_mitigation) {
  9536. case L1TF_MITIGATION_OFF:
  9537. case L1TF_MITIGATION_FLUSH_NOWARN:
  9538. /* 'I explicitly don't care' is set */
  9539. break;
  9540. case L1TF_MITIGATION_FLUSH:
  9541. case L1TF_MITIGATION_FLUSH_NOSMT:
  9542. case L1TF_MITIGATION_FULL:
  9543. /*
  9544. * Warn upon starting the first VM in a potentially
  9545. * insecure environment.
  9546. */
  9547. if (cpu_smt_control == CPU_SMT_ENABLED)
  9548. pr_warn_once(L1TF_MSG_SMT);
  9549. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
  9550. pr_warn_once(L1TF_MSG_L1D);
  9551. break;
  9552. case L1TF_MITIGATION_FULL_FORCE:
  9553. /* Flush is enforced */
  9554. break;
  9555. }
  9556. }
  9557. return 0;
  9558. }
  9559. static void __init vmx_check_processor_compat(void *rtn)
  9560. {
  9561. struct vmcs_config vmcs_conf;
  9562. *(int *)rtn = 0;
  9563. if (setup_vmcs_config(&vmcs_conf) < 0)
  9564. *(int *)rtn = -EIO;
  9565. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  9566. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  9567. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  9568. smp_processor_id());
  9569. *(int *)rtn = -EIO;
  9570. }
  9571. }
  9572. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  9573. {
  9574. u8 cache;
  9575. u64 ipat = 0;
  9576. /* For VT-d and EPT combination
  9577. * 1. MMIO: always map as UC
  9578. * 2. EPT with VT-d:
  9579. * a. VT-d without snooping control feature: can't guarantee the
  9580. * result, try to trust guest.
  9581. * b. VT-d with snooping control feature: snooping control feature of
  9582. * VT-d engine can guarantee the cache correctness. Just set it
  9583. * to WB to keep consistent with host. So the same as item 3.
  9584. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  9585. * consistent with host MTRR
  9586. */
  9587. if (is_mmio) {
  9588. cache = MTRR_TYPE_UNCACHABLE;
  9589. goto exit;
  9590. }
  9591. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  9592. ipat = VMX_EPT_IPAT_BIT;
  9593. cache = MTRR_TYPE_WRBACK;
  9594. goto exit;
  9595. }
  9596. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  9597. ipat = VMX_EPT_IPAT_BIT;
  9598. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  9599. cache = MTRR_TYPE_WRBACK;
  9600. else
  9601. cache = MTRR_TYPE_UNCACHABLE;
  9602. goto exit;
  9603. }
  9604. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  9605. exit:
  9606. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  9607. }
  9608. static int vmx_get_lpage_level(void)
  9609. {
  9610. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  9611. return PT_DIRECTORY_LEVEL;
  9612. else
  9613. /* For shadow and EPT supported 1GB page */
  9614. return PT_PDPE_LEVEL;
  9615. }
  9616. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  9617. {
  9618. /*
  9619. * These bits in the secondary execution controls field
  9620. * are dynamic, the others are mostly based on the hypervisor
  9621. * architecture and the guest's CPUID. Do not touch the
  9622. * dynamic bits.
  9623. */
  9624. u32 mask =
  9625. SECONDARY_EXEC_SHADOW_VMCS |
  9626. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  9627. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9628. SECONDARY_EXEC_DESC;
  9629. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9630. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  9631. (new_ctl & ~mask) | (cur_ctl & mask));
  9632. }
  9633. /*
  9634. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  9635. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  9636. */
  9637. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  9638. {
  9639. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9640. struct kvm_cpuid_entry2 *entry;
  9641. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  9642. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  9643. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  9644. if (entry && (entry->_reg & (_cpuid_mask))) \
  9645. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  9646. } while (0)
  9647. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  9648. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  9649. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  9650. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  9651. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  9652. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  9653. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  9654. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  9655. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  9656. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  9657. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  9658. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  9659. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  9660. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  9661. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  9662. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  9663. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  9664. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  9665. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  9666. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  9667. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  9668. #undef cr4_fixed1_update
  9669. }
  9670. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  9671. {
  9672. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9673. if (cpu_has_secondary_exec_ctrls()) {
  9674. vmx_compute_secondary_exec_control(vmx);
  9675. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  9676. }
  9677. if (nested_vmx_allowed(vcpu))
  9678. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  9679. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9680. else
  9681. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  9682. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9683. if (nested_vmx_allowed(vcpu))
  9684. nested_vmx_cr_fixed1_bits_update(vcpu);
  9685. }
  9686. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  9687. {
  9688. if (func == 1 && nested)
  9689. entry->ecx |= bit(X86_FEATURE_VMX);
  9690. }
  9691. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  9692. struct x86_exception *fault)
  9693. {
  9694. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9695. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9696. u32 exit_reason;
  9697. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  9698. if (vmx->nested.pml_full) {
  9699. exit_reason = EXIT_REASON_PML_FULL;
  9700. vmx->nested.pml_full = false;
  9701. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  9702. } else if (fault->error_code & PFERR_RSVD_MASK)
  9703. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  9704. else
  9705. exit_reason = EXIT_REASON_EPT_VIOLATION;
  9706. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  9707. vmcs12->guest_physical_address = fault->address;
  9708. }
  9709. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  9710. {
  9711. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  9712. }
  9713. /* Callbacks for nested_ept_init_mmu_context: */
  9714. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  9715. {
  9716. /* return the page table to be shadowed - in our case, EPT12 */
  9717. return get_vmcs12(vcpu)->ept_pointer;
  9718. }
  9719. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  9720. {
  9721. WARN_ON(mmu_is_nested(vcpu));
  9722. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  9723. return 1;
  9724. kvm_init_shadow_ept_mmu(vcpu,
  9725. to_vmx(vcpu)->nested.msrs.ept_caps &
  9726. VMX_EPT_EXECUTE_ONLY_BIT,
  9727. nested_ept_ad_enabled(vcpu),
  9728. nested_ept_get_cr3(vcpu));
  9729. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  9730. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  9731. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  9732. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  9733. return 0;
  9734. }
  9735. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  9736. {
  9737. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  9738. }
  9739. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  9740. u16 error_code)
  9741. {
  9742. bool inequality, bit;
  9743. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  9744. inequality =
  9745. (error_code & vmcs12->page_fault_error_code_mask) !=
  9746. vmcs12->page_fault_error_code_match;
  9747. return inequality ^ bit;
  9748. }
  9749. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  9750. struct x86_exception *fault)
  9751. {
  9752. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9753. WARN_ON(!is_guest_mode(vcpu));
  9754. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  9755. !to_vmx(vcpu)->nested.nested_run_pending) {
  9756. vmcs12->vm_exit_intr_error_code = fault->error_code;
  9757. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9758. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  9759. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  9760. fault->address);
  9761. } else {
  9762. kvm_inject_page_fault(vcpu, fault);
  9763. }
  9764. }
  9765. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9766. struct vmcs12 *vmcs12);
  9767. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
  9768. {
  9769. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9770. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9771. struct page *page;
  9772. u64 hpa;
  9773. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9774. /*
  9775. * Translate L1 physical address to host physical
  9776. * address for vmcs02. Keep the page pinned, so this
  9777. * physical address remains valid. We keep a reference
  9778. * to it so we can release it later.
  9779. */
  9780. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  9781. kvm_release_page_dirty(vmx->nested.apic_access_page);
  9782. vmx->nested.apic_access_page = NULL;
  9783. }
  9784. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  9785. /*
  9786. * If translation failed, no matter: This feature asks
  9787. * to exit when accessing the given address, and if it
  9788. * can never be accessed, this feature won't do
  9789. * anything anyway.
  9790. */
  9791. if (!is_error_page(page)) {
  9792. vmx->nested.apic_access_page = page;
  9793. hpa = page_to_phys(vmx->nested.apic_access_page);
  9794. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9795. } else {
  9796. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  9797. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  9798. }
  9799. }
  9800. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  9801. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  9802. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  9803. vmx->nested.virtual_apic_page = NULL;
  9804. }
  9805. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  9806. /*
  9807. * If translation failed, VM entry will fail because
  9808. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  9809. * Failing the vm entry is _not_ what the processor
  9810. * does but it's basically the only possibility we
  9811. * have. We could still enter the guest if CR8 load
  9812. * exits are enabled, CR8 store exits are enabled, and
  9813. * virtualize APIC access is disabled; in this case
  9814. * the processor would never use the TPR shadow and we
  9815. * could simply clear the bit from the execution
  9816. * control. But such a configuration is useless, so
  9817. * let's keep the code simple.
  9818. */
  9819. if (!is_error_page(page)) {
  9820. vmx->nested.virtual_apic_page = page;
  9821. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  9822. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  9823. }
  9824. }
  9825. if (nested_cpu_has_posted_intr(vmcs12)) {
  9826. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  9827. kunmap(vmx->nested.pi_desc_page);
  9828. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  9829. vmx->nested.pi_desc_page = NULL;
  9830. }
  9831. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  9832. if (is_error_page(page))
  9833. return;
  9834. vmx->nested.pi_desc_page = page;
  9835. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  9836. vmx->nested.pi_desc =
  9837. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  9838. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9839. (PAGE_SIZE - 1)));
  9840. vmcs_write64(POSTED_INTR_DESC_ADDR,
  9841. page_to_phys(vmx->nested.pi_desc_page) +
  9842. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9843. (PAGE_SIZE - 1)));
  9844. }
  9845. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  9846. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  9847. CPU_BASED_USE_MSR_BITMAPS);
  9848. else
  9849. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  9850. CPU_BASED_USE_MSR_BITMAPS);
  9851. }
  9852. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  9853. {
  9854. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  9855. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9856. if (vcpu->arch.virtual_tsc_khz == 0)
  9857. return;
  9858. /* Make sure short timeouts reliably trigger an immediate vmexit.
  9859. * hrtimer_start does not guarantee this. */
  9860. if (preemption_timeout <= 1) {
  9861. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  9862. return;
  9863. }
  9864. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9865. preemption_timeout *= 1000000;
  9866. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  9867. hrtimer_start(&vmx->nested.preemption_timer,
  9868. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  9869. }
  9870. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  9871. struct vmcs12 *vmcs12)
  9872. {
  9873. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  9874. return 0;
  9875. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  9876. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  9877. return -EINVAL;
  9878. return 0;
  9879. }
  9880. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  9881. struct vmcs12 *vmcs12)
  9882. {
  9883. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9884. return 0;
  9885. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  9886. return -EINVAL;
  9887. return 0;
  9888. }
  9889. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  9890. struct vmcs12 *vmcs12)
  9891. {
  9892. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9893. return 0;
  9894. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  9895. return -EINVAL;
  9896. return 0;
  9897. }
  9898. /*
  9899. * Merge L0's and L1's MSR bitmap, return false to indicate that
  9900. * we do not use the hardware.
  9901. */
  9902. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9903. struct vmcs12 *vmcs12)
  9904. {
  9905. int msr;
  9906. struct page *page;
  9907. unsigned long *msr_bitmap_l1;
  9908. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  9909. /*
  9910. * pred_cmd & spec_ctrl are trying to verify two things:
  9911. *
  9912. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  9913. * ensures that we do not accidentally generate an L02 MSR bitmap
  9914. * from the L12 MSR bitmap that is too permissive.
  9915. * 2. That L1 or L2s have actually used the MSR. This avoids
  9916. * unnecessarily merging of the bitmap if the MSR is unused. This
  9917. * works properly because we only update the L01 MSR bitmap lazily.
  9918. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  9919. * updated to reflect this when L1 (or its L2s) actually write to
  9920. * the MSR.
  9921. */
  9922. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  9923. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  9924. /* Nothing to do if the MSR bitmap is not in use. */
  9925. if (!cpu_has_vmx_msr_bitmap() ||
  9926. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9927. return false;
  9928. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9929. !pred_cmd && !spec_ctrl)
  9930. return false;
  9931. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  9932. if (is_error_page(page))
  9933. return false;
  9934. msr_bitmap_l1 = (unsigned long *)kmap(page);
  9935. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  9936. /*
  9937. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  9938. * just lets the processor take the value from the virtual-APIC page;
  9939. * take those 256 bits directly from the L1 bitmap.
  9940. */
  9941. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9942. unsigned word = msr / BITS_PER_LONG;
  9943. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  9944. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9945. }
  9946. } else {
  9947. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9948. unsigned word = msr / BITS_PER_LONG;
  9949. msr_bitmap_l0[word] = ~0;
  9950. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9951. }
  9952. }
  9953. nested_vmx_disable_intercept_for_msr(
  9954. msr_bitmap_l1, msr_bitmap_l0,
  9955. X2APIC_MSR(APIC_TASKPRI),
  9956. MSR_TYPE_W);
  9957. if (nested_cpu_has_vid(vmcs12)) {
  9958. nested_vmx_disable_intercept_for_msr(
  9959. msr_bitmap_l1, msr_bitmap_l0,
  9960. X2APIC_MSR(APIC_EOI),
  9961. MSR_TYPE_W);
  9962. nested_vmx_disable_intercept_for_msr(
  9963. msr_bitmap_l1, msr_bitmap_l0,
  9964. X2APIC_MSR(APIC_SELF_IPI),
  9965. MSR_TYPE_W);
  9966. }
  9967. if (spec_ctrl)
  9968. nested_vmx_disable_intercept_for_msr(
  9969. msr_bitmap_l1, msr_bitmap_l0,
  9970. MSR_IA32_SPEC_CTRL,
  9971. MSR_TYPE_R | MSR_TYPE_W);
  9972. if (pred_cmd)
  9973. nested_vmx_disable_intercept_for_msr(
  9974. msr_bitmap_l1, msr_bitmap_l0,
  9975. MSR_IA32_PRED_CMD,
  9976. MSR_TYPE_W);
  9977. kunmap(page);
  9978. kvm_release_page_clean(page);
  9979. return true;
  9980. }
  9981. static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
  9982. struct vmcs12 *vmcs12)
  9983. {
  9984. struct vmcs12 *shadow;
  9985. struct page *page;
  9986. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  9987. vmcs12->vmcs_link_pointer == -1ull)
  9988. return;
  9989. shadow = get_shadow_vmcs12(vcpu);
  9990. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  9991. memcpy(shadow, kmap(page), VMCS12_SIZE);
  9992. kunmap(page);
  9993. kvm_release_page_clean(page);
  9994. }
  9995. static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
  9996. struct vmcs12 *vmcs12)
  9997. {
  9998. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9999. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10000. vmcs12->vmcs_link_pointer == -1ull)
  10001. return;
  10002. kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
  10003. get_shadow_vmcs12(vcpu), VMCS12_SIZE);
  10004. }
  10005. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  10006. struct vmcs12 *vmcs12)
  10007. {
  10008. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  10009. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  10010. return -EINVAL;
  10011. else
  10012. return 0;
  10013. }
  10014. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  10015. struct vmcs12 *vmcs12)
  10016. {
  10017. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10018. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  10019. !nested_cpu_has_vid(vmcs12) &&
  10020. !nested_cpu_has_posted_intr(vmcs12))
  10021. return 0;
  10022. /*
  10023. * If virtualize x2apic mode is enabled,
  10024. * virtualize apic access must be disabled.
  10025. */
  10026. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10027. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  10028. return -EINVAL;
  10029. /*
  10030. * If virtual interrupt delivery is enabled,
  10031. * we must exit on external interrupts.
  10032. */
  10033. if (nested_cpu_has_vid(vmcs12) &&
  10034. !nested_exit_on_intr(vcpu))
  10035. return -EINVAL;
  10036. /*
  10037. * bits 15:8 should be zero in posted_intr_nv,
  10038. * the descriptor address has been already checked
  10039. * in nested_get_vmcs12_pages.
  10040. */
  10041. if (nested_cpu_has_posted_intr(vmcs12) &&
  10042. (!nested_cpu_has_vid(vmcs12) ||
  10043. !nested_exit_intr_ack_set(vcpu) ||
  10044. vmcs12->posted_intr_nv & 0xff00))
  10045. return -EINVAL;
  10046. /* tpr shadow is needed by all apicv features. */
  10047. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10048. return -EINVAL;
  10049. return 0;
  10050. }
  10051. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  10052. unsigned long count_field,
  10053. unsigned long addr_field)
  10054. {
  10055. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10056. int maxphyaddr;
  10057. u64 count, addr;
  10058. if (vmcs12_read_any(vmcs12, count_field, &count) ||
  10059. vmcs12_read_any(vmcs12, addr_field, &addr)) {
  10060. WARN_ON(1);
  10061. return -EINVAL;
  10062. }
  10063. if (count == 0)
  10064. return 0;
  10065. maxphyaddr = cpuid_maxphyaddr(vcpu);
  10066. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  10067. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  10068. pr_debug_ratelimited(
  10069. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  10070. addr_field, maxphyaddr, count, addr);
  10071. return -EINVAL;
  10072. }
  10073. return 0;
  10074. }
  10075. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  10076. struct vmcs12 *vmcs12)
  10077. {
  10078. if (vmcs12->vm_exit_msr_load_count == 0 &&
  10079. vmcs12->vm_exit_msr_store_count == 0 &&
  10080. vmcs12->vm_entry_msr_load_count == 0)
  10081. return 0; /* Fast path */
  10082. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  10083. VM_EXIT_MSR_LOAD_ADDR) ||
  10084. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  10085. VM_EXIT_MSR_STORE_ADDR) ||
  10086. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  10087. VM_ENTRY_MSR_LOAD_ADDR))
  10088. return -EINVAL;
  10089. return 0;
  10090. }
  10091. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  10092. struct vmcs12 *vmcs12)
  10093. {
  10094. u64 address = vmcs12->pml_address;
  10095. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  10096. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  10097. if (!nested_cpu_has_ept(vmcs12) ||
  10098. !IS_ALIGNED(address, 4096) ||
  10099. address >> maxphyaddr)
  10100. return -EINVAL;
  10101. }
  10102. return 0;
  10103. }
  10104. static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
  10105. struct vmcs12 *vmcs12)
  10106. {
  10107. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  10108. return 0;
  10109. if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
  10110. !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
  10111. return -EINVAL;
  10112. return 0;
  10113. }
  10114. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  10115. struct vmx_msr_entry *e)
  10116. {
  10117. /* x2APIC MSR accesses are not allowed */
  10118. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  10119. return -EINVAL;
  10120. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  10121. e->index == MSR_IA32_UCODE_REV)
  10122. return -EINVAL;
  10123. if (e->reserved != 0)
  10124. return -EINVAL;
  10125. return 0;
  10126. }
  10127. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  10128. struct vmx_msr_entry *e)
  10129. {
  10130. if (e->index == MSR_FS_BASE ||
  10131. e->index == MSR_GS_BASE ||
  10132. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  10133. nested_vmx_msr_check_common(vcpu, e))
  10134. return -EINVAL;
  10135. return 0;
  10136. }
  10137. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  10138. struct vmx_msr_entry *e)
  10139. {
  10140. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  10141. nested_vmx_msr_check_common(vcpu, e))
  10142. return -EINVAL;
  10143. return 0;
  10144. }
  10145. /*
  10146. * Load guest's/host's msr at nested entry/exit.
  10147. * return 0 for success, entry index for failure.
  10148. */
  10149. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10150. {
  10151. u32 i;
  10152. struct vmx_msr_entry e;
  10153. struct msr_data msr;
  10154. msr.host_initiated = false;
  10155. for (i = 0; i < count; i++) {
  10156. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  10157. &e, sizeof(e))) {
  10158. pr_debug_ratelimited(
  10159. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10160. __func__, i, gpa + i * sizeof(e));
  10161. goto fail;
  10162. }
  10163. if (nested_vmx_load_msr_check(vcpu, &e)) {
  10164. pr_debug_ratelimited(
  10165. "%s check failed (%u, 0x%x, 0x%x)\n",
  10166. __func__, i, e.index, e.reserved);
  10167. goto fail;
  10168. }
  10169. msr.index = e.index;
  10170. msr.data = e.value;
  10171. if (kvm_set_msr(vcpu, &msr)) {
  10172. pr_debug_ratelimited(
  10173. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10174. __func__, i, e.index, e.value);
  10175. goto fail;
  10176. }
  10177. }
  10178. return 0;
  10179. fail:
  10180. return i + 1;
  10181. }
  10182. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10183. {
  10184. u32 i;
  10185. struct vmx_msr_entry e;
  10186. for (i = 0; i < count; i++) {
  10187. struct msr_data msr_info;
  10188. if (kvm_vcpu_read_guest(vcpu,
  10189. gpa + i * sizeof(e),
  10190. &e, 2 * sizeof(u32))) {
  10191. pr_debug_ratelimited(
  10192. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10193. __func__, i, gpa + i * sizeof(e));
  10194. return -EINVAL;
  10195. }
  10196. if (nested_vmx_store_msr_check(vcpu, &e)) {
  10197. pr_debug_ratelimited(
  10198. "%s check failed (%u, 0x%x, 0x%x)\n",
  10199. __func__, i, e.index, e.reserved);
  10200. return -EINVAL;
  10201. }
  10202. msr_info.host_initiated = false;
  10203. msr_info.index = e.index;
  10204. if (kvm_get_msr(vcpu, &msr_info)) {
  10205. pr_debug_ratelimited(
  10206. "%s cannot read MSR (%u, 0x%x)\n",
  10207. __func__, i, e.index);
  10208. return -EINVAL;
  10209. }
  10210. if (kvm_vcpu_write_guest(vcpu,
  10211. gpa + i * sizeof(e) +
  10212. offsetof(struct vmx_msr_entry, value),
  10213. &msr_info.data, sizeof(msr_info.data))) {
  10214. pr_debug_ratelimited(
  10215. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10216. __func__, i, e.index, msr_info.data);
  10217. return -EINVAL;
  10218. }
  10219. }
  10220. return 0;
  10221. }
  10222. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  10223. {
  10224. unsigned long invalid_mask;
  10225. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  10226. return (val & invalid_mask) == 0;
  10227. }
  10228. /*
  10229. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  10230. * emulating VM entry into a guest with EPT enabled.
  10231. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10232. * is assigned to entry_failure_code on failure.
  10233. */
  10234. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  10235. u32 *entry_failure_code)
  10236. {
  10237. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  10238. if (!nested_cr3_valid(vcpu, cr3)) {
  10239. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10240. return 1;
  10241. }
  10242. /*
  10243. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  10244. * must not be dereferenced.
  10245. */
  10246. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  10247. !nested_ept) {
  10248. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  10249. *entry_failure_code = ENTRY_FAIL_PDPTE;
  10250. return 1;
  10251. }
  10252. }
  10253. }
  10254. if (!nested_ept)
  10255. kvm_mmu_new_cr3(vcpu, cr3, false);
  10256. vcpu->arch.cr3 = cr3;
  10257. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  10258. kvm_init_mmu(vcpu, false);
  10259. return 0;
  10260. }
  10261. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10262. {
  10263. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10264. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  10265. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  10266. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  10267. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  10268. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  10269. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  10270. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  10271. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  10272. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  10273. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  10274. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  10275. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  10276. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  10277. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  10278. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  10279. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  10280. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  10281. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  10282. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  10283. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  10284. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  10285. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  10286. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  10287. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  10288. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  10289. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  10290. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  10291. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  10292. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  10293. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  10294. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  10295. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  10296. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  10297. vmcs12->guest_pending_dbg_exceptions);
  10298. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  10299. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  10300. if (nested_cpu_has_xsaves(vmcs12))
  10301. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  10302. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  10303. if (cpu_has_vmx_posted_intr())
  10304. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  10305. /*
  10306. * Whether page-faults are trapped is determined by a combination of
  10307. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  10308. * If enable_ept, L0 doesn't care about page faults and we should
  10309. * set all of these to L1's desires. However, if !enable_ept, L0 does
  10310. * care about (at least some) page faults, and because it is not easy
  10311. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  10312. * to exit on each and every L2 page fault. This is done by setting
  10313. * MASK=MATCH=0 and (see below) EB.PF=1.
  10314. * Note that below we don't need special code to set EB.PF beyond the
  10315. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  10316. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  10317. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  10318. */
  10319. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  10320. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  10321. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  10322. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  10323. /* All VMFUNCs are currently emulated through L0 vmexits. */
  10324. if (cpu_has_vmx_vmfunc())
  10325. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  10326. if (cpu_has_vmx_apicv()) {
  10327. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  10328. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  10329. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  10330. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  10331. }
  10332. /*
  10333. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  10334. * Some constant fields are set here by vmx_set_constant_host_state().
  10335. * Other fields are different per CPU, and will be set later when
  10336. * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
  10337. * is called.
  10338. */
  10339. vmx_set_constant_host_state(vmx);
  10340. /*
  10341. * Set the MSR load/store lists to match L0's settings.
  10342. */
  10343. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  10344. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  10345. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  10346. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  10347. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  10348. set_cr4_guest_host_mask(vmx);
  10349. if (vmx_mpx_supported())
  10350. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  10351. if (enable_vpid) {
  10352. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  10353. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  10354. else
  10355. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  10356. }
  10357. /*
  10358. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  10359. */
  10360. if (enable_ept) {
  10361. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  10362. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  10363. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  10364. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  10365. }
  10366. if (cpu_has_vmx_msr_bitmap())
  10367. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  10368. }
  10369. /*
  10370. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  10371. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  10372. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  10373. * guest in a way that will both be appropriate to L1's requests, and our
  10374. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  10375. * function also has additional necessary side-effects, like setting various
  10376. * vcpu->arch fields.
  10377. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10378. * is assigned to entry_failure_code on failure.
  10379. */
  10380. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10381. u32 *entry_failure_code)
  10382. {
  10383. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10384. u32 exec_control, vmcs12_exec_ctrl;
  10385. if (vmx->nested.dirty_vmcs12) {
  10386. prepare_vmcs02_full(vcpu, vmcs12);
  10387. vmx->nested.dirty_vmcs12 = false;
  10388. }
  10389. /*
  10390. * First, the fields that are shadowed. This must be kept in sync
  10391. * with vmx_shadow_fields.h.
  10392. */
  10393. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  10394. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  10395. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  10396. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  10397. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  10398. if (vmx->nested.nested_run_pending &&
  10399. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  10400. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  10401. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  10402. } else {
  10403. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  10404. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  10405. }
  10406. if (vmx->nested.nested_run_pending) {
  10407. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  10408. vmcs12->vm_entry_intr_info_field);
  10409. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  10410. vmcs12->vm_entry_exception_error_code);
  10411. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  10412. vmcs12->vm_entry_instruction_len);
  10413. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  10414. vmcs12->guest_interruptibility_info);
  10415. vmx->loaded_vmcs->nmi_known_unmasked =
  10416. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  10417. } else {
  10418. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  10419. }
  10420. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  10421. exec_control = vmcs12->pin_based_vm_exec_control;
  10422. /* Preemption timer setting is only taken from vmcs01. */
  10423. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  10424. exec_control |= vmcs_config.pin_based_exec_ctrl;
  10425. if (vmx->hv_deadline_tsc == -1)
  10426. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  10427. /* Posted interrupts setting is only taken from vmcs12. */
  10428. if (nested_cpu_has_posted_intr(vmcs12)) {
  10429. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  10430. vmx->nested.pi_pending = false;
  10431. } else {
  10432. exec_control &= ~PIN_BASED_POSTED_INTR;
  10433. }
  10434. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  10435. vmx->nested.preemption_timer_expired = false;
  10436. if (nested_cpu_has_preemption_timer(vmcs12))
  10437. vmx_start_preemption_timer(vcpu);
  10438. if (cpu_has_secondary_exec_ctrls()) {
  10439. exec_control = vmx->secondary_exec_control;
  10440. /* Take the following fields only from vmcs12 */
  10441. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10442. SECONDARY_EXEC_ENABLE_INVPCID |
  10443. SECONDARY_EXEC_RDTSCP |
  10444. SECONDARY_EXEC_XSAVES |
  10445. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  10446. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  10447. SECONDARY_EXEC_ENABLE_VMFUNC);
  10448. if (nested_cpu_has(vmcs12,
  10449. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  10450. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  10451. ~SECONDARY_EXEC_ENABLE_PML;
  10452. exec_control |= vmcs12_exec_ctrl;
  10453. }
  10454. /* VMCS shadowing for L2 is emulated for now */
  10455. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  10456. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  10457. vmcs_write16(GUEST_INTR_STATUS,
  10458. vmcs12->guest_intr_status);
  10459. /*
  10460. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  10461. * nested_get_vmcs12_pages will either fix it up or
  10462. * remove the VM execution control.
  10463. */
  10464. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  10465. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  10466. if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
  10467. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  10468. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  10469. }
  10470. /*
  10471. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  10472. * entry, but only if the current (host) sp changed from the value
  10473. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  10474. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  10475. * here we just force the write to happen on entry.
  10476. */
  10477. vmx->host_rsp = 0;
  10478. exec_control = vmx_exec_control(vmx); /* L0's desires */
  10479. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  10480. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  10481. exec_control &= ~CPU_BASED_TPR_SHADOW;
  10482. exec_control |= vmcs12->cpu_based_vm_exec_control;
  10483. /*
  10484. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  10485. * nested_get_vmcs12_pages can't fix it up, the illegal value
  10486. * will result in a VM entry failure.
  10487. */
  10488. if (exec_control & CPU_BASED_TPR_SHADOW) {
  10489. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  10490. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  10491. } else {
  10492. #ifdef CONFIG_X86_64
  10493. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  10494. CPU_BASED_CR8_STORE_EXITING;
  10495. #endif
  10496. }
  10497. /*
  10498. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  10499. * for I/O port accesses.
  10500. */
  10501. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  10502. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  10503. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  10504. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  10505. * bitwise-or of what L1 wants to trap for L2, and what we want to
  10506. * trap. Note that CR0.TS also needs updating - we do this later.
  10507. */
  10508. update_exception_bitmap(vcpu);
  10509. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  10510. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  10511. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  10512. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  10513. * bits are further modified by vmx_set_efer() below.
  10514. */
  10515. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  10516. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  10517. * emulated by vmx_set_efer(), below.
  10518. */
  10519. vm_entry_controls_init(vmx,
  10520. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  10521. ~VM_ENTRY_IA32E_MODE) |
  10522. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  10523. if (vmx->nested.nested_run_pending &&
  10524. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  10525. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  10526. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  10527. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  10528. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  10529. }
  10530. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10531. if (kvm_has_tsc_control)
  10532. decache_tsc_multiplier(vmx);
  10533. if (enable_vpid) {
  10534. /*
  10535. * There is no direct mapping between vpid02 and vpid12, the
  10536. * vpid02 is per-vCPU for L0 and reused while the value of
  10537. * vpid12 is changed w/ one invvpid during nested vmentry.
  10538. * The vpid12 is allocated by L1 for L2, so it will not
  10539. * influence global bitmap(for vpid01 and vpid02 allocation)
  10540. * even if spawn a lot of nested vCPUs.
  10541. */
  10542. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  10543. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  10544. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  10545. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  10546. }
  10547. } else {
  10548. vmx_flush_tlb(vcpu, true);
  10549. }
  10550. }
  10551. if (enable_pml) {
  10552. /*
  10553. * Conceptually we want to copy the PML address and index from
  10554. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  10555. * since we always flush the log on each vmexit, this happens
  10556. * to be equivalent to simply resetting the fields in vmcs02.
  10557. */
  10558. ASSERT(vmx->pml_pg);
  10559. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  10560. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  10561. }
  10562. if (nested_cpu_has_ept(vmcs12)) {
  10563. if (nested_ept_init_mmu_context(vcpu)) {
  10564. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10565. return 1;
  10566. }
  10567. } else if (nested_cpu_has2(vmcs12,
  10568. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10569. vmx_flush_tlb(vcpu, true);
  10570. }
  10571. /*
  10572. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  10573. * bits which we consider mandatory enabled.
  10574. * The CR0_READ_SHADOW is what L2 should have expected to read given
  10575. * the specifications by L1; It's not enough to take
  10576. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  10577. * have more bits than L1 expected.
  10578. */
  10579. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  10580. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  10581. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  10582. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  10583. if (vmx->nested.nested_run_pending &&
  10584. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  10585. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  10586. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  10587. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10588. else
  10589. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10590. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  10591. vmx_set_efer(vcpu, vcpu->arch.efer);
  10592. /*
  10593. * Guest state is invalid and unrestricted guest is disabled,
  10594. * which means L1 attempted VMEntry to L2 with invalid state.
  10595. * Fail the VMEntry.
  10596. */
  10597. if (vmx->emulation_required) {
  10598. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10599. return 1;
  10600. }
  10601. /* Shadow page tables on either EPT or shadow page tables. */
  10602. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  10603. entry_failure_code))
  10604. return 1;
  10605. if (!enable_ept)
  10606. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  10607. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  10608. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  10609. return 0;
  10610. }
  10611. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  10612. {
  10613. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  10614. nested_cpu_has_virtual_nmis(vmcs12))
  10615. return -EINVAL;
  10616. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  10617. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  10618. return -EINVAL;
  10619. return 0;
  10620. }
  10621. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10622. {
  10623. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10624. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  10625. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  10626. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10627. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  10628. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10629. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  10630. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10631. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  10632. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10633. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  10634. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10635. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  10636. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10637. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  10638. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10639. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  10640. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10641. if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
  10642. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10643. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  10644. vmx->nested.msrs.procbased_ctls_low,
  10645. vmx->nested.msrs.procbased_ctls_high) ||
  10646. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  10647. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  10648. vmx->nested.msrs.secondary_ctls_low,
  10649. vmx->nested.msrs.secondary_ctls_high)) ||
  10650. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  10651. vmx->nested.msrs.pinbased_ctls_low,
  10652. vmx->nested.msrs.pinbased_ctls_high) ||
  10653. !vmx_control_verify(vmcs12->vm_exit_controls,
  10654. vmx->nested.msrs.exit_ctls_low,
  10655. vmx->nested.msrs.exit_ctls_high) ||
  10656. !vmx_control_verify(vmcs12->vm_entry_controls,
  10657. vmx->nested.msrs.entry_ctls_low,
  10658. vmx->nested.msrs.entry_ctls_high))
  10659. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10660. if (nested_vmx_check_nmi_controls(vmcs12))
  10661. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10662. if (nested_cpu_has_vmfunc(vmcs12)) {
  10663. if (vmcs12->vm_function_control &
  10664. ~vmx->nested.msrs.vmfunc_controls)
  10665. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10666. if (nested_cpu_has_eptp_switching(vmcs12)) {
  10667. if (!nested_cpu_has_ept(vmcs12) ||
  10668. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  10669. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10670. }
  10671. }
  10672. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  10673. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10674. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  10675. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  10676. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  10677. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  10678. /*
  10679. * From the Intel SDM, volume 3:
  10680. * Fields relevant to VM-entry event injection must be set properly.
  10681. * These fields are the VM-entry interruption-information field, the
  10682. * VM-entry exception error code, and the VM-entry instruction length.
  10683. */
  10684. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  10685. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  10686. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  10687. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  10688. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  10689. bool should_have_error_code;
  10690. bool urg = nested_cpu_has2(vmcs12,
  10691. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  10692. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  10693. /* VM-entry interruption-info field: interruption type */
  10694. if (intr_type == INTR_TYPE_RESERVED ||
  10695. (intr_type == INTR_TYPE_OTHER_EVENT &&
  10696. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  10697. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10698. /* VM-entry interruption-info field: vector */
  10699. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  10700. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  10701. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  10702. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10703. /* VM-entry interruption-info field: deliver error code */
  10704. should_have_error_code =
  10705. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  10706. x86_exception_has_error_code(vector);
  10707. if (has_error_code != should_have_error_code)
  10708. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10709. /* VM-entry exception error code */
  10710. if (has_error_code &&
  10711. vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
  10712. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10713. /* VM-entry interruption-info field: reserved bits */
  10714. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  10715. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10716. /* VM-entry instruction length */
  10717. switch (intr_type) {
  10718. case INTR_TYPE_SOFT_EXCEPTION:
  10719. case INTR_TYPE_SOFT_INTR:
  10720. case INTR_TYPE_PRIV_SW_EXCEPTION:
  10721. if ((vmcs12->vm_entry_instruction_len > 15) ||
  10722. (vmcs12->vm_entry_instruction_len == 0 &&
  10723. !nested_cpu_has_zero_length_injection(vcpu)))
  10724. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10725. }
  10726. }
  10727. return 0;
  10728. }
  10729. static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
  10730. struct vmcs12 *vmcs12)
  10731. {
  10732. int r;
  10733. struct page *page;
  10734. struct vmcs12 *shadow;
  10735. if (vmcs12->vmcs_link_pointer == -1ull)
  10736. return 0;
  10737. if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
  10738. return -EINVAL;
  10739. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10740. if (is_error_page(page))
  10741. return -EINVAL;
  10742. r = 0;
  10743. shadow = kmap(page);
  10744. if (shadow->hdr.revision_id != VMCS12_REVISION ||
  10745. shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
  10746. r = -EINVAL;
  10747. kunmap(page);
  10748. kvm_release_page_clean(page);
  10749. return r;
  10750. }
  10751. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10752. u32 *exit_qual)
  10753. {
  10754. bool ia32e;
  10755. *exit_qual = ENTRY_FAIL_DEFAULT;
  10756. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  10757. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  10758. return 1;
  10759. if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
  10760. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  10761. return 1;
  10762. }
  10763. /*
  10764. * If the load IA32_EFER VM-entry control is 1, the following checks
  10765. * are performed on the field for the IA32_EFER MSR:
  10766. * - Bits reserved in the IA32_EFER MSR must be 0.
  10767. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  10768. * the IA-32e mode guest VM-exit control. It must also be identical
  10769. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  10770. * CR0.PG) is 1.
  10771. */
  10772. if (to_vmx(vcpu)->nested.nested_run_pending &&
  10773. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  10774. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  10775. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  10776. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  10777. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  10778. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  10779. return 1;
  10780. }
  10781. /*
  10782. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  10783. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  10784. * the values of the LMA and LME bits in the field must each be that of
  10785. * the host address-space size VM-exit control.
  10786. */
  10787. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  10788. ia32e = (vmcs12->vm_exit_controls &
  10789. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  10790. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  10791. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  10792. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  10793. return 1;
  10794. }
  10795. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  10796. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  10797. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  10798. return 1;
  10799. return 0;
  10800. }
  10801. /*
  10802. * If exit_qual is NULL, this is being called from state restore (either RSM
  10803. * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
  10804. */
  10805. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
  10806. {
  10807. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10808. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10809. bool from_vmentry = !!exit_qual;
  10810. u32 dummy_exit_qual;
  10811. int r = 0;
  10812. enter_guest_mode(vcpu);
  10813. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  10814. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10815. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  10816. vmx_segment_cache_clear(vmx);
  10817. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10818. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  10819. r = EXIT_REASON_INVALID_STATE;
  10820. if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
  10821. goto fail;
  10822. if (from_vmentry) {
  10823. nested_get_vmcs12_pages(vcpu);
  10824. r = EXIT_REASON_MSR_LOAD_FAIL;
  10825. *exit_qual = nested_vmx_load_msr(vcpu,
  10826. vmcs12->vm_entry_msr_load_addr,
  10827. vmcs12->vm_entry_msr_load_count);
  10828. if (*exit_qual)
  10829. goto fail;
  10830. } else {
  10831. /*
  10832. * The MMU is not initialized to point at the right entities yet and
  10833. * "get pages" would need to read data from the guest (i.e. we will
  10834. * need to perform gpa to hpa translation). Request a call
  10835. * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
  10836. * have already been set at vmentry time and should not be reset.
  10837. */
  10838. kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
  10839. }
  10840. /*
  10841. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  10842. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  10843. * returned as far as L1 is concerned. It will only return (and set
  10844. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  10845. */
  10846. return 0;
  10847. fail:
  10848. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10849. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10850. leave_guest_mode(vcpu);
  10851. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10852. return r;
  10853. }
  10854. /*
  10855. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  10856. * for running an L2 nested guest.
  10857. */
  10858. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  10859. {
  10860. struct vmcs12 *vmcs12;
  10861. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10862. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  10863. u32 exit_qual;
  10864. int ret;
  10865. if (!nested_vmx_check_permission(vcpu))
  10866. return 1;
  10867. if (!nested_vmx_check_vmcs12(vcpu))
  10868. goto out;
  10869. vmcs12 = get_vmcs12(vcpu);
  10870. /*
  10871. * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
  10872. * that there *is* a valid VMCS pointer, RFLAGS.CF is set
  10873. * rather than RFLAGS.ZF, and no error number is stored to the
  10874. * VM-instruction error field.
  10875. */
  10876. if (vmcs12->hdr.shadow_vmcs) {
  10877. nested_vmx_failInvalid(vcpu);
  10878. goto out;
  10879. }
  10880. if (enable_shadow_vmcs)
  10881. copy_shadow_to_vmcs12(vmx);
  10882. /*
  10883. * The nested entry process starts with enforcing various prerequisites
  10884. * on vmcs12 as required by the Intel SDM, and act appropriately when
  10885. * they fail: As the SDM explains, some conditions should cause the
  10886. * instruction to fail, while others will cause the instruction to seem
  10887. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  10888. * To speed up the normal (success) code path, we should avoid checking
  10889. * for misconfigurations which will anyway be caught by the processor
  10890. * when using the merged vmcs02.
  10891. */
  10892. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  10893. nested_vmx_failValid(vcpu,
  10894. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  10895. goto out;
  10896. }
  10897. if (vmcs12->launch_state == launch) {
  10898. nested_vmx_failValid(vcpu,
  10899. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  10900. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  10901. goto out;
  10902. }
  10903. ret = check_vmentry_prereqs(vcpu, vmcs12);
  10904. if (ret) {
  10905. nested_vmx_failValid(vcpu, ret);
  10906. goto out;
  10907. }
  10908. /*
  10909. * After this point, the trap flag no longer triggers a singlestep trap
  10910. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  10911. * This is not 100% correct; for performance reasons, we delegate most
  10912. * of the checks on host state to the processor. If those fail,
  10913. * the singlestep trap is missed.
  10914. */
  10915. skip_emulated_instruction(vcpu);
  10916. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  10917. if (ret) {
  10918. nested_vmx_entry_failure(vcpu, vmcs12,
  10919. EXIT_REASON_INVALID_STATE, exit_qual);
  10920. return 1;
  10921. }
  10922. /*
  10923. * We're finally done with prerequisite checking, and can start with
  10924. * the nested entry.
  10925. */
  10926. vmx->nested.nested_run_pending = 1;
  10927. ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
  10928. if (ret) {
  10929. nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
  10930. vmx->nested.nested_run_pending = 0;
  10931. return 1;
  10932. }
  10933. /* Hide L1D cache contents from the nested guest. */
  10934. vmx->vcpu.arch.l1tf_flush_l1d = true;
  10935. /*
  10936. * Must happen outside of enter_vmx_non_root_mode() as it will
  10937. * also be used as part of restoring nVMX state for
  10938. * snapshot restore (migration).
  10939. *
  10940. * In this flow, it is assumed that vmcs12 cache was
  10941. * trasferred as part of captured nVMX state and should
  10942. * therefore not be read from guest memory (which may not
  10943. * exist on destination host yet).
  10944. */
  10945. nested_cache_shadow_vmcs12(vcpu, vmcs12);
  10946. /*
  10947. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  10948. * by event injection, halt vcpu.
  10949. */
  10950. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  10951. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
  10952. vmx->nested.nested_run_pending = 0;
  10953. return kvm_vcpu_halt(vcpu);
  10954. }
  10955. return 1;
  10956. out:
  10957. return kvm_skip_emulated_instruction(vcpu);
  10958. }
  10959. /*
  10960. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  10961. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  10962. * This function returns the new value we should put in vmcs12.guest_cr0.
  10963. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  10964. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  10965. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  10966. * didn't trap the bit, because if L1 did, so would L0).
  10967. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  10968. * been modified by L2, and L1 knows it. So just leave the old value of
  10969. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  10970. * isn't relevant, because if L0 traps this bit it can set it to anything.
  10971. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  10972. * changed these bits, and therefore they need to be updated, but L0
  10973. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  10974. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  10975. */
  10976. static inline unsigned long
  10977. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10978. {
  10979. return
  10980. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  10981. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  10982. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  10983. vcpu->arch.cr0_guest_owned_bits));
  10984. }
  10985. static inline unsigned long
  10986. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10987. {
  10988. return
  10989. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  10990. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  10991. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  10992. vcpu->arch.cr4_guest_owned_bits));
  10993. }
  10994. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  10995. struct vmcs12 *vmcs12)
  10996. {
  10997. u32 idt_vectoring;
  10998. unsigned int nr;
  10999. if (vcpu->arch.exception.injected) {
  11000. nr = vcpu->arch.exception.nr;
  11001. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11002. if (kvm_exception_is_soft(nr)) {
  11003. vmcs12->vm_exit_instruction_len =
  11004. vcpu->arch.event_exit_inst_len;
  11005. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  11006. } else
  11007. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  11008. if (vcpu->arch.exception.has_error_code) {
  11009. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  11010. vmcs12->idt_vectoring_error_code =
  11011. vcpu->arch.exception.error_code;
  11012. }
  11013. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11014. } else if (vcpu->arch.nmi_injected) {
  11015. vmcs12->idt_vectoring_info_field =
  11016. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  11017. } else if (vcpu->arch.interrupt.injected) {
  11018. nr = vcpu->arch.interrupt.nr;
  11019. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11020. if (vcpu->arch.interrupt.soft) {
  11021. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  11022. vmcs12->vm_entry_instruction_len =
  11023. vcpu->arch.event_exit_inst_len;
  11024. } else
  11025. idt_vectoring |= INTR_TYPE_EXT_INTR;
  11026. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11027. }
  11028. }
  11029. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  11030. {
  11031. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11032. unsigned long exit_qual;
  11033. bool block_nested_events =
  11034. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  11035. if (vcpu->arch.exception.pending &&
  11036. nested_vmx_check_exception(vcpu, &exit_qual)) {
  11037. if (block_nested_events)
  11038. return -EBUSY;
  11039. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  11040. return 0;
  11041. }
  11042. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  11043. vmx->nested.preemption_timer_expired) {
  11044. if (block_nested_events)
  11045. return -EBUSY;
  11046. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  11047. return 0;
  11048. }
  11049. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  11050. if (block_nested_events)
  11051. return -EBUSY;
  11052. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  11053. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  11054. INTR_INFO_VALID_MASK, 0);
  11055. /*
  11056. * The NMI-triggered VM exit counts as injection:
  11057. * clear this one and block further NMIs.
  11058. */
  11059. vcpu->arch.nmi_pending = 0;
  11060. vmx_set_nmi_mask(vcpu, true);
  11061. return 0;
  11062. }
  11063. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  11064. nested_exit_on_intr(vcpu)) {
  11065. if (block_nested_events)
  11066. return -EBUSY;
  11067. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  11068. return 0;
  11069. }
  11070. vmx_complete_nested_posted_interrupt(vcpu);
  11071. return 0;
  11072. }
  11073. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  11074. {
  11075. ktime_t remaining =
  11076. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  11077. u64 value;
  11078. if (ktime_to_ns(remaining) <= 0)
  11079. return 0;
  11080. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  11081. do_div(value, 1000000);
  11082. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  11083. }
  11084. /*
  11085. * Update the guest state fields of vmcs12 to reflect changes that
  11086. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  11087. * VM-entry controls is also updated, since this is really a guest
  11088. * state bit.)
  11089. */
  11090. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11091. {
  11092. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  11093. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  11094. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  11095. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  11096. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  11097. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  11098. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  11099. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  11100. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  11101. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  11102. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  11103. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  11104. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  11105. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  11106. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  11107. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  11108. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  11109. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  11110. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  11111. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  11112. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  11113. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  11114. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  11115. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  11116. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  11117. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  11118. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  11119. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  11120. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  11121. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  11122. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  11123. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  11124. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  11125. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  11126. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  11127. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  11128. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  11129. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  11130. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  11131. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  11132. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  11133. vmcs12->guest_interruptibility_info =
  11134. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  11135. vmcs12->guest_pending_dbg_exceptions =
  11136. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  11137. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  11138. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  11139. else
  11140. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  11141. if (nested_cpu_has_preemption_timer(vmcs12)) {
  11142. if (vmcs12->vm_exit_controls &
  11143. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  11144. vmcs12->vmx_preemption_timer_value =
  11145. vmx_get_preemption_timer_value(vcpu);
  11146. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  11147. }
  11148. /*
  11149. * In some cases (usually, nested EPT), L2 is allowed to change its
  11150. * own CR3 without exiting. If it has changed it, we must keep it.
  11151. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  11152. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  11153. *
  11154. * Additionally, restore L2's PDPTR to vmcs12.
  11155. */
  11156. if (enable_ept) {
  11157. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  11158. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  11159. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  11160. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  11161. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  11162. }
  11163. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  11164. if (nested_cpu_has_vid(vmcs12))
  11165. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  11166. vmcs12->vm_entry_controls =
  11167. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  11168. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  11169. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  11170. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  11171. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11172. }
  11173. /* TODO: These cannot have changed unless we have MSR bitmaps and
  11174. * the relevant bit asks not to trap the change */
  11175. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  11176. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  11177. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  11178. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  11179. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  11180. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  11181. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  11182. if (kvm_mpx_supported())
  11183. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11184. }
  11185. /*
  11186. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  11187. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  11188. * and this function updates it to reflect the changes to the guest state while
  11189. * L2 was running (and perhaps made some exits which were handled directly by L0
  11190. * without going back to L1), and to reflect the exit reason.
  11191. * Note that we do not have to copy here all VMCS fields, just those that
  11192. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  11193. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  11194. * which already writes to vmcs12 directly.
  11195. */
  11196. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11197. u32 exit_reason, u32 exit_intr_info,
  11198. unsigned long exit_qualification)
  11199. {
  11200. /* update guest state fields: */
  11201. sync_vmcs12(vcpu, vmcs12);
  11202. /* update exit information fields: */
  11203. vmcs12->vm_exit_reason = exit_reason;
  11204. vmcs12->exit_qualification = exit_qualification;
  11205. vmcs12->vm_exit_intr_info = exit_intr_info;
  11206. vmcs12->idt_vectoring_info_field = 0;
  11207. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  11208. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  11209. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  11210. vmcs12->launch_state = 1;
  11211. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  11212. * instead of reading the real value. */
  11213. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  11214. /*
  11215. * Transfer the event that L0 or L1 may wanted to inject into
  11216. * L2 to IDT_VECTORING_INFO_FIELD.
  11217. */
  11218. vmcs12_save_pending_event(vcpu, vmcs12);
  11219. }
  11220. /*
  11221. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  11222. * preserved above and would only end up incorrectly in L1.
  11223. */
  11224. vcpu->arch.nmi_injected = false;
  11225. kvm_clear_exception_queue(vcpu);
  11226. kvm_clear_interrupt_queue(vcpu);
  11227. }
  11228. static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
  11229. struct vmcs12 *vmcs12)
  11230. {
  11231. u32 entry_failure_code;
  11232. nested_ept_uninit_mmu_context(vcpu);
  11233. /*
  11234. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  11235. * couldn't have changed.
  11236. */
  11237. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  11238. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  11239. if (!enable_ept)
  11240. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  11241. }
  11242. /*
  11243. * A part of what we need to when the nested L2 guest exits and we want to
  11244. * run its L1 parent, is to reset L1's guest state to the host state specified
  11245. * in vmcs12.
  11246. * This function is to be called not only on normal nested exit, but also on
  11247. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  11248. * Failures During or After Loading Guest State").
  11249. * This function should be called when the active VMCS is L1's (vmcs01).
  11250. */
  11251. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11252. struct vmcs12 *vmcs12)
  11253. {
  11254. struct kvm_segment seg;
  11255. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  11256. vcpu->arch.efer = vmcs12->host_ia32_efer;
  11257. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11258. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  11259. else
  11260. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  11261. vmx_set_efer(vcpu, vcpu->arch.efer);
  11262. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  11263. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  11264. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  11265. /*
  11266. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  11267. * actually changed, because vmx_set_cr0 refers to efer set above.
  11268. *
  11269. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  11270. * (KVM doesn't change it);
  11271. */
  11272. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11273. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  11274. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  11275. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11276. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  11277. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  11278. /*
  11279. * If vmcs01 don't use VPID, CPU flushes TLB on every
  11280. * VMEntry/VMExit. Thus, no need to flush TLB.
  11281. *
  11282. * If vmcs12 uses VPID, TLB entries populated by L2 are
  11283. * tagged with vmx->nested.vpid02 while L1 entries are tagged
  11284. * with vmx->vpid. Thus, no need to flush TLB.
  11285. *
  11286. * Therefore, flush TLB only in case vmcs01 uses VPID and
  11287. * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
  11288. * are both tagged with vmx->vpid.
  11289. */
  11290. if (enable_vpid &&
  11291. !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
  11292. vmx_flush_tlb(vcpu, true);
  11293. }
  11294. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  11295. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  11296. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  11297. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  11298. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  11299. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  11300. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  11301. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  11302. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  11303. vmcs_write64(GUEST_BNDCFGS, 0);
  11304. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  11305. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  11306. vcpu->arch.pat = vmcs12->host_ia32_pat;
  11307. }
  11308. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  11309. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  11310. vmcs12->host_ia32_perf_global_ctrl);
  11311. /* Set L1 segment info according to Intel SDM
  11312. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  11313. seg = (struct kvm_segment) {
  11314. .base = 0,
  11315. .limit = 0xFFFFFFFF,
  11316. .selector = vmcs12->host_cs_selector,
  11317. .type = 11,
  11318. .present = 1,
  11319. .s = 1,
  11320. .g = 1
  11321. };
  11322. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11323. seg.l = 1;
  11324. else
  11325. seg.db = 1;
  11326. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  11327. seg = (struct kvm_segment) {
  11328. .base = 0,
  11329. .limit = 0xFFFFFFFF,
  11330. .type = 3,
  11331. .present = 1,
  11332. .s = 1,
  11333. .db = 1,
  11334. .g = 1
  11335. };
  11336. seg.selector = vmcs12->host_ds_selector;
  11337. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  11338. seg.selector = vmcs12->host_es_selector;
  11339. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  11340. seg.selector = vmcs12->host_ss_selector;
  11341. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  11342. seg.selector = vmcs12->host_fs_selector;
  11343. seg.base = vmcs12->host_fs_base;
  11344. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  11345. seg.selector = vmcs12->host_gs_selector;
  11346. seg.base = vmcs12->host_gs_base;
  11347. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  11348. seg = (struct kvm_segment) {
  11349. .base = vmcs12->host_tr_base,
  11350. .limit = 0x67,
  11351. .selector = vmcs12->host_tr_selector,
  11352. .type = 11,
  11353. .present = 1
  11354. };
  11355. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  11356. kvm_set_dr(vcpu, 7, 0x400);
  11357. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  11358. if (cpu_has_vmx_msr_bitmap())
  11359. vmx_update_msr_bitmap(vcpu);
  11360. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  11361. vmcs12->vm_exit_msr_load_count))
  11362. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  11363. }
  11364. /*
  11365. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  11366. * and modify vmcs12 to make it see what it would expect to see there if
  11367. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  11368. */
  11369. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  11370. u32 exit_intr_info,
  11371. unsigned long exit_qualification)
  11372. {
  11373. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11374. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11375. /* trying to cancel vmlaunch/vmresume is a bug */
  11376. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  11377. /*
  11378. * The only expected VM-instruction error is "VM entry with
  11379. * invalid control field(s)." Anything else indicates a
  11380. * problem with L0.
  11381. */
  11382. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  11383. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  11384. leave_guest_mode(vcpu);
  11385. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11386. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  11387. if (likely(!vmx->fail)) {
  11388. if (exit_reason == -1)
  11389. sync_vmcs12(vcpu, vmcs12);
  11390. else
  11391. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  11392. exit_qualification);
  11393. /*
  11394. * Must happen outside of sync_vmcs12() as it will
  11395. * also be used to capture vmcs12 cache as part of
  11396. * capturing nVMX state for snapshot (migration).
  11397. *
  11398. * Otherwise, this flush will dirty guest memory at a
  11399. * point it is already assumed by user-space to be
  11400. * immutable.
  11401. */
  11402. nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
  11403. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  11404. vmcs12->vm_exit_msr_store_count))
  11405. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  11406. }
  11407. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11408. vm_entry_controls_reset_shadow(vmx);
  11409. vm_exit_controls_reset_shadow(vmx);
  11410. vmx_segment_cache_clear(vmx);
  11411. /* Update any VMCS fields that might have changed while L2 ran */
  11412. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11413. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11414. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  11415. if (vmx->hv_deadline_tsc == -1)
  11416. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  11417. PIN_BASED_VMX_PREEMPTION_TIMER);
  11418. else
  11419. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  11420. PIN_BASED_VMX_PREEMPTION_TIMER);
  11421. if (kvm_has_tsc_control)
  11422. decache_tsc_multiplier(vmx);
  11423. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  11424. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  11425. vmx_set_virtual_apic_mode(vcpu);
  11426. } else if (!nested_cpu_has_ept(vmcs12) &&
  11427. nested_cpu_has2(vmcs12,
  11428. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  11429. vmx_flush_tlb(vcpu, true);
  11430. }
  11431. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  11432. vmx->host_rsp = 0;
  11433. /* Unpin physical memory we referred to in vmcs02 */
  11434. if (vmx->nested.apic_access_page) {
  11435. kvm_release_page_dirty(vmx->nested.apic_access_page);
  11436. vmx->nested.apic_access_page = NULL;
  11437. }
  11438. if (vmx->nested.virtual_apic_page) {
  11439. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  11440. vmx->nested.virtual_apic_page = NULL;
  11441. }
  11442. if (vmx->nested.pi_desc_page) {
  11443. kunmap(vmx->nested.pi_desc_page);
  11444. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  11445. vmx->nested.pi_desc_page = NULL;
  11446. vmx->nested.pi_desc = NULL;
  11447. }
  11448. /*
  11449. * We are now running in L2, mmu_notifier will force to reload the
  11450. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  11451. */
  11452. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  11453. if (enable_shadow_vmcs && exit_reason != -1)
  11454. vmx->nested.sync_shadow_vmcs = true;
  11455. /* in case we halted in L2 */
  11456. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  11457. if (likely(!vmx->fail)) {
  11458. /*
  11459. * TODO: SDM says that with acknowledge interrupt on
  11460. * exit, bit 31 of the VM-exit interrupt information
  11461. * (valid interrupt) is always set to 1 on
  11462. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  11463. * need kvm_cpu_has_interrupt(). See the commit
  11464. * message for details.
  11465. */
  11466. if (nested_exit_intr_ack_set(vcpu) &&
  11467. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  11468. kvm_cpu_has_interrupt(vcpu)) {
  11469. int irq = kvm_cpu_get_interrupt(vcpu);
  11470. WARN_ON(irq < 0);
  11471. vmcs12->vm_exit_intr_info = irq |
  11472. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  11473. }
  11474. if (exit_reason != -1)
  11475. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  11476. vmcs12->exit_qualification,
  11477. vmcs12->idt_vectoring_info_field,
  11478. vmcs12->vm_exit_intr_info,
  11479. vmcs12->vm_exit_intr_error_code,
  11480. KVM_ISA_VMX);
  11481. load_vmcs12_host_state(vcpu, vmcs12);
  11482. return;
  11483. }
  11484. /*
  11485. * After an early L2 VM-entry failure, we're now back
  11486. * in L1 which thinks it just finished a VMLAUNCH or
  11487. * VMRESUME instruction, so we need to set the failure
  11488. * flag and the VM-instruction error field of the VMCS
  11489. * accordingly.
  11490. */
  11491. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11492. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  11493. /*
  11494. * The emulated instruction was already skipped in
  11495. * nested_vmx_run, but the updated RIP was never
  11496. * written back to the vmcs01.
  11497. */
  11498. skip_emulated_instruction(vcpu);
  11499. vmx->fail = 0;
  11500. }
  11501. /*
  11502. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  11503. */
  11504. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  11505. {
  11506. if (is_guest_mode(vcpu)) {
  11507. to_vmx(vcpu)->nested.nested_run_pending = 0;
  11508. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11509. }
  11510. free_nested(to_vmx(vcpu));
  11511. }
  11512. /*
  11513. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  11514. * 23.7 "VM-entry failures during or after loading guest state" (this also
  11515. * lists the acceptable exit-reason and exit-qualification parameters).
  11516. * It should only be called before L2 actually succeeded to run, and when
  11517. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  11518. */
  11519. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  11520. struct vmcs12 *vmcs12,
  11521. u32 reason, unsigned long qualification)
  11522. {
  11523. load_vmcs12_host_state(vcpu, vmcs12);
  11524. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  11525. vmcs12->exit_qualification = qualification;
  11526. nested_vmx_succeed(vcpu);
  11527. if (enable_shadow_vmcs)
  11528. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  11529. }
  11530. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  11531. struct x86_instruction_info *info,
  11532. enum x86_intercept_stage stage)
  11533. {
  11534. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11535. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  11536. /*
  11537. * RDPID causes #UD if disabled through secondary execution controls.
  11538. * Because it is marked as EmulateOnUD, we need to intercept it here.
  11539. */
  11540. if (info->intercept == x86_intercept_rdtscp &&
  11541. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  11542. ctxt->exception.vector = UD_VECTOR;
  11543. ctxt->exception.error_code_valid = false;
  11544. return X86EMUL_PROPAGATE_FAULT;
  11545. }
  11546. /* TODO: check more intercepts... */
  11547. return X86EMUL_CONTINUE;
  11548. }
  11549. #ifdef CONFIG_X86_64
  11550. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  11551. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  11552. u64 divisor, u64 *result)
  11553. {
  11554. u64 low = a << shift, high = a >> (64 - shift);
  11555. /* To avoid the overflow on divq */
  11556. if (high >= divisor)
  11557. return 1;
  11558. /* Low hold the result, high hold rem which is discarded */
  11559. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  11560. "rm" (divisor), "0" (low), "1" (high));
  11561. *result = low;
  11562. return 0;
  11563. }
  11564. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  11565. {
  11566. struct vcpu_vmx *vmx;
  11567. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  11568. if (kvm_mwait_in_guest(vcpu->kvm))
  11569. return -EOPNOTSUPP;
  11570. vmx = to_vmx(vcpu);
  11571. tscl = rdtsc();
  11572. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  11573. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  11574. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  11575. if (delta_tsc > lapic_timer_advance_cycles)
  11576. delta_tsc -= lapic_timer_advance_cycles;
  11577. else
  11578. delta_tsc = 0;
  11579. /* Convert to host delta tsc if tsc scaling is enabled */
  11580. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  11581. u64_shl_div_u64(delta_tsc,
  11582. kvm_tsc_scaling_ratio_frac_bits,
  11583. vcpu->arch.tsc_scaling_ratio,
  11584. &delta_tsc))
  11585. return -ERANGE;
  11586. /*
  11587. * If the delta tsc can't fit in the 32 bit after the multi shift,
  11588. * we can't use the preemption timer.
  11589. * It's possible that it fits on later vmentries, but checking
  11590. * on every vmentry is costly so we just use an hrtimer.
  11591. */
  11592. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  11593. return -ERANGE;
  11594. vmx->hv_deadline_tsc = tscl + delta_tsc;
  11595. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  11596. PIN_BASED_VMX_PREEMPTION_TIMER);
  11597. return delta_tsc == 0;
  11598. }
  11599. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  11600. {
  11601. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11602. vmx->hv_deadline_tsc = -1;
  11603. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  11604. PIN_BASED_VMX_PREEMPTION_TIMER);
  11605. }
  11606. #endif
  11607. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  11608. {
  11609. if (!kvm_pause_in_guest(vcpu->kvm))
  11610. shrink_ple_window(vcpu);
  11611. }
  11612. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  11613. struct kvm_memory_slot *slot)
  11614. {
  11615. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  11616. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  11617. }
  11618. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  11619. struct kvm_memory_slot *slot)
  11620. {
  11621. kvm_mmu_slot_set_dirty(kvm, slot);
  11622. }
  11623. static void vmx_flush_log_dirty(struct kvm *kvm)
  11624. {
  11625. kvm_flush_pml_buffers(kvm);
  11626. }
  11627. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  11628. {
  11629. struct vmcs12 *vmcs12;
  11630. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11631. gpa_t gpa;
  11632. struct page *page = NULL;
  11633. u64 *pml_address;
  11634. if (is_guest_mode(vcpu)) {
  11635. WARN_ON_ONCE(vmx->nested.pml_full);
  11636. /*
  11637. * Check if PML is enabled for the nested guest.
  11638. * Whether eptp bit 6 is set is already checked
  11639. * as part of A/D emulation.
  11640. */
  11641. vmcs12 = get_vmcs12(vcpu);
  11642. if (!nested_cpu_has_pml(vmcs12))
  11643. return 0;
  11644. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  11645. vmx->nested.pml_full = true;
  11646. return 1;
  11647. }
  11648. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  11649. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  11650. if (is_error_page(page))
  11651. return 0;
  11652. pml_address = kmap(page);
  11653. pml_address[vmcs12->guest_pml_index--] = gpa;
  11654. kunmap(page);
  11655. kvm_release_page_clean(page);
  11656. }
  11657. return 0;
  11658. }
  11659. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  11660. struct kvm_memory_slot *memslot,
  11661. gfn_t offset, unsigned long mask)
  11662. {
  11663. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  11664. }
  11665. static void __pi_post_block(struct kvm_vcpu *vcpu)
  11666. {
  11667. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11668. struct pi_desc old, new;
  11669. unsigned int dest;
  11670. do {
  11671. old.control = new.control = pi_desc->control;
  11672. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  11673. "Wakeup handler not enabled while the VCPU is blocked\n");
  11674. dest = cpu_physical_id(vcpu->cpu);
  11675. if (x2apic_enabled())
  11676. new.ndst = dest;
  11677. else
  11678. new.ndst = (dest << 8) & 0xFF00;
  11679. /* set 'NV' to 'notification vector' */
  11680. new.nv = POSTED_INTR_VECTOR;
  11681. } while (cmpxchg64(&pi_desc->control, old.control,
  11682. new.control) != old.control);
  11683. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  11684. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11685. list_del(&vcpu->blocked_vcpu_list);
  11686. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11687. vcpu->pre_pcpu = -1;
  11688. }
  11689. }
  11690. /*
  11691. * This routine does the following things for vCPU which is going
  11692. * to be blocked if VT-d PI is enabled.
  11693. * - Store the vCPU to the wakeup list, so when interrupts happen
  11694. * we can find the right vCPU to wake up.
  11695. * - Change the Posted-interrupt descriptor as below:
  11696. * 'NDST' <-- vcpu->pre_pcpu
  11697. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  11698. * - If 'ON' is set during this process, which means at least one
  11699. * interrupt is posted for this vCPU, we cannot block it, in
  11700. * this case, return 1, otherwise, return 0.
  11701. *
  11702. */
  11703. static int pi_pre_block(struct kvm_vcpu *vcpu)
  11704. {
  11705. unsigned int dest;
  11706. struct pi_desc old, new;
  11707. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11708. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  11709. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11710. !kvm_vcpu_apicv_active(vcpu))
  11711. return 0;
  11712. WARN_ON(irqs_disabled());
  11713. local_irq_disable();
  11714. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  11715. vcpu->pre_pcpu = vcpu->cpu;
  11716. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11717. list_add_tail(&vcpu->blocked_vcpu_list,
  11718. &per_cpu(blocked_vcpu_on_cpu,
  11719. vcpu->pre_pcpu));
  11720. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11721. }
  11722. do {
  11723. old.control = new.control = pi_desc->control;
  11724. WARN((pi_desc->sn == 1),
  11725. "Warning: SN field of posted-interrupts "
  11726. "is set before blocking\n");
  11727. /*
  11728. * Since vCPU can be preempted during this process,
  11729. * vcpu->cpu could be different with pre_pcpu, we
  11730. * need to set pre_pcpu as the destination of wakeup
  11731. * notification event, then we can find the right vCPU
  11732. * to wakeup in wakeup handler if interrupts happen
  11733. * when the vCPU is in blocked state.
  11734. */
  11735. dest = cpu_physical_id(vcpu->pre_pcpu);
  11736. if (x2apic_enabled())
  11737. new.ndst = dest;
  11738. else
  11739. new.ndst = (dest << 8) & 0xFF00;
  11740. /* set 'NV' to 'wakeup vector' */
  11741. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  11742. } while (cmpxchg64(&pi_desc->control, old.control,
  11743. new.control) != old.control);
  11744. /* We should not block the vCPU if an interrupt is posted for it. */
  11745. if (pi_test_on(pi_desc) == 1)
  11746. __pi_post_block(vcpu);
  11747. local_irq_enable();
  11748. return (vcpu->pre_pcpu == -1);
  11749. }
  11750. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  11751. {
  11752. if (pi_pre_block(vcpu))
  11753. return 1;
  11754. if (kvm_lapic_hv_timer_in_use(vcpu))
  11755. kvm_lapic_switch_to_sw_timer(vcpu);
  11756. return 0;
  11757. }
  11758. static void pi_post_block(struct kvm_vcpu *vcpu)
  11759. {
  11760. if (vcpu->pre_pcpu == -1)
  11761. return;
  11762. WARN_ON(irqs_disabled());
  11763. local_irq_disable();
  11764. __pi_post_block(vcpu);
  11765. local_irq_enable();
  11766. }
  11767. static void vmx_post_block(struct kvm_vcpu *vcpu)
  11768. {
  11769. if (kvm_x86_ops->set_hv_timer)
  11770. kvm_lapic_switch_to_hv_timer(vcpu);
  11771. pi_post_block(vcpu);
  11772. }
  11773. /*
  11774. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  11775. *
  11776. * @kvm: kvm
  11777. * @host_irq: host irq of the interrupt
  11778. * @guest_irq: gsi of the interrupt
  11779. * @set: set or unset PI
  11780. * returns 0 on success, < 0 on failure
  11781. */
  11782. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  11783. uint32_t guest_irq, bool set)
  11784. {
  11785. struct kvm_kernel_irq_routing_entry *e;
  11786. struct kvm_irq_routing_table *irq_rt;
  11787. struct kvm_lapic_irq irq;
  11788. struct kvm_vcpu *vcpu;
  11789. struct vcpu_data vcpu_info;
  11790. int idx, ret = 0;
  11791. if (!kvm_arch_has_assigned_device(kvm) ||
  11792. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11793. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  11794. return 0;
  11795. idx = srcu_read_lock(&kvm->irq_srcu);
  11796. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  11797. if (guest_irq >= irq_rt->nr_rt_entries ||
  11798. hlist_empty(&irq_rt->map[guest_irq])) {
  11799. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  11800. guest_irq, irq_rt->nr_rt_entries);
  11801. goto out;
  11802. }
  11803. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  11804. if (e->type != KVM_IRQ_ROUTING_MSI)
  11805. continue;
  11806. /*
  11807. * VT-d PI cannot support posting multicast/broadcast
  11808. * interrupts to a vCPU, we still use interrupt remapping
  11809. * for these kind of interrupts.
  11810. *
  11811. * For lowest-priority interrupts, we only support
  11812. * those with single CPU as the destination, e.g. user
  11813. * configures the interrupts via /proc/irq or uses
  11814. * irqbalance to make the interrupts single-CPU.
  11815. *
  11816. * We will support full lowest-priority interrupt later.
  11817. */
  11818. kvm_set_msi_irq(kvm, e, &irq);
  11819. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  11820. /*
  11821. * Make sure the IRTE is in remapped mode if
  11822. * we don't handle it in posted mode.
  11823. */
  11824. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11825. if (ret < 0) {
  11826. printk(KERN_INFO
  11827. "failed to back to remapped mode, irq: %u\n",
  11828. host_irq);
  11829. goto out;
  11830. }
  11831. continue;
  11832. }
  11833. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  11834. vcpu_info.vector = irq.vector;
  11835. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  11836. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  11837. if (set)
  11838. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  11839. else
  11840. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11841. if (ret < 0) {
  11842. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  11843. __func__);
  11844. goto out;
  11845. }
  11846. }
  11847. ret = 0;
  11848. out:
  11849. srcu_read_unlock(&kvm->irq_srcu, idx);
  11850. return ret;
  11851. }
  11852. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  11853. {
  11854. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  11855. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  11856. FEATURE_CONTROL_LMCE;
  11857. else
  11858. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  11859. ~FEATURE_CONTROL_LMCE;
  11860. }
  11861. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  11862. {
  11863. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  11864. if (to_vmx(vcpu)->nested.nested_run_pending)
  11865. return 0;
  11866. return 1;
  11867. }
  11868. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  11869. {
  11870. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11871. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  11872. if (vmx->nested.smm.guest_mode)
  11873. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11874. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  11875. vmx->nested.vmxon = false;
  11876. vmx_clear_hlt(vcpu);
  11877. return 0;
  11878. }
  11879. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  11880. {
  11881. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11882. int ret;
  11883. if (vmx->nested.smm.vmxon) {
  11884. vmx->nested.vmxon = true;
  11885. vmx->nested.smm.vmxon = false;
  11886. }
  11887. if (vmx->nested.smm.guest_mode) {
  11888. vcpu->arch.hflags &= ~HF_SMM_MASK;
  11889. ret = enter_vmx_non_root_mode(vcpu, NULL);
  11890. vcpu->arch.hflags |= HF_SMM_MASK;
  11891. if (ret)
  11892. return ret;
  11893. vmx->nested.smm.guest_mode = false;
  11894. }
  11895. return 0;
  11896. }
  11897. static int enable_smi_window(struct kvm_vcpu *vcpu)
  11898. {
  11899. return 0;
  11900. }
  11901. static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
  11902. struct kvm_nested_state __user *user_kvm_nested_state,
  11903. u32 user_data_size)
  11904. {
  11905. struct vcpu_vmx *vmx;
  11906. struct vmcs12 *vmcs12;
  11907. struct kvm_nested_state kvm_state = {
  11908. .flags = 0,
  11909. .format = 0,
  11910. .size = sizeof(kvm_state),
  11911. .vmx.vmxon_pa = -1ull,
  11912. .vmx.vmcs_pa = -1ull,
  11913. };
  11914. if (!vcpu)
  11915. return kvm_state.size + 2 * VMCS12_SIZE;
  11916. vmx = to_vmx(vcpu);
  11917. vmcs12 = get_vmcs12(vcpu);
  11918. if (nested_vmx_allowed(vcpu) &&
  11919. (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
  11920. kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
  11921. kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
  11922. if (vmx->nested.current_vmptr != -1ull) {
  11923. kvm_state.size += VMCS12_SIZE;
  11924. if (is_guest_mode(vcpu) &&
  11925. nested_cpu_has_shadow_vmcs(vmcs12) &&
  11926. vmcs12->vmcs_link_pointer != -1ull)
  11927. kvm_state.size += VMCS12_SIZE;
  11928. }
  11929. if (vmx->nested.smm.vmxon)
  11930. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
  11931. if (vmx->nested.smm.guest_mode)
  11932. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
  11933. if (is_guest_mode(vcpu)) {
  11934. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  11935. if (vmx->nested.nested_run_pending)
  11936. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  11937. }
  11938. }
  11939. if (user_data_size < kvm_state.size)
  11940. goto out;
  11941. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  11942. return -EFAULT;
  11943. if (vmx->nested.current_vmptr == -1ull)
  11944. goto out;
  11945. /*
  11946. * When running L2, the authoritative vmcs12 state is in the
  11947. * vmcs02. When running L1, the authoritative vmcs12 state is
  11948. * in the shadow vmcs linked to vmcs01, unless
  11949. * sync_shadow_vmcs is set, in which case, the authoritative
  11950. * vmcs12 state is in the vmcs12 already.
  11951. */
  11952. if (is_guest_mode(vcpu))
  11953. sync_vmcs12(vcpu, vmcs12);
  11954. else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
  11955. copy_shadow_to_vmcs12(vmx);
  11956. if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
  11957. return -EFAULT;
  11958. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  11959. vmcs12->vmcs_link_pointer != -1ull) {
  11960. if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
  11961. get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
  11962. return -EFAULT;
  11963. }
  11964. out:
  11965. return kvm_state.size;
  11966. }
  11967. static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  11968. struct kvm_nested_state __user *user_kvm_nested_state,
  11969. struct kvm_nested_state *kvm_state)
  11970. {
  11971. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11972. struct vmcs12 *vmcs12;
  11973. u32 exit_qual;
  11974. int ret;
  11975. if (kvm_state->format != 0)
  11976. return -EINVAL;
  11977. if (!nested_vmx_allowed(vcpu))
  11978. return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
  11979. if (kvm_state->vmx.vmxon_pa == -1ull) {
  11980. if (kvm_state->vmx.smm.flags)
  11981. return -EINVAL;
  11982. if (kvm_state->vmx.vmcs_pa != -1ull)
  11983. return -EINVAL;
  11984. vmx_leave_nested(vcpu);
  11985. return 0;
  11986. }
  11987. if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
  11988. return -EINVAL;
  11989. if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
  11990. return -EINVAL;
  11991. if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
  11992. !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
  11993. return -EINVAL;
  11994. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  11995. (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  11996. return -EINVAL;
  11997. if (kvm_state->vmx.smm.flags &
  11998. ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
  11999. return -EINVAL;
  12000. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12001. !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
  12002. return -EINVAL;
  12003. vmx_leave_nested(vcpu);
  12004. if (kvm_state->vmx.vmxon_pa == -1ull)
  12005. return 0;
  12006. vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
  12007. ret = enter_vmx_operation(vcpu);
  12008. if (ret)
  12009. return ret;
  12010. set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
  12011. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
  12012. vmx->nested.smm.vmxon = true;
  12013. vmx->nested.vmxon = false;
  12014. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
  12015. vmx->nested.smm.guest_mode = true;
  12016. }
  12017. vmcs12 = get_vmcs12(vcpu);
  12018. if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
  12019. return -EFAULT;
  12020. if (vmcs12->hdr.revision_id != VMCS12_REVISION)
  12021. return -EINVAL;
  12022. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12023. return 0;
  12024. vmx->nested.nested_run_pending =
  12025. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  12026. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12027. vmcs12->vmcs_link_pointer != -1ull) {
  12028. struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
  12029. if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
  12030. return -EINVAL;
  12031. if (copy_from_user(shadow_vmcs12,
  12032. user_kvm_nested_state->data + VMCS12_SIZE,
  12033. sizeof(*vmcs12)))
  12034. return -EFAULT;
  12035. if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  12036. !shadow_vmcs12->hdr.shadow_vmcs)
  12037. return -EINVAL;
  12038. }
  12039. if (check_vmentry_prereqs(vcpu, vmcs12) ||
  12040. check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  12041. return -EINVAL;
  12042. if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
  12043. vmx->nested.nested_run_pending = 1;
  12044. vmx->nested.dirty_vmcs12 = true;
  12045. ret = enter_vmx_non_root_mode(vcpu, NULL);
  12046. if (ret)
  12047. return -EINVAL;
  12048. return 0;
  12049. }
  12050. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  12051. .cpu_has_kvm_support = cpu_has_kvm_support,
  12052. .disabled_by_bios = vmx_disabled_by_bios,
  12053. .hardware_setup = hardware_setup,
  12054. .hardware_unsetup = hardware_unsetup,
  12055. .check_processor_compatibility = vmx_check_processor_compat,
  12056. .hardware_enable = hardware_enable,
  12057. .hardware_disable = hardware_disable,
  12058. .cpu_has_accelerated_tpr = report_flexpriority,
  12059. .has_emulated_msr = vmx_has_emulated_msr,
  12060. .vm_init = vmx_vm_init,
  12061. .vm_alloc = vmx_vm_alloc,
  12062. .vm_free = vmx_vm_free,
  12063. .vcpu_create = vmx_create_vcpu,
  12064. .vcpu_free = vmx_free_vcpu,
  12065. .vcpu_reset = vmx_vcpu_reset,
  12066. .prepare_guest_switch = vmx_prepare_switch_to_guest,
  12067. .vcpu_load = vmx_vcpu_load,
  12068. .vcpu_put = vmx_vcpu_put,
  12069. .update_bp_intercept = update_exception_bitmap,
  12070. .get_msr_feature = vmx_get_msr_feature,
  12071. .get_msr = vmx_get_msr,
  12072. .set_msr = vmx_set_msr,
  12073. .get_segment_base = vmx_get_segment_base,
  12074. .get_segment = vmx_get_segment,
  12075. .set_segment = vmx_set_segment,
  12076. .get_cpl = vmx_get_cpl,
  12077. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  12078. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  12079. .decache_cr3 = vmx_decache_cr3,
  12080. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  12081. .set_cr0 = vmx_set_cr0,
  12082. .set_cr3 = vmx_set_cr3,
  12083. .set_cr4 = vmx_set_cr4,
  12084. .set_efer = vmx_set_efer,
  12085. .get_idt = vmx_get_idt,
  12086. .set_idt = vmx_set_idt,
  12087. .get_gdt = vmx_get_gdt,
  12088. .set_gdt = vmx_set_gdt,
  12089. .get_dr6 = vmx_get_dr6,
  12090. .set_dr6 = vmx_set_dr6,
  12091. .set_dr7 = vmx_set_dr7,
  12092. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  12093. .cache_reg = vmx_cache_reg,
  12094. .get_rflags = vmx_get_rflags,
  12095. .set_rflags = vmx_set_rflags,
  12096. .tlb_flush = vmx_flush_tlb,
  12097. .tlb_flush_gva = vmx_flush_tlb_gva,
  12098. .run = vmx_vcpu_run,
  12099. .handle_exit = vmx_handle_exit,
  12100. .skip_emulated_instruction = skip_emulated_instruction,
  12101. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  12102. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  12103. .patch_hypercall = vmx_patch_hypercall,
  12104. .set_irq = vmx_inject_irq,
  12105. .set_nmi = vmx_inject_nmi,
  12106. .queue_exception = vmx_queue_exception,
  12107. .cancel_injection = vmx_cancel_injection,
  12108. .interrupt_allowed = vmx_interrupt_allowed,
  12109. .nmi_allowed = vmx_nmi_allowed,
  12110. .get_nmi_mask = vmx_get_nmi_mask,
  12111. .set_nmi_mask = vmx_set_nmi_mask,
  12112. .enable_nmi_window = enable_nmi_window,
  12113. .enable_irq_window = enable_irq_window,
  12114. .update_cr8_intercept = update_cr8_intercept,
  12115. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  12116. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  12117. .get_enable_apicv = vmx_get_enable_apicv,
  12118. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  12119. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  12120. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  12121. .hwapic_irr_update = vmx_hwapic_irr_update,
  12122. .hwapic_isr_update = vmx_hwapic_isr_update,
  12123. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  12124. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  12125. .set_tss_addr = vmx_set_tss_addr,
  12126. .set_identity_map_addr = vmx_set_identity_map_addr,
  12127. .get_tdp_level = get_ept_level,
  12128. .get_mt_mask = vmx_get_mt_mask,
  12129. .get_exit_info = vmx_get_exit_info,
  12130. .get_lpage_level = vmx_get_lpage_level,
  12131. .cpuid_update = vmx_cpuid_update,
  12132. .rdtscp_supported = vmx_rdtscp_supported,
  12133. .invpcid_supported = vmx_invpcid_supported,
  12134. .set_supported_cpuid = vmx_set_supported_cpuid,
  12135. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  12136. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  12137. .write_tsc_offset = vmx_write_tsc_offset,
  12138. .set_tdp_cr3 = vmx_set_cr3,
  12139. .check_intercept = vmx_check_intercept,
  12140. .handle_external_intr = vmx_handle_external_intr,
  12141. .mpx_supported = vmx_mpx_supported,
  12142. .xsaves_supported = vmx_xsaves_supported,
  12143. .umip_emulated = vmx_umip_emulated,
  12144. .check_nested_events = vmx_check_nested_events,
  12145. .sched_in = vmx_sched_in,
  12146. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  12147. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  12148. .flush_log_dirty = vmx_flush_log_dirty,
  12149. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  12150. .write_log_dirty = vmx_write_pml_buffer,
  12151. .pre_block = vmx_pre_block,
  12152. .post_block = vmx_post_block,
  12153. .pmu_ops = &intel_pmu_ops,
  12154. .update_pi_irte = vmx_update_pi_irte,
  12155. #ifdef CONFIG_X86_64
  12156. .set_hv_timer = vmx_set_hv_timer,
  12157. .cancel_hv_timer = vmx_cancel_hv_timer,
  12158. #endif
  12159. .setup_mce = vmx_setup_mce,
  12160. .get_nested_state = vmx_get_nested_state,
  12161. .set_nested_state = vmx_set_nested_state,
  12162. .get_vmcs12_pages = nested_get_vmcs12_pages,
  12163. .smi_allowed = vmx_smi_allowed,
  12164. .pre_enter_smm = vmx_pre_enter_smm,
  12165. .pre_leave_smm = vmx_pre_leave_smm,
  12166. .enable_smi_window = enable_smi_window,
  12167. };
  12168. static void vmx_cleanup_l1d_flush(void)
  12169. {
  12170. if (vmx_l1d_flush_pages) {
  12171. free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
  12172. vmx_l1d_flush_pages = NULL;
  12173. }
  12174. /* Restore state so sysfs ignores VMX */
  12175. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  12176. }
  12177. static void vmx_exit(void)
  12178. {
  12179. #ifdef CONFIG_KEXEC_CORE
  12180. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  12181. synchronize_rcu();
  12182. #endif
  12183. kvm_exit();
  12184. #if IS_ENABLED(CONFIG_HYPERV)
  12185. if (static_branch_unlikely(&enable_evmcs)) {
  12186. int cpu;
  12187. struct hv_vp_assist_page *vp_ap;
  12188. /*
  12189. * Reset everything to support using non-enlightened VMCS
  12190. * access later (e.g. when we reload the module with
  12191. * enlightened_vmcs=0)
  12192. */
  12193. for_each_online_cpu(cpu) {
  12194. vp_ap = hv_get_vp_assist_page(cpu);
  12195. if (!vp_ap)
  12196. continue;
  12197. vp_ap->current_nested_vmcs = 0;
  12198. vp_ap->enlighten_vmentry = 0;
  12199. }
  12200. static_branch_disable(&enable_evmcs);
  12201. }
  12202. #endif
  12203. vmx_cleanup_l1d_flush();
  12204. }
  12205. module_exit(vmx_exit);
  12206. static int __init vmx_init(void)
  12207. {
  12208. int r;
  12209. #if IS_ENABLED(CONFIG_HYPERV)
  12210. /*
  12211. * Enlightened VMCS usage should be recommended and the host needs
  12212. * to support eVMCS v1 or above. We can also disable eVMCS support
  12213. * with module parameter.
  12214. */
  12215. if (enlightened_vmcs &&
  12216. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  12217. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  12218. KVM_EVMCS_VERSION) {
  12219. int cpu;
  12220. /* Check that we have assist pages on all online CPUs */
  12221. for_each_online_cpu(cpu) {
  12222. if (!hv_get_vp_assist_page(cpu)) {
  12223. enlightened_vmcs = false;
  12224. break;
  12225. }
  12226. }
  12227. if (enlightened_vmcs) {
  12228. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  12229. static_branch_enable(&enable_evmcs);
  12230. }
  12231. } else {
  12232. enlightened_vmcs = false;
  12233. }
  12234. #endif
  12235. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  12236. __alignof__(struct vcpu_vmx), THIS_MODULE);
  12237. if (r)
  12238. return r;
  12239. /*
  12240. * Must be called after kvm_init() so enable_ept is properly set
  12241. * up. Hand the parameter mitigation value in which was stored in
  12242. * the pre module init parser. If no parameter was given, it will
  12243. * contain 'auto' which will be turned into the default 'cond'
  12244. * mitigation mode.
  12245. */
  12246. if (boot_cpu_has(X86_BUG_L1TF)) {
  12247. r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
  12248. if (r) {
  12249. vmx_exit();
  12250. return r;
  12251. }
  12252. }
  12253. #ifdef CONFIG_KEXEC_CORE
  12254. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  12255. crash_vmclear_local_loaded_vmcss);
  12256. #endif
  12257. vmx_check_vmcs12_offsets();
  12258. return 0;
  12259. }
  12260. module_init(vmx_init);