x86.c 110 KB

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