x86.c 157 KB

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