x86.c 135 KB

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