vmx.c 329 KB

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