x86.c 234 KB

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