kvm_main.c 80 KB

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