kvm_main.c 78 KB

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