vmx.c 334 KB

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