kvm_main.c 78 KB

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