x86.c 169 KB

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