x86.c 142 KB

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