vmx.c 312 KB

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