x86.c 157 KB

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