vmx.c 210 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407
  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 <linux/kvm_host.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/ftrace_event.h>
  30. #include <linux/slab.h>
  31. #include <linux/tboot.h>
  32. #include "kvm_cache_regs.h"
  33. #include "x86.h"
  34. #include <asm/io.h>
  35. #include <asm/desc.h>
  36. #include <asm/vmx.h>
  37. #include <asm/virtext.h>
  38. #include <asm/mce.h>
  39. #include <asm/i387.h>
  40. #include <asm/xcr.h>
  41. #include <asm/perf_event.h>
  42. #include "trace.h"
  43. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  44. #define __ex_clear(x, reg) \
  45. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  46. MODULE_AUTHOR("Qumranet");
  47. MODULE_LICENSE("GPL");
  48. static const struct x86_cpu_id vmx_cpu_id[] = {
  49. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  50. {}
  51. };
  52. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  53. static bool __read_mostly enable_vpid = 1;
  54. module_param_named(vpid, enable_vpid, bool, 0444);
  55. static bool __read_mostly flexpriority_enabled = 1;
  56. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  57. static bool __read_mostly enable_ept = 1;
  58. module_param_named(ept, enable_ept, bool, S_IRUGO);
  59. static bool __read_mostly enable_unrestricted_guest = 1;
  60. module_param_named(unrestricted_guest,
  61. enable_unrestricted_guest, bool, S_IRUGO);
  62. static bool __read_mostly enable_ept_ad_bits = 1;
  63. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  64. static bool __read_mostly emulate_invalid_guest_state = true;
  65. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  66. static bool __read_mostly vmm_exclusive = 1;
  67. module_param(vmm_exclusive, bool, S_IRUGO);
  68. static bool __read_mostly fasteoi = 1;
  69. module_param(fasteoi, bool, S_IRUGO);
  70. /*
  71. * If nested=1, nested virtualization is supported, i.e., guests may use
  72. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  73. * use VMX instructions.
  74. */
  75. static bool __read_mostly nested = 0;
  76. module_param(nested, bool, S_IRUGO);
  77. #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
  78. (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
  79. #define KVM_GUEST_CR0_MASK \
  80. (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  81. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
  82. (X86_CR0_WP | X86_CR0_NE)
  83. #define KVM_VM_CR0_ALWAYS_ON \
  84. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  85. #define KVM_CR4_GUEST_OWNED_BITS \
  86. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  87. | X86_CR4_OSXMMEXCPT)
  88. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  89. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  90. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  91. /*
  92. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  93. * ple_gap: upper bound on the amount of time between two successive
  94. * executions of PAUSE in a loop. Also indicate if ple enabled.
  95. * According to test, this time is usually smaller than 128 cycles.
  96. * ple_window: upper bound on the amount of time a guest is allowed to execute
  97. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  98. * less than 2^12 cycles
  99. * Time is measured based on a counter that runs at the same rate as the TSC,
  100. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  101. */
  102. #define KVM_VMX_DEFAULT_PLE_GAP 128
  103. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  104. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  105. module_param(ple_gap, int, S_IRUGO);
  106. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  107. module_param(ple_window, int, S_IRUGO);
  108. #define NR_AUTOLOAD_MSRS 8
  109. #define VMCS02_POOL_SIZE 1
  110. struct vmcs {
  111. u32 revision_id;
  112. u32 abort;
  113. char data[0];
  114. };
  115. /*
  116. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  117. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  118. * loaded on this CPU (so we can clear them if the CPU goes down).
  119. */
  120. struct loaded_vmcs {
  121. struct vmcs *vmcs;
  122. int cpu;
  123. int launched;
  124. struct list_head loaded_vmcss_on_cpu_link;
  125. };
  126. struct shared_msr_entry {
  127. unsigned index;
  128. u64 data;
  129. u64 mask;
  130. };
  131. /*
  132. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  133. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  134. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  135. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  136. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  137. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  138. * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
  139. * underlying hardware which will be used to run L2.
  140. * This structure is packed to ensure that its layout is identical across
  141. * machines (necessary for live migration).
  142. * If there are changes in this struct, VMCS12_REVISION must be changed.
  143. */
  144. typedef u64 natural_width;
  145. struct __packed vmcs12 {
  146. /* According to the Intel spec, a VMCS region must start with the
  147. * following two fields. Then follow implementation-specific data.
  148. */
  149. u32 revision_id;
  150. u32 abort;
  151. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  152. u32 padding[7]; /* room for future expansion */
  153. u64 io_bitmap_a;
  154. u64 io_bitmap_b;
  155. u64 msr_bitmap;
  156. u64 vm_exit_msr_store_addr;
  157. u64 vm_exit_msr_load_addr;
  158. u64 vm_entry_msr_load_addr;
  159. u64 tsc_offset;
  160. u64 virtual_apic_page_addr;
  161. u64 apic_access_addr;
  162. u64 ept_pointer;
  163. u64 guest_physical_address;
  164. u64 vmcs_link_pointer;
  165. u64 guest_ia32_debugctl;
  166. u64 guest_ia32_pat;
  167. u64 guest_ia32_efer;
  168. u64 guest_ia32_perf_global_ctrl;
  169. u64 guest_pdptr0;
  170. u64 guest_pdptr1;
  171. u64 guest_pdptr2;
  172. u64 guest_pdptr3;
  173. u64 host_ia32_pat;
  174. u64 host_ia32_efer;
  175. u64 host_ia32_perf_global_ctrl;
  176. u64 padding64[8]; /* room for future expansion */
  177. /*
  178. * To allow migration of L1 (complete with its L2 guests) between
  179. * machines of different natural widths (32 or 64 bit), we cannot have
  180. * unsigned long fields with no explict size. We use u64 (aliased
  181. * natural_width) instead. Luckily, x86 is little-endian.
  182. */
  183. natural_width cr0_guest_host_mask;
  184. natural_width cr4_guest_host_mask;
  185. natural_width cr0_read_shadow;
  186. natural_width cr4_read_shadow;
  187. natural_width cr3_target_value0;
  188. natural_width cr3_target_value1;
  189. natural_width cr3_target_value2;
  190. natural_width cr3_target_value3;
  191. natural_width exit_qualification;
  192. natural_width guest_linear_address;
  193. natural_width guest_cr0;
  194. natural_width guest_cr3;
  195. natural_width guest_cr4;
  196. natural_width guest_es_base;
  197. natural_width guest_cs_base;
  198. natural_width guest_ss_base;
  199. natural_width guest_ds_base;
  200. natural_width guest_fs_base;
  201. natural_width guest_gs_base;
  202. natural_width guest_ldtr_base;
  203. natural_width guest_tr_base;
  204. natural_width guest_gdtr_base;
  205. natural_width guest_idtr_base;
  206. natural_width guest_dr7;
  207. natural_width guest_rsp;
  208. natural_width guest_rip;
  209. natural_width guest_rflags;
  210. natural_width guest_pending_dbg_exceptions;
  211. natural_width guest_sysenter_esp;
  212. natural_width guest_sysenter_eip;
  213. natural_width host_cr0;
  214. natural_width host_cr3;
  215. natural_width host_cr4;
  216. natural_width host_fs_base;
  217. natural_width host_gs_base;
  218. natural_width host_tr_base;
  219. natural_width host_gdtr_base;
  220. natural_width host_idtr_base;
  221. natural_width host_ia32_sysenter_esp;
  222. natural_width host_ia32_sysenter_eip;
  223. natural_width host_rsp;
  224. natural_width host_rip;
  225. natural_width paddingl[8]; /* room for future expansion */
  226. u32 pin_based_vm_exec_control;
  227. u32 cpu_based_vm_exec_control;
  228. u32 exception_bitmap;
  229. u32 page_fault_error_code_mask;
  230. u32 page_fault_error_code_match;
  231. u32 cr3_target_count;
  232. u32 vm_exit_controls;
  233. u32 vm_exit_msr_store_count;
  234. u32 vm_exit_msr_load_count;
  235. u32 vm_entry_controls;
  236. u32 vm_entry_msr_load_count;
  237. u32 vm_entry_intr_info_field;
  238. u32 vm_entry_exception_error_code;
  239. u32 vm_entry_instruction_len;
  240. u32 tpr_threshold;
  241. u32 secondary_vm_exec_control;
  242. u32 vm_instruction_error;
  243. u32 vm_exit_reason;
  244. u32 vm_exit_intr_info;
  245. u32 vm_exit_intr_error_code;
  246. u32 idt_vectoring_info_field;
  247. u32 idt_vectoring_error_code;
  248. u32 vm_exit_instruction_len;
  249. u32 vmx_instruction_info;
  250. u32 guest_es_limit;
  251. u32 guest_cs_limit;
  252. u32 guest_ss_limit;
  253. u32 guest_ds_limit;
  254. u32 guest_fs_limit;
  255. u32 guest_gs_limit;
  256. u32 guest_ldtr_limit;
  257. u32 guest_tr_limit;
  258. u32 guest_gdtr_limit;
  259. u32 guest_idtr_limit;
  260. u32 guest_es_ar_bytes;
  261. u32 guest_cs_ar_bytes;
  262. u32 guest_ss_ar_bytes;
  263. u32 guest_ds_ar_bytes;
  264. u32 guest_fs_ar_bytes;
  265. u32 guest_gs_ar_bytes;
  266. u32 guest_ldtr_ar_bytes;
  267. u32 guest_tr_ar_bytes;
  268. u32 guest_interruptibility_info;
  269. u32 guest_activity_state;
  270. u32 guest_sysenter_cs;
  271. u32 host_ia32_sysenter_cs;
  272. u32 padding32[8]; /* room for future expansion */
  273. u16 virtual_processor_id;
  274. u16 guest_es_selector;
  275. u16 guest_cs_selector;
  276. u16 guest_ss_selector;
  277. u16 guest_ds_selector;
  278. u16 guest_fs_selector;
  279. u16 guest_gs_selector;
  280. u16 guest_ldtr_selector;
  281. u16 guest_tr_selector;
  282. u16 host_es_selector;
  283. u16 host_cs_selector;
  284. u16 host_ss_selector;
  285. u16 host_ds_selector;
  286. u16 host_fs_selector;
  287. u16 host_gs_selector;
  288. u16 host_tr_selector;
  289. };
  290. /*
  291. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  292. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  293. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  294. */
  295. #define VMCS12_REVISION 0x11e57ed0
  296. /*
  297. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  298. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  299. * current implementation, 4K are reserved to avoid future complications.
  300. */
  301. #define VMCS12_SIZE 0x1000
  302. /* Used to remember the last vmcs02 used for some recently used vmcs12s */
  303. struct vmcs02_list {
  304. struct list_head list;
  305. gpa_t vmptr;
  306. struct loaded_vmcs vmcs02;
  307. };
  308. /*
  309. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  310. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  311. */
  312. struct nested_vmx {
  313. /* Has the level1 guest done vmxon? */
  314. bool vmxon;
  315. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  316. gpa_t current_vmptr;
  317. /* The host-usable pointer to the above */
  318. struct page *current_vmcs12_page;
  319. struct vmcs12 *current_vmcs12;
  320. /* vmcs02_list cache of VMCSs recently used to run L2 guests */
  321. struct list_head vmcs02_pool;
  322. int vmcs02_num;
  323. u64 vmcs01_tsc_offset;
  324. /* L2 must run next, and mustn't decide to exit to L1. */
  325. bool nested_run_pending;
  326. /*
  327. * Guest pages referred to in vmcs02 with host-physical pointers, so
  328. * we must keep them pinned while L2 runs.
  329. */
  330. struct page *apic_access_page;
  331. };
  332. struct vcpu_vmx {
  333. struct kvm_vcpu vcpu;
  334. unsigned long host_rsp;
  335. u8 fail;
  336. u8 cpl;
  337. bool nmi_known_unmasked;
  338. u32 exit_intr_info;
  339. u32 idt_vectoring_info;
  340. ulong rflags;
  341. struct shared_msr_entry *guest_msrs;
  342. int nmsrs;
  343. int save_nmsrs;
  344. #ifdef CONFIG_X86_64
  345. u64 msr_host_kernel_gs_base;
  346. u64 msr_guest_kernel_gs_base;
  347. #endif
  348. /*
  349. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  350. * non-nested (L1) guest, it always points to vmcs01. For a nested
  351. * guest (L2), it points to a different VMCS.
  352. */
  353. struct loaded_vmcs vmcs01;
  354. struct loaded_vmcs *loaded_vmcs;
  355. bool __launched; /* temporary, used in vmx_vcpu_run */
  356. struct msr_autoload {
  357. unsigned nr;
  358. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  359. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  360. } msr_autoload;
  361. struct {
  362. int loaded;
  363. u16 fs_sel, gs_sel, ldt_sel;
  364. #ifdef CONFIG_X86_64
  365. u16 ds_sel, es_sel;
  366. #endif
  367. int gs_ldt_reload_needed;
  368. int fs_reload_needed;
  369. } host_state;
  370. struct {
  371. int vm86_active;
  372. ulong save_rflags;
  373. struct kvm_segment segs[8];
  374. } rmode;
  375. struct {
  376. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  377. struct kvm_save_segment {
  378. u16 selector;
  379. unsigned long base;
  380. u32 limit;
  381. u32 ar;
  382. } seg[8];
  383. } segment_cache;
  384. int vpid;
  385. bool emulation_required;
  386. /* Support for vnmi-less CPUs */
  387. int soft_vnmi_blocked;
  388. ktime_t entry_time;
  389. s64 vnmi_blocked_time;
  390. u32 exit_reason;
  391. bool rdtscp_enabled;
  392. /* Support for a guest hypervisor (nested VMX) */
  393. struct nested_vmx nested;
  394. };
  395. enum segment_cache_field {
  396. SEG_FIELD_SEL = 0,
  397. SEG_FIELD_BASE = 1,
  398. SEG_FIELD_LIMIT = 2,
  399. SEG_FIELD_AR = 3,
  400. SEG_FIELD_NR = 4
  401. };
  402. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  403. {
  404. return container_of(vcpu, struct vcpu_vmx, vcpu);
  405. }
  406. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  407. #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
  408. #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
  409. [number##_HIGH] = VMCS12_OFFSET(name)+4
  410. static unsigned short vmcs_field_to_offset_table[] = {
  411. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  412. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  413. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  414. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  415. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  416. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  417. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  418. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  419. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  420. FIELD(HOST_ES_SELECTOR, host_es_selector),
  421. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  422. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  423. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  424. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  425. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  426. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  427. FIELD64(IO_BITMAP_A, io_bitmap_a),
  428. FIELD64(IO_BITMAP_B, io_bitmap_b),
  429. FIELD64(MSR_BITMAP, msr_bitmap),
  430. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  431. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  432. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  433. FIELD64(TSC_OFFSET, tsc_offset),
  434. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  435. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  436. FIELD64(EPT_POINTER, ept_pointer),
  437. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  438. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  439. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  440. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  441. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  442. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  443. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  444. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  445. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  446. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  447. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  448. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  449. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  450. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  451. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  452. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  453. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  454. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  455. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  456. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  457. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  458. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  459. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  460. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  461. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  462. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  463. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  464. FIELD(TPR_THRESHOLD, tpr_threshold),
  465. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  466. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  467. FIELD(VM_EXIT_REASON, vm_exit_reason),
  468. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  469. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  470. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  471. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  472. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  473. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  474. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  475. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  476. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  477. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  478. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  479. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  480. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  481. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  482. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  483. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  484. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  485. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  486. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  487. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  488. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  489. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  490. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  491. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  492. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  493. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  494. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  495. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  496. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  497. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  498. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  499. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  500. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  501. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  502. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  503. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  504. FIELD(EXIT_QUALIFICATION, exit_qualification),
  505. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  506. FIELD(GUEST_CR0, guest_cr0),
  507. FIELD(GUEST_CR3, guest_cr3),
  508. FIELD(GUEST_CR4, guest_cr4),
  509. FIELD(GUEST_ES_BASE, guest_es_base),
  510. FIELD(GUEST_CS_BASE, guest_cs_base),
  511. FIELD(GUEST_SS_BASE, guest_ss_base),
  512. FIELD(GUEST_DS_BASE, guest_ds_base),
  513. FIELD(GUEST_FS_BASE, guest_fs_base),
  514. FIELD(GUEST_GS_BASE, guest_gs_base),
  515. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  516. FIELD(GUEST_TR_BASE, guest_tr_base),
  517. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  518. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  519. FIELD(GUEST_DR7, guest_dr7),
  520. FIELD(GUEST_RSP, guest_rsp),
  521. FIELD(GUEST_RIP, guest_rip),
  522. FIELD(GUEST_RFLAGS, guest_rflags),
  523. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  524. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  525. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  526. FIELD(HOST_CR0, host_cr0),
  527. FIELD(HOST_CR3, host_cr3),
  528. FIELD(HOST_CR4, host_cr4),
  529. FIELD(HOST_FS_BASE, host_fs_base),
  530. FIELD(HOST_GS_BASE, host_gs_base),
  531. FIELD(HOST_TR_BASE, host_tr_base),
  532. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  533. FIELD(HOST_IDTR_BASE, host_idtr_base),
  534. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  535. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  536. FIELD(HOST_RSP, host_rsp),
  537. FIELD(HOST_RIP, host_rip),
  538. };
  539. static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
  540. static inline short vmcs_field_to_offset(unsigned long field)
  541. {
  542. if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
  543. return -1;
  544. return vmcs_field_to_offset_table[field];
  545. }
  546. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  547. {
  548. return to_vmx(vcpu)->nested.current_vmcs12;
  549. }
  550. static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
  551. {
  552. struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
  553. if (is_error_page(page))
  554. return NULL;
  555. return page;
  556. }
  557. static void nested_release_page(struct page *page)
  558. {
  559. kvm_release_page_dirty(page);
  560. }
  561. static void nested_release_page_clean(struct page *page)
  562. {
  563. kvm_release_page_clean(page);
  564. }
  565. static u64 construct_eptp(unsigned long root_hpa);
  566. static void kvm_cpu_vmxon(u64 addr);
  567. static void kvm_cpu_vmxoff(void);
  568. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  569. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  570. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  571. struct kvm_segment *var, int seg);
  572. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  573. struct kvm_segment *var, int seg);
  574. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  575. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  576. /*
  577. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  578. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  579. */
  580. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  581. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  582. static unsigned long *vmx_io_bitmap_a;
  583. static unsigned long *vmx_io_bitmap_b;
  584. static unsigned long *vmx_msr_bitmap_legacy;
  585. static unsigned long *vmx_msr_bitmap_longmode;
  586. static bool cpu_has_load_ia32_efer;
  587. static bool cpu_has_load_perf_global_ctrl;
  588. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  589. static DEFINE_SPINLOCK(vmx_vpid_lock);
  590. static struct vmcs_config {
  591. int size;
  592. int order;
  593. u32 revision_id;
  594. u32 pin_based_exec_ctrl;
  595. u32 cpu_based_exec_ctrl;
  596. u32 cpu_based_2nd_exec_ctrl;
  597. u32 vmexit_ctrl;
  598. u32 vmentry_ctrl;
  599. } vmcs_config;
  600. static struct vmx_capability {
  601. u32 ept;
  602. u32 vpid;
  603. } vmx_capability;
  604. #define VMX_SEGMENT_FIELD(seg) \
  605. [VCPU_SREG_##seg] = { \
  606. .selector = GUEST_##seg##_SELECTOR, \
  607. .base = GUEST_##seg##_BASE, \
  608. .limit = GUEST_##seg##_LIMIT, \
  609. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  610. }
  611. static struct kvm_vmx_segment_field {
  612. unsigned selector;
  613. unsigned base;
  614. unsigned limit;
  615. unsigned ar_bytes;
  616. } kvm_vmx_segment_fields[] = {
  617. VMX_SEGMENT_FIELD(CS),
  618. VMX_SEGMENT_FIELD(DS),
  619. VMX_SEGMENT_FIELD(ES),
  620. VMX_SEGMENT_FIELD(FS),
  621. VMX_SEGMENT_FIELD(GS),
  622. VMX_SEGMENT_FIELD(SS),
  623. VMX_SEGMENT_FIELD(TR),
  624. VMX_SEGMENT_FIELD(LDTR),
  625. };
  626. static u64 host_efer;
  627. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  628. /*
  629. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  630. * away by decrementing the array size.
  631. */
  632. static const u32 vmx_msr_index[] = {
  633. #ifdef CONFIG_X86_64
  634. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  635. #endif
  636. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  637. };
  638. #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
  639. static inline bool is_page_fault(u32 intr_info)
  640. {
  641. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  642. INTR_INFO_VALID_MASK)) ==
  643. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  644. }
  645. static inline bool is_no_device(u32 intr_info)
  646. {
  647. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  648. INTR_INFO_VALID_MASK)) ==
  649. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  650. }
  651. static inline bool is_invalid_opcode(u32 intr_info)
  652. {
  653. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  654. INTR_INFO_VALID_MASK)) ==
  655. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  656. }
  657. static inline bool is_external_interrupt(u32 intr_info)
  658. {
  659. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  660. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  661. }
  662. static inline bool is_machine_check(u32 intr_info)
  663. {
  664. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  665. INTR_INFO_VALID_MASK)) ==
  666. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  667. }
  668. static inline bool cpu_has_vmx_msr_bitmap(void)
  669. {
  670. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  671. }
  672. static inline bool cpu_has_vmx_tpr_shadow(void)
  673. {
  674. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  675. }
  676. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  677. {
  678. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  679. }
  680. static inline bool cpu_has_secondary_exec_ctrls(void)
  681. {
  682. return vmcs_config.cpu_based_exec_ctrl &
  683. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  684. }
  685. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  686. {
  687. return vmcs_config.cpu_based_2nd_exec_ctrl &
  688. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  689. }
  690. static inline bool cpu_has_vmx_flexpriority(void)
  691. {
  692. return cpu_has_vmx_tpr_shadow() &&
  693. cpu_has_vmx_virtualize_apic_accesses();
  694. }
  695. static inline bool cpu_has_vmx_ept_execute_only(void)
  696. {
  697. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  698. }
  699. static inline bool cpu_has_vmx_eptp_uncacheable(void)
  700. {
  701. return vmx_capability.ept & VMX_EPTP_UC_BIT;
  702. }
  703. static inline bool cpu_has_vmx_eptp_writeback(void)
  704. {
  705. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  706. }
  707. static inline bool cpu_has_vmx_ept_2m_page(void)
  708. {
  709. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  710. }
  711. static inline bool cpu_has_vmx_ept_1g_page(void)
  712. {
  713. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  714. }
  715. static inline bool cpu_has_vmx_ept_4levels(void)
  716. {
  717. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  718. }
  719. static inline bool cpu_has_vmx_ept_ad_bits(void)
  720. {
  721. return vmx_capability.ept & VMX_EPT_AD_BIT;
  722. }
  723. static inline bool cpu_has_vmx_invept_individual_addr(void)
  724. {
  725. return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
  726. }
  727. static inline bool cpu_has_vmx_invept_context(void)
  728. {
  729. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  730. }
  731. static inline bool cpu_has_vmx_invept_global(void)
  732. {
  733. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  734. }
  735. static inline bool cpu_has_vmx_invvpid_single(void)
  736. {
  737. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  738. }
  739. static inline bool cpu_has_vmx_invvpid_global(void)
  740. {
  741. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  742. }
  743. static inline bool cpu_has_vmx_ept(void)
  744. {
  745. return vmcs_config.cpu_based_2nd_exec_ctrl &
  746. SECONDARY_EXEC_ENABLE_EPT;
  747. }
  748. static inline bool cpu_has_vmx_unrestricted_guest(void)
  749. {
  750. return vmcs_config.cpu_based_2nd_exec_ctrl &
  751. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  752. }
  753. static inline bool cpu_has_vmx_ple(void)
  754. {
  755. return vmcs_config.cpu_based_2nd_exec_ctrl &
  756. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  757. }
  758. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  759. {
  760. return flexpriority_enabled && irqchip_in_kernel(kvm);
  761. }
  762. static inline bool cpu_has_vmx_vpid(void)
  763. {
  764. return vmcs_config.cpu_based_2nd_exec_ctrl &
  765. SECONDARY_EXEC_ENABLE_VPID;
  766. }
  767. static inline bool cpu_has_vmx_rdtscp(void)
  768. {
  769. return vmcs_config.cpu_based_2nd_exec_ctrl &
  770. SECONDARY_EXEC_RDTSCP;
  771. }
  772. static inline bool cpu_has_vmx_invpcid(void)
  773. {
  774. return vmcs_config.cpu_based_2nd_exec_ctrl &
  775. SECONDARY_EXEC_ENABLE_INVPCID;
  776. }
  777. static inline bool cpu_has_virtual_nmis(void)
  778. {
  779. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  780. }
  781. static inline bool cpu_has_vmx_wbinvd_exit(void)
  782. {
  783. return vmcs_config.cpu_based_2nd_exec_ctrl &
  784. SECONDARY_EXEC_WBINVD_EXITING;
  785. }
  786. static inline bool report_flexpriority(void)
  787. {
  788. return flexpriority_enabled;
  789. }
  790. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  791. {
  792. return vmcs12->cpu_based_vm_exec_control & bit;
  793. }
  794. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  795. {
  796. return (vmcs12->cpu_based_vm_exec_control &
  797. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  798. (vmcs12->secondary_vm_exec_control & bit);
  799. }
  800. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
  801. struct kvm_vcpu *vcpu)
  802. {
  803. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  804. }
  805. static inline bool is_exception(u32 intr_info)
  806. {
  807. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  808. == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
  809. }
  810. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
  811. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  812. struct vmcs12 *vmcs12,
  813. u32 reason, unsigned long qualification);
  814. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  815. {
  816. int i;
  817. for (i = 0; i < vmx->nmsrs; ++i)
  818. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  819. return i;
  820. return -1;
  821. }
  822. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  823. {
  824. struct {
  825. u64 vpid : 16;
  826. u64 rsvd : 48;
  827. u64 gva;
  828. } operand = { vpid, 0, gva };
  829. asm volatile (__ex(ASM_VMX_INVVPID)
  830. /* CF==1 or ZF==1 --> rc = -1 */
  831. "; ja 1f ; ud2 ; 1:"
  832. : : "a"(&operand), "c"(ext) : "cc", "memory");
  833. }
  834. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  835. {
  836. struct {
  837. u64 eptp, gpa;
  838. } operand = {eptp, gpa};
  839. asm volatile (__ex(ASM_VMX_INVEPT)
  840. /* CF==1 or ZF==1 --> rc = -1 */
  841. "; ja 1f ; ud2 ; 1:\n"
  842. : : "a" (&operand), "c" (ext) : "cc", "memory");
  843. }
  844. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  845. {
  846. int i;
  847. i = __find_msr_index(vmx, msr);
  848. if (i >= 0)
  849. return &vmx->guest_msrs[i];
  850. return NULL;
  851. }
  852. static void vmcs_clear(struct vmcs *vmcs)
  853. {
  854. u64 phys_addr = __pa(vmcs);
  855. u8 error;
  856. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  857. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  858. : "cc", "memory");
  859. if (error)
  860. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  861. vmcs, phys_addr);
  862. }
  863. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  864. {
  865. vmcs_clear(loaded_vmcs->vmcs);
  866. loaded_vmcs->cpu = -1;
  867. loaded_vmcs->launched = 0;
  868. }
  869. static void vmcs_load(struct vmcs *vmcs)
  870. {
  871. u64 phys_addr = __pa(vmcs);
  872. u8 error;
  873. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  874. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  875. : "cc", "memory");
  876. if (error)
  877. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  878. vmcs, phys_addr);
  879. }
  880. static void __loaded_vmcs_clear(void *arg)
  881. {
  882. struct loaded_vmcs *loaded_vmcs = arg;
  883. int cpu = raw_smp_processor_id();
  884. if (loaded_vmcs->cpu != cpu)
  885. return; /* vcpu migration can race with cpu offline */
  886. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  887. per_cpu(current_vmcs, cpu) = NULL;
  888. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  889. loaded_vmcs_init(loaded_vmcs);
  890. }
  891. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  892. {
  893. if (loaded_vmcs->cpu != -1)
  894. smp_call_function_single(
  895. loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
  896. }
  897. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  898. {
  899. if (vmx->vpid == 0)
  900. return;
  901. if (cpu_has_vmx_invvpid_single())
  902. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  903. }
  904. static inline void vpid_sync_vcpu_global(void)
  905. {
  906. if (cpu_has_vmx_invvpid_global())
  907. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  908. }
  909. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  910. {
  911. if (cpu_has_vmx_invvpid_single())
  912. vpid_sync_vcpu_single(vmx);
  913. else
  914. vpid_sync_vcpu_global();
  915. }
  916. static inline void ept_sync_global(void)
  917. {
  918. if (cpu_has_vmx_invept_global())
  919. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  920. }
  921. static inline void ept_sync_context(u64 eptp)
  922. {
  923. if (enable_ept) {
  924. if (cpu_has_vmx_invept_context())
  925. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  926. else
  927. ept_sync_global();
  928. }
  929. }
  930. static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
  931. {
  932. if (enable_ept) {
  933. if (cpu_has_vmx_invept_individual_addr())
  934. __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
  935. eptp, gpa);
  936. else
  937. ept_sync_context(eptp);
  938. }
  939. }
  940. static __always_inline unsigned long vmcs_readl(unsigned long field)
  941. {
  942. unsigned long value;
  943. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  944. : "=a"(value) : "d"(field) : "cc");
  945. return value;
  946. }
  947. static __always_inline u16 vmcs_read16(unsigned long field)
  948. {
  949. return vmcs_readl(field);
  950. }
  951. static __always_inline u32 vmcs_read32(unsigned long field)
  952. {
  953. return vmcs_readl(field);
  954. }
  955. static __always_inline u64 vmcs_read64(unsigned long field)
  956. {
  957. #ifdef CONFIG_X86_64
  958. return vmcs_readl(field);
  959. #else
  960. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  961. #endif
  962. }
  963. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  964. {
  965. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  966. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  967. dump_stack();
  968. }
  969. static void vmcs_writel(unsigned long field, unsigned long value)
  970. {
  971. u8 error;
  972. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  973. : "=q"(error) : "a"(value), "d"(field) : "cc");
  974. if (unlikely(error))
  975. vmwrite_error(field, value);
  976. }
  977. static void vmcs_write16(unsigned long field, u16 value)
  978. {
  979. vmcs_writel(field, value);
  980. }
  981. static void vmcs_write32(unsigned long field, u32 value)
  982. {
  983. vmcs_writel(field, value);
  984. }
  985. static void vmcs_write64(unsigned long field, u64 value)
  986. {
  987. vmcs_writel(field, value);
  988. #ifndef CONFIG_X86_64
  989. asm volatile ("");
  990. vmcs_writel(field+1, value >> 32);
  991. #endif
  992. }
  993. static void vmcs_clear_bits(unsigned long field, u32 mask)
  994. {
  995. vmcs_writel(field, vmcs_readl(field) & ~mask);
  996. }
  997. static void vmcs_set_bits(unsigned long field, u32 mask)
  998. {
  999. vmcs_writel(field, vmcs_readl(field) | mask);
  1000. }
  1001. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1002. {
  1003. vmx->segment_cache.bitmask = 0;
  1004. }
  1005. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1006. unsigned field)
  1007. {
  1008. bool ret;
  1009. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1010. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1011. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1012. vmx->segment_cache.bitmask = 0;
  1013. }
  1014. ret = vmx->segment_cache.bitmask & mask;
  1015. vmx->segment_cache.bitmask |= mask;
  1016. return ret;
  1017. }
  1018. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1019. {
  1020. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1021. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1022. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1023. return *p;
  1024. }
  1025. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1026. {
  1027. ulong *p = &vmx->segment_cache.seg[seg].base;
  1028. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1029. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1030. return *p;
  1031. }
  1032. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1033. {
  1034. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1035. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1036. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1037. return *p;
  1038. }
  1039. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1040. {
  1041. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1042. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1043. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1044. return *p;
  1045. }
  1046. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1047. {
  1048. u32 eb;
  1049. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1050. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  1051. if ((vcpu->guest_debug &
  1052. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1053. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1054. eb |= 1u << BP_VECTOR;
  1055. if (to_vmx(vcpu)->rmode.vm86_active)
  1056. eb = ~0;
  1057. if (enable_ept)
  1058. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1059. if (vcpu->fpu_active)
  1060. eb &= ~(1u << NM_VECTOR);
  1061. /* When we are running a nested L2 guest and L1 specified for it a
  1062. * certain exception bitmap, we must trap the same exceptions and pass
  1063. * them to L1. When running L2, we will only handle the exceptions
  1064. * specified above if L1 did not want them.
  1065. */
  1066. if (is_guest_mode(vcpu))
  1067. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1068. vmcs_write32(EXCEPTION_BITMAP, eb);
  1069. }
  1070. static void clear_atomic_switch_msr_special(unsigned long entry,
  1071. unsigned long exit)
  1072. {
  1073. vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
  1074. vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
  1075. }
  1076. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1077. {
  1078. unsigned i;
  1079. struct msr_autoload *m = &vmx->msr_autoload;
  1080. switch (msr) {
  1081. case MSR_EFER:
  1082. if (cpu_has_load_ia32_efer) {
  1083. clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
  1084. VM_EXIT_LOAD_IA32_EFER);
  1085. return;
  1086. }
  1087. break;
  1088. case MSR_CORE_PERF_GLOBAL_CTRL:
  1089. if (cpu_has_load_perf_global_ctrl) {
  1090. clear_atomic_switch_msr_special(
  1091. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1092. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1093. return;
  1094. }
  1095. break;
  1096. }
  1097. for (i = 0; i < m->nr; ++i)
  1098. if (m->guest[i].index == msr)
  1099. break;
  1100. if (i == m->nr)
  1101. return;
  1102. --m->nr;
  1103. m->guest[i] = m->guest[m->nr];
  1104. m->host[i] = m->host[m->nr];
  1105. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1106. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1107. }
  1108. static void add_atomic_switch_msr_special(unsigned long entry,
  1109. unsigned long exit, unsigned long guest_val_vmcs,
  1110. unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
  1111. {
  1112. vmcs_write64(guest_val_vmcs, guest_val);
  1113. vmcs_write64(host_val_vmcs, host_val);
  1114. vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
  1115. vmcs_set_bits(VM_EXIT_CONTROLS, exit);
  1116. }
  1117. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1118. u64 guest_val, u64 host_val)
  1119. {
  1120. unsigned i;
  1121. struct msr_autoload *m = &vmx->msr_autoload;
  1122. switch (msr) {
  1123. case MSR_EFER:
  1124. if (cpu_has_load_ia32_efer) {
  1125. add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
  1126. VM_EXIT_LOAD_IA32_EFER,
  1127. GUEST_IA32_EFER,
  1128. HOST_IA32_EFER,
  1129. guest_val, host_val);
  1130. return;
  1131. }
  1132. break;
  1133. case MSR_CORE_PERF_GLOBAL_CTRL:
  1134. if (cpu_has_load_perf_global_ctrl) {
  1135. add_atomic_switch_msr_special(
  1136. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1137. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1138. GUEST_IA32_PERF_GLOBAL_CTRL,
  1139. HOST_IA32_PERF_GLOBAL_CTRL,
  1140. guest_val, host_val);
  1141. return;
  1142. }
  1143. break;
  1144. }
  1145. for (i = 0; i < m->nr; ++i)
  1146. if (m->guest[i].index == msr)
  1147. break;
  1148. if (i == NR_AUTOLOAD_MSRS) {
  1149. printk_once(KERN_WARNING"Not enough mst switch entries. "
  1150. "Can't add msr %x\n", msr);
  1151. return;
  1152. } else if (i == m->nr) {
  1153. ++m->nr;
  1154. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1155. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1156. }
  1157. m->guest[i].index = msr;
  1158. m->guest[i].value = guest_val;
  1159. m->host[i].index = msr;
  1160. m->host[i].value = host_val;
  1161. }
  1162. static void reload_tss(void)
  1163. {
  1164. /*
  1165. * VT restores TR but not its size. Useless.
  1166. */
  1167. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  1168. struct desc_struct *descs;
  1169. descs = (void *)gdt->address;
  1170. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  1171. load_TR_desc();
  1172. }
  1173. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1174. {
  1175. u64 guest_efer;
  1176. u64 ignore_bits;
  1177. guest_efer = vmx->vcpu.arch.efer;
  1178. /*
  1179. * NX is emulated; LMA and LME handled by hardware; SCE meaningless
  1180. * outside long mode
  1181. */
  1182. ignore_bits = EFER_NX | EFER_SCE;
  1183. #ifdef CONFIG_X86_64
  1184. ignore_bits |= EFER_LMA | EFER_LME;
  1185. /* SCE is meaningful only in long mode on Intel */
  1186. if (guest_efer & EFER_LMA)
  1187. ignore_bits &= ~(u64)EFER_SCE;
  1188. #endif
  1189. guest_efer &= ~ignore_bits;
  1190. guest_efer |= host_efer & ignore_bits;
  1191. vmx->guest_msrs[efer_offset].data = guest_efer;
  1192. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1193. clear_atomic_switch_msr(vmx, MSR_EFER);
  1194. /* On ept, can't emulate nx, and must switch nx atomically */
  1195. if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
  1196. guest_efer = vmx->vcpu.arch.efer;
  1197. if (!(guest_efer & EFER_LMA))
  1198. guest_efer &= ~EFER_LME;
  1199. add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
  1200. return false;
  1201. }
  1202. return true;
  1203. }
  1204. static unsigned long segment_base(u16 selector)
  1205. {
  1206. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  1207. struct desc_struct *d;
  1208. unsigned long table_base;
  1209. unsigned long v;
  1210. if (!(selector & ~3))
  1211. return 0;
  1212. table_base = gdt->address;
  1213. if (selector & 4) { /* from ldt */
  1214. u16 ldt_selector = kvm_read_ldt();
  1215. if (!(ldt_selector & ~3))
  1216. return 0;
  1217. table_base = segment_base(ldt_selector);
  1218. }
  1219. d = (struct desc_struct *)(table_base + (selector & ~7));
  1220. v = get_desc_base(d);
  1221. #ifdef CONFIG_X86_64
  1222. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  1223. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  1224. #endif
  1225. return v;
  1226. }
  1227. static inline unsigned long kvm_read_tr_base(void)
  1228. {
  1229. u16 tr;
  1230. asm("str %0" : "=g"(tr));
  1231. return segment_base(tr);
  1232. }
  1233. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1234. {
  1235. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1236. int i;
  1237. if (vmx->host_state.loaded)
  1238. return;
  1239. vmx->host_state.loaded = 1;
  1240. /*
  1241. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1242. * allow segment selectors with cpl > 0 or ti == 1.
  1243. */
  1244. vmx->host_state.ldt_sel = kvm_read_ldt();
  1245. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1246. savesegment(fs, vmx->host_state.fs_sel);
  1247. if (!(vmx->host_state.fs_sel & 7)) {
  1248. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1249. vmx->host_state.fs_reload_needed = 0;
  1250. } else {
  1251. vmcs_write16(HOST_FS_SELECTOR, 0);
  1252. vmx->host_state.fs_reload_needed = 1;
  1253. }
  1254. savesegment(gs, vmx->host_state.gs_sel);
  1255. if (!(vmx->host_state.gs_sel & 7))
  1256. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1257. else {
  1258. vmcs_write16(HOST_GS_SELECTOR, 0);
  1259. vmx->host_state.gs_ldt_reload_needed = 1;
  1260. }
  1261. #ifdef CONFIG_X86_64
  1262. savesegment(ds, vmx->host_state.ds_sel);
  1263. savesegment(es, vmx->host_state.es_sel);
  1264. #endif
  1265. #ifdef CONFIG_X86_64
  1266. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1267. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1268. #else
  1269. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1270. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1271. #endif
  1272. #ifdef CONFIG_X86_64
  1273. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1274. if (is_long_mode(&vmx->vcpu))
  1275. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1276. #endif
  1277. for (i = 0; i < vmx->save_nmsrs; ++i)
  1278. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1279. vmx->guest_msrs[i].data,
  1280. vmx->guest_msrs[i].mask);
  1281. }
  1282. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1283. {
  1284. if (!vmx->host_state.loaded)
  1285. return;
  1286. ++vmx->vcpu.stat.host_state_reload;
  1287. vmx->host_state.loaded = 0;
  1288. #ifdef CONFIG_X86_64
  1289. if (is_long_mode(&vmx->vcpu))
  1290. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1291. #endif
  1292. if (vmx->host_state.gs_ldt_reload_needed) {
  1293. kvm_load_ldt(vmx->host_state.ldt_sel);
  1294. #ifdef CONFIG_X86_64
  1295. load_gs_index(vmx->host_state.gs_sel);
  1296. #else
  1297. loadsegment(gs, vmx->host_state.gs_sel);
  1298. #endif
  1299. }
  1300. if (vmx->host_state.fs_reload_needed)
  1301. loadsegment(fs, vmx->host_state.fs_sel);
  1302. #ifdef CONFIG_X86_64
  1303. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  1304. loadsegment(ds, vmx->host_state.ds_sel);
  1305. loadsegment(es, vmx->host_state.es_sel);
  1306. }
  1307. #endif
  1308. reload_tss();
  1309. #ifdef CONFIG_X86_64
  1310. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1311. #endif
  1312. if (user_has_fpu())
  1313. clts();
  1314. load_gdt(&__get_cpu_var(host_gdt));
  1315. }
  1316. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1317. {
  1318. preempt_disable();
  1319. __vmx_load_host_state(vmx);
  1320. preempt_enable();
  1321. }
  1322. /*
  1323. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1324. * vcpu mutex is already taken.
  1325. */
  1326. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1327. {
  1328. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1329. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1330. if (!vmm_exclusive)
  1331. kvm_cpu_vmxon(phys_addr);
  1332. else if (vmx->loaded_vmcs->cpu != cpu)
  1333. loaded_vmcs_clear(vmx->loaded_vmcs);
  1334. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1335. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1336. vmcs_load(vmx->loaded_vmcs->vmcs);
  1337. }
  1338. if (vmx->loaded_vmcs->cpu != cpu) {
  1339. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  1340. unsigned long sysenter_esp;
  1341. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1342. local_irq_disable();
  1343. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1344. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1345. local_irq_enable();
  1346. /*
  1347. * Linux uses per-cpu TSS and GDT, so set these when switching
  1348. * processors.
  1349. */
  1350. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  1351. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  1352. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  1353. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  1354. vmx->loaded_vmcs->cpu = cpu;
  1355. }
  1356. }
  1357. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  1358. {
  1359. __vmx_load_host_state(to_vmx(vcpu));
  1360. if (!vmm_exclusive) {
  1361. __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
  1362. vcpu->cpu = -1;
  1363. kvm_cpu_vmxoff();
  1364. }
  1365. }
  1366. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  1367. {
  1368. ulong cr0;
  1369. if (vcpu->fpu_active)
  1370. return;
  1371. vcpu->fpu_active = 1;
  1372. cr0 = vmcs_readl(GUEST_CR0);
  1373. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  1374. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  1375. vmcs_writel(GUEST_CR0, cr0);
  1376. update_exception_bitmap(vcpu);
  1377. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  1378. if (is_guest_mode(vcpu))
  1379. vcpu->arch.cr0_guest_owned_bits &=
  1380. ~get_vmcs12(vcpu)->cr0_guest_host_mask;
  1381. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1382. }
  1383. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  1384. /*
  1385. * Return the cr0 value that a nested guest would read. This is a combination
  1386. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  1387. * its hypervisor (cr0_read_shadow).
  1388. */
  1389. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  1390. {
  1391. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  1392. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  1393. }
  1394. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  1395. {
  1396. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  1397. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  1398. }
  1399. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  1400. {
  1401. /* Note that there is no vcpu->fpu_active = 0 here. The caller must
  1402. * set this *before* calling this function.
  1403. */
  1404. vmx_decache_cr0_guest_bits(vcpu);
  1405. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  1406. update_exception_bitmap(vcpu);
  1407. vcpu->arch.cr0_guest_owned_bits = 0;
  1408. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1409. if (is_guest_mode(vcpu)) {
  1410. /*
  1411. * L1's specified read shadow might not contain the TS bit,
  1412. * so now that we turned on shadowing of this bit, we need to
  1413. * set this bit of the shadow. Like in nested_vmx_run we need
  1414. * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
  1415. * up-to-date here because we just decached cr0.TS (and we'll
  1416. * only update vmcs12->guest_cr0 on nested exit).
  1417. */
  1418. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1419. vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
  1420. (vcpu->arch.cr0 & X86_CR0_TS);
  1421. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  1422. } else
  1423. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  1424. }
  1425. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  1426. {
  1427. unsigned long rflags, save_rflags;
  1428. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  1429. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1430. rflags = vmcs_readl(GUEST_RFLAGS);
  1431. if (to_vmx(vcpu)->rmode.vm86_active) {
  1432. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1433. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  1434. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1435. }
  1436. to_vmx(vcpu)->rflags = rflags;
  1437. }
  1438. return to_vmx(vcpu)->rflags;
  1439. }
  1440. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1441. {
  1442. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1443. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1444. to_vmx(vcpu)->rflags = rflags;
  1445. if (to_vmx(vcpu)->rmode.vm86_active) {
  1446. to_vmx(vcpu)->rmode.save_rflags = rflags;
  1447. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1448. }
  1449. vmcs_writel(GUEST_RFLAGS, rflags);
  1450. }
  1451. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1452. {
  1453. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1454. int ret = 0;
  1455. if (interruptibility & GUEST_INTR_STATE_STI)
  1456. ret |= KVM_X86_SHADOW_INT_STI;
  1457. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  1458. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  1459. return ret & mask;
  1460. }
  1461. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1462. {
  1463. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1464. u32 interruptibility = interruptibility_old;
  1465. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  1466. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  1467. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  1468. else if (mask & KVM_X86_SHADOW_INT_STI)
  1469. interruptibility |= GUEST_INTR_STATE_STI;
  1470. if ((interruptibility != interruptibility_old))
  1471. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  1472. }
  1473. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  1474. {
  1475. unsigned long rip;
  1476. rip = kvm_rip_read(vcpu);
  1477. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  1478. kvm_rip_write(vcpu, rip);
  1479. /* skipping an emulated instruction also counts */
  1480. vmx_set_interrupt_shadow(vcpu, 0);
  1481. }
  1482. /*
  1483. * KVM wants to inject page-faults which it got to the guest. This function
  1484. * checks whether in a nested guest, we need to inject them to L1 or L2.
  1485. * This function assumes it is called with the exit reason in vmcs02 being
  1486. * a #PF exception (this is the only case in which KVM injects a #PF when L2
  1487. * is running).
  1488. */
  1489. static int nested_pf_handled(struct kvm_vcpu *vcpu)
  1490. {
  1491. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1492. /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
  1493. if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
  1494. return 0;
  1495. nested_vmx_vmexit(vcpu);
  1496. return 1;
  1497. }
  1498. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  1499. bool has_error_code, u32 error_code,
  1500. bool reinject)
  1501. {
  1502. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1503. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  1504. if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
  1505. nested_pf_handled(vcpu))
  1506. return;
  1507. if (has_error_code) {
  1508. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  1509. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  1510. }
  1511. if (vmx->rmode.vm86_active) {
  1512. int inc_eip = 0;
  1513. if (kvm_exception_is_soft(nr))
  1514. inc_eip = vcpu->arch.event_exit_inst_len;
  1515. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  1516. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1517. return;
  1518. }
  1519. if (kvm_exception_is_soft(nr)) {
  1520. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  1521. vmx->vcpu.arch.event_exit_inst_len);
  1522. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  1523. } else
  1524. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  1525. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  1526. }
  1527. static bool vmx_rdtscp_supported(void)
  1528. {
  1529. return cpu_has_vmx_rdtscp();
  1530. }
  1531. static bool vmx_invpcid_supported(void)
  1532. {
  1533. return cpu_has_vmx_invpcid() && enable_ept;
  1534. }
  1535. /*
  1536. * Swap MSR entry in host/guest MSR entry array.
  1537. */
  1538. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  1539. {
  1540. struct shared_msr_entry tmp;
  1541. tmp = vmx->guest_msrs[to];
  1542. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  1543. vmx->guest_msrs[from] = tmp;
  1544. }
  1545. /*
  1546. * Set up the vmcs to automatically save and restore system
  1547. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  1548. * mode, as fiddling with msrs is very expensive.
  1549. */
  1550. static void setup_msrs(struct vcpu_vmx *vmx)
  1551. {
  1552. int save_nmsrs, index;
  1553. unsigned long *msr_bitmap;
  1554. save_nmsrs = 0;
  1555. #ifdef CONFIG_X86_64
  1556. if (is_long_mode(&vmx->vcpu)) {
  1557. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1558. if (index >= 0)
  1559. move_msr_up(vmx, index, save_nmsrs++);
  1560. index = __find_msr_index(vmx, MSR_LSTAR);
  1561. if (index >= 0)
  1562. move_msr_up(vmx, index, save_nmsrs++);
  1563. index = __find_msr_index(vmx, MSR_CSTAR);
  1564. if (index >= 0)
  1565. move_msr_up(vmx, index, save_nmsrs++);
  1566. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1567. if (index >= 0 && vmx->rdtscp_enabled)
  1568. move_msr_up(vmx, index, save_nmsrs++);
  1569. /*
  1570. * MSR_STAR is only needed on long mode guests, and only
  1571. * if efer.sce is enabled.
  1572. */
  1573. index = __find_msr_index(vmx, MSR_STAR);
  1574. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1575. move_msr_up(vmx, index, save_nmsrs++);
  1576. }
  1577. #endif
  1578. index = __find_msr_index(vmx, MSR_EFER);
  1579. if (index >= 0 && update_transition_efer(vmx, index))
  1580. move_msr_up(vmx, index, save_nmsrs++);
  1581. vmx->save_nmsrs = save_nmsrs;
  1582. if (cpu_has_vmx_msr_bitmap()) {
  1583. if (is_long_mode(&vmx->vcpu))
  1584. msr_bitmap = vmx_msr_bitmap_longmode;
  1585. else
  1586. msr_bitmap = vmx_msr_bitmap_legacy;
  1587. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1588. }
  1589. }
  1590. /*
  1591. * reads and returns guest's timestamp counter "register"
  1592. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1593. */
  1594. static u64 guest_read_tsc(void)
  1595. {
  1596. u64 host_tsc, tsc_offset;
  1597. rdtscll(host_tsc);
  1598. tsc_offset = vmcs_read64(TSC_OFFSET);
  1599. return host_tsc + tsc_offset;
  1600. }
  1601. /*
  1602. * Like guest_read_tsc, but always returns L1's notion of the timestamp
  1603. * counter, even if a nested guest (L2) is currently running.
  1604. */
  1605. u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu)
  1606. {
  1607. u64 host_tsc, tsc_offset;
  1608. rdtscll(host_tsc);
  1609. tsc_offset = is_guest_mode(vcpu) ?
  1610. to_vmx(vcpu)->nested.vmcs01_tsc_offset :
  1611. vmcs_read64(TSC_OFFSET);
  1612. return host_tsc + tsc_offset;
  1613. }
  1614. /*
  1615. * Engage any workarounds for mis-matched TSC rates. Currently limited to
  1616. * software catchup for faster rates on slower CPUs.
  1617. */
  1618. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
  1619. {
  1620. if (!scale)
  1621. return;
  1622. if (user_tsc_khz > tsc_khz) {
  1623. vcpu->arch.tsc_catchup = 1;
  1624. vcpu->arch.tsc_always_catchup = 1;
  1625. } else
  1626. WARN(1, "user requested TSC rate below hardware speed\n");
  1627. }
  1628. /*
  1629. * writes 'offset' into guest's timestamp counter offset register
  1630. */
  1631. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1632. {
  1633. if (is_guest_mode(vcpu)) {
  1634. /*
  1635. * We're here if L1 chose not to trap WRMSR to TSC. According
  1636. * to the spec, this should set L1's TSC; The offset that L1
  1637. * set for L2 remains unchanged, and still needs to be added
  1638. * to the newly set TSC to get L2's TSC.
  1639. */
  1640. struct vmcs12 *vmcs12;
  1641. to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
  1642. /* recalculate vmcs02.TSC_OFFSET: */
  1643. vmcs12 = get_vmcs12(vcpu);
  1644. vmcs_write64(TSC_OFFSET, offset +
  1645. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  1646. vmcs12->tsc_offset : 0));
  1647. } else {
  1648. vmcs_write64(TSC_OFFSET, offset);
  1649. }
  1650. }
  1651. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
  1652. {
  1653. u64 offset = vmcs_read64(TSC_OFFSET);
  1654. vmcs_write64(TSC_OFFSET, offset + adjustment);
  1655. if (is_guest_mode(vcpu)) {
  1656. /* Even when running L2, the adjustment needs to apply to L1 */
  1657. to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
  1658. }
  1659. }
  1660. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1661. {
  1662. return target_tsc - native_read_tsc();
  1663. }
  1664. static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
  1665. {
  1666. struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
  1667. return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
  1668. }
  1669. /*
  1670. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  1671. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  1672. * all guests if the "nested" module option is off, and can also be disabled
  1673. * for a single guest by disabling its VMX cpuid bit.
  1674. */
  1675. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  1676. {
  1677. return nested && guest_cpuid_has_vmx(vcpu);
  1678. }
  1679. /*
  1680. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  1681. * returned for the various VMX controls MSRs when nested VMX is enabled.
  1682. * The same values should also be used to verify that vmcs12 control fields are
  1683. * valid during nested entry from L1 to L2.
  1684. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  1685. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  1686. * bit in the high half is on if the corresponding bit in the control field
  1687. * may be on. See also vmx_control_verify().
  1688. * TODO: allow these variables to be modified (downgraded) by module options
  1689. * or other means.
  1690. */
  1691. static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
  1692. static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
  1693. static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
  1694. static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
  1695. static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
  1696. static __init void nested_vmx_setup_ctls_msrs(void)
  1697. {
  1698. /*
  1699. * Note that as a general rule, the high half of the MSRs (bits in
  1700. * the control fields which may be 1) should be initialized by the
  1701. * intersection of the underlying hardware's MSR (i.e., features which
  1702. * can be supported) and the list of features we want to expose -
  1703. * because they are known to be properly supported in our code.
  1704. * Also, usually, the low half of the MSRs (bits which must be 1) can
  1705. * be set to 0, meaning that L1 may turn off any of these bits. The
  1706. * reason is that if one of these bits is necessary, it will appear
  1707. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  1708. * fields of vmcs01 and vmcs02, will turn these bits off - and
  1709. * nested_vmx_exit_handled() will not pass related exits to L1.
  1710. * These rules have exceptions below.
  1711. */
  1712. /* pin-based controls */
  1713. /*
  1714. * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
  1715. * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
  1716. */
  1717. nested_vmx_pinbased_ctls_low = 0x16 ;
  1718. nested_vmx_pinbased_ctls_high = 0x16 |
  1719. PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
  1720. PIN_BASED_VIRTUAL_NMIS;
  1721. /* exit controls */
  1722. nested_vmx_exit_ctls_low = 0;
  1723. /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
  1724. #ifdef CONFIG_X86_64
  1725. nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1726. #else
  1727. nested_vmx_exit_ctls_high = 0;
  1728. #endif
  1729. /* entry controls */
  1730. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  1731. nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
  1732. nested_vmx_entry_ctls_low = 0;
  1733. nested_vmx_entry_ctls_high &=
  1734. VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
  1735. /* cpu-based controls */
  1736. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  1737. nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
  1738. nested_vmx_procbased_ctls_low = 0;
  1739. nested_vmx_procbased_ctls_high &=
  1740. CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  1741. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  1742. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  1743. CPU_BASED_CR3_STORE_EXITING |
  1744. #ifdef CONFIG_X86_64
  1745. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  1746. #endif
  1747. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  1748. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
  1749. CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
  1750. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1751. /*
  1752. * We can allow some features even when not supported by the
  1753. * hardware. For example, L1 can specify an MSR bitmap - and we
  1754. * can use it to avoid exits to L1 - even when L0 runs L2
  1755. * without MSR bitmaps.
  1756. */
  1757. nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
  1758. /* secondary cpu-based controls */
  1759. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  1760. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
  1761. nested_vmx_secondary_ctls_low = 0;
  1762. nested_vmx_secondary_ctls_high &=
  1763. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1764. }
  1765. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  1766. {
  1767. /*
  1768. * Bits 0 in high must be 0, and bits 1 in low must be 1.
  1769. */
  1770. return ((control & high) | low) == control;
  1771. }
  1772. static inline u64 vmx_control_msr(u32 low, u32 high)
  1773. {
  1774. return low | ((u64)high << 32);
  1775. }
  1776. /*
  1777. * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
  1778. * also let it use VMX-specific MSRs.
  1779. * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
  1780. * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
  1781. * like all other MSRs).
  1782. */
  1783. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1784. {
  1785. if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
  1786. msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
  1787. /*
  1788. * According to the spec, processors which do not support VMX
  1789. * should throw a #GP(0) when VMX capability MSRs are read.
  1790. */
  1791. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  1792. return 1;
  1793. }
  1794. switch (msr_index) {
  1795. case MSR_IA32_FEATURE_CONTROL:
  1796. *pdata = 0;
  1797. break;
  1798. case MSR_IA32_VMX_BASIC:
  1799. /*
  1800. * This MSR reports some information about VMX support. We
  1801. * should return information about the VMX we emulate for the
  1802. * guest, and the VMCS structure we give it - not about the
  1803. * VMX support of the underlying hardware.
  1804. */
  1805. *pdata = VMCS12_REVISION |
  1806. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  1807. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  1808. break;
  1809. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  1810. case MSR_IA32_VMX_PINBASED_CTLS:
  1811. *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
  1812. nested_vmx_pinbased_ctls_high);
  1813. break;
  1814. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  1815. case MSR_IA32_VMX_PROCBASED_CTLS:
  1816. *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
  1817. nested_vmx_procbased_ctls_high);
  1818. break;
  1819. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  1820. case MSR_IA32_VMX_EXIT_CTLS:
  1821. *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
  1822. nested_vmx_exit_ctls_high);
  1823. break;
  1824. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  1825. case MSR_IA32_VMX_ENTRY_CTLS:
  1826. *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
  1827. nested_vmx_entry_ctls_high);
  1828. break;
  1829. case MSR_IA32_VMX_MISC:
  1830. *pdata = 0;
  1831. break;
  1832. /*
  1833. * These MSRs specify bits which the guest must keep fixed (on or off)
  1834. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  1835. * We picked the standard core2 setting.
  1836. */
  1837. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  1838. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  1839. case MSR_IA32_VMX_CR0_FIXED0:
  1840. *pdata = VMXON_CR0_ALWAYSON;
  1841. break;
  1842. case MSR_IA32_VMX_CR0_FIXED1:
  1843. *pdata = -1ULL;
  1844. break;
  1845. case MSR_IA32_VMX_CR4_FIXED0:
  1846. *pdata = VMXON_CR4_ALWAYSON;
  1847. break;
  1848. case MSR_IA32_VMX_CR4_FIXED1:
  1849. *pdata = -1ULL;
  1850. break;
  1851. case MSR_IA32_VMX_VMCS_ENUM:
  1852. *pdata = 0x1f;
  1853. break;
  1854. case MSR_IA32_VMX_PROCBASED_CTLS2:
  1855. *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
  1856. nested_vmx_secondary_ctls_high);
  1857. break;
  1858. case MSR_IA32_VMX_EPT_VPID_CAP:
  1859. /* Currently, no nested ept or nested vpid */
  1860. *pdata = 0;
  1861. break;
  1862. default:
  1863. return 0;
  1864. }
  1865. return 1;
  1866. }
  1867. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1868. {
  1869. if (!nested_vmx_allowed(vcpu))
  1870. return 0;
  1871. if (msr_index == MSR_IA32_FEATURE_CONTROL)
  1872. /* TODO: the right thing. */
  1873. return 1;
  1874. /*
  1875. * No need to treat VMX capability MSRs specially: If we don't handle
  1876. * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
  1877. */
  1878. return 0;
  1879. }
  1880. /*
  1881. * Reads an msr value (of 'msr_index') into 'pdata'.
  1882. * Returns 0 on success, non-0 otherwise.
  1883. * Assumes vcpu_load() was already called.
  1884. */
  1885. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1886. {
  1887. u64 data;
  1888. struct shared_msr_entry *msr;
  1889. if (!pdata) {
  1890. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  1891. return -EINVAL;
  1892. }
  1893. switch (msr_index) {
  1894. #ifdef CONFIG_X86_64
  1895. case MSR_FS_BASE:
  1896. data = vmcs_readl(GUEST_FS_BASE);
  1897. break;
  1898. case MSR_GS_BASE:
  1899. data = vmcs_readl(GUEST_GS_BASE);
  1900. break;
  1901. case MSR_KERNEL_GS_BASE:
  1902. vmx_load_host_state(to_vmx(vcpu));
  1903. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  1904. break;
  1905. #endif
  1906. case MSR_EFER:
  1907. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1908. case MSR_IA32_TSC:
  1909. data = guest_read_tsc();
  1910. break;
  1911. case MSR_IA32_SYSENTER_CS:
  1912. data = vmcs_read32(GUEST_SYSENTER_CS);
  1913. break;
  1914. case MSR_IA32_SYSENTER_EIP:
  1915. data = vmcs_readl(GUEST_SYSENTER_EIP);
  1916. break;
  1917. case MSR_IA32_SYSENTER_ESP:
  1918. data = vmcs_readl(GUEST_SYSENTER_ESP);
  1919. break;
  1920. case MSR_TSC_AUX:
  1921. if (!to_vmx(vcpu)->rdtscp_enabled)
  1922. return 1;
  1923. /* Otherwise falls through */
  1924. default:
  1925. if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
  1926. return 0;
  1927. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  1928. if (msr) {
  1929. data = msr->data;
  1930. break;
  1931. }
  1932. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1933. }
  1934. *pdata = data;
  1935. return 0;
  1936. }
  1937. /*
  1938. * Writes msr value into into the appropriate "register".
  1939. * Returns 0 on success, non-0 otherwise.
  1940. * Assumes vcpu_load() was already called.
  1941. */
  1942. static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1943. {
  1944. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1945. struct shared_msr_entry *msr;
  1946. int ret = 0;
  1947. switch (msr_index) {
  1948. case MSR_EFER:
  1949. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1950. break;
  1951. #ifdef CONFIG_X86_64
  1952. case MSR_FS_BASE:
  1953. vmx_segment_cache_clear(vmx);
  1954. vmcs_writel(GUEST_FS_BASE, data);
  1955. break;
  1956. case MSR_GS_BASE:
  1957. vmx_segment_cache_clear(vmx);
  1958. vmcs_writel(GUEST_GS_BASE, data);
  1959. break;
  1960. case MSR_KERNEL_GS_BASE:
  1961. vmx_load_host_state(vmx);
  1962. vmx->msr_guest_kernel_gs_base = data;
  1963. break;
  1964. #endif
  1965. case MSR_IA32_SYSENTER_CS:
  1966. vmcs_write32(GUEST_SYSENTER_CS, data);
  1967. break;
  1968. case MSR_IA32_SYSENTER_EIP:
  1969. vmcs_writel(GUEST_SYSENTER_EIP, data);
  1970. break;
  1971. case MSR_IA32_SYSENTER_ESP:
  1972. vmcs_writel(GUEST_SYSENTER_ESP, data);
  1973. break;
  1974. case MSR_IA32_TSC:
  1975. kvm_write_tsc(vcpu, data);
  1976. break;
  1977. case MSR_IA32_CR_PAT:
  1978. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  1979. vmcs_write64(GUEST_IA32_PAT, data);
  1980. vcpu->arch.pat = data;
  1981. break;
  1982. }
  1983. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1984. break;
  1985. case MSR_TSC_AUX:
  1986. if (!vmx->rdtscp_enabled)
  1987. return 1;
  1988. /* Check reserved bit, higher 32 bits should be zero */
  1989. if ((data >> 32) != 0)
  1990. return 1;
  1991. /* Otherwise falls through */
  1992. default:
  1993. if (vmx_set_vmx_msr(vcpu, msr_index, data))
  1994. break;
  1995. msr = find_msr_entry(vmx, msr_index);
  1996. if (msr) {
  1997. msr->data = data;
  1998. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  1999. preempt_disable();
  2000. kvm_set_shared_msr(msr->index, msr->data,
  2001. msr->mask);
  2002. preempt_enable();
  2003. }
  2004. break;
  2005. }
  2006. ret = kvm_set_msr_common(vcpu, msr_index, data);
  2007. }
  2008. return ret;
  2009. }
  2010. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  2011. {
  2012. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  2013. switch (reg) {
  2014. case VCPU_REGS_RSP:
  2015. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  2016. break;
  2017. case VCPU_REGS_RIP:
  2018. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  2019. break;
  2020. case VCPU_EXREG_PDPTR:
  2021. if (enable_ept)
  2022. ept_save_pdptrs(vcpu);
  2023. break;
  2024. default:
  2025. break;
  2026. }
  2027. }
  2028. static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  2029. {
  2030. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  2031. vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
  2032. else
  2033. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  2034. update_exception_bitmap(vcpu);
  2035. }
  2036. static __init int cpu_has_kvm_support(void)
  2037. {
  2038. return cpu_has_vmx();
  2039. }
  2040. static __init int vmx_disabled_by_bios(void)
  2041. {
  2042. u64 msr;
  2043. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  2044. if (msr & FEATURE_CONTROL_LOCKED) {
  2045. /* launched w/ TXT and VMX disabled */
  2046. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2047. && tboot_enabled())
  2048. return 1;
  2049. /* launched w/o TXT and VMX only enabled w/ TXT */
  2050. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2051. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2052. && !tboot_enabled()) {
  2053. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  2054. "activate TXT before enabling KVM\n");
  2055. return 1;
  2056. }
  2057. /* launched w/o TXT and VMX disabled */
  2058. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2059. && !tboot_enabled())
  2060. return 1;
  2061. }
  2062. return 0;
  2063. }
  2064. static void kvm_cpu_vmxon(u64 addr)
  2065. {
  2066. asm volatile (ASM_VMX_VMXON_RAX
  2067. : : "a"(&addr), "m"(addr)
  2068. : "memory", "cc");
  2069. }
  2070. static int hardware_enable(void *garbage)
  2071. {
  2072. int cpu = raw_smp_processor_id();
  2073. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  2074. u64 old, test_bits;
  2075. if (read_cr4() & X86_CR4_VMXE)
  2076. return -EBUSY;
  2077. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  2078. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  2079. test_bits = FEATURE_CONTROL_LOCKED;
  2080. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  2081. if (tboot_enabled())
  2082. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  2083. if ((old & test_bits) != test_bits) {
  2084. /* enable and lock */
  2085. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  2086. }
  2087. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  2088. if (vmm_exclusive) {
  2089. kvm_cpu_vmxon(phys_addr);
  2090. ept_sync_global();
  2091. }
  2092. store_gdt(&__get_cpu_var(host_gdt));
  2093. return 0;
  2094. }
  2095. static void vmclear_local_loaded_vmcss(void)
  2096. {
  2097. int cpu = raw_smp_processor_id();
  2098. struct loaded_vmcs *v, *n;
  2099. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  2100. loaded_vmcss_on_cpu_link)
  2101. __loaded_vmcs_clear(v);
  2102. }
  2103. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  2104. * tricks.
  2105. */
  2106. static void kvm_cpu_vmxoff(void)
  2107. {
  2108. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  2109. }
  2110. static void hardware_disable(void *garbage)
  2111. {
  2112. if (vmm_exclusive) {
  2113. vmclear_local_loaded_vmcss();
  2114. kvm_cpu_vmxoff();
  2115. }
  2116. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  2117. }
  2118. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  2119. u32 msr, u32 *result)
  2120. {
  2121. u32 vmx_msr_low, vmx_msr_high;
  2122. u32 ctl = ctl_min | ctl_opt;
  2123. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2124. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  2125. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  2126. /* Ensure minimum (required) set of control bits are supported. */
  2127. if (ctl_min & ~ctl)
  2128. return -EIO;
  2129. *result = ctl;
  2130. return 0;
  2131. }
  2132. static __init bool allow_1_setting(u32 msr, u32 ctl)
  2133. {
  2134. u32 vmx_msr_low, vmx_msr_high;
  2135. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2136. return vmx_msr_high & ctl;
  2137. }
  2138. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  2139. {
  2140. u32 vmx_msr_low, vmx_msr_high;
  2141. u32 min, opt, min2, opt2;
  2142. u32 _pin_based_exec_control = 0;
  2143. u32 _cpu_based_exec_control = 0;
  2144. u32 _cpu_based_2nd_exec_control = 0;
  2145. u32 _vmexit_control = 0;
  2146. u32 _vmentry_control = 0;
  2147. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  2148. opt = PIN_BASED_VIRTUAL_NMIS;
  2149. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  2150. &_pin_based_exec_control) < 0)
  2151. return -EIO;
  2152. min = CPU_BASED_HLT_EXITING |
  2153. #ifdef CONFIG_X86_64
  2154. CPU_BASED_CR8_LOAD_EXITING |
  2155. CPU_BASED_CR8_STORE_EXITING |
  2156. #endif
  2157. CPU_BASED_CR3_LOAD_EXITING |
  2158. CPU_BASED_CR3_STORE_EXITING |
  2159. CPU_BASED_USE_IO_BITMAPS |
  2160. CPU_BASED_MOV_DR_EXITING |
  2161. CPU_BASED_USE_TSC_OFFSETING |
  2162. CPU_BASED_MWAIT_EXITING |
  2163. CPU_BASED_MONITOR_EXITING |
  2164. CPU_BASED_INVLPG_EXITING |
  2165. CPU_BASED_RDPMC_EXITING;
  2166. opt = CPU_BASED_TPR_SHADOW |
  2167. CPU_BASED_USE_MSR_BITMAPS |
  2168. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2169. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  2170. &_cpu_based_exec_control) < 0)
  2171. return -EIO;
  2172. #ifdef CONFIG_X86_64
  2173. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2174. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  2175. ~CPU_BASED_CR8_STORE_EXITING;
  2176. #endif
  2177. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  2178. min2 = 0;
  2179. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2180. SECONDARY_EXEC_WBINVD_EXITING |
  2181. SECONDARY_EXEC_ENABLE_VPID |
  2182. SECONDARY_EXEC_ENABLE_EPT |
  2183. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  2184. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  2185. SECONDARY_EXEC_RDTSCP |
  2186. SECONDARY_EXEC_ENABLE_INVPCID;
  2187. if (adjust_vmx_controls(min2, opt2,
  2188. MSR_IA32_VMX_PROCBASED_CTLS2,
  2189. &_cpu_based_2nd_exec_control) < 0)
  2190. return -EIO;
  2191. }
  2192. #ifndef CONFIG_X86_64
  2193. if (!(_cpu_based_2nd_exec_control &
  2194. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  2195. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  2196. #endif
  2197. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  2198. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  2199. enabled */
  2200. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  2201. CPU_BASED_CR3_STORE_EXITING |
  2202. CPU_BASED_INVLPG_EXITING);
  2203. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  2204. vmx_capability.ept, vmx_capability.vpid);
  2205. }
  2206. min = 0;
  2207. #ifdef CONFIG_X86_64
  2208. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  2209. #endif
  2210. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
  2211. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  2212. &_vmexit_control) < 0)
  2213. return -EIO;
  2214. min = 0;
  2215. opt = VM_ENTRY_LOAD_IA32_PAT;
  2216. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  2217. &_vmentry_control) < 0)
  2218. return -EIO;
  2219. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  2220. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  2221. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  2222. return -EIO;
  2223. #ifdef CONFIG_X86_64
  2224. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  2225. if (vmx_msr_high & (1u<<16))
  2226. return -EIO;
  2227. #endif
  2228. /* Require Write-Back (WB) memory type for VMCS accesses. */
  2229. if (((vmx_msr_high >> 18) & 15) != 6)
  2230. return -EIO;
  2231. vmcs_conf->size = vmx_msr_high & 0x1fff;
  2232. vmcs_conf->order = get_order(vmcs_config.size);
  2233. vmcs_conf->revision_id = vmx_msr_low;
  2234. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  2235. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  2236. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  2237. vmcs_conf->vmexit_ctrl = _vmexit_control;
  2238. vmcs_conf->vmentry_ctrl = _vmentry_control;
  2239. cpu_has_load_ia32_efer =
  2240. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2241. VM_ENTRY_LOAD_IA32_EFER)
  2242. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2243. VM_EXIT_LOAD_IA32_EFER);
  2244. cpu_has_load_perf_global_ctrl =
  2245. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2246. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  2247. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2248. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2249. /*
  2250. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  2251. * but due to arrata below it can't be used. Workaround is to use
  2252. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  2253. *
  2254. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  2255. *
  2256. * AAK155 (model 26)
  2257. * AAP115 (model 30)
  2258. * AAT100 (model 37)
  2259. * BC86,AAY89,BD102 (model 44)
  2260. * BA97 (model 46)
  2261. *
  2262. */
  2263. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  2264. switch (boot_cpu_data.x86_model) {
  2265. case 26:
  2266. case 30:
  2267. case 37:
  2268. case 44:
  2269. case 46:
  2270. cpu_has_load_perf_global_ctrl = false;
  2271. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  2272. "does not work properly. Using workaround\n");
  2273. break;
  2274. default:
  2275. break;
  2276. }
  2277. }
  2278. return 0;
  2279. }
  2280. static struct vmcs *alloc_vmcs_cpu(int cpu)
  2281. {
  2282. int node = cpu_to_node(cpu);
  2283. struct page *pages;
  2284. struct vmcs *vmcs;
  2285. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  2286. if (!pages)
  2287. return NULL;
  2288. vmcs = page_address(pages);
  2289. memset(vmcs, 0, vmcs_config.size);
  2290. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  2291. return vmcs;
  2292. }
  2293. static struct vmcs *alloc_vmcs(void)
  2294. {
  2295. return alloc_vmcs_cpu(raw_smp_processor_id());
  2296. }
  2297. static void free_vmcs(struct vmcs *vmcs)
  2298. {
  2299. free_pages((unsigned long)vmcs, vmcs_config.order);
  2300. }
  2301. /*
  2302. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  2303. */
  2304. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  2305. {
  2306. if (!loaded_vmcs->vmcs)
  2307. return;
  2308. loaded_vmcs_clear(loaded_vmcs);
  2309. free_vmcs(loaded_vmcs->vmcs);
  2310. loaded_vmcs->vmcs = NULL;
  2311. }
  2312. static void free_kvm_area(void)
  2313. {
  2314. int cpu;
  2315. for_each_possible_cpu(cpu) {
  2316. free_vmcs(per_cpu(vmxarea, cpu));
  2317. per_cpu(vmxarea, cpu) = NULL;
  2318. }
  2319. }
  2320. static __init int alloc_kvm_area(void)
  2321. {
  2322. int cpu;
  2323. for_each_possible_cpu(cpu) {
  2324. struct vmcs *vmcs;
  2325. vmcs = alloc_vmcs_cpu(cpu);
  2326. if (!vmcs) {
  2327. free_kvm_area();
  2328. return -ENOMEM;
  2329. }
  2330. per_cpu(vmxarea, cpu) = vmcs;
  2331. }
  2332. return 0;
  2333. }
  2334. static __init int hardware_setup(void)
  2335. {
  2336. if (setup_vmcs_config(&vmcs_config) < 0)
  2337. return -EIO;
  2338. if (boot_cpu_has(X86_FEATURE_NX))
  2339. kvm_enable_efer_bits(EFER_NX);
  2340. if (!cpu_has_vmx_vpid())
  2341. enable_vpid = 0;
  2342. if (!cpu_has_vmx_ept() ||
  2343. !cpu_has_vmx_ept_4levels()) {
  2344. enable_ept = 0;
  2345. enable_unrestricted_guest = 0;
  2346. enable_ept_ad_bits = 0;
  2347. }
  2348. if (!cpu_has_vmx_ept_ad_bits())
  2349. enable_ept_ad_bits = 0;
  2350. if (!cpu_has_vmx_unrestricted_guest())
  2351. enable_unrestricted_guest = 0;
  2352. if (!cpu_has_vmx_flexpriority())
  2353. flexpriority_enabled = 0;
  2354. if (!cpu_has_vmx_tpr_shadow())
  2355. kvm_x86_ops->update_cr8_intercept = NULL;
  2356. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  2357. kvm_disable_largepages();
  2358. if (!cpu_has_vmx_ple())
  2359. ple_gap = 0;
  2360. if (nested)
  2361. nested_vmx_setup_ctls_msrs();
  2362. return alloc_kvm_area();
  2363. }
  2364. static __exit void hardware_unsetup(void)
  2365. {
  2366. free_kvm_area();
  2367. }
  2368. static void fix_pmode_dataseg(struct kvm_vcpu *vcpu, int seg, struct kvm_segment *save)
  2369. {
  2370. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2371. struct kvm_segment tmp = *save;
  2372. if (!(vmcs_readl(sf->base) == tmp.base && tmp.s)) {
  2373. tmp.base = vmcs_readl(sf->base);
  2374. tmp.selector = vmcs_read16(sf->selector);
  2375. tmp.s = 1;
  2376. }
  2377. vmx_set_segment(vcpu, &tmp, seg);
  2378. }
  2379. static void enter_pmode(struct kvm_vcpu *vcpu)
  2380. {
  2381. unsigned long flags;
  2382. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2383. vmx->emulation_required = 1;
  2384. vmx->rmode.vm86_active = 0;
  2385. vmx_segment_cache_clear(vmx);
  2386. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2387. flags = vmcs_readl(GUEST_RFLAGS);
  2388. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2389. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2390. vmcs_writel(GUEST_RFLAGS, flags);
  2391. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  2392. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  2393. update_exception_bitmap(vcpu);
  2394. if (emulate_invalid_guest_state)
  2395. return;
  2396. fix_pmode_dataseg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2397. fix_pmode_dataseg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2398. fix_pmode_dataseg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2399. fix_pmode_dataseg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2400. vmx_segment_cache_clear(vmx);
  2401. vmcs_write16(GUEST_SS_SELECTOR, 0);
  2402. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  2403. vmcs_write16(GUEST_CS_SELECTOR,
  2404. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  2405. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  2406. }
  2407. static gva_t rmode_tss_base(struct kvm *kvm)
  2408. {
  2409. if (!kvm->arch.tss_addr) {
  2410. struct kvm_memslots *slots;
  2411. struct kvm_memory_slot *slot;
  2412. gfn_t base_gfn;
  2413. slots = kvm_memslots(kvm);
  2414. slot = id_to_memslot(slots, 0);
  2415. base_gfn = slot->base_gfn + slot->npages - 3;
  2416. return base_gfn << PAGE_SHIFT;
  2417. }
  2418. return kvm->arch.tss_addr;
  2419. }
  2420. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  2421. {
  2422. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2423. vmcs_write16(sf->selector, save->base >> 4);
  2424. vmcs_write32(sf->base, save->base & 0xffff0);
  2425. vmcs_write32(sf->limit, 0xffff);
  2426. vmcs_write32(sf->ar_bytes, 0xf3);
  2427. if (save->base & 0xf)
  2428. printk_once(KERN_WARNING "kvm: segment base is not paragraph"
  2429. " aligned when entering protected mode (seg=%d)",
  2430. seg);
  2431. }
  2432. static void enter_rmode(struct kvm_vcpu *vcpu)
  2433. {
  2434. unsigned long flags;
  2435. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2436. struct kvm_segment var;
  2437. if (enable_unrestricted_guest)
  2438. return;
  2439. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2440. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2441. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2442. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2443. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2444. vmx->emulation_required = 1;
  2445. vmx->rmode.vm86_active = 1;
  2446. /*
  2447. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  2448. * vcpu. Call it here with phys address pointing 16M below 4G.
  2449. */
  2450. if (!vcpu->kvm->arch.tss_addr) {
  2451. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  2452. "called before entering vcpu\n");
  2453. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  2454. vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
  2455. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  2456. }
  2457. vmx_segment_cache_clear(vmx);
  2458. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  2459. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  2460. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2461. flags = vmcs_readl(GUEST_RFLAGS);
  2462. vmx->rmode.save_rflags = flags;
  2463. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2464. vmcs_writel(GUEST_RFLAGS, flags);
  2465. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  2466. update_exception_bitmap(vcpu);
  2467. if (emulate_invalid_guest_state)
  2468. goto continue_rmode;
  2469. vmx_get_segment(vcpu, &var, VCPU_SREG_SS);
  2470. vmx_set_segment(vcpu, &var, VCPU_SREG_SS);
  2471. vmx_get_segment(vcpu, &var, VCPU_SREG_CS);
  2472. vmx_set_segment(vcpu, &var, VCPU_SREG_CS);
  2473. vmx_get_segment(vcpu, &var, VCPU_SREG_ES);
  2474. vmx_set_segment(vcpu, &var, VCPU_SREG_ES);
  2475. vmx_get_segment(vcpu, &var, VCPU_SREG_DS);
  2476. vmx_set_segment(vcpu, &var, VCPU_SREG_DS);
  2477. vmx_get_segment(vcpu, &var, VCPU_SREG_GS);
  2478. vmx_set_segment(vcpu, &var, VCPU_SREG_GS);
  2479. vmx_get_segment(vcpu, &var, VCPU_SREG_FS);
  2480. vmx_set_segment(vcpu, &var, VCPU_SREG_FS);
  2481. continue_rmode:
  2482. kvm_mmu_reset_context(vcpu);
  2483. }
  2484. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  2485. {
  2486. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2487. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  2488. if (!msr)
  2489. return;
  2490. /*
  2491. * Force kernel_gs_base reloading before EFER changes, as control
  2492. * of this msr depends on is_long_mode().
  2493. */
  2494. vmx_load_host_state(to_vmx(vcpu));
  2495. vcpu->arch.efer = efer;
  2496. if (efer & EFER_LMA) {
  2497. vmcs_write32(VM_ENTRY_CONTROLS,
  2498. vmcs_read32(VM_ENTRY_CONTROLS) |
  2499. VM_ENTRY_IA32E_MODE);
  2500. msr->data = efer;
  2501. } else {
  2502. vmcs_write32(VM_ENTRY_CONTROLS,
  2503. vmcs_read32(VM_ENTRY_CONTROLS) &
  2504. ~VM_ENTRY_IA32E_MODE);
  2505. msr->data = efer & ~EFER_LME;
  2506. }
  2507. setup_msrs(vmx);
  2508. }
  2509. #ifdef CONFIG_X86_64
  2510. static void enter_lmode(struct kvm_vcpu *vcpu)
  2511. {
  2512. u32 guest_tr_ar;
  2513. vmx_segment_cache_clear(to_vmx(vcpu));
  2514. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  2515. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  2516. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  2517. __func__);
  2518. vmcs_write32(GUEST_TR_AR_BYTES,
  2519. (guest_tr_ar & ~AR_TYPE_MASK)
  2520. | AR_TYPE_BUSY_64_TSS);
  2521. }
  2522. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  2523. }
  2524. static void exit_lmode(struct kvm_vcpu *vcpu)
  2525. {
  2526. vmcs_write32(VM_ENTRY_CONTROLS,
  2527. vmcs_read32(VM_ENTRY_CONTROLS)
  2528. & ~VM_ENTRY_IA32E_MODE);
  2529. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  2530. }
  2531. #endif
  2532. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  2533. {
  2534. vpid_sync_context(to_vmx(vcpu));
  2535. if (enable_ept) {
  2536. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2537. return;
  2538. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  2539. }
  2540. }
  2541. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  2542. {
  2543. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  2544. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  2545. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  2546. }
  2547. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  2548. {
  2549. if (enable_ept && is_paging(vcpu))
  2550. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  2551. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  2552. }
  2553. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  2554. {
  2555. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  2556. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  2557. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  2558. }
  2559. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  2560. {
  2561. if (!test_bit(VCPU_EXREG_PDPTR,
  2562. (unsigned long *)&vcpu->arch.regs_dirty))
  2563. return;
  2564. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2565. vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
  2566. vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
  2567. vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
  2568. vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
  2569. }
  2570. }
  2571. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  2572. {
  2573. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2574. vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  2575. vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  2576. vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  2577. vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  2578. }
  2579. __set_bit(VCPU_EXREG_PDPTR,
  2580. (unsigned long *)&vcpu->arch.regs_avail);
  2581. __set_bit(VCPU_EXREG_PDPTR,
  2582. (unsigned long *)&vcpu->arch.regs_dirty);
  2583. }
  2584. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  2585. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  2586. unsigned long cr0,
  2587. struct kvm_vcpu *vcpu)
  2588. {
  2589. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  2590. vmx_decache_cr3(vcpu);
  2591. if (!(cr0 & X86_CR0_PG)) {
  2592. /* From paging/starting to nonpaging */
  2593. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  2594. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  2595. (CPU_BASED_CR3_LOAD_EXITING |
  2596. CPU_BASED_CR3_STORE_EXITING));
  2597. vcpu->arch.cr0 = cr0;
  2598. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  2599. } else if (!is_paging(vcpu)) {
  2600. /* From nonpaging to paging */
  2601. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  2602. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  2603. ~(CPU_BASED_CR3_LOAD_EXITING |
  2604. CPU_BASED_CR3_STORE_EXITING));
  2605. vcpu->arch.cr0 = cr0;
  2606. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  2607. }
  2608. if (!(cr0 & X86_CR0_WP))
  2609. *hw_cr0 &= ~X86_CR0_WP;
  2610. }
  2611. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  2612. {
  2613. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2614. unsigned long hw_cr0;
  2615. if (enable_unrestricted_guest)
  2616. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
  2617. | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  2618. else
  2619. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
  2620. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  2621. enter_pmode(vcpu);
  2622. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  2623. enter_rmode(vcpu);
  2624. #ifdef CONFIG_X86_64
  2625. if (vcpu->arch.efer & EFER_LME) {
  2626. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  2627. enter_lmode(vcpu);
  2628. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  2629. exit_lmode(vcpu);
  2630. }
  2631. #endif
  2632. if (enable_ept)
  2633. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  2634. if (!vcpu->fpu_active)
  2635. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  2636. vmcs_writel(CR0_READ_SHADOW, cr0);
  2637. vmcs_writel(GUEST_CR0, hw_cr0);
  2638. vcpu->arch.cr0 = cr0;
  2639. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2640. }
  2641. static u64 construct_eptp(unsigned long root_hpa)
  2642. {
  2643. u64 eptp;
  2644. /* TODO write the value reading from MSR */
  2645. eptp = VMX_EPT_DEFAULT_MT |
  2646. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  2647. if (enable_ept_ad_bits)
  2648. eptp |= VMX_EPT_AD_ENABLE_BIT;
  2649. eptp |= (root_hpa & PAGE_MASK);
  2650. return eptp;
  2651. }
  2652. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  2653. {
  2654. unsigned long guest_cr3;
  2655. u64 eptp;
  2656. guest_cr3 = cr3;
  2657. if (enable_ept) {
  2658. eptp = construct_eptp(cr3);
  2659. vmcs_write64(EPT_POINTER, eptp);
  2660. guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
  2661. vcpu->kvm->arch.ept_identity_map_addr;
  2662. ept_load_pdptrs(vcpu);
  2663. }
  2664. vmx_flush_tlb(vcpu);
  2665. vmcs_writel(GUEST_CR3, guest_cr3);
  2666. }
  2667. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  2668. {
  2669. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  2670. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  2671. if (cr4 & X86_CR4_VMXE) {
  2672. /*
  2673. * To use VMXON (and later other VMX instructions), a guest
  2674. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  2675. * So basically the check on whether to allow nested VMX
  2676. * is here.
  2677. */
  2678. if (!nested_vmx_allowed(vcpu))
  2679. return 1;
  2680. } else if (to_vmx(vcpu)->nested.vmxon)
  2681. return 1;
  2682. vcpu->arch.cr4 = cr4;
  2683. if (enable_ept) {
  2684. if (!is_paging(vcpu)) {
  2685. hw_cr4 &= ~X86_CR4_PAE;
  2686. hw_cr4 |= X86_CR4_PSE;
  2687. } else if (!(cr4 & X86_CR4_PAE)) {
  2688. hw_cr4 &= ~X86_CR4_PAE;
  2689. }
  2690. }
  2691. vmcs_writel(CR4_READ_SHADOW, cr4);
  2692. vmcs_writel(GUEST_CR4, hw_cr4);
  2693. return 0;
  2694. }
  2695. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  2696. struct kvm_segment *var, int seg)
  2697. {
  2698. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2699. u32 ar;
  2700. if (vmx->rmode.vm86_active
  2701. && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
  2702. || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
  2703. || seg == VCPU_SREG_GS)) {
  2704. *var = vmx->rmode.segs[seg];
  2705. if (seg == VCPU_SREG_TR
  2706. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  2707. return;
  2708. var->base = vmx_read_guest_seg_base(vmx, seg);
  2709. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  2710. return;
  2711. }
  2712. var->base = vmx_read_guest_seg_base(vmx, seg);
  2713. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  2714. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  2715. ar = vmx_read_guest_seg_ar(vmx, seg);
  2716. if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
  2717. ar = 0;
  2718. var->type = ar & 15;
  2719. var->s = (ar >> 4) & 1;
  2720. var->dpl = (ar >> 5) & 3;
  2721. var->present = (ar >> 7) & 1;
  2722. var->avl = (ar >> 12) & 1;
  2723. var->l = (ar >> 13) & 1;
  2724. var->db = (ar >> 14) & 1;
  2725. var->g = (ar >> 15) & 1;
  2726. var->unusable = (ar >> 16) & 1;
  2727. }
  2728. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2729. {
  2730. struct kvm_segment s;
  2731. if (to_vmx(vcpu)->rmode.vm86_active) {
  2732. vmx_get_segment(vcpu, &s, seg);
  2733. return s.base;
  2734. }
  2735. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  2736. }
  2737. static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
  2738. {
  2739. if (!is_protmode(vcpu))
  2740. return 0;
  2741. if (!is_long_mode(vcpu)
  2742. && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
  2743. return 3;
  2744. return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
  2745. }
  2746. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  2747. {
  2748. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2749. /*
  2750. * If we enter real mode with cs.sel & 3 != 0, the normal CPL calculations
  2751. * fail; use the cache instead.
  2752. */
  2753. if (unlikely(vmx->emulation_required && emulate_invalid_guest_state)) {
  2754. return vmx->cpl;
  2755. }
  2756. if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
  2757. __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2758. vmx->cpl = __vmx_get_cpl(vcpu);
  2759. }
  2760. return vmx->cpl;
  2761. }
  2762. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  2763. {
  2764. u32 ar;
  2765. if (var->unusable || !var->present)
  2766. ar = 1 << 16;
  2767. else {
  2768. ar = var->type & 15;
  2769. ar |= (var->s & 1) << 4;
  2770. ar |= (var->dpl & 3) << 5;
  2771. ar |= (var->present & 1) << 7;
  2772. ar |= (var->avl & 1) << 12;
  2773. ar |= (var->l & 1) << 13;
  2774. ar |= (var->db & 1) << 14;
  2775. ar |= (var->g & 1) << 15;
  2776. }
  2777. return ar;
  2778. }
  2779. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  2780. struct kvm_segment *var, int seg)
  2781. {
  2782. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2783. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2784. u32 ar;
  2785. vmx_segment_cache_clear(vmx);
  2786. if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
  2787. vmcs_write16(sf->selector, var->selector);
  2788. vmx->rmode.segs[VCPU_SREG_TR] = *var;
  2789. return;
  2790. }
  2791. vmcs_writel(sf->base, var->base);
  2792. vmcs_write32(sf->limit, var->limit);
  2793. vmcs_write16(sf->selector, var->selector);
  2794. if (vmx->rmode.vm86_active && var->s) {
  2795. vmx->rmode.segs[seg] = *var;
  2796. /*
  2797. * Hack real-mode segments into vm86 compatibility.
  2798. */
  2799. if (var->base == 0xffff0000 && var->selector == 0xf000)
  2800. vmcs_writel(sf->base, 0xf0000);
  2801. ar = 0xf3;
  2802. } else
  2803. ar = vmx_segment_access_rights(var);
  2804. /*
  2805. * Fix the "Accessed" bit in AR field of segment registers for older
  2806. * qemu binaries.
  2807. * IA32 arch specifies that at the time of processor reset the
  2808. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  2809. * is setting it to 0 in the userland code. This causes invalid guest
  2810. * state vmexit when "unrestricted guest" mode is turned on.
  2811. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  2812. * tree. Newer qemu binaries with that qemu fix would not need this
  2813. * kvm hack.
  2814. */
  2815. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  2816. ar |= 0x1; /* Accessed */
  2817. vmcs_write32(sf->ar_bytes, ar);
  2818. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2819. /*
  2820. * Fix segments for real mode guest in hosts that don't have
  2821. * "unrestricted_mode" or it was disabled.
  2822. * This is done to allow migration of the guests from hosts with
  2823. * unrestricted guest like Westmere to older host that don't have
  2824. * unrestricted guest like Nehelem.
  2825. */
  2826. if (!enable_unrestricted_guest && vmx->rmode.vm86_active) {
  2827. switch (seg) {
  2828. case VCPU_SREG_CS:
  2829. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  2830. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  2831. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  2832. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  2833. vmcs_write16(GUEST_CS_SELECTOR,
  2834. vmcs_readl(GUEST_CS_BASE) >> 4);
  2835. break;
  2836. case VCPU_SREG_ES:
  2837. case VCPU_SREG_DS:
  2838. case VCPU_SREG_GS:
  2839. case VCPU_SREG_FS:
  2840. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  2841. break;
  2842. case VCPU_SREG_SS:
  2843. vmcs_write16(GUEST_SS_SELECTOR,
  2844. vmcs_readl(GUEST_SS_BASE) >> 4);
  2845. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  2846. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  2847. break;
  2848. }
  2849. }
  2850. }
  2851. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2852. {
  2853. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  2854. *db = (ar >> 14) & 1;
  2855. *l = (ar >> 13) & 1;
  2856. }
  2857. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2858. {
  2859. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  2860. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  2861. }
  2862. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2863. {
  2864. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  2865. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  2866. }
  2867. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2868. {
  2869. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  2870. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  2871. }
  2872. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2873. {
  2874. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  2875. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  2876. }
  2877. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2878. {
  2879. struct kvm_segment var;
  2880. u32 ar;
  2881. vmx_get_segment(vcpu, &var, seg);
  2882. ar = vmx_segment_access_rights(&var);
  2883. if (var.base != (var.selector << 4))
  2884. return false;
  2885. if (var.limit < 0xffff)
  2886. return false;
  2887. if ((ar | (3 << AR_DPL_SHIFT)) != 0xf3)
  2888. return false;
  2889. return true;
  2890. }
  2891. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  2892. {
  2893. struct kvm_segment cs;
  2894. unsigned int cs_rpl;
  2895. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2896. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  2897. if (cs.unusable)
  2898. return false;
  2899. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  2900. return false;
  2901. if (!cs.s)
  2902. return false;
  2903. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  2904. if (cs.dpl > cs_rpl)
  2905. return false;
  2906. } else {
  2907. if (cs.dpl != cs_rpl)
  2908. return false;
  2909. }
  2910. if (!cs.present)
  2911. return false;
  2912. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  2913. return true;
  2914. }
  2915. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  2916. {
  2917. struct kvm_segment ss;
  2918. unsigned int ss_rpl;
  2919. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2920. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  2921. if (ss.unusable)
  2922. return true;
  2923. if (ss.type != 3 && ss.type != 7)
  2924. return false;
  2925. if (!ss.s)
  2926. return false;
  2927. if (ss.dpl != ss_rpl) /* DPL != RPL */
  2928. return false;
  2929. if (!ss.present)
  2930. return false;
  2931. return true;
  2932. }
  2933. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2934. {
  2935. struct kvm_segment var;
  2936. unsigned int rpl;
  2937. vmx_get_segment(vcpu, &var, seg);
  2938. rpl = var.selector & SELECTOR_RPL_MASK;
  2939. if (var.unusable)
  2940. return true;
  2941. if (!var.s)
  2942. return false;
  2943. if (!var.present)
  2944. return false;
  2945. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  2946. if (var.dpl < rpl) /* DPL < RPL */
  2947. return false;
  2948. }
  2949. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  2950. * rights flags
  2951. */
  2952. return true;
  2953. }
  2954. static bool tr_valid(struct kvm_vcpu *vcpu)
  2955. {
  2956. struct kvm_segment tr;
  2957. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  2958. if (tr.unusable)
  2959. return false;
  2960. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2961. return false;
  2962. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  2963. return false;
  2964. if (!tr.present)
  2965. return false;
  2966. return true;
  2967. }
  2968. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  2969. {
  2970. struct kvm_segment ldtr;
  2971. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  2972. if (ldtr.unusable)
  2973. return true;
  2974. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2975. return false;
  2976. if (ldtr.type != 2)
  2977. return false;
  2978. if (!ldtr.present)
  2979. return false;
  2980. return true;
  2981. }
  2982. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  2983. {
  2984. struct kvm_segment cs, ss;
  2985. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2986. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2987. return ((cs.selector & SELECTOR_RPL_MASK) ==
  2988. (ss.selector & SELECTOR_RPL_MASK));
  2989. }
  2990. /*
  2991. * Check if guest state is valid. Returns true if valid, false if
  2992. * not.
  2993. * We assume that registers are always usable
  2994. */
  2995. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  2996. {
  2997. /* real mode guest state checks */
  2998. if (!is_protmode(vcpu)) {
  2999. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  3000. return false;
  3001. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  3002. return false;
  3003. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  3004. return false;
  3005. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  3006. return false;
  3007. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  3008. return false;
  3009. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  3010. return false;
  3011. } else {
  3012. /* protected mode guest state checks */
  3013. if (!cs_ss_rpl_check(vcpu))
  3014. return false;
  3015. if (!code_segment_valid(vcpu))
  3016. return false;
  3017. if (!stack_segment_valid(vcpu))
  3018. return false;
  3019. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  3020. return false;
  3021. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  3022. return false;
  3023. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  3024. return false;
  3025. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  3026. return false;
  3027. if (!tr_valid(vcpu))
  3028. return false;
  3029. if (!ldtr_valid(vcpu))
  3030. return false;
  3031. }
  3032. /* TODO:
  3033. * - Add checks on RIP
  3034. * - Add checks on RFLAGS
  3035. */
  3036. return true;
  3037. }
  3038. static int init_rmode_tss(struct kvm *kvm)
  3039. {
  3040. gfn_t fn;
  3041. u16 data = 0;
  3042. int r, idx, ret = 0;
  3043. idx = srcu_read_lock(&kvm->srcu);
  3044. fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  3045. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3046. if (r < 0)
  3047. goto out;
  3048. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  3049. r = kvm_write_guest_page(kvm, fn++, &data,
  3050. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  3051. if (r < 0)
  3052. goto out;
  3053. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  3054. if (r < 0)
  3055. goto out;
  3056. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3057. if (r < 0)
  3058. goto out;
  3059. data = ~0;
  3060. r = kvm_write_guest_page(kvm, fn, &data,
  3061. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  3062. sizeof(u8));
  3063. if (r < 0)
  3064. goto out;
  3065. ret = 1;
  3066. out:
  3067. srcu_read_unlock(&kvm->srcu, idx);
  3068. return ret;
  3069. }
  3070. static int init_rmode_identity_map(struct kvm *kvm)
  3071. {
  3072. int i, idx, r, ret;
  3073. pfn_t identity_map_pfn;
  3074. u32 tmp;
  3075. if (!enable_ept)
  3076. return 1;
  3077. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  3078. printk(KERN_ERR "EPT: identity-mapping pagetable "
  3079. "haven't been allocated!\n");
  3080. return 0;
  3081. }
  3082. if (likely(kvm->arch.ept_identity_pagetable_done))
  3083. return 1;
  3084. ret = 0;
  3085. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  3086. idx = srcu_read_lock(&kvm->srcu);
  3087. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  3088. if (r < 0)
  3089. goto out;
  3090. /* Set up identity-mapping pagetable for EPT in real mode */
  3091. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  3092. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  3093. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  3094. r = kvm_write_guest_page(kvm, identity_map_pfn,
  3095. &tmp, i * sizeof(tmp), sizeof(tmp));
  3096. if (r < 0)
  3097. goto out;
  3098. }
  3099. kvm->arch.ept_identity_pagetable_done = true;
  3100. ret = 1;
  3101. out:
  3102. srcu_read_unlock(&kvm->srcu, idx);
  3103. return ret;
  3104. }
  3105. static void seg_setup(int seg)
  3106. {
  3107. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3108. unsigned int ar;
  3109. vmcs_write16(sf->selector, 0);
  3110. vmcs_writel(sf->base, 0);
  3111. vmcs_write32(sf->limit, 0xffff);
  3112. if (enable_unrestricted_guest) {
  3113. ar = 0x93;
  3114. if (seg == VCPU_SREG_CS)
  3115. ar |= 0x08; /* code segment */
  3116. } else
  3117. ar = 0xf3;
  3118. vmcs_write32(sf->ar_bytes, ar);
  3119. }
  3120. static int alloc_apic_access_page(struct kvm *kvm)
  3121. {
  3122. struct kvm_userspace_memory_region kvm_userspace_mem;
  3123. int r = 0;
  3124. mutex_lock(&kvm->slots_lock);
  3125. if (kvm->arch.apic_access_page)
  3126. goto out;
  3127. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  3128. kvm_userspace_mem.flags = 0;
  3129. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  3130. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3131. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  3132. if (r)
  3133. goto out;
  3134. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  3135. out:
  3136. mutex_unlock(&kvm->slots_lock);
  3137. return r;
  3138. }
  3139. static int alloc_identity_pagetable(struct kvm *kvm)
  3140. {
  3141. struct kvm_userspace_memory_region kvm_userspace_mem;
  3142. int r = 0;
  3143. mutex_lock(&kvm->slots_lock);
  3144. if (kvm->arch.ept_identity_pagetable)
  3145. goto out;
  3146. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  3147. kvm_userspace_mem.flags = 0;
  3148. kvm_userspace_mem.guest_phys_addr =
  3149. kvm->arch.ept_identity_map_addr;
  3150. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3151. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  3152. if (r)
  3153. goto out;
  3154. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  3155. kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
  3156. out:
  3157. mutex_unlock(&kvm->slots_lock);
  3158. return r;
  3159. }
  3160. static void allocate_vpid(struct vcpu_vmx *vmx)
  3161. {
  3162. int vpid;
  3163. vmx->vpid = 0;
  3164. if (!enable_vpid)
  3165. return;
  3166. spin_lock(&vmx_vpid_lock);
  3167. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  3168. if (vpid < VMX_NR_VPIDS) {
  3169. vmx->vpid = vpid;
  3170. __set_bit(vpid, vmx_vpid_bitmap);
  3171. }
  3172. spin_unlock(&vmx_vpid_lock);
  3173. }
  3174. static void free_vpid(struct vcpu_vmx *vmx)
  3175. {
  3176. if (!enable_vpid)
  3177. return;
  3178. spin_lock(&vmx_vpid_lock);
  3179. if (vmx->vpid != 0)
  3180. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  3181. spin_unlock(&vmx_vpid_lock);
  3182. }
  3183. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
  3184. {
  3185. int f = sizeof(unsigned long);
  3186. if (!cpu_has_vmx_msr_bitmap())
  3187. return;
  3188. /*
  3189. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3190. * have the write-low and read-high bitmap offsets the wrong way round.
  3191. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3192. */
  3193. if (msr <= 0x1fff) {
  3194. __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
  3195. __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
  3196. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3197. msr &= 0x1fff;
  3198. __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
  3199. __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
  3200. }
  3201. }
  3202. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  3203. {
  3204. if (!longmode_only)
  3205. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
  3206. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
  3207. }
  3208. /*
  3209. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  3210. * will not change in the lifetime of the guest.
  3211. * Note that host-state that does change is set elsewhere. E.g., host-state
  3212. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  3213. */
  3214. static void vmx_set_constant_host_state(void)
  3215. {
  3216. u32 low32, high32;
  3217. unsigned long tmpl;
  3218. struct desc_ptr dt;
  3219. vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
  3220. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  3221. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  3222. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  3223. #ifdef CONFIG_X86_64
  3224. /*
  3225. * Load null selectors, so we can avoid reloading them in
  3226. * __vmx_load_host_state(), in case userspace uses the null selectors
  3227. * too (the expected case).
  3228. */
  3229. vmcs_write16(HOST_DS_SELECTOR, 0);
  3230. vmcs_write16(HOST_ES_SELECTOR, 0);
  3231. #else
  3232. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3233. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3234. #endif
  3235. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3236. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  3237. native_store_idt(&dt);
  3238. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  3239. asm("mov $.Lkvm_vmx_return, %0" : "=r"(tmpl));
  3240. vmcs_writel(HOST_RIP, tmpl); /* 22.2.5 */
  3241. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  3242. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  3243. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  3244. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  3245. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  3246. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  3247. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  3248. }
  3249. }
  3250. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  3251. {
  3252. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  3253. if (enable_ept)
  3254. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  3255. if (is_guest_mode(&vmx->vcpu))
  3256. vmx->vcpu.arch.cr4_guest_owned_bits &=
  3257. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  3258. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  3259. }
  3260. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  3261. {
  3262. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  3263. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  3264. exec_control &= ~CPU_BASED_TPR_SHADOW;
  3265. #ifdef CONFIG_X86_64
  3266. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  3267. CPU_BASED_CR8_LOAD_EXITING;
  3268. #endif
  3269. }
  3270. if (!enable_ept)
  3271. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  3272. CPU_BASED_CR3_LOAD_EXITING |
  3273. CPU_BASED_INVLPG_EXITING;
  3274. return exec_control;
  3275. }
  3276. static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
  3277. {
  3278. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  3279. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3280. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3281. if (vmx->vpid == 0)
  3282. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  3283. if (!enable_ept) {
  3284. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  3285. enable_unrestricted_guest = 0;
  3286. /* Enable INVPCID for non-ept guests may cause performance regression. */
  3287. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  3288. }
  3289. if (!enable_unrestricted_guest)
  3290. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3291. if (!ple_gap)
  3292. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  3293. return exec_control;
  3294. }
  3295. static void ept_set_mmio_spte_mask(void)
  3296. {
  3297. /*
  3298. * EPT Misconfigurations can be generated if the value of bits 2:0
  3299. * of an EPT paging-structure entry is 110b (write/execute).
  3300. * Also, magic bits (0xffull << 49) is set to quickly identify mmio
  3301. * spte.
  3302. */
  3303. kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
  3304. }
  3305. /*
  3306. * Sets up the vmcs for emulated real mode.
  3307. */
  3308. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  3309. {
  3310. #ifdef CONFIG_X86_64
  3311. unsigned long a;
  3312. #endif
  3313. int i;
  3314. /* I/O */
  3315. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  3316. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  3317. if (cpu_has_vmx_msr_bitmap())
  3318. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  3319. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  3320. /* Control */
  3321. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  3322. vmcs_config.pin_based_exec_ctrl);
  3323. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  3324. if (cpu_has_secondary_exec_ctrls()) {
  3325. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  3326. vmx_secondary_exec_control(vmx));
  3327. }
  3328. if (ple_gap) {
  3329. vmcs_write32(PLE_GAP, ple_gap);
  3330. vmcs_write32(PLE_WINDOW, ple_window);
  3331. }
  3332. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  3333. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  3334. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  3335. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  3336. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  3337. vmx_set_constant_host_state();
  3338. #ifdef CONFIG_X86_64
  3339. rdmsrl(MSR_FS_BASE, a);
  3340. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  3341. rdmsrl(MSR_GS_BASE, a);
  3342. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  3343. #else
  3344. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  3345. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  3346. #endif
  3347. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  3348. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  3349. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  3350. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  3351. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  3352. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3353. u32 msr_low, msr_high;
  3354. u64 host_pat;
  3355. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  3356. host_pat = msr_low | ((u64) msr_high << 32);
  3357. /* Write the default value follow host pat */
  3358. vmcs_write64(GUEST_IA32_PAT, host_pat);
  3359. /* Keep arch.pat sync with GUEST_IA32_PAT */
  3360. vmx->vcpu.arch.pat = host_pat;
  3361. }
  3362. for (i = 0; i < NR_VMX_MSR; ++i) {
  3363. u32 index = vmx_msr_index[i];
  3364. u32 data_low, data_high;
  3365. int j = vmx->nmsrs;
  3366. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  3367. continue;
  3368. if (wrmsr_safe(index, data_low, data_high) < 0)
  3369. continue;
  3370. vmx->guest_msrs[j].index = i;
  3371. vmx->guest_msrs[j].data = 0;
  3372. vmx->guest_msrs[j].mask = -1ull;
  3373. ++vmx->nmsrs;
  3374. }
  3375. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  3376. /* 22.2.1, 20.8.1 */
  3377. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  3378. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  3379. set_cr4_guest_host_mask(vmx);
  3380. kvm_write_tsc(&vmx->vcpu, 0);
  3381. return 0;
  3382. }
  3383. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  3384. {
  3385. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3386. u64 msr;
  3387. int ret;
  3388. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  3389. vmx->rmode.vm86_active = 0;
  3390. vmx->soft_vnmi_blocked = 0;
  3391. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  3392. kvm_set_cr8(&vmx->vcpu, 0);
  3393. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  3394. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  3395. msr |= MSR_IA32_APICBASE_BSP;
  3396. kvm_set_apic_base(&vmx->vcpu, msr);
  3397. ret = fx_init(&vmx->vcpu);
  3398. if (ret != 0)
  3399. goto out;
  3400. vmx_segment_cache_clear(vmx);
  3401. seg_setup(VCPU_SREG_CS);
  3402. /*
  3403. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  3404. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  3405. */
  3406. if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
  3407. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  3408. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  3409. } else {
  3410. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  3411. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  3412. }
  3413. seg_setup(VCPU_SREG_DS);
  3414. seg_setup(VCPU_SREG_ES);
  3415. seg_setup(VCPU_SREG_FS);
  3416. seg_setup(VCPU_SREG_GS);
  3417. seg_setup(VCPU_SREG_SS);
  3418. vmcs_write16(GUEST_TR_SELECTOR, 0);
  3419. vmcs_writel(GUEST_TR_BASE, 0);
  3420. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  3421. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  3422. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  3423. vmcs_writel(GUEST_LDTR_BASE, 0);
  3424. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  3425. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  3426. vmcs_write32(GUEST_SYSENTER_CS, 0);
  3427. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  3428. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  3429. vmcs_writel(GUEST_RFLAGS, 0x02);
  3430. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  3431. kvm_rip_write(vcpu, 0xfff0);
  3432. else
  3433. kvm_rip_write(vcpu, 0);
  3434. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  3435. vmcs_writel(GUEST_DR7, 0x400);
  3436. vmcs_writel(GUEST_GDTR_BASE, 0);
  3437. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  3438. vmcs_writel(GUEST_IDTR_BASE, 0);
  3439. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  3440. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  3441. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  3442. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  3443. /* Special registers */
  3444. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  3445. setup_msrs(vmx);
  3446. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  3447. if (cpu_has_vmx_tpr_shadow()) {
  3448. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  3449. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  3450. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  3451. __pa(vmx->vcpu.arch.apic->regs));
  3452. vmcs_write32(TPR_THRESHOLD, 0);
  3453. }
  3454. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3455. vmcs_write64(APIC_ACCESS_ADDR,
  3456. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  3457. if (vmx->vpid != 0)
  3458. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  3459. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  3460. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  3461. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  3462. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  3463. vmx_set_cr4(&vmx->vcpu, 0);
  3464. vmx_set_efer(&vmx->vcpu, 0);
  3465. vmx_fpu_activate(&vmx->vcpu);
  3466. update_exception_bitmap(&vmx->vcpu);
  3467. vpid_sync_context(vmx);
  3468. ret = 0;
  3469. /* HACK: Don't enable emulation on guest boot/reset */
  3470. vmx->emulation_required = 0;
  3471. out:
  3472. return ret;
  3473. }
  3474. /*
  3475. * In nested virtualization, check if L1 asked to exit on external interrupts.
  3476. * For most existing hypervisors, this will always return true.
  3477. */
  3478. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  3479. {
  3480. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  3481. PIN_BASED_EXT_INTR_MASK;
  3482. }
  3483. static void enable_irq_window(struct kvm_vcpu *vcpu)
  3484. {
  3485. u32 cpu_based_vm_exec_control;
  3486. if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
  3487. /*
  3488. * We get here if vmx_interrupt_allowed() said we can't
  3489. * inject to L1 now because L2 must run. Ask L2 to exit
  3490. * right after entry, so we can inject to L1 more promptly.
  3491. */
  3492. kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
  3493. return;
  3494. }
  3495. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3496. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  3497. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3498. }
  3499. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  3500. {
  3501. u32 cpu_based_vm_exec_control;
  3502. if (!cpu_has_virtual_nmis()) {
  3503. enable_irq_window(vcpu);
  3504. return;
  3505. }
  3506. if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  3507. enable_irq_window(vcpu);
  3508. return;
  3509. }
  3510. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3511. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  3512. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3513. }
  3514. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  3515. {
  3516. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3517. uint32_t intr;
  3518. int irq = vcpu->arch.interrupt.nr;
  3519. trace_kvm_inj_virq(irq);
  3520. ++vcpu->stat.irq_injections;
  3521. if (vmx->rmode.vm86_active) {
  3522. int inc_eip = 0;
  3523. if (vcpu->arch.interrupt.soft)
  3524. inc_eip = vcpu->arch.event_exit_inst_len;
  3525. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  3526. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  3527. return;
  3528. }
  3529. intr = irq | INTR_INFO_VALID_MASK;
  3530. if (vcpu->arch.interrupt.soft) {
  3531. intr |= INTR_TYPE_SOFT_INTR;
  3532. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  3533. vmx->vcpu.arch.event_exit_inst_len);
  3534. } else
  3535. intr |= INTR_TYPE_EXT_INTR;
  3536. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  3537. }
  3538. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  3539. {
  3540. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3541. if (is_guest_mode(vcpu))
  3542. return;
  3543. if (!cpu_has_virtual_nmis()) {
  3544. /*
  3545. * Tracking the NMI-blocked state in software is built upon
  3546. * finding the next open IRQ window. This, in turn, depends on
  3547. * well-behaving guests: They have to keep IRQs disabled at
  3548. * least as long as the NMI handler runs. Otherwise we may
  3549. * cause NMI nesting, maybe breaking the guest. But as this is
  3550. * highly unlikely, we can live with the residual risk.
  3551. */
  3552. vmx->soft_vnmi_blocked = 1;
  3553. vmx->vnmi_blocked_time = 0;
  3554. }
  3555. ++vcpu->stat.nmi_injections;
  3556. vmx->nmi_known_unmasked = false;
  3557. if (vmx->rmode.vm86_active) {
  3558. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  3559. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  3560. return;
  3561. }
  3562. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  3563. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  3564. }
  3565. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  3566. {
  3567. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  3568. return 0;
  3569. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  3570. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  3571. | GUEST_INTR_STATE_NMI));
  3572. }
  3573. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  3574. {
  3575. if (!cpu_has_virtual_nmis())
  3576. return to_vmx(vcpu)->soft_vnmi_blocked;
  3577. if (to_vmx(vcpu)->nmi_known_unmasked)
  3578. return false;
  3579. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  3580. }
  3581. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  3582. {
  3583. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3584. if (!cpu_has_virtual_nmis()) {
  3585. if (vmx->soft_vnmi_blocked != masked) {
  3586. vmx->soft_vnmi_blocked = masked;
  3587. vmx->vnmi_blocked_time = 0;
  3588. }
  3589. } else {
  3590. vmx->nmi_known_unmasked = !masked;
  3591. if (masked)
  3592. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  3593. GUEST_INTR_STATE_NMI);
  3594. else
  3595. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  3596. GUEST_INTR_STATE_NMI);
  3597. }
  3598. }
  3599. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  3600. {
  3601. if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
  3602. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  3603. if (to_vmx(vcpu)->nested.nested_run_pending ||
  3604. (vmcs12->idt_vectoring_info_field &
  3605. VECTORING_INFO_VALID_MASK))
  3606. return 0;
  3607. nested_vmx_vmexit(vcpu);
  3608. vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
  3609. vmcs12->vm_exit_intr_info = 0;
  3610. /* fall through to normal code, but now in L1, not L2 */
  3611. }
  3612. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  3613. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  3614. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  3615. }
  3616. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  3617. {
  3618. int ret;
  3619. struct kvm_userspace_memory_region tss_mem = {
  3620. .slot = TSS_PRIVATE_MEMSLOT,
  3621. .guest_phys_addr = addr,
  3622. .memory_size = PAGE_SIZE * 3,
  3623. .flags = 0,
  3624. };
  3625. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  3626. if (ret)
  3627. return ret;
  3628. kvm->arch.tss_addr = addr;
  3629. if (!init_rmode_tss(kvm))
  3630. return -ENOMEM;
  3631. return 0;
  3632. }
  3633. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  3634. int vec, u32 err_code)
  3635. {
  3636. /*
  3637. * Instruction with address size override prefix opcode 0x67
  3638. * Cause the #SS fault with 0 error code in VM86 mode.
  3639. */
  3640. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  3641. if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
  3642. return 1;
  3643. /*
  3644. * Forward all other exceptions that are valid in real mode.
  3645. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  3646. * the required debugging infrastructure rework.
  3647. */
  3648. switch (vec) {
  3649. case DB_VECTOR:
  3650. if (vcpu->guest_debug &
  3651. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  3652. return 0;
  3653. kvm_queue_exception(vcpu, vec);
  3654. return 1;
  3655. case BP_VECTOR:
  3656. /*
  3657. * Update instruction length as we may reinject the exception
  3658. * from user space while in guest debugging mode.
  3659. */
  3660. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  3661. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  3662. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  3663. return 0;
  3664. /* fall through */
  3665. case DE_VECTOR:
  3666. case OF_VECTOR:
  3667. case BR_VECTOR:
  3668. case UD_VECTOR:
  3669. case DF_VECTOR:
  3670. case SS_VECTOR:
  3671. case GP_VECTOR:
  3672. case MF_VECTOR:
  3673. kvm_queue_exception(vcpu, vec);
  3674. return 1;
  3675. }
  3676. return 0;
  3677. }
  3678. /*
  3679. * Trigger machine check on the host. We assume all the MSRs are already set up
  3680. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  3681. * We pass a fake environment to the machine check handler because we want
  3682. * the guest to be always treated like user space, no matter what context
  3683. * it used internally.
  3684. */
  3685. static void kvm_machine_check(void)
  3686. {
  3687. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  3688. struct pt_regs regs = {
  3689. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  3690. .flags = X86_EFLAGS_IF,
  3691. };
  3692. do_machine_check(&regs, 0);
  3693. #endif
  3694. }
  3695. static int handle_machine_check(struct kvm_vcpu *vcpu)
  3696. {
  3697. /* already handled by vcpu_run */
  3698. return 1;
  3699. }
  3700. static int handle_exception(struct kvm_vcpu *vcpu)
  3701. {
  3702. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3703. struct kvm_run *kvm_run = vcpu->run;
  3704. u32 intr_info, ex_no, error_code;
  3705. unsigned long cr2, rip, dr6;
  3706. u32 vect_info;
  3707. enum emulation_result er;
  3708. vect_info = vmx->idt_vectoring_info;
  3709. intr_info = vmx->exit_intr_info;
  3710. if (is_machine_check(intr_info))
  3711. return handle_machine_check(vcpu);
  3712. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  3713. !is_page_fault(intr_info)) {
  3714. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3715. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  3716. vcpu->run->internal.ndata = 2;
  3717. vcpu->run->internal.data[0] = vect_info;
  3718. vcpu->run->internal.data[1] = intr_info;
  3719. return 0;
  3720. }
  3721. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  3722. return 1; /* already handled by vmx_vcpu_run() */
  3723. if (is_no_device(intr_info)) {
  3724. vmx_fpu_activate(vcpu);
  3725. return 1;
  3726. }
  3727. if (is_invalid_opcode(intr_info)) {
  3728. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  3729. if (er != EMULATE_DONE)
  3730. kvm_queue_exception(vcpu, UD_VECTOR);
  3731. return 1;
  3732. }
  3733. error_code = 0;
  3734. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  3735. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  3736. if (is_page_fault(intr_info)) {
  3737. /* EPT won't cause page fault directly */
  3738. BUG_ON(enable_ept);
  3739. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  3740. trace_kvm_page_fault(cr2, error_code);
  3741. if (kvm_event_needs_reinjection(vcpu))
  3742. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  3743. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  3744. }
  3745. if (vmx->rmode.vm86_active &&
  3746. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  3747. error_code)) {
  3748. if (vcpu->arch.halt_request) {
  3749. vcpu->arch.halt_request = 0;
  3750. return kvm_emulate_halt(vcpu);
  3751. }
  3752. return 1;
  3753. }
  3754. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  3755. switch (ex_no) {
  3756. case DB_VECTOR:
  3757. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  3758. if (!(vcpu->guest_debug &
  3759. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  3760. vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
  3761. kvm_queue_exception(vcpu, DB_VECTOR);
  3762. return 1;
  3763. }
  3764. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  3765. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  3766. /* fall through */
  3767. case BP_VECTOR:
  3768. /*
  3769. * Update instruction length as we may reinject #BP from
  3770. * user space while in guest debugging mode. Reading it for
  3771. * #DB as well causes no harm, it is not used in that case.
  3772. */
  3773. vmx->vcpu.arch.event_exit_inst_len =
  3774. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  3775. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  3776. rip = kvm_rip_read(vcpu);
  3777. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  3778. kvm_run->debug.arch.exception = ex_no;
  3779. break;
  3780. default:
  3781. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  3782. kvm_run->ex.exception = ex_no;
  3783. kvm_run->ex.error_code = error_code;
  3784. break;
  3785. }
  3786. return 0;
  3787. }
  3788. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  3789. {
  3790. ++vcpu->stat.irq_exits;
  3791. return 1;
  3792. }
  3793. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  3794. {
  3795. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  3796. return 0;
  3797. }
  3798. static int handle_io(struct kvm_vcpu *vcpu)
  3799. {
  3800. unsigned long exit_qualification;
  3801. int size, in, string;
  3802. unsigned port;
  3803. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3804. string = (exit_qualification & 16) != 0;
  3805. in = (exit_qualification & 8) != 0;
  3806. ++vcpu->stat.io_exits;
  3807. if (string || in)
  3808. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3809. port = exit_qualification >> 16;
  3810. size = (exit_qualification & 7) + 1;
  3811. skip_emulated_instruction(vcpu);
  3812. return kvm_fast_pio_out(vcpu, size, port);
  3813. }
  3814. static void
  3815. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  3816. {
  3817. /*
  3818. * Patch in the VMCALL instruction:
  3819. */
  3820. hypercall[0] = 0x0f;
  3821. hypercall[1] = 0x01;
  3822. hypercall[2] = 0xc1;
  3823. }
  3824. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  3825. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  3826. {
  3827. if (to_vmx(vcpu)->nested.vmxon &&
  3828. ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
  3829. return 1;
  3830. if (is_guest_mode(vcpu)) {
  3831. /*
  3832. * We get here when L2 changed cr0 in a way that did not change
  3833. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  3834. * but did change L0 shadowed bits. This can currently happen
  3835. * with the TS bit: L0 may want to leave TS on (for lazy fpu
  3836. * loading) while pretending to allow the guest to change it.
  3837. */
  3838. if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) |
  3839. (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits)))
  3840. return 1;
  3841. vmcs_writel(CR0_READ_SHADOW, val);
  3842. return 0;
  3843. } else
  3844. return kvm_set_cr0(vcpu, val);
  3845. }
  3846. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  3847. {
  3848. if (is_guest_mode(vcpu)) {
  3849. if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) |
  3850. (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits)))
  3851. return 1;
  3852. vmcs_writel(CR4_READ_SHADOW, val);
  3853. return 0;
  3854. } else
  3855. return kvm_set_cr4(vcpu, val);
  3856. }
  3857. /* called to set cr0 as approriate for clts instruction exit. */
  3858. static void handle_clts(struct kvm_vcpu *vcpu)
  3859. {
  3860. if (is_guest_mode(vcpu)) {
  3861. /*
  3862. * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
  3863. * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
  3864. * just pretend it's off (also in arch.cr0 for fpu_activate).
  3865. */
  3866. vmcs_writel(CR0_READ_SHADOW,
  3867. vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
  3868. vcpu->arch.cr0 &= ~X86_CR0_TS;
  3869. } else
  3870. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  3871. }
  3872. static int handle_cr(struct kvm_vcpu *vcpu)
  3873. {
  3874. unsigned long exit_qualification, val;
  3875. int cr;
  3876. int reg;
  3877. int err;
  3878. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3879. cr = exit_qualification & 15;
  3880. reg = (exit_qualification >> 8) & 15;
  3881. switch ((exit_qualification >> 4) & 3) {
  3882. case 0: /* mov to cr */
  3883. val = kvm_register_read(vcpu, reg);
  3884. trace_kvm_cr_write(cr, val);
  3885. switch (cr) {
  3886. case 0:
  3887. err = handle_set_cr0(vcpu, val);
  3888. kvm_complete_insn_gp(vcpu, err);
  3889. return 1;
  3890. case 3:
  3891. err = kvm_set_cr3(vcpu, val);
  3892. kvm_complete_insn_gp(vcpu, err);
  3893. return 1;
  3894. case 4:
  3895. err = handle_set_cr4(vcpu, val);
  3896. kvm_complete_insn_gp(vcpu, err);
  3897. return 1;
  3898. case 8: {
  3899. u8 cr8_prev = kvm_get_cr8(vcpu);
  3900. u8 cr8 = kvm_register_read(vcpu, reg);
  3901. err = kvm_set_cr8(vcpu, cr8);
  3902. kvm_complete_insn_gp(vcpu, err);
  3903. if (irqchip_in_kernel(vcpu->kvm))
  3904. return 1;
  3905. if (cr8_prev <= cr8)
  3906. return 1;
  3907. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  3908. return 0;
  3909. }
  3910. };
  3911. break;
  3912. case 2: /* clts */
  3913. handle_clts(vcpu);
  3914. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  3915. skip_emulated_instruction(vcpu);
  3916. vmx_fpu_activate(vcpu);
  3917. return 1;
  3918. case 1: /*mov from cr*/
  3919. switch (cr) {
  3920. case 3:
  3921. val = kvm_read_cr3(vcpu);
  3922. kvm_register_write(vcpu, reg, val);
  3923. trace_kvm_cr_read(cr, val);
  3924. skip_emulated_instruction(vcpu);
  3925. return 1;
  3926. case 8:
  3927. val = kvm_get_cr8(vcpu);
  3928. kvm_register_write(vcpu, reg, val);
  3929. trace_kvm_cr_read(cr, val);
  3930. skip_emulated_instruction(vcpu);
  3931. return 1;
  3932. }
  3933. break;
  3934. case 3: /* lmsw */
  3935. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  3936. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  3937. kvm_lmsw(vcpu, val);
  3938. skip_emulated_instruction(vcpu);
  3939. return 1;
  3940. default:
  3941. break;
  3942. }
  3943. vcpu->run->exit_reason = 0;
  3944. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  3945. (int)(exit_qualification >> 4) & 3, cr);
  3946. return 0;
  3947. }
  3948. static int handle_dr(struct kvm_vcpu *vcpu)
  3949. {
  3950. unsigned long exit_qualification;
  3951. int dr, reg;
  3952. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  3953. if (!kvm_require_cpl(vcpu, 0))
  3954. return 1;
  3955. dr = vmcs_readl(GUEST_DR7);
  3956. if (dr & DR7_GD) {
  3957. /*
  3958. * As the vm-exit takes precedence over the debug trap, we
  3959. * need to emulate the latter, either for the host or the
  3960. * guest debugging itself.
  3961. */
  3962. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  3963. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  3964. vcpu->run->debug.arch.dr7 = dr;
  3965. vcpu->run->debug.arch.pc =
  3966. vmcs_readl(GUEST_CS_BASE) +
  3967. vmcs_readl(GUEST_RIP);
  3968. vcpu->run->debug.arch.exception = DB_VECTOR;
  3969. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  3970. return 0;
  3971. } else {
  3972. vcpu->arch.dr7 &= ~DR7_GD;
  3973. vcpu->arch.dr6 |= DR6_BD;
  3974. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  3975. kvm_queue_exception(vcpu, DB_VECTOR);
  3976. return 1;
  3977. }
  3978. }
  3979. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3980. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  3981. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  3982. if (exit_qualification & TYPE_MOV_FROM_DR) {
  3983. unsigned long val;
  3984. if (!kvm_get_dr(vcpu, dr, &val))
  3985. kvm_register_write(vcpu, reg, val);
  3986. } else
  3987. kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
  3988. skip_emulated_instruction(vcpu);
  3989. return 1;
  3990. }
  3991. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  3992. {
  3993. vmcs_writel(GUEST_DR7, val);
  3994. }
  3995. static int handle_cpuid(struct kvm_vcpu *vcpu)
  3996. {
  3997. kvm_emulate_cpuid(vcpu);
  3998. return 1;
  3999. }
  4000. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  4001. {
  4002. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4003. u64 data;
  4004. if (vmx_get_msr(vcpu, ecx, &data)) {
  4005. trace_kvm_msr_read_ex(ecx);
  4006. kvm_inject_gp(vcpu, 0);
  4007. return 1;
  4008. }
  4009. trace_kvm_msr_read(ecx, data);
  4010. /* FIXME: handling of bits 32:63 of rax, rdx */
  4011. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  4012. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  4013. skip_emulated_instruction(vcpu);
  4014. return 1;
  4015. }
  4016. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  4017. {
  4018. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4019. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  4020. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  4021. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  4022. trace_kvm_msr_write_ex(ecx, data);
  4023. kvm_inject_gp(vcpu, 0);
  4024. return 1;
  4025. }
  4026. trace_kvm_msr_write(ecx, data);
  4027. skip_emulated_instruction(vcpu);
  4028. return 1;
  4029. }
  4030. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  4031. {
  4032. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4033. return 1;
  4034. }
  4035. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  4036. {
  4037. u32 cpu_based_vm_exec_control;
  4038. /* clear pending irq */
  4039. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4040. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  4041. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4042. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4043. ++vcpu->stat.irq_window_exits;
  4044. /*
  4045. * If the user space waits to inject interrupts, exit as soon as
  4046. * possible
  4047. */
  4048. if (!irqchip_in_kernel(vcpu->kvm) &&
  4049. vcpu->run->request_interrupt_window &&
  4050. !kvm_cpu_has_interrupt(vcpu)) {
  4051. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  4052. return 0;
  4053. }
  4054. return 1;
  4055. }
  4056. static int handle_halt(struct kvm_vcpu *vcpu)
  4057. {
  4058. skip_emulated_instruction(vcpu);
  4059. return kvm_emulate_halt(vcpu);
  4060. }
  4061. static int handle_vmcall(struct kvm_vcpu *vcpu)
  4062. {
  4063. skip_emulated_instruction(vcpu);
  4064. kvm_emulate_hypercall(vcpu);
  4065. return 1;
  4066. }
  4067. static int handle_invd(struct kvm_vcpu *vcpu)
  4068. {
  4069. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4070. }
  4071. static int handle_invlpg(struct kvm_vcpu *vcpu)
  4072. {
  4073. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4074. kvm_mmu_invlpg(vcpu, exit_qualification);
  4075. skip_emulated_instruction(vcpu);
  4076. return 1;
  4077. }
  4078. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  4079. {
  4080. int err;
  4081. err = kvm_rdpmc(vcpu);
  4082. kvm_complete_insn_gp(vcpu, err);
  4083. return 1;
  4084. }
  4085. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  4086. {
  4087. skip_emulated_instruction(vcpu);
  4088. kvm_emulate_wbinvd(vcpu);
  4089. return 1;
  4090. }
  4091. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  4092. {
  4093. u64 new_bv = kvm_read_edx_eax(vcpu);
  4094. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4095. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  4096. skip_emulated_instruction(vcpu);
  4097. return 1;
  4098. }
  4099. static int handle_apic_access(struct kvm_vcpu *vcpu)
  4100. {
  4101. if (likely(fasteoi)) {
  4102. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4103. int access_type, offset;
  4104. access_type = exit_qualification & APIC_ACCESS_TYPE;
  4105. offset = exit_qualification & APIC_ACCESS_OFFSET;
  4106. /*
  4107. * Sane guest uses MOV to write EOI, with written value
  4108. * not cared. So make a short-circuit here by avoiding
  4109. * heavy instruction emulation.
  4110. */
  4111. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  4112. (offset == APIC_EOI)) {
  4113. kvm_lapic_set_eoi(vcpu);
  4114. skip_emulated_instruction(vcpu);
  4115. return 1;
  4116. }
  4117. }
  4118. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4119. }
  4120. static int handle_task_switch(struct kvm_vcpu *vcpu)
  4121. {
  4122. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4123. unsigned long exit_qualification;
  4124. bool has_error_code = false;
  4125. u32 error_code = 0;
  4126. u16 tss_selector;
  4127. int reason, type, idt_v, idt_index;
  4128. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  4129. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  4130. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  4131. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4132. reason = (u32)exit_qualification >> 30;
  4133. if (reason == TASK_SWITCH_GATE && idt_v) {
  4134. switch (type) {
  4135. case INTR_TYPE_NMI_INTR:
  4136. vcpu->arch.nmi_injected = false;
  4137. vmx_set_nmi_mask(vcpu, true);
  4138. break;
  4139. case INTR_TYPE_EXT_INTR:
  4140. case INTR_TYPE_SOFT_INTR:
  4141. kvm_clear_interrupt_queue(vcpu);
  4142. break;
  4143. case INTR_TYPE_HARD_EXCEPTION:
  4144. if (vmx->idt_vectoring_info &
  4145. VECTORING_INFO_DELIVER_CODE_MASK) {
  4146. has_error_code = true;
  4147. error_code =
  4148. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  4149. }
  4150. /* fall through */
  4151. case INTR_TYPE_SOFT_EXCEPTION:
  4152. kvm_clear_exception_queue(vcpu);
  4153. break;
  4154. default:
  4155. break;
  4156. }
  4157. }
  4158. tss_selector = exit_qualification;
  4159. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  4160. type != INTR_TYPE_EXT_INTR &&
  4161. type != INTR_TYPE_NMI_INTR))
  4162. skip_emulated_instruction(vcpu);
  4163. if (kvm_task_switch(vcpu, tss_selector,
  4164. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  4165. has_error_code, error_code) == EMULATE_FAIL) {
  4166. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4167. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4168. vcpu->run->internal.ndata = 0;
  4169. return 0;
  4170. }
  4171. /* clear all local breakpoint enable flags */
  4172. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
  4173. /*
  4174. * TODO: What about debug traps on tss switch?
  4175. * Are we supposed to inject them and update dr6?
  4176. */
  4177. return 1;
  4178. }
  4179. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  4180. {
  4181. unsigned long exit_qualification;
  4182. gpa_t gpa;
  4183. u32 error_code;
  4184. int gla_validity;
  4185. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4186. if (exit_qualification & (1 << 6)) {
  4187. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  4188. return -EINVAL;
  4189. }
  4190. gla_validity = (exit_qualification >> 7) & 0x3;
  4191. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  4192. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  4193. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  4194. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  4195. vmcs_readl(GUEST_LINEAR_ADDRESS));
  4196. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  4197. (long unsigned int)exit_qualification);
  4198. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4199. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  4200. return 0;
  4201. }
  4202. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4203. trace_kvm_page_fault(gpa, exit_qualification);
  4204. /* It is a write fault? */
  4205. error_code = exit_qualification & (1U << 1);
  4206. /* ept page table is present? */
  4207. error_code |= (exit_qualification >> 3) & 0x1;
  4208. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  4209. }
  4210. static u64 ept_rsvd_mask(u64 spte, int level)
  4211. {
  4212. int i;
  4213. u64 mask = 0;
  4214. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  4215. mask |= (1ULL << i);
  4216. if (level > 2)
  4217. /* bits 7:3 reserved */
  4218. mask |= 0xf8;
  4219. else if (level == 2) {
  4220. if (spte & (1ULL << 7))
  4221. /* 2MB ref, bits 20:12 reserved */
  4222. mask |= 0x1ff000;
  4223. else
  4224. /* bits 6:3 reserved */
  4225. mask |= 0x78;
  4226. }
  4227. return mask;
  4228. }
  4229. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  4230. int level)
  4231. {
  4232. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  4233. /* 010b (write-only) */
  4234. WARN_ON((spte & 0x7) == 0x2);
  4235. /* 110b (write/execute) */
  4236. WARN_ON((spte & 0x7) == 0x6);
  4237. /* 100b (execute-only) and value not supported by logical processor */
  4238. if (!cpu_has_vmx_ept_execute_only())
  4239. WARN_ON((spte & 0x7) == 0x4);
  4240. /* not 000b */
  4241. if ((spte & 0x7)) {
  4242. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  4243. if (rsvd_bits != 0) {
  4244. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  4245. __func__, rsvd_bits);
  4246. WARN_ON(1);
  4247. }
  4248. if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
  4249. u64 ept_mem_type = (spte & 0x38) >> 3;
  4250. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  4251. ept_mem_type == 7) {
  4252. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  4253. __func__, ept_mem_type);
  4254. WARN_ON(1);
  4255. }
  4256. }
  4257. }
  4258. }
  4259. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  4260. {
  4261. u64 sptes[4];
  4262. int nr_sptes, i, ret;
  4263. gpa_t gpa;
  4264. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4265. ret = handle_mmio_page_fault_common(vcpu, gpa, true);
  4266. if (likely(ret == 1))
  4267. return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
  4268. EMULATE_DONE;
  4269. if (unlikely(!ret))
  4270. return 1;
  4271. /* It is the real ept misconfig */
  4272. printk(KERN_ERR "EPT: Misconfiguration.\n");
  4273. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  4274. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  4275. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  4276. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  4277. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4278. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  4279. return 0;
  4280. }
  4281. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  4282. {
  4283. u32 cpu_based_vm_exec_control;
  4284. /* clear pending NMI */
  4285. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4286. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  4287. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4288. ++vcpu->stat.nmi_window_exits;
  4289. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4290. return 1;
  4291. }
  4292. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  4293. {
  4294. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4295. enum emulation_result err = EMULATE_DONE;
  4296. int ret = 1;
  4297. u32 cpu_exec_ctrl;
  4298. bool intr_window_requested;
  4299. unsigned count = 130;
  4300. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4301. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  4302. while (!guest_state_valid(vcpu) && count-- != 0) {
  4303. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  4304. return handle_interrupt_window(&vmx->vcpu);
  4305. if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
  4306. return 1;
  4307. err = emulate_instruction(vcpu, 0);
  4308. if (err == EMULATE_DO_MMIO) {
  4309. ret = 0;
  4310. goto out;
  4311. }
  4312. if (err != EMULATE_DONE) {
  4313. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4314. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4315. vcpu->run->internal.ndata = 0;
  4316. return 0;
  4317. }
  4318. if (signal_pending(current))
  4319. goto out;
  4320. if (need_resched())
  4321. schedule();
  4322. }
  4323. vmx->emulation_required = !guest_state_valid(vcpu);
  4324. out:
  4325. return ret;
  4326. }
  4327. /*
  4328. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  4329. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  4330. */
  4331. static int handle_pause(struct kvm_vcpu *vcpu)
  4332. {
  4333. skip_emulated_instruction(vcpu);
  4334. kvm_vcpu_on_spin(vcpu);
  4335. return 1;
  4336. }
  4337. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  4338. {
  4339. kvm_queue_exception(vcpu, UD_VECTOR);
  4340. return 1;
  4341. }
  4342. /*
  4343. * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
  4344. * We could reuse a single VMCS for all the L2 guests, but we also want the
  4345. * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
  4346. * allows keeping them loaded on the processor, and in the future will allow
  4347. * optimizations where prepare_vmcs02 doesn't need to set all the fields on
  4348. * every entry if they never change.
  4349. * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
  4350. * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
  4351. *
  4352. * The following functions allocate and free a vmcs02 in this pool.
  4353. */
  4354. /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
  4355. static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
  4356. {
  4357. struct vmcs02_list *item;
  4358. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  4359. if (item->vmptr == vmx->nested.current_vmptr) {
  4360. list_move(&item->list, &vmx->nested.vmcs02_pool);
  4361. return &item->vmcs02;
  4362. }
  4363. if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
  4364. /* Recycle the least recently used VMCS. */
  4365. item = list_entry(vmx->nested.vmcs02_pool.prev,
  4366. struct vmcs02_list, list);
  4367. item->vmptr = vmx->nested.current_vmptr;
  4368. list_move(&item->list, &vmx->nested.vmcs02_pool);
  4369. return &item->vmcs02;
  4370. }
  4371. /* Create a new VMCS */
  4372. item = (struct vmcs02_list *)
  4373. kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
  4374. if (!item)
  4375. return NULL;
  4376. item->vmcs02.vmcs = alloc_vmcs();
  4377. if (!item->vmcs02.vmcs) {
  4378. kfree(item);
  4379. return NULL;
  4380. }
  4381. loaded_vmcs_init(&item->vmcs02);
  4382. item->vmptr = vmx->nested.current_vmptr;
  4383. list_add(&(item->list), &(vmx->nested.vmcs02_pool));
  4384. vmx->nested.vmcs02_num++;
  4385. return &item->vmcs02;
  4386. }
  4387. /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
  4388. static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
  4389. {
  4390. struct vmcs02_list *item;
  4391. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  4392. if (item->vmptr == vmptr) {
  4393. free_loaded_vmcs(&item->vmcs02);
  4394. list_del(&item->list);
  4395. kfree(item);
  4396. vmx->nested.vmcs02_num--;
  4397. return;
  4398. }
  4399. }
  4400. /*
  4401. * Free all VMCSs saved for this vcpu, except the one pointed by
  4402. * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
  4403. * currently used, if running L2), and vmcs01 when running L2.
  4404. */
  4405. static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
  4406. {
  4407. struct vmcs02_list *item, *n;
  4408. list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
  4409. if (vmx->loaded_vmcs != &item->vmcs02)
  4410. free_loaded_vmcs(&item->vmcs02);
  4411. list_del(&item->list);
  4412. kfree(item);
  4413. }
  4414. vmx->nested.vmcs02_num = 0;
  4415. if (vmx->loaded_vmcs != &vmx->vmcs01)
  4416. free_loaded_vmcs(&vmx->vmcs01);
  4417. }
  4418. /*
  4419. * Emulate the VMXON instruction.
  4420. * Currently, we just remember that VMX is active, and do not save or even
  4421. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  4422. * do not currently need to store anything in that guest-allocated memory
  4423. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  4424. * argument is different from the VMXON pointer (which the spec says they do).
  4425. */
  4426. static int handle_vmon(struct kvm_vcpu *vcpu)
  4427. {
  4428. struct kvm_segment cs;
  4429. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4430. /* The Intel VMX Instruction Reference lists a bunch of bits that
  4431. * are prerequisite to running VMXON, most notably cr4.VMXE must be
  4432. * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
  4433. * Otherwise, we should fail with #UD. We test these now:
  4434. */
  4435. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
  4436. !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
  4437. (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4438. kvm_queue_exception(vcpu, UD_VECTOR);
  4439. return 1;
  4440. }
  4441. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4442. if (is_long_mode(vcpu) && !cs.l) {
  4443. kvm_queue_exception(vcpu, UD_VECTOR);
  4444. return 1;
  4445. }
  4446. if (vmx_get_cpl(vcpu)) {
  4447. kvm_inject_gp(vcpu, 0);
  4448. return 1;
  4449. }
  4450. INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
  4451. vmx->nested.vmcs02_num = 0;
  4452. vmx->nested.vmxon = true;
  4453. skip_emulated_instruction(vcpu);
  4454. return 1;
  4455. }
  4456. /*
  4457. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  4458. * for running VMX instructions (except VMXON, whose prerequisites are
  4459. * slightly different). It also specifies what exception to inject otherwise.
  4460. */
  4461. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  4462. {
  4463. struct kvm_segment cs;
  4464. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4465. if (!vmx->nested.vmxon) {
  4466. kvm_queue_exception(vcpu, UD_VECTOR);
  4467. return 0;
  4468. }
  4469. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4470. if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
  4471. (is_long_mode(vcpu) && !cs.l)) {
  4472. kvm_queue_exception(vcpu, UD_VECTOR);
  4473. return 0;
  4474. }
  4475. if (vmx_get_cpl(vcpu)) {
  4476. kvm_inject_gp(vcpu, 0);
  4477. return 0;
  4478. }
  4479. return 1;
  4480. }
  4481. /*
  4482. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  4483. * just stops using VMX.
  4484. */
  4485. static void free_nested(struct vcpu_vmx *vmx)
  4486. {
  4487. if (!vmx->nested.vmxon)
  4488. return;
  4489. vmx->nested.vmxon = false;
  4490. if (vmx->nested.current_vmptr != -1ull) {
  4491. kunmap(vmx->nested.current_vmcs12_page);
  4492. nested_release_page(vmx->nested.current_vmcs12_page);
  4493. vmx->nested.current_vmptr = -1ull;
  4494. vmx->nested.current_vmcs12 = NULL;
  4495. }
  4496. /* Unpin physical memory we referred to in current vmcs02 */
  4497. if (vmx->nested.apic_access_page) {
  4498. nested_release_page(vmx->nested.apic_access_page);
  4499. vmx->nested.apic_access_page = 0;
  4500. }
  4501. nested_free_all_saved_vmcss(vmx);
  4502. }
  4503. /* Emulate the VMXOFF instruction */
  4504. static int handle_vmoff(struct kvm_vcpu *vcpu)
  4505. {
  4506. if (!nested_vmx_check_permission(vcpu))
  4507. return 1;
  4508. free_nested(to_vmx(vcpu));
  4509. skip_emulated_instruction(vcpu);
  4510. return 1;
  4511. }
  4512. /*
  4513. * Decode the memory-address operand of a vmx instruction, as recorded on an
  4514. * exit caused by such an instruction (run by a guest hypervisor).
  4515. * On success, returns 0. When the operand is invalid, returns 1 and throws
  4516. * #UD or #GP.
  4517. */
  4518. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  4519. unsigned long exit_qualification,
  4520. u32 vmx_instruction_info, gva_t *ret)
  4521. {
  4522. /*
  4523. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  4524. * Execution", on an exit, vmx_instruction_info holds most of the
  4525. * addressing components of the operand. Only the displacement part
  4526. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  4527. * For how an actual address is calculated from all these components,
  4528. * refer to Vol. 1, "Operand Addressing".
  4529. */
  4530. int scaling = vmx_instruction_info & 3;
  4531. int addr_size = (vmx_instruction_info >> 7) & 7;
  4532. bool is_reg = vmx_instruction_info & (1u << 10);
  4533. int seg_reg = (vmx_instruction_info >> 15) & 7;
  4534. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  4535. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  4536. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  4537. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  4538. if (is_reg) {
  4539. kvm_queue_exception(vcpu, UD_VECTOR);
  4540. return 1;
  4541. }
  4542. /* Addr = segment_base + offset */
  4543. /* offset = base + [index * scale] + displacement */
  4544. *ret = vmx_get_segment_base(vcpu, seg_reg);
  4545. if (base_is_valid)
  4546. *ret += kvm_register_read(vcpu, base_reg);
  4547. if (index_is_valid)
  4548. *ret += kvm_register_read(vcpu, index_reg)<<scaling;
  4549. *ret += exit_qualification; /* holds the displacement */
  4550. if (addr_size == 1) /* 32 bit */
  4551. *ret &= 0xffffffff;
  4552. /*
  4553. * TODO: throw #GP (and return 1) in various cases that the VM*
  4554. * instructions require it - e.g., offset beyond segment limit,
  4555. * unusable or unreadable/unwritable segment, non-canonical 64-bit
  4556. * address, and so on. Currently these are not checked.
  4557. */
  4558. return 0;
  4559. }
  4560. /*
  4561. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  4562. * set the success or error code of an emulated VMX instruction, as specified
  4563. * by Vol 2B, VMX Instruction Reference, "Conventions".
  4564. */
  4565. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  4566. {
  4567. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  4568. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  4569. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  4570. }
  4571. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  4572. {
  4573. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  4574. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  4575. X86_EFLAGS_SF | X86_EFLAGS_OF))
  4576. | X86_EFLAGS_CF);
  4577. }
  4578. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  4579. u32 vm_instruction_error)
  4580. {
  4581. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  4582. /*
  4583. * failValid writes the error number to the current VMCS, which
  4584. * can't be done there isn't a current VMCS.
  4585. */
  4586. nested_vmx_failInvalid(vcpu);
  4587. return;
  4588. }
  4589. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  4590. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  4591. X86_EFLAGS_SF | X86_EFLAGS_OF))
  4592. | X86_EFLAGS_ZF);
  4593. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  4594. }
  4595. /* Emulate the VMCLEAR instruction */
  4596. static int handle_vmclear(struct kvm_vcpu *vcpu)
  4597. {
  4598. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4599. gva_t gva;
  4600. gpa_t vmptr;
  4601. struct vmcs12 *vmcs12;
  4602. struct page *page;
  4603. struct x86_exception e;
  4604. if (!nested_vmx_check_permission(vcpu))
  4605. return 1;
  4606. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  4607. vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
  4608. return 1;
  4609. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  4610. sizeof(vmptr), &e)) {
  4611. kvm_inject_page_fault(vcpu, &e);
  4612. return 1;
  4613. }
  4614. if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
  4615. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  4616. skip_emulated_instruction(vcpu);
  4617. return 1;
  4618. }
  4619. if (vmptr == vmx->nested.current_vmptr) {
  4620. kunmap(vmx->nested.current_vmcs12_page);
  4621. nested_release_page(vmx->nested.current_vmcs12_page);
  4622. vmx->nested.current_vmptr = -1ull;
  4623. vmx->nested.current_vmcs12 = NULL;
  4624. }
  4625. page = nested_get_page(vcpu, vmptr);
  4626. if (page == NULL) {
  4627. /*
  4628. * For accurate processor emulation, VMCLEAR beyond available
  4629. * physical memory should do nothing at all. However, it is
  4630. * possible that a nested vmx bug, not a guest hypervisor bug,
  4631. * resulted in this case, so let's shut down before doing any
  4632. * more damage:
  4633. */
  4634. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4635. return 1;
  4636. }
  4637. vmcs12 = kmap(page);
  4638. vmcs12->launch_state = 0;
  4639. kunmap(page);
  4640. nested_release_page(page);
  4641. nested_free_vmcs02(vmx, vmptr);
  4642. skip_emulated_instruction(vcpu);
  4643. nested_vmx_succeed(vcpu);
  4644. return 1;
  4645. }
  4646. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  4647. /* Emulate the VMLAUNCH instruction */
  4648. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  4649. {
  4650. return nested_vmx_run(vcpu, true);
  4651. }
  4652. /* Emulate the VMRESUME instruction */
  4653. static int handle_vmresume(struct kvm_vcpu *vcpu)
  4654. {
  4655. return nested_vmx_run(vcpu, false);
  4656. }
  4657. enum vmcs_field_type {
  4658. VMCS_FIELD_TYPE_U16 = 0,
  4659. VMCS_FIELD_TYPE_U64 = 1,
  4660. VMCS_FIELD_TYPE_U32 = 2,
  4661. VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
  4662. };
  4663. static inline int vmcs_field_type(unsigned long field)
  4664. {
  4665. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4666. return VMCS_FIELD_TYPE_U32;
  4667. return (field >> 13) & 0x3 ;
  4668. }
  4669. static inline int vmcs_field_readonly(unsigned long field)
  4670. {
  4671. return (((field >> 10) & 0x3) == 1);
  4672. }
  4673. /*
  4674. * Read a vmcs12 field. Since these can have varying lengths and we return
  4675. * one type, we chose the biggest type (u64) and zero-extend the return value
  4676. * to that size. Note that the caller, handle_vmread, might need to use only
  4677. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  4678. * 64-bit fields are to be returned).
  4679. */
  4680. static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
  4681. unsigned long field, u64 *ret)
  4682. {
  4683. short offset = vmcs_field_to_offset(field);
  4684. char *p;
  4685. if (offset < 0)
  4686. return 0;
  4687. p = ((char *)(get_vmcs12(vcpu))) + offset;
  4688. switch (vmcs_field_type(field)) {
  4689. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  4690. *ret = *((natural_width *)p);
  4691. return 1;
  4692. case VMCS_FIELD_TYPE_U16:
  4693. *ret = *((u16 *)p);
  4694. return 1;
  4695. case VMCS_FIELD_TYPE_U32:
  4696. *ret = *((u32 *)p);
  4697. return 1;
  4698. case VMCS_FIELD_TYPE_U64:
  4699. *ret = *((u64 *)p);
  4700. return 1;
  4701. default:
  4702. return 0; /* can never happen. */
  4703. }
  4704. }
  4705. /*
  4706. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  4707. * used before) all generate the same failure when it is missing.
  4708. */
  4709. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  4710. {
  4711. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4712. if (vmx->nested.current_vmptr == -1ull) {
  4713. nested_vmx_failInvalid(vcpu);
  4714. skip_emulated_instruction(vcpu);
  4715. return 0;
  4716. }
  4717. return 1;
  4718. }
  4719. static int handle_vmread(struct kvm_vcpu *vcpu)
  4720. {
  4721. unsigned long field;
  4722. u64 field_value;
  4723. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4724. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  4725. gva_t gva = 0;
  4726. if (!nested_vmx_check_permission(vcpu) ||
  4727. !nested_vmx_check_vmcs12(vcpu))
  4728. return 1;
  4729. /* Decode instruction info and find the field to read */
  4730. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  4731. /* Read the field, zero-extended to a u64 field_value */
  4732. if (!vmcs12_read_any(vcpu, field, &field_value)) {
  4733. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  4734. skip_emulated_instruction(vcpu);
  4735. return 1;
  4736. }
  4737. /*
  4738. * Now copy part of this value to register or memory, as requested.
  4739. * Note that the number of bits actually copied is 32 or 64 depending
  4740. * on the guest's mode (32 or 64 bit), not on the given field's length.
  4741. */
  4742. if (vmx_instruction_info & (1u << 10)) {
  4743. kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  4744. field_value);
  4745. } else {
  4746. if (get_vmx_mem_address(vcpu, exit_qualification,
  4747. vmx_instruction_info, &gva))
  4748. return 1;
  4749. /* _system ok, as nested_vmx_check_permission verified cpl=0 */
  4750. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  4751. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  4752. }
  4753. nested_vmx_succeed(vcpu);
  4754. skip_emulated_instruction(vcpu);
  4755. return 1;
  4756. }
  4757. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  4758. {
  4759. unsigned long field;
  4760. gva_t gva;
  4761. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4762. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  4763. char *p;
  4764. short offset;
  4765. /* The value to write might be 32 or 64 bits, depending on L1's long
  4766. * mode, and eventually we need to write that into a field of several
  4767. * possible lengths. The code below first zero-extends the value to 64
  4768. * bit (field_value), and then copies only the approriate number of
  4769. * bits into the vmcs12 field.
  4770. */
  4771. u64 field_value = 0;
  4772. struct x86_exception e;
  4773. if (!nested_vmx_check_permission(vcpu) ||
  4774. !nested_vmx_check_vmcs12(vcpu))
  4775. return 1;
  4776. if (vmx_instruction_info & (1u << 10))
  4777. field_value = kvm_register_read(vcpu,
  4778. (((vmx_instruction_info) >> 3) & 0xf));
  4779. else {
  4780. if (get_vmx_mem_address(vcpu, exit_qualification,
  4781. vmx_instruction_info, &gva))
  4782. return 1;
  4783. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  4784. &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
  4785. kvm_inject_page_fault(vcpu, &e);
  4786. return 1;
  4787. }
  4788. }
  4789. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  4790. if (vmcs_field_readonly(field)) {
  4791. nested_vmx_failValid(vcpu,
  4792. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  4793. skip_emulated_instruction(vcpu);
  4794. return 1;
  4795. }
  4796. offset = vmcs_field_to_offset(field);
  4797. if (offset < 0) {
  4798. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  4799. skip_emulated_instruction(vcpu);
  4800. return 1;
  4801. }
  4802. p = ((char *) get_vmcs12(vcpu)) + offset;
  4803. switch (vmcs_field_type(field)) {
  4804. case VMCS_FIELD_TYPE_U16:
  4805. *(u16 *)p = field_value;
  4806. break;
  4807. case VMCS_FIELD_TYPE_U32:
  4808. *(u32 *)p = field_value;
  4809. break;
  4810. case VMCS_FIELD_TYPE_U64:
  4811. *(u64 *)p = field_value;
  4812. break;
  4813. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  4814. *(natural_width *)p = field_value;
  4815. break;
  4816. default:
  4817. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  4818. skip_emulated_instruction(vcpu);
  4819. return 1;
  4820. }
  4821. nested_vmx_succeed(vcpu);
  4822. skip_emulated_instruction(vcpu);
  4823. return 1;
  4824. }
  4825. /* Emulate the VMPTRLD instruction */
  4826. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  4827. {
  4828. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4829. gva_t gva;
  4830. gpa_t vmptr;
  4831. struct x86_exception e;
  4832. if (!nested_vmx_check_permission(vcpu))
  4833. return 1;
  4834. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  4835. vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
  4836. return 1;
  4837. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  4838. sizeof(vmptr), &e)) {
  4839. kvm_inject_page_fault(vcpu, &e);
  4840. return 1;
  4841. }
  4842. if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
  4843. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  4844. skip_emulated_instruction(vcpu);
  4845. return 1;
  4846. }
  4847. if (vmx->nested.current_vmptr != vmptr) {
  4848. struct vmcs12 *new_vmcs12;
  4849. struct page *page;
  4850. page = nested_get_page(vcpu, vmptr);
  4851. if (page == NULL) {
  4852. nested_vmx_failInvalid(vcpu);
  4853. skip_emulated_instruction(vcpu);
  4854. return 1;
  4855. }
  4856. new_vmcs12 = kmap(page);
  4857. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  4858. kunmap(page);
  4859. nested_release_page_clean(page);
  4860. nested_vmx_failValid(vcpu,
  4861. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  4862. skip_emulated_instruction(vcpu);
  4863. return 1;
  4864. }
  4865. if (vmx->nested.current_vmptr != -1ull) {
  4866. kunmap(vmx->nested.current_vmcs12_page);
  4867. nested_release_page(vmx->nested.current_vmcs12_page);
  4868. }
  4869. vmx->nested.current_vmptr = vmptr;
  4870. vmx->nested.current_vmcs12 = new_vmcs12;
  4871. vmx->nested.current_vmcs12_page = page;
  4872. }
  4873. nested_vmx_succeed(vcpu);
  4874. skip_emulated_instruction(vcpu);
  4875. return 1;
  4876. }
  4877. /* Emulate the VMPTRST instruction */
  4878. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  4879. {
  4880. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4881. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  4882. gva_t vmcs_gva;
  4883. struct x86_exception e;
  4884. if (!nested_vmx_check_permission(vcpu))
  4885. return 1;
  4886. if (get_vmx_mem_address(vcpu, exit_qualification,
  4887. vmx_instruction_info, &vmcs_gva))
  4888. return 1;
  4889. /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
  4890. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  4891. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  4892. sizeof(u64), &e)) {
  4893. kvm_inject_page_fault(vcpu, &e);
  4894. return 1;
  4895. }
  4896. nested_vmx_succeed(vcpu);
  4897. skip_emulated_instruction(vcpu);
  4898. return 1;
  4899. }
  4900. /*
  4901. * The exit handlers return 1 if the exit was handled fully and guest execution
  4902. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  4903. * to be done to userspace and return 0.
  4904. */
  4905. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  4906. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  4907. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  4908. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  4909. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  4910. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  4911. [EXIT_REASON_CR_ACCESS] = handle_cr,
  4912. [EXIT_REASON_DR_ACCESS] = handle_dr,
  4913. [EXIT_REASON_CPUID] = handle_cpuid,
  4914. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  4915. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  4916. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  4917. [EXIT_REASON_HLT] = handle_halt,
  4918. [EXIT_REASON_INVD] = handle_invd,
  4919. [EXIT_REASON_INVLPG] = handle_invlpg,
  4920. [EXIT_REASON_RDPMC] = handle_rdpmc,
  4921. [EXIT_REASON_VMCALL] = handle_vmcall,
  4922. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  4923. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  4924. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  4925. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  4926. [EXIT_REASON_VMREAD] = handle_vmread,
  4927. [EXIT_REASON_VMRESUME] = handle_vmresume,
  4928. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  4929. [EXIT_REASON_VMOFF] = handle_vmoff,
  4930. [EXIT_REASON_VMON] = handle_vmon,
  4931. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  4932. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  4933. [EXIT_REASON_WBINVD] = handle_wbinvd,
  4934. [EXIT_REASON_XSETBV] = handle_xsetbv,
  4935. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  4936. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  4937. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  4938. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  4939. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  4940. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
  4941. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
  4942. };
  4943. static const int kvm_vmx_max_exit_handlers =
  4944. ARRAY_SIZE(kvm_vmx_exit_handlers);
  4945. /*
  4946. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  4947. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  4948. * disinterest in the current event (read or write a specific MSR) by using an
  4949. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  4950. */
  4951. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  4952. struct vmcs12 *vmcs12, u32 exit_reason)
  4953. {
  4954. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  4955. gpa_t bitmap;
  4956. if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS))
  4957. return 1;
  4958. /*
  4959. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  4960. * for the four combinations of read/write and low/high MSR numbers.
  4961. * First we need to figure out which of the four to use:
  4962. */
  4963. bitmap = vmcs12->msr_bitmap;
  4964. if (exit_reason == EXIT_REASON_MSR_WRITE)
  4965. bitmap += 2048;
  4966. if (msr_index >= 0xc0000000) {
  4967. msr_index -= 0xc0000000;
  4968. bitmap += 1024;
  4969. }
  4970. /* Then read the msr_index'th bit from this bitmap: */
  4971. if (msr_index < 1024*8) {
  4972. unsigned char b;
  4973. kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1);
  4974. return 1 & (b >> (msr_index & 7));
  4975. } else
  4976. return 1; /* let L1 handle the wrong parameter */
  4977. }
  4978. /*
  4979. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  4980. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  4981. * intercept (via guest_host_mask etc.) the current event.
  4982. */
  4983. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  4984. struct vmcs12 *vmcs12)
  4985. {
  4986. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4987. int cr = exit_qualification & 15;
  4988. int reg = (exit_qualification >> 8) & 15;
  4989. unsigned long val = kvm_register_read(vcpu, reg);
  4990. switch ((exit_qualification >> 4) & 3) {
  4991. case 0: /* mov to cr */
  4992. switch (cr) {
  4993. case 0:
  4994. if (vmcs12->cr0_guest_host_mask &
  4995. (val ^ vmcs12->cr0_read_shadow))
  4996. return 1;
  4997. break;
  4998. case 3:
  4999. if ((vmcs12->cr3_target_count >= 1 &&
  5000. vmcs12->cr3_target_value0 == val) ||
  5001. (vmcs12->cr3_target_count >= 2 &&
  5002. vmcs12->cr3_target_value1 == val) ||
  5003. (vmcs12->cr3_target_count >= 3 &&
  5004. vmcs12->cr3_target_value2 == val) ||
  5005. (vmcs12->cr3_target_count >= 4 &&
  5006. vmcs12->cr3_target_value3 == val))
  5007. return 0;
  5008. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  5009. return 1;
  5010. break;
  5011. case 4:
  5012. if (vmcs12->cr4_guest_host_mask &
  5013. (vmcs12->cr4_read_shadow ^ val))
  5014. return 1;
  5015. break;
  5016. case 8:
  5017. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  5018. return 1;
  5019. break;
  5020. }
  5021. break;
  5022. case 2: /* clts */
  5023. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  5024. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  5025. return 1;
  5026. break;
  5027. case 1: /* mov from cr */
  5028. switch (cr) {
  5029. case 3:
  5030. if (vmcs12->cpu_based_vm_exec_control &
  5031. CPU_BASED_CR3_STORE_EXITING)
  5032. return 1;
  5033. break;
  5034. case 8:
  5035. if (vmcs12->cpu_based_vm_exec_control &
  5036. CPU_BASED_CR8_STORE_EXITING)
  5037. return 1;
  5038. break;
  5039. }
  5040. break;
  5041. case 3: /* lmsw */
  5042. /*
  5043. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  5044. * cr0. Other attempted changes are ignored, with no exit.
  5045. */
  5046. if (vmcs12->cr0_guest_host_mask & 0xe &
  5047. (val ^ vmcs12->cr0_read_shadow))
  5048. return 1;
  5049. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  5050. !(vmcs12->cr0_read_shadow & 0x1) &&
  5051. (val & 0x1))
  5052. return 1;
  5053. break;
  5054. }
  5055. return 0;
  5056. }
  5057. /*
  5058. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  5059. * should handle it ourselves in L0 (and then continue L2). Only call this
  5060. * when in is_guest_mode (L2).
  5061. */
  5062. static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
  5063. {
  5064. u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
  5065. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  5066. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5067. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5068. if (vmx->nested.nested_run_pending)
  5069. return 0;
  5070. if (unlikely(vmx->fail)) {
  5071. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  5072. vmcs_read32(VM_INSTRUCTION_ERROR));
  5073. return 1;
  5074. }
  5075. switch (exit_reason) {
  5076. case EXIT_REASON_EXCEPTION_NMI:
  5077. if (!is_exception(intr_info))
  5078. return 0;
  5079. else if (is_page_fault(intr_info))
  5080. return enable_ept;
  5081. return vmcs12->exception_bitmap &
  5082. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  5083. case EXIT_REASON_EXTERNAL_INTERRUPT:
  5084. return 0;
  5085. case EXIT_REASON_TRIPLE_FAULT:
  5086. return 1;
  5087. case EXIT_REASON_PENDING_INTERRUPT:
  5088. case EXIT_REASON_NMI_WINDOW:
  5089. /*
  5090. * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit
  5091. * (aka Interrupt Window Exiting) only when L1 turned it on,
  5092. * so if we got a PENDING_INTERRUPT exit, this must be for L1.
  5093. * Same for NMI Window Exiting.
  5094. */
  5095. return 1;
  5096. case EXIT_REASON_TASK_SWITCH:
  5097. return 1;
  5098. case EXIT_REASON_CPUID:
  5099. return 1;
  5100. case EXIT_REASON_HLT:
  5101. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  5102. case EXIT_REASON_INVD:
  5103. return 1;
  5104. case EXIT_REASON_INVLPG:
  5105. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  5106. case EXIT_REASON_RDPMC:
  5107. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  5108. case EXIT_REASON_RDTSC:
  5109. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  5110. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  5111. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  5112. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  5113. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  5114. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  5115. /*
  5116. * VMX instructions trap unconditionally. This allows L1 to
  5117. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  5118. */
  5119. return 1;
  5120. case EXIT_REASON_CR_ACCESS:
  5121. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  5122. case EXIT_REASON_DR_ACCESS:
  5123. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  5124. case EXIT_REASON_IO_INSTRUCTION:
  5125. /* TODO: support IO bitmaps */
  5126. return 1;
  5127. case EXIT_REASON_MSR_READ:
  5128. case EXIT_REASON_MSR_WRITE:
  5129. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  5130. case EXIT_REASON_INVALID_STATE:
  5131. return 1;
  5132. case EXIT_REASON_MWAIT_INSTRUCTION:
  5133. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  5134. case EXIT_REASON_MONITOR_INSTRUCTION:
  5135. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  5136. case EXIT_REASON_PAUSE_INSTRUCTION:
  5137. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  5138. nested_cpu_has2(vmcs12,
  5139. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  5140. case EXIT_REASON_MCE_DURING_VMENTRY:
  5141. return 0;
  5142. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  5143. return 1;
  5144. case EXIT_REASON_APIC_ACCESS:
  5145. return nested_cpu_has2(vmcs12,
  5146. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  5147. case EXIT_REASON_EPT_VIOLATION:
  5148. case EXIT_REASON_EPT_MISCONFIG:
  5149. return 0;
  5150. case EXIT_REASON_WBINVD:
  5151. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  5152. case EXIT_REASON_XSETBV:
  5153. return 1;
  5154. default:
  5155. return 1;
  5156. }
  5157. }
  5158. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  5159. {
  5160. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  5161. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  5162. }
  5163. /*
  5164. * The guest has exited. See if we can fix it or if we need userspace
  5165. * assistance.
  5166. */
  5167. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  5168. {
  5169. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5170. u32 exit_reason = vmx->exit_reason;
  5171. u32 vectoring_info = vmx->idt_vectoring_info;
  5172. /* If guest state is invalid, start emulating */
  5173. if (vmx->emulation_required && emulate_invalid_guest_state)
  5174. return handle_invalid_guest_state(vcpu);
  5175. /*
  5176. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  5177. * we did not inject a still-pending event to L1 now because of
  5178. * nested_run_pending, we need to re-enable this bit.
  5179. */
  5180. if (vmx->nested.nested_run_pending)
  5181. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5182. if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
  5183. exit_reason == EXIT_REASON_VMRESUME))
  5184. vmx->nested.nested_run_pending = 1;
  5185. else
  5186. vmx->nested.nested_run_pending = 0;
  5187. if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
  5188. nested_vmx_vmexit(vcpu);
  5189. return 1;
  5190. }
  5191. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  5192. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  5193. vcpu->run->fail_entry.hardware_entry_failure_reason
  5194. = exit_reason;
  5195. return 0;
  5196. }
  5197. if (unlikely(vmx->fail)) {
  5198. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  5199. vcpu->run->fail_entry.hardware_entry_failure_reason
  5200. = vmcs_read32(VM_INSTRUCTION_ERROR);
  5201. return 0;
  5202. }
  5203. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  5204. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  5205. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  5206. exit_reason != EXIT_REASON_TASK_SWITCH))
  5207. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  5208. "(0x%x) and exit reason is 0x%x\n",
  5209. __func__, vectoring_info, exit_reason);
  5210. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
  5211. !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
  5212. get_vmcs12(vcpu), vcpu)))) {
  5213. if (vmx_interrupt_allowed(vcpu)) {
  5214. vmx->soft_vnmi_blocked = 0;
  5215. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  5216. vcpu->arch.nmi_pending) {
  5217. /*
  5218. * This CPU don't support us in finding the end of an
  5219. * NMI-blocked window if the guest runs with IRQs
  5220. * disabled. So we pull the trigger after 1 s of
  5221. * futile waiting, but inform the user about this.
  5222. */
  5223. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  5224. "state on VCPU %d after 1 s timeout\n",
  5225. __func__, vcpu->vcpu_id);
  5226. vmx->soft_vnmi_blocked = 0;
  5227. }
  5228. }
  5229. if (exit_reason < kvm_vmx_max_exit_handlers
  5230. && kvm_vmx_exit_handlers[exit_reason])
  5231. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  5232. else {
  5233. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  5234. vcpu->run->hw.hardware_exit_reason = exit_reason;
  5235. }
  5236. return 0;
  5237. }
  5238. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  5239. {
  5240. if (irr == -1 || tpr < irr) {
  5241. vmcs_write32(TPR_THRESHOLD, 0);
  5242. return;
  5243. }
  5244. vmcs_write32(TPR_THRESHOLD, irr);
  5245. }
  5246. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  5247. {
  5248. u32 exit_intr_info;
  5249. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  5250. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  5251. return;
  5252. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  5253. exit_intr_info = vmx->exit_intr_info;
  5254. /* Handle machine checks before interrupts are enabled */
  5255. if (is_machine_check(exit_intr_info))
  5256. kvm_machine_check();
  5257. /* We need to handle NMIs before interrupts are enabled */
  5258. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  5259. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  5260. kvm_before_handle_nmi(&vmx->vcpu);
  5261. asm("int $2");
  5262. kvm_after_handle_nmi(&vmx->vcpu);
  5263. }
  5264. }
  5265. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  5266. {
  5267. u32 exit_intr_info;
  5268. bool unblock_nmi;
  5269. u8 vector;
  5270. bool idtv_info_valid;
  5271. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  5272. if (cpu_has_virtual_nmis()) {
  5273. if (vmx->nmi_known_unmasked)
  5274. return;
  5275. /*
  5276. * Can't use vmx->exit_intr_info since we're not sure what
  5277. * the exit reason is.
  5278. */
  5279. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  5280. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  5281. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  5282. /*
  5283. * SDM 3: 27.7.1.2 (September 2008)
  5284. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  5285. * a guest IRET fault.
  5286. * SDM 3: 23.2.2 (September 2008)
  5287. * Bit 12 is undefined in any of the following cases:
  5288. * If the VM exit sets the valid bit in the IDT-vectoring
  5289. * information field.
  5290. * If the VM exit is due to a double fault.
  5291. */
  5292. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  5293. vector != DF_VECTOR && !idtv_info_valid)
  5294. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5295. GUEST_INTR_STATE_NMI);
  5296. else
  5297. vmx->nmi_known_unmasked =
  5298. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  5299. & GUEST_INTR_STATE_NMI);
  5300. } else if (unlikely(vmx->soft_vnmi_blocked))
  5301. vmx->vnmi_blocked_time +=
  5302. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  5303. }
  5304. static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
  5305. u32 idt_vectoring_info,
  5306. int instr_len_field,
  5307. int error_code_field)
  5308. {
  5309. u8 vector;
  5310. int type;
  5311. bool idtv_info_valid;
  5312. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  5313. vmx->vcpu.arch.nmi_injected = false;
  5314. kvm_clear_exception_queue(&vmx->vcpu);
  5315. kvm_clear_interrupt_queue(&vmx->vcpu);
  5316. if (!idtv_info_valid)
  5317. return;
  5318. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  5319. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  5320. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  5321. switch (type) {
  5322. case INTR_TYPE_NMI_INTR:
  5323. vmx->vcpu.arch.nmi_injected = true;
  5324. /*
  5325. * SDM 3: 27.7.1.2 (September 2008)
  5326. * Clear bit "block by NMI" before VM entry if a NMI
  5327. * delivery faulted.
  5328. */
  5329. vmx_set_nmi_mask(&vmx->vcpu, false);
  5330. break;
  5331. case INTR_TYPE_SOFT_EXCEPTION:
  5332. vmx->vcpu.arch.event_exit_inst_len =
  5333. vmcs_read32(instr_len_field);
  5334. /* fall through */
  5335. case INTR_TYPE_HARD_EXCEPTION:
  5336. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  5337. u32 err = vmcs_read32(error_code_field);
  5338. kvm_queue_exception_e(&vmx->vcpu, vector, err);
  5339. } else
  5340. kvm_queue_exception(&vmx->vcpu, vector);
  5341. break;
  5342. case INTR_TYPE_SOFT_INTR:
  5343. vmx->vcpu.arch.event_exit_inst_len =
  5344. vmcs_read32(instr_len_field);
  5345. /* fall through */
  5346. case INTR_TYPE_EXT_INTR:
  5347. kvm_queue_interrupt(&vmx->vcpu, vector,
  5348. type == INTR_TYPE_SOFT_INTR);
  5349. break;
  5350. default:
  5351. break;
  5352. }
  5353. }
  5354. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  5355. {
  5356. if (is_guest_mode(&vmx->vcpu))
  5357. return;
  5358. __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
  5359. VM_EXIT_INSTRUCTION_LEN,
  5360. IDT_VECTORING_ERROR_CODE);
  5361. }
  5362. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  5363. {
  5364. if (is_guest_mode(vcpu))
  5365. return;
  5366. __vmx_complete_interrupts(to_vmx(vcpu),
  5367. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  5368. VM_ENTRY_INSTRUCTION_LEN,
  5369. VM_ENTRY_EXCEPTION_ERROR_CODE);
  5370. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  5371. }
  5372. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  5373. {
  5374. int i, nr_msrs;
  5375. struct perf_guest_switch_msr *msrs;
  5376. msrs = perf_guest_get_msrs(&nr_msrs);
  5377. if (!msrs)
  5378. return;
  5379. for (i = 0; i < nr_msrs; i++)
  5380. if (msrs[i].host == msrs[i].guest)
  5381. clear_atomic_switch_msr(vmx, msrs[i].msr);
  5382. else
  5383. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  5384. msrs[i].host);
  5385. }
  5386. #ifdef CONFIG_X86_64
  5387. #define R "r"
  5388. #define Q "q"
  5389. #else
  5390. #define R "e"
  5391. #define Q "l"
  5392. #endif
  5393. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  5394. {
  5395. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5396. unsigned long debugctlmsr;
  5397. if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) {
  5398. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5399. if (vmcs12->idt_vectoring_info_field &
  5400. VECTORING_INFO_VALID_MASK) {
  5401. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5402. vmcs12->idt_vectoring_info_field);
  5403. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5404. vmcs12->vm_exit_instruction_len);
  5405. if (vmcs12->idt_vectoring_info_field &
  5406. VECTORING_INFO_DELIVER_CODE_MASK)
  5407. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  5408. vmcs12->idt_vectoring_error_code);
  5409. }
  5410. }
  5411. /* Record the guest's net vcpu time for enforced NMI injections. */
  5412. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  5413. vmx->entry_time = ktime_get();
  5414. /* Don't enter VMX if guest state is invalid, let the exit handler
  5415. start emulation until we arrive back to a valid state */
  5416. if (vmx->emulation_required && emulate_invalid_guest_state)
  5417. return;
  5418. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  5419. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  5420. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  5421. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  5422. /* When single-stepping over STI and MOV SS, we must clear the
  5423. * corresponding interruptibility bits in the guest state. Otherwise
  5424. * vmentry fails as it then expects bit 14 (BS) in pending debug
  5425. * exceptions being set, but that's not correct for the guest debugging
  5426. * case. */
  5427. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5428. vmx_set_interrupt_shadow(vcpu, 0);
  5429. atomic_switch_perf_msrs(vmx);
  5430. debugctlmsr = get_debugctlmsr();
  5431. vmx->__launched = vmx->loaded_vmcs->launched;
  5432. asm(
  5433. /* Store host registers */
  5434. "push %%"R"dx; push %%"R"bp;"
  5435. "push %%"R"cx \n\t" /* placeholder for guest rcx */
  5436. "push %%"R"cx \n\t"
  5437. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  5438. "je 1f \n\t"
  5439. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  5440. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  5441. "1: \n\t"
  5442. /* Reload cr2 if changed */
  5443. "mov %c[cr2](%0), %%"R"ax \n\t"
  5444. "mov %%cr2, %%"R"dx \n\t"
  5445. "cmp %%"R"ax, %%"R"dx \n\t"
  5446. "je 2f \n\t"
  5447. "mov %%"R"ax, %%cr2 \n\t"
  5448. "2: \n\t"
  5449. /* Check if vmlaunch of vmresume is needed */
  5450. "cmpl $0, %c[launched](%0) \n\t"
  5451. /* Load guest registers. Don't clobber flags. */
  5452. "mov %c[rax](%0), %%"R"ax \n\t"
  5453. "mov %c[rbx](%0), %%"R"bx \n\t"
  5454. "mov %c[rdx](%0), %%"R"dx \n\t"
  5455. "mov %c[rsi](%0), %%"R"si \n\t"
  5456. "mov %c[rdi](%0), %%"R"di \n\t"
  5457. "mov %c[rbp](%0), %%"R"bp \n\t"
  5458. #ifdef CONFIG_X86_64
  5459. "mov %c[r8](%0), %%r8 \n\t"
  5460. "mov %c[r9](%0), %%r9 \n\t"
  5461. "mov %c[r10](%0), %%r10 \n\t"
  5462. "mov %c[r11](%0), %%r11 \n\t"
  5463. "mov %c[r12](%0), %%r12 \n\t"
  5464. "mov %c[r13](%0), %%r13 \n\t"
  5465. "mov %c[r14](%0), %%r14 \n\t"
  5466. "mov %c[r15](%0), %%r15 \n\t"
  5467. #endif
  5468. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  5469. /* Enter guest mode */
  5470. "jne .Llaunched \n\t"
  5471. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  5472. "jmp .Lkvm_vmx_return \n\t"
  5473. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  5474. ".Lkvm_vmx_return: "
  5475. /* Save guest registers, load host registers, keep flags */
  5476. "mov %0, %c[wordsize](%%"R"sp) \n\t"
  5477. "pop %0 \n\t"
  5478. "mov %%"R"ax, %c[rax](%0) \n\t"
  5479. "mov %%"R"bx, %c[rbx](%0) \n\t"
  5480. "pop"Q" %c[rcx](%0) \n\t"
  5481. "mov %%"R"dx, %c[rdx](%0) \n\t"
  5482. "mov %%"R"si, %c[rsi](%0) \n\t"
  5483. "mov %%"R"di, %c[rdi](%0) \n\t"
  5484. "mov %%"R"bp, %c[rbp](%0) \n\t"
  5485. #ifdef CONFIG_X86_64
  5486. "mov %%r8, %c[r8](%0) \n\t"
  5487. "mov %%r9, %c[r9](%0) \n\t"
  5488. "mov %%r10, %c[r10](%0) \n\t"
  5489. "mov %%r11, %c[r11](%0) \n\t"
  5490. "mov %%r12, %c[r12](%0) \n\t"
  5491. "mov %%r13, %c[r13](%0) \n\t"
  5492. "mov %%r14, %c[r14](%0) \n\t"
  5493. "mov %%r15, %c[r15](%0) \n\t"
  5494. #endif
  5495. "mov %%cr2, %%"R"ax \n\t"
  5496. "mov %%"R"ax, %c[cr2](%0) \n\t"
  5497. "pop %%"R"bp; pop %%"R"dx \n\t"
  5498. "setbe %c[fail](%0) \n\t"
  5499. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  5500. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  5501. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  5502. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  5503. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  5504. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  5505. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  5506. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  5507. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  5508. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  5509. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  5510. #ifdef CONFIG_X86_64
  5511. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  5512. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  5513. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  5514. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  5515. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  5516. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  5517. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  5518. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  5519. #endif
  5520. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  5521. [wordsize]"i"(sizeof(ulong))
  5522. : "cc", "memory"
  5523. , R"ax", R"bx", R"di", R"si"
  5524. #ifdef CONFIG_X86_64
  5525. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  5526. #endif
  5527. );
  5528. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  5529. if (debugctlmsr)
  5530. update_debugctlmsr(debugctlmsr);
  5531. #ifndef CONFIG_X86_64
  5532. /*
  5533. * The sysexit path does not restore ds/es, so we must set them to
  5534. * a reasonable value ourselves.
  5535. *
  5536. * We can't defer this to vmx_load_host_state() since that function
  5537. * may be executed in interrupt context, which saves and restore segments
  5538. * around it, nullifying its effect.
  5539. */
  5540. loadsegment(ds, __USER_DS);
  5541. loadsegment(es, __USER_DS);
  5542. #endif
  5543. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  5544. | (1 << VCPU_EXREG_RFLAGS)
  5545. | (1 << VCPU_EXREG_CPL)
  5546. | (1 << VCPU_EXREG_PDPTR)
  5547. | (1 << VCPU_EXREG_SEGMENTS)
  5548. | (1 << VCPU_EXREG_CR3));
  5549. vcpu->arch.regs_dirty = 0;
  5550. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  5551. if (is_guest_mode(vcpu)) {
  5552. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5553. vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info;
  5554. if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
  5555. vmcs12->idt_vectoring_error_code =
  5556. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  5557. vmcs12->vm_exit_instruction_len =
  5558. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5559. }
  5560. }
  5561. vmx->loaded_vmcs->launched = 1;
  5562. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  5563. trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
  5564. vmx_complete_atomic_exit(vmx);
  5565. vmx_recover_nmi_blocking(vmx);
  5566. vmx_complete_interrupts(vmx);
  5567. }
  5568. #undef R
  5569. #undef Q
  5570. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  5571. {
  5572. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5573. free_vpid(vmx);
  5574. free_nested(vmx);
  5575. free_loaded_vmcs(vmx->loaded_vmcs);
  5576. kfree(vmx->guest_msrs);
  5577. kvm_vcpu_uninit(vcpu);
  5578. kmem_cache_free(kvm_vcpu_cache, vmx);
  5579. }
  5580. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  5581. {
  5582. int err;
  5583. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  5584. int cpu;
  5585. if (!vmx)
  5586. return ERR_PTR(-ENOMEM);
  5587. allocate_vpid(vmx);
  5588. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  5589. if (err)
  5590. goto free_vcpu;
  5591. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  5592. err = -ENOMEM;
  5593. if (!vmx->guest_msrs) {
  5594. goto uninit_vcpu;
  5595. }
  5596. vmx->loaded_vmcs = &vmx->vmcs01;
  5597. vmx->loaded_vmcs->vmcs = alloc_vmcs();
  5598. if (!vmx->loaded_vmcs->vmcs)
  5599. goto free_msrs;
  5600. if (!vmm_exclusive)
  5601. kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
  5602. loaded_vmcs_init(vmx->loaded_vmcs);
  5603. if (!vmm_exclusive)
  5604. kvm_cpu_vmxoff();
  5605. cpu = get_cpu();
  5606. vmx_vcpu_load(&vmx->vcpu, cpu);
  5607. vmx->vcpu.cpu = cpu;
  5608. err = vmx_vcpu_setup(vmx);
  5609. vmx_vcpu_put(&vmx->vcpu);
  5610. put_cpu();
  5611. if (err)
  5612. goto free_vmcs;
  5613. if (vm_need_virtualize_apic_accesses(kvm))
  5614. err = alloc_apic_access_page(kvm);
  5615. if (err)
  5616. goto free_vmcs;
  5617. if (enable_ept) {
  5618. if (!kvm->arch.ept_identity_map_addr)
  5619. kvm->arch.ept_identity_map_addr =
  5620. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5621. err = -ENOMEM;
  5622. if (alloc_identity_pagetable(kvm) != 0)
  5623. goto free_vmcs;
  5624. if (!init_rmode_identity_map(kvm))
  5625. goto free_vmcs;
  5626. }
  5627. vmx->nested.current_vmptr = -1ull;
  5628. vmx->nested.current_vmcs12 = NULL;
  5629. return &vmx->vcpu;
  5630. free_vmcs:
  5631. free_loaded_vmcs(vmx->loaded_vmcs);
  5632. free_msrs:
  5633. kfree(vmx->guest_msrs);
  5634. uninit_vcpu:
  5635. kvm_vcpu_uninit(&vmx->vcpu);
  5636. free_vcpu:
  5637. free_vpid(vmx);
  5638. kmem_cache_free(kvm_vcpu_cache, vmx);
  5639. return ERR_PTR(err);
  5640. }
  5641. static void __init vmx_check_processor_compat(void *rtn)
  5642. {
  5643. struct vmcs_config vmcs_conf;
  5644. *(int *)rtn = 0;
  5645. if (setup_vmcs_config(&vmcs_conf) < 0)
  5646. *(int *)rtn = -EIO;
  5647. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  5648. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  5649. smp_processor_id());
  5650. *(int *)rtn = -EIO;
  5651. }
  5652. }
  5653. static int get_ept_level(void)
  5654. {
  5655. return VMX_EPT_DEFAULT_GAW + 1;
  5656. }
  5657. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  5658. {
  5659. u64 ret;
  5660. /* For VT-d and EPT combination
  5661. * 1. MMIO: always map as UC
  5662. * 2. EPT with VT-d:
  5663. * a. VT-d without snooping control feature: can't guarantee the
  5664. * result, try to trust guest.
  5665. * b. VT-d with snooping control feature: snooping control feature of
  5666. * VT-d engine can guarantee the cache correctness. Just set it
  5667. * to WB to keep consistent with host. So the same as item 3.
  5668. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  5669. * consistent with host MTRR
  5670. */
  5671. if (is_mmio)
  5672. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  5673. else if (vcpu->kvm->arch.iommu_domain &&
  5674. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
  5675. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  5676. VMX_EPT_MT_EPTE_SHIFT;
  5677. else
  5678. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  5679. | VMX_EPT_IPAT_BIT;
  5680. return ret;
  5681. }
  5682. static int vmx_get_lpage_level(void)
  5683. {
  5684. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  5685. return PT_DIRECTORY_LEVEL;
  5686. else
  5687. /* For shadow and EPT supported 1GB page */
  5688. return PT_PDPE_LEVEL;
  5689. }
  5690. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  5691. {
  5692. struct kvm_cpuid_entry2 *best;
  5693. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5694. u32 exec_control;
  5695. vmx->rdtscp_enabled = false;
  5696. if (vmx_rdtscp_supported()) {
  5697. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5698. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  5699. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  5700. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  5701. vmx->rdtscp_enabled = true;
  5702. else {
  5703. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5704. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5705. exec_control);
  5706. }
  5707. }
  5708. }
  5709. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5710. /* Exposing INVPCID only when PCID is exposed */
  5711. best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  5712. if (vmx_invpcid_supported() &&
  5713. best && (best->ecx & bit(X86_FEATURE_INVPCID)) &&
  5714. guest_cpuid_has_pcid(vcpu)) {
  5715. exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
  5716. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5717. exec_control);
  5718. } else {
  5719. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5720. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5721. exec_control);
  5722. if (best)
  5723. best->ecx &= ~bit(X86_FEATURE_INVPCID);
  5724. }
  5725. }
  5726. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  5727. {
  5728. if (func == 1 && nested)
  5729. entry->ecx |= bit(X86_FEATURE_VMX);
  5730. }
  5731. /*
  5732. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  5733. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  5734. * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
  5735. * guest in a way that will both be appropriate to L1's requests, and our
  5736. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  5737. * function also has additional necessary side-effects, like setting various
  5738. * vcpu->arch fields.
  5739. */
  5740. static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  5741. {
  5742. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5743. u32 exec_control;
  5744. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  5745. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  5746. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  5747. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  5748. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  5749. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  5750. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  5751. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  5752. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  5753. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  5754. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  5755. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  5756. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  5757. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  5758. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  5759. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  5760. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  5761. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  5762. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  5763. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  5764. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  5765. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  5766. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  5767. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  5768. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  5769. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  5770. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  5771. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  5772. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  5773. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  5774. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  5775. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  5776. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  5777. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  5778. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  5779. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  5780. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  5781. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5782. vmcs12->vm_entry_intr_info_field);
  5783. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  5784. vmcs12->vm_entry_exception_error_code);
  5785. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5786. vmcs12->vm_entry_instruction_len);
  5787. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  5788. vmcs12->guest_interruptibility_info);
  5789. vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state);
  5790. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  5791. vmcs_writel(GUEST_DR7, vmcs12->guest_dr7);
  5792. vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
  5793. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  5794. vmcs12->guest_pending_dbg_exceptions);
  5795. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  5796. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  5797. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  5798. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  5799. (vmcs_config.pin_based_exec_ctrl |
  5800. vmcs12->pin_based_vm_exec_control));
  5801. /*
  5802. * Whether page-faults are trapped is determined by a combination of
  5803. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  5804. * If enable_ept, L0 doesn't care about page faults and we should
  5805. * set all of these to L1's desires. However, if !enable_ept, L0 does
  5806. * care about (at least some) page faults, and because it is not easy
  5807. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  5808. * to exit on each and every L2 page fault. This is done by setting
  5809. * MASK=MATCH=0 and (see below) EB.PF=1.
  5810. * Note that below we don't need special code to set EB.PF beyond the
  5811. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  5812. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  5813. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  5814. *
  5815. * A problem with this approach (when !enable_ept) is that L1 may be
  5816. * injected with more page faults than it asked for. This could have
  5817. * caused problems, but in practice existing hypervisors don't care.
  5818. * To fix this, we will need to emulate the PFEC checking (on the L1
  5819. * page tables), using walk_addr(), when injecting PFs to L1.
  5820. */
  5821. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  5822. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  5823. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  5824. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  5825. if (cpu_has_secondary_exec_ctrls()) {
  5826. u32 exec_control = vmx_secondary_exec_control(vmx);
  5827. if (!vmx->rdtscp_enabled)
  5828. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5829. /* Take the following fields only from vmcs12 */
  5830. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5831. if (nested_cpu_has(vmcs12,
  5832. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
  5833. exec_control |= vmcs12->secondary_vm_exec_control;
  5834. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
  5835. /*
  5836. * Translate L1 physical address to host physical
  5837. * address for vmcs02. Keep the page pinned, so this
  5838. * physical address remains valid. We keep a reference
  5839. * to it so we can release it later.
  5840. */
  5841. if (vmx->nested.apic_access_page) /* shouldn't happen */
  5842. nested_release_page(vmx->nested.apic_access_page);
  5843. vmx->nested.apic_access_page =
  5844. nested_get_page(vcpu, vmcs12->apic_access_addr);
  5845. /*
  5846. * If translation failed, no matter: This feature asks
  5847. * to exit when accessing the given address, and if it
  5848. * can never be accessed, this feature won't do
  5849. * anything anyway.
  5850. */
  5851. if (!vmx->nested.apic_access_page)
  5852. exec_control &=
  5853. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5854. else
  5855. vmcs_write64(APIC_ACCESS_ADDR,
  5856. page_to_phys(vmx->nested.apic_access_page));
  5857. }
  5858. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  5859. }
  5860. /*
  5861. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  5862. * Some constant fields are set here by vmx_set_constant_host_state().
  5863. * Other fields are different per CPU, and will be set later when
  5864. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  5865. */
  5866. vmx_set_constant_host_state();
  5867. /*
  5868. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  5869. * entry, but only if the current (host) sp changed from the value
  5870. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  5871. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  5872. * here we just force the write to happen on entry.
  5873. */
  5874. vmx->host_rsp = 0;
  5875. exec_control = vmx_exec_control(vmx); /* L0's desires */
  5876. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  5877. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  5878. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5879. exec_control |= vmcs12->cpu_based_vm_exec_control;
  5880. /*
  5881. * Merging of IO and MSR bitmaps not currently supported.
  5882. * Rather, exit every time.
  5883. */
  5884. exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
  5885. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  5886. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  5887. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  5888. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  5889. * bitwise-or of what L1 wants to trap for L2, and what we want to
  5890. * trap. Note that CR0.TS also needs updating - we do this later.
  5891. */
  5892. update_exception_bitmap(vcpu);
  5893. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  5894. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  5895. /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
  5896. vmcs_write32(VM_EXIT_CONTROLS,
  5897. vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
  5898. vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
  5899. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  5900. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
  5901. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  5902. else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5903. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5904. set_cr4_guest_host_mask(vmx);
  5905. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  5906. vmcs_write64(TSC_OFFSET,
  5907. vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
  5908. else
  5909. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  5910. if (enable_vpid) {
  5911. /*
  5912. * Trivially support vpid by letting L2s share their parent
  5913. * L1's vpid. TODO: move to a more elaborate solution, giving
  5914. * each L2 its own vpid and exposing the vpid feature to L1.
  5915. */
  5916. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5917. vmx_flush_tlb(vcpu);
  5918. }
  5919. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
  5920. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  5921. if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  5922. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  5923. else
  5924. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  5925. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  5926. vmx_set_efer(vcpu, vcpu->arch.efer);
  5927. /*
  5928. * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
  5929. * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
  5930. * The CR0_READ_SHADOW is what L2 should have expected to read given
  5931. * the specifications by L1; It's not enough to take
  5932. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  5933. * have more bits than L1 expected.
  5934. */
  5935. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  5936. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  5937. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  5938. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  5939. /* shadow page tables on either EPT or shadow page tables */
  5940. kvm_set_cr3(vcpu, vmcs12->guest_cr3);
  5941. kvm_mmu_reset_context(vcpu);
  5942. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  5943. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  5944. }
  5945. /*
  5946. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  5947. * for running an L2 nested guest.
  5948. */
  5949. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  5950. {
  5951. struct vmcs12 *vmcs12;
  5952. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5953. int cpu;
  5954. struct loaded_vmcs *vmcs02;
  5955. if (!nested_vmx_check_permission(vcpu) ||
  5956. !nested_vmx_check_vmcs12(vcpu))
  5957. return 1;
  5958. skip_emulated_instruction(vcpu);
  5959. vmcs12 = get_vmcs12(vcpu);
  5960. /*
  5961. * The nested entry process starts with enforcing various prerequisites
  5962. * on vmcs12 as required by the Intel SDM, and act appropriately when
  5963. * they fail: As the SDM explains, some conditions should cause the
  5964. * instruction to fail, while others will cause the instruction to seem
  5965. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  5966. * To speed up the normal (success) code path, we should avoid checking
  5967. * for misconfigurations which will anyway be caught by the processor
  5968. * when using the merged vmcs02.
  5969. */
  5970. if (vmcs12->launch_state == launch) {
  5971. nested_vmx_failValid(vcpu,
  5972. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  5973. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  5974. return 1;
  5975. }
  5976. if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
  5977. !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
  5978. /*TODO: Also verify bits beyond physical address width are 0*/
  5979. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5980. return 1;
  5981. }
  5982. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  5983. !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
  5984. /*TODO: Also verify bits beyond physical address width are 0*/
  5985. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5986. return 1;
  5987. }
  5988. if (vmcs12->vm_entry_msr_load_count > 0 ||
  5989. vmcs12->vm_exit_msr_load_count > 0 ||
  5990. vmcs12->vm_exit_msr_store_count > 0) {
  5991. pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
  5992. __func__);
  5993. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5994. return 1;
  5995. }
  5996. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  5997. nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
  5998. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  5999. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
  6000. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  6001. nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
  6002. !vmx_control_verify(vmcs12->vm_exit_controls,
  6003. nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
  6004. !vmx_control_verify(vmcs12->vm_entry_controls,
  6005. nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
  6006. {
  6007. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  6008. return 1;
  6009. }
  6010. if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  6011. ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  6012. nested_vmx_failValid(vcpu,
  6013. VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
  6014. return 1;
  6015. }
  6016. if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  6017. ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  6018. nested_vmx_entry_failure(vcpu, vmcs12,
  6019. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  6020. return 1;
  6021. }
  6022. if (vmcs12->vmcs_link_pointer != -1ull) {
  6023. nested_vmx_entry_failure(vcpu, vmcs12,
  6024. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
  6025. return 1;
  6026. }
  6027. /*
  6028. * We're finally done with prerequisite checking, and can start with
  6029. * the nested entry.
  6030. */
  6031. vmcs02 = nested_get_current_vmcs02(vmx);
  6032. if (!vmcs02)
  6033. return -ENOMEM;
  6034. enter_guest_mode(vcpu);
  6035. vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
  6036. cpu = get_cpu();
  6037. vmx->loaded_vmcs = vmcs02;
  6038. vmx_vcpu_put(vcpu);
  6039. vmx_vcpu_load(vcpu, cpu);
  6040. vcpu->cpu = cpu;
  6041. put_cpu();
  6042. vmcs12->launch_state = 1;
  6043. prepare_vmcs02(vcpu, vmcs12);
  6044. /*
  6045. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  6046. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  6047. * returned as far as L1 is concerned. It will only return (and set
  6048. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  6049. */
  6050. return 1;
  6051. }
  6052. /*
  6053. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  6054. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  6055. * This function returns the new value we should put in vmcs12.guest_cr0.
  6056. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  6057. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  6058. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  6059. * didn't trap the bit, because if L1 did, so would L0).
  6060. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  6061. * been modified by L2, and L1 knows it. So just leave the old value of
  6062. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  6063. * isn't relevant, because if L0 traps this bit it can set it to anything.
  6064. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  6065. * changed these bits, and therefore they need to be updated, but L0
  6066. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  6067. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  6068. */
  6069. static inline unsigned long
  6070. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  6071. {
  6072. return
  6073. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  6074. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  6075. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  6076. vcpu->arch.cr0_guest_owned_bits));
  6077. }
  6078. static inline unsigned long
  6079. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  6080. {
  6081. return
  6082. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  6083. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  6084. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  6085. vcpu->arch.cr4_guest_owned_bits));
  6086. }
  6087. /*
  6088. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  6089. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  6090. * and this function updates it to reflect the changes to the guest state while
  6091. * L2 was running (and perhaps made some exits which were handled directly by L0
  6092. * without going back to L1), and to reflect the exit reason.
  6093. * Note that we do not have to copy here all VMCS fields, just those that
  6094. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  6095. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  6096. * which already writes to vmcs12 directly.
  6097. */
  6098. void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  6099. {
  6100. /* update guest state fields: */
  6101. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  6102. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  6103. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  6104. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  6105. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  6106. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  6107. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  6108. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  6109. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  6110. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  6111. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  6112. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  6113. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  6114. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  6115. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  6116. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  6117. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  6118. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  6119. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  6120. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  6121. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  6122. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  6123. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  6124. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  6125. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  6126. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  6127. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  6128. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  6129. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  6130. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  6131. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  6132. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  6133. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  6134. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  6135. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  6136. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  6137. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  6138. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  6139. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  6140. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  6141. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  6142. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  6143. vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
  6144. vmcs12->guest_interruptibility_info =
  6145. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  6146. vmcs12->guest_pending_dbg_exceptions =
  6147. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  6148. /* TODO: These cannot have changed unless we have MSR bitmaps and
  6149. * the relevant bit asks not to trap the change */
  6150. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  6151. if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT)
  6152. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  6153. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  6154. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  6155. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  6156. /* update exit information fields: */
  6157. vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON);
  6158. vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6159. vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6160. vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  6161. vmcs12->idt_vectoring_info_field =
  6162. vmcs_read32(IDT_VECTORING_INFO_FIELD);
  6163. vmcs12->idt_vectoring_error_code =
  6164. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6165. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  6166. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6167. /* clear vm-entry fields which are to be cleared on exit */
  6168. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  6169. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  6170. }
  6171. /*
  6172. * A part of what we need to when the nested L2 guest exits and we want to
  6173. * run its L1 parent, is to reset L1's guest state to the host state specified
  6174. * in vmcs12.
  6175. * This function is to be called not only on normal nested exit, but also on
  6176. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  6177. * Failures During or After Loading Guest State").
  6178. * This function should be called when the active VMCS is L1's (vmcs01).
  6179. */
  6180. void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  6181. {
  6182. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  6183. vcpu->arch.efer = vmcs12->host_ia32_efer;
  6184. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  6185. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  6186. else
  6187. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  6188. vmx_set_efer(vcpu, vcpu->arch.efer);
  6189. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  6190. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  6191. /*
  6192. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  6193. * actually changed, because it depends on the current state of
  6194. * fpu_active (which may have changed).
  6195. * Note that vmx_set_cr0 refers to efer set above.
  6196. */
  6197. kvm_set_cr0(vcpu, vmcs12->host_cr0);
  6198. /*
  6199. * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
  6200. * to apply the same changes to L1's vmcs. We just set cr0 correctly,
  6201. * but we also need to update cr0_guest_host_mask and exception_bitmap.
  6202. */
  6203. update_exception_bitmap(vcpu);
  6204. vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
  6205. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  6206. /*
  6207. * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
  6208. * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
  6209. */
  6210. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  6211. kvm_set_cr4(vcpu, vmcs12->host_cr4);
  6212. /* shadow page tables on either EPT or shadow page tables */
  6213. kvm_set_cr3(vcpu, vmcs12->host_cr3);
  6214. kvm_mmu_reset_context(vcpu);
  6215. if (enable_vpid) {
  6216. /*
  6217. * Trivially support vpid by letting L2s share their parent
  6218. * L1's vpid. TODO: move to a more elaborate solution, giving
  6219. * each L2 its own vpid and exposing the vpid feature to L1.
  6220. */
  6221. vmx_flush_tlb(vcpu);
  6222. }
  6223. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  6224. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  6225. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  6226. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  6227. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  6228. vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
  6229. vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
  6230. vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
  6231. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
  6232. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
  6233. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
  6234. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
  6235. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
  6236. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
  6237. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
  6238. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
  6239. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  6240. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  6241. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  6242. vmcs12->host_ia32_perf_global_ctrl);
  6243. }
  6244. /*
  6245. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  6246. * and modify vmcs12 to make it see what it would expect to see there if
  6247. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  6248. */
  6249. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
  6250. {
  6251. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6252. int cpu;
  6253. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6254. leave_guest_mode(vcpu);
  6255. prepare_vmcs12(vcpu, vmcs12);
  6256. cpu = get_cpu();
  6257. vmx->loaded_vmcs = &vmx->vmcs01;
  6258. vmx_vcpu_put(vcpu);
  6259. vmx_vcpu_load(vcpu, cpu);
  6260. vcpu->cpu = cpu;
  6261. put_cpu();
  6262. /* if no vmcs02 cache requested, remove the one we used */
  6263. if (VMCS02_POOL_SIZE == 0)
  6264. nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
  6265. load_vmcs12_host_state(vcpu, vmcs12);
  6266. /* Update TSC_OFFSET if TSC was changed while L2 ran */
  6267. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  6268. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  6269. vmx->host_rsp = 0;
  6270. /* Unpin physical memory we referred to in vmcs02 */
  6271. if (vmx->nested.apic_access_page) {
  6272. nested_release_page(vmx->nested.apic_access_page);
  6273. vmx->nested.apic_access_page = 0;
  6274. }
  6275. /*
  6276. * Exiting from L2 to L1, we're now back to L1 which thinks it just
  6277. * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
  6278. * success or failure flag accordingly.
  6279. */
  6280. if (unlikely(vmx->fail)) {
  6281. vmx->fail = 0;
  6282. nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
  6283. } else
  6284. nested_vmx_succeed(vcpu);
  6285. }
  6286. /*
  6287. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  6288. * 23.7 "VM-entry failures during or after loading guest state" (this also
  6289. * lists the acceptable exit-reason and exit-qualification parameters).
  6290. * It should only be called before L2 actually succeeded to run, and when
  6291. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  6292. */
  6293. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  6294. struct vmcs12 *vmcs12,
  6295. u32 reason, unsigned long qualification)
  6296. {
  6297. load_vmcs12_host_state(vcpu, vmcs12);
  6298. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  6299. vmcs12->exit_qualification = qualification;
  6300. nested_vmx_succeed(vcpu);
  6301. }
  6302. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  6303. struct x86_instruction_info *info,
  6304. enum x86_intercept_stage stage)
  6305. {
  6306. return X86EMUL_CONTINUE;
  6307. }
  6308. static struct kvm_x86_ops vmx_x86_ops = {
  6309. .cpu_has_kvm_support = cpu_has_kvm_support,
  6310. .disabled_by_bios = vmx_disabled_by_bios,
  6311. .hardware_setup = hardware_setup,
  6312. .hardware_unsetup = hardware_unsetup,
  6313. .check_processor_compatibility = vmx_check_processor_compat,
  6314. .hardware_enable = hardware_enable,
  6315. .hardware_disable = hardware_disable,
  6316. .cpu_has_accelerated_tpr = report_flexpriority,
  6317. .vcpu_create = vmx_create_vcpu,
  6318. .vcpu_free = vmx_free_vcpu,
  6319. .vcpu_reset = vmx_vcpu_reset,
  6320. .prepare_guest_switch = vmx_save_host_state,
  6321. .vcpu_load = vmx_vcpu_load,
  6322. .vcpu_put = vmx_vcpu_put,
  6323. .set_guest_debug = set_guest_debug,
  6324. .get_msr = vmx_get_msr,
  6325. .set_msr = vmx_set_msr,
  6326. .get_segment_base = vmx_get_segment_base,
  6327. .get_segment = vmx_get_segment,
  6328. .set_segment = vmx_set_segment,
  6329. .get_cpl = vmx_get_cpl,
  6330. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  6331. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  6332. .decache_cr3 = vmx_decache_cr3,
  6333. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  6334. .set_cr0 = vmx_set_cr0,
  6335. .set_cr3 = vmx_set_cr3,
  6336. .set_cr4 = vmx_set_cr4,
  6337. .set_efer = vmx_set_efer,
  6338. .get_idt = vmx_get_idt,
  6339. .set_idt = vmx_set_idt,
  6340. .get_gdt = vmx_get_gdt,
  6341. .set_gdt = vmx_set_gdt,
  6342. .set_dr7 = vmx_set_dr7,
  6343. .cache_reg = vmx_cache_reg,
  6344. .get_rflags = vmx_get_rflags,
  6345. .set_rflags = vmx_set_rflags,
  6346. .fpu_activate = vmx_fpu_activate,
  6347. .fpu_deactivate = vmx_fpu_deactivate,
  6348. .tlb_flush = vmx_flush_tlb,
  6349. .run = vmx_vcpu_run,
  6350. .handle_exit = vmx_handle_exit,
  6351. .skip_emulated_instruction = skip_emulated_instruction,
  6352. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  6353. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  6354. .patch_hypercall = vmx_patch_hypercall,
  6355. .set_irq = vmx_inject_irq,
  6356. .set_nmi = vmx_inject_nmi,
  6357. .queue_exception = vmx_queue_exception,
  6358. .cancel_injection = vmx_cancel_injection,
  6359. .interrupt_allowed = vmx_interrupt_allowed,
  6360. .nmi_allowed = vmx_nmi_allowed,
  6361. .get_nmi_mask = vmx_get_nmi_mask,
  6362. .set_nmi_mask = vmx_set_nmi_mask,
  6363. .enable_nmi_window = enable_nmi_window,
  6364. .enable_irq_window = enable_irq_window,
  6365. .update_cr8_intercept = update_cr8_intercept,
  6366. .set_tss_addr = vmx_set_tss_addr,
  6367. .get_tdp_level = get_ept_level,
  6368. .get_mt_mask = vmx_get_mt_mask,
  6369. .get_exit_info = vmx_get_exit_info,
  6370. .get_lpage_level = vmx_get_lpage_level,
  6371. .cpuid_update = vmx_cpuid_update,
  6372. .rdtscp_supported = vmx_rdtscp_supported,
  6373. .invpcid_supported = vmx_invpcid_supported,
  6374. .set_supported_cpuid = vmx_set_supported_cpuid,
  6375. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  6376. .set_tsc_khz = vmx_set_tsc_khz,
  6377. .write_tsc_offset = vmx_write_tsc_offset,
  6378. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  6379. .compute_tsc_offset = vmx_compute_tsc_offset,
  6380. .read_l1_tsc = vmx_read_l1_tsc,
  6381. .set_tdp_cr3 = vmx_set_cr3,
  6382. .check_intercept = vmx_check_intercept,
  6383. };
  6384. static int __init vmx_init(void)
  6385. {
  6386. int r, i;
  6387. rdmsrl_safe(MSR_EFER, &host_efer);
  6388. for (i = 0; i < NR_VMX_MSR; ++i)
  6389. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6390. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  6391. if (!vmx_io_bitmap_a)
  6392. return -ENOMEM;
  6393. r = -ENOMEM;
  6394. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  6395. if (!vmx_io_bitmap_b)
  6396. goto out;
  6397. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  6398. if (!vmx_msr_bitmap_legacy)
  6399. goto out1;
  6400. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  6401. if (!vmx_msr_bitmap_longmode)
  6402. goto out2;
  6403. /*
  6404. * Allow direct access to the PC debug port (it is often used for I/O
  6405. * delays, but the vmexits simply slow things down).
  6406. */
  6407. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  6408. clear_bit(0x80, vmx_io_bitmap_a);
  6409. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  6410. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  6411. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  6412. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6413. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  6414. __alignof__(struct vcpu_vmx), THIS_MODULE);
  6415. if (r)
  6416. goto out3;
  6417. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  6418. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  6419. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  6420. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  6421. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  6422. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  6423. if (enable_ept) {
  6424. kvm_mmu_set_mask_ptes(0ull,
  6425. (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
  6426. (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
  6427. 0ull, VMX_EPT_EXECUTABLE_MASK);
  6428. ept_set_mmio_spte_mask();
  6429. kvm_enable_tdp();
  6430. } else
  6431. kvm_disable_tdp();
  6432. return 0;
  6433. out3:
  6434. free_page((unsigned long)vmx_msr_bitmap_longmode);
  6435. out2:
  6436. free_page((unsigned long)vmx_msr_bitmap_legacy);
  6437. out1:
  6438. free_page((unsigned long)vmx_io_bitmap_b);
  6439. out:
  6440. free_page((unsigned long)vmx_io_bitmap_a);
  6441. return r;
  6442. }
  6443. static void __exit vmx_exit(void)
  6444. {
  6445. free_page((unsigned long)vmx_msr_bitmap_legacy);
  6446. free_page((unsigned long)vmx_msr_bitmap_longmode);
  6447. free_page((unsigned long)vmx_io_bitmap_b);
  6448. free_page((unsigned long)vmx_io_bitmap_a);
  6449. kvm_exit();
  6450. }
  6451. module_init(vmx_init)
  6452. module_exit(vmx_exit)