vmx.c 322 KB

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