kvm_main.c 78 KB

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