x86.c 147 KB

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