x86.c 156 KB

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