kvm_main.c 78 KB

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