x86.c 133 KB

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