x86.c 170 KB

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