x86.c 170 KB

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