vmx.c 341 KB

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