x86.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Amit Shah <amit.shah@qumranet.com>
  14. * Ben-Ami Yassour <benami@il.ibm.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include <linux/kvm_host.h>
  21. #include "irq.h"
  22. #include "mmu.h"
  23. #include "i8254.h"
  24. #include "tss.h"
  25. #include "kvm_cache_regs.h"
  26. #include "x86.h"
  27. #include <linux/clocksource.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kvm.h>
  30. #include <linux/fs.h>
  31. #include <linux/pci.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/module.h>
  34. #include <linux/mman.h>
  35. #include <linux/highmem.h>
  36. #include <linux/intel-iommu.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/msr.h>
  39. #include <asm/desc.h>
  40. #define MAX_IO_MSRS 256
  41. #define CR0_RESERVED_BITS \
  42. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  43. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  44. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  45. #define CR4_RESERVED_BITS \
  46. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  47. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  48. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  49. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  50. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  51. /* EFER defaults:
  52. * - enable syscall per default because its emulated by KVM
  53. * - enable LME and LMA per default on 64 bit KVM
  54. */
  55. #ifdef CONFIG_X86_64
  56. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  57. #else
  58. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  59. #endif
  60. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  61. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  62. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  63. struct kvm_cpuid_entry2 __user *entries);
  64. struct kvm_x86_ops *kvm_x86_ops;
  65. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  66. struct kvm_stats_debugfs_item debugfs_entries[] = {
  67. { "pf_fixed", VCPU_STAT(pf_fixed) },
  68. { "pf_guest", VCPU_STAT(pf_guest) },
  69. { "tlb_flush", VCPU_STAT(tlb_flush) },
  70. { "invlpg", VCPU_STAT(invlpg) },
  71. { "exits", VCPU_STAT(exits) },
  72. { "io_exits", VCPU_STAT(io_exits) },
  73. { "mmio_exits", VCPU_STAT(mmio_exits) },
  74. { "signal_exits", VCPU_STAT(signal_exits) },
  75. { "irq_window", VCPU_STAT(irq_window_exits) },
  76. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  77. { "halt_exits", VCPU_STAT(halt_exits) },
  78. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  79. { "hypercalls", VCPU_STAT(hypercalls) },
  80. { "request_irq", VCPU_STAT(request_irq_exits) },
  81. { "irq_exits", VCPU_STAT(irq_exits) },
  82. { "host_state_reload", VCPU_STAT(host_state_reload) },
  83. { "efer_reload", VCPU_STAT(efer_reload) },
  84. { "fpu_reload", VCPU_STAT(fpu_reload) },
  85. { "insn_emulation", VCPU_STAT(insn_emulation) },
  86. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  87. { "irq_injections", VCPU_STAT(irq_injections) },
  88. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  89. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  90. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  91. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  92. { "mmu_flooded", VM_STAT(mmu_flooded) },
  93. { "mmu_recycled", VM_STAT(mmu_recycled) },
  94. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  95. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  96. { "largepages", VM_STAT(lpages) },
  97. { NULL }
  98. };
  99. static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
  100. int assigned_dev_id)
  101. {
  102. struct list_head *ptr;
  103. struct kvm_assigned_dev_kernel *match;
  104. list_for_each(ptr, head) {
  105. match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
  106. if (match->assigned_dev_id == assigned_dev_id)
  107. return match;
  108. }
  109. return NULL;
  110. }
  111. static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
  112. {
  113. struct kvm_assigned_dev_kernel *assigned_dev;
  114. assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
  115. interrupt_work);
  116. /* This is taken to safely inject irq inside the guest. When
  117. * the interrupt injection (or the ioapic code) uses a
  118. * finer-grained lock, update this
  119. */
  120. mutex_lock(&assigned_dev->kvm->lock);
  121. kvm_set_irq(assigned_dev->kvm,
  122. assigned_dev->guest_irq, 1);
  123. mutex_unlock(&assigned_dev->kvm->lock);
  124. kvm_put_kvm(assigned_dev->kvm);
  125. }
  126. /* FIXME: Implement the OR logic needed to make shared interrupts on
  127. * this line behave properly
  128. */
  129. static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
  130. {
  131. struct kvm_assigned_dev_kernel *assigned_dev =
  132. (struct kvm_assigned_dev_kernel *) dev_id;
  133. kvm_get_kvm(assigned_dev->kvm);
  134. schedule_work(&assigned_dev->interrupt_work);
  135. disable_irq_nosync(irq);
  136. return IRQ_HANDLED;
  137. }
  138. /* Ack the irq line for an assigned device */
  139. static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
  140. {
  141. struct kvm_assigned_dev_kernel *dev;
  142. if (kian->gsi == -1)
  143. return;
  144. dev = container_of(kian, struct kvm_assigned_dev_kernel,
  145. ack_notifier);
  146. kvm_set_irq(dev->kvm, dev->guest_irq, 0);
  147. enable_irq(dev->host_irq);
  148. }
  149. static void kvm_free_assigned_device(struct kvm *kvm,
  150. struct kvm_assigned_dev_kernel
  151. *assigned_dev)
  152. {
  153. if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
  154. free_irq(assigned_dev->host_irq, (void *)assigned_dev);
  155. kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
  156. if (cancel_work_sync(&assigned_dev->interrupt_work))
  157. /* We had pending work. That means we will have to take
  158. * care of kvm_put_kvm.
  159. */
  160. kvm_put_kvm(kvm);
  161. pci_release_regions(assigned_dev->dev);
  162. pci_disable_device(assigned_dev->dev);
  163. pci_dev_put(assigned_dev->dev);
  164. list_del(&assigned_dev->list);
  165. kfree(assigned_dev);
  166. }
  167. static void kvm_free_all_assigned_devices(struct kvm *kvm)
  168. {
  169. struct list_head *ptr, *ptr2;
  170. struct kvm_assigned_dev_kernel *assigned_dev;
  171. list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
  172. assigned_dev = list_entry(ptr,
  173. struct kvm_assigned_dev_kernel,
  174. list);
  175. kvm_free_assigned_device(kvm, assigned_dev);
  176. }
  177. }
  178. static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
  179. struct kvm_assigned_irq
  180. *assigned_irq)
  181. {
  182. int r = 0;
  183. struct kvm_assigned_dev_kernel *match;
  184. mutex_lock(&kvm->lock);
  185. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  186. assigned_irq->assigned_dev_id);
  187. if (!match) {
  188. mutex_unlock(&kvm->lock);
  189. return -EINVAL;
  190. }
  191. if (match->irq_requested) {
  192. match->guest_irq = assigned_irq->guest_irq;
  193. match->ack_notifier.gsi = assigned_irq->guest_irq;
  194. mutex_unlock(&kvm->lock);
  195. return 0;
  196. }
  197. INIT_WORK(&match->interrupt_work,
  198. kvm_assigned_dev_interrupt_work_handler);
  199. if (irqchip_in_kernel(kvm)) {
  200. if (!capable(CAP_SYS_RAWIO)) {
  201. r = -EPERM;
  202. goto out_release;
  203. }
  204. if (assigned_irq->host_irq)
  205. match->host_irq = assigned_irq->host_irq;
  206. else
  207. match->host_irq = match->dev->irq;
  208. match->guest_irq = assigned_irq->guest_irq;
  209. match->ack_notifier.gsi = assigned_irq->guest_irq;
  210. match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
  211. kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
  212. /* Even though this is PCI, we don't want to use shared
  213. * interrupts. Sharing host devices with guest-assigned devices
  214. * on the same interrupt line is not a happy situation: there
  215. * are going to be long delays in accepting, acking, etc.
  216. */
  217. if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
  218. "kvm_assigned_device", (void *)match)) {
  219. r = -EIO;
  220. goto out_release;
  221. }
  222. }
  223. match->irq_requested = true;
  224. mutex_unlock(&kvm->lock);
  225. return r;
  226. out_release:
  227. mutex_unlock(&kvm->lock);
  228. kvm_free_assigned_device(kvm, match);
  229. return r;
  230. }
  231. static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
  232. struct kvm_assigned_pci_dev *assigned_dev)
  233. {
  234. int r = 0;
  235. struct kvm_assigned_dev_kernel *match;
  236. struct pci_dev *dev;
  237. mutex_lock(&kvm->lock);
  238. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  239. assigned_dev->assigned_dev_id);
  240. if (match) {
  241. /* device already assigned */
  242. r = -EINVAL;
  243. goto out;
  244. }
  245. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  246. if (match == NULL) {
  247. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  248. __func__);
  249. r = -ENOMEM;
  250. goto out;
  251. }
  252. dev = pci_get_bus_and_slot(assigned_dev->busnr,
  253. assigned_dev->devfn);
  254. if (!dev) {
  255. printk(KERN_INFO "%s: host device not found\n", __func__);
  256. r = -EINVAL;
  257. goto out_free;
  258. }
  259. if (pci_enable_device(dev)) {
  260. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  261. r = -EBUSY;
  262. goto out_put;
  263. }
  264. r = pci_request_regions(dev, "kvm_assigned_device");
  265. if (r) {
  266. printk(KERN_INFO "%s: Could not get access to device regions\n",
  267. __func__);
  268. goto out_disable;
  269. }
  270. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  271. match->host_busnr = assigned_dev->busnr;
  272. match->host_devfn = assigned_dev->devfn;
  273. match->dev = dev;
  274. match->kvm = kvm;
  275. list_add(&match->list, &kvm->arch.assigned_dev_head);
  276. if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
  277. r = kvm_iommu_map_guest(kvm, match);
  278. if (r)
  279. goto out_list_del;
  280. }
  281. out:
  282. mutex_unlock(&kvm->lock);
  283. return r;
  284. out_list_del:
  285. list_del(&match->list);
  286. pci_release_regions(dev);
  287. out_disable:
  288. pci_disable_device(dev);
  289. out_put:
  290. pci_dev_put(dev);
  291. out_free:
  292. kfree(match);
  293. mutex_unlock(&kvm->lock);
  294. return r;
  295. }
  296. unsigned long segment_base(u16 selector)
  297. {
  298. struct descriptor_table gdt;
  299. struct desc_struct *d;
  300. unsigned long table_base;
  301. unsigned long v;
  302. if (selector == 0)
  303. return 0;
  304. asm("sgdt %0" : "=m"(gdt));
  305. table_base = gdt.base;
  306. if (selector & 4) { /* from ldt */
  307. u16 ldt_selector;
  308. asm("sldt %0" : "=g"(ldt_selector));
  309. table_base = segment_base(ldt_selector);
  310. }
  311. d = (struct desc_struct *)(table_base + (selector & ~7));
  312. v = d->base0 | ((unsigned long)d->base1 << 16) |
  313. ((unsigned long)d->base2 << 24);
  314. #ifdef CONFIG_X86_64
  315. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  316. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  317. #endif
  318. return v;
  319. }
  320. EXPORT_SYMBOL_GPL(segment_base);
  321. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  322. {
  323. if (irqchip_in_kernel(vcpu->kvm))
  324. return vcpu->arch.apic_base;
  325. else
  326. return vcpu->arch.apic_base;
  327. }
  328. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  329. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  330. {
  331. /* TODO: reserve bits check */
  332. if (irqchip_in_kernel(vcpu->kvm))
  333. kvm_lapic_set_base(vcpu, data);
  334. else
  335. vcpu->arch.apic_base = data;
  336. }
  337. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  338. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  339. {
  340. WARN_ON(vcpu->arch.exception.pending);
  341. vcpu->arch.exception.pending = true;
  342. vcpu->arch.exception.has_error_code = false;
  343. vcpu->arch.exception.nr = nr;
  344. }
  345. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  346. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  347. u32 error_code)
  348. {
  349. ++vcpu->stat.pf_guest;
  350. if (vcpu->arch.exception.pending) {
  351. if (vcpu->arch.exception.nr == PF_VECTOR) {
  352. printk(KERN_DEBUG "kvm: inject_page_fault:"
  353. " double fault 0x%lx\n", addr);
  354. vcpu->arch.exception.nr = DF_VECTOR;
  355. vcpu->arch.exception.error_code = 0;
  356. } else if (vcpu->arch.exception.nr == DF_VECTOR) {
  357. /* triple fault -> shutdown */
  358. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  359. }
  360. return;
  361. }
  362. vcpu->arch.cr2 = addr;
  363. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  364. }
  365. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  366. {
  367. vcpu->arch.nmi_pending = 1;
  368. }
  369. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  370. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  371. {
  372. WARN_ON(vcpu->arch.exception.pending);
  373. vcpu->arch.exception.pending = true;
  374. vcpu->arch.exception.has_error_code = true;
  375. vcpu->arch.exception.nr = nr;
  376. vcpu->arch.exception.error_code = error_code;
  377. }
  378. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  379. static void __queue_exception(struct kvm_vcpu *vcpu)
  380. {
  381. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  382. vcpu->arch.exception.has_error_code,
  383. vcpu->arch.exception.error_code);
  384. }
  385. /*
  386. * Load the pae pdptrs. Return true is they are all valid.
  387. */
  388. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  389. {
  390. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  391. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  392. int i;
  393. int ret;
  394. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  395. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  396. offset * sizeof(u64), sizeof(pdpte));
  397. if (ret < 0) {
  398. ret = 0;
  399. goto out;
  400. }
  401. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  402. if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
  403. ret = 0;
  404. goto out;
  405. }
  406. }
  407. ret = 1;
  408. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  409. out:
  410. return ret;
  411. }
  412. EXPORT_SYMBOL_GPL(load_pdptrs);
  413. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  414. {
  415. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  416. bool changed = true;
  417. int r;
  418. if (is_long_mode(vcpu) || !is_pae(vcpu))
  419. return false;
  420. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  421. if (r < 0)
  422. goto out;
  423. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  424. out:
  425. return changed;
  426. }
  427. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  428. {
  429. if (cr0 & CR0_RESERVED_BITS) {
  430. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  431. cr0, vcpu->arch.cr0);
  432. kvm_inject_gp(vcpu, 0);
  433. return;
  434. }
  435. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  436. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  437. kvm_inject_gp(vcpu, 0);
  438. return;
  439. }
  440. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  441. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  442. "and a clear PE flag\n");
  443. kvm_inject_gp(vcpu, 0);
  444. return;
  445. }
  446. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  447. #ifdef CONFIG_X86_64
  448. if ((vcpu->arch.shadow_efer & EFER_LME)) {
  449. int cs_db, cs_l;
  450. if (!is_pae(vcpu)) {
  451. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  452. "in long mode while PAE is disabled\n");
  453. kvm_inject_gp(vcpu, 0);
  454. return;
  455. }
  456. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  457. if (cs_l) {
  458. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  459. "in long mode while CS.L == 1\n");
  460. kvm_inject_gp(vcpu, 0);
  461. return;
  462. }
  463. } else
  464. #endif
  465. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  466. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  467. "reserved bits\n");
  468. kvm_inject_gp(vcpu, 0);
  469. return;
  470. }
  471. }
  472. kvm_x86_ops->set_cr0(vcpu, cr0);
  473. vcpu->arch.cr0 = cr0;
  474. kvm_mmu_reset_context(vcpu);
  475. return;
  476. }
  477. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  478. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  479. {
  480. kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
  481. KVMTRACE_1D(LMSW, vcpu,
  482. (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
  483. handler);
  484. }
  485. EXPORT_SYMBOL_GPL(kvm_lmsw);
  486. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  487. {
  488. if (cr4 & CR4_RESERVED_BITS) {
  489. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  490. kvm_inject_gp(vcpu, 0);
  491. return;
  492. }
  493. if (is_long_mode(vcpu)) {
  494. if (!(cr4 & X86_CR4_PAE)) {
  495. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  496. "in long mode\n");
  497. kvm_inject_gp(vcpu, 0);
  498. return;
  499. }
  500. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
  501. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  502. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  503. kvm_inject_gp(vcpu, 0);
  504. return;
  505. }
  506. if (cr4 & X86_CR4_VMXE) {
  507. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  508. kvm_inject_gp(vcpu, 0);
  509. return;
  510. }
  511. kvm_x86_ops->set_cr4(vcpu, cr4);
  512. vcpu->arch.cr4 = cr4;
  513. kvm_mmu_reset_context(vcpu);
  514. }
  515. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  516. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  517. {
  518. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  519. kvm_mmu_sync_roots(vcpu);
  520. kvm_mmu_flush_tlb(vcpu);
  521. return;
  522. }
  523. if (is_long_mode(vcpu)) {
  524. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  525. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  526. kvm_inject_gp(vcpu, 0);
  527. return;
  528. }
  529. } else {
  530. if (is_pae(vcpu)) {
  531. if (cr3 & CR3_PAE_RESERVED_BITS) {
  532. printk(KERN_DEBUG
  533. "set_cr3: #GP, reserved bits\n");
  534. kvm_inject_gp(vcpu, 0);
  535. return;
  536. }
  537. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  538. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  539. "reserved bits\n");
  540. kvm_inject_gp(vcpu, 0);
  541. return;
  542. }
  543. }
  544. /*
  545. * We don't check reserved bits in nonpae mode, because
  546. * this isn't enforced, and VMware depends on this.
  547. */
  548. }
  549. /*
  550. * Does the new cr3 value map to physical memory? (Note, we
  551. * catch an invalid cr3 even in real-mode, because it would
  552. * cause trouble later on when we turn on paging anyway.)
  553. *
  554. * A real CPU would silently accept an invalid cr3 and would
  555. * attempt to use it - with largely undefined (and often hard
  556. * to debug) behavior on the guest side.
  557. */
  558. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  559. kvm_inject_gp(vcpu, 0);
  560. else {
  561. vcpu->arch.cr3 = cr3;
  562. vcpu->arch.mmu.new_cr3(vcpu);
  563. }
  564. }
  565. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  566. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  567. {
  568. if (cr8 & CR8_RESERVED_BITS) {
  569. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  570. kvm_inject_gp(vcpu, 0);
  571. return;
  572. }
  573. if (irqchip_in_kernel(vcpu->kvm))
  574. kvm_lapic_set_tpr(vcpu, cr8);
  575. else
  576. vcpu->arch.cr8 = cr8;
  577. }
  578. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  579. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  580. {
  581. if (irqchip_in_kernel(vcpu->kvm))
  582. return kvm_lapic_get_cr8(vcpu);
  583. else
  584. return vcpu->arch.cr8;
  585. }
  586. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  587. /*
  588. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  589. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  590. *
  591. * This list is modified at module load time to reflect the
  592. * capabilities of the host cpu.
  593. */
  594. static u32 msrs_to_save[] = {
  595. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  596. MSR_K6_STAR,
  597. #ifdef CONFIG_X86_64
  598. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  599. #endif
  600. MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  601. MSR_IA32_PERF_STATUS,
  602. };
  603. static unsigned num_msrs_to_save;
  604. static u32 emulated_msrs[] = {
  605. MSR_IA32_MISC_ENABLE,
  606. };
  607. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  608. {
  609. if (efer & efer_reserved_bits) {
  610. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  611. efer);
  612. kvm_inject_gp(vcpu, 0);
  613. return;
  614. }
  615. if (is_paging(vcpu)
  616. && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  617. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  618. kvm_inject_gp(vcpu, 0);
  619. return;
  620. }
  621. kvm_x86_ops->set_efer(vcpu, efer);
  622. efer &= ~EFER_LMA;
  623. efer |= vcpu->arch.shadow_efer & EFER_LMA;
  624. vcpu->arch.shadow_efer = efer;
  625. }
  626. void kvm_enable_efer_bits(u64 mask)
  627. {
  628. efer_reserved_bits &= ~mask;
  629. }
  630. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  631. /*
  632. * Writes msr value into into the appropriate "register".
  633. * Returns 0 on success, non-0 otherwise.
  634. * Assumes vcpu_load() was already called.
  635. */
  636. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  637. {
  638. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  639. }
  640. /*
  641. * Adapt set_msr() to msr_io()'s calling convention
  642. */
  643. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  644. {
  645. return kvm_set_msr(vcpu, index, *data);
  646. }
  647. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  648. {
  649. static int version;
  650. struct pvclock_wall_clock wc;
  651. struct timespec now, sys, boot;
  652. if (!wall_clock)
  653. return;
  654. version++;
  655. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  656. /*
  657. * The guest calculates current wall clock time by adding
  658. * system time (updated by kvm_write_guest_time below) to the
  659. * wall clock specified here. guest system time equals host
  660. * system time for us, thus we must fill in host boot time here.
  661. */
  662. now = current_kernel_time();
  663. ktime_get_ts(&sys);
  664. boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
  665. wc.sec = boot.tv_sec;
  666. wc.nsec = boot.tv_nsec;
  667. wc.version = version;
  668. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  669. version++;
  670. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  671. }
  672. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  673. {
  674. uint32_t quotient, remainder;
  675. /* Don't try to replace with do_div(), this one calculates
  676. * "(dividend << 32) / divisor" */
  677. __asm__ ( "divl %4"
  678. : "=a" (quotient), "=d" (remainder)
  679. : "0" (0), "1" (dividend), "r" (divisor) );
  680. return quotient;
  681. }
  682. static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
  683. {
  684. uint64_t nsecs = 1000000000LL;
  685. int32_t shift = 0;
  686. uint64_t tps64;
  687. uint32_t tps32;
  688. tps64 = tsc_khz * 1000LL;
  689. while (tps64 > nsecs*2) {
  690. tps64 >>= 1;
  691. shift--;
  692. }
  693. tps32 = (uint32_t)tps64;
  694. while (tps32 <= (uint32_t)nsecs) {
  695. tps32 <<= 1;
  696. shift++;
  697. }
  698. hv_clock->tsc_shift = shift;
  699. hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
  700. pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
  701. __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
  702. hv_clock->tsc_to_system_mul);
  703. }
  704. static void kvm_write_guest_time(struct kvm_vcpu *v)
  705. {
  706. struct timespec ts;
  707. unsigned long flags;
  708. struct kvm_vcpu_arch *vcpu = &v->arch;
  709. void *shared_kaddr;
  710. if ((!vcpu->time_page))
  711. return;
  712. if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
  713. kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
  714. vcpu->hv_clock_tsc_khz = tsc_khz;
  715. }
  716. /* Keep irq disabled to prevent changes to the clock */
  717. local_irq_save(flags);
  718. kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
  719. &vcpu->hv_clock.tsc_timestamp);
  720. ktime_get_ts(&ts);
  721. local_irq_restore(flags);
  722. /* With all the info we got, fill in the values */
  723. vcpu->hv_clock.system_time = ts.tv_nsec +
  724. (NSEC_PER_SEC * (u64)ts.tv_sec);
  725. /*
  726. * The interface expects us to write an even number signaling that the
  727. * update is finished. Since the guest won't see the intermediate
  728. * state, we just increase by 2 at the end.
  729. */
  730. vcpu->hv_clock.version += 2;
  731. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  732. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  733. sizeof(vcpu->hv_clock));
  734. kunmap_atomic(shared_kaddr, KM_USER0);
  735. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  736. }
  737. static bool msr_mtrr_valid(unsigned msr)
  738. {
  739. switch (msr) {
  740. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  741. case MSR_MTRRfix64K_00000:
  742. case MSR_MTRRfix16K_80000:
  743. case MSR_MTRRfix16K_A0000:
  744. case MSR_MTRRfix4K_C0000:
  745. case MSR_MTRRfix4K_C8000:
  746. case MSR_MTRRfix4K_D0000:
  747. case MSR_MTRRfix4K_D8000:
  748. case MSR_MTRRfix4K_E0000:
  749. case MSR_MTRRfix4K_E8000:
  750. case MSR_MTRRfix4K_F0000:
  751. case MSR_MTRRfix4K_F8000:
  752. case MSR_MTRRdefType:
  753. case MSR_IA32_CR_PAT:
  754. return true;
  755. case 0x2f8:
  756. return true;
  757. }
  758. return false;
  759. }
  760. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  761. {
  762. if (!msr_mtrr_valid(msr))
  763. return 1;
  764. vcpu->arch.mtrr[msr - 0x200] = data;
  765. return 0;
  766. }
  767. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  768. {
  769. switch (msr) {
  770. case MSR_EFER:
  771. set_efer(vcpu, data);
  772. break;
  773. case MSR_IA32_MC0_STATUS:
  774. pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  775. __func__, data);
  776. break;
  777. case MSR_IA32_MCG_STATUS:
  778. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  779. __func__, data);
  780. break;
  781. case MSR_IA32_MCG_CTL:
  782. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
  783. __func__, data);
  784. break;
  785. case MSR_IA32_DEBUGCTLMSR:
  786. if (!data) {
  787. /* We support the non-activated case already */
  788. break;
  789. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  790. /* Values other than LBR and BTF are vendor-specific,
  791. thus reserved and should throw a #GP */
  792. return 1;
  793. }
  794. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  795. __func__, data);
  796. break;
  797. case MSR_IA32_UCODE_REV:
  798. case MSR_IA32_UCODE_WRITE:
  799. break;
  800. case 0x200 ... 0x2ff:
  801. return set_msr_mtrr(vcpu, msr, data);
  802. case MSR_IA32_APICBASE:
  803. kvm_set_apic_base(vcpu, data);
  804. break;
  805. case MSR_IA32_MISC_ENABLE:
  806. vcpu->arch.ia32_misc_enable_msr = data;
  807. break;
  808. case MSR_KVM_WALL_CLOCK:
  809. vcpu->kvm->arch.wall_clock = data;
  810. kvm_write_wall_clock(vcpu->kvm, data);
  811. break;
  812. case MSR_KVM_SYSTEM_TIME: {
  813. if (vcpu->arch.time_page) {
  814. kvm_release_page_dirty(vcpu->arch.time_page);
  815. vcpu->arch.time_page = NULL;
  816. }
  817. vcpu->arch.time = data;
  818. /* we verify if the enable bit is set... */
  819. if (!(data & 1))
  820. break;
  821. /* ...but clean it before doing the actual write */
  822. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  823. vcpu->arch.time_page =
  824. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  825. if (is_error_page(vcpu->arch.time_page)) {
  826. kvm_release_page_clean(vcpu->arch.time_page);
  827. vcpu->arch.time_page = NULL;
  828. }
  829. kvm_write_guest_time(vcpu);
  830. break;
  831. }
  832. default:
  833. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
  834. return 1;
  835. }
  836. return 0;
  837. }
  838. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  839. /*
  840. * Reads an msr value (of 'msr_index') into 'pdata'.
  841. * Returns 0 on success, non-0 otherwise.
  842. * Assumes vcpu_load() was already called.
  843. */
  844. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  845. {
  846. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  847. }
  848. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  849. {
  850. if (!msr_mtrr_valid(msr))
  851. return 1;
  852. *pdata = vcpu->arch.mtrr[msr - 0x200];
  853. return 0;
  854. }
  855. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  856. {
  857. u64 data;
  858. switch (msr) {
  859. case 0xc0010010: /* SYSCFG */
  860. case 0xc0010015: /* HWCR */
  861. case MSR_IA32_PLATFORM_ID:
  862. case MSR_IA32_P5_MC_ADDR:
  863. case MSR_IA32_P5_MC_TYPE:
  864. case MSR_IA32_MC0_CTL:
  865. case MSR_IA32_MCG_STATUS:
  866. case MSR_IA32_MCG_CAP:
  867. case MSR_IA32_MCG_CTL:
  868. case MSR_IA32_MC0_MISC:
  869. case MSR_IA32_MC0_MISC+4:
  870. case MSR_IA32_MC0_MISC+8:
  871. case MSR_IA32_MC0_MISC+12:
  872. case MSR_IA32_MC0_MISC+16:
  873. case MSR_IA32_MC0_MISC+20:
  874. case MSR_IA32_UCODE_REV:
  875. case MSR_IA32_EBL_CR_POWERON:
  876. case MSR_IA32_DEBUGCTLMSR:
  877. case MSR_IA32_LASTBRANCHFROMIP:
  878. case MSR_IA32_LASTBRANCHTOIP:
  879. case MSR_IA32_LASTINTFROMIP:
  880. case MSR_IA32_LASTINTTOIP:
  881. data = 0;
  882. break;
  883. case MSR_MTRRcap:
  884. data = 0x500 | KVM_NR_VAR_MTRR;
  885. break;
  886. case 0x200 ... 0x2ff:
  887. return get_msr_mtrr(vcpu, msr, pdata);
  888. case 0xcd: /* fsb frequency */
  889. data = 3;
  890. break;
  891. case MSR_IA32_APICBASE:
  892. data = kvm_get_apic_base(vcpu);
  893. break;
  894. case MSR_IA32_MISC_ENABLE:
  895. data = vcpu->arch.ia32_misc_enable_msr;
  896. break;
  897. case MSR_IA32_PERF_STATUS:
  898. /* TSC increment by tick */
  899. data = 1000ULL;
  900. /* CPU multiplier */
  901. data |= (((uint64_t)4ULL) << 40);
  902. break;
  903. case MSR_EFER:
  904. data = vcpu->arch.shadow_efer;
  905. break;
  906. case MSR_KVM_WALL_CLOCK:
  907. data = vcpu->kvm->arch.wall_clock;
  908. break;
  909. case MSR_KVM_SYSTEM_TIME:
  910. data = vcpu->arch.time;
  911. break;
  912. default:
  913. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  914. return 1;
  915. }
  916. *pdata = data;
  917. return 0;
  918. }
  919. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  920. /*
  921. * Read or write a bunch of msrs. All parameters are kernel addresses.
  922. *
  923. * @return number of msrs set successfully.
  924. */
  925. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  926. struct kvm_msr_entry *entries,
  927. int (*do_msr)(struct kvm_vcpu *vcpu,
  928. unsigned index, u64 *data))
  929. {
  930. int i;
  931. vcpu_load(vcpu);
  932. down_read(&vcpu->kvm->slots_lock);
  933. for (i = 0; i < msrs->nmsrs; ++i)
  934. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  935. break;
  936. up_read(&vcpu->kvm->slots_lock);
  937. vcpu_put(vcpu);
  938. return i;
  939. }
  940. /*
  941. * Read or write a bunch of msrs. Parameters are user addresses.
  942. *
  943. * @return number of msrs set successfully.
  944. */
  945. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  946. int (*do_msr)(struct kvm_vcpu *vcpu,
  947. unsigned index, u64 *data),
  948. int writeback)
  949. {
  950. struct kvm_msrs msrs;
  951. struct kvm_msr_entry *entries;
  952. int r, n;
  953. unsigned size;
  954. r = -EFAULT;
  955. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  956. goto out;
  957. r = -E2BIG;
  958. if (msrs.nmsrs >= MAX_IO_MSRS)
  959. goto out;
  960. r = -ENOMEM;
  961. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  962. entries = vmalloc(size);
  963. if (!entries)
  964. goto out;
  965. r = -EFAULT;
  966. if (copy_from_user(entries, user_msrs->entries, size))
  967. goto out_free;
  968. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  969. if (r < 0)
  970. goto out_free;
  971. r = -EFAULT;
  972. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  973. goto out_free;
  974. r = n;
  975. out_free:
  976. vfree(entries);
  977. out:
  978. return r;
  979. }
  980. int kvm_dev_ioctl_check_extension(long ext)
  981. {
  982. int r;
  983. switch (ext) {
  984. case KVM_CAP_IRQCHIP:
  985. case KVM_CAP_HLT:
  986. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  987. case KVM_CAP_USER_MEMORY:
  988. case KVM_CAP_SET_TSS_ADDR:
  989. case KVM_CAP_EXT_CPUID:
  990. case KVM_CAP_CLOCKSOURCE:
  991. case KVM_CAP_PIT:
  992. case KVM_CAP_NOP_IO_DELAY:
  993. case KVM_CAP_MP_STATE:
  994. case KVM_CAP_SYNC_MMU:
  995. r = 1;
  996. break;
  997. case KVM_CAP_COALESCED_MMIO:
  998. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  999. break;
  1000. case KVM_CAP_VAPIC:
  1001. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1002. break;
  1003. case KVM_CAP_NR_VCPUS:
  1004. r = KVM_MAX_VCPUS;
  1005. break;
  1006. case KVM_CAP_NR_MEMSLOTS:
  1007. r = KVM_MEMORY_SLOTS;
  1008. break;
  1009. case KVM_CAP_PV_MMU:
  1010. r = !tdp_enabled;
  1011. break;
  1012. case KVM_CAP_IOMMU:
  1013. r = intel_iommu_found();
  1014. break;
  1015. default:
  1016. r = 0;
  1017. break;
  1018. }
  1019. return r;
  1020. }
  1021. long kvm_arch_dev_ioctl(struct file *filp,
  1022. unsigned int ioctl, unsigned long arg)
  1023. {
  1024. void __user *argp = (void __user *)arg;
  1025. long r;
  1026. switch (ioctl) {
  1027. case KVM_GET_MSR_INDEX_LIST: {
  1028. struct kvm_msr_list __user *user_msr_list = argp;
  1029. struct kvm_msr_list msr_list;
  1030. unsigned n;
  1031. r = -EFAULT;
  1032. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1033. goto out;
  1034. n = msr_list.nmsrs;
  1035. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1036. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1037. goto out;
  1038. r = -E2BIG;
  1039. if (n < num_msrs_to_save)
  1040. goto out;
  1041. r = -EFAULT;
  1042. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1043. num_msrs_to_save * sizeof(u32)))
  1044. goto out;
  1045. if (copy_to_user(user_msr_list->indices
  1046. + num_msrs_to_save * sizeof(u32),
  1047. &emulated_msrs,
  1048. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1049. goto out;
  1050. r = 0;
  1051. break;
  1052. }
  1053. case KVM_GET_SUPPORTED_CPUID: {
  1054. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1055. struct kvm_cpuid2 cpuid;
  1056. r = -EFAULT;
  1057. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1058. goto out;
  1059. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1060. cpuid_arg->entries);
  1061. if (r)
  1062. goto out;
  1063. r = -EFAULT;
  1064. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1065. goto out;
  1066. r = 0;
  1067. break;
  1068. }
  1069. default:
  1070. r = -EINVAL;
  1071. }
  1072. out:
  1073. return r;
  1074. }
  1075. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1076. {
  1077. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1078. kvm_write_guest_time(vcpu);
  1079. }
  1080. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1081. {
  1082. kvm_x86_ops->vcpu_put(vcpu);
  1083. kvm_put_guest_fpu(vcpu);
  1084. }
  1085. static int is_efer_nx(void)
  1086. {
  1087. u64 efer;
  1088. rdmsrl(MSR_EFER, efer);
  1089. return efer & EFER_NX;
  1090. }
  1091. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1092. {
  1093. int i;
  1094. struct kvm_cpuid_entry2 *e, *entry;
  1095. entry = NULL;
  1096. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1097. e = &vcpu->arch.cpuid_entries[i];
  1098. if (e->function == 0x80000001) {
  1099. entry = e;
  1100. break;
  1101. }
  1102. }
  1103. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1104. entry->edx &= ~(1 << 20);
  1105. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1106. }
  1107. }
  1108. /* when an old userspace process fills a new kernel module */
  1109. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1110. struct kvm_cpuid *cpuid,
  1111. struct kvm_cpuid_entry __user *entries)
  1112. {
  1113. int r, i;
  1114. struct kvm_cpuid_entry *cpuid_entries;
  1115. r = -E2BIG;
  1116. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1117. goto out;
  1118. r = -ENOMEM;
  1119. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1120. if (!cpuid_entries)
  1121. goto out;
  1122. r = -EFAULT;
  1123. if (copy_from_user(cpuid_entries, entries,
  1124. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1125. goto out_free;
  1126. for (i = 0; i < cpuid->nent; i++) {
  1127. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1128. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1129. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1130. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1131. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1132. vcpu->arch.cpuid_entries[i].index = 0;
  1133. vcpu->arch.cpuid_entries[i].flags = 0;
  1134. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1135. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1136. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1137. }
  1138. vcpu->arch.cpuid_nent = cpuid->nent;
  1139. cpuid_fix_nx_cap(vcpu);
  1140. r = 0;
  1141. out_free:
  1142. vfree(cpuid_entries);
  1143. out:
  1144. return r;
  1145. }
  1146. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1147. struct kvm_cpuid2 *cpuid,
  1148. struct kvm_cpuid_entry2 __user *entries)
  1149. {
  1150. int r;
  1151. r = -E2BIG;
  1152. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1153. goto out;
  1154. r = -EFAULT;
  1155. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1156. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1157. goto out;
  1158. vcpu->arch.cpuid_nent = cpuid->nent;
  1159. return 0;
  1160. out:
  1161. return r;
  1162. }
  1163. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1164. struct kvm_cpuid2 *cpuid,
  1165. struct kvm_cpuid_entry2 __user *entries)
  1166. {
  1167. int r;
  1168. r = -E2BIG;
  1169. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1170. goto out;
  1171. r = -EFAULT;
  1172. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1173. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1174. goto out;
  1175. return 0;
  1176. out:
  1177. cpuid->nent = vcpu->arch.cpuid_nent;
  1178. return r;
  1179. }
  1180. static inline u32 bit(int bitno)
  1181. {
  1182. return 1 << (bitno & 31);
  1183. }
  1184. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1185. u32 index)
  1186. {
  1187. entry->function = function;
  1188. entry->index = index;
  1189. cpuid_count(entry->function, entry->index,
  1190. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  1191. entry->flags = 0;
  1192. }
  1193. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1194. u32 index, int *nent, int maxnent)
  1195. {
  1196. const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
  1197. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  1198. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  1199. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  1200. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  1201. bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
  1202. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  1203. bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
  1204. bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
  1205. bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
  1206. const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
  1207. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  1208. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  1209. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  1210. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  1211. bit(X86_FEATURE_PGE) |
  1212. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  1213. bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
  1214. bit(X86_FEATURE_SYSCALL) |
  1215. (bit(X86_FEATURE_NX) && is_efer_nx()) |
  1216. #ifdef CONFIG_X86_64
  1217. bit(X86_FEATURE_LM) |
  1218. #endif
  1219. bit(X86_FEATURE_MMXEXT) |
  1220. bit(X86_FEATURE_3DNOWEXT) |
  1221. bit(X86_FEATURE_3DNOW);
  1222. const u32 kvm_supported_word3_x86_features =
  1223. bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
  1224. const u32 kvm_supported_word6_x86_features =
  1225. bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
  1226. /* all func 2 cpuid_count() should be called on the same cpu */
  1227. get_cpu();
  1228. do_cpuid_1_ent(entry, function, index);
  1229. ++*nent;
  1230. switch (function) {
  1231. case 0:
  1232. entry->eax = min(entry->eax, (u32)0xb);
  1233. break;
  1234. case 1:
  1235. entry->edx &= kvm_supported_word0_x86_features;
  1236. entry->ecx &= kvm_supported_word3_x86_features;
  1237. break;
  1238. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  1239. * may return different values. This forces us to get_cpu() before
  1240. * issuing the first command, and also to emulate this annoying behavior
  1241. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  1242. case 2: {
  1243. int t, times = entry->eax & 0xff;
  1244. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1245. for (t = 1; t < times && *nent < maxnent; ++t) {
  1246. do_cpuid_1_ent(&entry[t], function, 0);
  1247. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1248. ++*nent;
  1249. }
  1250. break;
  1251. }
  1252. /* function 4 and 0xb have additional index. */
  1253. case 4: {
  1254. int i, cache_type;
  1255. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1256. /* read more entries until cache_type is zero */
  1257. for (i = 1; *nent < maxnent; ++i) {
  1258. cache_type = entry[i - 1].eax & 0x1f;
  1259. if (!cache_type)
  1260. break;
  1261. do_cpuid_1_ent(&entry[i], function, i);
  1262. entry[i].flags |=
  1263. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1264. ++*nent;
  1265. }
  1266. break;
  1267. }
  1268. case 0xb: {
  1269. int i, level_type;
  1270. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1271. /* read more entries until level_type is zero */
  1272. for (i = 1; *nent < maxnent; ++i) {
  1273. level_type = entry[i - 1].ecx & 0xff;
  1274. if (!level_type)
  1275. break;
  1276. do_cpuid_1_ent(&entry[i], function, i);
  1277. entry[i].flags |=
  1278. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1279. ++*nent;
  1280. }
  1281. break;
  1282. }
  1283. case 0x80000000:
  1284. entry->eax = min(entry->eax, 0x8000001a);
  1285. break;
  1286. case 0x80000001:
  1287. entry->edx &= kvm_supported_word1_x86_features;
  1288. entry->ecx &= kvm_supported_word6_x86_features;
  1289. break;
  1290. }
  1291. put_cpu();
  1292. }
  1293. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1294. struct kvm_cpuid_entry2 __user *entries)
  1295. {
  1296. struct kvm_cpuid_entry2 *cpuid_entries;
  1297. int limit, nent = 0, r = -E2BIG;
  1298. u32 func;
  1299. if (cpuid->nent < 1)
  1300. goto out;
  1301. r = -ENOMEM;
  1302. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1303. if (!cpuid_entries)
  1304. goto out;
  1305. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1306. limit = cpuid_entries[0].eax;
  1307. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1308. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1309. &nent, cpuid->nent);
  1310. r = -E2BIG;
  1311. if (nent >= cpuid->nent)
  1312. goto out_free;
  1313. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1314. limit = cpuid_entries[nent - 1].eax;
  1315. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1316. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1317. &nent, cpuid->nent);
  1318. r = -EFAULT;
  1319. if (copy_to_user(entries, cpuid_entries,
  1320. nent * sizeof(struct kvm_cpuid_entry2)))
  1321. goto out_free;
  1322. cpuid->nent = nent;
  1323. r = 0;
  1324. out_free:
  1325. vfree(cpuid_entries);
  1326. out:
  1327. return r;
  1328. }
  1329. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1330. struct kvm_lapic_state *s)
  1331. {
  1332. vcpu_load(vcpu);
  1333. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1334. vcpu_put(vcpu);
  1335. return 0;
  1336. }
  1337. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1338. struct kvm_lapic_state *s)
  1339. {
  1340. vcpu_load(vcpu);
  1341. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1342. kvm_apic_post_state_restore(vcpu);
  1343. vcpu_put(vcpu);
  1344. return 0;
  1345. }
  1346. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1347. struct kvm_interrupt *irq)
  1348. {
  1349. if (irq->irq < 0 || irq->irq >= 256)
  1350. return -EINVAL;
  1351. if (irqchip_in_kernel(vcpu->kvm))
  1352. return -ENXIO;
  1353. vcpu_load(vcpu);
  1354. set_bit(irq->irq, vcpu->arch.irq_pending);
  1355. set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  1356. vcpu_put(vcpu);
  1357. return 0;
  1358. }
  1359. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1360. struct kvm_tpr_access_ctl *tac)
  1361. {
  1362. if (tac->flags)
  1363. return -EINVAL;
  1364. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1365. return 0;
  1366. }
  1367. long kvm_arch_vcpu_ioctl(struct file *filp,
  1368. unsigned int ioctl, unsigned long arg)
  1369. {
  1370. struct kvm_vcpu *vcpu = filp->private_data;
  1371. void __user *argp = (void __user *)arg;
  1372. int r;
  1373. struct kvm_lapic_state *lapic = NULL;
  1374. switch (ioctl) {
  1375. case KVM_GET_LAPIC: {
  1376. lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1377. r = -ENOMEM;
  1378. if (!lapic)
  1379. goto out;
  1380. r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
  1381. if (r)
  1382. goto out;
  1383. r = -EFAULT;
  1384. if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
  1385. goto out;
  1386. r = 0;
  1387. break;
  1388. }
  1389. case KVM_SET_LAPIC: {
  1390. lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1391. r = -ENOMEM;
  1392. if (!lapic)
  1393. goto out;
  1394. r = -EFAULT;
  1395. if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
  1396. goto out;
  1397. r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
  1398. if (r)
  1399. goto out;
  1400. r = 0;
  1401. break;
  1402. }
  1403. case KVM_INTERRUPT: {
  1404. struct kvm_interrupt irq;
  1405. r = -EFAULT;
  1406. if (copy_from_user(&irq, argp, sizeof irq))
  1407. goto out;
  1408. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1409. if (r)
  1410. goto out;
  1411. r = 0;
  1412. break;
  1413. }
  1414. case KVM_SET_CPUID: {
  1415. struct kvm_cpuid __user *cpuid_arg = argp;
  1416. struct kvm_cpuid cpuid;
  1417. r = -EFAULT;
  1418. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1419. goto out;
  1420. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1421. if (r)
  1422. goto out;
  1423. break;
  1424. }
  1425. case KVM_SET_CPUID2: {
  1426. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1427. struct kvm_cpuid2 cpuid;
  1428. r = -EFAULT;
  1429. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1430. goto out;
  1431. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  1432. cpuid_arg->entries);
  1433. if (r)
  1434. goto out;
  1435. break;
  1436. }
  1437. case KVM_GET_CPUID2: {
  1438. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1439. struct kvm_cpuid2 cpuid;
  1440. r = -EFAULT;
  1441. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1442. goto out;
  1443. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  1444. cpuid_arg->entries);
  1445. if (r)
  1446. goto out;
  1447. r = -EFAULT;
  1448. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1449. goto out;
  1450. r = 0;
  1451. break;
  1452. }
  1453. case KVM_GET_MSRS:
  1454. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  1455. break;
  1456. case KVM_SET_MSRS:
  1457. r = msr_io(vcpu, argp, do_set_msr, 0);
  1458. break;
  1459. case KVM_TPR_ACCESS_REPORTING: {
  1460. struct kvm_tpr_access_ctl tac;
  1461. r = -EFAULT;
  1462. if (copy_from_user(&tac, argp, sizeof tac))
  1463. goto out;
  1464. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  1465. if (r)
  1466. goto out;
  1467. r = -EFAULT;
  1468. if (copy_to_user(argp, &tac, sizeof tac))
  1469. goto out;
  1470. r = 0;
  1471. break;
  1472. };
  1473. case KVM_SET_VAPIC_ADDR: {
  1474. struct kvm_vapic_addr va;
  1475. r = -EINVAL;
  1476. if (!irqchip_in_kernel(vcpu->kvm))
  1477. goto out;
  1478. r = -EFAULT;
  1479. if (copy_from_user(&va, argp, sizeof va))
  1480. goto out;
  1481. r = 0;
  1482. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  1483. break;
  1484. }
  1485. default:
  1486. r = -EINVAL;
  1487. }
  1488. out:
  1489. if (lapic)
  1490. kfree(lapic);
  1491. return r;
  1492. }
  1493. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  1494. {
  1495. int ret;
  1496. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  1497. return -1;
  1498. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  1499. return ret;
  1500. }
  1501. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  1502. u32 kvm_nr_mmu_pages)
  1503. {
  1504. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  1505. return -EINVAL;
  1506. down_write(&kvm->slots_lock);
  1507. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  1508. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  1509. up_write(&kvm->slots_lock);
  1510. return 0;
  1511. }
  1512. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  1513. {
  1514. return kvm->arch.n_alloc_mmu_pages;
  1515. }
  1516. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  1517. {
  1518. int i;
  1519. struct kvm_mem_alias *alias;
  1520. for (i = 0; i < kvm->arch.naliases; ++i) {
  1521. alias = &kvm->arch.aliases[i];
  1522. if (gfn >= alias->base_gfn
  1523. && gfn < alias->base_gfn + alias->npages)
  1524. return alias->target_gfn + gfn - alias->base_gfn;
  1525. }
  1526. return gfn;
  1527. }
  1528. /*
  1529. * Set a new alias region. Aliases map a portion of physical memory into
  1530. * another portion. This is useful for memory windows, for example the PC
  1531. * VGA region.
  1532. */
  1533. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  1534. struct kvm_memory_alias *alias)
  1535. {
  1536. int r, n;
  1537. struct kvm_mem_alias *p;
  1538. r = -EINVAL;
  1539. /* General sanity checks */
  1540. if (alias->memory_size & (PAGE_SIZE - 1))
  1541. goto out;
  1542. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  1543. goto out;
  1544. if (alias->slot >= KVM_ALIAS_SLOTS)
  1545. goto out;
  1546. if (alias->guest_phys_addr + alias->memory_size
  1547. < alias->guest_phys_addr)
  1548. goto out;
  1549. if (alias->target_phys_addr + alias->memory_size
  1550. < alias->target_phys_addr)
  1551. goto out;
  1552. down_write(&kvm->slots_lock);
  1553. spin_lock(&kvm->mmu_lock);
  1554. p = &kvm->arch.aliases[alias->slot];
  1555. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  1556. p->npages = alias->memory_size >> PAGE_SHIFT;
  1557. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  1558. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  1559. if (kvm->arch.aliases[n - 1].npages)
  1560. break;
  1561. kvm->arch.naliases = n;
  1562. spin_unlock(&kvm->mmu_lock);
  1563. kvm_mmu_zap_all(kvm);
  1564. up_write(&kvm->slots_lock);
  1565. return 0;
  1566. out:
  1567. return r;
  1568. }
  1569. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1570. {
  1571. int r;
  1572. r = 0;
  1573. switch (chip->chip_id) {
  1574. case KVM_IRQCHIP_PIC_MASTER:
  1575. memcpy(&chip->chip.pic,
  1576. &pic_irqchip(kvm)->pics[0],
  1577. sizeof(struct kvm_pic_state));
  1578. break;
  1579. case KVM_IRQCHIP_PIC_SLAVE:
  1580. memcpy(&chip->chip.pic,
  1581. &pic_irqchip(kvm)->pics[1],
  1582. sizeof(struct kvm_pic_state));
  1583. break;
  1584. case KVM_IRQCHIP_IOAPIC:
  1585. memcpy(&chip->chip.ioapic,
  1586. ioapic_irqchip(kvm),
  1587. sizeof(struct kvm_ioapic_state));
  1588. break;
  1589. default:
  1590. r = -EINVAL;
  1591. break;
  1592. }
  1593. return r;
  1594. }
  1595. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1596. {
  1597. int r;
  1598. r = 0;
  1599. switch (chip->chip_id) {
  1600. case KVM_IRQCHIP_PIC_MASTER:
  1601. memcpy(&pic_irqchip(kvm)->pics[0],
  1602. &chip->chip.pic,
  1603. sizeof(struct kvm_pic_state));
  1604. break;
  1605. case KVM_IRQCHIP_PIC_SLAVE:
  1606. memcpy(&pic_irqchip(kvm)->pics[1],
  1607. &chip->chip.pic,
  1608. sizeof(struct kvm_pic_state));
  1609. break;
  1610. case KVM_IRQCHIP_IOAPIC:
  1611. memcpy(ioapic_irqchip(kvm),
  1612. &chip->chip.ioapic,
  1613. sizeof(struct kvm_ioapic_state));
  1614. break;
  1615. default:
  1616. r = -EINVAL;
  1617. break;
  1618. }
  1619. kvm_pic_update_irq(pic_irqchip(kvm));
  1620. return r;
  1621. }
  1622. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1623. {
  1624. int r = 0;
  1625. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  1626. return r;
  1627. }
  1628. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1629. {
  1630. int r = 0;
  1631. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  1632. kvm_pit_load_count(kvm, 0, ps->channels[0].count);
  1633. return r;
  1634. }
  1635. /*
  1636. * Get (and clear) the dirty memory log for a memory slot.
  1637. */
  1638. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1639. struct kvm_dirty_log *log)
  1640. {
  1641. int r;
  1642. int n;
  1643. struct kvm_memory_slot *memslot;
  1644. int is_dirty = 0;
  1645. down_write(&kvm->slots_lock);
  1646. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1647. if (r)
  1648. goto out;
  1649. /* If nothing is dirty, don't bother messing with page tables. */
  1650. if (is_dirty) {
  1651. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  1652. kvm_flush_remote_tlbs(kvm);
  1653. memslot = &kvm->memslots[log->slot];
  1654. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  1655. memset(memslot->dirty_bitmap, 0, n);
  1656. }
  1657. r = 0;
  1658. out:
  1659. up_write(&kvm->slots_lock);
  1660. return r;
  1661. }
  1662. long kvm_arch_vm_ioctl(struct file *filp,
  1663. unsigned int ioctl, unsigned long arg)
  1664. {
  1665. struct kvm *kvm = filp->private_data;
  1666. void __user *argp = (void __user *)arg;
  1667. int r = -EINVAL;
  1668. /*
  1669. * This union makes it completely explicit to gcc-3.x
  1670. * that these two variables' stack usage should be
  1671. * combined, not added together.
  1672. */
  1673. union {
  1674. struct kvm_pit_state ps;
  1675. struct kvm_memory_alias alias;
  1676. } u;
  1677. switch (ioctl) {
  1678. case KVM_SET_TSS_ADDR:
  1679. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  1680. if (r < 0)
  1681. goto out;
  1682. break;
  1683. case KVM_SET_MEMORY_REGION: {
  1684. struct kvm_memory_region kvm_mem;
  1685. struct kvm_userspace_memory_region kvm_userspace_mem;
  1686. r = -EFAULT;
  1687. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1688. goto out;
  1689. kvm_userspace_mem.slot = kvm_mem.slot;
  1690. kvm_userspace_mem.flags = kvm_mem.flags;
  1691. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  1692. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  1693. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1694. if (r)
  1695. goto out;
  1696. break;
  1697. }
  1698. case KVM_SET_NR_MMU_PAGES:
  1699. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  1700. if (r)
  1701. goto out;
  1702. break;
  1703. case KVM_GET_NR_MMU_PAGES:
  1704. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  1705. break;
  1706. case KVM_SET_MEMORY_ALIAS:
  1707. r = -EFAULT;
  1708. if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
  1709. goto out;
  1710. r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
  1711. if (r)
  1712. goto out;
  1713. break;
  1714. case KVM_CREATE_IRQCHIP:
  1715. r = -ENOMEM;
  1716. kvm->arch.vpic = kvm_create_pic(kvm);
  1717. if (kvm->arch.vpic) {
  1718. r = kvm_ioapic_init(kvm);
  1719. if (r) {
  1720. kfree(kvm->arch.vpic);
  1721. kvm->arch.vpic = NULL;
  1722. goto out;
  1723. }
  1724. } else
  1725. goto out;
  1726. break;
  1727. case KVM_CREATE_PIT:
  1728. r = -ENOMEM;
  1729. kvm->arch.vpit = kvm_create_pit(kvm);
  1730. if (kvm->arch.vpit)
  1731. r = 0;
  1732. break;
  1733. case KVM_IRQ_LINE: {
  1734. struct kvm_irq_level irq_event;
  1735. r = -EFAULT;
  1736. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1737. goto out;
  1738. if (irqchip_in_kernel(kvm)) {
  1739. mutex_lock(&kvm->lock);
  1740. kvm_set_irq(kvm, irq_event.irq, irq_event.level);
  1741. mutex_unlock(&kvm->lock);
  1742. r = 0;
  1743. }
  1744. break;
  1745. }
  1746. case KVM_GET_IRQCHIP: {
  1747. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1748. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  1749. r = -ENOMEM;
  1750. if (!chip)
  1751. goto out;
  1752. r = -EFAULT;
  1753. if (copy_from_user(chip, argp, sizeof *chip))
  1754. goto get_irqchip_out;
  1755. r = -ENXIO;
  1756. if (!irqchip_in_kernel(kvm))
  1757. goto get_irqchip_out;
  1758. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  1759. if (r)
  1760. goto get_irqchip_out;
  1761. r = -EFAULT;
  1762. if (copy_to_user(argp, chip, sizeof *chip))
  1763. goto get_irqchip_out;
  1764. r = 0;
  1765. get_irqchip_out:
  1766. kfree(chip);
  1767. if (r)
  1768. goto out;
  1769. break;
  1770. }
  1771. case KVM_SET_IRQCHIP: {
  1772. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1773. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  1774. r = -ENOMEM;
  1775. if (!chip)
  1776. goto out;
  1777. r = -EFAULT;
  1778. if (copy_from_user(chip, argp, sizeof *chip))
  1779. goto set_irqchip_out;
  1780. r = -ENXIO;
  1781. if (!irqchip_in_kernel(kvm))
  1782. goto set_irqchip_out;
  1783. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  1784. if (r)
  1785. goto set_irqchip_out;
  1786. r = 0;
  1787. set_irqchip_out:
  1788. kfree(chip);
  1789. if (r)
  1790. goto out;
  1791. break;
  1792. }
  1793. case KVM_ASSIGN_PCI_DEVICE: {
  1794. struct kvm_assigned_pci_dev assigned_dev;
  1795. r = -EFAULT;
  1796. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1797. goto out;
  1798. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  1799. if (r)
  1800. goto out;
  1801. break;
  1802. }
  1803. case KVM_ASSIGN_IRQ: {
  1804. struct kvm_assigned_irq assigned_irq;
  1805. r = -EFAULT;
  1806. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  1807. goto out;
  1808. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  1809. if (r)
  1810. goto out;
  1811. break;
  1812. }
  1813. case KVM_GET_PIT: {
  1814. r = -EFAULT;
  1815. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  1816. goto out;
  1817. r = -ENXIO;
  1818. if (!kvm->arch.vpit)
  1819. goto out;
  1820. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  1821. if (r)
  1822. goto out;
  1823. r = -EFAULT;
  1824. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  1825. goto out;
  1826. r = 0;
  1827. break;
  1828. }
  1829. case KVM_SET_PIT: {
  1830. r = -EFAULT;
  1831. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  1832. goto out;
  1833. r = -ENXIO;
  1834. if (!kvm->arch.vpit)
  1835. goto out;
  1836. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  1837. if (r)
  1838. goto out;
  1839. r = 0;
  1840. break;
  1841. }
  1842. default:
  1843. ;
  1844. }
  1845. out:
  1846. return r;
  1847. }
  1848. static void kvm_init_msr_list(void)
  1849. {
  1850. u32 dummy[2];
  1851. unsigned i, j;
  1852. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1853. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1854. continue;
  1855. if (j < i)
  1856. msrs_to_save[j] = msrs_to_save[i];
  1857. j++;
  1858. }
  1859. num_msrs_to_save = j;
  1860. }
  1861. /*
  1862. * Only apic need an MMIO device hook, so shortcut now..
  1863. */
  1864. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  1865. gpa_t addr, int len,
  1866. int is_write)
  1867. {
  1868. struct kvm_io_device *dev;
  1869. if (vcpu->arch.apic) {
  1870. dev = &vcpu->arch.apic->dev;
  1871. if (dev->in_range(dev, addr, len, is_write))
  1872. return dev;
  1873. }
  1874. return NULL;
  1875. }
  1876. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  1877. gpa_t addr, int len,
  1878. int is_write)
  1879. {
  1880. struct kvm_io_device *dev;
  1881. dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
  1882. if (dev == NULL)
  1883. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
  1884. is_write);
  1885. return dev;
  1886. }
  1887. int emulator_read_std(unsigned long addr,
  1888. void *val,
  1889. unsigned int bytes,
  1890. struct kvm_vcpu *vcpu)
  1891. {
  1892. void *data = val;
  1893. int r = X86EMUL_CONTINUE;
  1894. while (bytes) {
  1895. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1896. unsigned offset = addr & (PAGE_SIZE-1);
  1897. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  1898. int ret;
  1899. if (gpa == UNMAPPED_GVA) {
  1900. r = X86EMUL_PROPAGATE_FAULT;
  1901. goto out;
  1902. }
  1903. ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
  1904. if (ret < 0) {
  1905. r = X86EMUL_UNHANDLEABLE;
  1906. goto out;
  1907. }
  1908. bytes -= tocopy;
  1909. data += tocopy;
  1910. addr += tocopy;
  1911. }
  1912. out:
  1913. return r;
  1914. }
  1915. EXPORT_SYMBOL_GPL(emulator_read_std);
  1916. static int emulator_read_emulated(unsigned long addr,
  1917. void *val,
  1918. unsigned int bytes,
  1919. struct kvm_vcpu *vcpu)
  1920. {
  1921. struct kvm_io_device *mmio_dev;
  1922. gpa_t gpa;
  1923. if (vcpu->mmio_read_completed) {
  1924. memcpy(val, vcpu->mmio_data, bytes);
  1925. vcpu->mmio_read_completed = 0;
  1926. return X86EMUL_CONTINUE;
  1927. }
  1928. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1929. /* For APIC access vmexit */
  1930. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1931. goto mmio;
  1932. if (emulator_read_std(addr, val, bytes, vcpu)
  1933. == X86EMUL_CONTINUE)
  1934. return X86EMUL_CONTINUE;
  1935. if (gpa == UNMAPPED_GVA)
  1936. return X86EMUL_PROPAGATE_FAULT;
  1937. mmio:
  1938. /*
  1939. * Is this MMIO handled locally?
  1940. */
  1941. mutex_lock(&vcpu->kvm->lock);
  1942. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
  1943. if (mmio_dev) {
  1944. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  1945. mutex_unlock(&vcpu->kvm->lock);
  1946. return X86EMUL_CONTINUE;
  1947. }
  1948. mutex_unlock(&vcpu->kvm->lock);
  1949. vcpu->mmio_needed = 1;
  1950. vcpu->mmio_phys_addr = gpa;
  1951. vcpu->mmio_size = bytes;
  1952. vcpu->mmio_is_write = 0;
  1953. return X86EMUL_UNHANDLEABLE;
  1954. }
  1955. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  1956. const void *val, int bytes)
  1957. {
  1958. int ret;
  1959. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  1960. if (ret < 0)
  1961. return 0;
  1962. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  1963. return 1;
  1964. }
  1965. static int emulator_write_emulated_onepage(unsigned long addr,
  1966. const void *val,
  1967. unsigned int bytes,
  1968. struct kvm_vcpu *vcpu)
  1969. {
  1970. struct kvm_io_device *mmio_dev;
  1971. gpa_t gpa;
  1972. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1973. if (gpa == UNMAPPED_GVA) {
  1974. kvm_inject_page_fault(vcpu, addr, 2);
  1975. return X86EMUL_PROPAGATE_FAULT;
  1976. }
  1977. /* For APIC access vmexit */
  1978. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1979. goto mmio;
  1980. if (emulator_write_phys(vcpu, gpa, val, bytes))
  1981. return X86EMUL_CONTINUE;
  1982. mmio:
  1983. /*
  1984. * Is this MMIO handled locally?
  1985. */
  1986. mutex_lock(&vcpu->kvm->lock);
  1987. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
  1988. if (mmio_dev) {
  1989. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  1990. mutex_unlock(&vcpu->kvm->lock);
  1991. return X86EMUL_CONTINUE;
  1992. }
  1993. mutex_unlock(&vcpu->kvm->lock);
  1994. vcpu->mmio_needed = 1;
  1995. vcpu->mmio_phys_addr = gpa;
  1996. vcpu->mmio_size = bytes;
  1997. vcpu->mmio_is_write = 1;
  1998. memcpy(vcpu->mmio_data, val, bytes);
  1999. return X86EMUL_CONTINUE;
  2000. }
  2001. int emulator_write_emulated(unsigned long addr,
  2002. const void *val,
  2003. unsigned int bytes,
  2004. struct kvm_vcpu *vcpu)
  2005. {
  2006. /* Crossing a page boundary? */
  2007. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  2008. int rc, now;
  2009. now = -addr & ~PAGE_MASK;
  2010. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  2011. if (rc != X86EMUL_CONTINUE)
  2012. return rc;
  2013. addr += now;
  2014. val += now;
  2015. bytes -= now;
  2016. }
  2017. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  2018. }
  2019. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  2020. static int emulator_cmpxchg_emulated(unsigned long addr,
  2021. const void *old,
  2022. const void *new,
  2023. unsigned int bytes,
  2024. struct kvm_vcpu *vcpu)
  2025. {
  2026. static int reported;
  2027. if (!reported) {
  2028. reported = 1;
  2029. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  2030. }
  2031. #ifndef CONFIG_X86_64
  2032. /* guests cmpxchg8b have to be emulated atomically */
  2033. if (bytes == 8) {
  2034. gpa_t gpa;
  2035. struct page *page;
  2036. char *kaddr;
  2037. u64 val;
  2038. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2039. if (gpa == UNMAPPED_GVA ||
  2040. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2041. goto emul_write;
  2042. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  2043. goto emul_write;
  2044. val = *(u64 *)new;
  2045. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  2046. kaddr = kmap_atomic(page, KM_USER0);
  2047. set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
  2048. kunmap_atomic(kaddr, KM_USER0);
  2049. kvm_release_page_dirty(page);
  2050. }
  2051. emul_write:
  2052. #endif
  2053. return emulator_write_emulated(addr, new, bytes, vcpu);
  2054. }
  2055. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2056. {
  2057. return kvm_x86_ops->get_segment_base(vcpu, seg);
  2058. }
  2059. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  2060. {
  2061. kvm_mmu_invlpg(vcpu, address);
  2062. return X86EMUL_CONTINUE;
  2063. }
  2064. int emulate_clts(struct kvm_vcpu *vcpu)
  2065. {
  2066. KVMTRACE_0D(CLTS, vcpu, handler);
  2067. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
  2068. return X86EMUL_CONTINUE;
  2069. }
  2070. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  2071. {
  2072. struct kvm_vcpu *vcpu = ctxt->vcpu;
  2073. switch (dr) {
  2074. case 0 ... 3:
  2075. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  2076. return X86EMUL_CONTINUE;
  2077. default:
  2078. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
  2079. return X86EMUL_UNHANDLEABLE;
  2080. }
  2081. }
  2082. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  2083. {
  2084. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  2085. int exception;
  2086. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  2087. if (exception) {
  2088. /* FIXME: better handling */
  2089. return X86EMUL_UNHANDLEABLE;
  2090. }
  2091. return X86EMUL_CONTINUE;
  2092. }
  2093. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  2094. {
  2095. u8 opcodes[4];
  2096. unsigned long rip = kvm_rip_read(vcpu);
  2097. unsigned long rip_linear;
  2098. if (!printk_ratelimit())
  2099. return;
  2100. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  2101. emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
  2102. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  2103. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  2104. }
  2105. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  2106. static struct x86_emulate_ops emulate_ops = {
  2107. .read_std = emulator_read_std,
  2108. .read_emulated = emulator_read_emulated,
  2109. .write_emulated = emulator_write_emulated,
  2110. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  2111. };
  2112. static void cache_all_regs(struct kvm_vcpu *vcpu)
  2113. {
  2114. kvm_register_read(vcpu, VCPU_REGS_RAX);
  2115. kvm_register_read(vcpu, VCPU_REGS_RSP);
  2116. kvm_register_read(vcpu, VCPU_REGS_RIP);
  2117. vcpu->arch.regs_dirty = ~0;
  2118. }
  2119. int emulate_instruction(struct kvm_vcpu *vcpu,
  2120. struct kvm_run *run,
  2121. unsigned long cr2,
  2122. u16 error_code,
  2123. int emulation_type)
  2124. {
  2125. int r;
  2126. struct decode_cache *c;
  2127. kvm_clear_exception_queue(vcpu);
  2128. vcpu->arch.mmio_fault_cr2 = cr2;
  2129. /*
  2130. * TODO: fix x86_emulate.c to use guest_read/write_register
  2131. * instead of direct ->regs accesses, can save hundred cycles
  2132. * on Intel for instructions that don't read/change RSP, for
  2133. * for example.
  2134. */
  2135. cache_all_regs(vcpu);
  2136. vcpu->mmio_is_write = 0;
  2137. vcpu->arch.pio.string = 0;
  2138. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  2139. int cs_db, cs_l;
  2140. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  2141. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  2142. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  2143. vcpu->arch.emulate_ctxt.mode =
  2144. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  2145. ? X86EMUL_MODE_REAL : cs_l
  2146. ? X86EMUL_MODE_PROT64 : cs_db
  2147. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  2148. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2149. /* Reject the instructions other than VMCALL/VMMCALL when
  2150. * try to emulate invalid opcode */
  2151. c = &vcpu->arch.emulate_ctxt.decode;
  2152. if ((emulation_type & EMULTYPE_TRAP_UD) &&
  2153. (!(c->twobyte && c->b == 0x01 &&
  2154. (c->modrm_reg == 0 || c->modrm_reg == 3) &&
  2155. c->modrm_mod == 3 && c->modrm_rm == 1)))
  2156. return EMULATE_FAIL;
  2157. ++vcpu->stat.insn_emulation;
  2158. if (r) {
  2159. ++vcpu->stat.insn_emulation_fail;
  2160. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2161. return EMULATE_DONE;
  2162. return EMULATE_FAIL;
  2163. }
  2164. }
  2165. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2166. if (vcpu->arch.pio.string)
  2167. return EMULATE_DO_MMIO;
  2168. if ((r || vcpu->mmio_is_write) && run) {
  2169. run->exit_reason = KVM_EXIT_MMIO;
  2170. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  2171. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  2172. run->mmio.len = vcpu->mmio_size;
  2173. run->mmio.is_write = vcpu->mmio_is_write;
  2174. }
  2175. if (r) {
  2176. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2177. return EMULATE_DONE;
  2178. if (!vcpu->mmio_needed) {
  2179. kvm_report_emulation_failure(vcpu, "mmio");
  2180. return EMULATE_FAIL;
  2181. }
  2182. return EMULATE_DO_MMIO;
  2183. }
  2184. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  2185. if (vcpu->mmio_is_write) {
  2186. vcpu->mmio_needed = 0;
  2187. return EMULATE_DO_MMIO;
  2188. }
  2189. return EMULATE_DONE;
  2190. }
  2191. EXPORT_SYMBOL_GPL(emulate_instruction);
  2192. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  2193. {
  2194. int i;
  2195. for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
  2196. if (vcpu->arch.pio.guest_pages[i]) {
  2197. kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
  2198. vcpu->arch.pio.guest_pages[i] = NULL;
  2199. }
  2200. }
  2201. static int pio_copy_data(struct kvm_vcpu *vcpu)
  2202. {
  2203. void *p = vcpu->arch.pio_data;
  2204. void *q;
  2205. unsigned bytes;
  2206. int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
  2207. q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  2208. PAGE_KERNEL);
  2209. if (!q) {
  2210. free_pio_guest_pages(vcpu);
  2211. return -ENOMEM;
  2212. }
  2213. q += vcpu->arch.pio.guest_page_offset;
  2214. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  2215. if (vcpu->arch.pio.in)
  2216. memcpy(q, p, bytes);
  2217. else
  2218. memcpy(p, q, bytes);
  2219. q -= vcpu->arch.pio.guest_page_offset;
  2220. vunmap(q);
  2221. free_pio_guest_pages(vcpu);
  2222. return 0;
  2223. }
  2224. int complete_pio(struct kvm_vcpu *vcpu)
  2225. {
  2226. struct kvm_pio_request *io = &vcpu->arch.pio;
  2227. long delta;
  2228. int r;
  2229. unsigned long val;
  2230. if (!io->string) {
  2231. if (io->in) {
  2232. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2233. memcpy(&val, vcpu->arch.pio_data, io->size);
  2234. kvm_register_write(vcpu, VCPU_REGS_RAX, val);
  2235. }
  2236. } else {
  2237. if (io->in) {
  2238. r = pio_copy_data(vcpu);
  2239. if (r)
  2240. return r;
  2241. }
  2242. delta = 1;
  2243. if (io->rep) {
  2244. delta *= io->cur_count;
  2245. /*
  2246. * The size of the register should really depend on
  2247. * current address size.
  2248. */
  2249. val = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2250. val -= delta;
  2251. kvm_register_write(vcpu, VCPU_REGS_RCX, val);
  2252. }
  2253. if (io->down)
  2254. delta = -delta;
  2255. delta *= io->size;
  2256. if (io->in) {
  2257. val = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2258. val += delta;
  2259. kvm_register_write(vcpu, VCPU_REGS_RDI, val);
  2260. } else {
  2261. val = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2262. val += delta;
  2263. kvm_register_write(vcpu, VCPU_REGS_RSI, val);
  2264. }
  2265. }
  2266. io->count -= io->cur_count;
  2267. io->cur_count = 0;
  2268. return 0;
  2269. }
  2270. static void kernel_pio(struct kvm_io_device *pio_dev,
  2271. struct kvm_vcpu *vcpu,
  2272. void *pd)
  2273. {
  2274. /* TODO: String I/O for in kernel device */
  2275. mutex_lock(&vcpu->kvm->lock);
  2276. if (vcpu->arch.pio.in)
  2277. kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
  2278. vcpu->arch.pio.size,
  2279. pd);
  2280. else
  2281. kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
  2282. vcpu->arch.pio.size,
  2283. pd);
  2284. mutex_unlock(&vcpu->kvm->lock);
  2285. }
  2286. static void pio_string_write(struct kvm_io_device *pio_dev,
  2287. struct kvm_vcpu *vcpu)
  2288. {
  2289. struct kvm_pio_request *io = &vcpu->arch.pio;
  2290. void *pd = vcpu->arch.pio_data;
  2291. int i;
  2292. mutex_lock(&vcpu->kvm->lock);
  2293. for (i = 0; i < io->cur_count; i++) {
  2294. kvm_iodevice_write(pio_dev, io->port,
  2295. io->size,
  2296. pd);
  2297. pd += io->size;
  2298. }
  2299. mutex_unlock(&vcpu->kvm->lock);
  2300. }
  2301. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  2302. gpa_t addr, int len,
  2303. int is_write)
  2304. {
  2305. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
  2306. }
  2307. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2308. int size, unsigned port)
  2309. {
  2310. struct kvm_io_device *pio_dev;
  2311. unsigned long val;
  2312. vcpu->run->exit_reason = KVM_EXIT_IO;
  2313. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2314. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2315. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2316. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  2317. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2318. vcpu->arch.pio.in = in;
  2319. vcpu->arch.pio.string = 0;
  2320. vcpu->arch.pio.down = 0;
  2321. vcpu->arch.pio.guest_page_offset = 0;
  2322. vcpu->arch.pio.rep = 0;
  2323. if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
  2324. KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
  2325. handler);
  2326. else
  2327. KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
  2328. handler);
  2329. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2330. memcpy(vcpu->arch.pio_data, &val, 4);
  2331. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2332. pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
  2333. if (pio_dev) {
  2334. kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
  2335. complete_pio(vcpu);
  2336. return 1;
  2337. }
  2338. return 0;
  2339. }
  2340. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  2341. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2342. int size, unsigned long count, int down,
  2343. gva_t address, int rep, unsigned port)
  2344. {
  2345. unsigned now, in_page;
  2346. int i, ret = 0;
  2347. int nr_pages = 1;
  2348. struct page *page;
  2349. struct kvm_io_device *pio_dev;
  2350. vcpu->run->exit_reason = KVM_EXIT_IO;
  2351. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2352. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2353. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2354. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  2355. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2356. vcpu->arch.pio.in = in;
  2357. vcpu->arch.pio.string = 1;
  2358. vcpu->arch.pio.down = down;
  2359. vcpu->arch.pio.guest_page_offset = offset_in_page(address);
  2360. vcpu->arch.pio.rep = rep;
  2361. if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
  2362. KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
  2363. handler);
  2364. else
  2365. KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
  2366. handler);
  2367. if (!count) {
  2368. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2369. return 1;
  2370. }
  2371. if (!down)
  2372. in_page = PAGE_SIZE - offset_in_page(address);
  2373. else
  2374. in_page = offset_in_page(address) + size;
  2375. now = min(count, (unsigned long)in_page / size);
  2376. if (!now) {
  2377. /*
  2378. * String I/O straddles page boundary. Pin two guest pages
  2379. * so that we satisfy atomicity constraints. Do just one
  2380. * transaction to avoid complexity.
  2381. */
  2382. nr_pages = 2;
  2383. now = 1;
  2384. }
  2385. if (down) {
  2386. /*
  2387. * String I/O in reverse. Yuck. Kill the guest, fix later.
  2388. */
  2389. pr_unimpl(vcpu, "guest string pio down\n");
  2390. kvm_inject_gp(vcpu, 0);
  2391. return 1;
  2392. }
  2393. vcpu->run->io.count = now;
  2394. vcpu->arch.pio.cur_count = now;
  2395. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  2396. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2397. for (i = 0; i < nr_pages; ++i) {
  2398. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  2399. vcpu->arch.pio.guest_pages[i] = page;
  2400. if (!page) {
  2401. kvm_inject_gp(vcpu, 0);
  2402. free_pio_guest_pages(vcpu);
  2403. return 1;
  2404. }
  2405. }
  2406. pio_dev = vcpu_find_pio_dev(vcpu, port,
  2407. vcpu->arch.pio.cur_count,
  2408. !vcpu->arch.pio.in);
  2409. if (!vcpu->arch.pio.in) {
  2410. /* string PIO write */
  2411. ret = pio_copy_data(vcpu);
  2412. if (ret >= 0 && pio_dev) {
  2413. pio_string_write(pio_dev, vcpu);
  2414. complete_pio(vcpu);
  2415. if (vcpu->arch.pio.count == 0)
  2416. ret = 1;
  2417. }
  2418. } else if (pio_dev)
  2419. pr_unimpl(vcpu, "no string pio read support yet, "
  2420. "port %x size %d count %ld\n",
  2421. port, size, count);
  2422. return ret;
  2423. }
  2424. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  2425. int kvm_arch_init(void *opaque)
  2426. {
  2427. int r;
  2428. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  2429. if (kvm_x86_ops) {
  2430. printk(KERN_ERR "kvm: already loaded the other module\n");
  2431. r = -EEXIST;
  2432. goto out;
  2433. }
  2434. if (!ops->cpu_has_kvm_support()) {
  2435. printk(KERN_ERR "kvm: no hardware support\n");
  2436. r = -EOPNOTSUPP;
  2437. goto out;
  2438. }
  2439. if (ops->disabled_by_bios()) {
  2440. printk(KERN_ERR "kvm: disabled by bios\n");
  2441. r = -EOPNOTSUPP;
  2442. goto out;
  2443. }
  2444. r = kvm_mmu_module_init();
  2445. if (r)
  2446. goto out;
  2447. kvm_init_msr_list();
  2448. kvm_x86_ops = ops;
  2449. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  2450. kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
  2451. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  2452. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  2453. return 0;
  2454. out:
  2455. return r;
  2456. }
  2457. void kvm_arch_exit(void)
  2458. {
  2459. kvm_x86_ops = NULL;
  2460. kvm_mmu_module_exit();
  2461. }
  2462. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  2463. {
  2464. ++vcpu->stat.halt_exits;
  2465. KVMTRACE_0D(HLT, vcpu, handler);
  2466. if (irqchip_in_kernel(vcpu->kvm)) {
  2467. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  2468. return 1;
  2469. } else {
  2470. vcpu->run->exit_reason = KVM_EXIT_HLT;
  2471. return 0;
  2472. }
  2473. }
  2474. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  2475. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  2476. unsigned long a1)
  2477. {
  2478. if (is_long_mode(vcpu))
  2479. return a0;
  2480. else
  2481. return a0 | ((gpa_t)a1 << 32);
  2482. }
  2483. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  2484. {
  2485. unsigned long nr, a0, a1, a2, a3, ret;
  2486. int r = 1;
  2487. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2488. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2489. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2490. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2491. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2492. KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
  2493. if (!is_long_mode(vcpu)) {
  2494. nr &= 0xFFFFFFFF;
  2495. a0 &= 0xFFFFFFFF;
  2496. a1 &= 0xFFFFFFFF;
  2497. a2 &= 0xFFFFFFFF;
  2498. a3 &= 0xFFFFFFFF;
  2499. }
  2500. switch (nr) {
  2501. case KVM_HC_VAPIC_POLL_IRQ:
  2502. ret = 0;
  2503. break;
  2504. case KVM_HC_MMU_OP:
  2505. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  2506. break;
  2507. default:
  2508. ret = -KVM_ENOSYS;
  2509. break;
  2510. }
  2511. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  2512. ++vcpu->stat.hypercalls;
  2513. return r;
  2514. }
  2515. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  2516. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  2517. {
  2518. char instruction[3];
  2519. int ret = 0;
  2520. unsigned long rip = kvm_rip_read(vcpu);
  2521. /*
  2522. * Blow out the MMU to ensure that no other VCPU has an active mapping
  2523. * to ensure that the updated hypercall appears atomically across all
  2524. * VCPUs.
  2525. */
  2526. kvm_mmu_zap_all(vcpu->kvm);
  2527. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  2528. if (emulator_write_emulated(rip, instruction, 3, vcpu)
  2529. != X86EMUL_CONTINUE)
  2530. ret = -EFAULT;
  2531. return ret;
  2532. }
  2533. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  2534. {
  2535. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  2536. }
  2537. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2538. {
  2539. struct descriptor_table dt = { limit, base };
  2540. kvm_x86_ops->set_gdt(vcpu, &dt);
  2541. }
  2542. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2543. {
  2544. struct descriptor_table dt = { limit, base };
  2545. kvm_x86_ops->set_idt(vcpu, &dt);
  2546. }
  2547. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  2548. unsigned long *rflags)
  2549. {
  2550. kvm_lmsw(vcpu, msw);
  2551. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2552. }
  2553. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  2554. {
  2555. unsigned long value;
  2556. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2557. switch (cr) {
  2558. case 0:
  2559. value = vcpu->arch.cr0;
  2560. break;
  2561. case 2:
  2562. value = vcpu->arch.cr2;
  2563. break;
  2564. case 3:
  2565. value = vcpu->arch.cr3;
  2566. break;
  2567. case 4:
  2568. value = vcpu->arch.cr4;
  2569. break;
  2570. case 8:
  2571. value = kvm_get_cr8(vcpu);
  2572. break;
  2573. default:
  2574. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2575. return 0;
  2576. }
  2577. KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
  2578. (u32)((u64)value >> 32), handler);
  2579. return value;
  2580. }
  2581. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  2582. unsigned long *rflags)
  2583. {
  2584. KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
  2585. (u32)((u64)val >> 32), handler);
  2586. switch (cr) {
  2587. case 0:
  2588. kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
  2589. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2590. break;
  2591. case 2:
  2592. vcpu->arch.cr2 = val;
  2593. break;
  2594. case 3:
  2595. kvm_set_cr3(vcpu, val);
  2596. break;
  2597. case 4:
  2598. kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
  2599. break;
  2600. case 8:
  2601. kvm_set_cr8(vcpu, val & 0xfUL);
  2602. break;
  2603. default:
  2604. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2605. }
  2606. }
  2607. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  2608. {
  2609. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  2610. int j, nent = vcpu->arch.cpuid_nent;
  2611. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  2612. /* when no next entry is found, the current entry[i] is reselected */
  2613. for (j = i + 1; j == i; j = (j + 1) % nent) {
  2614. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  2615. if (ej->function == e->function) {
  2616. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2617. return j;
  2618. }
  2619. }
  2620. return 0; /* silence gcc, even though control never reaches here */
  2621. }
  2622. /* find an entry with matching function, matching index (if needed), and that
  2623. * should be read next (if it's stateful) */
  2624. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  2625. u32 function, u32 index)
  2626. {
  2627. if (e->function != function)
  2628. return 0;
  2629. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  2630. return 0;
  2631. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  2632. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  2633. return 0;
  2634. return 1;
  2635. }
  2636. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  2637. {
  2638. int i;
  2639. u32 function, index;
  2640. struct kvm_cpuid_entry2 *e, *best;
  2641. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2642. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2643. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  2644. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  2645. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  2646. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  2647. best = NULL;
  2648. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  2649. e = &vcpu->arch.cpuid_entries[i];
  2650. if (is_matching_cpuid_entry(e, function, index)) {
  2651. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  2652. move_to_next_stateful_cpuid_entry(vcpu, i);
  2653. best = e;
  2654. break;
  2655. }
  2656. /*
  2657. * Both basic or both extended?
  2658. */
  2659. if (((e->function ^ function) & 0x80000000) == 0)
  2660. if (!best || e->function > best->function)
  2661. best = e;
  2662. }
  2663. if (best) {
  2664. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  2665. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  2666. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  2667. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  2668. }
  2669. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2670. KVMTRACE_5D(CPUID, vcpu, function,
  2671. (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
  2672. (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
  2673. (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
  2674. (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
  2675. }
  2676. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  2677. /*
  2678. * Check if userspace requested an interrupt window, and that the
  2679. * interrupt window is open.
  2680. *
  2681. * No need to exit to userspace if we already have an interrupt queued.
  2682. */
  2683. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  2684. struct kvm_run *kvm_run)
  2685. {
  2686. return (!vcpu->arch.irq_summary &&
  2687. kvm_run->request_interrupt_window &&
  2688. vcpu->arch.interrupt_window_open &&
  2689. (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
  2690. }
  2691. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  2692. struct kvm_run *kvm_run)
  2693. {
  2694. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  2695. kvm_run->cr8 = kvm_get_cr8(vcpu);
  2696. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  2697. if (irqchip_in_kernel(vcpu->kvm))
  2698. kvm_run->ready_for_interrupt_injection = 1;
  2699. else
  2700. kvm_run->ready_for_interrupt_injection =
  2701. (vcpu->arch.interrupt_window_open &&
  2702. vcpu->arch.irq_summary == 0);
  2703. }
  2704. static void vapic_enter(struct kvm_vcpu *vcpu)
  2705. {
  2706. struct kvm_lapic *apic = vcpu->arch.apic;
  2707. struct page *page;
  2708. if (!apic || !apic->vapic_addr)
  2709. return;
  2710. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2711. vcpu->arch.apic->vapic_page = page;
  2712. }
  2713. static void vapic_exit(struct kvm_vcpu *vcpu)
  2714. {
  2715. struct kvm_lapic *apic = vcpu->arch.apic;
  2716. if (!apic || !apic->vapic_addr)
  2717. return;
  2718. down_read(&vcpu->kvm->slots_lock);
  2719. kvm_release_page_dirty(apic->vapic_page);
  2720. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2721. up_read(&vcpu->kvm->slots_lock);
  2722. }
  2723. static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2724. {
  2725. int r;
  2726. if (vcpu->requests)
  2727. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  2728. kvm_mmu_unload(vcpu);
  2729. r = kvm_mmu_reload(vcpu);
  2730. if (unlikely(r))
  2731. goto out;
  2732. if (vcpu->requests) {
  2733. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  2734. __kvm_migrate_timers(vcpu);
  2735. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  2736. kvm_x86_ops->tlb_flush(vcpu);
  2737. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  2738. &vcpu->requests)) {
  2739. kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
  2740. r = 0;
  2741. goto out;
  2742. }
  2743. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  2744. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2745. r = 0;
  2746. goto out;
  2747. }
  2748. }
  2749. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  2750. kvm_inject_pending_timer_irqs(vcpu);
  2751. preempt_disable();
  2752. kvm_x86_ops->prepare_guest_switch(vcpu);
  2753. kvm_load_guest_fpu(vcpu);
  2754. local_irq_disable();
  2755. if (vcpu->requests || need_resched() || signal_pending(current)) {
  2756. local_irq_enable();
  2757. preempt_enable();
  2758. r = 1;
  2759. goto out;
  2760. }
  2761. if (vcpu->guest_debug.enabled)
  2762. kvm_x86_ops->guest_debug_pre(vcpu);
  2763. vcpu->guest_mode = 1;
  2764. /*
  2765. * Make sure that guest_mode assignment won't happen after
  2766. * testing the pending IRQ vector bitmap.
  2767. */
  2768. smp_wmb();
  2769. if (vcpu->arch.exception.pending)
  2770. __queue_exception(vcpu);
  2771. else if (irqchip_in_kernel(vcpu->kvm))
  2772. kvm_x86_ops->inject_pending_irq(vcpu);
  2773. else
  2774. kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
  2775. kvm_lapic_sync_to_vapic(vcpu);
  2776. up_read(&vcpu->kvm->slots_lock);
  2777. kvm_guest_enter();
  2778. KVMTRACE_0D(VMENTRY, vcpu, entryexit);
  2779. kvm_x86_ops->run(vcpu, kvm_run);
  2780. vcpu->guest_mode = 0;
  2781. local_irq_enable();
  2782. ++vcpu->stat.exits;
  2783. /*
  2784. * We must have an instruction between local_irq_enable() and
  2785. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  2786. * the interrupt shadow. The stat.exits increment will do nicely.
  2787. * But we need to prevent reordering, hence this barrier():
  2788. */
  2789. barrier();
  2790. kvm_guest_exit();
  2791. preempt_enable();
  2792. down_read(&vcpu->kvm->slots_lock);
  2793. /*
  2794. * Profile KVM exit RIPs:
  2795. */
  2796. if (unlikely(prof_on == KVM_PROFILING)) {
  2797. unsigned long rip = kvm_rip_read(vcpu);
  2798. profile_hit(KVM_PROFILING, (void *)rip);
  2799. }
  2800. if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
  2801. vcpu->arch.exception.pending = false;
  2802. kvm_lapic_sync_from_vapic(vcpu);
  2803. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  2804. out:
  2805. return r;
  2806. }
  2807. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2808. {
  2809. int r;
  2810. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  2811. printk("vcpu %d received sipi with vector # %x\n",
  2812. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  2813. kvm_lapic_reset(vcpu);
  2814. r = kvm_x86_ops->vcpu_reset(vcpu);
  2815. if (r)
  2816. return r;
  2817. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  2818. }
  2819. down_read(&vcpu->kvm->slots_lock);
  2820. vapic_enter(vcpu);
  2821. r = 1;
  2822. while (r > 0) {
  2823. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  2824. r = vcpu_enter_guest(vcpu, kvm_run);
  2825. else {
  2826. up_read(&vcpu->kvm->slots_lock);
  2827. kvm_vcpu_block(vcpu);
  2828. down_read(&vcpu->kvm->slots_lock);
  2829. if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
  2830. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  2831. vcpu->arch.mp_state =
  2832. KVM_MP_STATE_RUNNABLE;
  2833. if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
  2834. r = -EINTR;
  2835. }
  2836. if (r > 0) {
  2837. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  2838. r = -EINTR;
  2839. kvm_run->exit_reason = KVM_EXIT_INTR;
  2840. ++vcpu->stat.request_irq_exits;
  2841. }
  2842. if (signal_pending(current)) {
  2843. r = -EINTR;
  2844. kvm_run->exit_reason = KVM_EXIT_INTR;
  2845. ++vcpu->stat.signal_exits;
  2846. }
  2847. if (need_resched()) {
  2848. up_read(&vcpu->kvm->slots_lock);
  2849. kvm_resched(vcpu);
  2850. down_read(&vcpu->kvm->slots_lock);
  2851. }
  2852. }
  2853. }
  2854. up_read(&vcpu->kvm->slots_lock);
  2855. post_kvm_run_save(vcpu, kvm_run);
  2856. vapic_exit(vcpu);
  2857. return r;
  2858. }
  2859. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2860. {
  2861. int r;
  2862. sigset_t sigsaved;
  2863. vcpu_load(vcpu);
  2864. if (vcpu->sigset_active)
  2865. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  2866. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  2867. kvm_vcpu_block(vcpu);
  2868. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  2869. r = -EAGAIN;
  2870. goto out;
  2871. }
  2872. /* re-sync apic's tpr */
  2873. if (!irqchip_in_kernel(vcpu->kvm))
  2874. kvm_set_cr8(vcpu, kvm_run->cr8);
  2875. if (vcpu->arch.pio.cur_count) {
  2876. r = complete_pio(vcpu);
  2877. if (r)
  2878. goto out;
  2879. }
  2880. #if CONFIG_HAS_IOMEM
  2881. if (vcpu->mmio_needed) {
  2882. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  2883. vcpu->mmio_read_completed = 1;
  2884. vcpu->mmio_needed = 0;
  2885. down_read(&vcpu->kvm->slots_lock);
  2886. r = emulate_instruction(vcpu, kvm_run,
  2887. vcpu->arch.mmio_fault_cr2, 0,
  2888. EMULTYPE_NO_DECODE);
  2889. up_read(&vcpu->kvm->slots_lock);
  2890. if (r == EMULATE_DO_MMIO) {
  2891. /*
  2892. * Read-modify-write. Back to userspace.
  2893. */
  2894. r = 0;
  2895. goto out;
  2896. }
  2897. }
  2898. #endif
  2899. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  2900. kvm_register_write(vcpu, VCPU_REGS_RAX,
  2901. kvm_run->hypercall.ret);
  2902. r = __vcpu_run(vcpu, kvm_run);
  2903. out:
  2904. if (vcpu->sigset_active)
  2905. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  2906. vcpu_put(vcpu);
  2907. return r;
  2908. }
  2909. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2910. {
  2911. vcpu_load(vcpu);
  2912. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2913. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2914. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2915. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2916. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2917. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2918. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  2919. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  2920. #ifdef CONFIG_X86_64
  2921. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  2922. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  2923. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  2924. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  2925. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  2926. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  2927. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  2928. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  2929. #endif
  2930. regs->rip = kvm_rip_read(vcpu);
  2931. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  2932. /*
  2933. * Don't leak debug flags in case they were set for guest debugging
  2934. */
  2935. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  2936. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  2937. vcpu_put(vcpu);
  2938. return 0;
  2939. }
  2940. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2941. {
  2942. vcpu_load(vcpu);
  2943. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  2944. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  2945. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  2946. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  2947. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  2948. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  2949. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  2950. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  2951. #ifdef CONFIG_X86_64
  2952. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  2953. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  2954. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  2955. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  2956. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  2957. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  2958. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  2959. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  2960. #endif
  2961. kvm_rip_write(vcpu, regs->rip);
  2962. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  2963. vcpu->arch.exception.pending = false;
  2964. vcpu_put(vcpu);
  2965. return 0;
  2966. }
  2967. void kvm_get_segment(struct kvm_vcpu *vcpu,
  2968. struct kvm_segment *var, int seg)
  2969. {
  2970. kvm_x86_ops->get_segment(vcpu, var, seg);
  2971. }
  2972. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2973. {
  2974. struct kvm_segment cs;
  2975. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2976. *db = cs.db;
  2977. *l = cs.l;
  2978. }
  2979. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2980. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  2981. struct kvm_sregs *sregs)
  2982. {
  2983. struct descriptor_table dt;
  2984. int pending_vec;
  2985. vcpu_load(vcpu);
  2986. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2987. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2988. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2989. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2990. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2991. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2992. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2993. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2994. kvm_x86_ops->get_idt(vcpu, &dt);
  2995. sregs->idt.limit = dt.limit;
  2996. sregs->idt.base = dt.base;
  2997. kvm_x86_ops->get_gdt(vcpu, &dt);
  2998. sregs->gdt.limit = dt.limit;
  2999. sregs->gdt.base = dt.base;
  3000. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3001. sregs->cr0 = vcpu->arch.cr0;
  3002. sregs->cr2 = vcpu->arch.cr2;
  3003. sregs->cr3 = vcpu->arch.cr3;
  3004. sregs->cr4 = vcpu->arch.cr4;
  3005. sregs->cr8 = kvm_get_cr8(vcpu);
  3006. sregs->efer = vcpu->arch.shadow_efer;
  3007. sregs->apic_base = kvm_get_apic_base(vcpu);
  3008. if (irqchip_in_kernel(vcpu->kvm)) {
  3009. memset(sregs->interrupt_bitmap, 0,
  3010. sizeof sregs->interrupt_bitmap);
  3011. pending_vec = kvm_x86_ops->get_irq(vcpu);
  3012. if (pending_vec >= 0)
  3013. set_bit(pending_vec,
  3014. (unsigned long *)sregs->interrupt_bitmap);
  3015. } else
  3016. memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
  3017. sizeof sregs->interrupt_bitmap);
  3018. vcpu_put(vcpu);
  3019. return 0;
  3020. }
  3021. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  3022. struct kvm_mp_state *mp_state)
  3023. {
  3024. vcpu_load(vcpu);
  3025. mp_state->mp_state = vcpu->arch.mp_state;
  3026. vcpu_put(vcpu);
  3027. return 0;
  3028. }
  3029. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  3030. struct kvm_mp_state *mp_state)
  3031. {
  3032. vcpu_load(vcpu);
  3033. vcpu->arch.mp_state = mp_state->mp_state;
  3034. vcpu_put(vcpu);
  3035. return 0;
  3036. }
  3037. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3038. struct kvm_segment *var, int seg)
  3039. {
  3040. kvm_x86_ops->set_segment(vcpu, var, seg);
  3041. }
  3042. static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
  3043. struct kvm_segment *kvm_desct)
  3044. {
  3045. kvm_desct->base = seg_desc->base0;
  3046. kvm_desct->base |= seg_desc->base1 << 16;
  3047. kvm_desct->base |= seg_desc->base2 << 24;
  3048. kvm_desct->limit = seg_desc->limit0;
  3049. kvm_desct->limit |= seg_desc->limit << 16;
  3050. if (seg_desc->g) {
  3051. kvm_desct->limit <<= 12;
  3052. kvm_desct->limit |= 0xfff;
  3053. }
  3054. kvm_desct->selector = selector;
  3055. kvm_desct->type = seg_desc->type;
  3056. kvm_desct->present = seg_desc->p;
  3057. kvm_desct->dpl = seg_desc->dpl;
  3058. kvm_desct->db = seg_desc->d;
  3059. kvm_desct->s = seg_desc->s;
  3060. kvm_desct->l = seg_desc->l;
  3061. kvm_desct->g = seg_desc->g;
  3062. kvm_desct->avl = seg_desc->avl;
  3063. if (!selector)
  3064. kvm_desct->unusable = 1;
  3065. else
  3066. kvm_desct->unusable = 0;
  3067. kvm_desct->padding = 0;
  3068. }
  3069. static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
  3070. u16 selector,
  3071. struct descriptor_table *dtable)
  3072. {
  3073. if (selector & 1 << 2) {
  3074. struct kvm_segment kvm_seg;
  3075. kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
  3076. if (kvm_seg.unusable)
  3077. dtable->limit = 0;
  3078. else
  3079. dtable->limit = kvm_seg.limit;
  3080. dtable->base = kvm_seg.base;
  3081. }
  3082. else
  3083. kvm_x86_ops->get_gdt(vcpu, dtable);
  3084. }
  3085. /* allowed just for 8 bytes segments */
  3086. static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3087. struct desc_struct *seg_desc)
  3088. {
  3089. gpa_t gpa;
  3090. struct descriptor_table dtable;
  3091. u16 index = selector >> 3;
  3092. get_segment_descritptor_dtable(vcpu, selector, &dtable);
  3093. if (dtable.limit < index * 8 + 7) {
  3094. kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
  3095. return 1;
  3096. }
  3097. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3098. gpa += index * 8;
  3099. return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
  3100. }
  3101. /* allowed just for 8 bytes segments */
  3102. static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3103. struct desc_struct *seg_desc)
  3104. {
  3105. gpa_t gpa;
  3106. struct descriptor_table dtable;
  3107. u16 index = selector >> 3;
  3108. get_segment_descritptor_dtable(vcpu, selector, &dtable);
  3109. if (dtable.limit < index * 8 + 7)
  3110. return 1;
  3111. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3112. gpa += index * 8;
  3113. return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
  3114. }
  3115. static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
  3116. struct desc_struct *seg_desc)
  3117. {
  3118. u32 base_addr;
  3119. base_addr = seg_desc->base0;
  3120. base_addr |= (seg_desc->base1 << 16);
  3121. base_addr |= (seg_desc->base2 << 24);
  3122. return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
  3123. }
  3124. static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
  3125. {
  3126. struct kvm_segment kvm_seg;
  3127. kvm_get_segment(vcpu, &kvm_seg, seg);
  3128. return kvm_seg.selector;
  3129. }
  3130. static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
  3131. u16 selector,
  3132. struct kvm_segment *kvm_seg)
  3133. {
  3134. struct desc_struct seg_desc;
  3135. if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
  3136. return 1;
  3137. seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
  3138. return 0;
  3139. }
  3140. static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
  3141. {
  3142. struct kvm_segment segvar = {
  3143. .base = selector << 4,
  3144. .limit = 0xffff,
  3145. .selector = selector,
  3146. .type = 3,
  3147. .present = 1,
  3148. .dpl = 3,
  3149. .db = 0,
  3150. .s = 1,
  3151. .l = 0,
  3152. .g = 0,
  3153. .avl = 0,
  3154. .unusable = 0,
  3155. };
  3156. kvm_x86_ops->set_segment(vcpu, &segvar, seg);
  3157. return 0;
  3158. }
  3159. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3160. int type_bits, int seg)
  3161. {
  3162. struct kvm_segment kvm_seg;
  3163. if (!(vcpu->arch.cr0 & X86_CR0_PE))
  3164. return kvm_load_realmode_segment(vcpu, selector, seg);
  3165. if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
  3166. return 1;
  3167. kvm_seg.type |= type_bits;
  3168. if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
  3169. seg != VCPU_SREG_LDTR)
  3170. if (!kvm_seg.s)
  3171. kvm_seg.unusable = 1;
  3172. kvm_set_segment(vcpu, &kvm_seg, seg);
  3173. return 0;
  3174. }
  3175. static void save_state_to_tss32(struct kvm_vcpu *vcpu,
  3176. struct tss_segment_32 *tss)
  3177. {
  3178. tss->cr3 = vcpu->arch.cr3;
  3179. tss->eip = kvm_rip_read(vcpu);
  3180. tss->eflags = kvm_x86_ops->get_rflags(vcpu);
  3181. tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3182. tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3183. tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3184. tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3185. tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3186. tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3187. tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3188. tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3189. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3190. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3191. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3192. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3193. tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
  3194. tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
  3195. tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3196. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3197. }
  3198. static int load_state_from_tss32(struct kvm_vcpu *vcpu,
  3199. struct tss_segment_32 *tss)
  3200. {
  3201. kvm_set_cr3(vcpu, tss->cr3);
  3202. kvm_rip_write(vcpu, tss->eip);
  3203. kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
  3204. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
  3205. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
  3206. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
  3207. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
  3208. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
  3209. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
  3210. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
  3211. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
  3212. if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
  3213. return 1;
  3214. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3215. return 1;
  3216. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3217. return 1;
  3218. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3219. return 1;
  3220. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3221. return 1;
  3222. if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
  3223. return 1;
  3224. if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
  3225. return 1;
  3226. return 0;
  3227. }
  3228. static void save_state_to_tss16(struct kvm_vcpu *vcpu,
  3229. struct tss_segment_16 *tss)
  3230. {
  3231. tss->ip = kvm_rip_read(vcpu);
  3232. tss->flag = kvm_x86_ops->get_rflags(vcpu);
  3233. tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3234. tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3235. tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3236. tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3237. tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3238. tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3239. tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3240. tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3241. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3242. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3243. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3244. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3245. tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3246. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3247. }
  3248. static int load_state_from_tss16(struct kvm_vcpu *vcpu,
  3249. struct tss_segment_16 *tss)
  3250. {
  3251. kvm_rip_write(vcpu, tss->ip);
  3252. kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
  3253. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
  3254. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
  3255. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
  3256. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
  3257. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
  3258. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
  3259. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
  3260. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
  3261. if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
  3262. return 1;
  3263. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3264. return 1;
  3265. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3266. return 1;
  3267. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3268. return 1;
  3269. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3270. return 1;
  3271. return 0;
  3272. }
  3273. static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
  3274. u32 old_tss_base,
  3275. struct desc_struct *nseg_desc)
  3276. {
  3277. struct tss_segment_16 tss_segment_16;
  3278. int ret = 0;
  3279. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3280. sizeof tss_segment_16))
  3281. goto out;
  3282. save_state_to_tss16(vcpu, &tss_segment_16);
  3283. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3284. sizeof tss_segment_16))
  3285. goto out;
  3286. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3287. &tss_segment_16, sizeof tss_segment_16))
  3288. goto out;
  3289. if (load_state_from_tss16(vcpu, &tss_segment_16))
  3290. goto out;
  3291. ret = 1;
  3292. out:
  3293. return ret;
  3294. }
  3295. static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
  3296. u32 old_tss_base,
  3297. struct desc_struct *nseg_desc)
  3298. {
  3299. struct tss_segment_32 tss_segment_32;
  3300. int ret = 0;
  3301. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3302. sizeof tss_segment_32))
  3303. goto out;
  3304. save_state_to_tss32(vcpu, &tss_segment_32);
  3305. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3306. sizeof tss_segment_32))
  3307. goto out;
  3308. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3309. &tss_segment_32, sizeof tss_segment_32))
  3310. goto out;
  3311. if (load_state_from_tss32(vcpu, &tss_segment_32))
  3312. goto out;
  3313. ret = 1;
  3314. out:
  3315. return ret;
  3316. }
  3317. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
  3318. {
  3319. struct kvm_segment tr_seg;
  3320. struct desc_struct cseg_desc;
  3321. struct desc_struct nseg_desc;
  3322. int ret = 0;
  3323. u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
  3324. u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
  3325. old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
  3326. /* FIXME: Handle errors. Failure to read either TSS or their
  3327. * descriptors should generate a pagefault.
  3328. */
  3329. if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
  3330. goto out;
  3331. if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
  3332. goto out;
  3333. if (reason != TASK_SWITCH_IRET) {
  3334. int cpl;
  3335. cpl = kvm_x86_ops->get_cpl(vcpu);
  3336. if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
  3337. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  3338. return 1;
  3339. }
  3340. }
  3341. if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
  3342. kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
  3343. return 1;
  3344. }
  3345. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  3346. cseg_desc.type &= ~(1 << 1); //clear the B flag
  3347. save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
  3348. }
  3349. if (reason == TASK_SWITCH_IRET) {
  3350. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3351. kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
  3352. }
  3353. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3354. if (nseg_desc.type & 8)
  3355. ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base,
  3356. &nseg_desc);
  3357. else
  3358. ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base,
  3359. &nseg_desc);
  3360. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
  3361. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3362. kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
  3363. }
  3364. if (reason != TASK_SWITCH_IRET) {
  3365. nseg_desc.type |= (1 << 1);
  3366. save_guest_segment_descriptor(vcpu, tss_selector,
  3367. &nseg_desc);
  3368. }
  3369. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
  3370. seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
  3371. tr_seg.type = 11;
  3372. kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
  3373. out:
  3374. return ret;
  3375. }
  3376. EXPORT_SYMBOL_GPL(kvm_task_switch);
  3377. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  3378. struct kvm_sregs *sregs)
  3379. {
  3380. int mmu_reset_needed = 0;
  3381. int i, pending_vec, max_bits;
  3382. struct descriptor_table dt;
  3383. vcpu_load(vcpu);
  3384. dt.limit = sregs->idt.limit;
  3385. dt.base = sregs->idt.base;
  3386. kvm_x86_ops->set_idt(vcpu, &dt);
  3387. dt.limit = sregs->gdt.limit;
  3388. dt.base = sregs->gdt.base;
  3389. kvm_x86_ops->set_gdt(vcpu, &dt);
  3390. vcpu->arch.cr2 = sregs->cr2;
  3391. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  3392. vcpu->arch.cr3 = sregs->cr3;
  3393. kvm_set_cr8(vcpu, sregs->cr8);
  3394. mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
  3395. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  3396. kvm_set_apic_base(vcpu, sregs->apic_base);
  3397. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3398. mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
  3399. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  3400. vcpu->arch.cr0 = sregs->cr0;
  3401. mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
  3402. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  3403. if (!is_long_mode(vcpu) && is_pae(vcpu))
  3404. load_pdptrs(vcpu, vcpu->arch.cr3);
  3405. if (mmu_reset_needed)
  3406. kvm_mmu_reset_context(vcpu);
  3407. if (!irqchip_in_kernel(vcpu->kvm)) {
  3408. memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
  3409. sizeof vcpu->arch.irq_pending);
  3410. vcpu->arch.irq_summary = 0;
  3411. for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
  3412. if (vcpu->arch.irq_pending[i])
  3413. __set_bit(i, &vcpu->arch.irq_summary);
  3414. } else {
  3415. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  3416. pending_vec = find_first_bit(
  3417. (const unsigned long *)sregs->interrupt_bitmap,
  3418. max_bits);
  3419. /* Only pending external irq is handled here */
  3420. if (pending_vec < max_bits) {
  3421. kvm_x86_ops->set_irq(vcpu, pending_vec);
  3422. pr_debug("Set back pending irq %d\n",
  3423. pending_vec);
  3424. }
  3425. }
  3426. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  3427. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  3428. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  3429. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  3430. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  3431. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  3432. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  3433. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  3434. /* Older userspace won't unhalt the vcpu on reset. */
  3435. if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 &&
  3436. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  3437. !(vcpu->arch.cr0 & X86_CR0_PE))
  3438. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3439. vcpu_put(vcpu);
  3440. return 0;
  3441. }
  3442. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  3443. struct kvm_debug_guest *dbg)
  3444. {
  3445. int r;
  3446. vcpu_load(vcpu);
  3447. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  3448. vcpu_put(vcpu);
  3449. return r;
  3450. }
  3451. /*
  3452. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  3453. * we have asm/x86/processor.h
  3454. */
  3455. struct fxsave {
  3456. u16 cwd;
  3457. u16 swd;
  3458. u16 twd;
  3459. u16 fop;
  3460. u64 rip;
  3461. u64 rdp;
  3462. u32 mxcsr;
  3463. u32 mxcsr_mask;
  3464. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  3465. #ifdef CONFIG_X86_64
  3466. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  3467. #else
  3468. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  3469. #endif
  3470. };
  3471. /*
  3472. * Translate a guest virtual address to a guest physical address.
  3473. */
  3474. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  3475. struct kvm_translation *tr)
  3476. {
  3477. unsigned long vaddr = tr->linear_address;
  3478. gpa_t gpa;
  3479. vcpu_load(vcpu);
  3480. down_read(&vcpu->kvm->slots_lock);
  3481. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
  3482. up_read(&vcpu->kvm->slots_lock);
  3483. tr->physical_address = gpa;
  3484. tr->valid = gpa != UNMAPPED_GVA;
  3485. tr->writeable = 1;
  3486. tr->usermode = 0;
  3487. vcpu_put(vcpu);
  3488. return 0;
  3489. }
  3490. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3491. {
  3492. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3493. vcpu_load(vcpu);
  3494. memcpy(fpu->fpr, fxsave->st_space, 128);
  3495. fpu->fcw = fxsave->cwd;
  3496. fpu->fsw = fxsave->swd;
  3497. fpu->ftwx = fxsave->twd;
  3498. fpu->last_opcode = fxsave->fop;
  3499. fpu->last_ip = fxsave->rip;
  3500. fpu->last_dp = fxsave->rdp;
  3501. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  3502. vcpu_put(vcpu);
  3503. return 0;
  3504. }
  3505. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3506. {
  3507. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3508. vcpu_load(vcpu);
  3509. memcpy(fxsave->st_space, fpu->fpr, 128);
  3510. fxsave->cwd = fpu->fcw;
  3511. fxsave->swd = fpu->fsw;
  3512. fxsave->twd = fpu->ftwx;
  3513. fxsave->fop = fpu->last_opcode;
  3514. fxsave->rip = fpu->last_ip;
  3515. fxsave->rdp = fpu->last_dp;
  3516. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  3517. vcpu_put(vcpu);
  3518. return 0;
  3519. }
  3520. void fx_init(struct kvm_vcpu *vcpu)
  3521. {
  3522. unsigned after_mxcsr_mask;
  3523. /*
  3524. * Touch the fpu the first time in non atomic context as if
  3525. * this is the first fpu instruction the exception handler
  3526. * will fire before the instruction returns and it'll have to
  3527. * allocate ram with GFP_KERNEL.
  3528. */
  3529. if (!used_math())
  3530. kvm_fx_save(&vcpu->arch.host_fx_image);
  3531. /* Initialize guest FPU by resetting ours and saving into guest's */
  3532. preempt_disable();
  3533. kvm_fx_save(&vcpu->arch.host_fx_image);
  3534. kvm_fx_finit();
  3535. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3536. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3537. preempt_enable();
  3538. vcpu->arch.cr0 |= X86_CR0_ET;
  3539. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  3540. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  3541. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  3542. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  3543. }
  3544. EXPORT_SYMBOL_GPL(fx_init);
  3545. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  3546. {
  3547. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  3548. return;
  3549. vcpu->guest_fpu_loaded = 1;
  3550. kvm_fx_save(&vcpu->arch.host_fx_image);
  3551. kvm_fx_restore(&vcpu->arch.guest_fx_image);
  3552. }
  3553. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  3554. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  3555. {
  3556. if (!vcpu->guest_fpu_loaded)
  3557. return;
  3558. vcpu->guest_fpu_loaded = 0;
  3559. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3560. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3561. ++vcpu->stat.fpu_reload;
  3562. }
  3563. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  3564. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  3565. {
  3566. kvm_x86_ops->vcpu_free(vcpu);
  3567. }
  3568. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  3569. unsigned int id)
  3570. {
  3571. return kvm_x86_ops->vcpu_create(kvm, id);
  3572. }
  3573. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  3574. {
  3575. int r;
  3576. /* We do fxsave: this must be aligned. */
  3577. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  3578. vcpu_load(vcpu);
  3579. r = kvm_arch_vcpu_reset(vcpu);
  3580. if (r == 0)
  3581. r = kvm_mmu_setup(vcpu);
  3582. vcpu_put(vcpu);
  3583. if (r < 0)
  3584. goto free_vcpu;
  3585. return 0;
  3586. free_vcpu:
  3587. kvm_x86_ops->vcpu_free(vcpu);
  3588. return r;
  3589. }
  3590. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  3591. {
  3592. vcpu_load(vcpu);
  3593. kvm_mmu_unload(vcpu);
  3594. vcpu_put(vcpu);
  3595. kvm_x86_ops->vcpu_free(vcpu);
  3596. }
  3597. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  3598. {
  3599. return kvm_x86_ops->vcpu_reset(vcpu);
  3600. }
  3601. void kvm_arch_hardware_enable(void *garbage)
  3602. {
  3603. kvm_x86_ops->hardware_enable(garbage);
  3604. }
  3605. void kvm_arch_hardware_disable(void *garbage)
  3606. {
  3607. kvm_x86_ops->hardware_disable(garbage);
  3608. }
  3609. int kvm_arch_hardware_setup(void)
  3610. {
  3611. return kvm_x86_ops->hardware_setup();
  3612. }
  3613. void kvm_arch_hardware_unsetup(void)
  3614. {
  3615. kvm_x86_ops->hardware_unsetup();
  3616. }
  3617. void kvm_arch_check_processor_compat(void *rtn)
  3618. {
  3619. kvm_x86_ops->check_processor_compatibility(rtn);
  3620. }
  3621. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  3622. {
  3623. struct page *page;
  3624. struct kvm *kvm;
  3625. int r;
  3626. BUG_ON(vcpu->kvm == NULL);
  3627. kvm = vcpu->kvm;
  3628. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  3629. if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
  3630. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3631. else
  3632. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  3633. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  3634. if (!page) {
  3635. r = -ENOMEM;
  3636. goto fail;
  3637. }
  3638. vcpu->arch.pio_data = page_address(page);
  3639. r = kvm_mmu_create(vcpu);
  3640. if (r < 0)
  3641. goto fail_free_pio_data;
  3642. if (irqchip_in_kernel(kvm)) {
  3643. r = kvm_create_lapic(vcpu);
  3644. if (r < 0)
  3645. goto fail_mmu_destroy;
  3646. }
  3647. return 0;
  3648. fail_mmu_destroy:
  3649. kvm_mmu_destroy(vcpu);
  3650. fail_free_pio_data:
  3651. free_page((unsigned long)vcpu->arch.pio_data);
  3652. fail:
  3653. return r;
  3654. }
  3655. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  3656. {
  3657. kvm_free_lapic(vcpu);
  3658. down_read(&vcpu->kvm->slots_lock);
  3659. kvm_mmu_destroy(vcpu);
  3660. up_read(&vcpu->kvm->slots_lock);
  3661. free_page((unsigned long)vcpu->arch.pio_data);
  3662. }
  3663. struct kvm *kvm_arch_create_vm(void)
  3664. {
  3665. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  3666. if (!kvm)
  3667. return ERR_PTR(-ENOMEM);
  3668. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  3669. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  3670. return kvm;
  3671. }
  3672. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  3673. {
  3674. vcpu_load(vcpu);
  3675. kvm_mmu_unload(vcpu);
  3676. vcpu_put(vcpu);
  3677. }
  3678. static void kvm_free_vcpus(struct kvm *kvm)
  3679. {
  3680. unsigned int i;
  3681. /*
  3682. * Unpin any mmu pages first.
  3683. */
  3684. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  3685. if (kvm->vcpus[i])
  3686. kvm_unload_vcpu_mmu(kvm->vcpus[i]);
  3687. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  3688. if (kvm->vcpus[i]) {
  3689. kvm_arch_vcpu_free(kvm->vcpus[i]);
  3690. kvm->vcpus[i] = NULL;
  3691. }
  3692. }
  3693. }
  3694. void kvm_arch_destroy_vm(struct kvm *kvm)
  3695. {
  3696. kvm_iommu_unmap_guest(kvm);
  3697. kvm_free_all_assigned_devices(kvm);
  3698. kvm_free_pit(kvm);
  3699. kfree(kvm->arch.vpic);
  3700. kfree(kvm->arch.vioapic);
  3701. kvm_free_vcpus(kvm);
  3702. kvm_free_physmem(kvm);
  3703. if (kvm->arch.apic_access_page)
  3704. put_page(kvm->arch.apic_access_page);
  3705. if (kvm->arch.ept_identity_pagetable)
  3706. put_page(kvm->arch.ept_identity_pagetable);
  3707. kfree(kvm);
  3708. }
  3709. int kvm_arch_set_memory_region(struct kvm *kvm,
  3710. struct kvm_userspace_memory_region *mem,
  3711. struct kvm_memory_slot old,
  3712. int user_alloc)
  3713. {
  3714. int npages = mem->memory_size >> PAGE_SHIFT;
  3715. struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
  3716. /*To keep backward compatibility with older userspace,
  3717. *x86 needs to hanlde !user_alloc case.
  3718. */
  3719. if (!user_alloc) {
  3720. if (npages && !old.rmap) {
  3721. unsigned long userspace_addr;
  3722. down_write(&current->mm->mmap_sem);
  3723. userspace_addr = do_mmap(NULL, 0,
  3724. npages * PAGE_SIZE,
  3725. PROT_READ | PROT_WRITE,
  3726. MAP_PRIVATE | MAP_ANONYMOUS,
  3727. 0);
  3728. up_write(&current->mm->mmap_sem);
  3729. if (IS_ERR((void *)userspace_addr))
  3730. return PTR_ERR((void *)userspace_addr);
  3731. /* set userspace_addr atomically for kvm_hva_to_rmapp */
  3732. spin_lock(&kvm->mmu_lock);
  3733. memslot->userspace_addr = userspace_addr;
  3734. spin_unlock(&kvm->mmu_lock);
  3735. } else {
  3736. if (!old.user_alloc && old.rmap) {
  3737. int ret;
  3738. down_write(&current->mm->mmap_sem);
  3739. ret = do_munmap(current->mm, old.userspace_addr,
  3740. old.npages * PAGE_SIZE);
  3741. up_write(&current->mm->mmap_sem);
  3742. if (ret < 0)
  3743. printk(KERN_WARNING
  3744. "kvm_vm_ioctl_set_memory_region: "
  3745. "failed to munmap memory\n");
  3746. }
  3747. }
  3748. }
  3749. if (!kvm->arch.n_requested_mmu_pages) {
  3750. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  3751. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  3752. }
  3753. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  3754. kvm_flush_remote_tlbs(kvm);
  3755. return 0;
  3756. }
  3757. void kvm_arch_flush_shadow(struct kvm *kvm)
  3758. {
  3759. kvm_mmu_zap_all(kvm);
  3760. }
  3761. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  3762. {
  3763. return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
  3764. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
  3765. }
  3766. static void vcpu_kick_intr(void *info)
  3767. {
  3768. #ifdef DEBUG
  3769. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
  3770. printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
  3771. #endif
  3772. }
  3773. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  3774. {
  3775. int ipi_pcpu = vcpu->cpu;
  3776. int cpu = get_cpu();
  3777. if (waitqueue_active(&vcpu->wq)) {
  3778. wake_up_interruptible(&vcpu->wq);
  3779. ++vcpu->stat.halt_wakeup;
  3780. }
  3781. /*
  3782. * We may be called synchronously with irqs disabled in guest mode,
  3783. * So need not to call smp_call_function_single() in that case.
  3784. */
  3785. if (vcpu->guest_mode && vcpu->cpu != cpu)
  3786. smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
  3787. put_cpu();
  3788. }