x86.c 111 KB

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