x86.c 137 KB

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