vmx.c 324 KB

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