x86.c 234 KB

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