vmx.c 309 KB

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