x86.c 164 KB

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