vmx.c 412 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640
  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/sched/smt.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/trace_events.h>
  32. #include <linux/slab.h>
  33. #include <linux/tboot.h>
  34. #include <linux/hrtimer.h>
  35. #include <linux/frame.h>
  36. #include <linux/nospec.h>
  37. #include "kvm_cache_regs.h"
  38. #include "x86.h"
  39. #include <asm/asm.h>
  40. #include <asm/cpu.h>
  41. #include <asm/io.h>
  42. #include <asm/desc.h>
  43. #include <asm/vmx.h>
  44. #include <asm/virtext.h>
  45. #include <asm/mce.h>
  46. #include <asm/fpu/internal.h>
  47. #include <asm/perf_event.h>
  48. #include <asm/debugreg.h>
  49. #include <asm/kexec.h>
  50. #include <asm/apic.h>
  51. #include <asm/irq_remapping.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/spec-ctrl.h>
  54. #include <asm/mshyperv.h>
  55. #include "trace.h"
  56. #include "pmu.h"
  57. #include "vmx_evmcs.h"
  58. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  59. #define __ex_clear(x, reg) \
  60. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  61. MODULE_AUTHOR("Qumranet");
  62. MODULE_LICENSE("GPL");
  63. static const struct x86_cpu_id vmx_cpu_id[] = {
  64. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  65. {}
  66. };
  67. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  68. static bool __read_mostly enable_vpid = 1;
  69. module_param_named(vpid, enable_vpid, bool, 0444);
  70. static bool __read_mostly enable_vnmi = 1;
  71. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  72. static bool __read_mostly flexpriority_enabled = 1;
  73. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  74. static bool __read_mostly enable_ept = 1;
  75. module_param_named(ept, enable_ept, bool, S_IRUGO);
  76. static bool __read_mostly enable_unrestricted_guest = 1;
  77. module_param_named(unrestricted_guest,
  78. enable_unrestricted_guest, bool, S_IRUGO);
  79. static bool __read_mostly enable_ept_ad_bits = 1;
  80. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  81. static bool __read_mostly emulate_invalid_guest_state = true;
  82. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  83. static bool __read_mostly fasteoi = 1;
  84. module_param(fasteoi, bool, S_IRUGO);
  85. static bool __read_mostly enable_apicv = 1;
  86. module_param(enable_apicv, bool, S_IRUGO);
  87. static bool __read_mostly enable_shadow_vmcs = 1;
  88. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  89. /*
  90. * If nested=1, nested virtualization is supported, i.e., guests may use
  91. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  92. * use VMX instructions.
  93. */
  94. static bool __read_mostly nested = 0;
  95. module_param(nested, bool, S_IRUGO);
  96. static u64 __read_mostly host_xss;
  97. static bool __read_mostly enable_pml = 1;
  98. module_param_named(pml, enable_pml, bool, S_IRUGO);
  99. #define MSR_TYPE_R 1
  100. #define MSR_TYPE_W 2
  101. #define MSR_TYPE_RW 3
  102. #define MSR_BITMAP_MODE_X2APIC 1
  103. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  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. module_param(ple_gap, uint, 0444);
  146. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  147. module_param(ple_window, uint, 0444);
  148. /* Default doubles per-vcpu window every exit. */
  149. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  150. module_param(ple_window_grow, uint, 0444);
  151. /* Default resets per-vcpu window every exit to ple_window. */
  152. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  153. module_param(ple_window_shrink, uint, 0444);
  154. /* Default is to compute the maximum so we can never overflow. */
  155. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  156. module_param(ple_window_max, uint, 0444);
  157. extern const ulong vmx_return;
  158. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
  159. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
  160. static DEFINE_MUTEX(vmx_l1d_flush_mutex);
  161. /* Storage for pre module init parameter parsing */
  162. static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
  163. static const struct {
  164. const char *option;
  165. bool for_parse;
  166. } vmentry_l1d_param[] = {
  167. [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
  168. [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
  169. [VMENTER_L1D_FLUSH_COND] = {"cond", true},
  170. [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
  171. [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
  172. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
  173. };
  174. #define L1D_CACHE_ORDER 4
  175. static void *vmx_l1d_flush_pages;
  176. static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
  177. {
  178. struct page *page;
  179. unsigned int i;
  180. if (!enable_ept) {
  181. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
  182. return 0;
  183. }
  184. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
  185. u64 msr;
  186. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
  187. if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
  188. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
  189. return 0;
  190. }
  191. }
  192. /* If set to auto use the default l1tf mitigation method */
  193. if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
  194. switch (l1tf_mitigation) {
  195. case L1TF_MITIGATION_OFF:
  196. l1tf = VMENTER_L1D_FLUSH_NEVER;
  197. break;
  198. case L1TF_MITIGATION_FLUSH_NOWARN:
  199. case L1TF_MITIGATION_FLUSH:
  200. case L1TF_MITIGATION_FLUSH_NOSMT:
  201. l1tf = VMENTER_L1D_FLUSH_COND;
  202. break;
  203. case L1TF_MITIGATION_FULL:
  204. case L1TF_MITIGATION_FULL_FORCE:
  205. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  206. break;
  207. }
  208. } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
  209. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  210. }
  211. if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
  212. !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  213. page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
  214. if (!page)
  215. return -ENOMEM;
  216. vmx_l1d_flush_pages = page_address(page);
  217. /*
  218. * Initialize each page with a different pattern in
  219. * order to protect against KSM in the nested
  220. * virtualization case.
  221. */
  222. for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
  223. memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
  224. PAGE_SIZE);
  225. }
  226. }
  227. l1tf_vmx_mitigation = l1tf;
  228. if (l1tf != VMENTER_L1D_FLUSH_NEVER)
  229. static_branch_enable(&vmx_l1d_should_flush);
  230. else
  231. static_branch_disable(&vmx_l1d_should_flush);
  232. if (l1tf == VMENTER_L1D_FLUSH_COND)
  233. static_branch_enable(&vmx_l1d_flush_cond);
  234. else
  235. static_branch_disable(&vmx_l1d_flush_cond);
  236. return 0;
  237. }
  238. static int vmentry_l1d_flush_parse(const char *s)
  239. {
  240. unsigned int i;
  241. if (s) {
  242. for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
  243. if (vmentry_l1d_param[i].for_parse &&
  244. sysfs_streq(s, vmentry_l1d_param[i].option))
  245. return i;
  246. }
  247. }
  248. return -EINVAL;
  249. }
  250. static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
  251. {
  252. int l1tf, ret;
  253. l1tf = vmentry_l1d_flush_parse(s);
  254. if (l1tf < 0)
  255. return l1tf;
  256. if (!boot_cpu_has(X86_BUG_L1TF))
  257. return 0;
  258. /*
  259. * Has vmx_init() run already? If not then this is the pre init
  260. * parameter parsing. In that case just store the value and let
  261. * vmx_init() do the proper setup after enable_ept has been
  262. * established.
  263. */
  264. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
  265. vmentry_l1d_flush_param = l1tf;
  266. return 0;
  267. }
  268. mutex_lock(&vmx_l1d_flush_mutex);
  269. ret = vmx_setup_l1d_flush(l1tf);
  270. mutex_unlock(&vmx_l1d_flush_mutex);
  271. return ret;
  272. }
  273. static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
  274. {
  275. if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
  276. return sprintf(s, "???\n");
  277. return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
  278. }
  279. static const struct kernel_param_ops vmentry_l1d_flush_ops = {
  280. .set = vmentry_l1d_flush_set,
  281. .get = vmentry_l1d_flush_get,
  282. };
  283. module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
  284. enum ept_pointers_status {
  285. EPT_POINTERS_CHECK = 0,
  286. EPT_POINTERS_MATCH = 1,
  287. EPT_POINTERS_MISMATCH = 2
  288. };
  289. struct kvm_vmx {
  290. struct kvm kvm;
  291. unsigned int tss_addr;
  292. bool ept_identity_pagetable_done;
  293. gpa_t ept_identity_map_addr;
  294. enum ept_pointers_status ept_pointers_match;
  295. spinlock_t ept_pointer_lock;
  296. };
  297. #define NR_AUTOLOAD_MSRS 8
  298. struct vmcs_hdr {
  299. u32 revision_id:31;
  300. u32 shadow_vmcs:1;
  301. };
  302. struct vmcs {
  303. struct vmcs_hdr hdr;
  304. u32 abort;
  305. char data[0];
  306. };
  307. /*
  308. * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
  309. * and whose values change infrequently, but are not constant. I.e. this is
  310. * used as a write-through cache of the corresponding VMCS fields.
  311. */
  312. struct vmcs_host_state {
  313. unsigned long cr3; /* May not match real cr3 */
  314. unsigned long cr4; /* May not match real cr4 */
  315. unsigned long gs_base;
  316. unsigned long fs_base;
  317. u16 fs_sel, gs_sel, ldt_sel;
  318. #ifdef CONFIG_X86_64
  319. u16 ds_sel, es_sel;
  320. #endif
  321. };
  322. /*
  323. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  324. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  325. * loaded on this CPU (so we can clear them if the CPU goes down).
  326. */
  327. struct loaded_vmcs {
  328. struct vmcs *vmcs;
  329. struct vmcs *shadow_vmcs;
  330. int cpu;
  331. bool launched;
  332. bool nmi_known_unmasked;
  333. bool hv_timer_armed;
  334. /* Support for vnmi-less CPUs */
  335. int soft_vnmi_blocked;
  336. ktime_t entry_time;
  337. s64 vnmi_blocked_time;
  338. unsigned long *msr_bitmap;
  339. struct list_head loaded_vmcss_on_cpu_link;
  340. struct vmcs_host_state host_state;
  341. };
  342. struct shared_msr_entry {
  343. unsigned index;
  344. u64 data;
  345. u64 mask;
  346. };
  347. /*
  348. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  349. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  350. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  351. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  352. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  353. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  354. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  355. * underlying hardware which will be used to run L2.
  356. * This structure is packed to ensure that its layout is identical across
  357. * machines (necessary for live migration).
  358. *
  359. * IMPORTANT: Changing the layout of existing fields in this structure
  360. * will break save/restore compatibility with older kvm releases. When
  361. * adding new fields, either use space in the reserved padding* arrays
  362. * or add the new fields to the end of the structure.
  363. */
  364. typedef u64 natural_width;
  365. struct __packed vmcs12 {
  366. /* According to the Intel spec, a VMCS region must start with the
  367. * following two fields. Then follow implementation-specific data.
  368. */
  369. struct vmcs_hdr hdr;
  370. u32 abort;
  371. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  372. u32 padding[7]; /* room for future expansion */
  373. u64 io_bitmap_a;
  374. u64 io_bitmap_b;
  375. u64 msr_bitmap;
  376. u64 vm_exit_msr_store_addr;
  377. u64 vm_exit_msr_load_addr;
  378. u64 vm_entry_msr_load_addr;
  379. u64 tsc_offset;
  380. u64 virtual_apic_page_addr;
  381. u64 apic_access_addr;
  382. u64 posted_intr_desc_addr;
  383. u64 ept_pointer;
  384. u64 eoi_exit_bitmap0;
  385. u64 eoi_exit_bitmap1;
  386. u64 eoi_exit_bitmap2;
  387. u64 eoi_exit_bitmap3;
  388. u64 xss_exit_bitmap;
  389. u64 guest_physical_address;
  390. u64 vmcs_link_pointer;
  391. u64 guest_ia32_debugctl;
  392. u64 guest_ia32_pat;
  393. u64 guest_ia32_efer;
  394. u64 guest_ia32_perf_global_ctrl;
  395. u64 guest_pdptr0;
  396. u64 guest_pdptr1;
  397. u64 guest_pdptr2;
  398. u64 guest_pdptr3;
  399. u64 guest_bndcfgs;
  400. u64 host_ia32_pat;
  401. u64 host_ia32_efer;
  402. u64 host_ia32_perf_global_ctrl;
  403. u64 vmread_bitmap;
  404. u64 vmwrite_bitmap;
  405. u64 vm_function_control;
  406. u64 eptp_list_address;
  407. u64 pml_address;
  408. u64 padding64[3]; /* room for future expansion */
  409. /*
  410. * To allow migration of L1 (complete with its L2 guests) between
  411. * machines of different natural widths (32 or 64 bit), we cannot have
  412. * unsigned long fields with no explict size. We use u64 (aliased
  413. * natural_width) instead. Luckily, x86 is little-endian.
  414. */
  415. natural_width cr0_guest_host_mask;
  416. natural_width cr4_guest_host_mask;
  417. natural_width cr0_read_shadow;
  418. natural_width cr4_read_shadow;
  419. natural_width cr3_target_value0;
  420. natural_width cr3_target_value1;
  421. natural_width cr3_target_value2;
  422. natural_width cr3_target_value3;
  423. natural_width exit_qualification;
  424. natural_width guest_linear_address;
  425. natural_width guest_cr0;
  426. natural_width guest_cr3;
  427. natural_width guest_cr4;
  428. natural_width guest_es_base;
  429. natural_width guest_cs_base;
  430. natural_width guest_ss_base;
  431. natural_width guest_ds_base;
  432. natural_width guest_fs_base;
  433. natural_width guest_gs_base;
  434. natural_width guest_ldtr_base;
  435. natural_width guest_tr_base;
  436. natural_width guest_gdtr_base;
  437. natural_width guest_idtr_base;
  438. natural_width guest_dr7;
  439. natural_width guest_rsp;
  440. natural_width guest_rip;
  441. natural_width guest_rflags;
  442. natural_width guest_pending_dbg_exceptions;
  443. natural_width guest_sysenter_esp;
  444. natural_width guest_sysenter_eip;
  445. natural_width host_cr0;
  446. natural_width host_cr3;
  447. natural_width host_cr4;
  448. natural_width host_fs_base;
  449. natural_width host_gs_base;
  450. natural_width host_tr_base;
  451. natural_width host_gdtr_base;
  452. natural_width host_idtr_base;
  453. natural_width host_ia32_sysenter_esp;
  454. natural_width host_ia32_sysenter_eip;
  455. natural_width host_rsp;
  456. natural_width host_rip;
  457. natural_width paddingl[8]; /* room for future expansion */
  458. u32 pin_based_vm_exec_control;
  459. u32 cpu_based_vm_exec_control;
  460. u32 exception_bitmap;
  461. u32 page_fault_error_code_mask;
  462. u32 page_fault_error_code_match;
  463. u32 cr3_target_count;
  464. u32 vm_exit_controls;
  465. u32 vm_exit_msr_store_count;
  466. u32 vm_exit_msr_load_count;
  467. u32 vm_entry_controls;
  468. u32 vm_entry_msr_load_count;
  469. u32 vm_entry_intr_info_field;
  470. u32 vm_entry_exception_error_code;
  471. u32 vm_entry_instruction_len;
  472. u32 tpr_threshold;
  473. u32 secondary_vm_exec_control;
  474. u32 vm_instruction_error;
  475. u32 vm_exit_reason;
  476. u32 vm_exit_intr_info;
  477. u32 vm_exit_intr_error_code;
  478. u32 idt_vectoring_info_field;
  479. u32 idt_vectoring_error_code;
  480. u32 vm_exit_instruction_len;
  481. u32 vmx_instruction_info;
  482. u32 guest_es_limit;
  483. u32 guest_cs_limit;
  484. u32 guest_ss_limit;
  485. u32 guest_ds_limit;
  486. u32 guest_fs_limit;
  487. u32 guest_gs_limit;
  488. u32 guest_ldtr_limit;
  489. u32 guest_tr_limit;
  490. u32 guest_gdtr_limit;
  491. u32 guest_idtr_limit;
  492. u32 guest_es_ar_bytes;
  493. u32 guest_cs_ar_bytes;
  494. u32 guest_ss_ar_bytes;
  495. u32 guest_ds_ar_bytes;
  496. u32 guest_fs_ar_bytes;
  497. u32 guest_gs_ar_bytes;
  498. u32 guest_ldtr_ar_bytes;
  499. u32 guest_tr_ar_bytes;
  500. u32 guest_interruptibility_info;
  501. u32 guest_activity_state;
  502. u32 guest_sysenter_cs;
  503. u32 host_ia32_sysenter_cs;
  504. u32 vmx_preemption_timer_value;
  505. u32 padding32[7]; /* room for future expansion */
  506. u16 virtual_processor_id;
  507. u16 posted_intr_nv;
  508. u16 guest_es_selector;
  509. u16 guest_cs_selector;
  510. u16 guest_ss_selector;
  511. u16 guest_ds_selector;
  512. u16 guest_fs_selector;
  513. u16 guest_gs_selector;
  514. u16 guest_ldtr_selector;
  515. u16 guest_tr_selector;
  516. u16 guest_intr_status;
  517. u16 host_es_selector;
  518. u16 host_cs_selector;
  519. u16 host_ss_selector;
  520. u16 host_ds_selector;
  521. u16 host_fs_selector;
  522. u16 host_gs_selector;
  523. u16 host_tr_selector;
  524. u16 guest_pml_index;
  525. };
  526. /*
  527. * For save/restore compatibility, the vmcs12 field offsets must not change.
  528. */
  529. #define CHECK_OFFSET(field, loc) \
  530. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  531. "Offset of " #field " in struct vmcs12 has changed.")
  532. static inline void vmx_check_vmcs12_offsets(void) {
  533. CHECK_OFFSET(hdr, 0);
  534. CHECK_OFFSET(abort, 4);
  535. CHECK_OFFSET(launch_state, 8);
  536. CHECK_OFFSET(io_bitmap_a, 40);
  537. CHECK_OFFSET(io_bitmap_b, 48);
  538. CHECK_OFFSET(msr_bitmap, 56);
  539. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  540. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  541. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  542. CHECK_OFFSET(tsc_offset, 88);
  543. CHECK_OFFSET(virtual_apic_page_addr, 96);
  544. CHECK_OFFSET(apic_access_addr, 104);
  545. CHECK_OFFSET(posted_intr_desc_addr, 112);
  546. CHECK_OFFSET(ept_pointer, 120);
  547. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  548. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  549. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  550. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  551. CHECK_OFFSET(xss_exit_bitmap, 160);
  552. CHECK_OFFSET(guest_physical_address, 168);
  553. CHECK_OFFSET(vmcs_link_pointer, 176);
  554. CHECK_OFFSET(guest_ia32_debugctl, 184);
  555. CHECK_OFFSET(guest_ia32_pat, 192);
  556. CHECK_OFFSET(guest_ia32_efer, 200);
  557. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  558. CHECK_OFFSET(guest_pdptr0, 216);
  559. CHECK_OFFSET(guest_pdptr1, 224);
  560. CHECK_OFFSET(guest_pdptr2, 232);
  561. CHECK_OFFSET(guest_pdptr3, 240);
  562. CHECK_OFFSET(guest_bndcfgs, 248);
  563. CHECK_OFFSET(host_ia32_pat, 256);
  564. CHECK_OFFSET(host_ia32_efer, 264);
  565. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  566. CHECK_OFFSET(vmread_bitmap, 280);
  567. CHECK_OFFSET(vmwrite_bitmap, 288);
  568. CHECK_OFFSET(vm_function_control, 296);
  569. CHECK_OFFSET(eptp_list_address, 304);
  570. CHECK_OFFSET(pml_address, 312);
  571. CHECK_OFFSET(cr0_guest_host_mask, 344);
  572. CHECK_OFFSET(cr4_guest_host_mask, 352);
  573. CHECK_OFFSET(cr0_read_shadow, 360);
  574. CHECK_OFFSET(cr4_read_shadow, 368);
  575. CHECK_OFFSET(cr3_target_value0, 376);
  576. CHECK_OFFSET(cr3_target_value1, 384);
  577. CHECK_OFFSET(cr3_target_value2, 392);
  578. CHECK_OFFSET(cr3_target_value3, 400);
  579. CHECK_OFFSET(exit_qualification, 408);
  580. CHECK_OFFSET(guest_linear_address, 416);
  581. CHECK_OFFSET(guest_cr0, 424);
  582. CHECK_OFFSET(guest_cr3, 432);
  583. CHECK_OFFSET(guest_cr4, 440);
  584. CHECK_OFFSET(guest_es_base, 448);
  585. CHECK_OFFSET(guest_cs_base, 456);
  586. CHECK_OFFSET(guest_ss_base, 464);
  587. CHECK_OFFSET(guest_ds_base, 472);
  588. CHECK_OFFSET(guest_fs_base, 480);
  589. CHECK_OFFSET(guest_gs_base, 488);
  590. CHECK_OFFSET(guest_ldtr_base, 496);
  591. CHECK_OFFSET(guest_tr_base, 504);
  592. CHECK_OFFSET(guest_gdtr_base, 512);
  593. CHECK_OFFSET(guest_idtr_base, 520);
  594. CHECK_OFFSET(guest_dr7, 528);
  595. CHECK_OFFSET(guest_rsp, 536);
  596. CHECK_OFFSET(guest_rip, 544);
  597. CHECK_OFFSET(guest_rflags, 552);
  598. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  599. CHECK_OFFSET(guest_sysenter_esp, 568);
  600. CHECK_OFFSET(guest_sysenter_eip, 576);
  601. CHECK_OFFSET(host_cr0, 584);
  602. CHECK_OFFSET(host_cr3, 592);
  603. CHECK_OFFSET(host_cr4, 600);
  604. CHECK_OFFSET(host_fs_base, 608);
  605. CHECK_OFFSET(host_gs_base, 616);
  606. CHECK_OFFSET(host_tr_base, 624);
  607. CHECK_OFFSET(host_gdtr_base, 632);
  608. CHECK_OFFSET(host_idtr_base, 640);
  609. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  610. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  611. CHECK_OFFSET(host_rsp, 664);
  612. CHECK_OFFSET(host_rip, 672);
  613. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  614. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  615. CHECK_OFFSET(exception_bitmap, 752);
  616. CHECK_OFFSET(page_fault_error_code_mask, 756);
  617. CHECK_OFFSET(page_fault_error_code_match, 760);
  618. CHECK_OFFSET(cr3_target_count, 764);
  619. CHECK_OFFSET(vm_exit_controls, 768);
  620. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  621. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  622. CHECK_OFFSET(vm_entry_controls, 780);
  623. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  624. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  625. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  626. CHECK_OFFSET(vm_entry_instruction_len, 796);
  627. CHECK_OFFSET(tpr_threshold, 800);
  628. CHECK_OFFSET(secondary_vm_exec_control, 804);
  629. CHECK_OFFSET(vm_instruction_error, 808);
  630. CHECK_OFFSET(vm_exit_reason, 812);
  631. CHECK_OFFSET(vm_exit_intr_info, 816);
  632. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  633. CHECK_OFFSET(idt_vectoring_info_field, 824);
  634. CHECK_OFFSET(idt_vectoring_error_code, 828);
  635. CHECK_OFFSET(vm_exit_instruction_len, 832);
  636. CHECK_OFFSET(vmx_instruction_info, 836);
  637. CHECK_OFFSET(guest_es_limit, 840);
  638. CHECK_OFFSET(guest_cs_limit, 844);
  639. CHECK_OFFSET(guest_ss_limit, 848);
  640. CHECK_OFFSET(guest_ds_limit, 852);
  641. CHECK_OFFSET(guest_fs_limit, 856);
  642. CHECK_OFFSET(guest_gs_limit, 860);
  643. CHECK_OFFSET(guest_ldtr_limit, 864);
  644. CHECK_OFFSET(guest_tr_limit, 868);
  645. CHECK_OFFSET(guest_gdtr_limit, 872);
  646. CHECK_OFFSET(guest_idtr_limit, 876);
  647. CHECK_OFFSET(guest_es_ar_bytes, 880);
  648. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  649. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  650. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  651. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  652. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  653. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  654. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  655. CHECK_OFFSET(guest_interruptibility_info, 912);
  656. CHECK_OFFSET(guest_activity_state, 916);
  657. CHECK_OFFSET(guest_sysenter_cs, 920);
  658. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  659. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  660. CHECK_OFFSET(virtual_processor_id, 960);
  661. CHECK_OFFSET(posted_intr_nv, 962);
  662. CHECK_OFFSET(guest_es_selector, 964);
  663. CHECK_OFFSET(guest_cs_selector, 966);
  664. CHECK_OFFSET(guest_ss_selector, 968);
  665. CHECK_OFFSET(guest_ds_selector, 970);
  666. CHECK_OFFSET(guest_fs_selector, 972);
  667. CHECK_OFFSET(guest_gs_selector, 974);
  668. CHECK_OFFSET(guest_ldtr_selector, 976);
  669. CHECK_OFFSET(guest_tr_selector, 978);
  670. CHECK_OFFSET(guest_intr_status, 980);
  671. CHECK_OFFSET(host_es_selector, 982);
  672. CHECK_OFFSET(host_cs_selector, 984);
  673. CHECK_OFFSET(host_ss_selector, 986);
  674. CHECK_OFFSET(host_ds_selector, 988);
  675. CHECK_OFFSET(host_fs_selector, 990);
  676. CHECK_OFFSET(host_gs_selector, 992);
  677. CHECK_OFFSET(host_tr_selector, 994);
  678. CHECK_OFFSET(guest_pml_index, 996);
  679. }
  680. /*
  681. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  682. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  683. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  684. *
  685. * IMPORTANT: Changing this value will break save/restore compatibility with
  686. * older kvm releases.
  687. */
  688. #define VMCS12_REVISION 0x11e57ed0
  689. /*
  690. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  691. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  692. * current implementation, 4K are reserved to avoid future complications.
  693. */
  694. #define VMCS12_SIZE 0x1000
  695. /*
  696. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  697. * supported VMCS12 field encoding.
  698. */
  699. #define VMCS12_MAX_FIELD_INDEX 0x17
  700. struct nested_vmx_msrs {
  701. /*
  702. * We only store the "true" versions of the VMX capability MSRs. We
  703. * generate the "non-true" versions by setting the must-be-1 bits
  704. * according to the SDM.
  705. */
  706. u32 procbased_ctls_low;
  707. u32 procbased_ctls_high;
  708. u32 secondary_ctls_low;
  709. u32 secondary_ctls_high;
  710. u32 pinbased_ctls_low;
  711. u32 pinbased_ctls_high;
  712. u32 exit_ctls_low;
  713. u32 exit_ctls_high;
  714. u32 entry_ctls_low;
  715. u32 entry_ctls_high;
  716. u32 misc_low;
  717. u32 misc_high;
  718. u32 ept_caps;
  719. u32 vpid_caps;
  720. u64 basic;
  721. u64 cr0_fixed0;
  722. u64 cr0_fixed1;
  723. u64 cr4_fixed0;
  724. u64 cr4_fixed1;
  725. u64 vmcs_enum;
  726. u64 vmfunc_controls;
  727. };
  728. /*
  729. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  730. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  731. */
  732. struct nested_vmx {
  733. /* Has the level1 guest done vmxon? */
  734. bool vmxon;
  735. gpa_t vmxon_ptr;
  736. bool pml_full;
  737. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  738. gpa_t current_vmptr;
  739. /*
  740. * Cache of the guest's VMCS, existing outside of guest memory.
  741. * Loaded from guest memory during VMPTRLD. Flushed to guest
  742. * memory during VMCLEAR and VMPTRLD.
  743. */
  744. struct vmcs12 *cached_vmcs12;
  745. /*
  746. * Cache of the guest's shadow VMCS, existing outside of guest
  747. * memory. Loaded from guest memory during VM entry. Flushed
  748. * to guest memory during VM exit.
  749. */
  750. struct vmcs12 *cached_shadow_vmcs12;
  751. /*
  752. * Indicates if the shadow vmcs must be updated with the
  753. * data hold by vmcs12
  754. */
  755. bool sync_shadow_vmcs;
  756. bool dirty_vmcs12;
  757. bool change_vmcs01_virtual_apic_mode;
  758. /* L2 must run next, and mustn't decide to exit to L1. */
  759. bool nested_run_pending;
  760. struct loaded_vmcs vmcs02;
  761. /*
  762. * Guest pages referred to in the vmcs02 with host-physical
  763. * pointers, so we must keep them pinned while L2 runs.
  764. */
  765. struct page *apic_access_page;
  766. struct page *virtual_apic_page;
  767. struct page *pi_desc_page;
  768. struct pi_desc *pi_desc;
  769. bool pi_pending;
  770. u16 posted_intr_nv;
  771. struct hrtimer preemption_timer;
  772. bool preemption_timer_expired;
  773. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  774. u64 vmcs01_debugctl;
  775. u64 vmcs01_guest_bndcfgs;
  776. u16 vpid02;
  777. u16 last_vpid;
  778. struct nested_vmx_msrs msrs;
  779. /* SMM related state */
  780. struct {
  781. /* in VMX operation on SMM entry? */
  782. bool vmxon;
  783. /* in guest mode on SMM entry? */
  784. bool guest_mode;
  785. } smm;
  786. };
  787. #define POSTED_INTR_ON 0
  788. #define POSTED_INTR_SN 1
  789. /* Posted-Interrupt Descriptor */
  790. struct pi_desc {
  791. u32 pir[8]; /* Posted interrupt requested */
  792. union {
  793. struct {
  794. /* bit 256 - Outstanding Notification */
  795. u16 on : 1,
  796. /* bit 257 - Suppress Notification */
  797. sn : 1,
  798. /* bit 271:258 - Reserved */
  799. rsvd_1 : 14;
  800. /* bit 279:272 - Notification Vector */
  801. u8 nv;
  802. /* bit 287:280 - Reserved */
  803. u8 rsvd_2;
  804. /* bit 319:288 - Notification Destination */
  805. u32 ndst;
  806. };
  807. u64 control;
  808. };
  809. u32 rsvd[6];
  810. } __aligned(64);
  811. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  812. {
  813. return test_and_set_bit(POSTED_INTR_ON,
  814. (unsigned long *)&pi_desc->control);
  815. }
  816. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  817. {
  818. return test_and_clear_bit(POSTED_INTR_ON,
  819. (unsigned long *)&pi_desc->control);
  820. }
  821. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  822. {
  823. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  824. }
  825. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  826. {
  827. return clear_bit(POSTED_INTR_SN,
  828. (unsigned long *)&pi_desc->control);
  829. }
  830. static inline void pi_set_sn(struct pi_desc *pi_desc)
  831. {
  832. return set_bit(POSTED_INTR_SN,
  833. (unsigned long *)&pi_desc->control);
  834. }
  835. static inline void pi_clear_on(struct pi_desc *pi_desc)
  836. {
  837. clear_bit(POSTED_INTR_ON,
  838. (unsigned long *)&pi_desc->control);
  839. }
  840. static inline int pi_test_on(struct pi_desc *pi_desc)
  841. {
  842. return test_bit(POSTED_INTR_ON,
  843. (unsigned long *)&pi_desc->control);
  844. }
  845. static inline int pi_test_sn(struct pi_desc *pi_desc)
  846. {
  847. return test_bit(POSTED_INTR_SN,
  848. (unsigned long *)&pi_desc->control);
  849. }
  850. struct vmx_msrs {
  851. unsigned int nr;
  852. struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
  853. };
  854. struct vcpu_vmx {
  855. struct kvm_vcpu vcpu;
  856. unsigned long host_rsp;
  857. u8 fail;
  858. u8 msr_bitmap_mode;
  859. u32 exit_intr_info;
  860. u32 idt_vectoring_info;
  861. ulong rflags;
  862. struct shared_msr_entry *guest_msrs;
  863. int nmsrs;
  864. int save_nmsrs;
  865. bool guest_msrs_dirty;
  866. unsigned long host_idt_base;
  867. #ifdef CONFIG_X86_64
  868. u64 msr_host_kernel_gs_base;
  869. u64 msr_guest_kernel_gs_base;
  870. #endif
  871. u64 spec_ctrl;
  872. u32 vm_entry_controls_shadow;
  873. u32 vm_exit_controls_shadow;
  874. u32 secondary_exec_control;
  875. /*
  876. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  877. * non-nested (L1) guest, it always points to vmcs01. For a nested
  878. * guest (L2), it points to a different VMCS. loaded_cpu_state points
  879. * to the VMCS whose state is loaded into the CPU registers that only
  880. * need to be switched when transitioning to/from the kernel; a NULL
  881. * value indicates that host state is loaded.
  882. */
  883. struct loaded_vmcs vmcs01;
  884. struct loaded_vmcs *loaded_vmcs;
  885. struct loaded_vmcs *loaded_cpu_state;
  886. bool __launched; /* temporary, used in vmx_vcpu_run */
  887. struct msr_autoload {
  888. struct vmx_msrs guest;
  889. struct vmx_msrs host;
  890. } msr_autoload;
  891. struct {
  892. int vm86_active;
  893. ulong save_rflags;
  894. struct kvm_segment segs[8];
  895. } rmode;
  896. struct {
  897. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  898. struct kvm_save_segment {
  899. u16 selector;
  900. unsigned long base;
  901. u32 limit;
  902. u32 ar;
  903. } seg[8];
  904. } segment_cache;
  905. int vpid;
  906. bool emulation_required;
  907. u32 exit_reason;
  908. /* Posted interrupt descriptor */
  909. struct pi_desc pi_desc;
  910. /* Support for a guest hypervisor (nested VMX) */
  911. struct nested_vmx nested;
  912. /* Dynamic PLE window. */
  913. int ple_window;
  914. bool ple_window_dirty;
  915. bool req_immediate_exit;
  916. /* Support for PML */
  917. #define PML_ENTITY_NUM 512
  918. struct page *pml_pg;
  919. /* apic deadline value in host tsc */
  920. u64 hv_deadline_tsc;
  921. u64 current_tsc_ratio;
  922. u32 host_pkru;
  923. unsigned long host_debugctlmsr;
  924. /*
  925. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  926. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  927. * in msr_ia32_feature_control_valid_bits.
  928. */
  929. u64 msr_ia32_feature_control;
  930. u64 msr_ia32_feature_control_valid_bits;
  931. u64 ept_pointer;
  932. };
  933. enum segment_cache_field {
  934. SEG_FIELD_SEL = 0,
  935. SEG_FIELD_BASE = 1,
  936. SEG_FIELD_LIMIT = 2,
  937. SEG_FIELD_AR = 3,
  938. SEG_FIELD_NR = 4
  939. };
  940. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  941. {
  942. return container_of(kvm, struct kvm_vmx, kvm);
  943. }
  944. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  945. {
  946. return container_of(vcpu, struct vcpu_vmx, vcpu);
  947. }
  948. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  949. {
  950. return &(to_vmx(vcpu)->pi_desc);
  951. }
  952. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  953. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  954. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  955. #define FIELD64(number, name) \
  956. FIELD(number, name), \
  957. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  958. static u16 shadow_read_only_fields[] = {
  959. #define SHADOW_FIELD_RO(x) x,
  960. #include "vmx_shadow_fields.h"
  961. };
  962. static int max_shadow_read_only_fields =
  963. ARRAY_SIZE(shadow_read_only_fields);
  964. static u16 shadow_read_write_fields[] = {
  965. #define SHADOW_FIELD_RW(x) x,
  966. #include "vmx_shadow_fields.h"
  967. };
  968. static int max_shadow_read_write_fields =
  969. ARRAY_SIZE(shadow_read_write_fields);
  970. static const unsigned short vmcs_field_to_offset_table[] = {
  971. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  972. FIELD(POSTED_INTR_NV, posted_intr_nv),
  973. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  974. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  975. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  976. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  977. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  978. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  979. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  980. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  981. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  982. FIELD(GUEST_PML_INDEX, guest_pml_index),
  983. FIELD(HOST_ES_SELECTOR, host_es_selector),
  984. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  985. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  986. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  987. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  988. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  989. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  990. FIELD64(IO_BITMAP_A, io_bitmap_a),
  991. FIELD64(IO_BITMAP_B, io_bitmap_b),
  992. FIELD64(MSR_BITMAP, msr_bitmap),
  993. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  994. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  995. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  996. FIELD64(PML_ADDRESS, pml_address),
  997. FIELD64(TSC_OFFSET, tsc_offset),
  998. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  999. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  1000. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  1001. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  1002. FIELD64(EPT_POINTER, ept_pointer),
  1003. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  1004. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  1005. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  1006. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  1007. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  1008. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  1009. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  1010. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  1011. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  1012. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  1013. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  1014. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  1015. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  1016. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  1017. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  1018. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  1019. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  1020. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  1021. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  1022. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  1023. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  1024. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  1025. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  1026. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  1027. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  1028. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  1029. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  1030. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  1031. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  1032. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  1033. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  1034. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  1035. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  1036. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  1037. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  1038. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  1039. FIELD(TPR_THRESHOLD, tpr_threshold),
  1040. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  1041. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  1042. FIELD(VM_EXIT_REASON, vm_exit_reason),
  1043. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  1044. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  1045. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  1046. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  1047. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  1048. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  1049. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  1050. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  1051. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  1052. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  1053. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  1054. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  1055. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  1056. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  1057. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  1058. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  1059. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  1060. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  1061. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  1062. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  1063. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  1064. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  1065. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  1066. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  1067. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  1068. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  1069. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  1070. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  1071. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  1072. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  1073. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  1074. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  1075. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  1076. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  1077. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  1078. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  1079. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  1080. FIELD(EXIT_QUALIFICATION, exit_qualification),
  1081. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  1082. FIELD(GUEST_CR0, guest_cr0),
  1083. FIELD(GUEST_CR3, guest_cr3),
  1084. FIELD(GUEST_CR4, guest_cr4),
  1085. FIELD(GUEST_ES_BASE, guest_es_base),
  1086. FIELD(GUEST_CS_BASE, guest_cs_base),
  1087. FIELD(GUEST_SS_BASE, guest_ss_base),
  1088. FIELD(GUEST_DS_BASE, guest_ds_base),
  1089. FIELD(GUEST_FS_BASE, guest_fs_base),
  1090. FIELD(GUEST_GS_BASE, guest_gs_base),
  1091. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  1092. FIELD(GUEST_TR_BASE, guest_tr_base),
  1093. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  1094. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  1095. FIELD(GUEST_DR7, guest_dr7),
  1096. FIELD(GUEST_RSP, guest_rsp),
  1097. FIELD(GUEST_RIP, guest_rip),
  1098. FIELD(GUEST_RFLAGS, guest_rflags),
  1099. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  1100. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  1101. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  1102. FIELD(HOST_CR0, host_cr0),
  1103. FIELD(HOST_CR3, host_cr3),
  1104. FIELD(HOST_CR4, host_cr4),
  1105. FIELD(HOST_FS_BASE, host_fs_base),
  1106. FIELD(HOST_GS_BASE, host_gs_base),
  1107. FIELD(HOST_TR_BASE, host_tr_base),
  1108. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  1109. FIELD(HOST_IDTR_BASE, host_idtr_base),
  1110. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  1111. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  1112. FIELD(HOST_RSP, host_rsp),
  1113. FIELD(HOST_RIP, host_rip),
  1114. };
  1115. static inline short vmcs_field_to_offset(unsigned long field)
  1116. {
  1117. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  1118. unsigned short offset;
  1119. unsigned index;
  1120. if (field >> 15)
  1121. return -ENOENT;
  1122. index = ROL16(field, 6);
  1123. if (index >= size)
  1124. return -ENOENT;
  1125. index = array_index_nospec(index, size);
  1126. offset = vmcs_field_to_offset_table[index];
  1127. if (offset == 0)
  1128. return -ENOENT;
  1129. return offset;
  1130. }
  1131. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  1132. {
  1133. return to_vmx(vcpu)->nested.cached_vmcs12;
  1134. }
  1135. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  1136. {
  1137. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  1138. }
  1139. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  1140. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  1141. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  1142. static bool vmx_xsaves_supported(void);
  1143. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1144. struct kvm_segment *var, int seg);
  1145. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1146. struct kvm_segment *var, int seg);
  1147. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  1148. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  1149. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  1150. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  1151. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  1152. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  1153. u16 error_code);
  1154. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  1155. static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  1156. u32 msr, int type);
  1157. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  1158. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  1159. /*
  1160. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  1161. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  1162. */
  1163. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1164. /*
  1165. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1166. * can find which vCPU should be waken up.
  1167. */
  1168. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1169. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1170. enum {
  1171. VMX_VMREAD_BITMAP,
  1172. VMX_VMWRITE_BITMAP,
  1173. VMX_BITMAP_NR
  1174. };
  1175. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1176. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1177. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1178. static bool cpu_has_load_ia32_efer;
  1179. static bool cpu_has_load_perf_global_ctrl;
  1180. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1181. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1182. static struct vmcs_config {
  1183. int size;
  1184. int order;
  1185. u32 basic_cap;
  1186. u32 revision_id;
  1187. u32 pin_based_exec_ctrl;
  1188. u32 cpu_based_exec_ctrl;
  1189. u32 cpu_based_2nd_exec_ctrl;
  1190. u32 vmexit_ctrl;
  1191. u32 vmentry_ctrl;
  1192. struct nested_vmx_msrs nested;
  1193. } vmcs_config;
  1194. static struct vmx_capability {
  1195. u32 ept;
  1196. u32 vpid;
  1197. } vmx_capability;
  1198. #define VMX_SEGMENT_FIELD(seg) \
  1199. [VCPU_SREG_##seg] = { \
  1200. .selector = GUEST_##seg##_SELECTOR, \
  1201. .base = GUEST_##seg##_BASE, \
  1202. .limit = GUEST_##seg##_LIMIT, \
  1203. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1204. }
  1205. static const struct kvm_vmx_segment_field {
  1206. unsigned selector;
  1207. unsigned base;
  1208. unsigned limit;
  1209. unsigned ar_bytes;
  1210. } kvm_vmx_segment_fields[] = {
  1211. VMX_SEGMENT_FIELD(CS),
  1212. VMX_SEGMENT_FIELD(DS),
  1213. VMX_SEGMENT_FIELD(ES),
  1214. VMX_SEGMENT_FIELD(FS),
  1215. VMX_SEGMENT_FIELD(GS),
  1216. VMX_SEGMENT_FIELD(SS),
  1217. VMX_SEGMENT_FIELD(TR),
  1218. VMX_SEGMENT_FIELD(LDTR),
  1219. };
  1220. static u64 host_efer;
  1221. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1222. /*
  1223. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1224. * away by decrementing the array size.
  1225. */
  1226. static const u32 vmx_msr_index[] = {
  1227. #ifdef CONFIG_X86_64
  1228. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1229. #endif
  1230. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1231. };
  1232. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1233. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1234. #define KVM_EVMCS_VERSION 1
  1235. #if IS_ENABLED(CONFIG_HYPERV)
  1236. static bool __read_mostly enlightened_vmcs = true;
  1237. module_param(enlightened_vmcs, bool, 0444);
  1238. static inline void evmcs_write64(unsigned long field, u64 value)
  1239. {
  1240. u16 clean_field;
  1241. int offset = get_evmcs_offset(field, &clean_field);
  1242. if (offset < 0)
  1243. return;
  1244. *(u64 *)((char *)current_evmcs + offset) = value;
  1245. current_evmcs->hv_clean_fields &= ~clean_field;
  1246. }
  1247. static inline void evmcs_write32(unsigned long field, u32 value)
  1248. {
  1249. u16 clean_field;
  1250. int offset = get_evmcs_offset(field, &clean_field);
  1251. if (offset < 0)
  1252. return;
  1253. *(u32 *)((char *)current_evmcs + offset) = value;
  1254. current_evmcs->hv_clean_fields &= ~clean_field;
  1255. }
  1256. static inline void evmcs_write16(unsigned long field, u16 value)
  1257. {
  1258. u16 clean_field;
  1259. int offset = get_evmcs_offset(field, &clean_field);
  1260. if (offset < 0)
  1261. return;
  1262. *(u16 *)((char *)current_evmcs + offset) = value;
  1263. current_evmcs->hv_clean_fields &= ~clean_field;
  1264. }
  1265. static inline u64 evmcs_read64(unsigned long field)
  1266. {
  1267. int offset = get_evmcs_offset(field, NULL);
  1268. if (offset < 0)
  1269. return 0;
  1270. return *(u64 *)((char *)current_evmcs + offset);
  1271. }
  1272. static inline u32 evmcs_read32(unsigned long field)
  1273. {
  1274. int offset = get_evmcs_offset(field, NULL);
  1275. if (offset < 0)
  1276. return 0;
  1277. return *(u32 *)((char *)current_evmcs + offset);
  1278. }
  1279. static inline u16 evmcs_read16(unsigned long field)
  1280. {
  1281. int offset = get_evmcs_offset(field, NULL);
  1282. if (offset < 0)
  1283. return 0;
  1284. return *(u16 *)((char *)current_evmcs + offset);
  1285. }
  1286. static inline void evmcs_touch_msr_bitmap(void)
  1287. {
  1288. if (unlikely(!current_evmcs))
  1289. return;
  1290. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1291. current_evmcs->hv_clean_fields &=
  1292. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1293. }
  1294. static void evmcs_load(u64 phys_addr)
  1295. {
  1296. struct hv_vp_assist_page *vp_ap =
  1297. hv_get_vp_assist_page(smp_processor_id());
  1298. vp_ap->current_nested_vmcs = phys_addr;
  1299. vp_ap->enlighten_vmentry = 1;
  1300. }
  1301. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1302. {
  1303. /*
  1304. * Enlightened VMCSv1 doesn't support these:
  1305. *
  1306. * POSTED_INTR_NV = 0x00000002,
  1307. * GUEST_INTR_STATUS = 0x00000810,
  1308. * APIC_ACCESS_ADDR = 0x00002014,
  1309. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1310. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1311. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1312. * EOI_EXIT_BITMAP2 = 0x00002020,
  1313. * EOI_EXIT_BITMAP3 = 0x00002022,
  1314. */
  1315. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  1316. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1317. ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1318. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1319. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1320. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1321. ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1322. /*
  1323. * GUEST_PML_INDEX = 0x00000812,
  1324. * PML_ADDRESS = 0x0000200e,
  1325. */
  1326. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
  1327. /* VM_FUNCTION_CONTROL = 0x00002018, */
  1328. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
  1329. /*
  1330. * EPTP_LIST_ADDRESS = 0x00002024,
  1331. * VMREAD_BITMAP = 0x00002026,
  1332. * VMWRITE_BITMAP = 0x00002028,
  1333. */
  1334. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
  1335. /*
  1336. * TSC_MULTIPLIER = 0x00002032,
  1337. */
  1338. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
  1339. /*
  1340. * PLE_GAP = 0x00004020,
  1341. * PLE_WINDOW = 0x00004022,
  1342. */
  1343. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1344. /*
  1345. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1346. */
  1347. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  1348. /*
  1349. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1350. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1351. */
  1352. vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
  1353. vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  1354. /*
  1355. * Currently unsupported in KVM:
  1356. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1357. */
  1358. }
  1359. /* check_ept_pointer() should be under protection of ept_pointer_lock. */
  1360. static void check_ept_pointer_match(struct kvm *kvm)
  1361. {
  1362. struct kvm_vcpu *vcpu;
  1363. u64 tmp_eptp = INVALID_PAGE;
  1364. int i;
  1365. kvm_for_each_vcpu(i, vcpu, kvm) {
  1366. if (!VALID_PAGE(tmp_eptp)) {
  1367. tmp_eptp = to_vmx(vcpu)->ept_pointer;
  1368. } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
  1369. to_kvm_vmx(kvm)->ept_pointers_match
  1370. = EPT_POINTERS_MISMATCH;
  1371. return;
  1372. }
  1373. }
  1374. to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
  1375. }
  1376. static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
  1377. {
  1378. int ret;
  1379. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1380. if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
  1381. check_ept_pointer_match(kvm);
  1382. if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
  1383. ret = -ENOTSUPP;
  1384. goto out;
  1385. }
  1386. /*
  1387. * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
  1388. * base of EPT PML4 table, strip off EPT configuration information.
  1389. */
  1390. ret = hyperv_flush_guest_mapping(
  1391. to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
  1392. out:
  1393. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1394. return ret;
  1395. }
  1396. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1397. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1398. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1399. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1400. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1401. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1402. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1403. static inline void evmcs_load(u64 phys_addr) {}
  1404. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1405. static inline void evmcs_touch_msr_bitmap(void) {}
  1406. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1407. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1408. {
  1409. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1410. INTR_INFO_VALID_MASK)) ==
  1411. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1412. }
  1413. static inline bool is_debug(u32 intr_info)
  1414. {
  1415. return is_exception_n(intr_info, DB_VECTOR);
  1416. }
  1417. static inline bool is_breakpoint(u32 intr_info)
  1418. {
  1419. return is_exception_n(intr_info, BP_VECTOR);
  1420. }
  1421. static inline bool is_page_fault(u32 intr_info)
  1422. {
  1423. return is_exception_n(intr_info, PF_VECTOR);
  1424. }
  1425. static inline bool is_no_device(u32 intr_info)
  1426. {
  1427. return is_exception_n(intr_info, NM_VECTOR);
  1428. }
  1429. static inline bool is_invalid_opcode(u32 intr_info)
  1430. {
  1431. return is_exception_n(intr_info, UD_VECTOR);
  1432. }
  1433. static inline bool is_gp_fault(u32 intr_info)
  1434. {
  1435. return is_exception_n(intr_info, GP_VECTOR);
  1436. }
  1437. static inline bool is_external_interrupt(u32 intr_info)
  1438. {
  1439. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1440. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  1441. }
  1442. static inline bool is_machine_check(u32 intr_info)
  1443. {
  1444. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1445. INTR_INFO_VALID_MASK)) ==
  1446. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1447. }
  1448. /* Undocumented: icebp/int1 */
  1449. static inline bool is_icebp(u32 intr_info)
  1450. {
  1451. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1452. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1453. }
  1454. static inline bool cpu_has_vmx_msr_bitmap(void)
  1455. {
  1456. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1457. }
  1458. static inline bool cpu_has_vmx_tpr_shadow(void)
  1459. {
  1460. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1461. }
  1462. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1463. {
  1464. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1465. }
  1466. static inline bool cpu_has_secondary_exec_ctrls(void)
  1467. {
  1468. return vmcs_config.cpu_based_exec_ctrl &
  1469. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1470. }
  1471. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1472. {
  1473. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1474. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1475. }
  1476. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1477. {
  1478. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1479. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1480. }
  1481. static inline bool cpu_has_vmx_apic_register_virt(void)
  1482. {
  1483. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1484. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1485. }
  1486. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1487. {
  1488. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1489. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1490. }
  1491. static inline bool cpu_has_vmx_encls_vmexit(void)
  1492. {
  1493. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1494. SECONDARY_EXEC_ENCLS_EXITING;
  1495. }
  1496. /*
  1497. * Comment's format: document - errata name - stepping - processor name.
  1498. * Refer from
  1499. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1500. */
  1501. static u32 vmx_preemption_cpu_tfms[] = {
  1502. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1503. 0x000206E6,
  1504. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1505. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1506. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1507. 0x00020652,
  1508. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1509. 0x00020655,
  1510. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1511. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1512. /*
  1513. * 320767.pdf - AAP86 - B1 -
  1514. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1515. */
  1516. 0x000106E5,
  1517. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1518. 0x000106A0,
  1519. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1520. 0x000106A1,
  1521. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1522. 0x000106A4,
  1523. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1524. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1525. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1526. 0x000106A5,
  1527. };
  1528. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1529. {
  1530. u32 eax = cpuid_eax(0x00000001), i;
  1531. /* Clear the reserved bits */
  1532. eax &= ~(0x3U << 14 | 0xfU << 28);
  1533. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1534. if (eax == vmx_preemption_cpu_tfms[i])
  1535. return true;
  1536. return false;
  1537. }
  1538. static inline bool cpu_has_vmx_preemption_timer(void)
  1539. {
  1540. return vmcs_config.pin_based_exec_ctrl &
  1541. PIN_BASED_VMX_PREEMPTION_TIMER;
  1542. }
  1543. static inline bool cpu_has_vmx_posted_intr(void)
  1544. {
  1545. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1546. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1547. }
  1548. static inline bool cpu_has_vmx_apicv(void)
  1549. {
  1550. return cpu_has_vmx_apic_register_virt() &&
  1551. cpu_has_vmx_virtual_intr_delivery() &&
  1552. cpu_has_vmx_posted_intr();
  1553. }
  1554. static inline bool cpu_has_vmx_flexpriority(void)
  1555. {
  1556. return cpu_has_vmx_tpr_shadow() &&
  1557. cpu_has_vmx_virtualize_apic_accesses();
  1558. }
  1559. static inline bool cpu_has_vmx_ept_execute_only(void)
  1560. {
  1561. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1562. }
  1563. static inline bool cpu_has_vmx_ept_2m_page(void)
  1564. {
  1565. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1566. }
  1567. static inline bool cpu_has_vmx_ept_1g_page(void)
  1568. {
  1569. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1570. }
  1571. static inline bool cpu_has_vmx_ept_4levels(void)
  1572. {
  1573. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1574. }
  1575. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1576. {
  1577. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1578. }
  1579. static inline bool cpu_has_vmx_ept_5levels(void)
  1580. {
  1581. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1582. }
  1583. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1584. {
  1585. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1586. }
  1587. static inline bool cpu_has_vmx_invept_context(void)
  1588. {
  1589. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1590. }
  1591. static inline bool cpu_has_vmx_invept_global(void)
  1592. {
  1593. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1594. }
  1595. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1596. {
  1597. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1598. }
  1599. static inline bool cpu_has_vmx_invvpid_single(void)
  1600. {
  1601. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1602. }
  1603. static inline bool cpu_has_vmx_invvpid_global(void)
  1604. {
  1605. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1606. }
  1607. static inline bool cpu_has_vmx_invvpid(void)
  1608. {
  1609. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1610. }
  1611. static inline bool cpu_has_vmx_ept(void)
  1612. {
  1613. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1614. SECONDARY_EXEC_ENABLE_EPT;
  1615. }
  1616. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1617. {
  1618. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1619. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1620. }
  1621. static inline bool cpu_has_vmx_ple(void)
  1622. {
  1623. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1624. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1625. }
  1626. static inline bool cpu_has_vmx_basic_inout(void)
  1627. {
  1628. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1629. }
  1630. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1631. {
  1632. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1633. }
  1634. static inline bool cpu_has_vmx_vpid(void)
  1635. {
  1636. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1637. SECONDARY_EXEC_ENABLE_VPID;
  1638. }
  1639. static inline bool cpu_has_vmx_rdtscp(void)
  1640. {
  1641. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1642. SECONDARY_EXEC_RDTSCP;
  1643. }
  1644. static inline bool cpu_has_vmx_invpcid(void)
  1645. {
  1646. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1647. SECONDARY_EXEC_ENABLE_INVPCID;
  1648. }
  1649. static inline bool cpu_has_virtual_nmis(void)
  1650. {
  1651. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1652. }
  1653. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1654. {
  1655. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1656. SECONDARY_EXEC_WBINVD_EXITING;
  1657. }
  1658. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1659. {
  1660. u64 vmx_msr;
  1661. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1662. /* check if the cpu supports writing r/o exit information fields */
  1663. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1664. return false;
  1665. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1666. SECONDARY_EXEC_SHADOW_VMCS;
  1667. }
  1668. static inline bool cpu_has_vmx_pml(void)
  1669. {
  1670. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1671. }
  1672. static inline bool cpu_has_vmx_tsc_scaling(void)
  1673. {
  1674. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1675. SECONDARY_EXEC_TSC_SCALING;
  1676. }
  1677. static inline bool cpu_has_vmx_vmfunc(void)
  1678. {
  1679. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1680. SECONDARY_EXEC_ENABLE_VMFUNC;
  1681. }
  1682. static bool vmx_umip_emulated(void)
  1683. {
  1684. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1685. SECONDARY_EXEC_DESC;
  1686. }
  1687. static inline bool report_flexpriority(void)
  1688. {
  1689. return flexpriority_enabled;
  1690. }
  1691. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1692. {
  1693. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1694. }
  1695. /*
  1696. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1697. * to modify any valid field of the VMCS, or are the VM-exit
  1698. * information fields read-only?
  1699. */
  1700. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1701. {
  1702. return to_vmx(vcpu)->nested.msrs.misc_low &
  1703. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1704. }
  1705. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1706. {
  1707. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1708. }
  1709. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1710. {
  1711. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1712. CPU_BASED_MONITOR_TRAP_FLAG;
  1713. }
  1714. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  1715. {
  1716. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  1717. SECONDARY_EXEC_SHADOW_VMCS;
  1718. }
  1719. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1720. {
  1721. return vmcs12->cpu_based_vm_exec_control & bit;
  1722. }
  1723. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1724. {
  1725. return (vmcs12->cpu_based_vm_exec_control &
  1726. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1727. (vmcs12->secondary_vm_exec_control & bit);
  1728. }
  1729. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1730. {
  1731. return vmcs12->pin_based_vm_exec_control &
  1732. PIN_BASED_VMX_PREEMPTION_TIMER;
  1733. }
  1734. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1735. {
  1736. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1737. }
  1738. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1739. {
  1740. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1741. }
  1742. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1743. {
  1744. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1745. }
  1746. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1747. {
  1748. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1749. }
  1750. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1751. {
  1752. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1753. }
  1754. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1755. {
  1756. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1757. }
  1758. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1759. {
  1760. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1761. }
  1762. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1763. {
  1764. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1765. }
  1766. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1767. {
  1768. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1769. }
  1770. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1771. {
  1772. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1773. }
  1774. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1775. {
  1776. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1777. }
  1778. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1779. {
  1780. return nested_cpu_has_vmfunc(vmcs12) &&
  1781. (vmcs12->vm_function_control &
  1782. VMX_VMFUNC_EPTP_SWITCHING);
  1783. }
  1784. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  1785. {
  1786. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  1787. }
  1788. static inline bool is_nmi(u32 intr_info)
  1789. {
  1790. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1791. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1792. }
  1793. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1794. u32 exit_intr_info,
  1795. unsigned long exit_qualification);
  1796. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1797. struct vmcs12 *vmcs12,
  1798. u32 reason, unsigned long qualification);
  1799. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1800. {
  1801. int i;
  1802. for (i = 0; i < vmx->nmsrs; ++i)
  1803. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1804. return i;
  1805. return -1;
  1806. }
  1807. static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
  1808. {
  1809. struct {
  1810. u64 vpid : 16;
  1811. u64 rsvd : 48;
  1812. u64 gva;
  1813. } operand = { vpid, 0, gva };
  1814. bool error;
  1815. asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
  1816. : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
  1817. : "memory");
  1818. BUG_ON(error);
  1819. }
  1820. static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
  1821. {
  1822. struct {
  1823. u64 eptp, gpa;
  1824. } operand = {eptp, gpa};
  1825. bool error;
  1826. asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
  1827. : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
  1828. : "memory");
  1829. BUG_ON(error);
  1830. }
  1831. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1832. {
  1833. int i;
  1834. i = __find_msr_index(vmx, msr);
  1835. if (i >= 0)
  1836. return &vmx->guest_msrs[i];
  1837. return NULL;
  1838. }
  1839. static void vmcs_clear(struct vmcs *vmcs)
  1840. {
  1841. u64 phys_addr = __pa(vmcs);
  1842. bool error;
  1843. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
  1844. : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
  1845. : "memory");
  1846. if (unlikely(error))
  1847. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1848. vmcs, phys_addr);
  1849. }
  1850. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1851. {
  1852. vmcs_clear(loaded_vmcs->vmcs);
  1853. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1854. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1855. loaded_vmcs->cpu = -1;
  1856. loaded_vmcs->launched = 0;
  1857. }
  1858. static void vmcs_load(struct vmcs *vmcs)
  1859. {
  1860. u64 phys_addr = __pa(vmcs);
  1861. bool error;
  1862. if (static_branch_unlikely(&enable_evmcs))
  1863. return evmcs_load(phys_addr);
  1864. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
  1865. : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
  1866. : "memory");
  1867. if (unlikely(error))
  1868. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1869. vmcs, phys_addr);
  1870. }
  1871. #ifdef CONFIG_KEXEC_CORE
  1872. static void crash_vmclear_local_loaded_vmcss(void)
  1873. {
  1874. int cpu = raw_smp_processor_id();
  1875. struct loaded_vmcs *v;
  1876. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1877. loaded_vmcss_on_cpu_link)
  1878. vmcs_clear(v->vmcs);
  1879. }
  1880. #endif /* CONFIG_KEXEC_CORE */
  1881. static void __loaded_vmcs_clear(void *arg)
  1882. {
  1883. struct loaded_vmcs *loaded_vmcs = arg;
  1884. int cpu = raw_smp_processor_id();
  1885. if (loaded_vmcs->cpu != cpu)
  1886. return; /* vcpu migration can race with cpu offline */
  1887. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1888. per_cpu(current_vmcs, cpu) = NULL;
  1889. vmcs_clear(loaded_vmcs->vmcs);
  1890. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1891. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1892. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1893. /*
  1894. * Ensure all writes to loaded_vmcs, including deleting it from its
  1895. * current percpu list, complete before setting loaded_vmcs->vcpu to
  1896. * -1, otherwise a different cpu can see vcpu == -1 first and add
  1897. * loaded_vmcs to its percpu list before it's deleted from this cpu's
  1898. * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
  1899. */
  1900. smp_wmb();
  1901. loaded_vmcs->cpu = -1;
  1902. loaded_vmcs->launched = 0;
  1903. }
  1904. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1905. {
  1906. int cpu = loaded_vmcs->cpu;
  1907. if (cpu != -1)
  1908. smp_call_function_single(cpu,
  1909. __loaded_vmcs_clear, loaded_vmcs, 1);
  1910. }
  1911. static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
  1912. {
  1913. if (vpid == 0)
  1914. return true;
  1915. if (cpu_has_vmx_invvpid_individual_addr()) {
  1916. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
  1917. return true;
  1918. }
  1919. return false;
  1920. }
  1921. static inline void vpid_sync_vcpu_single(int vpid)
  1922. {
  1923. if (vpid == 0)
  1924. return;
  1925. if (cpu_has_vmx_invvpid_single())
  1926. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1927. }
  1928. static inline void vpid_sync_vcpu_global(void)
  1929. {
  1930. if (cpu_has_vmx_invvpid_global())
  1931. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1932. }
  1933. static inline void vpid_sync_context(int vpid)
  1934. {
  1935. if (cpu_has_vmx_invvpid_single())
  1936. vpid_sync_vcpu_single(vpid);
  1937. else
  1938. vpid_sync_vcpu_global();
  1939. }
  1940. static inline void ept_sync_global(void)
  1941. {
  1942. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1943. }
  1944. static inline void ept_sync_context(u64 eptp)
  1945. {
  1946. if (cpu_has_vmx_invept_context())
  1947. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1948. else
  1949. ept_sync_global();
  1950. }
  1951. static __always_inline void vmcs_check16(unsigned long field)
  1952. {
  1953. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1954. "16-bit accessor invalid for 64-bit field");
  1955. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1956. "16-bit accessor invalid for 64-bit high field");
  1957. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1958. "16-bit accessor invalid for 32-bit high field");
  1959. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1960. "16-bit accessor invalid for natural width field");
  1961. }
  1962. static __always_inline void vmcs_check32(unsigned long field)
  1963. {
  1964. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1965. "32-bit accessor invalid for 16-bit field");
  1966. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1967. "32-bit accessor invalid for natural width field");
  1968. }
  1969. static __always_inline void vmcs_check64(unsigned long field)
  1970. {
  1971. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1972. "64-bit accessor invalid for 16-bit field");
  1973. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1974. "64-bit accessor invalid for 64-bit high field");
  1975. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1976. "64-bit accessor invalid for 32-bit field");
  1977. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1978. "64-bit accessor invalid for natural width field");
  1979. }
  1980. static __always_inline void vmcs_checkl(unsigned long field)
  1981. {
  1982. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1983. "Natural width accessor invalid for 16-bit field");
  1984. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1985. "Natural width accessor invalid for 64-bit field");
  1986. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1987. "Natural width accessor invalid for 64-bit high field");
  1988. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1989. "Natural width accessor invalid for 32-bit field");
  1990. }
  1991. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  1992. {
  1993. unsigned long value;
  1994. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1995. : "=a"(value) : "d"(field) : "cc");
  1996. return value;
  1997. }
  1998. static __always_inline u16 vmcs_read16(unsigned long field)
  1999. {
  2000. vmcs_check16(field);
  2001. if (static_branch_unlikely(&enable_evmcs))
  2002. return evmcs_read16(field);
  2003. return __vmcs_readl(field);
  2004. }
  2005. static __always_inline u32 vmcs_read32(unsigned long field)
  2006. {
  2007. vmcs_check32(field);
  2008. if (static_branch_unlikely(&enable_evmcs))
  2009. return evmcs_read32(field);
  2010. return __vmcs_readl(field);
  2011. }
  2012. static __always_inline u64 vmcs_read64(unsigned long field)
  2013. {
  2014. vmcs_check64(field);
  2015. if (static_branch_unlikely(&enable_evmcs))
  2016. return evmcs_read64(field);
  2017. #ifdef CONFIG_X86_64
  2018. return __vmcs_readl(field);
  2019. #else
  2020. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  2021. #endif
  2022. }
  2023. static __always_inline unsigned long vmcs_readl(unsigned long field)
  2024. {
  2025. vmcs_checkl(field);
  2026. if (static_branch_unlikely(&enable_evmcs))
  2027. return evmcs_read64(field);
  2028. return __vmcs_readl(field);
  2029. }
  2030. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  2031. {
  2032. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  2033. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  2034. dump_stack();
  2035. }
  2036. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  2037. {
  2038. bool error;
  2039. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
  2040. : CC_OUT(na) (error) : "a"(value), "d"(field));
  2041. if (unlikely(error))
  2042. vmwrite_error(field, value);
  2043. }
  2044. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  2045. {
  2046. vmcs_check16(field);
  2047. if (static_branch_unlikely(&enable_evmcs))
  2048. return evmcs_write16(field, value);
  2049. __vmcs_writel(field, value);
  2050. }
  2051. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  2052. {
  2053. vmcs_check32(field);
  2054. if (static_branch_unlikely(&enable_evmcs))
  2055. return evmcs_write32(field, value);
  2056. __vmcs_writel(field, value);
  2057. }
  2058. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  2059. {
  2060. vmcs_check64(field);
  2061. if (static_branch_unlikely(&enable_evmcs))
  2062. return evmcs_write64(field, value);
  2063. __vmcs_writel(field, value);
  2064. #ifndef CONFIG_X86_64
  2065. asm volatile ("");
  2066. __vmcs_writel(field+1, value >> 32);
  2067. #endif
  2068. }
  2069. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  2070. {
  2071. vmcs_checkl(field);
  2072. if (static_branch_unlikely(&enable_evmcs))
  2073. return evmcs_write64(field, value);
  2074. __vmcs_writel(field, value);
  2075. }
  2076. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  2077. {
  2078. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2079. "vmcs_clear_bits does not support 64-bit fields");
  2080. if (static_branch_unlikely(&enable_evmcs))
  2081. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  2082. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  2083. }
  2084. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  2085. {
  2086. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2087. "vmcs_set_bits does not support 64-bit fields");
  2088. if (static_branch_unlikely(&enable_evmcs))
  2089. return evmcs_write32(field, evmcs_read32(field) | mask);
  2090. __vmcs_writel(field, __vmcs_readl(field) | mask);
  2091. }
  2092. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  2093. {
  2094. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  2095. }
  2096. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  2097. {
  2098. vmcs_write32(VM_ENTRY_CONTROLS, val);
  2099. vmx->vm_entry_controls_shadow = val;
  2100. }
  2101. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  2102. {
  2103. if (vmx->vm_entry_controls_shadow != val)
  2104. vm_entry_controls_init(vmx, val);
  2105. }
  2106. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  2107. {
  2108. return vmx->vm_entry_controls_shadow;
  2109. }
  2110. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2111. {
  2112. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  2113. }
  2114. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2115. {
  2116. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  2117. }
  2118. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  2119. {
  2120. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  2121. }
  2122. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  2123. {
  2124. vmcs_write32(VM_EXIT_CONTROLS, val);
  2125. vmx->vm_exit_controls_shadow = val;
  2126. }
  2127. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  2128. {
  2129. if (vmx->vm_exit_controls_shadow != val)
  2130. vm_exit_controls_init(vmx, val);
  2131. }
  2132. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  2133. {
  2134. return vmx->vm_exit_controls_shadow;
  2135. }
  2136. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2137. {
  2138. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  2139. }
  2140. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2141. {
  2142. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  2143. }
  2144. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  2145. {
  2146. vmx->segment_cache.bitmask = 0;
  2147. }
  2148. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  2149. unsigned field)
  2150. {
  2151. bool ret;
  2152. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  2153. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  2154. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  2155. vmx->segment_cache.bitmask = 0;
  2156. }
  2157. ret = vmx->segment_cache.bitmask & mask;
  2158. vmx->segment_cache.bitmask |= mask;
  2159. return ret;
  2160. }
  2161. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  2162. {
  2163. u16 *p = &vmx->segment_cache.seg[seg].selector;
  2164. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  2165. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  2166. return *p;
  2167. }
  2168. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  2169. {
  2170. ulong *p = &vmx->segment_cache.seg[seg].base;
  2171. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  2172. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  2173. return *p;
  2174. }
  2175. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  2176. {
  2177. u32 *p = &vmx->segment_cache.seg[seg].limit;
  2178. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  2179. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  2180. return *p;
  2181. }
  2182. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  2183. {
  2184. u32 *p = &vmx->segment_cache.seg[seg].ar;
  2185. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  2186. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  2187. return *p;
  2188. }
  2189. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  2190. {
  2191. u32 eb;
  2192. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  2193. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  2194. /*
  2195. * Guest access to VMware backdoor ports could legitimately
  2196. * trigger #GP because of TSS I/O permission bitmap.
  2197. * We intercept those #GP and allow access to them anyway
  2198. * as VMware does.
  2199. */
  2200. if (enable_vmware_backdoor)
  2201. eb |= (1u << GP_VECTOR);
  2202. if ((vcpu->guest_debug &
  2203. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  2204. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  2205. eb |= 1u << BP_VECTOR;
  2206. if (to_vmx(vcpu)->rmode.vm86_active)
  2207. eb = ~0;
  2208. if (enable_ept)
  2209. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2210. /* When we are running a nested L2 guest and L1 specified for it a
  2211. * certain exception bitmap, we must trap the same exceptions and pass
  2212. * them to L1. When running L2, we will only handle the exceptions
  2213. * specified above if L1 did not want them.
  2214. */
  2215. if (is_guest_mode(vcpu))
  2216. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2217. vmcs_write32(EXCEPTION_BITMAP, eb);
  2218. }
  2219. /*
  2220. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2221. */
  2222. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2223. {
  2224. unsigned long *msr_bitmap;
  2225. int f = sizeof(unsigned long);
  2226. if (!cpu_has_vmx_msr_bitmap())
  2227. return true;
  2228. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2229. if (msr <= 0x1fff) {
  2230. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2231. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2232. msr &= 0x1fff;
  2233. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2234. }
  2235. return true;
  2236. }
  2237. /*
  2238. * Check if MSR is intercepted for L01 MSR bitmap.
  2239. */
  2240. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2241. {
  2242. unsigned long *msr_bitmap;
  2243. int f = sizeof(unsigned long);
  2244. if (!cpu_has_vmx_msr_bitmap())
  2245. return true;
  2246. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2247. if (msr <= 0x1fff) {
  2248. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2249. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2250. msr &= 0x1fff;
  2251. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2252. }
  2253. return true;
  2254. }
  2255. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2256. unsigned long entry, unsigned long exit)
  2257. {
  2258. vm_entry_controls_clearbit(vmx, entry);
  2259. vm_exit_controls_clearbit(vmx, exit);
  2260. }
  2261. static int find_msr(struct vmx_msrs *m, unsigned int msr)
  2262. {
  2263. unsigned int i;
  2264. for (i = 0; i < m->nr; ++i) {
  2265. if (m->val[i].index == msr)
  2266. return i;
  2267. }
  2268. return -ENOENT;
  2269. }
  2270. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2271. {
  2272. int i;
  2273. struct msr_autoload *m = &vmx->msr_autoload;
  2274. switch (msr) {
  2275. case MSR_EFER:
  2276. if (cpu_has_load_ia32_efer) {
  2277. clear_atomic_switch_msr_special(vmx,
  2278. VM_ENTRY_LOAD_IA32_EFER,
  2279. VM_EXIT_LOAD_IA32_EFER);
  2280. return;
  2281. }
  2282. break;
  2283. case MSR_CORE_PERF_GLOBAL_CTRL:
  2284. if (cpu_has_load_perf_global_ctrl) {
  2285. clear_atomic_switch_msr_special(vmx,
  2286. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2287. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2288. return;
  2289. }
  2290. break;
  2291. }
  2292. i = find_msr(&m->guest, msr);
  2293. if (i < 0)
  2294. goto skip_guest;
  2295. --m->guest.nr;
  2296. m->guest.val[i] = m->guest.val[m->guest.nr];
  2297. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2298. skip_guest:
  2299. i = find_msr(&m->host, msr);
  2300. if (i < 0)
  2301. return;
  2302. --m->host.nr;
  2303. m->host.val[i] = m->host.val[m->host.nr];
  2304. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2305. }
  2306. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2307. unsigned long entry, unsigned long exit,
  2308. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2309. u64 guest_val, u64 host_val)
  2310. {
  2311. vmcs_write64(guest_val_vmcs, guest_val);
  2312. vmcs_write64(host_val_vmcs, host_val);
  2313. vm_entry_controls_setbit(vmx, entry);
  2314. vm_exit_controls_setbit(vmx, exit);
  2315. }
  2316. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2317. u64 guest_val, u64 host_val, bool entry_only)
  2318. {
  2319. int i, j = 0;
  2320. struct msr_autoload *m = &vmx->msr_autoload;
  2321. switch (msr) {
  2322. case MSR_EFER:
  2323. if (cpu_has_load_ia32_efer) {
  2324. add_atomic_switch_msr_special(vmx,
  2325. VM_ENTRY_LOAD_IA32_EFER,
  2326. VM_EXIT_LOAD_IA32_EFER,
  2327. GUEST_IA32_EFER,
  2328. HOST_IA32_EFER,
  2329. guest_val, host_val);
  2330. return;
  2331. }
  2332. break;
  2333. case MSR_CORE_PERF_GLOBAL_CTRL:
  2334. if (cpu_has_load_perf_global_ctrl) {
  2335. add_atomic_switch_msr_special(vmx,
  2336. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2337. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2338. GUEST_IA32_PERF_GLOBAL_CTRL,
  2339. HOST_IA32_PERF_GLOBAL_CTRL,
  2340. guest_val, host_val);
  2341. return;
  2342. }
  2343. break;
  2344. case MSR_IA32_PEBS_ENABLE:
  2345. /* PEBS needs a quiescent period after being disabled (to write
  2346. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2347. * provide that period, so a CPU could write host's record into
  2348. * guest's memory.
  2349. */
  2350. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2351. }
  2352. i = find_msr(&m->guest, msr);
  2353. if (!entry_only)
  2354. j = find_msr(&m->host, msr);
  2355. if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
  2356. (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
  2357. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2358. "Can't add msr %x\n", msr);
  2359. return;
  2360. }
  2361. if (i < 0) {
  2362. i = m->guest.nr++;
  2363. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2364. }
  2365. m->guest.val[i].index = msr;
  2366. m->guest.val[i].value = guest_val;
  2367. if (entry_only)
  2368. return;
  2369. if (j < 0) {
  2370. j = m->host.nr++;
  2371. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2372. }
  2373. m->host.val[j].index = msr;
  2374. m->host.val[j].value = host_val;
  2375. }
  2376. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2377. {
  2378. u64 guest_efer = vmx->vcpu.arch.efer;
  2379. u64 ignore_bits = 0;
  2380. /* Shadow paging assumes NX to be available. */
  2381. if (!enable_ept)
  2382. guest_efer |= EFER_NX;
  2383. /*
  2384. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2385. */
  2386. ignore_bits |= EFER_SCE;
  2387. #ifdef CONFIG_X86_64
  2388. ignore_bits |= EFER_LMA | EFER_LME;
  2389. /* SCE is meaningful only in long mode on Intel */
  2390. if (guest_efer & EFER_LMA)
  2391. ignore_bits &= ~(u64)EFER_SCE;
  2392. #endif
  2393. clear_atomic_switch_msr(vmx, MSR_EFER);
  2394. /*
  2395. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2396. * On CPUs that support "load IA32_EFER", always switch EFER
  2397. * atomically, since it's faster than switching it manually.
  2398. */
  2399. if (cpu_has_load_ia32_efer ||
  2400. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2401. if (!(guest_efer & EFER_LMA))
  2402. guest_efer &= ~EFER_LME;
  2403. if (guest_efer != host_efer)
  2404. add_atomic_switch_msr(vmx, MSR_EFER,
  2405. guest_efer, host_efer, false);
  2406. return false;
  2407. } else {
  2408. guest_efer &= ~ignore_bits;
  2409. guest_efer |= host_efer & ignore_bits;
  2410. vmx->guest_msrs[efer_offset].data = guest_efer;
  2411. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2412. return true;
  2413. }
  2414. }
  2415. #ifdef CONFIG_X86_32
  2416. /*
  2417. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2418. * VMCS rather than the segment table. KVM uses this helper to figure
  2419. * out the current bases to poke them into the VMCS before entry.
  2420. */
  2421. static unsigned long segment_base(u16 selector)
  2422. {
  2423. struct desc_struct *table;
  2424. unsigned long v;
  2425. if (!(selector & ~SEGMENT_RPL_MASK))
  2426. return 0;
  2427. table = get_current_gdt_ro();
  2428. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2429. u16 ldt_selector = kvm_read_ldt();
  2430. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2431. return 0;
  2432. table = (struct desc_struct *)segment_base(ldt_selector);
  2433. }
  2434. v = get_desc_base(&table[selector >> 3]);
  2435. return v;
  2436. }
  2437. #endif
  2438. static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
  2439. {
  2440. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2441. struct vmcs_host_state *host_state;
  2442. #ifdef CONFIG_X86_64
  2443. int cpu = raw_smp_processor_id();
  2444. #endif
  2445. unsigned long fs_base, gs_base;
  2446. u16 fs_sel, gs_sel;
  2447. int i;
  2448. vmx->req_immediate_exit = false;
  2449. /*
  2450. * Note that guest MSRs to be saved/restored can also be changed
  2451. * when guest state is loaded. This happens when guest transitions
  2452. * to/from long-mode by setting MSR_EFER.LMA.
  2453. */
  2454. if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
  2455. vmx->guest_msrs_dirty = false;
  2456. for (i = 0; i < vmx->save_nmsrs; ++i)
  2457. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2458. vmx->guest_msrs[i].data,
  2459. vmx->guest_msrs[i].mask);
  2460. }
  2461. if (vmx->loaded_cpu_state)
  2462. return;
  2463. vmx->loaded_cpu_state = vmx->loaded_vmcs;
  2464. host_state = &vmx->loaded_cpu_state->host_state;
  2465. /*
  2466. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2467. * allow segment selectors with cpl > 0 or ti == 1.
  2468. */
  2469. host_state->ldt_sel = kvm_read_ldt();
  2470. #ifdef CONFIG_X86_64
  2471. savesegment(ds, host_state->ds_sel);
  2472. savesegment(es, host_state->es_sel);
  2473. gs_base = cpu_kernelmode_gs_base(cpu);
  2474. if (likely(is_64bit_mm(current->mm))) {
  2475. save_fsgs_for_kvm();
  2476. fs_sel = current->thread.fsindex;
  2477. gs_sel = current->thread.gsindex;
  2478. fs_base = current->thread.fsbase;
  2479. vmx->msr_host_kernel_gs_base = current->thread.gsbase;
  2480. } else {
  2481. savesegment(fs, fs_sel);
  2482. savesegment(gs, gs_sel);
  2483. fs_base = read_msr(MSR_FS_BASE);
  2484. vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2485. }
  2486. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2487. #else
  2488. savesegment(fs, fs_sel);
  2489. savesegment(gs, gs_sel);
  2490. fs_base = segment_base(fs_sel);
  2491. gs_base = segment_base(gs_sel);
  2492. #endif
  2493. if (unlikely(fs_sel != host_state->fs_sel)) {
  2494. if (!(fs_sel & 7))
  2495. vmcs_write16(HOST_FS_SELECTOR, fs_sel);
  2496. else
  2497. vmcs_write16(HOST_FS_SELECTOR, 0);
  2498. host_state->fs_sel = fs_sel;
  2499. }
  2500. if (unlikely(gs_sel != host_state->gs_sel)) {
  2501. if (!(gs_sel & 7))
  2502. vmcs_write16(HOST_GS_SELECTOR, gs_sel);
  2503. else
  2504. vmcs_write16(HOST_GS_SELECTOR, 0);
  2505. host_state->gs_sel = gs_sel;
  2506. }
  2507. if (unlikely(fs_base != host_state->fs_base)) {
  2508. vmcs_writel(HOST_FS_BASE, fs_base);
  2509. host_state->fs_base = fs_base;
  2510. }
  2511. if (unlikely(gs_base != host_state->gs_base)) {
  2512. vmcs_writel(HOST_GS_BASE, gs_base);
  2513. host_state->gs_base = gs_base;
  2514. }
  2515. }
  2516. static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
  2517. {
  2518. struct vmcs_host_state *host_state;
  2519. if (!vmx->loaded_cpu_state)
  2520. return;
  2521. WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
  2522. host_state = &vmx->loaded_cpu_state->host_state;
  2523. ++vmx->vcpu.stat.host_state_reload;
  2524. vmx->loaded_cpu_state = NULL;
  2525. #ifdef CONFIG_X86_64
  2526. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2527. #endif
  2528. if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
  2529. kvm_load_ldt(host_state->ldt_sel);
  2530. #ifdef CONFIG_X86_64
  2531. load_gs_index(host_state->gs_sel);
  2532. #else
  2533. loadsegment(gs, host_state->gs_sel);
  2534. #endif
  2535. }
  2536. if (host_state->fs_sel & 7)
  2537. loadsegment(fs, host_state->fs_sel);
  2538. #ifdef CONFIG_X86_64
  2539. if (unlikely(host_state->ds_sel | host_state->es_sel)) {
  2540. loadsegment(ds, host_state->ds_sel);
  2541. loadsegment(es, host_state->es_sel);
  2542. }
  2543. #endif
  2544. invalidate_tss_limit();
  2545. #ifdef CONFIG_X86_64
  2546. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2547. #endif
  2548. load_fixmap_gdt(raw_smp_processor_id());
  2549. }
  2550. #ifdef CONFIG_X86_64
  2551. static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
  2552. {
  2553. preempt_disable();
  2554. if (vmx->loaded_cpu_state)
  2555. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2556. preempt_enable();
  2557. return vmx->msr_guest_kernel_gs_base;
  2558. }
  2559. static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
  2560. {
  2561. preempt_disable();
  2562. if (vmx->loaded_cpu_state)
  2563. wrmsrl(MSR_KERNEL_GS_BASE, data);
  2564. preempt_enable();
  2565. vmx->msr_guest_kernel_gs_base = data;
  2566. }
  2567. #endif
  2568. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2569. {
  2570. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2571. struct pi_desc old, new;
  2572. unsigned int dest;
  2573. /*
  2574. * In case of hot-plug or hot-unplug, we may have to undo
  2575. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2576. * always keep PI.NDST up to date for simplicity: it makes the
  2577. * code easier, and CPU migration is not a fast path.
  2578. */
  2579. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2580. return;
  2581. /*
  2582. * First handle the simple case where no cmpxchg is necessary; just
  2583. * allow posting non-urgent interrupts.
  2584. *
  2585. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2586. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2587. * expects the VCPU to be on the blocked_vcpu_list that matches
  2588. * PI.NDST.
  2589. */
  2590. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2591. vcpu->cpu == cpu) {
  2592. pi_clear_sn(pi_desc);
  2593. return;
  2594. }
  2595. /* The full case. */
  2596. do {
  2597. old.control = new.control = pi_desc->control;
  2598. dest = cpu_physical_id(cpu);
  2599. if (x2apic_enabled())
  2600. new.ndst = dest;
  2601. else
  2602. new.ndst = (dest << 8) & 0xFF00;
  2603. new.sn = 0;
  2604. } while (cmpxchg64(&pi_desc->control, old.control,
  2605. new.control) != old.control);
  2606. }
  2607. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2608. {
  2609. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2610. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2611. }
  2612. /*
  2613. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2614. * vcpu mutex is already taken.
  2615. */
  2616. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2617. {
  2618. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2619. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2620. if (!already_loaded) {
  2621. loaded_vmcs_clear(vmx->loaded_vmcs);
  2622. local_irq_disable();
  2623. /*
  2624. * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
  2625. * this cpu's percpu list, otherwise it may not yet be deleted
  2626. * from its previous cpu's percpu list. Pairs with the
  2627. * smb_wmb() in __loaded_vmcs_clear().
  2628. */
  2629. smp_rmb();
  2630. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2631. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2632. local_irq_enable();
  2633. }
  2634. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2635. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2636. vmcs_load(vmx->loaded_vmcs->vmcs);
  2637. indirect_branch_prediction_barrier();
  2638. }
  2639. if (!already_loaded) {
  2640. void *gdt = get_current_gdt_ro();
  2641. unsigned long sysenter_esp;
  2642. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2643. /*
  2644. * Linux uses per-cpu TSS and GDT, so set these when switching
  2645. * processors. See 22.2.4.
  2646. */
  2647. vmcs_writel(HOST_TR_BASE,
  2648. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2649. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2650. /*
  2651. * VM exits change the host TR limit to 0x67 after a VM
  2652. * exit. This is okay, since 0x67 covers everything except
  2653. * the IO bitmap and have have code to handle the IO bitmap
  2654. * being lost after a VM exit.
  2655. */
  2656. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2657. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2658. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2659. vmx->loaded_vmcs->cpu = cpu;
  2660. }
  2661. /* Setup TSC multiplier */
  2662. if (kvm_has_tsc_control &&
  2663. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2664. decache_tsc_multiplier(vmx);
  2665. vmx_vcpu_pi_load(vcpu, cpu);
  2666. vmx->host_pkru = read_pkru();
  2667. vmx->host_debugctlmsr = get_debugctlmsr();
  2668. }
  2669. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2670. {
  2671. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2672. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2673. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2674. !kvm_vcpu_apicv_active(vcpu))
  2675. return;
  2676. /* Set SN when the vCPU is preempted */
  2677. if (vcpu->preempted)
  2678. pi_set_sn(pi_desc);
  2679. }
  2680. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2681. {
  2682. vmx_vcpu_pi_put(vcpu);
  2683. vmx_prepare_switch_to_host(to_vmx(vcpu));
  2684. }
  2685. static bool emulation_required(struct kvm_vcpu *vcpu)
  2686. {
  2687. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2688. }
  2689. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2690. /*
  2691. * Return the cr0 value that a nested guest would read. This is a combination
  2692. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2693. * its hypervisor (cr0_read_shadow).
  2694. */
  2695. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2696. {
  2697. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2698. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2699. }
  2700. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2701. {
  2702. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2703. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2704. }
  2705. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2706. {
  2707. unsigned long rflags, save_rflags;
  2708. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2709. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2710. rflags = vmcs_readl(GUEST_RFLAGS);
  2711. if (to_vmx(vcpu)->rmode.vm86_active) {
  2712. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2713. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2714. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2715. }
  2716. to_vmx(vcpu)->rflags = rflags;
  2717. }
  2718. return to_vmx(vcpu)->rflags;
  2719. }
  2720. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2721. {
  2722. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2723. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2724. to_vmx(vcpu)->rflags = rflags;
  2725. if (to_vmx(vcpu)->rmode.vm86_active) {
  2726. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2727. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2728. }
  2729. vmcs_writel(GUEST_RFLAGS, rflags);
  2730. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2731. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2732. }
  2733. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2734. {
  2735. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2736. int ret = 0;
  2737. if (interruptibility & GUEST_INTR_STATE_STI)
  2738. ret |= KVM_X86_SHADOW_INT_STI;
  2739. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2740. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2741. return ret;
  2742. }
  2743. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2744. {
  2745. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2746. u32 interruptibility = interruptibility_old;
  2747. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2748. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2749. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2750. else if (mask & KVM_X86_SHADOW_INT_STI)
  2751. interruptibility |= GUEST_INTR_STATE_STI;
  2752. if ((interruptibility != interruptibility_old))
  2753. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2754. }
  2755. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2756. {
  2757. unsigned long rip;
  2758. rip = kvm_rip_read(vcpu);
  2759. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2760. kvm_rip_write(vcpu, rip);
  2761. /* skipping an emulated instruction also counts */
  2762. vmx_set_interrupt_shadow(vcpu, 0);
  2763. }
  2764. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2765. unsigned long exit_qual)
  2766. {
  2767. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2768. unsigned int nr = vcpu->arch.exception.nr;
  2769. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2770. if (vcpu->arch.exception.has_error_code) {
  2771. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2772. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2773. }
  2774. if (kvm_exception_is_soft(nr))
  2775. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2776. else
  2777. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2778. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2779. vmx_get_nmi_mask(vcpu))
  2780. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2781. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2782. }
  2783. /*
  2784. * KVM wants to inject page-faults which it got to the guest. This function
  2785. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2786. */
  2787. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2788. {
  2789. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2790. unsigned int nr = vcpu->arch.exception.nr;
  2791. if (nr == PF_VECTOR) {
  2792. if (vcpu->arch.exception.nested_apf) {
  2793. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2794. return 1;
  2795. }
  2796. /*
  2797. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2798. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2799. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2800. * can be written only when inject_pending_event runs. This should be
  2801. * conditional on a new capability---if the capability is disabled,
  2802. * kvm_multiple_exception would write the ancillary information to
  2803. * CR2 or DR6, for backwards ABI-compatibility.
  2804. */
  2805. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2806. vcpu->arch.exception.error_code)) {
  2807. *exit_qual = vcpu->arch.cr2;
  2808. return 1;
  2809. }
  2810. } else {
  2811. if (vmcs12->exception_bitmap & (1u << nr)) {
  2812. if (nr == DB_VECTOR) {
  2813. *exit_qual = vcpu->arch.dr6;
  2814. *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
  2815. *exit_qual ^= DR6_RTM;
  2816. } else {
  2817. *exit_qual = 0;
  2818. }
  2819. return 1;
  2820. }
  2821. }
  2822. return 0;
  2823. }
  2824. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2825. {
  2826. /*
  2827. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2828. * explicitly skip the instruction because if the HLT state is set,
  2829. * then the instruction is already executing and RIP has already been
  2830. * advanced.
  2831. */
  2832. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2833. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2834. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2835. }
  2836. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2837. {
  2838. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2839. unsigned nr = vcpu->arch.exception.nr;
  2840. bool has_error_code = vcpu->arch.exception.has_error_code;
  2841. u32 error_code = vcpu->arch.exception.error_code;
  2842. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2843. if (has_error_code) {
  2844. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2845. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2846. }
  2847. if (vmx->rmode.vm86_active) {
  2848. int inc_eip = 0;
  2849. if (kvm_exception_is_soft(nr))
  2850. inc_eip = vcpu->arch.event_exit_inst_len;
  2851. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2852. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2853. return;
  2854. }
  2855. WARN_ON_ONCE(vmx->emulation_required);
  2856. if (kvm_exception_is_soft(nr)) {
  2857. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2858. vmx->vcpu.arch.event_exit_inst_len);
  2859. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2860. } else
  2861. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2862. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2863. vmx_clear_hlt(vcpu);
  2864. }
  2865. static bool vmx_rdtscp_supported(void)
  2866. {
  2867. return cpu_has_vmx_rdtscp();
  2868. }
  2869. static bool vmx_invpcid_supported(void)
  2870. {
  2871. return cpu_has_vmx_invpcid();
  2872. }
  2873. /*
  2874. * Swap MSR entry in host/guest MSR entry array.
  2875. */
  2876. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2877. {
  2878. struct shared_msr_entry tmp;
  2879. tmp = vmx->guest_msrs[to];
  2880. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2881. vmx->guest_msrs[from] = tmp;
  2882. }
  2883. /*
  2884. * Set up the vmcs to automatically save and restore system
  2885. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2886. * mode, as fiddling with msrs is very expensive.
  2887. */
  2888. static void setup_msrs(struct vcpu_vmx *vmx)
  2889. {
  2890. int save_nmsrs, index;
  2891. save_nmsrs = 0;
  2892. #ifdef CONFIG_X86_64
  2893. if (is_long_mode(&vmx->vcpu)) {
  2894. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2895. if (index >= 0)
  2896. move_msr_up(vmx, index, save_nmsrs++);
  2897. index = __find_msr_index(vmx, MSR_LSTAR);
  2898. if (index >= 0)
  2899. move_msr_up(vmx, index, save_nmsrs++);
  2900. index = __find_msr_index(vmx, MSR_CSTAR);
  2901. if (index >= 0)
  2902. move_msr_up(vmx, index, save_nmsrs++);
  2903. /*
  2904. * MSR_STAR is only needed on long mode guests, and only
  2905. * if efer.sce is enabled.
  2906. */
  2907. index = __find_msr_index(vmx, MSR_STAR);
  2908. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2909. move_msr_up(vmx, index, save_nmsrs++);
  2910. }
  2911. #endif
  2912. index = __find_msr_index(vmx, MSR_EFER);
  2913. if (index >= 0 && update_transition_efer(vmx, index))
  2914. move_msr_up(vmx, index, save_nmsrs++);
  2915. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2916. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2917. move_msr_up(vmx, index, save_nmsrs++);
  2918. vmx->save_nmsrs = save_nmsrs;
  2919. vmx->guest_msrs_dirty = true;
  2920. if (cpu_has_vmx_msr_bitmap())
  2921. vmx_update_msr_bitmap(&vmx->vcpu);
  2922. }
  2923. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2924. {
  2925. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2926. if (is_guest_mode(vcpu) &&
  2927. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2928. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2929. return vcpu->arch.tsc_offset;
  2930. }
  2931. static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2932. {
  2933. u64 active_offset = offset;
  2934. if (is_guest_mode(vcpu)) {
  2935. /*
  2936. * We're here if L1 chose not to trap WRMSR to TSC. According
  2937. * to the spec, this should set L1's TSC; The offset that L1
  2938. * set for L2 remains unchanged, and still needs to be added
  2939. * to the newly set TSC to get L2's TSC.
  2940. */
  2941. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2942. if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING))
  2943. active_offset += vmcs12->tsc_offset;
  2944. } else {
  2945. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2946. vmcs_read64(TSC_OFFSET), offset);
  2947. }
  2948. vmcs_write64(TSC_OFFSET, active_offset);
  2949. return active_offset;
  2950. }
  2951. /*
  2952. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2953. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2954. * all guests if the "nested" module option is off, and can also be disabled
  2955. * for a single guest by disabling its VMX cpuid bit.
  2956. */
  2957. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2958. {
  2959. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2960. }
  2961. /*
  2962. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2963. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2964. * The same values should also be used to verify that vmcs12 control fields are
  2965. * valid during nested entry from L1 to L2.
  2966. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2967. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2968. * bit in the high half is on if the corresponding bit in the control field
  2969. * may be on. See also vmx_control_verify().
  2970. */
  2971. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  2972. {
  2973. if (!nested) {
  2974. memset(msrs, 0, sizeof(*msrs));
  2975. return;
  2976. }
  2977. /*
  2978. * Note that as a general rule, the high half of the MSRs (bits in
  2979. * the control fields which may be 1) should be initialized by the
  2980. * intersection of the underlying hardware's MSR (i.e., features which
  2981. * can be supported) and the list of features we want to expose -
  2982. * because they are known to be properly supported in our code.
  2983. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2984. * be set to 0, meaning that L1 may turn off any of these bits. The
  2985. * reason is that if one of these bits is necessary, it will appear
  2986. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2987. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2988. * nested_vmx_exit_reflected() will not pass related exits to L1.
  2989. * These rules have exceptions below.
  2990. */
  2991. /* pin-based controls */
  2992. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2993. msrs->pinbased_ctls_low,
  2994. msrs->pinbased_ctls_high);
  2995. msrs->pinbased_ctls_low |=
  2996. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2997. msrs->pinbased_ctls_high &=
  2998. PIN_BASED_EXT_INTR_MASK |
  2999. PIN_BASED_NMI_EXITING |
  3000. PIN_BASED_VIRTUAL_NMIS |
  3001. (apicv ? PIN_BASED_POSTED_INTR : 0);
  3002. msrs->pinbased_ctls_high |=
  3003. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3004. PIN_BASED_VMX_PREEMPTION_TIMER;
  3005. /* exit controls */
  3006. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  3007. msrs->exit_ctls_low,
  3008. msrs->exit_ctls_high);
  3009. msrs->exit_ctls_low =
  3010. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3011. msrs->exit_ctls_high &=
  3012. #ifdef CONFIG_X86_64
  3013. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  3014. #endif
  3015. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  3016. msrs->exit_ctls_high |=
  3017. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  3018. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  3019. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  3020. /* We support free control of debug control saving. */
  3021. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  3022. /* entry controls */
  3023. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  3024. msrs->entry_ctls_low,
  3025. msrs->entry_ctls_high);
  3026. msrs->entry_ctls_low =
  3027. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3028. msrs->entry_ctls_high &=
  3029. #ifdef CONFIG_X86_64
  3030. VM_ENTRY_IA32E_MODE |
  3031. #endif
  3032. VM_ENTRY_LOAD_IA32_PAT;
  3033. msrs->entry_ctls_high |=
  3034. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  3035. /* We support free control of debug control loading. */
  3036. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3037. /* cpu-based controls */
  3038. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  3039. msrs->procbased_ctls_low,
  3040. msrs->procbased_ctls_high);
  3041. msrs->procbased_ctls_low =
  3042. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3043. msrs->procbased_ctls_high &=
  3044. CPU_BASED_VIRTUAL_INTR_PENDING |
  3045. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  3046. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  3047. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  3048. CPU_BASED_CR3_STORE_EXITING |
  3049. #ifdef CONFIG_X86_64
  3050. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  3051. #endif
  3052. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  3053. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  3054. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  3055. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  3056. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3057. /*
  3058. * We can allow some features even when not supported by the
  3059. * hardware. For example, L1 can specify an MSR bitmap - and we
  3060. * can use it to avoid exits to L1 - even when L0 runs L2
  3061. * without MSR bitmaps.
  3062. */
  3063. msrs->procbased_ctls_high |=
  3064. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3065. CPU_BASED_USE_MSR_BITMAPS;
  3066. /* We support free control of CR3 access interception. */
  3067. msrs->procbased_ctls_low &=
  3068. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  3069. /*
  3070. * secondary cpu-based controls. Do not include those that
  3071. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  3072. */
  3073. if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
  3074. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  3075. msrs->secondary_ctls_low,
  3076. msrs->secondary_ctls_high);
  3077. msrs->secondary_ctls_low = 0;
  3078. msrs->secondary_ctls_high &=
  3079. SECONDARY_EXEC_DESC |
  3080. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3081. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3082. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3083. SECONDARY_EXEC_WBINVD_EXITING;
  3084. /*
  3085. * We can emulate "VMCS shadowing," even if the hardware
  3086. * doesn't support it.
  3087. */
  3088. msrs->secondary_ctls_high |=
  3089. SECONDARY_EXEC_SHADOW_VMCS;
  3090. if (enable_ept) {
  3091. /* nested EPT: emulate EPT also to L1 */
  3092. msrs->secondary_ctls_high |=
  3093. SECONDARY_EXEC_ENABLE_EPT;
  3094. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  3095. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  3096. if (cpu_has_vmx_ept_execute_only())
  3097. msrs->ept_caps |=
  3098. VMX_EPT_EXECUTE_ONLY_BIT;
  3099. msrs->ept_caps &= vmx_capability.ept;
  3100. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  3101. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  3102. VMX_EPT_1GB_PAGE_BIT;
  3103. if (enable_ept_ad_bits) {
  3104. msrs->secondary_ctls_high |=
  3105. SECONDARY_EXEC_ENABLE_PML;
  3106. msrs->ept_caps |= VMX_EPT_AD_BIT;
  3107. }
  3108. }
  3109. if (cpu_has_vmx_vmfunc()) {
  3110. msrs->secondary_ctls_high |=
  3111. SECONDARY_EXEC_ENABLE_VMFUNC;
  3112. /*
  3113. * Advertise EPTP switching unconditionally
  3114. * since we emulate it
  3115. */
  3116. if (enable_ept)
  3117. msrs->vmfunc_controls =
  3118. VMX_VMFUNC_EPTP_SWITCHING;
  3119. }
  3120. /*
  3121. * Old versions of KVM use the single-context version without
  3122. * checking for support, so declare that it is supported even
  3123. * though it is treated as global context. The alternative is
  3124. * not failing the single-context invvpid, and it is worse.
  3125. */
  3126. if (enable_vpid) {
  3127. msrs->secondary_ctls_high |=
  3128. SECONDARY_EXEC_ENABLE_VPID;
  3129. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  3130. VMX_VPID_EXTENT_SUPPORTED_MASK;
  3131. }
  3132. if (enable_unrestricted_guest)
  3133. msrs->secondary_ctls_high |=
  3134. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3135. if (flexpriority_enabled)
  3136. msrs->secondary_ctls_high |=
  3137. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3138. /* miscellaneous data */
  3139. rdmsr(MSR_IA32_VMX_MISC,
  3140. msrs->misc_low,
  3141. msrs->misc_high);
  3142. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  3143. msrs->misc_low |=
  3144. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  3145. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  3146. VMX_MISC_ACTIVITY_HLT;
  3147. msrs->misc_high = 0;
  3148. /*
  3149. * This MSR reports some information about VMX support. We
  3150. * should return information about the VMX we emulate for the
  3151. * guest, and the VMCS structure we give it - not about the
  3152. * VMX support of the underlying hardware.
  3153. */
  3154. msrs->basic =
  3155. VMCS12_REVISION |
  3156. VMX_BASIC_TRUE_CTLS |
  3157. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  3158. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  3159. if (cpu_has_vmx_basic_inout())
  3160. msrs->basic |= VMX_BASIC_INOUT;
  3161. /*
  3162. * These MSRs specify bits which the guest must keep fixed on
  3163. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  3164. * We picked the standard core2 setting.
  3165. */
  3166. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  3167. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  3168. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  3169. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  3170. /* These MSRs specify bits which the guest must keep fixed off. */
  3171. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  3172. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  3173. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  3174. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  3175. }
  3176. /*
  3177. * if fixed0[i] == 1: val[i] must be 1
  3178. * if fixed1[i] == 0: val[i] must be 0
  3179. */
  3180. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  3181. {
  3182. return ((val & fixed1) | fixed0) == val;
  3183. }
  3184. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  3185. {
  3186. return fixed_bits_valid(control, low, high);
  3187. }
  3188. static inline u64 vmx_control_msr(u32 low, u32 high)
  3189. {
  3190. return low | ((u64)high << 32);
  3191. }
  3192. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  3193. {
  3194. superset &= mask;
  3195. subset &= mask;
  3196. return (superset | subset) == superset;
  3197. }
  3198. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  3199. {
  3200. const u64 feature_and_reserved =
  3201. /* feature (except bit 48; see below) */
  3202. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  3203. /* reserved */
  3204. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  3205. u64 vmx_basic = vmx->nested.msrs.basic;
  3206. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  3207. return -EINVAL;
  3208. /*
  3209. * KVM does not emulate a version of VMX that constrains physical
  3210. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  3211. */
  3212. if (data & BIT_ULL(48))
  3213. return -EINVAL;
  3214. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  3215. vmx_basic_vmcs_revision_id(data))
  3216. return -EINVAL;
  3217. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  3218. return -EINVAL;
  3219. vmx->nested.msrs.basic = data;
  3220. return 0;
  3221. }
  3222. static int
  3223. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3224. {
  3225. u64 supported;
  3226. u32 *lowp, *highp;
  3227. switch (msr_index) {
  3228. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3229. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  3230. highp = &vmx->nested.msrs.pinbased_ctls_high;
  3231. break;
  3232. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3233. lowp = &vmx->nested.msrs.procbased_ctls_low;
  3234. highp = &vmx->nested.msrs.procbased_ctls_high;
  3235. break;
  3236. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3237. lowp = &vmx->nested.msrs.exit_ctls_low;
  3238. highp = &vmx->nested.msrs.exit_ctls_high;
  3239. break;
  3240. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3241. lowp = &vmx->nested.msrs.entry_ctls_low;
  3242. highp = &vmx->nested.msrs.entry_ctls_high;
  3243. break;
  3244. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3245. lowp = &vmx->nested.msrs.secondary_ctls_low;
  3246. highp = &vmx->nested.msrs.secondary_ctls_high;
  3247. break;
  3248. default:
  3249. BUG();
  3250. }
  3251. supported = vmx_control_msr(*lowp, *highp);
  3252. /* Check must-be-1 bits are still 1. */
  3253. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3254. return -EINVAL;
  3255. /* Check must-be-0 bits are still 0. */
  3256. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3257. return -EINVAL;
  3258. *lowp = data;
  3259. *highp = data >> 32;
  3260. return 0;
  3261. }
  3262. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3263. {
  3264. const u64 feature_and_reserved_bits =
  3265. /* feature */
  3266. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3267. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3268. /* reserved */
  3269. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3270. u64 vmx_misc;
  3271. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3272. vmx->nested.msrs.misc_high);
  3273. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3274. return -EINVAL;
  3275. if ((vmx->nested.msrs.pinbased_ctls_high &
  3276. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3277. vmx_misc_preemption_timer_rate(data) !=
  3278. vmx_misc_preemption_timer_rate(vmx_misc))
  3279. return -EINVAL;
  3280. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3281. return -EINVAL;
  3282. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3283. return -EINVAL;
  3284. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3285. return -EINVAL;
  3286. vmx->nested.msrs.misc_low = data;
  3287. vmx->nested.msrs.misc_high = data >> 32;
  3288. /*
  3289. * If L1 has read-only VM-exit information fields, use the
  3290. * less permissive vmx_vmwrite_bitmap to specify write
  3291. * permissions for the shadow VMCS.
  3292. */
  3293. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3294. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3295. return 0;
  3296. }
  3297. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3298. {
  3299. u64 vmx_ept_vpid_cap;
  3300. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3301. vmx->nested.msrs.vpid_caps);
  3302. /* Every bit is either reserved or a feature bit. */
  3303. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3304. return -EINVAL;
  3305. vmx->nested.msrs.ept_caps = data;
  3306. vmx->nested.msrs.vpid_caps = data >> 32;
  3307. return 0;
  3308. }
  3309. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3310. {
  3311. u64 *msr;
  3312. switch (msr_index) {
  3313. case MSR_IA32_VMX_CR0_FIXED0:
  3314. msr = &vmx->nested.msrs.cr0_fixed0;
  3315. break;
  3316. case MSR_IA32_VMX_CR4_FIXED0:
  3317. msr = &vmx->nested.msrs.cr4_fixed0;
  3318. break;
  3319. default:
  3320. BUG();
  3321. }
  3322. /*
  3323. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3324. * must be 1 in the restored value.
  3325. */
  3326. if (!is_bitwise_subset(data, *msr, -1ULL))
  3327. return -EINVAL;
  3328. *msr = data;
  3329. return 0;
  3330. }
  3331. /*
  3332. * Called when userspace is restoring VMX MSRs.
  3333. *
  3334. * Returns 0 on success, non-0 otherwise.
  3335. */
  3336. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3337. {
  3338. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3339. /*
  3340. * Don't allow changes to the VMX capability MSRs while the vCPU
  3341. * is in VMX operation.
  3342. */
  3343. if (vmx->nested.vmxon)
  3344. return -EBUSY;
  3345. switch (msr_index) {
  3346. case MSR_IA32_VMX_BASIC:
  3347. return vmx_restore_vmx_basic(vmx, data);
  3348. case MSR_IA32_VMX_PINBASED_CTLS:
  3349. case MSR_IA32_VMX_PROCBASED_CTLS:
  3350. case MSR_IA32_VMX_EXIT_CTLS:
  3351. case MSR_IA32_VMX_ENTRY_CTLS:
  3352. /*
  3353. * The "non-true" VMX capability MSRs are generated from the
  3354. * "true" MSRs, so we do not support restoring them directly.
  3355. *
  3356. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3357. * should restore the "true" MSRs with the must-be-1 bits
  3358. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3359. * DEFAULT SETTINGS".
  3360. */
  3361. return -EINVAL;
  3362. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3363. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3364. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3365. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3366. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3367. return vmx_restore_control_msr(vmx, msr_index, data);
  3368. case MSR_IA32_VMX_MISC:
  3369. return vmx_restore_vmx_misc(vmx, data);
  3370. case MSR_IA32_VMX_CR0_FIXED0:
  3371. case MSR_IA32_VMX_CR4_FIXED0:
  3372. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3373. case MSR_IA32_VMX_CR0_FIXED1:
  3374. case MSR_IA32_VMX_CR4_FIXED1:
  3375. /*
  3376. * These MSRs are generated based on the vCPU's CPUID, so we
  3377. * do not support restoring them directly.
  3378. */
  3379. return -EINVAL;
  3380. case MSR_IA32_VMX_EPT_VPID_CAP:
  3381. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3382. case MSR_IA32_VMX_VMCS_ENUM:
  3383. vmx->nested.msrs.vmcs_enum = data;
  3384. return 0;
  3385. default:
  3386. /*
  3387. * The rest of the VMX capability MSRs do not support restore.
  3388. */
  3389. return -EINVAL;
  3390. }
  3391. }
  3392. /* Returns 0 on success, non-0 otherwise. */
  3393. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3394. {
  3395. switch (msr_index) {
  3396. case MSR_IA32_VMX_BASIC:
  3397. *pdata = msrs->basic;
  3398. break;
  3399. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3400. case MSR_IA32_VMX_PINBASED_CTLS:
  3401. *pdata = vmx_control_msr(
  3402. msrs->pinbased_ctls_low,
  3403. msrs->pinbased_ctls_high);
  3404. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3405. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3406. break;
  3407. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3408. case MSR_IA32_VMX_PROCBASED_CTLS:
  3409. *pdata = vmx_control_msr(
  3410. msrs->procbased_ctls_low,
  3411. msrs->procbased_ctls_high);
  3412. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3413. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3414. break;
  3415. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3416. case MSR_IA32_VMX_EXIT_CTLS:
  3417. *pdata = vmx_control_msr(
  3418. msrs->exit_ctls_low,
  3419. msrs->exit_ctls_high);
  3420. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3421. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3422. break;
  3423. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3424. case MSR_IA32_VMX_ENTRY_CTLS:
  3425. *pdata = vmx_control_msr(
  3426. msrs->entry_ctls_low,
  3427. msrs->entry_ctls_high);
  3428. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3429. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3430. break;
  3431. case MSR_IA32_VMX_MISC:
  3432. *pdata = vmx_control_msr(
  3433. msrs->misc_low,
  3434. msrs->misc_high);
  3435. break;
  3436. case MSR_IA32_VMX_CR0_FIXED0:
  3437. *pdata = msrs->cr0_fixed0;
  3438. break;
  3439. case MSR_IA32_VMX_CR0_FIXED1:
  3440. *pdata = msrs->cr0_fixed1;
  3441. break;
  3442. case MSR_IA32_VMX_CR4_FIXED0:
  3443. *pdata = msrs->cr4_fixed0;
  3444. break;
  3445. case MSR_IA32_VMX_CR4_FIXED1:
  3446. *pdata = msrs->cr4_fixed1;
  3447. break;
  3448. case MSR_IA32_VMX_VMCS_ENUM:
  3449. *pdata = msrs->vmcs_enum;
  3450. break;
  3451. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3452. *pdata = vmx_control_msr(
  3453. msrs->secondary_ctls_low,
  3454. msrs->secondary_ctls_high);
  3455. break;
  3456. case MSR_IA32_VMX_EPT_VPID_CAP:
  3457. *pdata = msrs->ept_caps |
  3458. ((u64)msrs->vpid_caps << 32);
  3459. break;
  3460. case MSR_IA32_VMX_VMFUNC:
  3461. *pdata = msrs->vmfunc_controls;
  3462. break;
  3463. default:
  3464. return 1;
  3465. }
  3466. return 0;
  3467. }
  3468. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3469. uint64_t val)
  3470. {
  3471. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3472. return !(val & ~valid_bits);
  3473. }
  3474. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3475. {
  3476. switch (msr->index) {
  3477. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3478. if (!nested)
  3479. return 1;
  3480. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3481. default:
  3482. return 1;
  3483. }
  3484. return 0;
  3485. }
  3486. /*
  3487. * Reads an msr value (of 'msr_index') into 'pdata'.
  3488. * Returns 0 on success, non-0 otherwise.
  3489. * Assumes vcpu_load() was already called.
  3490. */
  3491. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3492. {
  3493. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3494. struct shared_msr_entry *msr;
  3495. switch (msr_info->index) {
  3496. #ifdef CONFIG_X86_64
  3497. case MSR_FS_BASE:
  3498. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3499. break;
  3500. case MSR_GS_BASE:
  3501. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3502. break;
  3503. case MSR_KERNEL_GS_BASE:
  3504. msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
  3505. break;
  3506. #endif
  3507. case MSR_EFER:
  3508. return kvm_get_msr_common(vcpu, msr_info);
  3509. case MSR_IA32_SPEC_CTRL:
  3510. if (!msr_info->host_initiated &&
  3511. !guest_has_spec_ctrl_msr(vcpu))
  3512. return 1;
  3513. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3514. break;
  3515. case MSR_IA32_SYSENTER_CS:
  3516. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3517. break;
  3518. case MSR_IA32_SYSENTER_EIP:
  3519. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3520. break;
  3521. case MSR_IA32_SYSENTER_ESP:
  3522. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3523. break;
  3524. case MSR_IA32_BNDCFGS:
  3525. if (!kvm_mpx_supported() ||
  3526. (!msr_info->host_initiated &&
  3527. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3528. return 1;
  3529. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3530. break;
  3531. case MSR_IA32_MCG_EXT_CTL:
  3532. if (!msr_info->host_initiated &&
  3533. !(vmx->msr_ia32_feature_control &
  3534. FEATURE_CONTROL_LMCE))
  3535. return 1;
  3536. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3537. break;
  3538. case MSR_IA32_FEATURE_CONTROL:
  3539. msr_info->data = vmx->msr_ia32_feature_control;
  3540. break;
  3541. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3542. if (!nested_vmx_allowed(vcpu))
  3543. return 1;
  3544. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3545. &msr_info->data);
  3546. case MSR_IA32_XSS:
  3547. if (!vmx_xsaves_supported() ||
  3548. (!msr_info->host_initiated &&
  3549. !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  3550. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
  3551. return 1;
  3552. msr_info->data = vcpu->arch.ia32_xss;
  3553. break;
  3554. case MSR_TSC_AUX:
  3555. if (!msr_info->host_initiated &&
  3556. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3557. return 1;
  3558. /* Otherwise falls through */
  3559. default:
  3560. msr = find_msr_entry(vmx, msr_info->index);
  3561. if (msr) {
  3562. msr_info->data = msr->data;
  3563. break;
  3564. }
  3565. return kvm_get_msr_common(vcpu, msr_info);
  3566. }
  3567. return 0;
  3568. }
  3569. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3570. /*
  3571. * Writes msr value into into the appropriate "register".
  3572. * Returns 0 on success, non-0 otherwise.
  3573. * Assumes vcpu_load() was already called.
  3574. */
  3575. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3576. {
  3577. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3578. struct shared_msr_entry *msr;
  3579. int ret = 0;
  3580. u32 msr_index = msr_info->index;
  3581. u64 data = msr_info->data;
  3582. switch (msr_index) {
  3583. case MSR_EFER:
  3584. ret = kvm_set_msr_common(vcpu, msr_info);
  3585. break;
  3586. #ifdef CONFIG_X86_64
  3587. case MSR_FS_BASE:
  3588. vmx_segment_cache_clear(vmx);
  3589. vmcs_writel(GUEST_FS_BASE, data);
  3590. break;
  3591. case MSR_GS_BASE:
  3592. vmx_segment_cache_clear(vmx);
  3593. vmcs_writel(GUEST_GS_BASE, data);
  3594. break;
  3595. case MSR_KERNEL_GS_BASE:
  3596. vmx_write_guest_kernel_gs_base(vmx, data);
  3597. break;
  3598. #endif
  3599. case MSR_IA32_SYSENTER_CS:
  3600. vmcs_write32(GUEST_SYSENTER_CS, data);
  3601. break;
  3602. case MSR_IA32_SYSENTER_EIP:
  3603. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3604. break;
  3605. case MSR_IA32_SYSENTER_ESP:
  3606. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3607. break;
  3608. case MSR_IA32_BNDCFGS:
  3609. if (!kvm_mpx_supported() ||
  3610. (!msr_info->host_initiated &&
  3611. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3612. return 1;
  3613. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3614. (data & MSR_IA32_BNDCFGS_RSVD))
  3615. return 1;
  3616. vmcs_write64(GUEST_BNDCFGS, data);
  3617. break;
  3618. case MSR_IA32_SPEC_CTRL:
  3619. if (!msr_info->host_initiated &&
  3620. !guest_has_spec_ctrl_msr(vcpu))
  3621. return 1;
  3622. /* The STIBP bit doesn't fault even if it's not advertised */
  3623. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3624. return 1;
  3625. vmx->spec_ctrl = data;
  3626. if (!data)
  3627. break;
  3628. /*
  3629. * For non-nested:
  3630. * When it's written (to non-zero) for the first time, pass
  3631. * it through.
  3632. *
  3633. * For nested:
  3634. * The handling of the MSR bitmap for L2 guests is done in
  3635. * nested_vmx_merge_msr_bitmap. We should not touch the
  3636. * vmcs02.msr_bitmap here since it gets completely overwritten
  3637. * in the merging. We update the vmcs01 here for L1 as well
  3638. * since it will end up touching the MSR anyway now.
  3639. */
  3640. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3641. MSR_IA32_SPEC_CTRL,
  3642. MSR_TYPE_RW);
  3643. break;
  3644. case MSR_IA32_PRED_CMD:
  3645. if (!msr_info->host_initiated &&
  3646. !guest_has_pred_cmd_msr(vcpu))
  3647. return 1;
  3648. if (data & ~PRED_CMD_IBPB)
  3649. return 1;
  3650. if (!data)
  3651. break;
  3652. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3653. /*
  3654. * For non-nested:
  3655. * When it's written (to non-zero) for the first time, pass
  3656. * it through.
  3657. *
  3658. * For nested:
  3659. * The handling of the MSR bitmap for L2 guests is done in
  3660. * nested_vmx_merge_msr_bitmap. We should not touch the
  3661. * vmcs02.msr_bitmap here since it gets completely overwritten
  3662. * in the merging.
  3663. */
  3664. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3665. MSR_TYPE_W);
  3666. break;
  3667. case MSR_IA32_CR_PAT:
  3668. if (!kvm_pat_valid(data))
  3669. return 1;
  3670. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3671. vmcs_write64(GUEST_IA32_PAT, data);
  3672. vcpu->arch.pat = data;
  3673. break;
  3674. }
  3675. ret = kvm_set_msr_common(vcpu, msr_info);
  3676. break;
  3677. case MSR_IA32_TSC_ADJUST:
  3678. ret = kvm_set_msr_common(vcpu, msr_info);
  3679. break;
  3680. case MSR_IA32_MCG_EXT_CTL:
  3681. if ((!msr_info->host_initiated &&
  3682. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3683. FEATURE_CONTROL_LMCE)) ||
  3684. (data & ~MCG_EXT_CTL_LMCE_EN))
  3685. return 1;
  3686. vcpu->arch.mcg_ext_ctl = data;
  3687. break;
  3688. case MSR_IA32_FEATURE_CONTROL:
  3689. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3690. (to_vmx(vcpu)->msr_ia32_feature_control &
  3691. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3692. return 1;
  3693. vmx->msr_ia32_feature_control = data;
  3694. if (msr_info->host_initiated && data == 0)
  3695. vmx_leave_nested(vcpu);
  3696. break;
  3697. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3698. if (!msr_info->host_initiated)
  3699. return 1; /* they are read-only */
  3700. if (!nested_vmx_allowed(vcpu))
  3701. return 1;
  3702. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3703. case MSR_IA32_XSS:
  3704. if (!vmx_xsaves_supported() ||
  3705. (!msr_info->host_initiated &&
  3706. !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  3707. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
  3708. return 1;
  3709. /*
  3710. * The only supported bit as of Skylake is bit 8, but
  3711. * it is not supported on KVM.
  3712. */
  3713. if (data != 0)
  3714. return 1;
  3715. vcpu->arch.ia32_xss = data;
  3716. if (vcpu->arch.ia32_xss != host_xss)
  3717. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3718. vcpu->arch.ia32_xss, host_xss, false);
  3719. else
  3720. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3721. break;
  3722. case MSR_TSC_AUX:
  3723. if (!msr_info->host_initiated &&
  3724. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3725. return 1;
  3726. /* Check reserved bit, higher 32 bits should be zero */
  3727. if ((data >> 32) != 0)
  3728. return 1;
  3729. /* Otherwise falls through */
  3730. default:
  3731. msr = find_msr_entry(vmx, msr_index);
  3732. if (msr) {
  3733. u64 old_msr_data = msr->data;
  3734. msr->data = data;
  3735. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3736. preempt_disable();
  3737. ret = kvm_set_shared_msr(msr->index, msr->data,
  3738. msr->mask);
  3739. preempt_enable();
  3740. if (ret)
  3741. msr->data = old_msr_data;
  3742. }
  3743. break;
  3744. }
  3745. ret = kvm_set_msr_common(vcpu, msr_info);
  3746. }
  3747. return ret;
  3748. }
  3749. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3750. {
  3751. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3752. switch (reg) {
  3753. case VCPU_REGS_RSP:
  3754. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3755. break;
  3756. case VCPU_REGS_RIP:
  3757. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3758. break;
  3759. case VCPU_EXREG_PDPTR:
  3760. if (enable_ept)
  3761. ept_save_pdptrs(vcpu);
  3762. break;
  3763. default:
  3764. break;
  3765. }
  3766. }
  3767. static __init int cpu_has_kvm_support(void)
  3768. {
  3769. return cpu_has_vmx();
  3770. }
  3771. static __init int vmx_disabled_by_bios(void)
  3772. {
  3773. u64 msr;
  3774. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3775. if (msr & FEATURE_CONTROL_LOCKED) {
  3776. /* launched w/ TXT and VMX disabled */
  3777. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3778. && tboot_enabled())
  3779. return 1;
  3780. /* launched w/o TXT and VMX only enabled w/ TXT */
  3781. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3782. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3783. && !tboot_enabled()) {
  3784. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3785. "activate TXT before enabling KVM\n");
  3786. return 1;
  3787. }
  3788. /* launched w/o TXT and VMX disabled */
  3789. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3790. && !tboot_enabled())
  3791. return 1;
  3792. }
  3793. return 0;
  3794. }
  3795. static void kvm_cpu_vmxon(u64 addr)
  3796. {
  3797. cr4_set_bits(X86_CR4_VMXE);
  3798. intel_pt_handle_vmx(1);
  3799. asm volatile (ASM_VMX_VMXON_RAX
  3800. : : "a"(&addr), "m"(addr)
  3801. : "memory", "cc");
  3802. }
  3803. static int hardware_enable(void)
  3804. {
  3805. int cpu = raw_smp_processor_id();
  3806. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3807. u64 old, test_bits;
  3808. if (cr4_read_shadow() & X86_CR4_VMXE)
  3809. return -EBUSY;
  3810. /*
  3811. * This can happen if we hot-added a CPU but failed to allocate
  3812. * VP assist page for it.
  3813. */
  3814. if (static_branch_unlikely(&enable_evmcs) &&
  3815. !hv_get_vp_assist_page(cpu))
  3816. return -EFAULT;
  3817. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3818. test_bits = FEATURE_CONTROL_LOCKED;
  3819. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3820. if (tboot_enabled())
  3821. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3822. if ((old & test_bits) != test_bits) {
  3823. /* enable and lock */
  3824. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3825. }
  3826. kvm_cpu_vmxon(phys_addr);
  3827. if (enable_ept)
  3828. ept_sync_global();
  3829. return 0;
  3830. }
  3831. static void vmclear_local_loaded_vmcss(void)
  3832. {
  3833. int cpu = raw_smp_processor_id();
  3834. struct loaded_vmcs *v, *n;
  3835. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3836. loaded_vmcss_on_cpu_link)
  3837. __loaded_vmcs_clear(v);
  3838. }
  3839. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3840. * tricks.
  3841. */
  3842. static void kvm_cpu_vmxoff(void)
  3843. {
  3844. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3845. intel_pt_handle_vmx(0);
  3846. cr4_clear_bits(X86_CR4_VMXE);
  3847. }
  3848. static void hardware_disable(void)
  3849. {
  3850. vmclear_local_loaded_vmcss();
  3851. kvm_cpu_vmxoff();
  3852. }
  3853. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3854. u32 msr, u32 *result)
  3855. {
  3856. u32 vmx_msr_low, vmx_msr_high;
  3857. u32 ctl = ctl_min | ctl_opt;
  3858. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3859. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3860. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3861. /* Ensure minimum (required) set of control bits are supported. */
  3862. if (ctl_min & ~ctl)
  3863. return -EIO;
  3864. *result = ctl;
  3865. return 0;
  3866. }
  3867. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3868. {
  3869. u32 vmx_msr_low, vmx_msr_high;
  3870. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3871. return vmx_msr_high & ctl;
  3872. }
  3873. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3874. {
  3875. u32 vmx_msr_low, vmx_msr_high;
  3876. u32 min, opt, min2, opt2;
  3877. u32 _pin_based_exec_control = 0;
  3878. u32 _cpu_based_exec_control = 0;
  3879. u32 _cpu_based_2nd_exec_control = 0;
  3880. u32 _vmexit_control = 0;
  3881. u32 _vmentry_control = 0;
  3882. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3883. min = CPU_BASED_HLT_EXITING |
  3884. #ifdef CONFIG_X86_64
  3885. CPU_BASED_CR8_LOAD_EXITING |
  3886. CPU_BASED_CR8_STORE_EXITING |
  3887. #endif
  3888. CPU_BASED_CR3_LOAD_EXITING |
  3889. CPU_BASED_CR3_STORE_EXITING |
  3890. CPU_BASED_UNCOND_IO_EXITING |
  3891. CPU_BASED_MOV_DR_EXITING |
  3892. CPU_BASED_USE_TSC_OFFSETING |
  3893. CPU_BASED_MWAIT_EXITING |
  3894. CPU_BASED_MONITOR_EXITING |
  3895. CPU_BASED_INVLPG_EXITING |
  3896. CPU_BASED_RDPMC_EXITING;
  3897. opt = CPU_BASED_TPR_SHADOW |
  3898. CPU_BASED_USE_MSR_BITMAPS |
  3899. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3900. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3901. &_cpu_based_exec_control) < 0)
  3902. return -EIO;
  3903. #ifdef CONFIG_X86_64
  3904. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3905. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3906. ~CPU_BASED_CR8_STORE_EXITING;
  3907. #endif
  3908. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3909. min2 = 0;
  3910. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3911. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3912. SECONDARY_EXEC_WBINVD_EXITING |
  3913. SECONDARY_EXEC_ENABLE_VPID |
  3914. SECONDARY_EXEC_ENABLE_EPT |
  3915. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3916. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3917. SECONDARY_EXEC_DESC |
  3918. SECONDARY_EXEC_RDTSCP |
  3919. SECONDARY_EXEC_ENABLE_INVPCID |
  3920. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3921. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3922. SECONDARY_EXEC_SHADOW_VMCS |
  3923. SECONDARY_EXEC_XSAVES |
  3924. SECONDARY_EXEC_RDSEED_EXITING |
  3925. SECONDARY_EXEC_RDRAND_EXITING |
  3926. SECONDARY_EXEC_ENABLE_PML |
  3927. SECONDARY_EXEC_TSC_SCALING |
  3928. SECONDARY_EXEC_ENABLE_VMFUNC |
  3929. SECONDARY_EXEC_ENCLS_EXITING;
  3930. if (adjust_vmx_controls(min2, opt2,
  3931. MSR_IA32_VMX_PROCBASED_CTLS2,
  3932. &_cpu_based_2nd_exec_control) < 0)
  3933. return -EIO;
  3934. }
  3935. #ifndef CONFIG_X86_64
  3936. if (!(_cpu_based_2nd_exec_control &
  3937. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3938. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3939. #endif
  3940. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3941. _cpu_based_2nd_exec_control &= ~(
  3942. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3943. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3944. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3945. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3946. &vmx_capability.ept, &vmx_capability.vpid);
  3947. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3948. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3949. enabled */
  3950. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3951. CPU_BASED_CR3_STORE_EXITING |
  3952. CPU_BASED_INVLPG_EXITING);
  3953. } else if (vmx_capability.ept) {
  3954. vmx_capability.ept = 0;
  3955. pr_warn_once("EPT CAP should not exist if not support "
  3956. "1-setting enable EPT VM-execution control\n");
  3957. }
  3958. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3959. vmx_capability.vpid) {
  3960. vmx_capability.vpid = 0;
  3961. pr_warn_once("VPID CAP should not exist if not support "
  3962. "1-setting enable VPID VM-execution control\n");
  3963. }
  3964. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3965. #ifdef CONFIG_X86_64
  3966. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  3967. #endif
  3968. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  3969. VM_EXIT_CLEAR_BNDCFGS;
  3970. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  3971. &_vmexit_control) < 0)
  3972. return -EIO;
  3973. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  3974. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  3975. PIN_BASED_VMX_PREEMPTION_TIMER;
  3976. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  3977. &_pin_based_exec_control) < 0)
  3978. return -EIO;
  3979. if (cpu_has_broken_vmx_preemption_timer())
  3980. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  3981. if (!(_cpu_based_2nd_exec_control &
  3982. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  3983. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  3984. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3985. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  3986. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  3987. &_vmentry_control) < 0)
  3988. return -EIO;
  3989. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  3990. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  3991. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  3992. return -EIO;
  3993. #ifdef CONFIG_X86_64
  3994. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  3995. if (vmx_msr_high & (1u<<16))
  3996. return -EIO;
  3997. #endif
  3998. /* Require Write-Back (WB) memory type for VMCS accesses. */
  3999. if (((vmx_msr_high >> 18) & 15) != 6)
  4000. return -EIO;
  4001. vmcs_conf->size = vmx_msr_high & 0x1fff;
  4002. vmcs_conf->order = get_order(vmcs_conf->size);
  4003. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  4004. vmcs_conf->revision_id = vmx_msr_low;
  4005. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  4006. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  4007. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  4008. vmcs_conf->vmexit_ctrl = _vmexit_control;
  4009. vmcs_conf->vmentry_ctrl = _vmentry_control;
  4010. if (static_branch_unlikely(&enable_evmcs))
  4011. evmcs_sanitize_exec_ctrls(vmcs_conf);
  4012. cpu_has_load_ia32_efer =
  4013. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4014. VM_ENTRY_LOAD_IA32_EFER)
  4015. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4016. VM_EXIT_LOAD_IA32_EFER);
  4017. cpu_has_load_perf_global_ctrl =
  4018. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4019. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  4020. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4021. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  4022. /*
  4023. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  4024. * but due to errata below it can't be used. Workaround is to use
  4025. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  4026. *
  4027. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  4028. *
  4029. * AAK155 (model 26)
  4030. * AAP115 (model 30)
  4031. * AAT100 (model 37)
  4032. * BC86,AAY89,BD102 (model 44)
  4033. * BA97 (model 46)
  4034. *
  4035. */
  4036. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  4037. switch (boot_cpu_data.x86_model) {
  4038. case 26:
  4039. case 30:
  4040. case 37:
  4041. case 44:
  4042. case 46:
  4043. cpu_has_load_perf_global_ctrl = false;
  4044. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  4045. "does not work properly. Using workaround\n");
  4046. break;
  4047. default:
  4048. break;
  4049. }
  4050. }
  4051. if (boot_cpu_has(X86_FEATURE_XSAVES))
  4052. rdmsrl(MSR_IA32_XSS, host_xss);
  4053. return 0;
  4054. }
  4055. static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
  4056. {
  4057. int node = cpu_to_node(cpu);
  4058. struct page *pages;
  4059. struct vmcs *vmcs;
  4060. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  4061. if (!pages)
  4062. return NULL;
  4063. vmcs = page_address(pages);
  4064. memset(vmcs, 0, vmcs_config.size);
  4065. /* KVM supports Enlightened VMCS v1 only */
  4066. if (static_branch_unlikely(&enable_evmcs))
  4067. vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
  4068. else
  4069. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4070. if (shadow)
  4071. vmcs->hdr.shadow_vmcs = 1;
  4072. return vmcs;
  4073. }
  4074. static void free_vmcs(struct vmcs *vmcs)
  4075. {
  4076. free_pages((unsigned long)vmcs, vmcs_config.order);
  4077. }
  4078. /*
  4079. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  4080. */
  4081. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4082. {
  4083. if (!loaded_vmcs->vmcs)
  4084. return;
  4085. loaded_vmcs_clear(loaded_vmcs);
  4086. free_vmcs(loaded_vmcs->vmcs);
  4087. loaded_vmcs->vmcs = NULL;
  4088. if (loaded_vmcs->msr_bitmap)
  4089. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  4090. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  4091. }
  4092. static struct vmcs *alloc_vmcs(bool shadow)
  4093. {
  4094. return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
  4095. }
  4096. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4097. {
  4098. loaded_vmcs->vmcs = alloc_vmcs(false);
  4099. if (!loaded_vmcs->vmcs)
  4100. return -ENOMEM;
  4101. loaded_vmcs->shadow_vmcs = NULL;
  4102. loaded_vmcs_init(loaded_vmcs);
  4103. if (cpu_has_vmx_msr_bitmap()) {
  4104. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  4105. if (!loaded_vmcs->msr_bitmap)
  4106. goto out_vmcs;
  4107. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  4108. if (IS_ENABLED(CONFIG_HYPERV) &&
  4109. static_branch_unlikely(&enable_evmcs) &&
  4110. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  4111. struct hv_enlightened_vmcs *evmcs =
  4112. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  4113. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  4114. }
  4115. }
  4116. memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
  4117. return 0;
  4118. out_vmcs:
  4119. free_loaded_vmcs(loaded_vmcs);
  4120. return -ENOMEM;
  4121. }
  4122. static void free_kvm_area(void)
  4123. {
  4124. int cpu;
  4125. for_each_possible_cpu(cpu) {
  4126. free_vmcs(per_cpu(vmxarea, cpu));
  4127. per_cpu(vmxarea, cpu) = NULL;
  4128. }
  4129. }
  4130. enum vmcs_field_width {
  4131. VMCS_FIELD_WIDTH_U16 = 0,
  4132. VMCS_FIELD_WIDTH_U64 = 1,
  4133. VMCS_FIELD_WIDTH_U32 = 2,
  4134. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  4135. };
  4136. static inline int vmcs_field_width(unsigned long field)
  4137. {
  4138. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4139. return VMCS_FIELD_WIDTH_U32;
  4140. return (field >> 13) & 0x3 ;
  4141. }
  4142. static inline int vmcs_field_readonly(unsigned long field)
  4143. {
  4144. return (((field >> 10) & 0x3) == 1);
  4145. }
  4146. static void init_vmcs_shadow_fields(void)
  4147. {
  4148. int i, j;
  4149. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  4150. u16 field = shadow_read_only_fields[i];
  4151. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4152. (i + 1 == max_shadow_read_only_fields ||
  4153. shadow_read_only_fields[i + 1] != field + 1))
  4154. pr_err("Missing field from shadow_read_only_field %x\n",
  4155. field + 1);
  4156. clear_bit(field, vmx_vmread_bitmap);
  4157. #ifdef CONFIG_X86_64
  4158. if (field & 1)
  4159. continue;
  4160. #endif
  4161. if (j < i)
  4162. shadow_read_only_fields[j] = field;
  4163. j++;
  4164. }
  4165. max_shadow_read_only_fields = j;
  4166. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  4167. u16 field = shadow_read_write_fields[i];
  4168. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4169. (i + 1 == max_shadow_read_write_fields ||
  4170. shadow_read_write_fields[i + 1] != field + 1))
  4171. pr_err("Missing field from shadow_read_write_field %x\n",
  4172. field + 1);
  4173. /*
  4174. * PML and the preemption timer can be emulated, but the
  4175. * processor cannot vmwrite to fields that don't exist
  4176. * on bare metal.
  4177. */
  4178. switch (field) {
  4179. case GUEST_PML_INDEX:
  4180. if (!cpu_has_vmx_pml())
  4181. continue;
  4182. break;
  4183. case VMX_PREEMPTION_TIMER_VALUE:
  4184. if (!cpu_has_vmx_preemption_timer())
  4185. continue;
  4186. break;
  4187. case GUEST_INTR_STATUS:
  4188. if (!cpu_has_vmx_apicv())
  4189. continue;
  4190. break;
  4191. default:
  4192. break;
  4193. }
  4194. clear_bit(field, vmx_vmwrite_bitmap);
  4195. clear_bit(field, vmx_vmread_bitmap);
  4196. #ifdef CONFIG_X86_64
  4197. if (field & 1)
  4198. continue;
  4199. #endif
  4200. if (j < i)
  4201. shadow_read_write_fields[j] = field;
  4202. j++;
  4203. }
  4204. max_shadow_read_write_fields = j;
  4205. }
  4206. static __init int alloc_kvm_area(void)
  4207. {
  4208. int cpu;
  4209. for_each_possible_cpu(cpu) {
  4210. struct vmcs *vmcs;
  4211. vmcs = alloc_vmcs_cpu(false, cpu);
  4212. if (!vmcs) {
  4213. free_kvm_area();
  4214. return -ENOMEM;
  4215. }
  4216. /*
  4217. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  4218. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  4219. * revision_id reported by MSR_IA32_VMX_BASIC.
  4220. *
  4221. * However, even though not explictly documented by
  4222. * TLFS, VMXArea passed as VMXON argument should
  4223. * still be marked with revision_id reported by
  4224. * physical CPU.
  4225. */
  4226. if (static_branch_unlikely(&enable_evmcs))
  4227. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4228. per_cpu(vmxarea, cpu) = vmcs;
  4229. }
  4230. return 0;
  4231. }
  4232. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4233. struct kvm_segment *save)
  4234. {
  4235. if (!emulate_invalid_guest_state) {
  4236. /*
  4237. * CS and SS RPL should be equal during guest entry according
  4238. * to VMX spec, but in reality it is not always so. Since vcpu
  4239. * is in the middle of the transition from real mode to
  4240. * protected mode it is safe to assume that RPL 0 is a good
  4241. * default value.
  4242. */
  4243. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4244. save->selector &= ~SEGMENT_RPL_MASK;
  4245. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4246. save->s = 1;
  4247. }
  4248. vmx_set_segment(vcpu, save, seg);
  4249. }
  4250. static void enter_pmode(struct kvm_vcpu *vcpu)
  4251. {
  4252. unsigned long flags;
  4253. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4254. /*
  4255. * Update real mode segment cache. It may be not up-to-date if sement
  4256. * register was written while vcpu was in a guest mode.
  4257. */
  4258. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4259. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4260. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4261. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4262. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4263. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4264. vmx->rmode.vm86_active = 0;
  4265. vmx_segment_cache_clear(vmx);
  4266. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4267. flags = vmcs_readl(GUEST_RFLAGS);
  4268. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4269. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4270. vmcs_writel(GUEST_RFLAGS, flags);
  4271. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4272. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4273. update_exception_bitmap(vcpu);
  4274. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4275. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4276. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4277. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4278. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4279. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4280. }
  4281. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4282. {
  4283. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4284. struct kvm_segment var = *save;
  4285. var.dpl = 0x3;
  4286. if (seg == VCPU_SREG_CS)
  4287. var.type = 0x3;
  4288. if (!emulate_invalid_guest_state) {
  4289. var.selector = var.base >> 4;
  4290. var.base = var.base & 0xffff0;
  4291. var.limit = 0xffff;
  4292. var.g = 0;
  4293. var.db = 0;
  4294. var.present = 1;
  4295. var.s = 1;
  4296. var.l = 0;
  4297. var.unusable = 0;
  4298. var.type = 0x3;
  4299. var.avl = 0;
  4300. if (save->base & 0xf)
  4301. printk_once(KERN_WARNING "kvm: segment base is not "
  4302. "paragraph aligned when entering "
  4303. "protected mode (seg=%d)", seg);
  4304. }
  4305. vmcs_write16(sf->selector, var.selector);
  4306. vmcs_writel(sf->base, var.base);
  4307. vmcs_write32(sf->limit, var.limit);
  4308. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4309. }
  4310. static void enter_rmode(struct kvm_vcpu *vcpu)
  4311. {
  4312. unsigned long flags;
  4313. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4314. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4315. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4316. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4317. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4318. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4319. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4320. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4321. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4322. vmx->rmode.vm86_active = 1;
  4323. /*
  4324. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4325. * vcpu. Warn the user that an update is overdue.
  4326. */
  4327. if (!kvm_vmx->tss_addr)
  4328. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4329. "called before entering vcpu\n");
  4330. vmx_segment_cache_clear(vmx);
  4331. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4332. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4333. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4334. flags = vmcs_readl(GUEST_RFLAGS);
  4335. vmx->rmode.save_rflags = flags;
  4336. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4337. vmcs_writel(GUEST_RFLAGS, flags);
  4338. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4339. update_exception_bitmap(vcpu);
  4340. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4341. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4342. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4343. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4344. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4345. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4346. kvm_mmu_reset_context(vcpu);
  4347. }
  4348. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4349. {
  4350. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4351. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4352. if (!msr)
  4353. return;
  4354. vcpu->arch.efer = efer;
  4355. if (efer & EFER_LMA) {
  4356. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4357. msr->data = efer;
  4358. } else {
  4359. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4360. msr->data = efer & ~EFER_LME;
  4361. }
  4362. setup_msrs(vmx);
  4363. }
  4364. #ifdef CONFIG_X86_64
  4365. static void enter_lmode(struct kvm_vcpu *vcpu)
  4366. {
  4367. u32 guest_tr_ar;
  4368. vmx_segment_cache_clear(to_vmx(vcpu));
  4369. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4370. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4371. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4372. __func__);
  4373. vmcs_write32(GUEST_TR_AR_BYTES,
  4374. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4375. | VMX_AR_TYPE_BUSY_64_TSS);
  4376. }
  4377. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4378. }
  4379. static void exit_lmode(struct kvm_vcpu *vcpu)
  4380. {
  4381. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4382. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4383. }
  4384. #endif
  4385. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4386. bool invalidate_gpa)
  4387. {
  4388. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4389. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  4390. return;
  4391. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  4392. } else {
  4393. vpid_sync_context(vpid);
  4394. }
  4395. }
  4396. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4397. {
  4398. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4399. }
  4400. static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
  4401. {
  4402. int vpid = to_vmx(vcpu)->vpid;
  4403. if (!vpid_sync_vcpu_addr(vpid, addr))
  4404. vpid_sync_context(vpid);
  4405. /*
  4406. * If VPIDs are not supported or enabled, then the above is a no-op.
  4407. * But we don't really need a TLB flush in that case anyway, because
  4408. * each VM entry/exit includes an implicit flush when VPID is 0.
  4409. */
  4410. }
  4411. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4412. {
  4413. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4414. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4415. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4416. }
  4417. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4418. {
  4419. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4420. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4421. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4422. }
  4423. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4424. {
  4425. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4426. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4427. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4428. }
  4429. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4430. {
  4431. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4432. if (!test_bit(VCPU_EXREG_PDPTR,
  4433. (unsigned long *)&vcpu->arch.regs_dirty))
  4434. return;
  4435. if (is_pae_paging(vcpu)) {
  4436. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4437. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4438. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4439. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4440. }
  4441. }
  4442. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4443. {
  4444. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4445. if (is_pae_paging(vcpu)) {
  4446. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4447. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4448. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4449. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4450. }
  4451. __set_bit(VCPU_EXREG_PDPTR,
  4452. (unsigned long *)&vcpu->arch.regs_avail);
  4453. __set_bit(VCPU_EXREG_PDPTR,
  4454. (unsigned long *)&vcpu->arch.regs_dirty);
  4455. }
  4456. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4457. {
  4458. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4459. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4460. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4461. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4462. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4463. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4464. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4465. return fixed_bits_valid(val, fixed0, fixed1);
  4466. }
  4467. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4468. {
  4469. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4470. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4471. return fixed_bits_valid(val, fixed0, fixed1);
  4472. }
  4473. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4474. {
  4475. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4476. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4477. return fixed_bits_valid(val, fixed0, fixed1);
  4478. }
  4479. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4480. #define nested_guest_cr4_valid nested_cr4_valid
  4481. #define nested_host_cr4_valid nested_cr4_valid
  4482. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4483. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4484. unsigned long cr0,
  4485. struct kvm_vcpu *vcpu)
  4486. {
  4487. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4488. vmx_decache_cr3(vcpu);
  4489. if (!(cr0 & X86_CR0_PG)) {
  4490. /* From paging/starting to nonpaging */
  4491. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4492. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4493. (CPU_BASED_CR3_LOAD_EXITING |
  4494. CPU_BASED_CR3_STORE_EXITING));
  4495. vcpu->arch.cr0 = cr0;
  4496. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4497. } else if (!is_paging(vcpu)) {
  4498. /* From nonpaging to paging */
  4499. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4500. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4501. ~(CPU_BASED_CR3_LOAD_EXITING |
  4502. CPU_BASED_CR3_STORE_EXITING));
  4503. vcpu->arch.cr0 = cr0;
  4504. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4505. }
  4506. if (!(cr0 & X86_CR0_WP))
  4507. *hw_cr0 &= ~X86_CR0_WP;
  4508. }
  4509. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4510. {
  4511. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4512. unsigned long hw_cr0;
  4513. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  4514. if (enable_unrestricted_guest)
  4515. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4516. else {
  4517. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4518. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4519. enter_pmode(vcpu);
  4520. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4521. enter_rmode(vcpu);
  4522. }
  4523. #ifdef CONFIG_X86_64
  4524. if (vcpu->arch.efer & EFER_LME) {
  4525. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4526. enter_lmode(vcpu);
  4527. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4528. exit_lmode(vcpu);
  4529. }
  4530. #endif
  4531. if (enable_ept && !enable_unrestricted_guest)
  4532. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4533. vmcs_writel(CR0_READ_SHADOW, cr0);
  4534. vmcs_writel(GUEST_CR0, hw_cr0);
  4535. vcpu->arch.cr0 = cr0;
  4536. /* depends on vcpu->arch.cr0 to be set to a new value */
  4537. vmx->emulation_required = emulation_required(vcpu);
  4538. }
  4539. static int get_ept_level(struct kvm_vcpu *vcpu)
  4540. {
  4541. /* Nested EPT currently only supports 4-level walks. */
  4542. if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
  4543. return 4;
  4544. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4545. return 5;
  4546. return 4;
  4547. }
  4548. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4549. {
  4550. u64 eptp = VMX_EPTP_MT_WB;
  4551. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4552. if (enable_ept_ad_bits &&
  4553. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4554. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4555. eptp |= (root_hpa & PAGE_MASK);
  4556. return eptp;
  4557. }
  4558. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4559. {
  4560. struct kvm *kvm = vcpu->kvm;
  4561. unsigned long guest_cr3;
  4562. u64 eptp;
  4563. guest_cr3 = cr3;
  4564. if (enable_ept) {
  4565. eptp = construct_eptp(vcpu, cr3);
  4566. vmcs_write64(EPT_POINTER, eptp);
  4567. if (kvm_x86_ops->tlb_remote_flush) {
  4568. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4569. to_vmx(vcpu)->ept_pointer = eptp;
  4570. to_kvm_vmx(kvm)->ept_pointers_match
  4571. = EPT_POINTERS_CHECK;
  4572. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4573. }
  4574. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4575. is_guest_mode(vcpu))
  4576. guest_cr3 = kvm_read_cr3(vcpu);
  4577. else
  4578. guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
  4579. ept_load_pdptrs(vcpu);
  4580. }
  4581. vmcs_writel(GUEST_CR3, guest_cr3);
  4582. }
  4583. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4584. {
  4585. /*
  4586. * Pass through host's Machine Check Enable value to hw_cr4, which
  4587. * is in force while we are in guest mode. Do not let guests control
  4588. * this bit, even if host CR4.MCE == 0.
  4589. */
  4590. unsigned long hw_cr4;
  4591. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4592. if (enable_unrestricted_guest)
  4593. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4594. else if (to_vmx(vcpu)->rmode.vm86_active)
  4595. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4596. else
  4597. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4598. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4599. if (cr4 & X86_CR4_UMIP) {
  4600. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4601. SECONDARY_EXEC_DESC);
  4602. hw_cr4 &= ~X86_CR4_UMIP;
  4603. } else if (!is_guest_mode(vcpu) ||
  4604. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4605. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4606. SECONDARY_EXEC_DESC);
  4607. }
  4608. if (cr4 & X86_CR4_VMXE) {
  4609. /*
  4610. * To use VMXON (and later other VMX instructions), a guest
  4611. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4612. * So basically the check on whether to allow nested VMX
  4613. * is here. We operate under the default treatment of SMM,
  4614. * so VMX cannot be enabled under SMM.
  4615. */
  4616. if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
  4617. return 1;
  4618. }
  4619. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4620. return 1;
  4621. vcpu->arch.cr4 = cr4;
  4622. if (!enable_unrestricted_guest) {
  4623. if (enable_ept) {
  4624. if (!is_paging(vcpu)) {
  4625. hw_cr4 &= ~X86_CR4_PAE;
  4626. hw_cr4 |= X86_CR4_PSE;
  4627. } else if (!(cr4 & X86_CR4_PAE)) {
  4628. hw_cr4 &= ~X86_CR4_PAE;
  4629. }
  4630. }
  4631. /*
  4632. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4633. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4634. * to be manually disabled when guest switches to non-paging
  4635. * mode.
  4636. *
  4637. * If !enable_unrestricted_guest, the CPU is always running
  4638. * with CR0.PG=1 and CR4 needs to be modified.
  4639. * If enable_unrestricted_guest, the CPU automatically
  4640. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4641. */
  4642. if (!is_paging(vcpu))
  4643. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4644. }
  4645. vmcs_writel(CR4_READ_SHADOW, cr4);
  4646. vmcs_writel(GUEST_CR4, hw_cr4);
  4647. return 0;
  4648. }
  4649. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4650. struct kvm_segment *var, int seg)
  4651. {
  4652. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4653. u32 ar;
  4654. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4655. *var = vmx->rmode.segs[seg];
  4656. if (seg == VCPU_SREG_TR
  4657. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4658. return;
  4659. var->base = vmx_read_guest_seg_base(vmx, seg);
  4660. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4661. return;
  4662. }
  4663. var->base = vmx_read_guest_seg_base(vmx, seg);
  4664. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4665. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4666. ar = vmx_read_guest_seg_ar(vmx, seg);
  4667. var->unusable = (ar >> 16) & 1;
  4668. var->type = ar & 15;
  4669. var->s = (ar >> 4) & 1;
  4670. var->dpl = (ar >> 5) & 3;
  4671. /*
  4672. * Some userspaces do not preserve unusable property. Since usable
  4673. * segment has to be present according to VMX spec we can use present
  4674. * property to amend userspace bug by making unusable segment always
  4675. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4676. * segment as unusable.
  4677. */
  4678. var->present = !var->unusable;
  4679. var->avl = (ar >> 12) & 1;
  4680. var->l = (ar >> 13) & 1;
  4681. var->db = (ar >> 14) & 1;
  4682. var->g = (ar >> 15) & 1;
  4683. }
  4684. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4685. {
  4686. struct kvm_segment s;
  4687. if (to_vmx(vcpu)->rmode.vm86_active) {
  4688. vmx_get_segment(vcpu, &s, seg);
  4689. return s.base;
  4690. }
  4691. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4692. }
  4693. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4694. {
  4695. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4696. if (unlikely(vmx->rmode.vm86_active))
  4697. return 0;
  4698. else {
  4699. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4700. return VMX_AR_DPL(ar);
  4701. }
  4702. }
  4703. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4704. {
  4705. u32 ar;
  4706. if (var->unusable || !var->present)
  4707. ar = 1 << 16;
  4708. else {
  4709. ar = var->type & 15;
  4710. ar |= (var->s & 1) << 4;
  4711. ar |= (var->dpl & 3) << 5;
  4712. ar |= (var->present & 1) << 7;
  4713. ar |= (var->avl & 1) << 12;
  4714. ar |= (var->l & 1) << 13;
  4715. ar |= (var->db & 1) << 14;
  4716. ar |= (var->g & 1) << 15;
  4717. }
  4718. return ar;
  4719. }
  4720. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4721. struct kvm_segment *var, int seg)
  4722. {
  4723. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4724. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4725. vmx_segment_cache_clear(vmx);
  4726. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4727. vmx->rmode.segs[seg] = *var;
  4728. if (seg == VCPU_SREG_TR)
  4729. vmcs_write16(sf->selector, var->selector);
  4730. else if (var->s)
  4731. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4732. goto out;
  4733. }
  4734. vmcs_writel(sf->base, var->base);
  4735. vmcs_write32(sf->limit, var->limit);
  4736. vmcs_write16(sf->selector, var->selector);
  4737. /*
  4738. * Fix the "Accessed" bit in AR field of segment registers for older
  4739. * qemu binaries.
  4740. * IA32 arch specifies that at the time of processor reset the
  4741. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4742. * is setting it to 0 in the userland code. This causes invalid guest
  4743. * state vmexit when "unrestricted guest" mode is turned on.
  4744. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4745. * tree. Newer qemu binaries with that qemu fix would not need this
  4746. * kvm hack.
  4747. */
  4748. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4749. var->type |= 0x1; /* Accessed */
  4750. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4751. out:
  4752. vmx->emulation_required = emulation_required(vcpu);
  4753. }
  4754. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4755. {
  4756. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4757. *db = (ar >> 14) & 1;
  4758. *l = (ar >> 13) & 1;
  4759. }
  4760. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4761. {
  4762. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4763. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4764. }
  4765. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4766. {
  4767. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4768. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4769. }
  4770. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4771. {
  4772. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4773. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4774. }
  4775. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4776. {
  4777. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4778. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4779. }
  4780. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4781. {
  4782. struct kvm_segment var;
  4783. u32 ar;
  4784. vmx_get_segment(vcpu, &var, seg);
  4785. var.dpl = 0x3;
  4786. if (seg == VCPU_SREG_CS)
  4787. var.type = 0x3;
  4788. ar = vmx_segment_access_rights(&var);
  4789. if (var.base != (var.selector << 4))
  4790. return false;
  4791. if (var.limit != 0xffff)
  4792. return false;
  4793. if (ar != 0xf3)
  4794. return false;
  4795. return true;
  4796. }
  4797. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4798. {
  4799. struct kvm_segment cs;
  4800. unsigned int cs_rpl;
  4801. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4802. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4803. if (cs.unusable)
  4804. return false;
  4805. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4806. return false;
  4807. if (!cs.s)
  4808. return false;
  4809. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4810. if (cs.dpl > cs_rpl)
  4811. return false;
  4812. } else {
  4813. if (cs.dpl != cs_rpl)
  4814. return false;
  4815. }
  4816. if (!cs.present)
  4817. return false;
  4818. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4819. return true;
  4820. }
  4821. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4822. {
  4823. struct kvm_segment ss;
  4824. unsigned int ss_rpl;
  4825. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4826. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4827. if (ss.unusable)
  4828. return true;
  4829. if (ss.type != 3 && ss.type != 7)
  4830. return false;
  4831. if (!ss.s)
  4832. return false;
  4833. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4834. return false;
  4835. if (!ss.present)
  4836. return false;
  4837. return true;
  4838. }
  4839. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4840. {
  4841. struct kvm_segment var;
  4842. unsigned int rpl;
  4843. vmx_get_segment(vcpu, &var, seg);
  4844. rpl = var.selector & SEGMENT_RPL_MASK;
  4845. if (var.unusable)
  4846. return true;
  4847. if (!var.s)
  4848. return false;
  4849. if (!var.present)
  4850. return false;
  4851. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4852. if (var.dpl < rpl) /* DPL < RPL */
  4853. return false;
  4854. }
  4855. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4856. * rights flags
  4857. */
  4858. return true;
  4859. }
  4860. static bool tr_valid(struct kvm_vcpu *vcpu)
  4861. {
  4862. struct kvm_segment tr;
  4863. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4864. if (tr.unusable)
  4865. return false;
  4866. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4867. return false;
  4868. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4869. return false;
  4870. if (!tr.present)
  4871. return false;
  4872. return true;
  4873. }
  4874. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4875. {
  4876. struct kvm_segment ldtr;
  4877. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4878. if (ldtr.unusable)
  4879. return true;
  4880. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4881. return false;
  4882. if (ldtr.type != 2)
  4883. return false;
  4884. if (!ldtr.present)
  4885. return false;
  4886. return true;
  4887. }
  4888. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4889. {
  4890. struct kvm_segment cs, ss;
  4891. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4892. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4893. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4894. (ss.selector & SEGMENT_RPL_MASK));
  4895. }
  4896. static bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu,
  4897. unsigned int port, int size);
  4898. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  4899. struct vmcs12 *vmcs12)
  4900. {
  4901. unsigned long exit_qualification;
  4902. unsigned short port;
  4903. int size;
  4904. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  4905. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  4906. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4907. port = exit_qualification >> 16;
  4908. size = (exit_qualification & 7) + 1;
  4909. return nested_vmx_check_io_bitmaps(vcpu, port, size);
  4910. }
  4911. /*
  4912. * Check if guest state is valid. Returns true if valid, false if
  4913. * not.
  4914. * We assume that registers are always usable
  4915. */
  4916. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4917. {
  4918. if (enable_unrestricted_guest)
  4919. return true;
  4920. /* real mode guest state checks */
  4921. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4922. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4923. return false;
  4924. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4925. return false;
  4926. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4927. return false;
  4928. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4929. return false;
  4930. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4931. return false;
  4932. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4933. return false;
  4934. } else {
  4935. /* protected mode guest state checks */
  4936. if (!cs_ss_rpl_check(vcpu))
  4937. return false;
  4938. if (!code_segment_valid(vcpu))
  4939. return false;
  4940. if (!stack_segment_valid(vcpu))
  4941. return false;
  4942. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4943. return false;
  4944. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4945. return false;
  4946. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4947. return false;
  4948. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4949. return false;
  4950. if (!tr_valid(vcpu))
  4951. return false;
  4952. if (!ldtr_valid(vcpu))
  4953. return false;
  4954. }
  4955. /* TODO:
  4956. * - Add checks on RIP
  4957. * - Add checks on RFLAGS
  4958. */
  4959. return true;
  4960. }
  4961. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4962. {
  4963. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4964. }
  4965. static int init_rmode_tss(struct kvm *kvm)
  4966. {
  4967. gfn_t fn;
  4968. u16 data = 0;
  4969. int idx, r;
  4970. idx = srcu_read_lock(&kvm->srcu);
  4971. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  4972. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4973. if (r < 0)
  4974. goto out;
  4975. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  4976. r = kvm_write_guest_page(kvm, fn++, &data,
  4977. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  4978. if (r < 0)
  4979. goto out;
  4980. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  4981. if (r < 0)
  4982. goto out;
  4983. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4984. if (r < 0)
  4985. goto out;
  4986. data = ~0;
  4987. r = kvm_write_guest_page(kvm, fn, &data,
  4988. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  4989. sizeof(u8));
  4990. out:
  4991. srcu_read_unlock(&kvm->srcu, idx);
  4992. return r;
  4993. }
  4994. static int init_rmode_identity_map(struct kvm *kvm)
  4995. {
  4996. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  4997. int i, idx, r = 0;
  4998. kvm_pfn_t identity_map_pfn;
  4999. u32 tmp;
  5000. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  5001. mutex_lock(&kvm->slots_lock);
  5002. if (likely(kvm_vmx->ept_identity_pagetable_done))
  5003. goto out2;
  5004. if (!kvm_vmx->ept_identity_map_addr)
  5005. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5006. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  5007. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  5008. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  5009. if (r < 0)
  5010. goto out2;
  5011. idx = srcu_read_lock(&kvm->srcu);
  5012. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  5013. if (r < 0)
  5014. goto out;
  5015. /* Set up identity-mapping pagetable for EPT in real mode */
  5016. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  5017. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  5018. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  5019. r = kvm_write_guest_page(kvm, identity_map_pfn,
  5020. &tmp, i * sizeof(tmp), sizeof(tmp));
  5021. if (r < 0)
  5022. goto out;
  5023. }
  5024. kvm_vmx->ept_identity_pagetable_done = true;
  5025. out:
  5026. srcu_read_unlock(&kvm->srcu, idx);
  5027. out2:
  5028. mutex_unlock(&kvm->slots_lock);
  5029. return r;
  5030. }
  5031. static void seg_setup(int seg)
  5032. {
  5033. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  5034. unsigned int ar;
  5035. vmcs_write16(sf->selector, 0);
  5036. vmcs_writel(sf->base, 0);
  5037. vmcs_write32(sf->limit, 0xffff);
  5038. ar = 0x93;
  5039. if (seg == VCPU_SREG_CS)
  5040. ar |= 0x08; /* code segment */
  5041. vmcs_write32(sf->ar_bytes, ar);
  5042. }
  5043. static int alloc_apic_access_page(struct kvm *kvm)
  5044. {
  5045. struct page *page;
  5046. int r = 0;
  5047. mutex_lock(&kvm->slots_lock);
  5048. if (kvm->arch.apic_access_page_done)
  5049. goto out;
  5050. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  5051. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  5052. if (r)
  5053. goto out;
  5054. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5055. if (is_error_page(page)) {
  5056. r = -EFAULT;
  5057. goto out;
  5058. }
  5059. /*
  5060. * Do not pin the page in memory, so that memory hot-unplug
  5061. * is able to migrate it.
  5062. */
  5063. put_page(page);
  5064. kvm->arch.apic_access_page_done = true;
  5065. out:
  5066. mutex_unlock(&kvm->slots_lock);
  5067. return r;
  5068. }
  5069. static int allocate_vpid(void)
  5070. {
  5071. int vpid;
  5072. if (!enable_vpid)
  5073. return 0;
  5074. spin_lock(&vmx_vpid_lock);
  5075. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  5076. if (vpid < VMX_NR_VPIDS)
  5077. __set_bit(vpid, vmx_vpid_bitmap);
  5078. else
  5079. vpid = 0;
  5080. spin_unlock(&vmx_vpid_lock);
  5081. return vpid;
  5082. }
  5083. static void free_vpid(int vpid)
  5084. {
  5085. if (!enable_vpid || vpid == 0)
  5086. return;
  5087. spin_lock(&vmx_vpid_lock);
  5088. __clear_bit(vpid, vmx_vpid_bitmap);
  5089. spin_unlock(&vmx_vpid_lock);
  5090. }
  5091. static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  5092. u32 msr, int type)
  5093. {
  5094. int f = sizeof(unsigned long);
  5095. if (!cpu_has_vmx_msr_bitmap())
  5096. return;
  5097. if (static_branch_unlikely(&enable_evmcs))
  5098. evmcs_touch_msr_bitmap();
  5099. /*
  5100. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5101. * have the write-low and read-high bitmap offsets the wrong way round.
  5102. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5103. */
  5104. if (msr <= 0x1fff) {
  5105. if (type & MSR_TYPE_R)
  5106. /* read-low */
  5107. __clear_bit(msr, msr_bitmap + 0x000 / f);
  5108. if (type & MSR_TYPE_W)
  5109. /* write-low */
  5110. __clear_bit(msr, msr_bitmap + 0x800 / f);
  5111. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5112. msr &= 0x1fff;
  5113. if (type & MSR_TYPE_R)
  5114. /* read-high */
  5115. __clear_bit(msr, msr_bitmap + 0x400 / f);
  5116. if (type & MSR_TYPE_W)
  5117. /* write-high */
  5118. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  5119. }
  5120. }
  5121. static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  5122. u32 msr, int type)
  5123. {
  5124. int f = sizeof(unsigned long);
  5125. if (!cpu_has_vmx_msr_bitmap())
  5126. return;
  5127. if (static_branch_unlikely(&enable_evmcs))
  5128. evmcs_touch_msr_bitmap();
  5129. /*
  5130. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5131. * have the write-low and read-high bitmap offsets the wrong way round.
  5132. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5133. */
  5134. if (msr <= 0x1fff) {
  5135. if (type & MSR_TYPE_R)
  5136. /* read-low */
  5137. __set_bit(msr, msr_bitmap + 0x000 / f);
  5138. if (type & MSR_TYPE_W)
  5139. /* write-low */
  5140. __set_bit(msr, msr_bitmap + 0x800 / f);
  5141. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5142. msr &= 0x1fff;
  5143. if (type & MSR_TYPE_R)
  5144. /* read-high */
  5145. __set_bit(msr, msr_bitmap + 0x400 / f);
  5146. if (type & MSR_TYPE_W)
  5147. /* write-high */
  5148. __set_bit(msr, msr_bitmap + 0xc00 / f);
  5149. }
  5150. }
  5151. static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  5152. u32 msr, int type, bool value)
  5153. {
  5154. if (value)
  5155. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  5156. else
  5157. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  5158. }
  5159. /*
  5160. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  5161. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  5162. */
  5163. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  5164. unsigned long *msr_bitmap_nested,
  5165. u32 msr, int type)
  5166. {
  5167. int f = sizeof(unsigned long);
  5168. /*
  5169. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5170. * have the write-low and read-high bitmap offsets the wrong way round.
  5171. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5172. */
  5173. if (msr <= 0x1fff) {
  5174. if (type & MSR_TYPE_R &&
  5175. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  5176. /* read-low */
  5177. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  5178. if (type & MSR_TYPE_W &&
  5179. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  5180. /* write-low */
  5181. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  5182. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5183. msr &= 0x1fff;
  5184. if (type & MSR_TYPE_R &&
  5185. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  5186. /* read-high */
  5187. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  5188. if (type & MSR_TYPE_W &&
  5189. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  5190. /* write-high */
  5191. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  5192. }
  5193. }
  5194. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  5195. {
  5196. u8 mode = 0;
  5197. if (cpu_has_secondary_exec_ctrls() &&
  5198. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  5199. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  5200. mode |= MSR_BITMAP_MODE_X2APIC;
  5201. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  5202. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  5203. }
  5204. return mode;
  5205. }
  5206. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  5207. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  5208. u8 mode)
  5209. {
  5210. int msr;
  5211. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  5212. unsigned word = msr / BITS_PER_LONG;
  5213. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  5214. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  5215. }
  5216. if (mode & MSR_BITMAP_MODE_X2APIC) {
  5217. /*
  5218. * TPR reads and writes can be virtualized even if virtual interrupt
  5219. * delivery is not in use.
  5220. */
  5221. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  5222. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  5223. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  5224. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  5225. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  5226. }
  5227. }
  5228. }
  5229. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  5230. {
  5231. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5232. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  5233. u8 mode = vmx_msr_bitmap_mode(vcpu);
  5234. u8 changed = mode ^ vmx->msr_bitmap_mode;
  5235. if (!changed)
  5236. return;
  5237. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  5238. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  5239. vmx->msr_bitmap_mode = mode;
  5240. }
  5241. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  5242. {
  5243. return enable_apicv;
  5244. }
  5245. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  5246. {
  5247. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5248. gfn_t gfn;
  5249. /*
  5250. * Don't need to mark the APIC access page dirty; it is never
  5251. * written to by the CPU during APIC virtualization.
  5252. */
  5253. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  5254. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  5255. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5256. }
  5257. if (nested_cpu_has_posted_intr(vmcs12)) {
  5258. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5259. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5260. }
  5261. }
  5262. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5263. {
  5264. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5265. int max_irr;
  5266. void *vapic_page;
  5267. u16 status;
  5268. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5269. return;
  5270. vmx->nested.pi_pending = false;
  5271. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5272. return;
  5273. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5274. if (max_irr != 256) {
  5275. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5276. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5277. vapic_page, &max_irr);
  5278. kunmap(vmx->nested.virtual_apic_page);
  5279. status = vmcs_read16(GUEST_INTR_STATUS);
  5280. if ((u8)max_irr > ((u8)status & 0xff)) {
  5281. status &= ~0xff;
  5282. status |= (u8)max_irr;
  5283. vmcs_write16(GUEST_INTR_STATUS, status);
  5284. }
  5285. }
  5286. nested_mark_vmcs12_pages_dirty(vcpu);
  5287. }
  5288. static u8 vmx_get_rvi(void)
  5289. {
  5290. return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
  5291. }
  5292. static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
  5293. {
  5294. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5295. void *vapic_page;
  5296. u32 vppr;
  5297. int rvi;
  5298. if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
  5299. !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
  5300. WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
  5301. return false;
  5302. rvi = vmx_get_rvi();
  5303. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5304. vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
  5305. kunmap(vmx->nested.virtual_apic_page);
  5306. return ((rvi & 0xf0) > (vppr & 0xf0));
  5307. }
  5308. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5309. bool nested)
  5310. {
  5311. #ifdef CONFIG_SMP
  5312. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5313. if (vcpu->mode == IN_GUEST_MODE) {
  5314. /*
  5315. * The vector of interrupt to be delivered to vcpu had
  5316. * been set in PIR before this function.
  5317. *
  5318. * Following cases will be reached in this block, and
  5319. * we always send a notification event in all cases as
  5320. * explained below.
  5321. *
  5322. * Case 1: vcpu keeps in non-root mode. Sending a
  5323. * notification event posts the interrupt to vcpu.
  5324. *
  5325. * Case 2: vcpu exits to root mode and is still
  5326. * runnable. PIR will be synced to vIRR before the
  5327. * next vcpu entry. Sending a notification event in
  5328. * this case has no effect, as vcpu is not in root
  5329. * mode.
  5330. *
  5331. * Case 3: vcpu exits to root mode and is blocked.
  5332. * vcpu_block() has already synced PIR to vIRR and
  5333. * never blocks vcpu if vIRR is not cleared. Therefore,
  5334. * a blocked vcpu here does not wait for any requested
  5335. * interrupts in PIR, and sending a notification event
  5336. * which has no effect is safe here.
  5337. */
  5338. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5339. return true;
  5340. }
  5341. #endif
  5342. return false;
  5343. }
  5344. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5345. int vector)
  5346. {
  5347. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5348. if (is_guest_mode(vcpu) &&
  5349. vector == vmx->nested.posted_intr_nv) {
  5350. /*
  5351. * If a posted intr is not recognized by hardware,
  5352. * we will accomplish it in the next vmentry.
  5353. */
  5354. vmx->nested.pi_pending = true;
  5355. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5356. /* the PIR and ON have been set by L1. */
  5357. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5358. kvm_vcpu_kick(vcpu);
  5359. return 0;
  5360. }
  5361. return -1;
  5362. }
  5363. /*
  5364. * Send interrupt to vcpu via posted interrupt way.
  5365. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5366. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5367. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5368. * interrupt from PIR in next vmentry.
  5369. */
  5370. static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5371. {
  5372. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5373. int r;
  5374. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5375. if (!r)
  5376. return 0;
  5377. if (!vcpu->arch.apicv_active)
  5378. return -1;
  5379. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5380. return 0;
  5381. /* If a previous notification has sent the IPI, nothing to do. */
  5382. if (pi_test_and_set_on(&vmx->pi_desc))
  5383. return 0;
  5384. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5385. kvm_vcpu_kick(vcpu);
  5386. return 0;
  5387. }
  5388. /*
  5389. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5390. * will not change in the lifetime of the guest.
  5391. * Note that host-state that does change is set elsewhere. E.g., host-state
  5392. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5393. */
  5394. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5395. {
  5396. u32 low32, high32;
  5397. unsigned long tmpl;
  5398. struct desc_ptr dt;
  5399. unsigned long cr0, cr3, cr4;
  5400. cr0 = read_cr0();
  5401. WARN_ON(cr0 & X86_CR0_TS);
  5402. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5403. /*
  5404. * Save the most likely value for this task's CR3 in the VMCS.
  5405. * We can't use __get_current_cr3_fast() because we're not atomic.
  5406. */
  5407. cr3 = __read_cr3();
  5408. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5409. vmx->loaded_vmcs->host_state.cr3 = cr3;
  5410. /* Save the most likely value for this task's CR4 in the VMCS. */
  5411. cr4 = cr4_read_shadow();
  5412. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5413. vmx->loaded_vmcs->host_state.cr4 = cr4;
  5414. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5415. #ifdef CONFIG_X86_64
  5416. /*
  5417. * Load null selectors, so we can avoid reloading them in
  5418. * vmx_prepare_switch_to_host(), in case userspace uses
  5419. * the null selectors too (the expected case).
  5420. */
  5421. vmcs_write16(HOST_DS_SELECTOR, 0);
  5422. vmcs_write16(HOST_ES_SELECTOR, 0);
  5423. #else
  5424. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5425. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5426. #endif
  5427. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5428. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5429. store_idt(&dt);
  5430. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5431. vmx->host_idt_base = dt.address;
  5432. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5433. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5434. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5435. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5436. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5437. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5438. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5439. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5440. }
  5441. }
  5442. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5443. {
  5444. BUILD_BUG_ON(KVM_CR4_GUEST_OWNED_BITS & ~KVM_POSSIBLE_CR4_GUEST_BITS);
  5445. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5446. if (enable_ept)
  5447. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5448. if (is_guest_mode(&vmx->vcpu))
  5449. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5450. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5451. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5452. }
  5453. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5454. {
  5455. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5456. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5457. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5458. if (!enable_vnmi)
  5459. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5460. /* Enable the preemption timer dynamically */
  5461. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5462. return pin_based_exec_ctrl;
  5463. }
  5464. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5465. {
  5466. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5467. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5468. if (cpu_has_secondary_exec_ctrls()) {
  5469. if (kvm_vcpu_apicv_active(vcpu))
  5470. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5471. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5472. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5473. else
  5474. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5475. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5476. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5477. }
  5478. if (cpu_has_vmx_msr_bitmap())
  5479. vmx_update_msr_bitmap(vcpu);
  5480. }
  5481. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5482. {
  5483. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5484. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5485. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5486. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5487. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5488. #ifdef CONFIG_X86_64
  5489. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5490. CPU_BASED_CR8_LOAD_EXITING;
  5491. #endif
  5492. }
  5493. if (!enable_ept)
  5494. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5495. CPU_BASED_CR3_LOAD_EXITING |
  5496. CPU_BASED_INVLPG_EXITING;
  5497. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5498. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5499. CPU_BASED_MONITOR_EXITING);
  5500. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5501. exec_control &= ~CPU_BASED_HLT_EXITING;
  5502. return exec_control;
  5503. }
  5504. static bool vmx_rdrand_supported(void)
  5505. {
  5506. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5507. SECONDARY_EXEC_RDRAND_EXITING;
  5508. }
  5509. static bool vmx_rdseed_supported(void)
  5510. {
  5511. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5512. SECONDARY_EXEC_RDSEED_EXITING;
  5513. }
  5514. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5515. {
  5516. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5517. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5518. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5519. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5520. if (vmx->vpid == 0)
  5521. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5522. if (!enable_ept) {
  5523. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5524. enable_unrestricted_guest = 0;
  5525. }
  5526. if (!enable_unrestricted_guest)
  5527. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5528. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5529. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5530. if (!kvm_vcpu_apicv_active(vcpu))
  5531. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5532. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5533. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5534. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5535. * in vmx_set_cr4. */
  5536. exec_control &= ~SECONDARY_EXEC_DESC;
  5537. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5538. (handle_vmptrld).
  5539. We can NOT enable shadow_vmcs here because we don't have yet
  5540. a current VMCS12
  5541. */
  5542. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5543. if (!enable_pml)
  5544. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5545. if (vmx_xsaves_supported()) {
  5546. /* Exposing XSAVES only when XSAVE is exposed */
  5547. bool xsaves_enabled =
  5548. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5549. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5550. if (!xsaves_enabled)
  5551. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5552. if (nested) {
  5553. if (xsaves_enabled)
  5554. vmx->nested.msrs.secondary_ctls_high |=
  5555. SECONDARY_EXEC_XSAVES;
  5556. else
  5557. vmx->nested.msrs.secondary_ctls_high &=
  5558. ~SECONDARY_EXEC_XSAVES;
  5559. }
  5560. }
  5561. if (vmx_rdtscp_supported()) {
  5562. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5563. if (!rdtscp_enabled)
  5564. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5565. if (nested) {
  5566. if (rdtscp_enabled)
  5567. vmx->nested.msrs.secondary_ctls_high |=
  5568. SECONDARY_EXEC_RDTSCP;
  5569. else
  5570. vmx->nested.msrs.secondary_ctls_high &=
  5571. ~SECONDARY_EXEC_RDTSCP;
  5572. }
  5573. }
  5574. if (vmx_invpcid_supported()) {
  5575. /* Exposing INVPCID only when PCID is exposed */
  5576. bool invpcid_enabled =
  5577. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5578. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5579. if (!invpcid_enabled) {
  5580. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5581. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5582. }
  5583. if (nested) {
  5584. if (invpcid_enabled)
  5585. vmx->nested.msrs.secondary_ctls_high |=
  5586. SECONDARY_EXEC_ENABLE_INVPCID;
  5587. else
  5588. vmx->nested.msrs.secondary_ctls_high &=
  5589. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5590. }
  5591. }
  5592. if (vmx_rdrand_supported()) {
  5593. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5594. if (rdrand_enabled)
  5595. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5596. if (nested) {
  5597. if (rdrand_enabled)
  5598. vmx->nested.msrs.secondary_ctls_high |=
  5599. SECONDARY_EXEC_RDRAND_EXITING;
  5600. else
  5601. vmx->nested.msrs.secondary_ctls_high &=
  5602. ~SECONDARY_EXEC_RDRAND_EXITING;
  5603. }
  5604. }
  5605. if (vmx_rdseed_supported()) {
  5606. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5607. if (rdseed_enabled)
  5608. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5609. if (nested) {
  5610. if (rdseed_enabled)
  5611. vmx->nested.msrs.secondary_ctls_high |=
  5612. SECONDARY_EXEC_RDSEED_EXITING;
  5613. else
  5614. vmx->nested.msrs.secondary_ctls_high &=
  5615. ~SECONDARY_EXEC_RDSEED_EXITING;
  5616. }
  5617. }
  5618. vmx->secondary_exec_control = exec_control;
  5619. }
  5620. static void ept_set_mmio_spte_mask(void)
  5621. {
  5622. /*
  5623. * EPT Misconfigurations can be generated if the value of bits 2:0
  5624. * of an EPT paging-structure entry is 110b (write/execute).
  5625. */
  5626. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5627. VMX_EPT_MISCONFIG_WX_VALUE);
  5628. }
  5629. #define VMX_XSS_EXIT_BITMAP 0
  5630. /*
  5631. * Sets up the vmcs for emulated real mode.
  5632. */
  5633. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5634. {
  5635. int i;
  5636. if (enable_shadow_vmcs) {
  5637. /*
  5638. * At vCPU creation, "VMWRITE to any supported field
  5639. * in the VMCS" is supported, so use the more
  5640. * permissive vmx_vmread_bitmap to specify both read
  5641. * and write permissions for the shadow VMCS.
  5642. */
  5643. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5644. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5645. }
  5646. if (cpu_has_vmx_msr_bitmap())
  5647. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5648. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5649. /* Control */
  5650. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5651. vmx->hv_deadline_tsc = -1;
  5652. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5653. if (cpu_has_secondary_exec_ctrls()) {
  5654. vmx_compute_secondary_exec_control(vmx);
  5655. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5656. vmx->secondary_exec_control);
  5657. }
  5658. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5659. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5660. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5661. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5662. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5663. vmcs_write16(GUEST_INTR_STATUS, 0);
  5664. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5665. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5666. }
  5667. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5668. vmcs_write32(PLE_GAP, ple_gap);
  5669. vmx->ple_window = ple_window;
  5670. vmx->ple_window_dirty = true;
  5671. }
  5672. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5673. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5674. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5675. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5676. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5677. vmx_set_constant_host_state(vmx);
  5678. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5679. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5680. if (cpu_has_vmx_vmfunc())
  5681. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5682. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5683. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5684. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  5685. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5686. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  5687. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5688. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5689. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5690. u32 index = vmx_msr_index[i];
  5691. u32 data_low, data_high;
  5692. int j = vmx->nmsrs;
  5693. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5694. continue;
  5695. if (wrmsr_safe(index, data_low, data_high) < 0)
  5696. continue;
  5697. vmx->guest_msrs[j].index = i;
  5698. vmx->guest_msrs[j].data = 0;
  5699. vmx->guest_msrs[j].mask = -1ull;
  5700. ++vmx->nmsrs;
  5701. }
  5702. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5703. /* 22.2.1, 20.8.1 */
  5704. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5705. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5706. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5707. set_cr4_guest_host_mask(vmx);
  5708. if (vmx_xsaves_supported())
  5709. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5710. if (enable_pml) {
  5711. ASSERT(vmx->pml_pg);
  5712. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5713. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5714. }
  5715. if (cpu_has_vmx_encls_vmexit())
  5716. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  5717. }
  5718. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5719. {
  5720. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5721. struct msr_data apic_base_msr;
  5722. u64 cr0;
  5723. vmx->rmode.vm86_active = 0;
  5724. vmx->spec_ctrl = 0;
  5725. vcpu->arch.microcode_version = 0x100000000ULL;
  5726. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5727. kvm_set_cr8(vcpu, 0);
  5728. if (!init_event) {
  5729. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5730. MSR_IA32_APICBASE_ENABLE;
  5731. if (kvm_vcpu_is_reset_bsp(vcpu))
  5732. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5733. apic_base_msr.host_initiated = true;
  5734. kvm_set_apic_base(vcpu, &apic_base_msr);
  5735. }
  5736. vmx_segment_cache_clear(vmx);
  5737. seg_setup(VCPU_SREG_CS);
  5738. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5739. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5740. seg_setup(VCPU_SREG_DS);
  5741. seg_setup(VCPU_SREG_ES);
  5742. seg_setup(VCPU_SREG_FS);
  5743. seg_setup(VCPU_SREG_GS);
  5744. seg_setup(VCPU_SREG_SS);
  5745. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5746. vmcs_writel(GUEST_TR_BASE, 0);
  5747. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5748. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5749. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5750. vmcs_writel(GUEST_LDTR_BASE, 0);
  5751. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5752. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5753. if (!init_event) {
  5754. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5755. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5756. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5757. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5758. }
  5759. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5760. kvm_rip_write(vcpu, 0xfff0);
  5761. vmcs_writel(GUEST_GDTR_BASE, 0);
  5762. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5763. vmcs_writel(GUEST_IDTR_BASE, 0);
  5764. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5765. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5766. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5767. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5768. if (kvm_mpx_supported())
  5769. vmcs_write64(GUEST_BNDCFGS, 0);
  5770. setup_msrs(vmx);
  5771. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5772. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5773. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5774. if (cpu_need_tpr_shadow(vcpu))
  5775. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5776. __pa(vcpu->arch.apic->regs));
  5777. vmcs_write32(TPR_THRESHOLD, 0);
  5778. }
  5779. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5780. if (vmx->vpid != 0)
  5781. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5782. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5783. vmx->vcpu.arch.cr0 = cr0;
  5784. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5785. vmx_set_cr4(vcpu, 0);
  5786. vmx_set_efer(vcpu, 0);
  5787. update_exception_bitmap(vcpu);
  5788. vpid_sync_context(vmx->vpid);
  5789. if (init_event)
  5790. vmx_clear_hlt(vcpu);
  5791. }
  5792. /*
  5793. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5794. * For most existing hypervisors, this will always return true.
  5795. */
  5796. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5797. {
  5798. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5799. PIN_BASED_EXT_INTR_MASK;
  5800. }
  5801. /*
  5802. * In nested virtualization, check if L1 has set
  5803. * VM_EXIT_ACK_INTR_ON_EXIT
  5804. */
  5805. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5806. {
  5807. return get_vmcs12(vcpu)->vm_exit_controls &
  5808. VM_EXIT_ACK_INTR_ON_EXIT;
  5809. }
  5810. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5811. {
  5812. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5813. }
  5814. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5815. {
  5816. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5817. CPU_BASED_VIRTUAL_INTR_PENDING);
  5818. }
  5819. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5820. {
  5821. if (!enable_vnmi ||
  5822. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5823. enable_irq_window(vcpu);
  5824. return;
  5825. }
  5826. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5827. CPU_BASED_VIRTUAL_NMI_PENDING);
  5828. }
  5829. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5830. {
  5831. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5832. uint32_t intr;
  5833. int irq = vcpu->arch.interrupt.nr;
  5834. trace_kvm_inj_virq(irq);
  5835. ++vcpu->stat.irq_injections;
  5836. if (vmx->rmode.vm86_active) {
  5837. int inc_eip = 0;
  5838. if (vcpu->arch.interrupt.soft)
  5839. inc_eip = vcpu->arch.event_exit_inst_len;
  5840. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5841. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5842. return;
  5843. }
  5844. intr = irq | INTR_INFO_VALID_MASK;
  5845. if (vcpu->arch.interrupt.soft) {
  5846. intr |= INTR_TYPE_SOFT_INTR;
  5847. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5848. vmx->vcpu.arch.event_exit_inst_len);
  5849. } else
  5850. intr |= INTR_TYPE_EXT_INTR;
  5851. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5852. vmx_clear_hlt(vcpu);
  5853. }
  5854. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5855. {
  5856. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5857. if (!enable_vnmi) {
  5858. /*
  5859. * Tracking the NMI-blocked state in software is built upon
  5860. * finding the next open IRQ window. This, in turn, depends on
  5861. * well-behaving guests: They have to keep IRQs disabled at
  5862. * least as long as the NMI handler runs. Otherwise we may
  5863. * cause NMI nesting, maybe breaking the guest. But as this is
  5864. * highly unlikely, we can live with the residual risk.
  5865. */
  5866. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5867. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5868. }
  5869. ++vcpu->stat.nmi_injections;
  5870. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5871. if (vmx->rmode.vm86_active) {
  5872. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5873. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5874. return;
  5875. }
  5876. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5877. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5878. vmx_clear_hlt(vcpu);
  5879. }
  5880. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5881. {
  5882. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5883. bool masked;
  5884. if (!enable_vnmi)
  5885. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5886. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5887. return false;
  5888. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5889. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5890. return masked;
  5891. }
  5892. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5893. {
  5894. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5895. if (!enable_vnmi) {
  5896. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5897. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5898. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5899. }
  5900. } else {
  5901. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5902. if (masked)
  5903. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5904. GUEST_INTR_STATE_NMI);
  5905. else
  5906. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5907. GUEST_INTR_STATE_NMI);
  5908. }
  5909. }
  5910. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5911. {
  5912. if (to_vmx(vcpu)->nested.nested_run_pending)
  5913. return 0;
  5914. if (!enable_vnmi &&
  5915. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5916. return 0;
  5917. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5918. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5919. | GUEST_INTR_STATE_NMI));
  5920. }
  5921. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5922. {
  5923. if (to_vmx(vcpu)->nested.nested_run_pending)
  5924. return false;
  5925. if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
  5926. return true;
  5927. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5928. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5929. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5930. }
  5931. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5932. {
  5933. int ret;
  5934. if (enable_unrestricted_guest)
  5935. return 0;
  5936. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5937. PAGE_SIZE * 3);
  5938. if (ret)
  5939. return ret;
  5940. to_kvm_vmx(kvm)->tss_addr = addr;
  5941. return init_rmode_tss(kvm);
  5942. }
  5943. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5944. {
  5945. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5946. return 0;
  5947. }
  5948. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5949. {
  5950. switch (vec) {
  5951. case BP_VECTOR:
  5952. /*
  5953. * Update instruction length as we may reinject the exception
  5954. * from user space while in guest debugging mode.
  5955. */
  5956. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5957. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5958. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5959. return false;
  5960. /* fall through */
  5961. case DB_VECTOR:
  5962. if (vcpu->guest_debug &
  5963. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5964. return false;
  5965. /* fall through */
  5966. case DE_VECTOR:
  5967. case OF_VECTOR:
  5968. case BR_VECTOR:
  5969. case UD_VECTOR:
  5970. case DF_VECTOR:
  5971. case SS_VECTOR:
  5972. case GP_VECTOR:
  5973. case MF_VECTOR:
  5974. return true;
  5975. break;
  5976. }
  5977. return false;
  5978. }
  5979. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5980. int vec, u32 err_code)
  5981. {
  5982. /*
  5983. * Instruction with address size override prefix opcode 0x67
  5984. * Cause the #SS fault with 0 error code in VM86 mode.
  5985. */
  5986. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5987. if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5988. if (vcpu->arch.halt_request) {
  5989. vcpu->arch.halt_request = 0;
  5990. return kvm_vcpu_halt(vcpu);
  5991. }
  5992. return 1;
  5993. }
  5994. return 0;
  5995. }
  5996. /*
  5997. * Forward all other exceptions that are valid in real mode.
  5998. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  5999. * the required debugging infrastructure rework.
  6000. */
  6001. kvm_queue_exception(vcpu, vec);
  6002. return 1;
  6003. }
  6004. /*
  6005. * Trigger machine check on the host. We assume all the MSRs are already set up
  6006. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  6007. * We pass a fake environment to the machine check handler because we want
  6008. * the guest to be always treated like user space, no matter what context
  6009. * it used internally.
  6010. */
  6011. static void kvm_machine_check(void)
  6012. {
  6013. #if defined(CONFIG_X86_MCE)
  6014. struct pt_regs regs = {
  6015. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  6016. .flags = X86_EFLAGS_IF,
  6017. };
  6018. do_machine_check(&regs, 0);
  6019. #endif
  6020. }
  6021. static int handle_machine_check(struct kvm_vcpu *vcpu)
  6022. {
  6023. /* already handled by vcpu_run */
  6024. return 1;
  6025. }
  6026. static int handle_exception(struct kvm_vcpu *vcpu)
  6027. {
  6028. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6029. struct kvm_run *kvm_run = vcpu->run;
  6030. u32 intr_info, ex_no, error_code;
  6031. unsigned long cr2, rip, dr6;
  6032. u32 vect_info;
  6033. enum emulation_result er;
  6034. vect_info = vmx->idt_vectoring_info;
  6035. intr_info = vmx->exit_intr_info;
  6036. if (is_machine_check(intr_info))
  6037. return handle_machine_check(vcpu);
  6038. if (is_nmi(intr_info))
  6039. return 1; /* already handled by vmx_vcpu_run() */
  6040. if (is_invalid_opcode(intr_info))
  6041. return handle_ud(vcpu);
  6042. error_code = 0;
  6043. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  6044. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  6045. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  6046. WARN_ON_ONCE(!enable_vmware_backdoor);
  6047. er = kvm_emulate_instruction(vcpu,
  6048. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  6049. if (er == EMULATE_USER_EXIT)
  6050. return 0;
  6051. else if (er != EMULATE_DONE)
  6052. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  6053. return 1;
  6054. }
  6055. /*
  6056. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  6057. * MMIO, it is better to report an internal error.
  6058. * See the comments in vmx_handle_exit.
  6059. */
  6060. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  6061. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  6062. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6063. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  6064. vcpu->run->internal.ndata = 3;
  6065. vcpu->run->internal.data[0] = vect_info;
  6066. vcpu->run->internal.data[1] = intr_info;
  6067. vcpu->run->internal.data[2] = error_code;
  6068. return 0;
  6069. }
  6070. if (is_page_fault(intr_info)) {
  6071. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  6072. /* EPT won't cause page fault directly */
  6073. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  6074. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  6075. }
  6076. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  6077. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  6078. return handle_rmode_exception(vcpu, ex_no, error_code);
  6079. switch (ex_no) {
  6080. case AC_VECTOR:
  6081. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  6082. return 1;
  6083. case DB_VECTOR:
  6084. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  6085. if (!(vcpu->guest_debug &
  6086. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  6087. vcpu->arch.dr6 &= ~15;
  6088. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  6089. if (is_icebp(intr_info))
  6090. skip_emulated_instruction(vcpu);
  6091. kvm_queue_exception(vcpu, DB_VECTOR);
  6092. return 1;
  6093. }
  6094. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  6095. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  6096. /* fall through */
  6097. case BP_VECTOR:
  6098. /*
  6099. * Update instruction length as we may reinject #BP from
  6100. * user space while in guest debugging mode. Reading it for
  6101. * #DB as well causes no harm, it is not used in that case.
  6102. */
  6103. vmx->vcpu.arch.event_exit_inst_len =
  6104. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  6105. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  6106. rip = kvm_rip_read(vcpu);
  6107. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  6108. kvm_run->debug.arch.exception = ex_no;
  6109. break;
  6110. default:
  6111. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  6112. kvm_run->ex.exception = ex_no;
  6113. kvm_run->ex.error_code = error_code;
  6114. break;
  6115. }
  6116. return 0;
  6117. }
  6118. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  6119. {
  6120. ++vcpu->stat.irq_exits;
  6121. return 1;
  6122. }
  6123. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  6124. {
  6125. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  6126. vcpu->mmio_needed = 0;
  6127. return 0;
  6128. }
  6129. static int handle_io(struct kvm_vcpu *vcpu)
  6130. {
  6131. unsigned long exit_qualification;
  6132. int size, in, string;
  6133. unsigned port;
  6134. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6135. string = (exit_qualification & 16) != 0;
  6136. ++vcpu->stat.io_exits;
  6137. if (string)
  6138. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6139. port = exit_qualification >> 16;
  6140. size = (exit_qualification & 7) + 1;
  6141. in = (exit_qualification & 8) != 0;
  6142. return kvm_fast_pio(vcpu, size, port, in);
  6143. }
  6144. static void
  6145. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  6146. {
  6147. /*
  6148. * Patch in the VMCALL instruction:
  6149. */
  6150. hypercall[0] = 0x0f;
  6151. hypercall[1] = 0x01;
  6152. hypercall[2] = 0xc1;
  6153. }
  6154. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  6155. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  6156. {
  6157. if (is_guest_mode(vcpu)) {
  6158. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6159. unsigned long orig_val = val;
  6160. /*
  6161. * We get here when L2 changed cr0 in a way that did not change
  6162. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  6163. * but did change L0 shadowed bits. So we first calculate the
  6164. * effective cr0 value that L1 would like to write into the
  6165. * hardware. It consists of the L2-owned bits from the new
  6166. * value combined with the L1-owned bits from L1's guest_cr0.
  6167. */
  6168. val = (val & ~vmcs12->cr0_guest_host_mask) |
  6169. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  6170. if (!nested_guest_cr0_valid(vcpu, val))
  6171. return 1;
  6172. if (kvm_set_cr0(vcpu, val))
  6173. return 1;
  6174. vmcs_writel(CR0_READ_SHADOW, orig_val);
  6175. return 0;
  6176. } else {
  6177. if (to_vmx(vcpu)->nested.vmxon &&
  6178. !nested_host_cr0_valid(vcpu, val))
  6179. return 1;
  6180. return kvm_set_cr0(vcpu, val);
  6181. }
  6182. }
  6183. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  6184. {
  6185. if (is_guest_mode(vcpu)) {
  6186. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6187. unsigned long orig_val = val;
  6188. /* analogously to handle_set_cr0 */
  6189. val = (val & ~vmcs12->cr4_guest_host_mask) |
  6190. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  6191. if (kvm_set_cr4(vcpu, val))
  6192. return 1;
  6193. vmcs_writel(CR4_READ_SHADOW, orig_val);
  6194. return 0;
  6195. } else
  6196. return kvm_set_cr4(vcpu, val);
  6197. }
  6198. static int handle_desc(struct kvm_vcpu *vcpu)
  6199. {
  6200. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  6201. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6202. }
  6203. static int handle_cr(struct kvm_vcpu *vcpu)
  6204. {
  6205. unsigned long exit_qualification, val;
  6206. int cr;
  6207. int reg;
  6208. int err;
  6209. int ret;
  6210. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6211. cr = exit_qualification & 15;
  6212. reg = (exit_qualification >> 8) & 15;
  6213. switch ((exit_qualification >> 4) & 3) {
  6214. case 0: /* mov to cr */
  6215. val = kvm_register_readl(vcpu, reg);
  6216. trace_kvm_cr_write(cr, val);
  6217. switch (cr) {
  6218. case 0:
  6219. err = handle_set_cr0(vcpu, val);
  6220. return kvm_complete_insn_gp(vcpu, err);
  6221. case 3:
  6222. WARN_ON_ONCE(enable_unrestricted_guest);
  6223. err = kvm_set_cr3(vcpu, val);
  6224. return kvm_complete_insn_gp(vcpu, err);
  6225. case 4:
  6226. err = handle_set_cr4(vcpu, val);
  6227. return kvm_complete_insn_gp(vcpu, err);
  6228. case 8: {
  6229. u8 cr8_prev = kvm_get_cr8(vcpu);
  6230. u8 cr8 = (u8)val;
  6231. err = kvm_set_cr8(vcpu, cr8);
  6232. ret = kvm_complete_insn_gp(vcpu, err);
  6233. if (lapic_in_kernel(vcpu))
  6234. return ret;
  6235. if (cr8_prev <= cr8)
  6236. return ret;
  6237. /*
  6238. * TODO: we might be squashing a
  6239. * KVM_GUESTDBG_SINGLESTEP-triggered
  6240. * KVM_EXIT_DEBUG here.
  6241. */
  6242. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  6243. return 0;
  6244. }
  6245. }
  6246. break;
  6247. case 2: /* clts */
  6248. WARN_ONCE(1, "Guest should always own CR0.TS");
  6249. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  6250. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  6251. return kvm_skip_emulated_instruction(vcpu);
  6252. case 1: /*mov from cr*/
  6253. switch (cr) {
  6254. case 3:
  6255. WARN_ON_ONCE(enable_unrestricted_guest);
  6256. val = kvm_read_cr3(vcpu);
  6257. kvm_register_write(vcpu, reg, val);
  6258. trace_kvm_cr_read(cr, val);
  6259. return kvm_skip_emulated_instruction(vcpu);
  6260. case 8:
  6261. val = kvm_get_cr8(vcpu);
  6262. kvm_register_write(vcpu, reg, val);
  6263. trace_kvm_cr_read(cr, val);
  6264. return kvm_skip_emulated_instruction(vcpu);
  6265. }
  6266. break;
  6267. case 3: /* lmsw */
  6268. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  6269. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  6270. kvm_lmsw(vcpu, val);
  6271. return kvm_skip_emulated_instruction(vcpu);
  6272. default:
  6273. break;
  6274. }
  6275. vcpu->run->exit_reason = 0;
  6276. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  6277. (int)(exit_qualification >> 4) & 3, cr);
  6278. return 0;
  6279. }
  6280. static int handle_dr(struct kvm_vcpu *vcpu)
  6281. {
  6282. unsigned long exit_qualification;
  6283. int dr, dr7, reg;
  6284. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6285. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  6286. /* First, if DR does not exist, trigger UD */
  6287. if (!kvm_require_dr(vcpu, dr))
  6288. return 1;
  6289. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  6290. if (!kvm_require_cpl(vcpu, 0))
  6291. return 1;
  6292. dr7 = vmcs_readl(GUEST_DR7);
  6293. if (dr7 & DR7_GD) {
  6294. /*
  6295. * As the vm-exit takes precedence over the debug trap, we
  6296. * need to emulate the latter, either for the host or the
  6297. * guest debugging itself.
  6298. */
  6299. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6300. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6301. vcpu->run->debug.arch.dr7 = dr7;
  6302. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6303. vcpu->run->debug.arch.exception = DB_VECTOR;
  6304. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6305. return 0;
  6306. } else {
  6307. vcpu->arch.dr6 &= ~15;
  6308. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6309. kvm_queue_exception(vcpu, DB_VECTOR);
  6310. return 1;
  6311. }
  6312. }
  6313. if (vcpu->guest_debug == 0) {
  6314. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6315. CPU_BASED_MOV_DR_EXITING);
  6316. /*
  6317. * No more DR vmexits; force a reload of the debug registers
  6318. * and reenter on this instruction. The next vmexit will
  6319. * retrieve the full state of the debug registers.
  6320. */
  6321. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6322. return 1;
  6323. }
  6324. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6325. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6326. unsigned long val;
  6327. if (kvm_get_dr(vcpu, dr, &val))
  6328. return 1;
  6329. kvm_register_write(vcpu, reg, val);
  6330. } else
  6331. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6332. return 1;
  6333. return kvm_skip_emulated_instruction(vcpu);
  6334. }
  6335. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6336. {
  6337. return vcpu->arch.dr6;
  6338. }
  6339. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6340. {
  6341. }
  6342. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6343. {
  6344. get_debugreg(vcpu->arch.db[0], 0);
  6345. get_debugreg(vcpu->arch.db[1], 1);
  6346. get_debugreg(vcpu->arch.db[2], 2);
  6347. get_debugreg(vcpu->arch.db[3], 3);
  6348. get_debugreg(vcpu->arch.dr6, 6);
  6349. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6350. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6351. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6352. }
  6353. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6354. {
  6355. vmcs_writel(GUEST_DR7, val);
  6356. }
  6357. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6358. {
  6359. return kvm_emulate_cpuid(vcpu);
  6360. }
  6361. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6362. {
  6363. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6364. struct msr_data msr_info;
  6365. msr_info.index = ecx;
  6366. msr_info.host_initiated = false;
  6367. if (vmx_get_msr(vcpu, &msr_info)) {
  6368. trace_kvm_msr_read_ex(ecx);
  6369. kvm_inject_gp(vcpu, 0);
  6370. return 1;
  6371. }
  6372. trace_kvm_msr_read(ecx, msr_info.data);
  6373. /* FIXME: handling of bits 32:63 of rax, rdx */
  6374. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6375. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6376. return kvm_skip_emulated_instruction(vcpu);
  6377. }
  6378. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6379. {
  6380. struct msr_data msr;
  6381. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6382. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6383. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6384. msr.data = data;
  6385. msr.index = ecx;
  6386. msr.host_initiated = false;
  6387. if (kvm_set_msr(vcpu, &msr) != 0) {
  6388. trace_kvm_msr_write_ex(ecx, data);
  6389. kvm_inject_gp(vcpu, 0);
  6390. return 1;
  6391. }
  6392. trace_kvm_msr_write(ecx, data);
  6393. return kvm_skip_emulated_instruction(vcpu);
  6394. }
  6395. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6396. {
  6397. kvm_apic_update_ppr(vcpu);
  6398. return 1;
  6399. }
  6400. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6401. {
  6402. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6403. CPU_BASED_VIRTUAL_INTR_PENDING);
  6404. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6405. ++vcpu->stat.irq_window_exits;
  6406. return 1;
  6407. }
  6408. static int handle_halt(struct kvm_vcpu *vcpu)
  6409. {
  6410. return kvm_emulate_halt(vcpu);
  6411. }
  6412. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6413. {
  6414. return kvm_emulate_hypercall(vcpu);
  6415. }
  6416. static int handle_invd(struct kvm_vcpu *vcpu)
  6417. {
  6418. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6419. }
  6420. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6421. {
  6422. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6423. kvm_mmu_invlpg(vcpu, exit_qualification);
  6424. return kvm_skip_emulated_instruction(vcpu);
  6425. }
  6426. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6427. {
  6428. int err;
  6429. err = kvm_rdpmc(vcpu);
  6430. return kvm_complete_insn_gp(vcpu, err);
  6431. }
  6432. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6433. {
  6434. return kvm_emulate_wbinvd(vcpu);
  6435. }
  6436. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6437. {
  6438. u64 new_bv = kvm_read_edx_eax(vcpu);
  6439. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6440. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6441. return kvm_skip_emulated_instruction(vcpu);
  6442. return 1;
  6443. }
  6444. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6445. {
  6446. kvm_skip_emulated_instruction(vcpu);
  6447. WARN(1, "this should never happen\n");
  6448. return 1;
  6449. }
  6450. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6451. {
  6452. kvm_skip_emulated_instruction(vcpu);
  6453. WARN(1, "this should never happen\n");
  6454. return 1;
  6455. }
  6456. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6457. {
  6458. if (likely(fasteoi)) {
  6459. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6460. int access_type, offset;
  6461. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6462. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6463. /*
  6464. * Sane guest uses MOV to write EOI, with written value
  6465. * not cared. So make a short-circuit here by avoiding
  6466. * heavy instruction emulation.
  6467. */
  6468. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6469. (offset == APIC_EOI)) {
  6470. kvm_lapic_set_eoi(vcpu);
  6471. return kvm_skip_emulated_instruction(vcpu);
  6472. }
  6473. }
  6474. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6475. }
  6476. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6477. {
  6478. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6479. int vector = exit_qualification & 0xff;
  6480. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6481. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6482. return 1;
  6483. }
  6484. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6485. {
  6486. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6487. u32 offset = exit_qualification & 0xfff;
  6488. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6489. kvm_apic_write_nodecode(vcpu, offset);
  6490. return 1;
  6491. }
  6492. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6493. {
  6494. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6495. unsigned long exit_qualification;
  6496. bool has_error_code = false;
  6497. u32 error_code = 0;
  6498. u16 tss_selector;
  6499. int reason, type, idt_v, idt_index;
  6500. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6501. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6502. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6503. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6504. reason = (u32)exit_qualification >> 30;
  6505. if (reason == TASK_SWITCH_GATE && idt_v) {
  6506. switch (type) {
  6507. case INTR_TYPE_NMI_INTR:
  6508. vcpu->arch.nmi_injected = false;
  6509. vmx_set_nmi_mask(vcpu, true);
  6510. break;
  6511. case INTR_TYPE_EXT_INTR:
  6512. case INTR_TYPE_SOFT_INTR:
  6513. kvm_clear_interrupt_queue(vcpu);
  6514. break;
  6515. case INTR_TYPE_HARD_EXCEPTION:
  6516. if (vmx->idt_vectoring_info &
  6517. VECTORING_INFO_DELIVER_CODE_MASK) {
  6518. has_error_code = true;
  6519. error_code =
  6520. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6521. }
  6522. /* fall through */
  6523. case INTR_TYPE_SOFT_EXCEPTION:
  6524. kvm_clear_exception_queue(vcpu);
  6525. break;
  6526. default:
  6527. break;
  6528. }
  6529. }
  6530. tss_selector = exit_qualification;
  6531. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6532. type != INTR_TYPE_EXT_INTR &&
  6533. type != INTR_TYPE_NMI_INTR))
  6534. skip_emulated_instruction(vcpu);
  6535. if (kvm_task_switch(vcpu, tss_selector,
  6536. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6537. has_error_code, error_code) == EMULATE_FAIL) {
  6538. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6539. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6540. vcpu->run->internal.ndata = 0;
  6541. return 0;
  6542. }
  6543. /*
  6544. * TODO: What about debug traps on tss switch?
  6545. * Are we supposed to inject them and update dr6?
  6546. */
  6547. return 1;
  6548. }
  6549. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6550. {
  6551. unsigned long exit_qualification;
  6552. gpa_t gpa;
  6553. u64 error_code;
  6554. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6555. /*
  6556. * EPT violation happened while executing iret from NMI,
  6557. * "blocked by NMI" bit has to be set before next VM entry.
  6558. * There are errata that may cause this bit to not be set:
  6559. * AAK134, BY25.
  6560. */
  6561. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6562. enable_vnmi &&
  6563. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6564. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6565. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6566. trace_kvm_page_fault(gpa, exit_qualification);
  6567. /* Is it a read fault? */
  6568. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6569. ? PFERR_USER_MASK : 0;
  6570. /* Is it a write fault? */
  6571. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6572. ? PFERR_WRITE_MASK : 0;
  6573. /* Is it a fetch fault? */
  6574. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6575. ? PFERR_FETCH_MASK : 0;
  6576. /* ept page table entry is present? */
  6577. error_code |= (exit_qualification &
  6578. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6579. EPT_VIOLATION_EXECUTABLE))
  6580. ? PFERR_PRESENT_MASK : 0;
  6581. error_code |= (exit_qualification & 0x100) != 0 ?
  6582. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6583. vcpu->arch.exit_qualification = exit_qualification;
  6584. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6585. }
  6586. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6587. {
  6588. gpa_t gpa;
  6589. /*
  6590. * A nested guest cannot optimize MMIO vmexits, because we have an
  6591. * nGPA here instead of the required GPA.
  6592. */
  6593. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6594. if (!is_guest_mode(vcpu) &&
  6595. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6596. trace_kvm_fast_mmio(gpa);
  6597. /*
  6598. * Doing kvm_skip_emulated_instruction() depends on undefined
  6599. * behavior: Intel's manual doesn't mandate
  6600. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6601. * occurs and while on real hardware it was observed to be set,
  6602. * other hypervisors (namely Hyper-V) don't set it, we end up
  6603. * advancing IP with some random value. Disable fast mmio when
  6604. * running nested and keep it for real hardware in hope that
  6605. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6606. */
  6607. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6608. return kvm_skip_emulated_instruction(vcpu);
  6609. else
  6610. return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
  6611. EMULATE_DONE;
  6612. }
  6613. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6614. }
  6615. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6616. {
  6617. WARN_ON_ONCE(!enable_vnmi);
  6618. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6619. CPU_BASED_VIRTUAL_NMI_PENDING);
  6620. ++vcpu->stat.nmi_window_exits;
  6621. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6622. return 1;
  6623. }
  6624. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6625. {
  6626. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6627. enum emulation_result err = EMULATE_DONE;
  6628. int ret = 1;
  6629. u32 cpu_exec_ctrl;
  6630. bool intr_window_requested;
  6631. unsigned count = 130;
  6632. /*
  6633. * We should never reach the point where we are emulating L2
  6634. * due to invalid guest state as that means we incorrectly
  6635. * allowed a nested VMEntry with an invalid vmcs12.
  6636. */
  6637. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6638. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6639. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6640. while (vmx->emulation_required && count-- != 0) {
  6641. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6642. return handle_interrupt_window(&vmx->vcpu);
  6643. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6644. return 1;
  6645. err = kvm_emulate_instruction(vcpu, 0);
  6646. if (err == EMULATE_USER_EXIT) {
  6647. ++vcpu->stat.mmio_exits;
  6648. ret = 0;
  6649. goto out;
  6650. }
  6651. if (err != EMULATE_DONE)
  6652. goto emulation_error;
  6653. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6654. vcpu->arch.exception.pending)
  6655. goto emulation_error;
  6656. if (vcpu->arch.halt_request) {
  6657. vcpu->arch.halt_request = 0;
  6658. ret = kvm_vcpu_halt(vcpu);
  6659. goto out;
  6660. }
  6661. if (signal_pending(current))
  6662. goto out;
  6663. if (need_resched())
  6664. schedule();
  6665. }
  6666. out:
  6667. return ret;
  6668. emulation_error:
  6669. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6670. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6671. vcpu->run->internal.ndata = 0;
  6672. return 0;
  6673. }
  6674. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6675. {
  6676. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6677. int old = vmx->ple_window;
  6678. vmx->ple_window = __grow_ple_window(old, ple_window,
  6679. ple_window_grow,
  6680. ple_window_max);
  6681. if (vmx->ple_window != old)
  6682. vmx->ple_window_dirty = true;
  6683. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6684. }
  6685. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6686. {
  6687. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6688. int old = vmx->ple_window;
  6689. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6690. ple_window_shrink,
  6691. ple_window);
  6692. if (vmx->ple_window != old)
  6693. vmx->ple_window_dirty = true;
  6694. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6695. }
  6696. /*
  6697. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6698. */
  6699. static void wakeup_handler(void)
  6700. {
  6701. struct kvm_vcpu *vcpu;
  6702. int cpu = smp_processor_id();
  6703. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6704. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6705. blocked_vcpu_list) {
  6706. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6707. if (pi_test_on(pi_desc) == 1)
  6708. kvm_vcpu_kick(vcpu);
  6709. }
  6710. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6711. }
  6712. static void vmx_enable_tdp(void)
  6713. {
  6714. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6715. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6716. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6717. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6718. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6719. VMX_EPT_RWX_MASK, 0ull);
  6720. ept_set_mmio_spte_mask();
  6721. kvm_enable_tdp();
  6722. }
  6723. static __init int hardware_setup(void)
  6724. {
  6725. unsigned long host_bndcfgs;
  6726. int r = -ENOMEM, i;
  6727. rdmsrl_safe(MSR_EFER, &host_efer);
  6728. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6729. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6730. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6731. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6732. if (!vmx_bitmap[i])
  6733. goto out;
  6734. }
  6735. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6736. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6737. if (setup_vmcs_config(&vmcs_config) < 0) {
  6738. r = -EIO;
  6739. goto out;
  6740. }
  6741. if (boot_cpu_has(X86_FEATURE_NX))
  6742. kvm_enable_efer_bits(EFER_NX);
  6743. if (boot_cpu_has(X86_FEATURE_MPX)) {
  6744. rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
  6745. WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
  6746. }
  6747. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6748. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6749. enable_vpid = 0;
  6750. if (!cpu_has_vmx_ept() ||
  6751. !cpu_has_vmx_ept_4levels() ||
  6752. !cpu_has_vmx_ept_mt_wb() ||
  6753. !cpu_has_vmx_invept_global())
  6754. enable_ept = 0;
  6755. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6756. enable_ept_ad_bits = 0;
  6757. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6758. enable_unrestricted_guest = 0;
  6759. if (!cpu_has_vmx_flexpriority())
  6760. flexpriority_enabled = 0;
  6761. if (!cpu_has_virtual_nmis())
  6762. enable_vnmi = 0;
  6763. /*
  6764. * set_apic_access_page_addr() is used to reload apic access
  6765. * page upon invalidation. No need to do anything if not
  6766. * using the APIC_ACCESS_ADDR VMCS field.
  6767. */
  6768. if (!flexpriority_enabled)
  6769. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6770. if (!cpu_has_vmx_tpr_shadow())
  6771. kvm_x86_ops->update_cr8_intercept = NULL;
  6772. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6773. kvm_disable_largepages();
  6774. #if IS_ENABLED(CONFIG_HYPERV)
  6775. if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
  6776. && enable_ept)
  6777. kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
  6778. #endif
  6779. if (!cpu_has_vmx_ple()) {
  6780. ple_gap = 0;
  6781. ple_window = 0;
  6782. ple_window_grow = 0;
  6783. ple_window_max = 0;
  6784. ple_window_shrink = 0;
  6785. }
  6786. if (!cpu_has_vmx_apicv()) {
  6787. enable_apicv = 0;
  6788. kvm_x86_ops->sync_pir_to_irr = NULL;
  6789. }
  6790. if (cpu_has_vmx_tsc_scaling()) {
  6791. kvm_has_tsc_control = true;
  6792. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6793. kvm_tsc_scaling_ratio_frac_bits = 48;
  6794. }
  6795. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6796. if (enable_ept)
  6797. vmx_enable_tdp();
  6798. else
  6799. kvm_disable_tdp();
  6800. if (!nested) {
  6801. kvm_x86_ops->get_nested_state = NULL;
  6802. kvm_x86_ops->set_nested_state = NULL;
  6803. }
  6804. /*
  6805. * Only enable PML when hardware supports PML feature, and both EPT
  6806. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6807. */
  6808. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6809. enable_pml = 0;
  6810. if (!enable_pml) {
  6811. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6812. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6813. kvm_x86_ops->flush_log_dirty = NULL;
  6814. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6815. }
  6816. if (!cpu_has_vmx_preemption_timer())
  6817. kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
  6818. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6819. u64 vmx_msr;
  6820. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6821. cpu_preemption_timer_multi =
  6822. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6823. } else {
  6824. kvm_x86_ops->set_hv_timer = NULL;
  6825. kvm_x86_ops->cancel_hv_timer = NULL;
  6826. }
  6827. if (!cpu_has_vmx_shadow_vmcs())
  6828. enable_shadow_vmcs = 0;
  6829. if (enable_shadow_vmcs)
  6830. init_vmcs_shadow_fields();
  6831. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6832. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6833. kvm_mce_cap_supported |= MCG_LMCE_P;
  6834. r = alloc_kvm_area();
  6835. if (r)
  6836. goto out;
  6837. return 0;
  6838. out:
  6839. for (i = 0; i < VMX_BITMAP_NR; i++)
  6840. free_page((unsigned long)vmx_bitmap[i]);
  6841. return r;
  6842. }
  6843. static __exit void hardware_unsetup(void)
  6844. {
  6845. int i;
  6846. for (i = 0; i < VMX_BITMAP_NR; i++)
  6847. free_page((unsigned long)vmx_bitmap[i]);
  6848. free_kvm_area();
  6849. }
  6850. /*
  6851. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6852. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6853. */
  6854. static int handle_pause(struct kvm_vcpu *vcpu)
  6855. {
  6856. if (!kvm_pause_in_guest(vcpu->kvm))
  6857. grow_ple_window(vcpu);
  6858. /*
  6859. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6860. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6861. * never set PAUSE_EXITING and just set PLE if supported,
  6862. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6863. */
  6864. kvm_vcpu_on_spin(vcpu, true);
  6865. return kvm_skip_emulated_instruction(vcpu);
  6866. }
  6867. static int handle_nop(struct kvm_vcpu *vcpu)
  6868. {
  6869. return kvm_skip_emulated_instruction(vcpu);
  6870. }
  6871. static int handle_mwait(struct kvm_vcpu *vcpu)
  6872. {
  6873. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6874. return handle_nop(vcpu);
  6875. }
  6876. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6877. {
  6878. kvm_queue_exception(vcpu, UD_VECTOR);
  6879. return 1;
  6880. }
  6881. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6882. {
  6883. return 1;
  6884. }
  6885. static int handle_monitor(struct kvm_vcpu *vcpu)
  6886. {
  6887. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6888. return handle_nop(vcpu);
  6889. }
  6890. /*
  6891. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6892. * set the success or error code of an emulated VMX instruction, as specified
  6893. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6894. */
  6895. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6896. {
  6897. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6898. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6899. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6900. }
  6901. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6902. {
  6903. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6904. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6905. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6906. | X86_EFLAGS_CF);
  6907. }
  6908. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6909. u32 vm_instruction_error)
  6910. {
  6911. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6912. /*
  6913. * failValid writes the error number to the current VMCS, which
  6914. * can't be done there isn't a current VMCS.
  6915. */
  6916. nested_vmx_failInvalid(vcpu);
  6917. return;
  6918. }
  6919. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6920. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6921. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6922. | X86_EFLAGS_ZF);
  6923. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6924. /*
  6925. * We don't need to force a shadow sync because
  6926. * VM_INSTRUCTION_ERROR is not shadowed
  6927. */
  6928. }
  6929. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6930. {
  6931. /* TODO: not to reset guest simply here. */
  6932. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6933. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6934. }
  6935. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6936. {
  6937. struct vcpu_vmx *vmx =
  6938. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6939. vmx->nested.preemption_timer_expired = true;
  6940. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6941. kvm_vcpu_kick(&vmx->vcpu);
  6942. return HRTIMER_NORESTART;
  6943. }
  6944. /*
  6945. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6946. * exit caused by such an instruction (run by a guest hypervisor).
  6947. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6948. * #UD or #GP.
  6949. */
  6950. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6951. unsigned long exit_qualification,
  6952. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6953. {
  6954. gva_t off;
  6955. bool exn;
  6956. struct kvm_segment s;
  6957. /*
  6958. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6959. * Execution", on an exit, vmx_instruction_info holds most of the
  6960. * addressing components of the operand. Only the displacement part
  6961. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6962. * For how an actual address is calculated from all these components,
  6963. * refer to Vol. 1, "Operand Addressing".
  6964. */
  6965. int scaling = vmx_instruction_info & 3;
  6966. int addr_size = (vmx_instruction_info >> 7) & 7;
  6967. bool is_reg = vmx_instruction_info & (1u << 10);
  6968. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6969. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6970. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6971. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6972. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6973. if (is_reg) {
  6974. kvm_queue_exception(vcpu, UD_VECTOR);
  6975. return 1;
  6976. }
  6977. /* Addr = segment_base + offset */
  6978. /* offset = base + [index * scale] + displacement */
  6979. off = exit_qualification; /* holds the displacement */
  6980. if (addr_size == 1)
  6981. off = (gva_t)sign_extend64(off, 31);
  6982. else if (addr_size == 0)
  6983. off = (gva_t)sign_extend64(off, 15);
  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. /*
  6990. * The effective address, i.e. @off, of a memory operand is truncated
  6991. * based on the address size of the instruction. Note that this is
  6992. * the *effective address*, i.e. the address prior to accounting for
  6993. * the segment's base.
  6994. */
  6995. if (addr_size == 1) /* 32 bit */
  6996. off &= 0xffffffff;
  6997. else if (addr_size == 0) /* 16 bit */
  6998. off &= 0xffff;
  6999. /* Checks for #GP/#SS exceptions. */
  7000. exn = false;
  7001. if (is_long_mode(vcpu)) {
  7002. /*
  7003. * The virtual/linear address is never truncated in 64-bit
  7004. * mode, e.g. a 32-bit address size can yield a 64-bit virtual
  7005. * address when using FS/GS with a non-zero base.
  7006. */
  7007. *ret = s.base + off;
  7008. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  7009. * non-canonical form. This is the only check on the memory
  7010. * destination for long mode!
  7011. */
  7012. exn = is_noncanonical_address(*ret, vcpu);
  7013. } else if (is_protmode(vcpu)) {
  7014. /*
  7015. * When not in long mode, the virtual/linear address is
  7016. * unconditionally truncated to 32 bits regardless of the
  7017. * address size.
  7018. */
  7019. *ret = (s.base + off) & 0xffffffff;
  7020. /* Protected mode: apply checks for segment validity in the
  7021. * following order:
  7022. * - segment type check (#GP(0) may be thrown)
  7023. * - usability check (#GP(0)/#SS(0))
  7024. * - limit check (#GP(0)/#SS(0))
  7025. */
  7026. if (wr)
  7027. /* #GP(0) if the destination operand is located in a
  7028. * read-only data segment or any code segment.
  7029. */
  7030. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  7031. else
  7032. /* #GP(0) if the source operand is located in an
  7033. * execute-only code segment
  7034. */
  7035. exn = ((s.type & 0xa) == 8);
  7036. if (exn) {
  7037. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  7038. return 1;
  7039. }
  7040. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  7041. */
  7042. exn = (s.unusable != 0);
  7043. /*
  7044. * Protected mode: #GP(0)/#SS(0) if the memory operand is
  7045. * outside the segment limit. All CPUs that support VMX ignore
  7046. * limit checks for flat segments, i.e. segments with base==0,
  7047. * limit==0xffffffff and of type expand-up data or code.
  7048. */
  7049. if (!(s.base == 0 && s.limit == 0xffffffff &&
  7050. ((s.type & 8) || !(s.type & 4))))
  7051. exn = exn || (off + sizeof(u64) > s.limit);
  7052. }
  7053. if (exn) {
  7054. kvm_queue_exception_e(vcpu,
  7055. seg_reg == VCPU_SREG_SS ?
  7056. SS_VECTOR : GP_VECTOR,
  7057. 0);
  7058. return 1;
  7059. }
  7060. return 0;
  7061. }
  7062. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  7063. {
  7064. gva_t gva;
  7065. struct x86_exception e;
  7066. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7067. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  7068. return 1;
  7069. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  7070. kvm_inject_page_fault(vcpu, &e);
  7071. return 1;
  7072. }
  7073. return 0;
  7074. }
  7075. /*
  7076. * Allocate a shadow VMCS and associate it with the currently loaded
  7077. * VMCS, unless such a shadow VMCS already exists. The newly allocated
  7078. * VMCS is also VMCLEARed, so that it is ready for use.
  7079. */
  7080. static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
  7081. {
  7082. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7083. struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
  7084. /*
  7085. * We should allocate a shadow vmcs for vmcs01 only when L1
  7086. * executes VMXON and free it when L1 executes VMXOFF.
  7087. * As it is invalid to execute VMXON twice, we shouldn't reach
  7088. * here when vmcs01 already have an allocated shadow vmcs.
  7089. */
  7090. WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
  7091. if (!loaded_vmcs->shadow_vmcs) {
  7092. loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
  7093. if (loaded_vmcs->shadow_vmcs)
  7094. vmcs_clear(loaded_vmcs->shadow_vmcs);
  7095. }
  7096. return loaded_vmcs->shadow_vmcs;
  7097. }
  7098. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  7099. {
  7100. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7101. int r;
  7102. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  7103. if (r < 0)
  7104. goto out_vmcs02;
  7105. vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
  7106. if (!vmx->nested.cached_vmcs12)
  7107. goto out_cached_vmcs12;
  7108. vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
  7109. if (!vmx->nested.cached_shadow_vmcs12)
  7110. goto out_cached_shadow_vmcs12;
  7111. if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
  7112. goto out_shadow_vmcs;
  7113. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  7114. HRTIMER_MODE_REL_PINNED);
  7115. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  7116. vmx->nested.vpid02 = allocate_vpid();
  7117. vmx->nested.vmxon = true;
  7118. return 0;
  7119. out_shadow_vmcs:
  7120. kfree(vmx->nested.cached_shadow_vmcs12);
  7121. out_cached_shadow_vmcs12:
  7122. kfree(vmx->nested.cached_vmcs12);
  7123. out_cached_vmcs12:
  7124. free_loaded_vmcs(&vmx->nested.vmcs02);
  7125. out_vmcs02:
  7126. return -ENOMEM;
  7127. }
  7128. /*
  7129. * Emulate the VMXON instruction.
  7130. * Currently, we just remember that VMX is active, and do not save or even
  7131. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  7132. * do not currently need to store anything in that guest-allocated memory
  7133. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  7134. * argument is different from the VMXON pointer (which the spec says they do).
  7135. */
  7136. static int handle_vmon(struct kvm_vcpu *vcpu)
  7137. {
  7138. int ret;
  7139. gpa_t vmptr;
  7140. struct page *page;
  7141. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7142. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  7143. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  7144. /*
  7145. * The Intel VMX Instruction Reference lists a bunch of bits that are
  7146. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  7147. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  7148. * Otherwise, we should fail with #UD. But most faulting conditions
  7149. * have already been checked by hardware, prior to the VM-exit for
  7150. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  7151. * that bit set to 1 in non-root mode.
  7152. */
  7153. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  7154. kvm_queue_exception(vcpu, UD_VECTOR);
  7155. return 1;
  7156. }
  7157. /* CPL=0 must be checked manually. */
  7158. if (vmx_get_cpl(vcpu)) {
  7159. kvm_inject_gp(vcpu, 0);
  7160. return 1;
  7161. }
  7162. if (vmx->nested.vmxon) {
  7163. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  7164. return kvm_skip_emulated_instruction(vcpu);
  7165. }
  7166. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  7167. != VMXON_NEEDED_FEATURES) {
  7168. kvm_inject_gp(vcpu, 0);
  7169. return 1;
  7170. }
  7171. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7172. return 1;
  7173. /*
  7174. * SDM 3: 24.11.5
  7175. * The first 4 bytes of VMXON region contain the supported
  7176. * VMCS revision identifier
  7177. *
  7178. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  7179. * which replaces physical address width with 32
  7180. */
  7181. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7182. nested_vmx_failInvalid(vcpu);
  7183. return kvm_skip_emulated_instruction(vcpu);
  7184. }
  7185. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7186. if (is_error_page(page)) {
  7187. nested_vmx_failInvalid(vcpu);
  7188. return kvm_skip_emulated_instruction(vcpu);
  7189. }
  7190. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  7191. kunmap(page);
  7192. kvm_release_page_clean(page);
  7193. nested_vmx_failInvalid(vcpu);
  7194. return kvm_skip_emulated_instruction(vcpu);
  7195. }
  7196. kunmap(page);
  7197. kvm_release_page_clean(page);
  7198. vmx->nested.vmxon_ptr = vmptr;
  7199. ret = enter_vmx_operation(vcpu);
  7200. if (ret)
  7201. return ret;
  7202. nested_vmx_succeed(vcpu);
  7203. return kvm_skip_emulated_instruction(vcpu);
  7204. }
  7205. /*
  7206. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  7207. * for running VMX instructions (except VMXON, whose prerequisites are
  7208. * slightly different). It also specifies what exception to inject otherwise.
  7209. * Note that many of these exceptions have priority over VM exits, so they
  7210. * don't have to be checked again here.
  7211. */
  7212. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  7213. {
  7214. if (!to_vmx(vcpu)->nested.vmxon) {
  7215. kvm_queue_exception(vcpu, UD_VECTOR);
  7216. return 0;
  7217. }
  7218. if (vmx_get_cpl(vcpu)) {
  7219. kvm_inject_gp(vcpu, 0);
  7220. return 0;
  7221. }
  7222. return 1;
  7223. }
  7224. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  7225. {
  7226. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  7227. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7228. vmx->nested.sync_shadow_vmcs = false;
  7229. }
  7230. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  7231. {
  7232. if (vmx->nested.current_vmptr == -1ull)
  7233. return;
  7234. if (enable_shadow_vmcs) {
  7235. /* copy to memory all shadowed fields in case
  7236. they were modified */
  7237. copy_shadow_to_vmcs12(vmx);
  7238. vmx_disable_shadow_vmcs(vmx);
  7239. }
  7240. vmx->nested.posted_intr_nv = -1;
  7241. /* Flush VMCS12 to guest memory */
  7242. kvm_vcpu_write_guest_page(&vmx->vcpu,
  7243. vmx->nested.current_vmptr >> PAGE_SHIFT,
  7244. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  7245. vmx->nested.current_vmptr = -1ull;
  7246. }
  7247. /*
  7248. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  7249. * just stops using VMX.
  7250. */
  7251. static void free_nested(struct vcpu_vmx *vmx)
  7252. {
  7253. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  7254. return;
  7255. kvm_clear_request(KVM_REQ_GET_VMCS12_PAGES, &vmx->vcpu);
  7256. hrtimer_cancel(&vmx->nested.preemption_timer);
  7257. vmx->nested.vmxon = false;
  7258. vmx->nested.smm.vmxon = false;
  7259. free_vpid(vmx->nested.vpid02);
  7260. vmx->nested.posted_intr_nv = -1;
  7261. vmx->nested.current_vmptr = -1ull;
  7262. if (enable_shadow_vmcs) {
  7263. vmx_disable_shadow_vmcs(vmx);
  7264. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  7265. free_vmcs(vmx->vmcs01.shadow_vmcs);
  7266. vmx->vmcs01.shadow_vmcs = NULL;
  7267. }
  7268. kfree(vmx->nested.cached_vmcs12);
  7269. kfree(vmx->nested.cached_shadow_vmcs12);
  7270. /* Unpin physical memory we referred to in the vmcs02 */
  7271. if (vmx->nested.apic_access_page) {
  7272. kvm_release_page_dirty(vmx->nested.apic_access_page);
  7273. vmx->nested.apic_access_page = NULL;
  7274. }
  7275. if (vmx->nested.virtual_apic_page) {
  7276. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  7277. vmx->nested.virtual_apic_page = NULL;
  7278. }
  7279. if (vmx->nested.pi_desc_page) {
  7280. kunmap(vmx->nested.pi_desc_page);
  7281. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  7282. vmx->nested.pi_desc_page = NULL;
  7283. vmx->nested.pi_desc = NULL;
  7284. }
  7285. free_loaded_vmcs(&vmx->nested.vmcs02);
  7286. }
  7287. /* Emulate the VMXOFF instruction */
  7288. static int handle_vmoff(struct kvm_vcpu *vcpu)
  7289. {
  7290. if (!nested_vmx_check_permission(vcpu))
  7291. return 1;
  7292. free_nested(to_vmx(vcpu));
  7293. nested_vmx_succeed(vcpu);
  7294. return kvm_skip_emulated_instruction(vcpu);
  7295. }
  7296. /* Emulate the VMCLEAR instruction */
  7297. static int handle_vmclear(struct kvm_vcpu *vcpu)
  7298. {
  7299. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7300. u32 zero = 0;
  7301. gpa_t vmptr;
  7302. if (!nested_vmx_check_permission(vcpu))
  7303. return 1;
  7304. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7305. return 1;
  7306. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7307. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  7308. return kvm_skip_emulated_instruction(vcpu);
  7309. }
  7310. if (vmptr == vmx->nested.vmxon_ptr) {
  7311. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  7312. return kvm_skip_emulated_instruction(vcpu);
  7313. }
  7314. if (vmptr == vmx->nested.current_vmptr)
  7315. nested_release_vmcs12(vmx);
  7316. kvm_vcpu_write_guest(vcpu,
  7317. vmptr + offsetof(struct vmcs12, launch_state),
  7318. &zero, sizeof(zero));
  7319. nested_vmx_succeed(vcpu);
  7320. return kvm_skip_emulated_instruction(vcpu);
  7321. }
  7322. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  7323. /* Emulate the VMLAUNCH instruction */
  7324. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  7325. {
  7326. return nested_vmx_run(vcpu, true);
  7327. }
  7328. /* Emulate the VMRESUME instruction */
  7329. static int handle_vmresume(struct kvm_vcpu *vcpu)
  7330. {
  7331. return nested_vmx_run(vcpu, false);
  7332. }
  7333. /*
  7334. * Read a vmcs12 field. Since these can have varying lengths and we return
  7335. * one type, we chose the biggest type (u64) and zero-extend the return value
  7336. * to that size. Note that the caller, handle_vmread, might need to use only
  7337. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  7338. * 64-bit fields are to be returned).
  7339. */
  7340. static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
  7341. unsigned long field, u64 *ret)
  7342. {
  7343. short offset = vmcs_field_to_offset(field);
  7344. char *p;
  7345. if (offset < 0)
  7346. return offset;
  7347. p = (char *)vmcs12 + offset;
  7348. switch (vmcs_field_width(field)) {
  7349. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7350. *ret = *((natural_width *)p);
  7351. return 0;
  7352. case VMCS_FIELD_WIDTH_U16:
  7353. *ret = *((u16 *)p);
  7354. return 0;
  7355. case VMCS_FIELD_WIDTH_U32:
  7356. *ret = *((u32 *)p);
  7357. return 0;
  7358. case VMCS_FIELD_WIDTH_U64:
  7359. *ret = *((u64 *)p);
  7360. return 0;
  7361. default:
  7362. WARN_ON(1);
  7363. return -ENOENT;
  7364. }
  7365. }
  7366. static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
  7367. unsigned long field, u64 field_value){
  7368. short offset = vmcs_field_to_offset(field);
  7369. char *p = (char *)vmcs12 + offset;
  7370. if (offset < 0)
  7371. return offset;
  7372. switch (vmcs_field_width(field)) {
  7373. case VMCS_FIELD_WIDTH_U16:
  7374. *(u16 *)p = field_value;
  7375. return 0;
  7376. case VMCS_FIELD_WIDTH_U32:
  7377. *(u32 *)p = field_value;
  7378. return 0;
  7379. case VMCS_FIELD_WIDTH_U64:
  7380. *(u64 *)p = field_value;
  7381. return 0;
  7382. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7383. *(natural_width *)p = field_value;
  7384. return 0;
  7385. default:
  7386. WARN_ON(1);
  7387. return -ENOENT;
  7388. }
  7389. }
  7390. /*
  7391. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7392. * they have been modified by the L1 guest. Note that the "read-only"
  7393. * VM-exit information fields are actually writable if the vCPU is
  7394. * configured to support "VMWRITE to any supported field in the VMCS."
  7395. */
  7396. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7397. {
  7398. const u16 *fields[] = {
  7399. shadow_read_write_fields,
  7400. shadow_read_only_fields
  7401. };
  7402. const int max_fields[] = {
  7403. max_shadow_read_write_fields,
  7404. max_shadow_read_only_fields
  7405. };
  7406. int i, q;
  7407. unsigned long field;
  7408. u64 field_value;
  7409. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7410. if (WARN_ON(!shadow_vmcs))
  7411. return;
  7412. preempt_disable();
  7413. vmcs_load(shadow_vmcs);
  7414. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7415. for (i = 0; i < max_fields[q]; i++) {
  7416. field = fields[q][i];
  7417. field_value = __vmcs_readl(field);
  7418. vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
  7419. }
  7420. /*
  7421. * Skip the VM-exit information fields if they are read-only.
  7422. */
  7423. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7424. break;
  7425. }
  7426. vmcs_clear(shadow_vmcs);
  7427. vmcs_load(vmx->loaded_vmcs->vmcs);
  7428. preempt_enable();
  7429. }
  7430. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7431. {
  7432. const u16 *fields[] = {
  7433. shadow_read_write_fields,
  7434. shadow_read_only_fields
  7435. };
  7436. const int max_fields[] = {
  7437. max_shadow_read_write_fields,
  7438. max_shadow_read_only_fields
  7439. };
  7440. int i, q;
  7441. unsigned long field;
  7442. u64 field_value = 0;
  7443. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7444. if (WARN_ON(!shadow_vmcs))
  7445. return;
  7446. vmcs_load(shadow_vmcs);
  7447. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7448. for (i = 0; i < max_fields[q]; i++) {
  7449. field = fields[q][i];
  7450. vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
  7451. __vmcs_writel(field, field_value);
  7452. }
  7453. }
  7454. vmcs_clear(shadow_vmcs);
  7455. vmcs_load(vmx->loaded_vmcs->vmcs);
  7456. }
  7457. /*
  7458. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  7459. * used before) all generate the same failure when it is missing.
  7460. */
  7461. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  7462. {
  7463. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7464. if (vmx->nested.current_vmptr == -1ull) {
  7465. nested_vmx_failInvalid(vcpu);
  7466. return 0;
  7467. }
  7468. return 1;
  7469. }
  7470. static int handle_vmread(struct kvm_vcpu *vcpu)
  7471. {
  7472. unsigned long field;
  7473. u64 field_value;
  7474. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7475. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7476. gva_t gva = 0;
  7477. struct vmcs12 *vmcs12;
  7478. struct x86_exception e;
  7479. if (!nested_vmx_check_permission(vcpu))
  7480. return 1;
  7481. if (!nested_vmx_check_vmcs12(vcpu))
  7482. return kvm_skip_emulated_instruction(vcpu);
  7483. if (!is_guest_mode(vcpu))
  7484. vmcs12 = get_vmcs12(vcpu);
  7485. else {
  7486. /*
  7487. * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
  7488. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7489. */
  7490. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7491. nested_vmx_failInvalid(vcpu);
  7492. return kvm_skip_emulated_instruction(vcpu);
  7493. }
  7494. vmcs12 = get_shadow_vmcs12(vcpu);
  7495. }
  7496. /* Decode instruction info and find the field to read */
  7497. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7498. /* Read the field, zero-extended to a u64 field_value */
  7499. if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
  7500. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7501. return kvm_skip_emulated_instruction(vcpu);
  7502. }
  7503. /*
  7504. * Now copy part of this value to register or memory, as requested.
  7505. * Note that the number of bits actually copied is 32 or 64 depending
  7506. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7507. */
  7508. if (vmx_instruction_info & (1u << 10)) {
  7509. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7510. field_value);
  7511. } else {
  7512. if (get_vmx_mem_address(vcpu, exit_qualification,
  7513. vmx_instruction_info, true, &gva))
  7514. return 1;
  7515. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7516. if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7517. (is_long_mode(vcpu) ? 8 : 4),
  7518. &e)) {
  7519. kvm_inject_page_fault(vcpu, &e);
  7520. return 1;
  7521. }
  7522. }
  7523. nested_vmx_succeed(vcpu);
  7524. return kvm_skip_emulated_instruction(vcpu);
  7525. }
  7526. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7527. {
  7528. unsigned long field;
  7529. gva_t gva;
  7530. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7531. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7532. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7533. /* The value to write might be 32 or 64 bits, depending on L1's long
  7534. * mode, and eventually we need to write that into a field of several
  7535. * possible lengths. The code below first zero-extends the value to 64
  7536. * bit (field_value), and then copies only the appropriate number of
  7537. * bits into the vmcs12 field.
  7538. */
  7539. u64 field_value = 0;
  7540. struct x86_exception e;
  7541. struct vmcs12 *vmcs12;
  7542. if (!nested_vmx_check_permission(vcpu))
  7543. return 1;
  7544. if (!nested_vmx_check_vmcs12(vcpu))
  7545. return kvm_skip_emulated_instruction(vcpu);
  7546. if (vmx_instruction_info & (1u << 10))
  7547. field_value = kvm_register_readl(vcpu,
  7548. (((vmx_instruction_info) >> 3) & 0xf));
  7549. else {
  7550. if (get_vmx_mem_address(vcpu, exit_qualification,
  7551. vmx_instruction_info, false, &gva))
  7552. return 1;
  7553. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7554. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7555. kvm_inject_page_fault(vcpu, &e);
  7556. return 1;
  7557. }
  7558. }
  7559. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7560. /*
  7561. * If the vCPU supports "VMWRITE to any supported field in the
  7562. * VMCS," then the "read-only" fields are actually read/write.
  7563. */
  7564. if (vmcs_field_readonly(field) &&
  7565. !nested_cpu_has_vmwrite_any_field(vcpu)) {
  7566. nested_vmx_failValid(vcpu,
  7567. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7568. return kvm_skip_emulated_instruction(vcpu);
  7569. }
  7570. if (!is_guest_mode(vcpu))
  7571. vmcs12 = get_vmcs12(vcpu);
  7572. else {
  7573. /*
  7574. * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
  7575. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7576. */
  7577. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7578. nested_vmx_failInvalid(vcpu);
  7579. return kvm_skip_emulated_instruction(vcpu);
  7580. }
  7581. vmcs12 = get_shadow_vmcs12(vcpu);
  7582. }
  7583. if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
  7584. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7585. return kvm_skip_emulated_instruction(vcpu);
  7586. }
  7587. /*
  7588. * Do not track vmcs12 dirty-state if in guest-mode
  7589. * as we actually dirty shadow vmcs12 instead of vmcs12.
  7590. */
  7591. if (!is_guest_mode(vcpu)) {
  7592. switch (field) {
  7593. #define SHADOW_FIELD_RW(x) case x:
  7594. #include "vmx_shadow_fields.h"
  7595. /*
  7596. * The fields that can be updated by L1 without a vmexit are
  7597. * always updated in the vmcs02, the others go down the slow
  7598. * path of prepare_vmcs02.
  7599. */
  7600. break;
  7601. default:
  7602. vmx->nested.dirty_vmcs12 = true;
  7603. break;
  7604. }
  7605. }
  7606. nested_vmx_succeed(vcpu);
  7607. return kvm_skip_emulated_instruction(vcpu);
  7608. }
  7609. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7610. {
  7611. vmx->nested.current_vmptr = vmptr;
  7612. if (enable_shadow_vmcs) {
  7613. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7614. SECONDARY_EXEC_SHADOW_VMCS);
  7615. vmcs_write64(VMCS_LINK_POINTER,
  7616. __pa(vmx->vmcs01.shadow_vmcs));
  7617. vmx->nested.sync_shadow_vmcs = true;
  7618. }
  7619. vmx->nested.dirty_vmcs12 = true;
  7620. }
  7621. /* Emulate the VMPTRLD instruction */
  7622. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7623. {
  7624. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7625. gpa_t vmptr;
  7626. if (!nested_vmx_check_permission(vcpu))
  7627. return 1;
  7628. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7629. return 1;
  7630. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7631. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  7632. return kvm_skip_emulated_instruction(vcpu);
  7633. }
  7634. if (vmptr == vmx->nested.vmxon_ptr) {
  7635. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  7636. return kvm_skip_emulated_instruction(vcpu);
  7637. }
  7638. if (vmx->nested.current_vmptr != vmptr) {
  7639. struct vmcs12 *new_vmcs12;
  7640. struct page *page;
  7641. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7642. if (is_error_page(page)) {
  7643. nested_vmx_failInvalid(vcpu);
  7644. return kvm_skip_emulated_instruction(vcpu);
  7645. }
  7646. new_vmcs12 = kmap(page);
  7647. if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  7648. (new_vmcs12->hdr.shadow_vmcs &&
  7649. !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
  7650. kunmap(page);
  7651. kvm_release_page_clean(page);
  7652. nested_vmx_failValid(vcpu,
  7653. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7654. return kvm_skip_emulated_instruction(vcpu);
  7655. }
  7656. nested_release_vmcs12(vmx);
  7657. /*
  7658. * Load VMCS12 from guest memory since it is not already
  7659. * cached.
  7660. */
  7661. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7662. kunmap(page);
  7663. kvm_release_page_clean(page);
  7664. set_current_vmptr(vmx, vmptr);
  7665. }
  7666. nested_vmx_succeed(vcpu);
  7667. return kvm_skip_emulated_instruction(vcpu);
  7668. }
  7669. /* Emulate the VMPTRST instruction */
  7670. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  7671. {
  7672. unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
  7673. u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7674. gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
  7675. struct x86_exception e;
  7676. gva_t gva;
  7677. if (!nested_vmx_check_permission(vcpu))
  7678. return 1;
  7679. if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
  7680. return 1;
  7681. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  7682. if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
  7683. sizeof(gpa_t), &e)) {
  7684. kvm_inject_page_fault(vcpu, &e);
  7685. return 1;
  7686. }
  7687. nested_vmx_succeed(vcpu);
  7688. return kvm_skip_emulated_instruction(vcpu);
  7689. }
  7690. /* Emulate the INVEPT instruction */
  7691. static int handle_invept(struct kvm_vcpu *vcpu)
  7692. {
  7693. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7694. u32 vmx_instruction_info, types;
  7695. unsigned long type;
  7696. gva_t gva;
  7697. struct x86_exception e;
  7698. struct {
  7699. u64 eptp, gpa;
  7700. } operand;
  7701. if (!(vmx->nested.msrs.secondary_ctls_high &
  7702. SECONDARY_EXEC_ENABLE_EPT) ||
  7703. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  7704. kvm_queue_exception(vcpu, UD_VECTOR);
  7705. return 1;
  7706. }
  7707. if (!nested_vmx_check_permission(vcpu))
  7708. return 1;
  7709. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7710. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7711. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  7712. if (type >= 32 || !(types & (1 << type))) {
  7713. nested_vmx_failValid(vcpu,
  7714. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7715. return kvm_skip_emulated_instruction(vcpu);
  7716. }
  7717. /* According to the Intel VMX instruction reference, the memory
  7718. * operand is read even if it isn't needed (e.g., for type==global)
  7719. */
  7720. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7721. vmx_instruction_info, false, &gva))
  7722. return 1;
  7723. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7724. kvm_inject_page_fault(vcpu, &e);
  7725. return 1;
  7726. }
  7727. switch (type) {
  7728. case VMX_EPT_EXTENT_GLOBAL:
  7729. /*
  7730. * TODO: track mappings and invalidate
  7731. * single context requests appropriately
  7732. */
  7733. case VMX_EPT_EXTENT_CONTEXT:
  7734. kvm_mmu_sync_roots(vcpu);
  7735. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7736. nested_vmx_succeed(vcpu);
  7737. break;
  7738. default:
  7739. BUG_ON(1);
  7740. break;
  7741. }
  7742. return kvm_skip_emulated_instruction(vcpu);
  7743. }
  7744. static int handle_invvpid(struct kvm_vcpu *vcpu)
  7745. {
  7746. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7747. u32 vmx_instruction_info;
  7748. unsigned long type, types;
  7749. gva_t gva;
  7750. struct x86_exception e;
  7751. struct {
  7752. u64 vpid;
  7753. u64 gla;
  7754. } operand;
  7755. if (!(vmx->nested.msrs.secondary_ctls_high &
  7756. SECONDARY_EXEC_ENABLE_VPID) ||
  7757. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  7758. kvm_queue_exception(vcpu, UD_VECTOR);
  7759. return 1;
  7760. }
  7761. if (!nested_vmx_check_permission(vcpu))
  7762. return 1;
  7763. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7764. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7765. types = (vmx->nested.msrs.vpid_caps &
  7766. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  7767. if (type >= 32 || !(types & (1 << type))) {
  7768. nested_vmx_failValid(vcpu,
  7769. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7770. return kvm_skip_emulated_instruction(vcpu);
  7771. }
  7772. /* according to the intel vmx instruction reference, the memory
  7773. * operand is read even if it isn't needed (e.g., for type==global)
  7774. */
  7775. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7776. vmx_instruction_info, false, &gva))
  7777. return 1;
  7778. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7779. kvm_inject_page_fault(vcpu, &e);
  7780. return 1;
  7781. }
  7782. if (operand.vpid >> 16) {
  7783. nested_vmx_failValid(vcpu,
  7784. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7785. return kvm_skip_emulated_instruction(vcpu);
  7786. }
  7787. switch (type) {
  7788. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  7789. if (!operand.vpid ||
  7790. is_noncanonical_address(operand.gla, vcpu)) {
  7791. nested_vmx_failValid(vcpu,
  7792. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7793. return kvm_skip_emulated_instruction(vcpu);
  7794. }
  7795. if (cpu_has_vmx_invvpid_individual_addr() &&
  7796. vmx->nested.vpid02) {
  7797. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  7798. vmx->nested.vpid02, operand.gla);
  7799. } else
  7800. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7801. break;
  7802. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  7803. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  7804. if (!operand.vpid) {
  7805. nested_vmx_failValid(vcpu,
  7806. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7807. return kvm_skip_emulated_instruction(vcpu);
  7808. }
  7809. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7810. break;
  7811. case VMX_VPID_EXTENT_ALL_CONTEXT:
  7812. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7813. break;
  7814. default:
  7815. WARN_ON_ONCE(1);
  7816. return kvm_skip_emulated_instruction(vcpu);
  7817. }
  7818. nested_vmx_succeed(vcpu);
  7819. return kvm_skip_emulated_instruction(vcpu);
  7820. }
  7821. static int handle_invpcid(struct kvm_vcpu *vcpu)
  7822. {
  7823. u32 vmx_instruction_info;
  7824. unsigned long type;
  7825. bool pcid_enabled;
  7826. gva_t gva;
  7827. struct x86_exception e;
  7828. unsigned i;
  7829. unsigned long roots_to_free = 0;
  7830. struct {
  7831. u64 pcid;
  7832. u64 gla;
  7833. } operand;
  7834. if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
  7835. kvm_queue_exception(vcpu, UD_VECTOR);
  7836. return 1;
  7837. }
  7838. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7839. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7840. if (type > 3) {
  7841. kvm_inject_gp(vcpu, 0);
  7842. return 1;
  7843. }
  7844. /* According to the Intel instruction reference, the memory operand
  7845. * is read even if it isn't needed (e.g., for type==all)
  7846. */
  7847. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7848. vmx_instruction_info, false, &gva))
  7849. return 1;
  7850. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7851. kvm_inject_page_fault(vcpu, &e);
  7852. return 1;
  7853. }
  7854. if (operand.pcid >> 12 != 0) {
  7855. kvm_inject_gp(vcpu, 0);
  7856. return 1;
  7857. }
  7858. pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
  7859. switch (type) {
  7860. case INVPCID_TYPE_INDIV_ADDR:
  7861. if ((!pcid_enabled && (operand.pcid != 0)) ||
  7862. is_noncanonical_address(operand.gla, vcpu)) {
  7863. kvm_inject_gp(vcpu, 0);
  7864. return 1;
  7865. }
  7866. kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
  7867. return kvm_skip_emulated_instruction(vcpu);
  7868. case INVPCID_TYPE_SINGLE_CTXT:
  7869. if (!pcid_enabled && (operand.pcid != 0)) {
  7870. kvm_inject_gp(vcpu, 0);
  7871. return 1;
  7872. }
  7873. if (kvm_get_active_pcid(vcpu) == operand.pcid) {
  7874. kvm_mmu_sync_roots(vcpu);
  7875. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7876. }
  7877. for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
  7878. if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
  7879. == operand.pcid)
  7880. roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
  7881. kvm_mmu_free_roots(vcpu, roots_to_free);
  7882. /*
  7883. * If neither the current cr3 nor any of the prev_roots use the
  7884. * given PCID, then nothing needs to be done here because a
  7885. * resync will happen anyway before switching to any other CR3.
  7886. */
  7887. return kvm_skip_emulated_instruction(vcpu);
  7888. case INVPCID_TYPE_ALL_NON_GLOBAL:
  7889. /*
  7890. * Currently, KVM doesn't mark global entries in the shadow
  7891. * page tables, so a non-global flush just degenerates to a
  7892. * global flush. If needed, we could optimize this later by
  7893. * keeping track of global entries in shadow page tables.
  7894. */
  7895. /* fall-through */
  7896. case INVPCID_TYPE_ALL_INCL_GLOBAL:
  7897. kvm_mmu_unload(vcpu);
  7898. return kvm_skip_emulated_instruction(vcpu);
  7899. default:
  7900. BUG(); /* We have already checked above that type <= 3 */
  7901. }
  7902. }
  7903. static int handle_pml_full(struct kvm_vcpu *vcpu)
  7904. {
  7905. unsigned long exit_qualification;
  7906. trace_kvm_pml_full(vcpu->vcpu_id);
  7907. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7908. /*
  7909. * PML buffer FULL happened while executing iret from NMI,
  7910. * "blocked by NMI" bit has to be set before next VM entry.
  7911. */
  7912. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7913. enable_vnmi &&
  7914. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  7915. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7916. GUEST_INTR_STATE_NMI);
  7917. /*
  7918. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  7919. * here.., and there's no userspace involvement needed for PML.
  7920. */
  7921. return 1;
  7922. }
  7923. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  7924. {
  7925. if (!to_vmx(vcpu)->req_immediate_exit)
  7926. kvm_lapic_expired_hv_timer(vcpu);
  7927. return 1;
  7928. }
  7929. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  7930. {
  7931. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7932. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7933. /* Check for memory type validity */
  7934. switch (address & VMX_EPTP_MT_MASK) {
  7935. case VMX_EPTP_MT_UC:
  7936. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  7937. return false;
  7938. break;
  7939. case VMX_EPTP_MT_WB:
  7940. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  7941. return false;
  7942. break;
  7943. default:
  7944. return false;
  7945. }
  7946. /* only 4 levels page-walk length are valid */
  7947. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  7948. return false;
  7949. /* Reserved bits should not be set */
  7950. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  7951. return false;
  7952. /* AD, if set, should be supported */
  7953. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  7954. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  7955. return false;
  7956. }
  7957. return true;
  7958. }
  7959. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  7960. struct vmcs12 *vmcs12)
  7961. {
  7962. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  7963. u64 address;
  7964. bool accessed_dirty;
  7965. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  7966. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  7967. !nested_cpu_has_ept(vmcs12))
  7968. return 1;
  7969. if (index >= VMFUNC_EPTP_ENTRIES)
  7970. return 1;
  7971. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7972. &address, index * 8, 8))
  7973. return 1;
  7974. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7975. /*
  7976. * If the (L2) guest does a vmfunc to the currently
  7977. * active ept pointer, we don't have to do anything else
  7978. */
  7979. if (vmcs12->ept_pointer != address) {
  7980. if (!valid_ept_address(vcpu, address))
  7981. return 1;
  7982. kvm_mmu_unload(vcpu);
  7983. mmu->ept_ad = accessed_dirty;
  7984. mmu->base_role.ad_disabled = !accessed_dirty;
  7985. vmcs12->ept_pointer = address;
  7986. /*
  7987. * TODO: Check what's the correct approach in case
  7988. * mmu reload fails. Currently, we just let the next
  7989. * reload potentially fail
  7990. */
  7991. kvm_mmu_reload(vcpu);
  7992. }
  7993. return 0;
  7994. }
  7995. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7996. {
  7997. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7998. struct vmcs12 *vmcs12;
  7999. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  8000. /*
  8001. * VMFUNC is only supported for nested guests, but we always enable the
  8002. * secondary control for simplicity; for non-nested mode, fake that we
  8003. * didn't by injecting #UD.
  8004. */
  8005. if (!is_guest_mode(vcpu)) {
  8006. kvm_queue_exception(vcpu, UD_VECTOR);
  8007. return 1;
  8008. }
  8009. vmcs12 = get_vmcs12(vcpu);
  8010. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  8011. goto fail;
  8012. switch (function) {
  8013. case 0:
  8014. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  8015. goto fail;
  8016. break;
  8017. default:
  8018. goto fail;
  8019. }
  8020. return kvm_skip_emulated_instruction(vcpu);
  8021. fail:
  8022. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  8023. vmcs_read32(VM_EXIT_INTR_INFO),
  8024. vmcs_readl(EXIT_QUALIFICATION));
  8025. return 1;
  8026. }
  8027. static int handle_encls(struct kvm_vcpu *vcpu)
  8028. {
  8029. /*
  8030. * SGX virtualization is not yet supported. There is no software
  8031. * enable bit for SGX, so we have to trap ENCLS and inject a #UD
  8032. * to prevent the guest from executing ENCLS.
  8033. */
  8034. kvm_queue_exception(vcpu, UD_VECTOR);
  8035. return 1;
  8036. }
  8037. /*
  8038. * The exit handlers return 1 if the exit was handled fully and guest execution
  8039. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  8040. * to be done to userspace and return 0.
  8041. */
  8042. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  8043. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  8044. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  8045. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  8046. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  8047. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  8048. [EXIT_REASON_CR_ACCESS] = handle_cr,
  8049. [EXIT_REASON_DR_ACCESS] = handle_dr,
  8050. [EXIT_REASON_CPUID] = handle_cpuid,
  8051. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  8052. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  8053. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  8054. [EXIT_REASON_HLT] = handle_halt,
  8055. [EXIT_REASON_INVD] = handle_invd,
  8056. [EXIT_REASON_INVLPG] = handle_invlpg,
  8057. [EXIT_REASON_RDPMC] = handle_rdpmc,
  8058. [EXIT_REASON_VMCALL] = handle_vmcall,
  8059. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  8060. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  8061. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  8062. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  8063. [EXIT_REASON_VMREAD] = handle_vmread,
  8064. [EXIT_REASON_VMRESUME] = handle_vmresume,
  8065. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  8066. [EXIT_REASON_VMOFF] = handle_vmoff,
  8067. [EXIT_REASON_VMON] = handle_vmon,
  8068. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  8069. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  8070. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  8071. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  8072. [EXIT_REASON_WBINVD] = handle_wbinvd,
  8073. [EXIT_REASON_XSETBV] = handle_xsetbv,
  8074. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  8075. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  8076. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  8077. [EXIT_REASON_LDTR_TR] = handle_desc,
  8078. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  8079. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  8080. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  8081. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  8082. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  8083. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  8084. [EXIT_REASON_INVEPT] = handle_invept,
  8085. [EXIT_REASON_INVVPID] = handle_invvpid,
  8086. [EXIT_REASON_RDRAND] = handle_invalid_op,
  8087. [EXIT_REASON_RDSEED] = handle_invalid_op,
  8088. [EXIT_REASON_XSAVES] = handle_xsaves,
  8089. [EXIT_REASON_XRSTORS] = handle_xrstors,
  8090. [EXIT_REASON_PML_FULL] = handle_pml_full,
  8091. [EXIT_REASON_INVPCID] = handle_invpcid,
  8092. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  8093. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  8094. [EXIT_REASON_ENCLS] = handle_encls,
  8095. };
  8096. static const int kvm_vmx_max_exit_handlers =
  8097. ARRAY_SIZE(kvm_vmx_exit_handlers);
  8098. /*
  8099. * Return true if an IO instruction with the specified port and size should cause
  8100. * a VM-exit into L1.
  8101. */
  8102. bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
  8103. int size)
  8104. {
  8105. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8106. gpa_t bitmap, last_bitmap;
  8107. u8 b;
  8108. last_bitmap = (gpa_t)-1;
  8109. b = -1;
  8110. while (size > 0) {
  8111. if (port < 0x8000)
  8112. bitmap = vmcs12->io_bitmap_a;
  8113. else if (port < 0x10000)
  8114. bitmap = vmcs12->io_bitmap_b;
  8115. else
  8116. return true;
  8117. bitmap += (port & 0x7fff) / 8;
  8118. if (last_bitmap != bitmap)
  8119. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  8120. return true;
  8121. if (b & (1 << (port & 7)))
  8122. return true;
  8123. port++;
  8124. size--;
  8125. last_bitmap = bitmap;
  8126. }
  8127. return false;
  8128. }
  8129. /*
  8130. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  8131. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  8132. * disinterest in the current event (read or write a specific MSR) by using an
  8133. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  8134. */
  8135. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  8136. struct vmcs12 *vmcs12, u32 exit_reason)
  8137. {
  8138. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  8139. gpa_t bitmap;
  8140. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8141. return true;
  8142. /*
  8143. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  8144. * for the four combinations of read/write and low/high MSR numbers.
  8145. * First we need to figure out which of the four to use:
  8146. */
  8147. bitmap = vmcs12->msr_bitmap;
  8148. if (exit_reason == EXIT_REASON_MSR_WRITE)
  8149. bitmap += 2048;
  8150. if (msr_index >= 0xc0000000) {
  8151. msr_index -= 0xc0000000;
  8152. bitmap += 1024;
  8153. }
  8154. /* Then read the msr_index'th bit from this bitmap: */
  8155. if (msr_index < 1024*8) {
  8156. unsigned char b;
  8157. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  8158. return true;
  8159. return 1 & (b >> (msr_index & 7));
  8160. } else
  8161. return true; /* let L1 handle the wrong parameter */
  8162. }
  8163. /*
  8164. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  8165. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  8166. * intercept (via guest_host_mask etc.) the current event.
  8167. */
  8168. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  8169. struct vmcs12 *vmcs12)
  8170. {
  8171. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8172. int cr = exit_qualification & 15;
  8173. int reg;
  8174. unsigned long val;
  8175. switch ((exit_qualification >> 4) & 3) {
  8176. case 0: /* mov to cr */
  8177. reg = (exit_qualification >> 8) & 15;
  8178. val = kvm_register_readl(vcpu, reg);
  8179. switch (cr) {
  8180. case 0:
  8181. if (vmcs12->cr0_guest_host_mask &
  8182. (val ^ vmcs12->cr0_read_shadow))
  8183. return true;
  8184. break;
  8185. case 3:
  8186. if ((vmcs12->cr3_target_count >= 1 &&
  8187. vmcs12->cr3_target_value0 == val) ||
  8188. (vmcs12->cr3_target_count >= 2 &&
  8189. vmcs12->cr3_target_value1 == val) ||
  8190. (vmcs12->cr3_target_count >= 3 &&
  8191. vmcs12->cr3_target_value2 == val) ||
  8192. (vmcs12->cr3_target_count >= 4 &&
  8193. vmcs12->cr3_target_value3 == val))
  8194. return false;
  8195. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  8196. return true;
  8197. break;
  8198. case 4:
  8199. if (vmcs12->cr4_guest_host_mask &
  8200. (vmcs12->cr4_read_shadow ^ val))
  8201. return true;
  8202. break;
  8203. case 8:
  8204. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  8205. return true;
  8206. break;
  8207. }
  8208. break;
  8209. case 2: /* clts */
  8210. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  8211. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  8212. return true;
  8213. break;
  8214. case 1: /* mov from cr */
  8215. switch (cr) {
  8216. case 3:
  8217. if (vmcs12->cpu_based_vm_exec_control &
  8218. CPU_BASED_CR3_STORE_EXITING)
  8219. return true;
  8220. break;
  8221. case 8:
  8222. if (vmcs12->cpu_based_vm_exec_control &
  8223. CPU_BASED_CR8_STORE_EXITING)
  8224. return true;
  8225. break;
  8226. }
  8227. break;
  8228. case 3: /* lmsw */
  8229. /*
  8230. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  8231. * cr0. Other attempted changes are ignored, with no exit.
  8232. */
  8233. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  8234. if (vmcs12->cr0_guest_host_mask & 0xe &
  8235. (val ^ vmcs12->cr0_read_shadow))
  8236. return true;
  8237. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  8238. !(vmcs12->cr0_read_shadow & 0x1) &&
  8239. (val & 0x1))
  8240. return true;
  8241. break;
  8242. }
  8243. return false;
  8244. }
  8245. static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
  8246. struct vmcs12 *vmcs12, gpa_t bitmap)
  8247. {
  8248. u32 vmx_instruction_info;
  8249. unsigned long field;
  8250. u8 b;
  8251. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  8252. return true;
  8253. /* Decode instruction info and find the field to access */
  8254. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8255. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  8256. /* Out-of-range fields always cause a VM exit from L2 to L1 */
  8257. if (field >> 15)
  8258. return true;
  8259. if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
  8260. return true;
  8261. return 1 & (b >> (field & 7));
  8262. }
  8263. /*
  8264. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  8265. * should handle it ourselves in L0 (and then continue L2). Only call this
  8266. * when in is_guest_mode (L2).
  8267. */
  8268. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  8269. {
  8270. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8271. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8272. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8273. if (vmx->nested.nested_run_pending)
  8274. return false;
  8275. if (unlikely(vmx->fail)) {
  8276. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  8277. vmcs_read32(VM_INSTRUCTION_ERROR));
  8278. return true;
  8279. }
  8280. /*
  8281. * The host physical addresses of some pages of guest memory
  8282. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  8283. * Page). The CPU may write to these pages via their host
  8284. * physical address while L2 is running, bypassing any
  8285. * address-translation-based dirty tracking (e.g. EPT write
  8286. * protection).
  8287. *
  8288. * Mark them dirty on every exit from L2 to prevent them from
  8289. * getting out of sync with dirty tracking.
  8290. */
  8291. nested_mark_vmcs12_pages_dirty(vcpu);
  8292. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  8293. vmcs_readl(EXIT_QUALIFICATION),
  8294. vmx->idt_vectoring_info,
  8295. intr_info,
  8296. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8297. KVM_ISA_VMX);
  8298. switch ((u16)exit_reason) {
  8299. case EXIT_REASON_EXCEPTION_NMI:
  8300. if (is_nmi(intr_info))
  8301. return false;
  8302. else if (is_page_fault(intr_info))
  8303. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  8304. else if (is_no_device(intr_info) &&
  8305. !(vmcs12->guest_cr0 & X86_CR0_TS))
  8306. return false;
  8307. else if (is_debug(intr_info) &&
  8308. vcpu->guest_debug &
  8309. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  8310. return false;
  8311. else if (is_breakpoint(intr_info) &&
  8312. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  8313. return false;
  8314. return vmcs12->exception_bitmap &
  8315. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  8316. case EXIT_REASON_EXTERNAL_INTERRUPT:
  8317. return false;
  8318. case EXIT_REASON_TRIPLE_FAULT:
  8319. return true;
  8320. case EXIT_REASON_PENDING_INTERRUPT:
  8321. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  8322. case EXIT_REASON_NMI_WINDOW:
  8323. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  8324. case EXIT_REASON_TASK_SWITCH:
  8325. return true;
  8326. case EXIT_REASON_CPUID:
  8327. return true;
  8328. case EXIT_REASON_HLT:
  8329. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  8330. case EXIT_REASON_INVD:
  8331. return true;
  8332. case EXIT_REASON_INVLPG:
  8333. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8334. case EXIT_REASON_RDPMC:
  8335. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  8336. case EXIT_REASON_RDRAND:
  8337. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  8338. case EXIT_REASON_RDSEED:
  8339. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  8340. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  8341. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  8342. case EXIT_REASON_VMREAD:
  8343. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8344. vmcs12->vmread_bitmap);
  8345. case EXIT_REASON_VMWRITE:
  8346. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8347. vmcs12->vmwrite_bitmap);
  8348. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  8349. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  8350. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
  8351. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  8352. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  8353. /*
  8354. * VMX instructions trap unconditionally. This allows L1 to
  8355. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  8356. */
  8357. return true;
  8358. case EXIT_REASON_CR_ACCESS:
  8359. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  8360. case EXIT_REASON_DR_ACCESS:
  8361. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  8362. case EXIT_REASON_IO_INSTRUCTION:
  8363. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  8364. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  8365. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  8366. case EXIT_REASON_MSR_READ:
  8367. case EXIT_REASON_MSR_WRITE:
  8368. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  8369. case EXIT_REASON_INVALID_STATE:
  8370. return true;
  8371. case EXIT_REASON_MWAIT_INSTRUCTION:
  8372. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  8373. case EXIT_REASON_MONITOR_TRAP_FLAG:
  8374. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  8375. case EXIT_REASON_MONITOR_INSTRUCTION:
  8376. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  8377. case EXIT_REASON_PAUSE_INSTRUCTION:
  8378. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  8379. nested_cpu_has2(vmcs12,
  8380. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  8381. case EXIT_REASON_MCE_DURING_VMENTRY:
  8382. return false;
  8383. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  8384. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  8385. case EXIT_REASON_APIC_ACCESS:
  8386. case EXIT_REASON_APIC_WRITE:
  8387. case EXIT_REASON_EOI_INDUCED:
  8388. /*
  8389. * The controls for "virtualize APIC accesses," "APIC-
  8390. * register virtualization," and "virtual-interrupt
  8391. * delivery" only come from vmcs12.
  8392. */
  8393. return true;
  8394. case EXIT_REASON_EPT_VIOLATION:
  8395. /*
  8396. * L0 always deals with the EPT violation. If nested EPT is
  8397. * used, and the nested mmu code discovers that the address is
  8398. * missing in the guest EPT table (EPT12), the EPT violation
  8399. * will be injected with nested_ept_inject_page_fault()
  8400. */
  8401. return false;
  8402. case EXIT_REASON_EPT_MISCONFIG:
  8403. /*
  8404. * L2 never uses directly L1's EPT, but rather L0's own EPT
  8405. * table (shadow on EPT) or a merged EPT table that L0 built
  8406. * (EPT on EPT). So any problems with the structure of the
  8407. * table is L0's fault.
  8408. */
  8409. return false;
  8410. case EXIT_REASON_INVPCID:
  8411. return
  8412. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  8413. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8414. case EXIT_REASON_WBINVD:
  8415. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  8416. case EXIT_REASON_XSETBV:
  8417. return true;
  8418. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  8419. /*
  8420. * This should never happen, since it is not possible to
  8421. * set XSS to a non-zero value---neither in L1 nor in L2.
  8422. * If if it were, XSS would have to be checked against
  8423. * the XSS exit bitmap in vmcs12.
  8424. */
  8425. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  8426. case EXIT_REASON_PREEMPTION_TIMER:
  8427. return false;
  8428. case EXIT_REASON_PML_FULL:
  8429. /* We emulate PML support to L1. */
  8430. return false;
  8431. case EXIT_REASON_VMFUNC:
  8432. /* VM functions are emulated through L2->L0 vmexits. */
  8433. return false;
  8434. case EXIT_REASON_ENCLS:
  8435. /* SGX is never exposed to L1 */
  8436. return false;
  8437. default:
  8438. return true;
  8439. }
  8440. }
  8441. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  8442. {
  8443. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8444. /*
  8445. * At this point, the exit interruption info in exit_intr_info
  8446. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  8447. * we need to query the in-kernel LAPIC.
  8448. */
  8449. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  8450. if ((exit_intr_info &
  8451. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8452. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  8453. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8454. vmcs12->vm_exit_intr_error_code =
  8455. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8456. }
  8457. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  8458. vmcs_readl(EXIT_QUALIFICATION));
  8459. return 1;
  8460. }
  8461. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  8462. {
  8463. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  8464. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  8465. }
  8466. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  8467. {
  8468. if (vmx->pml_pg) {
  8469. __free_page(vmx->pml_pg);
  8470. vmx->pml_pg = NULL;
  8471. }
  8472. }
  8473. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  8474. {
  8475. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8476. u64 *pml_buf;
  8477. u16 pml_idx;
  8478. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  8479. /* Do nothing if PML buffer is empty */
  8480. if (pml_idx == (PML_ENTITY_NUM - 1))
  8481. return;
  8482. /* PML index always points to next available PML buffer entity */
  8483. if (pml_idx >= PML_ENTITY_NUM)
  8484. pml_idx = 0;
  8485. else
  8486. pml_idx++;
  8487. pml_buf = page_address(vmx->pml_pg);
  8488. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  8489. u64 gpa;
  8490. gpa = pml_buf[pml_idx];
  8491. WARN_ON(gpa & (PAGE_SIZE - 1));
  8492. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  8493. }
  8494. /* reset PML index */
  8495. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  8496. }
  8497. /*
  8498. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  8499. * Called before reporting dirty_bitmap to userspace.
  8500. */
  8501. static void kvm_flush_pml_buffers(struct kvm *kvm)
  8502. {
  8503. int i;
  8504. struct kvm_vcpu *vcpu;
  8505. /*
  8506. * We only need to kick vcpu out of guest mode here, as PML buffer
  8507. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8508. * vcpus running in guest are possible to have unflushed GPAs in PML
  8509. * buffer.
  8510. */
  8511. kvm_for_each_vcpu(i, vcpu, kvm)
  8512. kvm_vcpu_kick(vcpu);
  8513. }
  8514. static void vmx_dump_sel(char *name, uint32_t sel)
  8515. {
  8516. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8517. name, vmcs_read16(sel),
  8518. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8519. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8520. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8521. }
  8522. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8523. {
  8524. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8525. name, vmcs_read32(limit),
  8526. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8527. }
  8528. static void dump_vmcs(void)
  8529. {
  8530. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8531. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8532. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8533. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8534. u32 secondary_exec_control = 0;
  8535. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8536. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8537. int i, n;
  8538. if (cpu_has_secondary_exec_ctrls())
  8539. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8540. pr_err("*** Guest State ***\n");
  8541. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8542. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8543. vmcs_readl(CR0_GUEST_HOST_MASK));
  8544. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8545. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8546. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8547. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8548. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8549. {
  8550. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8551. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8552. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8553. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8554. }
  8555. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8556. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8557. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8558. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8559. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8560. vmcs_readl(GUEST_SYSENTER_ESP),
  8561. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8562. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8563. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8564. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8565. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8566. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8567. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8568. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8569. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8570. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8571. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8572. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8573. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8574. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8575. efer, vmcs_read64(GUEST_IA32_PAT));
  8576. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8577. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8578. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8579. if (cpu_has_load_perf_global_ctrl &&
  8580. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8581. pr_err("PerfGlobCtl = 0x%016llx\n",
  8582. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8583. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8584. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8585. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8586. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8587. vmcs_read32(GUEST_ACTIVITY_STATE));
  8588. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8589. pr_err("InterruptStatus = %04x\n",
  8590. vmcs_read16(GUEST_INTR_STATUS));
  8591. pr_err("*** Host State ***\n");
  8592. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8593. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8594. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8595. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8596. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8597. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8598. vmcs_read16(HOST_TR_SELECTOR));
  8599. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8600. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8601. vmcs_readl(HOST_TR_BASE));
  8602. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8603. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8604. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8605. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8606. vmcs_readl(HOST_CR4));
  8607. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8608. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8609. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8610. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8611. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8612. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8613. vmcs_read64(HOST_IA32_EFER),
  8614. vmcs_read64(HOST_IA32_PAT));
  8615. if (cpu_has_load_perf_global_ctrl &&
  8616. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8617. pr_err("PerfGlobCtl = 0x%016llx\n",
  8618. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8619. pr_err("*** Control State ***\n");
  8620. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8621. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8622. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8623. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8624. vmcs_read32(EXCEPTION_BITMAP),
  8625. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8626. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8627. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8628. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8629. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  8630. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  8631. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  8632. vmcs_read32(VM_EXIT_INTR_INFO),
  8633. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8634. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  8635. pr_err(" reason=%08x qualification=%016lx\n",
  8636. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  8637. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  8638. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  8639. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  8640. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  8641. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  8642. pr_err("TSC Multiplier = 0x%016llx\n",
  8643. vmcs_read64(TSC_MULTIPLIER));
  8644. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  8645. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  8646. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  8647. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  8648. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  8649. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  8650. n = vmcs_read32(CR3_TARGET_COUNT);
  8651. for (i = 0; i + 1 < n; i += 4)
  8652. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  8653. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  8654. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  8655. if (i < n)
  8656. pr_err("CR3 target%u=%016lx\n",
  8657. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  8658. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  8659. pr_err("PLE Gap=%08x Window=%08x\n",
  8660. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  8661. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  8662. pr_err("Virtual processor ID = 0x%04x\n",
  8663. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  8664. }
  8665. /*
  8666. * The guest has exited. See if we can fix it or if we need userspace
  8667. * assistance.
  8668. */
  8669. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  8670. {
  8671. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8672. u32 exit_reason = vmx->exit_reason;
  8673. u32 vectoring_info = vmx->idt_vectoring_info;
  8674. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  8675. /*
  8676. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  8677. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  8678. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  8679. * mode as if vcpus is in root mode, the PML buffer must has been
  8680. * flushed already.
  8681. */
  8682. if (enable_pml)
  8683. vmx_flush_pml_buffer(vcpu);
  8684. /* If guest state is invalid, start emulating */
  8685. if (vmx->emulation_required)
  8686. return handle_invalid_guest_state(vcpu);
  8687. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  8688. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  8689. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  8690. dump_vmcs();
  8691. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8692. vcpu->run->fail_entry.hardware_entry_failure_reason
  8693. = exit_reason;
  8694. return 0;
  8695. }
  8696. if (unlikely(vmx->fail)) {
  8697. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8698. vcpu->run->fail_entry.hardware_entry_failure_reason
  8699. = vmcs_read32(VM_INSTRUCTION_ERROR);
  8700. return 0;
  8701. }
  8702. /*
  8703. * Note:
  8704. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  8705. * delivery event since it indicates guest is accessing MMIO.
  8706. * The vm-exit can be triggered again after return to guest that
  8707. * will cause infinite loop.
  8708. */
  8709. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8710. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  8711. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  8712. exit_reason != EXIT_REASON_PML_FULL &&
  8713. exit_reason != EXIT_REASON_APIC_ACCESS &&
  8714. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  8715. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  8716. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  8717. vcpu->run->internal.ndata = 3;
  8718. vcpu->run->internal.data[0] = vectoring_info;
  8719. vcpu->run->internal.data[1] = exit_reason;
  8720. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  8721. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  8722. vcpu->run->internal.ndata++;
  8723. vcpu->run->internal.data[3] =
  8724. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  8725. }
  8726. return 0;
  8727. }
  8728. if (unlikely(!enable_vnmi &&
  8729. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  8730. if (vmx_interrupt_allowed(vcpu)) {
  8731. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8732. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  8733. vcpu->arch.nmi_pending) {
  8734. /*
  8735. * This CPU don't support us in finding the end of an
  8736. * NMI-blocked window if the guest runs with IRQs
  8737. * disabled. So we pull the trigger after 1 s of
  8738. * futile waiting, but inform the user about this.
  8739. */
  8740. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  8741. "state on VCPU %d after 1 s timeout\n",
  8742. __func__, vcpu->vcpu_id);
  8743. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8744. }
  8745. }
  8746. if (exit_reason < kvm_vmx_max_exit_handlers
  8747. && kvm_vmx_exit_handlers[exit_reason])
  8748. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  8749. else {
  8750. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  8751. exit_reason);
  8752. kvm_queue_exception(vcpu, UD_VECTOR);
  8753. return 1;
  8754. }
  8755. }
  8756. /*
  8757. * Software based L1D cache flush which is used when microcode providing
  8758. * the cache control MSR is not loaded.
  8759. *
  8760. * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
  8761. * flush it is required to read in 64 KiB because the replacement algorithm
  8762. * is not exactly LRU. This could be sized at runtime via topology
  8763. * information but as all relevant affected CPUs have 32KiB L1D cache size
  8764. * there is no point in doing so.
  8765. */
  8766. static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
  8767. {
  8768. int size = PAGE_SIZE << L1D_CACHE_ORDER;
  8769. /*
  8770. * This code is only executed when the the flush mode is 'cond' or
  8771. * 'always'
  8772. */
  8773. if (static_branch_likely(&vmx_l1d_flush_cond)) {
  8774. bool flush_l1d;
  8775. /*
  8776. * Clear the per-vcpu flush bit, it gets set again
  8777. * either from vcpu_run() or from one of the unsafe
  8778. * VMEXIT handlers.
  8779. */
  8780. flush_l1d = vcpu->arch.l1tf_flush_l1d;
  8781. vcpu->arch.l1tf_flush_l1d = false;
  8782. /*
  8783. * Clear the per-cpu flush bit, it gets set again from
  8784. * the interrupt handlers.
  8785. */
  8786. flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
  8787. kvm_clear_cpu_l1tf_flush_l1d();
  8788. if (!flush_l1d)
  8789. return;
  8790. }
  8791. vcpu->stat.l1d_flush++;
  8792. if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  8793. wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
  8794. return;
  8795. }
  8796. asm volatile(
  8797. /* First ensure the pages are in the TLB */
  8798. "xorl %%eax, %%eax\n"
  8799. ".Lpopulate_tlb:\n\t"
  8800. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  8801. "addl $4096, %%eax\n\t"
  8802. "cmpl %%eax, %[size]\n\t"
  8803. "jne .Lpopulate_tlb\n\t"
  8804. "xorl %%eax, %%eax\n\t"
  8805. "cpuid\n\t"
  8806. /* Now fill the cache */
  8807. "xorl %%eax, %%eax\n"
  8808. ".Lfill_cache:\n"
  8809. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  8810. "addl $64, %%eax\n\t"
  8811. "cmpl %%eax, %[size]\n\t"
  8812. "jne .Lfill_cache\n\t"
  8813. "lfence\n"
  8814. :: [flush_pages] "r" (vmx_l1d_flush_pages),
  8815. [size] "r" (size)
  8816. : "eax", "ebx", "ecx", "edx");
  8817. }
  8818. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  8819. {
  8820. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8821. if (is_guest_mode(vcpu) &&
  8822. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8823. return;
  8824. if (irr == -1 || tpr < irr) {
  8825. vmcs_write32(TPR_THRESHOLD, 0);
  8826. return;
  8827. }
  8828. vmcs_write32(TPR_THRESHOLD, irr);
  8829. }
  8830. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  8831. {
  8832. u32 sec_exec_control;
  8833. if (!lapic_in_kernel(vcpu))
  8834. return;
  8835. if (!flexpriority_enabled &&
  8836. !cpu_has_vmx_virtualize_x2apic_mode())
  8837. return;
  8838. /* Postpone execution until vmcs01 is the current VMCS. */
  8839. if (is_guest_mode(vcpu)) {
  8840. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  8841. return;
  8842. }
  8843. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8844. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8845. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  8846. switch (kvm_get_apic_mode(vcpu)) {
  8847. case LAPIC_MODE_INVALID:
  8848. WARN_ONCE(true, "Invalid local APIC state");
  8849. case LAPIC_MODE_DISABLED:
  8850. break;
  8851. case LAPIC_MODE_XAPIC:
  8852. if (flexpriority_enabled) {
  8853. sec_exec_control |=
  8854. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8855. vmx_flush_tlb(vcpu, true);
  8856. }
  8857. break;
  8858. case LAPIC_MODE_X2APIC:
  8859. if (cpu_has_vmx_virtualize_x2apic_mode())
  8860. sec_exec_control |=
  8861. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  8862. break;
  8863. }
  8864. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  8865. vmx_update_msr_bitmap(vcpu);
  8866. }
  8867. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  8868. {
  8869. if (!is_guest_mode(vcpu)) {
  8870. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8871. vmx_flush_tlb(vcpu, true);
  8872. }
  8873. }
  8874. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  8875. {
  8876. u16 status;
  8877. u8 old;
  8878. if (max_isr == -1)
  8879. max_isr = 0;
  8880. status = vmcs_read16(GUEST_INTR_STATUS);
  8881. old = status >> 8;
  8882. if (max_isr != old) {
  8883. status &= 0xff;
  8884. status |= max_isr << 8;
  8885. vmcs_write16(GUEST_INTR_STATUS, status);
  8886. }
  8887. }
  8888. static void vmx_set_rvi(int vector)
  8889. {
  8890. u16 status;
  8891. u8 old;
  8892. if (vector == -1)
  8893. vector = 0;
  8894. status = vmcs_read16(GUEST_INTR_STATUS);
  8895. old = (u8)status & 0xff;
  8896. if ((u8)vector != old) {
  8897. status &= ~0xff;
  8898. status |= (u8)vector;
  8899. vmcs_write16(GUEST_INTR_STATUS, status);
  8900. }
  8901. }
  8902. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  8903. {
  8904. /*
  8905. * When running L2, updating RVI is only relevant when
  8906. * vmcs12 virtual-interrupt-delivery enabled.
  8907. * However, it can be enabled only when L1 also
  8908. * intercepts external-interrupts and in that case
  8909. * we should not update vmcs02 RVI but instead intercept
  8910. * interrupt. Therefore, do nothing when running L2.
  8911. */
  8912. if (!is_guest_mode(vcpu))
  8913. vmx_set_rvi(max_irr);
  8914. }
  8915. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  8916. {
  8917. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8918. int max_irr;
  8919. bool max_irr_updated;
  8920. WARN_ON(!vcpu->arch.apicv_active);
  8921. if (pi_test_on(&vmx->pi_desc)) {
  8922. pi_clear_on(&vmx->pi_desc);
  8923. /*
  8924. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  8925. * But on x86 this is just a compiler barrier anyway.
  8926. */
  8927. smp_mb__after_atomic();
  8928. max_irr_updated =
  8929. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  8930. /*
  8931. * If we are running L2 and L1 has a new pending interrupt
  8932. * which can be injected, we should re-evaluate
  8933. * what should be done with this new L1 interrupt.
  8934. * If L1 intercepts external-interrupts, we should
  8935. * exit from L2 to L1. Otherwise, interrupt should be
  8936. * delivered directly to L2.
  8937. */
  8938. if (is_guest_mode(vcpu) && max_irr_updated) {
  8939. if (nested_exit_on_intr(vcpu))
  8940. kvm_vcpu_exiting_guest_mode(vcpu);
  8941. else
  8942. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8943. }
  8944. } else {
  8945. max_irr = kvm_lapic_find_highest_irr(vcpu);
  8946. }
  8947. vmx_hwapic_irr_update(vcpu, max_irr);
  8948. return max_irr;
  8949. }
  8950. static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
  8951. {
  8952. u8 rvi = vmx_get_rvi();
  8953. u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
  8954. return ((rvi & 0xf0) > (vppr & 0xf0));
  8955. }
  8956. static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
  8957. {
  8958. return pi_test_on(vcpu_to_pi_desc(vcpu));
  8959. }
  8960. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  8961. {
  8962. if (!kvm_vcpu_apicv_active(vcpu))
  8963. return;
  8964. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  8965. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  8966. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  8967. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  8968. }
  8969. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  8970. {
  8971. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8972. pi_clear_on(&vmx->pi_desc);
  8973. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  8974. }
  8975. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  8976. {
  8977. if (vmx->exit_reason != EXIT_REASON_EXCEPTION_NMI)
  8978. return;
  8979. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8980. /* if exit due to PF check for async PF */
  8981. if (is_page_fault(vmx->exit_intr_info))
  8982. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  8983. /* Handle machine checks before interrupts are enabled */
  8984. if (is_machine_check(vmx->exit_intr_info))
  8985. kvm_machine_check();
  8986. /* We need to handle NMIs before interrupts are enabled */
  8987. if (is_nmi(vmx->exit_intr_info)) {
  8988. kvm_before_interrupt(&vmx->vcpu);
  8989. asm("int $2");
  8990. kvm_after_interrupt(&vmx->vcpu);
  8991. }
  8992. }
  8993. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  8994. {
  8995. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8996. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  8997. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  8998. unsigned int vector;
  8999. unsigned long entry;
  9000. gate_desc *desc;
  9001. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9002. #ifdef CONFIG_X86_64
  9003. unsigned long tmp;
  9004. #endif
  9005. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9006. desc = (gate_desc *)vmx->host_idt_base + vector;
  9007. entry = gate_offset(desc);
  9008. asm volatile(
  9009. #ifdef CONFIG_X86_64
  9010. "mov %%" _ASM_SP ", %[sp]\n\t"
  9011. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  9012. "push $%c[ss]\n\t"
  9013. "push %[sp]\n\t"
  9014. #endif
  9015. "pushf\n\t"
  9016. __ASM_SIZE(push) " $%c[cs]\n\t"
  9017. CALL_NOSPEC
  9018. :
  9019. #ifdef CONFIG_X86_64
  9020. [sp]"=&r"(tmp),
  9021. #endif
  9022. ASM_CALL_CONSTRAINT
  9023. :
  9024. THUNK_TARGET(entry),
  9025. [ss]"i"(__KERNEL_DS),
  9026. [cs]"i"(__KERNEL_CS)
  9027. );
  9028. }
  9029. }
  9030. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  9031. static bool vmx_has_emulated_msr(int index)
  9032. {
  9033. switch (index) {
  9034. case MSR_IA32_SMBASE:
  9035. /*
  9036. * We cannot do SMM unless we can run the guest in big
  9037. * real mode.
  9038. */
  9039. return enable_unrestricted_guest || emulate_invalid_guest_state;
  9040. case MSR_AMD64_VIRT_SPEC_CTRL:
  9041. /* This is AMD only. */
  9042. return false;
  9043. default:
  9044. return true;
  9045. }
  9046. }
  9047. static bool vmx_mpx_supported(void)
  9048. {
  9049. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  9050. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  9051. }
  9052. static bool vmx_xsaves_supported(void)
  9053. {
  9054. return vmcs_config.cpu_based_2nd_exec_ctrl &
  9055. SECONDARY_EXEC_XSAVES;
  9056. }
  9057. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  9058. {
  9059. u32 exit_intr_info;
  9060. bool unblock_nmi;
  9061. u8 vector;
  9062. bool idtv_info_valid;
  9063. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9064. if (enable_vnmi) {
  9065. if (vmx->loaded_vmcs->nmi_known_unmasked)
  9066. return;
  9067. /*
  9068. * Can't use vmx->exit_intr_info since we're not sure what
  9069. * the exit reason is.
  9070. */
  9071. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9072. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  9073. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9074. /*
  9075. * SDM 3: 27.7.1.2 (September 2008)
  9076. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  9077. * a guest IRET fault.
  9078. * SDM 3: 23.2.2 (September 2008)
  9079. * Bit 12 is undefined in any of the following cases:
  9080. * If the VM exit sets the valid bit in the IDT-vectoring
  9081. * information field.
  9082. * If the VM exit is due to a double fault.
  9083. */
  9084. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  9085. vector != DF_VECTOR && !idtv_info_valid)
  9086. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  9087. GUEST_INTR_STATE_NMI);
  9088. else
  9089. vmx->loaded_vmcs->nmi_known_unmasked =
  9090. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  9091. & GUEST_INTR_STATE_NMI);
  9092. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  9093. vmx->loaded_vmcs->vnmi_blocked_time +=
  9094. ktime_to_ns(ktime_sub(ktime_get(),
  9095. vmx->loaded_vmcs->entry_time));
  9096. }
  9097. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  9098. u32 idt_vectoring_info,
  9099. int instr_len_field,
  9100. int error_code_field)
  9101. {
  9102. u8 vector;
  9103. int type;
  9104. bool idtv_info_valid;
  9105. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9106. vcpu->arch.nmi_injected = false;
  9107. kvm_clear_exception_queue(vcpu);
  9108. kvm_clear_interrupt_queue(vcpu);
  9109. if (!idtv_info_valid)
  9110. return;
  9111. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9112. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  9113. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  9114. switch (type) {
  9115. case INTR_TYPE_NMI_INTR:
  9116. vcpu->arch.nmi_injected = true;
  9117. /*
  9118. * SDM 3: 27.7.1.2 (September 2008)
  9119. * Clear bit "block by NMI" before VM entry if a NMI
  9120. * delivery faulted.
  9121. */
  9122. vmx_set_nmi_mask(vcpu, false);
  9123. break;
  9124. case INTR_TYPE_SOFT_EXCEPTION:
  9125. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9126. /* fall through */
  9127. case INTR_TYPE_HARD_EXCEPTION:
  9128. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  9129. u32 err = vmcs_read32(error_code_field);
  9130. kvm_requeue_exception_e(vcpu, vector, err);
  9131. } else
  9132. kvm_requeue_exception(vcpu, vector);
  9133. break;
  9134. case INTR_TYPE_SOFT_INTR:
  9135. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9136. /* fall through */
  9137. case INTR_TYPE_EXT_INTR:
  9138. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  9139. break;
  9140. default:
  9141. break;
  9142. }
  9143. }
  9144. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  9145. {
  9146. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  9147. VM_EXIT_INSTRUCTION_LEN,
  9148. IDT_VECTORING_ERROR_CODE);
  9149. }
  9150. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  9151. {
  9152. __vmx_complete_interrupts(vcpu,
  9153. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9154. VM_ENTRY_INSTRUCTION_LEN,
  9155. VM_ENTRY_EXCEPTION_ERROR_CODE);
  9156. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9157. }
  9158. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  9159. {
  9160. int i, nr_msrs;
  9161. struct perf_guest_switch_msr *msrs;
  9162. msrs = perf_guest_get_msrs(&nr_msrs);
  9163. if (!msrs)
  9164. return;
  9165. for (i = 0; i < nr_msrs; i++)
  9166. if (msrs[i].host == msrs[i].guest)
  9167. clear_atomic_switch_msr(vmx, msrs[i].msr);
  9168. else
  9169. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  9170. msrs[i].host, false);
  9171. }
  9172. static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
  9173. {
  9174. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
  9175. if (!vmx->loaded_vmcs->hv_timer_armed)
  9176. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  9177. PIN_BASED_VMX_PREEMPTION_TIMER);
  9178. vmx->loaded_vmcs->hv_timer_armed = true;
  9179. }
  9180. static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
  9181. {
  9182. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9183. u64 tscl;
  9184. u32 delta_tsc;
  9185. if (vmx->req_immediate_exit) {
  9186. vmx_arm_hv_timer(vmx, 0);
  9187. return;
  9188. }
  9189. if (vmx->hv_deadline_tsc != -1) {
  9190. tscl = rdtsc();
  9191. if (vmx->hv_deadline_tsc > tscl)
  9192. /* set_hv_timer ensures the delta fits in 32-bits */
  9193. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  9194. cpu_preemption_timer_multi);
  9195. else
  9196. delta_tsc = 0;
  9197. vmx_arm_hv_timer(vmx, delta_tsc);
  9198. return;
  9199. }
  9200. if (vmx->loaded_vmcs->hv_timer_armed)
  9201. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  9202. PIN_BASED_VMX_PREEMPTION_TIMER);
  9203. vmx->loaded_vmcs->hv_timer_armed = false;
  9204. }
  9205. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  9206. {
  9207. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9208. unsigned long cr3, cr4, evmcs_rsp;
  9209. /* Record the guest's net vcpu time for enforced NMI injections. */
  9210. if (unlikely(!enable_vnmi &&
  9211. vmx->loaded_vmcs->soft_vnmi_blocked))
  9212. vmx->loaded_vmcs->entry_time = ktime_get();
  9213. /* Don't enter VMX if guest state is invalid, let the exit handler
  9214. start emulation until we arrive back to a valid state */
  9215. if (vmx->emulation_required)
  9216. return;
  9217. if (vmx->ple_window_dirty) {
  9218. vmx->ple_window_dirty = false;
  9219. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  9220. }
  9221. if (vmx->nested.sync_shadow_vmcs) {
  9222. copy_vmcs12_to_shadow(vmx);
  9223. vmx->nested.sync_shadow_vmcs = false;
  9224. }
  9225. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  9226. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  9227. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  9228. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  9229. cr3 = __get_current_cr3_fast();
  9230. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  9231. vmcs_writel(HOST_CR3, cr3);
  9232. vmx->loaded_vmcs->host_state.cr3 = cr3;
  9233. }
  9234. cr4 = cr4_read_shadow();
  9235. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  9236. vmcs_writel(HOST_CR4, cr4);
  9237. vmx->loaded_vmcs->host_state.cr4 = cr4;
  9238. }
  9239. /* When single-stepping over STI and MOV SS, we must clear the
  9240. * corresponding interruptibility bits in the guest state. Otherwise
  9241. * vmentry fails as it then expects bit 14 (BS) in pending debug
  9242. * exceptions being set, but that's not correct for the guest debugging
  9243. * case. */
  9244. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  9245. vmx_set_interrupt_shadow(vcpu, 0);
  9246. kvm_load_guest_xcr0(vcpu);
  9247. if (static_cpu_has(X86_FEATURE_PKU) &&
  9248. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  9249. vcpu->arch.pkru != vmx->host_pkru)
  9250. __write_pkru(vcpu->arch.pkru);
  9251. atomic_switch_perf_msrs(vmx);
  9252. vmx_update_hv_timer(vcpu);
  9253. /*
  9254. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  9255. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  9256. * is no need to worry about the conditional branch over the wrmsr
  9257. * being speculatively taken.
  9258. */
  9259. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  9260. vmx->__launched = vmx->loaded_vmcs->launched;
  9261. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  9262. (unsigned long)&current_evmcs->host_rsp : 0;
  9263. /* L1D Flush includes CPU buffer clear to mitigate MDS */
  9264. if (static_branch_unlikely(&vmx_l1d_should_flush))
  9265. vmx_l1d_flush(vcpu);
  9266. else if (static_branch_unlikely(&mds_user_clear))
  9267. mds_clear_cpu_buffers();
  9268. asm volatile (
  9269. /* Store host registers */
  9270. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  9271. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  9272. "push %%" _ASM_CX " \n\t"
  9273. "cmp %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t"
  9274. "je 1f \n\t"
  9275. "mov %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t"
  9276. /* Avoid VMWRITE when Enlightened VMCS is in use */
  9277. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  9278. "jz 2f \n\t"
  9279. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  9280. "jmp 1f \n\t"
  9281. "2: \n\t"
  9282. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  9283. "1: \n\t"
  9284. /* Reload cr2 if changed */
  9285. "mov %c[cr2](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
  9286. "mov %%cr2, %%" _ASM_DX " \n\t"
  9287. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  9288. "je 3f \n\t"
  9289. "mov %%" _ASM_AX", %%cr2 \n\t"
  9290. "3: \n\t"
  9291. /* Check if vmlaunch of vmresume is needed */
  9292. "cmpb $0, %c[launched](%%" _ASM_CX ") \n\t"
  9293. /* Load guest registers. Don't clobber flags. */
  9294. "mov %c[rax](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
  9295. "mov %c[rbx](%%" _ASM_CX "), %%" _ASM_BX " \n\t"
  9296. "mov %c[rdx](%%" _ASM_CX "), %%" _ASM_DX " \n\t"
  9297. "mov %c[rsi](%%" _ASM_CX "), %%" _ASM_SI " \n\t"
  9298. "mov %c[rdi](%%" _ASM_CX "), %%" _ASM_DI " \n\t"
  9299. "mov %c[rbp](%%" _ASM_CX "), %%" _ASM_BP " \n\t"
  9300. #ifdef CONFIG_X86_64
  9301. "mov %c[r8](%%" _ASM_CX "), %%r8 \n\t"
  9302. "mov %c[r9](%%" _ASM_CX "), %%r9 \n\t"
  9303. "mov %c[r10](%%" _ASM_CX "), %%r10 \n\t"
  9304. "mov %c[r11](%%" _ASM_CX "), %%r11 \n\t"
  9305. "mov %c[r12](%%" _ASM_CX "), %%r12 \n\t"
  9306. "mov %c[r13](%%" _ASM_CX "), %%r13 \n\t"
  9307. "mov %c[r14](%%" _ASM_CX "), %%r14 \n\t"
  9308. "mov %c[r15](%%" _ASM_CX "), %%r15 \n\t"
  9309. #endif
  9310. /* Load guest RCX. This kills the vmx_vcpu pointer! */
  9311. "mov %c[rcx](%%" _ASM_CX "), %%" _ASM_CX " \n\t"
  9312. /* Enter guest mode */
  9313. "jne 1f \n\t"
  9314. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  9315. "jmp 2f \n\t"
  9316. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  9317. "2: "
  9318. /* Save guest's RCX to the stack placeholder (see above) */
  9319. "mov %%" _ASM_CX ", %c[wordsize](%%" _ASM_SP ") \n\t"
  9320. /* Load host's RCX, i.e. the vmx_vcpu pointer */
  9321. "pop %%" _ASM_CX " \n\t"
  9322. /* Set vmx->fail based on EFLAGS.{CF,ZF} */
  9323. "setbe %c[fail](%%" _ASM_CX ")\n\t"
  9324. /* Save all guest registers, including RCX from the stack */
  9325. "mov %%" _ASM_AX ", %c[rax](%%" _ASM_CX ") \n\t"
  9326. "mov %%" _ASM_BX ", %c[rbx](%%" _ASM_CX ") \n\t"
  9327. __ASM_SIZE(pop) " %c[rcx](%%" _ASM_CX ") \n\t"
  9328. "mov %%" _ASM_DX ", %c[rdx](%%" _ASM_CX ") \n\t"
  9329. "mov %%" _ASM_SI ", %c[rsi](%%" _ASM_CX ") \n\t"
  9330. "mov %%" _ASM_DI ", %c[rdi](%%" _ASM_CX ") \n\t"
  9331. "mov %%" _ASM_BP ", %c[rbp](%%" _ASM_CX ") \n\t"
  9332. #ifdef CONFIG_X86_64
  9333. "mov %%r8, %c[r8](%%" _ASM_CX ") \n\t"
  9334. "mov %%r9, %c[r9](%%" _ASM_CX ") \n\t"
  9335. "mov %%r10, %c[r10](%%" _ASM_CX ") \n\t"
  9336. "mov %%r11, %c[r11](%%" _ASM_CX ") \n\t"
  9337. "mov %%r12, %c[r12](%%" _ASM_CX ") \n\t"
  9338. "mov %%r13, %c[r13](%%" _ASM_CX ") \n\t"
  9339. "mov %%r14, %c[r14](%%" _ASM_CX ") \n\t"
  9340. "mov %%r15, %c[r15](%%" _ASM_CX ") \n\t"
  9341. /*
  9342. * Clear all general purpose registers (except RSP, which is loaded by
  9343. * the CPU during VM-Exit) to prevent speculative use of the guest's
  9344. * values, even those that are saved/loaded via the stack. In theory,
  9345. * an L1 cache miss when restoring registers could lead to speculative
  9346. * execution with the guest's values. Zeroing XORs are dirt cheap,
  9347. * i.e. the extra paranoia is essentially free.
  9348. */
  9349. "xor %%r8d, %%r8d \n\t"
  9350. "xor %%r9d, %%r9d \n\t"
  9351. "xor %%r10d, %%r10d \n\t"
  9352. "xor %%r11d, %%r11d \n\t"
  9353. "xor %%r12d, %%r12d \n\t"
  9354. "xor %%r13d, %%r13d \n\t"
  9355. "xor %%r14d, %%r14d \n\t"
  9356. "xor %%r15d, %%r15d \n\t"
  9357. #endif
  9358. "mov %%cr2, %%" _ASM_AX " \n\t"
  9359. "mov %%" _ASM_AX ", %c[cr2](%%" _ASM_CX ") \n\t"
  9360. "xor %%eax, %%eax \n\t"
  9361. "xor %%ebx, %%ebx \n\t"
  9362. "xor %%ecx, %%ecx \n\t"
  9363. "xor %%edx, %%edx \n\t"
  9364. "xor %%esi, %%esi \n\t"
  9365. "xor %%edi, %%edi \n\t"
  9366. "xor %%ebp, %%ebp \n\t"
  9367. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  9368. ".pushsection .rodata \n\t"
  9369. ".global vmx_return \n\t"
  9370. "vmx_return: " _ASM_PTR " 2b \n\t"
  9371. ".popsection"
  9372. : "=c"((int){0}), "=d"((int){0}), "=S"((int){0})
  9373. : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  9374. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  9375. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  9376. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  9377. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  9378. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  9379. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  9380. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  9381. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  9382. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  9383. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  9384. #ifdef CONFIG_X86_64
  9385. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  9386. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  9387. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  9388. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  9389. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  9390. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  9391. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  9392. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  9393. #endif
  9394. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  9395. [wordsize]"i"(sizeof(ulong))
  9396. : "cc", "memory"
  9397. #ifdef CONFIG_X86_64
  9398. , "rax", "rbx", "rdi"
  9399. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  9400. #else
  9401. , "eax", "ebx", "edi"
  9402. #endif
  9403. );
  9404. /*
  9405. * We do not use IBRS in the kernel. If this vCPU has used the
  9406. * SPEC_CTRL MSR it may have left it on; save the value and
  9407. * turn it off. This is much more efficient than blindly adding
  9408. * it to the atomic save/restore list. Especially as the former
  9409. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  9410. *
  9411. * For non-nested case:
  9412. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  9413. * save it.
  9414. *
  9415. * For nested case:
  9416. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  9417. * save it.
  9418. */
  9419. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  9420. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  9421. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  9422. /* Eliminate branch target predictions from guest mode */
  9423. vmexit_fill_RSB();
  9424. /* All fields are clean at this point */
  9425. if (static_branch_unlikely(&enable_evmcs))
  9426. current_evmcs->hv_clean_fields |=
  9427. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9428. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  9429. if (vmx->host_debugctlmsr)
  9430. update_debugctlmsr(vmx->host_debugctlmsr);
  9431. #ifndef CONFIG_X86_64
  9432. /*
  9433. * The sysexit path does not restore ds/es, so we must set them to
  9434. * a reasonable value ourselves.
  9435. *
  9436. * We can't defer this to vmx_prepare_switch_to_host() since that
  9437. * function may be executed in interrupt context, which saves and
  9438. * restore segments around it, nullifying its effect.
  9439. */
  9440. loadsegment(ds, __USER_DS);
  9441. loadsegment(es, __USER_DS);
  9442. #endif
  9443. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  9444. | (1 << VCPU_EXREG_RFLAGS)
  9445. | (1 << VCPU_EXREG_PDPTR)
  9446. | (1 << VCPU_EXREG_SEGMENTS)
  9447. | (1 << VCPU_EXREG_CR3));
  9448. vcpu->arch.regs_dirty = 0;
  9449. /*
  9450. * eager fpu is enabled if PKEY is supported and CR4 is switched
  9451. * back on host, so it is safe to read guest PKRU from current
  9452. * XSAVE.
  9453. */
  9454. if (static_cpu_has(X86_FEATURE_PKU) &&
  9455. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  9456. vcpu->arch.pkru = __read_pkru();
  9457. if (vcpu->arch.pkru != vmx->host_pkru)
  9458. __write_pkru(vmx->host_pkru);
  9459. }
  9460. kvm_put_guest_xcr0(vcpu);
  9461. vmx->nested.nested_run_pending = 0;
  9462. vmx->idt_vectoring_info = 0;
  9463. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  9464. if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
  9465. kvm_machine_check();
  9466. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9467. return;
  9468. vmx->loaded_vmcs->launched = 1;
  9469. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  9470. vmx_complete_atomic_exit(vmx);
  9471. vmx_recover_nmi_blocking(vmx);
  9472. vmx_complete_interrupts(vmx);
  9473. }
  9474. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  9475. static struct kvm *vmx_vm_alloc(void)
  9476. {
  9477. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  9478. if (!kvm_vmx)
  9479. return NULL;
  9480. return &kvm_vmx->kvm;
  9481. }
  9482. static void vmx_vm_free(struct kvm *kvm)
  9483. {
  9484. vfree(to_kvm_vmx(kvm));
  9485. }
  9486. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  9487. {
  9488. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9489. int cpu;
  9490. if (vmx->loaded_vmcs == vmcs)
  9491. return;
  9492. cpu = get_cpu();
  9493. vmx_vcpu_put(vcpu);
  9494. vmx->loaded_vmcs = vmcs;
  9495. vmx_vcpu_load(vcpu, cpu);
  9496. put_cpu();
  9497. }
  9498. /*
  9499. * Ensure that the current vmcs of the logical processor is the
  9500. * vmcs01 of the vcpu before calling free_nested().
  9501. */
  9502. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  9503. {
  9504. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9505. vcpu_load(vcpu);
  9506. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9507. free_nested(vmx);
  9508. vcpu_put(vcpu);
  9509. }
  9510. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  9511. {
  9512. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9513. if (enable_pml)
  9514. vmx_destroy_pml_buffer(vmx);
  9515. free_vpid(vmx->vpid);
  9516. leave_guest_mode(vcpu);
  9517. vmx_free_vcpu_nested(vcpu);
  9518. free_loaded_vmcs(vmx->loaded_vmcs);
  9519. kfree(vmx->guest_msrs);
  9520. kvm_vcpu_uninit(vcpu);
  9521. kmem_cache_free(kvm_vcpu_cache, vmx);
  9522. }
  9523. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  9524. {
  9525. int err;
  9526. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  9527. unsigned long *msr_bitmap;
  9528. int cpu;
  9529. if (!vmx)
  9530. return ERR_PTR(-ENOMEM);
  9531. vmx->vpid = allocate_vpid();
  9532. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  9533. if (err)
  9534. goto free_vcpu;
  9535. err = -ENOMEM;
  9536. /*
  9537. * If PML is turned on, failure on enabling PML just results in failure
  9538. * of creating the vcpu, therefore we can simplify PML logic (by
  9539. * avoiding dealing with cases, such as enabling PML partially on vcpus
  9540. * for the guest, etc.
  9541. */
  9542. if (enable_pml) {
  9543. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  9544. if (!vmx->pml_pg)
  9545. goto uninit_vcpu;
  9546. }
  9547. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  9548. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  9549. > PAGE_SIZE);
  9550. if (!vmx->guest_msrs)
  9551. goto free_pml;
  9552. err = alloc_loaded_vmcs(&vmx->vmcs01);
  9553. if (err < 0)
  9554. goto free_msrs;
  9555. msr_bitmap = vmx->vmcs01.msr_bitmap;
  9556. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  9557. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  9558. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  9559. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  9560. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  9561. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  9562. vmx->msr_bitmap_mode = 0;
  9563. vmx->loaded_vmcs = &vmx->vmcs01;
  9564. cpu = get_cpu();
  9565. vmx_vcpu_load(&vmx->vcpu, cpu);
  9566. vmx->vcpu.cpu = cpu;
  9567. vmx_vcpu_setup(vmx);
  9568. vmx_vcpu_put(&vmx->vcpu);
  9569. put_cpu();
  9570. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  9571. err = alloc_apic_access_page(kvm);
  9572. if (err)
  9573. goto free_vmcs;
  9574. }
  9575. if (enable_ept && !enable_unrestricted_guest) {
  9576. err = init_rmode_identity_map(kvm);
  9577. if (err)
  9578. goto free_vmcs;
  9579. }
  9580. if (nested)
  9581. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  9582. kvm_vcpu_apicv_active(&vmx->vcpu));
  9583. vmx->nested.posted_intr_nv = -1;
  9584. vmx->nested.current_vmptr = -1ull;
  9585. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  9586. /*
  9587. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  9588. * or POSTED_INTR_WAKEUP_VECTOR.
  9589. */
  9590. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  9591. vmx->pi_desc.sn = 1;
  9592. return &vmx->vcpu;
  9593. free_vmcs:
  9594. free_loaded_vmcs(vmx->loaded_vmcs);
  9595. free_msrs:
  9596. kfree(vmx->guest_msrs);
  9597. free_pml:
  9598. vmx_destroy_pml_buffer(vmx);
  9599. uninit_vcpu:
  9600. kvm_vcpu_uninit(&vmx->vcpu);
  9601. free_vcpu:
  9602. free_vpid(vmx->vpid);
  9603. kmem_cache_free(kvm_vcpu_cache, vmx);
  9604. return ERR_PTR(err);
  9605. }
  9606. #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/hw-vuln/l1tf.html for details.\n"
  9607. #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/hw-vuln/l1tf.html for details.\n"
  9608. static int vmx_vm_init(struct kvm *kvm)
  9609. {
  9610. spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
  9611. if (!ple_gap)
  9612. kvm->arch.pause_in_guest = true;
  9613. if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
  9614. switch (l1tf_mitigation) {
  9615. case L1TF_MITIGATION_OFF:
  9616. case L1TF_MITIGATION_FLUSH_NOWARN:
  9617. /* 'I explicitly don't care' is set */
  9618. break;
  9619. case L1TF_MITIGATION_FLUSH:
  9620. case L1TF_MITIGATION_FLUSH_NOSMT:
  9621. case L1TF_MITIGATION_FULL:
  9622. /*
  9623. * Warn upon starting the first VM in a potentially
  9624. * insecure environment.
  9625. */
  9626. if (sched_smt_active())
  9627. pr_warn_once(L1TF_MSG_SMT);
  9628. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
  9629. pr_warn_once(L1TF_MSG_L1D);
  9630. break;
  9631. case L1TF_MITIGATION_FULL_FORCE:
  9632. /* Flush is enforced */
  9633. break;
  9634. }
  9635. }
  9636. return 0;
  9637. }
  9638. static void __init vmx_check_processor_compat(void *rtn)
  9639. {
  9640. struct vmcs_config vmcs_conf;
  9641. *(int *)rtn = 0;
  9642. if (setup_vmcs_config(&vmcs_conf) < 0)
  9643. *(int *)rtn = -EIO;
  9644. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  9645. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  9646. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  9647. smp_processor_id());
  9648. *(int *)rtn = -EIO;
  9649. }
  9650. }
  9651. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  9652. {
  9653. u8 cache;
  9654. u64 ipat = 0;
  9655. /* For VT-d and EPT combination
  9656. * 1. MMIO: always map as UC
  9657. * 2. EPT with VT-d:
  9658. * a. VT-d without snooping control feature: can't guarantee the
  9659. * result, try to trust guest.
  9660. * b. VT-d with snooping control feature: snooping control feature of
  9661. * VT-d engine can guarantee the cache correctness. Just set it
  9662. * to WB to keep consistent with host. So the same as item 3.
  9663. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  9664. * consistent with host MTRR
  9665. */
  9666. if (is_mmio) {
  9667. cache = MTRR_TYPE_UNCACHABLE;
  9668. goto exit;
  9669. }
  9670. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  9671. ipat = VMX_EPT_IPAT_BIT;
  9672. cache = MTRR_TYPE_WRBACK;
  9673. goto exit;
  9674. }
  9675. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  9676. ipat = VMX_EPT_IPAT_BIT;
  9677. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  9678. cache = MTRR_TYPE_WRBACK;
  9679. else
  9680. cache = MTRR_TYPE_UNCACHABLE;
  9681. goto exit;
  9682. }
  9683. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  9684. exit:
  9685. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  9686. }
  9687. static int vmx_get_lpage_level(void)
  9688. {
  9689. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  9690. return PT_DIRECTORY_LEVEL;
  9691. else
  9692. /* For shadow and EPT supported 1GB page */
  9693. return PT_PDPE_LEVEL;
  9694. }
  9695. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  9696. {
  9697. /*
  9698. * These bits in the secondary execution controls field
  9699. * are dynamic, the others are mostly based on the hypervisor
  9700. * architecture and the guest's CPUID. Do not touch the
  9701. * dynamic bits.
  9702. */
  9703. u32 mask =
  9704. SECONDARY_EXEC_SHADOW_VMCS |
  9705. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  9706. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9707. SECONDARY_EXEC_DESC;
  9708. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9709. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  9710. (new_ctl & ~mask) | (cur_ctl & mask));
  9711. }
  9712. /*
  9713. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  9714. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  9715. */
  9716. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  9717. {
  9718. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9719. struct kvm_cpuid_entry2 *entry;
  9720. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  9721. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  9722. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  9723. if (entry && (entry->_reg & (_cpuid_mask))) \
  9724. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  9725. } while (0)
  9726. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  9727. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  9728. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  9729. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  9730. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  9731. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  9732. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  9733. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  9734. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  9735. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  9736. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  9737. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  9738. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  9739. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  9740. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  9741. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  9742. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  9743. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  9744. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  9745. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  9746. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  9747. #undef cr4_fixed1_update
  9748. }
  9749. static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
  9750. {
  9751. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9752. if (kvm_mpx_supported()) {
  9753. bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
  9754. if (mpx_enabled) {
  9755. vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  9756. vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  9757. } else {
  9758. vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
  9759. vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
  9760. }
  9761. }
  9762. }
  9763. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  9764. {
  9765. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9766. if (cpu_has_secondary_exec_ctrls()) {
  9767. vmx_compute_secondary_exec_control(vmx);
  9768. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  9769. }
  9770. if (nested_vmx_allowed(vcpu))
  9771. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  9772. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9773. else
  9774. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  9775. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9776. if (nested_vmx_allowed(vcpu)) {
  9777. nested_vmx_cr_fixed1_bits_update(vcpu);
  9778. nested_vmx_entry_exit_ctls_update(vcpu);
  9779. }
  9780. }
  9781. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  9782. {
  9783. if (func == 1 && nested)
  9784. entry->ecx |= bit(X86_FEATURE_VMX);
  9785. }
  9786. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  9787. struct x86_exception *fault)
  9788. {
  9789. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9790. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9791. u32 exit_reason;
  9792. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  9793. if (vmx->nested.pml_full) {
  9794. exit_reason = EXIT_REASON_PML_FULL;
  9795. vmx->nested.pml_full = false;
  9796. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  9797. } else if (fault->error_code & PFERR_RSVD_MASK)
  9798. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  9799. else
  9800. exit_reason = EXIT_REASON_EPT_VIOLATION;
  9801. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  9802. vmcs12->guest_physical_address = fault->address;
  9803. }
  9804. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  9805. {
  9806. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  9807. }
  9808. /* Callbacks for nested_ept_init_mmu_context: */
  9809. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  9810. {
  9811. /* return the page table to be shadowed - in our case, EPT12 */
  9812. return get_vmcs12(vcpu)->ept_pointer;
  9813. }
  9814. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  9815. {
  9816. WARN_ON(mmu_is_nested(vcpu));
  9817. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  9818. return 1;
  9819. kvm_init_shadow_ept_mmu(vcpu,
  9820. to_vmx(vcpu)->nested.msrs.ept_caps &
  9821. VMX_EPT_EXECUTE_ONLY_BIT,
  9822. nested_ept_ad_enabled(vcpu),
  9823. nested_ept_get_cr3(vcpu));
  9824. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  9825. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  9826. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  9827. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  9828. return 0;
  9829. }
  9830. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  9831. {
  9832. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  9833. }
  9834. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  9835. u16 error_code)
  9836. {
  9837. bool inequality, bit;
  9838. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  9839. inequality =
  9840. (error_code & vmcs12->page_fault_error_code_mask) !=
  9841. vmcs12->page_fault_error_code_match;
  9842. return inequality ^ bit;
  9843. }
  9844. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  9845. struct x86_exception *fault)
  9846. {
  9847. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9848. WARN_ON(!is_guest_mode(vcpu));
  9849. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  9850. !to_vmx(vcpu)->nested.nested_run_pending) {
  9851. vmcs12->vm_exit_intr_error_code = fault->error_code;
  9852. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9853. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  9854. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  9855. fault->address);
  9856. } else {
  9857. kvm_inject_page_fault(vcpu, fault);
  9858. }
  9859. }
  9860. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9861. struct vmcs12 *vmcs12);
  9862. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
  9863. {
  9864. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9865. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9866. struct page *page;
  9867. u64 hpa;
  9868. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9869. /*
  9870. * Translate L1 physical address to host physical
  9871. * address for vmcs02. Keep the page pinned, so this
  9872. * physical address remains valid. We keep a reference
  9873. * to it so we can release it later.
  9874. */
  9875. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  9876. kvm_release_page_dirty(vmx->nested.apic_access_page);
  9877. vmx->nested.apic_access_page = NULL;
  9878. }
  9879. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  9880. /*
  9881. * If translation failed, no matter: This feature asks
  9882. * to exit when accessing the given address, and if it
  9883. * can never be accessed, this feature won't do
  9884. * anything anyway.
  9885. */
  9886. if (!is_error_page(page)) {
  9887. vmx->nested.apic_access_page = page;
  9888. hpa = page_to_phys(vmx->nested.apic_access_page);
  9889. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9890. } else {
  9891. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  9892. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  9893. }
  9894. }
  9895. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  9896. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  9897. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  9898. vmx->nested.virtual_apic_page = NULL;
  9899. }
  9900. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  9901. /*
  9902. * If translation failed, VM entry will fail because
  9903. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  9904. * Failing the vm entry is _not_ what the processor
  9905. * does but it's basically the only possibility we
  9906. * have. We could still enter the guest if CR8 load
  9907. * exits are enabled, CR8 store exits are enabled, and
  9908. * virtualize APIC access is disabled; in this case
  9909. * the processor would never use the TPR shadow and we
  9910. * could simply clear the bit from the execution
  9911. * control. But such a configuration is useless, so
  9912. * let's keep the code simple.
  9913. */
  9914. if (!is_error_page(page)) {
  9915. vmx->nested.virtual_apic_page = page;
  9916. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  9917. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  9918. }
  9919. }
  9920. if (nested_cpu_has_posted_intr(vmcs12)) {
  9921. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  9922. kunmap(vmx->nested.pi_desc_page);
  9923. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  9924. vmx->nested.pi_desc_page = NULL;
  9925. vmx->nested.pi_desc = NULL;
  9926. vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull);
  9927. }
  9928. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  9929. if (is_error_page(page))
  9930. return;
  9931. vmx->nested.pi_desc_page = page;
  9932. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  9933. vmx->nested.pi_desc =
  9934. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  9935. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9936. (PAGE_SIZE - 1)));
  9937. vmcs_write64(POSTED_INTR_DESC_ADDR,
  9938. page_to_phys(vmx->nested.pi_desc_page) +
  9939. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9940. (PAGE_SIZE - 1)));
  9941. }
  9942. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  9943. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  9944. CPU_BASED_USE_MSR_BITMAPS);
  9945. else
  9946. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  9947. CPU_BASED_USE_MSR_BITMAPS);
  9948. }
  9949. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  9950. {
  9951. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  9952. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9953. /*
  9954. * A timer value of zero is architecturally guaranteed to cause
  9955. * a VMExit prior to executing any instructions in the guest.
  9956. */
  9957. if (preemption_timeout == 0) {
  9958. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  9959. return;
  9960. }
  9961. if (vcpu->arch.virtual_tsc_khz == 0)
  9962. return;
  9963. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9964. preemption_timeout *= 1000000;
  9965. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  9966. hrtimer_start(&vmx->nested.preemption_timer,
  9967. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  9968. }
  9969. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  9970. struct vmcs12 *vmcs12)
  9971. {
  9972. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  9973. return 0;
  9974. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  9975. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  9976. return -EINVAL;
  9977. return 0;
  9978. }
  9979. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  9980. struct vmcs12 *vmcs12)
  9981. {
  9982. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9983. return 0;
  9984. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  9985. return -EINVAL;
  9986. return 0;
  9987. }
  9988. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  9989. struct vmcs12 *vmcs12)
  9990. {
  9991. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9992. return 0;
  9993. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  9994. return -EINVAL;
  9995. return 0;
  9996. }
  9997. static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) {
  9998. int msr;
  9999. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10000. unsigned word = msr / BITS_PER_LONG;
  10001. msr_bitmap[word] = ~0;
  10002. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  10003. }
  10004. }
  10005. /*
  10006. * Merge L0's and L1's MSR bitmap, return false to indicate that
  10007. * we do not use the hardware.
  10008. */
  10009. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  10010. struct vmcs12 *vmcs12)
  10011. {
  10012. int msr;
  10013. struct page *page;
  10014. unsigned long *msr_bitmap_l1;
  10015. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  10016. /*
  10017. * pred_cmd & spec_ctrl are trying to verify two things:
  10018. *
  10019. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  10020. * ensures that we do not accidentally generate an L02 MSR bitmap
  10021. * from the L12 MSR bitmap that is too permissive.
  10022. * 2. That L1 or L2s have actually used the MSR. This avoids
  10023. * unnecessarily merging of the bitmap if the MSR is unused. This
  10024. * works properly because we only update the L01 MSR bitmap lazily.
  10025. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  10026. * updated to reflect this when L1 (or its L2s) actually write to
  10027. * the MSR.
  10028. */
  10029. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  10030. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  10031. /* Nothing to do if the MSR bitmap is not in use. */
  10032. if (!cpu_has_vmx_msr_bitmap() ||
  10033. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  10034. return false;
  10035. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10036. !pred_cmd && !spec_ctrl)
  10037. return false;
  10038. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  10039. if (is_error_page(page))
  10040. return false;
  10041. msr_bitmap_l1 = (unsigned long *)kmap(page);
  10042. /*
  10043. * To keep the control flow simple, pay eight 8-byte writes (sixteen
  10044. * 4-byte writes on 32-bit systems) up front to enable intercepts for
  10045. * the x2APIC MSR range and selectively disable them below.
  10046. */
  10047. enable_x2apic_msr_intercepts(msr_bitmap_l0);
  10048. if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
  10049. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  10050. /*
  10051. * L0 need not intercept reads for MSRs between 0x800
  10052. * and 0x8ff, it just lets the processor take the value
  10053. * from the virtual-APIC page; take those 256 bits
  10054. * directly from the L1 bitmap.
  10055. */
  10056. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10057. unsigned word = msr / BITS_PER_LONG;
  10058. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  10059. }
  10060. }
  10061. nested_vmx_disable_intercept_for_msr(
  10062. msr_bitmap_l1, msr_bitmap_l0,
  10063. X2APIC_MSR(APIC_TASKPRI),
  10064. MSR_TYPE_R | MSR_TYPE_W);
  10065. if (nested_cpu_has_vid(vmcs12)) {
  10066. nested_vmx_disable_intercept_for_msr(
  10067. msr_bitmap_l1, msr_bitmap_l0,
  10068. X2APIC_MSR(APIC_EOI),
  10069. MSR_TYPE_W);
  10070. nested_vmx_disable_intercept_for_msr(
  10071. msr_bitmap_l1, msr_bitmap_l0,
  10072. X2APIC_MSR(APIC_SELF_IPI),
  10073. MSR_TYPE_W);
  10074. }
  10075. }
  10076. if (spec_ctrl)
  10077. nested_vmx_disable_intercept_for_msr(
  10078. msr_bitmap_l1, msr_bitmap_l0,
  10079. MSR_IA32_SPEC_CTRL,
  10080. MSR_TYPE_R | MSR_TYPE_W);
  10081. if (pred_cmd)
  10082. nested_vmx_disable_intercept_for_msr(
  10083. msr_bitmap_l1, msr_bitmap_l0,
  10084. MSR_IA32_PRED_CMD,
  10085. MSR_TYPE_W);
  10086. kunmap(page);
  10087. kvm_release_page_clean(page);
  10088. return true;
  10089. }
  10090. static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10091. struct vmcs12 *vmcs12)
  10092. {
  10093. struct vmcs12 *shadow;
  10094. struct page *page;
  10095. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10096. vmcs12->vmcs_link_pointer == -1ull)
  10097. return;
  10098. shadow = get_shadow_vmcs12(vcpu);
  10099. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10100. memcpy(shadow, kmap(page), VMCS12_SIZE);
  10101. kunmap(page);
  10102. kvm_release_page_clean(page);
  10103. }
  10104. static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10105. struct vmcs12 *vmcs12)
  10106. {
  10107. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10108. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10109. vmcs12->vmcs_link_pointer == -1ull)
  10110. return;
  10111. kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
  10112. get_shadow_vmcs12(vcpu), VMCS12_SIZE);
  10113. }
  10114. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  10115. struct vmcs12 *vmcs12)
  10116. {
  10117. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  10118. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  10119. return -EINVAL;
  10120. else
  10121. return 0;
  10122. }
  10123. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  10124. struct vmcs12 *vmcs12)
  10125. {
  10126. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10127. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  10128. !nested_cpu_has_vid(vmcs12) &&
  10129. !nested_cpu_has_posted_intr(vmcs12))
  10130. return 0;
  10131. /*
  10132. * If virtualize x2apic mode is enabled,
  10133. * virtualize apic access must be disabled.
  10134. */
  10135. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10136. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  10137. return -EINVAL;
  10138. /*
  10139. * If virtual interrupt delivery is enabled,
  10140. * we must exit on external interrupts.
  10141. */
  10142. if (nested_cpu_has_vid(vmcs12) &&
  10143. !nested_exit_on_intr(vcpu))
  10144. return -EINVAL;
  10145. /*
  10146. * bits 15:8 should be zero in posted_intr_nv,
  10147. * the descriptor address has been already checked
  10148. * in nested_get_vmcs12_pages.
  10149. *
  10150. * bits 5:0 of posted_intr_desc_addr should be zero.
  10151. */
  10152. if (nested_cpu_has_posted_intr(vmcs12) &&
  10153. (!nested_cpu_has_vid(vmcs12) ||
  10154. !nested_exit_intr_ack_set(vcpu) ||
  10155. (vmcs12->posted_intr_nv & 0xff00) ||
  10156. (vmcs12->posted_intr_desc_addr & 0x3f) ||
  10157. (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
  10158. return -EINVAL;
  10159. /* tpr shadow is needed by all apicv features. */
  10160. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10161. return -EINVAL;
  10162. return 0;
  10163. }
  10164. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  10165. unsigned long count_field,
  10166. unsigned long addr_field)
  10167. {
  10168. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10169. int maxphyaddr;
  10170. u64 count, addr;
  10171. if (vmcs12_read_any(vmcs12, count_field, &count) ||
  10172. vmcs12_read_any(vmcs12, addr_field, &addr)) {
  10173. WARN_ON(1);
  10174. return -EINVAL;
  10175. }
  10176. if (count == 0)
  10177. return 0;
  10178. maxphyaddr = cpuid_maxphyaddr(vcpu);
  10179. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  10180. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  10181. pr_debug_ratelimited(
  10182. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  10183. addr_field, maxphyaddr, count, addr);
  10184. return -EINVAL;
  10185. }
  10186. return 0;
  10187. }
  10188. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  10189. struct vmcs12 *vmcs12)
  10190. {
  10191. if (vmcs12->vm_exit_msr_load_count == 0 &&
  10192. vmcs12->vm_exit_msr_store_count == 0 &&
  10193. vmcs12->vm_entry_msr_load_count == 0)
  10194. return 0; /* Fast path */
  10195. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  10196. VM_EXIT_MSR_LOAD_ADDR) ||
  10197. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  10198. VM_EXIT_MSR_STORE_ADDR) ||
  10199. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  10200. VM_ENTRY_MSR_LOAD_ADDR))
  10201. return -EINVAL;
  10202. return 0;
  10203. }
  10204. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  10205. struct vmcs12 *vmcs12)
  10206. {
  10207. u64 address = vmcs12->pml_address;
  10208. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  10209. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  10210. if (!nested_cpu_has_ept(vmcs12) ||
  10211. !IS_ALIGNED(address, 4096) ||
  10212. address >> maxphyaddr)
  10213. return -EINVAL;
  10214. }
  10215. return 0;
  10216. }
  10217. static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
  10218. struct vmcs12 *vmcs12)
  10219. {
  10220. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  10221. return 0;
  10222. if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
  10223. !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
  10224. return -EINVAL;
  10225. return 0;
  10226. }
  10227. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  10228. struct vmx_msr_entry *e)
  10229. {
  10230. /* x2APIC MSR accesses are not allowed */
  10231. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  10232. return -EINVAL;
  10233. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  10234. e->index == MSR_IA32_UCODE_REV)
  10235. return -EINVAL;
  10236. if (e->reserved != 0)
  10237. return -EINVAL;
  10238. return 0;
  10239. }
  10240. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  10241. struct vmx_msr_entry *e)
  10242. {
  10243. if (e->index == MSR_FS_BASE ||
  10244. e->index == MSR_GS_BASE ||
  10245. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  10246. nested_vmx_msr_check_common(vcpu, e))
  10247. return -EINVAL;
  10248. return 0;
  10249. }
  10250. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  10251. struct vmx_msr_entry *e)
  10252. {
  10253. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  10254. nested_vmx_msr_check_common(vcpu, e))
  10255. return -EINVAL;
  10256. return 0;
  10257. }
  10258. /*
  10259. * Load guest's/host's msr at nested entry/exit.
  10260. * return 0 for success, entry index for failure.
  10261. */
  10262. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10263. {
  10264. u32 i;
  10265. struct vmx_msr_entry e;
  10266. struct msr_data msr;
  10267. msr.host_initiated = false;
  10268. for (i = 0; i < count; i++) {
  10269. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  10270. &e, sizeof(e))) {
  10271. pr_debug_ratelimited(
  10272. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10273. __func__, i, gpa + i * sizeof(e));
  10274. goto fail;
  10275. }
  10276. if (nested_vmx_load_msr_check(vcpu, &e)) {
  10277. pr_debug_ratelimited(
  10278. "%s check failed (%u, 0x%x, 0x%x)\n",
  10279. __func__, i, e.index, e.reserved);
  10280. goto fail;
  10281. }
  10282. msr.index = e.index;
  10283. msr.data = e.value;
  10284. if (kvm_set_msr(vcpu, &msr)) {
  10285. pr_debug_ratelimited(
  10286. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10287. __func__, i, e.index, e.value);
  10288. goto fail;
  10289. }
  10290. }
  10291. return 0;
  10292. fail:
  10293. return i + 1;
  10294. }
  10295. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10296. {
  10297. u32 i;
  10298. struct vmx_msr_entry e;
  10299. for (i = 0; i < count; i++) {
  10300. struct msr_data msr_info;
  10301. if (kvm_vcpu_read_guest(vcpu,
  10302. gpa + i * sizeof(e),
  10303. &e, 2 * sizeof(u32))) {
  10304. pr_debug_ratelimited(
  10305. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10306. __func__, i, gpa + i * sizeof(e));
  10307. return -EINVAL;
  10308. }
  10309. if (nested_vmx_store_msr_check(vcpu, &e)) {
  10310. pr_debug_ratelimited(
  10311. "%s check failed (%u, 0x%x, 0x%x)\n",
  10312. __func__, i, e.index, e.reserved);
  10313. return -EINVAL;
  10314. }
  10315. msr_info.host_initiated = false;
  10316. msr_info.index = e.index;
  10317. if (kvm_get_msr(vcpu, &msr_info)) {
  10318. pr_debug_ratelimited(
  10319. "%s cannot read MSR (%u, 0x%x)\n",
  10320. __func__, i, e.index);
  10321. return -EINVAL;
  10322. }
  10323. if (kvm_vcpu_write_guest(vcpu,
  10324. gpa + i * sizeof(e) +
  10325. offsetof(struct vmx_msr_entry, value),
  10326. &msr_info.data, sizeof(msr_info.data))) {
  10327. pr_debug_ratelimited(
  10328. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10329. __func__, i, e.index, msr_info.data);
  10330. return -EINVAL;
  10331. }
  10332. }
  10333. return 0;
  10334. }
  10335. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  10336. {
  10337. unsigned long invalid_mask;
  10338. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  10339. return (val & invalid_mask) == 0;
  10340. }
  10341. /*
  10342. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  10343. * emulating VM entry into a guest with EPT enabled.
  10344. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10345. * is assigned to entry_failure_code on failure.
  10346. */
  10347. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  10348. u32 *entry_failure_code)
  10349. {
  10350. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  10351. if (!nested_cr3_valid(vcpu, cr3)) {
  10352. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10353. return 1;
  10354. }
  10355. /*
  10356. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  10357. * must not be dereferenced.
  10358. */
  10359. if (is_pae_paging(vcpu) && !nested_ept) {
  10360. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  10361. *entry_failure_code = ENTRY_FAIL_PDPTE;
  10362. return 1;
  10363. }
  10364. }
  10365. }
  10366. if (!nested_ept)
  10367. kvm_mmu_new_cr3(vcpu, cr3, false);
  10368. vcpu->arch.cr3 = cr3;
  10369. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  10370. kvm_init_mmu(vcpu, false);
  10371. return 0;
  10372. }
  10373. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10374. {
  10375. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10376. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  10377. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  10378. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  10379. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  10380. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  10381. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  10382. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  10383. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  10384. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  10385. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  10386. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  10387. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  10388. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  10389. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  10390. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  10391. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  10392. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  10393. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  10394. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  10395. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  10396. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  10397. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  10398. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  10399. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  10400. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  10401. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  10402. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  10403. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  10404. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  10405. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  10406. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  10407. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  10408. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  10409. vmcs12->guest_pending_dbg_exceptions);
  10410. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  10411. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  10412. if (nested_cpu_has_xsaves(vmcs12))
  10413. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  10414. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  10415. if (cpu_has_vmx_posted_intr())
  10416. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  10417. /*
  10418. * Whether page-faults are trapped is determined by a combination of
  10419. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  10420. * If enable_ept, L0 doesn't care about page faults and we should
  10421. * set all of these to L1's desires. However, if !enable_ept, L0 does
  10422. * care about (at least some) page faults, and because it is not easy
  10423. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  10424. * to exit on each and every L2 page fault. This is done by setting
  10425. * MASK=MATCH=0 and (see below) EB.PF=1.
  10426. * Note that below we don't need special code to set EB.PF beyond the
  10427. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  10428. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  10429. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  10430. */
  10431. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  10432. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  10433. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  10434. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  10435. /* All VMFUNCs are currently emulated through L0 vmexits. */
  10436. if (cpu_has_vmx_vmfunc())
  10437. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  10438. if (cpu_has_vmx_apicv()) {
  10439. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  10440. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  10441. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  10442. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  10443. }
  10444. /*
  10445. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  10446. * Some constant fields are set here by vmx_set_constant_host_state().
  10447. * Other fields are different per CPU, and will be set later when
  10448. * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
  10449. * is called.
  10450. */
  10451. vmx_set_constant_host_state(vmx);
  10452. /*
  10453. * Set the MSR load/store lists to match L0's settings.
  10454. */
  10455. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  10456. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  10457. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  10458. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  10459. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  10460. set_cr4_guest_host_mask(vmx);
  10461. if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
  10462. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  10463. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  10464. if (enable_vpid) {
  10465. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  10466. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  10467. else
  10468. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  10469. }
  10470. /*
  10471. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  10472. */
  10473. if (enable_ept) {
  10474. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  10475. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  10476. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  10477. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  10478. }
  10479. if (cpu_has_vmx_msr_bitmap())
  10480. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  10481. }
  10482. /*
  10483. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  10484. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  10485. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  10486. * guest in a way that will both be appropriate to L1's requests, and our
  10487. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  10488. * function also has additional necessary side-effects, like setting various
  10489. * vcpu->arch fields.
  10490. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10491. * is assigned to entry_failure_code on failure.
  10492. */
  10493. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10494. u32 *entry_failure_code)
  10495. {
  10496. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10497. u32 exec_control, vmcs12_exec_ctrl;
  10498. if (vmx->nested.dirty_vmcs12) {
  10499. prepare_vmcs02_full(vcpu, vmcs12);
  10500. vmx->nested.dirty_vmcs12 = false;
  10501. }
  10502. /*
  10503. * First, the fields that are shadowed. This must be kept in sync
  10504. * with vmx_shadow_fields.h.
  10505. */
  10506. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  10507. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  10508. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  10509. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  10510. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  10511. if (vmx->nested.nested_run_pending &&
  10512. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  10513. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  10514. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  10515. } else {
  10516. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  10517. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  10518. }
  10519. if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
  10520. !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
  10521. vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
  10522. if (vmx->nested.nested_run_pending) {
  10523. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  10524. vmcs12->vm_entry_intr_info_field);
  10525. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  10526. vmcs12->vm_entry_exception_error_code);
  10527. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  10528. vmcs12->vm_entry_instruction_len);
  10529. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  10530. vmcs12->guest_interruptibility_info);
  10531. vmx->loaded_vmcs->nmi_known_unmasked =
  10532. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  10533. } else {
  10534. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  10535. }
  10536. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  10537. exec_control = vmcs12->pin_based_vm_exec_control;
  10538. /* Preemption timer setting is computed directly in vmx_vcpu_run. */
  10539. exec_control |= vmcs_config.pin_based_exec_ctrl;
  10540. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  10541. vmx->loaded_vmcs->hv_timer_armed = false;
  10542. /* Posted interrupts setting is only taken from vmcs12. */
  10543. if (nested_cpu_has_posted_intr(vmcs12)) {
  10544. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  10545. vmx->nested.pi_pending = false;
  10546. } else {
  10547. exec_control &= ~PIN_BASED_POSTED_INTR;
  10548. }
  10549. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  10550. vmx->nested.preemption_timer_expired = false;
  10551. if (nested_cpu_has_preemption_timer(vmcs12))
  10552. vmx_start_preemption_timer(vcpu);
  10553. if (cpu_has_secondary_exec_ctrls()) {
  10554. exec_control = vmx->secondary_exec_control;
  10555. /* Take the following fields only from vmcs12 */
  10556. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10557. SECONDARY_EXEC_ENABLE_INVPCID |
  10558. SECONDARY_EXEC_RDTSCP |
  10559. SECONDARY_EXEC_XSAVES |
  10560. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  10561. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  10562. SECONDARY_EXEC_ENABLE_VMFUNC);
  10563. if (nested_cpu_has(vmcs12,
  10564. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  10565. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  10566. ~SECONDARY_EXEC_ENABLE_PML;
  10567. exec_control |= vmcs12_exec_ctrl;
  10568. }
  10569. /* VMCS shadowing for L2 is emulated for now */
  10570. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  10571. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  10572. vmcs_write16(GUEST_INTR_STATUS,
  10573. vmcs12->guest_intr_status);
  10574. /*
  10575. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  10576. * nested_get_vmcs12_pages will either fix it up or
  10577. * remove the VM execution control.
  10578. */
  10579. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  10580. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  10581. if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
  10582. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  10583. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  10584. }
  10585. /*
  10586. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  10587. * entry, but only if the current (host) sp changed from the value
  10588. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  10589. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  10590. * here we just force the write to happen on entry.
  10591. */
  10592. vmx->host_rsp = 0;
  10593. exec_control = vmx_exec_control(vmx); /* L0's desires */
  10594. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  10595. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  10596. exec_control &= ~CPU_BASED_TPR_SHADOW;
  10597. exec_control |= vmcs12->cpu_based_vm_exec_control;
  10598. /*
  10599. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  10600. * nested_get_vmcs12_pages can't fix it up, the illegal value
  10601. * will result in a VM entry failure.
  10602. */
  10603. if (exec_control & CPU_BASED_TPR_SHADOW) {
  10604. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  10605. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  10606. } else {
  10607. #ifdef CONFIG_X86_64
  10608. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  10609. CPU_BASED_CR8_STORE_EXITING;
  10610. #endif
  10611. }
  10612. /*
  10613. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  10614. * for I/O port accesses.
  10615. */
  10616. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  10617. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  10618. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  10619. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  10620. * bitwise-or of what L1 wants to trap for L2, and what we want to
  10621. * trap. Note that CR0.TS also needs updating - we do this later.
  10622. */
  10623. update_exception_bitmap(vcpu);
  10624. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  10625. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  10626. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  10627. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  10628. * bits are further modified by vmx_set_efer() below.
  10629. */
  10630. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  10631. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  10632. * emulated by vmx_set_efer(), below.
  10633. */
  10634. vm_entry_controls_init(vmx,
  10635. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  10636. ~VM_ENTRY_IA32E_MODE) |
  10637. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  10638. if (vmx->nested.nested_run_pending &&
  10639. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  10640. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  10641. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  10642. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  10643. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  10644. }
  10645. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10646. if (kvm_has_tsc_control)
  10647. decache_tsc_multiplier(vmx);
  10648. if (enable_vpid) {
  10649. /*
  10650. * There is no direct mapping between vpid02 and vpid12, the
  10651. * vpid02 is per-vCPU for L0 and reused while the value of
  10652. * vpid12 is changed w/ one invvpid during nested vmentry.
  10653. * The vpid12 is allocated by L1 for L2, so it will not
  10654. * influence global bitmap(for vpid01 and vpid02 allocation)
  10655. * even if spawn a lot of nested vCPUs.
  10656. */
  10657. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  10658. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  10659. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  10660. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  10661. }
  10662. } else {
  10663. vmx_flush_tlb(vcpu, true);
  10664. }
  10665. }
  10666. if (enable_pml) {
  10667. /*
  10668. * Conceptually we want to copy the PML address and index from
  10669. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  10670. * since we always flush the log on each vmexit, this happens
  10671. * to be equivalent to simply resetting the fields in vmcs02.
  10672. */
  10673. ASSERT(vmx->pml_pg);
  10674. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  10675. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  10676. }
  10677. if (nested_cpu_has_ept(vmcs12)) {
  10678. if (nested_ept_init_mmu_context(vcpu)) {
  10679. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10680. return 1;
  10681. }
  10682. } else if (nested_cpu_has2(vmcs12,
  10683. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10684. vmx_flush_tlb(vcpu, true);
  10685. }
  10686. /*
  10687. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  10688. * bits which we consider mandatory enabled.
  10689. * The CR0_READ_SHADOW is what L2 should have expected to read given
  10690. * the specifications by L1; It's not enough to take
  10691. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  10692. * have more bits than L1 expected.
  10693. */
  10694. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  10695. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  10696. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  10697. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  10698. if (vmx->nested.nested_run_pending &&
  10699. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  10700. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  10701. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  10702. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10703. else
  10704. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10705. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  10706. vmx_set_efer(vcpu, vcpu->arch.efer);
  10707. /*
  10708. * Guest state is invalid and unrestricted guest is disabled,
  10709. * which means L1 attempted VMEntry to L2 with invalid state.
  10710. * Fail the VMEntry.
  10711. */
  10712. if (vmx->emulation_required) {
  10713. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10714. return 1;
  10715. }
  10716. /* Shadow page tables on either EPT or shadow page tables. */
  10717. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  10718. entry_failure_code))
  10719. return 1;
  10720. if (!enable_ept)
  10721. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  10722. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  10723. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  10724. return 0;
  10725. }
  10726. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  10727. {
  10728. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  10729. nested_cpu_has_virtual_nmis(vmcs12))
  10730. return -EINVAL;
  10731. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  10732. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  10733. return -EINVAL;
  10734. return 0;
  10735. }
  10736. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10737. {
  10738. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10739. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  10740. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  10741. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10742. if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
  10743. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10744. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  10745. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10746. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  10747. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10748. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  10749. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10750. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  10751. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10752. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  10753. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10754. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  10755. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10756. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  10757. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10758. if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
  10759. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10760. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  10761. vmx->nested.msrs.procbased_ctls_low,
  10762. vmx->nested.msrs.procbased_ctls_high) ||
  10763. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  10764. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  10765. vmx->nested.msrs.secondary_ctls_low,
  10766. vmx->nested.msrs.secondary_ctls_high)) ||
  10767. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  10768. vmx->nested.msrs.pinbased_ctls_low,
  10769. vmx->nested.msrs.pinbased_ctls_high) ||
  10770. !vmx_control_verify(vmcs12->vm_exit_controls,
  10771. vmx->nested.msrs.exit_ctls_low,
  10772. vmx->nested.msrs.exit_ctls_high) ||
  10773. !vmx_control_verify(vmcs12->vm_entry_controls,
  10774. vmx->nested.msrs.entry_ctls_low,
  10775. vmx->nested.msrs.entry_ctls_high))
  10776. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10777. if (nested_vmx_check_nmi_controls(vmcs12))
  10778. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10779. if (nested_cpu_has_vmfunc(vmcs12)) {
  10780. if (vmcs12->vm_function_control &
  10781. ~vmx->nested.msrs.vmfunc_controls)
  10782. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10783. if (nested_cpu_has_eptp_switching(vmcs12)) {
  10784. if (!nested_cpu_has_ept(vmcs12) ||
  10785. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  10786. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10787. }
  10788. }
  10789. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  10790. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10791. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  10792. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  10793. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  10794. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  10795. /*
  10796. * From the Intel SDM, volume 3:
  10797. * Fields relevant to VM-entry event injection must be set properly.
  10798. * These fields are the VM-entry interruption-information field, the
  10799. * VM-entry exception error code, and the VM-entry instruction length.
  10800. */
  10801. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  10802. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  10803. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  10804. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  10805. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  10806. bool should_have_error_code;
  10807. bool urg = nested_cpu_has2(vmcs12,
  10808. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  10809. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  10810. /* VM-entry interruption-info field: interruption type */
  10811. if (intr_type == INTR_TYPE_RESERVED ||
  10812. (intr_type == INTR_TYPE_OTHER_EVENT &&
  10813. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  10814. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10815. /* VM-entry interruption-info field: vector */
  10816. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  10817. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  10818. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  10819. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10820. /* VM-entry interruption-info field: deliver error code */
  10821. should_have_error_code =
  10822. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  10823. x86_exception_has_error_code(vector);
  10824. if (has_error_code != should_have_error_code)
  10825. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10826. /* VM-entry exception error code */
  10827. if (has_error_code &&
  10828. vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))
  10829. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10830. /* VM-entry interruption-info field: reserved bits */
  10831. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  10832. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10833. /* VM-entry instruction length */
  10834. switch (intr_type) {
  10835. case INTR_TYPE_SOFT_EXCEPTION:
  10836. case INTR_TYPE_SOFT_INTR:
  10837. case INTR_TYPE_PRIV_SW_EXCEPTION:
  10838. if ((vmcs12->vm_entry_instruction_len > 15) ||
  10839. (vmcs12->vm_entry_instruction_len == 0 &&
  10840. !nested_cpu_has_zero_length_injection(vcpu)))
  10841. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10842. }
  10843. }
  10844. return 0;
  10845. }
  10846. static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
  10847. struct vmcs12 *vmcs12)
  10848. {
  10849. int r;
  10850. struct page *page;
  10851. struct vmcs12 *shadow;
  10852. if (vmcs12->vmcs_link_pointer == -1ull)
  10853. return 0;
  10854. if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
  10855. return -EINVAL;
  10856. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10857. if (is_error_page(page))
  10858. return -EINVAL;
  10859. r = 0;
  10860. shadow = kmap(page);
  10861. if (shadow->hdr.revision_id != VMCS12_REVISION ||
  10862. shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
  10863. r = -EINVAL;
  10864. kunmap(page);
  10865. kvm_release_page_clean(page);
  10866. return r;
  10867. }
  10868. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10869. u32 *exit_qual)
  10870. {
  10871. bool ia32e;
  10872. *exit_qual = ENTRY_FAIL_DEFAULT;
  10873. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  10874. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  10875. return 1;
  10876. if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
  10877. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  10878. return 1;
  10879. }
  10880. /*
  10881. * If the load IA32_EFER VM-entry control is 1, the following checks
  10882. * are performed on the field for the IA32_EFER MSR:
  10883. * - Bits reserved in the IA32_EFER MSR must be 0.
  10884. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  10885. * the IA-32e mode guest VM-exit control. It must also be identical
  10886. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  10887. * CR0.PG) is 1.
  10888. */
  10889. if (to_vmx(vcpu)->nested.nested_run_pending &&
  10890. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  10891. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  10892. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  10893. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  10894. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  10895. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  10896. return 1;
  10897. }
  10898. /*
  10899. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  10900. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  10901. * the values of the LMA and LME bits in the field must each be that of
  10902. * the host address-space size VM-exit control.
  10903. */
  10904. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  10905. ia32e = (vmcs12->vm_exit_controls &
  10906. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  10907. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  10908. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  10909. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  10910. return 1;
  10911. }
  10912. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  10913. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  10914. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  10915. return 1;
  10916. return 0;
  10917. }
  10918. /*
  10919. * If exit_qual is NULL, this is being called from state restore (either RSM
  10920. * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
  10921. */
  10922. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
  10923. {
  10924. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10925. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10926. bool from_vmentry = !!exit_qual;
  10927. u32 dummy_exit_qual;
  10928. bool evaluate_pending_interrupts;
  10929. int r = 0;
  10930. evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  10931. (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
  10932. if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
  10933. evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
  10934. enter_guest_mode(vcpu);
  10935. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  10936. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10937. if (kvm_mpx_supported() &&
  10938. !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  10939. vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  10940. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  10941. vmx_segment_cache_clear(vmx);
  10942. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10943. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  10944. r = EXIT_REASON_INVALID_STATE;
  10945. if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
  10946. goto fail;
  10947. if (from_vmentry) {
  10948. nested_get_vmcs12_pages(vcpu);
  10949. r = EXIT_REASON_MSR_LOAD_FAIL;
  10950. *exit_qual = nested_vmx_load_msr(vcpu,
  10951. vmcs12->vm_entry_msr_load_addr,
  10952. vmcs12->vm_entry_msr_load_count);
  10953. if (*exit_qual)
  10954. goto fail;
  10955. } else {
  10956. /*
  10957. * The MMU is not initialized to point at the right entities yet and
  10958. * "get pages" would need to read data from the guest (i.e. we will
  10959. * need to perform gpa to hpa translation). Request a call
  10960. * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
  10961. * have already been set at vmentry time and should not be reset.
  10962. */
  10963. kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
  10964. }
  10965. /*
  10966. * If L1 had a pending IRQ/NMI until it executed
  10967. * VMLAUNCH/VMRESUME which wasn't delivered because it was
  10968. * disallowed (e.g. interrupts disabled), L0 needs to
  10969. * evaluate if this pending event should cause an exit from L2
  10970. * to L1 or delivered directly to L2 (e.g. In case L1 don't
  10971. * intercept EXTERNAL_INTERRUPT).
  10972. *
  10973. * Usually this would be handled by the processor noticing an
  10974. * IRQ/NMI window request, or checking RVI during evaluation of
  10975. * pending virtual interrupts. However, this setting was done
  10976. * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
  10977. * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
  10978. */
  10979. if (unlikely(evaluate_pending_interrupts))
  10980. kvm_make_request(KVM_REQ_EVENT, vcpu);
  10981. /*
  10982. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  10983. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  10984. * returned as far as L1 is concerned. It will only return (and set
  10985. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  10986. */
  10987. return 0;
  10988. fail:
  10989. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10990. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10991. leave_guest_mode(vcpu);
  10992. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10993. return r;
  10994. }
  10995. /*
  10996. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  10997. * for running an L2 nested guest.
  10998. */
  10999. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  11000. {
  11001. struct vmcs12 *vmcs12;
  11002. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11003. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  11004. u32 exit_qual;
  11005. int ret;
  11006. if (!nested_vmx_check_permission(vcpu))
  11007. return 1;
  11008. if (!nested_vmx_check_vmcs12(vcpu))
  11009. goto out;
  11010. vmcs12 = get_vmcs12(vcpu);
  11011. /*
  11012. * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
  11013. * that there *is* a valid VMCS pointer, RFLAGS.CF is set
  11014. * rather than RFLAGS.ZF, and no error number is stored to the
  11015. * VM-instruction error field.
  11016. */
  11017. if (vmcs12->hdr.shadow_vmcs) {
  11018. nested_vmx_failInvalid(vcpu);
  11019. goto out;
  11020. }
  11021. if (enable_shadow_vmcs)
  11022. copy_shadow_to_vmcs12(vmx);
  11023. /*
  11024. * The nested entry process starts with enforcing various prerequisites
  11025. * on vmcs12 as required by the Intel SDM, and act appropriately when
  11026. * they fail: As the SDM explains, some conditions should cause the
  11027. * instruction to fail, while others will cause the instruction to seem
  11028. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  11029. * To speed up the normal (success) code path, we should avoid checking
  11030. * for misconfigurations which will anyway be caught by the processor
  11031. * when using the merged vmcs02.
  11032. */
  11033. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  11034. nested_vmx_failValid(vcpu,
  11035. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  11036. goto out;
  11037. }
  11038. if (vmcs12->launch_state == launch) {
  11039. nested_vmx_failValid(vcpu,
  11040. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  11041. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  11042. goto out;
  11043. }
  11044. ret = check_vmentry_prereqs(vcpu, vmcs12);
  11045. if (ret) {
  11046. nested_vmx_failValid(vcpu, ret);
  11047. goto out;
  11048. }
  11049. /*
  11050. * After this point, the trap flag no longer triggers a singlestep trap
  11051. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  11052. * This is not 100% correct; for performance reasons, we delegate most
  11053. * of the checks on host state to the processor. If those fail,
  11054. * the singlestep trap is missed.
  11055. */
  11056. skip_emulated_instruction(vcpu);
  11057. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  11058. if (ret) {
  11059. nested_vmx_entry_failure(vcpu, vmcs12,
  11060. EXIT_REASON_INVALID_STATE, exit_qual);
  11061. return 1;
  11062. }
  11063. /*
  11064. * We're finally done with prerequisite checking, and can start with
  11065. * the nested entry.
  11066. */
  11067. vmx->nested.nested_run_pending = 1;
  11068. ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
  11069. if (ret) {
  11070. nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
  11071. vmx->nested.nested_run_pending = 0;
  11072. return 1;
  11073. }
  11074. /* Hide L1D cache contents from the nested guest. */
  11075. vmx->vcpu.arch.l1tf_flush_l1d = true;
  11076. /*
  11077. * Must happen outside of enter_vmx_non_root_mode() as it will
  11078. * also be used as part of restoring nVMX state for
  11079. * snapshot restore (migration).
  11080. *
  11081. * In this flow, it is assumed that vmcs12 cache was
  11082. * trasferred as part of captured nVMX state and should
  11083. * therefore not be read from guest memory (which may not
  11084. * exist on destination host yet).
  11085. */
  11086. nested_cache_shadow_vmcs12(vcpu, vmcs12);
  11087. /*
  11088. * If we're entering a halted L2 vcpu and the L2 vcpu won't be
  11089. * awakened by event injection or by an NMI-window VM-exit or
  11090. * by an interrupt-window VM-exit, halt the vcpu.
  11091. */
  11092. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  11093. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
  11094. !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_NMI_PENDING) &&
  11095. !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING) &&
  11096. (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
  11097. vmx->nested.nested_run_pending = 0;
  11098. return kvm_vcpu_halt(vcpu);
  11099. }
  11100. return 1;
  11101. out:
  11102. return kvm_skip_emulated_instruction(vcpu);
  11103. }
  11104. /*
  11105. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  11106. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  11107. * This function returns the new value we should put in vmcs12.guest_cr0.
  11108. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  11109. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  11110. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  11111. * didn't trap the bit, because if L1 did, so would L0).
  11112. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  11113. * been modified by L2, and L1 knows it. So just leave the old value of
  11114. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  11115. * isn't relevant, because if L0 traps this bit it can set it to anything.
  11116. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  11117. * changed these bits, and therefore they need to be updated, but L0
  11118. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  11119. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  11120. */
  11121. static inline unsigned long
  11122. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11123. {
  11124. return
  11125. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  11126. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  11127. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  11128. vcpu->arch.cr0_guest_owned_bits));
  11129. }
  11130. static inline unsigned long
  11131. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11132. {
  11133. return
  11134. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  11135. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  11136. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  11137. vcpu->arch.cr4_guest_owned_bits));
  11138. }
  11139. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  11140. struct vmcs12 *vmcs12)
  11141. {
  11142. u32 idt_vectoring;
  11143. unsigned int nr;
  11144. if (vcpu->arch.exception.injected) {
  11145. nr = vcpu->arch.exception.nr;
  11146. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11147. if (kvm_exception_is_soft(nr)) {
  11148. vmcs12->vm_exit_instruction_len =
  11149. vcpu->arch.event_exit_inst_len;
  11150. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  11151. } else
  11152. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  11153. if (vcpu->arch.exception.has_error_code) {
  11154. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  11155. vmcs12->idt_vectoring_error_code =
  11156. vcpu->arch.exception.error_code;
  11157. }
  11158. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11159. } else if (vcpu->arch.nmi_injected) {
  11160. vmcs12->idt_vectoring_info_field =
  11161. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  11162. } else if (vcpu->arch.interrupt.injected) {
  11163. nr = vcpu->arch.interrupt.nr;
  11164. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11165. if (vcpu->arch.interrupt.soft) {
  11166. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  11167. vmcs12->vm_entry_instruction_len =
  11168. vcpu->arch.event_exit_inst_len;
  11169. } else
  11170. idt_vectoring |= INTR_TYPE_EXT_INTR;
  11171. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11172. }
  11173. }
  11174. static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
  11175. {
  11176. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11177. unsigned long exit_qual;
  11178. bool block_nested_events =
  11179. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  11180. if (vcpu->arch.exception.pending &&
  11181. nested_vmx_check_exception(vcpu, &exit_qual)) {
  11182. if (block_nested_events)
  11183. return -EBUSY;
  11184. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  11185. return 0;
  11186. }
  11187. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  11188. vmx->nested.preemption_timer_expired) {
  11189. if (block_nested_events)
  11190. return -EBUSY;
  11191. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  11192. return 0;
  11193. }
  11194. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  11195. if (block_nested_events)
  11196. return -EBUSY;
  11197. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  11198. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  11199. INTR_INFO_VALID_MASK, 0);
  11200. /*
  11201. * The NMI-triggered VM exit counts as injection:
  11202. * clear this one and block further NMIs.
  11203. */
  11204. vcpu->arch.nmi_pending = 0;
  11205. vmx_set_nmi_mask(vcpu, true);
  11206. return 0;
  11207. }
  11208. if (kvm_cpu_has_interrupt(vcpu) && nested_exit_on_intr(vcpu)) {
  11209. if (block_nested_events)
  11210. return -EBUSY;
  11211. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  11212. return 0;
  11213. }
  11214. vmx_complete_nested_posted_interrupt(vcpu);
  11215. return 0;
  11216. }
  11217. static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
  11218. {
  11219. to_vmx(vcpu)->req_immediate_exit = true;
  11220. }
  11221. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  11222. {
  11223. ktime_t remaining =
  11224. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  11225. u64 value;
  11226. if (ktime_to_ns(remaining) <= 0)
  11227. return 0;
  11228. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  11229. do_div(value, 1000000);
  11230. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  11231. }
  11232. /*
  11233. * Update the guest state fields of vmcs12 to reflect changes that
  11234. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  11235. * VM-entry controls is also updated, since this is really a guest
  11236. * state bit.)
  11237. */
  11238. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11239. {
  11240. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  11241. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  11242. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  11243. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  11244. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  11245. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  11246. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  11247. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  11248. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  11249. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  11250. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  11251. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  11252. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  11253. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  11254. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  11255. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  11256. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  11257. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  11258. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  11259. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  11260. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  11261. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  11262. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  11263. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  11264. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  11265. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  11266. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  11267. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  11268. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  11269. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  11270. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  11271. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  11272. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  11273. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  11274. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  11275. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  11276. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  11277. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  11278. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  11279. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  11280. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  11281. vmcs12->guest_interruptibility_info =
  11282. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  11283. vmcs12->guest_pending_dbg_exceptions =
  11284. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  11285. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  11286. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  11287. else
  11288. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  11289. if (nested_cpu_has_preemption_timer(vmcs12)) {
  11290. if (vmcs12->vm_exit_controls &
  11291. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  11292. vmcs12->vmx_preemption_timer_value =
  11293. vmx_get_preemption_timer_value(vcpu);
  11294. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  11295. }
  11296. /*
  11297. * In some cases (usually, nested EPT), L2 is allowed to change its
  11298. * own CR3 without exiting. If it has changed it, we must keep it.
  11299. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  11300. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  11301. *
  11302. * Additionally, restore L2's PDPTR to vmcs12.
  11303. */
  11304. if (enable_ept) {
  11305. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  11306. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  11307. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  11308. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  11309. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  11310. }
  11311. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  11312. if (nested_cpu_has_vid(vmcs12))
  11313. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  11314. vmcs12->vm_entry_controls =
  11315. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  11316. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  11317. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  11318. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  11319. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11320. }
  11321. /* TODO: These cannot have changed unless we have MSR bitmaps and
  11322. * the relevant bit asks not to trap the change */
  11323. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  11324. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  11325. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  11326. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  11327. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  11328. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  11329. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  11330. if (kvm_mpx_supported())
  11331. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11332. }
  11333. /*
  11334. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  11335. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  11336. * and this function updates it to reflect the changes to the guest state while
  11337. * L2 was running (and perhaps made some exits which were handled directly by L0
  11338. * without going back to L1), and to reflect the exit reason.
  11339. * Note that we do not have to copy here all VMCS fields, just those that
  11340. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  11341. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  11342. * which already writes to vmcs12 directly.
  11343. */
  11344. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11345. u32 exit_reason, u32 exit_intr_info,
  11346. unsigned long exit_qualification)
  11347. {
  11348. /* update guest state fields: */
  11349. sync_vmcs12(vcpu, vmcs12);
  11350. /* update exit information fields: */
  11351. vmcs12->vm_exit_reason = exit_reason;
  11352. vmcs12->exit_qualification = exit_qualification;
  11353. vmcs12->vm_exit_intr_info = exit_intr_info;
  11354. vmcs12->idt_vectoring_info_field = 0;
  11355. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  11356. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  11357. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  11358. vmcs12->launch_state = 1;
  11359. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  11360. * instead of reading the real value. */
  11361. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  11362. /*
  11363. * Transfer the event that L0 or L1 may wanted to inject into
  11364. * L2 to IDT_VECTORING_INFO_FIELD.
  11365. */
  11366. vmcs12_save_pending_event(vcpu, vmcs12);
  11367. }
  11368. /*
  11369. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  11370. * preserved above and would only end up incorrectly in L1.
  11371. */
  11372. vcpu->arch.nmi_injected = false;
  11373. kvm_clear_exception_queue(vcpu);
  11374. kvm_clear_interrupt_queue(vcpu);
  11375. }
  11376. /*
  11377. * A part of what we need to when the nested L2 guest exits and we want to
  11378. * run its L1 parent, is to reset L1's guest state to the host state specified
  11379. * in vmcs12.
  11380. * This function is to be called not only on normal nested exit, but also on
  11381. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  11382. * Failures During or After Loading Guest State").
  11383. * This function should be called when the active VMCS is L1's (vmcs01).
  11384. */
  11385. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11386. struct vmcs12 *vmcs12)
  11387. {
  11388. struct kvm_segment seg;
  11389. u32 entry_failure_code;
  11390. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  11391. vcpu->arch.efer = vmcs12->host_ia32_efer;
  11392. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11393. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  11394. else
  11395. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  11396. vmx_set_efer(vcpu, vcpu->arch.efer);
  11397. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  11398. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  11399. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  11400. /*
  11401. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  11402. * actually changed, because vmx_set_cr0 refers to efer set above.
  11403. *
  11404. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  11405. * (KVM doesn't change it);
  11406. */
  11407. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11408. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  11409. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  11410. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11411. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  11412. nested_ept_uninit_mmu_context(vcpu);
  11413. /*
  11414. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  11415. * couldn't have changed.
  11416. */
  11417. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  11418. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  11419. if (!enable_ept)
  11420. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  11421. /*
  11422. * If vmcs01 don't use VPID, CPU flushes TLB on every
  11423. * VMEntry/VMExit. Thus, no need to flush TLB.
  11424. *
  11425. * If vmcs12 uses VPID, TLB entries populated by L2 are
  11426. * tagged with vmx->nested.vpid02 while L1 entries are tagged
  11427. * with vmx->vpid. Thus, no need to flush TLB.
  11428. *
  11429. * Therefore, flush TLB only in case vmcs01 uses VPID and
  11430. * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
  11431. * are both tagged with vmx->vpid.
  11432. */
  11433. if (enable_vpid &&
  11434. !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
  11435. vmx_flush_tlb(vcpu, true);
  11436. }
  11437. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  11438. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  11439. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  11440. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  11441. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  11442. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  11443. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  11444. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  11445. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  11446. vmcs_write64(GUEST_BNDCFGS, 0);
  11447. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  11448. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  11449. vcpu->arch.pat = vmcs12->host_ia32_pat;
  11450. }
  11451. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  11452. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  11453. vmcs12->host_ia32_perf_global_ctrl);
  11454. /* Set L1 segment info according to Intel SDM
  11455. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  11456. seg = (struct kvm_segment) {
  11457. .base = 0,
  11458. .limit = 0xFFFFFFFF,
  11459. .selector = vmcs12->host_cs_selector,
  11460. .type = 11,
  11461. .present = 1,
  11462. .s = 1,
  11463. .g = 1
  11464. };
  11465. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11466. seg.l = 1;
  11467. else
  11468. seg.db = 1;
  11469. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  11470. seg = (struct kvm_segment) {
  11471. .base = 0,
  11472. .limit = 0xFFFFFFFF,
  11473. .type = 3,
  11474. .present = 1,
  11475. .s = 1,
  11476. .db = 1,
  11477. .g = 1
  11478. };
  11479. seg.selector = vmcs12->host_ds_selector;
  11480. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  11481. seg.selector = vmcs12->host_es_selector;
  11482. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  11483. seg.selector = vmcs12->host_ss_selector;
  11484. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  11485. seg.selector = vmcs12->host_fs_selector;
  11486. seg.base = vmcs12->host_fs_base;
  11487. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  11488. seg.selector = vmcs12->host_gs_selector;
  11489. seg.base = vmcs12->host_gs_base;
  11490. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  11491. seg = (struct kvm_segment) {
  11492. .base = vmcs12->host_tr_base,
  11493. .limit = 0x67,
  11494. .selector = vmcs12->host_tr_selector,
  11495. .type = 11,
  11496. .present = 1
  11497. };
  11498. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  11499. kvm_set_dr(vcpu, 7, 0x400);
  11500. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  11501. if (cpu_has_vmx_msr_bitmap())
  11502. vmx_update_msr_bitmap(vcpu);
  11503. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  11504. vmcs12->vm_exit_msr_load_count))
  11505. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  11506. }
  11507. static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
  11508. {
  11509. struct shared_msr_entry *efer_msr;
  11510. unsigned int i;
  11511. if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
  11512. return vmcs_read64(GUEST_IA32_EFER);
  11513. if (cpu_has_load_ia32_efer)
  11514. return host_efer;
  11515. for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
  11516. if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
  11517. return vmx->msr_autoload.guest.val[i].value;
  11518. }
  11519. efer_msr = find_msr_entry(vmx, MSR_EFER);
  11520. if (efer_msr)
  11521. return efer_msr->data;
  11522. return host_efer;
  11523. }
  11524. static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
  11525. {
  11526. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11527. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11528. struct vmx_msr_entry g, h;
  11529. struct msr_data msr;
  11530. gpa_t gpa;
  11531. u32 i, j;
  11532. vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
  11533. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  11534. /*
  11535. * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
  11536. * as vmcs01.GUEST_DR7 contains a userspace defined value
  11537. * and vcpu->arch.dr7 is not squirreled away before the
  11538. * nested VMENTER (not worth adding a variable in nested_vmx).
  11539. */
  11540. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  11541. kvm_set_dr(vcpu, 7, DR7_FIXED_1);
  11542. else
  11543. WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
  11544. }
  11545. /*
  11546. * Note that calling vmx_set_{efer,cr0,cr4} is important as they
  11547. * handle a variety of side effects to KVM's software model.
  11548. */
  11549. vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
  11550. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11551. vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
  11552. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11553. vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
  11554. nested_ept_uninit_mmu_context(vcpu);
  11555. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  11556. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  11557. /*
  11558. * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
  11559. * from vmcs01 (if necessary). The PDPTRs are not loaded on
  11560. * VMFail, like everything else we just need to ensure our
  11561. * software model is up-to-date.
  11562. */
  11563. ept_save_pdptrs(vcpu);
  11564. kvm_mmu_reset_context(vcpu);
  11565. if (cpu_has_vmx_msr_bitmap())
  11566. vmx_update_msr_bitmap(vcpu);
  11567. /*
  11568. * This nasty bit of open coding is a compromise between blindly
  11569. * loading L1's MSRs using the exit load lists (incorrect emulation
  11570. * of VMFail), leaving the nested VM's MSRs in the software model
  11571. * (incorrect behavior) and snapshotting the modified MSRs (too
  11572. * expensive since the lists are unbound by hardware). For each
  11573. * MSR that was (prematurely) loaded from the nested VMEntry load
  11574. * list, reload it from the exit load list if it exists and differs
  11575. * from the guest value. The intent is to stuff host state as
  11576. * silently as possible, not to fully process the exit load list.
  11577. */
  11578. msr.host_initiated = false;
  11579. for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
  11580. gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
  11581. if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
  11582. pr_debug_ratelimited(
  11583. "%s read MSR index failed (%u, 0x%08llx)\n",
  11584. __func__, i, gpa);
  11585. goto vmabort;
  11586. }
  11587. for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
  11588. gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
  11589. if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
  11590. pr_debug_ratelimited(
  11591. "%s read MSR failed (%u, 0x%08llx)\n",
  11592. __func__, j, gpa);
  11593. goto vmabort;
  11594. }
  11595. if (h.index != g.index)
  11596. continue;
  11597. if (h.value == g.value)
  11598. break;
  11599. if (nested_vmx_load_msr_check(vcpu, &h)) {
  11600. pr_debug_ratelimited(
  11601. "%s check failed (%u, 0x%x, 0x%x)\n",
  11602. __func__, j, h.index, h.reserved);
  11603. goto vmabort;
  11604. }
  11605. msr.index = h.index;
  11606. msr.data = h.value;
  11607. if (kvm_set_msr(vcpu, &msr)) {
  11608. pr_debug_ratelimited(
  11609. "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
  11610. __func__, j, h.index, h.value);
  11611. goto vmabort;
  11612. }
  11613. }
  11614. }
  11615. return;
  11616. vmabort:
  11617. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  11618. }
  11619. /*
  11620. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  11621. * and modify vmcs12 to make it see what it would expect to see there if
  11622. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  11623. */
  11624. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  11625. u32 exit_intr_info,
  11626. unsigned long exit_qualification)
  11627. {
  11628. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11629. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11630. /* trying to cancel vmlaunch/vmresume is a bug */
  11631. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  11632. /*
  11633. * The only expected VM-instruction error is "VM entry with
  11634. * invalid control field(s)." Anything else indicates a
  11635. * problem with L0.
  11636. */
  11637. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  11638. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  11639. leave_guest_mode(vcpu);
  11640. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11641. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  11642. if (likely(!vmx->fail)) {
  11643. if (exit_reason == -1)
  11644. sync_vmcs12(vcpu, vmcs12);
  11645. else
  11646. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  11647. exit_qualification);
  11648. /*
  11649. * Must happen outside of sync_vmcs12() as it will
  11650. * also be used to capture vmcs12 cache as part of
  11651. * capturing nVMX state for snapshot (migration).
  11652. *
  11653. * Otherwise, this flush will dirty guest memory at a
  11654. * point it is already assumed by user-space to be
  11655. * immutable.
  11656. */
  11657. nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
  11658. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  11659. vmcs12->vm_exit_msr_store_count))
  11660. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  11661. }
  11662. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11663. vm_entry_controls_reset_shadow(vmx);
  11664. vm_exit_controls_reset_shadow(vmx);
  11665. vmx_segment_cache_clear(vmx);
  11666. /* Update any VMCS fields that might have changed while L2 ran */
  11667. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11668. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11669. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  11670. if (kvm_has_tsc_control)
  11671. decache_tsc_multiplier(vmx);
  11672. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  11673. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  11674. vmx_set_virtual_apic_mode(vcpu);
  11675. } else if (!nested_cpu_has_ept(vmcs12) &&
  11676. nested_cpu_has2(vmcs12,
  11677. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  11678. vmx_flush_tlb(vcpu, true);
  11679. }
  11680. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  11681. vmx->host_rsp = 0;
  11682. /* Unpin physical memory we referred to in vmcs02 */
  11683. if (vmx->nested.apic_access_page) {
  11684. kvm_release_page_dirty(vmx->nested.apic_access_page);
  11685. vmx->nested.apic_access_page = NULL;
  11686. }
  11687. if (vmx->nested.virtual_apic_page) {
  11688. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  11689. vmx->nested.virtual_apic_page = NULL;
  11690. }
  11691. if (vmx->nested.pi_desc_page) {
  11692. kunmap(vmx->nested.pi_desc_page);
  11693. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  11694. vmx->nested.pi_desc_page = NULL;
  11695. vmx->nested.pi_desc = NULL;
  11696. }
  11697. /*
  11698. * We are now running in L2, mmu_notifier will force to reload the
  11699. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  11700. */
  11701. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  11702. if (enable_shadow_vmcs && exit_reason != -1)
  11703. vmx->nested.sync_shadow_vmcs = true;
  11704. /* in case we halted in L2 */
  11705. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  11706. if (likely(!vmx->fail)) {
  11707. if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  11708. nested_exit_intr_ack_set(vcpu)) {
  11709. int irq = kvm_cpu_get_interrupt(vcpu);
  11710. WARN_ON(irq < 0);
  11711. vmcs12->vm_exit_intr_info = irq |
  11712. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  11713. }
  11714. if (exit_reason != -1)
  11715. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  11716. vmcs12->exit_qualification,
  11717. vmcs12->idt_vectoring_info_field,
  11718. vmcs12->vm_exit_intr_info,
  11719. vmcs12->vm_exit_intr_error_code,
  11720. KVM_ISA_VMX);
  11721. load_vmcs12_host_state(vcpu, vmcs12);
  11722. return;
  11723. }
  11724. /*
  11725. * After an early L2 VM-entry failure, we're now back
  11726. * in L1 which thinks it just finished a VMLAUNCH or
  11727. * VMRESUME instruction, so we need to set the failure
  11728. * flag and the VM-instruction error field of the VMCS
  11729. * accordingly.
  11730. */
  11731. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11732. /*
  11733. * Restore L1's host state to KVM's software model. We're here
  11734. * because a consistency check was caught by hardware, which
  11735. * means some amount of guest state has been propagated to KVM's
  11736. * model and needs to be unwound to the host's state.
  11737. */
  11738. nested_vmx_restore_host_state(vcpu);
  11739. /*
  11740. * The emulated instruction was already skipped in
  11741. * nested_vmx_run, but the updated RIP was never
  11742. * written back to the vmcs01.
  11743. */
  11744. skip_emulated_instruction(vcpu);
  11745. vmx->fail = 0;
  11746. }
  11747. /*
  11748. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  11749. */
  11750. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  11751. {
  11752. if (is_guest_mode(vcpu)) {
  11753. to_vmx(vcpu)->nested.nested_run_pending = 0;
  11754. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11755. }
  11756. free_nested(to_vmx(vcpu));
  11757. }
  11758. /*
  11759. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  11760. * 23.7 "VM-entry failures during or after loading guest state" (this also
  11761. * lists the acceptable exit-reason and exit-qualification parameters).
  11762. * It should only be called before L2 actually succeeded to run, and when
  11763. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  11764. */
  11765. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  11766. struct vmcs12 *vmcs12,
  11767. u32 reason, unsigned long qualification)
  11768. {
  11769. load_vmcs12_host_state(vcpu, vmcs12);
  11770. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  11771. vmcs12->exit_qualification = qualification;
  11772. nested_vmx_succeed(vcpu);
  11773. if (enable_shadow_vmcs)
  11774. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  11775. }
  11776. static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
  11777. struct x86_instruction_info *info)
  11778. {
  11779. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11780. unsigned short port;
  11781. bool intercept;
  11782. int size;
  11783. if (info->intercept == x86_intercept_in ||
  11784. info->intercept == x86_intercept_ins) {
  11785. port = info->src_val;
  11786. size = info->dst_bytes;
  11787. } else {
  11788. port = info->dst_val;
  11789. size = info->src_bytes;
  11790. }
  11791. /*
  11792. * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
  11793. * VM-exits depend on the 'unconditional IO exiting' VM-execution
  11794. * control.
  11795. *
  11796. * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
  11797. */
  11798. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  11799. intercept = nested_cpu_has(vmcs12,
  11800. CPU_BASED_UNCOND_IO_EXITING);
  11801. else
  11802. intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
  11803. /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
  11804. return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
  11805. }
  11806. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  11807. struct x86_instruction_info *info,
  11808. enum x86_intercept_stage stage)
  11809. {
  11810. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11811. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  11812. switch (info->intercept) {
  11813. /*
  11814. * RDPID causes #UD if disabled through secondary execution controls.
  11815. * Because it is marked as EmulateOnUD, we need to intercept it here.
  11816. */
  11817. case x86_intercept_rdtscp:
  11818. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  11819. ctxt->exception.vector = UD_VECTOR;
  11820. ctxt->exception.error_code_valid = false;
  11821. return X86EMUL_PROPAGATE_FAULT;
  11822. }
  11823. break;
  11824. case x86_intercept_in:
  11825. case x86_intercept_ins:
  11826. case x86_intercept_out:
  11827. case x86_intercept_outs:
  11828. return vmx_check_intercept_io(vcpu, info);
  11829. case x86_intercept_lgdt:
  11830. case x86_intercept_lidt:
  11831. case x86_intercept_lldt:
  11832. case x86_intercept_ltr:
  11833. case x86_intercept_sgdt:
  11834. case x86_intercept_sidt:
  11835. case x86_intercept_sldt:
  11836. case x86_intercept_str:
  11837. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
  11838. return X86EMUL_CONTINUE;
  11839. /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
  11840. break;
  11841. /* TODO: check more intercepts... */
  11842. default:
  11843. break;
  11844. }
  11845. return X86EMUL_UNHANDLEABLE;
  11846. }
  11847. #ifdef CONFIG_X86_64
  11848. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  11849. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  11850. u64 divisor, u64 *result)
  11851. {
  11852. u64 low = a << shift, high = a >> (64 - shift);
  11853. /* To avoid the overflow on divq */
  11854. if (high >= divisor)
  11855. return 1;
  11856. /* Low hold the result, high hold rem which is discarded */
  11857. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  11858. "rm" (divisor), "0" (low), "1" (high));
  11859. *result = low;
  11860. return 0;
  11861. }
  11862. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  11863. {
  11864. struct vcpu_vmx *vmx;
  11865. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  11866. if (kvm_mwait_in_guest(vcpu->kvm))
  11867. return -EOPNOTSUPP;
  11868. vmx = to_vmx(vcpu);
  11869. tscl = rdtsc();
  11870. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  11871. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  11872. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  11873. if (delta_tsc > lapic_timer_advance_cycles)
  11874. delta_tsc -= lapic_timer_advance_cycles;
  11875. else
  11876. delta_tsc = 0;
  11877. /* Convert to host delta tsc if tsc scaling is enabled */
  11878. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  11879. u64_shl_div_u64(delta_tsc,
  11880. kvm_tsc_scaling_ratio_frac_bits,
  11881. vcpu->arch.tsc_scaling_ratio,
  11882. &delta_tsc))
  11883. return -ERANGE;
  11884. /*
  11885. * If the delta tsc can't fit in the 32 bit after the multi shift,
  11886. * we can't use the preemption timer.
  11887. * It's possible that it fits on later vmentries, but checking
  11888. * on every vmentry is costly so we just use an hrtimer.
  11889. */
  11890. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  11891. return -ERANGE;
  11892. vmx->hv_deadline_tsc = tscl + delta_tsc;
  11893. return delta_tsc == 0;
  11894. }
  11895. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  11896. {
  11897. to_vmx(vcpu)->hv_deadline_tsc = -1;
  11898. }
  11899. #endif
  11900. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  11901. {
  11902. if (!kvm_pause_in_guest(vcpu->kvm))
  11903. shrink_ple_window(vcpu);
  11904. }
  11905. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  11906. struct kvm_memory_slot *slot)
  11907. {
  11908. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  11909. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  11910. }
  11911. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  11912. struct kvm_memory_slot *slot)
  11913. {
  11914. kvm_mmu_slot_set_dirty(kvm, slot);
  11915. }
  11916. static void vmx_flush_log_dirty(struct kvm *kvm)
  11917. {
  11918. kvm_flush_pml_buffers(kvm);
  11919. }
  11920. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
  11921. {
  11922. struct vmcs12 *vmcs12;
  11923. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11924. struct page *page = NULL;
  11925. u64 *pml_address;
  11926. if (is_guest_mode(vcpu)) {
  11927. WARN_ON_ONCE(vmx->nested.pml_full);
  11928. /*
  11929. * Check if PML is enabled for the nested guest.
  11930. * Whether eptp bit 6 is set is already checked
  11931. * as part of A/D emulation.
  11932. */
  11933. vmcs12 = get_vmcs12(vcpu);
  11934. if (!nested_cpu_has_pml(vmcs12))
  11935. return 0;
  11936. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  11937. vmx->nested.pml_full = true;
  11938. return 1;
  11939. }
  11940. gpa &= ~0xFFFull;
  11941. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  11942. if (is_error_page(page))
  11943. return 0;
  11944. pml_address = kmap(page);
  11945. pml_address[vmcs12->guest_pml_index--] = gpa;
  11946. kunmap(page);
  11947. kvm_release_page_clean(page);
  11948. }
  11949. return 0;
  11950. }
  11951. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  11952. struct kvm_memory_slot *memslot,
  11953. gfn_t offset, unsigned long mask)
  11954. {
  11955. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  11956. }
  11957. static void __pi_post_block(struct kvm_vcpu *vcpu)
  11958. {
  11959. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11960. struct pi_desc old, new;
  11961. unsigned int dest;
  11962. do {
  11963. old.control = new.control = pi_desc->control;
  11964. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  11965. "Wakeup handler not enabled while the VCPU is blocked\n");
  11966. dest = cpu_physical_id(vcpu->cpu);
  11967. if (x2apic_enabled())
  11968. new.ndst = dest;
  11969. else
  11970. new.ndst = (dest << 8) & 0xFF00;
  11971. /* set 'NV' to 'notification vector' */
  11972. new.nv = POSTED_INTR_VECTOR;
  11973. } while (cmpxchg64(&pi_desc->control, old.control,
  11974. new.control) != old.control);
  11975. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  11976. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11977. list_del(&vcpu->blocked_vcpu_list);
  11978. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11979. vcpu->pre_pcpu = -1;
  11980. }
  11981. }
  11982. /*
  11983. * This routine does the following things for vCPU which is going
  11984. * to be blocked if VT-d PI is enabled.
  11985. * - Store the vCPU to the wakeup list, so when interrupts happen
  11986. * we can find the right vCPU to wake up.
  11987. * - Change the Posted-interrupt descriptor as below:
  11988. * 'NDST' <-- vcpu->pre_pcpu
  11989. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  11990. * - If 'ON' is set during this process, which means at least one
  11991. * interrupt is posted for this vCPU, we cannot block it, in
  11992. * this case, return 1, otherwise, return 0.
  11993. *
  11994. */
  11995. static int pi_pre_block(struct kvm_vcpu *vcpu)
  11996. {
  11997. unsigned int dest;
  11998. struct pi_desc old, new;
  11999. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  12000. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  12001. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12002. !kvm_vcpu_apicv_active(vcpu))
  12003. return 0;
  12004. WARN_ON(irqs_disabled());
  12005. local_irq_disable();
  12006. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  12007. vcpu->pre_pcpu = vcpu->cpu;
  12008. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12009. list_add_tail(&vcpu->blocked_vcpu_list,
  12010. &per_cpu(blocked_vcpu_on_cpu,
  12011. vcpu->pre_pcpu));
  12012. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12013. }
  12014. do {
  12015. old.control = new.control = pi_desc->control;
  12016. WARN((pi_desc->sn == 1),
  12017. "Warning: SN field of posted-interrupts "
  12018. "is set before blocking\n");
  12019. /*
  12020. * Since vCPU can be preempted during this process,
  12021. * vcpu->cpu could be different with pre_pcpu, we
  12022. * need to set pre_pcpu as the destination of wakeup
  12023. * notification event, then we can find the right vCPU
  12024. * to wakeup in wakeup handler if interrupts happen
  12025. * when the vCPU is in blocked state.
  12026. */
  12027. dest = cpu_physical_id(vcpu->pre_pcpu);
  12028. if (x2apic_enabled())
  12029. new.ndst = dest;
  12030. else
  12031. new.ndst = (dest << 8) & 0xFF00;
  12032. /* set 'NV' to 'wakeup vector' */
  12033. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  12034. } while (cmpxchg64(&pi_desc->control, old.control,
  12035. new.control) != old.control);
  12036. /* We should not block the vCPU if an interrupt is posted for it. */
  12037. if (pi_test_on(pi_desc) == 1)
  12038. __pi_post_block(vcpu);
  12039. local_irq_enable();
  12040. return (vcpu->pre_pcpu == -1);
  12041. }
  12042. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  12043. {
  12044. if (pi_pre_block(vcpu))
  12045. return 1;
  12046. if (kvm_lapic_hv_timer_in_use(vcpu))
  12047. kvm_lapic_switch_to_sw_timer(vcpu);
  12048. return 0;
  12049. }
  12050. static void pi_post_block(struct kvm_vcpu *vcpu)
  12051. {
  12052. if (vcpu->pre_pcpu == -1)
  12053. return;
  12054. WARN_ON(irqs_disabled());
  12055. local_irq_disable();
  12056. __pi_post_block(vcpu);
  12057. local_irq_enable();
  12058. }
  12059. static void vmx_post_block(struct kvm_vcpu *vcpu)
  12060. {
  12061. if (kvm_x86_ops->set_hv_timer)
  12062. kvm_lapic_switch_to_hv_timer(vcpu);
  12063. pi_post_block(vcpu);
  12064. }
  12065. /*
  12066. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  12067. *
  12068. * @kvm: kvm
  12069. * @host_irq: host irq of the interrupt
  12070. * @guest_irq: gsi of the interrupt
  12071. * @set: set or unset PI
  12072. * returns 0 on success, < 0 on failure
  12073. */
  12074. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  12075. uint32_t guest_irq, bool set)
  12076. {
  12077. struct kvm_kernel_irq_routing_entry *e;
  12078. struct kvm_irq_routing_table *irq_rt;
  12079. struct kvm_lapic_irq irq;
  12080. struct kvm_vcpu *vcpu;
  12081. struct vcpu_data vcpu_info;
  12082. int idx, ret = 0;
  12083. if (!kvm_arch_has_assigned_device(kvm) ||
  12084. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12085. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  12086. return 0;
  12087. idx = srcu_read_lock(&kvm->irq_srcu);
  12088. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  12089. if (guest_irq >= irq_rt->nr_rt_entries ||
  12090. hlist_empty(&irq_rt->map[guest_irq])) {
  12091. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  12092. guest_irq, irq_rt->nr_rt_entries);
  12093. goto out;
  12094. }
  12095. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  12096. if (e->type != KVM_IRQ_ROUTING_MSI)
  12097. continue;
  12098. /*
  12099. * VT-d PI cannot support posting multicast/broadcast
  12100. * interrupts to a vCPU, we still use interrupt remapping
  12101. * for these kind of interrupts.
  12102. *
  12103. * For lowest-priority interrupts, we only support
  12104. * those with single CPU as the destination, e.g. user
  12105. * configures the interrupts via /proc/irq or uses
  12106. * irqbalance to make the interrupts single-CPU.
  12107. *
  12108. * We will support full lowest-priority interrupt later.
  12109. */
  12110. kvm_set_msi_irq(kvm, e, &irq);
  12111. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  12112. /*
  12113. * Make sure the IRTE is in remapped mode if
  12114. * we don't handle it in posted mode.
  12115. */
  12116. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12117. if (ret < 0) {
  12118. printk(KERN_INFO
  12119. "failed to back to remapped mode, irq: %u\n",
  12120. host_irq);
  12121. goto out;
  12122. }
  12123. continue;
  12124. }
  12125. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  12126. vcpu_info.vector = irq.vector;
  12127. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  12128. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  12129. if (set)
  12130. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  12131. else
  12132. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12133. if (ret < 0) {
  12134. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  12135. __func__);
  12136. goto out;
  12137. }
  12138. }
  12139. ret = 0;
  12140. out:
  12141. srcu_read_unlock(&kvm->irq_srcu, idx);
  12142. return ret;
  12143. }
  12144. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  12145. {
  12146. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  12147. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  12148. FEATURE_CONTROL_LMCE;
  12149. else
  12150. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  12151. ~FEATURE_CONTROL_LMCE;
  12152. }
  12153. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  12154. {
  12155. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  12156. if (to_vmx(vcpu)->nested.nested_run_pending)
  12157. return 0;
  12158. return 1;
  12159. }
  12160. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  12161. {
  12162. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12163. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  12164. if (vmx->nested.smm.guest_mode)
  12165. nested_vmx_vmexit(vcpu, -1, 0, 0);
  12166. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  12167. vmx->nested.vmxon = false;
  12168. vmx_clear_hlt(vcpu);
  12169. return 0;
  12170. }
  12171. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  12172. {
  12173. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12174. int ret;
  12175. if (vmx->nested.smm.vmxon) {
  12176. vmx->nested.vmxon = true;
  12177. vmx->nested.smm.vmxon = false;
  12178. }
  12179. if (vmx->nested.smm.guest_mode) {
  12180. vcpu->arch.hflags &= ~HF_SMM_MASK;
  12181. ret = enter_vmx_non_root_mode(vcpu, NULL);
  12182. vcpu->arch.hflags |= HF_SMM_MASK;
  12183. if (ret)
  12184. return ret;
  12185. vmx->nested.smm.guest_mode = false;
  12186. }
  12187. return 0;
  12188. }
  12189. static int enable_smi_window(struct kvm_vcpu *vcpu)
  12190. {
  12191. return 0;
  12192. }
  12193. static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
  12194. struct kvm_nested_state __user *user_kvm_nested_state,
  12195. u32 user_data_size)
  12196. {
  12197. struct vcpu_vmx *vmx;
  12198. struct vmcs12 *vmcs12;
  12199. struct kvm_nested_state kvm_state = {
  12200. .flags = 0,
  12201. .format = 0,
  12202. .size = sizeof(kvm_state),
  12203. .vmx.vmxon_pa = -1ull,
  12204. .vmx.vmcs_pa = -1ull,
  12205. };
  12206. if (!vcpu)
  12207. return kvm_state.size + 2 * VMCS12_SIZE;
  12208. vmx = to_vmx(vcpu);
  12209. vmcs12 = get_vmcs12(vcpu);
  12210. if (nested_vmx_allowed(vcpu) &&
  12211. (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
  12212. kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
  12213. kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
  12214. if (vmx->nested.current_vmptr != -1ull) {
  12215. kvm_state.size += VMCS12_SIZE;
  12216. if (is_guest_mode(vcpu) &&
  12217. nested_cpu_has_shadow_vmcs(vmcs12) &&
  12218. vmcs12->vmcs_link_pointer != -1ull)
  12219. kvm_state.size += VMCS12_SIZE;
  12220. }
  12221. if (vmx->nested.smm.vmxon)
  12222. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
  12223. if (vmx->nested.smm.guest_mode)
  12224. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
  12225. if (is_guest_mode(vcpu)) {
  12226. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  12227. if (vmx->nested.nested_run_pending)
  12228. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  12229. }
  12230. }
  12231. if (user_data_size < kvm_state.size)
  12232. goto out;
  12233. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  12234. return -EFAULT;
  12235. if (vmx->nested.current_vmptr == -1ull)
  12236. goto out;
  12237. /*
  12238. * When running L2, the authoritative vmcs12 state is in the
  12239. * vmcs02. When running L1, the authoritative vmcs12 state is
  12240. * in the shadow vmcs linked to vmcs01, unless
  12241. * sync_shadow_vmcs is set, in which case, the authoritative
  12242. * vmcs12 state is in the vmcs12 already.
  12243. */
  12244. if (is_guest_mode(vcpu))
  12245. sync_vmcs12(vcpu, vmcs12);
  12246. else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
  12247. copy_shadow_to_vmcs12(vmx);
  12248. /*
  12249. * Copy over the full allocated size of vmcs12 rather than just the size
  12250. * of the struct.
  12251. */
  12252. if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
  12253. return -EFAULT;
  12254. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12255. vmcs12->vmcs_link_pointer != -1ull) {
  12256. if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
  12257. get_shadow_vmcs12(vcpu), VMCS12_SIZE))
  12258. return -EFAULT;
  12259. }
  12260. out:
  12261. return kvm_state.size;
  12262. }
  12263. static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  12264. struct kvm_nested_state __user *user_kvm_nested_state,
  12265. struct kvm_nested_state *kvm_state)
  12266. {
  12267. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12268. struct vmcs12 *vmcs12;
  12269. u32 exit_qual;
  12270. int ret;
  12271. if (kvm_state->format != 0)
  12272. return -EINVAL;
  12273. if (!nested_vmx_allowed(vcpu))
  12274. return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
  12275. if (kvm_state->vmx.vmxon_pa == -1ull) {
  12276. if (kvm_state->vmx.smm.flags)
  12277. return -EINVAL;
  12278. if (kvm_state->vmx.vmcs_pa != -1ull)
  12279. return -EINVAL;
  12280. vmx_leave_nested(vcpu);
  12281. return 0;
  12282. }
  12283. if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
  12284. return -EINVAL;
  12285. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12286. (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12287. return -EINVAL;
  12288. if (kvm_state->vmx.smm.flags &
  12289. ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
  12290. return -EINVAL;
  12291. /*
  12292. * SMM temporarily disables VMX, so we cannot be in guest mode,
  12293. * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
  12294. * must be zero.
  12295. */
  12296. if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
  12297. return -EINVAL;
  12298. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12299. !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
  12300. return -EINVAL;
  12301. vmx_leave_nested(vcpu);
  12302. if (kvm_state->vmx.vmxon_pa == -1ull)
  12303. return 0;
  12304. vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
  12305. ret = enter_vmx_operation(vcpu);
  12306. if (ret)
  12307. return ret;
  12308. /* Empty 'VMXON' state is permitted */
  12309. if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
  12310. return 0;
  12311. if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
  12312. !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
  12313. return -EINVAL;
  12314. set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
  12315. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
  12316. vmx->nested.smm.vmxon = true;
  12317. vmx->nested.vmxon = false;
  12318. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
  12319. vmx->nested.smm.guest_mode = true;
  12320. }
  12321. vmcs12 = get_vmcs12(vcpu);
  12322. if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
  12323. return -EFAULT;
  12324. if (vmcs12->hdr.revision_id != VMCS12_REVISION)
  12325. return -EINVAL;
  12326. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12327. return 0;
  12328. vmx->nested.nested_run_pending =
  12329. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  12330. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12331. vmcs12->vmcs_link_pointer != -1ull) {
  12332. struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
  12333. if (kvm_state->size < sizeof(*kvm_state) + 2 * sizeof(*vmcs12))
  12334. return -EINVAL;
  12335. if (copy_from_user(shadow_vmcs12,
  12336. user_kvm_nested_state->data + VMCS12_SIZE,
  12337. sizeof(*vmcs12)))
  12338. return -EFAULT;
  12339. if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  12340. !shadow_vmcs12->hdr.shadow_vmcs)
  12341. return -EINVAL;
  12342. }
  12343. if (check_vmentry_prereqs(vcpu, vmcs12) ||
  12344. check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  12345. return -EINVAL;
  12346. vmx->nested.dirty_vmcs12 = true;
  12347. ret = enter_vmx_non_root_mode(vcpu, NULL);
  12348. if (ret)
  12349. return -EINVAL;
  12350. return 0;
  12351. }
  12352. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  12353. .cpu_has_kvm_support = cpu_has_kvm_support,
  12354. .disabled_by_bios = vmx_disabled_by_bios,
  12355. .hardware_setup = hardware_setup,
  12356. .hardware_unsetup = hardware_unsetup,
  12357. .check_processor_compatibility = vmx_check_processor_compat,
  12358. .hardware_enable = hardware_enable,
  12359. .hardware_disable = hardware_disable,
  12360. .cpu_has_accelerated_tpr = report_flexpriority,
  12361. .has_emulated_msr = vmx_has_emulated_msr,
  12362. .vm_init = vmx_vm_init,
  12363. .vm_alloc = vmx_vm_alloc,
  12364. .vm_free = vmx_vm_free,
  12365. .vcpu_create = vmx_create_vcpu,
  12366. .vcpu_free = vmx_free_vcpu,
  12367. .vcpu_reset = vmx_vcpu_reset,
  12368. .prepare_guest_switch = vmx_prepare_switch_to_guest,
  12369. .vcpu_load = vmx_vcpu_load,
  12370. .vcpu_put = vmx_vcpu_put,
  12371. .update_bp_intercept = update_exception_bitmap,
  12372. .get_msr_feature = vmx_get_msr_feature,
  12373. .get_msr = vmx_get_msr,
  12374. .set_msr = vmx_set_msr,
  12375. .get_segment_base = vmx_get_segment_base,
  12376. .get_segment = vmx_get_segment,
  12377. .set_segment = vmx_set_segment,
  12378. .get_cpl = vmx_get_cpl,
  12379. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  12380. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  12381. .decache_cr3 = vmx_decache_cr3,
  12382. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  12383. .set_cr0 = vmx_set_cr0,
  12384. .set_cr3 = vmx_set_cr3,
  12385. .set_cr4 = vmx_set_cr4,
  12386. .set_efer = vmx_set_efer,
  12387. .get_idt = vmx_get_idt,
  12388. .set_idt = vmx_set_idt,
  12389. .get_gdt = vmx_get_gdt,
  12390. .set_gdt = vmx_set_gdt,
  12391. .get_dr6 = vmx_get_dr6,
  12392. .set_dr6 = vmx_set_dr6,
  12393. .set_dr7 = vmx_set_dr7,
  12394. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  12395. .cache_reg = vmx_cache_reg,
  12396. .get_rflags = vmx_get_rflags,
  12397. .set_rflags = vmx_set_rflags,
  12398. .tlb_flush = vmx_flush_tlb,
  12399. .tlb_flush_gva = vmx_flush_tlb_gva,
  12400. .run = vmx_vcpu_run,
  12401. .handle_exit = vmx_handle_exit,
  12402. .skip_emulated_instruction = skip_emulated_instruction,
  12403. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  12404. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  12405. .patch_hypercall = vmx_patch_hypercall,
  12406. .set_irq = vmx_inject_irq,
  12407. .set_nmi = vmx_inject_nmi,
  12408. .queue_exception = vmx_queue_exception,
  12409. .cancel_injection = vmx_cancel_injection,
  12410. .interrupt_allowed = vmx_interrupt_allowed,
  12411. .nmi_allowed = vmx_nmi_allowed,
  12412. .get_nmi_mask = vmx_get_nmi_mask,
  12413. .set_nmi_mask = vmx_set_nmi_mask,
  12414. .enable_nmi_window = enable_nmi_window,
  12415. .enable_irq_window = enable_irq_window,
  12416. .update_cr8_intercept = update_cr8_intercept,
  12417. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  12418. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  12419. .get_enable_apicv = vmx_get_enable_apicv,
  12420. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  12421. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  12422. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  12423. .hwapic_irr_update = vmx_hwapic_irr_update,
  12424. .hwapic_isr_update = vmx_hwapic_isr_update,
  12425. .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
  12426. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  12427. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  12428. .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
  12429. .set_tss_addr = vmx_set_tss_addr,
  12430. .set_identity_map_addr = vmx_set_identity_map_addr,
  12431. .get_tdp_level = get_ept_level,
  12432. .get_mt_mask = vmx_get_mt_mask,
  12433. .get_exit_info = vmx_get_exit_info,
  12434. .get_lpage_level = vmx_get_lpage_level,
  12435. .cpuid_update = vmx_cpuid_update,
  12436. .rdtscp_supported = vmx_rdtscp_supported,
  12437. .invpcid_supported = vmx_invpcid_supported,
  12438. .set_supported_cpuid = vmx_set_supported_cpuid,
  12439. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  12440. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  12441. .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
  12442. .set_tdp_cr3 = vmx_set_cr3,
  12443. .check_intercept = vmx_check_intercept,
  12444. .handle_external_intr = vmx_handle_external_intr,
  12445. .mpx_supported = vmx_mpx_supported,
  12446. .xsaves_supported = vmx_xsaves_supported,
  12447. .umip_emulated = vmx_umip_emulated,
  12448. .check_nested_events = vmx_check_nested_events,
  12449. .request_immediate_exit = vmx_request_immediate_exit,
  12450. .sched_in = vmx_sched_in,
  12451. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  12452. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  12453. .flush_log_dirty = vmx_flush_log_dirty,
  12454. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  12455. .write_log_dirty = vmx_write_pml_buffer,
  12456. .pre_block = vmx_pre_block,
  12457. .post_block = vmx_post_block,
  12458. .pmu_ops = &intel_pmu_ops,
  12459. .update_pi_irte = vmx_update_pi_irte,
  12460. #ifdef CONFIG_X86_64
  12461. .set_hv_timer = vmx_set_hv_timer,
  12462. .cancel_hv_timer = vmx_cancel_hv_timer,
  12463. #endif
  12464. .setup_mce = vmx_setup_mce,
  12465. .get_nested_state = vmx_get_nested_state,
  12466. .set_nested_state = vmx_set_nested_state,
  12467. .get_vmcs12_pages = nested_get_vmcs12_pages,
  12468. .smi_allowed = vmx_smi_allowed,
  12469. .pre_enter_smm = vmx_pre_enter_smm,
  12470. .pre_leave_smm = vmx_pre_leave_smm,
  12471. .enable_smi_window = enable_smi_window,
  12472. };
  12473. static void vmx_cleanup_l1d_flush(void)
  12474. {
  12475. if (vmx_l1d_flush_pages) {
  12476. free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
  12477. vmx_l1d_flush_pages = NULL;
  12478. }
  12479. /* Restore state so sysfs ignores VMX */
  12480. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  12481. }
  12482. static void vmx_exit(void)
  12483. {
  12484. #ifdef CONFIG_KEXEC_CORE
  12485. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  12486. synchronize_rcu();
  12487. #endif
  12488. kvm_exit();
  12489. #if IS_ENABLED(CONFIG_HYPERV)
  12490. if (static_branch_unlikely(&enable_evmcs)) {
  12491. int cpu;
  12492. struct hv_vp_assist_page *vp_ap;
  12493. /*
  12494. * Reset everything to support using non-enlightened VMCS
  12495. * access later (e.g. when we reload the module with
  12496. * enlightened_vmcs=0)
  12497. */
  12498. for_each_online_cpu(cpu) {
  12499. vp_ap = hv_get_vp_assist_page(cpu);
  12500. if (!vp_ap)
  12501. continue;
  12502. vp_ap->current_nested_vmcs = 0;
  12503. vp_ap->enlighten_vmentry = 0;
  12504. }
  12505. static_branch_disable(&enable_evmcs);
  12506. }
  12507. #endif
  12508. vmx_cleanup_l1d_flush();
  12509. }
  12510. module_exit(vmx_exit);
  12511. static int __init vmx_init(void)
  12512. {
  12513. int r, cpu;
  12514. #if IS_ENABLED(CONFIG_HYPERV)
  12515. /*
  12516. * Enlightened VMCS usage should be recommended and the host needs
  12517. * to support eVMCS v1 or above. We can also disable eVMCS support
  12518. * with module parameter.
  12519. */
  12520. if (enlightened_vmcs &&
  12521. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  12522. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  12523. KVM_EVMCS_VERSION) {
  12524. int cpu;
  12525. /* Check that we have assist pages on all online CPUs */
  12526. for_each_online_cpu(cpu) {
  12527. if (!hv_get_vp_assist_page(cpu)) {
  12528. enlightened_vmcs = false;
  12529. break;
  12530. }
  12531. }
  12532. if (enlightened_vmcs) {
  12533. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  12534. static_branch_enable(&enable_evmcs);
  12535. }
  12536. } else {
  12537. enlightened_vmcs = false;
  12538. }
  12539. #endif
  12540. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  12541. __alignof__(struct vcpu_vmx), THIS_MODULE);
  12542. if (r)
  12543. return r;
  12544. /*
  12545. * Must be called after kvm_init() so enable_ept is properly set
  12546. * up. Hand the parameter mitigation value in which was stored in
  12547. * the pre module init parser. If no parameter was given, it will
  12548. * contain 'auto' which will be turned into the default 'cond'
  12549. * mitigation mode.
  12550. */
  12551. if (boot_cpu_has(X86_BUG_L1TF)) {
  12552. r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
  12553. if (r) {
  12554. vmx_exit();
  12555. return r;
  12556. }
  12557. }
  12558. for_each_possible_cpu(cpu) {
  12559. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  12560. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  12561. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  12562. }
  12563. #ifdef CONFIG_KEXEC_CORE
  12564. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  12565. crash_vmclear_local_loaded_vmcss);
  12566. #endif
  12567. vmx_check_vmcs12_offsets();
  12568. return 0;
  12569. }
  12570. module_init(vmx_init);