vmx.c 206 KB

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