vmx.c 197 KB

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