kvm_main.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649
  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. vcpu->guest_mode = 0;
  1771. local_irq_enable();
  1772. ++vcpu->stat.exits;
  1773. /*
  1774. * We must have an instruction between local_irq_enable() and
  1775. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  1776. * the interrupt shadow. The stat.exits increment will do nicely.
  1777. * But we need to prevent reordering, hence this barrier():
  1778. */
  1779. barrier();
  1780. kvm_guest_exit();
  1781. preempt_enable();
  1782. /*
  1783. * Profile KVM exit RIPs:
  1784. */
  1785. if (unlikely(prof_on == KVM_PROFILING)) {
  1786. kvm_x86_ops->cache_regs(vcpu);
  1787. profile_hit(KVM_PROFILING, (void *)vcpu->rip);
  1788. }
  1789. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  1790. if (r > 0) {
  1791. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  1792. r = -EINTR;
  1793. kvm_run->exit_reason = KVM_EXIT_INTR;
  1794. ++vcpu->stat.request_irq_exits;
  1795. goto out;
  1796. }
  1797. if (!need_resched()) {
  1798. ++vcpu->stat.light_exits;
  1799. goto again;
  1800. }
  1801. }
  1802. out:
  1803. if (r > 0) {
  1804. kvm_resched(vcpu);
  1805. goto preempted;
  1806. }
  1807. post_kvm_run_save(vcpu, kvm_run);
  1808. return r;
  1809. }
  1810. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1811. {
  1812. int r;
  1813. sigset_t sigsaved;
  1814. vcpu_load(vcpu);
  1815. if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
  1816. kvm_vcpu_block(vcpu);
  1817. vcpu_put(vcpu);
  1818. return -EAGAIN;
  1819. }
  1820. if (vcpu->sigset_active)
  1821. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1822. /* re-sync apic's tpr */
  1823. if (!irqchip_in_kernel(vcpu->kvm))
  1824. set_cr8(vcpu, kvm_run->cr8);
  1825. if (vcpu->pio.cur_count) {
  1826. r = complete_pio(vcpu);
  1827. if (r)
  1828. goto out;
  1829. }
  1830. if (vcpu->mmio_needed) {
  1831. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1832. vcpu->mmio_read_completed = 1;
  1833. vcpu->mmio_needed = 0;
  1834. r = emulate_instruction(vcpu, kvm_run,
  1835. vcpu->mmio_fault_cr2, 0);
  1836. if (r == EMULATE_DO_MMIO) {
  1837. /*
  1838. * Read-modify-write. Back to userspace.
  1839. */
  1840. r = 0;
  1841. goto out;
  1842. }
  1843. }
  1844. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1845. kvm_x86_ops->cache_regs(vcpu);
  1846. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1847. kvm_x86_ops->decache_regs(vcpu);
  1848. }
  1849. r = __vcpu_run(vcpu, kvm_run);
  1850. out:
  1851. if (vcpu->sigset_active)
  1852. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1853. vcpu_put(vcpu);
  1854. return r;
  1855. }
  1856. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1857. struct kvm_regs *regs)
  1858. {
  1859. vcpu_load(vcpu);
  1860. kvm_x86_ops->cache_regs(vcpu);
  1861. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1862. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1863. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1864. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1865. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1866. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1867. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1868. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1869. #ifdef CONFIG_X86_64
  1870. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1871. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1872. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1873. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1874. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1875. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1876. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1877. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1878. #endif
  1879. regs->rip = vcpu->rip;
  1880. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  1881. /*
  1882. * Don't leak debug flags in case they were set for guest debugging
  1883. */
  1884. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1885. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1886. vcpu_put(vcpu);
  1887. return 0;
  1888. }
  1889. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1890. struct kvm_regs *regs)
  1891. {
  1892. vcpu_load(vcpu);
  1893. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1894. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1895. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1896. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1897. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1898. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1899. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1900. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1901. #ifdef CONFIG_X86_64
  1902. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1903. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1904. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1905. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1906. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1907. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1908. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1909. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1910. #endif
  1911. vcpu->rip = regs->rip;
  1912. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  1913. kvm_x86_ops->decache_regs(vcpu);
  1914. vcpu_put(vcpu);
  1915. return 0;
  1916. }
  1917. static void get_segment(struct kvm_vcpu *vcpu,
  1918. struct kvm_segment *var, int seg)
  1919. {
  1920. return kvm_x86_ops->get_segment(vcpu, var, seg);
  1921. }
  1922. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1923. struct kvm_sregs *sregs)
  1924. {
  1925. struct descriptor_table dt;
  1926. int pending_vec;
  1927. vcpu_load(vcpu);
  1928. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1929. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1930. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1931. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1932. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1933. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1934. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1935. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1936. kvm_x86_ops->get_idt(vcpu, &dt);
  1937. sregs->idt.limit = dt.limit;
  1938. sregs->idt.base = dt.base;
  1939. kvm_x86_ops->get_gdt(vcpu, &dt);
  1940. sregs->gdt.limit = dt.limit;
  1941. sregs->gdt.base = dt.base;
  1942. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  1943. sregs->cr0 = vcpu->cr0;
  1944. sregs->cr2 = vcpu->cr2;
  1945. sregs->cr3 = vcpu->cr3;
  1946. sregs->cr4 = vcpu->cr4;
  1947. sregs->cr8 = get_cr8(vcpu);
  1948. sregs->efer = vcpu->shadow_efer;
  1949. sregs->apic_base = kvm_get_apic_base(vcpu);
  1950. if (irqchip_in_kernel(vcpu->kvm)) {
  1951. memset(sregs->interrupt_bitmap, 0,
  1952. sizeof sregs->interrupt_bitmap);
  1953. pending_vec = kvm_x86_ops->get_irq(vcpu);
  1954. if (pending_vec >= 0)
  1955. set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
  1956. } else
  1957. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1958. sizeof sregs->interrupt_bitmap);
  1959. vcpu_put(vcpu);
  1960. return 0;
  1961. }
  1962. static void set_segment(struct kvm_vcpu *vcpu,
  1963. struct kvm_segment *var, int seg)
  1964. {
  1965. return kvm_x86_ops->set_segment(vcpu, var, seg);
  1966. }
  1967. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1968. struct kvm_sregs *sregs)
  1969. {
  1970. int mmu_reset_needed = 0;
  1971. int i, pending_vec, max_bits;
  1972. struct descriptor_table dt;
  1973. vcpu_load(vcpu);
  1974. dt.limit = sregs->idt.limit;
  1975. dt.base = sregs->idt.base;
  1976. kvm_x86_ops->set_idt(vcpu, &dt);
  1977. dt.limit = sregs->gdt.limit;
  1978. dt.base = sregs->gdt.base;
  1979. kvm_x86_ops->set_gdt(vcpu, &dt);
  1980. vcpu->cr2 = sregs->cr2;
  1981. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1982. vcpu->cr3 = sregs->cr3;
  1983. set_cr8(vcpu, sregs->cr8);
  1984. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1985. #ifdef CONFIG_X86_64
  1986. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  1987. #endif
  1988. kvm_set_apic_base(vcpu, sregs->apic_base);
  1989. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  1990. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1991. vcpu->cr0 = sregs->cr0;
  1992. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  1993. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1994. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  1995. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1996. load_pdptrs(vcpu, vcpu->cr3);
  1997. if (mmu_reset_needed)
  1998. kvm_mmu_reset_context(vcpu);
  1999. if (!irqchip_in_kernel(vcpu->kvm)) {
  2000. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  2001. sizeof vcpu->irq_pending);
  2002. vcpu->irq_summary = 0;
  2003. for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
  2004. if (vcpu->irq_pending[i])
  2005. __set_bit(i, &vcpu->irq_summary);
  2006. } else {
  2007. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  2008. pending_vec = find_first_bit(
  2009. (const unsigned long *)sregs->interrupt_bitmap,
  2010. max_bits);
  2011. /* Only pending external irq is handled here */
  2012. if (pending_vec < max_bits) {
  2013. kvm_x86_ops->set_irq(vcpu, pending_vec);
  2014. printk("Set back pending irq %d\n", pending_vec);
  2015. }
  2016. }
  2017. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2018. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2019. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2020. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2021. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2022. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2023. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2024. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2025. vcpu_put(vcpu);
  2026. return 0;
  2027. }
  2028. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2029. {
  2030. struct kvm_segment cs;
  2031. get_segment(vcpu, &cs, VCPU_SREG_CS);
  2032. *db = cs.db;
  2033. *l = cs.l;
  2034. }
  2035. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2036. /*
  2037. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  2038. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  2039. *
  2040. * This list is modified at module load time to reflect the
  2041. * capabilities of the host cpu.
  2042. */
  2043. static u32 msrs_to_save[] = {
  2044. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  2045. MSR_K6_STAR,
  2046. #ifdef CONFIG_X86_64
  2047. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  2048. #endif
  2049. MSR_IA32_TIME_STAMP_COUNTER,
  2050. };
  2051. static unsigned num_msrs_to_save;
  2052. static u32 emulated_msrs[] = {
  2053. MSR_IA32_MISC_ENABLE,
  2054. };
  2055. static __init void kvm_init_msr_list(void)
  2056. {
  2057. u32 dummy[2];
  2058. unsigned i, j;
  2059. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  2060. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  2061. continue;
  2062. if (j < i)
  2063. msrs_to_save[j] = msrs_to_save[i];
  2064. j++;
  2065. }
  2066. num_msrs_to_save = j;
  2067. }
  2068. /*
  2069. * Adapt set_msr() to msr_io()'s calling convention
  2070. */
  2071. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  2072. {
  2073. return kvm_set_msr(vcpu, index, *data);
  2074. }
  2075. /*
  2076. * Read or write a bunch of msrs. All parameters are kernel addresses.
  2077. *
  2078. * @return number of msrs set successfully.
  2079. */
  2080. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  2081. struct kvm_msr_entry *entries,
  2082. int (*do_msr)(struct kvm_vcpu *vcpu,
  2083. unsigned index, u64 *data))
  2084. {
  2085. int i;
  2086. vcpu_load(vcpu);
  2087. for (i = 0; i < msrs->nmsrs; ++i)
  2088. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  2089. break;
  2090. vcpu_put(vcpu);
  2091. return i;
  2092. }
  2093. /*
  2094. * Read or write a bunch of msrs. Parameters are user addresses.
  2095. *
  2096. * @return number of msrs set successfully.
  2097. */
  2098. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  2099. int (*do_msr)(struct kvm_vcpu *vcpu,
  2100. unsigned index, u64 *data),
  2101. int writeback)
  2102. {
  2103. struct kvm_msrs msrs;
  2104. struct kvm_msr_entry *entries;
  2105. int r, n;
  2106. unsigned size;
  2107. r = -EFAULT;
  2108. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  2109. goto out;
  2110. r = -E2BIG;
  2111. if (msrs.nmsrs >= MAX_IO_MSRS)
  2112. goto out;
  2113. r = -ENOMEM;
  2114. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  2115. entries = vmalloc(size);
  2116. if (!entries)
  2117. goto out;
  2118. r = -EFAULT;
  2119. if (copy_from_user(entries, user_msrs->entries, size))
  2120. goto out_free;
  2121. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  2122. if (r < 0)
  2123. goto out_free;
  2124. r = -EFAULT;
  2125. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  2126. goto out_free;
  2127. r = n;
  2128. out_free:
  2129. vfree(entries);
  2130. out:
  2131. return r;
  2132. }
  2133. /*
  2134. * Translate a guest virtual address to a guest physical address.
  2135. */
  2136. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  2137. struct kvm_translation *tr)
  2138. {
  2139. unsigned long vaddr = tr->linear_address;
  2140. gpa_t gpa;
  2141. vcpu_load(vcpu);
  2142. mutex_lock(&vcpu->kvm->lock);
  2143. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  2144. tr->physical_address = gpa;
  2145. tr->valid = gpa != UNMAPPED_GVA;
  2146. tr->writeable = 1;
  2147. tr->usermode = 0;
  2148. mutex_unlock(&vcpu->kvm->lock);
  2149. vcpu_put(vcpu);
  2150. return 0;
  2151. }
  2152. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  2153. struct kvm_interrupt *irq)
  2154. {
  2155. if (irq->irq < 0 || irq->irq >= 256)
  2156. return -EINVAL;
  2157. if (irqchip_in_kernel(vcpu->kvm))
  2158. return -ENXIO;
  2159. vcpu_load(vcpu);
  2160. set_bit(irq->irq, vcpu->irq_pending);
  2161. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  2162. vcpu_put(vcpu);
  2163. return 0;
  2164. }
  2165. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  2166. struct kvm_debug_guest *dbg)
  2167. {
  2168. int r;
  2169. vcpu_load(vcpu);
  2170. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  2171. vcpu_put(vcpu);
  2172. return r;
  2173. }
  2174. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  2175. unsigned long address,
  2176. int *type)
  2177. {
  2178. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  2179. unsigned long pgoff;
  2180. struct page *page;
  2181. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2182. if (pgoff == 0)
  2183. page = virt_to_page(vcpu->run);
  2184. else if (pgoff == KVM_PIO_PAGE_OFFSET)
  2185. page = virt_to_page(vcpu->pio_data);
  2186. else
  2187. return NOPAGE_SIGBUS;
  2188. get_page(page);
  2189. if (type != NULL)
  2190. *type = VM_FAULT_MINOR;
  2191. return page;
  2192. }
  2193. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  2194. .nopage = kvm_vcpu_nopage,
  2195. };
  2196. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  2197. {
  2198. vma->vm_ops = &kvm_vcpu_vm_ops;
  2199. return 0;
  2200. }
  2201. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  2202. {
  2203. struct kvm_vcpu *vcpu = filp->private_data;
  2204. fput(vcpu->kvm->filp);
  2205. return 0;
  2206. }
  2207. static struct file_operations kvm_vcpu_fops = {
  2208. .release = kvm_vcpu_release,
  2209. .unlocked_ioctl = kvm_vcpu_ioctl,
  2210. .compat_ioctl = kvm_vcpu_ioctl,
  2211. .mmap = kvm_vcpu_mmap,
  2212. };
  2213. /*
  2214. * Allocates an inode for the vcpu.
  2215. */
  2216. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  2217. {
  2218. int fd, r;
  2219. struct inode *inode;
  2220. struct file *file;
  2221. r = anon_inode_getfd(&fd, &inode, &file,
  2222. "kvm-vcpu", &kvm_vcpu_fops, vcpu);
  2223. if (r)
  2224. return r;
  2225. atomic_inc(&vcpu->kvm->filp->f_count);
  2226. return fd;
  2227. }
  2228. /*
  2229. * Creates some virtual cpus. Good luck creating more than one.
  2230. */
  2231. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  2232. {
  2233. int r;
  2234. struct kvm_vcpu *vcpu;
  2235. if (!valid_vcpu(n))
  2236. return -EINVAL;
  2237. vcpu = kvm_x86_ops->vcpu_create(kvm, n);
  2238. if (IS_ERR(vcpu))
  2239. return PTR_ERR(vcpu);
  2240. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  2241. /* We do fxsave: this must be aligned. */
  2242. BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
  2243. vcpu_load(vcpu);
  2244. r = kvm_mmu_setup(vcpu);
  2245. vcpu_put(vcpu);
  2246. if (r < 0)
  2247. goto free_vcpu;
  2248. mutex_lock(&kvm->lock);
  2249. if (kvm->vcpus[n]) {
  2250. r = -EEXIST;
  2251. mutex_unlock(&kvm->lock);
  2252. goto mmu_unload;
  2253. }
  2254. kvm->vcpus[n] = vcpu;
  2255. mutex_unlock(&kvm->lock);
  2256. /* Now it's all set up, let userspace reach it */
  2257. r = create_vcpu_fd(vcpu);
  2258. if (r < 0)
  2259. goto unlink;
  2260. return r;
  2261. unlink:
  2262. mutex_lock(&kvm->lock);
  2263. kvm->vcpus[n] = NULL;
  2264. mutex_unlock(&kvm->lock);
  2265. mmu_unload:
  2266. vcpu_load(vcpu);
  2267. kvm_mmu_unload(vcpu);
  2268. vcpu_put(vcpu);
  2269. free_vcpu:
  2270. kvm_x86_ops->vcpu_free(vcpu);
  2271. return r;
  2272. }
  2273. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  2274. {
  2275. u64 efer;
  2276. int i;
  2277. struct kvm_cpuid_entry *e, *entry;
  2278. rdmsrl(MSR_EFER, efer);
  2279. entry = NULL;
  2280. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  2281. e = &vcpu->cpuid_entries[i];
  2282. if (e->function == 0x80000001) {
  2283. entry = e;
  2284. break;
  2285. }
  2286. }
  2287. if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
  2288. entry->edx &= ~(1 << 20);
  2289. printk(KERN_INFO "kvm: guest NX capability removed\n");
  2290. }
  2291. }
  2292. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  2293. struct kvm_cpuid *cpuid,
  2294. struct kvm_cpuid_entry __user *entries)
  2295. {
  2296. int r;
  2297. r = -E2BIG;
  2298. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  2299. goto out;
  2300. r = -EFAULT;
  2301. if (copy_from_user(&vcpu->cpuid_entries, entries,
  2302. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  2303. goto out;
  2304. vcpu->cpuid_nent = cpuid->nent;
  2305. cpuid_fix_nx_cap(vcpu);
  2306. return 0;
  2307. out:
  2308. return r;
  2309. }
  2310. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2311. {
  2312. if (sigset) {
  2313. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2314. vcpu->sigset_active = 1;
  2315. vcpu->sigset = *sigset;
  2316. } else
  2317. vcpu->sigset_active = 0;
  2318. return 0;
  2319. }
  2320. /*
  2321. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2322. * we have asm/x86/processor.h
  2323. */
  2324. struct fxsave {
  2325. u16 cwd;
  2326. u16 swd;
  2327. u16 twd;
  2328. u16 fop;
  2329. u64 rip;
  2330. u64 rdp;
  2331. u32 mxcsr;
  2332. u32 mxcsr_mask;
  2333. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2334. #ifdef CONFIG_X86_64
  2335. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2336. #else
  2337. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2338. #endif
  2339. };
  2340. static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2341. {
  2342. struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
  2343. vcpu_load(vcpu);
  2344. memcpy(fpu->fpr, fxsave->st_space, 128);
  2345. fpu->fcw = fxsave->cwd;
  2346. fpu->fsw = fxsave->swd;
  2347. fpu->ftwx = fxsave->twd;
  2348. fpu->last_opcode = fxsave->fop;
  2349. fpu->last_ip = fxsave->rip;
  2350. fpu->last_dp = fxsave->rdp;
  2351. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2352. vcpu_put(vcpu);
  2353. return 0;
  2354. }
  2355. static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2356. {
  2357. struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
  2358. vcpu_load(vcpu);
  2359. memcpy(fxsave->st_space, fpu->fpr, 128);
  2360. fxsave->cwd = fpu->fcw;
  2361. fxsave->swd = fpu->fsw;
  2362. fxsave->twd = fpu->ftwx;
  2363. fxsave->fop = fpu->last_opcode;
  2364. fxsave->rip = fpu->last_ip;
  2365. fxsave->rdp = fpu->last_dp;
  2366. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2367. vcpu_put(vcpu);
  2368. return 0;
  2369. }
  2370. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  2371. struct kvm_lapic_state *s)
  2372. {
  2373. vcpu_load(vcpu);
  2374. memcpy(s->regs, vcpu->apic->regs, sizeof *s);
  2375. vcpu_put(vcpu);
  2376. return 0;
  2377. }
  2378. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  2379. struct kvm_lapic_state *s)
  2380. {
  2381. vcpu_load(vcpu);
  2382. memcpy(vcpu->apic->regs, s->regs, sizeof *s);
  2383. kvm_apic_post_state_restore(vcpu);
  2384. vcpu_put(vcpu);
  2385. return 0;
  2386. }
  2387. static long kvm_vcpu_ioctl(struct file *filp,
  2388. unsigned int ioctl, unsigned long arg)
  2389. {
  2390. struct kvm_vcpu *vcpu = filp->private_data;
  2391. void __user *argp = (void __user *)arg;
  2392. int r = -EINVAL;
  2393. switch (ioctl) {
  2394. case KVM_RUN:
  2395. r = -EINVAL;
  2396. if (arg)
  2397. goto out;
  2398. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  2399. break;
  2400. case KVM_GET_REGS: {
  2401. struct kvm_regs kvm_regs;
  2402. memset(&kvm_regs, 0, sizeof kvm_regs);
  2403. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  2404. if (r)
  2405. goto out;
  2406. r = -EFAULT;
  2407. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  2408. goto out;
  2409. r = 0;
  2410. break;
  2411. }
  2412. case KVM_SET_REGS: {
  2413. struct kvm_regs kvm_regs;
  2414. r = -EFAULT;
  2415. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  2416. goto out;
  2417. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  2418. if (r)
  2419. goto out;
  2420. r = 0;
  2421. break;
  2422. }
  2423. case KVM_GET_SREGS: {
  2424. struct kvm_sregs kvm_sregs;
  2425. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  2426. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  2427. if (r)
  2428. goto out;
  2429. r = -EFAULT;
  2430. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  2431. goto out;
  2432. r = 0;
  2433. break;
  2434. }
  2435. case KVM_SET_SREGS: {
  2436. struct kvm_sregs kvm_sregs;
  2437. r = -EFAULT;
  2438. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  2439. goto out;
  2440. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  2441. if (r)
  2442. goto out;
  2443. r = 0;
  2444. break;
  2445. }
  2446. case KVM_TRANSLATE: {
  2447. struct kvm_translation tr;
  2448. r = -EFAULT;
  2449. if (copy_from_user(&tr, argp, sizeof tr))
  2450. goto out;
  2451. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  2452. if (r)
  2453. goto out;
  2454. r = -EFAULT;
  2455. if (copy_to_user(argp, &tr, sizeof tr))
  2456. goto out;
  2457. r = 0;
  2458. break;
  2459. }
  2460. case KVM_INTERRUPT: {
  2461. struct kvm_interrupt irq;
  2462. r = -EFAULT;
  2463. if (copy_from_user(&irq, argp, sizeof irq))
  2464. goto out;
  2465. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2466. if (r)
  2467. goto out;
  2468. r = 0;
  2469. break;
  2470. }
  2471. case KVM_DEBUG_GUEST: {
  2472. struct kvm_debug_guest dbg;
  2473. r = -EFAULT;
  2474. if (copy_from_user(&dbg, argp, sizeof dbg))
  2475. goto out;
  2476. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  2477. if (r)
  2478. goto out;
  2479. r = 0;
  2480. break;
  2481. }
  2482. case KVM_GET_MSRS:
  2483. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2484. break;
  2485. case KVM_SET_MSRS:
  2486. r = msr_io(vcpu, argp, do_set_msr, 0);
  2487. break;
  2488. case KVM_SET_CPUID: {
  2489. struct kvm_cpuid __user *cpuid_arg = argp;
  2490. struct kvm_cpuid cpuid;
  2491. r = -EFAULT;
  2492. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2493. goto out;
  2494. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2495. if (r)
  2496. goto out;
  2497. break;
  2498. }
  2499. case KVM_SET_SIGNAL_MASK: {
  2500. struct kvm_signal_mask __user *sigmask_arg = argp;
  2501. struct kvm_signal_mask kvm_sigmask;
  2502. sigset_t sigset, *p;
  2503. p = NULL;
  2504. if (argp) {
  2505. r = -EFAULT;
  2506. if (copy_from_user(&kvm_sigmask, argp,
  2507. sizeof kvm_sigmask))
  2508. goto out;
  2509. r = -EINVAL;
  2510. if (kvm_sigmask.len != sizeof sigset)
  2511. goto out;
  2512. r = -EFAULT;
  2513. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2514. sizeof sigset))
  2515. goto out;
  2516. p = &sigset;
  2517. }
  2518. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2519. break;
  2520. }
  2521. case KVM_GET_FPU: {
  2522. struct kvm_fpu fpu;
  2523. memset(&fpu, 0, sizeof fpu);
  2524. r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
  2525. if (r)
  2526. goto out;
  2527. r = -EFAULT;
  2528. if (copy_to_user(argp, &fpu, sizeof fpu))
  2529. goto out;
  2530. r = 0;
  2531. break;
  2532. }
  2533. case KVM_SET_FPU: {
  2534. struct kvm_fpu fpu;
  2535. r = -EFAULT;
  2536. if (copy_from_user(&fpu, argp, sizeof fpu))
  2537. goto out;
  2538. r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
  2539. if (r)
  2540. goto out;
  2541. r = 0;
  2542. break;
  2543. }
  2544. case KVM_GET_LAPIC: {
  2545. struct kvm_lapic_state lapic;
  2546. memset(&lapic, 0, sizeof lapic);
  2547. r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
  2548. if (r)
  2549. goto out;
  2550. r = -EFAULT;
  2551. if (copy_to_user(argp, &lapic, sizeof lapic))
  2552. goto out;
  2553. r = 0;
  2554. break;
  2555. }
  2556. case KVM_SET_LAPIC: {
  2557. struct kvm_lapic_state lapic;
  2558. r = -EFAULT;
  2559. if (copy_from_user(&lapic, argp, sizeof lapic))
  2560. goto out;
  2561. r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
  2562. if (r)
  2563. goto out;
  2564. r = 0;
  2565. break;
  2566. }
  2567. default:
  2568. ;
  2569. }
  2570. out:
  2571. return r;
  2572. }
  2573. static long kvm_vm_ioctl(struct file *filp,
  2574. unsigned int ioctl, unsigned long arg)
  2575. {
  2576. struct kvm *kvm = filp->private_data;
  2577. void __user *argp = (void __user *)arg;
  2578. int r = -EINVAL;
  2579. switch (ioctl) {
  2580. case KVM_CREATE_VCPU:
  2581. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2582. if (r < 0)
  2583. goto out;
  2584. break;
  2585. case KVM_SET_MEMORY_REGION: {
  2586. struct kvm_memory_region kvm_mem;
  2587. r = -EFAULT;
  2588. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2589. goto out;
  2590. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  2591. if (r)
  2592. goto out;
  2593. break;
  2594. }
  2595. case KVM_GET_DIRTY_LOG: {
  2596. struct kvm_dirty_log log;
  2597. r = -EFAULT;
  2598. if (copy_from_user(&log, argp, sizeof log))
  2599. goto out;
  2600. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2601. if (r)
  2602. goto out;
  2603. break;
  2604. }
  2605. case KVM_SET_MEMORY_ALIAS: {
  2606. struct kvm_memory_alias alias;
  2607. r = -EFAULT;
  2608. if (copy_from_user(&alias, argp, sizeof alias))
  2609. goto out;
  2610. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  2611. if (r)
  2612. goto out;
  2613. break;
  2614. }
  2615. case KVM_CREATE_IRQCHIP:
  2616. r = -ENOMEM;
  2617. kvm->vpic = kvm_create_pic(kvm);
  2618. if (kvm->vpic) {
  2619. r = kvm_ioapic_init(kvm);
  2620. if (r) {
  2621. kfree(kvm->vpic);
  2622. kvm->vpic = NULL;
  2623. goto out;
  2624. }
  2625. }
  2626. else
  2627. goto out;
  2628. break;
  2629. case KVM_IRQ_LINE: {
  2630. struct kvm_irq_level irq_event;
  2631. r = -EFAULT;
  2632. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2633. goto out;
  2634. if (irqchip_in_kernel(kvm)) {
  2635. mutex_lock(&kvm->lock);
  2636. if (irq_event.irq < 16)
  2637. kvm_pic_set_irq(pic_irqchip(kvm),
  2638. irq_event.irq,
  2639. irq_event.level);
  2640. kvm_ioapic_set_irq(kvm->vioapic,
  2641. irq_event.irq,
  2642. irq_event.level);
  2643. mutex_unlock(&kvm->lock);
  2644. r = 0;
  2645. }
  2646. break;
  2647. }
  2648. case KVM_GET_IRQCHIP: {
  2649. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2650. struct kvm_irqchip chip;
  2651. r = -EFAULT;
  2652. if (copy_from_user(&chip, argp, sizeof chip))
  2653. goto out;
  2654. r = -ENXIO;
  2655. if (!irqchip_in_kernel(kvm))
  2656. goto out;
  2657. r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
  2658. if (r)
  2659. goto out;
  2660. r = -EFAULT;
  2661. if (copy_to_user(argp, &chip, sizeof chip))
  2662. goto out;
  2663. r = 0;
  2664. break;
  2665. }
  2666. case KVM_SET_IRQCHIP: {
  2667. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2668. struct kvm_irqchip chip;
  2669. r = -EFAULT;
  2670. if (copy_from_user(&chip, argp, sizeof chip))
  2671. goto out;
  2672. r = -ENXIO;
  2673. if (!irqchip_in_kernel(kvm))
  2674. goto out;
  2675. r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
  2676. if (r)
  2677. goto out;
  2678. r = 0;
  2679. break;
  2680. }
  2681. default:
  2682. ;
  2683. }
  2684. out:
  2685. return r;
  2686. }
  2687. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  2688. unsigned long address,
  2689. int *type)
  2690. {
  2691. struct kvm *kvm = vma->vm_file->private_data;
  2692. unsigned long pgoff;
  2693. struct page *page;
  2694. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2695. page = gfn_to_page(kvm, pgoff);
  2696. if (!page)
  2697. return NOPAGE_SIGBUS;
  2698. get_page(page);
  2699. if (type != NULL)
  2700. *type = VM_FAULT_MINOR;
  2701. return page;
  2702. }
  2703. static struct vm_operations_struct kvm_vm_vm_ops = {
  2704. .nopage = kvm_vm_nopage,
  2705. };
  2706. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2707. {
  2708. vma->vm_ops = &kvm_vm_vm_ops;
  2709. return 0;
  2710. }
  2711. static struct file_operations kvm_vm_fops = {
  2712. .release = kvm_vm_release,
  2713. .unlocked_ioctl = kvm_vm_ioctl,
  2714. .compat_ioctl = kvm_vm_ioctl,
  2715. .mmap = kvm_vm_mmap,
  2716. };
  2717. static int kvm_dev_ioctl_create_vm(void)
  2718. {
  2719. int fd, r;
  2720. struct inode *inode;
  2721. struct file *file;
  2722. struct kvm *kvm;
  2723. kvm = kvm_create_vm();
  2724. if (IS_ERR(kvm))
  2725. return PTR_ERR(kvm);
  2726. r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
  2727. if (r) {
  2728. kvm_destroy_vm(kvm);
  2729. return r;
  2730. }
  2731. kvm->filp = file;
  2732. return fd;
  2733. }
  2734. static long kvm_dev_ioctl(struct file *filp,
  2735. unsigned int ioctl, unsigned long arg)
  2736. {
  2737. void __user *argp = (void __user *)arg;
  2738. long r = -EINVAL;
  2739. switch (ioctl) {
  2740. case KVM_GET_API_VERSION:
  2741. r = -EINVAL;
  2742. if (arg)
  2743. goto out;
  2744. r = KVM_API_VERSION;
  2745. break;
  2746. case KVM_CREATE_VM:
  2747. r = -EINVAL;
  2748. if (arg)
  2749. goto out;
  2750. r = kvm_dev_ioctl_create_vm();
  2751. break;
  2752. case KVM_GET_MSR_INDEX_LIST: {
  2753. struct kvm_msr_list __user *user_msr_list = argp;
  2754. struct kvm_msr_list msr_list;
  2755. unsigned n;
  2756. r = -EFAULT;
  2757. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2758. goto out;
  2759. n = msr_list.nmsrs;
  2760. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2761. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2762. goto out;
  2763. r = -E2BIG;
  2764. if (n < num_msrs_to_save)
  2765. goto out;
  2766. r = -EFAULT;
  2767. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2768. num_msrs_to_save * sizeof(u32)))
  2769. goto out;
  2770. if (copy_to_user(user_msr_list->indices
  2771. + num_msrs_to_save * sizeof(u32),
  2772. &emulated_msrs,
  2773. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2774. goto out;
  2775. r = 0;
  2776. break;
  2777. }
  2778. case KVM_CHECK_EXTENSION: {
  2779. int ext = (long)argp;
  2780. switch (ext) {
  2781. case KVM_CAP_IRQCHIP:
  2782. case KVM_CAP_HLT:
  2783. r = 1;
  2784. break;
  2785. default:
  2786. r = 0;
  2787. break;
  2788. }
  2789. break;
  2790. }
  2791. case KVM_GET_VCPU_MMAP_SIZE:
  2792. r = -EINVAL;
  2793. if (arg)
  2794. goto out;
  2795. r = 2 * PAGE_SIZE;
  2796. break;
  2797. default:
  2798. ;
  2799. }
  2800. out:
  2801. return r;
  2802. }
  2803. static struct file_operations kvm_chardev_ops = {
  2804. .unlocked_ioctl = kvm_dev_ioctl,
  2805. .compat_ioctl = kvm_dev_ioctl,
  2806. };
  2807. static struct miscdevice kvm_dev = {
  2808. KVM_MINOR,
  2809. "kvm",
  2810. &kvm_chardev_ops,
  2811. };
  2812. /*
  2813. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2814. * cached on it.
  2815. */
  2816. static void decache_vcpus_on_cpu(int cpu)
  2817. {
  2818. struct kvm *vm;
  2819. struct kvm_vcpu *vcpu;
  2820. int i;
  2821. spin_lock(&kvm_lock);
  2822. list_for_each_entry(vm, &vm_list, vm_list)
  2823. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2824. vcpu = vm->vcpus[i];
  2825. if (!vcpu)
  2826. continue;
  2827. /*
  2828. * If the vcpu is locked, then it is running on some
  2829. * other cpu and therefore it is not cached on the
  2830. * cpu in question.
  2831. *
  2832. * If it's not locked, check the last cpu it executed
  2833. * on.
  2834. */
  2835. if (mutex_trylock(&vcpu->mutex)) {
  2836. if (vcpu->cpu == cpu) {
  2837. kvm_x86_ops->vcpu_decache(vcpu);
  2838. vcpu->cpu = -1;
  2839. }
  2840. mutex_unlock(&vcpu->mutex);
  2841. }
  2842. }
  2843. spin_unlock(&kvm_lock);
  2844. }
  2845. static void hardware_enable(void *junk)
  2846. {
  2847. int cpu = raw_smp_processor_id();
  2848. if (cpu_isset(cpu, cpus_hardware_enabled))
  2849. return;
  2850. cpu_set(cpu, cpus_hardware_enabled);
  2851. kvm_x86_ops->hardware_enable(NULL);
  2852. }
  2853. static void hardware_disable(void *junk)
  2854. {
  2855. int cpu = raw_smp_processor_id();
  2856. if (!cpu_isset(cpu, cpus_hardware_enabled))
  2857. return;
  2858. cpu_clear(cpu, cpus_hardware_enabled);
  2859. decache_vcpus_on_cpu(cpu);
  2860. kvm_x86_ops->hardware_disable(NULL);
  2861. }
  2862. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2863. void *v)
  2864. {
  2865. int cpu = (long)v;
  2866. switch (val) {
  2867. case CPU_DYING:
  2868. case CPU_DYING_FROZEN:
  2869. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2870. cpu);
  2871. hardware_disable(NULL);
  2872. break;
  2873. case CPU_UP_CANCELED:
  2874. case CPU_UP_CANCELED_FROZEN:
  2875. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2876. cpu);
  2877. smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
  2878. break;
  2879. case CPU_ONLINE:
  2880. case CPU_ONLINE_FROZEN:
  2881. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2882. cpu);
  2883. smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
  2884. break;
  2885. }
  2886. return NOTIFY_OK;
  2887. }
  2888. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2889. void *v)
  2890. {
  2891. if (val == SYS_RESTART) {
  2892. /*
  2893. * Some (well, at least mine) BIOSes hang on reboot if
  2894. * in vmx root mode.
  2895. */
  2896. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2897. on_each_cpu(hardware_disable, NULL, 0, 1);
  2898. }
  2899. return NOTIFY_OK;
  2900. }
  2901. static struct notifier_block kvm_reboot_notifier = {
  2902. .notifier_call = kvm_reboot,
  2903. .priority = 0,
  2904. };
  2905. void kvm_io_bus_init(struct kvm_io_bus *bus)
  2906. {
  2907. memset(bus, 0, sizeof(*bus));
  2908. }
  2909. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2910. {
  2911. int i;
  2912. for (i = 0; i < bus->dev_count; i++) {
  2913. struct kvm_io_device *pos = bus->devs[i];
  2914. kvm_iodevice_destructor(pos);
  2915. }
  2916. }
  2917. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
  2918. {
  2919. int i;
  2920. for (i = 0; i < bus->dev_count; i++) {
  2921. struct kvm_io_device *pos = bus->devs[i];
  2922. if (pos->in_range(pos, addr))
  2923. return pos;
  2924. }
  2925. return NULL;
  2926. }
  2927. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  2928. {
  2929. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  2930. bus->devs[bus->dev_count++] = dev;
  2931. }
  2932. static struct notifier_block kvm_cpu_notifier = {
  2933. .notifier_call = kvm_cpu_hotplug,
  2934. .priority = 20, /* must be > scheduler priority */
  2935. };
  2936. static u64 stat_get(void *_offset)
  2937. {
  2938. unsigned offset = (long)_offset;
  2939. u64 total = 0;
  2940. struct kvm *kvm;
  2941. struct kvm_vcpu *vcpu;
  2942. int i;
  2943. spin_lock(&kvm_lock);
  2944. list_for_each_entry(kvm, &vm_list, vm_list)
  2945. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2946. vcpu = kvm->vcpus[i];
  2947. if (vcpu)
  2948. total += *(u32 *)((void *)vcpu + offset);
  2949. }
  2950. spin_unlock(&kvm_lock);
  2951. return total;
  2952. }
  2953. DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
  2954. static __init void kvm_init_debug(void)
  2955. {
  2956. struct kvm_stats_debugfs_item *p;
  2957. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2958. for (p = debugfs_entries; p->name; ++p)
  2959. p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
  2960. (void *)(long)p->offset,
  2961. &stat_fops);
  2962. }
  2963. static void kvm_exit_debug(void)
  2964. {
  2965. struct kvm_stats_debugfs_item *p;
  2966. for (p = debugfs_entries; p->name; ++p)
  2967. debugfs_remove(p->dentry);
  2968. debugfs_remove(debugfs_dir);
  2969. }
  2970. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2971. {
  2972. hardware_disable(NULL);
  2973. return 0;
  2974. }
  2975. static int kvm_resume(struct sys_device *dev)
  2976. {
  2977. hardware_enable(NULL);
  2978. return 0;
  2979. }
  2980. static struct sysdev_class kvm_sysdev_class = {
  2981. set_kset_name("kvm"),
  2982. .suspend = kvm_suspend,
  2983. .resume = kvm_resume,
  2984. };
  2985. static struct sys_device kvm_sysdev = {
  2986. .id = 0,
  2987. .cls = &kvm_sysdev_class,
  2988. };
  2989. hpa_t bad_page_address;
  2990. static inline
  2991. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2992. {
  2993. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2994. }
  2995. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2996. {
  2997. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2998. kvm_x86_ops->vcpu_load(vcpu, cpu);
  2999. }
  3000. static void kvm_sched_out(struct preempt_notifier *pn,
  3001. struct task_struct *next)
  3002. {
  3003. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  3004. kvm_x86_ops->vcpu_put(vcpu);
  3005. }
  3006. int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
  3007. struct module *module)
  3008. {
  3009. int r;
  3010. int cpu;
  3011. if (kvm_x86_ops) {
  3012. printk(KERN_ERR "kvm: already loaded the other module\n");
  3013. return -EEXIST;
  3014. }
  3015. if (!ops->cpu_has_kvm_support()) {
  3016. printk(KERN_ERR "kvm: no hardware support\n");
  3017. return -EOPNOTSUPP;
  3018. }
  3019. if (ops->disabled_by_bios()) {
  3020. printk(KERN_ERR "kvm: disabled by bios\n");
  3021. return -EOPNOTSUPP;
  3022. }
  3023. kvm_x86_ops = ops;
  3024. r = kvm_x86_ops->hardware_setup();
  3025. if (r < 0)
  3026. goto out;
  3027. for_each_online_cpu(cpu) {
  3028. smp_call_function_single(cpu,
  3029. kvm_x86_ops->check_processor_compatibility,
  3030. &r, 0, 1);
  3031. if (r < 0)
  3032. goto out_free_0;
  3033. }
  3034. on_each_cpu(hardware_enable, NULL, 0, 1);
  3035. r = register_cpu_notifier(&kvm_cpu_notifier);
  3036. if (r)
  3037. goto out_free_1;
  3038. register_reboot_notifier(&kvm_reboot_notifier);
  3039. r = sysdev_class_register(&kvm_sysdev_class);
  3040. if (r)
  3041. goto out_free_2;
  3042. r = sysdev_register(&kvm_sysdev);
  3043. if (r)
  3044. goto out_free_3;
  3045. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  3046. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
  3047. __alignof__(struct kvm_vcpu), 0, 0);
  3048. if (!kvm_vcpu_cache) {
  3049. r = -ENOMEM;
  3050. goto out_free_4;
  3051. }
  3052. kvm_chardev_ops.owner = module;
  3053. r = misc_register(&kvm_dev);
  3054. if (r) {
  3055. printk (KERN_ERR "kvm: misc device register failed\n");
  3056. goto out_free;
  3057. }
  3058. kvm_preempt_ops.sched_in = kvm_sched_in;
  3059. kvm_preempt_ops.sched_out = kvm_sched_out;
  3060. return r;
  3061. out_free:
  3062. kmem_cache_destroy(kvm_vcpu_cache);
  3063. out_free_4:
  3064. sysdev_unregister(&kvm_sysdev);
  3065. out_free_3:
  3066. sysdev_class_unregister(&kvm_sysdev_class);
  3067. out_free_2:
  3068. unregister_reboot_notifier(&kvm_reboot_notifier);
  3069. unregister_cpu_notifier(&kvm_cpu_notifier);
  3070. out_free_1:
  3071. on_each_cpu(hardware_disable, NULL, 0, 1);
  3072. out_free_0:
  3073. kvm_x86_ops->hardware_unsetup();
  3074. out:
  3075. kvm_x86_ops = NULL;
  3076. return r;
  3077. }
  3078. void kvm_exit_x86(void)
  3079. {
  3080. misc_deregister(&kvm_dev);
  3081. kmem_cache_destroy(kvm_vcpu_cache);
  3082. sysdev_unregister(&kvm_sysdev);
  3083. sysdev_class_unregister(&kvm_sysdev_class);
  3084. unregister_reboot_notifier(&kvm_reboot_notifier);
  3085. unregister_cpu_notifier(&kvm_cpu_notifier);
  3086. on_each_cpu(hardware_disable, NULL, 0, 1);
  3087. kvm_x86_ops->hardware_unsetup();
  3088. kvm_x86_ops = NULL;
  3089. }
  3090. static __init int kvm_init(void)
  3091. {
  3092. static struct page *bad_page;
  3093. int r;
  3094. r = kvm_mmu_module_init();
  3095. if (r)
  3096. goto out4;
  3097. kvm_init_debug();
  3098. kvm_init_msr_list();
  3099. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  3100. r = -ENOMEM;
  3101. goto out;
  3102. }
  3103. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  3104. memset(__va(bad_page_address), 0, PAGE_SIZE);
  3105. return 0;
  3106. out:
  3107. kvm_exit_debug();
  3108. kvm_mmu_module_exit();
  3109. out4:
  3110. return r;
  3111. }
  3112. static __exit void kvm_exit(void)
  3113. {
  3114. kvm_exit_debug();
  3115. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  3116. kvm_mmu_module_exit();
  3117. }
  3118. module_init(kvm_init)
  3119. module_exit(kvm_exit)
  3120. EXPORT_SYMBOL_GPL(kvm_init_x86);
  3121. EXPORT_SYMBOL_GPL(kvm_exit_x86);