x86.c 157 KB

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