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. vcpu->cr4 = cr4;
  466. mutex_lock(&vcpu->kvm->lock);
  467. kvm_mmu_reset_context(vcpu);
  468. mutex_unlock(&vcpu->kvm->lock);
  469. }
  470. EXPORT_SYMBOL_GPL(set_cr4);
  471. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  472. {
  473. if (is_long_mode(vcpu)) {
  474. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  475. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  476. inject_gp(vcpu);
  477. return;
  478. }
  479. } else {
  480. if (is_pae(vcpu)) {
  481. if (cr3 & CR3_PAE_RESERVED_BITS) {
  482. printk(KERN_DEBUG
  483. "set_cr3: #GP, reserved bits\n");
  484. inject_gp(vcpu);
  485. return;
  486. }
  487. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  488. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  489. "reserved bits\n");
  490. inject_gp(vcpu);
  491. return;
  492. }
  493. } else {
  494. if (cr3 & CR3_NONPAE_RESERVED_BITS) {
  495. printk(KERN_DEBUG
  496. "set_cr3: #GP, reserved bits\n");
  497. inject_gp(vcpu);
  498. return;
  499. }
  500. }
  501. }
  502. mutex_lock(&vcpu->kvm->lock);
  503. /*
  504. * Does the new cr3 value map to physical memory? (Note, we
  505. * catch an invalid cr3 even in real-mode, because it would
  506. * cause trouble later on when we turn on paging anyway.)
  507. *
  508. * A real CPU would silently accept an invalid cr3 and would
  509. * attempt to use it - with largely undefined (and often hard
  510. * to debug) behavior on the guest side.
  511. */
  512. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  513. inject_gp(vcpu);
  514. else {
  515. vcpu->cr3 = cr3;
  516. vcpu->mmu.new_cr3(vcpu);
  517. }
  518. mutex_unlock(&vcpu->kvm->lock);
  519. }
  520. EXPORT_SYMBOL_GPL(set_cr3);
  521. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  522. {
  523. if (cr8 & CR8_RESERVED_BITS) {
  524. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  525. inject_gp(vcpu);
  526. return;
  527. }
  528. if (irqchip_in_kernel(vcpu->kvm))
  529. kvm_lapic_set_tpr(vcpu, cr8);
  530. else
  531. vcpu->cr8 = cr8;
  532. }
  533. EXPORT_SYMBOL_GPL(set_cr8);
  534. unsigned long get_cr8(struct kvm_vcpu *vcpu)
  535. {
  536. if (irqchip_in_kernel(vcpu->kvm))
  537. return kvm_lapic_get_cr8(vcpu);
  538. else
  539. return vcpu->cr8;
  540. }
  541. EXPORT_SYMBOL_GPL(get_cr8);
  542. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  543. {
  544. if (irqchip_in_kernel(vcpu->kvm))
  545. return vcpu->apic_base;
  546. else
  547. return vcpu->apic_base;
  548. }
  549. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  550. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  551. {
  552. /* TODO: reserve bits check */
  553. if (irqchip_in_kernel(vcpu->kvm))
  554. kvm_lapic_set_base(vcpu, data);
  555. else
  556. vcpu->apic_base = data;
  557. }
  558. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  559. void fx_init(struct kvm_vcpu *vcpu)
  560. {
  561. unsigned after_mxcsr_mask;
  562. /* Initialize guest FPU by resetting ours and saving into guest's */
  563. preempt_disable();
  564. fx_save(&vcpu->host_fx_image);
  565. fpu_init();
  566. fx_save(&vcpu->guest_fx_image);
  567. fx_restore(&vcpu->host_fx_image);
  568. preempt_enable();
  569. vcpu->cr0 |= X86_CR0_ET;
  570. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  571. vcpu->guest_fx_image.mxcsr = 0x1f80;
  572. memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
  573. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  574. }
  575. EXPORT_SYMBOL_GPL(fx_init);
  576. /*
  577. * Allocate some memory and give it an address in the guest physical address
  578. * space.
  579. *
  580. * Discontiguous memory is allowed, mostly for framebuffers.
  581. */
  582. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  583. struct kvm_memory_region *mem)
  584. {
  585. int r;
  586. gfn_t base_gfn;
  587. unsigned long npages;
  588. unsigned long i;
  589. struct kvm_memory_slot *memslot;
  590. struct kvm_memory_slot old, new;
  591. int memory_config_version;
  592. r = -EINVAL;
  593. /* General sanity checks */
  594. if (mem->memory_size & (PAGE_SIZE - 1))
  595. goto out;
  596. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  597. goto out;
  598. if (mem->slot >= KVM_MEMORY_SLOTS)
  599. goto out;
  600. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  601. goto out;
  602. memslot = &kvm->memslots[mem->slot];
  603. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  604. npages = mem->memory_size >> PAGE_SHIFT;
  605. if (!npages)
  606. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  607. raced:
  608. mutex_lock(&kvm->lock);
  609. memory_config_version = kvm->memory_config_version;
  610. new = old = *memslot;
  611. new.base_gfn = base_gfn;
  612. new.npages = npages;
  613. new.flags = mem->flags;
  614. /* Disallow changing a memory slot's size. */
  615. r = -EINVAL;
  616. if (npages && old.npages && npages != old.npages)
  617. goto out_unlock;
  618. /* Check for overlaps */
  619. r = -EEXIST;
  620. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  621. struct kvm_memory_slot *s = &kvm->memslots[i];
  622. if (s == memslot)
  623. continue;
  624. if (!((base_gfn + npages <= s->base_gfn) ||
  625. (base_gfn >= s->base_gfn + s->npages)))
  626. goto out_unlock;
  627. }
  628. /*
  629. * Do memory allocations outside lock. memory_config_version will
  630. * detect any races.
  631. */
  632. mutex_unlock(&kvm->lock);
  633. /* Deallocate if slot is being removed */
  634. if (!npages)
  635. new.phys_mem = NULL;
  636. /* Free page dirty bitmap if unneeded */
  637. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  638. new.dirty_bitmap = NULL;
  639. r = -ENOMEM;
  640. /* Allocate if a slot is being created */
  641. if (npages && !new.phys_mem) {
  642. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  643. if (!new.phys_mem)
  644. goto out_free;
  645. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  646. for (i = 0; i < npages; ++i) {
  647. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  648. | __GFP_ZERO);
  649. if (!new.phys_mem[i])
  650. goto out_free;
  651. set_page_private(new.phys_mem[i],0);
  652. }
  653. }
  654. /* Allocate page dirty bitmap if needed */
  655. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  656. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  657. new.dirty_bitmap = vmalloc(dirty_bytes);
  658. if (!new.dirty_bitmap)
  659. goto out_free;
  660. memset(new.dirty_bitmap, 0, dirty_bytes);
  661. }
  662. mutex_lock(&kvm->lock);
  663. if (memory_config_version != kvm->memory_config_version) {
  664. mutex_unlock(&kvm->lock);
  665. kvm_free_physmem_slot(&new, &old);
  666. goto raced;
  667. }
  668. r = -EAGAIN;
  669. if (kvm->busy)
  670. goto out_unlock;
  671. if (mem->slot >= kvm->nmemslots)
  672. kvm->nmemslots = mem->slot + 1;
  673. *memslot = new;
  674. ++kvm->memory_config_version;
  675. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  676. kvm_flush_remote_tlbs(kvm);
  677. mutex_unlock(&kvm->lock);
  678. kvm_free_physmem_slot(&old, &new);
  679. return 0;
  680. out_unlock:
  681. mutex_unlock(&kvm->lock);
  682. out_free:
  683. kvm_free_physmem_slot(&new, &old);
  684. out:
  685. return r;
  686. }
  687. /*
  688. * Get (and clear) the dirty memory log for a memory slot.
  689. */
  690. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  691. struct kvm_dirty_log *log)
  692. {
  693. struct kvm_memory_slot *memslot;
  694. int r, i;
  695. int n;
  696. unsigned long any = 0;
  697. mutex_lock(&kvm->lock);
  698. /*
  699. * Prevent changes to guest memory configuration even while the lock
  700. * is not taken.
  701. */
  702. ++kvm->busy;
  703. mutex_unlock(&kvm->lock);
  704. r = -EINVAL;
  705. if (log->slot >= KVM_MEMORY_SLOTS)
  706. goto out;
  707. memslot = &kvm->memslots[log->slot];
  708. r = -ENOENT;
  709. if (!memslot->dirty_bitmap)
  710. goto out;
  711. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  712. for (i = 0; !any && i < n/sizeof(long); ++i)
  713. any = memslot->dirty_bitmap[i];
  714. r = -EFAULT;
  715. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  716. goto out;
  717. /* If nothing is dirty, don't bother messing with page tables. */
  718. if (any) {
  719. mutex_lock(&kvm->lock);
  720. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  721. kvm_flush_remote_tlbs(kvm);
  722. memset(memslot->dirty_bitmap, 0, n);
  723. mutex_unlock(&kvm->lock);
  724. }
  725. r = 0;
  726. out:
  727. mutex_lock(&kvm->lock);
  728. --kvm->busy;
  729. mutex_unlock(&kvm->lock);
  730. return r;
  731. }
  732. /*
  733. * Set a new alias region. Aliases map a portion of physical memory into
  734. * another portion. This is useful for memory windows, for example the PC
  735. * VGA region.
  736. */
  737. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  738. struct kvm_memory_alias *alias)
  739. {
  740. int r, n;
  741. struct kvm_mem_alias *p;
  742. r = -EINVAL;
  743. /* General sanity checks */
  744. if (alias->memory_size & (PAGE_SIZE - 1))
  745. goto out;
  746. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  747. goto out;
  748. if (alias->slot >= KVM_ALIAS_SLOTS)
  749. goto out;
  750. if (alias->guest_phys_addr + alias->memory_size
  751. < alias->guest_phys_addr)
  752. goto out;
  753. if (alias->target_phys_addr + alias->memory_size
  754. < alias->target_phys_addr)
  755. goto out;
  756. mutex_lock(&kvm->lock);
  757. p = &kvm->aliases[alias->slot];
  758. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  759. p->npages = alias->memory_size >> PAGE_SHIFT;
  760. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  761. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  762. if (kvm->aliases[n - 1].npages)
  763. break;
  764. kvm->naliases = n;
  765. kvm_mmu_zap_all(kvm);
  766. mutex_unlock(&kvm->lock);
  767. return 0;
  768. out:
  769. return r;
  770. }
  771. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  772. {
  773. int r;
  774. r = 0;
  775. switch (chip->chip_id) {
  776. case KVM_IRQCHIP_PIC_MASTER:
  777. memcpy (&chip->chip.pic,
  778. &pic_irqchip(kvm)->pics[0],
  779. sizeof(struct kvm_pic_state));
  780. break;
  781. case KVM_IRQCHIP_PIC_SLAVE:
  782. memcpy (&chip->chip.pic,
  783. &pic_irqchip(kvm)->pics[1],
  784. sizeof(struct kvm_pic_state));
  785. break;
  786. case KVM_IRQCHIP_IOAPIC:
  787. memcpy (&chip->chip.ioapic,
  788. ioapic_irqchip(kvm),
  789. sizeof(struct kvm_ioapic_state));
  790. break;
  791. default:
  792. r = -EINVAL;
  793. break;
  794. }
  795. return r;
  796. }
  797. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  798. {
  799. int r;
  800. r = 0;
  801. switch (chip->chip_id) {
  802. case KVM_IRQCHIP_PIC_MASTER:
  803. memcpy (&pic_irqchip(kvm)->pics[0],
  804. &chip->chip.pic,
  805. sizeof(struct kvm_pic_state));
  806. break;
  807. case KVM_IRQCHIP_PIC_SLAVE:
  808. memcpy (&pic_irqchip(kvm)->pics[1],
  809. &chip->chip.pic,
  810. sizeof(struct kvm_pic_state));
  811. break;
  812. case KVM_IRQCHIP_IOAPIC:
  813. memcpy (ioapic_irqchip(kvm),
  814. &chip->chip.ioapic,
  815. sizeof(struct kvm_ioapic_state));
  816. break;
  817. default:
  818. r = -EINVAL;
  819. break;
  820. }
  821. kvm_pic_update_irq(pic_irqchip(kvm));
  822. return r;
  823. }
  824. static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  825. {
  826. int i;
  827. struct kvm_mem_alias *alias;
  828. for (i = 0; i < kvm->naliases; ++i) {
  829. alias = &kvm->aliases[i];
  830. if (gfn >= alias->base_gfn
  831. && gfn < alias->base_gfn + alias->npages)
  832. return alias->target_gfn + gfn - alias->base_gfn;
  833. }
  834. return gfn;
  835. }
  836. static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  837. {
  838. int i;
  839. for (i = 0; i < kvm->nmemslots; ++i) {
  840. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  841. if (gfn >= memslot->base_gfn
  842. && gfn < memslot->base_gfn + memslot->npages)
  843. return memslot;
  844. }
  845. return NULL;
  846. }
  847. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  848. {
  849. gfn = unalias_gfn(kvm, gfn);
  850. return __gfn_to_memslot(kvm, gfn);
  851. }
  852. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  853. {
  854. struct kvm_memory_slot *slot;
  855. gfn = unalias_gfn(kvm, gfn);
  856. slot = __gfn_to_memslot(kvm, gfn);
  857. if (!slot)
  858. return NULL;
  859. return slot->phys_mem[gfn - slot->base_gfn];
  860. }
  861. EXPORT_SYMBOL_GPL(gfn_to_page);
  862. /* WARNING: Does not work on aliased pages. */
  863. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  864. {
  865. struct kvm_memory_slot *memslot;
  866. memslot = __gfn_to_memslot(kvm, gfn);
  867. if (memslot && memslot->dirty_bitmap) {
  868. unsigned long rel_gfn = gfn - memslot->base_gfn;
  869. /* avoid RMW */
  870. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  871. set_bit(rel_gfn, memslot->dirty_bitmap);
  872. }
  873. }
  874. int emulator_read_std(unsigned long addr,
  875. void *val,
  876. unsigned int bytes,
  877. struct kvm_vcpu *vcpu)
  878. {
  879. void *data = val;
  880. while (bytes) {
  881. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  882. unsigned offset = addr & (PAGE_SIZE-1);
  883. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  884. unsigned long pfn;
  885. struct page *page;
  886. void *page_virt;
  887. if (gpa == UNMAPPED_GVA)
  888. return X86EMUL_PROPAGATE_FAULT;
  889. pfn = gpa >> PAGE_SHIFT;
  890. page = gfn_to_page(vcpu->kvm, pfn);
  891. if (!page)
  892. return X86EMUL_UNHANDLEABLE;
  893. page_virt = kmap_atomic(page, KM_USER0);
  894. memcpy(data, page_virt + offset, tocopy);
  895. kunmap_atomic(page_virt, KM_USER0);
  896. bytes -= tocopy;
  897. data += tocopy;
  898. addr += tocopy;
  899. }
  900. return X86EMUL_CONTINUE;
  901. }
  902. EXPORT_SYMBOL_GPL(emulator_read_std);
  903. static int emulator_write_std(unsigned long addr,
  904. const void *val,
  905. unsigned int bytes,
  906. struct kvm_vcpu *vcpu)
  907. {
  908. pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
  909. return X86EMUL_UNHANDLEABLE;
  910. }
  911. /*
  912. * Only apic need an MMIO device hook, so shortcut now..
  913. */
  914. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  915. gpa_t addr)
  916. {
  917. struct kvm_io_device *dev;
  918. if (vcpu->apic) {
  919. dev = &vcpu->apic->dev;
  920. if (dev->in_range(dev, addr))
  921. return dev;
  922. }
  923. return NULL;
  924. }
  925. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  926. gpa_t addr)
  927. {
  928. struct kvm_io_device *dev;
  929. dev = vcpu_find_pervcpu_dev(vcpu, addr);
  930. if (dev == NULL)
  931. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
  932. return dev;
  933. }
  934. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  935. gpa_t addr)
  936. {
  937. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
  938. }
  939. static int emulator_read_emulated(unsigned long addr,
  940. void *val,
  941. unsigned int bytes,
  942. struct kvm_vcpu *vcpu)
  943. {
  944. struct kvm_io_device *mmio_dev;
  945. gpa_t gpa;
  946. if (vcpu->mmio_read_completed) {
  947. memcpy(val, vcpu->mmio_data, bytes);
  948. vcpu->mmio_read_completed = 0;
  949. return X86EMUL_CONTINUE;
  950. } else if (emulator_read_std(addr, val, bytes, vcpu)
  951. == X86EMUL_CONTINUE)
  952. return X86EMUL_CONTINUE;
  953. gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  954. if (gpa == UNMAPPED_GVA)
  955. return X86EMUL_PROPAGATE_FAULT;
  956. /*
  957. * Is this MMIO handled locally?
  958. */
  959. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  960. if (mmio_dev) {
  961. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  962. return X86EMUL_CONTINUE;
  963. }
  964. vcpu->mmio_needed = 1;
  965. vcpu->mmio_phys_addr = gpa;
  966. vcpu->mmio_size = bytes;
  967. vcpu->mmio_is_write = 0;
  968. return X86EMUL_UNHANDLEABLE;
  969. }
  970. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  971. const void *val, int bytes)
  972. {
  973. struct page *page;
  974. void *virt;
  975. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  976. return 0;
  977. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  978. if (!page)
  979. return 0;
  980. mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
  981. virt = kmap_atomic(page, KM_USER0);
  982. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  983. memcpy(virt + offset_in_page(gpa), val, bytes);
  984. kunmap_atomic(virt, KM_USER0);
  985. return 1;
  986. }
  987. static int emulator_write_emulated_onepage(unsigned long addr,
  988. const void *val,
  989. unsigned int bytes,
  990. struct kvm_vcpu *vcpu)
  991. {
  992. struct kvm_io_device *mmio_dev;
  993. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  994. if (gpa == UNMAPPED_GVA) {
  995. kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
  996. return X86EMUL_PROPAGATE_FAULT;
  997. }
  998. if (emulator_write_phys(vcpu, gpa, val, bytes))
  999. return X86EMUL_CONTINUE;
  1000. /*
  1001. * Is this MMIO handled locally?
  1002. */
  1003. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  1004. if (mmio_dev) {
  1005. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  1006. return X86EMUL_CONTINUE;
  1007. }
  1008. vcpu->mmio_needed = 1;
  1009. vcpu->mmio_phys_addr = gpa;
  1010. vcpu->mmio_size = bytes;
  1011. vcpu->mmio_is_write = 1;
  1012. memcpy(vcpu->mmio_data, val, bytes);
  1013. return X86EMUL_CONTINUE;
  1014. }
  1015. int emulator_write_emulated(unsigned long addr,
  1016. const void *val,
  1017. unsigned int bytes,
  1018. struct kvm_vcpu *vcpu)
  1019. {
  1020. /* Crossing a page boundary? */
  1021. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  1022. int rc, now;
  1023. now = -addr & ~PAGE_MASK;
  1024. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  1025. if (rc != X86EMUL_CONTINUE)
  1026. return rc;
  1027. addr += now;
  1028. val += now;
  1029. bytes -= now;
  1030. }
  1031. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  1032. }
  1033. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  1034. static int emulator_cmpxchg_emulated(unsigned long addr,
  1035. const void *old,
  1036. const void *new,
  1037. unsigned int bytes,
  1038. struct kvm_vcpu *vcpu)
  1039. {
  1040. static int reported;
  1041. if (!reported) {
  1042. reported = 1;
  1043. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  1044. }
  1045. return emulator_write_emulated(addr, new, bytes, vcpu);
  1046. }
  1047. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1048. {
  1049. return kvm_arch_ops->get_segment_base(vcpu, seg);
  1050. }
  1051. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  1052. {
  1053. return X86EMUL_CONTINUE;
  1054. }
  1055. int emulate_clts(struct kvm_vcpu *vcpu)
  1056. {
  1057. vcpu->cr0 &= ~X86_CR0_TS;
  1058. kvm_arch_ops->set_cr0(vcpu, 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. vcpu->cr0 = sregs->cr0;
  1913. kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
  1914. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1915. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1916. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1917. load_pdptrs(vcpu, vcpu->cr3);
  1918. if (mmu_reset_needed)
  1919. kvm_mmu_reset_context(vcpu);
  1920. if (!irqchip_in_kernel(vcpu->kvm)) {
  1921. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1922. sizeof vcpu->irq_pending);
  1923. vcpu->irq_summary = 0;
  1924. for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
  1925. if (vcpu->irq_pending[i])
  1926. __set_bit(i, &vcpu->irq_summary);
  1927. } else {
  1928. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  1929. pending_vec = find_first_bit(
  1930. (const unsigned long *)sregs->interrupt_bitmap,
  1931. max_bits);
  1932. /* Only pending external irq is handled here */
  1933. if (pending_vec < max_bits) {
  1934. kvm_arch_ops->set_irq(vcpu, pending_vec);
  1935. printk("Set back pending irq %d\n", pending_vec);
  1936. }
  1937. }
  1938. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1939. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1940. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1941. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1942. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1943. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1944. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1945. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1946. vcpu_put(vcpu);
  1947. return 0;
  1948. }
  1949. /*
  1950. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1951. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1952. *
  1953. * This list is modified at module load time to reflect the
  1954. * capabilities of the host cpu.
  1955. */
  1956. static u32 msrs_to_save[] = {
  1957. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1958. MSR_K6_STAR,
  1959. #ifdef CONFIG_X86_64
  1960. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1961. #endif
  1962. MSR_IA32_TIME_STAMP_COUNTER,
  1963. };
  1964. static unsigned num_msrs_to_save;
  1965. static u32 emulated_msrs[] = {
  1966. MSR_IA32_MISC_ENABLE,
  1967. };
  1968. static __init void kvm_init_msr_list(void)
  1969. {
  1970. u32 dummy[2];
  1971. unsigned i, j;
  1972. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1973. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1974. continue;
  1975. if (j < i)
  1976. msrs_to_save[j] = msrs_to_save[i];
  1977. j++;
  1978. }
  1979. num_msrs_to_save = j;
  1980. }
  1981. /*
  1982. * Adapt set_msr() to msr_io()'s calling convention
  1983. */
  1984. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1985. {
  1986. return kvm_set_msr(vcpu, index, *data);
  1987. }
  1988. /*
  1989. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1990. *
  1991. * @return number of msrs set successfully.
  1992. */
  1993. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1994. struct kvm_msr_entry *entries,
  1995. int (*do_msr)(struct kvm_vcpu *vcpu,
  1996. unsigned index, u64 *data))
  1997. {
  1998. int i;
  1999. vcpu_load(vcpu);
  2000. for (i = 0; i < msrs->nmsrs; ++i)
  2001. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  2002. break;
  2003. vcpu_put(vcpu);
  2004. return i;
  2005. }
  2006. /*
  2007. * Read or write a bunch of msrs. Parameters are user addresses.
  2008. *
  2009. * @return number of msrs set successfully.
  2010. */
  2011. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  2012. int (*do_msr)(struct kvm_vcpu *vcpu,
  2013. unsigned index, u64 *data),
  2014. int writeback)
  2015. {
  2016. struct kvm_msrs msrs;
  2017. struct kvm_msr_entry *entries;
  2018. int r, n;
  2019. unsigned size;
  2020. r = -EFAULT;
  2021. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  2022. goto out;
  2023. r = -E2BIG;
  2024. if (msrs.nmsrs >= MAX_IO_MSRS)
  2025. goto out;
  2026. r = -ENOMEM;
  2027. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  2028. entries = vmalloc(size);
  2029. if (!entries)
  2030. goto out;
  2031. r = -EFAULT;
  2032. if (copy_from_user(entries, user_msrs->entries, size))
  2033. goto out_free;
  2034. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  2035. if (r < 0)
  2036. goto out_free;
  2037. r = -EFAULT;
  2038. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  2039. goto out_free;
  2040. r = n;
  2041. out_free:
  2042. vfree(entries);
  2043. out:
  2044. return r;
  2045. }
  2046. /*
  2047. * Translate a guest virtual address to a guest physical address.
  2048. */
  2049. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  2050. struct kvm_translation *tr)
  2051. {
  2052. unsigned long vaddr = tr->linear_address;
  2053. gpa_t gpa;
  2054. vcpu_load(vcpu);
  2055. mutex_lock(&vcpu->kvm->lock);
  2056. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  2057. tr->physical_address = gpa;
  2058. tr->valid = gpa != UNMAPPED_GVA;
  2059. tr->writeable = 1;
  2060. tr->usermode = 0;
  2061. mutex_unlock(&vcpu->kvm->lock);
  2062. vcpu_put(vcpu);
  2063. return 0;
  2064. }
  2065. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  2066. struct kvm_interrupt *irq)
  2067. {
  2068. if (irq->irq < 0 || irq->irq >= 256)
  2069. return -EINVAL;
  2070. if (irqchip_in_kernel(vcpu->kvm))
  2071. return -ENXIO;
  2072. vcpu_load(vcpu);
  2073. set_bit(irq->irq, vcpu->irq_pending);
  2074. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  2075. vcpu_put(vcpu);
  2076. return 0;
  2077. }
  2078. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  2079. struct kvm_debug_guest *dbg)
  2080. {
  2081. int r;
  2082. vcpu_load(vcpu);
  2083. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  2084. vcpu_put(vcpu);
  2085. return r;
  2086. }
  2087. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  2088. unsigned long address,
  2089. int *type)
  2090. {
  2091. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  2092. unsigned long pgoff;
  2093. struct page *page;
  2094. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2095. if (pgoff == 0)
  2096. page = virt_to_page(vcpu->run);
  2097. else if (pgoff == KVM_PIO_PAGE_OFFSET)
  2098. page = virt_to_page(vcpu->pio_data);
  2099. else
  2100. return NOPAGE_SIGBUS;
  2101. get_page(page);
  2102. if (type != NULL)
  2103. *type = VM_FAULT_MINOR;
  2104. return page;
  2105. }
  2106. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  2107. .nopage = kvm_vcpu_nopage,
  2108. };
  2109. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  2110. {
  2111. vma->vm_ops = &kvm_vcpu_vm_ops;
  2112. return 0;
  2113. }
  2114. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  2115. {
  2116. struct kvm_vcpu *vcpu = filp->private_data;
  2117. fput(vcpu->kvm->filp);
  2118. return 0;
  2119. }
  2120. static struct file_operations kvm_vcpu_fops = {
  2121. .release = kvm_vcpu_release,
  2122. .unlocked_ioctl = kvm_vcpu_ioctl,
  2123. .compat_ioctl = kvm_vcpu_ioctl,
  2124. .mmap = kvm_vcpu_mmap,
  2125. };
  2126. /*
  2127. * Allocates an inode for the vcpu.
  2128. */
  2129. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  2130. {
  2131. int fd, r;
  2132. struct inode *inode;
  2133. struct file *file;
  2134. r = anon_inode_getfd(&fd, &inode, &file,
  2135. "kvm-vcpu", &kvm_vcpu_fops, vcpu);
  2136. if (r)
  2137. return r;
  2138. atomic_inc(&vcpu->kvm->filp->f_count);
  2139. return fd;
  2140. }
  2141. /*
  2142. * Creates some virtual cpus. Good luck creating more than one.
  2143. */
  2144. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  2145. {
  2146. int r;
  2147. struct kvm_vcpu *vcpu;
  2148. if (!valid_vcpu(n))
  2149. return -EINVAL;
  2150. vcpu = kvm_arch_ops->vcpu_create(kvm, n);
  2151. if (IS_ERR(vcpu))
  2152. return PTR_ERR(vcpu);
  2153. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  2154. /* We do fxsave: this must be aligned. */
  2155. BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
  2156. vcpu_load(vcpu);
  2157. r = kvm_mmu_setup(vcpu);
  2158. vcpu_put(vcpu);
  2159. if (r < 0)
  2160. goto free_vcpu;
  2161. mutex_lock(&kvm->lock);
  2162. if (kvm->vcpus[n]) {
  2163. r = -EEXIST;
  2164. mutex_unlock(&kvm->lock);
  2165. goto mmu_unload;
  2166. }
  2167. kvm->vcpus[n] = vcpu;
  2168. mutex_unlock(&kvm->lock);
  2169. /* Now it's all set up, let userspace reach it */
  2170. r = create_vcpu_fd(vcpu);
  2171. if (r < 0)
  2172. goto unlink;
  2173. return r;
  2174. unlink:
  2175. mutex_lock(&kvm->lock);
  2176. kvm->vcpus[n] = NULL;
  2177. mutex_unlock(&kvm->lock);
  2178. mmu_unload:
  2179. vcpu_load(vcpu);
  2180. kvm_mmu_unload(vcpu);
  2181. vcpu_put(vcpu);
  2182. free_vcpu:
  2183. kvm_arch_ops->vcpu_free(vcpu);
  2184. return r;
  2185. }
  2186. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  2187. {
  2188. u64 efer;
  2189. int i;
  2190. struct kvm_cpuid_entry *e, *entry;
  2191. rdmsrl(MSR_EFER, efer);
  2192. entry = NULL;
  2193. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  2194. e = &vcpu->cpuid_entries[i];
  2195. if (e->function == 0x80000001) {
  2196. entry = e;
  2197. break;
  2198. }
  2199. }
  2200. if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
  2201. entry->edx &= ~(1 << 20);
  2202. printk(KERN_INFO "kvm: guest NX capability removed\n");
  2203. }
  2204. }
  2205. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  2206. struct kvm_cpuid *cpuid,
  2207. struct kvm_cpuid_entry __user *entries)
  2208. {
  2209. int r;
  2210. r = -E2BIG;
  2211. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  2212. goto out;
  2213. r = -EFAULT;
  2214. if (copy_from_user(&vcpu->cpuid_entries, entries,
  2215. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  2216. goto out;
  2217. vcpu->cpuid_nent = cpuid->nent;
  2218. cpuid_fix_nx_cap(vcpu);
  2219. return 0;
  2220. out:
  2221. return r;
  2222. }
  2223. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2224. {
  2225. if (sigset) {
  2226. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2227. vcpu->sigset_active = 1;
  2228. vcpu->sigset = *sigset;
  2229. } else
  2230. vcpu->sigset_active = 0;
  2231. return 0;
  2232. }
  2233. /*
  2234. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2235. * we have asm/x86/processor.h
  2236. */
  2237. struct fxsave {
  2238. u16 cwd;
  2239. u16 swd;
  2240. u16 twd;
  2241. u16 fop;
  2242. u64 rip;
  2243. u64 rdp;
  2244. u32 mxcsr;
  2245. u32 mxcsr_mask;
  2246. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2247. #ifdef CONFIG_X86_64
  2248. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2249. #else
  2250. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2251. #endif
  2252. };
  2253. static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2254. {
  2255. struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
  2256. vcpu_load(vcpu);
  2257. memcpy(fpu->fpr, fxsave->st_space, 128);
  2258. fpu->fcw = fxsave->cwd;
  2259. fpu->fsw = fxsave->swd;
  2260. fpu->ftwx = fxsave->twd;
  2261. fpu->last_opcode = fxsave->fop;
  2262. fpu->last_ip = fxsave->rip;
  2263. fpu->last_dp = fxsave->rdp;
  2264. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2265. vcpu_put(vcpu);
  2266. return 0;
  2267. }
  2268. static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2269. {
  2270. struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
  2271. vcpu_load(vcpu);
  2272. memcpy(fxsave->st_space, fpu->fpr, 128);
  2273. fxsave->cwd = fpu->fcw;
  2274. fxsave->swd = fpu->fsw;
  2275. fxsave->twd = fpu->ftwx;
  2276. fxsave->fop = fpu->last_opcode;
  2277. fxsave->rip = fpu->last_ip;
  2278. fxsave->rdp = fpu->last_dp;
  2279. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2280. vcpu_put(vcpu);
  2281. return 0;
  2282. }
  2283. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  2284. struct kvm_lapic_state *s)
  2285. {
  2286. vcpu_load(vcpu);
  2287. memcpy(s->regs, vcpu->apic->regs, sizeof *s);
  2288. vcpu_put(vcpu);
  2289. return 0;
  2290. }
  2291. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  2292. struct kvm_lapic_state *s)
  2293. {
  2294. vcpu_load(vcpu);
  2295. memcpy(vcpu->apic->regs, s->regs, sizeof *s);
  2296. kvm_apic_post_state_restore(vcpu);
  2297. vcpu_put(vcpu);
  2298. return 0;
  2299. }
  2300. static long kvm_vcpu_ioctl(struct file *filp,
  2301. unsigned int ioctl, unsigned long arg)
  2302. {
  2303. struct kvm_vcpu *vcpu = filp->private_data;
  2304. void __user *argp = (void __user *)arg;
  2305. int r = -EINVAL;
  2306. switch (ioctl) {
  2307. case KVM_RUN:
  2308. r = -EINVAL;
  2309. if (arg)
  2310. goto out;
  2311. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  2312. break;
  2313. case KVM_GET_REGS: {
  2314. struct kvm_regs kvm_regs;
  2315. memset(&kvm_regs, 0, sizeof kvm_regs);
  2316. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  2317. if (r)
  2318. goto out;
  2319. r = -EFAULT;
  2320. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  2321. goto out;
  2322. r = 0;
  2323. break;
  2324. }
  2325. case KVM_SET_REGS: {
  2326. struct kvm_regs kvm_regs;
  2327. r = -EFAULT;
  2328. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  2329. goto out;
  2330. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  2331. if (r)
  2332. goto out;
  2333. r = 0;
  2334. break;
  2335. }
  2336. case KVM_GET_SREGS: {
  2337. struct kvm_sregs kvm_sregs;
  2338. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  2339. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  2340. if (r)
  2341. goto out;
  2342. r = -EFAULT;
  2343. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  2344. goto out;
  2345. r = 0;
  2346. break;
  2347. }
  2348. case KVM_SET_SREGS: {
  2349. struct kvm_sregs kvm_sregs;
  2350. r = -EFAULT;
  2351. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  2352. goto out;
  2353. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  2354. if (r)
  2355. goto out;
  2356. r = 0;
  2357. break;
  2358. }
  2359. case KVM_TRANSLATE: {
  2360. struct kvm_translation tr;
  2361. r = -EFAULT;
  2362. if (copy_from_user(&tr, argp, sizeof tr))
  2363. goto out;
  2364. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  2365. if (r)
  2366. goto out;
  2367. r = -EFAULT;
  2368. if (copy_to_user(argp, &tr, sizeof tr))
  2369. goto out;
  2370. r = 0;
  2371. break;
  2372. }
  2373. case KVM_INTERRUPT: {
  2374. struct kvm_interrupt irq;
  2375. r = -EFAULT;
  2376. if (copy_from_user(&irq, argp, sizeof irq))
  2377. goto out;
  2378. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2379. if (r)
  2380. goto out;
  2381. r = 0;
  2382. break;
  2383. }
  2384. case KVM_DEBUG_GUEST: {
  2385. struct kvm_debug_guest dbg;
  2386. r = -EFAULT;
  2387. if (copy_from_user(&dbg, argp, sizeof dbg))
  2388. goto out;
  2389. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  2390. if (r)
  2391. goto out;
  2392. r = 0;
  2393. break;
  2394. }
  2395. case KVM_GET_MSRS:
  2396. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2397. break;
  2398. case KVM_SET_MSRS:
  2399. r = msr_io(vcpu, argp, do_set_msr, 0);
  2400. break;
  2401. case KVM_SET_CPUID: {
  2402. struct kvm_cpuid __user *cpuid_arg = argp;
  2403. struct kvm_cpuid cpuid;
  2404. r = -EFAULT;
  2405. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2406. goto out;
  2407. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2408. if (r)
  2409. goto out;
  2410. break;
  2411. }
  2412. case KVM_SET_SIGNAL_MASK: {
  2413. struct kvm_signal_mask __user *sigmask_arg = argp;
  2414. struct kvm_signal_mask kvm_sigmask;
  2415. sigset_t sigset, *p;
  2416. p = NULL;
  2417. if (argp) {
  2418. r = -EFAULT;
  2419. if (copy_from_user(&kvm_sigmask, argp,
  2420. sizeof kvm_sigmask))
  2421. goto out;
  2422. r = -EINVAL;
  2423. if (kvm_sigmask.len != sizeof sigset)
  2424. goto out;
  2425. r = -EFAULT;
  2426. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2427. sizeof sigset))
  2428. goto out;
  2429. p = &sigset;
  2430. }
  2431. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2432. break;
  2433. }
  2434. case KVM_GET_FPU: {
  2435. struct kvm_fpu fpu;
  2436. memset(&fpu, 0, sizeof fpu);
  2437. r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
  2438. if (r)
  2439. goto out;
  2440. r = -EFAULT;
  2441. if (copy_to_user(argp, &fpu, sizeof fpu))
  2442. goto out;
  2443. r = 0;
  2444. break;
  2445. }
  2446. case KVM_SET_FPU: {
  2447. struct kvm_fpu fpu;
  2448. r = -EFAULT;
  2449. if (copy_from_user(&fpu, argp, sizeof fpu))
  2450. goto out;
  2451. r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
  2452. if (r)
  2453. goto out;
  2454. r = 0;
  2455. break;
  2456. }
  2457. case KVM_GET_LAPIC: {
  2458. struct kvm_lapic_state lapic;
  2459. memset(&lapic, 0, sizeof lapic);
  2460. r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
  2461. if (r)
  2462. goto out;
  2463. r = -EFAULT;
  2464. if (copy_to_user(argp, &lapic, sizeof lapic))
  2465. goto out;
  2466. r = 0;
  2467. break;
  2468. }
  2469. case KVM_SET_LAPIC: {
  2470. struct kvm_lapic_state lapic;
  2471. r = -EFAULT;
  2472. if (copy_from_user(&lapic, argp, sizeof lapic))
  2473. goto out;
  2474. r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
  2475. if (r)
  2476. goto out;
  2477. r = 0;
  2478. break;
  2479. }
  2480. default:
  2481. ;
  2482. }
  2483. out:
  2484. return r;
  2485. }
  2486. static long kvm_vm_ioctl(struct file *filp,
  2487. unsigned int ioctl, unsigned long arg)
  2488. {
  2489. struct kvm *kvm = filp->private_data;
  2490. void __user *argp = (void __user *)arg;
  2491. int r = -EINVAL;
  2492. switch (ioctl) {
  2493. case KVM_CREATE_VCPU:
  2494. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2495. if (r < 0)
  2496. goto out;
  2497. break;
  2498. case KVM_SET_MEMORY_REGION: {
  2499. struct kvm_memory_region kvm_mem;
  2500. r = -EFAULT;
  2501. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2502. goto out;
  2503. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  2504. if (r)
  2505. goto out;
  2506. break;
  2507. }
  2508. case KVM_GET_DIRTY_LOG: {
  2509. struct kvm_dirty_log log;
  2510. r = -EFAULT;
  2511. if (copy_from_user(&log, argp, sizeof log))
  2512. goto out;
  2513. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2514. if (r)
  2515. goto out;
  2516. break;
  2517. }
  2518. case KVM_SET_MEMORY_ALIAS: {
  2519. struct kvm_memory_alias alias;
  2520. r = -EFAULT;
  2521. if (copy_from_user(&alias, argp, sizeof alias))
  2522. goto out;
  2523. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  2524. if (r)
  2525. goto out;
  2526. break;
  2527. }
  2528. case KVM_CREATE_IRQCHIP:
  2529. r = -ENOMEM;
  2530. kvm->vpic = kvm_create_pic(kvm);
  2531. if (kvm->vpic) {
  2532. r = kvm_ioapic_init(kvm);
  2533. if (r) {
  2534. kfree(kvm->vpic);
  2535. kvm->vpic = NULL;
  2536. goto out;
  2537. }
  2538. }
  2539. else
  2540. goto out;
  2541. break;
  2542. case KVM_IRQ_LINE: {
  2543. struct kvm_irq_level irq_event;
  2544. r = -EFAULT;
  2545. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2546. goto out;
  2547. if (irqchip_in_kernel(kvm)) {
  2548. mutex_lock(&kvm->lock);
  2549. if (irq_event.irq < 16)
  2550. kvm_pic_set_irq(pic_irqchip(kvm),
  2551. irq_event.irq,
  2552. irq_event.level);
  2553. kvm_ioapic_set_irq(kvm->vioapic,
  2554. irq_event.irq,
  2555. irq_event.level);
  2556. mutex_unlock(&kvm->lock);
  2557. r = 0;
  2558. }
  2559. break;
  2560. }
  2561. case KVM_GET_IRQCHIP: {
  2562. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2563. struct kvm_irqchip chip;
  2564. r = -EFAULT;
  2565. if (copy_from_user(&chip, argp, sizeof chip))
  2566. goto out;
  2567. r = -ENXIO;
  2568. if (!irqchip_in_kernel(kvm))
  2569. goto out;
  2570. r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
  2571. if (r)
  2572. goto out;
  2573. r = -EFAULT;
  2574. if (copy_to_user(argp, &chip, sizeof chip))
  2575. goto out;
  2576. r = 0;
  2577. break;
  2578. }
  2579. case KVM_SET_IRQCHIP: {
  2580. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2581. struct kvm_irqchip chip;
  2582. r = -EFAULT;
  2583. if (copy_from_user(&chip, argp, sizeof chip))
  2584. goto out;
  2585. r = -ENXIO;
  2586. if (!irqchip_in_kernel(kvm))
  2587. goto out;
  2588. r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
  2589. if (r)
  2590. goto out;
  2591. r = 0;
  2592. break;
  2593. }
  2594. default:
  2595. ;
  2596. }
  2597. out:
  2598. return r;
  2599. }
  2600. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  2601. unsigned long address,
  2602. int *type)
  2603. {
  2604. struct kvm *kvm = vma->vm_file->private_data;
  2605. unsigned long pgoff;
  2606. struct page *page;
  2607. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2608. page = gfn_to_page(kvm, pgoff);
  2609. if (!page)
  2610. return NOPAGE_SIGBUS;
  2611. get_page(page);
  2612. if (type != NULL)
  2613. *type = VM_FAULT_MINOR;
  2614. return page;
  2615. }
  2616. static struct vm_operations_struct kvm_vm_vm_ops = {
  2617. .nopage = kvm_vm_nopage,
  2618. };
  2619. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2620. {
  2621. vma->vm_ops = &kvm_vm_vm_ops;
  2622. return 0;
  2623. }
  2624. static struct file_operations kvm_vm_fops = {
  2625. .release = kvm_vm_release,
  2626. .unlocked_ioctl = kvm_vm_ioctl,
  2627. .compat_ioctl = kvm_vm_ioctl,
  2628. .mmap = kvm_vm_mmap,
  2629. };
  2630. static int kvm_dev_ioctl_create_vm(void)
  2631. {
  2632. int fd, r;
  2633. struct inode *inode;
  2634. struct file *file;
  2635. struct kvm *kvm;
  2636. kvm = kvm_create_vm();
  2637. if (IS_ERR(kvm))
  2638. return PTR_ERR(kvm);
  2639. r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
  2640. if (r) {
  2641. kvm_destroy_vm(kvm);
  2642. return r;
  2643. }
  2644. kvm->filp = file;
  2645. return fd;
  2646. }
  2647. static long kvm_dev_ioctl(struct file *filp,
  2648. unsigned int ioctl, unsigned long arg)
  2649. {
  2650. void __user *argp = (void __user *)arg;
  2651. long r = -EINVAL;
  2652. switch (ioctl) {
  2653. case KVM_GET_API_VERSION:
  2654. r = -EINVAL;
  2655. if (arg)
  2656. goto out;
  2657. r = KVM_API_VERSION;
  2658. break;
  2659. case KVM_CREATE_VM:
  2660. r = -EINVAL;
  2661. if (arg)
  2662. goto out;
  2663. r = kvm_dev_ioctl_create_vm();
  2664. break;
  2665. case KVM_GET_MSR_INDEX_LIST: {
  2666. struct kvm_msr_list __user *user_msr_list = argp;
  2667. struct kvm_msr_list msr_list;
  2668. unsigned n;
  2669. r = -EFAULT;
  2670. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2671. goto out;
  2672. n = msr_list.nmsrs;
  2673. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2674. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2675. goto out;
  2676. r = -E2BIG;
  2677. if (n < num_msrs_to_save)
  2678. goto out;
  2679. r = -EFAULT;
  2680. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2681. num_msrs_to_save * sizeof(u32)))
  2682. goto out;
  2683. if (copy_to_user(user_msr_list->indices
  2684. + num_msrs_to_save * sizeof(u32),
  2685. &emulated_msrs,
  2686. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2687. goto out;
  2688. r = 0;
  2689. break;
  2690. }
  2691. case KVM_CHECK_EXTENSION: {
  2692. int ext = (long)argp;
  2693. switch (ext) {
  2694. case KVM_CAP_IRQCHIP:
  2695. case KVM_CAP_HLT:
  2696. r = 1;
  2697. break;
  2698. default:
  2699. r = 0;
  2700. break;
  2701. }
  2702. break;
  2703. }
  2704. case KVM_GET_VCPU_MMAP_SIZE:
  2705. r = -EINVAL;
  2706. if (arg)
  2707. goto out;
  2708. r = 2 * PAGE_SIZE;
  2709. break;
  2710. default:
  2711. ;
  2712. }
  2713. out:
  2714. return r;
  2715. }
  2716. static struct file_operations kvm_chardev_ops = {
  2717. .unlocked_ioctl = kvm_dev_ioctl,
  2718. .compat_ioctl = kvm_dev_ioctl,
  2719. };
  2720. static struct miscdevice kvm_dev = {
  2721. KVM_MINOR,
  2722. "kvm",
  2723. &kvm_chardev_ops,
  2724. };
  2725. /*
  2726. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2727. * cached on it.
  2728. */
  2729. static void decache_vcpus_on_cpu(int cpu)
  2730. {
  2731. struct kvm *vm;
  2732. struct kvm_vcpu *vcpu;
  2733. int i;
  2734. spin_lock(&kvm_lock);
  2735. list_for_each_entry(vm, &vm_list, vm_list)
  2736. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2737. vcpu = vm->vcpus[i];
  2738. if (!vcpu)
  2739. continue;
  2740. /*
  2741. * If the vcpu is locked, then it is running on some
  2742. * other cpu and therefore it is not cached on the
  2743. * cpu in question.
  2744. *
  2745. * If it's not locked, check the last cpu it executed
  2746. * on.
  2747. */
  2748. if (mutex_trylock(&vcpu->mutex)) {
  2749. if (vcpu->cpu == cpu) {
  2750. kvm_arch_ops->vcpu_decache(vcpu);
  2751. vcpu->cpu = -1;
  2752. }
  2753. mutex_unlock(&vcpu->mutex);
  2754. }
  2755. }
  2756. spin_unlock(&kvm_lock);
  2757. }
  2758. static void hardware_enable(void *junk)
  2759. {
  2760. int cpu = raw_smp_processor_id();
  2761. if (cpu_isset(cpu, cpus_hardware_enabled))
  2762. return;
  2763. cpu_set(cpu, cpus_hardware_enabled);
  2764. kvm_arch_ops->hardware_enable(NULL);
  2765. }
  2766. static void hardware_disable(void *junk)
  2767. {
  2768. int cpu = raw_smp_processor_id();
  2769. if (!cpu_isset(cpu, cpus_hardware_enabled))
  2770. return;
  2771. cpu_clear(cpu, cpus_hardware_enabled);
  2772. decache_vcpus_on_cpu(cpu);
  2773. kvm_arch_ops->hardware_disable(NULL);
  2774. }
  2775. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2776. void *v)
  2777. {
  2778. int cpu = (long)v;
  2779. switch (val) {
  2780. case CPU_DYING:
  2781. case CPU_DYING_FROZEN:
  2782. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2783. cpu);
  2784. hardware_disable(NULL);
  2785. break;
  2786. case CPU_UP_CANCELED:
  2787. case CPU_UP_CANCELED_FROZEN:
  2788. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2789. cpu);
  2790. smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
  2791. break;
  2792. case CPU_ONLINE:
  2793. case CPU_ONLINE_FROZEN:
  2794. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2795. cpu);
  2796. smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
  2797. break;
  2798. }
  2799. return NOTIFY_OK;
  2800. }
  2801. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2802. void *v)
  2803. {
  2804. if (val == SYS_RESTART) {
  2805. /*
  2806. * Some (well, at least mine) BIOSes hang on reboot if
  2807. * in vmx root mode.
  2808. */
  2809. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2810. on_each_cpu(hardware_disable, NULL, 0, 1);
  2811. }
  2812. return NOTIFY_OK;
  2813. }
  2814. static struct notifier_block kvm_reboot_notifier = {
  2815. .notifier_call = kvm_reboot,
  2816. .priority = 0,
  2817. };
  2818. void kvm_io_bus_init(struct kvm_io_bus *bus)
  2819. {
  2820. memset(bus, 0, sizeof(*bus));
  2821. }
  2822. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2823. {
  2824. int i;
  2825. for (i = 0; i < bus->dev_count; i++) {
  2826. struct kvm_io_device *pos = bus->devs[i];
  2827. kvm_iodevice_destructor(pos);
  2828. }
  2829. }
  2830. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
  2831. {
  2832. int i;
  2833. for (i = 0; i < bus->dev_count; i++) {
  2834. struct kvm_io_device *pos = bus->devs[i];
  2835. if (pos->in_range(pos, addr))
  2836. return pos;
  2837. }
  2838. return NULL;
  2839. }
  2840. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  2841. {
  2842. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  2843. bus->devs[bus->dev_count++] = dev;
  2844. }
  2845. static struct notifier_block kvm_cpu_notifier = {
  2846. .notifier_call = kvm_cpu_hotplug,
  2847. .priority = 20, /* must be > scheduler priority */
  2848. };
  2849. static u64 stat_get(void *_offset)
  2850. {
  2851. unsigned offset = (long)_offset;
  2852. u64 total = 0;
  2853. struct kvm *kvm;
  2854. struct kvm_vcpu *vcpu;
  2855. int i;
  2856. spin_lock(&kvm_lock);
  2857. list_for_each_entry(kvm, &vm_list, vm_list)
  2858. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2859. vcpu = kvm->vcpus[i];
  2860. if (vcpu)
  2861. total += *(u32 *)((void *)vcpu + offset);
  2862. }
  2863. spin_unlock(&kvm_lock);
  2864. return total;
  2865. }
  2866. DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
  2867. static __init void kvm_init_debug(void)
  2868. {
  2869. struct kvm_stats_debugfs_item *p;
  2870. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2871. for (p = debugfs_entries; p->name; ++p)
  2872. p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
  2873. (void *)(long)p->offset,
  2874. &stat_fops);
  2875. }
  2876. static void kvm_exit_debug(void)
  2877. {
  2878. struct kvm_stats_debugfs_item *p;
  2879. for (p = debugfs_entries; p->name; ++p)
  2880. debugfs_remove(p->dentry);
  2881. debugfs_remove(debugfs_dir);
  2882. }
  2883. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2884. {
  2885. hardware_disable(NULL);
  2886. return 0;
  2887. }
  2888. static int kvm_resume(struct sys_device *dev)
  2889. {
  2890. hardware_enable(NULL);
  2891. return 0;
  2892. }
  2893. static struct sysdev_class kvm_sysdev_class = {
  2894. set_kset_name("kvm"),
  2895. .suspend = kvm_suspend,
  2896. .resume = kvm_resume,
  2897. };
  2898. static struct sys_device kvm_sysdev = {
  2899. .id = 0,
  2900. .cls = &kvm_sysdev_class,
  2901. };
  2902. hpa_t bad_page_address;
  2903. static inline
  2904. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2905. {
  2906. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2907. }
  2908. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2909. {
  2910. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2911. kvm_arch_ops->vcpu_load(vcpu, cpu);
  2912. }
  2913. static void kvm_sched_out(struct preempt_notifier *pn,
  2914. struct task_struct *next)
  2915. {
  2916. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2917. kvm_arch_ops->vcpu_put(vcpu);
  2918. }
  2919. int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
  2920. struct module *module)
  2921. {
  2922. int r;
  2923. int cpu;
  2924. if (kvm_arch_ops) {
  2925. printk(KERN_ERR "kvm: already loaded the other module\n");
  2926. return -EEXIST;
  2927. }
  2928. if (!ops->cpu_has_kvm_support()) {
  2929. printk(KERN_ERR "kvm: no hardware support\n");
  2930. return -EOPNOTSUPP;
  2931. }
  2932. if (ops->disabled_by_bios()) {
  2933. printk(KERN_ERR "kvm: disabled by bios\n");
  2934. return -EOPNOTSUPP;
  2935. }
  2936. kvm_arch_ops = ops;
  2937. r = kvm_arch_ops->hardware_setup();
  2938. if (r < 0)
  2939. goto out;
  2940. for_each_online_cpu(cpu) {
  2941. smp_call_function_single(cpu,
  2942. kvm_arch_ops->check_processor_compatibility,
  2943. &r, 0, 1);
  2944. if (r < 0)
  2945. goto out_free_0;
  2946. }
  2947. on_each_cpu(hardware_enable, NULL, 0, 1);
  2948. r = register_cpu_notifier(&kvm_cpu_notifier);
  2949. if (r)
  2950. goto out_free_1;
  2951. register_reboot_notifier(&kvm_reboot_notifier);
  2952. r = sysdev_class_register(&kvm_sysdev_class);
  2953. if (r)
  2954. goto out_free_2;
  2955. r = sysdev_register(&kvm_sysdev);
  2956. if (r)
  2957. goto out_free_3;
  2958. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  2959. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
  2960. __alignof__(struct kvm_vcpu), 0, 0);
  2961. if (!kvm_vcpu_cache) {
  2962. r = -ENOMEM;
  2963. goto out_free_4;
  2964. }
  2965. kvm_chardev_ops.owner = module;
  2966. r = misc_register(&kvm_dev);
  2967. if (r) {
  2968. printk (KERN_ERR "kvm: misc device register failed\n");
  2969. goto out_free;
  2970. }
  2971. kvm_preempt_ops.sched_in = kvm_sched_in;
  2972. kvm_preempt_ops.sched_out = kvm_sched_out;
  2973. return r;
  2974. out_free:
  2975. kmem_cache_destroy(kvm_vcpu_cache);
  2976. out_free_4:
  2977. sysdev_unregister(&kvm_sysdev);
  2978. out_free_3:
  2979. sysdev_class_unregister(&kvm_sysdev_class);
  2980. out_free_2:
  2981. unregister_reboot_notifier(&kvm_reboot_notifier);
  2982. unregister_cpu_notifier(&kvm_cpu_notifier);
  2983. out_free_1:
  2984. on_each_cpu(hardware_disable, NULL, 0, 1);
  2985. out_free_0:
  2986. kvm_arch_ops->hardware_unsetup();
  2987. out:
  2988. kvm_arch_ops = NULL;
  2989. return r;
  2990. }
  2991. void kvm_exit_arch(void)
  2992. {
  2993. misc_deregister(&kvm_dev);
  2994. kmem_cache_destroy(kvm_vcpu_cache);
  2995. sysdev_unregister(&kvm_sysdev);
  2996. sysdev_class_unregister(&kvm_sysdev_class);
  2997. unregister_reboot_notifier(&kvm_reboot_notifier);
  2998. unregister_cpu_notifier(&kvm_cpu_notifier);
  2999. on_each_cpu(hardware_disable, NULL, 0, 1);
  3000. kvm_arch_ops->hardware_unsetup();
  3001. kvm_arch_ops = NULL;
  3002. }
  3003. static __init int kvm_init(void)
  3004. {
  3005. static struct page *bad_page;
  3006. int r;
  3007. r = kvm_mmu_module_init();
  3008. if (r)
  3009. goto out4;
  3010. kvm_init_debug();
  3011. kvm_init_msr_list();
  3012. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  3013. r = -ENOMEM;
  3014. goto out;
  3015. }
  3016. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  3017. memset(__va(bad_page_address), 0, PAGE_SIZE);
  3018. return 0;
  3019. out:
  3020. kvm_exit_debug();
  3021. kvm_mmu_module_exit();
  3022. out4:
  3023. return r;
  3024. }
  3025. static __exit void kvm_exit(void)
  3026. {
  3027. kvm_exit_debug();
  3028. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  3029. kvm_mmu_module_exit();
  3030. }
  3031. module_init(kvm_init)
  3032. module_exit(kvm_exit)
  3033. EXPORT_SYMBOL_GPL(kvm_init_arch);
  3034. EXPORT_SYMBOL_GPL(kvm_exit_arch);