x86.c 167 KB

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