kvm_main.c 72 KB

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