x86.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465
  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. *
  8. * Authors:
  9. * Avi Kivity <avi@qumranet.com>
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2. See
  13. * the COPYING file in the top-level directory.
  14. *
  15. */
  16. #include <linux/kvm_host.h>
  17. #include "irq.h"
  18. #include "mmu.h"
  19. #include "i8254.h"
  20. #include <linux/clocksource.h>
  21. #include <linux/kvm.h>
  22. #include <linux/fs.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/module.h>
  25. #include <linux/mman.h>
  26. #include <linux/highmem.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/msr.h>
  29. #include <asm/desc.h>
  30. #define MAX_IO_MSRS 256
  31. #define CR0_RESERVED_BITS \
  32. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  33. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  34. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  35. #define CR4_RESERVED_BITS \
  36. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  37. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  38. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  39. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  40. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  41. /* EFER defaults:
  42. * - enable syscall per default because its emulated by KVM
  43. * - enable LME and LMA per default on 64 bit KVM
  44. */
  45. #ifdef CONFIG_X86_64
  46. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  47. #else
  48. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  49. #endif
  50. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  51. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  52. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  53. struct kvm_cpuid_entry2 __user *entries);
  54. struct kvm_x86_ops *kvm_x86_ops;
  55. struct kvm_stats_debugfs_item debugfs_entries[] = {
  56. { "pf_fixed", VCPU_STAT(pf_fixed) },
  57. { "pf_guest", VCPU_STAT(pf_guest) },
  58. { "tlb_flush", VCPU_STAT(tlb_flush) },
  59. { "invlpg", VCPU_STAT(invlpg) },
  60. { "exits", VCPU_STAT(exits) },
  61. { "io_exits", VCPU_STAT(io_exits) },
  62. { "mmio_exits", VCPU_STAT(mmio_exits) },
  63. { "signal_exits", VCPU_STAT(signal_exits) },
  64. { "irq_window", VCPU_STAT(irq_window_exits) },
  65. { "halt_exits", VCPU_STAT(halt_exits) },
  66. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  67. { "hypercalls", VCPU_STAT(hypercalls) },
  68. { "request_irq", VCPU_STAT(request_irq_exits) },
  69. { "irq_exits", VCPU_STAT(irq_exits) },
  70. { "host_state_reload", VCPU_STAT(host_state_reload) },
  71. { "efer_reload", VCPU_STAT(efer_reload) },
  72. { "fpu_reload", VCPU_STAT(fpu_reload) },
  73. { "insn_emulation", VCPU_STAT(insn_emulation) },
  74. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  75. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  76. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  77. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  78. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  79. { "mmu_flooded", VM_STAT(mmu_flooded) },
  80. { "mmu_recycled", VM_STAT(mmu_recycled) },
  81. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  82. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  83. { "largepages", VM_STAT(lpages) },
  84. { NULL }
  85. };
  86. unsigned long segment_base(u16 selector)
  87. {
  88. struct descriptor_table gdt;
  89. struct desc_struct *d;
  90. unsigned long table_base;
  91. unsigned long v;
  92. if (selector == 0)
  93. return 0;
  94. asm("sgdt %0" : "=m"(gdt));
  95. table_base = gdt.base;
  96. if (selector & 4) { /* from ldt */
  97. u16 ldt_selector;
  98. asm("sldt %0" : "=g"(ldt_selector));
  99. table_base = segment_base(ldt_selector);
  100. }
  101. d = (struct desc_struct *)(table_base + (selector & ~7));
  102. v = d->base0 | ((unsigned long)d->base1 << 16) |
  103. ((unsigned long)d->base2 << 24);
  104. #ifdef CONFIG_X86_64
  105. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  106. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  107. #endif
  108. return v;
  109. }
  110. EXPORT_SYMBOL_GPL(segment_base);
  111. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  112. {
  113. if (irqchip_in_kernel(vcpu->kvm))
  114. return vcpu->arch.apic_base;
  115. else
  116. return vcpu->arch.apic_base;
  117. }
  118. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  119. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  120. {
  121. /* TODO: reserve bits check */
  122. if (irqchip_in_kernel(vcpu->kvm))
  123. kvm_lapic_set_base(vcpu, data);
  124. else
  125. vcpu->arch.apic_base = data;
  126. }
  127. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  128. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  129. {
  130. WARN_ON(vcpu->arch.exception.pending);
  131. vcpu->arch.exception.pending = true;
  132. vcpu->arch.exception.has_error_code = false;
  133. vcpu->arch.exception.nr = nr;
  134. }
  135. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  136. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  137. u32 error_code)
  138. {
  139. ++vcpu->stat.pf_guest;
  140. if (vcpu->arch.exception.pending) {
  141. if (vcpu->arch.exception.nr == PF_VECTOR) {
  142. printk(KERN_DEBUG "kvm: inject_page_fault:"
  143. " double fault 0x%lx\n", addr);
  144. vcpu->arch.exception.nr = DF_VECTOR;
  145. vcpu->arch.exception.error_code = 0;
  146. } else if (vcpu->arch.exception.nr == DF_VECTOR) {
  147. /* triple fault -> shutdown */
  148. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  149. }
  150. return;
  151. }
  152. vcpu->arch.cr2 = addr;
  153. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  154. }
  155. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  156. {
  157. WARN_ON(vcpu->arch.exception.pending);
  158. vcpu->arch.exception.pending = true;
  159. vcpu->arch.exception.has_error_code = true;
  160. vcpu->arch.exception.nr = nr;
  161. vcpu->arch.exception.error_code = error_code;
  162. }
  163. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  164. static void __queue_exception(struct kvm_vcpu *vcpu)
  165. {
  166. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  167. vcpu->arch.exception.has_error_code,
  168. vcpu->arch.exception.error_code);
  169. }
  170. /*
  171. * Load the pae pdptrs. Return true is they are all valid.
  172. */
  173. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  174. {
  175. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  176. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  177. int i;
  178. int ret;
  179. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  180. down_read(&vcpu->kvm->slots_lock);
  181. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  182. offset * sizeof(u64), sizeof(pdpte));
  183. if (ret < 0) {
  184. ret = 0;
  185. goto out;
  186. }
  187. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  188. if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
  189. ret = 0;
  190. goto out;
  191. }
  192. }
  193. ret = 1;
  194. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  195. out:
  196. up_read(&vcpu->kvm->slots_lock);
  197. return ret;
  198. }
  199. EXPORT_SYMBOL_GPL(load_pdptrs);
  200. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  201. {
  202. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  203. bool changed = true;
  204. int r;
  205. if (is_long_mode(vcpu) || !is_pae(vcpu))
  206. return false;
  207. down_read(&vcpu->kvm->slots_lock);
  208. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  209. if (r < 0)
  210. goto out;
  211. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  212. out:
  213. up_read(&vcpu->kvm->slots_lock);
  214. return changed;
  215. }
  216. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  217. {
  218. if (cr0 & CR0_RESERVED_BITS) {
  219. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  220. cr0, vcpu->arch.cr0);
  221. kvm_inject_gp(vcpu, 0);
  222. return;
  223. }
  224. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  225. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  226. kvm_inject_gp(vcpu, 0);
  227. return;
  228. }
  229. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  230. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  231. "and a clear PE flag\n");
  232. kvm_inject_gp(vcpu, 0);
  233. return;
  234. }
  235. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  236. #ifdef CONFIG_X86_64
  237. if ((vcpu->arch.shadow_efer & EFER_LME)) {
  238. int cs_db, cs_l;
  239. if (!is_pae(vcpu)) {
  240. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  241. "in long mode while PAE is disabled\n");
  242. kvm_inject_gp(vcpu, 0);
  243. return;
  244. }
  245. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  246. if (cs_l) {
  247. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  248. "in long mode while CS.L == 1\n");
  249. kvm_inject_gp(vcpu, 0);
  250. return;
  251. }
  252. } else
  253. #endif
  254. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  255. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  256. "reserved bits\n");
  257. kvm_inject_gp(vcpu, 0);
  258. return;
  259. }
  260. }
  261. kvm_x86_ops->set_cr0(vcpu, cr0);
  262. vcpu->arch.cr0 = cr0;
  263. kvm_mmu_reset_context(vcpu);
  264. return;
  265. }
  266. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  267. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  268. {
  269. kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
  270. }
  271. EXPORT_SYMBOL_GPL(kvm_lmsw);
  272. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  273. {
  274. if (cr4 & CR4_RESERVED_BITS) {
  275. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  276. kvm_inject_gp(vcpu, 0);
  277. return;
  278. }
  279. if (is_long_mode(vcpu)) {
  280. if (!(cr4 & X86_CR4_PAE)) {
  281. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  282. "in long mode\n");
  283. kvm_inject_gp(vcpu, 0);
  284. return;
  285. }
  286. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
  287. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  288. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  289. kvm_inject_gp(vcpu, 0);
  290. return;
  291. }
  292. if (cr4 & X86_CR4_VMXE) {
  293. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  294. kvm_inject_gp(vcpu, 0);
  295. return;
  296. }
  297. kvm_x86_ops->set_cr4(vcpu, cr4);
  298. vcpu->arch.cr4 = cr4;
  299. kvm_mmu_reset_context(vcpu);
  300. }
  301. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  302. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  303. {
  304. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  305. kvm_mmu_flush_tlb(vcpu);
  306. return;
  307. }
  308. if (is_long_mode(vcpu)) {
  309. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  310. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  311. kvm_inject_gp(vcpu, 0);
  312. return;
  313. }
  314. } else {
  315. if (is_pae(vcpu)) {
  316. if (cr3 & CR3_PAE_RESERVED_BITS) {
  317. printk(KERN_DEBUG
  318. "set_cr3: #GP, reserved bits\n");
  319. kvm_inject_gp(vcpu, 0);
  320. return;
  321. }
  322. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  323. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  324. "reserved bits\n");
  325. kvm_inject_gp(vcpu, 0);
  326. return;
  327. }
  328. }
  329. /*
  330. * We don't check reserved bits in nonpae mode, because
  331. * this isn't enforced, and VMware depends on this.
  332. */
  333. }
  334. down_read(&vcpu->kvm->slots_lock);
  335. /*
  336. * Does the new cr3 value map to physical memory? (Note, we
  337. * catch an invalid cr3 even in real-mode, because it would
  338. * cause trouble later on when we turn on paging anyway.)
  339. *
  340. * A real CPU would silently accept an invalid cr3 and would
  341. * attempt to use it - with largely undefined (and often hard
  342. * to debug) behavior on the guest side.
  343. */
  344. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  345. kvm_inject_gp(vcpu, 0);
  346. else {
  347. vcpu->arch.cr3 = cr3;
  348. vcpu->arch.mmu.new_cr3(vcpu);
  349. }
  350. up_read(&vcpu->kvm->slots_lock);
  351. }
  352. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  353. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  354. {
  355. if (cr8 & CR8_RESERVED_BITS) {
  356. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  357. kvm_inject_gp(vcpu, 0);
  358. return;
  359. }
  360. if (irqchip_in_kernel(vcpu->kvm))
  361. kvm_lapic_set_tpr(vcpu, cr8);
  362. else
  363. vcpu->arch.cr8 = cr8;
  364. }
  365. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  366. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  367. {
  368. if (irqchip_in_kernel(vcpu->kvm))
  369. return kvm_lapic_get_cr8(vcpu);
  370. else
  371. return vcpu->arch.cr8;
  372. }
  373. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  374. /*
  375. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  376. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  377. *
  378. * This list is modified at module load time to reflect the
  379. * capabilities of the host cpu.
  380. */
  381. static u32 msrs_to_save[] = {
  382. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  383. MSR_K6_STAR,
  384. #ifdef CONFIG_X86_64
  385. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  386. #endif
  387. MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  388. MSR_IA32_PERF_STATUS,
  389. };
  390. static unsigned num_msrs_to_save;
  391. static u32 emulated_msrs[] = {
  392. MSR_IA32_MISC_ENABLE,
  393. };
  394. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  395. {
  396. if (efer & efer_reserved_bits) {
  397. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  398. efer);
  399. kvm_inject_gp(vcpu, 0);
  400. return;
  401. }
  402. if (is_paging(vcpu)
  403. && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  404. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  405. kvm_inject_gp(vcpu, 0);
  406. return;
  407. }
  408. kvm_x86_ops->set_efer(vcpu, efer);
  409. efer &= ~EFER_LMA;
  410. efer |= vcpu->arch.shadow_efer & EFER_LMA;
  411. vcpu->arch.shadow_efer = efer;
  412. }
  413. void kvm_enable_efer_bits(u64 mask)
  414. {
  415. efer_reserved_bits &= ~mask;
  416. }
  417. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  418. /*
  419. * Writes msr value into into the appropriate "register".
  420. * Returns 0 on success, non-0 otherwise.
  421. * Assumes vcpu_load() was already called.
  422. */
  423. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  424. {
  425. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  426. }
  427. /*
  428. * Adapt set_msr() to msr_io()'s calling convention
  429. */
  430. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  431. {
  432. return kvm_set_msr(vcpu, index, *data);
  433. }
  434. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  435. {
  436. static int version;
  437. struct kvm_wall_clock wc;
  438. struct timespec wc_ts;
  439. if (!wall_clock)
  440. return;
  441. version++;
  442. down_read(&kvm->slots_lock);
  443. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  444. wc_ts = current_kernel_time();
  445. wc.wc_sec = wc_ts.tv_sec;
  446. wc.wc_nsec = wc_ts.tv_nsec;
  447. wc.wc_version = version;
  448. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  449. version++;
  450. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  451. up_read(&kvm->slots_lock);
  452. }
  453. static void kvm_write_guest_time(struct kvm_vcpu *v)
  454. {
  455. struct timespec ts;
  456. unsigned long flags;
  457. struct kvm_vcpu_arch *vcpu = &v->arch;
  458. void *shared_kaddr;
  459. if ((!vcpu->time_page))
  460. return;
  461. /* Keep irq disabled to prevent changes to the clock */
  462. local_irq_save(flags);
  463. kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
  464. &vcpu->hv_clock.tsc_timestamp);
  465. ktime_get_ts(&ts);
  466. local_irq_restore(flags);
  467. /* With all the info we got, fill in the values */
  468. vcpu->hv_clock.system_time = ts.tv_nsec +
  469. (NSEC_PER_SEC * (u64)ts.tv_sec);
  470. /*
  471. * The interface expects us to write an even number signaling that the
  472. * update is finished. Since the guest won't see the intermediate
  473. * state, we just write "2" at the end
  474. */
  475. vcpu->hv_clock.version = 2;
  476. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  477. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  478. sizeof(vcpu->hv_clock));
  479. kunmap_atomic(shared_kaddr, KM_USER0);
  480. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  481. }
  482. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  483. {
  484. switch (msr) {
  485. case MSR_EFER:
  486. set_efer(vcpu, data);
  487. break;
  488. case MSR_IA32_MC0_STATUS:
  489. pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  490. __func__, data);
  491. break;
  492. case MSR_IA32_MCG_STATUS:
  493. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  494. __func__, data);
  495. break;
  496. case MSR_IA32_MCG_CTL:
  497. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
  498. __func__, data);
  499. break;
  500. case MSR_IA32_UCODE_REV:
  501. case MSR_IA32_UCODE_WRITE:
  502. case 0x200 ... 0x2ff: /* MTRRs */
  503. break;
  504. case MSR_IA32_APICBASE:
  505. kvm_set_apic_base(vcpu, data);
  506. break;
  507. case MSR_IA32_MISC_ENABLE:
  508. vcpu->arch.ia32_misc_enable_msr = data;
  509. break;
  510. case MSR_KVM_WALL_CLOCK:
  511. vcpu->kvm->arch.wall_clock = data;
  512. kvm_write_wall_clock(vcpu->kvm, data);
  513. break;
  514. case MSR_KVM_SYSTEM_TIME: {
  515. if (vcpu->arch.time_page) {
  516. kvm_release_page_dirty(vcpu->arch.time_page);
  517. vcpu->arch.time_page = NULL;
  518. }
  519. vcpu->arch.time = data;
  520. /* we verify if the enable bit is set... */
  521. if (!(data & 1))
  522. break;
  523. /* ...but clean it before doing the actual write */
  524. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  525. vcpu->arch.hv_clock.tsc_to_system_mul =
  526. clocksource_khz2mult(tsc_khz, 22);
  527. vcpu->arch.hv_clock.tsc_shift = 22;
  528. down_read(&current->mm->mmap_sem);
  529. down_read(&vcpu->kvm->slots_lock);
  530. vcpu->arch.time_page =
  531. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  532. up_read(&vcpu->kvm->slots_lock);
  533. up_read(&current->mm->mmap_sem);
  534. if (is_error_page(vcpu->arch.time_page)) {
  535. kvm_release_page_clean(vcpu->arch.time_page);
  536. vcpu->arch.time_page = NULL;
  537. }
  538. kvm_write_guest_time(vcpu);
  539. break;
  540. }
  541. default:
  542. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
  543. return 1;
  544. }
  545. return 0;
  546. }
  547. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  548. /*
  549. * Reads an msr value (of 'msr_index') into 'pdata'.
  550. * Returns 0 on success, non-0 otherwise.
  551. * Assumes vcpu_load() was already called.
  552. */
  553. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  554. {
  555. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  556. }
  557. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  558. {
  559. u64 data;
  560. switch (msr) {
  561. case 0xc0010010: /* SYSCFG */
  562. case 0xc0010015: /* HWCR */
  563. case MSR_IA32_PLATFORM_ID:
  564. case MSR_IA32_P5_MC_ADDR:
  565. case MSR_IA32_P5_MC_TYPE:
  566. case MSR_IA32_MC0_CTL:
  567. case MSR_IA32_MCG_STATUS:
  568. case MSR_IA32_MCG_CAP:
  569. case MSR_IA32_MCG_CTL:
  570. case MSR_IA32_MC0_MISC:
  571. case MSR_IA32_MC0_MISC+4:
  572. case MSR_IA32_MC0_MISC+8:
  573. case MSR_IA32_MC0_MISC+12:
  574. case MSR_IA32_MC0_MISC+16:
  575. case MSR_IA32_UCODE_REV:
  576. case MSR_IA32_EBL_CR_POWERON:
  577. /* MTRR registers */
  578. case 0xfe:
  579. case 0x200 ... 0x2ff:
  580. data = 0;
  581. break;
  582. case 0xcd: /* fsb frequency */
  583. data = 3;
  584. break;
  585. case MSR_IA32_APICBASE:
  586. data = kvm_get_apic_base(vcpu);
  587. break;
  588. case MSR_IA32_MISC_ENABLE:
  589. data = vcpu->arch.ia32_misc_enable_msr;
  590. break;
  591. case MSR_IA32_PERF_STATUS:
  592. /* TSC increment by tick */
  593. data = 1000ULL;
  594. /* CPU multiplier */
  595. data |= (((uint64_t)4ULL) << 40);
  596. break;
  597. case MSR_EFER:
  598. data = vcpu->arch.shadow_efer;
  599. break;
  600. case MSR_KVM_WALL_CLOCK:
  601. data = vcpu->kvm->arch.wall_clock;
  602. break;
  603. case MSR_KVM_SYSTEM_TIME:
  604. data = vcpu->arch.time;
  605. break;
  606. default:
  607. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  608. return 1;
  609. }
  610. *pdata = data;
  611. return 0;
  612. }
  613. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  614. /*
  615. * Read or write a bunch of msrs. All parameters are kernel addresses.
  616. *
  617. * @return number of msrs set successfully.
  618. */
  619. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  620. struct kvm_msr_entry *entries,
  621. int (*do_msr)(struct kvm_vcpu *vcpu,
  622. unsigned index, u64 *data))
  623. {
  624. int i;
  625. vcpu_load(vcpu);
  626. for (i = 0; i < msrs->nmsrs; ++i)
  627. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  628. break;
  629. vcpu_put(vcpu);
  630. return i;
  631. }
  632. /*
  633. * Read or write a bunch of msrs. Parameters are user addresses.
  634. *
  635. * @return number of msrs set successfully.
  636. */
  637. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  638. int (*do_msr)(struct kvm_vcpu *vcpu,
  639. unsigned index, u64 *data),
  640. int writeback)
  641. {
  642. struct kvm_msrs msrs;
  643. struct kvm_msr_entry *entries;
  644. int r, n;
  645. unsigned size;
  646. r = -EFAULT;
  647. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  648. goto out;
  649. r = -E2BIG;
  650. if (msrs.nmsrs >= MAX_IO_MSRS)
  651. goto out;
  652. r = -ENOMEM;
  653. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  654. entries = vmalloc(size);
  655. if (!entries)
  656. goto out;
  657. r = -EFAULT;
  658. if (copy_from_user(entries, user_msrs->entries, size))
  659. goto out_free;
  660. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  661. if (r < 0)
  662. goto out_free;
  663. r = -EFAULT;
  664. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  665. goto out_free;
  666. r = n;
  667. out_free:
  668. vfree(entries);
  669. out:
  670. return r;
  671. }
  672. /*
  673. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  674. * cached on it.
  675. */
  676. void decache_vcpus_on_cpu(int cpu)
  677. {
  678. struct kvm *vm;
  679. struct kvm_vcpu *vcpu;
  680. int i;
  681. spin_lock(&kvm_lock);
  682. list_for_each_entry(vm, &vm_list, vm_list)
  683. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  684. vcpu = vm->vcpus[i];
  685. if (!vcpu)
  686. continue;
  687. /*
  688. * If the vcpu is locked, then it is running on some
  689. * other cpu and therefore it is not cached on the
  690. * cpu in question.
  691. *
  692. * If it's not locked, check the last cpu it executed
  693. * on.
  694. */
  695. if (mutex_trylock(&vcpu->mutex)) {
  696. if (vcpu->cpu == cpu) {
  697. kvm_x86_ops->vcpu_decache(vcpu);
  698. vcpu->cpu = -1;
  699. }
  700. mutex_unlock(&vcpu->mutex);
  701. }
  702. }
  703. spin_unlock(&kvm_lock);
  704. }
  705. int kvm_dev_ioctl_check_extension(long ext)
  706. {
  707. int r;
  708. switch (ext) {
  709. case KVM_CAP_IRQCHIP:
  710. case KVM_CAP_HLT:
  711. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  712. case KVM_CAP_USER_MEMORY:
  713. case KVM_CAP_SET_TSS_ADDR:
  714. case KVM_CAP_EXT_CPUID:
  715. case KVM_CAP_CLOCKSOURCE:
  716. case KVM_CAP_PIT:
  717. r = 1;
  718. break;
  719. case KVM_CAP_VAPIC:
  720. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  721. break;
  722. case KVM_CAP_NR_VCPUS:
  723. r = KVM_MAX_VCPUS;
  724. break;
  725. case KVM_CAP_NR_MEMSLOTS:
  726. r = KVM_MEMORY_SLOTS;
  727. break;
  728. default:
  729. r = 0;
  730. break;
  731. }
  732. return r;
  733. }
  734. long kvm_arch_dev_ioctl(struct file *filp,
  735. unsigned int ioctl, unsigned long arg)
  736. {
  737. void __user *argp = (void __user *)arg;
  738. long r;
  739. switch (ioctl) {
  740. case KVM_GET_MSR_INDEX_LIST: {
  741. struct kvm_msr_list __user *user_msr_list = argp;
  742. struct kvm_msr_list msr_list;
  743. unsigned n;
  744. r = -EFAULT;
  745. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  746. goto out;
  747. n = msr_list.nmsrs;
  748. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  749. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  750. goto out;
  751. r = -E2BIG;
  752. if (n < num_msrs_to_save)
  753. goto out;
  754. r = -EFAULT;
  755. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  756. num_msrs_to_save * sizeof(u32)))
  757. goto out;
  758. if (copy_to_user(user_msr_list->indices
  759. + num_msrs_to_save * sizeof(u32),
  760. &emulated_msrs,
  761. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  762. goto out;
  763. r = 0;
  764. break;
  765. }
  766. case KVM_GET_SUPPORTED_CPUID: {
  767. struct kvm_cpuid2 __user *cpuid_arg = argp;
  768. struct kvm_cpuid2 cpuid;
  769. r = -EFAULT;
  770. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  771. goto out;
  772. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  773. cpuid_arg->entries);
  774. if (r)
  775. goto out;
  776. r = -EFAULT;
  777. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  778. goto out;
  779. r = 0;
  780. break;
  781. }
  782. default:
  783. r = -EINVAL;
  784. }
  785. out:
  786. return r;
  787. }
  788. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  789. {
  790. kvm_x86_ops->vcpu_load(vcpu, cpu);
  791. kvm_write_guest_time(vcpu);
  792. }
  793. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  794. {
  795. kvm_x86_ops->vcpu_put(vcpu);
  796. kvm_put_guest_fpu(vcpu);
  797. }
  798. static int is_efer_nx(void)
  799. {
  800. u64 efer;
  801. rdmsrl(MSR_EFER, efer);
  802. return efer & EFER_NX;
  803. }
  804. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  805. {
  806. int i;
  807. struct kvm_cpuid_entry2 *e, *entry;
  808. entry = NULL;
  809. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  810. e = &vcpu->arch.cpuid_entries[i];
  811. if (e->function == 0x80000001) {
  812. entry = e;
  813. break;
  814. }
  815. }
  816. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  817. entry->edx &= ~(1 << 20);
  818. printk(KERN_INFO "kvm: guest NX capability removed\n");
  819. }
  820. }
  821. /* when an old userspace process fills a new kernel module */
  822. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  823. struct kvm_cpuid *cpuid,
  824. struct kvm_cpuid_entry __user *entries)
  825. {
  826. int r, i;
  827. struct kvm_cpuid_entry *cpuid_entries;
  828. r = -E2BIG;
  829. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  830. goto out;
  831. r = -ENOMEM;
  832. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  833. if (!cpuid_entries)
  834. goto out;
  835. r = -EFAULT;
  836. if (copy_from_user(cpuid_entries, entries,
  837. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  838. goto out_free;
  839. for (i = 0; i < cpuid->nent; i++) {
  840. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  841. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  842. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  843. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  844. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  845. vcpu->arch.cpuid_entries[i].index = 0;
  846. vcpu->arch.cpuid_entries[i].flags = 0;
  847. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  848. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  849. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  850. }
  851. vcpu->arch.cpuid_nent = cpuid->nent;
  852. cpuid_fix_nx_cap(vcpu);
  853. r = 0;
  854. out_free:
  855. vfree(cpuid_entries);
  856. out:
  857. return r;
  858. }
  859. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  860. struct kvm_cpuid2 *cpuid,
  861. struct kvm_cpuid_entry2 __user *entries)
  862. {
  863. int r;
  864. r = -E2BIG;
  865. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  866. goto out;
  867. r = -EFAULT;
  868. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  869. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  870. goto out;
  871. vcpu->arch.cpuid_nent = cpuid->nent;
  872. return 0;
  873. out:
  874. return r;
  875. }
  876. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  877. struct kvm_cpuid2 *cpuid,
  878. struct kvm_cpuid_entry2 __user *entries)
  879. {
  880. int r;
  881. r = -E2BIG;
  882. if (cpuid->nent < vcpu->arch.cpuid_nent)
  883. goto out;
  884. r = -EFAULT;
  885. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  886. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  887. goto out;
  888. return 0;
  889. out:
  890. cpuid->nent = vcpu->arch.cpuid_nent;
  891. return r;
  892. }
  893. static inline u32 bit(int bitno)
  894. {
  895. return 1 << (bitno & 31);
  896. }
  897. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  898. u32 index)
  899. {
  900. entry->function = function;
  901. entry->index = index;
  902. cpuid_count(entry->function, entry->index,
  903. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  904. entry->flags = 0;
  905. }
  906. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  907. u32 index, int *nent, int maxnent)
  908. {
  909. const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
  910. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  911. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  912. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  913. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  914. bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
  915. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  916. bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
  917. bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
  918. bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
  919. const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
  920. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  921. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  922. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  923. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  924. bit(X86_FEATURE_PGE) |
  925. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  926. bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
  927. bit(X86_FEATURE_SYSCALL) |
  928. (bit(X86_FEATURE_NX) && is_efer_nx()) |
  929. #ifdef CONFIG_X86_64
  930. bit(X86_FEATURE_LM) |
  931. #endif
  932. bit(X86_FEATURE_MMXEXT) |
  933. bit(X86_FEATURE_3DNOWEXT) |
  934. bit(X86_FEATURE_3DNOW);
  935. const u32 kvm_supported_word3_x86_features =
  936. bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
  937. const u32 kvm_supported_word6_x86_features =
  938. bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
  939. /* all func 2 cpuid_count() should be called on the same cpu */
  940. get_cpu();
  941. do_cpuid_1_ent(entry, function, index);
  942. ++*nent;
  943. switch (function) {
  944. case 0:
  945. entry->eax = min(entry->eax, (u32)0xb);
  946. break;
  947. case 1:
  948. entry->edx &= kvm_supported_word0_x86_features;
  949. entry->ecx &= kvm_supported_word3_x86_features;
  950. break;
  951. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  952. * may return different values. This forces us to get_cpu() before
  953. * issuing the first command, and also to emulate this annoying behavior
  954. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  955. case 2: {
  956. int t, times = entry->eax & 0xff;
  957. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  958. for (t = 1; t < times && *nent < maxnent; ++t) {
  959. do_cpuid_1_ent(&entry[t], function, 0);
  960. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  961. ++*nent;
  962. }
  963. break;
  964. }
  965. /* function 4 and 0xb have additional index. */
  966. case 4: {
  967. int i, cache_type;
  968. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  969. /* read more entries until cache_type is zero */
  970. for (i = 1; *nent < maxnent; ++i) {
  971. cache_type = entry[i - 1].eax & 0x1f;
  972. if (!cache_type)
  973. break;
  974. do_cpuid_1_ent(&entry[i], function, i);
  975. entry[i].flags |=
  976. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  977. ++*nent;
  978. }
  979. break;
  980. }
  981. case 0xb: {
  982. int i, level_type;
  983. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  984. /* read more entries until level_type is zero */
  985. for (i = 1; *nent < maxnent; ++i) {
  986. level_type = entry[i - 1].ecx & 0xff;
  987. if (!level_type)
  988. break;
  989. do_cpuid_1_ent(&entry[i], function, i);
  990. entry[i].flags |=
  991. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  992. ++*nent;
  993. }
  994. break;
  995. }
  996. case 0x80000000:
  997. entry->eax = min(entry->eax, 0x8000001a);
  998. break;
  999. case 0x80000001:
  1000. entry->edx &= kvm_supported_word1_x86_features;
  1001. entry->ecx &= kvm_supported_word6_x86_features;
  1002. break;
  1003. }
  1004. put_cpu();
  1005. }
  1006. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1007. struct kvm_cpuid_entry2 __user *entries)
  1008. {
  1009. struct kvm_cpuid_entry2 *cpuid_entries;
  1010. int limit, nent = 0, r = -E2BIG;
  1011. u32 func;
  1012. if (cpuid->nent < 1)
  1013. goto out;
  1014. r = -ENOMEM;
  1015. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1016. if (!cpuid_entries)
  1017. goto out;
  1018. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1019. limit = cpuid_entries[0].eax;
  1020. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1021. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1022. &nent, cpuid->nent);
  1023. r = -E2BIG;
  1024. if (nent >= cpuid->nent)
  1025. goto out_free;
  1026. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1027. limit = cpuid_entries[nent - 1].eax;
  1028. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1029. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1030. &nent, cpuid->nent);
  1031. r = -EFAULT;
  1032. if (copy_to_user(entries, cpuid_entries,
  1033. nent * sizeof(struct kvm_cpuid_entry2)))
  1034. goto out_free;
  1035. cpuid->nent = nent;
  1036. r = 0;
  1037. out_free:
  1038. vfree(cpuid_entries);
  1039. out:
  1040. return r;
  1041. }
  1042. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1043. struct kvm_lapic_state *s)
  1044. {
  1045. vcpu_load(vcpu);
  1046. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1047. vcpu_put(vcpu);
  1048. return 0;
  1049. }
  1050. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1051. struct kvm_lapic_state *s)
  1052. {
  1053. vcpu_load(vcpu);
  1054. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1055. kvm_apic_post_state_restore(vcpu);
  1056. vcpu_put(vcpu);
  1057. return 0;
  1058. }
  1059. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1060. struct kvm_interrupt *irq)
  1061. {
  1062. if (irq->irq < 0 || irq->irq >= 256)
  1063. return -EINVAL;
  1064. if (irqchip_in_kernel(vcpu->kvm))
  1065. return -ENXIO;
  1066. vcpu_load(vcpu);
  1067. set_bit(irq->irq, vcpu->arch.irq_pending);
  1068. set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  1069. vcpu_put(vcpu);
  1070. return 0;
  1071. }
  1072. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1073. struct kvm_tpr_access_ctl *tac)
  1074. {
  1075. if (tac->flags)
  1076. return -EINVAL;
  1077. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1078. return 0;
  1079. }
  1080. long kvm_arch_vcpu_ioctl(struct file *filp,
  1081. unsigned int ioctl, unsigned long arg)
  1082. {
  1083. struct kvm_vcpu *vcpu = filp->private_data;
  1084. void __user *argp = (void __user *)arg;
  1085. int r;
  1086. switch (ioctl) {
  1087. case KVM_GET_LAPIC: {
  1088. struct kvm_lapic_state lapic;
  1089. memset(&lapic, 0, sizeof lapic);
  1090. r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
  1091. if (r)
  1092. goto out;
  1093. r = -EFAULT;
  1094. if (copy_to_user(argp, &lapic, sizeof lapic))
  1095. goto out;
  1096. r = 0;
  1097. break;
  1098. }
  1099. case KVM_SET_LAPIC: {
  1100. struct kvm_lapic_state lapic;
  1101. r = -EFAULT;
  1102. if (copy_from_user(&lapic, argp, sizeof lapic))
  1103. goto out;
  1104. r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
  1105. if (r)
  1106. goto out;
  1107. r = 0;
  1108. break;
  1109. }
  1110. case KVM_INTERRUPT: {
  1111. struct kvm_interrupt irq;
  1112. r = -EFAULT;
  1113. if (copy_from_user(&irq, argp, sizeof irq))
  1114. goto out;
  1115. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1116. if (r)
  1117. goto out;
  1118. r = 0;
  1119. break;
  1120. }
  1121. case KVM_SET_CPUID: {
  1122. struct kvm_cpuid __user *cpuid_arg = argp;
  1123. struct kvm_cpuid cpuid;
  1124. r = -EFAULT;
  1125. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1126. goto out;
  1127. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1128. if (r)
  1129. goto out;
  1130. break;
  1131. }
  1132. case KVM_SET_CPUID2: {
  1133. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1134. struct kvm_cpuid2 cpuid;
  1135. r = -EFAULT;
  1136. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1137. goto out;
  1138. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  1139. cpuid_arg->entries);
  1140. if (r)
  1141. goto out;
  1142. break;
  1143. }
  1144. case KVM_GET_CPUID2: {
  1145. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1146. struct kvm_cpuid2 cpuid;
  1147. r = -EFAULT;
  1148. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1149. goto out;
  1150. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  1151. cpuid_arg->entries);
  1152. if (r)
  1153. goto out;
  1154. r = -EFAULT;
  1155. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1156. goto out;
  1157. r = 0;
  1158. break;
  1159. }
  1160. case KVM_GET_MSRS:
  1161. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  1162. break;
  1163. case KVM_SET_MSRS:
  1164. r = msr_io(vcpu, argp, do_set_msr, 0);
  1165. break;
  1166. case KVM_TPR_ACCESS_REPORTING: {
  1167. struct kvm_tpr_access_ctl tac;
  1168. r = -EFAULT;
  1169. if (copy_from_user(&tac, argp, sizeof tac))
  1170. goto out;
  1171. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  1172. if (r)
  1173. goto out;
  1174. r = -EFAULT;
  1175. if (copy_to_user(argp, &tac, sizeof tac))
  1176. goto out;
  1177. r = 0;
  1178. break;
  1179. };
  1180. case KVM_SET_VAPIC_ADDR: {
  1181. struct kvm_vapic_addr va;
  1182. r = -EINVAL;
  1183. if (!irqchip_in_kernel(vcpu->kvm))
  1184. goto out;
  1185. r = -EFAULT;
  1186. if (copy_from_user(&va, argp, sizeof va))
  1187. goto out;
  1188. r = 0;
  1189. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  1190. break;
  1191. }
  1192. default:
  1193. r = -EINVAL;
  1194. }
  1195. out:
  1196. return r;
  1197. }
  1198. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  1199. {
  1200. int ret;
  1201. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  1202. return -1;
  1203. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  1204. return ret;
  1205. }
  1206. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  1207. u32 kvm_nr_mmu_pages)
  1208. {
  1209. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  1210. return -EINVAL;
  1211. down_write(&kvm->slots_lock);
  1212. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  1213. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  1214. up_write(&kvm->slots_lock);
  1215. return 0;
  1216. }
  1217. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  1218. {
  1219. return kvm->arch.n_alloc_mmu_pages;
  1220. }
  1221. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  1222. {
  1223. int i;
  1224. struct kvm_mem_alias *alias;
  1225. for (i = 0; i < kvm->arch.naliases; ++i) {
  1226. alias = &kvm->arch.aliases[i];
  1227. if (gfn >= alias->base_gfn
  1228. && gfn < alias->base_gfn + alias->npages)
  1229. return alias->target_gfn + gfn - alias->base_gfn;
  1230. }
  1231. return gfn;
  1232. }
  1233. /*
  1234. * Set a new alias region. Aliases map a portion of physical memory into
  1235. * another portion. This is useful for memory windows, for example the PC
  1236. * VGA region.
  1237. */
  1238. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  1239. struct kvm_memory_alias *alias)
  1240. {
  1241. int r, n;
  1242. struct kvm_mem_alias *p;
  1243. r = -EINVAL;
  1244. /* General sanity checks */
  1245. if (alias->memory_size & (PAGE_SIZE - 1))
  1246. goto out;
  1247. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  1248. goto out;
  1249. if (alias->slot >= KVM_ALIAS_SLOTS)
  1250. goto out;
  1251. if (alias->guest_phys_addr + alias->memory_size
  1252. < alias->guest_phys_addr)
  1253. goto out;
  1254. if (alias->target_phys_addr + alias->memory_size
  1255. < alias->target_phys_addr)
  1256. goto out;
  1257. down_write(&kvm->slots_lock);
  1258. p = &kvm->arch.aliases[alias->slot];
  1259. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  1260. p->npages = alias->memory_size >> PAGE_SHIFT;
  1261. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  1262. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  1263. if (kvm->arch.aliases[n - 1].npages)
  1264. break;
  1265. kvm->arch.naliases = n;
  1266. kvm_mmu_zap_all(kvm);
  1267. up_write(&kvm->slots_lock);
  1268. return 0;
  1269. out:
  1270. return r;
  1271. }
  1272. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1273. {
  1274. int r;
  1275. r = 0;
  1276. switch (chip->chip_id) {
  1277. case KVM_IRQCHIP_PIC_MASTER:
  1278. memcpy(&chip->chip.pic,
  1279. &pic_irqchip(kvm)->pics[0],
  1280. sizeof(struct kvm_pic_state));
  1281. break;
  1282. case KVM_IRQCHIP_PIC_SLAVE:
  1283. memcpy(&chip->chip.pic,
  1284. &pic_irqchip(kvm)->pics[1],
  1285. sizeof(struct kvm_pic_state));
  1286. break;
  1287. case KVM_IRQCHIP_IOAPIC:
  1288. memcpy(&chip->chip.ioapic,
  1289. ioapic_irqchip(kvm),
  1290. sizeof(struct kvm_ioapic_state));
  1291. break;
  1292. default:
  1293. r = -EINVAL;
  1294. break;
  1295. }
  1296. return r;
  1297. }
  1298. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1299. {
  1300. int r;
  1301. r = 0;
  1302. switch (chip->chip_id) {
  1303. case KVM_IRQCHIP_PIC_MASTER:
  1304. memcpy(&pic_irqchip(kvm)->pics[0],
  1305. &chip->chip.pic,
  1306. sizeof(struct kvm_pic_state));
  1307. break;
  1308. case KVM_IRQCHIP_PIC_SLAVE:
  1309. memcpy(&pic_irqchip(kvm)->pics[1],
  1310. &chip->chip.pic,
  1311. sizeof(struct kvm_pic_state));
  1312. break;
  1313. case KVM_IRQCHIP_IOAPIC:
  1314. memcpy(ioapic_irqchip(kvm),
  1315. &chip->chip.ioapic,
  1316. sizeof(struct kvm_ioapic_state));
  1317. break;
  1318. default:
  1319. r = -EINVAL;
  1320. break;
  1321. }
  1322. kvm_pic_update_irq(pic_irqchip(kvm));
  1323. return r;
  1324. }
  1325. /*
  1326. * Get (and clear) the dirty memory log for a memory slot.
  1327. */
  1328. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1329. struct kvm_dirty_log *log)
  1330. {
  1331. int r;
  1332. int n;
  1333. struct kvm_memory_slot *memslot;
  1334. int is_dirty = 0;
  1335. down_write(&kvm->slots_lock);
  1336. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1337. if (r)
  1338. goto out;
  1339. /* If nothing is dirty, don't bother messing with page tables. */
  1340. if (is_dirty) {
  1341. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  1342. kvm_flush_remote_tlbs(kvm);
  1343. memslot = &kvm->memslots[log->slot];
  1344. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  1345. memset(memslot->dirty_bitmap, 0, n);
  1346. }
  1347. r = 0;
  1348. out:
  1349. up_write(&kvm->slots_lock);
  1350. return r;
  1351. }
  1352. long kvm_arch_vm_ioctl(struct file *filp,
  1353. unsigned int ioctl, unsigned long arg)
  1354. {
  1355. struct kvm *kvm = filp->private_data;
  1356. void __user *argp = (void __user *)arg;
  1357. int r = -EINVAL;
  1358. switch (ioctl) {
  1359. case KVM_SET_TSS_ADDR:
  1360. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  1361. if (r < 0)
  1362. goto out;
  1363. break;
  1364. case KVM_SET_MEMORY_REGION: {
  1365. struct kvm_memory_region kvm_mem;
  1366. struct kvm_userspace_memory_region kvm_userspace_mem;
  1367. r = -EFAULT;
  1368. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1369. goto out;
  1370. kvm_userspace_mem.slot = kvm_mem.slot;
  1371. kvm_userspace_mem.flags = kvm_mem.flags;
  1372. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  1373. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  1374. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1375. if (r)
  1376. goto out;
  1377. break;
  1378. }
  1379. case KVM_SET_NR_MMU_PAGES:
  1380. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  1381. if (r)
  1382. goto out;
  1383. break;
  1384. case KVM_GET_NR_MMU_PAGES:
  1385. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  1386. break;
  1387. case KVM_SET_MEMORY_ALIAS: {
  1388. struct kvm_memory_alias alias;
  1389. r = -EFAULT;
  1390. if (copy_from_user(&alias, argp, sizeof alias))
  1391. goto out;
  1392. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  1393. if (r)
  1394. goto out;
  1395. break;
  1396. }
  1397. case KVM_CREATE_IRQCHIP:
  1398. r = -ENOMEM;
  1399. kvm->arch.vpic = kvm_create_pic(kvm);
  1400. if (kvm->arch.vpic) {
  1401. r = kvm_ioapic_init(kvm);
  1402. if (r) {
  1403. kfree(kvm->arch.vpic);
  1404. kvm->arch.vpic = NULL;
  1405. goto out;
  1406. }
  1407. } else
  1408. goto out;
  1409. break;
  1410. case KVM_CREATE_PIT:
  1411. r = -ENOMEM;
  1412. kvm->arch.vpit = kvm_create_pit(kvm);
  1413. if (kvm->arch.vpit)
  1414. r = 0;
  1415. break;
  1416. case KVM_IRQ_LINE: {
  1417. struct kvm_irq_level irq_event;
  1418. r = -EFAULT;
  1419. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1420. goto out;
  1421. if (irqchip_in_kernel(kvm)) {
  1422. mutex_lock(&kvm->lock);
  1423. if (irq_event.irq < 16)
  1424. kvm_pic_set_irq(pic_irqchip(kvm),
  1425. irq_event.irq,
  1426. irq_event.level);
  1427. kvm_ioapic_set_irq(kvm->arch.vioapic,
  1428. irq_event.irq,
  1429. irq_event.level);
  1430. mutex_unlock(&kvm->lock);
  1431. r = 0;
  1432. }
  1433. break;
  1434. }
  1435. case KVM_GET_IRQCHIP: {
  1436. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1437. struct kvm_irqchip chip;
  1438. r = -EFAULT;
  1439. if (copy_from_user(&chip, argp, sizeof chip))
  1440. goto out;
  1441. r = -ENXIO;
  1442. if (!irqchip_in_kernel(kvm))
  1443. goto out;
  1444. r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
  1445. if (r)
  1446. goto out;
  1447. r = -EFAULT;
  1448. if (copy_to_user(argp, &chip, sizeof chip))
  1449. goto out;
  1450. r = 0;
  1451. break;
  1452. }
  1453. case KVM_SET_IRQCHIP: {
  1454. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1455. struct kvm_irqchip chip;
  1456. r = -EFAULT;
  1457. if (copy_from_user(&chip, argp, sizeof chip))
  1458. goto out;
  1459. r = -ENXIO;
  1460. if (!irqchip_in_kernel(kvm))
  1461. goto out;
  1462. r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
  1463. if (r)
  1464. goto out;
  1465. r = 0;
  1466. break;
  1467. }
  1468. default:
  1469. ;
  1470. }
  1471. out:
  1472. return r;
  1473. }
  1474. static void kvm_init_msr_list(void)
  1475. {
  1476. u32 dummy[2];
  1477. unsigned i, j;
  1478. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1479. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1480. continue;
  1481. if (j < i)
  1482. msrs_to_save[j] = msrs_to_save[i];
  1483. j++;
  1484. }
  1485. num_msrs_to_save = j;
  1486. }
  1487. /*
  1488. * Only apic need an MMIO device hook, so shortcut now..
  1489. */
  1490. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  1491. gpa_t addr)
  1492. {
  1493. struct kvm_io_device *dev;
  1494. if (vcpu->arch.apic) {
  1495. dev = &vcpu->arch.apic->dev;
  1496. if (dev->in_range(dev, addr))
  1497. return dev;
  1498. }
  1499. return NULL;
  1500. }
  1501. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  1502. gpa_t addr)
  1503. {
  1504. struct kvm_io_device *dev;
  1505. dev = vcpu_find_pervcpu_dev(vcpu, addr);
  1506. if (dev == NULL)
  1507. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
  1508. return dev;
  1509. }
  1510. int emulator_read_std(unsigned long addr,
  1511. void *val,
  1512. unsigned int bytes,
  1513. struct kvm_vcpu *vcpu)
  1514. {
  1515. void *data = val;
  1516. int r = X86EMUL_CONTINUE;
  1517. down_read(&vcpu->kvm->slots_lock);
  1518. while (bytes) {
  1519. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1520. unsigned offset = addr & (PAGE_SIZE-1);
  1521. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  1522. int ret;
  1523. if (gpa == UNMAPPED_GVA) {
  1524. r = X86EMUL_PROPAGATE_FAULT;
  1525. goto out;
  1526. }
  1527. ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
  1528. if (ret < 0) {
  1529. r = X86EMUL_UNHANDLEABLE;
  1530. goto out;
  1531. }
  1532. bytes -= tocopy;
  1533. data += tocopy;
  1534. addr += tocopy;
  1535. }
  1536. out:
  1537. up_read(&vcpu->kvm->slots_lock);
  1538. return r;
  1539. }
  1540. EXPORT_SYMBOL_GPL(emulator_read_std);
  1541. static int emulator_read_emulated(unsigned long addr,
  1542. void *val,
  1543. unsigned int bytes,
  1544. struct kvm_vcpu *vcpu)
  1545. {
  1546. struct kvm_io_device *mmio_dev;
  1547. gpa_t gpa;
  1548. if (vcpu->mmio_read_completed) {
  1549. memcpy(val, vcpu->mmio_data, bytes);
  1550. vcpu->mmio_read_completed = 0;
  1551. return X86EMUL_CONTINUE;
  1552. }
  1553. down_read(&vcpu->kvm->slots_lock);
  1554. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1555. up_read(&vcpu->kvm->slots_lock);
  1556. /* For APIC access vmexit */
  1557. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1558. goto mmio;
  1559. if (emulator_read_std(addr, val, bytes, vcpu)
  1560. == X86EMUL_CONTINUE)
  1561. return X86EMUL_CONTINUE;
  1562. if (gpa == UNMAPPED_GVA)
  1563. return X86EMUL_PROPAGATE_FAULT;
  1564. mmio:
  1565. /*
  1566. * Is this MMIO handled locally?
  1567. */
  1568. mutex_lock(&vcpu->kvm->lock);
  1569. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  1570. if (mmio_dev) {
  1571. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  1572. mutex_unlock(&vcpu->kvm->lock);
  1573. return X86EMUL_CONTINUE;
  1574. }
  1575. mutex_unlock(&vcpu->kvm->lock);
  1576. vcpu->mmio_needed = 1;
  1577. vcpu->mmio_phys_addr = gpa;
  1578. vcpu->mmio_size = bytes;
  1579. vcpu->mmio_is_write = 0;
  1580. return X86EMUL_UNHANDLEABLE;
  1581. }
  1582. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  1583. const void *val, int bytes)
  1584. {
  1585. int ret;
  1586. down_read(&vcpu->kvm->slots_lock);
  1587. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  1588. if (ret < 0) {
  1589. up_read(&vcpu->kvm->slots_lock);
  1590. return 0;
  1591. }
  1592. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  1593. up_read(&vcpu->kvm->slots_lock);
  1594. return 1;
  1595. }
  1596. static int emulator_write_emulated_onepage(unsigned long addr,
  1597. const void *val,
  1598. unsigned int bytes,
  1599. struct kvm_vcpu *vcpu)
  1600. {
  1601. struct kvm_io_device *mmio_dev;
  1602. gpa_t gpa;
  1603. down_read(&vcpu->kvm->slots_lock);
  1604. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1605. up_read(&vcpu->kvm->slots_lock);
  1606. if (gpa == UNMAPPED_GVA) {
  1607. kvm_inject_page_fault(vcpu, addr, 2);
  1608. return X86EMUL_PROPAGATE_FAULT;
  1609. }
  1610. /* For APIC access vmexit */
  1611. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1612. goto mmio;
  1613. if (emulator_write_phys(vcpu, gpa, val, bytes))
  1614. return X86EMUL_CONTINUE;
  1615. mmio:
  1616. /*
  1617. * Is this MMIO handled locally?
  1618. */
  1619. mutex_lock(&vcpu->kvm->lock);
  1620. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  1621. if (mmio_dev) {
  1622. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  1623. mutex_unlock(&vcpu->kvm->lock);
  1624. return X86EMUL_CONTINUE;
  1625. }
  1626. mutex_unlock(&vcpu->kvm->lock);
  1627. vcpu->mmio_needed = 1;
  1628. vcpu->mmio_phys_addr = gpa;
  1629. vcpu->mmio_size = bytes;
  1630. vcpu->mmio_is_write = 1;
  1631. memcpy(vcpu->mmio_data, val, bytes);
  1632. return X86EMUL_CONTINUE;
  1633. }
  1634. int emulator_write_emulated(unsigned long addr,
  1635. const void *val,
  1636. unsigned int bytes,
  1637. struct kvm_vcpu *vcpu)
  1638. {
  1639. /* Crossing a page boundary? */
  1640. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  1641. int rc, now;
  1642. now = -addr & ~PAGE_MASK;
  1643. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  1644. if (rc != X86EMUL_CONTINUE)
  1645. return rc;
  1646. addr += now;
  1647. val += now;
  1648. bytes -= now;
  1649. }
  1650. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  1651. }
  1652. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  1653. static int emulator_cmpxchg_emulated(unsigned long addr,
  1654. const void *old,
  1655. const void *new,
  1656. unsigned int bytes,
  1657. struct kvm_vcpu *vcpu)
  1658. {
  1659. static int reported;
  1660. if (!reported) {
  1661. reported = 1;
  1662. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  1663. }
  1664. #ifndef CONFIG_X86_64
  1665. /* guests cmpxchg8b have to be emulated atomically */
  1666. if (bytes == 8) {
  1667. gpa_t gpa;
  1668. struct page *page;
  1669. char *kaddr;
  1670. u64 val;
  1671. down_read(&vcpu->kvm->slots_lock);
  1672. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1673. if (gpa == UNMAPPED_GVA ||
  1674. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1675. goto emul_write;
  1676. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  1677. goto emul_write;
  1678. val = *(u64 *)new;
  1679. down_read(&current->mm->mmap_sem);
  1680. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  1681. up_read(&current->mm->mmap_sem);
  1682. kaddr = kmap_atomic(page, KM_USER0);
  1683. set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
  1684. kunmap_atomic(kaddr, KM_USER0);
  1685. kvm_release_page_dirty(page);
  1686. emul_write:
  1687. up_read(&vcpu->kvm->slots_lock);
  1688. }
  1689. #endif
  1690. return emulator_write_emulated(addr, new, bytes, vcpu);
  1691. }
  1692. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1693. {
  1694. return kvm_x86_ops->get_segment_base(vcpu, seg);
  1695. }
  1696. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  1697. {
  1698. return X86EMUL_CONTINUE;
  1699. }
  1700. int emulate_clts(struct kvm_vcpu *vcpu)
  1701. {
  1702. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
  1703. return X86EMUL_CONTINUE;
  1704. }
  1705. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  1706. {
  1707. struct kvm_vcpu *vcpu = ctxt->vcpu;
  1708. switch (dr) {
  1709. case 0 ... 3:
  1710. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  1711. return X86EMUL_CONTINUE;
  1712. default:
  1713. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
  1714. return X86EMUL_UNHANDLEABLE;
  1715. }
  1716. }
  1717. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  1718. {
  1719. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  1720. int exception;
  1721. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  1722. if (exception) {
  1723. /* FIXME: better handling */
  1724. return X86EMUL_UNHANDLEABLE;
  1725. }
  1726. return X86EMUL_CONTINUE;
  1727. }
  1728. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  1729. {
  1730. static int reported;
  1731. u8 opcodes[4];
  1732. unsigned long rip = vcpu->arch.rip;
  1733. unsigned long rip_linear;
  1734. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  1735. if (reported)
  1736. return;
  1737. emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
  1738. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  1739. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  1740. reported = 1;
  1741. }
  1742. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  1743. static struct x86_emulate_ops emulate_ops = {
  1744. .read_std = emulator_read_std,
  1745. .read_emulated = emulator_read_emulated,
  1746. .write_emulated = emulator_write_emulated,
  1747. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  1748. };
  1749. int emulate_instruction(struct kvm_vcpu *vcpu,
  1750. struct kvm_run *run,
  1751. unsigned long cr2,
  1752. u16 error_code,
  1753. int emulation_type)
  1754. {
  1755. int r;
  1756. struct decode_cache *c;
  1757. vcpu->arch.mmio_fault_cr2 = cr2;
  1758. kvm_x86_ops->cache_regs(vcpu);
  1759. vcpu->mmio_is_write = 0;
  1760. vcpu->arch.pio.string = 0;
  1761. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  1762. int cs_db, cs_l;
  1763. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  1764. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  1765. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  1766. vcpu->arch.emulate_ctxt.mode =
  1767. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  1768. ? X86EMUL_MODE_REAL : cs_l
  1769. ? X86EMUL_MODE_PROT64 : cs_db
  1770. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  1771. if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  1772. vcpu->arch.emulate_ctxt.cs_base = 0;
  1773. vcpu->arch.emulate_ctxt.ds_base = 0;
  1774. vcpu->arch.emulate_ctxt.es_base = 0;
  1775. vcpu->arch.emulate_ctxt.ss_base = 0;
  1776. } else {
  1777. vcpu->arch.emulate_ctxt.cs_base =
  1778. get_segment_base(vcpu, VCPU_SREG_CS);
  1779. vcpu->arch.emulate_ctxt.ds_base =
  1780. get_segment_base(vcpu, VCPU_SREG_DS);
  1781. vcpu->arch.emulate_ctxt.es_base =
  1782. get_segment_base(vcpu, VCPU_SREG_ES);
  1783. vcpu->arch.emulate_ctxt.ss_base =
  1784. get_segment_base(vcpu, VCPU_SREG_SS);
  1785. }
  1786. vcpu->arch.emulate_ctxt.gs_base =
  1787. get_segment_base(vcpu, VCPU_SREG_GS);
  1788. vcpu->arch.emulate_ctxt.fs_base =
  1789. get_segment_base(vcpu, VCPU_SREG_FS);
  1790. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  1791. /* Reject the instructions other than VMCALL/VMMCALL when
  1792. * try to emulate invalid opcode */
  1793. c = &vcpu->arch.emulate_ctxt.decode;
  1794. if ((emulation_type & EMULTYPE_TRAP_UD) &&
  1795. (!(c->twobyte && c->b == 0x01 &&
  1796. (c->modrm_reg == 0 || c->modrm_reg == 3) &&
  1797. c->modrm_mod == 3 && c->modrm_rm == 1)))
  1798. return EMULATE_FAIL;
  1799. ++vcpu->stat.insn_emulation;
  1800. if (r) {
  1801. ++vcpu->stat.insn_emulation_fail;
  1802. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1803. return EMULATE_DONE;
  1804. return EMULATE_FAIL;
  1805. }
  1806. }
  1807. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  1808. if (vcpu->arch.pio.string)
  1809. return EMULATE_DO_MMIO;
  1810. if ((r || vcpu->mmio_is_write) && run) {
  1811. run->exit_reason = KVM_EXIT_MMIO;
  1812. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  1813. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  1814. run->mmio.len = vcpu->mmio_size;
  1815. run->mmio.is_write = vcpu->mmio_is_write;
  1816. }
  1817. if (r) {
  1818. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1819. return EMULATE_DONE;
  1820. if (!vcpu->mmio_needed) {
  1821. kvm_report_emulation_failure(vcpu, "mmio");
  1822. return EMULATE_FAIL;
  1823. }
  1824. return EMULATE_DO_MMIO;
  1825. }
  1826. kvm_x86_ops->decache_regs(vcpu);
  1827. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  1828. if (vcpu->mmio_is_write) {
  1829. vcpu->mmio_needed = 0;
  1830. return EMULATE_DO_MMIO;
  1831. }
  1832. return EMULATE_DONE;
  1833. }
  1834. EXPORT_SYMBOL_GPL(emulate_instruction);
  1835. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  1836. {
  1837. int i;
  1838. for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
  1839. if (vcpu->arch.pio.guest_pages[i]) {
  1840. kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
  1841. vcpu->arch.pio.guest_pages[i] = NULL;
  1842. }
  1843. }
  1844. static int pio_copy_data(struct kvm_vcpu *vcpu)
  1845. {
  1846. void *p = vcpu->arch.pio_data;
  1847. void *q;
  1848. unsigned bytes;
  1849. int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
  1850. q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  1851. PAGE_KERNEL);
  1852. if (!q) {
  1853. free_pio_guest_pages(vcpu);
  1854. return -ENOMEM;
  1855. }
  1856. q += vcpu->arch.pio.guest_page_offset;
  1857. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  1858. if (vcpu->arch.pio.in)
  1859. memcpy(q, p, bytes);
  1860. else
  1861. memcpy(p, q, bytes);
  1862. q -= vcpu->arch.pio.guest_page_offset;
  1863. vunmap(q);
  1864. free_pio_guest_pages(vcpu);
  1865. return 0;
  1866. }
  1867. int complete_pio(struct kvm_vcpu *vcpu)
  1868. {
  1869. struct kvm_pio_request *io = &vcpu->arch.pio;
  1870. long delta;
  1871. int r;
  1872. kvm_x86_ops->cache_regs(vcpu);
  1873. if (!io->string) {
  1874. if (io->in)
  1875. memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
  1876. io->size);
  1877. } else {
  1878. if (io->in) {
  1879. r = pio_copy_data(vcpu);
  1880. if (r) {
  1881. kvm_x86_ops->cache_regs(vcpu);
  1882. return r;
  1883. }
  1884. }
  1885. delta = 1;
  1886. if (io->rep) {
  1887. delta *= io->cur_count;
  1888. /*
  1889. * The size of the register should really depend on
  1890. * current address size.
  1891. */
  1892. vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
  1893. }
  1894. if (io->down)
  1895. delta = -delta;
  1896. delta *= io->size;
  1897. if (io->in)
  1898. vcpu->arch.regs[VCPU_REGS_RDI] += delta;
  1899. else
  1900. vcpu->arch.regs[VCPU_REGS_RSI] += delta;
  1901. }
  1902. kvm_x86_ops->decache_regs(vcpu);
  1903. io->count -= io->cur_count;
  1904. io->cur_count = 0;
  1905. return 0;
  1906. }
  1907. static void kernel_pio(struct kvm_io_device *pio_dev,
  1908. struct kvm_vcpu *vcpu,
  1909. void *pd)
  1910. {
  1911. /* TODO: String I/O for in kernel device */
  1912. mutex_lock(&vcpu->kvm->lock);
  1913. if (vcpu->arch.pio.in)
  1914. kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
  1915. vcpu->arch.pio.size,
  1916. pd);
  1917. else
  1918. kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
  1919. vcpu->arch.pio.size,
  1920. pd);
  1921. mutex_unlock(&vcpu->kvm->lock);
  1922. }
  1923. static void pio_string_write(struct kvm_io_device *pio_dev,
  1924. struct kvm_vcpu *vcpu)
  1925. {
  1926. struct kvm_pio_request *io = &vcpu->arch.pio;
  1927. void *pd = vcpu->arch.pio_data;
  1928. int i;
  1929. mutex_lock(&vcpu->kvm->lock);
  1930. for (i = 0; i < io->cur_count; i++) {
  1931. kvm_iodevice_write(pio_dev, io->port,
  1932. io->size,
  1933. pd);
  1934. pd += io->size;
  1935. }
  1936. mutex_unlock(&vcpu->kvm->lock);
  1937. }
  1938. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  1939. gpa_t addr)
  1940. {
  1941. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
  1942. }
  1943. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1944. int size, unsigned port)
  1945. {
  1946. struct kvm_io_device *pio_dev;
  1947. vcpu->run->exit_reason = KVM_EXIT_IO;
  1948. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1949. vcpu->run->io.size = vcpu->arch.pio.size = size;
  1950. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1951. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  1952. vcpu->run->io.port = vcpu->arch.pio.port = port;
  1953. vcpu->arch.pio.in = in;
  1954. vcpu->arch.pio.string = 0;
  1955. vcpu->arch.pio.down = 0;
  1956. vcpu->arch.pio.guest_page_offset = 0;
  1957. vcpu->arch.pio.rep = 0;
  1958. kvm_x86_ops->cache_regs(vcpu);
  1959. memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
  1960. kvm_x86_ops->decache_regs(vcpu);
  1961. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1962. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1963. if (pio_dev) {
  1964. kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
  1965. complete_pio(vcpu);
  1966. return 1;
  1967. }
  1968. return 0;
  1969. }
  1970. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  1971. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1972. int size, unsigned long count, int down,
  1973. gva_t address, int rep, unsigned port)
  1974. {
  1975. unsigned now, in_page;
  1976. int i, ret = 0;
  1977. int nr_pages = 1;
  1978. struct page *page;
  1979. struct kvm_io_device *pio_dev;
  1980. vcpu->run->exit_reason = KVM_EXIT_IO;
  1981. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1982. vcpu->run->io.size = vcpu->arch.pio.size = size;
  1983. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1984. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  1985. vcpu->run->io.port = vcpu->arch.pio.port = port;
  1986. vcpu->arch.pio.in = in;
  1987. vcpu->arch.pio.string = 1;
  1988. vcpu->arch.pio.down = down;
  1989. vcpu->arch.pio.guest_page_offset = offset_in_page(address);
  1990. vcpu->arch.pio.rep = rep;
  1991. if (!count) {
  1992. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1993. return 1;
  1994. }
  1995. if (!down)
  1996. in_page = PAGE_SIZE - offset_in_page(address);
  1997. else
  1998. in_page = offset_in_page(address) + size;
  1999. now = min(count, (unsigned long)in_page / size);
  2000. if (!now) {
  2001. /*
  2002. * String I/O straddles page boundary. Pin two guest pages
  2003. * so that we satisfy atomicity constraints. Do just one
  2004. * transaction to avoid complexity.
  2005. */
  2006. nr_pages = 2;
  2007. now = 1;
  2008. }
  2009. if (down) {
  2010. /*
  2011. * String I/O in reverse. Yuck. Kill the guest, fix later.
  2012. */
  2013. pr_unimpl(vcpu, "guest string pio down\n");
  2014. kvm_inject_gp(vcpu, 0);
  2015. return 1;
  2016. }
  2017. vcpu->run->io.count = now;
  2018. vcpu->arch.pio.cur_count = now;
  2019. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  2020. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2021. for (i = 0; i < nr_pages; ++i) {
  2022. down_read(&vcpu->kvm->slots_lock);
  2023. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  2024. vcpu->arch.pio.guest_pages[i] = page;
  2025. up_read(&vcpu->kvm->slots_lock);
  2026. if (!page) {
  2027. kvm_inject_gp(vcpu, 0);
  2028. free_pio_guest_pages(vcpu);
  2029. return 1;
  2030. }
  2031. }
  2032. pio_dev = vcpu_find_pio_dev(vcpu, port);
  2033. if (!vcpu->arch.pio.in) {
  2034. /* string PIO write */
  2035. ret = pio_copy_data(vcpu);
  2036. if (ret >= 0 && pio_dev) {
  2037. pio_string_write(pio_dev, vcpu);
  2038. complete_pio(vcpu);
  2039. if (vcpu->arch.pio.count == 0)
  2040. ret = 1;
  2041. }
  2042. } else if (pio_dev)
  2043. pr_unimpl(vcpu, "no string pio read support yet, "
  2044. "port %x size %d count %ld\n",
  2045. port, size, count);
  2046. return ret;
  2047. }
  2048. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  2049. int kvm_arch_init(void *opaque)
  2050. {
  2051. int r;
  2052. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  2053. if (kvm_x86_ops) {
  2054. printk(KERN_ERR "kvm: already loaded the other module\n");
  2055. r = -EEXIST;
  2056. goto out;
  2057. }
  2058. if (!ops->cpu_has_kvm_support()) {
  2059. printk(KERN_ERR "kvm: no hardware support\n");
  2060. r = -EOPNOTSUPP;
  2061. goto out;
  2062. }
  2063. if (ops->disabled_by_bios()) {
  2064. printk(KERN_ERR "kvm: disabled by bios\n");
  2065. r = -EOPNOTSUPP;
  2066. goto out;
  2067. }
  2068. r = kvm_mmu_module_init();
  2069. if (r)
  2070. goto out;
  2071. kvm_init_msr_list();
  2072. kvm_x86_ops = ops;
  2073. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  2074. return 0;
  2075. out:
  2076. return r;
  2077. }
  2078. void kvm_arch_exit(void)
  2079. {
  2080. kvm_x86_ops = NULL;
  2081. kvm_mmu_module_exit();
  2082. }
  2083. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  2084. {
  2085. ++vcpu->stat.halt_exits;
  2086. if (irqchip_in_kernel(vcpu->kvm)) {
  2087. vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
  2088. kvm_vcpu_block(vcpu);
  2089. if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
  2090. return -EINTR;
  2091. return 1;
  2092. } else {
  2093. vcpu->run->exit_reason = KVM_EXIT_HLT;
  2094. return 0;
  2095. }
  2096. }
  2097. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  2098. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  2099. {
  2100. unsigned long nr, a0, a1, a2, a3, ret;
  2101. kvm_x86_ops->cache_regs(vcpu);
  2102. nr = vcpu->arch.regs[VCPU_REGS_RAX];
  2103. a0 = vcpu->arch.regs[VCPU_REGS_RBX];
  2104. a1 = vcpu->arch.regs[VCPU_REGS_RCX];
  2105. a2 = vcpu->arch.regs[VCPU_REGS_RDX];
  2106. a3 = vcpu->arch.regs[VCPU_REGS_RSI];
  2107. if (!is_long_mode(vcpu)) {
  2108. nr &= 0xFFFFFFFF;
  2109. a0 &= 0xFFFFFFFF;
  2110. a1 &= 0xFFFFFFFF;
  2111. a2 &= 0xFFFFFFFF;
  2112. a3 &= 0xFFFFFFFF;
  2113. }
  2114. switch (nr) {
  2115. case KVM_HC_VAPIC_POLL_IRQ:
  2116. ret = 0;
  2117. break;
  2118. default:
  2119. ret = -KVM_ENOSYS;
  2120. break;
  2121. }
  2122. vcpu->arch.regs[VCPU_REGS_RAX] = ret;
  2123. kvm_x86_ops->decache_regs(vcpu);
  2124. ++vcpu->stat.hypercalls;
  2125. return 0;
  2126. }
  2127. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  2128. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  2129. {
  2130. char instruction[3];
  2131. int ret = 0;
  2132. /*
  2133. * Blow out the MMU to ensure that no other VCPU has an active mapping
  2134. * to ensure that the updated hypercall appears atomically across all
  2135. * VCPUs.
  2136. */
  2137. kvm_mmu_zap_all(vcpu->kvm);
  2138. kvm_x86_ops->cache_regs(vcpu);
  2139. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  2140. if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
  2141. != X86EMUL_CONTINUE)
  2142. ret = -EFAULT;
  2143. return ret;
  2144. }
  2145. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  2146. {
  2147. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  2148. }
  2149. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2150. {
  2151. struct descriptor_table dt = { limit, base };
  2152. kvm_x86_ops->set_gdt(vcpu, &dt);
  2153. }
  2154. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2155. {
  2156. struct descriptor_table dt = { limit, base };
  2157. kvm_x86_ops->set_idt(vcpu, &dt);
  2158. }
  2159. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  2160. unsigned long *rflags)
  2161. {
  2162. kvm_lmsw(vcpu, msw);
  2163. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2164. }
  2165. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  2166. {
  2167. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2168. switch (cr) {
  2169. case 0:
  2170. return vcpu->arch.cr0;
  2171. case 2:
  2172. return vcpu->arch.cr2;
  2173. case 3:
  2174. return vcpu->arch.cr3;
  2175. case 4:
  2176. return vcpu->arch.cr4;
  2177. case 8:
  2178. return kvm_get_cr8(vcpu);
  2179. default:
  2180. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2181. return 0;
  2182. }
  2183. }
  2184. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  2185. unsigned long *rflags)
  2186. {
  2187. switch (cr) {
  2188. case 0:
  2189. kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
  2190. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2191. break;
  2192. case 2:
  2193. vcpu->arch.cr2 = val;
  2194. break;
  2195. case 3:
  2196. kvm_set_cr3(vcpu, val);
  2197. break;
  2198. case 4:
  2199. kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
  2200. break;
  2201. case 8:
  2202. kvm_set_cr8(vcpu, val & 0xfUL);
  2203. break;
  2204. default:
  2205. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2206. }
  2207. }
  2208. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  2209. {
  2210. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  2211. int j, nent = vcpu->arch.cpuid_nent;
  2212. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  2213. /* when no next entry is found, the current entry[i] is reselected */
  2214. for (j = i + 1; j == i; j = (j + 1) % nent) {
  2215. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  2216. if (ej->function == e->function) {
  2217. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2218. return j;
  2219. }
  2220. }
  2221. return 0; /* silence gcc, even though control never reaches here */
  2222. }
  2223. /* find an entry with matching function, matching index (if needed), and that
  2224. * should be read next (if it's stateful) */
  2225. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  2226. u32 function, u32 index)
  2227. {
  2228. if (e->function != function)
  2229. return 0;
  2230. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  2231. return 0;
  2232. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  2233. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  2234. return 0;
  2235. return 1;
  2236. }
  2237. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  2238. {
  2239. int i;
  2240. u32 function, index;
  2241. struct kvm_cpuid_entry2 *e, *best;
  2242. kvm_x86_ops->cache_regs(vcpu);
  2243. function = vcpu->arch.regs[VCPU_REGS_RAX];
  2244. index = vcpu->arch.regs[VCPU_REGS_RCX];
  2245. vcpu->arch.regs[VCPU_REGS_RAX] = 0;
  2246. vcpu->arch.regs[VCPU_REGS_RBX] = 0;
  2247. vcpu->arch.regs[VCPU_REGS_RCX] = 0;
  2248. vcpu->arch.regs[VCPU_REGS_RDX] = 0;
  2249. best = NULL;
  2250. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  2251. e = &vcpu->arch.cpuid_entries[i];
  2252. if (is_matching_cpuid_entry(e, function, index)) {
  2253. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  2254. move_to_next_stateful_cpuid_entry(vcpu, i);
  2255. best = e;
  2256. break;
  2257. }
  2258. /*
  2259. * Both basic or both extended?
  2260. */
  2261. if (((e->function ^ function) & 0x80000000) == 0)
  2262. if (!best || e->function > best->function)
  2263. best = e;
  2264. }
  2265. if (best) {
  2266. vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
  2267. vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
  2268. vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
  2269. vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
  2270. }
  2271. kvm_x86_ops->decache_regs(vcpu);
  2272. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2273. }
  2274. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  2275. /*
  2276. * Check if userspace requested an interrupt window, and that the
  2277. * interrupt window is open.
  2278. *
  2279. * No need to exit to userspace if we already have an interrupt queued.
  2280. */
  2281. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  2282. struct kvm_run *kvm_run)
  2283. {
  2284. return (!vcpu->arch.irq_summary &&
  2285. kvm_run->request_interrupt_window &&
  2286. vcpu->arch.interrupt_window_open &&
  2287. (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
  2288. }
  2289. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  2290. struct kvm_run *kvm_run)
  2291. {
  2292. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  2293. kvm_run->cr8 = kvm_get_cr8(vcpu);
  2294. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  2295. if (irqchip_in_kernel(vcpu->kvm))
  2296. kvm_run->ready_for_interrupt_injection = 1;
  2297. else
  2298. kvm_run->ready_for_interrupt_injection =
  2299. (vcpu->arch.interrupt_window_open &&
  2300. vcpu->arch.irq_summary == 0);
  2301. }
  2302. static void vapic_enter(struct kvm_vcpu *vcpu)
  2303. {
  2304. struct kvm_lapic *apic = vcpu->arch.apic;
  2305. struct page *page;
  2306. if (!apic || !apic->vapic_addr)
  2307. return;
  2308. down_read(&current->mm->mmap_sem);
  2309. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2310. up_read(&current->mm->mmap_sem);
  2311. vcpu->arch.apic->vapic_page = page;
  2312. }
  2313. static void vapic_exit(struct kvm_vcpu *vcpu)
  2314. {
  2315. struct kvm_lapic *apic = vcpu->arch.apic;
  2316. if (!apic || !apic->vapic_addr)
  2317. return;
  2318. kvm_release_page_dirty(apic->vapic_page);
  2319. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2320. }
  2321. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2322. {
  2323. int r;
  2324. if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
  2325. pr_debug("vcpu %d received sipi with vector # %x\n",
  2326. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  2327. kvm_lapic_reset(vcpu);
  2328. r = kvm_x86_ops->vcpu_reset(vcpu);
  2329. if (r)
  2330. return r;
  2331. vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
  2332. }
  2333. vapic_enter(vcpu);
  2334. preempted:
  2335. if (vcpu->guest_debug.enabled)
  2336. kvm_x86_ops->guest_debug_pre(vcpu);
  2337. again:
  2338. if (vcpu->requests)
  2339. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  2340. kvm_mmu_unload(vcpu);
  2341. r = kvm_mmu_reload(vcpu);
  2342. if (unlikely(r))
  2343. goto out;
  2344. if (vcpu->requests) {
  2345. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  2346. __kvm_migrate_apic_timer(vcpu);
  2347. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  2348. &vcpu->requests)) {
  2349. kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
  2350. r = 0;
  2351. goto out;
  2352. }
  2353. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  2354. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2355. r = 0;
  2356. goto out;
  2357. }
  2358. }
  2359. kvm_inject_pending_timer_irqs(vcpu);
  2360. preempt_disable();
  2361. kvm_x86_ops->prepare_guest_switch(vcpu);
  2362. kvm_load_guest_fpu(vcpu);
  2363. local_irq_disable();
  2364. if (need_resched()) {
  2365. local_irq_enable();
  2366. preempt_enable();
  2367. r = 1;
  2368. goto out;
  2369. }
  2370. if (vcpu->requests)
  2371. if (test_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) {
  2372. local_irq_enable();
  2373. preempt_enable();
  2374. r = 1;
  2375. goto out;
  2376. }
  2377. if (signal_pending(current)) {
  2378. local_irq_enable();
  2379. preempt_enable();
  2380. r = -EINTR;
  2381. kvm_run->exit_reason = KVM_EXIT_INTR;
  2382. ++vcpu->stat.signal_exits;
  2383. goto out;
  2384. }
  2385. if (vcpu->arch.exception.pending)
  2386. __queue_exception(vcpu);
  2387. else if (irqchip_in_kernel(vcpu->kvm))
  2388. kvm_x86_ops->inject_pending_irq(vcpu);
  2389. else
  2390. kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
  2391. kvm_lapic_sync_to_vapic(vcpu);
  2392. vcpu->guest_mode = 1;
  2393. kvm_guest_enter();
  2394. if (vcpu->requests)
  2395. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  2396. kvm_x86_ops->tlb_flush(vcpu);
  2397. kvm_x86_ops->run(vcpu, kvm_run);
  2398. vcpu->guest_mode = 0;
  2399. local_irq_enable();
  2400. ++vcpu->stat.exits;
  2401. /*
  2402. * We must have an instruction between local_irq_enable() and
  2403. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  2404. * the interrupt shadow. The stat.exits increment will do nicely.
  2405. * But we need to prevent reordering, hence this barrier():
  2406. */
  2407. barrier();
  2408. kvm_guest_exit();
  2409. preempt_enable();
  2410. /*
  2411. * Profile KVM exit RIPs:
  2412. */
  2413. if (unlikely(prof_on == KVM_PROFILING)) {
  2414. kvm_x86_ops->cache_regs(vcpu);
  2415. profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
  2416. }
  2417. if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
  2418. vcpu->arch.exception.pending = false;
  2419. kvm_lapic_sync_from_vapic(vcpu);
  2420. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  2421. if (r > 0) {
  2422. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  2423. r = -EINTR;
  2424. kvm_run->exit_reason = KVM_EXIT_INTR;
  2425. ++vcpu->stat.request_irq_exits;
  2426. goto out;
  2427. }
  2428. if (!need_resched())
  2429. goto again;
  2430. }
  2431. out:
  2432. if (r > 0) {
  2433. kvm_resched(vcpu);
  2434. goto preempted;
  2435. }
  2436. post_kvm_run_save(vcpu, kvm_run);
  2437. vapic_exit(vcpu);
  2438. return r;
  2439. }
  2440. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2441. {
  2442. int r;
  2443. sigset_t sigsaved;
  2444. vcpu_load(vcpu);
  2445. if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
  2446. kvm_vcpu_block(vcpu);
  2447. vcpu_put(vcpu);
  2448. return -EAGAIN;
  2449. }
  2450. if (vcpu->sigset_active)
  2451. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  2452. /* re-sync apic's tpr */
  2453. if (!irqchip_in_kernel(vcpu->kvm))
  2454. kvm_set_cr8(vcpu, kvm_run->cr8);
  2455. if (vcpu->arch.pio.cur_count) {
  2456. r = complete_pio(vcpu);
  2457. if (r)
  2458. goto out;
  2459. }
  2460. #if CONFIG_HAS_IOMEM
  2461. if (vcpu->mmio_needed) {
  2462. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  2463. vcpu->mmio_read_completed = 1;
  2464. vcpu->mmio_needed = 0;
  2465. r = emulate_instruction(vcpu, kvm_run,
  2466. vcpu->arch.mmio_fault_cr2, 0,
  2467. EMULTYPE_NO_DECODE);
  2468. if (r == EMULATE_DO_MMIO) {
  2469. /*
  2470. * Read-modify-write. Back to userspace.
  2471. */
  2472. r = 0;
  2473. goto out;
  2474. }
  2475. }
  2476. #endif
  2477. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  2478. kvm_x86_ops->cache_regs(vcpu);
  2479. vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  2480. kvm_x86_ops->decache_regs(vcpu);
  2481. }
  2482. r = __vcpu_run(vcpu, kvm_run);
  2483. out:
  2484. if (vcpu->sigset_active)
  2485. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  2486. vcpu_put(vcpu);
  2487. return r;
  2488. }
  2489. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2490. {
  2491. vcpu_load(vcpu);
  2492. kvm_x86_ops->cache_regs(vcpu);
  2493. regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2494. regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
  2495. regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
  2496. regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
  2497. regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
  2498. regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
  2499. regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2500. regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
  2501. #ifdef CONFIG_X86_64
  2502. regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
  2503. regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
  2504. regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
  2505. regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
  2506. regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
  2507. regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
  2508. regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
  2509. regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
  2510. #endif
  2511. regs->rip = vcpu->arch.rip;
  2512. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  2513. /*
  2514. * Don't leak debug flags in case they were set for guest debugging
  2515. */
  2516. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  2517. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  2518. vcpu_put(vcpu);
  2519. return 0;
  2520. }
  2521. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2522. {
  2523. vcpu_load(vcpu);
  2524. vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
  2525. vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
  2526. vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
  2527. vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
  2528. vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
  2529. vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
  2530. vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
  2531. vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
  2532. #ifdef CONFIG_X86_64
  2533. vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
  2534. vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
  2535. vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
  2536. vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
  2537. vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
  2538. vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
  2539. vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
  2540. vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
  2541. #endif
  2542. vcpu->arch.rip = regs->rip;
  2543. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  2544. kvm_x86_ops->decache_regs(vcpu);
  2545. vcpu_put(vcpu);
  2546. return 0;
  2547. }
  2548. static void get_segment(struct kvm_vcpu *vcpu,
  2549. struct kvm_segment *var, int seg)
  2550. {
  2551. kvm_x86_ops->get_segment(vcpu, var, seg);
  2552. }
  2553. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2554. {
  2555. struct kvm_segment cs;
  2556. get_segment(vcpu, &cs, VCPU_SREG_CS);
  2557. *db = cs.db;
  2558. *l = cs.l;
  2559. }
  2560. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2561. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  2562. struct kvm_sregs *sregs)
  2563. {
  2564. struct descriptor_table dt;
  2565. int pending_vec;
  2566. vcpu_load(vcpu);
  2567. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2568. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2569. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2570. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2571. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2572. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2573. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2574. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2575. kvm_x86_ops->get_idt(vcpu, &dt);
  2576. sregs->idt.limit = dt.limit;
  2577. sregs->idt.base = dt.base;
  2578. kvm_x86_ops->get_gdt(vcpu, &dt);
  2579. sregs->gdt.limit = dt.limit;
  2580. sregs->gdt.base = dt.base;
  2581. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2582. sregs->cr0 = vcpu->arch.cr0;
  2583. sregs->cr2 = vcpu->arch.cr2;
  2584. sregs->cr3 = vcpu->arch.cr3;
  2585. sregs->cr4 = vcpu->arch.cr4;
  2586. sregs->cr8 = kvm_get_cr8(vcpu);
  2587. sregs->efer = vcpu->arch.shadow_efer;
  2588. sregs->apic_base = kvm_get_apic_base(vcpu);
  2589. if (irqchip_in_kernel(vcpu->kvm)) {
  2590. memset(sregs->interrupt_bitmap, 0,
  2591. sizeof sregs->interrupt_bitmap);
  2592. pending_vec = kvm_x86_ops->get_irq(vcpu);
  2593. if (pending_vec >= 0)
  2594. set_bit(pending_vec,
  2595. (unsigned long *)sregs->interrupt_bitmap);
  2596. } else
  2597. memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
  2598. sizeof sregs->interrupt_bitmap);
  2599. vcpu_put(vcpu);
  2600. return 0;
  2601. }
  2602. static void set_segment(struct kvm_vcpu *vcpu,
  2603. struct kvm_segment *var, int seg)
  2604. {
  2605. kvm_x86_ops->set_segment(vcpu, var, seg);
  2606. }
  2607. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  2608. struct kvm_sregs *sregs)
  2609. {
  2610. int mmu_reset_needed = 0;
  2611. int i, pending_vec, max_bits;
  2612. struct descriptor_table dt;
  2613. vcpu_load(vcpu);
  2614. dt.limit = sregs->idt.limit;
  2615. dt.base = sregs->idt.base;
  2616. kvm_x86_ops->set_idt(vcpu, &dt);
  2617. dt.limit = sregs->gdt.limit;
  2618. dt.base = sregs->gdt.base;
  2619. kvm_x86_ops->set_gdt(vcpu, &dt);
  2620. vcpu->arch.cr2 = sregs->cr2;
  2621. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  2622. vcpu->arch.cr3 = sregs->cr3;
  2623. kvm_set_cr8(vcpu, sregs->cr8);
  2624. mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
  2625. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  2626. kvm_set_apic_base(vcpu, sregs->apic_base);
  2627. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2628. mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
  2629. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  2630. vcpu->arch.cr0 = sregs->cr0;
  2631. mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
  2632. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  2633. if (!is_long_mode(vcpu) && is_pae(vcpu))
  2634. load_pdptrs(vcpu, vcpu->arch.cr3);
  2635. if (mmu_reset_needed)
  2636. kvm_mmu_reset_context(vcpu);
  2637. if (!irqchip_in_kernel(vcpu->kvm)) {
  2638. memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
  2639. sizeof vcpu->arch.irq_pending);
  2640. vcpu->arch.irq_summary = 0;
  2641. for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
  2642. if (vcpu->arch.irq_pending[i])
  2643. __set_bit(i, &vcpu->arch.irq_summary);
  2644. } else {
  2645. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  2646. pending_vec = find_first_bit(
  2647. (const unsigned long *)sregs->interrupt_bitmap,
  2648. max_bits);
  2649. /* Only pending external irq is handled here */
  2650. if (pending_vec < max_bits) {
  2651. kvm_x86_ops->set_irq(vcpu, pending_vec);
  2652. pr_debug("Set back pending irq %d\n",
  2653. pending_vec);
  2654. }
  2655. }
  2656. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2657. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2658. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2659. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2660. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2661. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2662. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2663. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2664. vcpu_put(vcpu);
  2665. return 0;
  2666. }
  2667. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  2668. struct kvm_debug_guest *dbg)
  2669. {
  2670. int r;
  2671. vcpu_load(vcpu);
  2672. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  2673. vcpu_put(vcpu);
  2674. return r;
  2675. }
  2676. /*
  2677. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2678. * we have asm/x86/processor.h
  2679. */
  2680. struct fxsave {
  2681. u16 cwd;
  2682. u16 swd;
  2683. u16 twd;
  2684. u16 fop;
  2685. u64 rip;
  2686. u64 rdp;
  2687. u32 mxcsr;
  2688. u32 mxcsr_mask;
  2689. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2690. #ifdef CONFIG_X86_64
  2691. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2692. #else
  2693. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2694. #endif
  2695. };
  2696. /*
  2697. * Translate a guest virtual address to a guest physical address.
  2698. */
  2699. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  2700. struct kvm_translation *tr)
  2701. {
  2702. unsigned long vaddr = tr->linear_address;
  2703. gpa_t gpa;
  2704. vcpu_load(vcpu);
  2705. down_read(&vcpu->kvm->slots_lock);
  2706. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
  2707. up_read(&vcpu->kvm->slots_lock);
  2708. tr->physical_address = gpa;
  2709. tr->valid = gpa != UNMAPPED_GVA;
  2710. tr->writeable = 1;
  2711. tr->usermode = 0;
  2712. vcpu_put(vcpu);
  2713. return 0;
  2714. }
  2715. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2716. {
  2717. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  2718. vcpu_load(vcpu);
  2719. memcpy(fpu->fpr, fxsave->st_space, 128);
  2720. fpu->fcw = fxsave->cwd;
  2721. fpu->fsw = fxsave->swd;
  2722. fpu->ftwx = fxsave->twd;
  2723. fpu->last_opcode = fxsave->fop;
  2724. fpu->last_ip = fxsave->rip;
  2725. fpu->last_dp = fxsave->rdp;
  2726. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2727. vcpu_put(vcpu);
  2728. return 0;
  2729. }
  2730. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2731. {
  2732. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  2733. vcpu_load(vcpu);
  2734. memcpy(fxsave->st_space, fpu->fpr, 128);
  2735. fxsave->cwd = fpu->fcw;
  2736. fxsave->swd = fpu->fsw;
  2737. fxsave->twd = fpu->ftwx;
  2738. fxsave->fop = fpu->last_opcode;
  2739. fxsave->rip = fpu->last_ip;
  2740. fxsave->rdp = fpu->last_dp;
  2741. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2742. vcpu_put(vcpu);
  2743. return 0;
  2744. }
  2745. void fx_init(struct kvm_vcpu *vcpu)
  2746. {
  2747. unsigned after_mxcsr_mask;
  2748. /* Initialize guest FPU by resetting ours and saving into guest's */
  2749. preempt_disable();
  2750. fx_save(&vcpu->arch.host_fx_image);
  2751. fpu_init();
  2752. fx_save(&vcpu->arch.guest_fx_image);
  2753. fx_restore(&vcpu->arch.host_fx_image);
  2754. preempt_enable();
  2755. vcpu->arch.cr0 |= X86_CR0_ET;
  2756. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  2757. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  2758. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  2759. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  2760. }
  2761. EXPORT_SYMBOL_GPL(fx_init);
  2762. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  2763. {
  2764. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  2765. return;
  2766. vcpu->guest_fpu_loaded = 1;
  2767. fx_save(&vcpu->arch.host_fx_image);
  2768. fx_restore(&vcpu->arch.guest_fx_image);
  2769. }
  2770. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  2771. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  2772. {
  2773. if (!vcpu->guest_fpu_loaded)
  2774. return;
  2775. vcpu->guest_fpu_loaded = 0;
  2776. fx_save(&vcpu->arch.guest_fx_image);
  2777. fx_restore(&vcpu->arch.host_fx_image);
  2778. ++vcpu->stat.fpu_reload;
  2779. }
  2780. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  2781. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  2782. {
  2783. kvm_x86_ops->vcpu_free(vcpu);
  2784. }
  2785. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  2786. unsigned int id)
  2787. {
  2788. return kvm_x86_ops->vcpu_create(kvm, id);
  2789. }
  2790. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  2791. {
  2792. int r;
  2793. /* We do fxsave: this must be aligned. */
  2794. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  2795. vcpu_load(vcpu);
  2796. r = kvm_arch_vcpu_reset(vcpu);
  2797. if (r == 0)
  2798. r = kvm_mmu_setup(vcpu);
  2799. vcpu_put(vcpu);
  2800. if (r < 0)
  2801. goto free_vcpu;
  2802. return 0;
  2803. free_vcpu:
  2804. kvm_x86_ops->vcpu_free(vcpu);
  2805. return r;
  2806. }
  2807. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  2808. {
  2809. vcpu_load(vcpu);
  2810. kvm_mmu_unload(vcpu);
  2811. vcpu_put(vcpu);
  2812. kvm_x86_ops->vcpu_free(vcpu);
  2813. }
  2814. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  2815. {
  2816. return kvm_x86_ops->vcpu_reset(vcpu);
  2817. }
  2818. void kvm_arch_hardware_enable(void *garbage)
  2819. {
  2820. kvm_x86_ops->hardware_enable(garbage);
  2821. }
  2822. void kvm_arch_hardware_disable(void *garbage)
  2823. {
  2824. kvm_x86_ops->hardware_disable(garbage);
  2825. }
  2826. int kvm_arch_hardware_setup(void)
  2827. {
  2828. return kvm_x86_ops->hardware_setup();
  2829. }
  2830. void kvm_arch_hardware_unsetup(void)
  2831. {
  2832. kvm_x86_ops->hardware_unsetup();
  2833. }
  2834. void kvm_arch_check_processor_compat(void *rtn)
  2835. {
  2836. kvm_x86_ops->check_processor_compatibility(rtn);
  2837. }
  2838. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  2839. {
  2840. struct page *page;
  2841. struct kvm *kvm;
  2842. int r;
  2843. BUG_ON(vcpu->kvm == NULL);
  2844. kvm = vcpu->kvm;
  2845. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  2846. if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
  2847. vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
  2848. else
  2849. vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
  2850. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  2851. if (!page) {
  2852. r = -ENOMEM;
  2853. goto fail;
  2854. }
  2855. vcpu->arch.pio_data = page_address(page);
  2856. r = kvm_mmu_create(vcpu);
  2857. if (r < 0)
  2858. goto fail_free_pio_data;
  2859. if (irqchip_in_kernel(kvm)) {
  2860. r = kvm_create_lapic(vcpu);
  2861. if (r < 0)
  2862. goto fail_mmu_destroy;
  2863. }
  2864. return 0;
  2865. fail_mmu_destroy:
  2866. kvm_mmu_destroy(vcpu);
  2867. fail_free_pio_data:
  2868. free_page((unsigned long)vcpu->arch.pio_data);
  2869. fail:
  2870. return r;
  2871. }
  2872. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  2873. {
  2874. kvm_free_lapic(vcpu);
  2875. kvm_mmu_destroy(vcpu);
  2876. free_page((unsigned long)vcpu->arch.pio_data);
  2877. }
  2878. struct kvm *kvm_arch_create_vm(void)
  2879. {
  2880. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  2881. if (!kvm)
  2882. return ERR_PTR(-ENOMEM);
  2883. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  2884. return kvm;
  2885. }
  2886. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  2887. {
  2888. vcpu_load(vcpu);
  2889. kvm_mmu_unload(vcpu);
  2890. vcpu_put(vcpu);
  2891. }
  2892. static void kvm_free_vcpus(struct kvm *kvm)
  2893. {
  2894. unsigned int i;
  2895. /*
  2896. * Unpin any mmu pages first.
  2897. */
  2898. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  2899. if (kvm->vcpus[i])
  2900. kvm_unload_vcpu_mmu(kvm->vcpus[i]);
  2901. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2902. if (kvm->vcpus[i]) {
  2903. kvm_arch_vcpu_free(kvm->vcpus[i]);
  2904. kvm->vcpus[i] = NULL;
  2905. }
  2906. }
  2907. }
  2908. void kvm_arch_destroy_vm(struct kvm *kvm)
  2909. {
  2910. kvm_free_pit(kvm);
  2911. kfree(kvm->arch.vpic);
  2912. kfree(kvm->arch.vioapic);
  2913. kvm_free_vcpus(kvm);
  2914. kvm_free_physmem(kvm);
  2915. kfree(kvm);
  2916. }
  2917. int kvm_arch_set_memory_region(struct kvm *kvm,
  2918. struct kvm_userspace_memory_region *mem,
  2919. struct kvm_memory_slot old,
  2920. int user_alloc)
  2921. {
  2922. int npages = mem->memory_size >> PAGE_SHIFT;
  2923. struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
  2924. /*To keep backward compatibility with older userspace,
  2925. *x86 needs to hanlde !user_alloc case.
  2926. */
  2927. if (!user_alloc) {
  2928. if (npages && !old.rmap) {
  2929. down_write(&current->mm->mmap_sem);
  2930. memslot->userspace_addr = do_mmap(NULL, 0,
  2931. npages * PAGE_SIZE,
  2932. PROT_READ | PROT_WRITE,
  2933. MAP_SHARED | MAP_ANONYMOUS,
  2934. 0);
  2935. up_write(&current->mm->mmap_sem);
  2936. if (IS_ERR((void *)memslot->userspace_addr))
  2937. return PTR_ERR((void *)memslot->userspace_addr);
  2938. } else {
  2939. if (!old.user_alloc && old.rmap) {
  2940. int ret;
  2941. down_write(&current->mm->mmap_sem);
  2942. ret = do_munmap(current->mm, old.userspace_addr,
  2943. old.npages * PAGE_SIZE);
  2944. up_write(&current->mm->mmap_sem);
  2945. if (ret < 0)
  2946. printk(KERN_WARNING
  2947. "kvm_vm_ioctl_set_memory_region: "
  2948. "failed to munmap memory\n");
  2949. }
  2950. }
  2951. }
  2952. if (!kvm->arch.n_requested_mmu_pages) {
  2953. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  2954. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  2955. }
  2956. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  2957. kvm_flush_remote_tlbs(kvm);
  2958. return 0;
  2959. }
  2960. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  2961. {
  2962. return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
  2963. || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
  2964. }
  2965. static void vcpu_kick_intr(void *info)
  2966. {
  2967. #ifdef DEBUG
  2968. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
  2969. printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
  2970. #endif
  2971. }
  2972. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  2973. {
  2974. int ipi_pcpu = vcpu->cpu;
  2975. if (waitqueue_active(&vcpu->wq)) {
  2976. wake_up_interruptible(&vcpu->wq);
  2977. ++vcpu->stat.halt_wakeup;
  2978. }
  2979. if (vcpu->guest_mode)
  2980. smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
  2981. }