vmx.c 206 KB

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