x86.c 165 KB

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