x86.c 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  10. *
  11. * Authors:
  12. * Avi Kivity <avi@qumranet.com>
  13. * Yaniv Kamay <yaniv@qumranet.com>
  14. * Amit Shah <amit.shah@qumranet.com>
  15. * Ben-Ami Yassour <benami@il.ibm.com>
  16. *
  17. * This work is licensed under the terms of the GNU GPL, version 2. See
  18. * the COPYING file in the top-level directory.
  19. *
  20. */
  21. #include <linux/kvm_host.h>
  22. #include "irq.h"
  23. #include "mmu.h"
  24. #include "i8254.h"
  25. #include "tss.h"
  26. #include "kvm_cache_regs.h"
  27. #include "x86.h"
  28. #include <linux/clocksource.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/kvm.h>
  31. #include <linux/fs.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/module.h>
  34. #include <linux/mman.h>
  35. #include <linux/highmem.h>
  36. #include <linux/iommu.h>
  37. #include <linux/intel-iommu.h>
  38. #include <linux/cpufreq.h>
  39. #include <linux/user-return-notifier.h>
  40. #include <linux/srcu.h>
  41. #include <linux/slab.h>
  42. #include <linux/perf_event.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/hash.h>
  45. #include <trace/events/kvm.h>
  46. #define CREATE_TRACE_POINTS
  47. #include "trace.h"
  48. #include <asm/debugreg.h>
  49. #include <asm/msr.h>
  50. #include <asm/desc.h>
  51. #include <asm/mtrr.h>
  52. #include <asm/mce.h>
  53. #include <asm/i387.h>
  54. #include <asm/xcr.h>
  55. #include <asm/pvclock.h>
  56. #include <asm/div64.h>
  57. #define MAX_IO_MSRS 256
  58. #define KVM_MAX_MCE_BANKS 32
  59. #define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
  60. /* EFER defaults:
  61. * - enable syscall per default because its emulated by KVM
  62. * - enable LME and LMA per default on 64 bit KVM
  63. */
  64. #ifdef CONFIG_X86_64
  65. static
  66. u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
  67. #else
  68. static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
  69. #endif
  70. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  71. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  72. static void update_cr8_intercept(struct kvm_vcpu *vcpu);
  73. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  74. struct kvm_cpuid_entry2 __user *entries);
  75. struct kvm_x86_ops *kvm_x86_ops;
  76. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  77. int ignore_msrs = 0;
  78. module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
  79. bool kvm_has_tsc_control;
  80. EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
  81. u32 kvm_max_guest_tsc_khz;
  82. EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
  83. #define KVM_NR_SHARED_MSRS 16
  84. struct kvm_shared_msrs_global {
  85. int nr;
  86. u32 msrs[KVM_NR_SHARED_MSRS];
  87. };
  88. struct kvm_shared_msrs {
  89. struct user_return_notifier urn;
  90. bool registered;
  91. struct kvm_shared_msr_values {
  92. u64 host;
  93. u64 curr;
  94. } values[KVM_NR_SHARED_MSRS];
  95. };
  96. static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
  97. static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
  98. struct kvm_stats_debugfs_item debugfs_entries[] = {
  99. { "pf_fixed", VCPU_STAT(pf_fixed) },
  100. { "pf_guest", VCPU_STAT(pf_guest) },
  101. { "tlb_flush", VCPU_STAT(tlb_flush) },
  102. { "invlpg", VCPU_STAT(invlpg) },
  103. { "exits", VCPU_STAT(exits) },
  104. { "io_exits", VCPU_STAT(io_exits) },
  105. { "mmio_exits", VCPU_STAT(mmio_exits) },
  106. { "signal_exits", VCPU_STAT(signal_exits) },
  107. { "irq_window", VCPU_STAT(irq_window_exits) },
  108. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  109. { "halt_exits", VCPU_STAT(halt_exits) },
  110. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  111. { "hypercalls", VCPU_STAT(hypercalls) },
  112. { "request_irq", VCPU_STAT(request_irq_exits) },
  113. { "irq_exits", VCPU_STAT(irq_exits) },
  114. { "host_state_reload", VCPU_STAT(host_state_reload) },
  115. { "efer_reload", VCPU_STAT(efer_reload) },
  116. { "fpu_reload", VCPU_STAT(fpu_reload) },
  117. { "insn_emulation", VCPU_STAT(insn_emulation) },
  118. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  119. { "irq_injections", VCPU_STAT(irq_injections) },
  120. { "nmi_injections", VCPU_STAT(nmi_injections) },
  121. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  122. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  123. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  124. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  125. { "mmu_flooded", VM_STAT(mmu_flooded) },
  126. { "mmu_recycled", VM_STAT(mmu_recycled) },
  127. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  128. { "mmu_unsync", VM_STAT(mmu_unsync) },
  129. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  130. { "largepages", VM_STAT(lpages) },
  131. { NULL }
  132. };
  133. u64 __read_mostly host_xcr0;
  134. static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
  135. {
  136. int i;
  137. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
  138. vcpu->arch.apf.gfns[i] = ~0;
  139. }
  140. static void kvm_on_user_return(struct user_return_notifier *urn)
  141. {
  142. unsigned slot;
  143. struct kvm_shared_msrs *locals
  144. = container_of(urn, struct kvm_shared_msrs, urn);
  145. struct kvm_shared_msr_values *values;
  146. for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
  147. values = &locals->values[slot];
  148. if (values->host != values->curr) {
  149. wrmsrl(shared_msrs_global.msrs[slot], values->host);
  150. values->curr = values->host;
  151. }
  152. }
  153. locals->registered = false;
  154. user_return_notifier_unregister(urn);
  155. }
  156. static void shared_msr_update(unsigned slot, u32 msr)
  157. {
  158. struct kvm_shared_msrs *smsr;
  159. u64 value;
  160. smsr = &__get_cpu_var(shared_msrs);
  161. /* only read, and nobody should modify it at this time,
  162. * so don't need lock */
  163. if (slot >= shared_msrs_global.nr) {
  164. printk(KERN_ERR "kvm: invalid MSR slot!");
  165. return;
  166. }
  167. rdmsrl_safe(msr, &value);
  168. smsr->values[slot].host = value;
  169. smsr->values[slot].curr = value;
  170. }
  171. void kvm_define_shared_msr(unsigned slot, u32 msr)
  172. {
  173. if (slot >= shared_msrs_global.nr)
  174. shared_msrs_global.nr = slot + 1;
  175. shared_msrs_global.msrs[slot] = msr;
  176. /* we need ensured the shared_msr_global have been updated */
  177. smp_wmb();
  178. }
  179. EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
  180. static void kvm_shared_msr_cpu_online(void)
  181. {
  182. unsigned i;
  183. for (i = 0; i < shared_msrs_global.nr; ++i)
  184. shared_msr_update(i, shared_msrs_global.msrs[i]);
  185. }
  186. void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
  187. {
  188. struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
  189. if (((value ^ smsr->values[slot].curr) & mask) == 0)
  190. return;
  191. smsr->values[slot].curr = value;
  192. wrmsrl(shared_msrs_global.msrs[slot], value);
  193. if (!smsr->registered) {
  194. smsr->urn.on_user_return = kvm_on_user_return;
  195. user_return_notifier_register(&smsr->urn);
  196. smsr->registered = true;
  197. }
  198. }
  199. EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
  200. static void drop_user_return_notifiers(void *ignore)
  201. {
  202. struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
  203. if (smsr->registered)
  204. kvm_on_user_return(&smsr->urn);
  205. }
  206. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  207. {
  208. if (irqchip_in_kernel(vcpu->kvm))
  209. return vcpu->arch.apic_base;
  210. else
  211. return vcpu->arch.apic_base;
  212. }
  213. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  214. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  215. {
  216. /* TODO: reserve bits check */
  217. if (irqchip_in_kernel(vcpu->kvm))
  218. kvm_lapic_set_base(vcpu, data);
  219. else
  220. vcpu->arch.apic_base = data;
  221. }
  222. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  223. #define EXCPT_BENIGN 0
  224. #define EXCPT_CONTRIBUTORY 1
  225. #define EXCPT_PF 2
  226. static int exception_class(int vector)
  227. {
  228. switch (vector) {
  229. case PF_VECTOR:
  230. return EXCPT_PF;
  231. case DE_VECTOR:
  232. case TS_VECTOR:
  233. case NP_VECTOR:
  234. case SS_VECTOR:
  235. case GP_VECTOR:
  236. return EXCPT_CONTRIBUTORY;
  237. default:
  238. break;
  239. }
  240. return EXCPT_BENIGN;
  241. }
  242. static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
  243. unsigned nr, bool has_error, u32 error_code,
  244. bool reinject)
  245. {
  246. u32 prev_nr;
  247. int class1, class2;
  248. kvm_make_request(KVM_REQ_EVENT, vcpu);
  249. if (!vcpu->arch.exception.pending) {
  250. queue:
  251. vcpu->arch.exception.pending = true;
  252. vcpu->arch.exception.has_error_code = has_error;
  253. vcpu->arch.exception.nr = nr;
  254. vcpu->arch.exception.error_code = error_code;
  255. vcpu->arch.exception.reinject = reinject;
  256. return;
  257. }
  258. /* to check exception */
  259. prev_nr = vcpu->arch.exception.nr;
  260. if (prev_nr == DF_VECTOR) {
  261. /* triple fault -> shutdown */
  262. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  263. return;
  264. }
  265. class1 = exception_class(prev_nr);
  266. class2 = exception_class(nr);
  267. if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
  268. || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
  269. /* generate double fault per SDM Table 5-5 */
  270. vcpu->arch.exception.pending = true;
  271. vcpu->arch.exception.has_error_code = true;
  272. vcpu->arch.exception.nr = DF_VECTOR;
  273. vcpu->arch.exception.error_code = 0;
  274. } else
  275. /* replace previous exception with a new one in a hope
  276. that instruction re-execution will regenerate lost
  277. exception */
  278. goto queue;
  279. }
  280. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  281. {
  282. kvm_multiple_exception(vcpu, nr, false, 0, false);
  283. }
  284. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  285. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  286. {
  287. kvm_multiple_exception(vcpu, nr, false, 0, true);
  288. }
  289. EXPORT_SYMBOL_GPL(kvm_requeue_exception);
  290. void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
  291. {
  292. if (err)
  293. kvm_inject_gp(vcpu, 0);
  294. else
  295. kvm_x86_ops->skip_emulated_instruction(vcpu);
  296. }
  297. EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
  298. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
  299. {
  300. ++vcpu->stat.pf_guest;
  301. vcpu->arch.cr2 = fault->address;
  302. kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
  303. }
  304. void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
  305. {
  306. if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
  307. vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
  308. else
  309. vcpu->arch.mmu.inject_page_fault(vcpu, fault);
  310. }
  311. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  312. {
  313. kvm_make_request(KVM_REQ_EVENT, vcpu);
  314. vcpu->arch.nmi_pending = 1;
  315. }
  316. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  317. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  318. {
  319. kvm_multiple_exception(vcpu, nr, true, error_code, false);
  320. }
  321. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  322. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  323. {
  324. kvm_multiple_exception(vcpu, nr, true, error_code, true);
  325. }
  326. EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
  327. /*
  328. * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
  329. * a #GP and return false.
  330. */
  331. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
  332. {
  333. if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
  334. return true;
  335. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  336. return false;
  337. }
  338. EXPORT_SYMBOL_GPL(kvm_require_cpl);
  339. /*
  340. * This function will be used to read from the physical memory of the currently
  341. * running guest. The difference to kvm_read_guest_page is that this function
  342. * can read from guest physical or from the guest's guest physical memory.
  343. */
  344. int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  345. gfn_t ngfn, void *data, int offset, int len,
  346. u32 access)
  347. {
  348. gfn_t real_gfn;
  349. gpa_t ngpa;
  350. ngpa = gfn_to_gpa(ngfn);
  351. real_gfn = mmu->translate_gpa(vcpu, ngpa, access);
  352. if (real_gfn == UNMAPPED_GVA)
  353. return -EFAULT;
  354. real_gfn = gpa_to_gfn(real_gfn);
  355. return kvm_read_guest_page(vcpu->kvm, real_gfn, data, offset, len);
  356. }
  357. EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
  358. int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
  359. void *data, int offset, int len, u32 access)
  360. {
  361. return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
  362. data, offset, len, access);
  363. }
  364. /*
  365. * Load the pae pdptrs. Return true is they are all valid.
  366. */
  367. int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
  368. {
  369. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  370. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  371. int i;
  372. int ret;
  373. u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
  374. ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
  375. offset * sizeof(u64), sizeof(pdpte),
  376. PFERR_USER_MASK|PFERR_WRITE_MASK);
  377. if (ret < 0) {
  378. ret = 0;
  379. goto out;
  380. }
  381. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  382. if (is_present_gpte(pdpte[i]) &&
  383. (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
  384. ret = 0;
  385. goto out;
  386. }
  387. }
  388. ret = 1;
  389. memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
  390. __set_bit(VCPU_EXREG_PDPTR,
  391. (unsigned long *)&vcpu->arch.regs_avail);
  392. __set_bit(VCPU_EXREG_PDPTR,
  393. (unsigned long *)&vcpu->arch.regs_dirty);
  394. out:
  395. return ret;
  396. }
  397. EXPORT_SYMBOL_GPL(load_pdptrs);
  398. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  399. {
  400. u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
  401. bool changed = true;
  402. int offset;
  403. gfn_t gfn;
  404. int r;
  405. if (is_long_mode(vcpu) || !is_pae(vcpu))
  406. return false;
  407. if (!test_bit(VCPU_EXREG_PDPTR,
  408. (unsigned long *)&vcpu->arch.regs_avail))
  409. return true;
  410. gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
  411. offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
  412. r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
  413. PFERR_USER_MASK | PFERR_WRITE_MASK);
  414. if (r < 0)
  415. goto out;
  416. changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
  417. out:
  418. return changed;
  419. }
  420. int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  421. {
  422. unsigned long old_cr0 = kvm_read_cr0(vcpu);
  423. unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
  424. X86_CR0_CD | X86_CR0_NW;
  425. cr0 |= X86_CR0_ET;
  426. #ifdef CONFIG_X86_64
  427. if (cr0 & 0xffffffff00000000UL)
  428. return 1;
  429. #endif
  430. cr0 &= ~CR0_RESERVED_BITS;
  431. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
  432. return 1;
  433. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
  434. return 1;
  435. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  436. #ifdef CONFIG_X86_64
  437. if ((vcpu->arch.efer & EFER_LME)) {
  438. int cs_db, cs_l;
  439. if (!is_pae(vcpu))
  440. return 1;
  441. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  442. if (cs_l)
  443. return 1;
  444. } else
  445. #endif
  446. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
  447. kvm_read_cr3(vcpu)))
  448. return 1;
  449. }
  450. kvm_x86_ops->set_cr0(vcpu, cr0);
  451. if ((cr0 ^ old_cr0) & X86_CR0_PG) {
  452. kvm_clear_async_pf_completion_queue(vcpu);
  453. kvm_async_pf_hash_reset(vcpu);
  454. }
  455. if ((cr0 ^ old_cr0) & update_bits)
  456. kvm_mmu_reset_context(vcpu);
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  460. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  461. {
  462. (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
  463. }
  464. EXPORT_SYMBOL_GPL(kvm_lmsw);
  465. int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  466. {
  467. u64 xcr0;
  468. /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
  469. if (index != XCR_XFEATURE_ENABLED_MASK)
  470. return 1;
  471. xcr0 = xcr;
  472. if (kvm_x86_ops->get_cpl(vcpu) != 0)
  473. return 1;
  474. if (!(xcr0 & XSTATE_FP))
  475. return 1;
  476. if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
  477. return 1;
  478. if (xcr0 & ~host_xcr0)
  479. return 1;
  480. vcpu->arch.xcr0 = xcr0;
  481. vcpu->guest_xcr0_loaded = 0;
  482. return 0;
  483. }
  484. int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  485. {
  486. if (__kvm_set_xcr(vcpu, index, xcr)) {
  487. kvm_inject_gp(vcpu, 0);
  488. return 1;
  489. }
  490. return 0;
  491. }
  492. EXPORT_SYMBOL_GPL(kvm_set_xcr);
  493. static bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu)
  494. {
  495. struct kvm_cpuid_entry2 *best;
  496. best = kvm_find_cpuid_entry(vcpu, 1, 0);
  497. return best && (best->ecx & bit(X86_FEATURE_XSAVE));
  498. }
  499. static void update_cpuid(struct kvm_vcpu *vcpu)
  500. {
  501. struct kvm_cpuid_entry2 *best;
  502. best = kvm_find_cpuid_entry(vcpu, 1, 0);
  503. if (!best)
  504. return;
  505. /* Update OSXSAVE bit */
  506. if (cpu_has_xsave && best->function == 0x1) {
  507. best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
  508. if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
  509. best->ecx |= bit(X86_FEATURE_OSXSAVE);
  510. }
  511. }
  512. int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  513. {
  514. unsigned long old_cr4 = kvm_read_cr4(vcpu);
  515. unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
  516. if (cr4 & CR4_RESERVED_BITS)
  517. return 1;
  518. if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
  519. return 1;
  520. if (is_long_mode(vcpu)) {
  521. if (!(cr4 & X86_CR4_PAE))
  522. return 1;
  523. } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
  524. && ((cr4 ^ old_cr4) & pdptr_bits)
  525. && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
  526. kvm_read_cr3(vcpu)))
  527. return 1;
  528. if (cr4 & X86_CR4_VMXE)
  529. return 1;
  530. kvm_x86_ops->set_cr4(vcpu, cr4);
  531. if ((cr4 ^ old_cr4) & pdptr_bits)
  532. kvm_mmu_reset_context(vcpu);
  533. if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
  534. update_cpuid(vcpu);
  535. return 0;
  536. }
  537. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  538. int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  539. {
  540. if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
  541. kvm_mmu_sync_roots(vcpu);
  542. kvm_mmu_flush_tlb(vcpu);
  543. return 0;
  544. }
  545. if (is_long_mode(vcpu)) {
  546. if (cr3 & CR3_L_MODE_RESERVED_BITS)
  547. return 1;
  548. } else {
  549. if (is_pae(vcpu)) {
  550. if (cr3 & CR3_PAE_RESERVED_BITS)
  551. return 1;
  552. if (is_paging(vcpu) &&
  553. !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
  554. return 1;
  555. }
  556. /*
  557. * We don't check reserved bits in nonpae mode, because
  558. * this isn't enforced, and VMware depends on this.
  559. */
  560. }
  561. /*
  562. * Does the new cr3 value map to physical memory? (Note, we
  563. * catch an invalid cr3 even in real-mode, because it would
  564. * cause trouble later on when we turn on paging anyway.)
  565. *
  566. * A real CPU would silently accept an invalid cr3 and would
  567. * attempt to use it - with largely undefined (and often hard
  568. * to debug) behavior on the guest side.
  569. */
  570. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  571. return 1;
  572. vcpu->arch.cr3 = cr3;
  573. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  574. vcpu->arch.mmu.new_cr3(vcpu);
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  578. int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  579. {
  580. if (cr8 & CR8_RESERVED_BITS)
  581. return 1;
  582. if (irqchip_in_kernel(vcpu->kvm))
  583. kvm_lapic_set_tpr(vcpu, cr8);
  584. else
  585. vcpu->arch.cr8 = cr8;
  586. return 0;
  587. }
  588. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  589. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  590. {
  591. if (irqchip_in_kernel(vcpu->kvm))
  592. return kvm_lapic_get_cr8(vcpu);
  593. else
  594. return vcpu->arch.cr8;
  595. }
  596. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  597. static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  598. {
  599. switch (dr) {
  600. case 0 ... 3:
  601. vcpu->arch.db[dr] = val;
  602. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  603. vcpu->arch.eff_db[dr] = val;
  604. break;
  605. case 4:
  606. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  607. return 1; /* #UD */
  608. /* fall through */
  609. case 6:
  610. if (val & 0xffffffff00000000ULL)
  611. return -1; /* #GP */
  612. vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
  613. break;
  614. case 5:
  615. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  616. return 1; /* #UD */
  617. /* fall through */
  618. default: /* 7 */
  619. if (val & 0xffffffff00000000ULL)
  620. return -1; /* #GP */
  621. vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
  622. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
  623. kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7);
  624. vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK);
  625. }
  626. break;
  627. }
  628. return 0;
  629. }
  630. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  631. {
  632. int res;
  633. res = __kvm_set_dr(vcpu, dr, val);
  634. if (res > 0)
  635. kvm_queue_exception(vcpu, UD_VECTOR);
  636. else if (res < 0)
  637. kvm_inject_gp(vcpu, 0);
  638. return res;
  639. }
  640. EXPORT_SYMBOL_GPL(kvm_set_dr);
  641. static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
  642. {
  643. switch (dr) {
  644. case 0 ... 3:
  645. *val = vcpu->arch.db[dr];
  646. break;
  647. case 4:
  648. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  649. return 1;
  650. /* fall through */
  651. case 6:
  652. *val = vcpu->arch.dr6;
  653. break;
  654. case 5:
  655. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  656. return 1;
  657. /* fall through */
  658. default: /* 7 */
  659. *val = vcpu->arch.dr7;
  660. break;
  661. }
  662. return 0;
  663. }
  664. int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
  665. {
  666. if (_kvm_get_dr(vcpu, dr, val)) {
  667. kvm_queue_exception(vcpu, UD_VECTOR);
  668. return 1;
  669. }
  670. return 0;
  671. }
  672. EXPORT_SYMBOL_GPL(kvm_get_dr);
  673. /*
  674. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  675. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  676. *
  677. * This list is modified at module load time to reflect the
  678. * capabilities of the host cpu. This capabilities test skips MSRs that are
  679. * kvm-specific. Those are put in the beginning of the list.
  680. */
  681. #define KVM_SAVE_MSRS_BEGIN 8
  682. static u32 msrs_to_save[] = {
  683. MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  684. MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
  685. HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
  686. HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN,
  687. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  688. MSR_STAR,
  689. #ifdef CONFIG_X86_64
  690. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  691. #endif
  692. MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
  693. };
  694. static unsigned num_msrs_to_save;
  695. static u32 emulated_msrs[] = {
  696. MSR_IA32_MISC_ENABLE,
  697. MSR_IA32_MCG_STATUS,
  698. MSR_IA32_MCG_CTL,
  699. };
  700. static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
  701. {
  702. u64 old_efer = vcpu->arch.efer;
  703. if (efer & efer_reserved_bits)
  704. return 1;
  705. if (is_paging(vcpu)
  706. && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
  707. return 1;
  708. if (efer & EFER_FFXSR) {
  709. struct kvm_cpuid_entry2 *feat;
  710. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  711. if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
  712. return 1;
  713. }
  714. if (efer & EFER_SVME) {
  715. struct kvm_cpuid_entry2 *feat;
  716. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  717. if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
  718. return 1;
  719. }
  720. efer &= ~EFER_LMA;
  721. efer |= vcpu->arch.efer & EFER_LMA;
  722. kvm_x86_ops->set_efer(vcpu, efer);
  723. vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
  724. /* Update reserved bits */
  725. if ((efer ^ old_efer) & EFER_NX)
  726. kvm_mmu_reset_context(vcpu);
  727. return 0;
  728. }
  729. void kvm_enable_efer_bits(u64 mask)
  730. {
  731. efer_reserved_bits &= ~mask;
  732. }
  733. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  734. /*
  735. * Writes msr value into into the appropriate "register".
  736. * Returns 0 on success, non-0 otherwise.
  737. * Assumes vcpu_load() was already called.
  738. */
  739. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  740. {
  741. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  742. }
  743. /*
  744. * Adapt set_msr() to msr_io()'s calling convention
  745. */
  746. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  747. {
  748. return kvm_set_msr(vcpu, index, *data);
  749. }
  750. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  751. {
  752. int version;
  753. int r;
  754. struct pvclock_wall_clock wc;
  755. struct timespec boot;
  756. if (!wall_clock)
  757. return;
  758. r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
  759. if (r)
  760. return;
  761. if (version & 1)
  762. ++version; /* first time write, random junk */
  763. ++version;
  764. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  765. /*
  766. * The guest calculates current wall clock time by adding
  767. * system time (updated by kvm_guest_time_update below) to the
  768. * wall clock specified here. guest system time equals host
  769. * system time for us, thus we must fill in host boot time here.
  770. */
  771. getboottime(&boot);
  772. wc.sec = boot.tv_sec;
  773. wc.nsec = boot.tv_nsec;
  774. wc.version = version;
  775. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  776. version++;
  777. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  778. }
  779. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  780. {
  781. uint32_t quotient, remainder;
  782. /* Don't try to replace with do_div(), this one calculates
  783. * "(dividend << 32) / divisor" */
  784. __asm__ ( "divl %4"
  785. : "=a" (quotient), "=d" (remainder)
  786. : "0" (0), "1" (dividend), "r" (divisor) );
  787. return quotient;
  788. }
  789. static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
  790. s8 *pshift, u32 *pmultiplier)
  791. {
  792. uint64_t scaled64;
  793. int32_t shift = 0;
  794. uint64_t tps64;
  795. uint32_t tps32;
  796. tps64 = base_khz * 1000LL;
  797. scaled64 = scaled_khz * 1000LL;
  798. while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
  799. tps64 >>= 1;
  800. shift--;
  801. }
  802. tps32 = (uint32_t)tps64;
  803. while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
  804. if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
  805. scaled64 >>= 1;
  806. else
  807. tps32 <<= 1;
  808. shift++;
  809. }
  810. *pshift = shift;
  811. *pmultiplier = div_frac(scaled64, tps32);
  812. pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
  813. __func__, base_khz, scaled_khz, shift, *pmultiplier);
  814. }
  815. static inline u64 get_kernel_ns(void)
  816. {
  817. struct timespec ts;
  818. WARN_ON(preemptible());
  819. ktime_get_ts(&ts);
  820. monotonic_to_bootbased(&ts);
  821. return timespec_to_ns(&ts);
  822. }
  823. static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
  824. unsigned long max_tsc_khz;
  825. static inline int kvm_tsc_changes_freq(void)
  826. {
  827. int cpu = get_cpu();
  828. int ret = !boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
  829. cpufreq_quick_get(cpu) != 0;
  830. put_cpu();
  831. return ret;
  832. }
  833. static u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu)
  834. {
  835. if (vcpu->arch.virtual_tsc_khz)
  836. return vcpu->arch.virtual_tsc_khz;
  837. else
  838. return __this_cpu_read(cpu_tsc_khz);
  839. }
  840. static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
  841. {
  842. u64 ret;
  843. WARN_ON(preemptible());
  844. if (kvm_tsc_changes_freq())
  845. printk_once(KERN_WARNING
  846. "kvm: unreliable cycle conversion on adjustable rate TSC\n");
  847. ret = nsec * vcpu_tsc_khz(vcpu);
  848. do_div(ret, USEC_PER_SEC);
  849. return ret;
  850. }
  851. static void kvm_init_tsc_catchup(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
  852. {
  853. /* Compute a scale to convert nanoseconds in TSC cycles */
  854. kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
  855. &vcpu->arch.tsc_catchup_shift,
  856. &vcpu->arch.tsc_catchup_mult);
  857. }
  858. static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
  859. {
  860. u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.last_tsc_nsec,
  861. vcpu->arch.tsc_catchup_mult,
  862. vcpu->arch.tsc_catchup_shift);
  863. tsc += vcpu->arch.last_tsc_write;
  864. return tsc;
  865. }
  866. void kvm_write_tsc(struct kvm_vcpu *vcpu, u64 data)
  867. {
  868. struct kvm *kvm = vcpu->kvm;
  869. u64 offset, ns, elapsed;
  870. unsigned long flags;
  871. s64 sdiff;
  872. raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
  873. offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
  874. ns = get_kernel_ns();
  875. elapsed = ns - kvm->arch.last_tsc_nsec;
  876. sdiff = data - kvm->arch.last_tsc_write;
  877. if (sdiff < 0)
  878. sdiff = -sdiff;
  879. /*
  880. * Special case: close write to TSC within 5 seconds of
  881. * another CPU is interpreted as an attempt to synchronize
  882. * The 5 seconds is to accommodate host load / swapping as
  883. * well as any reset of TSC during the boot process.
  884. *
  885. * In that case, for a reliable TSC, we can match TSC offsets,
  886. * or make a best guest using elapsed value.
  887. */
  888. if (sdiff < nsec_to_cycles(vcpu, 5ULL * NSEC_PER_SEC) &&
  889. elapsed < 5ULL * NSEC_PER_SEC) {
  890. if (!check_tsc_unstable()) {
  891. offset = kvm->arch.last_tsc_offset;
  892. pr_debug("kvm: matched tsc offset for %llu\n", data);
  893. } else {
  894. u64 delta = nsec_to_cycles(vcpu, elapsed);
  895. offset += delta;
  896. pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
  897. }
  898. ns = kvm->arch.last_tsc_nsec;
  899. }
  900. kvm->arch.last_tsc_nsec = ns;
  901. kvm->arch.last_tsc_write = data;
  902. kvm->arch.last_tsc_offset = offset;
  903. kvm_x86_ops->write_tsc_offset(vcpu, offset);
  904. raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
  905. /* Reset of TSC must disable overshoot protection below */
  906. vcpu->arch.hv_clock.tsc_timestamp = 0;
  907. vcpu->arch.last_tsc_write = data;
  908. vcpu->arch.last_tsc_nsec = ns;
  909. }
  910. EXPORT_SYMBOL_GPL(kvm_write_tsc);
  911. static int kvm_guest_time_update(struct kvm_vcpu *v)
  912. {
  913. unsigned long flags;
  914. struct kvm_vcpu_arch *vcpu = &v->arch;
  915. void *shared_kaddr;
  916. unsigned long this_tsc_khz;
  917. s64 kernel_ns, max_kernel_ns;
  918. u64 tsc_timestamp;
  919. /* Keep irq disabled to prevent changes to the clock */
  920. local_irq_save(flags);
  921. kvm_get_msr(v, MSR_IA32_TSC, &tsc_timestamp);
  922. kernel_ns = get_kernel_ns();
  923. this_tsc_khz = vcpu_tsc_khz(v);
  924. if (unlikely(this_tsc_khz == 0)) {
  925. local_irq_restore(flags);
  926. kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
  927. return 1;
  928. }
  929. /*
  930. * We may have to catch up the TSC to match elapsed wall clock
  931. * time for two reasons, even if kvmclock is used.
  932. * 1) CPU could have been running below the maximum TSC rate
  933. * 2) Broken TSC compensation resets the base at each VCPU
  934. * entry to avoid unknown leaps of TSC even when running
  935. * again on the same CPU. This may cause apparent elapsed
  936. * time to disappear, and the guest to stand still or run
  937. * very slowly.
  938. */
  939. if (vcpu->tsc_catchup) {
  940. u64 tsc = compute_guest_tsc(v, kernel_ns);
  941. if (tsc > tsc_timestamp) {
  942. kvm_x86_ops->adjust_tsc_offset(v, tsc - tsc_timestamp);
  943. tsc_timestamp = tsc;
  944. }
  945. }
  946. local_irq_restore(flags);
  947. if (!vcpu->time_page)
  948. return 0;
  949. /*
  950. * Time as measured by the TSC may go backwards when resetting the base
  951. * tsc_timestamp. The reason for this is that the TSC resolution is
  952. * higher than the resolution of the other clock scales. Thus, many
  953. * possible measurments of the TSC correspond to one measurement of any
  954. * other clock, and so a spread of values is possible. This is not a
  955. * problem for the computation of the nanosecond clock; with TSC rates
  956. * around 1GHZ, there can only be a few cycles which correspond to one
  957. * nanosecond value, and any path through this code will inevitably
  958. * take longer than that. However, with the kernel_ns value itself,
  959. * the precision may be much lower, down to HZ granularity. If the
  960. * first sampling of TSC against kernel_ns ends in the low part of the
  961. * range, and the second in the high end of the range, we can get:
  962. *
  963. * (TSC - offset_low) * S + kns_old > (TSC - offset_high) * S + kns_new
  964. *
  965. * As the sampling errors potentially range in the thousands of cycles,
  966. * it is possible such a time value has already been observed by the
  967. * guest. To protect against this, we must compute the system time as
  968. * observed by the guest and ensure the new system time is greater.
  969. */
  970. max_kernel_ns = 0;
  971. if (vcpu->hv_clock.tsc_timestamp && vcpu->last_guest_tsc) {
  972. max_kernel_ns = vcpu->last_guest_tsc -
  973. vcpu->hv_clock.tsc_timestamp;
  974. max_kernel_ns = pvclock_scale_delta(max_kernel_ns,
  975. vcpu->hv_clock.tsc_to_system_mul,
  976. vcpu->hv_clock.tsc_shift);
  977. max_kernel_ns += vcpu->last_kernel_ns;
  978. }
  979. if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
  980. kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
  981. &vcpu->hv_clock.tsc_shift,
  982. &vcpu->hv_clock.tsc_to_system_mul);
  983. vcpu->hw_tsc_khz = this_tsc_khz;
  984. }
  985. if (max_kernel_ns > kernel_ns)
  986. kernel_ns = max_kernel_ns;
  987. /* With all the info we got, fill in the values */
  988. vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
  989. vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
  990. vcpu->last_kernel_ns = kernel_ns;
  991. vcpu->last_guest_tsc = tsc_timestamp;
  992. vcpu->hv_clock.flags = 0;
  993. /*
  994. * The interface expects us to write an even number signaling that the
  995. * update is finished. Since the guest won't see the intermediate
  996. * state, we just increase by 2 at the end.
  997. */
  998. vcpu->hv_clock.version += 2;
  999. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  1000. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  1001. sizeof(vcpu->hv_clock));
  1002. kunmap_atomic(shared_kaddr, KM_USER0);
  1003. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  1004. return 0;
  1005. }
  1006. static bool msr_mtrr_valid(unsigned msr)
  1007. {
  1008. switch (msr) {
  1009. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  1010. case MSR_MTRRfix64K_00000:
  1011. case MSR_MTRRfix16K_80000:
  1012. case MSR_MTRRfix16K_A0000:
  1013. case MSR_MTRRfix4K_C0000:
  1014. case MSR_MTRRfix4K_C8000:
  1015. case MSR_MTRRfix4K_D0000:
  1016. case MSR_MTRRfix4K_D8000:
  1017. case MSR_MTRRfix4K_E0000:
  1018. case MSR_MTRRfix4K_E8000:
  1019. case MSR_MTRRfix4K_F0000:
  1020. case MSR_MTRRfix4K_F8000:
  1021. case MSR_MTRRdefType:
  1022. case MSR_IA32_CR_PAT:
  1023. return true;
  1024. case 0x2f8:
  1025. return true;
  1026. }
  1027. return false;
  1028. }
  1029. static bool valid_pat_type(unsigned t)
  1030. {
  1031. return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
  1032. }
  1033. static bool valid_mtrr_type(unsigned t)
  1034. {
  1035. return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
  1036. }
  1037. static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1038. {
  1039. int i;
  1040. if (!msr_mtrr_valid(msr))
  1041. return false;
  1042. if (msr == MSR_IA32_CR_PAT) {
  1043. for (i = 0; i < 8; i++)
  1044. if (!valid_pat_type((data >> (i * 8)) & 0xff))
  1045. return false;
  1046. return true;
  1047. } else if (msr == MSR_MTRRdefType) {
  1048. if (data & ~0xcff)
  1049. return false;
  1050. return valid_mtrr_type(data & 0xff);
  1051. } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
  1052. for (i = 0; i < 8 ; i++)
  1053. if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
  1054. return false;
  1055. return true;
  1056. }
  1057. /* variable MTRRs */
  1058. return valid_mtrr_type(data & 0xff);
  1059. }
  1060. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1061. {
  1062. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1063. if (!mtrr_valid(vcpu, msr, data))
  1064. return 1;
  1065. if (msr == MSR_MTRRdefType) {
  1066. vcpu->arch.mtrr_state.def_type = data;
  1067. vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
  1068. } else if (msr == MSR_MTRRfix64K_00000)
  1069. p[0] = data;
  1070. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1071. p[1 + msr - MSR_MTRRfix16K_80000] = data;
  1072. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1073. p[3 + msr - MSR_MTRRfix4K_C0000] = data;
  1074. else if (msr == MSR_IA32_CR_PAT)
  1075. vcpu->arch.pat = data;
  1076. else { /* Variable MTRRs */
  1077. int idx, is_mtrr_mask;
  1078. u64 *pt;
  1079. idx = (msr - 0x200) / 2;
  1080. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1081. if (!is_mtrr_mask)
  1082. pt =
  1083. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1084. else
  1085. pt =
  1086. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1087. *pt = data;
  1088. }
  1089. kvm_mmu_reset_context(vcpu);
  1090. return 0;
  1091. }
  1092. static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1093. {
  1094. u64 mcg_cap = vcpu->arch.mcg_cap;
  1095. unsigned bank_num = mcg_cap & 0xff;
  1096. switch (msr) {
  1097. case MSR_IA32_MCG_STATUS:
  1098. vcpu->arch.mcg_status = data;
  1099. break;
  1100. case MSR_IA32_MCG_CTL:
  1101. if (!(mcg_cap & MCG_CTL_P))
  1102. return 1;
  1103. if (data != 0 && data != ~(u64)0)
  1104. return -1;
  1105. vcpu->arch.mcg_ctl = data;
  1106. break;
  1107. default:
  1108. if (msr >= MSR_IA32_MC0_CTL &&
  1109. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1110. u32 offset = msr - MSR_IA32_MC0_CTL;
  1111. /* only 0 or all 1s can be written to IA32_MCi_CTL
  1112. * some Linux kernels though clear bit 10 in bank 4 to
  1113. * workaround a BIOS/GART TBL issue on AMD K8s, ignore
  1114. * this to avoid an uncatched #GP in the guest
  1115. */
  1116. if ((offset & 0x3) == 0 &&
  1117. data != 0 && (data | (1 << 10)) != ~(u64)0)
  1118. return -1;
  1119. vcpu->arch.mce_banks[offset] = data;
  1120. break;
  1121. }
  1122. return 1;
  1123. }
  1124. return 0;
  1125. }
  1126. static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
  1127. {
  1128. struct kvm *kvm = vcpu->kvm;
  1129. int lm = is_long_mode(vcpu);
  1130. u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
  1131. : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
  1132. u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
  1133. : kvm->arch.xen_hvm_config.blob_size_32;
  1134. u32 page_num = data & ~PAGE_MASK;
  1135. u64 page_addr = data & PAGE_MASK;
  1136. u8 *page;
  1137. int r;
  1138. r = -E2BIG;
  1139. if (page_num >= blob_size)
  1140. goto out;
  1141. r = -ENOMEM;
  1142. page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  1143. if (!page)
  1144. goto out;
  1145. r = -EFAULT;
  1146. if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
  1147. goto out_free;
  1148. if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
  1149. goto out_free;
  1150. r = 0;
  1151. out_free:
  1152. kfree(page);
  1153. out:
  1154. return r;
  1155. }
  1156. static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  1157. {
  1158. return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  1159. }
  1160. static bool kvm_hv_msr_partition_wide(u32 msr)
  1161. {
  1162. bool r = false;
  1163. switch (msr) {
  1164. case HV_X64_MSR_GUEST_OS_ID:
  1165. case HV_X64_MSR_HYPERCALL:
  1166. r = true;
  1167. break;
  1168. }
  1169. return r;
  1170. }
  1171. static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1172. {
  1173. struct kvm *kvm = vcpu->kvm;
  1174. switch (msr) {
  1175. case HV_X64_MSR_GUEST_OS_ID:
  1176. kvm->arch.hv_guest_os_id = data;
  1177. /* setting guest os id to zero disables hypercall page */
  1178. if (!kvm->arch.hv_guest_os_id)
  1179. kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  1180. break;
  1181. case HV_X64_MSR_HYPERCALL: {
  1182. u64 gfn;
  1183. unsigned long addr;
  1184. u8 instructions[4];
  1185. /* if guest os id is not set hypercall should remain disabled */
  1186. if (!kvm->arch.hv_guest_os_id)
  1187. break;
  1188. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  1189. kvm->arch.hv_hypercall = data;
  1190. break;
  1191. }
  1192. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  1193. addr = gfn_to_hva(kvm, gfn);
  1194. if (kvm_is_error_hva(addr))
  1195. return 1;
  1196. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  1197. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  1198. if (copy_to_user((void __user *)addr, instructions, 4))
  1199. return 1;
  1200. kvm->arch.hv_hypercall = data;
  1201. break;
  1202. }
  1203. default:
  1204. pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  1205. "data 0x%llx\n", msr, data);
  1206. return 1;
  1207. }
  1208. return 0;
  1209. }
  1210. static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1211. {
  1212. switch (msr) {
  1213. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  1214. unsigned long addr;
  1215. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  1216. vcpu->arch.hv_vapic = data;
  1217. break;
  1218. }
  1219. addr = gfn_to_hva(vcpu->kvm, data >>
  1220. HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
  1221. if (kvm_is_error_hva(addr))
  1222. return 1;
  1223. if (clear_user((void __user *)addr, PAGE_SIZE))
  1224. return 1;
  1225. vcpu->arch.hv_vapic = data;
  1226. break;
  1227. }
  1228. case HV_X64_MSR_EOI:
  1229. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  1230. case HV_X64_MSR_ICR:
  1231. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  1232. case HV_X64_MSR_TPR:
  1233. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  1234. default:
  1235. pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  1236. "data 0x%llx\n", msr, data);
  1237. return 1;
  1238. }
  1239. return 0;
  1240. }
  1241. static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
  1242. {
  1243. gpa_t gpa = data & ~0x3f;
  1244. /* Bits 2:5 are resrved, Should be zero */
  1245. if (data & 0x3c)
  1246. return 1;
  1247. vcpu->arch.apf.msr_val = data;
  1248. if (!(data & KVM_ASYNC_PF_ENABLED)) {
  1249. kvm_clear_async_pf_completion_queue(vcpu);
  1250. kvm_async_pf_hash_reset(vcpu);
  1251. return 0;
  1252. }
  1253. if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa))
  1254. return 1;
  1255. vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
  1256. kvm_async_pf_wakeup_all(vcpu);
  1257. return 0;
  1258. }
  1259. static void kvmclock_reset(struct kvm_vcpu *vcpu)
  1260. {
  1261. if (vcpu->arch.time_page) {
  1262. kvm_release_page_dirty(vcpu->arch.time_page);
  1263. vcpu->arch.time_page = NULL;
  1264. }
  1265. }
  1266. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1267. {
  1268. switch (msr) {
  1269. case MSR_EFER:
  1270. return set_efer(vcpu, data);
  1271. case MSR_K7_HWCR:
  1272. data &= ~(u64)0x40; /* ignore flush filter disable */
  1273. data &= ~(u64)0x100; /* ignore ignne emulation enable */
  1274. if (data != 0) {
  1275. pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  1276. data);
  1277. return 1;
  1278. }
  1279. break;
  1280. case MSR_FAM10H_MMIO_CONF_BASE:
  1281. if (data != 0) {
  1282. pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  1283. "0x%llx\n", data);
  1284. return 1;
  1285. }
  1286. break;
  1287. case MSR_AMD64_NB_CFG:
  1288. break;
  1289. case MSR_IA32_DEBUGCTLMSR:
  1290. if (!data) {
  1291. /* We support the non-activated case already */
  1292. break;
  1293. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  1294. /* Values other than LBR and BTF are vendor-specific,
  1295. thus reserved and should throw a #GP */
  1296. return 1;
  1297. }
  1298. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  1299. __func__, data);
  1300. break;
  1301. case MSR_IA32_UCODE_REV:
  1302. case MSR_IA32_UCODE_WRITE:
  1303. case MSR_VM_HSAVE_PA:
  1304. case MSR_AMD64_PATCH_LOADER:
  1305. break;
  1306. case 0x200 ... 0x2ff:
  1307. return set_msr_mtrr(vcpu, msr, data);
  1308. case MSR_IA32_APICBASE:
  1309. kvm_set_apic_base(vcpu, data);
  1310. break;
  1311. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1312. return kvm_x2apic_msr_write(vcpu, msr, data);
  1313. case MSR_IA32_MISC_ENABLE:
  1314. vcpu->arch.ia32_misc_enable_msr = data;
  1315. break;
  1316. case MSR_KVM_WALL_CLOCK_NEW:
  1317. case MSR_KVM_WALL_CLOCK:
  1318. vcpu->kvm->arch.wall_clock = data;
  1319. kvm_write_wall_clock(vcpu->kvm, data);
  1320. break;
  1321. case MSR_KVM_SYSTEM_TIME_NEW:
  1322. case MSR_KVM_SYSTEM_TIME: {
  1323. kvmclock_reset(vcpu);
  1324. vcpu->arch.time = data;
  1325. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1326. /* we verify if the enable bit is set... */
  1327. if (!(data & 1))
  1328. break;
  1329. /* ...but clean it before doing the actual write */
  1330. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  1331. vcpu->arch.time_page =
  1332. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  1333. if (is_error_page(vcpu->arch.time_page)) {
  1334. kvm_release_page_clean(vcpu->arch.time_page);
  1335. vcpu->arch.time_page = NULL;
  1336. }
  1337. break;
  1338. }
  1339. case MSR_KVM_ASYNC_PF_EN:
  1340. if (kvm_pv_enable_async_pf(vcpu, data))
  1341. return 1;
  1342. break;
  1343. case MSR_IA32_MCG_CTL:
  1344. case MSR_IA32_MCG_STATUS:
  1345. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1346. return set_msr_mce(vcpu, msr, data);
  1347. /* Performance counters are not protected by a CPUID bit,
  1348. * so we should check all of them in the generic path for the sake of
  1349. * cross vendor migration.
  1350. * Writing a zero into the event select MSRs disables them,
  1351. * which we perfectly emulate ;-). Any other value should be at least
  1352. * reported, some guests depend on them.
  1353. */
  1354. case MSR_P6_EVNTSEL0:
  1355. case MSR_P6_EVNTSEL1:
  1356. case MSR_K7_EVNTSEL0:
  1357. case MSR_K7_EVNTSEL1:
  1358. case MSR_K7_EVNTSEL2:
  1359. case MSR_K7_EVNTSEL3:
  1360. if (data != 0)
  1361. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1362. "0x%x data 0x%llx\n", msr, data);
  1363. break;
  1364. /* at least RHEL 4 unconditionally writes to the perfctr registers,
  1365. * so we ignore writes to make it happy.
  1366. */
  1367. case MSR_P6_PERFCTR0:
  1368. case MSR_P6_PERFCTR1:
  1369. case MSR_K7_PERFCTR0:
  1370. case MSR_K7_PERFCTR1:
  1371. case MSR_K7_PERFCTR2:
  1372. case MSR_K7_PERFCTR3:
  1373. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1374. "0x%x data 0x%llx\n", msr, data);
  1375. break;
  1376. case MSR_K7_CLK_CTL:
  1377. /*
  1378. * Ignore all writes to this no longer documented MSR.
  1379. * Writes are only relevant for old K7 processors,
  1380. * all pre-dating SVM, but a recommended workaround from
  1381. * AMD for these chips. It is possible to speicify the
  1382. * affected processor models on the command line, hence
  1383. * the need to ignore the workaround.
  1384. */
  1385. break;
  1386. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1387. if (kvm_hv_msr_partition_wide(msr)) {
  1388. int r;
  1389. mutex_lock(&vcpu->kvm->lock);
  1390. r = set_msr_hyperv_pw(vcpu, msr, data);
  1391. mutex_unlock(&vcpu->kvm->lock);
  1392. return r;
  1393. } else
  1394. return set_msr_hyperv(vcpu, msr, data);
  1395. break;
  1396. case MSR_IA32_BBL_CR_CTL3:
  1397. /* Drop writes to this legacy MSR -- see rdmsr
  1398. * counterpart for further detail.
  1399. */
  1400. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
  1401. break;
  1402. default:
  1403. if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
  1404. return xen_hvm_config(vcpu, data);
  1405. if (!ignore_msrs) {
  1406. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
  1407. msr, data);
  1408. return 1;
  1409. } else {
  1410. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
  1411. msr, data);
  1412. break;
  1413. }
  1414. }
  1415. return 0;
  1416. }
  1417. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1418. /*
  1419. * Reads an msr value (of 'msr_index') into 'pdata'.
  1420. * Returns 0 on success, non-0 otherwise.
  1421. * Assumes vcpu_load() was already called.
  1422. */
  1423. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1424. {
  1425. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  1426. }
  1427. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1428. {
  1429. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1430. if (!msr_mtrr_valid(msr))
  1431. return 1;
  1432. if (msr == MSR_MTRRdefType)
  1433. *pdata = vcpu->arch.mtrr_state.def_type +
  1434. (vcpu->arch.mtrr_state.enabled << 10);
  1435. else if (msr == MSR_MTRRfix64K_00000)
  1436. *pdata = p[0];
  1437. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1438. *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
  1439. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1440. *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
  1441. else if (msr == MSR_IA32_CR_PAT)
  1442. *pdata = vcpu->arch.pat;
  1443. else { /* Variable MTRRs */
  1444. int idx, is_mtrr_mask;
  1445. u64 *pt;
  1446. idx = (msr - 0x200) / 2;
  1447. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1448. if (!is_mtrr_mask)
  1449. pt =
  1450. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1451. else
  1452. pt =
  1453. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1454. *pdata = *pt;
  1455. }
  1456. return 0;
  1457. }
  1458. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1459. {
  1460. u64 data;
  1461. u64 mcg_cap = vcpu->arch.mcg_cap;
  1462. unsigned bank_num = mcg_cap & 0xff;
  1463. switch (msr) {
  1464. case MSR_IA32_P5_MC_ADDR:
  1465. case MSR_IA32_P5_MC_TYPE:
  1466. data = 0;
  1467. break;
  1468. case MSR_IA32_MCG_CAP:
  1469. data = vcpu->arch.mcg_cap;
  1470. break;
  1471. case MSR_IA32_MCG_CTL:
  1472. if (!(mcg_cap & MCG_CTL_P))
  1473. return 1;
  1474. data = vcpu->arch.mcg_ctl;
  1475. break;
  1476. case MSR_IA32_MCG_STATUS:
  1477. data = vcpu->arch.mcg_status;
  1478. break;
  1479. default:
  1480. if (msr >= MSR_IA32_MC0_CTL &&
  1481. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1482. u32 offset = msr - MSR_IA32_MC0_CTL;
  1483. data = vcpu->arch.mce_banks[offset];
  1484. break;
  1485. }
  1486. return 1;
  1487. }
  1488. *pdata = data;
  1489. return 0;
  1490. }
  1491. static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1492. {
  1493. u64 data = 0;
  1494. struct kvm *kvm = vcpu->kvm;
  1495. switch (msr) {
  1496. case HV_X64_MSR_GUEST_OS_ID:
  1497. data = kvm->arch.hv_guest_os_id;
  1498. break;
  1499. case HV_X64_MSR_HYPERCALL:
  1500. data = kvm->arch.hv_hypercall;
  1501. break;
  1502. default:
  1503. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1504. return 1;
  1505. }
  1506. *pdata = data;
  1507. return 0;
  1508. }
  1509. static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1510. {
  1511. u64 data = 0;
  1512. switch (msr) {
  1513. case HV_X64_MSR_VP_INDEX: {
  1514. int r;
  1515. struct kvm_vcpu *v;
  1516. kvm_for_each_vcpu(r, v, vcpu->kvm)
  1517. if (v == vcpu)
  1518. data = r;
  1519. break;
  1520. }
  1521. case HV_X64_MSR_EOI:
  1522. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  1523. case HV_X64_MSR_ICR:
  1524. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  1525. case HV_X64_MSR_TPR:
  1526. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  1527. default:
  1528. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1529. return 1;
  1530. }
  1531. *pdata = data;
  1532. return 0;
  1533. }
  1534. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1535. {
  1536. u64 data;
  1537. switch (msr) {
  1538. case MSR_IA32_PLATFORM_ID:
  1539. case MSR_IA32_UCODE_REV:
  1540. case MSR_IA32_EBL_CR_POWERON:
  1541. case MSR_IA32_DEBUGCTLMSR:
  1542. case MSR_IA32_LASTBRANCHFROMIP:
  1543. case MSR_IA32_LASTBRANCHTOIP:
  1544. case MSR_IA32_LASTINTFROMIP:
  1545. case MSR_IA32_LASTINTTOIP:
  1546. case MSR_K8_SYSCFG:
  1547. case MSR_K7_HWCR:
  1548. case MSR_VM_HSAVE_PA:
  1549. case MSR_P6_PERFCTR0:
  1550. case MSR_P6_PERFCTR1:
  1551. case MSR_P6_EVNTSEL0:
  1552. case MSR_P6_EVNTSEL1:
  1553. case MSR_K7_EVNTSEL0:
  1554. case MSR_K7_PERFCTR0:
  1555. case MSR_K8_INT_PENDING_MSG:
  1556. case MSR_AMD64_NB_CFG:
  1557. case MSR_FAM10H_MMIO_CONF_BASE:
  1558. data = 0;
  1559. break;
  1560. case MSR_MTRRcap:
  1561. data = 0x500 | KVM_NR_VAR_MTRR;
  1562. break;
  1563. case 0x200 ... 0x2ff:
  1564. return get_msr_mtrr(vcpu, msr, pdata);
  1565. case 0xcd: /* fsb frequency */
  1566. data = 3;
  1567. break;
  1568. /*
  1569. * MSR_EBC_FREQUENCY_ID
  1570. * Conservative value valid for even the basic CPU models.
  1571. * Models 0,1: 000 in bits 23:21 indicating a bus speed of
  1572. * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
  1573. * and 266MHz for model 3, or 4. Set Core Clock
  1574. * Frequency to System Bus Frequency Ratio to 1 (bits
  1575. * 31:24) even though these are only valid for CPU
  1576. * models > 2, however guests may end up dividing or
  1577. * multiplying by zero otherwise.
  1578. */
  1579. case MSR_EBC_FREQUENCY_ID:
  1580. data = 1 << 24;
  1581. break;
  1582. case MSR_IA32_APICBASE:
  1583. data = kvm_get_apic_base(vcpu);
  1584. break;
  1585. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1586. return kvm_x2apic_msr_read(vcpu, msr, pdata);
  1587. break;
  1588. case MSR_IA32_MISC_ENABLE:
  1589. data = vcpu->arch.ia32_misc_enable_msr;
  1590. break;
  1591. case MSR_IA32_PERF_STATUS:
  1592. /* TSC increment by tick */
  1593. data = 1000ULL;
  1594. /* CPU multiplier */
  1595. data |= (((uint64_t)4ULL) << 40);
  1596. break;
  1597. case MSR_EFER:
  1598. data = vcpu->arch.efer;
  1599. break;
  1600. case MSR_KVM_WALL_CLOCK:
  1601. case MSR_KVM_WALL_CLOCK_NEW:
  1602. data = vcpu->kvm->arch.wall_clock;
  1603. break;
  1604. case MSR_KVM_SYSTEM_TIME:
  1605. case MSR_KVM_SYSTEM_TIME_NEW:
  1606. data = vcpu->arch.time;
  1607. break;
  1608. case MSR_KVM_ASYNC_PF_EN:
  1609. data = vcpu->arch.apf.msr_val;
  1610. break;
  1611. case MSR_IA32_P5_MC_ADDR:
  1612. case MSR_IA32_P5_MC_TYPE:
  1613. case MSR_IA32_MCG_CAP:
  1614. case MSR_IA32_MCG_CTL:
  1615. case MSR_IA32_MCG_STATUS:
  1616. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1617. return get_msr_mce(vcpu, msr, pdata);
  1618. case MSR_K7_CLK_CTL:
  1619. /*
  1620. * Provide expected ramp-up count for K7. All other
  1621. * are set to zero, indicating minimum divisors for
  1622. * every field.
  1623. *
  1624. * This prevents guest kernels on AMD host with CPU
  1625. * type 6, model 8 and higher from exploding due to
  1626. * the rdmsr failing.
  1627. */
  1628. data = 0x20000000;
  1629. break;
  1630. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1631. if (kvm_hv_msr_partition_wide(msr)) {
  1632. int r;
  1633. mutex_lock(&vcpu->kvm->lock);
  1634. r = get_msr_hyperv_pw(vcpu, msr, pdata);
  1635. mutex_unlock(&vcpu->kvm->lock);
  1636. return r;
  1637. } else
  1638. return get_msr_hyperv(vcpu, msr, pdata);
  1639. break;
  1640. case MSR_IA32_BBL_CR_CTL3:
  1641. /* This legacy MSR exists but isn't fully documented in current
  1642. * silicon. It is however accessed by winxp in very narrow
  1643. * scenarios where it sets bit #19, itself documented as
  1644. * a "reserved" bit. Best effort attempt to source coherent
  1645. * read data here should the balance of the register be
  1646. * interpreted by the guest:
  1647. *
  1648. * L2 cache control register 3: 64GB range, 256KB size,
  1649. * enabled, latency 0x1, configured
  1650. */
  1651. data = 0xbe702111;
  1652. break;
  1653. default:
  1654. if (!ignore_msrs) {
  1655. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  1656. return 1;
  1657. } else {
  1658. pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
  1659. data = 0;
  1660. }
  1661. break;
  1662. }
  1663. *pdata = data;
  1664. return 0;
  1665. }
  1666. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1667. /*
  1668. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1669. *
  1670. * @return number of msrs set successfully.
  1671. */
  1672. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1673. struct kvm_msr_entry *entries,
  1674. int (*do_msr)(struct kvm_vcpu *vcpu,
  1675. unsigned index, u64 *data))
  1676. {
  1677. int i, idx;
  1678. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1679. for (i = 0; i < msrs->nmsrs; ++i)
  1680. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1681. break;
  1682. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1683. return i;
  1684. }
  1685. /*
  1686. * Read or write a bunch of msrs. Parameters are user addresses.
  1687. *
  1688. * @return number of msrs set successfully.
  1689. */
  1690. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1691. int (*do_msr)(struct kvm_vcpu *vcpu,
  1692. unsigned index, u64 *data),
  1693. int writeback)
  1694. {
  1695. struct kvm_msrs msrs;
  1696. struct kvm_msr_entry *entries;
  1697. int r, n;
  1698. unsigned size;
  1699. r = -EFAULT;
  1700. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1701. goto out;
  1702. r = -E2BIG;
  1703. if (msrs.nmsrs >= MAX_IO_MSRS)
  1704. goto out;
  1705. r = -ENOMEM;
  1706. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1707. entries = kmalloc(size, GFP_KERNEL);
  1708. if (!entries)
  1709. goto out;
  1710. r = -EFAULT;
  1711. if (copy_from_user(entries, user_msrs->entries, size))
  1712. goto out_free;
  1713. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1714. if (r < 0)
  1715. goto out_free;
  1716. r = -EFAULT;
  1717. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1718. goto out_free;
  1719. r = n;
  1720. out_free:
  1721. kfree(entries);
  1722. out:
  1723. return r;
  1724. }
  1725. int kvm_dev_ioctl_check_extension(long ext)
  1726. {
  1727. int r;
  1728. switch (ext) {
  1729. case KVM_CAP_IRQCHIP:
  1730. case KVM_CAP_HLT:
  1731. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  1732. case KVM_CAP_SET_TSS_ADDR:
  1733. case KVM_CAP_EXT_CPUID:
  1734. case KVM_CAP_CLOCKSOURCE:
  1735. case KVM_CAP_PIT:
  1736. case KVM_CAP_NOP_IO_DELAY:
  1737. case KVM_CAP_MP_STATE:
  1738. case KVM_CAP_SYNC_MMU:
  1739. case KVM_CAP_USER_NMI:
  1740. case KVM_CAP_REINJECT_CONTROL:
  1741. case KVM_CAP_IRQ_INJECT_STATUS:
  1742. case KVM_CAP_ASSIGN_DEV_IRQ:
  1743. case KVM_CAP_IRQFD:
  1744. case KVM_CAP_IOEVENTFD:
  1745. case KVM_CAP_PIT2:
  1746. case KVM_CAP_PIT_STATE2:
  1747. case KVM_CAP_SET_IDENTITY_MAP_ADDR:
  1748. case KVM_CAP_XEN_HVM:
  1749. case KVM_CAP_ADJUST_CLOCK:
  1750. case KVM_CAP_VCPU_EVENTS:
  1751. case KVM_CAP_HYPERV:
  1752. case KVM_CAP_HYPERV_VAPIC:
  1753. case KVM_CAP_HYPERV_SPIN:
  1754. case KVM_CAP_PCI_SEGMENT:
  1755. case KVM_CAP_DEBUGREGS:
  1756. case KVM_CAP_X86_ROBUST_SINGLESTEP:
  1757. case KVM_CAP_XSAVE:
  1758. case KVM_CAP_ASYNC_PF:
  1759. case KVM_CAP_GET_TSC_KHZ:
  1760. r = 1;
  1761. break;
  1762. case KVM_CAP_COALESCED_MMIO:
  1763. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  1764. break;
  1765. case KVM_CAP_VAPIC:
  1766. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1767. break;
  1768. case KVM_CAP_NR_VCPUS:
  1769. r = KVM_MAX_VCPUS;
  1770. break;
  1771. case KVM_CAP_NR_MEMSLOTS:
  1772. r = KVM_MEMORY_SLOTS;
  1773. break;
  1774. case KVM_CAP_PV_MMU: /* obsolete */
  1775. r = 0;
  1776. break;
  1777. case KVM_CAP_IOMMU:
  1778. r = iommu_found();
  1779. break;
  1780. case KVM_CAP_MCE:
  1781. r = KVM_MAX_MCE_BANKS;
  1782. break;
  1783. case KVM_CAP_XCRS:
  1784. r = cpu_has_xsave;
  1785. break;
  1786. case KVM_CAP_TSC_CONTROL:
  1787. r = kvm_has_tsc_control;
  1788. break;
  1789. default:
  1790. r = 0;
  1791. break;
  1792. }
  1793. return r;
  1794. }
  1795. long kvm_arch_dev_ioctl(struct file *filp,
  1796. unsigned int ioctl, unsigned long arg)
  1797. {
  1798. void __user *argp = (void __user *)arg;
  1799. long r;
  1800. switch (ioctl) {
  1801. case KVM_GET_MSR_INDEX_LIST: {
  1802. struct kvm_msr_list __user *user_msr_list = argp;
  1803. struct kvm_msr_list msr_list;
  1804. unsigned n;
  1805. r = -EFAULT;
  1806. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1807. goto out;
  1808. n = msr_list.nmsrs;
  1809. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1810. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1811. goto out;
  1812. r = -E2BIG;
  1813. if (n < msr_list.nmsrs)
  1814. goto out;
  1815. r = -EFAULT;
  1816. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1817. num_msrs_to_save * sizeof(u32)))
  1818. goto out;
  1819. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  1820. &emulated_msrs,
  1821. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1822. goto out;
  1823. r = 0;
  1824. break;
  1825. }
  1826. case KVM_GET_SUPPORTED_CPUID: {
  1827. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1828. struct kvm_cpuid2 cpuid;
  1829. r = -EFAULT;
  1830. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1831. goto out;
  1832. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1833. cpuid_arg->entries);
  1834. if (r)
  1835. goto out;
  1836. r = -EFAULT;
  1837. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1838. goto out;
  1839. r = 0;
  1840. break;
  1841. }
  1842. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  1843. u64 mce_cap;
  1844. mce_cap = KVM_MCE_CAP_SUPPORTED;
  1845. r = -EFAULT;
  1846. if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
  1847. goto out;
  1848. r = 0;
  1849. break;
  1850. }
  1851. default:
  1852. r = -EINVAL;
  1853. }
  1854. out:
  1855. return r;
  1856. }
  1857. static void wbinvd_ipi(void *garbage)
  1858. {
  1859. wbinvd();
  1860. }
  1861. static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
  1862. {
  1863. return vcpu->kvm->arch.iommu_domain &&
  1864. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
  1865. }
  1866. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1867. {
  1868. /* Address WBINVD may be executed by guest */
  1869. if (need_emulate_wbinvd(vcpu)) {
  1870. if (kvm_x86_ops->has_wbinvd_exit())
  1871. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  1872. else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
  1873. smp_call_function_single(vcpu->cpu,
  1874. wbinvd_ipi, NULL, 1);
  1875. }
  1876. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1877. if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
  1878. /* Make sure TSC doesn't go backwards */
  1879. s64 tsc_delta;
  1880. u64 tsc;
  1881. kvm_get_msr(vcpu, MSR_IA32_TSC, &tsc);
  1882. tsc_delta = !vcpu->arch.last_guest_tsc ? 0 :
  1883. tsc - vcpu->arch.last_guest_tsc;
  1884. if (tsc_delta < 0)
  1885. mark_tsc_unstable("KVM discovered backwards TSC");
  1886. if (check_tsc_unstable()) {
  1887. kvm_x86_ops->adjust_tsc_offset(vcpu, -tsc_delta);
  1888. vcpu->arch.tsc_catchup = 1;
  1889. }
  1890. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1891. if (vcpu->cpu != cpu)
  1892. kvm_migrate_timers(vcpu);
  1893. vcpu->cpu = cpu;
  1894. }
  1895. }
  1896. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1897. {
  1898. kvm_x86_ops->vcpu_put(vcpu);
  1899. kvm_put_guest_fpu(vcpu);
  1900. vcpu->arch.last_host_tsc = native_read_tsc();
  1901. }
  1902. static int is_efer_nx(void)
  1903. {
  1904. unsigned long long efer = 0;
  1905. rdmsrl_safe(MSR_EFER, &efer);
  1906. return efer & EFER_NX;
  1907. }
  1908. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1909. {
  1910. int i;
  1911. struct kvm_cpuid_entry2 *e, *entry;
  1912. entry = NULL;
  1913. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1914. e = &vcpu->arch.cpuid_entries[i];
  1915. if (e->function == 0x80000001) {
  1916. entry = e;
  1917. break;
  1918. }
  1919. }
  1920. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1921. entry->edx &= ~(1 << 20);
  1922. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1923. }
  1924. }
  1925. /* when an old userspace process fills a new kernel module */
  1926. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1927. struct kvm_cpuid *cpuid,
  1928. struct kvm_cpuid_entry __user *entries)
  1929. {
  1930. int r, i;
  1931. struct kvm_cpuid_entry *cpuid_entries;
  1932. r = -E2BIG;
  1933. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1934. goto out;
  1935. r = -ENOMEM;
  1936. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1937. if (!cpuid_entries)
  1938. goto out;
  1939. r = -EFAULT;
  1940. if (copy_from_user(cpuid_entries, entries,
  1941. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1942. goto out_free;
  1943. for (i = 0; i < cpuid->nent; i++) {
  1944. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1945. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1946. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1947. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1948. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1949. vcpu->arch.cpuid_entries[i].index = 0;
  1950. vcpu->arch.cpuid_entries[i].flags = 0;
  1951. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1952. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1953. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1954. }
  1955. vcpu->arch.cpuid_nent = cpuid->nent;
  1956. cpuid_fix_nx_cap(vcpu);
  1957. r = 0;
  1958. kvm_apic_set_version(vcpu);
  1959. kvm_x86_ops->cpuid_update(vcpu);
  1960. update_cpuid(vcpu);
  1961. out_free:
  1962. vfree(cpuid_entries);
  1963. out:
  1964. return r;
  1965. }
  1966. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1967. struct kvm_cpuid2 *cpuid,
  1968. struct kvm_cpuid_entry2 __user *entries)
  1969. {
  1970. int r;
  1971. r = -E2BIG;
  1972. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1973. goto out;
  1974. r = -EFAULT;
  1975. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1976. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1977. goto out;
  1978. vcpu->arch.cpuid_nent = cpuid->nent;
  1979. kvm_apic_set_version(vcpu);
  1980. kvm_x86_ops->cpuid_update(vcpu);
  1981. update_cpuid(vcpu);
  1982. return 0;
  1983. out:
  1984. return r;
  1985. }
  1986. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1987. struct kvm_cpuid2 *cpuid,
  1988. struct kvm_cpuid_entry2 __user *entries)
  1989. {
  1990. int r;
  1991. r = -E2BIG;
  1992. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1993. goto out;
  1994. r = -EFAULT;
  1995. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1996. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1997. goto out;
  1998. return 0;
  1999. out:
  2000. cpuid->nent = vcpu->arch.cpuid_nent;
  2001. return r;
  2002. }
  2003. static void cpuid_mask(u32 *word, int wordnum)
  2004. {
  2005. *word &= boot_cpu_data.x86_capability[wordnum];
  2006. }
  2007. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  2008. u32 index)
  2009. {
  2010. entry->function = function;
  2011. entry->index = index;
  2012. cpuid_count(entry->function, entry->index,
  2013. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  2014. entry->flags = 0;
  2015. }
  2016. #define F(x) bit(X86_FEATURE_##x)
  2017. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  2018. u32 index, int *nent, int maxnent)
  2019. {
  2020. unsigned f_nx = is_efer_nx() ? F(NX) : 0;
  2021. #ifdef CONFIG_X86_64
  2022. unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
  2023. ? F(GBPAGES) : 0;
  2024. unsigned f_lm = F(LM);
  2025. #else
  2026. unsigned f_gbpages = 0;
  2027. unsigned f_lm = 0;
  2028. #endif
  2029. unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
  2030. /* cpuid 1.edx */
  2031. const u32 kvm_supported_word0_x86_features =
  2032. F(FPU) | F(VME) | F(DE) | F(PSE) |
  2033. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  2034. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
  2035. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  2036. F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
  2037. 0 /* Reserved, DS, ACPI */ | F(MMX) |
  2038. F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
  2039. 0 /* HTT, TM, Reserved, PBE */;
  2040. /* cpuid 0x80000001.edx */
  2041. const u32 kvm_supported_word1_x86_features =
  2042. F(FPU) | F(VME) | F(DE) | F(PSE) |
  2043. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  2044. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
  2045. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  2046. F(PAT) | F(PSE36) | 0 /* Reserved */ |
  2047. f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
  2048. F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
  2049. 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
  2050. /* cpuid 1.ecx */
  2051. const u32 kvm_supported_word4_x86_features =
  2052. F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
  2053. 0 /* DS-CPL, VMX, SMX, EST */ |
  2054. 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
  2055. 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
  2056. 0 /* Reserved, DCA */ | F(XMM4_1) |
  2057. F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
  2058. 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
  2059. F(F16C);
  2060. /* cpuid 0x80000001.ecx */
  2061. const u32 kvm_supported_word6_x86_features =
  2062. F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
  2063. F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
  2064. F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
  2065. 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
  2066. /* all calls to cpuid_count() should be made on the same cpu */
  2067. get_cpu();
  2068. do_cpuid_1_ent(entry, function, index);
  2069. ++*nent;
  2070. switch (function) {
  2071. case 0:
  2072. entry->eax = min(entry->eax, (u32)0xd);
  2073. break;
  2074. case 1:
  2075. entry->edx &= kvm_supported_word0_x86_features;
  2076. cpuid_mask(&entry->edx, 0);
  2077. entry->ecx &= kvm_supported_word4_x86_features;
  2078. cpuid_mask(&entry->ecx, 4);
  2079. /* we support x2apic emulation even if host does not support
  2080. * it since we emulate x2apic in software */
  2081. entry->ecx |= F(X2APIC);
  2082. break;
  2083. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  2084. * may return different values. This forces us to get_cpu() before
  2085. * issuing the first command, and also to emulate this annoying behavior
  2086. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  2087. case 2: {
  2088. int t, times = entry->eax & 0xff;
  2089. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  2090. entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2091. for (t = 1; t < times && *nent < maxnent; ++t) {
  2092. do_cpuid_1_ent(&entry[t], function, 0);
  2093. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  2094. ++*nent;
  2095. }
  2096. break;
  2097. }
  2098. /* function 4 and 0xb have additional index. */
  2099. case 4: {
  2100. int i, cache_type;
  2101. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  2102. /* read more entries until cache_type is zero */
  2103. for (i = 1; *nent < maxnent; ++i) {
  2104. cache_type = entry[i - 1].eax & 0x1f;
  2105. if (!cache_type)
  2106. break;
  2107. do_cpuid_1_ent(&entry[i], function, i);
  2108. entry[i].flags |=
  2109. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  2110. ++*nent;
  2111. }
  2112. break;
  2113. }
  2114. case 0xb: {
  2115. int i, level_type;
  2116. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  2117. /* read more entries until level_type is zero */
  2118. for (i = 1; *nent < maxnent; ++i) {
  2119. level_type = entry[i - 1].ecx & 0xff00;
  2120. if (!level_type)
  2121. break;
  2122. do_cpuid_1_ent(&entry[i], function, i);
  2123. entry[i].flags |=
  2124. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  2125. ++*nent;
  2126. }
  2127. break;
  2128. }
  2129. case 0xd: {
  2130. int i;
  2131. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  2132. for (i = 1; *nent < maxnent && i < 64; ++i) {
  2133. if (entry[i].eax == 0)
  2134. continue;
  2135. do_cpuid_1_ent(&entry[i], function, i);
  2136. entry[i].flags |=
  2137. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  2138. ++*nent;
  2139. }
  2140. break;
  2141. }
  2142. case KVM_CPUID_SIGNATURE: {
  2143. char signature[12] = "KVMKVMKVM\0\0";
  2144. u32 *sigptr = (u32 *)signature;
  2145. entry->eax = 0;
  2146. entry->ebx = sigptr[0];
  2147. entry->ecx = sigptr[1];
  2148. entry->edx = sigptr[2];
  2149. break;
  2150. }
  2151. case KVM_CPUID_FEATURES:
  2152. entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
  2153. (1 << KVM_FEATURE_NOP_IO_DELAY) |
  2154. (1 << KVM_FEATURE_CLOCKSOURCE2) |
  2155. (1 << KVM_FEATURE_ASYNC_PF) |
  2156. (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
  2157. entry->ebx = 0;
  2158. entry->ecx = 0;
  2159. entry->edx = 0;
  2160. break;
  2161. case 0x80000000:
  2162. entry->eax = min(entry->eax, 0x8000001a);
  2163. break;
  2164. case 0x80000001:
  2165. entry->edx &= kvm_supported_word1_x86_features;
  2166. cpuid_mask(&entry->edx, 1);
  2167. entry->ecx &= kvm_supported_word6_x86_features;
  2168. cpuid_mask(&entry->ecx, 6);
  2169. break;
  2170. }
  2171. kvm_x86_ops->set_supported_cpuid(function, entry);
  2172. put_cpu();
  2173. }
  2174. #undef F
  2175. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  2176. struct kvm_cpuid_entry2 __user *entries)
  2177. {
  2178. struct kvm_cpuid_entry2 *cpuid_entries;
  2179. int limit, nent = 0, r = -E2BIG;
  2180. u32 func;
  2181. if (cpuid->nent < 1)
  2182. goto out;
  2183. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  2184. cpuid->nent = KVM_MAX_CPUID_ENTRIES;
  2185. r = -ENOMEM;
  2186. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  2187. if (!cpuid_entries)
  2188. goto out;
  2189. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  2190. limit = cpuid_entries[0].eax;
  2191. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  2192. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  2193. &nent, cpuid->nent);
  2194. r = -E2BIG;
  2195. if (nent >= cpuid->nent)
  2196. goto out_free;
  2197. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  2198. limit = cpuid_entries[nent - 1].eax;
  2199. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  2200. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  2201. &nent, cpuid->nent);
  2202. r = -E2BIG;
  2203. if (nent >= cpuid->nent)
  2204. goto out_free;
  2205. do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent,
  2206. cpuid->nent);
  2207. r = -E2BIG;
  2208. if (nent >= cpuid->nent)
  2209. goto out_free;
  2210. do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent,
  2211. cpuid->nent);
  2212. r = -E2BIG;
  2213. if (nent >= cpuid->nent)
  2214. goto out_free;
  2215. r = -EFAULT;
  2216. if (copy_to_user(entries, cpuid_entries,
  2217. nent * sizeof(struct kvm_cpuid_entry2)))
  2218. goto out_free;
  2219. cpuid->nent = nent;
  2220. r = 0;
  2221. out_free:
  2222. vfree(cpuid_entries);
  2223. out:
  2224. return r;
  2225. }
  2226. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  2227. struct kvm_lapic_state *s)
  2228. {
  2229. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  2230. return 0;
  2231. }
  2232. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  2233. struct kvm_lapic_state *s)
  2234. {
  2235. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  2236. kvm_apic_post_state_restore(vcpu);
  2237. update_cr8_intercept(vcpu);
  2238. return 0;
  2239. }
  2240. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  2241. struct kvm_interrupt *irq)
  2242. {
  2243. if (irq->irq < 0 || irq->irq >= 256)
  2244. return -EINVAL;
  2245. if (irqchip_in_kernel(vcpu->kvm))
  2246. return -ENXIO;
  2247. kvm_queue_interrupt(vcpu, irq->irq, false);
  2248. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2249. return 0;
  2250. }
  2251. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  2252. {
  2253. kvm_inject_nmi(vcpu);
  2254. return 0;
  2255. }
  2256. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  2257. struct kvm_tpr_access_ctl *tac)
  2258. {
  2259. if (tac->flags)
  2260. return -EINVAL;
  2261. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  2262. return 0;
  2263. }
  2264. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  2265. u64 mcg_cap)
  2266. {
  2267. int r;
  2268. unsigned bank_num = mcg_cap & 0xff, bank;
  2269. r = -EINVAL;
  2270. if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
  2271. goto out;
  2272. if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
  2273. goto out;
  2274. r = 0;
  2275. vcpu->arch.mcg_cap = mcg_cap;
  2276. /* Init IA32_MCG_CTL to all 1s */
  2277. if (mcg_cap & MCG_CTL_P)
  2278. vcpu->arch.mcg_ctl = ~(u64)0;
  2279. /* Init IA32_MCi_CTL to all 1s */
  2280. for (bank = 0; bank < bank_num; bank++)
  2281. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  2282. out:
  2283. return r;
  2284. }
  2285. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  2286. struct kvm_x86_mce *mce)
  2287. {
  2288. u64 mcg_cap = vcpu->arch.mcg_cap;
  2289. unsigned bank_num = mcg_cap & 0xff;
  2290. u64 *banks = vcpu->arch.mce_banks;
  2291. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  2292. return -EINVAL;
  2293. /*
  2294. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  2295. * reporting is disabled
  2296. */
  2297. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  2298. vcpu->arch.mcg_ctl != ~(u64)0)
  2299. return 0;
  2300. banks += 4 * mce->bank;
  2301. /*
  2302. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  2303. * reporting is disabled for the bank
  2304. */
  2305. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  2306. return 0;
  2307. if (mce->status & MCI_STATUS_UC) {
  2308. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  2309. !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
  2310. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2311. return 0;
  2312. }
  2313. if (banks[1] & MCI_STATUS_VAL)
  2314. mce->status |= MCI_STATUS_OVER;
  2315. banks[2] = mce->addr;
  2316. banks[3] = mce->misc;
  2317. vcpu->arch.mcg_status = mce->mcg_status;
  2318. banks[1] = mce->status;
  2319. kvm_queue_exception(vcpu, MC_VECTOR);
  2320. } else if (!(banks[1] & MCI_STATUS_VAL)
  2321. || !(banks[1] & MCI_STATUS_UC)) {
  2322. if (banks[1] & MCI_STATUS_VAL)
  2323. mce->status |= MCI_STATUS_OVER;
  2324. banks[2] = mce->addr;
  2325. banks[3] = mce->misc;
  2326. banks[1] = mce->status;
  2327. } else
  2328. banks[1] |= MCI_STATUS_OVER;
  2329. return 0;
  2330. }
  2331. static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
  2332. struct kvm_vcpu_events *events)
  2333. {
  2334. events->exception.injected =
  2335. vcpu->arch.exception.pending &&
  2336. !kvm_exception_is_soft(vcpu->arch.exception.nr);
  2337. events->exception.nr = vcpu->arch.exception.nr;
  2338. events->exception.has_error_code = vcpu->arch.exception.has_error_code;
  2339. events->exception.pad = 0;
  2340. events->exception.error_code = vcpu->arch.exception.error_code;
  2341. events->interrupt.injected =
  2342. vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
  2343. events->interrupt.nr = vcpu->arch.interrupt.nr;
  2344. events->interrupt.soft = 0;
  2345. events->interrupt.shadow =
  2346. kvm_x86_ops->get_interrupt_shadow(vcpu,
  2347. KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
  2348. events->nmi.injected = vcpu->arch.nmi_injected;
  2349. events->nmi.pending = vcpu->arch.nmi_pending;
  2350. events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
  2351. events->nmi.pad = 0;
  2352. events->sipi_vector = vcpu->arch.sipi_vector;
  2353. events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
  2354. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2355. | KVM_VCPUEVENT_VALID_SHADOW);
  2356. memset(&events->reserved, 0, sizeof(events->reserved));
  2357. }
  2358. static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  2359. struct kvm_vcpu_events *events)
  2360. {
  2361. if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
  2362. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2363. | KVM_VCPUEVENT_VALID_SHADOW))
  2364. return -EINVAL;
  2365. vcpu->arch.exception.pending = events->exception.injected;
  2366. vcpu->arch.exception.nr = events->exception.nr;
  2367. vcpu->arch.exception.has_error_code = events->exception.has_error_code;
  2368. vcpu->arch.exception.error_code = events->exception.error_code;
  2369. vcpu->arch.interrupt.pending = events->interrupt.injected;
  2370. vcpu->arch.interrupt.nr = events->interrupt.nr;
  2371. vcpu->arch.interrupt.soft = events->interrupt.soft;
  2372. if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
  2373. kvm_x86_ops->set_interrupt_shadow(vcpu,
  2374. events->interrupt.shadow);
  2375. vcpu->arch.nmi_injected = events->nmi.injected;
  2376. if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
  2377. vcpu->arch.nmi_pending = events->nmi.pending;
  2378. kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
  2379. if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
  2380. vcpu->arch.sipi_vector = events->sipi_vector;
  2381. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2382. return 0;
  2383. }
  2384. static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
  2385. struct kvm_debugregs *dbgregs)
  2386. {
  2387. memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
  2388. dbgregs->dr6 = vcpu->arch.dr6;
  2389. dbgregs->dr7 = vcpu->arch.dr7;
  2390. dbgregs->flags = 0;
  2391. memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
  2392. }
  2393. static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
  2394. struct kvm_debugregs *dbgregs)
  2395. {
  2396. if (dbgregs->flags)
  2397. return -EINVAL;
  2398. memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
  2399. vcpu->arch.dr6 = dbgregs->dr6;
  2400. vcpu->arch.dr7 = dbgregs->dr7;
  2401. return 0;
  2402. }
  2403. static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
  2404. struct kvm_xsave *guest_xsave)
  2405. {
  2406. if (cpu_has_xsave)
  2407. memcpy(guest_xsave->region,
  2408. &vcpu->arch.guest_fpu.state->xsave,
  2409. xstate_size);
  2410. else {
  2411. memcpy(guest_xsave->region,
  2412. &vcpu->arch.guest_fpu.state->fxsave,
  2413. sizeof(struct i387_fxsave_struct));
  2414. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
  2415. XSTATE_FPSSE;
  2416. }
  2417. }
  2418. static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
  2419. struct kvm_xsave *guest_xsave)
  2420. {
  2421. u64 xstate_bv =
  2422. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
  2423. if (cpu_has_xsave)
  2424. memcpy(&vcpu->arch.guest_fpu.state->xsave,
  2425. guest_xsave->region, xstate_size);
  2426. else {
  2427. if (xstate_bv & ~XSTATE_FPSSE)
  2428. return -EINVAL;
  2429. memcpy(&vcpu->arch.guest_fpu.state->fxsave,
  2430. guest_xsave->region, sizeof(struct i387_fxsave_struct));
  2431. }
  2432. return 0;
  2433. }
  2434. static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
  2435. struct kvm_xcrs *guest_xcrs)
  2436. {
  2437. if (!cpu_has_xsave) {
  2438. guest_xcrs->nr_xcrs = 0;
  2439. return;
  2440. }
  2441. guest_xcrs->nr_xcrs = 1;
  2442. guest_xcrs->flags = 0;
  2443. guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
  2444. guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
  2445. }
  2446. static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
  2447. struct kvm_xcrs *guest_xcrs)
  2448. {
  2449. int i, r = 0;
  2450. if (!cpu_has_xsave)
  2451. return -EINVAL;
  2452. if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
  2453. return -EINVAL;
  2454. for (i = 0; i < guest_xcrs->nr_xcrs; i++)
  2455. /* Only support XCR0 currently */
  2456. if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) {
  2457. r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
  2458. guest_xcrs->xcrs[0].value);
  2459. break;
  2460. }
  2461. if (r)
  2462. r = -EINVAL;
  2463. return r;
  2464. }
  2465. long kvm_arch_vcpu_ioctl(struct file *filp,
  2466. unsigned int ioctl, unsigned long arg)
  2467. {
  2468. struct kvm_vcpu *vcpu = filp->private_data;
  2469. void __user *argp = (void __user *)arg;
  2470. int r;
  2471. union {
  2472. struct kvm_lapic_state *lapic;
  2473. struct kvm_xsave *xsave;
  2474. struct kvm_xcrs *xcrs;
  2475. void *buffer;
  2476. } u;
  2477. u.buffer = NULL;
  2478. switch (ioctl) {
  2479. case KVM_GET_LAPIC: {
  2480. r = -EINVAL;
  2481. if (!vcpu->arch.apic)
  2482. goto out;
  2483. u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2484. r = -ENOMEM;
  2485. if (!u.lapic)
  2486. goto out;
  2487. r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
  2488. if (r)
  2489. goto out;
  2490. r = -EFAULT;
  2491. if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
  2492. goto out;
  2493. r = 0;
  2494. break;
  2495. }
  2496. case KVM_SET_LAPIC: {
  2497. r = -EINVAL;
  2498. if (!vcpu->arch.apic)
  2499. goto out;
  2500. u.lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2501. r = -ENOMEM;
  2502. if (!u.lapic)
  2503. goto out;
  2504. r = -EFAULT;
  2505. if (copy_from_user(u.lapic, argp, sizeof(struct kvm_lapic_state)))
  2506. goto out;
  2507. r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
  2508. if (r)
  2509. goto out;
  2510. r = 0;
  2511. break;
  2512. }
  2513. case KVM_INTERRUPT: {
  2514. struct kvm_interrupt irq;
  2515. r = -EFAULT;
  2516. if (copy_from_user(&irq, argp, sizeof irq))
  2517. goto out;
  2518. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2519. if (r)
  2520. goto out;
  2521. r = 0;
  2522. break;
  2523. }
  2524. case KVM_NMI: {
  2525. r = kvm_vcpu_ioctl_nmi(vcpu);
  2526. if (r)
  2527. goto out;
  2528. r = 0;
  2529. break;
  2530. }
  2531. case KVM_SET_CPUID: {
  2532. struct kvm_cpuid __user *cpuid_arg = argp;
  2533. struct kvm_cpuid cpuid;
  2534. r = -EFAULT;
  2535. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2536. goto out;
  2537. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2538. if (r)
  2539. goto out;
  2540. break;
  2541. }
  2542. case KVM_SET_CPUID2: {
  2543. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2544. struct kvm_cpuid2 cpuid;
  2545. r = -EFAULT;
  2546. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2547. goto out;
  2548. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  2549. cpuid_arg->entries);
  2550. if (r)
  2551. goto out;
  2552. break;
  2553. }
  2554. case KVM_GET_CPUID2: {
  2555. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2556. struct kvm_cpuid2 cpuid;
  2557. r = -EFAULT;
  2558. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2559. goto out;
  2560. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  2561. cpuid_arg->entries);
  2562. if (r)
  2563. goto out;
  2564. r = -EFAULT;
  2565. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2566. goto out;
  2567. r = 0;
  2568. break;
  2569. }
  2570. case KVM_GET_MSRS:
  2571. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2572. break;
  2573. case KVM_SET_MSRS:
  2574. r = msr_io(vcpu, argp, do_set_msr, 0);
  2575. break;
  2576. case KVM_TPR_ACCESS_REPORTING: {
  2577. struct kvm_tpr_access_ctl tac;
  2578. r = -EFAULT;
  2579. if (copy_from_user(&tac, argp, sizeof tac))
  2580. goto out;
  2581. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  2582. if (r)
  2583. goto out;
  2584. r = -EFAULT;
  2585. if (copy_to_user(argp, &tac, sizeof tac))
  2586. goto out;
  2587. r = 0;
  2588. break;
  2589. };
  2590. case KVM_SET_VAPIC_ADDR: {
  2591. struct kvm_vapic_addr va;
  2592. r = -EINVAL;
  2593. if (!irqchip_in_kernel(vcpu->kvm))
  2594. goto out;
  2595. r = -EFAULT;
  2596. if (copy_from_user(&va, argp, sizeof va))
  2597. goto out;
  2598. r = 0;
  2599. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  2600. break;
  2601. }
  2602. case KVM_X86_SETUP_MCE: {
  2603. u64 mcg_cap;
  2604. r = -EFAULT;
  2605. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  2606. goto out;
  2607. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  2608. break;
  2609. }
  2610. case KVM_X86_SET_MCE: {
  2611. struct kvm_x86_mce mce;
  2612. r = -EFAULT;
  2613. if (copy_from_user(&mce, argp, sizeof mce))
  2614. goto out;
  2615. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  2616. break;
  2617. }
  2618. case KVM_GET_VCPU_EVENTS: {
  2619. struct kvm_vcpu_events events;
  2620. kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
  2621. r = -EFAULT;
  2622. if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
  2623. break;
  2624. r = 0;
  2625. break;
  2626. }
  2627. case KVM_SET_VCPU_EVENTS: {
  2628. struct kvm_vcpu_events events;
  2629. r = -EFAULT;
  2630. if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
  2631. break;
  2632. r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
  2633. break;
  2634. }
  2635. case KVM_GET_DEBUGREGS: {
  2636. struct kvm_debugregs dbgregs;
  2637. kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
  2638. r = -EFAULT;
  2639. if (copy_to_user(argp, &dbgregs,
  2640. sizeof(struct kvm_debugregs)))
  2641. break;
  2642. r = 0;
  2643. break;
  2644. }
  2645. case KVM_SET_DEBUGREGS: {
  2646. struct kvm_debugregs dbgregs;
  2647. r = -EFAULT;
  2648. if (copy_from_user(&dbgregs, argp,
  2649. sizeof(struct kvm_debugregs)))
  2650. break;
  2651. r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
  2652. break;
  2653. }
  2654. case KVM_GET_XSAVE: {
  2655. u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
  2656. r = -ENOMEM;
  2657. if (!u.xsave)
  2658. break;
  2659. kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
  2660. r = -EFAULT;
  2661. if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
  2662. break;
  2663. r = 0;
  2664. break;
  2665. }
  2666. case KVM_SET_XSAVE: {
  2667. u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
  2668. r = -ENOMEM;
  2669. if (!u.xsave)
  2670. break;
  2671. r = -EFAULT;
  2672. if (copy_from_user(u.xsave, argp, sizeof(struct kvm_xsave)))
  2673. break;
  2674. r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
  2675. break;
  2676. }
  2677. case KVM_GET_XCRS: {
  2678. u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
  2679. r = -ENOMEM;
  2680. if (!u.xcrs)
  2681. break;
  2682. kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
  2683. r = -EFAULT;
  2684. if (copy_to_user(argp, u.xcrs,
  2685. sizeof(struct kvm_xcrs)))
  2686. break;
  2687. r = 0;
  2688. break;
  2689. }
  2690. case KVM_SET_XCRS: {
  2691. u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
  2692. r = -ENOMEM;
  2693. if (!u.xcrs)
  2694. break;
  2695. r = -EFAULT;
  2696. if (copy_from_user(u.xcrs, argp,
  2697. sizeof(struct kvm_xcrs)))
  2698. break;
  2699. r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
  2700. break;
  2701. }
  2702. case KVM_SET_TSC_KHZ: {
  2703. u32 user_tsc_khz;
  2704. r = -EINVAL;
  2705. if (!kvm_has_tsc_control)
  2706. break;
  2707. user_tsc_khz = (u32)arg;
  2708. if (user_tsc_khz >= kvm_max_guest_tsc_khz)
  2709. goto out;
  2710. kvm_x86_ops->set_tsc_khz(vcpu, user_tsc_khz);
  2711. r = 0;
  2712. goto out;
  2713. }
  2714. case KVM_GET_TSC_KHZ: {
  2715. r = -EIO;
  2716. if (check_tsc_unstable())
  2717. goto out;
  2718. r = vcpu_tsc_khz(vcpu);
  2719. goto out;
  2720. }
  2721. default:
  2722. r = -EINVAL;
  2723. }
  2724. out:
  2725. kfree(u.buffer);
  2726. return r;
  2727. }
  2728. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  2729. {
  2730. int ret;
  2731. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  2732. return -1;
  2733. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  2734. return ret;
  2735. }
  2736. static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
  2737. u64 ident_addr)
  2738. {
  2739. kvm->arch.ept_identity_map_addr = ident_addr;
  2740. return 0;
  2741. }
  2742. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  2743. u32 kvm_nr_mmu_pages)
  2744. {
  2745. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  2746. return -EINVAL;
  2747. mutex_lock(&kvm->slots_lock);
  2748. spin_lock(&kvm->mmu_lock);
  2749. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  2750. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  2751. spin_unlock(&kvm->mmu_lock);
  2752. mutex_unlock(&kvm->slots_lock);
  2753. return 0;
  2754. }
  2755. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  2756. {
  2757. return kvm->arch.n_max_mmu_pages;
  2758. }
  2759. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2760. {
  2761. int r;
  2762. r = 0;
  2763. switch (chip->chip_id) {
  2764. case KVM_IRQCHIP_PIC_MASTER:
  2765. memcpy(&chip->chip.pic,
  2766. &pic_irqchip(kvm)->pics[0],
  2767. sizeof(struct kvm_pic_state));
  2768. break;
  2769. case KVM_IRQCHIP_PIC_SLAVE:
  2770. memcpy(&chip->chip.pic,
  2771. &pic_irqchip(kvm)->pics[1],
  2772. sizeof(struct kvm_pic_state));
  2773. break;
  2774. case KVM_IRQCHIP_IOAPIC:
  2775. r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
  2776. break;
  2777. default:
  2778. r = -EINVAL;
  2779. break;
  2780. }
  2781. return r;
  2782. }
  2783. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2784. {
  2785. int r;
  2786. r = 0;
  2787. switch (chip->chip_id) {
  2788. case KVM_IRQCHIP_PIC_MASTER:
  2789. spin_lock(&pic_irqchip(kvm)->lock);
  2790. memcpy(&pic_irqchip(kvm)->pics[0],
  2791. &chip->chip.pic,
  2792. sizeof(struct kvm_pic_state));
  2793. spin_unlock(&pic_irqchip(kvm)->lock);
  2794. break;
  2795. case KVM_IRQCHIP_PIC_SLAVE:
  2796. spin_lock(&pic_irqchip(kvm)->lock);
  2797. memcpy(&pic_irqchip(kvm)->pics[1],
  2798. &chip->chip.pic,
  2799. sizeof(struct kvm_pic_state));
  2800. spin_unlock(&pic_irqchip(kvm)->lock);
  2801. break;
  2802. case KVM_IRQCHIP_IOAPIC:
  2803. r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
  2804. break;
  2805. default:
  2806. r = -EINVAL;
  2807. break;
  2808. }
  2809. kvm_pic_update_irq(pic_irqchip(kvm));
  2810. return r;
  2811. }
  2812. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2813. {
  2814. int r = 0;
  2815. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2816. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  2817. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2818. return r;
  2819. }
  2820. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2821. {
  2822. int r = 0;
  2823. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2824. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  2825. kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
  2826. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2827. return r;
  2828. }
  2829. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2830. {
  2831. int r = 0;
  2832. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2833. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  2834. sizeof(ps->channels));
  2835. ps->flags = kvm->arch.vpit->pit_state.flags;
  2836. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2837. memset(&ps->reserved, 0, sizeof(ps->reserved));
  2838. return r;
  2839. }
  2840. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2841. {
  2842. int r = 0, start = 0;
  2843. u32 prev_legacy, cur_legacy;
  2844. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2845. prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2846. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2847. if (!prev_legacy && cur_legacy)
  2848. start = 1;
  2849. memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
  2850. sizeof(kvm->arch.vpit->pit_state.channels));
  2851. kvm->arch.vpit->pit_state.flags = ps->flags;
  2852. kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
  2853. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2854. return r;
  2855. }
  2856. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  2857. struct kvm_reinject_control *control)
  2858. {
  2859. if (!kvm->arch.vpit)
  2860. return -ENXIO;
  2861. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2862. kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
  2863. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2864. return 0;
  2865. }
  2866. /*
  2867. * Get (and clear) the dirty memory log for a memory slot.
  2868. */
  2869. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  2870. struct kvm_dirty_log *log)
  2871. {
  2872. int r, i;
  2873. struct kvm_memory_slot *memslot;
  2874. unsigned long n;
  2875. unsigned long is_dirty = 0;
  2876. mutex_lock(&kvm->slots_lock);
  2877. r = -EINVAL;
  2878. if (log->slot >= KVM_MEMORY_SLOTS)
  2879. goto out;
  2880. memslot = &kvm->memslots->memslots[log->slot];
  2881. r = -ENOENT;
  2882. if (!memslot->dirty_bitmap)
  2883. goto out;
  2884. n = kvm_dirty_bitmap_bytes(memslot);
  2885. for (i = 0; !is_dirty && i < n/sizeof(long); i++)
  2886. is_dirty = memslot->dirty_bitmap[i];
  2887. /* If nothing is dirty, don't bother messing with page tables. */
  2888. if (is_dirty) {
  2889. struct kvm_memslots *slots, *old_slots;
  2890. unsigned long *dirty_bitmap;
  2891. dirty_bitmap = memslot->dirty_bitmap_head;
  2892. if (memslot->dirty_bitmap == dirty_bitmap)
  2893. dirty_bitmap += n / sizeof(long);
  2894. memset(dirty_bitmap, 0, n);
  2895. r = -ENOMEM;
  2896. slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
  2897. if (!slots)
  2898. goto out;
  2899. memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
  2900. slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
  2901. slots->generation++;
  2902. old_slots = kvm->memslots;
  2903. rcu_assign_pointer(kvm->memslots, slots);
  2904. synchronize_srcu_expedited(&kvm->srcu);
  2905. dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
  2906. kfree(old_slots);
  2907. spin_lock(&kvm->mmu_lock);
  2908. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  2909. spin_unlock(&kvm->mmu_lock);
  2910. r = -EFAULT;
  2911. if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
  2912. goto out;
  2913. } else {
  2914. r = -EFAULT;
  2915. if (clear_user(log->dirty_bitmap, n))
  2916. goto out;
  2917. }
  2918. r = 0;
  2919. out:
  2920. mutex_unlock(&kvm->slots_lock);
  2921. return r;
  2922. }
  2923. long kvm_arch_vm_ioctl(struct file *filp,
  2924. unsigned int ioctl, unsigned long arg)
  2925. {
  2926. struct kvm *kvm = filp->private_data;
  2927. void __user *argp = (void __user *)arg;
  2928. int r = -ENOTTY;
  2929. /*
  2930. * This union makes it completely explicit to gcc-3.x
  2931. * that these two variables' stack usage should be
  2932. * combined, not added together.
  2933. */
  2934. union {
  2935. struct kvm_pit_state ps;
  2936. struct kvm_pit_state2 ps2;
  2937. struct kvm_pit_config pit_config;
  2938. } u;
  2939. switch (ioctl) {
  2940. case KVM_SET_TSS_ADDR:
  2941. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  2942. if (r < 0)
  2943. goto out;
  2944. break;
  2945. case KVM_SET_IDENTITY_MAP_ADDR: {
  2946. u64 ident_addr;
  2947. r = -EFAULT;
  2948. if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
  2949. goto out;
  2950. r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
  2951. if (r < 0)
  2952. goto out;
  2953. break;
  2954. }
  2955. case KVM_SET_NR_MMU_PAGES:
  2956. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  2957. if (r)
  2958. goto out;
  2959. break;
  2960. case KVM_GET_NR_MMU_PAGES:
  2961. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  2962. break;
  2963. case KVM_CREATE_IRQCHIP: {
  2964. struct kvm_pic *vpic;
  2965. mutex_lock(&kvm->lock);
  2966. r = -EEXIST;
  2967. if (kvm->arch.vpic)
  2968. goto create_irqchip_unlock;
  2969. r = -ENOMEM;
  2970. vpic = kvm_create_pic(kvm);
  2971. if (vpic) {
  2972. r = kvm_ioapic_init(kvm);
  2973. if (r) {
  2974. mutex_lock(&kvm->slots_lock);
  2975. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  2976. &vpic->dev);
  2977. mutex_unlock(&kvm->slots_lock);
  2978. kfree(vpic);
  2979. goto create_irqchip_unlock;
  2980. }
  2981. } else
  2982. goto create_irqchip_unlock;
  2983. smp_wmb();
  2984. kvm->arch.vpic = vpic;
  2985. smp_wmb();
  2986. r = kvm_setup_default_irq_routing(kvm);
  2987. if (r) {
  2988. mutex_lock(&kvm->slots_lock);
  2989. mutex_lock(&kvm->irq_lock);
  2990. kvm_ioapic_destroy(kvm);
  2991. kvm_destroy_pic(kvm);
  2992. mutex_unlock(&kvm->irq_lock);
  2993. mutex_unlock(&kvm->slots_lock);
  2994. }
  2995. create_irqchip_unlock:
  2996. mutex_unlock(&kvm->lock);
  2997. break;
  2998. }
  2999. case KVM_CREATE_PIT:
  3000. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  3001. goto create_pit;
  3002. case KVM_CREATE_PIT2:
  3003. r = -EFAULT;
  3004. if (copy_from_user(&u.pit_config, argp,
  3005. sizeof(struct kvm_pit_config)))
  3006. goto out;
  3007. create_pit:
  3008. mutex_lock(&kvm->slots_lock);
  3009. r = -EEXIST;
  3010. if (kvm->arch.vpit)
  3011. goto create_pit_unlock;
  3012. r = -ENOMEM;
  3013. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  3014. if (kvm->arch.vpit)
  3015. r = 0;
  3016. create_pit_unlock:
  3017. mutex_unlock(&kvm->slots_lock);
  3018. break;
  3019. case KVM_IRQ_LINE_STATUS:
  3020. case KVM_IRQ_LINE: {
  3021. struct kvm_irq_level irq_event;
  3022. r = -EFAULT;
  3023. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  3024. goto out;
  3025. r = -ENXIO;
  3026. if (irqchip_in_kernel(kvm)) {
  3027. __s32 status;
  3028. status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  3029. irq_event.irq, irq_event.level);
  3030. if (ioctl == KVM_IRQ_LINE_STATUS) {
  3031. r = -EFAULT;
  3032. irq_event.status = status;
  3033. if (copy_to_user(argp, &irq_event,
  3034. sizeof irq_event))
  3035. goto out;
  3036. }
  3037. r = 0;
  3038. }
  3039. break;
  3040. }
  3041. case KVM_GET_IRQCHIP: {
  3042. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  3043. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  3044. r = -ENOMEM;
  3045. if (!chip)
  3046. goto out;
  3047. r = -EFAULT;
  3048. if (copy_from_user(chip, argp, sizeof *chip))
  3049. goto get_irqchip_out;
  3050. r = -ENXIO;
  3051. if (!irqchip_in_kernel(kvm))
  3052. goto get_irqchip_out;
  3053. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  3054. if (r)
  3055. goto get_irqchip_out;
  3056. r = -EFAULT;
  3057. if (copy_to_user(argp, chip, sizeof *chip))
  3058. goto get_irqchip_out;
  3059. r = 0;
  3060. get_irqchip_out:
  3061. kfree(chip);
  3062. if (r)
  3063. goto out;
  3064. break;
  3065. }
  3066. case KVM_SET_IRQCHIP: {
  3067. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  3068. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  3069. r = -ENOMEM;
  3070. if (!chip)
  3071. goto out;
  3072. r = -EFAULT;
  3073. if (copy_from_user(chip, argp, sizeof *chip))
  3074. goto set_irqchip_out;
  3075. r = -ENXIO;
  3076. if (!irqchip_in_kernel(kvm))
  3077. goto set_irqchip_out;
  3078. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  3079. if (r)
  3080. goto set_irqchip_out;
  3081. r = 0;
  3082. set_irqchip_out:
  3083. kfree(chip);
  3084. if (r)
  3085. goto out;
  3086. break;
  3087. }
  3088. case KVM_GET_PIT: {
  3089. r = -EFAULT;
  3090. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  3091. goto out;
  3092. r = -ENXIO;
  3093. if (!kvm->arch.vpit)
  3094. goto out;
  3095. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  3096. if (r)
  3097. goto out;
  3098. r = -EFAULT;
  3099. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  3100. goto out;
  3101. r = 0;
  3102. break;
  3103. }
  3104. case KVM_SET_PIT: {
  3105. r = -EFAULT;
  3106. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  3107. goto out;
  3108. r = -ENXIO;
  3109. if (!kvm->arch.vpit)
  3110. goto out;
  3111. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  3112. if (r)
  3113. goto out;
  3114. r = 0;
  3115. break;
  3116. }
  3117. case KVM_GET_PIT2: {
  3118. r = -ENXIO;
  3119. if (!kvm->arch.vpit)
  3120. goto out;
  3121. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  3122. if (r)
  3123. goto out;
  3124. r = -EFAULT;
  3125. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  3126. goto out;
  3127. r = 0;
  3128. break;
  3129. }
  3130. case KVM_SET_PIT2: {
  3131. r = -EFAULT;
  3132. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  3133. goto out;
  3134. r = -ENXIO;
  3135. if (!kvm->arch.vpit)
  3136. goto out;
  3137. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  3138. if (r)
  3139. goto out;
  3140. r = 0;
  3141. break;
  3142. }
  3143. case KVM_REINJECT_CONTROL: {
  3144. struct kvm_reinject_control control;
  3145. r = -EFAULT;
  3146. if (copy_from_user(&control, argp, sizeof(control)))
  3147. goto out;
  3148. r = kvm_vm_ioctl_reinject(kvm, &control);
  3149. if (r)
  3150. goto out;
  3151. r = 0;
  3152. break;
  3153. }
  3154. case KVM_XEN_HVM_CONFIG: {
  3155. r = -EFAULT;
  3156. if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
  3157. sizeof(struct kvm_xen_hvm_config)))
  3158. goto out;
  3159. r = -EINVAL;
  3160. if (kvm->arch.xen_hvm_config.flags)
  3161. goto out;
  3162. r = 0;
  3163. break;
  3164. }
  3165. case KVM_SET_CLOCK: {
  3166. struct kvm_clock_data user_ns;
  3167. u64 now_ns;
  3168. s64 delta;
  3169. r = -EFAULT;
  3170. if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
  3171. goto out;
  3172. r = -EINVAL;
  3173. if (user_ns.flags)
  3174. goto out;
  3175. r = 0;
  3176. local_irq_disable();
  3177. now_ns = get_kernel_ns();
  3178. delta = user_ns.clock - now_ns;
  3179. local_irq_enable();
  3180. kvm->arch.kvmclock_offset = delta;
  3181. break;
  3182. }
  3183. case KVM_GET_CLOCK: {
  3184. struct kvm_clock_data user_ns;
  3185. u64 now_ns;
  3186. local_irq_disable();
  3187. now_ns = get_kernel_ns();
  3188. user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
  3189. local_irq_enable();
  3190. user_ns.flags = 0;
  3191. memset(&user_ns.pad, 0, sizeof(user_ns.pad));
  3192. r = -EFAULT;
  3193. if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
  3194. goto out;
  3195. r = 0;
  3196. break;
  3197. }
  3198. default:
  3199. ;
  3200. }
  3201. out:
  3202. return r;
  3203. }
  3204. static void kvm_init_msr_list(void)
  3205. {
  3206. u32 dummy[2];
  3207. unsigned i, j;
  3208. /* skip the first msrs in the list. KVM-specific */
  3209. for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
  3210. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  3211. continue;
  3212. if (j < i)
  3213. msrs_to_save[j] = msrs_to_save[i];
  3214. j++;
  3215. }
  3216. num_msrs_to_save = j;
  3217. }
  3218. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  3219. const void *v)
  3220. {
  3221. int handled = 0;
  3222. int n;
  3223. do {
  3224. n = min(len, 8);
  3225. if (!(vcpu->arch.apic &&
  3226. !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
  3227. && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
  3228. break;
  3229. handled += n;
  3230. addr += n;
  3231. len -= n;
  3232. v += n;
  3233. } while (len);
  3234. return handled;
  3235. }
  3236. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  3237. {
  3238. int handled = 0;
  3239. int n;
  3240. do {
  3241. n = min(len, 8);
  3242. if (!(vcpu->arch.apic &&
  3243. !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
  3244. && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
  3245. break;
  3246. trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
  3247. handled += n;
  3248. addr += n;
  3249. len -= n;
  3250. v += n;
  3251. } while (len);
  3252. return handled;
  3253. }
  3254. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3255. struct kvm_segment *var, int seg)
  3256. {
  3257. kvm_x86_ops->set_segment(vcpu, var, seg);
  3258. }
  3259. void kvm_get_segment(struct kvm_vcpu *vcpu,
  3260. struct kvm_segment *var, int seg)
  3261. {
  3262. kvm_x86_ops->get_segment(vcpu, var, seg);
  3263. }
  3264. static gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
  3265. {
  3266. return gpa;
  3267. }
  3268. static gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
  3269. {
  3270. gpa_t t_gpa;
  3271. struct x86_exception exception;
  3272. BUG_ON(!mmu_is_nested(vcpu));
  3273. /* NPT walks are always user-walks */
  3274. access |= PFERR_USER_MASK;
  3275. t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, &exception);
  3276. return t_gpa;
  3277. }
  3278. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  3279. struct x86_exception *exception)
  3280. {
  3281. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3282. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3283. }
  3284. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  3285. struct x86_exception *exception)
  3286. {
  3287. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3288. access |= PFERR_FETCH_MASK;
  3289. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3290. }
  3291. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  3292. struct x86_exception *exception)
  3293. {
  3294. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3295. access |= PFERR_WRITE_MASK;
  3296. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3297. }
  3298. /* uses this to access any guest's mapped memory without checking CPL */
  3299. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  3300. struct x86_exception *exception)
  3301. {
  3302. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
  3303. }
  3304. static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  3305. struct kvm_vcpu *vcpu, u32 access,
  3306. struct x86_exception *exception)
  3307. {
  3308. void *data = val;
  3309. int r = X86EMUL_CONTINUE;
  3310. while (bytes) {
  3311. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
  3312. exception);
  3313. unsigned offset = addr & (PAGE_SIZE-1);
  3314. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  3315. int ret;
  3316. if (gpa == UNMAPPED_GVA)
  3317. return X86EMUL_PROPAGATE_FAULT;
  3318. ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
  3319. if (ret < 0) {
  3320. r = X86EMUL_IO_NEEDED;
  3321. goto out;
  3322. }
  3323. bytes -= toread;
  3324. data += toread;
  3325. addr += toread;
  3326. }
  3327. out:
  3328. return r;
  3329. }
  3330. /* used for instruction fetching */
  3331. static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
  3332. struct kvm_vcpu *vcpu,
  3333. struct x86_exception *exception)
  3334. {
  3335. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3336. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
  3337. access | PFERR_FETCH_MASK,
  3338. exception);
  3339. }
  3340. static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
  3341. struct kvm_vcpu *vcpu,
  3342. struct x86_exception *exception)
  3343. {
  3344. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3345. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
  3346. exception);
  3347. }
  3348. static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
  3349. struct kvm_vcpu *vcpu,
  3350. struct x86_exception *exception)
  3351. {
  3352. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
  3353. }
  3354. static int kvm_write_guest_virt_system(gva_t addr, void *val,
  3355. unsigned int bytes,
  3356. struct kvm_vcpu *vcpu,
  3357. struct x86_exception *exception)
  3358. {
  3359. void *data = val;
  3360. int r = X86EMUL_CONTINUE;
  3361. while (bytes) {
  3362. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
  3363. PFERR_WRITE_MASK,
  3364. exception);
  3365. unsigned offset = addr & (PAGE_SIZE-1);
  3366. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  3367. int ret;
  3368. if (gpa == UNMAPPED_GVA)
  3369. return X86EMUL_PROPAGATE_FAULT;
  3370. ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
  3371. if (ret < 0) {
  3372. r = X86EMUL_IO_NEEDED;
  3373. goto out;
  3374. }
  3375. bytes -= towrite;
  3376. data += towrite;
  3377. addr += towrite;
  3378. }
  3379. out:
  3380. return r;
  3381. }
  3382. static int emulator_read_emulated(unsigned long addr,
  3383. void *val,
  3384. unsigned int bytes,
  3385. struct x86_exception *exception,
  3386. struct kvm_vcpu *vcpu)
  3387. {
  3388. gpa_t gpa;
  3389. int handled;
  3390. if (vcpu->mmio_read_completed) {
  3391. memcpy(val, vcpu->mmio_data, bytes);
  3392. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  3393. vcpu->mmio_phys_addr, *(u64 *)val);
  3394. vcpu->mmio_read_completed = 0;
  3395. return X86EMUL_CONTINUE;
  3396. }
  3397. gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, exception);
  3398. if (gpa == UNMAPPED_GVA)
  3399. return X86EMUL_PROPAGATE_FAULT;
  3400. /* For APIC access vmexit */
  3401. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3402. goto mmio;
  3403. if (kvm_read_guest_virt(addr, val, bytes, vcpu, exception)
  3404. == X86EMUL_CONTINUE)
  3405. return X86EMUL_CONTINUE;
  3406. mmio:
  3407. /*
  3408. * Is this MMIO handled locally?
  3409. */
  3410. handled = vcpu_mmio_read(vcpu, gpa, bytes, val);
  3411. if (handled == bytes)
  3412. return X86EMUL_CONTINUE;
  3413. gpa += handled;
  3414. bytes -= handled;
  3415. val += handled;
  3416. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
  3417. vcpu->mmio_needed = 1;
  3418. vcpu->run->exit_reason = KVM_EXIT_MMIO;
  3419. vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
  3420. vcpu->mmio_size = bytes;
  3421. vcpu->run->mmio.len = min(vcpu->mmio_size, 8);
  3422. vcpu->run->mmio.is_write = vcpu->mmio_is_write = 0;
  3423. vcpu->mmio_index = 0;
  3424. return X86EMUL_IO_NEEDED;
  3425. }
  3426. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  3427. const void *val, int bytes)
  3428. {
  3429. int ret;
  3430. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  3431. if (ret < 0)
  3432. return 0;
  3433. kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
  3434. return 1;
  3435. }
  3436. static int emulator_write_emulated_onepage(unsigned long addr,
  3437. const void *val,
  3438. unsigned int bytes,
  3439. struct x86_exception *exception,
  3440. struct kvm_vcpu *vcpu)
  3441. {
  3442. gpa_t gpa;
  3443. int handled;
  3444. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, exception);
  3445. if (gpa == UNMAPPED_GVA)
  3446. return X86EMUL_PROPAGATE_FAULT;
  3447. /* For APIC access vmexit */
  3448. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3449. goto mmio;
  3450. if (emulator_write_phys(vcpu, gpa, val, bytes))
  3451. return X86EMUL_CONTINUE;
  3452. mmio:
  3453. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
  3454. /*
  3455. * Is this MMIO handled locally?
  3456. */
  3457. handled = vcpu_mmio_write(vcpu, gpa, bytes, val);
  3458. if (handled == bytes)
  3459. return X86EMUL_CONTINUE;
  3460. gpa += handled;
  3461. bytes -= handled;
  3462. val += handled;
  3463. vcpu->mmio_needed = 1;
  3464. memcpy(vcpu->mmio_data, val, bytes);
  3465. vcpu->run->exit_reason = KVM_EXIT_MMIO;
  3466. vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
  3467. vcpu->mmio_size = bytes;
  3468. vcpu->run->mmio.len = min(vcpu->mmio_size, 8);
  3469. vcpu->run->mmio.is_write = vcpu->mmio_is_write = 1;
  3470. memcpy(vcpu->run->mmio.data, vcpu->mmio_data, 8);
  3471. vcpu->mmio_index = 0;
  3472. return X86EMUL_CONTINUE;
  3473. }
  3474. int emulator_write_emulated(unsigned long addr,
  3475. const void *val,
  3476. unsigned int bytes,
  3477. struct x86_exception *exception,
  3478. struct kvm_vcpu *vcpu)
  3479. {
  3480. /* Crossing a page boundary? */
  3481. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  3482. int rc, now;
  3483. now = -addr & ~PAGE_MASK;
  3484. rc = emulator_write_emulated_onepage(addr, val, now, exception,
  3485. vcpu);
  3486. if (rc != X86EMUL_CONTINUE)
  3487. return rc;
  3488. addr += now;
  3489. val += now;
  3490. bytes -= now;
  3491. }
  3492. return emulator_write_emulated_onepage(addr, val, bytes, exception,
  3493. vcpu);
  3494. }
  3495. #define CMPXCHG_TYPE(t, ptr, old, new) \
  3496. (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
  3497. #ifdef CONFIG_X86_64
  3498. # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
  3499. #else
  3500. # define CMPXCHG64(ptr, old, new) \
  3501. (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
  3502. #endif
  3503. static int emulator_cmpxchg_emulated(unsigned long addr,
  3504. const void *old,
  3505. const void *new,
  3506. unsigned int bytes,
  3507. struct x86_exception *exception,
  3508. struct kvm_vcpu *vcpu)
  3509. {
  3510. gpa_t gpa;
  3511. struct page *page;
  3512. char *kaddr;
  3513. bool exchanged;
  3514. /* guests cmpxchg8b have to be emulated atomically */
  3515. if (bytes > 8 || (bytes & (bytes - 1)))
  3516. goto emul_write;
  3517. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
  3518. if (gpa == UNMAPPED_GVA ||
  3519. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3520. goto emul_write;
  3521. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  3522. goto emul_write;
  3523. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3524. if (is_error_page(page)) {
  3525. kvm_release_page_clean(page);
  3526. goto emul_write;
  3527. }
  3528. kaddr = kmap_atomic(page, KM_USER0);
  3529. kaddr += offset_in_page(gpa);
  3530. switch (bytes) {
  3531. case 1:
  3532. exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
  3533. break;
  3534. case 2:
  3535. exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
  3536. break;
  3537. case 4:
  3538. exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
  3539. break;
  3540. case 8:
  3541. exchanged = CMPXCHG64(kaddr, old, new);
  3542. break;
  3543. default:
  3544. BUG();
  3545. }
  3546. kunmap_atomic(kaddr, KM_USER0);
  3547. kvm_release_page_dirty(page);
  3548. if (!exchanged)
  3549. return X86EMUL_CMPXCHG_FAILED;
  3550. kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1);
  3551. return X86EMUL_CONTINUE;
  3552. emul_write:
  3553. printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
  3554. return emulator_write_emulated(addr, new, bytes, exception, vcpu);
  3555. }
  3556. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  3557. {
  3558. /* TODO: String I/O for in kernel device */
  3559. int r;
  3560. if (vcpu->arch.pio.in)
  3561. r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
  3562. vcpu->arch.pio.size, pd);
  3563. else
  3564. r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
  3565. vcpu->arch.pio.port, vcpu->arch.pio.size,
  3566. pd);
  3567. return r;
  3568. }
  3569. static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
  3570. unsigned int count, struct kvm_vcpu *vcpu)
  3571. {
  3572. if (vcpu->arch.pio.count)
  3573. goto data_avail;
  3574. trace_kvm_pio(0, port, size, count);
  3575. vcpu->arch.pio.port = port;
  3576. vcpu->arch.pio.in = 1;
  3577. vcpu->arch.pio.count = count;
  3578. vcpu->arch.pio.size = size;
  3579. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3580. data_avail:
  3581. memcpy(val, vcpu->arch.pio_data, size * count);
  3582. vcpu->arch.pio.count = 0;
  3583. return 1;
  3584. }
  3585. vcpu->run->exit_reason = KVM_EXIT_IO;
  3586. vcpu->run->io.direction = KVM_EXIT_IO_IN;
  3587. vcpu->run->io.size = size;
  3588. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3589. vcpu->run->io.count = count;
  3590. vcpu->run->io.port = port;
  3591. return 0;
  3592. }
  3593. static int emulator_pio_out_emulated(int size, unsigned short port,
  3594. const void *val, unsigned int count,
  3595. struct kvm_vcpu *vcpu)
  3596. {
  3597. trace_kvm_pio(1, port, size, count);
  3598. vcpu->arch.pio.port = port;
  3599. vcpu->arch.pio.in = 0;
  3600. vcpu->arch.pio.count = count;
  3601. vcpu->arch.pio.size = size;
  3602. memcpy(vcpu->arch.pio_data, val, size * count);
  3603. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3604. vcpu->arch.pio.count = 0;
  3605. return 1;
  3606. }
  3607. vcpu->run->exit_reason = KVM_EXIT_IO;
  3608. vcpu->run->io.direction = KVM_EXIT_IO_OUT;
  3609. vcpu->run->io.size = size;
  3610. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3611. vcpu->run->io.count = count;
  3612. vcpu->run->io.port = port;
  3613. return 0;
  3614. }
  3615. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3616. {
  3617. return kvm_x86_ops->get_segment_base(vcpu, seg);
  3618. }
  3619. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  3620. {
  3621. kvm_mmu_invlpg(vcpu, address);
  3622. return X86EMUL_CONTINUE;
  3623. }
  3624. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
  3625. {
  3626. if (!need_emulate_wbinvd(vcpu))
  3627. return X86EMUL_CONTINUE;
  3628. if (kvm_x86_ops->has_wbinvd_exit()) {
  3629. int cpu = get_cpu();
  3630. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  3631. smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
  3632. wbinvd_ipi, NULL, 1);
  3633. put_cpu();
  3634. cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
  3635. } else
  3636. wbinvd();
  3637. return X86EMUL_CONTINUE;
  3638. }
  3639. EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
  3640. int emulate_clts(struct kvm_vcpu *vcpu)
  3641. {
  3642. kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  3643. kvm_x86_ops->fpu_activate(vcpu);
  3644. return X86EMUL_CONTINUE;
  3645. }
  3646. int emulator_get_dr(int dr, unsigned long *dest, struct kvm_vcpu *vcpu)
  3647. {
  3648. return _kvm_get_dr(vcpu, dr, dest);
  3649. }
  3650. int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu)
  3651. {
  3652. return __kvm_set_dr(vcpu, dr, value);
  3653. }
  3654. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  3655. {
  3656. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  3657. }
  3658. static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
  3659. {
  3660. unsigned long value;
  3661. switch (cr) {
  3662. case 0:
  3663. value = kvm_read_cr0(vcpu);
  3664. break;
  3665. case 2:
  3666. value = vcpu->arch.cr2;
  3667. break;
  3668. case 3:
  3669. value = kvm_read_cr3(vcpu);
  3670. break;
  3671. case 4:
  3672. value = kvm_read_cr4(vcpu);
  3673. break;
  3674. case 8:
  3675. value = kvm_get_cr8(vcpu);
  3676. break;
  3677. default:
  3678. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3679. return 0;
  3680. }
  3681. return value;
  3682. }
  3683. static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
  3684. {
  3685. int res = 0;
  3686. switch (cr) {
  3687. case 0:
  3688. res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
  3689. break;
  3690. case 2:
  3691. vcpu->arch.cr2 = val;
  3692. break;
  3693. case 3:
  3694. res = kvm_set_cr3(vcpu, val);
  3695. break;
  3696. case 4:
  3697. res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
  3698. break;
  3699. case 8:
  3700. res = kvm_set_cr8(vcpu, val);
  3701. break;
  3702. default:
  3703. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3704. res = -1;
  3705. }
  3706. return res;
  3707. }
  3708. static int emulator_get_cpl(struct kvm_vcpu *vcpu)
  3709. {
  3710. return kvm_x86_ops->get_cpl(vcpu);
  3711. }
  3712. static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
  3713. {
  3714. kvm_x86_ops->get_gdt(vcpu, dt);
  3715. }
  3716. static void emulator_get_idt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
  3717. {
  3718. kvm_x86_ops->get_idt(vcpu, dt);
  3719. }
  3720. static unsigned long emulator_get_cached_segment_base(int seg,
  3721. struct kvm_vcpu *vcpu)
  3722. {
  3723. return get_segment_base(vcpu, seg);
  3724. }
  3725. static bool emulator_get_cached_descriptor(struct desc_struct *desc, u32 *base3,
  3726. int seg, struct kvm_vcpu *vcpu)
  3727. {
  3728. struct kvm_segment var;
  3729. kvm_get_segment(vcpu, &var, seg);
  3730. if (var.unusable)
  3731. return false;
  3732. if (var.g)
  3733. var.limit >>= 12;
  3734. set_desc_limit(desc, var.limit);
  3735. set_desc_base(desc, (unsigned long)var.base);
  3736. #ifdef CONFIG_X86_64
  3737. if (base3)
  3738. *base3 = var.base >> 32;
  3739. #endif
  3740. desc->type = var.type;
  3741. desc->s = var.s;
  3742. desc->dpl = var.dpl;
  3743. desc->p = var.present;
  3744. desc->avl = var.avl;
  3745. desc->l = var.l;
  3746. desc->d = var.db;
  3747. desc->g = var.g;
  3748. return true;
  3749. }
  3750. static void emulator_set_cached_descriptor(struct desc_struct *desc, u32 base3,
  3751. int seg, struct kvm_vcpu *vcpu)
  3752. {
  3753. struct kvm_segment var;
  3754. /* needed to preserve selector */
  3755. kvm_get_segment(vcpu, &var, seg);
  3756. var.base = get_desc_base(desc);
  3757. #ifdef CONFIG_X86_64
  3758. var.base |= ((u64)base3) << 32;
  3759. #endif
  3760. var.limit = get_desc_limit(desc);
  3761. if (desc->g)
  3762. var.limit = (var.limit << 12) | 0xfff;
  3763. var.type = desc->type;
  3764. var.present = desc->p;
  3765. var.dpl = desc->dpl;
  3766. var.db = desc->d;
  3767. var.s = desc->s;
  3768. var.l = desc->l;
  3769. var.g = desc->g;
  3770. var.avl = desc->avl;
  3771. var.present = desc->p;
  3772. var.unusable = !var.present;
  3773. var.padding = 0;
  3774. kvm_set_segment(vcpu, &var, seg);
  3775. return;
  3776. }
  3777. static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
  3778. {
  3779. struct kvm_segment kvm_seg;
  3780. kvm_get_segment(vcpu, &kvm_seg, seg);
  3781. return kvm_seg.selector;
  3782. }
  3783. static void emulator_set_segment_selector(u16 sel, int seg,
  3784. struct kvm_vcpu *vcpu)
  3785. {
  3786. struct kvm_segment kvm_seg;
  3787. kvm_get_segment(vcpu, &kvm_seg, seg);
  3788. kvm_seg.selector = sel;
  3789. kvm_set_segment(vcpu, &kvm_seg, seg);
  3790. }
  3791. static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
  3792. {
  3793. preempt_disable();
  3794. kvm_load_guest_fpu(ctxt->vcpu);
  3795. /*
  3796. * CR0.TS may reference the host fpu state, not the guest fpu state,
  3797. * so it may be clear at this point.
  3798. */
  3799. clts();
  3800. }
  3801. static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
  3802. {
  3803. preempt_enable();
  3804. }
  3805. static int emulator_intercept(struct kvm_vcpu *vcpu,
  3806. struct x86_instruction_info *info,
  3807. enum x86_intercept_stage stage)
  3808. {
  3809. return kvm_x86_ops->check_intercept(vcpu, info, stage);
  3810. }
  3811. static struct x86_emulate_ops emulate_ops = {
  3812. .read_std = kvm_read_guest_virt_system,
  3813. .write_std = kvm_write_guest_virt_system,
  3814. .fetch = kvm_fetch_guest_virt,
  3815. .read_emulated = emulator_read_emulated,
  3816. .write_emulated = emulator_write_emulated,
  3817. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  3818. .pio_in_emulated = emulator_pio_in_emulated,
  3819. .pio_out_emulated = emulator_pio_out_emulated,
  3820. .get_cached_descriptor = emulator_get_cached_descriptor,
  3821. .set_cached_descriptor = emulator_set_cached_descriptor,
  3822. .get_segment_selector = emulator_get_segment_selector,
  3823. .set_segment_selector = emulator_set_segment_selector,
  3824. .get_cached_segment_base = emulator_get_cached_segment_base,
  3825. .get_gdt = emulator_get_gdt,
  3826. .get_idt = emulator_get_idt,
  3827. .get_cr = emulator_get_cr,
  3828. .set_cr = emulator_set_cr,
  3829. .cpl = emulator_get_cpl,
  3830. .get_dr = emulator_get_dr,
  3831. .set_dr = emulator_set_dr,
  3832. .set_msr = kvm_set_msr,
  3833. .get_msr = kvm_get_msr,
  3834. .get_fpu = emulator_get_fpu,
  3835. .put_fpu = emulator_put_fpu,
  3836. .intercept = emulator_intercept,
  3837. };
  3838. static void cache_all_regs(struct kvm_vcpu *vcpu)
  3839. {
  3840. kvm_register_read(vcpu, VCPU_REGS_RAX);
  3841. kvm_register_read(vcpu, VCPU_REGS_RSP);
  3842. kvm_register_read(vcpu, VCPU_REGS_RIP);
  3843. vcpu->arch.regs_dirty = ~0;
  3844. }
  3845. static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
  3846. {
  3847. u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask);
  3848. /*
  3849. * an sti; sti; sequence only disable interrupts for the first
  3850. * instruction. So, if the last instruction, be it emulated or
  3851. * not, left the system with the INT_STI flag enabled, it
  3852. * means that the last instruction is an sti. We should not
  3853. * leave the flag on in this case. The same goes for mov ss
  3854. */
  3855. if (!(int_shadow & mask))
  3856. kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
  3857. }
  3858. static void inject_emulated_exception(struct kvm_vcpu *vcpu)
  3859. {
  3860. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  3861. if (ctxt->exception.vector == PF_VECTOR)
  3862. kvm_propagate_fault(vcpu, &ctxt->exception);
  3863. else if (ctxt->exception.error_code_valid)
  3864. kvm_queue_exception_e(vcpu, ctxt->exception.vector,
  3865. ctxt->exception.error_code);
  3866. else
  3867. kvm_queue_exception(vcpu, ctxt->exception.vector);
  3868. }
  3869. static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
  3870. {
  3871. struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
  3872. int cs_db, cs_l;
  3873. cache_all_regs(vcpu);
  3874. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  3875. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  3876. vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
  3877. vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
  3878. vcpu->arch.emulate_ctxt.mode =
  3879. (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  3880. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  3881. ? X86EMUL_MODE_VM86 : cs_l
  3882. ? X86EMUL_MODE_PROT64 : cs_db
  3883. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  3884. vcpu->arch.emulate_ctxt.guest_mode = is_guest_mode(vcpu);
  3885. memset(c, 0, sizeof(struct decode_cache));
  3886. memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
  3887. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  3888. }
  3889. int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq)
  3890. {
  3891. struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
  3892. int ret;
  3893. init_emulate_ctxt(vcpu);
  3894. vcpu->arch.emulate_ctxt.decode.op_bytes = 2;
  3895. vcpu->arch.emulate_ctxt.decode.ad_bytes = 2;
  3896. vcpu->arch.emulate_ctxt.decode.eip = vcpu->arch.emulate_ctxt.eip;
  3897. ret = emulate_int_real(&vcpu->arch.emulate_ctxt, &emulate_ops, irq);
  3898. if (ret != X86EMUL_CONTINUE)
  3899. return EMULATE_FAIL;
  3900. vcpu->arch.emulate_ctxt.eip = c->eip;
  3901. memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
  3902. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
  3903. kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  3904. if (irq == NMI_VECTOR)
  3905. vcpu->arch.nmi_pending = false;
  3906. else
  3907. vcpu->arch.interrupt.pending = false;
  3908. return EMULATE_DONE;
  3909. }
  3910. EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
  3911. static int handle_emulation_failure(struct kvm_vcpu *vcpu)
  3912. {
  3913. int r = EMULATE_DONE;
  3914. ++vcpu->stat.insn_emulation_fail;
  3915. trace_kvm_emulate_insn_failed(vcpu);
  3916. if (!is_guest_mode(vcpu)) {
  3917. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3918. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3919. vcpu->run->internal.ndata = 0;
  3920. r = EMULATE_FAIL;
  3921. }
  3922. kvm_queue_exception(vcpu, UD_VECTOR);
  3923. return r;
  3924. }
  3925. static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
  3926. {
  3927. gpa_t gpa;
  3928. if (tdp_enabled)
  3929. return false;
  3930. /*
  3931. * if emulation was due to access to shadowed page table
  3932. * and it failed try to unshadow page and re-entetr the
  3933. * guest to let CPU execute the instruction.
  3934. */
  3935. if (kvm_mmu_unprotect_page_virt(vcpu, gva))
  3936. return true;
  3937. gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);
  3938. if (gpa == UNMAPPED_GVA)
  3939. return true; /* let cpu generate fault */
  3940. if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT)))
  3941. return true;
  3942. return false;
  3943. }
  3944. int x86_emulate_instruction(struct kvm_vcpu *vcpu,
  3945. unsigned long cr2,
  3946. int emulation_type,
  3947. void *insn,
  3948. int insn_len)
  3949. {
  3950. int r;
  3951. struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
  3952. bool writeback = true;
  3953. kvm_clear_exception_queue(vcpu);
  3954. vcpu->arch.mmio_fault_cr2 = cr2;
  3955. /*
  3956. * TODO: fix emulate.c to use guest_read/write_register
  3957. * instead of direct ->regs accesses, can save hundred cycles
  3958. * on Intel for instructions that don't read/change RSP, for
  3959. * for example.
  3960. */
  3961. cache_all_regs(vcpu);
  3962. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  3963. init_emulate_ctxt(vcpu);
  3964. vcpu->arch.emulate_ctxt.interruptibility = 0;
  3965. vcpu->arch.emulate_ctxt.have_exception = false;
  3966. vcpu->arch.emulate_ctxt.perm_ok = false;
  3967. vcpu->arch.emulate_ctxt.only_vendor_specific_insn
  3968. = emulation_type & EMULTYPE_TRAP_UD;
  3969. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, insn, insn_len);
  3970. trace_kvm_emulate_insn_start(vcpu);
  3971. ++vcpu->stat.insn_emulation;
  3972. if (r) {
  3973. if (emulation_type & EMULTYPE_TRAP_UD)
  3974. return EMULATE_FAIL;
  3975. if (reexecute_instruction(vcpu, cr2))
  3976. return EMULATE_DONE;
  3977. if (emulation_type & EMULTYPE_SKIP)
  3978. return EMULATE_FAIL;
  3979. return handle_emulation_failure(vcpu);
  3980. }
  3981. }
  3982. if (emulation_type & EMULTYPE_SKIP) {
  3983. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
  3984. return EMULATE_DONE;
  3985. }
  3986. /* this is needed for vmware backdoor interface to work since it
  3987. changes registers values during IO operation */
  3988. if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
  3989. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  3990. memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
  3991. }
  3992. restart:
  3993. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);
  3994. if (r == EMULATION_INTERCEPTED)
  3995. return EMULATE_DONE;
  3996. if (r == EMULATION_FAILED) {
  3997. if (reexecute_instruction(vcpu, cr2))
  3998. return EMULATE_DONE;
  3999. return handle_emulation_failure(vcpu);
  4000. }
  4001. if (vcpu->arch.emulate_ctxt.have_exception) {
  4002. inject_emulated_exception(vcpu);
  4003. r = EMULATE_DONE;
  4004. } else if (vcpu->arch.pio.count) {
  4005. if (!vcpu->arch.pio.in)
  4006. vcpu->arch.pio.count = 0;
  4007. else
  4008. writeback = false;
  4009. r = EMULATE_DO_MMIO;
  4010. } else if (vcpu->mmio_needed) {
  4011. if (!vcpu->mmio_is_write)
  4012. writeback = false;
  4013. r = EMULATE_DO_MMIO;
  4014. } else if (r == EMULATION_RESTART)
  4015. goto restart;
  4016. else
  4017. r = EMULATE_DONE;
  4018. if (writeback) {
  4019. toggle_interruptibility(vcpu,
  4020. vcpu->arch.emulate_ctxt.interruptibility);
  4021. kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  4022. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4023. memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
  4024. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4025. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
  4026. } else
  4027. vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
  4028. return r;
  4029. }
  4030. EXPORT_SYMBOL_GPL(x86_emulate_instruction);
  4031. int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
  4032. {
  4033. unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4034. int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
  4035. /* do not return to emulator after return from userspace */
  4036. vcpu->arch.pio.count = 0;
  4037. return ret;
  4038. }
  4039. EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
  4040. static void tsc_bad(void *info)
  4041. {
  4042. __this_cpu_write(cpu_tsc_khz, 0);
  4043. }
  4044. static void tsc_khz_changed(void *data)
  4045. {
  4046. struct cpufreq_freqs *freq = data;
  4047. unsigned long khz = 0;
  4048. if (data)
  4049. khz = freq->new;
  4050. else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  4051. khz = cpufreq_quick_get(raw_smp_processor_id());
  4052. if (!khz)
  4053. khz = tsc_khz;
  4054. __this_cpu_write(cpu_tsc_khz, khz);
  4055. }
  4056. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  4057. void *data)
  4058. {
  4059. struct cpufreq_freqs *freq = data;
  4060. struct kvm *kvm;
  4061. struct kvm_vcpu *vcpu;
  4062. int i, send_ipi = 0;
  4063. /*
  4064. * We allow guests to temporarily run on slowing clocks,
  4065. * provided we notify them after, or to run on accelerating
  4066. * clocks, provided we notify them before. Thus time never
  4067. * goes backwards.
  4068. *
  4069. * However, we have a problem. We can't atomically update
  4070. * the frequency of a given CPU from this function; it is
  4071. * merely a notifier, which can be called from any CPU.
  4072. * Changing the TSC frequency at arbitrary points in time
  4073. * requires a recomputation of local variables related to
  4074. * the TSC for each VCPU. We must flag these local variables
  4075. * to be updated and be sure the update takes place with the
  4076. * new frequency before any guests proceed.
  4077. *
  4078. * Unfortunately, the combination of hotplug CPU and frequency
  4079. * change creates an intractable locking scenario; the order
  4080. * of when these callouts happen is undefined with respect to
  4081. * CPU hotplug, and they can race with each other. As such,
  4082. * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
  4083. * undefined; you can actually have a CPU frequency change take
  4084. * place in between the computation of X and the setting of the
  4085. * variable. To protect against this problem, all updates of
  4086. * the per_cpu tsc_khz variable are done in an interrupt
  4087. * protected IPI, and all callers wishing to update the value
  4088. * must wait for a synchronous IPI to complete (which is trivial
  4089. * if the caller is on the CPU already). This establishes the
  4090. * necessary total order on variable updates.
  4091. *
  4092. * Note that because a guest time update may take place
  4093. * anytime after the setting of the VCPU's request bit, the
  4094. * correct TSC value must be set before the request. However,
  4095. * to ensure the update actually makes it to any guest which
  4096. * starts running in hardware virtualization between the set
  4097. * and the acquisition of the spinlock, we must also ping the
  4098. * CPU after setting the request bit.
  4099. *
  4100. */
  4101. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  4102. return 0;
  4103. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  4104. return 0;
  4105. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4106. raw_spin_lock(&kvm_lock);
  4107. list_for_each_entry(kvm, &vm_list, vm_list) {
  4108. kvm_for_each_vcpu(i, vcpu, kvm) {
  4109. if (vcpu->cpu != freq->cpu)
  4110. continue;
  4111. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  4112. if (vcpu->cpu != smp_processor_id())
  4113. send_ipi = 1;
  4114. }
  4115. }
  4116. raw_spin_unlock(&kvm_lock);
  4117. if (freq->old < freq->new && send_ipi) {
  4118. /*
  4119. * We upscale the frequency. Must make the guest
  4120. * doesn't see old kvmclock values while running with
  4121. * the new frequency, otherwise we risk the guest sees
  4122. * time go backwards.
  4123. *
  4124. * In case we update the frequency for another cpu
  4125. * (which might be in guest context) send an interrupt
  4126. * to kick the cpu out of guest context. Next time
  4127. * guest context is entered kvmclock will be updated,
  4128. * so the guest will not see stale values.
  4129. */
  4130. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4131. }
  4132. return 0;
  4133. }
  4134. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  4135. .notifier_call = kvmclock_cpufreq_notifier
  4136. };
  4137. static int kvmclock_cpu_notifier(struct notifier_block *nfb,
  4138. unsigned long action, void *hcpu)
  4139. {
  4140. unsigned int cpu = (unsigned long)hcpu;
  4141. switch (action) {
  4142. case CPU_ONLINE:
  4143. case CPU_DOWN_FAILED:
  4144. smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
  4145. break;
  4146. case CPU_DOWN_PREPARE:
  4147. smp_call_function_single(cpu, tsc_bad, NULL, 1);
  4148. break;
  4149. }
  4150. return NOTIFY_OK;
  4151. }
  4152. static struct notifier_block kvmclock_cpu_notifier_block = {
  4153. .notifier_call = kvmclock_cpu_notifier,
  4154. .priority = -INT_MAX
  4155. };
  4156. static void kvm_timer_init(void)
  4157. {
  4158. int cpu;
  4159. max_tsc_khz = tsc_khz;
  4160. register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
  4161. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4162. #ifdef CONFIG_CPU_FREQ
  4163. struct cpufreq_policy policy;
  4164. memset(&policy, 0, sizeof(policy));
  4165. cpu = get_cpu();
  4166. cpufreq_get_policy(&policy, cpu);
  4167. if (policy.cpuinfo.max_freq)
  4168. max_tsc_khz = policy.cpuinfo.max_freq;
  4169. put_cpu();
  4170. #endif
  4171. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  4172. CPUFREQ_TRANSITION_NOTIFIER);
  4173. }
  4174. pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
  4175. for_each_online_cpu(cpu)
  4176. smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
  4177. }
  4178. static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
  4179. static int kvm_is_in_guest(void)
  4180. {
  4181. return percpu_read(current_vcpu) != NULL;
  4182. }
  4183. static int kvm_is_user_mode(void)
  4184. {
  4185. int user_mode = 3;
  4186. if (percpu_read(current_vcpu))
  4187. user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
  4188. return user_mode != 0;
  4189. }
  4190. static unsigned long kvm_get_guest_ip(void)
  4191. {
  4192. unsigned long ip = 0;
  4193. if (percpu_read(current_vcpu))
  4194. ip = kvm_rip_read(percpu_read(current_vcpu));
  4195. return ip;
  4196. }
  4197. static struct perf_guest_info_callbacks kvm_guest_cbs = {
  4198. .is_in_guest = kvm_is_in_guest,
  4199. .is_user_mode = kvm_is_user_mode,
  4200. .get_guest_ip = kvm_get_guest_ip,
  4201. };
  4202. void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
  4203. {
  4204. percpu_write(current_vcpu, vcpu);
  4205. }
  4206. EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
  4207. void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
  4208. {
  4209. percpu_write(current_vcpu, NULL);
  4210. }
  4211. EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
  4212. int kvm_arch_init(void *opaque)
  4213. {
  4214. int r;
  4215. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  4216. if (kvm_x86_ops) {
  4217. printk(KERN_ERR "kvm: already loaded the other module\n");
  4218. r = -EEXIST;
  4219. goto out;
  4220. }
  4221. if (!ops->cpu_has_kvm_support()) {
  4222. printk(KERN_ERR "kvm: no hardware support\n");
  4223. r = -EOPNOTSUPP;
  4224. goto out;
  4225. }
  4226. if (ops->disabled_by_bios()) {
  4227. printk(KERN_ERR "kvm: disabled by bios\n");
  4228. r = -EOPNOTSUPP;
  4229. goto out;
  4230. }
  4231. r = kvm_mmu_module_init();
  4232. if (r)
  4233. goto out;
  4234. kvm_init_msr_list();
  4235. kvm_x86_ops = ops;
  4236. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  4237. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  4238. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  4239. kvm_timer_init();
  4240. perf_register_guest_info_callbacks(&kvm_guest_cbs);
  4241. if (cpu_has_xsave)
  4242. host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  4243. return 0;
  4244. out:
  4245. return r;
  4246. }
  4247. void kvm_arch_exit(void)
  4248. {
  4249. perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
  4250. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  4251. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  4252. CPUFREQ_TRANSITION_NOTIFIER);
  4253. unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
  4254. kvm_x86_ops = NULL;
  4255. kvm_mmu_module_exit();
  4256. }
  4257. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  4258. {
  4259. ++vcpu->stat.halt_exits;
  4260. if (irqchip_in_kernel(vcpu->kvm)) {
  4261. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  4262. return 1;
  4263. } else {
  4264. vcpu->run->exit_reason = KVM_EXIT_HLT;
  4265. return 0;
  4266. }
  4267. }
  4268. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  4269. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  4270. unsigned long a1)
  4271. {
  4272. if (is_long_mode(vcpu))
  4273. return a0;
  4274. else
  4275. return a0 | ((gpa_t)a1 << 32);
  4276. }
  4277. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  4278. {
  4279. u64 param, ingpa, outgpa, ret;
  4280. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  4281. bool fast, longmode;
  4282. int cs_db, cs_l;
  4283. /*
  4284. * hypercall generates UD from non zero cpl and real mode
  4285. * per HYPER-V spec
  4286. */
  4287. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  4288. kvm_queue_exception(vcpu, UD_VECTOR);
  4289. return 0;
  4290. }
  4291. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  4292. longmode = is_long_mode(vcpu) && cs_l == 1;
  4293. if (!longmode) {
  4294. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  4295. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  4296. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  4297. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  4298. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  4299. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  4300. }
  4301. #ifdef CONFIG_X86_64
  4302. else {
  4303. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4304. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4305. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  4306. }
  4307. #endif
  4308. code = param & 0xffff;
  4309. fast = (param >> 16) & 0x1;
  4310. rep_cnt = (param >> 32) & 0xfff;
  4311. rep_idx = (param >> 48) & 0xfff;
  4312. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  4313. switch (code) {
  4314. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  4315. kvm_vcpu_on_spin(vcpu);
  4316. break;
  4317. default:
  4318. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  4319. break;
  4320. }
  4321. ret = res | (((u64)rep_done & 0xfff) << 32);
  4322. if (longmode) {
  4323. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  4324. } else {
  4325. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  4326. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  4327. }
  4328. return 1;
  4329. }
  4330. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  4331. {
  4332. unsigned long nr, a0, a1, a2, a3, ret;
  4333. int r = 1;
  4334. if (kvm_hv_hypercall_enabled(vcpu->kvm))
  4335. return kvm_hv_hypercall(vcpu);
  4336. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4337. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4338. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4339. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4340. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4341. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  4342. if (!is_long_mode(vcpu)) {
  4343. nr &= 0xFFFFFFFF;
  4344. a0 &= 0xFFFFFFFF;
  4345. a1 &= 0xFFFFFFFF;
  4346. a2 &= 0xFFFFFFFF;
  4347. a3 &= 0xFFFFFFFF;
  4348. }
  4349. if (kvm_x86_ops->get_cpl(vcpu) != 0) {
  4350. ret = -KVM_EPERM;
  4351. goto out;
  4352. }
  4353. switch (nr) {
  4354. case KVM_HC_VAPIC_POLL_IRQ:
  4355. ret = 0;
  4356. break;
  4357. case KVM_HC_MMU_OP:
  4358. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  4359. break;
  4360. default:
  4361. ret = -KVM_ENOSYS;
  4362. break;
  4363. }
  4364. out:
  4365. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  4366. ++vcpu->stat.hypercalls;
  4367. return r;
  4368. }
  4369. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  4370. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  4371. {
  4372. char instruction[3];
  4373. unsigned long rip = kvm_rip_read(vcpu);
  4374. /*
  4375. * Blow out the MMU to ensure that no other VCPU has an active mapping
  4376. * to ensure that the updated hypercall appears atomically across all
  4377. * VCPUs.
  4378. */
  4379. kvm_mmu_zap_all(vcpu->kvm);
  4380. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  4381. return emulator_write_emulated(rip, instruction, 3, NULL, vcpu);
  4382. }
  4383. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  4384. {
  4385. struct desc_ptr dt = { limit, base };
  4386. kvm_x86_ops->set_gdt(vcpu, &dt);
  4387. }
  4388. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  4389. {
  4390. struct desc_ptr dt = { limit, base };
  4391. kvm_x86_ops->set_idt(vcpu, &dt);
  4392. }
  4393. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  4394. {
  4395. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  4396. int j, nent = vcpu->arch.cpuid_nent;
  4397. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  4398. /* when no next entry is found, the current entry[i] is reselected */
  4399. for (j = i + 1; ; j = (j + 1) % nent) {
  4400. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  4401. if (ej->function == e->function) {
  4402. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  4403. return j;
  4404. }
  4405. }
  4406. return 0; /* silence gcc, even though control never reaches here */
  4407. }
  4408. /* find an entry with matching function, matching index (if needed), and that
  4409. * should be read next (if it's stateful) */
  4410. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  4411. u32 function, u32 index)
  4412. {
  4413. if (e->function != function)
  4414. return 0;
  4415. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  4416. return 0;
  4417. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  4418. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  4419. return 0;
  4420. return 1;
  4421. }
  4422. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
  4423. u32 function, u32 index)
  4424. {
  4425. int i;
  4426. struct kvm_cpuid_entry2 *best = NULL;
  4427. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  4428. struct kvm_cpuid_entry2 *e;
  4429. e = &vcpu->arch.cpuid_entries[i];
  4430. if (is_matching_cpuid_entry(e, function, index)) {
  4431. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  4432. move_to_next_stateful_cpuid_entry(vcpu, i);
  4433. best = e;
  4434. break;
  4435. }
  4436. }
  4437. return best;
  4438. }
  4439. EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
  4440. int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
  4441. {
  4442. struct kvm_cpuid_entry2 *best;
  4443. best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
  4444. if (!best || best->eax < 0x80000008)
  4445. goto not_found;
  4446. best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
  4447. if (best)
  4448. return best->eax & 0xff;
  4449. not_found:
  4450. return 36;
  4451. }
  4452. /*
  4453. * If no match is found, check whether we exceed the vCPU's limit
  4454. * and return the content of the highest valid _standard_ leaf instead.
  4455. * This is to satisfy the CPUID specification.
  4456. */
  4457. static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,
  4458. u32 function, u32 index)
  4459. {
  4460. struct kvm_cpuid_entry2 *maxlevel;
  4461. maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
  4462. if (!maxlevel || maxlevel->eax >= function)
  4463. return NULL;
  4464. if (function & 0x80000000) {
  4465. maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0);
  4466. if (!maxlevel)
  4467. return NULL;
  4468. }
  4469. return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index);
  4470. }
  4471. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  4472. {
  4473. u32 function, index;
  4474. struct kvm_cpuid_entry2 *best;
  4475. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4476. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4477. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  4478. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  4479. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  4480. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  4481. best = kvm_find_cpuid_entry(vcpu, function, index);
  4482. if (!best)
  4483. best = check_cpuid_limit(vcpu, function, index);
  4484. if (best) {
  4485. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  4486. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  4487. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  4488. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  4489. }
  4490. kvm_x86_ops->skip_emulated_instruction(vcpu);
  4491. trace_kvm_cpuid(function,
  4492. kvm_register_read(vcpu, VCPU_REGS_RAX),
  4493. kvm_register_read(vcpu, VCPU_REGS_RBX),
  4494. kvm_register_read(vcpu, VCPU_REGS_RCX),
  4495. kvm_register_read(vcpu, VCPU_REGS_RDX));
  4496. }
  4497. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  4498. /*
  4499. * Check if userspace requested an interrupt window, and that the
  4500. * interrupt window is open.
  4501. *
  4502. * No need to exit to userspace if we already have an interrupt queued.
  4503. */
  4504. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
  4505. {
  4506. return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
  4507. vcpu->run->request_interrupt_window &&
  4508. kvm_arch_interrupt_allowed(vcpu));
  4509. }
  4510. static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  4511. {
  4512. struct kvm_run *kvm_run = vcpu->run;
  4513. kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  4514. kvm_run->cr8 = kvm_get_cr8(vcpu);
  4515. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  4516. if (irqchip_in_kernel(vcpu->kvm))
  4517. kvm_run->ready_for_interrupt_injection = 1;
  4518. else
  4519. kvm_run->ready_for_interrupt_injection =
  4520. kvm_arch_interrupt_allowed(vcpu) &&
  4521. !kvm_cpu_has_interrupt(vcpu) &&
  4522. !kvm_event_needs_reinjection(vcpu);
  4523. }
  4524. static void vapic_enter(struct kvm_vcpu *vcpu)
  4525. {
  4526. struct kvm_lapic *apic = vcpu->arch.apic;
  4527. struct page *page;
  4528. if (!apic || !apic->vapic_addr)
  4529. return;
  4530. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  4531. vcpu->arch.apic->vapic_page = page;
  4532. }
  4533. static void vapic_exit(struct kvm_vcpu *vcpu)
  4534. {
  4535. struct kvm_lapic *apic = vcpu->arch.apic;
  4536. int idx;
  4537. if (!apic || !apic->vapic_addr)
  4538. return;
  4539. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4540. kvm_release_page_dirty(apic->vapic_page);
  4541. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  4542. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4543. }
  4544. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  4545. {
  4546. int max_irr, tpr;
  4547. if (!kvm_x86_ops->update_cr8_intercept)
  4548. return;
  4549. if (!vcpu->arch.apic)
  4550. return;
  4551. if (!vcpu->arch.apic->vapic_addr)
  4552. max_irr = kvm_lapic_find_highest_irr(vcpu);
  4553. else
  4554. max_irr = -1;
  4555. if (max_irr != -1)
  4556. max_irr >>= 4;
  4557. tpr = kvm_lapic_get_cr8(vcpu);
  4558. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  4559. }
  4560. static void inject_pending_event(struct kvm_vcpu *vcpu)
  4561. {
  4562. /* try to reinject previous events if any */
  4563. if (vcpu->arch.exception.pending) {
  4564. trace_kvm_inj_exception(vcpu->arch.exception.nr,
  4565. vcpu->arch.exception.has_error_code,
  4566. vcpu->arch.exception.error_code);
  4567. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  4568. vcpu->arch.exception.has_error_code,
  4569. vcpu->arch.exception.error_code,
  4570. vcpu->arch.exception.reinject);
  4571. return;
  4572. }
  4573. if (vcpu->arch.nmi_injected) {
  4574. kvm_x86_ops->set_nmi(vcpu);
  4575. return;
  4576. }
  4577. if (vcpu->arch.interrupt.pending) {
  4578. kvm_x86_ops->set_irq(vcpu);
  4579. return;
  4580. }
  4581. /* try to inject new event if pending */
  4582. if (vcpu->arch.nmi_pending) {
  4583. if (kvm_x86_ops->nmi_allowed(vcpu)) {
  4584. vcpu->arch.nmi_pending = false;
  4585. vcpu->arch.nmi_injected = true;
  4586. kvm_x86_ops->set_nmi(vcpu);
  4587. }
  4588. } else if (kvm_cpu_has_interrupt(vcpu)) {
  4589. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  4590. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  4591. false);
  4592. kvm_x86_ops->set_irq(vcpu);
  4593. }
  4594. }
  4595. }
  4596. static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
  4597. {
  4598. if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
  4599. !vcpu->guest_xcr0_loaded) {
  4600. /* kvm_set_xcr() also depends on this */
  4601. xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
  4602. vcpu->guest_xcr0_loaded = 1;
  4603. }
  4604. }
  4605. static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
  4606. {
  4607. if (vcpu->guest_xcr0_loaded) {
  4608. if (vcpu->arch.xcr0 != host_xcr0)
  4609. xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
  4610. vcpu->guest_xcr0_loaded = 0;
  4611. }
  4612. }
  4613. static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  4614. {
  4615. int r;
  4616. bool nmi_pending;
  4617. bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
  4618. vcpu->run->request_interrupt_window;
  4619. if (vcpu->requests) {
  4620. if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
  4621. kvm_mmu_unload(vcpu);
  4622. if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
  4623. __kvm_migrate_timers(vcpu);
  4624. if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
  4625. r = kvm_guest_time_update(vcpu);
  4626. if (unlikely(r))
  4627. goto out;
  4628. }
  4629. if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
  4630. kvm_mmu_sync_roots(vcpu);
  4631. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  4632. kvm_x86_ops->tlb_flush(vcpu);
  4633. if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
  4634. vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
  4635. r = 0;
  4636. goto out;
  4637. }
  4638. if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
  4639. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  4640. r = 0;
  4641. goto out;
  4642. }
  4643. if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
  4644. vcpu->fpu_active = 0;
  4645. kvm_x86_ops->fpu_deactivate(vcpu);
  4646. }
  4647. if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
  4648. /* Page is swapped out. Do synthetic halt */
  4649. vcpu->arch.apf.halted = true;
  4650. r = 1;
  4651. goto out;
  4652. }
  4653. }
  4654. r = kvm_mmu_reload(vcpu);
  4655. if (unlikely(r))
  4656. goto out;
  4657. /*
  4658. * An NMI can be injected between local nmi_pending read and
  4659. * vcpu->arch.nmi_pending read inside inject_pending_event().
  4660. * But in that case, KVM_REQ_EVENT will be set, which makes
  4661. * the race described above benign.
  4662. */
  4663. nmi_pending = ACCESS_ONCE(vcpu->arch.nmi_pending);
  4664. if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
  4665. inject_pending_event(vcpu);
  4666. /* enable NMI/IRQ window open exits if needed */
  4667. if (nmi_pending)
  4668. kvm_x86_ops->enable_nmi_window(vcpu);
  4669. else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
  4670. kvm_x86_ops->enable_irq_window(vcpu);
  4671. if (kvm_lapic_enabled(vcpu)) {
  4672. update_cr8_intercept(vcpu);
  4673. kvm_lapic_sync_to_vapic(vcpu);
  4674. }
  4675. }
  4676. preempt_disable();
  4677. kvm_x86_ops->prepare_guest_switch(vcpu);
  4678. if (vcpu->fpu_active)
  4679. kvm_load_guest_fpu(vcpu);
  4680. kvm_load_guest_xcr0(vcpu);
  4681. vcpu->mode = IN_GUEST_MODE;
  4682. /* We should set ->mode before check ->requests,
  4683. * see the comment in make_all_cpus_request.
  4684. */
  4685. smp_mb();
  4686. local_irq_disable();
  4687. if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
  4688. || need_resched() || signal_pending(current)) {
  4689. vcpu->mode = OUTSIDE_GUEST_MODE;
  4690. smp_wmb();
  4691. local_irq_enable();
  4692. preempt_enable();
  4693. kvm_x86_ops->cancel_injection(vcpu);
  4694. r = 1;
  4695. goto out;
  4696. }
  4697. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  4698. kvm_guest_enter();
  4699. if (unlikely(vcpu->arch.switch_db_regs)) {
  4700. set_debugreg(0, 7);
  4701. set_debugreg(vcpu->arch.eff_db[0], 0);
  4702. set_debugreg(vcpu->arch.eff_db[1], 1);
  4703. set_debugreg(vcpu->arch.eff_db[2], 2);
  4704. set_debugreg(vcpu->arch.eff_db[3], 3);
  4705. }
  4706. trace_kvm_entry(vcpu->vcpu_id);
  4707. kvm_x86_ops->run(vcpu);
  4708. /*
  4709. * If the guest has used debug registers, at least dr7
  4710. * will be disabled while returning to the host.
  4711. * If we don't have active breakpoints in the host, we don't
  4712. * care about the messed up debug address registers. But if
  4713. * we have some of them active, restore the old state.
  4714. */
  4715. if (hw_breakpoint_active())
  4716. hw_breakpoint_restore();
  4717. kvm_get_msr(vcpu, MSR_IA32_TSC, &vcpu->arch.last_guest_tsc);
  4718. vcpu->mode = OUTSIDE_GUEST_MODE;
  4719. smp_wmb();
  4720. local_irq_enable();
  4721. ++vcpu->stat.exits;
  4722. /*
  4723. * We must have an instruction between local_irq_enable() and
  4724. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  4725. * the interrupt shadow. The stat.exits increment will do nicely.
  4726. * But we need to prevent reordering, hence this barrier():
  4727. */
  4728. barrier();
  4729. kvm_guest_exit();
  4730. preempt_enable();
  4731. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4732. /*
  4733. * Profile KVM exit RIPs:
  4734. */
  4735. if (unlikely(prof_on == KVM_PROFILING)) {
  4736. unsigned long rip = kvm_rip_read(vcpu);
  4737. profile_hit(KVM_PROFILING, (void *)rip);
  4738. }
  4739. kvm_lapic_sync_from_vapic(vcpu);
  4740. r = kvm_x86_ops->handle_exit(vcpu);
  4741. out:
  4742. return r;
  4743. }
  4744. static int __vcpu_run(struct kvm_vcpu *vcpu)
  4745. {
  4746. int r;
  4747. struct kvm *kvm = vcpu->kvm;
  4748. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  4749. pr_debug("vcpu %d received sipi with vector # %x\n",
  4750. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  4751. kvm_lapic_reset(vcpu);
  4752. r = kvm_arch_vcpu_reset(vcpu);
  4753. if (r)
  4754. return r;
  4755. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4756. }
  4757. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4758. vapic_enter(vcpu);
  4759. r = 1;
  4760. while (r > 0) {
  4761. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  4762. !vcpu->arch.apf.halted)
  4763. r = vcpu_enter_guest(vcpu);
  4764. else {
  4765. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4766. kvm_vcpu_block(vcpu);
  4767. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4768. if (kvm_check_request(KVM_REQ_UNHALT, vcpu))
  4769. {
  4770. switch(vcpu->arch.mp_state) {
  4771. case KVM_MP_STATE_HALTED:
  4772. vcpu->arch.mp_state =
  4773. KVM_MP_STATE_RUNNABLE;
  4774. case KVM_MP_STATE_RUNNABLE:
  4775. vcpu->arch.apf.halted = false;
  4776. break;
  4777. case KVM_MP_STATE_SIPI_RECEIVED:
  4778. default:
  4779. r = -EINTR;
  4780. break;
  4781. }
  4782. }
  4783. }
  4784. if (r <= 0)
  4785. break;
  4786. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  4787. if (kvm_cpu_has_pending_timer(vcpu))
  4788. kvm_inject_pending_timer_irqs(vcpu);
  4789. if (dm_request_for_irq_injection(vcpu)) {
  4790. r = -EINTR;
  4791. vcpu->run->exit_reason = KVM_EXIT_INTR;
  4792. ++vcpu->stat.request_irq_exits;
  4793. }
  4794. kvm_check_async_pf_completion(vcpu);
  4795. if (signal_pending(current)) {
  4796. r = -EINTR;
  4797. vcpu->run->exit_reason = KVM_EXIT_INTR;
  4798. ++vcpu->stat.signal_exits;
  4799. }
  4800. if (need_resched()) {
  4801. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4802. kvm_resched(vcpu);
  4803. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4804. }
  4805. }
  4806. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4807. vapic_exit(vcpu);
  4808. return r;
  4809. }
  4810. static int complete_mmio(struct kvm_vcpu *vcpu)
  4811. {
  4812. struct kvm_run *run = vcpu->run;
  4813. int r;
  4814. if (!(vcpu->arch.pio.count || vcpu->mmio_needed))
  4815. return 1;
  4816. if (vcpu->mmio_needed) {
  4817. vcpu->mmio_needed = 0;
  4818. if (!vcpu->mmio_is_write)
  4819. memcpy(vcpu->mmio_data, run->mmio.data, 8);
  4820. vcpu->mmio_index += 8;
  4821. if (vcpu->mmio_index < vcpu->mmio_size) {
  4822. run->exit_reason = KVM_EXIT_MMIO;
  4823. run->mmio.phys_addr = vcpu->mmio_phys_addr + vcpu->mmio_index;
  4824. memcpy(run->mmio.data, vcpu->mmio_data + vcpu->mmio_index, 8);
  4825. run->mmio.len = min(vcpu->mmio_size - vcpu->mmio_index, 8);
  4826. run->mmio.is_write = vcpu->mmio_is_write;
  4827. vcpu->mmio_needed = 1;
  4828. return 0;
  4829. }
  4830. if (vcpu->mmio_is_write)
  4831. return 1;
  4832. vcpu->mmio_read_completed = 1;
  4833. }
  4834. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4835. r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
  4836. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  4837. if (r != EMULATE_DONE)
  4838. return 0;
  4839. return 1;
  4840. }
  4841. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  4842. {
  4843. int r;
  4844. sigset_t sigsaved;
  4845. if (!tsk_used_math(current) && init_fpu(current))
  4846. return -ENOMEM;
  4847. if (vcpu->sigset_active)
  4848. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  4849. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  4850. kvm_vcpu_block(vcpu);
  4851. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  4852. r = -EAGAIN;
  4853. goto out;
  4854. }
  4855. /* re-sync apic's tpr */
  4856. if (!irqchip_in_kernel(vcpu->kvm)) {
  4857. if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
  4858. r = -EINVAL;
  4859. goto out;
  4860. }
  4861. }
  4862. r = complete_mmio(vcpu);
  4863. if (r <= 0)
  4864. goto out;
  4865. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  4866. kvm_register_write(vcpu, VCPU_REGS_RAX,
  4867. kvm_run->hypercall.ret);
  4868. r = __vcpu_run(vcpu);
  4869. out:
  4870. post_kvm_run_save(vcpu);
  4871. if (vcpu->sigset_active)
  4872. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  4873. return r;
  4874. }
  4875. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  4876. {
  4877. if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
  4878. /*
  4879. * We are here if userspace calls get_regs() in the middle of
  4880. * instruction emulation. Registers state needs to be copied
  4881. * back from emulation context to vcpu. Usrapace shouldn't do
  4882. * that usually, but some bad designed PV devices (vmware
  4883. * backdoor interface) need this to work
  4884. */
  4885. struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
  4886. memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
  4887. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4888. }
  4889. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4890. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4891. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4892. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4893. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4894. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  4895. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  4896. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  4897. #ifdef CONFIG_X86_64
  4898. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  4899. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  4900. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  4901. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  4902. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  4903. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  4904. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  4905. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  4906. #endif
  4907. regs->rip = kvm_rip_read(vcpu);
  4908. regs->rflags = kvm_get_rflags(vcpu);
  4909. return 0;
  4910. }
  4911. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  4912. {
  4913. vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
  4914. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4915. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  4916. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  4917. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  4918. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  4919. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  4920. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  4921. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  4922. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  4923. #ifdef CONFIG_X86_64
  4924. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  4925. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  4926. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  4927. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  4928. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  4929. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  4930. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  4931. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  4932. #endif
  4933. kvm_rip_write(vcpu, regs->rip);
  4934. kvm_set_rflags(vcpu, regs->rflags);
  4935. vcpu->arch.exception.pending = false;
  4936. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4937. return 0;
  4938. }
  4939. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4940. {
  4941. struct kvm_segment cs;
  4942. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4943. *db = cs.db;
  4944. *l = cs.l;
  4945. }
  4946. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  4947. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  4948. struct kvm_sregs *sregs)
  4949. {
  4950. struct desc_ptr dt;
  4951. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4952. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4953. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4954. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4955. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4956. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4957. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4958. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4959. kvm_x86_ops->get_idt(vcpu, &dt);
  4960. sregs->idt.limit = dt.size;
  4961. sregs->idt.base = dt.address;
  4962. kvm_x86_ops->get_gdt(vcpu, &dt);
  4963. sregs->gdt.limit = dt.size;
  4964. sregs->gdt.base = dt.address;
  4965. sregs->cr0 = kvm_read_cr0(vcpu);
  4966. sregs->cr2 = vcpu->arch.cr2;
  4967. sregs->cr3 = kvm_read_cr3(vcpu);
  4968. sregs->cr4 = kvm_read_cr4(vcpu);
  4969. sregs->cr8 = kvm_get_cr8(vcpu);
  4970. sregs->efer = vcpu->arch.efer;
  4971. sregs->apic_base = kvm_get_apic_base(vcpu);
  4972. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  4973. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  4974. set_bit(vcpu->arch.interrupt.nr,
  4975. (unsigned long *)sregs->interrupt_bitmap);
  4976. return 0;
  4977. }
  4978. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  4979. struct kvm_mp_state *mp_state)
  4980. {
  4981. mp_state->mp_state = vcpu->arch.mp_state;
  4982. return 0;
  4983. }
  4984. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  4985. struct kvm_mp_state *mp_state)
  4986. {
  4987. vcpu->arch.mp_state = mp_state->mp_state;
  4988. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4989. return 0;
  4990. }
  4991. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
  4992. bool has_error_code, u32 error_code)
  4993. {
  4994. struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
  4995. int ret;
  4996. init_emulate_ctxt(vcpu);
  4997. ret = emulator_task_switch(&vcpu->arch.emulate_ctxt,
  4998. tss_selector, reason, has_error_code,
  4999. error_code);
  5000. if (ret)
  5001. return EMULATE_FAIL;
  5002. memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
  5003. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
  5004. kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  5005. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5006. return EMULATE_DONE;
  5007. }
  5008. EXPORT_SYMBOL_GPL(kvm_task_switch);
  5009. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  5010. struct kvm_sregs *sregs)
  5011. {
  5012. int mmu_reset_needed = 0;
  5013. int pending_vec, max_bits, idx;
  5014. struct desc_ptr dt;
  5015. dt.size = sregs->idt.limit;
  5016. dt.address = sregs->idt.base;
  5017. kvm_x86_ops->set_idt(vcpu, &dt);
  5018. dt.size = sregs->gdt.limit;
  5019. dt.address = sregs->gdt.base;
  5020. kvm_x86_ops->set_gdt(vcpu, &dt);
  5021. vcpu->arch.cr2 = sregs->cr2;
  5022. mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
  5023. vcpu->arch.cr3 = sregs->cr3;
  5024. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  5025. kvm_set_cr8(vcpu, sregs->cr8);
  5026. mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
  5027. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  5028. kvm_set_apic_base(vcpu, sregs->apic_base);
  5029. mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
  5030. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  5031. vcpu->arch.cr0 = sregs->cr0;
  5032. mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
  5033. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  5034. if (sregs->cr4 & X86_CR4_OSXSAVE)
  5035. update_cpuid(vcpu);
  5036. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5037. if (!is_long_mode(vcpu) && is_pae(vcpu)) {
  5038. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  5039. mmu_reset_needed = 1;
  5040. }
  5041. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5042. if (mmu_reset_needed)
  5043. kvm_mmu_reset_context(vcpu);
  5044. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  5045. pending_vec = find_first_bit(
  5046. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  5047. if (pending_vec < max_bits) {
  5048. kvm_queue_interrupt(vcpu, pending_vec, false);
  5049. pr_debug("Set back pending irq %d\n", pending_vec);
  5050. }
  5051. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  5052. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  5053. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  5054. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  5055. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  5056. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  5057. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  5058. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  5059. update_cr8_intercept(vcpu);
  5060. /* Older userspace won't unhalt the vcpu on reset. */
  5061. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  5062. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  5063. !is_protmode(vcpu))
  5064. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  5065. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5066. return 0;
  5067. }
  5068. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  5069. struct kvm_guest_debug *dbg)
  5070. {
  5071. unsigned long rflags;
  5072. int i, r;
  5073. if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
  5074. r = -EBUSY;
  5075. if (vcpu->arch.exception.pending)
  5076. goto out;
  5077. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  5078. kvm_queue_exception(vcpu, DB_VECTOR);
  5079. else
  5080. kvm_queue_exception(vcpu, BP_VECTOR);
  5081. }
  5082. /*
  5083. * Read rflags as long as potentially injected trace flags are still
  5084. * filtered out.
  5085. */
  5086. rflags = kvm_get_rflags(vcpu);
  5087. vcpu->guest_debug = dbg->control;
  5088. if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
  5089. vcpu->guest_debug = 0;
  5090. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  5091. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  5092. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  5093. vcpu->arch.switch_db_regs =
  5094. (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
  5095. } else {
  5096. for (i = 0; i < KVM_NR_DB_REGS; i++)
  5097. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  5098. vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
  5099. }
  5100. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5101. vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
  5102. get_segment_base(vcpu, VCPU_SREG_CS);
  5103. /*
  5104. * Trigger an rflags update that will inject or remove the trace
  5105. * flags.
  5106. */
  5107. kvm_set_rflags(vcpu, rflags);
  5108. kvm_x86_ops->set_guest_debug(vcpu, dbg);
  5109. r = 0;
  5110. out:
  5111. return r;
  5112. }
  5113. /*
  5114. * Translate a guest virtual address to a guest physical address.
  5115. */
  5116. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  5117. struct kvm_translation *tr)
  5118. {
  5119. unsigned long vaddr = tr->linear_address;
  5120. gpa_t gpa;
  5121. int idx;
  5122. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5123. gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
  5124. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5125. tr->physical_address = gpa;
  5126. tr->valid = gpa != UNMAPPED_GVA;
  5127. tr->writeable = 1;
  5128. tr->usermode = 0;
  5129. return 0;
  5130. }
  5131. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  5132. {
  5133. struct i387_fxsave_struct *fxsave =
  5134. &vcpu->arch.guest_fpu.state->fxsave;
  5135. memcpy(fpu->fpr, fxsave->st_space, 128);
  5136. fpu->fcw = fxsave->cwd;
  5137. fpu->fsw = fxsave->swd;
  5138. fpu->ftwx = fxsave->twd;
  5139. fpu->last_opcode = fxsave->fop;
  5140. fpu->last_ip = fxsave->rip;
  5141. fpu->last_dp = fxsave->rdp;
  5142. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  5143. return 0;
  5144. }
  5145. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  5146. {
  5147. struct i387_fxsave_struct *fxsave =
  5148. &vcpu->arch.guest_fpu.state->fxsave;
  5149. memcpy(fxsave->st_space, fpu->fpr, 128);
  5150. fxsave->cwd = fpu->fcw;
  5151. fxsave->swd = fpu->fsw;
  5152. fxsave->twd = fpu->ftwx;
  5153. fxsave->fop = fpu->last_opcode;
  5154. fxsave->rip = fpu->last_ip;
  5155. fxsave->rdp = fpu->last_dp;
  5156. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  5157. return 0;
  5158. }
  5159. int fx_init(struct kvm_vcpu *vcpu)
  5160. {
  5161. int err;
  5162. err = fpu_alloc(&vcpu->arch.guest_fpu);
  5163. if (err)
  5164. return err;
  5165. fpu_finit(&vcpu->arch.guest_fpu);
  5166. /*
  5167. * Ensure guest xcr0 is valid for loading
  5168. */
  5169. vcpu->arch.xcr0 = XSTATE_FP;
  5170. vcpu->arch.cr0 |= X86_CR0_ET;
  5171. return 0;
  5172. }
  5173. EXPORT_SYMBOL_GPL(fx_init);
  5174. static void fx_free(struct kvm_vcpu *vcpu)
  5175. {
  5176. fpu_free(&vcpu->arch.guest_fpu);
  5177. }
  5178. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  5179. {
  5180. if (vcpu->guest_fpu_loaded)
  5181. return;
  5182. /*
  5183. * Restore all possible states in the guest,
  5184. * and assume host would use all available bits.
  5185. * Guest xcr0 would be loaded later.
  5186. */
  5187. kvm_put_guest_xcr0(vcpu);
  5188. vcpu->guest_fpu_loaded = 1;
  5189. unlazy_fpu(current);
  5190. fpu_restore_checking(&vcpu->arch.guest_fpu);
  5191. trace_kvm_fpu(1);
  5192. }
  5193. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  5194. {
  5195. kvm_put_guest_xcr0(vcpu);
  5196. if (!vcpu->guest_fpu_loaded)
  5197. return;
  5198. vcpu->guest_fpu_loaded = 0;
  5199. fpu_save_init(&vcpu->arch.guest_fpu);
  5200. ++vcpu->stat.fpu_reload;
  5201. kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
  5202. trace_kvm_fpu(0);
  5203. }
  5204. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  5205. {
  5206. kvmclock_reset(vcpu);
  5207. free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
  5208. fx_free(vcpu);
  5209. kvm_x86_ops->vcpu_free(vcpu);
  5210. }
  5211. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  5212. unsigned int id)
  5213. {
  5214. if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
  5215. printk_once(KERN_WARNING
  5216. "kvm: SMP vm created on host with unstable TSC; "
  5217. "guest TSC will not be reliable\n");
  5218. return kvm_x86_ops->vcpu_create(kvm, id);
  5219. }
  5220. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  5221. {
  5222. int r;
  5223. vcpu->arch.mtrr_state.have_fixed = 1;
  5224. vcpu_load(vcpu);
  5225. r = kvm_arch_vcpu_reset(vcpu);
  5226. if (r == 0)
  5227. r = kvm_mmu_setup(vcpu);
  5228. vcpu_put(vcpu);
  5229. if (r < 0)
  5230. goto free_vcpu;
  5231. return 0;
  5232. free_vcpu:
  5233. kvm_x86_ops->vcpu_free(vcpu);
  5234. return r;
  5235. }
  5236. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  5237. {
  5238. vcpu->arch.apf.msr_val = 0;
  5239. vcpu_load(vcpu);
  5240. kvm_mmu_unload(vcpu);
  5241. vcpu_put(vcpu);
  5242. fx_free(vcpu);
  5243. kvm_x86_ops->vcpu_free(vcpu);
  5244. }
  5245. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  5246. {
  5247. vcpu->arch.nmi_pending = false;
  5248. vcpu->arch.nmi_injected = false;
  5249. vcpu->arch.switch_db_regs = 0;
  5250. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  5251. vcpu->arch.dr6 = DR6_FIXED_1;
  5252. vcpu->arch.dr7 = DR7_FIXED_1;
  5253. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5254. vcpu->arch.apf.msr_val = 0;
  5255. kvmclock_reset(vcpu);
  5256. kvm_clear_async_pf_completion_queue(vcpu);
  5257. kvm_async_pf_hash_reset(vcpu);
  5258. vcpu->arch.apf.halted = false;
  5259. return kvm_x86_ops->vcpu_reset(vcpu);
  5260. }
  5261. int kvm_arch_hardware_enable(void *garbage)
  5262. {
  5263. struct kvm *kvm;
  5264. struct kvm_vcpu *vcpu;
  5265. int i;
  5266. kvm_shared_msr_cpu_online();
  5267. list_for_each_entry(kvm, &vm_list, vm_list)
  5268. kvm_for_each_vcpu(i, vcpu, kvm)
  5269. if (vcpu->cpu == smp_processor_id())
  5270. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  5271. return kvm_x86_ops->hardware_enable(garbage);
  5272. }
  5273. void kvm_arch_hardware_disable(void *garbage)
  5274. {
  5275. kvm_x86_ops->hardware_disable(garbage);
  5276. drop_user_return_notifiers(garbage);
  5277. }
  5278. int kvm_arch_hardware_setup(void)
  5279. {
  5280. return kvm_x86_ops->hardware_setup();
  5281. }
  5282. void kvm_arch_hardware_unsetup(void)
  5283. {
  5284. kvm_x86_ops->hardware_unsetup();
  5285. }
  5286. void kvm_arch_check_processor_compat(void *rtn)
  5287. {
  5288. kvm_x86_ops->check_processor_compatibility(rtn);
  5289. }
  5290. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  5291. {
  5292. struct page *page;
  5293. struct kvm *kvm;
  5294. int r;
  5295. BUG_ON(vcpu->kvm == NULL);
  5296. kvm = vcpu->kvm;
  5297. vcpu->arch.emulate_ctxt.ops = &emulate_ops;
  5298. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  5299. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  5300. vcpu->arch.mmu.translate_gpa = translate_gpa;
  5301. vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
  5302. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
  5303. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  5304. else
  5305. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  5306. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  5307. if (!page) {
  5308. r = -ENOMEM;
  5309. goto fail;
  5310. }
  5311. vcpu->arch.pio_data = page_address(page);
  5312. kvm_init_tsc_catchup(vcpu, max_tsc_khz);
  5313. r = kvm_mmu_create(vcpu);
  5314. if (r < 0)
  5315. goto fail_free_pio_data;
  5316. if (irqchip_in_kernel(kvm)) {
  5317. r = kvm_create_lapic(vcpu);
  5318. if (r < 0)
  5319. goto fail_mmu_destroy;
  5320. }
  5321. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  5322. GFP_KERNEL);
  5323. if (!vcpu->arch.mce_banks) {
  5324. r = -ENOMEM;
  5325. goto fail_free_lapic;
  5326. }
  5327. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  5328. if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL))
  5329. goto fail_free_mce_banks;
  5330. kvm_async_pf_hash_reset(vcpu);
  5331. return 0;
  5332. fail_free_mce_banks:
  5333. kfree(vcpu->arch.mce_banks);
  5334. fail_free_lapic:
  5335. kvm_free_lapic(vcpu);
  5336. fail_mmu_destroy:
  5337. kvm_mmu_destroy(vcpu);
  5338. fail_free_pio_data:
  5339. free_page((unsigned long)vcpu->arch.pio_data);
  5340. fail:
  5341. return r;
  5342. }
  5343. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  5344. {
  5345. int idx;
  5346. kfree(vcpu->arch.mce_banks);
  5347. kvm_free_lapic(vcpu);
  5348. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5349. kvm_mmu_destroy(vcpu);
  5350. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5351. free_page((unsigned long)vcpu->arch.pio_data);
  5352. }
  5353. int kvm_arch_init_vm(struct kvm *kvm)
  5354. {
  5355. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  5356. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  5357. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  5358. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  5359. raw_spin_lock_init(&kvm->arch.tsc_write_lock);
  5360. return 0;
  5361. }
  5362. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  5363. {
  5364. vcpu_load(vcpu);
  5365. kvm_mmu_unload(vcpu);
  5366. vcpu_put(vcpu);
  5367. }
  5368. static void kvm_free_vcpus(struct kvm *kvm)
  5369. {
  5370. unsigned int i;
  5371. struct kvm_vcpu *vcpu;
  5372. /*
  5373. * Unpin any mmu pages first.
  5374. */
  5375. kvm_for_each_vcpu(i, vcpu, kvm) {
  5376. kvm_clear_async_pf_completion_queue(vcpu);
  5377. kvm_unload_vcpu_mmu(vcpu);
  5378. }
  5379. kvm_for_each_vcpu(i, vcpu, kvm)
  5380. kvm_arch_vcpu_free(vcpu);
  5381. mutex_lock(&kvm->lock);
  5382. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  5383. kvm->vcpus[i] = NULL;
  5384. atomic_set(&kvm->online_vcpus, 0);
  5385. mutex_unlock(&kvm->lock);
  5386. }
  5387. void kvm_arch_sync_events(struct kvm *kvm)
  5388. {
  5389. kvm_free_all_assigned_devices(kvm);
  5390. kvm_free_pit(kvm);
  5391. }
  5392. void kvm_arch_destroy_vm(struct kvm *kvm)
  5393. {
  5394. kvm_iommu_unmap_guest(kvm);
  5395. kfree(kvm->arch.vpic);
  5396. kfree(kvm->arch.vioapic);
  5397. kvm_free_vcpus(kvm);
  5398. if (kvm->arch.apic_access_page)
  5399. put_page(kvm->arch.apic_access_page);
  5400. if (kvm->arch.ept_identity_pagetable)
  5401. put_page(kvm->arch.ept_identity_pagetable);
  5402. }
  5403. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  5404. struct kvm_memory_slot *memslot,
  5405. struct kvm_memory_slot old,
  5406. struct kvm_userspace_memory_region *mem,
  5407. int user_alloc)
  5408. {
  5409. int npages = memslot->npages;
  5410. int map_flags = MAP_PRIVATE | MAP_ANONYMOUS;
  5411. /* Prevent internal slot pages from being moved by fork()/COW. */
  5412. if (memslot->id >= KVM_MEMORY_SLOTS)
  5413. map_flags = MAP_SHARED | MAP_ANONYMOUS;
  5414. /*To keep backward compatibility with older userspace,
  5415. *x86 needs to hanlde !user_alloc case.
  5416. */
  5417. if (!user_alloc) {
  5418. if (npages && !old.rmap) {
  5419. unsigned long userspace_addr;
  5420. down_write(&current->mm->mmap_sem);
  5421. userspace_addr = do_mmap(NULL, 0,
  5422. npages * PAGE_SIZE,
  5423. PROT_READ | PROT_WRITE,
  5424. map_flags,
  5425. 0);
  5426. up_write(&current->mm->mmap_sem);
  5427. if (IS_ERR((void *)userspace_addr))
  5428. return PTR_ERR((void *)userspace_addr);
  5429. memslot->userspace_addr = userspace_addr;
  5430. }
  5431. }
  5432. return 0;
  5433. }
  5434. void kvm_arch_commit_memory_region(struct kvm *kvm,
  5435. struct kvm_userspace_memory_region *mem,
  5436. struct kvm_memory_slot old,
  5437. int user_alloc)
  5438. {
  5439. int nr_mmu_pages = 0, npages = mem->memory_size >> PAGE_SHIFT;
  5440. if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
  5441. int ret;
  5442. down_write(&current->mm->mmap_sem);
  5443. ret = do_munmap(current->mm, old.userspace_addr,
  5444. old.npages * PAGE_SIZE);
  5445. up_write(&current->mm->mmap_sem);
  5446. if (ret < 0)
  5447. printk(KERN_WARNING
  5448. "kvm_vm_ioctl_set_memory_region: "
  5449. "failed to munmap memory\n");
  5450. }
  5451. if (!kvm->arch.n_requested_mmu_pages)
  5452. nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  5453. spin_lock(&kvm->mmu_lock);
  5454. if (nr_mmu_pages)
  5455. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  5456. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  5457. spin_unlock(&kvm->mmu_lock);
  5458. }
  5459. void kvm_arch_flush_shadow(struct kvm *kvm)
  5460. {
  5461. kvm_mmu_zap_all(kvm);
  5462. kvm_reload_remote_mmus(kvm);
  5463. }
  5464. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  5465. {
  5466. return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  5467. !vcpu->arch.apf.halted)
  5468. || !list_empty_careful(&vcpu->async_pf.done)
  5469. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
  5470. || vcpu->arch.nmi_pending ||
  5471. (kvm_arch_interrupt_allowed(vcpu) &&
  5472. kvm_cpu_has_interrupt(vcpu));
  5473. }
  5474. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  5475. {
  5476. int me;
  5477. int cpu = vcpu->cpu;
  5478. if (waitqueue_active(&vcpu->wq)) {
  5479. wake_up_interruptible(&vcpu->wq);
  5480. ++vcpu->stat.halt_wakeup;
  5481. }
  5482. me = get_cpu();
  5483. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  5484. if (kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE)
  5485. smp_send_reschedule(cpu);
  5486. put_cpu();
  5487. }
  5488. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  5489. {
  5490. return kvm_x86_ops->interrupt_allowed(vcpu);
  5491. }
  5492. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
  5493. {
  5494. unsigned long current_rip = kvm_rip_read(vcpu) +
  5495. get_segment_base(vcpu, VCPU_SREG_CS);
  5496. return current_rip == linear_rip;
  5497. }
  5498. EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
  5499. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
  5500. {
  5501. unsigned long rflags;
  5502. rflags = kvm_x86_ops->get_rflags(vcpu);
  5503. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5504. rflags &= ~X86_EFLAGS_TF;
  5505. return rflags;
  5506. }
  5507. EXPORT_SYMBOL_GPL(kvm_get_rflags);
  5508. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  5509. {
  5510. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
  5511. kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
  5512. rflags |= X86_EFLAGS_TF;
  5513. kvm_x86_ops->set_rflags(vcpu, rflags);
  5514. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5515. }
  5516. EXPORT_SYMBOL_GPL(kvm_set_rflags);
  5517. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
  5518. {
  5519. int r;
  5520. if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
  5521. is_error_page(work->page))
  5522. return;
  5523. r = kvm_mmu_reload(vcpu);
  5524. if (unlikely(r))
  5525. return;
  5526. if (!vcpu->arch.mmu.direct_map &&
  5527. work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
  5528. return;
  5529. vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
  5530. }
  5531. static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
  5532. {
  5533. return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
  5534. }
  5535. static inline u32 kvm_async_pf_next_probe(u32 key)
  5536. {
  5537. return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
  5538. }
  5539. static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  5540. {
  5541. u32 key = kvm_async_pf_hash_fn(gfn);
  5542. while (vcpu->arch.apf.gfns[key] != ~0)
  5543. key = kvm_async_pf_next_probe(key);
  5544. vcpu->arch.apf.gfns[key] = gfn;
  5545. }
  5546. static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
  5547. {
  5548. int i;
  5549. u32 key = kvm_async_pf_hash_fn(gfn);
  5550. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
  5551. (vcpu->arch.apf.gfns[key] != gfn &&
  5552. vcpu->arch.apf.gfns[key] != ~0); i++)
  5553. key = kvm_async_pf_next_probe(key);
  5554. return key;
  5555. }
  5556. bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  5557. {
  5558. return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
  5559. }
  5560. static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  5561. {
  5562. u32 i, j, k;
  5563. i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
  5564. while (true) {
  5565. vcpu->arch.apf.gfns[i] = ~0;
  5566. do {
  5567. j = kvm_async_pf_next_probe(j);
  5568. if (vcpu->arch.apf.gfns[j] == ~0)
  5569. return;
  5570. k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
  5571. /*
  5572. * k lies cyclically in ]i,j]
  5573. * | i.k.j |
  5574. * |....j i.k.| or |.k..j i...|
  5575. */
  5576. } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
  5577. vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
  5578. i = j;
  5579. }
  5580. }
  5581. static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
  5582. {
  5583. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
  5584. sizeof(val));
  5585. }
  5586. void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  5587. struct kvm_async_pf *work)
  5588. {
  5589. struct x86_exception fault;
  5590. trace_kvm_async_pf_not_present(work->arch.token, work->gva);
  5591. kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
  5592. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
  5593. (vcpu->arch.apf.send_user_only &&
  5594. kvm_x86_ops->get_cpl(vcpu) == 0))
  5595. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  5596. else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
  5597. fault.vector = PF_VECTOR;
  5598. fault.error_code_valid = true;
  5599. fault.error_code = 0;
  5600. fault.nested_page_fault = false;
  5601. fault.address = work->arch.token;
  5602. kvm_inject_page_fault(vcpu, &fault);
  5603. }
  5604. }
  5605. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  5606. struct kvm_async_pf *work)
  5607. {
  5608. struct x86_exception fault;
  5609. trace_kvm_async_pf_ready(work->arch.token, work->gva);
  5610. if (is_error_page(work->page))
  5611. work->arch.token = ~0; /* broadcast wakeup */
  5612. else
  5613. kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
  5614. if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
  5615. !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
  5616. fault.vector = PF_VECTOR;
  5617. fault.error_code_valid = true;
  5618. fault.error_code = 0;
  5619. fault.nested_page_fault = false;
  5620. fault.address = work->arch.token;
  5621. kvm_inject_page_fault(vcpu, &fault);
  5622. }
  5623. vcpu->arch.apf.halted = false;
  5624. }
  5625. bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
  5626. {
  5627. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
  5628. return true;
  5629. else
  5630. return !kvm_event_needs_reinjection(vcpu) &&
  5631. kvm_x86_ops->interrupt_allowed(vcpu);
  5632. }
  5633. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  5634. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  5635. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  5636. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  5637. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
  5638. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
  5639. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
  5640. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
  5641. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
  5642. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
  5643. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
  5644. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);