kvm_main.c 78 KB

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