vmx.c 331 KB

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