x86.c 164 KB

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