x86.c 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634
  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 "assigned-dev.h"
  30. #include "pmu.h"
  31. #include "hyperv.h"
  32. #include <linux/clocksource.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/kvm.h>
  35. #include <linux/fs.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/export.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/mman.h>
  40. #include <linux/highmem.h>
  41. #include <linux/iommu.h>
  42. #include <linux/intel-iommu.h>
  43. #include <linux/cpufreq.h>
  44. #include <linux/user-return-notifier.h>
  45. #include <linux/srcu.h>
  46. #include <linux/slab.h>
  47. #include <linux/perf_event.h>
  48. #include <linux/uaccess.h>
  49. #include <linux/hash.h>
  50. #include <linux/pci.h>
  51. #include <linux/timekeeper_internal.h>
  52. #include <linux/pvclock_gtod.h>
  53. #include <linux/kvm_irqfd.h>
  54. #include <linux/irqbypass.h>
  55. #include <trace/events/kvm.h>
  56. #include <asm/debugreg.h>
  57. #include <asm/msr.h>
  58. #include <asm/desc.h>
  59. #include <asm/mce.h>
  60. #include <linux/kernel_stat.h>
  61. #include <asm/fpu/internal.h> /* Ugh! */
  62. #include <asm/pvclock.h>
  63. #include <asm/div64.h>
  64. #include <asm/irq_remapping.h>
  65. #define CREATE_TRACE_POINTS
  66. #include "trace.h"
  67. #define MAX_IO_MSRS 256
  68. #define KVM_MAX_MCE_BANKS 32
  69. u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
  70. EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
  71. #define emul_to_vcpu(ctxt) \
  72. container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
  73. /* EFER defaults:
  74. * - enable syscall per default because its emulated by KVM
  75. * - enable LME and LMA per default on 64 bit KVM
  76. */
  77. #ifdef CONFIG_X86_64
  78. static
  79. u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
  80. #else
  81. static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
  82. #endif
  83. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  84. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  85. #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
  86. KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
  87. static void update_cr8_intercept(struct kvm_vcpu *vcpu);
  88. static void process_nmi(struct kvm_vcpu *vcpu);
  89. static void enter_smm(struct kvm_vcpu *vcpu);
  90. static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
  91. struct kvm_x86_ops *kvm_x86_ops __read_mostly;
  92. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  93. static bool __read_mostly ignore_msrs = 0;
  94. module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
  95. unsigned int min_timer_period_us = 500;
  96. module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
  97. static bool __read_mostly kvmclock_periodic_sync = true;
  98. module_param(kvmclock_periodic_sync, bool, S_IRUGO);
  99. bool __read_mostly kvm_has_tsc_control;
  100. EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
  101. u32 __read_mostly kvm_max_guest_tsc_khz;
  102. EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
  103. u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
  104. EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
  105. u64 __read_mostly kvm_max_tsc_scaling_ratio;
  106. EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
  107. u64 __read_mostly kvm_default_tsc_scaling_ratio;
  108. EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
  109. /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
  110. static u32 __read_mostly tsc_tolerance_ppm = 250;
  111. module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
  112. /* lapic timer advance (tscdeadline mode only) in nanoseconds */
  113. unsigned int __read_mostly lapic_timer_advance_ns = 0;
  114. module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
  115. static bool __read_mostly vector_hashing = true;
  116. module_param(vector_hashing, bool, S_IRUGO);
  117. static bool __read_mostly backwards_tsc_observed = false;
  118. #define KVM_NR_SHARED_MSRS 16
  119. struct kvm_shared_msrs_global {
  120. int nr;
  121. u32 msrs[KVM_NR_SHARED_MSRS];
  122. };
  123. struct kvm_shared_msrs {
  124. struct user_return_notifier urn;
  125. bool registered;
  126. struct kvm_shared_msr_values {
  127. u64 host;
  128. u64 curr;
  129. } values[KVM_NR_SHARED_MSRS];
  130. };
  131. static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
  132. static struct kvm_shared_msrs __percpu *shared_msrs;
  133. struct kvm_stats_debugfs_item debugfs_entries[] = {
  134. { "pf_fixed", VCPU_STAT(pf_fixed) },
  135. { "pf_guest", VCPU_STAT(pf_guest) },
  136. { "tlb_flush", VCPU_STAT(tlb_flush) },
  137. { "invlpg", VCPU_STAT(invlpg) },
  138. { "exits", VCPU_STAT(exits) },
  139. { "io_exits", VCPU_STAT(io_exits) },
  140. { "mmio_exits", VCPU_STAT(mmio_exits) },
  141. { "signal_exits", VCPU_STAT(signal_exits) },
  142. { "irq_window", VCPU_STAT(irq_window_exits) },
  143. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  144. { "halt_exits", VCPU_STAT(halt_exits) },
  145. { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
  146. { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
  147. { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
  148. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  149. { "hypercalls", VCPU_STAT(hypercalls) },
  150. { "request_irq", VCPU_STAT(request_irq_exits) },
  151. { "irq_exits", VCPU_STAT(irq_exits) },
  152. { "host_state_reload", VCPU_STAT(host_state_reload) },
  153. { "efer_reload", VCPU_STAT(efer_reload) },
  154. { "fpu_reload", VCPU_STAT(fpu_reload) },
  155. { "insn_emulation", VCPU_STAT(insn_emulation) },
  156. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  157. { "irq_injections", VCPU_STAT(irq_injections) },
  158. { "nmi_injections", VCPU_STAT(nmi_injections) },
  159. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  160. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  161. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  162. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  163. { "mmu_flooded", VM_STAT(mmu_flooded) },
  164. { "mmu_recycled", VM_STAT(mmu_recycled) },
  165. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  166. { "mmu_unsync", VM_STAT(mmu_unsync) },
  167. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  168. { "largepages", VM_STAT(lpages) },
  169. { NULL }
  170. };
  171. u64 __read_mostly host_xcr0;
  172. static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
  173. static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
  174. {
  175. int i;
  176. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
  177. vcpu->arch.apf.gfns[i] = ~0;
  178. }
  179. static void kvm_on_user_return(struct user_return_notifier *urn)
  180. {
  181. unsigned slot;
  182. struct kvm_shared_msrs *locals
  183. = container_of(urn, struct kvm_shared_msrs, urn);
  184. struct kvm_shared_msr_values *values;
  185. unsigned long flags;
  186. /*
  187. * Disabling irqs at this point since the following code could be
  188. * interrupted and executed through kvm_arch_hardware_disable()
  189. */
  190. local_irq_save(flags);
  191. if (locals->registered) {
  192. locals->registered = false;
  193. user_return_notifier_unregister(urn);
  194. }
  195. local_irq_restore(flags);
  196. for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
  197. values = &locals->values[slot];
  198. if (values->host != values->curr) {
  199. wrmsrl(shared_msrs_global.msrs[slot], values->host);
  200. values->curr = values->host;
  201. }
  202. }
  203. }
  204. static void shared_msr_update(unsigned slot, u32 msr)
  205. {
  206. u64 value;
  207. unsigned int cpu = smp_processor_id();
  208. struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
  209. /* only read, and nobody should modify it at this time,
  210. * so don't need lock */
  211. if (slot >= shared_msrs_global.nr) {
  212. printk(KERN_ERR "kvm: invalid MSR slot!");
  213. return;
  214. }
  215. rdmsrl_safe(msr, &value);
  216. smsr->values[slot].host = value;
  217. smsr->values[slot].curr = value;
  218. }
  219. void kvm_define_shared_msr(unsigned slot, u32 msr)
  220. {
  221. BUG_ON(slot >= KVM_NR_SHARED_MSRS);
  222. shared_msrs_global.msrs[slot] = msr;
  223. if (slot >= shared_msrs_global.nr)
  224. shared_msrs_global.nr = slot + 1;
  225. }
  226. EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
  227. static void kvm_shared_msr_cpu_online(void)
  228. {
  229. unsigned i;
  230. for (i = 0; i < shared_msrs_global.nr; ++i)
  231. shared_msr_update(i, shared_msrs_global.msrs[i]);
  232. }
  233. int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
  234. {
  235. unsigned int cpu = smp_processor_id();
  236. struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
  237. int err;
  238. if (((value ^ smsr->values[slot].curr) & mask) == 0)
  239. return 0;
  240. smsr->values[slot].curr = value;
  241. err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
  242. if (err)
  243. return 1;
  244. if (!smsr->registered) {
  245. smsr->urn.on_user_return = kvm_on_user_return;
  246. user_return_notifier_register(&smsr->urn);
  247. smsr->registered = true;
  248. }
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
  252. static void drop_user_return_notifiers(void)
  253. {
  254. unsigned int cpu = smp_processor_id();
  255. struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
  256. if (smsr->registered)
  257. kvm_on_user_return(&smsr->urn);
  258. }
  259. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  260. {
  261. return vcpu->arch.apic_base;
  262. }
  263. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  264. int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  265. {
  266. u64 old_state = vcpu->arch.apic_base &
  267. (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
  268. u64 new_state = msr_info->data &
  269. (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
  270. u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
  271. 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
  272. if (!msr_info->host_initiated &&
  273. ((msr_info->data & reserved_bits) != 0 ||
  274. new_state == X2APIC_ENABLE ||
  275. (new_state == MSR_IA32_APICBASE_ENABLE &&
  276. old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
  277. (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
  278. old_state == 0)))
  279. return 1;
  280. kvm_lapic_set_base(vcpu, msr_info->data);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  284. asmlinkage __visible void kvm_spurious_fault(void)
  285. {
  286. /* Fault while not rebooting. We want the trace. */
  287. BUG();
  288. }
  289. EXPORT_SYMBOL_GPL(kvm_spurious_fault);
  290. #define EXCPT_BENIGN 0
  291. #define EXCPT_CONTRIBUTORY 1
  292. #define EXCPT_PF 2
  293. static int exception_class(int vector)
  294. {
  295. switch (vector) {
  296. case PF_VECTOR:
  297. return EXCPT_PF;
  298. case DE_VECTOR:
  299. case TS_VECTOR:
  300. case NP_VECTOR:
  301. case SS_VECTOR:
  302. case GP_VECTOR:
  303. return EXCPT_CONTRIBUTORY;
  304. default:
  305. break;
  306. }
  307. return EXCPT_BENIGN;
  308. }
  309. #define EXCPT_FAULT 0
  310. #define EXCPT_TRAP 1
  311. #define EXCPT_ABORT 2
  312. #define EXCPT_INTERRUPT 3
  313. static int exception_type(int vector)
  314. {
  315. unsigned int mask;
  316. if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
  317. return EXCPT_INTERRUPT;
  318. mask = 1 << vector;
  319. /* #DB is trap, as instruction watchpoints are handled elsewhere */
  320. if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
  321. return EXCPT_TRAP;
  322. if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
  323. return EXCPT_ABORT;
  324. /* Reserved exceptions will result in fault */
  325. return EXCPT_FAULT;
  326. }
  327. static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
  328. unsigned nr, bool has_error, u32 error_code,
  329. bool reinject)
  330. {
  331. u32 prev_nr;
  332. int class1, class2;
  333. kvm_make_request(KVM_REQ_EVENT, vcpu);
  334. if (!vcpu->arch.exception.pending) {
  335. queue:
  336. if (has_error && !is_protmode(vcpu))
  337. has_error = false;
  338. vcpu->arch.exception.pending = true;
  339. vcpu->arch.exception.has_error_code = has_error;
  340. vcpu->arch.exception.nr = nr;
  341. vcpu->arch.exception.error_code = error_code;
  342. vcpu->arch.exception.reinject = reinject;
  343. return;
  344. }
  345. /* to check exception */
  346. prev_nr = vcpu->arch.exception.nr;
  347. if (prev_nr == DF_VECTOR) {
  348. /* triple fault -> shutdown */
  349. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  350. return;
  351. }
  352. class1 = exception_class(prev_nr);
  353. class2 = exception_class(nr);
  354. if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
  355. || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
  356. /* generate double fault per SDM Table 5-5 */
  357. vcpu->arch.exception.pending = true;
  358. vcpu->arch.exception.has_error_code = true;
  359. vcpu->arch.exception.nr = DF_VECTOR;
  360. vcpu->arch.exception.error_code = 0;
  361. } else
  362. /* replace previous exception with a new one in a hope
  363. that instruction re-execution will regenerate lost
  364. exception */
  365. goto queue;
  366. }
  367. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  368. {
  369. kvm_multiple_exception(vcpu, nr, false, 0, false);
  370. }
  371. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  372. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  373. {
  374. kvm_multiple_exception(vcpu, nr, false, 0, true);
  375. }
  376. EXPORT_SYMBOL_GPL(kvm_requeue_exception);
  377. void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
  378. {
  379. if (err)
  380. kvm_inject_gp(vcpu, 0);
  381. else
  382. kvm_x86_ops->skip_emulated_instruction(vcpu);
  383. }
  384. EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
  385. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
  386. {
  387. ++vcpu->stat.pf_guest;
  388. vcpu->arch.cr2 = fault->address;
  389. kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
  390. }
  391. EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
  392. static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
  393. {
  394. if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
  395. vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
  396. else
  397. vcpu->arch.mmu.inject_page_fault(vcpu, fault);
  398. return fault->nested_page_fault;
  399. }
  400. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  401. {
  402. atomic_inc(&vcpu->arch.nmi_queued);
  403. kvm_make_request(KVM_REQ_NMI, vcpu);
  404. }
  405. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  406. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  407. {
  408. kvm_multiple_exception(vcpu, nr, true, error_code, false);
  409. }
  410. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  411. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  412. {
  413. kvm_multiple_exception(vcpu, nr, true, error_code, true);
  414. }
  415. EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
  416. /*
  417. * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
  418. * a #GP and return false.
  419. */
  420. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
  421. {
  422. if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
  423. return true;
  424. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  425. return false;
  426. }
  427. EXPORT_SYMBOL_GPL(kvm_require_cpl);
  428. bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
  429. {
  430. if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  431. return true;
  432. kvm_queue_exception(vcpu, UD_VECTOR);
  433. return false;
  434. }
  435. EXPORT_SYMBOL_GPL(kvm_require_dr);
  436. /*
  437. * This function will be used to read from the physical memory of the currently
  438. * running guest. The difference to kvm_vcpu_read_guest_page is that this function
  439. * can read from guest physical or from the guest's guest physical memory.
  440. */
  441. int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  442. gfn_t ngfn, void *data, int offset, int len,
  443. u32 access)
  444. {
  445. struct x86_exception exception;
  446. gfn_t real_gfn;
  447. gpa_t ngpa;
  448. ngpa = gfn_to_gpa(ngfn);
  449. real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
  450. if (real_gfn == UNMAPPED_GVA)
  451. return -EFAULT;
  452. real_gfn = gpa_to_gfn(real_gfn);
  453. return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
  454. }
  455. EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
  456. static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
  457. void *data, int offset, int len, u32 access)
  458. {
  459. return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
  460. data, offset, len, access);
  461. }
  462. /*
  463. * Load the pae pdptrs. Return true is they are all valid.
  464. */
  465. int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
  466. {
  467. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  468. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  469. int i;
  470. int ret;
  471. u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
  472. ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
  473. offset * sizeof(u64), sizeof(pdpte),
  474. PFERR_USER_MASK|PFERR_WRITE_MASK);
  475. if (ret < 0) {
  476. ret = 0;
  477. goto out;
  478. }
  479. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  480. if ((pdpte[i] & PT_PRESENT_MASK) &&
  481. (pdpte[i] &
  482. vcpu->arch.mmu.guest_rsvd_check.rsvd_bits_mask[0][2])) {
  483. ret = 0;
  484. goto out;
  485. }
  486. }
  487. ret = 1;
  488. memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
  489. __set_bit(VCPU_EXREG_PDPTR,
  490. (unsigned long *)&vcpu->arch.regs_avail);
  491. __set_bit(VCPU_EXREG_PDPTR,
  492. (unsigned long *)&vcpu->arch.regs_dirty);
  493. out:
  494. return ret;
  495. }
  496. EXPORT_SYMBOL_GPL(load_pdptrs);
  497. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  498. {
  499. u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
  500. bool changed = true;
  501. int offset;
  502. gfn_t gfn;
  503. int r;
  504. if (is_long_mode(vcpu) || !is_pae(vcpu))
  505. return false;
  506. if (!test_bit(VCPU_EXREG_PDPTR,
  507. (unsigned long *)&vcpu->arch.regs_avail))
  508. return true;
  509. gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
  510. offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
  511. r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
  512. PFERR_USER_MASK | PFERR_WRITE_MASK);
  513. if (r < 0)
  514. goto out;
  515. changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
  516. out:
  517. return changed;
  518. }
  519. int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  520. {
  521. unsigned long old_cr0 = kvm_read_cr0(vcpu);
  522. unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
  523. cr0 |= X86_CR0_ET;
  524. #ifdef CONFIG_X86_64
  525. if (cr0 & 0xffffffff00000000UL)
  526. return 1;
  527. #endif
  528. cr0 &= ~CR0_RESERVED_BITS;
  529. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
  530. return 1;
  531. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
  532. return 1;
  533. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  534. #ifdef CONFIG_X86_64
  535. if ((vcpu->arch.efer & EFER_LME)) {
  536. int cs_db, cs_l;
  537. if (!is_pae(vcpu))
  538. return 1;
  539. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  540. if (cs_l)
  541. return 1;
  542. } else
  543. #endif
  544. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
  545. kvm_read_cr3(vcpu)))
  546. return 1;
  547. }
  548. if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
  549. return 1;
  550. kvm_x86_ops->set_cr0(vcpu, cr0);
  551. if ((cr0 ^ old_cr0) & X86_CR0_PG) {
  552. kvm_clear_async_pf_completion_queue(vcpu);
  553. kvm_async_pf_hash_reset(vcpu);
  554. }
  555. if ((cr0 ^ old_cr0) & update_bits)
  556. kvm_mmu_reset_context(vcpu);
  557. if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
  558. kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
  559. !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  560. kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
  561. return 0;
  562. }
  563. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  564. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  565. {
  566. (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
  567. }
  568. EXPORT_SYMBOL_GPL(kvm_lmsw);
  569. static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
  570. {
  571. if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
  572. !vcpu->guest_xcr0_loaded) {
  573. /* kvm_set_xcr() also depends on this */
  574. xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
  575. vcpu->guest_xcr0_loaded = 1;
  576. }
  577. }
  578. static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
  579. {
  580. if (vcpu->guest_xcr0_loaded) {
  581. if (vcpu->arch.xcr0 != host_xcr0)
  582. xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
  583. vcpu->guest_xcr0_loaded = 0;
  584. }
  585. }
  586. static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  587. {
  588. u64 xcr0 = xcr;
  589. u64 old_xcr0 = vcpu->arch.xcr0;
  590. u64 valid_bits;
  591. /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
  592. if (index != XCR_XFEATURE_ENABLED_MASK)
  593. return 1;
  594. if (!(xcr0 & XFEATURE_MASK_FP))
  595. return 1;
  596. if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
  597. return 1;
  598. /*
  599. * Do not allow the guest to set bits that we do not support
  600. * saving. However, xcr0 bit 0 is always set, even if the
  601. * emulated CPU does not support XSAVE (see fx_init).
  602. */
  603. valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
  604. if (xcr0 & ~valid_bits)
  605. return 1;
  606. if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
  607. (!(xcr0 & XFEATURE_MASK_BNDCSR)))
  608. return 1;
  609. if (xcr0 & XFEATURE_MASK_AVX512) {
  610. if (!(xcr0 & XFEATURE_MASK_YMM))
  611. return 1;
  612. if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
  613. return 1;
  614. }
  615. vcpu->arch.xcr0 = xcr0;
  616. if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
  617. kvm_update_cpuid(vcpu);
  618. return 0;
  619. }
  620. int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  621. {
  622. if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
  623. __kvm_set_xcr(vcpu, index, xcr)) {
  624. kvm_inject_gp(vcpu, 0);
  625. return 1;
  626. }
  627. return 0;
  628. }
  629. EXPORT_SYMBOL_GPL(kvm_set_xcr);
  630. int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  631. {
  632. unsigned long old_cr4 = kvm_read_cr4(vcpu);
  633. unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
  634. X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
  635. if (cr4 & CR4_RESERVED_BITS)
  636. return 1;
  637. if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
  638. return 1;
  639. if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
  640. return 1;
  641. if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
  642. return 1;
  643. if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
  644. return 1;
  645. if (!guest_cpuid_has_pku(vcpu) && (cr4 & X86_CR4_PKE))
  646. return 1;
  647. if (is_long_mode(vcpu)) {
  648. if (!(cr4 & X86_CR4_PAE))
  649. return 1;
  650. } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
  651. && ((cr4 ^ old_cr4) & pdptr_bits)
  652. && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
  653. kvm_read_cr3(vcpu)))
  654. return 1;
  655. if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
  656. if (!guest_cpuid_has_pcid(vcpu))
  657. return 1;
  658. /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
  659. if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_ASID_MASK) ||
  660. !is_long_mode(vcpu))
  661. return 1;
  662. }
  663. if (kvm_x86_ops->set_cr4(vcpu, cr4))
  664. return 1;
  665. if (((cr4 ^ old_cr4) & pdptr_bits) ||
  666. (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
  667. kvm_mmu_reset_context(vcpu);
  668. if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
  669. kvm_update_cpuid(vcpu);
  670. return 0;
  671. }
  672. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  673. int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  674. {
  675. #ifdef CONFIG_X86_64
  676. cr3 &= ~CR3_PCID_INVD;
  677. #endif
  678. if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
  679. kvm_mmu_sync_roots(vcpu);
  680. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  681. return 0;
  682. }
  683. if (is_long_mode(vcpu)) {
  684. if (cr3 & CR3_L_MODE_RESERVED_BITS)
  685. return 1;
  686. } else if (is_pae(vcpu) && is_paging(vcpu) &&
  687. !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
  688. return 1;
  689. vcpu->arch.cr3 = cr3;
  690. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  691. kvm_mmu_new_cr3(vcpu);
  692. return 0;
  693. }
  694. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  695. int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  696. {
  697. if (cr8 & CR8_RESERVED_BITS)
  698. return 1;
  699. if (lapic_in_kernel(vcpu))
  700. kvm_lapic_set_tpr(vcpu, cr8);
  701. else
  702. vcpu->arch.cr8 = cr8;
  703. return 0;
  704. }
  705. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  706. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  707. {
  708. if (lapic_in_kernel(vcpu))
  709. return kvm_lapic_get_cr8(vcpu);
  710. else
  711. return vcpu->arch.cr8;
  712. }
  713. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  714. static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
  715. {
  716. int i;
  717. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
  718. for (i = 0; i < KVM_NR_DB_REGS; i++)
  719. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  720. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
  721. }
  722. }
  723. static void kvm_update_dr6(struct kvm_vcpu *vcpu)
  724. {
  725. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  726. kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
  727. }
  728. static void kvm_update_dr7(struct kvm_vcpu *vcpu)
  729. {
  730. unsigned long dr7;
  731. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  732. dr7 = vcpu->arch.guest_debug_dr7;
  733. else
  734. dr7 = vcpu->arch.dr7;
  735. kvm_x86_ops->set_dr7(vcpu, dr7);
  736. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
  737. if (dr7 & DR7_BP_EN_MASK)
  738. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
  739. }
  740. static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
  741. {
  742. u64 fixed = DR6_FIXED_1;
  743. if (!guest_cpuid_has_rtm(vcpu))
  744. fixed |= DR6_RTM;
  745. return fixed;
  746. }
  747. static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  748. {
  749. switch (dr) {
  750. case 0 ... 3:
  751. vcpu->arch.db[dr] = val;
  752. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  753. vcpu->arch.eff_db[dr] = val;
  754. break;
  755. case 4:
  756. /* fall through */
  757. case 6:
  758. if (val & 0xffffffff00000000ULL)
  759. return -1; /* #GP */
  760. vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
  761. kvm_update_dr6(vcpu);
  762. break;
  763. case 5:
  764. /* fall through */
  765. default: /* 7 */
  766. if (val & 0xffffffff00000000ULL)
  767. return -1; /* #GP */
  768. vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
  769. kvm_update_dr7(vcpu);
  770. break;
  771. }
  772. return 0;
  773. }
  774. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  775. {
  776. if (__kvm_set_dr(vcpu, dr, val)) {
  777. kvm_inject_gp(vcpu, 0);
  778. return 1;
  779. }
  780. return 0;
  781. }
  782. EXPORT_SYMBOL_GPL(kvm_set_dr);
  783. int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
  784. {
  785. switch (dr) {
  786. case 0 ... 3:
  787. *val = vcpu->arch.db[dr];
  788. break;
  789. case 4:
  790. /* fall through */
  791. case 6:
  792. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  793. *val = vcpu->arch.dr6;
  794. else
  795. *val = kvm_x86_ops->get_dr6(vcpu);
  796. break;
  797. case 5:
  798. /* fall through */
  799. default: /* 7 */
  800. *val = vcpu->arch.dr7;
  801. break;
  802. }
  803. return 0;
  804. }
  805. EXPORT_SYMBOL_GPL(kvm_get_dr);
  806. bool kvm_rdpmc(struct kvm_vcpu *vcpu)
  807. {
  808. u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  809. u64 data;
  810. int err;
  811. err = kvm_pmu_rdpmc(vcpu, ecx, &data);
  812. if (err)
  813. return err;
  814. kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
  815. kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
  816. return err;
  817. }
  818. EXPORT_SYMBOL_GPL(kvm_rdpmc);
  819. /*
  820. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  821. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  822. *
  823. * This list is modified at module load time to reflect the
  824. * capabilities of the host cpu. This capabilities test skips MSRs that are
  825. * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
  826. * may depend on host virtualization features rather than host cpu features.
  827. */
  828. static u32 msrs_to_save[] = {
  829. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  830. MSR_STAR,
  831. #ifdef CONFIG_X86_64
  832. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  833. #endif
  834. MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
  835. MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
  836. MSR_IA32_SPEC_CTRL, MSR_IA32_ARCH_CAPABILITIES
  837. };
  838. static unsigned num_msrs_to_save;
  839. static u32 emulated_msrs[] = {
  840. MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  841. MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
  842. HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
  843. HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
  844. HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
  845. HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
  846. HV_X64_MSR_RESET,
  847. HV_X64_MSR_VP_INDEX,
  848. HV_X64_MSR_VP_RUNTIME,
  849. HV_X64_MSR_SCONTROL,
  850. HV_X64_MSR_STIMER0_CONFIG,
  851. HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
  852. MSR_KVM_PV_EOI_EN,
  853. MSR_IA32_TSC_ADJUST,
  854. MSR_IA32_TSCDEADLINE,
  855. MSR_IA32_MISC_ENABLE,
  856. MSR_IA32_MCG_STATUS,
  857. MSR_IA32_MCG_CTL,
  858. MSR_IA32_MCG_EXT_CTL,
  859. MSR_IA32_SMBASE,
  860. MSR_AMD64_VIRT_SPEC_CTRL,
  861. };
  862. static unsigned num_emulated_msrs;
  863. bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
  864. {
  865. if (efer & efer_reserved_bits)
  866. return false;
  867. if (efer & EFER_FFXSR) {
  868. struct kvm_cpuid_entry2 *feat;
  869. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  870. if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
  871. return false;
  872. }
  873. if (efer & EFER_SVME) {
  874. struct kvm_cpuid_entry2 *feat;
  875. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  876. if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
  877. return false;
  878. }
  879. return true;
  880. }
  881. EXPORT_SYMBOL_GPL(kvm_valid_efer);
  882. static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
  883. {
  884. u64 old_efer = vcpu->arch.efer;
  885. if (!kvm_valid_efer(vcpu, efer))
  886. return 1;
  887. if (is_paging(vcpu)
  888. && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
  889. return 1;
  890. efer &= ~EFER_LMA;
  891. efer |= vcpu->arch.efer & EFER_LMA;
  892. kvm_x86_ops->set_efer(vcpu, efer);
  893. /* Update reserved bits */
  894. if ((efer ^ old_efer) & EFER_NX)
  895. kvm_mmu_reset_context(vcpu);
  896. return 0;
  897. }
  898. void kvm_enable_efer_bits(u64 mask)
  899. {
  900. efer_reserved_bits &= ~mask;
  901. }
  902. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  903. /*
  904. * Writes msr value into into the appropriate "register".
  905. * Returns 0 on success, non-0 otherwise.
  906. * Assumes vcpu_load() was already called.
  907. */
  908. int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  909. {
  910. switch (msr->index) {
  911. case MSR_FS_BASE:
  912. case MSR_GS_BASE:
  913. case MSR_KERNEL_GS_BASE:
  914. case MSR_CSTAR:
  915. case MSR_LSTAR:
  916. if (is_noncanonical_address(msr->data))
  917. return 1;
  918. break;
  919. case MSR_IA32_SYSENTER_EIP:
  920. case MSR_IA32_SYSENTER_ESP:
  921. /*
  922. * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
  923. * non-canonical address is written on Intel but not on
  924. * AMD (which ignores the top 32-bits, because it does
  925. * not implement 64-bit SYSENTER).
  926. *
  927. * 64-bit code should hence be able to write a non-canonical
  928. * value on AMD. Making the address canonical ensures that
  929. * vmentry does not fail on Intel after writing a non-canonical
  930. * value, and that something deterministic happens if the guest
  931. * invokes 64-bit SYSENTER.
  932. */
  933. msr->data = get_canonical(msr->data);
  934. }
  935. return kvm_x86_ops->set_msr(vcpu, msr);
  936. }
  937. EXPORT_SYMBOL_GPL(kvm_set_msr);
  938. /*
  939. * Adapt set_msr() to msr_io()'s calling convention
  940. */
  941. static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  942. {
  943. struct msr_data msr;
  944. int r;
  945. msr.index = index;
  946. msr.host_initiated = true;
  947. r = kvm_get_msr(vcpu, &msr);
  948. if (r)
  949. return r;
  950. *data = msr.data;
  951. return 0;
  952. }
  953. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  954. {
  955. struct msr_data msr;
  956. msr.data = *data;
  957. msr.index = index;
  958. msr.host_initiated = true;
  959. return kvm_set_msr(vcpu, &msr);
  960. }
  961. #ifdef CONFIG_X86_64
  962. struct pvclock_gtod_data {
  963. seqcount_t seq;
  964. struct { /* extract of a clocksource struct */
  965. int vclock_mode;
  966. cycle_t cycle_last;
  967. cycle_t mask;
  968. u32 mult;
  969. u32 shift;
  970. } clock;
  971. u64 boot_ns;
  972. u64 nsec_base;
  973. };
  974. static struct pvclock_gtod_data pvclock_gtod_data;
  975. static void update_pvclock_gtod(struct timekeeper *tk)
  976. {
  977. struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
  978. u64 boot_ns;
  979. boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
  980. write_seqcount_begin(&vdata->seq);
  981. /* copy pvclock gtod data */
  982. vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
  983. vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
  984. vdata->clock.mask = tk->tkr_mono.mask;
  985. vdata->clock.mult = tk->tkr_mono.mult;
  986. vdata->clock.shift = tk->tkr_mono.shift;
  987. vdata->boot_ns = boot_ns;
  988. vdata->nsec_base = tk->tkr_mono.xtime_nsec;
  989. write_seqcount_end(&vdata->seq);
  990. }
  991. #endif
  992. void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
  993. {
  994. /*
  995. * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
  996. * vcpu_enter_guest. This function is only called from
  997. * the physical CPU that is running vcpu.
  998. */
  999. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1000. }
  1001. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  1002. {
  1003. int version;
  1004. int r;
  1005. struct pvclock_wall_clock wc;
  1006. struct timespec64 boot;
  1007. if (!wall_clock)
  1008. return;
  1009. r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
  1010. if (r)
  1011. return;
  1012. if (version & 1)
  1013. ++version; /* first time write, random junk */
  1014. ++version;
  1015. if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
  1016. return;
  1017. /*
  1018. * The guest calculates current wall clock time by adding
  1019. * system time (updated by kvm_guest_time_update below) to the
  1020. * wall clock specified here. guest system time equals host
  1021. * system time for us, thus we must fill in host boot time here.
  1022. */
  1023. getboottime64(&boot);
  1024. if (kvm->arch.kvmclock_offset) {
  1025. struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
  1026. boot = timespec64_sub(boot, ts);
  1027. }
  1028. wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
  1029. wc.nsec = boot.tv_nsec;
  1030. wc.version = version;
  1031. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  1032. version++;
  1033. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  1034. }
  1035. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  1036. {
  1037. do_shl32_div32(dividend, divisor);
  1038. return dividend;
  1039. }
  1040. static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
  1041. s8 *pshift, u32 *pmultiplier)
  1042. {
  1043. uint64_t scaled64;
  1044. int32_t shift = 0;
  1045. uint64_t tps64;
  1046. uint32_t tps32;
  1047. tps64 = base_hz;
  1048. scaled64 = scaled_hz;
  1049. while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
  1050. tps64 >>= 1;
  1051. shift--;
  1052. }
  1053. tps32 = (uint32_t)tps64;
  1054. while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
  1055. if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
  1056. scaled64 >>= 1;
  1057. else
  1058. tps32 <<= 1;
  1059. shift++;
  1060. }
  1061. *pshift = shift;
  1062. *pmultiplier = div_frac(scaled64, tps32);
  1063. pr_debug("%s: base_hz %llu => %llu, shift %d, mul %u\n",
  1064. __func__, base_hz, scaled_hz, shift, *pmultiplier);
  1065. }
  1066. #ifdef CONFIG_X86_64
  1067. static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
  1068. #endif
  1069. static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
  1070. static unsigned long max_tsc_khz;
  1071. static u32 adjust_tsc_khz(u32 khz, s32 ppm)
  1072. {
  1073. u64 v = (u64)khz * (1000000 + ppm);
  1074. do_div(v, 1000000);
  1075. return v;
  1076. }
  1077. static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
  1078. {
  1079. u64 ratio;
  1080. /* Guest TSC same frequency as host TSC? */
  1081. if (!scale) {
  1082. vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
  1083. return 0;
  1084. }
  1085. /* TSC scaling supported? */
  1086. if (!kvm_has_tsc_control) {
  1087. if (user_tsc_khz > tsc_khz) {
  1088. vcpu->arch.tsc_catchup = 1;
  1089. vcpu->arch.tsc_always_catchup = 1;
  1090. return 0;
  1091. } else {
  1092. WARN(1, "user requested TSC rate below hardware speed\n");
  1093. return -1;
  1094. }
  1095. }
  1096. /* TSC scaling required - calculate ratio */
  1097. ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
  1098. user_tsc_khz, tsc_khz);
  1099. if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
  1100. WARN_ONCE(1, "Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
  1101. user_tsc_khz);
  1102. return -1;
  1103. }
  1104. vcpu->arch.tsc_scaling_ratio = ratio;
  1105. return 0;
  1106. }
  1107. static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
  1108. {
  1109. u32 thresh_lo, thresh_hi;
  1110. int use_scaling = 0;
  1111. /* tsc_khz can be zero if TSC calibration fails */
  1112. if (user_tsc_khz == 0) {
  1113. /* set tsc_scaling_ratio to a safe value */
  1114. vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
  1115. return -1;
  1116. }
  1117. /* Compute a scale to convert nanoseconds in TSC cycles */
  1118. kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
  1119. &vcpu->arch.virtual_tsc_shift,
  1120. &vcpu->arch.virtual_tsc_mult);
  1121. vcpu->arch.virtual_tsc_khz = user_tsc_khz;
  1122. /*
  1123. * Compute the variation in TSC rate which is acceptable
  1124. * within the range of tolerance and decide if the
  1125. * rate being applied is within that bounds of the hardware
  1126. * rate. If so, no scaling or compensation need be done.
  1127. */
  1128. thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
  1129. thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
  1130. if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
  1131. pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi);
  1132. use_scaling = 1;
  1133. }
  1134. return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
  1135. }
  1136. static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
  1137. {
  1138. u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
  1139. vcpu->arch.virtual_tsc_mult,
  1140. vcpu->arch.virtual_tsc_shift);
  1141. tsc += vcpu->arch.this_tsc_write;
  1142. return tsc;
  1143. }
  1144. static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
  1145. {
  1146. #ifdef CONFIG_X86_64
  1147. bool vcpus_matched;
  1148. struct kvm_arch *ka = &vcpu->kvm->arch;
  1149. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  1150. vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
  1151. atomic_read(&vcpu->kvm->online_vcpus));
  1152. /*
  1153. * Once the masterclock is enabled, always perform request in
  1154. * order to update it.
  1155. *
  1156. * In order to enable masterclock, the host clocksource must be TSC
  1157. * and the vcpus need to have matched TSCs. When that happens,
  1158. * perform request to enable masterclock.
  1159. */
  1160. if (ka->use_master_clock ||
  1161. (gtod->clock.vclock_mode == VCLOCK_TSC && vcpus_matched))
  1162. kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
  1163. trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
  1164. atomic_read(&vcpu->kvm->online_vcpus),
  1165. ka->use_master_clock, gtod->clock.vclock_mode);
  1166. #endif
  1167. }
  1168. static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
  1169. {
  1170. u64 curr_offset = vcpu->arch.tsc_offset;
  1171. vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
  1172. }
  1173. /*
  1174. * Multiply tsc by a fixed point number represented by ratio.
  1175. *
  1176. * The most significant 64-N bits (mult) of ratio represent the
  1177. * integral part of the fixed point number; the remaining N bits
  1178. * (frac) represent the fractional part, ie. ratio represents a fixed
  1179. * point number (mult + frac * 2^(-N)).
  1180. *
  1181. * N equals to kvm_tsc_scaling_ratio_frac_bits.
  1182. */
  1183. static inline u64 __scale_tsc(u64 ratio, u64 tsc)
  1184. {
  1185. return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
  1186. }
  1187. u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
  1188. {
  1189. u64 _tsc = tsc;
  1190. u64 ratio = vcpu->arch.tsc_scaling_ratio;
  1191. if (ratio != kvm_default_tsc_scaling_ratio)
  1192. _tsc = __scale_tsc(ratio, tsc);
  1193. return _tsc;
  1194. }
  1195. EXPORT_SYMBOL_GPL(kvm_scale_tsc);
  1196. static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1197. {
  1198. u64 tsc;
  1199. tsc = kvm_scale_tsc(vcpu, rdtsc());
  1200. return target_tsc - tsc;
  1201. }
  1202. u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
  1203. {
  1204. return vcpu->arch.tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
  1205. }
  1206. EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
  1207. static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1208. {
  1209. kvm_x86_ops->write_tsc_offset(vcpu, offset);
  1210. vcpu->arch.tsc_offset = offset;
  1211. }
  1212. void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
  1213. {
  1214. struct kvm *kvm = vcpu->kvm;
  1215. u64 offset, ns, elapsed;
  1216. unsigned long flags;
  1217. s64 usdiff;
  1218. bool matched;
  1219. bool already_matched;
  1220. u64 data = msr->data;
  1221. raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
  1222. offset = kvm_compute_tsc_offset(vcpu, data);
  1223. ns = ktime_get_boot_ns();
  1224. elapsed = ns - kvm->arch.last_tsc_nsec;
  1225. if (vcpu->arch.virtual_tsc_khz) {
  1226. int faulted = 0;
  1227. /* n.b - signed multiplication and division required */
  1228. usdiff = data - kvm->arch.last_tsc_write;
  1229. #ifdef CONFIG_X86_64
  1230. usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
  1231. #else
  1232. /* do_div() only does unsigned */
  1233. asm("1: idivl %[divisor]\n"
  1234. "2: xor %%edx, %%edx\n"
  1235. " movl $0, %[faulted]\n"
  1236. "3:\n"
  1237. ".section .fixup,\"ax\"\n"
  1238. "4: movl $1, %[faulted]\n"
  1239. " jmp 3b\n"
  1240. ".previous\n"
  1241. _ASM_EXTABLE(1b, 4b)
  1242. : "=A"(usdiff), [faulted] "=r" (faulted)
  1243. : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
  1244. #endif
  1245. do_div(elapsed, 1000);
  1246. usdiff -= elapsed;
  1247. if (usdiff < 0)
  1248. usdiff = -usdiff;
  1249. /* idivl overflow => difference is larger than USEC_PER_SEC */
  1250. if (faulted)
  1251. usdiff = USEC_PER_SEC;
  1252. } else
  1253. usdiff = USEC_PER_SEC; /* disable TSC match window below */
  1254. /*
  1255. * Special case: TSC write with a small delta (1 second) of virtual
  1256. * cycle time against real time is interpreted as an attempt to
  1257. * synchronize the CPU.
  1258. *
  1259. * For a reliable TSC, we can match TSC offsets, and for an unstable
  1260. * TSC, we add elapsed time in this computation. We could let the
  1261. * compensation code attempt to catch up if we fall behind, but
  1262. * it's better to try to match offsets from the beginning.
  1263. */
  1264. if (usdiff < USEC_PER_SEC &&
  1265. vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
  1266. if (!check_tsc_unstable()) {
  1267. offset = kvm->arch.cur_tsc_offset;
  1268. pr_debug("kvm: matched tsc offset for %llu\n", data);
  1269. } else {
  1270. u64 delta = nsec_to_cycles(vcpu, elapsed);
  1271. data += delta;
  1272. offset = kvm_compute_tsc_offset(vcpu, data);
  1273. pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
  1274. }
  1275. matched = true;
  1276. already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
  1277. } else {
  1278. /*
  1279. * We split periods of matched TSC writes into generations.
  1280. * For each generation, we track the original measured
  1281. * nanosecond time, offset, and write, so if TSCs are in
  1282. * sync, we can match exact offset, and if not, we can match
  1283. * exact software computation in compute_guest_tsc()
  1284. *
  1285. * These values are tracked in kvm->arch.cur_xxx variables.
  1286. */
  1287. kvm->arch.cur_tsc_generation++;
  1288. kvm->arch.cur_tsc_nsec = ns;
  1289. kvm->arch.cur_tsc_write = data;
  1290. kvm->arch.cur_tsc_offset = offset;
  1291. matched = false;
  1292. pr_debug("kvm: new tsc generation %llu, clock %llu\n",
  1293. kvm->arch.cur_tsc_generation, data);
  1294. }
  1295. /*
  1296. * We also track th most recent recorded KHZ, write and time to
  1297. * allow the matching interval to be extended at each write.
  1298. */
  1299. kvm->arch.last_tsc_nsec = ns;
  1300. kvm->arch.last_tsc_write = data;
  1301. kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
  1302. vcpu->arch.last_guest_tsc = data;
  1303. /* Keep track of which generation this VCPU has synchronized to */
  1304. vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
  1305. vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
  1306. vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
  1307. if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
  1308. update_ia32_tsc_adjust_msr(vcpu, offset);
  1309. kvm_vcpu_write_tsc_offset(vcpu, offset);
  1310. raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
  1311. spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
  1312. if (!matched) {
  1313. kvm->arch.nr_vcpus_matched_tsc = 0;
  1314. } else if (!already_matched) {
  1315. kvm->arch.nr_vcpus_matched_tsc++;
  1316. }
  1317. kvm_track_tsc_matching(vcpu);
  1318. spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
  1319. }
  1320. EXPORT_SYMBOL_GPL(kvm_write_tsc);
  1321. static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
  1322. s64 adjustment)
  1323. {
  1324. kvm_vcpu_write_tsc_offset(vcpu, vcpu->arch.tsc_offset + adjustment);
  1325. }
  1326. static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
  1327. {
  1328. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
  1329. WARN_ON(adjustment < 0);
  1330. adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
  1331. adjust_tsc_offset_guest(vcpu, adjustment);
  1332. }
  1333. #ifdef CONFIG_X86_64
  1334. static cycle_t read_tsc(void)
  1335. {
  1336. cycle_t ret = (cycle_t)rdtsc_ordered();
  1337. u64 last = pvclock_gtod_data.clock.cycle_last;
  1338. if (likely(ret >= last))
  1339. return ret;
  1340. /*
  1341. * GCC likes to generate cmov here, but this branch is extremely
  1342. * predictable (it's just a function of time and the likely is
  1343. * very likely) and there's a data dependence, so force GCC
  1344. * to generate a branch instead. I don't barrier() because
  1345. * we don't actually need a barrier, and if this function
  1346. * ever gets inlined it will generate worse code.
  1347. */
  1348. asm volatile ("");
  1349. return last;
  1350. }
  1351. static inline u64 vgettsc(cycle_t *cycle_now)
  1352. {
  1353. long v;
  1354. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  1355. *cycle_now = read_tsc();
  1356. v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
  1357. return v * gtod->clock.mult;
  1358. }
  1359. static int do_monotonic_boot(s64 *t, cycle_t *cycle_now)
  1360. {
  1361. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  1362. unsigned long seq;
  1363. int mode;
  1364. u64 ns;
  1365. do {
  1366. seq = read_seqcount_begin(&gtod->seq);
  1367. mode = gtod->clock.vclock_mode;
  1368. ns = gtod->nsec_base;
  1369. ns += vgettsc(cycle_now);
  1370. ns >>= gtod->clock.shift;
  1371. ns += gtod->boot_ns;
  1372. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  1373. *t = ns;
  1374. return mode;
  1375. }
  1376. /* returns true if host is using tsc clocksource */
  1377. static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
  1378. {
  1379. /* checked again under seqlock below */
  1380. if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
  1381. return false;
  1382. return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
  1383. }
  1384. #endif
  1385. /*
  1386. *
  1387. * Assuming a stable TSC across physical CPUS, and a stable TSC
  1388. * across virtual CPUs, the following condition is possible.
  1389. * Each numbered line represents an event visible to both
  1390. * CPUs at the next numbered event.
  1391. *
  1392. * "timespecX" represents host monotonic time. "tscX" represents
  1393. * RDTSC value.
  1394. *
  1395. * VCPU0 on CPU0 | VCPU1 on CPU1
  1396. *
  1397. * 1. read timespec0,tsc0
  1398. * 2. | timespec1 = timespec0 + N
  1399. * | tsc1 = tsc0 + M
  1400. * 3. transition to guest | transition to guest
  1401. * 4. ret0 = timespec0 + (rdtsc - tsc0) |
  1402. * 5. | ret1 = timespec1 + (rdtsc - tsc1)
  1403. * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
  1404. *
  1405. * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
  1406. *
  1407. * - ret0 < ret1
  1408. * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
  1409. * ...
  1410. * - 0 < N - M => M < N
  1411. *
  1412. * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
  1413. * always the case (the difference between two distinct xtime instances
  1414. * might be smaller then the difference between corresponding TSC reads,
  1415. * when updating guest vcpus pvclock areas).
  1416. *
  1417. * To avoid that problem, do not allow visibility of distinct
  1418. * system_timestamp/tsc_timestamp values simultaneously: use a master
  1419. * copy of host monotonic time values. Update that master copy
  1420. * in lockstep.
  1421. *
  1422. * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
  1423. *
  1424. */
  1425. static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
  1426. {
  1427. #ifdef CONFIG_X86_64
  1428. struct kvm_arch *ka = &kvm->arch;
  1429. int vclock_mode;
  1430. bool host_tsc_clocksource, vcpus_matched;
  1431. vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
  1432. atomic_read(&kvm->online_vcpus));
  1433. /*
  1434. * If the host uses TSC clock, then passthrough TSC as stable
  1435. * to the guest.
  1436. */
  1437. host_tsc_clocksource = kvm_get_time_and_clockread(
  1438. &ka->master_kernel_ns,
  1439. &ka->master_cycle_now);
  1440. ka->use_master_clock = host_tsc_clocksource && vcpus_matched
  1441. && !backwards_tsc_observed
  1442. && !ka->boot_vcpu_runs_old_kvmclock;
  1443. if (ka->use_master_clock)
  1444. atomic_set(&kvm_guest_has_master_clock, 1);
  1445. vclock_mode = pvclock_gtod_data.clock.vclock_mode;
  1446. trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
  1447. vcpus_matched);
  1448. #endif
  1449. }
  1450. void kvm_make_mclock_inprogress_request(struct kvm *kvm)
  1451. {
  1452. kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
  1453. }
  1454. static void kvm_gen_update_masterclock(struct kvm *kvm)
  1455. {
  1456. #ifdef CONFIG_X86_64
  1457. int i;
  1458. struct kvm_vcpu *vcpu;
  1459. struct kvm_arch *ka = &kvm->arch;
  1460. spin_lock(&ka->pvclock_gtod_sync_lock);
  1461. kvm_make_mclock_inprogress_request(kvm);
  1462. /* no guest entries from this point */
  1463. pvclock_update_vm_gtod_copy(kvm);
  1464. kvm_for_each_vcpu(i, vcpu, kvm)
  1465. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1466. /* guest entries allowed */
  1467. kvm_for_each_vcpu(i, vcpu, kvm)
  1468. clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
  1469. spin_unlock(&ka->pvclock_gtod_sync_lock);
  1470. #endif
  1471. }
  1472. static u64 __get_kvmclock_ns(struct kvm *kvm)
  1473. {
  1474. struct kvm_arch *ka = &kvm->arch;
  1475. struct pvclock_vcpu_time_info hv_clock;
  1476. u64 ret;
  1477. spin_lock(&ka->pvclock_gtod_sync_lock);
  1478. if (!ka->use_master_clock) {
  1479. spin_unlock(&ka->pvclock_gtod_sync_lock);
  1480. return ktime_get_boot_ns() + ka->kvmclock_offset;
  1481. }
  1482. hv_clock.tsc_timestamp = ka->master_cycle_now;
  1483. hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
  1484. spin_unlock(&ka->pvclock_gtod_sync_lock);
  1485. /* both __this_cpu_read() and rdtsc() should be on the same cpu */
  1486. get_cpu();
  1487. if (__this_cpu_read(cpu_tsc_khz)) {
  1488. kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
  1489. &hv_clock.tsc_shift,
  1490. &hv_clock.tsc_to_system_mul);
  1491. ret = __pvclock_read_cycles(&hv_clock, rdtsc());
  1492. } else
  1493. ret = ktime_get_boot_ns() + ka->kvmclock_offset;
  1494. put_cpu();
  1495. return ret;
  1496. }
  1497. u64 get_kvmclock_ns(struct kvm *kvm)
  1498. {
  1499. unsigned long flags;
  1500. s64 ns;
  1501. local_irq_save(flags);
  1502. ns = __get_kvmclock_ns(kvm);
  1503. local_irq_restore(flags);
  1504. return ns;
  1505. }
  1506. static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
  1507. {
  1508. struct kvm_vcpu_arch *vcpu = &v->arch;
  1509. struct pvclock_vcpu_time_info guest_hv_clock;
  1510. if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
  1511. &guest_hv_clock, sizeof(guest_hv_clock))))
  1512. return;
  1513. /* This VCPU is paused, but it's legal for a guest to read another
  1514. * VCPU's kvmclock, so we really have to follow the specification where
  1515. * it says that version is odd if data is being modified, and even after
  1516. * it is consistent.
  1517. *
  1518. * Version field updates must be kept separate. This is because
  1519. * kvm_write_guest_cached might use a "rep movs" instruction, and
  1520. * writes within a string instruction are weakly ordered. So there
  1521. * are three writes overall.
  1522. *
  1523. * As a small optimization, only write the version field in the first
  1524. * and third write. The vcpu->pv_time cache is still valid, because the
  1525. * version field is the first in the struct.
  1526. */
  1527. BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
  1528. if (guest_hv_clock.version & 1)
  1529. ++guest_hv_clock.version; /* first time write, random junk */
  1530. vcpu->hv_clock.version = guest_hv_clock.version + 1;
  1531. kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
  1532. &vcpu->hv_clock,
  1533. sizeof(vcpu->hv_clock.version));
  1534. smp_wmb();
  1535. /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
  1536. vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
  1537. if (vcpu->pvclock_set_guest_stopped_request) {
  1538. vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
  1539. vcpu->pvclock_set_guest_stopped_request = false;
  1540. }
  1541. trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
  1542. kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
  1543. &vcpu->hv_clock,
  1544. sizeof(vcpu->hv_clock));
  1545. smp_wmb();
  1546. vcpu->hv_clock.version++;
  1547. kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
  1548. &vcpu->hv_clock,
  1549. sizeof(vcpu->hv_clock.version));
  1550. }
  1551. static int kvm_guest_time_update(struct kvm_vcpu *v)
  1552. {
  1553. unsigned long flags, tgt_tsc_khz;
  1554. struct kvm_vcpu_arch *vcpu = &v->arch;
  1555. struct kvm_arch *ka = &v->kvm->arch;
  1556. s64 kernel_ns;
  1557. u64 tsc_timestamp, host_tsc;
  1558. u8 pvclock_flags;
  1559. bool use_master_clock;
  1560. kernel_ns = 0;
  1561. host_tsc = 0;
  1562. /*
  1563. * If the host uses TSC clock, then passthrough TSC as stable
  1564. * to the guest.
  1565. */
  1566. spin_lock(&ka->pvclock_gtod_sync_lock);
  1567. use_master_clock = ka->use_master_clock;
  1568. if (use_master_clock) {
  1569. host_tsc = ka->master_cycle_now;
  1570. kernel_ns = ka->master_kernel_ns;
  1571. }
  1572. spin_unlock(&ka->pvclock_gtod_sync_lock);
  1573. /* Keep irq disabled to prevent changes to the clock */
  1574. local_irq_save(flags);
  1575. tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
  1576. if (unlikely(tgt_tsc_khz == 0)) {
  1577. local_irq_restore(flags);
  1578. kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
  1579. return 1;
  1580. }
  1581. if (!use_master_clock) {
  1582. host_tsc = rdtsc();
  1583. kernel_ns = ktime_get_boot_ns();
  1584. }
  1585. tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
  1586. /*
  1587. * We may have to catch up the TSC to match elapsed wall clock
  1588. * time for two reasons, even if kvmclock is used.
  1589. * 1) CPU could have been running below the maximum TSC rate
  1590. * 2) Broken TSC compensation resets the base at each VCPU
  1591. * entry to avoid unknown leaps of TSC even when running
  1592. * again on the same CPU. This may cause apparent elapsed
  1593. * time to disappear, and the guest to stand still or run
  1594. * very slowly.
  1595. */
  1596. if (vcpu->tsc_catchup) {
  1597. u64 tsc = compute_guest_tsc(v, kernel_ns);
  1598. if (tsc > tsc_timestamp) {
  1599. adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
  1600. tsc_timestamp = tsc;
  1601. }
  1602. }
  1603. local_irq_restore(flags);
  1604. /* With all the info we got, fill in the values */
  1605. if (kvm_has_tsc_control)
  1606. tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
  1607. if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
  1608. kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
  1609. &vcpu->hv_clock.tsc_shift,
  1610. &vcpu->hv_clock.tsc_to_system_mul);
  1611. vcpu->hw_tsc_khz = tgt_tsc_khz;
  1612. }
  1613. vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
  1614. vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
  1615. vcpu->last_guest_tsc = tsc_timestamp;
  1616. /* If the host uses TSC clocksource, then it is stable */
  1617. pvclock_flags = 0;
  1618. if (use_master_clock)
  1619. pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
  1620. vcpu->hv_clock.flags = pvclock_flags;
  1621. if (vcpu->pv_time_enabled)
  1622. kvm_setup_pvclock_page(v);
  1623. if (v == kvm_get_vcpu(v->kvm, 0))
  1624. kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
  1625. return 0;
  1626. }
  1627. /*
  1628. * kvmclock updates which are isolated to a given vcpu, such as
  1629. * vcpu->cpu migration, should not allow system_timestamp from
  1630. * the rest of the vcpus to remain static. Otherwise ntp frequency
  1631. * correction applies to one vcpu's system_timestamp but not
  1632. * the others.
  1633. *
  1634. * So in those cases, request a kvmclock update for all vcpus.
  1635. * We need to rate-limit these requests though, as they can
  1636. * considerably slow guests that have a large number of vcpus.
  1637. * The time for a remote vcpu to update its kvmclock is bound
  1638. * by the delay we use to rate-limit the updates.
  1639. */
  1640. #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
  1641. static void kvmclock_update_fn(struct work_struct *work)
  1642. {
  1643. int i;
  1644. struct delayed_work *dwork = to_delayed_work(work);
  1645. struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
  1646. kvmclock_update_work);
  1647. struct kvm *kvm = container_of(ka, struct kvm, arch);
  1648. struct kvm_vcpu *vcpu;
  1649. kvm_for_each_vcpu(i, vcpu, kvm) {
  1650. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1651. kvm_vcpu_kick(vcpu);
  1652. }
  1653. }
  1654. static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
  1655. {
  1656. struct kvm *kvm = v->kvm;
  1657. kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
  1658. schedule_delayed_work(&kvm->arch.kvmclock_update_work,
  1659. KVMCLOCK_UPDATE_DELAY);
  1660. }
  1661. #define KVMCLOCK_SYNC_PERIOD (300 * HZ)
  1662. static void kvmclock_sync_fn(struct work_struct *work)
  1663. {
  1664. struct delayed_work *dwork = to_delayed_work(work);
  1665. struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
  1666. kvmclock_sync_work);
  1667. struct kvm *kvm = container_of(ka, struct kvm, arch);
  1668. if (!kvmclock_periodic_sync)
  1669. return;
  1670. schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
  1671. schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
  1672. KVMCLOCK_SYNC_PERIOD);
  1673. }
  1674. static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1675. {
  1676. u64 mcg_cap = vcpu->arch.mcg_cap;
  1677. unsigned bank_num = mcg_cap & 0xff;
  1678. switch (msr) {
  1679. case MSR_IA32_MCG_STATUS:
  1680. vcpu->arch.mcg_status = data;
  1681. break;
  1682. case MSR_IA32_MCG_CTL:
  1683. if (!(mcg_cap & MCG_CTL_P))
  1684. return 1;
  1685. if (data != 0 && data != ~(u64)0)
  1686. return -1;
  1687. vcpu->arch.mcg_ctl = data;
  1688. break;
  1689. default:
  1690. if (msr >= MSR_IA32_MC0_CTL &&
  1691. msr < MSR_IA32_MCx_CTL(bank_num)) {
  1692. u32 offset = msr - MSR_IA32_MC0_CTL;
  1693. /* only 0 or all 1s can be written to IA32_MCi_CTL
  1694. * some Linux kernels though clear bit 10 in bank 4 to
  1695. * workaround a BIOS/GART TBL issue on AMD K8s, ignore
  1696. * this to avoid an uncatched #GP in the guest
  1697. */
  1698. if ((offset & 0x3) == 0 &&
  1699. data != 0 && (data | (1 << 10)) != ~(u64)0)
  1700. return -1;
  1701. vcpu->arch.mce_banks[offset] = data;
  1702. break;
  1703. }
  1704. return 1;
  1705. }
  1706. return 0;
  1707. }
  1708. static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
  1709. {
  1710. struct kvm *kvm = vcpu->kvm;
  1711. int lm = is_long_mode(vcpu);
  1712. u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
  1713. : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
  1714. u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
  1715. : kvm->arch.xen_hvm_config.blob_size_32;
  1716. u32 page_num = data & ~PAGE_MASK;
  1717. u64 page_addr = data & PAGE_MASK;
  1718. u8 *page;
  1719. int r;
  1720. r = -E2BIG;
  1721. if (page_num >= blob_size)
  1722. goto out;
  1723. r = -ENOMEM;
  1724. page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
  1725. if (IS_ERR(page)) {
  1726. r = PTR_ERR(page);
  1727. goto out;
  1728. }
  1729. if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
  1730. goto out_free;
  1731. r = 0;
  1732. out_free:
  1733. kfree(page);
  1734. out:
  1735. return r;
  1736. }
  1737. static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
  1738. {
  1739. gpa_t gpa = data & ~0x3f;
  1740. /* Bits 2:5 are reserved, Should be zero */
  1741. if (data & 0x3c)
  1742. return 1;
  1743. vcpu->arch.apf.msr_val = data;
  1744. if (!(data & KVM_ASYNC_PF_ENABLED)) {
  1745. kvm_clear_async_pf_completion_queue(vcpu);
  1746. kvm_async_pf_hash_reset(vcpu);
  1747. return 0;
  1748. }
  1749. if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
  1750. sizeof(u32)))
  1751. return 1;
  1752. vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
  1753. kvm_async_pf_wakeup_all(vcpu);
  1754. return 0;
  1755. }
  1756. static void kvmclock_reset(struct kvm_vcpu *vcpu)
  1757. {
  1758. vcpu->arch.pv_time_enabled = false;
  1759. }
  1760. static void record_steal_time(struct kvm_vcpu *vcpu)
  1761. {
  1762. if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
  1763. return;
  1764. if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
  1765. &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
  1766. return;
  1767. if (vcpu->arch.st.steal.version & 1)
  1768. vcpu->arch.st.steal.version += 1; /* first time write, random junk */
  1769. vcpu->arch.st.steal.version += 1;
  1770. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
  1771. &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
  1772. smp_wmb();
  1773. vcpu->arch.st.steal.steal += current->sched_info.run_delay -
  1774. vcpu->arch.st.last_steal;
  1775. vcpu->arch.st.last_steal = current->sched_info.run_delay;
  1776. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
  1777. &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
  1778. smp_wmb();
  1779. vcpu->arch.st.steal.version += 1;
  1780. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
  1781. &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
  1782. }
  1783. int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  1784. {
  1785. bool pr = false;
  1786. u32 msr = msr_info->index;
  1787. u64 data = msr_info->data;
  1788. switch (msr) {
  1789. case MSR_AMD64_NB_CFG:
  1790. case MSR_IA32_UCODE_REV:
  1791. case MSR_IA32_UCODE_WRITE:
  1792. case MSR_VM_HSAVE_PA:
  1793. case MSR_AMD64_PATCH_LOADER:
  1794. case MSR_AMD64_BU_CFG2:
  1795. break;
  1796. case MSR_EFER:
  1797. return set_efer(vcpu, data);
  1798. case MSR_K7_HWCR:
  1799. data &= ~(u64)0x40; /* ignore flush filter disable */
  1800. data &= ~(u64)0x100; /* ignore ignne emulation enable */
  1801. data &= ~(u64)0x8; /* ignore TLB cache disable */
  1802. data &= ~(u64)0x40000; /* ignore Mc status write enable */
  1803. if (data != 0) {
  1804. vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  1805. data);
  1806. return 1;
  1807. }
  1808. break;
  1809. case MSR_FAM10H_MMIO_CONF_BASE:
  1810. if (data != 0) {
  1811. vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  1812. "0x%llx\n", data);
  1813. return 1;
  1814. }
  1815. break;
  1816. case MSR_IA32_DEBUGCTLMSR:
  1817. if (!data) {
  1818. /* We support the non-activated case already */
  1819. break;
  1820. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  1821. /* Values other than LBR and BTF are vendor-specific,
  1822. thus reserved and should throw a #GP */
  1823. return 1;
  1824. }
  1825. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  1826. __func__, data);
  1827. break;
  1828. case 0x200 ... 0x2ff:
  1829. return kvm_mtrr_set_msr(vcpu, msr, data);
  1830. case MSR_IA32_APICBASE:
  1831. return kvm_set_apic_base(vcpu, msr_info);
  1832. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1833. return kvm_x2apic_msr_write(vcpu, msr, data);
  1834. case MSR_IA32_TSCDEADLINE:
  1835. kvm_set_lapic_tscdeadline_msr(vcpu, data);
  1836. break;
  1837. case MSR_IA32_TSC_ADJUST:
  1838. if (guest_cpuid_has_tsc_adjust(vcpu)) {
  1839. if (!msr_info->host_initiated) {
  1840. s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
  1841. adjust_tsc_offset_guest(vcpu, adj);
  1842. }
  1843. vcpu->arch.ia32_tsc_adjust_msr = data;
  1844. }
  1845. break;
  1846. case MSR_IA32_MISC_ENABLE:
  1847. vcpu->arch.ia32_misc_enable_msr = data;
  1848. break;
  1849. case MSR_IA32_SMBASE:
  1850. if (!msr_info->host_initiated)
  1851. return 1;
  1852. vcpu->arch.smbase = data;
  1853. break;
  1854. case MSR_KVM_WALL_CLOCK_NEW:
  1855. case MSR_KVM_WALL_CLOCK:
  1856. vcpu->kvm->arch.wall_clock = data;
  1857. kvm_write_wall_clock(vcpu->kvm, data);
  1858. break;
  1859. case MSR_KVM_SYSTEM_TIME_NEW:
  1860. case MSR_KVM_SYSTEM_TIME: {
  1861. u64 gpa_offset;
  1862. struct kvm_arch *ka = &vcpu->kvm->arch;
  1863. kvmclock_reset(vcpu);
  1864. if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
  1865. bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
  1866. if (ka->boot_vcpu_runs_old_kvmclock != tmp)
  1867. set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
  1868. &vcpu->requests);
  1869. ka->boot_vcpu_runs_old_kvmclock = tmp;
  1870. }
  1871. vcpu->arch.time = data;
  1872. kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
  1873. /* we verify if the enable bit is set... */
  1874. if (!(data & 1))
  1875. break;
  1876. gpa_offset = data & ~(PAGE_MASK | 1);
  1877. if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
  1878. &vcpu->arch.pv_time, data & ~1ULL,
  1879. sizeof(struct pvclock_vcpu_time_info)))
  1880. vcpu->arch.pv_time_enabled = false;
  1881. else
  1882. vcpu->arch.pv_time_enabled = true;
  1883. break;
  1884. }
  1885. case MSR_KVM_ASYNC_PF_EN:
  1886. if (kvm_pv_enable_async_pf(vcpu, data))
  1887. return 1;
  1888. break;
  1889. case MSR_KVM_STEAL_TIME:
  1890. if (unlikely(!sched_info_on()))
  1891. return 1;
  1892. if (data & KVM_STEAL_RESERVED_MASK)
  1893. return 1;
  1894. if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
  1895. data & KVM_STEAL_VALID_BITS,
  1896. sizeof(struct kvm_steal_time)))
  1897. return 1;
  1898. vcpu->arch.st.msr_val = data;
  1899. if (!(data & KVM_MSR_ENABLED))
  1900. break;
  1901. kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
  1902. break;
  1903. case MSR_KVM_PV_EOI_EN:
  1904. if (kvm_lapic_enable_pv_eoi(vcpu, data))
  1905. return 1;
  1906. break;
  1907. case MSR_IA32_MCG_CTL:
  1908. case MSR_IA32_MCG_STATUS:
  1909. case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
  1910. return set_msr_mce(vcpu, msr, data);
  1911. case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
  1912. case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
  1913. pr = true; /* fall through */
  1914. case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
  1915. case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
  1916. if (kvm_pmu_is_valid_msr(vcpu, msr))
  1917. return kvm_pmu_set_msr(vcpu, msr_info);
  1918. if (pr || data != 0)
  1919. vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
  1920. "0x%x data 0x%llx\n", msr, data);
  1921. break;
  1922. case MSR_K7_CLK_CTL:
  1923. /*
  1924. * Ignore all writes to this no longer documented MSR.
  1925. * Writes are only relevant for old K7 processors,
  1926. * all pre-dating SVM, but a recommended workaround from
  1927. * AMD for these chips. It is possible to specify the
  1928. * affected processor models on the command line, hence
  1929. * the need to ignore the workaround.
  1930. */
  1931. break;
  1932. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1933. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  1934. case HV_X64_MSR_CRASH_CTL:
  1935. case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
  1936. return kvm_hv_set_msr_common(vcpu, msr, data,
  1937. msr_info->host_initiated);
  1938. case MSR_IA32_BBL_CR_CTL3:
  1939. /* Drop writes to this legacy MSR -- see rdmsr
  1940. * counterpart for further detail.
  1941. */
  1942. vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", msr, data);
  1943. break;
  1944. case MSR_AMD64_OSVW_ID_LENGTH:
  1945. if (!guest_cpuid_has_osvw(vcpu))
  1946. return 1;
  1947. vcpu->arch.osvw.length = data;
  1948. break;
  1949. case MSR_AMD64_OSVW_STATUS:
  1950. if (!guest_cpuid_has_osvw(vcpu))
  1951. return 1;
  1952. vcpu->arch.osvw.status = data;
  1953. break;
  1954. default:
  1955. if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
  1956. return xen_hvm_config(vcpu, data);
  1957. if (kvm_pmu_is_valid_msr(vcpu, msr))
  1958. return kvm_pmu_set_msr(vcpu, msr_info);
  1959. if (!ignore_msrs) {
  1960. vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
  1961. msr, data);
  1962. return 1;
  1963. } else {
  1964. vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
  1965. msr, data);
  1966. break;
  1967. }
  1968. }
  1969. return 0;
  1970. }
  1971. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1972. /*
  1973. * Reads an msr value (of 'msr_index') into 'pdata'.
  1974. * Returns 0 on success, non-0 otherwise.
  1975. * Assumes vcpu_load() was already called.
  1976. */
  1977. int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  1978. {
  1979. return kvm_x86_ops->get_msr(vcpu, msr);
  1980. }
  1981. EXPORT_SYMBOL_GPL(kvm_get_msr);
  1982. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1983. {
  1984. u64 data;
  1985. u64 mcg_cap = vcpu->arch.mcg_cap;
  1986. unsigned bank_num = mcg_cap & 0xff;
  1987. switch (msr) {
  1988. case MSR_IA32_P5_MC_ADDR:
  1989. case MSR_IA32_P5_MC_TYPE:
  1990. data = 0;
  1991. break;
  1992. case MSR_IA32_MCG_CAP:
  1993. data = vcpu->arch.mcg_cap;
  1994. break;
  1995. case MSR_IA32_MCG_CTL:
  1996. if (!(mcg_cap & MCG_CTL_P))
  1997. return 1;
  1998. data = vcpu->arch.mcg_ctl;
  1999. break;
  2000. case MSR_IA32_MCG_STATUS:
  2001. data = vcpu->arch.mcg_status;
  2002. break;
  2003. default:
  2004. if (msr >= MSR_IA32_MC0_CTL &&
  2005. msr < MSR_IA32_MCx_CTL(bank_num)) {
  2006. u32 offset = msr - MSR_IA32_MC0_CTL;
  2007. data = vcpu->arch.mce_banks[offset];
  2008. break;
  2009. }
  2010. return 1;
  2011. }
  2012. *pdata = data;
  2013. return 0;
  2014. }
  2015. int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2016. {
  2017. switch (msr_info->index) {
  2018. case MSR_IA32_PLATFORM_ID:
  2019. case MSR_IA32_EBL_CR_POWERON:
  2020. case MSR_IA32_DEBUGCTLMSR:
  2021. case MSR_IA32_LASTBRANCHFROMIP:
  2022. case MSR_IA32_LASTBRANCHTOIP:
  2023. case MSR_IA32_LASTINTFROMIP:
  2024. case MSR_IA32_LASTINTTOIP:
  2025. case MSR_K8_SYSCFG:
  2026. case MSR_K8_TSEG_ADDR:
  2027. case MSR_K8_TSEG_MASK:
  2028. case MSR_K7_HWCR:
  2029. case MSR_VM_HSAVE_PA:
  2030. case MSR_K8_INT_PENDING_MSG:
  2031. case MSR_AMD64_NB_CFG:
  2032. case MSR_FAM10H_MMIO_CONF_BASE:
  2033. case MSR_AMD64_BU_CFG2:
  2034. case MSR_IA32_PERF_CTL:
  2035. msr_info->data = 0;
  2036. break;
  2037. case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
  2038. case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
  2039. case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
  2040. case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
  2041. if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
  2042. return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
  2043. msr_info->data = 0;
  2044. break;
  2045. case MSR_IA32_UCODE_REV:
  2046. msr_info->data = 0x100000000ULL;
  2047. break;
  2048. case MSR_MTRRcap:
  2049. case 0x200 ... 0x2ff:
  2050. return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
  2051. case 0xcd: /* fsb frequency */
  2052. msr_info->data = 3;
  2053. break;
  2054. /*
  2055. * MSR_EBC_FREQUENCY_ID
  2056. * Conservative value valid for even the basic CPU models.
  2057. * Models 0,1: 000 in bits 23:21 indicating a bus speed of
  2058. * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
  2059. * and 266MHz for model 3, or 4. Set Core Clock
  2060. * Frequency to System Bus Frequency Ratio to 1 (bits
  2061. * 31:24) even though these are only valid for CPU
  2062. * models > 2, however guests may end up dividing or
  2063. * multiplying by zero otherwise.
  2064. */
  2065. case MSR_EBC_FREQUENCY_ID:
  2066. msr_info->data = 1 << 24;
  2067. break;
  2068. case MSR_IA32_APICBASE:
  2069. msr_info->data = kvm_get_apic_base(vcpu);
  2070. break;
  2071. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  2072. return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
  2073. break;
  2074. case MSR_IA32_TSCDEADLINE:
  2075. msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
  2076. break;
  2077. case MSR_IA32_TSC_ADJUST:
  2078. msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
  2079. break;
  2080. case MSR_IA32_MISC_ENABLE:
  2081. msr_info->data = vcpu->arch.ia32_misc_enable_msr;
  2082. break;
  2083. case MSR_IA32_SMBASE:
  2084. if (!msr_info->host_initiated)
  2085. return 1;
  2086. msr_info->data = vcpu->arch.smbase;
  2087. break;
  2088. case MSR_IA32_PERF_STATUS:
  2089. /* TSC increment by tick */
  2090. msr_info->data = 1000ULL;
  2091. /* CPU multiplier */
  2092. msr_info->data |= (((uint64_t)4ULL) << 40);
  2093. break;
  2094. case MSR_EFER:
  2095. msr_info->data = vcpu->arch.efer;
  2096. break;
  2097. case MSR_KVM_WALL_CLOCK:
  2098. case MSR_KVM_WALL_CLOCK_NEW:
  2099. msr_info->data = vcpu->kvm->arch.wall_clock;
  2100. break;
  2101. case MSR_KVM_SYSTEM_TIME:
  2102. case MSR_KVM_SYSTEM_TIME_NEW:
  2103. msr_info->data = vcpu->arch.time;
  2104. break;
  2105. case MSR_KVM_ASYNC_PF_EN:
  2106. msr_info->data = vcpu->arch.apf.msr_val;
  2107. break;
  2108. case MSR_KVM_STEAL_TIME:
  2109. msr_info->data = vcpu->arch.st.msr_val;
  2110. break;
  2111. case MSR_KVM_PV_EOI_EN:
  2112. msr_info->data = vcpu->arch.pv_eoi.msr_val;
  2113. break;
  2114. case MSR_IA32_P5_MC_ADDR:
  2115. case MSR_IA32_P5_MC_TYPE:
  2116. case MSR_IA32_MCG_CAP:
  2117. case MSR_IA32_MCG_CTL:
  2118. case MSR_IA32_MCG_STATUS:
  2119. case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
  2120. return get_msr_mce(vcpu, msr_info->index, &msr_info->data);
  2121. case MSR_K7_CLK_CTL:
  2122. /*
  2123. * Provide expected ramp-up count for K7. All other
  2124. * are set to zero, indicating minimum divisors for
  2125. * every field.
  2126. *
  2127. * This prevents guest kernels on AMD host with CPU
  2128. * type 6, model 8 and higher from exploding due to
  2129. * the rdmsr failing.
  2130. */
  2131. msr_info->data = 0x20000000;
  2132. break;
  2133. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  2134. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  2135. case HV_X64_MSR_CRASH_CTL:
  2136. case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
  2137. return kvm_hv_get_msr_common(vcpu,
  2138. msr_info->index, &msr_info->data);
  2139. break;
  2140. case MSR_IA32_BBL_CR_CTL3:
  2141. /* This legacy MSR exists but isn't fully documented in current
  2142. * silicon. It is however accessed by winxp in very narrow
  2143. * scenarios where it sets bit #19, itself documented as
  2144. * a "reserved" bit. Best effort attempt to source coherent
  2145. * read data here should the balance of the register be
  2146. * interpreted by the guest:
  2147. *
  2148. * L2 cache control register 3: 64GB range, 256KB size,
  2149. * enabled, latency 0x1, configured
  2150. */
  2151. msr_info->data = 0xbe702111;
  2152. break;
  2153. case MSR_AMD64_OSVW_ID_LENGTH:
  2154. if (!guest_cpuid_has_osvw(vcpu))
  2155. return 1;
  2156. msr_info->data = vcpu->arch.osvw.length;
  2157. break;
  2158. case MSR_AMD64_OSVW_STATUS:
  2159. if (!guest_cpuid_has_osvw(vcpu))
  2160. return 1;
  2161. msr_info->data = vcpu->arch.osvw.status;
  2162. break;
  2163. default:
  2164. if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
  2165. return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
  2166. if (!ignore_msrs) {
  2167. vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr_info->index);
  2168. return 1;
  2169. } else {
  2170. vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr_info->index);
  2171. msr_info->data = 0;
  2172. }
  2173. break;
  2174. }
  2175. return 0;
  2176. }
  2177. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  2178. /*
  2179. * Read or write a bunch of msrs. All parameters are kernel addresses.
  2180. *
  2181. * @return number of msrs set successfully.
  2182. */
  2183. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  2184. struct kvm_msr_entry *entries,
  2185. int (*do_msr)(struct kvm_vcpu *vcpu,
  2186. unsigned index, u64 *data))
  2187. {
  2188. int i, idx;
  2189. idx = srcu_read_lock(&vcpu->kvm->srcu);
  2190. for (i = 0; i < msrs->nmsrs; ++i)
  2191. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  2192. break;
  2193. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  2194. return i;
  2195. }
  2196. /*
  2197. * Read or write a bunch of msrs. Parameters are user addresses.
  2198. *
  2199. * @return number of msrs set successfully.
  2200. */
  2201. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  2202. int (*do_msr)(struct kvm_vcpu *vcpu,
  2203. unsigned index, u64 *data),
  2204. int writeback)
  2205. {
  2206. struct kvm_msrs msrs;
  2207. struct kvm_msr_entry *entries;
  2208. int r, n;
  2209. unsigned size;
  2210. r = -EFAULT;
  2211. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  2212. goto out;
  2213. r = -E2BIG;
  2214. if (msrs.nmsrs >= MAX_IO_MSRS)
  2215. goto out;
  2216. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  2217. entries = memdup_user(user_msrs->entries, size);
  2218. if (IS_ERR(entries)) {
  2219. r = PTR_ERR(entries);
  2220. goto out;
  2221. }
  2222. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  2223. if (r < 0)
  2224. goto out_free;
  2225. r = -EFAULT;
  2226. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  2227. goto out_free;
  2228. r = n;
  2229. out_free:
  2230. kfree(entries);
  2231. out:
  2232. return r;
  2233. }
  2234. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  2235. {
  2236. int r;
  2237. switch (ext) {
  2238. case KVM_CAP_IRQCHIP:
  2239. case KVM_CAP_HLT:
  2240. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  2241. case KVM_CAP_SET_TSS_ADDR:
  2242. case KVM_CAP_EXT_CPUID:
  2243. case KVM_CAP_EXT_EMUL_CPUID:
  2244. case KVM_CAP_CLOCKSOURCE:
  2245. case KVM_CAP_PIT:
  2246. case KVM_CAP_NOP_IO_DELAY:
  2247. case KVM_CAP_MP_STATE:
  2248. case KVM_CAP_SYNC_MMU:
  2249. case KVM_CAP_USER_NMI:
  2250. case KVM_CAP_REINJECT_CONTROL:
  2251. case KVM_CAP_IRQ_INJECT_STATUS:
  2252. case KVM_CAP_IOEVENTFD:
  2253. case KVM_CAP_IOEVENTFD_NO_LENGTH:
  2254. case KVM_CAP_PIT2:
  2255. case KVM_CAP_PIT_STATE2:
  2256. case KVM_CAP_SET_IDENTITY_MAP_ADDR:
  2257. case KVM_CAP_XEN_HVM:
  2258. case KVM_CAP_VCPU_EVENTS:
  2259. case KVM_CAP_HYPERV:
  2260. case KVM_CAP_HYPERV_VAPIC:
  2261. case KVM_CAP_HYPERV_SPIN:
  2262. case KVM_CAP_HYPERV_SYNIC:
  2263. case KVM_CAP_PCI_SEGMENT:
  2264. case KVM_CAP_DEBUGREGS:
  2265. case KVM_CAP_X86_ROBUST_SINGLESTEP:
  2266. case KVM_CAP_XSAVE:
  2267. case KVM_CAP_ASYNC_PF:
  2268. case KVM_CAP_GET_TSC_KHZ:
  2269. case KVM_CAP_KVMCLOCK_CTRL:
  2270. case KVM_CAP_READONLY_MEM:
  2271. case KVM_CAP_HYPERV_TIME:
  2272. case KVM_CAP_IOAPIC_POLARITY_IGNORED:
  2273. case KVM_CAP_TSC_DEADLINE_TIMER:
  2274. case KVM_CAP_ENABLE_CAP_VM:
  2275. case KVM_CAP_DISABLE_QUIRKS:
  2276. case KVM_CAP_SET_BOOT_CPU_ID:
  2277. case KVM_CAP_SPLIT_IRQCHIP:
  2278. #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
  2279. case KVM_CAP_ASSIGN_DEV_IRQ:
  2280. case KVM_CAP_PCI_2_3:
  2281. #endif
  2282. r = 1;
  2283. break;
  2284. case KVM_CAP_ADJUST_CLOCK:
  2285. r = KVM_CLOCK_TSC_STABLE;
  2286. break;
  2287. case KVM_CAP_X86_SMM:
  2288. /* SMBASE is usually relocated above 1M on modern chipsets,
  2289. * and SMM handlers might indeed rely on 4G segment limits,
  2290. * so do not report SMM to be available if real mode is
  2291. * emulated via vm86 mode. Still, do not go to great lengths
  2292. * to avoid userspace's usage of the feature, because it is a
  2293. * fringe case that is not enabled except via specific settings
  2294. * of the module parameters.
  2295. */
  2296. r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
  2297. break;
  2298. case KVM_CAP_COALESCED_MMIO:
  2299. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  2300. break;
  2301. case KVM_CAP_VAPIC:
  2302. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  2303. break;
  2304. case KVM_CAP_NR_VCPUS:
  2305. r = KVM_SOFT_MAX_VCPUS;
  2306. break;
  2307. case KVM_CAP_MAX_VCPUS:
  2308. r = KVM_MAX_VCPUS;
  2309. break;
  2310. case KVM_CAP_NR_MEMSLOTS:
  2311. r = KVM_USER_MEM_SLOTS;
  2312. break;
  2313. case KVM_CAP_PV_MMU: /* obsolete */
  2314. r = 0;
  2315. break;
  2316. #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
  2317. case KVM_CAP_IOMMU:
  2318. r = iommu_present(&pci_bus_type);
  2319. break;
  2320. #endif
  2321. case KVM_CAP_MCE:
  2322. r = KVM_MAX_MCE_BANKS;
  2323. break;
  2324. case KVM_CAP_XCRS:
  2325. r = boot_cpu_has(X86_FEATURE_XSAVE);
  2326. break;
  2327. case KVM_CAP_TSC_CONTROL:
  2328. r = kvm_has_tsc_control;
  2329. break;
  2330. case KVM_CAP_X2APIC_API:
  2331. r = KVM_X2APIC_API_VALID_FLAGS;
  2332. break;
  2333. default:
  2334. r = 0;
  2335. break;
  2336. }
  2337. return r;
  2338. }
  2339. long kvm_arch_dev_ioctl(struct file *filp,
  2340. unsigned int ioctl, unsigned long arg)
  2341. {
  2342. void __user *argp = (void __user *)arg;
  2343. long r;
  2344. switch (ioctl) {
  2345. case KVM_GET_MSR_INDEX_LIST: {
  2346. struct kvm_msr_list __user *user_msr_list = argp;
  2347. struct kvm_msr_list msr_list;
  2348. unsigned n;
  2349. r = -EFAULT;
  2350. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2351. goto out;
  2352. n = msr_list.nmsrs;
  2353. msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
  2354. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2355. goto out;
  2356. r = -E2BIG;
  2357. if (n < msr_list.nmsrs)
  2358. goto out;
  2359. r = -EFAULT;
  2360. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2361. num_msrs_to_save * sizeof(u32)))
  2362. goto out;
  2363. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  2364. &emulated_msrs,
  2365. num_emulated_msrs * sizeof(u32)))
  2366. goto out;
  2367. r = 0;
  2368. break;
  2369. }
  2370. case KVM_GET_SUPPORTED_CPUID:
  2371. case KVM_GET_EMULATED_CPUID: {
  2372. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2373. struct kvm_cpuid2 cpuid;
  2374. r = -EFAULT;
  2375. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2376. goto out;
  2377. r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
  2378. ioctl);
  2379. if (r)
  2380. goto out;
  2381. r = -EFAULT;
  2382. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2383. goto out;
  2384. r = 0;
  2385. break;
  2386. }
  2387. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  2388. r = -EFAULT;
  2389. if (copy_to_user(argp, &kvm_mce_cap_supported,
  2390. sizeof(kvm_mce_cap_supported)))
  2391. goto out;
  2392. r = 0;
  2393. break;
  2394. }
  2395. default:
  2396. r = -EINVAL;
  2397. }
  2398. out:
  2399. return r;
  2400. }
  2401. static void wbinvd_ipi(void *garbage)
  2402. {
  2403. wbinvd();
  2404. }
  2405. static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
  2406. {
  2407. return kvm_arch_has_noncoherent_dma(vcpu->kvm);
  2408. }
  2409. static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
  2410. {
  2411. set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
  2412. }
  2413. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2414. {
  2415. /* Address WBINVD may be executed by guest */
  2416. if (need_emulate_wbinvd(vcpu)) {
  2417. if (kvm_x86_ops->has_wbinvd_exit())
  2418. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  2419. else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
  2420. smp_call_function_single(vcpu->cpu,
  2421. wbinvd_ipi, NULL, 1);
  2422. }
  2423. kvm_x86_ops->vcpu_load(vcpu, cpu);
  2424. /* Apply any externally detected TSC adjustments (due to suspend) */
  2425. if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
  2426. adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
  2427. vcpu->arch.tsc_offset_adjustment = 0;
  2428. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  2429. }
  2430. if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
  2431. s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
  2432. rdtsc() - vcpu->arch.last_host_tsc;
  2433. if (tsc_delta < 0)
  2434. mark_tsc_unstable("KVM discovered backwards TSC");
  2435. if (check_tsc_unstable()) {
  2436. u64 offset = kvm_compute_tsc_offset(vcpu,
  2437. vcpu->arch.last_guest_tsc);
  2438. kvm_vcpu_write_tsc_offset(vcpu, offset);
  2439. vcpu->arch.tsc_catchup = 1;
  2440. }
  2441. if (kvm_lapic_hv_timer_in_use(vcpu) &&
  2442. kvm_x86_ops->set_hv_timer(vcpu,
  2443. kvm_get_lapic_tscdeadline_msr(vcpu)))
  2444. kvm_lapic_switch_to_sw_timer(vcpu);
  2445. /*
  2446. * On a host with synchronized TSC, there is no need to update
  2447. * kvmclock on vcpu->cpu migration
  2448. */
  2449. if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
  2450. kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
  2451. if (vcpu->cpu != cpu)
  2452. kvm_migrate_timers(vcpu);
  2453. vcpu->cpu = cpu;
  2454. }
  2455. kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
  2456. }
  2457. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  2458. {
  2459. kvm_x86_ops->vcpu_put(vcpu);
  2460. kvm_put_guest_fpu(vcpu);
  2461. vcpu->arch.last_host_tsc = rdtsc();
  2462. /*
  2463. * If userspace has set any breakpoints or watchpoints, dr6 is restored
  2464. * on every vmexit, but if not, we might have a stale dr6 from the
  2465. * guest. do_debug expects dr6 to be cleared after it runs, do the same.
  2466. */
  2467. set_debugreg(0, 6);
  2468. }
  2469. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  2470. struct kvm_lapic_state *s)
  2471. {
  2472. if (vcpu->arch.apicv_active)
  2473. kvm_x86_ops->sync_pir_to_irr(vcpu);
  2474. return kvm_apic_get_state(vcpu, s);
  2475. }
  2476. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  2477. struct kvm_lapic_state *s)
  2478. {
  2479. int r;
  2480. r = kvm_apic_set_state(vcpu, s);
  2481. if (r)
  2482. return r;
  2483. update_cr8_intercept(vcpu);
  2484. return 0;
  2485. }
  2486. static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
  2487. {
  2488. return (!lapic_in_kernel(vcpu) ||
  2489. kvm_apic_accept_pic_intr(vcpu));
  2490. }
  2491. /*
  2492. * if userspace requested an interrupt window, check that the
  2493. * interrupt window is open.
  2494. *
  2495. * No need to exit to userspace if we already have an interrupt queued.
  2496. */
  2497. static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
  2498. {
  2499. return kvm_arch_interrupt_allowed(vcpu) &&
  2500. !kvm_cpu_has_interrupt(vcpu) &&
  2501. !kvm_event_needs_reinjection(vcpu) &&
  2502. kvm_cpu_accept_dm_intr(vcpu);
  2503. }
  2504. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  2505. struct kvm_interrupt *irq)
  2506. {
  2507. if (irq->irq >= KVM_NR_INTERRUPTS)
  2508. return -EINVAL;
  2509. if (!irqchip_in_kernel(vcpu->kvm)) {
  2510. kvm_queue_interrupt(vcpu, irq->irq, false);
  2511. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2512. return 0;
  2513. }
  2514. /*
  2515. * With in-kernel LAPIC, we only use this to inject EXTINT, so
  2516. * fail for in-kernel 8259.
  2517. */
  2518. if (pic_in_kernel(vcpu->kvm))
  2519. return -ENXIO;
  2520. if (vcpu->arch.pending_external_vector != -1)
  2521. return -EEXIST;
  2522. vcpu->arch.pending_external_vector = irq->irq;
  2523. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2524. return 0;
  2525. }
  2526. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  2527. {
  2528. kvm_inject_nmi(vcpu);
  2529. return 0;
  2530. }
  2531. static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
  2532. {
  2533. kvm_make_request(KVM_REQ_SMI, vcpu);
  2534. return 0;
  2535. }
  2536. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  2537. struct kvm_tpr_access_ctl *tac)
  2538. {
  2539. if (tac->flags)
  2540. return -EINVAL;
  2541. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  2542. return 0;
  2543. }
  2544. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  2545. u64 mcg_cap)
  2546. {
  2547. int r;
  2548. unsigned bank_num = mcg_cap & 0xff, bank;
  2549. r = -EINVAL;
  2550. if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
  2551. goto out;
  2552. if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
  2553. goto out;
  2554. r = 0;
  2555. vcpu->arch.mcg_cap = mcg_cap;
  2556. /* Init IA32_MCG_CTL to all 1s */
  2557. if (mcg_cap & MCG_CTL_P)
  2558. vcpu->arch.mcg_ctl = ~(u64)0;
  2559. /* Init IA32_MCi_CTL to all 1s */
  2560. for (bank = 0; bank < bank_num; bank++)
  2561. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  2562. if (kvm_x86_ops->setup_mce)
  2563. kvm_x86_ops->setup_mce(vcpu);
  2564. out:
  2565. return r;
  2566. }
  2567. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  2568. struct kvm_x86_mce *mce)
  2569. {
  2570. u64 mcg_cap = vcpu->arch.mcg_cap;
  2571. unsigned bank_num = mcg_cap & 0xff;
  2572. u64 *banks = vcpu->arch.mce_banks;
  2573. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  2574. return -EINVAL;
  2575. /*
  2576. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  2577. * reporting is disabled
  2578. */
  2579. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  2580. vcpu->arch.mcg_ctl != ~(u64)0)
  2581. return 0;
  2582. banks += 4 * mce->bank;
  2583. /*
  2584. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  2585. * reporting is disabled for the bank
  2586. */
  2587. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  2588. return 0;
  2589. if (mce->status & MCI_STATUS_UC) {
  2590. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  2591. !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
  2592. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2593. return 0;
  2594. }
  2595. if (banks[1] & MCI_STATUS_VAL)
  2596. mce->status |= MCI_STATUS_OVER;
  2597. banks[2] = mce->addr;
  2598. banks[3] = mce->misc;
  2599. vcpu->arch.mcg_status = mce->mcg_status;
  2600. banks[1] = mce->status;
  2601. kvm_queue_exception(vcpu, MC_VECTOR);
  2602. } else if (!(banks[1] & MCI_STATUS_VAL)
  2603. || !(banks[1] & MCI_STATUS_UC)) {
  2604. if (banks[1] & MCI_STATUS_VAL)
  2605. mce->status |= MCI_STATUS_OVER;
  2606. banks[2] = mce->addr;
  2607. banks[3] = mce->misc;
  2608. banks[1] = mce->status;
  2609. } else
  2610. banks[1] |= MCI_STATUS_OVER;
  2611. return 0;
  2612. }
  2613. static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
  2614. struct kvm_vcpu_events *events)
  2615. {
  2616. process_nmi(vcpu);
  2617. events->exception.injected =
  2618. vcpu->arch.exception.pending &&
  2619. !kvm_exception_is_soft(vcpu->arch.exception.nr);
  2620. events->exception.nr = vcpu->arch.exception.nr;
  2621. events->exception.has_error_code = vcpu->arch.exception.has_error_code;
  2622. events->exception.pad = 0;
  2623. events->exception.error_code = vcpu->arch.exception.error_code;
  2624. events->interrupt.injected =
  2625. vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
  2626. events->interrupt.nr = vcpu->arch.interrupt.nr;
  2627. events->interrupt.soft = 0;
  2628. events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
  2629. events->nmi.injected = vcpu->arch.nmi_injected;
  2630. events->nmi.pending = vcpu->arch.nmi_pending != 0;
  2631. events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
  2632. events->nmi.pad = 0;
  2633. events->sipi_vector = 0; /* never valid when reporting to user space */
  2634. events->smi.smm = is_smm(vcpu);
  2635. events->smi.pending = vcpu->arch.smi_pending;
  2636. events->smi.smm_inside_nmi =
  2637. !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
  2638. events->smi.latched_init = kvm_lapic_latched_init(vcpu);
  2639. events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
  2640. | KVM_VCPUEVENT_VALID_SHADOW
  2641. | KVM_VCPUEVENT_VALID_SMM);
  2642. memset(&events->reserved, 0, sizeof(events->reserved));
  2643. }
  2644. static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags);
  2645. static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  2646. struct kvm_vcpu_events *events)
  2647. {
  2648. if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
  2649. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2650. | KVM_VCPUEVENT_VALID_SHADOW
  2651. | KVM_VCPUEVENT_VALID_SMM))
  2652. return -EINVAL;
  2653. if (events->exception.injected &&
  2654. (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR ||
  2655. is_guest_mode(vcpu)))
  2656. return -EINVAL;
  2657. /* INITs are latched while in SMM */
  2658. if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
  2659. (events->smi.smm || events->smi.pending) &&
  2660. vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
  2661. return -EINVAL;
  2662. process_nmi(vcpu);
  2663. vcpu->arch.exception.pending = events->exception.injected;
  2664. vcpu->arch.exception.nr = events->exception.nr;
  2665. vcpu->arch.exception.has_error_code = events->exception.has_error_code;
  2666. vcpu->arch.exception.error_code = events->exception.error_code;
  2667. vcpu->arch.interrupt.pending = events->interrupt.injected;
  2668. vcpu->arch.interrupt.nr = events->interrupt.nr;
  2669. vcpu->arch.interrupt.soft = events->interrupt.soft;
  2670. if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
  2671. kvm_x86_ops->set_interrupt_shadow(vcpu,
  2672. events->interrupt.shadow);
  2673. vcpu->arch.nmi_injected = events->nmi.injected;
  2674. if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
  2675. vcpu->arch.nmi_pending = events->nmi.pending;
  2676. kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
  2677. if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
  2678. lapic_in_kernel(vcpu))
  2679. vcpu->arch.apic->sipi_vector = events->sipi_vector;
  2680. if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
  2681. u32 hflags = vcpu->arch.hflags;
  2682. if (events->smi.smm)
  2683. hflags |= HF_SMM_MASK;
  2684. else
  2685. hflags &= ~HF_SMM_MASK;
  2686. kvm_set_hflags(vcpu, hflags);
  2687. vcpu->arch.smi_pending = events->smi.pending;
  2688. if (events->smi.smm_inside_nmi)
  2689. vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
  2690. else
  2691. vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
  2692. if (lapic_in_kernel(vcpu)) {
  2693. if (events->smi.latched_init)
  2694. set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
  2695. else
  2696. clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
  2697. }
  2698. }
  2699. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2700. return 0;
  2701. }
  2702. static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
  2703. struct kvm_debugregs *dbgregs)
  2704. {
  2705. unsigned long val;
  2706. memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
  2707. kvm_get_dr(vcpu, 6, &val);
  2708. dbgregs->dr6 = val;
  2709. dbgregs->dr7 = vcpu->arch.dr7;
  2710. dbgregs->flags = 0;
  2711. memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
  2712. }
  2713. static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
  2714. struct kvm_debugregs *dbgregs)
  2715. {
  2716. if (dbgregs->flags)
  2717. return -EINVAL;
  2718. if (dbgregs->dr6 & ~0xffffffffull)
  2719. return -EINVAL;
  2720. if (dbgregs->dr7 & ~0xffffffffull)
  2721. return -EINVAL;
  2722. memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
  2723. kvm_update_dr0123(vcpu);
  2724. vcpu->arch.dr6 = dbgregs->dr6;
  2725. kvm_update_dr6(vcpu);
  2726. vcpu->arch.dr7 = dbgregs->dr7;
  2727. kvm_update_dr7(vcpu);
  2728. return 0;
  2729. }
  2730. #define XSTATE_COMPACTION_ENABLED (1ULL << 63)
  2731. static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
  2732. {
  2733. struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
  2734. u64 xstate_bv = xsave->header.xfeatures;
  2735. u64 valid;
  2736. /*
  2737. * Copy legacy XSAVE area, to avoid complications with CPUID
  2738. * leaves 0 and 1 in the loop below.
  2739. */
  2740. memcpy(dest, xsave, XSAVE_HDR_OFFSET);
  2741. /* Set XSTATE_BV */
  2742. xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
  2743. *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
  2744. /*
  2745. * Copy each region from the possibly compacted offset to the
  2746. * non-compacted offset.
  2747. */
  2748. valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
  2749. while (valid) {
  2750. u64 feature = valid & -valid;
  2751. int index = fls64(feature) - 1;
  2752. void *src = get_xsave_addr(xsave, feature);
  2753. if (src) {
  2754. u32 size, offset, ecx, edx;
  2755. cpuid_count(XSTATE_CPUID, index,
  2756. &size, &offset, &ecx, &edx);
  2757. memcpy(dest + offset, src, size);
  2758. }
  2759. valid -= feature;
  2760. }
  2761. }
  2762. static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
  2763. {
  2764. struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
  2765. u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
  2766. u64 valid;
  2767. /*
  2768. * Copy legacy XSAVE area, to avoid complications with CPUID
  2769. * leaves 0 and 1 in the loop below.
  2770. */
  2771. memcpy(xsave, src, XSAVE_HDR_OFFSET);
  2772. /* Set XSTATE_BV and possibly XCOMP_BV. */
  2773. xsave->header.xfeatures = xstate_bv;
  2774. if (boot_cpu_has(X86_FEATURE_XSAVES))
  2775. xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
  2776. /*
  2777. * Copy each region from the non-compacted offset to the
  2778. * possibly compacted offset.
  2779. */
  2780. valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
  2781. while (valid) {
  2782. u64 feature = valid & -valid;
  2783. int index = fls64(feature) - 1;
  2784. void *dest = get_xsave_addr(xsave, feature);
  2785. if (dest) {
  2786. u32 size, offset, ecx, edx;
  2787. cpuid_count(XSTATE_CPUID, index,
  2788. &size, &offset, &ecx, &edx);
  2789. memcpy(dest, src + offset, size);
  2790. }
  2791. valid -= feature;
  2792. }
  2793. }
  2794. static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
  2795. struct kvm_xsave *guest_xsave)
  2796. {
  2797. if (boot_cpu_has(X86_FEATURE_XSAVE)) {
  2798. memset(guest_xsave, 0, sizeof(struct kvm_xsave));
  2799. fill_xsave((u8 *) guest_xsave->region, vcpu);
  2800. } else {
  2801. memcpy(guest_xsave->region,
  2802. &vcpu->arch.guest_fpu.state.fxsave,
  2803. sizeof(struct fxregs_state));
  2804. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
  2805. XFEATURE_MASK_FPSSE;
  2806. }
  2807. }
  2808. #define XSAVE_MXCSR_OFFSET 24
  2809. static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
  2810. struct kvm_xsave *guest_xsave)
  2811. {
  2812. u64 xstate_bv =
  2813. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
  2814. u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
  2815. if (boot_cpu_has(X86_FEATURE_XSAVE)) {
  2816. /*
  2817. * Here we allow setting states that are not present in
  2818. * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
  2819. * with old userspace.
  2820. */
  2821. if (xstate_bv & ~kvm_supported_xcr0() ||
  2822. mxcsr & ~mxcsr_feature_mask)
  2823. return -EINVAL;
  2824. load_xsave(vcpu, (u8 *)guest_xsave->region);
  2825. } else {
  2826. if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
  2827. mxcsr & ~mxcsr_feature_mask)
  2828. return -EINVAL;
  2829. memcpy(&vcpu->arch.guest_fpu.state.fxsave,
  2830. guest_xsave->region, sizeof(struct fxregs_state));
  2831. }
  2832. return 0;
  2833. }
  2834. static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
  2835. struct kvm_xcrs *guest_xcrs)
  2836. {
  2837. if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
  2838. guest_xcrs->nr_xcrs = 0;
  2839. return;
  2840. }
  2841. guest_xcrs->nr_xcrs = 1;
  2842. guest_xcrs->flags = 0;
  2843. guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
  2844. guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
  2845. }
  2846. static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
  2847. struct kvm_xcrs *guest_xcrs)
  2848. {
  2849. int i, r = 0;
  2850. if (!boot_cpu_has(X86_FEATURE_XSAVE))
  2851. return -EINVAL;
  2852. if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
  2853. return -EINVAL;
  2854. for (i = 0; i < guest_xcrs->nr_xcrs; i++)
  2855. /* Only support XCR0 currently */
  2856. if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
  2857. r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
  2858. guest_xcrs->xcrs[i].value);
  2859. break;
  2860. }
  2861. if (r)
  2862. r = -EINVAL;
  2863. return r;
  2864. }
  2865. /*
  2866. * kvm_set_guest_paused() indicates to the guest kernel that it has been
  2867. * stopped by the hypervisor. This function will be called from the host only.
  2868. * EINVAL is returned when the host attempts to set the flag for a guest that
  2869. * does not support pv clocks.
  2870. */
  2871. static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
  2872. {
  2873. if (!vcpu->arch.pv_time_enabled)
  2874. return -EINVAL;
  2875. vcpu->arch.pvclock_set_guest_stopped_request = true;
  2876. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  2877. return 0;
  2878. }
  2879. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  2880. struct kvm_enable_cap *cap)
  2881. {
  2882. if (cap->flags)
  2883. return -EINVAL;
  2884. switch (cap->cap) {
  2885. case KVM_CAP_HYPERV_SYNIC:
  2886. if (!irqchip_in_kernel(vcpu->kvm))
  2887. return -EINVAL;
  2888. return kvm_hv_activate_synic(vcpu);
  2889. default:
  2890. return -EINVAL;
  2891. }
  2892. }
  2893. long kvm_arch_vcpu_ioctl(struct file *filp,
  2894. unsigned int ioctl, unsigned long arg)
  2895. {
  2896. struct kvm_vcpu *vcpu = filp->private_data;
  2897. void __user *argp = (void __user *)arg;
  2898. int r;
  2899. union {
  2900. struct kvm_lapic_state *lapic;
  2901. struct kvm_xsave *xsave;
  2902. struct kvm_xcrs *xcrs;
  2903. void *buffer;
  2904. } u;
  2905. u.buffer = NULL;
  2906. switch (ioctl) {
  2907. case KVM_GET_LAPIC: {
  2908. r = -EINVAL;
  2909. if (!lapic_in_kernel(vcpu))
  2910. goto out;
  2911. u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2912. r = -ENOMEM;
  2913. if (!u.lapic)
  2914. goto out;
  2915. r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
  2916. if (r)
  2917. goto out;
  2918. r = -EFAULT;
  2919. if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
  2920. goto out;
  2921. r = 0;
  2922. break;
  2923. }
  2924. case KVM_SET_LAPIC: {
  2925. r = -EINVAL;
  2926. if (!lapic_in_kernel(vcpu))
  2927. goto out;
  2928. u.lapic = memdup_user(argp, sizeof(*u.lapic));
  2929. if (IS_ERR(u.lapic))
  2930. return PTR_ERR(u.lapic);
  2931. r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
  2932. break;
  2933. }
  2934. case KVM_INTERRUPT: {
  2935. struct kvm_interrupt irq;
  2936. r = -EFAULT;
  2937. if (copy_from_user(&irq, argp, sizeof irq))
  2938. goto out;
  2939. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2940. break;
  2941. }
  2942. case KVM_NMI: {
  2943. r = kvm_vcpu_ioctl_nmi(vcpu);
  2944. break;
  2945. }
  2946. case KVM_SMI: {
  2947. r = kvm_vcpu_ioctl_smi(vcpu);
  2948. break;
  2949. }
  2950. case KVM_SET_CPUID: {
  2951. struct kvm_cpuid __user *cpuid_arg = argp;
  2952. struct kvm_cpuid cpuid;
  2953. r = -EFAULT;
  2954. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2955. goto out;
  2956. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2957. break;
  2958. }
  2959. case KVM_SET_CPUID2: {
  2960. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2961. struct kvm_cpuid2 cpuid;
  2962. r = -EFAULT;
  2963. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2964. goto out;
  2965. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  2966. cpuid_arg->entries);
  2967. break;
  2968. }
  2969. case KVM_GET_CPUID2: {
  2970. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2971. struct kvm_cpuid2 cpuid;
  2972. r = -EFAULT;
  2973. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2974. goto out;
  2975. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  2976. cpuid_arg->entries);
  2977. if (r)
  2978. goto out;
  2979. r = -EFAULT;
  2980. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2981. goto out;
  2982. r = 0;
  2983. break;
  2984. }
  2985. case KVM_GET_MSRS:
  2986. r = msr_io(vcpu, argp, do_get_msr, 1);
  2987. break;
  2988. case KVM_SET_MSRS:
  2989. r = msr_io(vcpu, argp, do_set_msr, 0);
  2990. break;
  2991. case KVM_TPR_ACCESS_REPORTING: {
  2992. struct kvm_tpr_access_ctl tac;
  2993. r = -EFAULT;
  2994. if (copy_from_user(&tac, argp, sizeof tac))
  2995. goto out;
  2996. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  2997. if (r)
  2998. goto out;
  2999. r = -EFAULT;
  3000. if (copy_to_user(argp, &tac, sizeof tac))
  3001. goto out;
  3002. r = 0;
  3003. break;
  3004. };
  3005. case KVM_SET_VAPIC_ADDR: {
  3006. struct kvm_vapic_addr va;
  3007. int idx;
  3008. r = -EINVAL;
  3009. if (!lapic_in_kernel(vcpu))
  3010. goto out;
  3011. r = -EFAULT;
  3012. if (copy_from_user(&va, argp, sizeof va))
  3013. goto out;
  3014. idx = srcu_read_lock(&vcpu->kvm->srcu);
  3015. r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  3016. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  3017. break;
  3018. }
  3019. case KVM_X86_SETUP_MCE: {
  3020. u64 mcg_cap;
  3021. r = -EFAULT;
  3022. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  3023. goto out;
  3024. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  3025. break;
  3026. }
  3027. case KVM_X86_SET_MCE: {
  3028. struct kvm_x86_mce mce;
  3029. r = -EFAULT;
  3030. if (copy_from_user(&mce, argp, sizeof mce))
  3031. goto out;
  3032. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  3033. break;
  3034. }
  3035. case KVM_GET_VCPU_EVENTS: {
  3036. struct kvm_vcpu_events events;
  3037. kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
  3038. r = -EFAULT;
  3039. if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
  3040. break;
  3041. r = 0;
  3042. break;
  3043. }
  3044. case KVM_SET_VCPU_EVENTS: {
  3045. struct kvm_vcpu_events events;
  3046. r = -EFAULT;
  3047. if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
  3048. break;
  3049. r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
  3050. break;
  3051. }
  3052. case KVM_GET_DEBUGREGS: {
  3053. struct kvm_debugregs dbgregs;
  3054. kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
  3055. r = -EFAULT;
  3056. if (copy_to_user(argp, &dbgregs,
  3057. sizeof(struct kvm_debugregs)))
  3058. break;
  3059. r = 0;
  3060. break;
  3061. }
  3062. case KVM_SET_DEBUGREGS: {
  3063. struct kvm_debugregs dbgregs;
  3064. r = -EFAULT;
  3065. if (copy_from_user(&dbgregs, argp,
  3066. sizeof(struct kvm_debugregs)))
  3067. break;
  3068. r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
  3069. break;
  3070. }
  3071. case KVM_GET_XSAVE: {
  3072. u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
  3073. r = -ENOMEM;
  3074. if (!u.xsave)
  3075. break;
  3076. kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
  3077. r = -EFAULT;
  3078. if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
  3079. break;
  3080. r = 0;
  3081. break;
  3082. }
  3083. case KVM_SET_XSAVE: {
  3084. u.xsave = memdup_user(argp, sizeof(*u.xsave));
  3085. if (IS_ERR(u.xsave))
  3086. return PTR_ERR(u.xsave);
  3087. r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
  3088. break;
  3089. }
  3090. case KVM_GET_XCRS: {
  3091. u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
  3092. r = -ENOMEM;
  3093. if (!u.xcrs)
  3094. break;
  3095. kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
  3096. r = -EFAULT;
  3097. if (copy_to_user(argp, u.xcrs,
  3098. sizeof(struct kvm_xcrs)))
  3099. break;
  3100. r = 0;
  3101. break;
  3102. }
  3103. case KVM_SET_XCRS: {
  3104. u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
  3105. if (IS_ERR(u.xcrs))
  3106. return PTR_ERR(u.xcrs);
  3107. r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
  3108. break;
  3109. }
  3110. case KVM_SET_TSC_KHZ: {
  3111. u32 user_tsc_khz;
  3112. r = -EINVAL;
  3113. user_tsc_khz = (u32)arg;
  3114. if (user_tsc_khz >= kvm_max_guest_tsc_khz)
  3115. goto out;
  3116. if (user_tsc_khz == 0)
  3117. user_tsc_khz = tsc_khz;
  3118. if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
  3119. r = 0;
  3120. goto out;
  3121. }
  3122. case KVM_GET_TSC_KHZ: {
  3123. r = vcpu->arch.virtual_tsc_khz;
  3124. goto out;
  3125. }
  3126. case KVM_KVMCLOCK_CTRL: {
  3127. r = kvm_set_guest_paused(vcpu);
  3128. goto out;
  3129. }
  3130. case KVM_ENABLE_CAP: {
  3131. struct kvm_enable_cap cap;
  3132. r = -EFAULT;
  3133. if (copy_from_user(&cap, argp, sizeof(cap)))
  3134. goto out;
  3135. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  3136. break;
  3137. }
  3138. default:
  3139. r = -EINVAL;
  3140. }
  3141. out:
  3142. kfree(u.buffer);
  3143. return r;
  3144. }
  3145. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  3146. {
  3147. return VM_FAULT_SIGBUS;
  3148. }
  3149. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  3150. {
  3151. int ret;
  3152. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  3153. return -EINVAL;
  3154. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  3155. return ret;
  3156. }
  3157. static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
  3158. u64 ident_addr)
  3159. {
  3160. kvm->arch.ept_identity_map_addr = ident_addr;
  3161. return 0;
  3162. }
  3163. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  3164. u32 kvm_nr_mmu_pages)
  3165. {
  3166. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  3167. return -EINVAL;
  3168. mutex_lock(&kvm->slots_lock);
  3169. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  3170. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  3171. mutex_unlock(&kvm->slots_lock);
  3172. return 0;
  3173. }
  3174. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  3175. {
  3176. return kvm->arch.n_max_mmu_pages;
  3177. }
  3178. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  3179. {
  3180. int r;
  3181. r = 0;
  3182. switch (chip->chip_id) {
  3183. case KVM_IRQCHIP_PIC_MASTER:
  3184. memcpy(&chip->chip.pic,
  3185. &pic_irqchip(kvm)->pics[0],
  3186. sizeof(struct kvm_pic_state));
  3187. break;
  3188. case KVM_IRQCHIP_PIC_SLAVE:
  3189. memcpy(&chip->chip.pic,
  3190. &pic_irqchip(kvm)->pics[1],
  3191. sizeof(struct kvm_pic_state));
  3192. break;
  3193. case KVM_IRQCHIP_IOAPIC:
  3194. r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
  3195. break;
  3196. default:
  3197. r = -EINVAL;
  3198. break;
  3199. }
  3200. return r;
  3201. }
  3202. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  3203. {
  3204. int r;
  3205. r = 0;
  3206. switch (chip->chip_id) {
  3207. case KVM_IRQCHIP_PIC_MASTER:
  3208. spin_lock(&pic_irqchip(kvm)->lock);
  3209. memcpy(&pic_irqchip(kvm)->pics[0],
  3210. &chip->chip.pic,
  3211. sizeof(struct kvm_pic_state));
  3212. spin_unlock(&pic_irqchip(kvm)->lock);
  3213. break;
  3214. case KVM_IRQCHIP_PIC_SLAVE:
  3215. spin_lock(&pic_irqchip(kvm)->lock);
  3216. memcpy(&pic_irqchip(kvm)->pics[1],
  3217. &chip->chip.pic,
  3218. sizeof(struct kvm_pic_state));
  3219. spin_unlock(&pic_irqchip(kvm)->lock);
  3220. break;
  3221. case KVM_IRQCHIP_IOAPIC:
  3222. r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
  3223. break;
  3224. default:
  3225. r = -EINVAL;
  3226. break;
  3227. }
  3228. kvm_pic_update_irq(pic_irqchip(kvm));
  3229. return r;
  3230. }
  3231. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  3232. {
  3233. struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
  3234. BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
  3235. mutex_lock(&kps->lock);
  3236. memcpy(ps, &kps->channels, sizeof(*ps));
  3237. mutex_unlock(&kps->lock);
  3238. return 0;
  3239. }
  3240. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  3241. {
  3242. int i;
  3243. struct kvm_pit *pit = kvm->arch.vpit;
  3244. mutex_lock(&pit->pit_state.lock);
  3245. memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
  3246. for (i = 0; i < 3; i++)
  3247. kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
  3248. mutex_unlock(&pit->pit_state.lock);
  3249. return 0;
  3250. }
  3251. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  3252. {
  3253. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  3254. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  3255. sizeof(ps->channels));
  3256. ps->flags = kvm->arch.vpit->pit_state.flags;
  3257. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  3258. memset(&ps->reserved, 0, sizeof(ps->reserved));
  3259. return 0;
  3260. }
  3261. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  3262. {
  3263. int start = 0;
  3264. int i;
  3265. u32 prev_legacy, cur_legacy;
  3266. struct kvm_pit *pit = kvm->arch.vpit;
  3267. mutex_lock(&pit->pit_state.lock);
  3268. prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  3269. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  3270. if (!prev_legacy && cur_legacy)
  3271. start = 1;
  3272. memcpy(&pit->pit_state.channels, &ps->channels,
  3273. sizeof(pit->pit_state.channels));
  3274. pit->pit_state.flags = ps->flags;
  3275. for (i = 0; i < 3; i++)
  3276. kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
  3277. start && i == 0);
  3278. mutex_unlock(&pit->pit_state.lock);
  3279. return 0;
  3280. }
  3281. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  3282. struct kvm_reinject_control *control)
  3283. {
  3284. struct kvm_pit *pit = kvm->arch.vpit;
  3285. if (!pit)
  3286. return -ENXIO;
  3287. /* pit->pit_state.lock was overloaded to prevent userspace from getting
  3288. * an inconsistent state after running multiple KVM_REINJECT_CONTROL
  3289. * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
  3290. */
  3291. mutex_lock(&pit->pit_state.lock);
  3292. kvm_pit_set_reinject(pit, control->pit_reinject);
  3293. mutex_unlock(&pit->pit_state.lock);
  3294. return 0;
  3295. }
  3296. /**
  3297. * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
  3298. * @kvm: kvm instance
  3299. * @log: slot id and address to which we copy the log
  3300. *
  3301. * Steps 1-4 below provide general overview of dirty page logging. See
  3302. * kvm_get_dirty_log_protect() function description for additional details.
  3303. *
  3304. * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
  3305. * always flush the TLB (step 4) even if previous step failed and the dirty
  3306. * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
  3307. * does not preclude user space subsequent dirty log read. Flushing TLB ensures
  3308. * writes will be marked dirty for next log read.
  3309. *
  3310. * 1. Take a snapshot of the bit and clear it if needed.
  3311. * 2. Write protect the corresponding page.
  3312. * 3. Copy the snapshot to the userspace.
  3313. * 4. Flush TLB's if needed.
  3314. */
  3315. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  3316. {
  3317. bool is_dirty = false;
  3318. int r;
  3319. mutex_lock(&kvm->slots_lock);
  3320. /*
  3321. * Flush potentially hardware-cached dirty pages to dirty_bitmap.
  3322. */
  3323. if (kvm_x86_ops->flush_log_dirty)
  3324. kvm_x86_ops->flush_log_dirty(kvm);
  3325. r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
  3326. /*
  3327. * All the TLBs can be flushed out of mmu lock, see the comments in
  3328. * kvm_mmu_slot_remove_write_access().
  3329. */
  3330. lockdep_assert_held(&kvm->slots_lock);
  3331. if (is_dirty)
  3332. kvm_flush_remote_tlbs(kvm);
  3333. mutex_unlock(&kvm->slots_lock);
  3334. return r;
  3335. }
  3336. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  3337. bool line_status)
  3338. {
  3339. if (!irqchip_in_kernel(kvm))
  3340. return -ENXIO;
  3341. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  3342. irq_event->irq, irq_event->level,
  3343. line_status);
  3344. return 0;
  3345. }
  3346. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  3347. struct kvm_enable_cap *cap)
  3348. {
  3349. int r;
  3350. if (cap->flags)
  3351. return -EINVAL;
  3352. switch (cap->cap) {
  3353. case KVM_CAP_DISABLE_QUIRKS:
  3354. kvm->arch.disabled_quirks = cap->args[0];
  3355. r = 0;
  3356. break;
  3357. case KVM_CAP_SPLIT_IRQCHIP: {
  3358. mutex_lock(&kvm->lock);
  3359. r = -EINVAL;
  3360. if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
  3361. goto split_irqchip_unlock;
  3362. r = -EEXIST;
  3363. if (irqchip_in_kernel(kvm))
  3364. goto split_irqchip_unlock;
  3365. if (kvm->created_vcpus)
  3366. goto split_irqchip_unlock;
  3367. r = kvm_setup_empty_irq_routing(kvm);
  3368. if (r)
  3369. goto split_irqchip_unlock;
  3370. /* Pairs with irqchip_in_kernel. */
  3371. smp_wmb();
  3372. kvm->arch.irqchip_split = true;
  3373. kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
  3374. r = 0;
  3375. split_irqchip_unlock:
  3376. mutex_unlock(&kvm->lock);
  3377. break;
  3378. }
  3379. case KVM_CAP_X2APIC_API:
  3380. r = -EINVAL;
  3381. if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
  3382. break;
  3383. if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
  3384. kvm->arch.x2apic_format = true;
  3385. if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
  3386. kvm->arch.x2apic_broadcast_quirk_disabled = true;
  3387. r = 0;
  3388. break;
  3389. default:
  3390. r = -EINVAL;
  3391. break;
  3392. }
  3393. return r;
  3394. }
  3395. long kvm_arch_vm_ioctl(struct file *filp,
  3396. unsigned int ioctl, unsigned long arg)
  3397. {
  3398. struct kvm *kvm = filp->private_data;
  3399. void __user *argp = (void __user *)arg;
  3400. int r = -ENOTTY;
  3401. /*
  3402. * This union makes it completely explicit to gcc-3.x
  3403. * that these two variables' stack usage should be
  3404. * combined, not added together.
  3405. */
  3406. union {
  3407. struct kvm_pit_state ps;
  3408. struct kvm_pit_state2 ps2;
  3409. struct kvm_pit_config pit_config;
  3410. } u;
  3411. switch (ioctl) {
  3412. case KVM_SET_TSS_ADDR:
  3413. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  3414. break;
  3415. case KVM_SET_IDENTITY_MAP_ADDR: {
  3416. u64 ident_addr;
  3417. r = -EFAULT;
  3418. if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
  3419. goto out;
  3420. r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
  3421. break;
  3422. }
  3423. case KVM_SET_NR_MMU_PAGES:
  3424. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  3425. break;
  3426. case KVM_GET_NR_MMU_PAGES:
  3427. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  3428. break;
  3429. case KVM_CREATE_IRQCHIP: {
  3430. struct kvm_pic *vpic;
  3431. mutex_lock(&kvm->lock);
  3432. r = -EEXIST;
  3433. if (kvm->arch.vpic)
  3434. goto create_irqchip_unlock;
  3435. r = -EINVAL;
  3436. if (kvm->created_vcpus)
  3437. goto create_irqchip_unlock;
  3438. r = -ENOMEM;
  3439. vpic = kvm_create_pic(kvm);
  3440. if (vpic) {
  3441. r = kvm_ioapic_init(kvm);
  3442. if (r) {
  3443. mutex_lock(&kvm->slots_lock);
  3444. kvm_destroy_pic(vpic);
  3445. mutex_unlock(&kvm->slots_lock);
  3446. goto create_irqchip_unlock;
  3447. }
  3448. } else
  3449. goto create_irqchip_unlock;
  3450. r = kvm_setup_default_irq_routing(kvm);
  3451. if (r) {
  3452. mutex_lock(&kvm->slots_lock);
  3453. mutex_lock(&kvm->irq_lock);
  3454. kvm_ioapic_destroy(kvm);
  3455. kvm_destroy_pic(vpic);
  3456. mutex_unlock(&kvm->irq_lock);
  3457. mutex_unlock(&kvm->slots_lock);
  3458. goto create_irqchip_unlock;
  3459. }
  3460. /* Write kvm->irq_routing before kvm->arch.vpic. */
  3461. smp_wmb();
  3462. kvm->arch.vpic = vpic;
  3463. create_irqchip_unlock:
  3464. mutex_unlock(&kvm->lock);
  3465. break;
  3466. }
  3467. case KVM_CREATE_PIT:
  3468. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  3469. goto create_pit;
  3470. case KVM_CREATE_PIT2:
  3471. r = -EFAULT;
  3472. if (copy_from_user(&u.pit_config, argp,
  3473. sizeof(struct kvm_pit_config)))
  3474. goto out;
  3475. create_pit:
  3476. mutex_lock(&kvm->lock);
  3477. r = -EEXIST;
  3478. if (kvm->arch.vpit)
  3479. goto create_pit_unlock;
  3480. r = -ENOMEM;
  3481. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  3482. if (kvm->arch.vpit)
  3483. r = 0;
  3484. create_pit_unlock:
  3485. mutex_unlock(&kvm->lock);
  3486. break;
  3487. case KVM_GET_IRQCHIP: {
  3488. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  3489. struct kvm_irqchip *chip;
  3490. chip = memdup_user(argp, sizeof(*chip));
  3491. if (IS_ERR(chip)) {
  3492. r = PTR_ERR(chip);
  3493. goto out;
  3494. }
  3495. r = -ENXIO;
  3496. if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
  3497. goto get_irqchip_out;
  3498. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  3499. if (r)
  3500. goto get_irqchip_out;
  3501. r = -EFAULT;
  3502. if (copy_to_user(argp, chip, sizeof *chip))
  3503. goto get_irqchip_out;
  3504. r = 0;
  3505. get_irqchip_out:
  3506. kfree(chip);
  3507. break;
  3508. }
  3509. case KVM_SET_IRQCHIP: {
  3510. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  3511. struct kvm_irqchip *chip;
  3512. chip = memdup_user(argp, sizeof(*chip));
  3513. if (IS_ERR(chip)) {
  3514. r = PTR_ERR(chip);
  3515. goto out;
  3516. }
  3517. r = -ENXIO;
  3518. if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
  3519. goto set_irqchip_out;
  3520. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  3521. if (r)
  3522. goto set_irqchip_out;
  3523. r = 0;
  3524. set_irqchip_out:
  3525. kfree(chip);
  3526. break;
  3527. }
  3528. case KVM_GET_PIT: {
  3529. r = -EFAULT;
  3530. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  3531. goto out;
  3532. r = -ENXIO;
  3533. if (!kvm->arch.vpit)
  3534. goto out;
  3535. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  3536. if (r)
  3537. goto out;
  3538. r = -EFAULT;
  3539. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  3540. goto out;
  3541. r = 0;
  3542. break;
  3543. }
  3544. case KVM_SET_PIT: {
  3545. r = -EFAULT;
  3546. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  3547. goto out;
  3548. r = -ENXIO;
  3549. if (!kvm->arch.vpit)
  3550. goto out;
  3551. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  3552. break;
  3553. }
  3554. case KVM_GET_PIT2: {
  3555. r = -ENXIO;
  3556. if (!kvm->arch.vpit)
  3557. goto out;
  3558. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  3559. if (r)
  3560. goto out;
  3561. r = -EFAULT;
  3562. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  3563. goto out;
  3564. r = 0;
  3565. break;
  3566. }
  3567. case KVM_SET_PIT2: {
  3568. r = -EFAULT;
  3569. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  3570. goto out;
  3571. r = -ENXIO;
  3572. if (!kvm->arch.vpit)
  3573. goto out;
  3574. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  3575. break;
  3576. }
  3577. case KVM_REINJECT_CONTROL: {
  3578. struct kvm_reinject_control control;
  3579. r = -EFAULT;
  3580. if (copy_from_user(&control, argp, sizeof(control)))
  3581. goto out;
  3582. r = kvm_vm_ioctl_reinject(kvm, &control);
  3583. break;
  3584. }
  3585. case KVM_SET_BOOT_CPU_ID:
  3586. r = 0;
  3587. mutex_lock(&kvm->lock);
  3588. if (kvm->created_vcpus)
  3589. r = -EBUSY;
  3590. else
  3591. kvm->arch.bsp_vcpu_id = arg;
  3592. mutex_unlock(&kvm->lock);
  3593. break;
  3594. case KVM_XEN_HVM_CONFIG: {
  3595. struct kvm_xen_hvm_config xhc;
  3596. r = -EFAULT;
  3597. if (copy_from_user(&xhc, argp, sizeof(xhc)))
  3598. goto out;
  3599. r = -EINVAL;
  3600. if (xhc.flags)
  3601. goto out;
  3602. memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
  3603. r = 0;
  3604. break;
  3605. }
  3606. case KVM_SET_CLOCK: {
  3607. struct kvm_clock_data user_ns;
  3608. u64 now_ns;
  3609. r = -EFAULT;
  3610. if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
  3611. goto out;
  3612. r = -EINVAL;
  3613. if (user_ns.flags)
  3614. goto out;
  3615. r = 0;
  3616. local_irq_disable();
  3617. now_ns = __get_kvmclock_ns(kvm);
  3618. kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
  3619. local_irq_enable();
  3620. kvm_gen_update_masterclock(kvm);
  3621. break;
  3622. }
  3623. case KVM_GET_CLOCK: {
  3624. struct kvm_clock_data user_ns;
  3625. u64 now_ns;
  3626. local_irq_disable();
  3627. now_ns = __get_kvmclock_ns(kvm);
  3628. user_ns.clock = now_ns;
  3629. user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
  3630. local_irq_enable();
  3631. memset(&user_ns.pad, 0, sizeof(user_ns.pad));
  3632. r = -EFAULT;
  3633. if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
  3634. goto out;
  3635. r = 0;
  3636. break;
  3637. }
  3638. case KVM_ENABLE_CAP: {
  3639. struct kvm_enable_cap cap;
  3640. r = -EFAULT;
  3641. if (copy_from_user(&cap, argp, sizeof(cap)))
  3642. goto out;
  3643. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  3644. break;
  3645. }
  3646. default:
  3647. r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
  3648. }
  3649. out:
  3650. return r;
  3651. }
  3652. static void kvm_init_msr_list(void)
  3653. {
  3654. u32 dummy[2];
  3655. unsigned i, j;
  3656. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  3657. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  3658. continue;
  3659. /*
  3660. * Even MSRs that are valid in the host may not be exposed
  3661. * to the guests in some cases.
  3662. */
  3663. switch (msrs_to_save[i]) {
  3664. case MSR_IA32_BNDCFGS:
  3665. if (!kvm_x86_ops->mpx_supported())
  3666. continue;
  3667. break;
  3668. case MSR_TSC_AUX:
  3669. if (!kvm_x86_ops->rdtscp_supported())
  3670. continue;
  3671. break;
  3672. default:
  3673. break;
  3674. }
  3675. if (j < i)
  3676. msrs_to_save[j] = msrs_to_save[i];
  3677. j++;
  3678. }
  3679. num_msrs_to_save = j;
  3680. for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
  3681. if (!kvm_x86_ops->has_emulated_msr(emulated_msrs[i]))
  3682. continue;
  3683. if (j < i)
  3684. emulated_msrs[j] = emulated_msrs[i];
  3685. j++;
  3686. }
  3687. num_emulated_msrs = j;
  3688. }
  3689. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  3690. const void *v)
  3691. {
  3692. int handled = 0;
  3693. int n;
  3694. do {
  3695. n = min(len, 8);
  3696. if (!(lapic_in_kernel(vcpu) &&
  3697. !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
  3698. && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
  3699. break;
  3700. handled += n;
  3701. addr += n;
  3702. len -= n;
  3703. v += n;
  3704. } while (len);
  3705. return handled;
  3706. }
  3707. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  3708. {
  3709. int handled = 0;
  3710. int n;
  3711. do {
  3712. n = min(len, 8);
  3713. if (!(lapic_in_kernel(vcpu) &&
  3714. !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
  3715. addr, n, v))
  3716. && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
  3717. break;
  3718. trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
  3719. handled += n;
  3720. addr += n;
  3721. len -= n;
  3722. v += n;
  3723. } while (len);
  3724. return handled;
  3725. }
  3726. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3727. struct kvm_segment *var, int seg)
  3728. {
  3729. kvm_x86_ops->set_segment(vcpu, var, seg);
  3730. }
  3731. void kvm_get_segment(struct kvm_vcpu *vcpu,
  3732. struct kvm_segment *var, int seg)
  3733. {
  3734. kvm_x86_ops->get_segment(vcpu, var, seg);
  3735. }
  3736. gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
  3737. struct x86_exception *exception)
  3738. {
  3739. gpa_t t_gpa;
  3740. BUG_ON(!mmu_is_nested(vcpu));
  3741. /* NPT walks are always user-walks */
  3742. access |= PFERR_USER_MASK;
  3743. t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
  3744. return t_gpa;
  3745. }
  3746. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  3747. struct x86_exception *exception)
  3748. {
  3749. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3750. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3751. }
  3752. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  3753. struct x86_exception *exception)
  3754. {
  3755. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3756. access |= PFERR_FETCH_MASK;
  3757. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3758. }
  3759. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  3760. struct x86_exception *exception)
  3761. {
  3762. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3763. access |= PFERR_WRITE_MASK;
  3764. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3765. }
  3766. /* uses this to access any guest's mapped memory without checking CPL */
  3767. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  3768. struct x86_exception *exception)
  3769. {
  3770. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
  3771. }
  3772. static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  3773. struct kvm_vcpu *vcpu, u32 access,
  3774. struct x86_exception *exception)
  3775. {
  3776. void *data = val;
  3777. int r = X86EMUL_CONTINUE;
  3778. while (bytes) {
  3779. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
  3780. exception);
  3781. unsigned offset = addr & (PAGE_SIZE-1);
  3782. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  3783. int ret;
  3784. if (gpa == UNMAPPED_GVA)
  3785. return X86EMUL_PROPAGATE_FAULT;
  3786. ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
  3787. offset, toread);
  3788. if (ret < 0) {
  3789. r = X86EMUL_IO_NEEDED;
  3790. goto out;
  3791. }
  3792. bytes -= toread;
  3793. data += toread;
  3794. addr += toread;
  3795. }
  3796. out:
  3797. return r;
  3798. }
  3799. /* used for instruction fetching */
  3800. static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
  3801. gva_t addr, void *val, unsigned int bytes,
  3802. struct x86_exception *exception)
  3803. {
  3804. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3805. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3806. unsigned offset;
  3807. int ret;
  3808. /* Inline kvm_read_guest_virt_helper for speed. */
  3809. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
  3810. exception);
  3811. if (unlikely(gpa == UNMAPPED_GVA))
  3812. return X86EMUL_PROPAGATE_FAULT;
  3813. offset = addr & (PAGE_SIZE-1);
  3814. if (WARN_ON(offset + bytes > PAGE_SIZE))
  3815. bytes = (unsigned)PAGE_SIZE - offset;
  3816. ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
  3817. offset, bytes);
  3818. if (unlikely(ret < 0))
  3819. return X86EMUL_IO_NEEDED;
  3820. return X86EMUL_CONTINUE;
  3821. }
  3822. int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
  3823. gva_t addr, void *val, unsigned int bytes,
  3824. struct x86_exception *exception)
  3825. {
  3826. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3827. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
  3828. exception);
  3829. }
  3830. EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
  3831. static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
  3832. gva_t addr, void *val, unsigned int bytes,
  3833. struct x86_exception *exception, bool system)
  3834. {
  3835. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3836. u32 access = 0;
  3837. if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
  3838. access |= PFERR_USER_MASK;
  3839. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
  3840. }
  3841. static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
  3842. unsigned long addr, void *val, unsigned int bytes)
  3843. {
  3844. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3845. int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
  3846. return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
  3847. }
  3848. static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  3849. struct kvm_vcpu *vcpu, u32 access,
  3850. struct x86_exception *exception)
  3851. {
  3852. void *data = val;
  3853. int r = X86EMUL_CONTINUE;
  3854. while (bytes) {
  3855. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
  3856. access,
  3857. exception);
  3858. unsigned offset = addr & (PAGE_SIZE-1);
  3859. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  3860. int ret;
  3861. if (gpa == UNMAPPED_GVA)
  3862. return X86EMUL_PROPAGATE_FAULT;
  3863. ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
  3864. if (ret < 0) {
  3865. r = X86EMUL_IO_NEEDED;
  3866. goto out;
  3867. }
  3868. bytes -= towrite;
  3869. data += towrite;
  3870. addr += towrite;
  3871. }
  3872. out:
  3873. return r;
  3874. }
  3875. static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
  3876. unsigned int bytes, struct x86_exception *exception,
  3877. bool system)
  3878. {
  3879. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3880. u32 access = PFERR_WRITE_MASK;
  3881. if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
  3882. access |= PFERR_USER_MASK;
  3883. return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
  3884. access, exception);
  3885. }
  3886. int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
  3887. unsigned int bytes, struct x86_exception *exception)
  3888. {
  3889. return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
  3890. PFERR_WRITE_MASK, exception);
  3891. }
  3892. EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
  3893. static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
  3894. gpa_t *gpa, struct x86_exception *exception,
  3895. bool write)
  3896. {
  3897. u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
  3898. | (write ? PFERR_WRITE_MASK : 0);
  3899. /*
  3900. * currently PKRU is only applied to ept enabled guest so
  3901. * there is no pkey in EPT page table for L1 guest or EPT
  3902. * shadow page table for L2 guest.
  3903. */
  3904. if (vcpu_match_mmio_gva(vcpu, gva)
  3905. && !permission_fault(vcpu, vcpu->arch.walk_mmu,
  3906. vcpu->arch.access, 0, access)) {
  3907. *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
  3908. (gva & (PAGE_SIZE - 1));
  3909. trace_vcpu_match_mmio(gva, *gpa, write, false);
  3910. return 1;
  3911. }
  3912. *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3913. if (*gpa == UNMAPPED_GVA)
  3914. return -1;
  3915. /* For APIC access vmexit */
  3916. if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3917. return 1;
  3918. if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
  3919. trace_vcpu_match_mmio(gva, *gpa, write, true);
  3920. return 1;
  3921. }
  3922. return 0;
  3923. }
  3924. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  3925. const void *val, int bytes)
  3926. {
  3927. int ret;
  3928. ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
  3929. if (ret < 0)
  3930. return 0;
  3931. kvm_page_track_write(vcpu, gpa, val, bytes);
  3932. return 1;
  3933. }
  3934. struct read_write_emulator_ops {
  3935. int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
  3936. int bytes);
  3937. int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3938. void *val, int bytes);
  3939. int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3940. int bytes, void *val);
  3941. int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3942. void *val, int bytes);
  3943. bool write;
  3944. };
  3945. static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
  3946. {
  3947. if (vcpu->mmio_read_completed) {
  3948. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  3949. vcpu->mmio_fragments[0].gpa, val);
  3950. vcpu->mmio_read_completed = 0;
  3951. return 1;
  3952. }
  3953. return 0;
  3954. }
  3955. static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
  3956. void *val, int bytes)
  3957. {
  3958. return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
  3959. }
  3960. static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
  3961. void *val, int bytes)
  3962. {
  3963. return emulator_write_phys(vcpu, gpa, val, bytes);
  3964. }
  3965. static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
  3966. {
  3967. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
  3968. return vcpu_mmio_write(vcpu, gpa, bytes, val);
  3969. }
  3970. static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
  3971. void *val, int bytes)
  3972. {
  3973. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
  3974. return X86EMUL_IO_NEEDED;
  3975. }
  3976. static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
  3977. void *val, int bytes)
  3978. {
  3979. struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
  3980. memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
  3981. return X86EMUL_CONTINUE;
  3982. }
  3983. static const struct read_write_emulator_ops read_emultor = {
  3984. .read_write_prepare = read_prepare,
  3985. .read_write_emulate = read_emulate,
  3986. .read_write_mmio = vcpu_mmio_read,
  3987. .read_write_exit_mmio = read_exit_mmio,
  3988. };
  3989. static const struct read_write_emulator_ops write_emultor = {
  3990. .read_write_emulate = write_emulate,
  3991. .read_write_mmio = write_mmio,
  3992. .read_write_exit_mmio = write_exit_mmio,
  3993. .write = true,
  3994. };
  3995. static int emulator_read_write_onepage(unsigned long addr, void *val,
  3996. unsigned int bytes,
  3997. struct x86_exception *exception,
  3998. struct kvm_vcpu *vcpu,
  3999. const struct read_write_emulator_ops *ops)
  4000. {
  4001. gpa_t gpa;
  4002. int handled, ret;
  4003. bool write = ops->write;
  4004. struct kvm_mmio_fragment *frag;
  4005. ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
  4006. if (ret < 0)
  4007. return X86EMUL_PROPAGATE_FAULT;
  4008. /* For APIC access vmexit */
  4009. if (ret)
  4010. goto mmio;
  4011. if (ops->read_write_emulate(vcpu, gpa, val, bytes))
  4012. return X86EMUL_CONTINUE;
  4013. mmio:
  4014. /*
  4015. * Is this MMIO handled locally?
  4016. */
  4017. handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
  4018. if (handled == bytes)
  4019. return X86EMUL_CONTINUE;
  4020. gpa += handled;
  4021. bytes -= handled;
  4022. val += handled;
  4023. WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
  4024. frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
  4025. frag->gpa = gpa;
  4026. frag->data = val;
  4027. frag->len = bytes;
  4028. return X86EMUL_CONTINUE;
  4029. }
  4030. static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
  4031. unsigned long addr,
  4032. void *val, unsigned int bytes,
  4033. struct x86_exception *exception,
  4034. const struct read_write_emulator_ops *ops)
  4035. {
  4036. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4037. gpa_t gpa;
  4038. int rc;
  4039. if (ops->read_write_prepare &&
  4040. ops->read_write_prepare(vcpu, val, bytes))
  4041. return X86EMUL_CONTINUE;
  4042. vcpu->mmio_nr_fragments = 0;
  4043. /* Crossing a page boundary? */
  4044. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  4045. int now;
  4046. now = -addr & ~PAGE_MASK;
  4047. rc = emulator_read_write_onepage(addr, val, now, exception,
  4048. vcpu, ops);
  4049. if (rc != X86EMUL_CONTINUE)
  4050. return rc;
  4051. addr += now;
  4052. if (ctxt->mode != X86EMUL_MODE_PROT64)
  4053. addr = (u32)addr;
  4054. val += now;
  4055. bytes -= now;
  4056. }
  4057. rc = emulator_read_write_onepage(addr, val, bytes, exception,
  4058. vcpu, ops);
  4059. if (rc != X86EMUL_CONTINUE)
  4060. return rc;
  4061. if (!vcpu->mmio_nr_fragments)
  4062. return rc;
  4063. gpa = vcpu->mmio_fragments[0].gpa;
  4064. vcpu->mmio_needed = 1;
  4065. vcpu->mmio_cur_fragment = 0;
  4066. vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
  4067. vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
  4068. vcpu->run->exit_reason = KVM_EXIT_MMIO;
  4069. vcpu->run->mmio.phys_addr = gpa;
  4070. return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
  4071. }
  4072. static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
  4073. unsigned long addr,
  4074. void *val,
  4075. unsigned int bytes,
  4076. struct x86_exception *exception)
  4077. {
  4078. return emulator_read_write(ctxt, addr, val, bytes,
  4079. exception, &read_emultor);
  4080. }
  4081. static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
  4082. unsigned long addr,
  4083. const void *val,
  4084. unsigned int bytes,
  4085. struct x86_exception *exception)
  4086. {
  4087. return emulator_read_write(ctxt, addr, (void *)val, bytes,
  4088. exception, &write_emultor);
  4089. }
  4090. #define CMPXCHG_TYPE(t, ptr, old, new) \
  4091. (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
  4092. #ifdef CONFIG_X86_64
  4093. # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
  4094. #else
  4095. # define CMPXCHG64(ptr, old, new) \
  4096. (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
  4097. #endif
  4098. static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
  4099. unsigned long addr,
  4100. const void *old,
  4101. const void *new,
  4102. unsigned int bytes,
  4103. struct x86_exception *exception)
  4104. {
  4105. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4106. gpa_t gpa;
  4107. struct page *page;
  4108. char *kaddr;
  4109. bool exchanged;
  4110. /* guests cmpxchg8b have to be emulated atomically */
  4111. if (bytes > 8 || (bytes & (bytes - 1)))
  4112. goto emul_write;
  4113. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
  4114. if (gpa == UNMAPPED_GVA ||
  4115. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  4116. goto emul_write;
  4117. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  4118. goto emul_write;
  4119. page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
  4120. if (is_error_page(page))
  4121. goto emul_write;
  4122. kaddr = kmap_atomic(page);
  4123. kaddr += offset_in_page(gpa);
  4124. switch (bytes) {
  4125. case 1:
  4126. exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
  4127. break;
  4128. case 2:
  4129. exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
  4130. break;
  4131. case 4:
  4132. exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
  4133. break;
  4134. case 8:
  4135. exchanged = CMPXCHG64(kaddr, old, new);
  4136. break;
  4137. default:
  4138. BUG();
  4139. }
  4140. kunmap_atomic(kaddr);
  4141. kvm_release_page_dirty(page);
  4142. if (!exchanged)
  4143. return X86EMUL_CMPXCHG_FAILED;
  4144. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  4145. kvm_page_track_write(vcpu, gpa, new, bytes);
  4146. return X86EMUL_CONTINUE;
  4147. emul_write:
  4148. printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
  4149. return emulator_write_emulated(ctxt, addr, new, bytes, exception);
  4150. }
  4151. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  4152. {
  4153. int r = 0, i;
  4154. for (i = 0; i < vcpu->arch.pio.count; i++) {
  4155. if (vcpu->arch.pio.in)
  4156. r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
  4157. vcpu->arch.pio.size, pd);
  4158. else
  4159. r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
  4160. vcpu->arch.pio.port, vcpu->arch.pio.size,
  4161. pd);
  4162. if (r)
  4163. break;
  4164. pd += vcpu->arch.pio.size;
  4165. }
  4166. return r;
  4167. }
  4168. static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
  4169. unsigned short port, void *val,
  4170. unsigned int count, bool in)
  4171. {
  4172. vcpu->arch.pio.port = port;
  4173. vcpu->arch.pio.in = in;
  4174. vcpu->arch.pio.count = count;
  4175. vcpu->arch.pio.size = size;
  4176. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  4177. vcpu->arch.pio.count = 0;
  4178. return 1;
  4179. }
  4180. vcpu->run->exit_reason = KVM_EXIT_IO;
  4181. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  4182. vcpu->run->io.size = size;
  4183. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  4184. vcpu->run->io.count = count;
  4185. vcpu->run->io.port = port;
  4186. return 0;
  4187. }
  4188. static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
  4189. int size, unsigned short port, void *val,
  4190. unsigned int count)
  4191. {
  4192. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4193. int ret;
  4194. if (vcpu->arch.pio.count)
  4195. goto data_avail;
  4196. memset(vcpu->arch.pio_data, 0, size * count);
  4197. ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
  4198. if (ret) {
  4199. data_avail:
  4200. memcpy(val, vcpu->arch.pio_data, size * count);
  4201. trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
  4202. vcpu->arch.pio.count = 0;
  4203. return 1;
  4204. }
  4205. return 0;
  4206. }
  4207. static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
  4208. int size, unsigned short port,
  4209. const void *val, unsigned int count)
  4210. {
  4211. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4212. memcpy(vcpu->arch.pio_data, val, size * count);
  4213. trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
  4214. return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
  4215. }
  4216. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4217. {
  4218. return kvm_x86_ops->get_segment_base(vcpu, seg);
  4219. }
  4220. static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
  4221. {
  4222. kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
  4223. }
  4224. int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
  4225. {
  4226. if (!need_emulate_wbinvd(vcpu))
  4227. return X86EMUL_CONTINUE;
  4228. if (kvm_x86_ops->has_wbinvd_exit()) {
  4229. int cpu = get_cpu();
  4230. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  4231. smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
  4232. wbinvd_ipi, NULL, 1);
  4233. put_cpu();
  4234. cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
  4235. } else
  4236. wbinvd();
  4237. return X86EMUL_CONTINUE;
  4238. }
  4239. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
  4240. {
  4241. kvm_x86_ops->skip_emulated_instruction(vcpu);
  4242. return kvm_emulate_wbinvd_noskip(vcpu);
  4243. }
  4244. EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
  4245. static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
  4246. {
  4247. kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
  4248. }
  4249. static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
  4250. unsigned long *dest)
  4251. {
  4252. return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
  4253. }
  4254. static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  4255. unsigned long value)
  4256. {
  4257. return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
  4258. }
  4259. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  4260. {
  4261. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  4262. }
  4263. static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
  4264. {
  4265. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4266. unsigned long value;
  4267. switch (cr) {
  4268. case 0:
  4269. value = kvm_read_cr0(vcpu);
  4270. break;
  4271. case 2:
  4272. value = vcpu->arch.cr2;
  4273. break;
  4274. case 3:
  4275. value = kvm_read_cr3(vcpu);
  4276. break;
  4277. case 4:
  4278. value = kvm_read_cr4(vcpu);
  4279. break;
  4280. case 8:
  4281. value = kvm_get_cr8(vcpu);
  4282. break;
  4283. default:
  4284. kvm_err("%s: unexpected cr %u\n", __func__, cr);
  4285. return 0;
  4286. }
  4287. return value;
  4288. }
  4289. static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
  4290. {
  4291. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4292. int res = 0;
  4293. switch (cr) {
  4294. case 0:
  4295. res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
  4296. break;
  4297. case 2:
  4298. vcpu->arch.cr2 = val;
  4299. break;
  4300. case 3:
  4301. res = kvm_set_cr3(vcpu, val);
  4302. break;
  4303. case 4:
  4304. res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
  4305. break;
  4306. case 8:
  4307. res = kvm_set_cr8(vcpu, val);
  4308. break;
  4309. default:
  4310. kvm_err("%s: unexpected cr %u\n", __func__, cr);
  4311. res = -1;
  4312. }
  4313. return res;
  4314. }
  4315. static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
  4316. {
  4317. return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
  4318. }
  4319. static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  4320. {
  4321. kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
  4322. }
  4323. static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  4324. {
  4325. kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
  4326. }
  4327. static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  4328. {
  4329. kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
  4330. }
  4331. static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  4332. {
  4333. kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
  4334. }
  4335. static unsigned long emulator_get_cached_segment_base(
  4336. struct x86_emulate_ctxt *ctxt, int seg)
  4337. {
  4338. return get_segment_base(emul_to_vcpu(ctxt), seg);
  4339. }
  4340. static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
  4341. struct desc_struct *desc, u32 *base3,
  4342. int seg)
  4343. {
  4344. struct kvm_segment var;
  4345. kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
  4346. *selector = var.selector;
  4347. if (var.unusable) {
  4348. memset(desc, 0, sizeof(*desc));
  4349. if (base3)
  4350. *base3 = 0;
  4351. return false;
  4352. }
  4353. if (var.g)
  4354. var.limit >>= 12;
  4355. set_desc_limit(desc, var.limit);
  4356. set_desc_base(desc, (unsigned long)var.base);
  4357. #ifdef CONFIG_X86_64
  4358. if (base3)
  4359. *base3 = var.base >> 32;
  4360. #endif
  4361. desc->type = var.type;
  4362. desc->s = var.s;
  4363. desc->dpl = var.dpl;
  4364. desc->p = var.present;
  4365. desc->avl = var.avl;
  4366. desc->l = var.l;
  4367. desc->d = var.db;
  4368. desc->g = var.g;
  4369. return true;
  4370. }
  4371. static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
  4372. struct desc_struct *desc, u32 base3,
  4373. int seg)
  4374. {
  4375. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4376. struct kvm_segment var;
  4377. var.selector = selector;
  4378. var.base = get_desc_base(desc);
  4379. #ifdef CONFIG_X86_64
  4380. var.base |= ((u64)base3) << 32;
  4381. #endif
  4382. var.limit = get_desc_limit(desc);
  4383. if (desc->g)
  4384. var.limit = (var.limit << 12) | 0xfff;
  4385. var.type = desc->type;
  4386. var.dpl = desc->dpl;
  4387. var.db = desc->d;
  4388. var.s = desc->s;
  4389. var.l = desc->l;
  4390. var.g = desc->g;
  4391. var.avl = desc->avl;
  4392. var.present = desc->p;
  4393. var.unusable = !var.present;
  4394. var.padding = 0;
  4395. kvm_set_segment(vcpu, &var, seg);
  4396. return;
  4397. }
  4398. static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
  4399. u32 msr_index, u64 *pdata)
  4400. {
  4401. struct msr_data msr;
  4402. int r;
  4403. msr.index = msr_index;
  4404. msr.host_initiated = false;
  4405. r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
  4406. if (r)
  4407. return r;
  4408. *pdata = msr.data;
  4409. return 0;
  4410. }
  4411. static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
  4412. u32 msr_index, u64 data)
  4413. {
  4414. struct msr_data msr;
  4415. msr.data = data;
  4416. msr.index = msr_index;
  4417. msr.host_initiated = false;
  4418. return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
  4419. }
  4420. static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
  4421. {
  4422. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4423. return vcpu->arch.smbase;
  4424. }
  4425. static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
  4426. {
  4427. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4428. vcpu->arch.smbase = smbase;
  4429. }
  4430. static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
  4431. u32 pmc)
  4432. {
  4433. return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
  4434. }
  4435. static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
  4436. u32 pmc, u64 *pdata)
  4437. {
  4438. return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
  4439. }
  4440. static void emulator_halt(struct x86_emulate_ctxt *ctxt)
  4441. {
  4442. emul_to_vcpu(ctxt)->arch.halt_request = 1;
  4443. }
  4444. static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
  4445. {
  4446. preempt_disable();
  4447. kvm_load_guest_fpu(emul_to_vcpu(ctxt));
  4448. /*
  4449. * CR0.TS may reference the host fpu state, not the guest fpu state,
  4450. * so it may be clear at this point.
  4451. */
  4452. clts();
  4453. }
  4454. static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
  4455. {
  4456. preempt_enable();
  4457. }
  4458. static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
  4459. struct x86_instruction_info *info,
  4460. enum x86_intercept_stage stage)
  4461. {
  4462. return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
  4463. }
  4464. static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
  4465. u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
  4466. {
  4467. kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
  4468. }
  4469. static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
  4470. {
  4471. return kvm_register_read(emul_to_vcpu(ctxt), reg);
  4472. }
  4473. static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
  4474. {
  4475. kvm_register_write(emul_to_vcpu(ctxt), reg, val);
  4476. }
  4477. static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
  4478. {
  4479. kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
  4480. }
  4481. static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
  4482. {
  4483. return emul_to_vcpu(ctxt)->arch.hflags;
  4484. }
  4485. static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
  4486. {
  4487. kvm_set_hflags(emul_to_vcpu(ctxt), emul_flags);
  4488. }
  4489. static const struct x86_emulate_ops emulate_ops = {
  4490. .read_gpr = emulator_read_gpr,
  4491. .write_gpr = emulator_write_gpr,
  4492. .read_std = emulator_read_std,
  4493. .write_std = emulator_write_std,
  4494. .read_phys = kvm_read_guest_phys_system,
  4495. .fetch = kvm_fetch_guest_virt,
  4496. .read_emulated = emulator_read_emulated,
  4497. .write_emulated = emulator_write_emulated,
  4498. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  4499. .invlpg = emulator_invlpg,
  4500. .pio_in_emulated = emulator_pio_in_emulated,
  4501. .pio_out_emulated = emulator_pio_out_emulated,
  4502. .get_segment = emulator_get_segment,
  4503. .set_segment = emulator_set_segment,
  4504. .get_cached_segment_base = emulator_get_cached_segment_base,
  4505. .get_gdt = emulator_get_gdt,
  4506. .get_idt = emulator_get_idt,
  4507. .set_gdt = emulator_set_gdt,
  4508. .set_idt = emulator_set_idt,
  4509. .get_cr = emulator_get_cr,
  4510. .set_cr = emulator_set_cr,
  4511. .cpl = emulator_get_cpl,
  4512. .get_dr = emulator_get_dr,
  4513. .set_dr = emulator_set_dr,
  4514. .get_smbase = emulator_get_smbase,
  4515. .set_smbase = emulator_set_smbase,
  4516. .set_msr = emulator_set_msr,
  4517. .get_msr = emulator_get_msr,
  4518. .check_pmc = emulator_check_pmc,
  4519. .read_pmc = emulator_read_pmc,
  4520. .halt = emulator_halt,
  4521. .wbinvd = emulator_wbinvd,
  4522. .fix_hypercall = emulator_fix_hypercall,
  4523. .get_fpu = emulator_get_fpu,
  4524. .put_fpu = emulator_put_fpu,
  4525. .intercept = emulator_intercept,
  4526. .get_cpuid = emulator_get_cpuid,
  4527. .set_nmi_mask = emulator_set_nmi_mask,
  4528. .get_hflags = emulator_get_hflags,
  4529. .set_hflags = emulator_set_hflags,
  4530. };
  4531. static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
  4532. {
  4533. u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
  4534. /*
  4535. * an sti; sti; sequence only disable interrupts for the first
  4536. * instruction. So, if the last instruction, be it emulated or
  4537. * not, left the system with the INT_STI flag enabled, it
  4538. * means that the last instruction is an sti. We should not
  4539. * leave the flag on in this case. The same goes for mov ss
  4540. */
  4541. if (int_shadow & mask)
  4542. mask = 0;
  4543. if (unlikely(int_shadow || mask)) {
  4544. kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
  4545. if (!mask)
  4546. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4547. }
  4548. }
  4549. static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
  4550. {
  4551. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4552. if (ctxt->exception.vector == PF_VECTOR)
  4553. return kvm_propagate_fault(vcpu, &ctxt->exception);
  4554. if (ctxt->exception.error_code_valid)
  4555. kvm_queue_exception_e(vcpu, ctxt->exception.vector,
  4556. ctxt->exception.error_code);
  4557. else
  4558. kvm_queue_exception(vcpu, ctxt->exception.vector);
  4559. return false;
  4560. }
  4561. static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
  4562. {
  4563. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4564. int cs_db, cs_l;
  4565. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  4566. ctxt->eflags = kvm_get_rflags(vcpu);
  4567. ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
  4568. ctxt->eip = kvm_rip_read(vcpu);
  4569. ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  4570. (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
  4571. (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
  4572. cs_db ? X86EMUL_MODE_PROT32 :
  4573. X86EMUL_MODE_PROT16;
  4574. BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
  4575. BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
  4576. BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
  4577. init_decode_cache(ctxt);
  4578. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  4579. }
  4580. int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
  4581. {
  4582. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4583. int ret;
  4584. init_emulate_ctxt(vcpu);
  4585. ctxt->op_bytes = 2;
  4586. ctxt->ad_bytes = 2;
  4587. ctxt->_eip = ctxt->eip + inc_eip;
  4588. ret = emulate_int_real(ctxt, irq);
  4589. if (ret != X86EMUL_CONTINUE)
  4590. return EMULATE_FAIL;
  4591. ctxt->eip = ctxt->_eip;
  4592. kvm_rip_write(vcpu, ctxt->eip);
  4593. kvm_set_rflags(vcpu, ctxt->eflags);
  4594. if (irq == NMI_VECTOR)
  4595. vcpu->arch.nmi_pending = 0;
  4596. else
  4597. vcpu->arch.interrupt.pending = false;
  4598. return EMULATE_DONE;
  4599. }
  4600. EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
  4601. static int handle_emulation_failure(struct kvm_vcpu *vcpu)
  4602. {
  4603. int r = EMULATE_DONE;
  4604. ++vcpu->stat.insn_emulation_fail;
  4605. trace_kvm_emulate_insn_failed(vcpu);
  4606. if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
  4607. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4608. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4609. vcpu->run->internal.ndata = 0;
  4610. r = EMULATE_USER_EXIT;
  4611. }
  4612. kvm_queue_exception(vcpu, UD_VECTOR);
  4613. return r;
  4614. }
  4615. static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
  4616. bool write_fault_to_shadow_pgtable,
  4617. int emulation_type)
  4618. {
  4619. gpa_t gpa = cr2;
  4620. kvm_pfn_t pfn;
  4621. if (emulation_type & EMULTYPE_NO_REEXECUTE)
  4622. return false;
  4623. if (!vcpu->arch.mmu.direct_map) {
  4624. /*
  4625. * Write permission should be allowed since only
  4626. * write access need to be emulated.
  4627. */
  4628. gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
  4629. /*
  4630. * If the mapping is invalid in guest, let cpu retry
  4631. * it to generate fault.
  4632. */
  4633. if (gpa == UNMAPPED_GVA)
  4634. return true;
  4635. }
  4636. /*
  4637. * Do not retry the unhandleable instruction if it faults on the
  4638. * readonly host memory, otherwise it will goto a infinite loop:
  4639. * retry instruction -> write #PF -> emulation fail -> retry
  4640. * instruction -> ...
  4641. */
  4642. pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
  4643. /*
  4644. * If the instruction failed on the error pfn, it can not be fixed,
  4645. * report the error to userspace.
  4646. */
  4647. if (is_error_noslot_pfn(pfn))
  4648. return false;
  4649. kvm_release_pfn_clean(pfn);
  4650. /* The instructions are well-emulated on direct mmu. */
  4651. if (vcpu->arch.mmu.direct_map) {
  4652. unsigned int indirect_shadow_pages;
  4653. spin_lock(&vcpu->kvm->mmu_lock);
  4654. indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
  4655. spin_unlock(&vcpu->kvm->mmu_lock);
  4656. if (indirect_shadow_pages)
  4657. kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
  4658. return true;
  4659. }
  4660. /*
  4661. * if emulation was due to access to shadowed page table
  4662. * and it failed try to unshadow page and re-enter the
  4663. * guest to let CPU execute the instruction.
  4664. */
  4665. kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
  4666. /*
  4667. * If the access faults on its page table, it can not
  4668. * be fixed by unprotecting shadow page and it should
  4669. * be reported to userspace.
  4670. */
  4671. return !write_fault_to_shadow_pgtable;
  4672. }
  4673. static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
  4674. unsigned long cr2, int emulation_type)
  4675. {
  4676. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4677. unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
  4678. last_retry_eip = vcpu->arch.last_retry_eip;
  4679. last_retry_addr = vcpu->arch.last_retry_addr;
  4680. /*
  4681. * If the emulation is caused by #PF and it is non-page_table
  4682. * writing instruction, it means the VM-EXIT is caused by shadow
  4683. * page protected, we can zap the shadow page and retry this
  4684. * instruction directly.
  4685. *
  4686. * Note: if the guest uses a non-page-table modifying instruction
  4687. * on the PDE that points to the instruction, then we will unmap
  4688. * the instruction and go to an infinite loop. So, we cache the
  4689. * last retried eip and the last fault address, if we meet the eip
  4690. * and the address again, we can break out of the potential infinite
  4691. * loop.
  4692. */
  4693. vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
  4694. if (!(emulation_type & EMULTYPE_RETRY))
  4695. return false;
  4696. if (x86_page_table_writing_insn(ctxt))
  4697. return false;
  4698. if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
  4699. return false;
  4700. vcpu->arch.last_retry_eip = ctxt->eip;
  4701. vcpu->arch.last_retry_addr = cr2;
  4702. if (!vcpu->arch.mmu.direct_map)
  4703. gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
  4704. kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
  4705. return true;
  4706. }
  4707. static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
  4708. static int complete_emulated_pio(struct kvm_vcpu *vcpu);
  4709. static void kvm_smm_changed(struct kvm_vcpu *vcpu)
  4710. {
  4711. if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
  4712. /* This is a good place to trace that we are exiting SMM. */
  4713. trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
  4714. /* Process a latched INIT or SMI, if any. */
  4715. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4716. }
  4717. kvm_mmu_reset_context(vcpu);
  4718. }
  4719. static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
  4720. {
  4721. unsigned changed = vcpu->arch.hflags ^ emul_flags;
  4722. vcpu->arch.hflags = emul_flags;
  4723. if (changed & HF_SMM_MASK)
  4724. kvm_smm_changed(vcpu);
  4725. }
  4726. static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
  4727. unsigned long *db)
  4728. {
  4729. u32 dr6 = 0;
  4730. int i;
  4731. u32 enable, rwlen;
  4732. enable = dr7;
  4733. rwlen = dr7 >> 16;
  4734. for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
  4735. if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
  4736. dr6 |= (1 << i);
  4737. return dr6;
  4738. }
  4739. static void kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu, int *r)
  4740. {
  4741. struct kvm_run *kvm_run = vcpu->run;
  4742. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  4743. kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
  4744. kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
  4745. kvm_run->debug.arch.exception = DB_VECTOR;
  4746. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  4747. *r = EMULATE_USER_EXIT;
  4748. } else {
  4749. vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
  4750. /*
  4751. * "Certain debug exceptions may clear bit 0-3. The
  4752. * remaining contents of the DR6 register are never
  4753. * cleared by the processor".
  4754. */
  4755. vcpu->arch.dr6 &= ~15;
  4756. vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
  4757. kvm_queue_exception(vcpu, DB_VECTOR);
  4758. }
  4759. }
  4760. static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
  4761. {
  4762. if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
  4763. (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
  4764. struct kvm_run *kvm_run = vcpu->run;
  4765. unsigned long eip = kvm_get_linear_rip(vcpu);
  4766. u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
  4767. vcpu->arch.guest_debug_dr7,
  4768. vcpu->arch.eff_db);
  4769. if (dr6 != 0) {
  4770. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
  4771. kvm_run->debug.arch.pc = eip;
  4772. kvm_run->debug.arch.exception = DB_VECTOR;
  4773. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  4774. *r = EMULATE_USER_EXIT;
  4775. return true;
  4776. }
  4777. }
  4778. if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
  4779. !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
  4780. unsigned long eip = kvm_get_linear_rip(vcpu);
  4781. u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
  4782. vcpu->arch.dr7,
  4783. vcpu->arch.db);
  4784. if (dr6 != 0) {
  4785. vcpu->arch.dr6 &= ~15;
  4786. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  4787. kvm_queue_exception(vcpu, DB_VECTOR);
  4788. *r = EMULATE_DONE;
  4789. return true;
  4790. }
  4791. }
  4792. return false;
  4793. }
  4794. int x86_emulate_instruction(struct kvm_vcpu *vcpu,
  4795. unsigned long cr2,
  4796. int emulation_type,
  4797. void *insn,
  4798. int insn_len)
  4799. {
  4800. int r;
  4801. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4802. bool writeback = true;
  4803. bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
  4804. /*
  4805. * Clear write_fault_to_shadow_pgtable here to ensure it is
  4806. * never reused.
  4807. */
  4808. vcpu->arch.write_fault_to_shadow_pgtable = false;
  4809. kvm_clear_exception_queue(vcpu);
  4810. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  4811. init_emulate_ctxt(vcpu);
  4812. /*
  4813. * We will reenter on the same instruction since
  4814. * we do not set complete_userspace_io. This does not
  4815. * handle watchpoints yet, those would be handled in
  4816. * the emulate_ops.
  4817. */
  4818. if (kvm_vcpu_check_breakpoint(vcpu, &r))
  4819. return r;
  4820. ctxt->interruptibility = 0;
  4821. ctxt->have_exception = false;
  4822. ctxt->exception.vector = -1;
  4823. ctxt->perm_ok = false;
  4824. ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
  4825. r = x86_decode_insn(ctxt, insn, insn_len);
  4826. trace_kvm_emulate_insn_start(vcpu);
  4827. ++vcpu->stat.insn_emulation;
  4828. if (r != EMULATION_OK) {
  4829. if (emulation_type & EMULTYPE_TRAP_UD)
  4830. return EMULATE_FAIL;
  4831. if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
  4832. emulation_type))
  4833. return EMULATE_DONE;
  4834. if (ctxt->have_exception && inject_emulated_exception(vcpu))
  4835. return EMULATE_DONE;
  4836. if (emulation_type & EMULTYPE_SKIP)
  4837. return EMULATE_FAIL;
  4838. return handle_emulation_failure(vcpu);
  4839. }
  4840. }
  4841. if (emulation_type & EMULTYPE_SKIP) {
  4842. kvm_rip_write(vcpu, ctxt->_eip);
  4843. if (ctxt->eflags & X86_EFLAGS_RF)
  4844. kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
  4845. return EMULATE_DONE;
  4846. }
  4847. if (retry_instruction(ctxt, cr2, emulation_type))
  4848. return EMULATE_DONE;
  4849. /* this is needed for vmware backdoor interface to work since it
  4850. changes registers values during IO operation */
  4851. if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
  4852. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  4853. emulator_invalidate_register_cache(ctxt);
  4854. }
  4855. restart:
  4856. r = x86_emulate_insn(ctxt);
  4857. if (r == EMULATION_INTERCEPTED)
  4858. return EMULATE_DONE;
  4859. if (r == EMULATION_FAILED) {
  4860. if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
  4861. emulation_type))
  4862. return EMULATE_DONE;
  4863. return handle_emulation_failure(vcpu);
  4864. }
  4865. if (ctxt->have_exception) {
  4866. r = EMULATE_DONE;
  4867. if (inject_emulated_exception(vcpu))
  4868. return r;
  4869. } else if (vcpu->arch.pio.count) {
  4870. if (!vcpu->arch.pio.in) {
  4871. /* FIXME: return into emulator if single-stepping. */
  4872. vcpu->arch.pio.count = 0;
  4873. } else {
  4874. writeback = false;
  4875. vcpu->arch.complete_userspace_io = complete_emulated_pio;
  4876. }
  4877. r = EMULATE_USER_EXIT;
  4878. } else if (vcpu->mmio_needed) {
  4879. if (!vcpu->mmio_is_write)
  4880. writeback = false;
  4881. r = EMULATE_USER_EXIT;
  4882. vcpu->arch.complete_userspace_io = complete_emulated_mmio;
  4883. } else if (r == EMULATION_RESTART)
  4884. goto restart;
  4885. else
  4886. r = EMULATE_DONE;
  4887. if (writeback) {
  4888. unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
  4889. toggle_interruptibility(vcpu, ctxt->interruptibility);
  4890. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4891. kvm_rip_write(vcpu, ctxt->eip);
  4892. if (r == EMULATE_DONE &&
  4893. (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
  4894. kvm_vcpu_do_singlestep(vcpu, &r);
  4895. if (!ctxt->have_exception ||
  4896. exception_type(ctxt->exception.vector) == EXCPT_TRAP)
  4897. __kvm_set_rflags(vcpu, ctxt->eflags);
  4898. /*
  4899. * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
  4900. * do nothing, and it will be requested again as soon as
  4901. * the shadow expires. But we still need to check here,
  4902. * because POPF has no interrupt shadow.
  4903. */
  4904. if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
  4905. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4906. } else
  4907. vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
  4908. return r;
  4909. }
  4910. EXPORT_SYMBOL_GPL(x86_emulate_instruction);
  4911. int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
  4912. {
  4913. unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4914. int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
  4915. size, port, &val, 1);
  4916. /* do not return to emulator after return from userspace */
  4917. vcpu->arch.pio.count = 0;
  4918. return ret;
  4919. }
  4920. EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
  4921. static int kvmclock_cpu_down_prep(unsigned int cpu)
  4922. {
  4923. __this_cpu_write(cpu_tsc_khz, 0);
  4924. return 0;
  4925. }
  4926. static void tsc_khz_changed(void *data)
  4927. {
  4928. struct cpufreq_freqs *freq = data;
  4929. unsigned long khz = 0;
  4930. if (data)
  4931. khz = freq->new;
  4932. else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  4933. khz = cpufreq_quick_get(raw_smp_processor_id());
  4934. if (!khz)
  4935. khz = tsc_khz;
  4936. __this_cpu_write(cpu_tsc_khz, khz);
  4937. }
  4938. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  4939. void *data)
  4940. {
  4941. struct cpufreq_freqs *freq = data;
  4942. struct kvm *kvm;
  4943. struct kvm_vcpu *vcpu;
  4944. int i, send_ipi = 0;
  4945. /*
  4946. * We allow guests to temporarily run on slowing clocks,
  4947. * provided we notify them after, or to run on accelerating
  4948. * clocks, provided we notify them before. Thus time never
  4949. * goes backwards.
  4950. *
  4951. * However, we have a problem. We can't atomically update
  4952. * the frequency of a given CPU from this function; it is
  4953. * merely a notifier, which can be called from any CPU.
  4954. * Changing the TSC frequency at arbitrary points in time
  4955. * requires a recomputation of local variables related to
  4956. * the TSC for each VCPU. We must flag these local variables
  4957. * to be updated and be sure the update takes place with the
  4958. * new frequency before any guests proceed.
  4959. *
  4960. * Unfortunately, the combination of hotplug CPU and frequency
  4961. * change creates an intractable locking scenario; the order
  4962. * of when these callouts happen is undefined with respect to
  4963. * CPU hotplug, and they can race with each other. As such,
  4964. * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
  4965. * undefined; you can actually have a CPU frequency change take
  4966. * place in between the computation of X and the setting of the
  4967. * variable. To protect against this problem, all updates of
  4968. * the per_cpu tsc_khz variable are done in an interrupt
  4969. * protected IPI, and all callers wishing to update the value
  4970. * must wait for a synchronous IPI to complete (which is trivial
  4971. * if the caller is on the CPU already). This establishes the
  4972. * necessary total order on variable updates.
  4973. *
  4974. * Note that because a guest time update may take place
  4975. * anytime after the setting of the VCPU's request bit, the
  4976. * correct TSC value must be set before the request. However,
  4977. * to ensure the update actually makes it to any guest which
  4978. * starts running in hardware virtualization between the set
  4979. * and the acquisition of the spinlock, we must also ping the
  4980. * CPU after setting the request bit.
  4981. *
  4982. */
  4983. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  4984. return 0;
  4985. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  4986. return 0;
  4987. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4988. spin_lock(&kvm_lock);
  4989. list_for_each_entry(kvm, &vm_list, vm_list) {
  4990. kvm_for_each_vcpu(i, vcpu, kvm) {
  4991. if (vcpu->cpu != freq->cpu)
  4992. continue;
  4993. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  4994. if (vcpu->cpu != smp_processor_id())
  4995. send_ipi = 1;
  4996. }
  4997. }
  4998. spin_unlock(&kvm_lock);
  4999. if (freq->old < freq->new && send_ipi) {
  5000. /*
  5001. * We upscale the frequency. Must make the guest
  5002. * doesn't see old kvmclock values while running with
  5003. * the new frequency, otherwise we risk the guest sees
  5004. * time go backwards.
  5005. *
  5006. * In case we update the frequency for another cpu
  5007. * (which might be in guest context) send an interrupt
  5008. * to kick the cpu out of guest context. Next time
  5009. * guest context is entered kvmclock will be updated,
  5010. * so the guest will not see stale values.
  5011. */
  5012. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  5013. }
  5014. return 0;
  5015. }
  5016. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  5017. .notifier_call = kvmclock_cpufreq_notifier
  5018. };
  5019. static int kvmclock_cpu_online(unsigned int cpu)
  5020. {
  5021. tsc_khz_changed(NULL);
  5022. return 0;
  5023. }
  5024. static void kvm_timer_init(void)
  5025. {
  5026. max_tsc_khz = tsc_khz;
  5027. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  5028. #ifdef CONFIG_CPU_FREQ
  5029. struct cpufreq_policy policy;
  5030. int cpu;
  5031. memset(&policy, 0, sizeof(policy));
  5032. cpu = get_cpu();
  5033. cpufreq_get_policy(&policy, cpu);
  5034. if (policy.cpuinfo.max_freq)
  5035. max_tsc_khz = policy.cpuinfo.max_freq;
  5036. put_cpu();
  5037. #endif
  5038. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  5039. CPUFREQ_TRANSITION_NOTIFIER);
  5040. }
  5041. pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
  5042. cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "AP_X86_KVM_CLK_ONLINE",
  5043. kvmclock_cpu_online, kvmclock_cpu_down_prep);
  5044. }
  5045. static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
  5046. int kvm_is_in_guest(void)
  5047. {
  5048. return __this_cpu_read(current_vcpu) != NULL;
  5049. }
  5050. static int kvm_is_user_mode(void)
  5051. {
  5052. int user_mode = 3;
  5053. if (__this_cpu_read(current_vcpu))
  5054. user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
  5055. return user_mode != 0;
  5056. }
  5057. static unsigned long kvm_get_guest_ip(void)
  5058. {
  5059. unsigned long ip = 0;
  5060. if (__this_cpu_read(current_vcpu))
  5061. ip = kvm_rip_read(__this_cpu_read(current_vcpu));
  5062. return ip;
  5063. }
  5064. static struct perf_guest_info_callbacks kvm_guest_cbs = {
  5065. .is_in_guest = kvm_is_in_guest,
  5066. .is_user_mode = kvm_is_user_mode,
  5067. .get_guest_ip = kvm_get_guest_ip,
  5068. };
  5069. void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
  5070. {
  5071. __this_cpu_write(current_vcpu, vcpu);
  5072. }
  5073. EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
  5074. void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
  5075. {
  5076. __this_cpu_write(current_vcpu, NULL);
  5077. }
  5078. EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
  5079. static void kvm_set_mmio_spte_mask(void)
  5080. {
  5081. u64 mask;
  5082. int maxphyaddr = boot_cpu_data.x86_phys_bits;
  5083. /*
  5084. * Set the reserved bits and the present bit of an paging-structure
  5085. * entry to generate page fault with PFER.RSV = 1.
  5086. */
  5087. /* Mask the reserved physical address bits. */
  5088. mask = rsvd_bits(maxphyaddr, 51);
  5089. /* Bit 62 is always reserved for 32bit host. */
  5090. mask |= 0x3ull << 62;
  5091. /* Set the present bit. */
  5092. mask |= 1ull;
  5093. #ifdef CONFIG_X86_64
  5094. /*
  5095. * If reserved bit is not supported, clear the present bit to disable
  5096. * mmio page fault.
  5097. */
  5098. if (maxphyaddr == 52)
  5099. mask &= ~1ull;
  5100. #endif
  5101. kvm_mmu_set_mmio_spte_mask(mask);
  5102. }
  5103. #ifdef CONFIG_X86_64
  5104. static void pvclock_gtod_update_fn(struct work_struct *work)
  5105. {
  5106. struct kvm *kvm;
  5107. struct kvm_vcpu *vcpu;
  5108. int i;
  5109. spin_lock(&kvm_lock);
  5110. list_for_each_entry(kvm, &vm_list, vm_list)
  5111. kvm_for_each_vcpu(i, vcpu, kvm)
  5112. kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
  5113. atomic_set(&kvm_guest_has_master_clock, 0);
  5114. spin_unlock(&kvm_lock);
  5115. }
  5116. static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
  5117. /*
  5118. * Notification about pvclock gtod data update.
  5119. */
  5120. static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
  5121. void *priv)
  5122. {
  5123. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  5124. struct timekeeper *tk = priv;
  5125. update_pvclock_gtod(tk);
  5126. /* disable master clock if host does not trust, or does not
  5127. * use, TSC clocksource
  5128. */
  5129. if (gtod->clock.vclock_mode != VCLOCK_TSC &&
  5130. atomic_read(&kvm_guest_has_master_clock) != 0)
  5131. queue_work(system_long_wq, &pvclock_gtod_work);
  5132. return 0;
  5133. }
  5134. static struct notifier_block pvclock_gtod_notifier = {
  5135. .notifier_call = pvclock_gtod_notify,
  5136. };
  5137. #endif
  5138. int kvm_arch_init(void *opaque)
  5139. {
  5140. int r;
  5141. struct kvm_x86_ops *ops = opaque;
  5142. if (kvm_x86_ops) {
  5143. printk(KERN_ERR "kvm: already loaded the other module\n");
  5144. r = -EEXIST;
  5145. goto out;
  5146. }
  5147. if (!ops->cpu_has_kvm_support()) {
  5148. printk(KERN_ERR "kvm: no hardware support\n");
  5149. r = -EOPNOTSUPP;
  5150. goto out;
  5151. }
  5152. if (ops->disabled_by_bios()) {
  5153. printk(KERN_ERR "kvm: disabled by bios\n");
  5154. r = -EOPNOTSUPP;
  5155. goto out;
  5156. }
  5157. r = -ENOMEM;
  5158. shared_msrs = alloc_percpu(struct kvm_shared_msrs);
  5159. if (!shared_msrs) {
  5160. printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
  5161. goto out;
  5162. }
  5163. r = kvm_mmu_module_init();
  5164. if (r)
  5165. goto out_free_percpu;
  5166. kvm_set_mmio_spte_mask();
  5167. kvm_x86_ops = ops;
  5168. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  5169. PT_DIRTY_MASK, PT64_NX_MASK, 0,
  5170. PT_PRESENT_MASK);
  5171. kvm_timer_init();
  5172. perf_register_guest_info_callbacks(&kvm_guest_cbs);
  5173. if (boot_cpu_has(X86_FEATURE_XSAVE))
  5174. host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  5175. kvm_lapic_init();
  5176. #ifdef CONFIG_X86_64
  5177. pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
  5178. #endif
  5179. return 0;
  5180. out_free_percpu:
  5181. free_percpu(shared_msrs);
  5182. out:
  5183. return r;
  5184. }
  5185. void kvm_arch_exit(void)
  5186. {
  5187. kvm_lapic_exit();
  5188. perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
  5189. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  5190. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  5191. CPUFREQ_TRANSITION_NOTIFIER);
  5192. cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
  5193. #ifdef CONFIG_X86_64
  5194. pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
  5195. #endif
  5196. kvm_x86_ops = NULL;
  5197. kvm_mmu_module_exit();
  5198. free_percpu(shared_msrs);
  5199. }
  5200. int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
  5201. {
  5202. ++vcpu->stat.halt_exits;
  5203. if (lapic_in_kernel(vcpu)) {
  5204. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  5205. return 1;
  5206. } else {
  5207. vcpu->run->exit_reason = KVM_EXIT_HLT;
  5208. return 0;
  5209. }
  5210. }
  5211. EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
  5212. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  5213. {
  5214. kvm_x86_ops->skip_emulated_instruction(vcpu);
  5215. return kvm_vcpu_halt(vcpu);
  5216. }
  5217. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  5218. /*
  5219. * kvm_pv_kick_cpu_op: Kick a vcpu.
  5220. *
  5221. * @apicid - apicid of vcpu to be kicked.
  5222. */
  5223. static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
  5224. {
  5225. struct kvm_lapic_irq lapic_irq;
  5226. lapic_irq.shorthand = 0;
  5227. lapic_irq.dest_mode = 0;
  5228. lapic_irq.dest_id = apicid;
  5229. lapic_irq.msi_redir_hint = false;
  5230. lapic_irq.delivery_mode = APIC_DM_REMRD;
  5231. kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
  5232. }
  5233. void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
  5234. {
  5235. vcpu->arch.apicv_active = false;
  5236. kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
  5237. }
  5238. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  5239. {
  5240. unsigned long nr, a0, a1, a2, a3, ret;
  5241. int op_64_bit, r = 1;
  5242. kvm_x86_ops->skip_emulated_instruction(vcpu);
  5243. if (kvm_hv_hypercall_enabled(vcpu->kvm))
  5244. return kvm_hv_hypercall(vcpu);
  5245. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  5246. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  5247. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  5248. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  5249. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  5250. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  5251. op_64_bit = is_64_bit_mode(vcpu);
  5252. if (!op_64_bit) {
  5253. nr &= 0xFFFFFFFF;
  5254. a0 &= 0xFFFFFFFF;
  5255. a1 &= 0xFFFFFFFF;
  5256. a2 &= 0xFFFFFFFF;
  5257. a3 &= 0xFFFFFFFF;
  5258. }
  5259. if (kvm_x86_ops->get_cpl(vcpu) != 0) {
  5260. ret = -KVM_EPERM;
  5261. goto out;
  5262. }
  5263. switch (nr) {
  5264. case KVM_HC_VAPIC_POLL_IRQ:
  5265. ret = 0;
  5266. break;
  5267. case KVM_HC_KICK_CPU:
  5268. kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
  5269. ret = 0;
  5270. break;
  5271. default:
  5272. ret = -KVM_ENOSYS;
  5273. break;
  5274. }
  5275. out:
  5276. if (!op_64_bit)
  5277. ret = (u32)ret;
  5278. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  5279. ++vcpu->stat.hypercalls;
  5280. return r;
  5281. }
  5282. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  5283. static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
  5284. {
  5285. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  5286. char instruction[3];
  5287. unsigned long rip = kvm_rip_read(vcpu);
  5288. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  5289. return emulator_write_emulated(ctxt, rip, instruction, 3,
  5290. &ctxt->exception);
  5291. }
  5292. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
  5293. {
  5294. return vcpu->run->request_interrupt_window &&
  5295. likely(!pic_in_kernel(vcpu->kvm));
  5296. }
  5297. static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  5298. {
  5299. struct kvm_run *kvm_run = vcpu->run;
  5300. kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  5301. kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
  5302. kvm_run->cr8 = kvm_get_cr8(vcpu);
  5303. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  5304. kvm_run->ready_for_interrupt_injection =
  5305. pic_in_kernel(vcpu->kvm) ||
  5306. kvm_vcpu_ready_for_interrupt_injection(vcpu);
  5307. }
  5308. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  5309. {
  5310. int max_irr, tpr;
  5311. if (!kvm_x86_ops->update_cr8_intercept)
  5312. return;
  5313. if (!lapic_in_kernel(vcpu))
  5314. return;
  5315. if (vcpu->arch.apicv_active)
  5316. return;
  5317. if (!vcpu->arch.apic->vapic_addr)
  5318. max_irr = kvm_lapic_find_highest_irr(vcpu);
  5319. else
  5320. max_irr = -1;
  5321. if (max_irr != -1)
  5322. max_irr >>= 4;
  5323. tpr = kvm_lapic_get_cr8(vcpu);
  5324. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  5325. }
  5326. static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
  5327. {
  5328. int r;
  5329. /* try to reinject previous events if any */
  5330. if (vcpu->arch.exception.pending) {
  5331. trace_kvm_inj_exception(vcpu->arch.exception.nr,
  5332. vcpu->arch.exception.has_error_code,
  5333. vcpu->arch.exception.error_code);
  5334. if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
  5335. __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
  5336. X86_EFLAGS_RF);
  5337. if (vcpu->arch.exception.nr == DB_VECTOR &&
  5338. (vcpu->arch.dr7 & DR7_GD)) {
  5339. vcpu->arch.dr7 &= ~DR7_GD;
  5340. kvm_update_dr7(vcpu);
  5341. }
  5342. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  5343. vcpu->arch.exception.has_error_code,
  5344. vcpu->arch.exception.error_code,
  5345. vcpu->arch.exception.reinject);
  5346. return 0;
  5347. }
  5348. if (vcpu->arch.nmi_injected) {
  5349. kvm_x86_ops->set_nmi(vcpu);
  5350. return 0;
  5351. }
  5352. if (vcpu->arch.interrupt.pending) {
  5353. kvm_x86_ops->set_irq(vcpu);
  5354. return 0;
  5355. }
  5356. if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
  5357. r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
  5358. if (r != 0)
  5359. return r;
  5360. }
  5361. /* try to inject new event if pending */
  5362. if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
  5363. vcpu->arch.smi_pending = false;
  5364. enter_smm(vcpu);
  5365. } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
  5366. --vcpu->arch.nmi_pending;
  5367. vcpu->arch.nmi_injected = true;
  5368. kvm_x86_ops->set_nmi(vcpu);
  5369. } else if (kvm_cpu_has_injectable_intr(vcpu)) {
  5370. /*
  5371. * Because interrupts can be injected asynchronously, we are
  5372. * calling check_nested_events again here to avoid a race condition.
  5373. * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
  5374. * proposal and current concerns. Perhaps we should be setting
  5375. * KVM_REQ_EVENT only on certain events and not unconditionally?
  5376. */
  5377. if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
  5378. r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
  5379. if (r != 0)
  5380. return r;
  5381. }
  5382. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  5383. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  5384. false);
  5385. kvm_x86_ops->set_irq(vcpu);
  5386. }
  5387. }
  5388. return 0;
  5389. }
  5390. static void process_nmi(struct kvm_vcpu *vcpu)
  5391. {
  5392. unsigned limit = 2;
  5393. /*
  5394. * x86 is limited to one NMI running, and one NMI pending after it.
  5395. * If an NMI is already in progress, limit further NMIs to just one.
  5396. * Otherwise, allow two (and we'll inject the first one immediately).
  5397. */
  5398. if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
  5399. limit = 1;
  5400. vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
  5401. vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
  5402. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5403. }
  5404. #define put_smstate(type, buf, offset, val) \
  5405. *(type *)((buf) + (offset) - 0x7e00) = val
  5406. static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
  5407. {
  5408. u32 flags = 0;
  5409. flags |= seg->g << 23;
  5410. flags |= seg->db << 22;
  5411. flags |= seg->l << 21;
  5412. flags |= seg->avl << 20;
  5413. flags |= seg->present << 15;
  5414. flags |= seg->dpl << 13;
  5415. flags |= seg->s << 12;
  5416. flags |= seg->type << 8;
  5417. return flags;
  5418. }
  5419. static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
  5420. {
  5421. struct kvm_segment seg;
  5422. int offset;
  5423. kvm_get_segment(vcpu, &seg, n);
  5424. put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
  5425. if (n < 3)
  5426. offset = 0x7f84 + n * 12;
  5427. else
  5428. offset = 0x7f2c + (n - 3) * 12;
  5429. put_smstate(u32, buf, offset + 8, seg.base);
  5430. put_smstate(u32, buf, offset + 4, seg.limit);
  5431. put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
  5432. }
  5433. #ifdef CONFIG_X86_64
  5434. static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
  5435. {
  5436. struct kvm_segment seg;
  5437. int offset;
  5438. u16 flags;
  5439. kvm_get_segment(vcpu, &seg, n);
  5440. offset = 0x7e00 + n * 16;
  5441. flags = enter_smm_get_segment_flags(&seg) >> 8;
  5442. put_smstate(u16, buf, offset, seg.selector);
  5443. put_smstate(u16, buf, offset + 2, flags);
  5444. put_smstate(u32, buf, offset + 4, seg.limit);
  5445. put_smstate(u64, buf, offset + 8, seg.base);
  5446. }
  5447. #endif
  5448. static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
  5449. {
  5450. struct desc_ptr dt;
  5451. struct kvm_segment seg;
  5452. unsigned long val;
  5453. int i;
  5454. put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
  5455. put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
  5456. put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
  5457. put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
  5458. for (i = 0; i < 8; i++)
  5459. put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
  5460. kvm_get_dr(vcpu, 6, &val);
  5461. put_smstate(u32, buf, 0x7fcc, (u32)val);
  5462. kvm_get_dr(vcpu, 7, &val);
  5463. put_smstate(u32, buf, 0x7fc8, (u32)val);
  5464. kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
  5465. put_smstate(u32, buf, 0x7fc4, seg.selector);
  5466. put_smstate(u32, buf, 0x7f64, seg.base);
  5467. put_smstate(u32, buf, 0x7f60, seg.limit);
  5468. put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
  5469. kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
  5470. put_smstate(u32, buf, 0x7fc0, seg.selector);
  5471. put_smstate(u32, buf, 0x7f80, seg.base);
  5472. put_smstate(u32, buf, 0x7f7c, seg.limit);
  5473. put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
  5474. kvm_x86_ops->get_gdt(vcpu, &dt);
  5475. put_smstate(u32, buf, 0x7f74, dt.address);
  5476. put_smstate(u32, buf, 0x7f70, dt.size);
  5477. kvm_x86_ops->get_idt(vcpu, &dt);
  5478. put_smstate(u32, buf, 0x7f58, dt.address);
  5479. put_smstate(u32, buf, 0x7f54, dt.size);
  5480. for (i = 0; i < 6; i++)
  5481. enter_smm_save_seg_32(vcpu, buf, i);
  5482. put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
  5483. /* revision id */
  5484. put_smstate(u32, buf, 0x7efc, 0x00020000);
  5485. put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
  5486. }
  5487. static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
  5488. {
  5489. #ifdef CONFIG_X86_64
  5490. struct desc_ptr dt;
  5491. struct kvm_segment seg;
  5492. unsigned long val;
  5493. int i;
  5494. for (i = 0; i < 16; i++)
  5495. put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
  5496. put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
  5497. put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
  5498. kvm_get_dr(vcpu, 6, &val);
  5499. put_smstate(u64, buf, 0x7f68, val);
  5500. kvm_get_dr(vcpu, 7, &val);
  5501. put_smstate(u64, buf, 0x7f60, val);
  5502. put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
  5503. put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
  5504. put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
  5505. put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
  5506. /* revision id */
  5507. put_smstate(u32, buf, 0x7efc, 0x00020064);
  5508. put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
  5509. kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
  5510. put_smstate(u16, buf, 0x7e90, seg.selector);
  5511. put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
  5512. put_smstate(u32, buf, 0x7e94, seg.limit);
  5513. put_smstate(u64, buf, 0x7e98, seg.base);
  5514. kvm_x86_ops->get_idt(vcpu, &dt);
  5515. put_smstate(u32, buf, 0x7e84, dt.size);
  5516. put_smstate(u64, buf, 0x7e88, dt.address);
  5517. kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
  5518. put_smstate(u16, buf, 0x7e70, seg.selector);
  5519. put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
  5520. put_smstate(u32, buf, 0x7e74, seg.limit);
  5521. put_smstate(u64, buf, 0x7e78, seg.base);
  5522. kvm_x86_ops->get_gdt(vcpu, &dt);
  5523. put_smstate(u32, buf, 0x7e64, dt.size);
  5524. put_smstate(u64, buf, 0x7e68, dt.address);
  5525. for (i = 0; i < 6; i++)
  5526. enter_smm_save_seg_64(vcpu, buf, i);
  5527. #else
  5528. WARN_ON_ONCE(1);
  5529. #endif
  5530. }
  5531. static void enter_smm(struct kvm_vcpu *vcpu)
  5532. {
  5533. struct kvm_segment cs, ds;
  5534. struct desc_ptr dt;
  5535. char buf[512];
  5536. u32 cr0;
  5537. trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
  5538. vcpu->arch.hflags |= HF_SMM_MASK;
  5539. memset(buf, 0, 512);
  5540. if (guest_cpuid_has_longmode(vcpu))
  5541. enter_smm_save_state_64(vcpu, buf);
  5542. else
  5543. enter_smm_save_state_32(vcpu, buf);
  5544. kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
  5545. if (kvm_x86_ops->get_nmi_mask(vcpu))
  5546. vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
  5547. else
  5548. kvm_x86_ops->set_nmi_mask(vcpu, true);
  5549. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5550. kvm_rip_write(vcpu, 0x8000);
  5551. cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
  5552. kvm_x86_ops->set_cr0(vcpu, cr0);
  5553. vcpu->arch.cr0 = cr0;
  5554. kvm_x86_ops->set_cr4(vcpu, 0);
  5555. /* Undocumented: IDT limit is set to zero on entry to SMM. */
  5556. dt.address = dt.size = 0;
  5557. kvm_x86_ops->set_idt(vcpu, &dt);
  5558. __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
  5559. cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
  5560. cs.base = vcpu->arch.smbase;
  5561. ds.selector = 0;
  5562. ds.base = 0;
  5563. cs.limit = ds.limit = 0xffffffff;
  5564. cs.type = ds.type = 0x3;
  5565. cs.dpl = ds.dpl = 0;
  5566. cs.db = ds.db = 0;
  5567. cs.s = ds.s = 1;
  5568. cs.l = ds.l = 0;
  5569. cs.g = ds.g = 1;
  5570. cs.avl = ds.avl = 0;
  5571. cs.present = ds.present = 1;
  5572. cs.unusable = ds.unusable = 0;
  5573. cs.padding = ds.padding = 0;
  5574. kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
  5575. kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
  5576. kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
  5577. kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
  5578. kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
  5579. kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
  5580. if (guest_cpuid_has_longmode(vcpu))
  5581. kvm_x86_ops->set_efer(vcpu, 0);
  5582. kvm_update_cpuid(vcpu);
  5583. kvm_mmu_reset_context(vcpu);
  5584. }
  5585. static void process_smi(struct kvm_vcpu *vcpu)
  5586. {
  5587. vcpu->arch.smi_pending = true;
  5588. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5589. }
  5590. void kvm_make_scan_ioapic_request(struct kvm *kvm)
  5591. {
  5592. kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
  5593. }
  5594. static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
  5595. {
  5596. u64 eoi_exit_bitmap[4];
  5597. if (!kvm_apic_hw_enabled(vcpu->arch.apic))
  5598. return;
  5599. bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
  5600. if (irqchip_split(vcpu->kvm))
  5601. kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
  5602. else {
  5603. if (vcpu->arch.apicv_active)
  5604. kvm_x86_ops->sync_pir_to_irr(vcpu);
  5605. kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
  5606. }
  5607. bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
  5608. vcpu_to_synic(vcpu)->vec_bitmap, 256);
  5609. kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
  5610. }
  5611. static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
  5612. {
  5613. ++vcpu->stat.tlb_flush;
  5614. kvm_x86_ops->tlb_flush(vcpu);
  5615. }
  5616. void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
  5617. unsigned long start, unsigned long end)
  5618. {
  5619. unsigned long apic_address;
  5620. /*
  5621. * The physical address of apic access page is stored in the VMCS.
  5622. * Update it when it becomes invalid.
  5623. */
  5624. apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5625. if (start <= apic_address && apic_address < end)
  5626. kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
  5627. }
  5628. void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
  5629. {
  5630. struct page *page = NULL;
  5631. if (!lapic_in_kernel(vcpu))
  5632. return;
  5633. if (!kvm_x86_ops->set_apic_access_page_addr)
  5634. return;
  5635. page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5636. if (is_error_page(page))
  5637. return;
  5638. kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
  5639. /*
  5640. * Do not pin apic access page in memory, the MMU notifier
  5641. * will call us again if it is migrated or swapped out.
  5642. */
  5643. put_page(page);
  5644. }
  5645. EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
  5646. void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
  5647. unsigned long address)
  5648. {
  5649. /*
  5650. * The physical address of apic access page is stored in the VMCS.
  5651. * Update it when it becomes invalid.
  5652. */
  5653. if (address == gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT))
  5654. kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
  5655. }
  5656. /*
  5657. * Returns 1 to let vcpu_run() continue the guest execution loop without
  5658. * exiting to the userspace. Otherwise, the value will be returned to the
  5659. * userspace.
  5660. */
  5661. static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  5662. {
  5663. int r;
  5664. bool req_int_win =
  5665. dm_request_for_irq_injection(vcpu) &&
  5666. kvm_cpu_accept_dm_intr(vcpu);
  5667. bool req_immediate_exit = false;
  5668. if (vcpu->requests) {
  5669. if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
  5670. kvm_mmu_unload(vcpu);
  5671. if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
  5672. __kvm_migrate_timers(vcpu);
  5673. if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
  5674. kvm_gen_update_masterclock(vcpu->kvm);
  5675. if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
  5676. kvm_gen_kvmclock_update(vcpu);
  5677. if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
  5678. r = kvm_guest_time_update(vcpu);
  5679. if (unlikely(r))
  5680. goto out;
  5681. }
  5682. if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
  5683. kvm_mmu_sync_roots(vcpu);
  5684. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  5685. kvm_vcpu_flush_tlb(vcpu);
  5686. if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
  5687. vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
  5688. r = 0;
  5689. goto out;
  5690. }
  5691. if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
  5692. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  5693. r = 0;
  5694. goto out;
  5695. }
  5696. if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
  5697. vcpu->fpu_active = 0;
  5698. kvm_x86_ops->fpu_deactivate(vcpu);
  5699. }
  5700. if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
  5701. /* Page is swapped out. Do synthetic halt */
  5702. vcpu->arch.apf.halted = true;
  5703. r = 1;
  5704. goto out;
  5705. }
  5706. if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
  5707. record_steal_time(vcpu);
  5708. if (kvm_check_request(KVM_REQ_SMI, vcpu))
  5709. process_smi(vcpu);
  5710. if (kvm_check_request(KVM_REQ_NMI, vcpu))
  5711. process_nmi(vcpu);
  5712. if (kvm_check_request(KVM_REQ_PMU, vcpu))
  5713. kvm_pmu_handle_event(vcpu);
  5714. if (kvm_check_request(KVM_REQ_PMI, vcpu))
  5715. kvm_pmu_deliver_pmi(vcpu);
  5716. if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
  5717. BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
  5718. if (test_bit(vcpu->arch.pending_ioapic_eoi,
  5719. vcpu->arch.ioapic_handled_vectors)) {
  5720. vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
  5721. vcpu->run->eoi.vector =
  5722. vcpu->arch.pending_ioapic_eoi;
  5723. r = 0;
  5724. goto out;
  5725. }
  5726. }
  5727. if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
  5728. vcpu_scan_ioapic(vcpu);
  5729. if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
  5730. kvm_vcpu_reload_apic_access_page(vcpu);
  5731. if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
  5732. vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
  5733. vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
  5734. r = 0;
  5735. goto out;
  5736. }
  5737. if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
  5738. vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
  5739. vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
  5740. r = 0;
  5741. goto out;
  5742. }
  5743. if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
  5744. vcpu->run->exit_reason = KVM_EXIT_HYPERV;
  5745. vcpu->run->hyperv = vcpu->arch.hyperv.exit;
  5746. r = 0;
  5747. goto out;
  5748. }
  5749. /*
  5750. * KVM_REQ_HV_STIMER has to be processed after
  5751. * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
  5752. * depend on the guest clock being up-to-date
  5753. */
  5754. if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
  5755. kvm_hv_process_stimers(vcpu);
  5756. }
  5757. /*
  5758. * KVM_REQ_EVENT is not set when posted interrupts are set by
  5759. * VT-d hardware, so we have to update RVI unconditionally.
  5760. */
  5761. if (kvm_lapic_enabled(vcpu)) {
  5762. /*
  5763. * Update architecture specific hints for APIC
  5764. * virtual interrupt delivery.
  5765. */
  5766. if (vcpu->arch.apicv_active)
  5767. kvm_x86_ops->hwapic_irr_update(vcpu,
  5768. kvm_lapic_find_highest_irr(vcpu));
  5769. }
  5770. if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
  5771. kvm_apic_accept_events(vcpu);
  5772. if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  5773. r = 1;
  5774. goto out;
  5775. }
  5776. if (inject_pending_event(vcpu, req_int_win) != 0)
  5777. req_immediate_exit = true;
  5778. else {
  5779. /* Enable NMI/IRQ window open exits if needed.
  5780. *
  5781. * SMIs have two cases: 1) they can be nested, and
  5782. * then there is nothing to do here because RSM will
  5783. * cause a vmexit anyway; 2) or the SMI can be pending
  5784. * because inject_pending_event has completed the
  5785. * injection of an IRQ or NMI from the previous vmexit,
  5786. * and then we request an immediate exit to inject the SMI.
  5787. */
  5788. if (vcpu->arch.smi_pending && !is_smm(vcpu))
  5789. req_immediate_exit = true;
  5790. if (vcpu->arch.nmi_pending)
  5791. kvm_x86_ops->enable_nmi_window(vcpu);
  5792. if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
  5793. kvm_x86_ops->enable_irq_window(vcpu);
  5794. }
  5795. if (kvm_lapic_enabled(vcpu)) {
  5796. update_cr8_intercept(vcpu);
  5797. kvm_lapic_sync_to_vapic(vcpu);
  5798. }
  5799. }
  5800. r = kvm_mmu_reload(vcpu);
  5801. if (unlikely(r)) {
  5802. goto cancel_injection;
  5803. }
  5804. preempt_disable();
  5805. kvm_x86_ops->prepare_guest_switch(vcpu);
  5806. if (vcpu->fpu_active)
  5807. kvm_load_guest_fpu(vcpu);
  5808. vcpu->mode = IN_GUEST_MODE;
  5809. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  5810. /*
  5811. * We should set ->mode before check ->requests,
  5812. * Please see the comment in kvm_make_all_cpus_request.
  5813. * This also orders the write to mode from any reads
  5814. * to the page tables done while the VCPU is running.
  5815. * Please see the comment in kvm_flush_remote_tlbs.
  5816. */
  5817. smp_mb__after_srcu_read_unlock();
  5818. local_irq_disable();
  5819. if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
  5820. || need_resched() || signal_pending(current)) {
  5821. vcpu->mode = OUTSIDE_GUEST_MODE;
  5822. smp_wmb();
  5823. local_irq_enable();
  5824. preempt_enable();
  5825. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  5826. r = 1;
  5827. goto cancel_injection;
  5828. }
  5829. kvm_load_guest_xcr0(vcpu);
  5830. if (req_immediate_exit) {
  5831. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5832. smp_send_reschedule(vcpu->cpu);
  5833. }
  5834. trace_kvm_entry(vcpu->vcpu_id);
  5835. wait_lapic_expire(vcpu);
  5836. guest_enter_irqoff();
  5837. if (unlikely(vcpu->arch.switch_db_regs)) {
  5838. set_debugreg(0, 7);
  5839. set_debugreg(vcpu->arch.eff_db[0], 0);
  5840. set_debugreg(vcpu->arch.eff_db[1], 1);
  5841. set_debugreg(vcpu->arch.eff_db[2], 2);
  5842. set_debugreg(vcpu->arch.eff_db[3], 3);
  5843. set_debugreg(vcpu->arch.dr6, 6);
  5844. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
  5845. }
  5846. kvm_x86_ops->run(vcpu);
  5847. /*
  5848. * Do this here before restoring debug registers on the host. And
  5849. * since we do this before handling the vmexit, a DR access vmexit
  5850. * can (a) read the correct value of the debug registers, (b) set
  5851. * KVM_DEBUGREG_WONT_EXIT again.
  5852. */
  5853. if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
  5854. WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
  5855. kvm_x86_ops->sync_dirty_debug_regs(vcpu);
  5856. kvm_update_dr0123(vcpu);
  5857. kvm_update_dr6(vcpu);
  5858. kvm_update_dr7(vcpu);
  5859. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
  5860. }
  5861. /*
  5862. * If the guest has used debug registers, at least dr7
  5863. * will be disabled while returning to the host.
  5864. * If we don't have active breakpoints in the host, we don't
  5865. * care about the messed up debug address registers. But if
  5866. * we have some of them active, restore the old state.
  5867. */
  5868. if (hw_breakpoint_active())
  5869. hw_breakpoint_restore();
  5870. vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  5871. vcpu->mode = OUTSIDE_GUEST_MODE;
  5872. smp_wmb();
  5873. kvm_put_guest_xcr0(vcpu);
  5874. kvm_x86_ops->handle_external_intr(vcpu);
  5875. ++vcpu->stat.exits;
  5876. guest_exit_irqoff();
  5877. local_irq_enable();
  5878. preempt_enable();
  5879. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  5880. /*
  5881. * Profile KVM exit RIPs:
  5882. */
  5883. if (unlikely(prof_on == KVM_PROFILING)) {
  5884. unsigned long rip = kvm_rip_read(vcpu);
  5885. profile_hit(KVM_PROFILING, (void *)rip);
  5886. }
  5887. if (unlikely(vcpu->arch.tsc_always_catchup))
  5888. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  5889. if (vcpu->arch.apic_attention)
  5890. kvm_lapic_sync_from_vapic(vcpu);
  5891. r = kvm_x86_ops->handle_exit(vcpu);
  5892. return r;
  5893. cancel_injection:
  5894. kvm_x86_ops->cancel_injection(vcpu);
  5895. if (unlikely(vcpu->arch.apic_attention))
  5896. kvm_lapic_sync_from_vapic(vcpu);
  5897. out:
  5898. return r;
  5899. }
  5900. static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
  5901. {
  5902. if (!kvm_arch_vcpu_runnable(vcpu) &&
  5903. (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
  5904. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5905. kvm_vcpu_block(vcpu);
  5906. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  5907. if (kvm_x86_ops->post_block)
  5908. kvm_x86_ops->post_block(vcpu);
  5909. if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
  5910. return 1;
  5911. }
  5912. kvm_apic_accept_events(vcpu);
  5913. switch(vcpu->arch.mp_state) {
  5914. case KVM_MP_STATE_HALTED:
  5915. vcpu->arch.pv.pv_unhalted = false;
  5916. vcpu->arch.mp_state =
  5917. KVM_MP_STATE_RUNNABLE;
  5918. case KVM_MP_STATE_RUNNABLE:
  5919. vcpu->arch.apf.halted = false;
  5920. break;
  5921. case KVM_MP_STATE_INIT_RECEIVED:
  5922. break;
  5923. default:
  5924. return -EINTR;
  5925. break;
  5926. }
  5927. return 1;
  5928. }
  5929. static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
  5930. {
  5931. return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  5932. !vcpu->arch.apf.halted);
  5933. }
  5934. static int vcpu_run(struct kvm_vcpu *vcpu)
  5935. {
  5936. int r;
  5937. struct kvm *kvm = vcpu->kvm;
  5938. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  5939. for (;;) {
  5940. if (kvm_vcpu_running(vcpu)) {
  5941. r = vcpu_enter_guest(vcpu);
  5942. } else {
  5943. r = vcpu_block(kvm, vcpu);
  5944. }
  5945. if (r <= 0)
  5946. break;
  5947. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  5948. if (kvm_cpu_has_pending_timer(vcpu))
  5949. kvm_inject_pending_timer_irqs(vcpu);
  5950. if (dm_request_for_irq_injection(vcpu) &&
  5951. kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
  5952. r = 0;
  5953. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  5954. ++vcpu->stat.request_irq_exits;
  5955. break;
  5956. }
  5957. kvm_check_async_pf_completion(vcpu);
  5958. if (signal_pending(current)) {
  5959. r = -EINTR;
  5960. vcpu->run->exit_reason = KVM_EXIT_INTR;
  5961. ++vcpu->stat.signal_exits;
  5962. break;
  5963. }
  5964. if (need_resched()) {
  5965. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5966. cond_resched();
  5967. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  5968. }
  5969. }
  5970. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5971. return r;
  5972. }
  5973. static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
  5974. {
  5975. int r;
  5976. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  5977. r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
  5978. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  5979. if (r != EMULATE_DONE)
  5980. return 0;
  5981. return 1;
  5982. }
  5983. static int complete_emulated_pio(struct kvm_vcpu *vcpu)
  5984. {
  5985. BUG_ON(!vcpu->arch.pio.count);
  5986. return complete_emulated_io(vcpu);
  5987. }
  5988. /*
  5989. * Implements the following, as a state machine:
  5990. *
  5991. * read:
  5992. * for each fragment
  5993. * for each mmio piece in the fragment
  5994. * write gpa, len
  5995. * exit
  5996. * copy data
  5997. * execute insn
  5998. *
  5999. * write:
  6000. * for each fragment
  6001. * for each mmio piece in the fragment
  6002. * write gpa, len
  6003. * copy data
  6004. * exit
  6005. */
  6006. static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
  6007. {
  6008. struct kvm_run *run = vcpu->run;
  6009. struct kvm_mmio_fragment *frag;
  6010. unsigned len;
  6011. BUG_ON(!vcpu->mmio_needed);
  6012. /* Complete previous fragment */
  6013. frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
  6014. len = min(8u, frag->len);
  6015. if (!vcpu->mmio_is_write)
  6016. memcpy(frag->data, run->mmio.data, len);
  6017. if (frag->len <= 8) {
  6018. /* Switch to the next fragment. */
  6019. frag++;
  6020. vcpu->mmio_cur_fragment++;
  6021. } else {
  6022. /* Go forward to the next mmio piece. */
  6023. frag->data += len;
  6024. frag->gpa += len;
  6025. frag->len -= len;
  6026. }
  6027. if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
  6028. vcpu->mmio_needed = 0;
  6029. /* FIXME: return into emulator if single-stepping. */
  6030. if (vcpu->mmio_is_write)
  6031. return 1;
  6032. vcpu->mmio_read_completed = 1;
  6033. return complete_emulated_io(vcpu);
  6034. }
  6035. run->exit_reason = KVM_EXIT_MMIO;
  6036. run->mmio.phys_addr = frag->gpa;
  6037. if (vcpu->mmio_is_write)
  6038. memcpy(run->mmio.data, frag->data, min(8u, frag->len));
  6039. run->mmio.len = min(8u, frag->len);
  6040. run->mmio.is_write = vcpu->mmio_is_write;
  6041. vcpu->arch.complete_userspace_io = complete_emulated_mmio;
  6042. return 0;
  6043. }
  6044. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  6045. {
  6046. struct fpu *fpu = &current->thread.fpu;
  6047. int r;
  6048. sigset_t sigsaved;
  6049. fpu__activate_curr(fpu);
  6050. if (vcpu->sigset_active)
  6051. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  6052. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  6053. kvm_vcpu_block(vcpu);
  6054. kvm_apic_accept_events(vcpu);
  6055. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  6056. r = -EAGAIN;
  6057. goto out;
  6058. }
  6059. /* re-sync apic's tpr */
  6060. if (!lapic_in_kernel(vcpu)) {
  6061. if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
  6062. r = -EINVAL;
  6063. goto out;
  6064. }
  6065. }
  6066. if (unlikely(vcpu->arch.complete_userspace_io)) {
  6067. int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
  6068. vcpu->arch.complete_userspace_io = NULL;
  6069. r = cui(vcpu);
  6070. if (r <= 0)
  6071. goto out;
  6072. } else
  6073. WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
  6074. r = vcpu_run(vcpu);
  6075. out:
  6076. post_kvm_run_save(vcpu);
  6077. if (vcpu->sigset_active)
  6078. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  6079. return r;
  6080. }
  6081. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  6082. {
  6083. if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
  6084. /*
  6085. * We are here if userspace calls get_regs() in the middle of
  6086. * instruction emulation. Registers state needs to be copied
  6087. * back from emulation context to vcpu. Userspace shouldn't do
  6088. * that usually, but some bad designed PV devices (vmware
  6089. * backdoor interface) need this to work
  6090. */
  6091. emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
  6092. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  6093. }
  6094. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  6095. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  6096. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6097. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  6098. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  6099. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  6100. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  6101. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  6102. #ifdef CONFIG_X86_64
  6103. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  6104. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  6105. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  6106. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  6107. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  6108. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  6109. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  6110. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  6111. #endif
  6112. regs->rip = kvm_rip_read(vcpu);
  6113. regs->rflags = kvm_get_rflags(vcpu);
  6114. return 0;
  6115. }
  6116. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  6117. {
  6118. vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
  6119. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  6120. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  6121. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  6122. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  6123. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  6124. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  6125. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  6126. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  6127. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  6128. #ifdef CONFIG_X86_64
  6129. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  6130. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  6131. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  6132. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  6133. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  6134. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  6135. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  6136. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  6137. #endif
  6138. kvm_rip_write(vcpu, regs->rip);
  6139. kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
  6140. vcpu->arch.exception.pending = false;
  6141. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6142. return 0;
  6143. }
  6144. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  6145. {
  6146. struct kvm_segment cs;
  6147. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  6148. *db = cs.db;
  6149. *l = cs.l;
  6150. }
  6151. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  6152. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  6153. struct kvm_sregs *sregs)
  6154. {
  6155. struct desc_ptr dt;
  6156. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  6157. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  6158. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  6159. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  6160. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  6161. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  6162. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  6163. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  6164. kvm_x86_ops->get_idt(vcpu, &dt);
  6165. sregs->idt.limit = dt.size;
  6166. sregs->idt.base = dt.address;
  6167. kvm_x86_ops->get_gdt(vcpu, &dt);
  6168. sregs->gdt.limit = dt.size;
  6169. sregs->gdt.base = dt.address;
  6170. sregs->cr0 = kvm_read_cr0(vcpu);
  6171. sregs->cr2 = vcpu->arch.cr2;
  6172. sregs->cr3 = kvm_read_cr3(vcpu);
  6173. sregs->cr4 = kvm_read_cr4(vcpu);
  6174. sregs->cr8 = kvm_get_cr8(vcpu);
  6175. sregs->efer = vcpu->arch.efer;
  6176. sregs->apic_base = kvm_get_apic_base(vcpu);
  6177. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  6178. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  6179. set_bit(vcpu->arch.interrupt.nr,
  6180. (unsigned long *)sregs->interrupt_bitmap);
  6181. return 0;
  6182. }
  6183. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  6184. struct kvm_mp_state *mp_state)
  6185. {
  6186. kvm_apic_accept_events(vcpu);
  6187. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
  6188. vcpu->arch.pv.pv_unhalted)
  6189. mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
  6190. else
  6191. mp_state->mp_state = vcpu->arch.mp_state;
  6192. return 0;
  6193. }
  6194. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  6195. struct kvm_mp_state *mp_state)
  6196. {
  6197. if (!lapic_in_kernel(vcpu) &&
  6198. mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
  6199. return -EINVAL;
  6200. /* INITs are latched while in SMM */
  6201. if ((is_smm(vcpu) || vcpu->arch.smi_pending) &&
  6202. (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
  6203. mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
  6204. return -EINVAL;
  6205. if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
  6206. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  6207. set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
  6208. } else
  6209. vcpu->arch.mp_state = mp_state->mp_state;
  6210. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6211. return 0;
  6212. }
  6213. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
  6214. int reason, bool has_error_code, u32 error_code)
  6215. {
  6216. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  6217. int ret;
  6218. init_emulate_ctxt(vcpu);
  6219. ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
  6220. has_error_code, error_code);
  6221. if (ret)
  6222. return EMULATE_FAIL;
  6223. kvm_rip_write(vcpu, ctxt->eip);
  6224. kvm_set_rflags(vcpu, ctxt->eflags);
  6225. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6226. return EMULATE_DONE;
  6227. }
  6228. EXPORT_SYMBOL_GPL(kvm_task_switch);
  6229. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  6230. struct kvm_sregs *sregs)
  6231. {
  6232. struct msr_data apic_base_msr;
  6233. int mmu_reset_needed = 0;
  6234. int cpuid_update_needed = 0;
  6235. int pending_vec, max_bits, idx;
  6236. struct desc_ptr dt;
  6237. if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
  6238. return -EINVAL;
  6239. dt.size = sregs->idt.limit;
  6240. dt.address = sregs->idt.base;
  6241. kvm_x86_ops->set_idt(vcpu, &dt);
  6242. dt.size = sregs->gdt.limit;
  6243. dt.address = sregs->gdt.base;
  6244. kvm_x86_ops->set_gdt(vcpu, &dt);
  6245. vcpu->arch.cr2 = sregs->cr2;
  6246. mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
  6247. vcpu->arch.cr3 = sregs->cr3;
  6248. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  6249. kvm_set_cr8(vcpu, sregs->cr8);
  6250. mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
  6251. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  6252. apic_base_msr.data = sregs->apic_base;
  6253. apic_base_msr.host_initiated = true;
  6254. kvm_set_apic_base(vcpu, &apic_base_msr);
  6255. mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
  6256. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  6257. vcpu->arch.cr0 = sregs->cr0;
  6258. mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
  6259. cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
  6260. (X86_CR4_OSXSAVE | X86_CR4_PKE));
  6261. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  6262. if (cpuid_update_needed)
  6263. kvm_update_cpuid(vcpu);
  6264. idx = srcu_read_lock(&vcpu->kvm->srcu);
  6265. if (!is_long_mode(vcpu) && is_pae(vcpu)) {
  6266. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  6267. mmu_reset_needed = 1;
  6268. }
  6269. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  6270. if (mmu_reset_needed)
  6271. kvm_mmu_reset_context(vcpu);
  6272. max_bits = KVM_NR_INTERRUPTS;
  6273. pending_vec = find_first_bit(
  6274. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  6275. if (pending_vec < max_bits) {
  6276. kvm_queue_interrupt(vcpu, pending_vec, false);
  6277. pr_debug("Set back pending irq %d\n", pending_vec);
  6278. }
  6279. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  6280. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  6281. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  6282. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  6283. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  6284. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  6285. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  6286. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  6287. update_cr8_intercept(vcpu);
  6288. /* Older userspace won't unhalt the vcpu on reset. */
  6289. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  6290. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  6291. !is_protmode(vcpu))
  6292. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  6293. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6294. return 0;
  6295. }
  6296. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  6297. struct kvm_guest_debug *dbg)
  6298. {
  6299. unsigned long rflags;
  6300. int i, r;
  6301. if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
  6302. r = -EBUSY;
  6303. if (vcpu->arch.exception.pending)
  6304. goto out;
  6305. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  6306. kvm_queue_exception(vcpu, DB_VECTOR);
  6307. else
  6308. kvm_queue_exception(vcpu, BP_VECTOR);
  6309. }
  6310. /*
  6311. * Read rflags as long as potentially injected trace flags are still
  6312. * filtered out.
  6313. */
  6314. rflags = kvm_get_rflags(vcpu);
  6315. vcpu->guest_debug = dbg->control;
  6316. if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
  6317. vcpu->guest_debug = 0;
  6318. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6319. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  6320. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  6321. vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
  6322. } else {
  6323. for (i = 0; i < KVM_NR_DB_REGS; i++)
  6324. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  6325. }
  6326. kvm_update_dr7(vcpu);
  6327. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  6328. vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
  6329. get_segment_base(vcpu, VCPU_SREG_CS);
  6330. /*
  6331. * Trigger an rflags update that will inject or remove the trace
  6332. * flags.
  6333. */
  6334. kvm_set_rflags(vcpu, rflags);
  6335. kvm_x86_ops->update_bp_intercept(vcpu);
  6336. r = 0;
  6337. out:
  6338. return r;
  6339. }
  6340. /*
  6341. * Translate a guest virtual address to a guest physical address.
  6342. */
  6343. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  6344. struct kvm_translation *tr)
  6345. {
  6346. unsigned long vaddr = tr->linear_address;
  6347. gpa_t gpa;
  6348. int idx;
  6349. idx = srcu_read_lock(&vcpu->kvm->srcu);
  6350. gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
  6351. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  6352. tr->physical_address = gpa;
  6353. tr->valid = gpa != UNMAPPED_GVA;
  6354. tr->writeable = 1;
  6355. tr->usermode = 0;
  6356. return 0;
  6357. }
  6358. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  6359. {
  6360. struct fxregs_state *fxsave =
  6361. &vcpu->arch.guest_fpu.state.fxsave;
  6362. memcpy(fpu->fpr, fxsave->st_space, 128);
  6363. fpu->fcw = fxsave->cwd;
  6364. fpu->fsw = fxsave->swd;
  6365. fpu->ftwx = fxsave->twd;
  6366. fpu->last_opcode = fxsave->fop;
  6367. fpu->last_ip = fxsave->rip;
  6368. fpu->last_dp = fxsave->rdp;
  6369. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  6370. return 0;
  6371. }
  6372. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  6373. {
  6374. struct fxregs_state *fxsave =
  6375. &vcpu->arch.guest_fpu.state.fxsave;
  6376. memcpy(fxsave->st_space, fpu->fpr, 128);
  6377. fxsave->cwd = fpu->fcw;
  6378. fxsave->swd = fpu->fsw;
  6379. fxsave->twd = fpu->ftwx;
  6380. fxsave->fop = fpu->last_opcode;
  6381. fxsave->rip = fpu->last_ip;
  6382. fxsave->rdp = fpu->last_dp;
  6383. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  6384. return 0;
  6385. }
  6386. static void fx_init(struct kvm_vcpu *vcpu)
  6387. {
  6388. fpstate_init(&vcpu->arch.guest_fpu.state);
  6389. if (boot_cpu_has(X86_FEATURE_XSAVES))
  6390. vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv =
  6391. host_xcr0 | XSTATE_COMPACTION_ENABLED;
  6392. /*
  6393. * Ensure guest xcr0 is valid for loading
  6394. */
  6395. vcpu->arch.xcr0 = XFEATURE_MASK_FP;
  6396. vcpu->arch.cr0 |= X86_CR0_ET;
  6397. }
  6398. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  6399. {
  6400. if (vcpu->guest_fpu_loaded)
  6401. return;
  6402. /*
  6403. * Restore all possible states in the guest,
  6404. * and assume host would use all available bits.
  6405. * Guest xcr0 would be loaded later.
  6406. */
  6407. vcpu->guest_fpu_loaded = 1;
  6408. __kernel_fpu_begin();
  6409. __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state);
  6410. trace_kvm_fpu(1);
  6411. }
  6412. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  6413. {
  6414. if (!vcpu->guest_fpu_loaded) {
  6415. vcpu->fpu_counter = 0;
  6416. return;
  6417. }
  6418. vcpu->guest_fpu_loaded = 0;
  6419. copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
  6420. __kernel_fpu_end();
  6421. ++vcpu->stat.fpu_reload;
  6422. /*
  6423. * If using eager FPU mode, or if the guest is a frequent user
  6424. * of the FPU, just leave the FPU active for next time.
  6425. * Every 255 times fpu_counter rolls over to 0; a guest that uses
  6426. * the FPU in bursts will revert to loading it on demand.
  6427. */
  6428. if (!use_eager_fpu()) {
  6429. if (++vcpu->fpu_counter < 5)
  6430. kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
  6431. }
  6432. trace_kvm_fpu(0);
  6433. }
  6434. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  6435. {
  6436. void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask;
  6437. kvmclock_reset(vcpu);
  6438. kvm_x86_ops->vcpu_free(vcpu);
  6439. free_cpumask_var(wbinvd_dirty_mask);
  6440. }
  6441. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  6442. unsigned int id)
  6443. {
  6444. struct kvm_vcpu *vcpu;
  6445. if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
  6446. printk_once(KERN_WARNING
  6447. "kvm: SMP vm created on host with unstable TSC; "
  6448. "guest TSC will not be reliable\n");
  6449. vcpu = kvm_x86_ops->vcpu_create(kvm, id);
  6450. return vcpu;
  6451. }
  6452. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  6453. {
  6454. int r;
  6455. kvm_vcpu_mtrr_init(vcpu);
  6456. r = vcpu_load(vcpu);
  6457. if (r)
  6458. return r;
  6459. kvm_vcpu_reset(vcpu, false);
  6460. kvm_mmu_setup(vcpu);
  6461. vcpu_put(vcpu);
  6462. return r;
  6463. }
  6464. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  6465. {
  6466. struct msr_data msr;
  6467. struct kvm *kvm = vcpu->kvm;
  6468. if (vcpu_load(vcpu))
  6469. return;
  6470. msr.data = 0x0;
  6471. msr.index = MSR_IA32_TSC;
  6472. msr.host_initiated = true;
  6473. kvm_write_tsc(vcpu, &msr);
  6474. vcpu_put(vcpu);
  6475. if (!kvmclock_periodic_sync)
  6476. return;
  6477. schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
  6478. KVMCLOCK_SYNC_PERIOD);
  6479. }
  6480. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  6481. {
  6482. int r;
  6483. vcpu->arch.apf.msr_val = 0;
  6484. r = vcpu_load(vcpu);
  6485. BUG_ON(r);
  6486. kvm_mmu_unload(vcpu);
  6487. vcpu_put(vcpu);
  6488. kvm_x86_ops->vcpu_free(vcpu);
  6489. }
  6490. void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  6491. {
  6492. vcpu->arch.hflags = 0;
  6493. vcpu->arch.smi_pending = 0;
  6494. atomic_set(&vcpu->arch.nmi_queued, 0);
  6495. vcpu->arch.nmi_pending = 0;
  6496. vcpu->arch.nmi_injected = false;
  6497. kvm_clear_interrupt_queue(vcpu);
  6498. kvm_clear_exception_queue(vcpu);
  6499. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  6500. kvm_update_dr0123(vcpu);
  6501. vcpu->arch.dr6 = DR6_INIT;
  6502. kvm_update_dr6(vcpu);
  6503. vcpu->arch.dr7 = DR7_FIXED_1;
  6504. kvm_update_dr7(vcpu);
  6505. vcpu->arch.cr2 = 0;
  6506. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6507. vcpu->arch.apf.msr_val = 0;
  6508. vcpu->arch.st.msr_val = 0;
  6509. kvmclock_reset(vcpu);
  6510. kvm_clear_async_pf_completion_queue(vcpu);
  6511. kvm_async_pf_hash_reset(vcpu);
  6512. vcpu->arch.apf.halted = false;
  6513. if (!init_event) {
  6514. kvm_pmu_reset(vcpu);
  6515. vcpu->arch.smbase = 0x30000;
  6516. }
  6517. memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
  6518. vcpu->arch.regs_avail = ~0;
  6519. vcpu->arch.regs_dirty = ~0;
  6520. kvm_x86_ops->vcpu_reset(vcpu, init_event);
  6521. }
  6522. void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
  6523. {
  6524. struct kvm_segment cs;
  6525. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  6526. cs.selector = vector << 8;
  6527. cs.base = vector << 12;
  6528. kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
  6529. kvm_rip_write(vcpu, 0);
  6530. }
  6531. int kvm_arch_hardware_enable(void)
  6532. {
  6533. struct kvm *kvm;
  6534. struct kvm_vcpu *vcpu;
  6535. int i;
  6536. int ret;
  6537. u64 local_tsc;
  6538. u64 max_tsc = 0;
  6539. bool stable, backwards_tsc = false;
  6540. kvm_shared_msr_cpu_online();
  6541. ret = kvm_x86_ops->hardware_enable();
  6542. if (ret != 0)
  6543. return ret;
  6544. local_tsc = rdtsc();
  6545. stable = !check_tsc_unstable();
  6546. list_for_each_entry(kvm, &vm_list, vm_list) {
  6547. kvm_for_each_vcpu(i, vcpu, kvm) {
  6548. if (!stable && vcpu->cpu == smp_processor_id())
  6549. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  6550. if (stable && vcpu->arch.last_host_tsc > local_tsc) {
  6551. backwards_tsc = true;
  6552. if (vcpu->arch.last_host_tsc > max_tsc)
  6553. max_tsc = vcpu->arch.last_host_tsc;
  6554. }
  6555. }
  6556. }
  6557. /*
  6558. * Sometimes, even reliable TSCs go backwards. This happens on
  6559. * platforms that reset TSC during suspend or hibernate actions, but
  6560. * maintain synchronization. We must compensate. Fortunately, we can
  6561. * detect that condition here, which happens early in CPU bringup,
  6562. * before any KVM threads can be running. Unfortunately, we can't
  6563. * bring the TSCs fully up to date with real time, as we aren't yet far
  6564. * enough into CPU bringup that we know how much real time has actually
  6565. * elapsed; our helper function, ktime_get_boot_ns() will be using boot
  6566. * variables that haven't been updated yet.
  6567. *
  6568. * So we simply find the maximum observed TSC above, then record the
  6569. * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
  6570. * the adjustment will be applied. Note that we accumulate
  6571. * adjustments, in case multiple suspend cycles happen before some VCPU
  6572. * gets a chance to run again. In the event that no KVM threads get a
  6573. * chance to run, we will miss the entire elapsed period, as we'll have
  6574. * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
  6575. * loose cycle time. This isn't too big a deal, since the loss will be
  6576. * uniform across all VCPUs (not to mention the scenario is extremely
  6577. * unlikely). It is possible that a second hibernate recovery happens
  6578. * much faster than a first, causing the observed TSC here to be
  6579. * smaller; this would require additional padding adjustment, which is
  6580. * why we set last_host_tsc to the local tsc observed here.
  6581. *
  6582. * N.B. - this code below runs only on platforms with reliable TSC,
  6583. * as that is the only way backwards_tsc is set above. Also note
  6584. * that this runs for ALL vcpus, which is not a bug; all VCPUs should
  6585. * have the same delta_cyc adjustment applied if backwards_tsc
  6586. * is detected. Note further, this adjustment is only done once,
  6587. * as we reset last_host_tsc on all VCPUs to stop this from being
  6588. * called multiple times (one for each physical CPU bringup).
  6589. *
  6590. * Platforms with unreliable TSCs don't have to deal with this, they
  6591. * will be compensated by the logic in vcpu_load, which sets the TSC to
  6592. * catchup mode. This will catchup all VCPUs to real time, but cannot
  6593. * guarantee that they stay in perfect synchronization.
  6594. */
  6595. if (backwards_tsc) {
  6596. u64 delta_cyc = max_tsc - local_tsc;
  6597. backwards_tsc_observed = true;
  6598. list_for_each_entry(kvm, &vm_list, vm_list) {
  6599. kvm_for_each_vcpu(i, vcpu, kvm) {
  6600. vcpu->arch.tsc_offset_adjustment += delta_cyc;
  6601. vcpu->arch.last_host_tsc = local_tsc;
  6602. kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
  6603. }
  6604. /*
  6605. * We have to disable TSC offset matching.. if you were
  6606. * booting a VM while issuing an S4 host suspend....
  6607. * you may have some problem. Solving this issue is
  6608. * left as an exercise to the reader.
  6609. */
  6610. kvm->arch.last_tsc_nsec = 0;
  6611. kvm->arch.last_tsc_write = 0;
  6612. }
  6613. }
  6614. return 0;
  6615. }
  6616. void kvm_arch_hardware_disable(void)
  6617. {
  6618. kvm_x86_ops->hardware_disable();
  6619. drop_user_return_notifiers();
  6620. }
  6621. int kvm_arch_hardware_setup(void)
  6622. {
  6623. int r;
  6624. r = kvm_x86_ops->hardware_setup();
  6625. if (r != 0)
  6626. return r;
  6627. if (kvm_has_tsc_control) {
  6628. /*
  6629. * Make sure the user can only configure tsc_khz values that
  6630. * fit into a signed integer.
  6631. * A min value is not calculated needed because it will always
  6632. * be 1 on all machines.
  6633. */
  6634. u64 max = min(0x7fffffffULL,
  6635. __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
  6636. kvm_max_guest_tsc_khz = max;
  6637. kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
  6638. }
  6639. kvm_init_msr_list();
  6640. return 0;
  6641. }
  6642. void kvm_arch_hardware_unsetup(void)
  6643. {
  6644. kvm_x86_ops->hardware_unsetup();
  6645. }
  6646. void kvm_arch_check_processor_compat(void *rtn)
  6647. {
  6648. kvm_x86_ops->check_processor_compatibility(rtn);
  6649. }
  6650. bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
  6651. {
  6652. return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
  6653. }
  6654. EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
  6655. bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
  6656. {
  6657. return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
  6658. }
  6659. struct static_key kvm_no_apic_vcpu __read_mostly;
  6660. EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
  6661. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  6662. {
  6663. struct page *page;
  6664. struct kvm *kvm;
  6665. int r;
  6666. BUG_ON(vcpu->kvm == NULL);
  6667. kvm = vcpu->kvm;
  6668. vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv();
  6669. vcpu->arch.pv.pv_unhalted = false;
  6670. vcpu->arch.emulate_ctxt.ops = &emulate_ops;
  6671. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_reset_bsp(vcpu))
  6672. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  6673. else
  6674. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  6675. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  6676. if (!page) {
  6677. r = -ENOMEM;
  6678. goto fail;
  6679. }
  6680. vcpu->arch.pio_data = page_address(page);
  6681. kvm_set_tsc_khz(vcpu, max_tsc_khz);
  6682. r = kvm_mmu_create(vcpu);
  6683. if (r < 0)
  6684. goto fail_free_pio_data;
  6685. if (irqchip_in_kernel(kvm)) {
  6686. r = kvm_create_lapic(vcpu);
  6687. if (r < 0)
  6688. goto fail_mmu_destroy;
  6689. } else
  6690. static_key_slow_inc(&kvm_no_apic_vcpu);
  6691. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  6692. GFP_KERNEL);
  6693. if (!vcpu->arch.mce_banks) {
  6694. r = -ENOMEM;
  6695. goto fail_free_lapic;
  6696. }
  6697. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  6698. if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
  6699. r = -ENOMEM;
  6700. goto fail_free_mce_banks;
  6701. }
  6702. fx_init(vcpu);
  6703. vcpu->arch.ia32_tsc_adjust_msr = 0x0;
  6704. vcpu->arch.pv_time_enabled = false;
  6705. vcpu->arch.guest_supported_xcr0 = 0;
  6706. vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
  6707. vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
  6708. vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
  6709. kvm_async_pf_hash_reset(vcpu);
  6710. kvm_pmu_init(vcpu);
  6711. vcpu->arch.pending_external_vector = -1;
  6712. kvm_hv_vcpu_init(vcpu);
  6713. return 0;
  6714. fail_free_mce_banks:
  6715. kfree(vcpu->arch.mce_banks);
  6716. fail_free_lapic:
  6717. kvm_free_lapic(vcpu);
  6718. fail_mmu_destroy:
  6719. kvm_mmu_destroy(vcpu);
  6720. fail_free_pio_data:
  6721. free_page((unsigned long)vcpu->arch.pio_data);
  6722. fail:
  6723. return r;
  6724. }
  6725. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  6726. {
  6727. int idx;
  6728. kvm_hv_vcpu_uninit(vcpu);
  6729. kvm_pmu_destroy(vcpu);
  6730. kfree(vcpu->arch.mce_banks);
  6731. kvm_free_lapic(vcpu);
  6732. idx = srcu_read_lock(&vcpu->kvm->srcu);
  6733. kvm_mmu_destroy(vcpu);
  6734. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  6735. free_page((unsigned long)vcpu->arch.pio_data);
  6736. if (!lapic_in_kernel(vcpu))
  6737. static_key_slow_dec(&kvm_no_apic_vcpu);
  6738. }
  6739. void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
  6740. {
  6741. kvm_x86_ops->sched_in(vcpu, cpu);
  6742. }
  6743. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  6744. {
  6745. if (type)
  6746. return -EINVAL;
  6747. INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
  6748. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  6749. INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
  6750. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  6751. atomic_set(&kvm->arch.noncoherent_dma_count, 0);
  6752. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  6753. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  6754. /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
  6755. set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  6756. &kvm->arch.irq_sources_bitmap);
  6757. raw_spin_lock_init(&kvm->arch.tsc_write_lock);
  6758. mutex_init(&kvm->arch.apic_map_lock);
  6759. spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
  6760. kvm->arch.kvmclock_offset = -ktime_get_boot_ns();
  6761. pvclock_update_vm_gtod_copy(kvm);
  6762. INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
  6763. INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
  6764. kvm_page_track_init(kvm);
  6765. kvm_mmu_init_vm(kvm);
  6766. if (kvm_x86_ops->vm_init)
  6767. return kvm_x86_ops->vm_init(kvm);
  6768. return 0;
  6769. }
  6770. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  6771. {
  6772. int r;
  6773. r = vcpu_load(vcpu);
  6774. BUG_ON(r);
  6775. kvm_mmu_unload(vcpu);
  6776. vcpu_put(vcpu);
  6777. }
  6778. static void kvm_free_vcpus(struct kvm *kvm)
  6779. {
  6780. unsigned int i;
  6781. struct kvm_vcpu *vcpu;
  6782. /*
  6783. * Unpin any mmu pages first.
  6784. */
  6785. kvm_for_each_vcpu(i, vcpu, kvm) {
  6786. kvm_clear_async_pf_completion_queue(vcpu);
  6787. kvm_unload_vcpu_mmu(vcpu);
  6788. }
  6789. kvm_for_each_vcpu(i, vcpu, kvm)
  6790. kvm_arch_vcpu_free(vcpu);
  6791. mutex_lock(&kvm->lock);
  6792. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  6793. kvm->vcpus[i] = NULL;
  6794. atomic_set(&kvm->online_vcpus, 0);
  6795. mutex_unlock(&kvm->lock);
  6796. }
  6797. void kvm_arch_sync_events(struct kvm *kvm)
  6798. {
  6799. cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
  6800. cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
  6801. kvm_free_all_assigned_devices(kvm);
  6802. kvm_free_pit(kvm);
  6803. }
  6804. int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
  6805. {
  6806. int i, r;
  6807. unsigned long hva;
  6808. struct kvm_memslots *slots = kvm_memslots(kvm);
  6809. struct kvm_memory_slot *slot, old;
  6810. /* Called with kvm->slots_lock held. */
  6811. if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
  6812. return -EINVAL;
  6813. slot = id_to_memslot(slots, id);
  6814. if (size) {
  6815. if (slot->npages)
  6816. return -EEXIST;
  6817. /*
  6818. * MAP_SHARED to prevent internal slot pages from being moved
  6819. * by fork()/COW.
  6820. */
  6821. hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
  6822. MAP_SHARED | MAP_ANONYMOUS, 0);
  6823. if (IS_ERR((void *)hva))
  6824. return PTR_ERR((void *)hva);
  6825. } else {
  6826. if (!slot->npages)
  6827. return 0;
  6828. hva = 0;
  6829. }
  6830. old = *slot;
  6831. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  6832. struct kvm_userspace_memory_region m;
  6833. m.slot = id | (i << 16);
  6834. m.flags = 0;
  6835. m.guest_phys_addr = gpa;
  6836. m.userspace_addr = hva;
  6837. m.memory_size = size;
  6838. r = __kvm_set_memory_region(kvm, &m);
  6839. if (r < 0)
  6840. return r;
  6841. }
  6842. if (!size) {
  6843. r = vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
  6844. WARN_ON(r < 0);
  6845. }
  6846. return 0;
  6847. }
  6848. EXPORT_SYMBOL_GPL(__x86_set_memory_region);
  6849. int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
  6850. {
  6851. int r;
  6852. mutex_lock(&kvm->slots_lock);
  6853. r = __x86_set_memory_region(kvm, id, gpa, size);
  6854. mutex_unlock(&kvm->slots_lock);
  6855. return r;
  6856. }
  6857. EXPORT_SYMBOL_GPL(x86_set_memory_region);
  6858. void kvm_arch_destroy_vm(struct kvm *kvm)
  6859. {
  6860. if (current->mm == kvm->mm) {
  6861. /*
  6862. * Free memory regions allocated on behalf of userspace,
  6863. * unless the the memory map has changed due to process exit
  6864. * or fd copying.
  6865. */
  6866. x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
  6867. x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
  6868. x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
  6869. }
  6870. if (kvm_x86_ops->vm_destroy)
  6871. kvm_x86_ops->vm_destroy(kvm);
  6872. kvm_iommu_unmap_guest(kvm);
  6873. kfree(kvm->arch.vpic);
  6874. kfree(kvm->arch.vioapic);
  6875. kvm_free_vcpus(kvm);
  6876. kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
  6877. kvm_mmu_uninit_vm(kvm);
  6878. kvm_page_track_cleanup(kvm);
  6879. }
  6880. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  6881. struct kvm_memory_slot *dont)
  6882. {
  6883. int i;
  6884. for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
  6885. if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
  6886. kvfree(free->arch.rmap[i]);
  6887. free->arch.rmap[i] = NULL;
  6888. }
  6889. if (i == 0)
  6890. continue;
  6891. if (!dont || free->arch.lpage_info[i - 1] !=
  6892. dont->arch.lpage_info[i - 1]) {
  6893. kvfree(free->arch.lpage_info[i - 1]);
  6894. free->arch.lpage_info[i - 1] = NULL;
  6895. }
  6896. }
  6897. kvm_page_track_free_memslot(free, dont);
  6898. }
  6899. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  6900. unsigned long npages)
  6901. {
  6902. int i;
  6903. for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
  6904. struct kvm_lpage_info *linfo;
  6905. unsigned long ugfn;
  6906. int lpages;
  6907. int level = i + 1;
  6908. lpages = gfn_to_index(slot->base_gfn + npages - 1,
  6909. slot->base_gfn, level) + 1;
  6910. slot->arch.rmap[i] =
  6911. kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
  6912. if (!slot->arch.rmap[i])
  6913. goto out_free;
  6914. if (i == 0)
  6915. continue;
  6916. linfo = kvm_kvzalloc(lpages * sizeof(*linfo));
  6917. if (!linfo)
  6918. goto out_free;
  6919. slot->arch.lpage_info[i - 1] = linfo;
  6920. if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
  6921. linfo[0].disallow_lpage = 1;
  6922. if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
  6923. linfo[lpages - 1].disallow_lpage = 1;
  6924. ugfn = slot->userspace_addr >> PAGE_SHIFT;
  6925. /*
  6926. * If the gfn and userspace address are not aligned wrt each
  6927. * other, or if explicitly asked to, disable large page
  6928. * support for this slot
  6929. */
  6930. if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
  6931. !kvm_largepages_enabled()) {
  6932. unsigned long j;
  6933. for (j = 0; j < lpages; ++j)
  6934. linfo[j].disallow_lpage = 1;
  6935. }
  6936. }
  6937. if (kvm_page_track_create_memslot(slot, npages))
  6938. goto out_free;
  6939. return 0;
  6940. out_free:
  6941. for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
  6942. kvfree(slot->arch.rmap[i]);
  6943. slot->arch.rmap[i] = NULL;
  6944. if (i == 0)
  6945. continue;
  6946. kvfree(slot->arch.lpage_info[i - 1]);
  6947. slot->arch.lpage_info[i - 1] = NULL;
  6948. }
  6949. return -ENOMEM;
  6950. }
  6951. void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
  6952. {
  6953. /*
  6954. * memslots->generation has been incremented.
  6955. * mmio generation may have reached its maximum value.
  6956. */
  6957. kvm_mmu_invalidate_mmio_sptes(kvm, slots);
  6958. }
  6959. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  6960. struct kvm_memory_slot *memslot,
  6961. const struct kvm_userspace_memory_region *mem,
  6962. enum kvm_mr_change change)
  6963. {
  6964. return 0;
  6965. }
  6966. static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
  6967. struct kvm_memory_slot *new)
  6968. {
  6969. /* Still write protect RO slot */
  6970. if (new->flags & KVM_MEM_READONLY) {
  6971. kvm_mmu_slot_remove_write_access(kvm, new);
  6972. return;
  6973. }
  6974. /*
  6975. * Call kvm_x86_ops dirty logging hooks when they are valid.
  6976. *
  6977. * kvm_x86_ops->slot_disable_log_dirty is called when:
  6978. *
  6979. * - KVM_MR_CREATE with dirty logging is disabled
  6980. * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
  6981. *
  6982. * The reason is, in case of PML, we need to set D-bit for any slots
  6983. * with dirty logging disabled in order to eliminate unnecessary GPA
  6984. * logging in PML buffer (and potential PML buffer full VMEXT). This
  6985. * guarantees leaving PML enabled during guest's lifetime won't have
  6986. * any additonal overhead from PML when guest is running with dirty
  6987. * logging disabled for memory slots.
  6988. *
  6989. * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
  6990. * to dirty logging mode.
  6991. *
  6992. * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
  6993. *
  6994. * In case of write protect:
  6995. *
  6996. * Write protect all pages for dirty logging.
  6997. *
  6998. * All the sptes including the large sptes which point to this
  6999. * slot are set to readonly. We can not create any new large
  7000. * spte on this slot until the end of the logging.
  7001. *
  7002. * See the comments in fast_page_fault().
  7003. */
  7004. if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
  7005. if (kvm_x86_ops->slot_enable_log_dirty)
  7006. kvm_x86_ops->slot_enable_log_dirty(kvm, new);
  7007. else
  7008. kvm_mmu_slot_remove_write_access(kvm, new);
  7009. } else {
  7010. if (kvm_x86_ops->slot_disable_log_dirty)
  7011. kvm_x86_ops->slot_disable_log_dirty(kvm, new);
  7012. }
  7013. }
  7014. void kvm_arch_commit_memory_region(struct kvm *kvm,
  7015. const struct kvm_userspace_memory_region *mem,
  7016. const struct kvm_memory_slot *old,
  7017. const struct kvm_memory_slot *new,
  7018. enum kvm_mr_change change)
  7019. {
  7020. int nr_mmu_pages = 0;
  7021. if (!kvm->arch.n_requested_mmu_pages)
  7022. nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  7023. if (nr_mmu_pages)
  7024. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  7025. /*
  7026. * Dirty logging tracks sptes in 4k granularity, meaning that large
  7027. * sptes have to be split. If live migration is successful, the guest
  7028. * in the source machine will be destroyed and large sptes will be
  7029. * created in the destination. However, if the guest continues to run
  7030. * in the source machine (for example if live migration fails), small
  7031. * sptes will remain around and cause bad performance.
  7032. *
  7033. * Scan sptes if dirty logging has been stopped, dropping those
  7034. * which can be collapsed into a single large-page spte. Later
  7035. * page faults will create the large-page sptes.
  7036. */
  7037. if ((change != KVM_MR_DELETE) &&
  7038. (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
  7039. !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
  7040. kvm_mmu_zap_collapsible_sptes(kvm, new);
  7041. /*
  7042. * Set up write protection and/or dirty logging for the new slot.
  7043. *
  7044. * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
  7045. * been zapped so no dirty logging staff is needed for old slot. For
  7046. * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
  7047. * new and it's also covered when dealing with the new slot.
  7048. *
  7049. * FIXME: const-ify all uses of struct kvm_memory_slot.
  7050. */
  7051. if (change != KVM_MR_DELETE)
  7052. kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
  7053. }
  7054. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  7055. {
  7056. kvm_mmu_invalidate_zap_all_pages(kvm);
  7057. }
  7058. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  7059. struct kvm_memory_slot *slot)
  7060. {
  7061. kvm_mmu_invalidate_zap_all_pages(kvm);
  7062. }
  7063. static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
  7064. {
  7065. if (!list_empty_careful(&vcpu->async_pf.done))
  7066. return true;
  7067. if (kvm_apic_has_events(vcpu))
  7068. return true;
  7069. if (vcpu->arch.pv.pv_unhalted)
  7070. return true;
  7071. if (atomic_read(&vcpu->arch.nmi_queued))
  7072. return true;
  7073. if (test_bit(KVM_REQ_SMI, &vcpu->requests))
  7074. return true;
  7075. if (kvm_arch_interrupt_allowed(vcpu) &&
  7076. kvm_cpu_has_interrupt(vcpu))
  7077. return true;
  7078. if (kvm_hv_has_stimer_pending(vcpu))
  7079. return true;
  7080. return false;
  7081. }
  7082. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  7083. {
  7084. if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
  7085. kvm_x86_ops->check_nested_events(vcpu, false);
  7086. return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
  7087. }
  7088. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  7089. {
  7090. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  7091. }
  7092. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  7093. {
  7094. return kvm_x86_ops->interrupt_allowed(vcpu);
  7095. }
  7096. unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
  7097. {
  7098. if (is_64_bit_mode(vcpu))
  7099. return kvm_rip_read(vcpu);
  7100. return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
  7101. kvm_rip_read(vcpu));
  7102. }
  7103. EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
  7104. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
  7105. {
  7106. return kvm_get_linear_rip(vcpu) == linear_rip;
  7107. }
  7108. EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
  7109. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
  7110. {
  7111. unsigned long rflags;
  7112. rflags = kvm_x86_ops->get_rflags(vcpu);
  7113. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  7114. rflags &= ~X86_EFLAGS_TF;
  7115. return rflags;
  7116. }
  7117. EXPORT_SYMBOL_GPL(kvm_get_rflags);
  7118. static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  7119. {
  7120. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
  7121. kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
  7122. rflags |= X86_EFLAGS_TF;
  7123. kvm_x86_ops->set_rflags(vcpu, rflags);
  7124. }
  7125. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  7126. {
  7127. __kvm_set_rflags(vcpu, rflags);
  7128. kvm_make_request(KVM_REQ_EVENT, vcpu);
  7129. }
  7130. EXPORT_SYMBOL_GPL(kvm_set_rflags);
  7131. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
  7132. {
  7133. int r;
  7134. if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
  7135. work->wakeup_all)
  7136. return;
  7137. r = kvm_mmu_reload(vcpu);
  7138. if (unlikely(r))
  7139. return;
  7140. if (!vcpu->arch.mmu.direct_map &&
  7141. work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
  7142. return;
  7143. vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
  7144. }
  7145. static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
  7146. {
  7147. return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
  7148. }
  7149. static inline u32 kvm_async_pf_next_probe(u32 key)
  7150. {
  7151. return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
  7152. }
  7153. static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  7154. {
  7155. u32 key = kvm_async_pf_hash_fn(gfn);
  7156. while (vcpu->arch.apf.gfns[key] != ~0)
  7157. key = kvm_async_pf_next_probe(key);
  7158. vcpu->arch.apf.gfns[key] = gfn;
  7159. }
  7160. static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
  7161. {
  7162. int i;
  7163. u32 key = kvm_async_pf_hash_fn(gfn);
  7164. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
  7165. (vcpu->arch.apf.gfns[key] != gfn &&
  7166. vcpu->arch.apf.gfns[key] != ~0); i++)
  7167. key = kvm_async_pf_next_probe(key);
  7168. return key;
  7169. }
  7170. bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  7171. {
  7172. return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
  7173. }
  7174. static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  7175. {
  7176. u32 i, j, k;
  7177. i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
  7178. while (true) {
  7179. vcpu->arch.apf.gfns[i] = ~0;
  7180. do {
  7181. j = kvm_async_pf_next_probe(j);
  7182. if (vcpu->arch.apf.gfns[j] == ~0)
  7183. return;
  7184. k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
  7185. /*
  7186. * k lies cyclically in ]i,j]
  7187. * | i.k.j |
  7188. * |....j i.k.| or |.k..j i...|
  7189. */
  7190. } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
  7191. vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
  7192. i = j;
  7193. }
  7194. }
  7195. static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
  7196. {
  7197. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
  7198. sizeof(val));
  7199. }
  7200. static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
  7201. {
  7202. return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
  7203. sizeof(u32));
  7204. }
  7205. void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  7206. struct kvm_async_pf *work)
  7207. {
  7208. struct x86_exception fault;
  7209. trace_kvm_async_pf_not_present(work->arch.token, work->gva);
  7210. kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
  7211. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
  7212. (vcpu->arch.apf.send_user_only &&
  7213. kvm_x86_ops->get_cpl(vcpu) == 0))
  7214. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  7215. else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
  7216. fault.vector = PF_VECTOR;
  7217. fault.error_code_valid = true;
  7218. fault.error_code = 0;
  7219. fault.nested_page_fault = false;
  7220. fault.address = work->arch.token;
  7221. kvm_inject_page_fault(vcpu, &fault);
  7222. }
  7223. }
  7224. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  7225. struct kvm_async_pf *work)
  7226. {
  7227. struct x86_exception fault;
  7228. u32 val;
  7229. if (work->wakeup_all)
  7230. work->arch.token = ~0; /* broadcast wakeup */
  7231. else
  7232. kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
  7233. trace_kvm_async_pf_ready(work->arch.token, work->gva);
  7234. if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
  7235. !apf_get_user(vcpu, &val)) {
  7236. if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
  7237. vcpu->arch.exception.pending &&
  7238. vcpu->arch.exception.nr == PF_VECTOR &&
  7239. !apf_put_user(vcpu, 0)) {
  7240. vcpu->arch.exception.pending = false;
  7241. vcpu->arch.exception.nr = 0;
  7242. vcpu->arch.exception.has_error_code = false;
  7243. vcpu->arch.exception.error_code = 0;
  7244. } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
  7245. fault.vector = PF_VECTOR;
  7246. fault.error_code_valid = true;
  7247. fault.error_code = 0;
  7248. fault.nested_page_fault = false;
  7249. fault.address = work->arch.token;
  7250. kvm_inject_page_fault(vcpu, &fault);
  7251. }
  7252. }
  7253. vcpu->arch.apf.halted = false;
  7254. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  7255. }
  7256. bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
  7257. {
  7258. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
  7259. return true;
  7260. else
  7261. return kvm_can_do_async_pf(vcpu);
  7262. }
  7263. void kvm_arch_start_assignment(struct kvm *kvm)
  7264. {
  7265. atomic_inc(&kvm->arch.assigned_device_count);
  7266. }
  7267. EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
  7268. void kvm_arch_end_assignment(struct kvm *kvm)
  7269. {
  7270. atomic_dec(&kvm->arch.assigned_device_count);
  7271. }
  7272. EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
  7273. bool kvm_arch_has_assigned_device(struct kvm *kvm)
  7274. {
  7275. return atomic_read(&kvm->arch.assigned_device_count);
  7276. }
  7277. EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
  7278. void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
  7279. {
  7280. atomic_inc(&kvm->arch.noncoherent_dma_count);
  7281. }
  7282. EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
  7283. void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
  7284. {
  7285. atomic_dec(&kvm->arch.noncoherent_dma_count);
  7286. }
  7287. EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
  7288. bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
  7289. {
  7290. return atomic_read(&kvm->arch.noncoherent_dma_count);
  7291. }
  7292. EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
  7293. bool kvm_arch_has_irq_bypass(void)
  7294. {
  7295. return kvm_x86_ops->update_pi_irte != NULL;
  7296. }
  7297. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  7298. struct irq_bypass_producer *prod)
  7299. {
  7300. struct kvm_kernel_irqfd *irqfd =
  7301. container_of(cons, struct kvm_kernel_irqfd, consumer);
  7302. irqfd->producer = prod;
  7303. return kvm_x86_ops->update_pi_irte(irqfd->kvm,
  7304. prod->irq, irqfd->gsi, 1);
  7305. }
  7306. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  7307. struct irq_bypass_producer *prod)
  7308. {
  7309. int ret;
  7310. struct kvm_kernel_irqfd *irqfd =
  7311. container_of(cons, struct kvm_kernel_irqfd, consumer);
  7312. WARN_ON(irqfd->producer != prod);
  7313. irqfd->producer = NULL;
  7314. /*
  7315. * When producer of consumer is unregistered, we change back to
  7316. * remapped mode, so we can re-use the current implementation
  7317. * when the irq is masked/disabled or the consumer side (KVM
  7318. * int this case doesn't want to receive the interrupts.
  7319. */
  7320. ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
  7321. if (ret)
  7322. printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
  7323. " fails: %d\n", irqfd->consumer.token, ret);
  7324. }
  7325. int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
  7326. uint32_t guest_irq, bool set)
  7327. {
  7328. if (!kvm_x86_ops->update_pi_irte)
  7329. return -EINVAL;
  7330. return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
  7331. }
  7332. bool kvm_vector_hashing_enabled(void)
  7333. {
  7334. return vector_hashing;
  7335. }
  7336. EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
  7337. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  7338. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
  7339. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  7340. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  7341. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  7342. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
  7343. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
  7344. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
  7345. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
  7346. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
  7347. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
  7348. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
  7349. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
  7350. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
  7351. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
  7352. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
  7353. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
  7354. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
  7355. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);