x86.c 144 KB

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