vmx.c 352 KB

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