x86.c 178 KB

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