x86.c 142 KB

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