x86.c 165 KB

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