kvm_main.c 80 KB

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