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. mutex_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. mutex_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. mutex_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. mutex_lock(&vcpu->kvm->lock);
  484. kvm_mmu_reset_context(vcpu);
  485. mutex_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. mutex_lock(&vcpu->kvm->lock);
  521. kvm_mmu_reset_context(vcpu);
  522. mutex_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. mutex_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. mutex_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. /* Initialize guest FPU by resetting ours and saving into guest's */
  597. preempt_disable();
  598. fx_save(vcpu->host_fx_image);
  599. fpu_init();
  600. fx_save(vcpu->guest_fx_image);
  601. fx_restore(vcpu->host_fx_image);
  602. preempt_enable();
  603. fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
  604. fx_image->mxcsr = 0x1f80;
  605. memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
  606. 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
  607. }
  608. EXPORT_SYMBOL_GPL(fx_init);
  609. /*
  610. * Allocate some memory and give it an address in the guest physical address
  611. * space.
  612. *
  613. * Discontiguous memory is allowed, mostly for framebuffers.
  614. */
  615. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  616. struct kvm_memory_region *mem)
  617. {
  618. int r;
  619. gfn_t base_gfn;
  620. unsigned long npages;
  621. unsigned long i;
  622. struct kvm_memory_slot *memslot;
  623. struct kvm_memory_slot old, new;
  624. int memory_config_version;
  625. r = -EINVAL;
  626. /* General sanity checks */
  627. if (mem->memory_size & (PAGE_SIZE - 1))
  628. goto out;
  629. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  630. goto out;
  631. if (mem->slot >= KVM_MEMORY_SLOTS)
  632. goto out;
  633. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  634. goto out;
  635. memslot = &kvm->memslots[mem->slot];
  636. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  637. npages = mem->memory_size >> PAGE_SHIFT;
  638. if (!npages)
  639. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  640. raced:
  641. mutex_lock(&kvm->lock);
  642. memory_config_version = kvm->memory_config_version;
  643. new = old = *memslot;
  644. new.base_gfn = base_gfn;
  645. new.npages = npages;
  646. new.flags = mem->flags;
  647. /* Disallow changing a memory slot's size. */
  648. r = -EINVAL;
  649. if (npages && old.npages && npages != old.npages)
  650. goto out_unlock;
  651. /* Check for overlaps */
  652. r = -EEXIST;
  653. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  654. struct kvm_memory_slot *s = &kvm->memslots[i];
  655. if (s == memslot)
  656. continue;
  657. if (!((base_gfn + npages <= s->base_gfn) ||
  658. (base_gfn >= s->base_gfn + s->npages)))
  659. goto out_unlock;
  660. }
  661. /*
  662. * Do memory allocations outside lock. memory_config_version will
  663. * detect any races.
  664. */
  665. mutex_unlock(&kvm->lock);
  666. /* Deallocate if slot is being removed */
  667. if (!npages)
  668. new.phys_mem = NULL;
  669. /* Free page dirty bitmap if unneeded */
  670. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  671. new.dirty_bitmap = NULL;
  672. r = -ENOMEM;
  673. /* Allocate if a slot is being created */
  674. if (npages && !new.phys_mem) {
  675. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  676. if (!new.phys_mem)
  677. goto out_free;
  678. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  679. for (i = 0; i < npages; ++i) {
  680. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  681. | __GFP_ZERO);
  682. if (!new.phys_mem[i])
  683. goto out_free;
  684. set_page_private(new.phys_mem[i],0);
  685. }
  686. }
  687. /* Allocate page dirty bitmap if needed */
  688. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  689. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  690. new.dirty_bitmap = vmalloc(dirty_bytes);
  691. if (!new.dirty_bitmap)
  692. goto out_free;
  693. memset(new.dirty_bitmap, 0, dirty_bytes);
  694. }
  695. mutex_lock(&kvm->lock);
  696. if (memory_config_version != kvm->memory_config_version) {
  697. mutex_unlock(&kvm->lock);
  698. kvm_free_physmem_slot(&new, &old);
  699. goto raced;
  700. }
  701. r = -EAGAIN;
  702. if (kvm->busy)
  703. goto out_unlock;
  704. if (mem->slot >= kvm->nmemslots)
  705. kvm->nmemslots = mem->slot + 1;
  706. *memslot = new;
  707. ++kvm->memory_config_version;
  708. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  709. kvm_flush_remote_tlbs(kvm);
  710. mutex_unlock(&kvm->lock);
  711. kvm_free_physmem_slot(&old, &new);
  712. return 0;
  713. out_unlock:
  714. mutex_unlock(&kvm->lock);
  715. out_free:
  716. kvm_free_physmem_slot(&new, &old);
  717. out:
  718. return r;
  719. }
  720. /*
  721. * Get (and clear) the dirty memory log for a memory slot.
  722. */
  723. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  724. struct kvm_dirty_log *log)
  725. {
  726. struct kvm_memory_slot *memslot;
  727. int r, i;
  728. int n;
  729. unsigned long any = 0;
  730. mutex_lock(&kvm->lock);
  731. /*
  732. * Prevent changes to guest memory configuration even while the lock
  733. * is not taken.
  734. */
  735. ++kvm->busy;
  736. mutex_unlock(&kvm->lock);
  737. r = -EINVAL;
  738. if (log->slot >= KVM_MEMORY_SLOTS)
  739. goto out;
  740. memslot = &kvm->memslots[log->slot];
  741. r = -ENOENT;
  742. if (!memslot->dirty_bitmap)
  743. goto out;
  744. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  745. for (i = 0; !any && i < n/sizeof(long); ++i)
  746. any = memslot->dirty_bitmap[i];
  747. r = -EFAULT;
  748. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  749. goto out;
  750. mutex_lock(&kvm->lock);
  751. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  752. kvm_flush_remote_tlbs(kvm);
  753. memset(memslot->dirty_bitmap, 0, n);
  754. mutex_unlock(&kvm->lock);
  755. r = 0;
  756. out:
  757. mutex_lock(&kvm->lock);
  758. --kvm->busy;
  759. mutex_unlock(&kvm->lock);
  760. return r;
  761. }
  762. /*
  763. * Set a new alias region. Aliases map a portion of physical memory into
  764. * another portion. This is useful for memory windows, for example the PC
  765. * VGA region.
  766. */
  767. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  768. struct kvm_memory_alias *alias)
  769. {
  770. int r, n;
  771. struct kvm_mem_alias *p;
  772. r = -EINVAL;
  773. /* General sanity checks */
  774. if (alias->memory_size & (PAGE_SIZE - 1))
  775. goto out;
  776. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  777. goto out;
  778. if (alias->slot >= KVM_ALIAS_SLOTS)
  779. goto out;
  780. if (alias->guest_phys_addr + alias->memory_size
  781. < alias->guest_phys_addr)
  782. goto out;
  783. if (alias->target_phys_addr + alias->memory_size
  784. < alias->target_phys_addr)
  785. goto out;
  786. mutex_lock(&kvm->lock);
  787. p = &kvm->aliases[alias->slot];
  788. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  789. p->npages = alias->memory_size >> PAGE_SHIFT;
  790. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  791. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  792. if (kvm->aliases[n - 1].npages)
  793. break;
  794. kvm->naliases = n;
  795. kvm_mmu_zap_all(kvm);
  796. mutex_unlock(&kvm->lock);
  797. return 0;
  798. out:
  799. return r;
  800. }
  801. static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  802. {
  803. int i;
  804. struct kvm_mem_alias *alias;
  805. for (i = 0; i < kvm->naliases; ++i) {
  806. alias = &kvm->aliases[i];
  807. if (gfn >= alias->base_gfn
  808. && gfn < alias->base_gfn + alias->npages)
  809. return alias->target_gfn + gfn - alias->base_gfn;
  810. }
  811. return gfn;
  812. }
  813. static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  814. {
  815. int i;
  816. for (i = 0; i < kvm->nmemslots; ++i) {
  817. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  818. if (gfn >= memslot->base_gfn
  819. && gfn < memslot->base_gfn + memslot->npages)
  820. return memslot;
  821. }
  822. return NULL;
  823. }
  824. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  825. {
  826. gfn = unalias_gfn(kvm, gfn);
  827. return __gfn_to_memslot(kvm, gfn);
  828. }
  829. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  830. {
  831. struct kvm_memory_slot *slot;
  832. gfn = unalias_gfn(kvm, gfn);
  833. slot = __gfn_to_memslot(kvm, gfn);
  834. if (!slot)
  835. return NULL;
  836. return slot->phys_mem[gfn - slot->base_gfn];
  837. }
  838. EXPORT_SYMBOL_GPL(gfn_to_page);
  839. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  840. {
  841. int i;
  842. struct kvm_memory_slot *memslot;
  843. unsigned long rel_gfn;
  844. for (i = 0; i < kvm->nmemslots; ++i) {
  845. memslot = &kvm->memslots[i];
  846. if (gfn >= memslot->base_gfn
  847. && gfn < memslot->base_gfn + memslot->npages) {
  848. if (!memslot->dirty_bitmap)
  849. return;
  850. rel_gfn = gfn - memslot->base_gfn;
  851. /* avoid RMW */
  852. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  853. set_bit(rel_gfn, memslot->dirty_bitmap);
  854. return;
  855. }
  856. }
  857. }
  858. static int emulator_read_std(unsigned long addr,
  859. void *val,
  860. unsigned int bytes,
  861. struct x86_emulate_ctxt *ctxt)
  862. {
  863. struct kvm_vcpu *vcpu = ctxt->vcpu;
  864. void *data = val;
  865. while (bytes) {
  866. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  867. unsigned offset = addr & (PAGE_SIZE-1);
  868. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  869. unsigned long pfn;
  870. struct page *page;
  871. void *page_virt;
  872. if (gpa == UNMAPPED_GVA)
  873. return X86EMUL_PROPAGATE_FAULT;
  874. pfn = gpa >> PAGE_SHIFT;
  875. page = gfn_to_page(vcpu->kvm, pfn);
  876. if (!page)
  877. return X86EMUL_UNHANDLEABLE;
  878. page_virt = kmap_atomic(page, KM_USER0);
  879. memcpy(data, page_virt + offset, tocopy);
  880. kunmap_atomic(page_virt, KM_USER0);
  881. bytes -= tocopy;
  882. data += tocopy;
  883. addr += tocopy;
  884. }
  885. return X86EMUL_CONTINUE;
  886. }
  887. static int emulator_write_std(unsigned long addr,
  888. const void *val,
  889. unsigned int bytes,
  890. struct x86_emulate_ctxt *ctxt)
  891. {
  892. printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
  893. addr, bytes);
  894. return X86EMUL_UNHANDLEABLE;
  895. }
  896. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  897. gpa_t addr)
  898. {
  899. /*
  900. * Note that its important to have this wrapper function because
  901. * in the very near future we will be checking for MMIOs against
  902. * the LAPIC as well as the general MMIO bus
  903. */
  904. return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
  905. }
  906. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  907. gpa_t addr)
  908. {
  909. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
  910. }
  911. static int emulator_read_emulated(unsigned long addr,
  912. void *val,
  913. unsigned int bytes,
  914. struct x86_emulate_ctxt *ctxt)
  915. {
  916. struct kvm_vcpu *vcpu = ctxt->vcpu;
  917. struct kvm_io_device *mmio_dev;
  918. gpa_t gpa;
  919. if (vcpu->mmio_read_completed) {
  920. memcpy(val, vcpu->mmio_data, bytes);
  921. vcpu->mmio_read_completed = 0;
  922. return X86EMUL_CONTINUE;
  923. } else if (emulator_read_std(addr, val, bytes, ctxt)
  924. == X86EMUL_CONTINUE)
  925. return X86EMUL_CONTINUE;
  926. gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  927. if (gpa == UNMAPPED_GVA)
  928. return X86EMUL_PROPAGATE_FAULT;
  929. /*
  930. * Is this MMIO handled locally?
  931. */
  932. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  933. if (mmio_dev) {
  934. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  935. return X86EMUL_CONTINUE;
  936. }
  937. vcpu->mmio_needed = 1;
  938. vcpu->mmio_phys_addr = gpa;
  939. vcpu->mmio_size = bytes;
  940. vcpu->mmio_is_write = 0;
  941. return X86EMUL_UNHANDLEABLE;
  942. }
  943. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  944. const void *val, int bytes)
  945. {
  946. struct page *page;
  947. void *virt;
  948. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  949. return 0;
  950. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  951. if (!page)
  952. return 0;
  953. mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
  954. virt = kmap_atomic(page, KM_USER0);
  955. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  956. memcpy(virt + offset_in_page(gpa), val, bytes);
  957. kunmap_atomic(virt, KM_USER0);
  958. return 1;
  959. }
  960. static int emulator_write_emulated_onepage(unsigned long addr,
  961. const void *val,
  962. unsigned int bytes,
  963. struct x86_emulate_ctxt *ctxt)
  964. {
  965. struct kvm_vcpu *vcpu = ctxt->vcpu;
  966. struct kvm_io_device *mmio_dev;
  967. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  968. if (gpa == UNMAPPED_GVA) {
  969. kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
  970. return X86EMUL_PROPAGATE_FAULT;
  971. }
  972. if (emulator_write_phys(vcpu, gpa, val, bytes))
  973. return X86EMUL_CONTINUE;
  974. /*
  975. * Is this MMIO handled locally?
  976. */
  977. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  978. if (mmio_dev) {
  979. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  980. return X86EMUL_CONTINUE;
  981. }
  982. vcpu->mmio_needed = 1;
  983. vcpu->mmio_phys_addr = gpa;
  984. vcpu->mmio_size = bytes;
  985. vcpu->mmio_is_write = 1;
  986. memcpy(vcpu->mmio_data, val, bytes);
  987. return X86EMUL_CONTINUE;
  988. }
  989. static int emulator_write_emulated(unsigned long addr,
  990. const void *val,
  991. unsigned int bytes,
  992. struct x86_emulate_ctxt *ctxt)
  993. {
  994. /* Crossing a page boundary? */
  995. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  996. int rc, now;
  997. now = -addr & ~PAGE_MASK;
  998. rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
  999. if (rc != X86EMUL_CONTINUE)
  1000. return rc;
  1001. addr += now;
  1002. val += now;
  1003. bytes -= now;
  1004. }
  1005. return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
  1006. }
  1007. static int emulator_cmpxchg_emulated(unsigned long addr,
  1008. const void *old,
  1009. const void *new,
  1010. unsigned int bytes,
  1011. struct x86_emulate_ctxt *ctxt)
  1012. {
  1013. static int reported;
  1014. if (!reported) {
  1015. reported = 1;
  1016. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  1017. }
  1018. return emulator_write_emulated(addr, new, bytes, ctxt);
  1019. }
  1020. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1021. {
  1022. return kvm_arch_ops->get_segment_base(vcpu, seg);
  1023. }
  1024. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  1025. {
  1026. return X86EMUL_CONTINUE;
  1027. }
  1028. int emulate_clts(struct kvm_vcpu *vcpu)
  1029. {
  1030. unsigned long cr0;
  1031. cr0 = vcpu->cr0 & ~X86_CR0_TS;
  1032. kvm_arch_ops->set_cr0(vcpu, cr0);
  1033. return X86EMUL_CONTINUE;
  1034. }
  1035. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  1036. {
  1037. struct kvm_vcpu *vcpu = ctxt->vcpu;
  1038. switch (dr) {
  1039. case 0 ... 3:
  1040. *dest = kvm_arch_ops->get_dr(vcpu, dr);
  1041. return X86EMUL_CONTINUE;
  1042. default:
  1043. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  1044. __FUNCTION__, dr);
  1045. return X86EMUL_UNHANDLEABLE;
  1046. }
  1047. }
  1048. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  1049. {
  1050. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  1051. int exception;
  1052. kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  1053. if (exception) {
  1054. /* FIXME: better handling */
  1055. return X86EMUL_UNHANDLEABLE;
  1056. }
  1057. return X86EMUL_CONTINUE;
  1058. }
  1059. static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
  1060. {
  1061. static int reported;
  1062. u8 opcodes[4];
  1063. unsigned long rip = ctxt->vcpu->rip;
  1064. unsigned long rip_linear;
  1065. rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
  1066. if (reported)
  1067. return;
  1068. emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
  1069. printk(KERN_ERR "emulation failed but !mmio_needed?"
  1070. " rip %lx %02x %02x %02x %02x\n",
  1071. rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  1072. reported = 1;
  1073. }
  1074. struct x86_emulate_ops emulate_ops = {
  1075. .read_std = emulator_read_std,
  1076. .write_std = emulator_write_std,
  1077. .read_emulated = emulator_read_emulated,
  1078. .write_emulated = emulator_write_emulated,
  1079. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  1080. };
  1081. int emulate_instruction(struct kvm_vcpu *vcpu,
  1082. struct kvm_run *run,
  1083. unsigned long cr2,
  1084. u16 error_code)
  1085. {
  1086. struct x86_emulate_ctxt emulate_ctxt;
  1087. int r;
  1088. int cs_db, cs_l;
  1089. vcpu->mmio_fault_cr2 = cr2;
  1090. kvm_arch_ops->cache_regs(vcpu);
  1091. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  1092. emulate_ctxt.vcpu = vcpu;
  1093. emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
  1094. emulate_ctxt.cr2 = cr2;
  1095. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  1096. ? X86EMUL_MODE_REAL : cs_l
  1097. ? X86EMUL_MODE_PROT64 : cs_db
  1098. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  1099. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  1100. emulate_ctxt.cs_base = 0;
  1101. emulate_ctxt.ds_base = 0;
  1102. emulate_ctxt.es_base = 0;
  1103. emulate_ctxt.ss_base = 0;
  1104. } else {
  1105. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  1106. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  1107. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  1108. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  1109. }
  1110. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  1111. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  1112. vcpu->mmio_is_write = 0;
  1113. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  1114. if ((r || vcpu->mmio_is_write) && run) {
  1115. run->exit_reason = KVM_EXIT_MMIO;
  1116. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  1117. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  1118. run->mmio.len = vcpu->mmio_size;
  1119. run->mmio.is_write = vcpu->mmio_is_write;
  1120. }
  1121. if (r) {
  1122. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1123. return EMULATE_DONE;
  1124. if (!vcpu->mmio_needed) {
  1125. report_emulation_failure(&emulate_ctxt);
  1126. return EMULATE_FAIL;
  1127. }
  1128. return EMULATE_DO_MMIO;
  1129. }
  1130. kvm_arch_ops->decache_regs(vcpu);
  1131. kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  1132. if (vcpu->mmio_is_write) {
  1133. vcpu->mmio_needed = 0;
  1134. return EMULATE_DO_MMIO;
  1135. }
  1136. return EMULATE_DONE;
  1137. }
  1138. EXPORT_SYMBOL_GPL(emulate_instruction);
  1139. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  1140. {
  1141. if (vcpu->irq_summary)
  1142. return 1;
  1143. vcpu->run->exit_reason = KVM_EXIT_HLT;
  1144. ++vcpu->stat.halt_exits;
  1145. return 0;
  1146. }
  1147. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  1148. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1149. {
  1150. unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
  1151. kvm_arch_ops->cache_regs(vcpu);
  1152. ret = -KVM_EINVAL;
  1153. #ifdef CONFIG_X86_64
  1154. if (is_long_mode(vcpu)) {
  1155. nr = vcpu->regs[VCPU_REGS_RAX];
  1156. a0 = vcpu->regs[VCPU_REGS_RDI];
  1157. a1 = vcpu->regs[VCPU_REGS_RSI];
  1158. a2 = vcpu->regs[VCPU_REGS_RDX];
  1159. a3 = vcpu->regs[VCPU_REGS_RCX];
  1160. a4 = vcpu->regs[VCPU_REGS_R8];
  1161. a5 = vcpu->regs[VCPU_REGS_R9];
  1162. } else
  1163. #endif
  1164. {
  1165. nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
  1166. a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
  1167. a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
  1168. a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
  1169. a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
  1170. a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
  1171. a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
  1172. }
  1173. switch (nr) {
  1174. default:
  1175. run->hypercall.nr = nr;
  1176. run->hypercall.args[0] = a0;
  1177. run->hypercall.args[1] = a1;
  1178. run->hypercall.args[2] = a2;
  1179. run->hypercall.args[3] = a3;
  1180. run->hypercall.args[4] = a4;
  1181. run->hypercall.args[5] = a5;
  1182. run->hypercall.ret = ret;
  1183. run->hypercall.longmode = is_long_mode(vcpu);
  1184. kvm_arch_ops->decache_regs(vcpu);
  1185. return 0;
  1186. }
  1187. vcpu->regs[VCPU_REGS_RAX] = ret;
  1188. kvm_arch_ops->decache_regs(vcpu);
  1189. return 1;
  1190. }
  1191. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1192. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1193. {
  1194. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1195. }
  1196. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1197. {
  1198. struct descriptor_table dt = { limit, base };
  1199. kvm_arch_ops->set_gdt(vcpu, &dt);
  1200. }
  1201. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1202. {
  1203. struct descriptor_table dt = { limit, base };
  1204. kvm_arch_ops->set_idt(vcpu, &dt);
  1205. }
  1206. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1207. unsigned long *rflags)
  1208. {
  1209. lmsw(vcpu, msw);
  1210. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1211. }
  1212. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1213. {
  1214. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1215. switch (cr) {
  1216. case 0:
  1217. return vcpu->cr0;
  1218. case 2:
  1219. return vcpu->cr2;
  1220. case 3:
  1221. return vcpu->cr3;
  1222. case 4:
  1223. return vcpu->cr4;
  1224. default:
  1225. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1226. return 0;
  1227. }
  1228. }
  1229. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1230. unsigned long *rflags)
  1231. {
  1232. switch (cr) {
  1233. case 0:
  1234. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1235. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1236. break;
  1237. case 2:
  1238. vcpu->cr2 = val;
  1239. break;
  1240. case 3:
  1241. set_cr3(vcpu, val);
  1242. break;
  1243. case 4:
  1244. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1245. break;
  1246. default:
  1247. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1248. }
  1249. }
  1250. /*
  1251. * Register the para guest with the host:
  1252. */
  1253. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1254. {
  1255. struct kvm_vcpu_para_state *para_state;
  1256. hpa_t para_state_hpa, hypercall_hpa;
  1257. struct page *para_state_page;
  1258. unsigned char *hypercall;
  1259. gpa_t hypercall_gpa;
  1260. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1261. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1262. /*
  1263. * Needs to be page aligned:
  1264. */
  1265. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1266. goto err_gp;
  1267. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1268. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1269. if (is_error_hpa(para_state_hpa))
  1270. goto err_gp;
  1271. mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
  1272. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1273. para_state = kmap(para_state_page);
  1274. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1275. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1276. para_state->host_version = KVM_PARA_API_VERSION;
  1277. /*
  1278. * We cannot support guests that try to register themselves
  1279. * with a newer API version than the host supports:
  1280. */
  1281. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1282. para_state->ret = -KVM_EINVAL;
  1283. goto err_kunmap_skip;
  1284. }
  1285. hypercall_gpa = para_state->hypercall_gpa;
  1286. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1287. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1288. if (is_error_hpa(hypercall_hpa)) {
  1289. para_state->ret = -KVM_EINVAL;
  1290. goto err_kunmap_skip;
  1291. }
  1292. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1293. vcpu->para_state_page = para_state_page;
  1294. vcpu->para_state_gpa = para_state_gpa;
  1295. vcpu->hypercall_gpa = hypercall_gpa;
  1296. mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
  1297. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1298. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1299. kvm_arch_ops->patch_hypercall(vcpu, hypercall);
  1300. kunmap_atomic(hypercall, KM_USER1);
  1301. para_state->ret = 0;
  1302. err_kunmap_skip:
  1303. kunmap(para_state_page);
  1304. return 0;
  1305. err_gp:
  1306. return 1;
  1307. }
  1308. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1309. {
  1310. u64 data;
  1311. switch (msr) {
  1312. case 0xc0010010: /* SYSCFG */
  1313. case 0xc0010015: /* HWCR */
  1314. case MSR_IA32_PLATFORM_ID:
  1315. case MSR_IA32_P5_MC_ADDR:
  1316. case MSR_IA32_P5_MC_TYPE:
  1317. case MSR_IA32_MC0_CTL:
  1318. case MSR_IA32_MCG_STATUS:
  1319. case MSR_IA32_MCG_CAP:
  1320. case MSR_IA32_MC0_MISC:
  1321. case MSR_IA32_MC0_MISC+4:
  1322. case MSR_IA32_MC0_MISC+8:
  1323. case MSR_IA32_MC0_MISC+12:
  1324. case MSR_IA32_MC0_MISC+16:
  1325. case MSR_IA32_UCODE_REV:
  1326. case MSR_IA32_PERF_STATUS:
  1327. case MSR_IA32_EBL_CR_POWERON:
  1328. /* MTRR registers */
  1329. case 0xfe:
  1330. case 0x200 ... 0x2ff:
  1331. data = 0;
  1332. break;
  1333. case 0xcd: /* fsb frequency */
  1334. data = 3;
  1335. break;
  1336. case MSR_IA32_APICBASE:
  1337. data = vcpu->apic_base;
  1338. break;
  1339. case MSR_IA32_MISC_ENABLE:
  1340. data = vcpu->ia32_misc_enable_msr;
  1341. break;
  1342. #ifdef CONFIG_X86_64
  1343. case MSR_EFER:
  1344. data = vcpu->shadow_efer;
  1345. break;
  1346. #endif
  1347. default:
  1348. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  1349. return 1;
  1350. }
  1351. *pdata = data;
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1355. /*
  1356. * Reads an msr value (of 'msr_index') into 'pdata'.
  1357. * Returns 0 on success, non-0 otherwise.
  1358. * Assumes vcpu_load() was already called.
  1359. */
  1360. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1361. {
  1362. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  1363. }
  1364. #ifdef CONFIG_X86_64
  1365. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1366. {
  1367. if (efer & EFER_RESERVED_BITS) {
  1368. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1369. efer);
  1370. inject_gp(vcpu);
  1371. return;
  1372. }
  1373. if (is_paging(vcpu)
  1374. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1375. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1376. inject_gp(vcpu);
  1377. return;
  1378. }
  1379. kvm_arch_ops->set_efer(vcpu, efer);
  1380. efer &= ~EFER_LMA;
  1381. efer |= vcpu->shadow_efer & EFER_LMA;
  1382. vcpu->shadow_efer = efer;
  1383. }
  1384. #endif
  1385. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1386. {
  1387. switch (msr) {
  1388. #ifdef CONFIG_X86_64
  1389. case MSR_EFER:
  1390. set_efer(vcpu, data);
  1391. break;
  1392. #endif
  1393. case MSR_IA32_MC0_STATUS:
  1394. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1395. __FUNCTION__, data);
  1396. break;
  1397. case MSR_IA32_MCG_STATUS:
  1398. printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  1399. __FUNCTION__, data);
  1400. break;
  1401. case MSR_IA32_UCODE_REV:
  1402. case MSR_IA32_UCODE_WRITE:
  1403. case 0x200 ... 0x2ff: /* MTRRs */
  1404. break;
  1405. case MSR_IA32_APICBASE:
  1406. vcpu->apic_base = data;
  1407. break;
  1408. case MSR_IA32_MISC_ENABLE:
  1409. vcpu->ia32_misc_enable_msr = data;
  1410. break;
  1411. /*
  1412. * This is the 'probe whether the host is KVM' logic:
  1413. */
  1414. case MSR_KVM_API_MAGIC:
  1415. return vcpu_register_para(vcpu, data);
  1416. default:
  1417. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1418. return 1;
  1419. }
  1420. return 0;
  1421. }
  1422. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1423. /*
  1424. * Writes msr value into into the appropriate "register".
  1425. * Returns 0 on success, non-0 otherwise.
  1426. * Assumes vcpu_load() was already called.
  1427. */
  1428. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1429. {
  1430. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1431. }
  1432. void kvm_resched(struct kvm_vcpu *vcpu)
  1433. {
  1434. if (!need_resched())
  1435. return;
  1436. cond_resched();
  1437. }
  1438. EXPORT_SYMBOL_GPL(kvm_resched);
  1439. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1440. {
  1441. int i;
  1442. u32 function;
  1443. struct kvm_cpuid_entry *e, *best;
  1444. kvm_arch_ops->cache_regs(vcpu);
  1445. function = vcpu->regs[VCPU_REGS_RAX];
  1446. vcpu->regs[VCPU_REGS_RAX] = 0;
  1447. vcpu->regs[VCPU_REGS_RBX] = 0;
  1448. vcpu->regs[VCPU_REGS_RCX] = 0;
  1449. vcpu->regs[VCPU_REGS_RDX] = 0;
  1450. best = NULL;
  1451. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  1452. e = &vcpu->cpuid_entries[i];
  1453. if (e->function == function) {
  1454. best = e;
  1455. break;
  1456. }
  1457. /*
  1458. * Both basic or both extended?
  1459. */
  1460. if (((e->function ^ function) & 0x80000000) == 0)
  1461. if (!best || e->function > best->function)
  1462. best = e;
  1463. }
  1464. if (best) {
  1465. vcpu->regs[VCPU_REGS_RAX] = best->eax;
  1466. vcpu->regs[VCPU_REGS_RBX] = best->ebx;
  1467. vcpu->regs[VCPU_REGS_RCX] = best->ecx;
  1468. vcpu->regs[VCPU_REGS_RDX] = best->edx;
  1469. }
  1470. kvm_arch_ops->decache_regs(vcpu);
  1471. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1472. }
  1473. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  1474. static int pio_copy_data(struct kvm_vcpu *vcpu)
  1475. {
  1476. void *p = vcpu->pio_data;
  1477. void *q;
  1478. unsigned bytes;
  1479. int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
  1480. q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  1481. PAGE_KERNEL);
  1482. if (!q) {
  1483. free_pio_guest_pages(vcpu);
  1484. return -ENOMEM;
  1485. }
  1486. q += vcpu->pio.guest_page_offset;
  1487. bytes = vcpu->pio.size * vcpu->pio.cur_count;
  1488. if (vcpu->pio.in)
  1489. memcpy(q, p, bytes);
  1490. else
  1491. memcpy(p, q, bytes);
  1492. q -= vcpu->pio.guest_page_offset;
  1493. vunmap(q);
  1494. free_pio_guest_pages(vcpu);
  1495. return 0;
  1496. }
  1497. static int complete_pio(struct kvm_vcpu *vcpu)
  1498. {
  1499. struct kvm_pio_request *io = &vcpu->pio;
  1500. long delta;
  1501. int r;
  1502. kvm_arch_ops->cache_regs(vcpu);
  1503. if (!io->string) {
  1504. if (io->in)
  1505. memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
  1506. io->size);
  1507. } else {
  1508. if (io->in) {
  1509. r = pio_copy_data(vcpu);
  1510. if (r) {
  1511. kvm_arch_ops->cache_regs(vcpu);
  1512. return r;
  1513. }
  1514. }
  1515. delta = 1;
  1516. if (io->rep) {
  1517. delta *= io->cur_count;
  1518. /*
  1519. * The size of the register should really depend on
  1520. * current address size.
  1521. */
  1522. vcpu->regs[VCPU_REGS_RCX] -= delta;
  1523. }
  1524. if (io->down)
  1525. delta = -delta;
  1526. delta *= io->size;
  1527. if (io->in)
  1528. vcpu->regs[VCPU_REGS_RDI] += delta;
  1529. else
  1530. vcpu->regs[VCPU_REGS_RSI] += delta;
  1531. }
  1532. kvm_arch_ops->decache_regs(vcpu);
  1533. io->count -= io->cur_count;
  1534. io->cur_count = 0;
  1535. if (!io->count)
  1536. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1537. return 0;
  1538. }
  1539. static void kernel_pio(struct kvm_io_device *pio_dev,
  1540. struct kvm_vcpu *vcpu,
  1541. void *pd)
  1542. {
  1543. /* TODO: String I/O for in kernel device */
  1544. if (vcpu->pio.in)
  1545. kvm_iodevice_read(pio_dev, vcpu->pio.port,
  1546. vcpu->pio.size,
  1547. pd);
  1548. else
  1549. kvm_iodevice_write(pio_dev, vcpu->pio.port,
  1550. vcpu->pio.size,
  1551. pd);
  1552. }
  1553. static void pio_string_write(struct kvm_io_device *pio_dev,
  1554. struct kvm_vcpu *vcpu)
  1555. {
  1556. struct kvm_pio_request *io = &vcpu->pio;
  1557. void *pd = vcpu->pio_data;
  1558. int i;
  1559. for (i = 0; i < io->cur_count; i++) {
  1560. kvm_iodevice_write(pio_dev, io->port,
  1561. io->size,
  1562. pd);
  1563. pd += io->size;
  1564. }
  1565. }
  1566. int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1567. int size, unsigned long count, int string, int down,
  1568. gva_t address, int rep, unsigned port)
  1569. {
  1570. unsigned now, in_page;
  1571. int i, ret = 0;
  1572. int nr_pages = 1;
  1573. struct page *page;
  1574. struct kvm_io_device *pio_dev;
  1575. vcpu->run->exit_reason = KVM_EXIT_IO;
  1576. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1577. vcpu->run->io.size = size;
  1578. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1579. vcpu->run->io.count = count;
  1580. vcpu->run->io.port = port;
  1581. vcpu->pio.count = count;
  1582. vcpu->pio.cur_count = count;
  1583. vcpu->pio.size = size;
  1584. vcpu->pio.in = in;
  1585. vcpu->pio.port = port;
  1586. vcpu->pio.string = string;
  1587. vcpu->pio.down = down;
  1588. vcpu->pio.guest_page_offset = offset_in_page(address);
  1589. vcpu->pio.rep = rep;
  1590. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1591. if (!string) {
  1592. kvm_arch_ops->cache_regs(vcpu);
  1593. memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
  1594. kvm_arch_ops->decache_regs(vcpu);
  1595. if (pio_dev) {
  1596. kernel_pio(pio_dev, vcpu, vcpu->pio_data);
  1597. complete_pio(vcpu);
  1598. return 1;
  1599. }
  1600. return 0;
  1601. }
  1602. if (!count) {
  1603. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1604. return 1;
  1605. }
  1606. now = min(count, PAGE_SIZE / size);
  1607. if (!down)
  1608. in_page = PAGE_SIZE - offset_in_page(address);
  1609. else
  1610. in_page = offset_in_page(address) + size;
  1611. now = min(count, (unsigned long)in_page / size);
  1612. if (!now) {
  1613. /*
  1614. * String I/O straddles page boundary. Pin two guest pages
  1615. * so that we satisfy atomicity constraints. Do just one
  1616. * transaction to avoid complexity.
  1617. */
  1618. nr_pages = 2;
  1619. now = 1;
  1620. }
  1621. if (down) {
  1622. /*
  1623. * String I/O in reverse. Yuck. Kill the guest, fix later.
  1624. */
  1625. printk(KERN_ERR "kvm: guest string pio down\n");
  1626. inject_gp(vcpu);
  1627. return 1;
  1628. }
  1629. vcpu->run->io.count = now;
  1630. vcpu->pio.cur_count = now;
  1631. for (i = 0; i < nr_pages; ++i) {
  1632. mutex_lock(&vcpu->kvm->lock);
  1633. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  1634. if (page)
  1635. get_page(page);
  1636. vcpu->pio.guest_pages[i] = page;
  1637. mutex_unlock(&vcpu->kvm->lock);
  1638. if (!page) {
  1639. inject_gp(vcpu);
  1640. free_pio_guest_pages(vcpu);
  1641. return 1;
  1642. }
  1643. }
  1644. if (!vcpu->pio.in) {
  1645. /* string PIO write */
  1646. ret = pio_copy_data(vcpu);
  1647. if (ret >= 0 && pio_dev) {
  1648. pio_string_write(pio_dev, vcpu);
  1649. complete_pio(vcpu);
  1650. if (vcpu->pio.count == 0)
  1651. ret = 1;
  1652. }
  1653. } else if (pio_dev)
  1654. printk(KERN_ERR "no string pio read support yet, "
  1655. "port %x size %d count %ld\n",
  1656. port, size, count);
  1657. return ret;
  1658. }
  1659. EXPORT_SYMBOL_GPL(kvm_setup_pio);
  1660. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1661. {
  1662. int r;
  1663. sigset_t sigsaved;
  1664. vcpu_load(vcpu);
  1665. if (vcpu->sigset_active)
  1666. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1667. /* re-sync apic's tpr */
  1668. vcpu->cr8 = kvm_run->cr8;
  1669. if (vcpu->pio.cur_count) {
  1670. r = complete_pio(vcpu);
  1671. if (r)
  1672. goto out;
  1673. }
  1674. if (vcpu->mmio_needed) {
  1675. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1676. vcpu->mmio_read_completed = 1;
  1677. vcpu->mmio_needed = 0;
  1678. r = emulate_instruction(vcpu, kvm_run,
  1679. vcpu->mmio_fault_cr2, 0);
  1680. if (r == EMULATE_DO_MMIO) {
  1681. /*
  1682. * Read-modify-write. Back to userspace.
  1683. */
  1684. r = 0;
  1685. goto out;
  1686. }
  1687. }
  1688. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1689. kvm_arch_ops->cache_regs(vcpu);
  1690. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1691. kvm_arch_ops->decache_regs(vcpu);
  1692. }
  1693. r = kvm_arch_ops->run(vcpu, kvm_run);
  1694. out:
  1695. if (vcpu->sigset_active)
  1696. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1697. vcpu_put(vcpu);
  1698. return r;
  1699. }
  1700. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1701. struct kvm_regs *regs)
  1702. {
  1703. vcpu_load(vcpu);
  1704. kvm_arch_ops->cache_regs(vcpu);
  1705. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1706. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1707. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1708. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1709. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1710. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1711. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1712. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1713. #ifdef CONFIG_X86_64
  1714. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1715. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1716. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1717. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1718. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1719. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1720. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1721. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1722. #endif
  1723. regs->rip = vcpu->rip;
  1724. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1725. /*
  1726. * Don't leak debug flags in case they were set for guest debugging
  1727. */
  1728. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1729. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1730. vcpu_put(vcpu);
  1731. return 0;
  1732. }
  1733. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1734. struct kvm_regs *regs)
  1735. {
  1736. vcpu_load(vcpu);
  1737. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1738. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1739. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1740. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1741. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1742. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1743. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1744. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1745. #ifdef CONFIG_X86_64
  1746. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1747. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1748. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1749. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1750. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1751. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1752. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1753. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1754. #endif
  1755. vcpu->rip = regs->rip;
  1756. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1757. kvm_arch_ops->decache_regs(vcpu);
  1758. vcpu_put(vcpu);
  1759. return 0;
  1760. }
  1761. static void get_segment(struct kvm_vcpu *vcpu,
  1762. struct kvm_segment *var, int seg)
  1763. {
  1764. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1765. }
  1766. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1767. struct kvm_sregs *sregs)
  1768. {
  1769. struct descriptor_table dt;
  1770. vcpu_load(vcpu);
  1771. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1772. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1773. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1774. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1775. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1776. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1777. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1778. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1779. kvm_arch_ops->get_idt(vcpu, &dt);
  1780. sregs->idt.limit = dt.limit;
  1781. sregs->idt.base = dt.base;
  1782. kvm_arch_ops->get_gdt(vcpu, &dt);
  1783. sregs->gdt.limit = dt.limit;
  1784. sregs->gdt.base = dt.base;
  1785. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1786. sregs->cr0 = vcpu->cr0;
  1787. sregs->cr2 = vcpu->cr2;
  1788. sregs->cr3 = vcpu->cr3;
  1789. sregs->cr4 = vcpu->cr4;
  1790. sregs->cr8 = vcpu->cr8;
  1791. sregs->efer = vcpu->shadow_efer;
  1792. sregs->apic_base = vcpu->apic_base;
  1793. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1794. sizeof sregs->interrupt_bitmap);
  1795. vcpu_put(vcpu);
  1796. return 0;
  1797. }
  1798. static void set_segment(struct kvm_vcpu *vcpu,
  1799. struct kvm_segment *var, int seg)
  1800. {
  1801. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1802. }
  1803. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1804. struct kvm_sregs *sregs)
  1805. {
  1806. int mmu_reset_needed = 0;
  1807. int i;
  1808. struct descriptor_table dt;
  1809. vcpu_load(vcpu);
  1810. dt.limit = sregs->idt.limit;
  1811. dt.base = sregs->idt.base;
  1812. kvm_arch_ops->set_idt(vcpu, &dt);
  1813. dt.limit = sregs->gdt.limit;
  1814. dt.base = sregs->gdt.base;
  1815. kvm_arch_ops->set_gdt(vcpu, &dt);
  1816. vcpu->cr2 = sregs->cr2;
  1817. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1818. vcpu->cr3 = sregs->cr3;
  1819. vcpu->cr8 = sregs->cr8;
  1820. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1821. #ifdef CONFIG_X86_64
  1822. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1823. #endif
  1824. vcpu->apic_base = sregs->apic_base;
  1825. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1826. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1827. kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
  1828. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1829. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1830. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1831. load_pdptrs(vcpu, vcpu->cr3);
  1832. if (mmu_reset_needed)
  1833. kvm_mmu_reset_context(vcpu);
  1834. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1835. sizeof vcpu->irq_pending);
  1836. vcpu->irq_summary = 0;
  1837. for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
  1838. if (vcpu->irq_pending[i])
  1839. __set_bit(i, &vcpu->irq_summary);
  1840. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1841. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1842. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1843. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1844. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1845. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1846. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1847. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1848. vcpu_put(vcpu);
  1849. return 0;
  1850. }
  1851. /*
  1852. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1853. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1854. *
  1855. * This list is modified at module load time to reflect the
  1856. * capabilities of the host cpu.
  1857. */
  1858. static u32 msrs_to_save[] = {
  1859. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1860. MSR_K6_STAR,
  1861. #ifdef CONFIG_X86_64
  1862. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1863. #endif
  1864. MSR_IA32_TIME_STAMP_COUNTER,
  1865. };
  1866. static unsigned num_msrs_to_save;
  1867. static u32 emulated_msrs[] = {
  1868. MSR_IA32_MISC_ENABLE,
  1869. };
  1870. static __init void kvm_init_msr_list(void)
  1871. {
  1872. u32 dummy[2];
  1873. unsigned i, j;
  1874. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1875. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1876. continue;
  1877. if (j < i)
  1878. msrs_to_save[j] = msrs_to_save[i];
  1879. j++;
  1880. }
  1881. num_msrs_to_save = j;
  1882. }
  1883. /*
  1884. * Adapt set_msr() to msr_io()'s calling convention
  1885. */
  1886. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1887. {
  1888. return kvm_set_msr(vcpu, index, *data);
  1889. }
  1890. /*
  1891. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1892. *
  1893. * @return number of msrs set successfully.
  1894. */
  1895. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1896. struct kvm_msr_entry *entries,
  1897. int (*do_msr)(struct kvm_vcpu *vcpu,
  1898. unsigned index, u64 *data))
  1899. {
  1900. int i;
  1901. vcpu_load(vcpu);
  1902. for (i = 0; i < msrs->nmsrs; ++i)
  1903. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1904. break;
  1905. vcpu_put(vcpu);
  1906. return i;
  1907. }
  1908. /*
  1909. * Read or write a bunch of msrs. Parameters are user addresses.
  1910. *
  1911. * @return number of msrs set successfully.
  1912. */
  1913. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1914. int (*do_msr)(struct kvm_vcpu *vcpu,
  1915. unsigned index, u64 *data),
  1916. int writeback)
  1917. {
  1918. struct kvm_msrs msrs;
  1919. struct kvm_msr_entry *entries;
  1920. int r, n;
  1921. unsigned size;
  1922. r = -EFAULT;
  1923. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1924. goto out;
  1925. r = -E2BIG;
  1926. if (msrs.nmsrs >= MAX_IO_MSRS)
  1927. goto out;
  1928. r = -ENOMEM;
  1929. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1930. entries = vmalloc(size);
  1931. if (!entries)
  1932. goto out;
  1933. r = -EFAULT;
  1934. if (copy_from_user(entries, user_msrs->entries, size))
  1935. goto out_free;
  1936. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1937. if (r < 0)
  1938. goto out_free;
  1939. r = -EFAULT;
  1940. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1941. goto out_free;
  1942. r = n;
  1943. out_free:
  1944. vfree(entries);
  1945. out:
  1946. return r;
  1947. }
  1948. /*
  1949. * Translate a guest virtual address to a guest physical address.
  1950. */
  1951. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1952. struct kvm_translation *tr)
  1953. {
  1954. unsigned long vaddr = tr->linear_address;
  1955. gpa_t gpa;
  1956. vcpu_load(vcpu);
  1957. mutex_lock(&vcpu->kvm->lock);
  1958. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1959. tr->physical_address = gpa;
  1960. tr->valid = gpa != UNMAPPED_GVA;
  1961. tr->writeable = 1;
  1962. tr->usermode = 0;
  1963. mutex_unlock(&vcpu->kvm->lock);
  1964. vcpu_put(vcpu);
  1965. return 0;
  1966. }
  1967. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1968. struct kvm_interrupt *irq)
  1969. {
  1970. if (irq->irq < 0 || irq->irq >= 256)
  1971. return -EINVAL;
  1972. vcpu_load(vcpu);
  1973. set_bit(irq->irq, vcpu->irq_pending);
  1974. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1975. vcpu_put(vcpu);
  1976. return 0;
  1977. }
  1978. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  1979. struct kvm_debug_guest *dbg)
  1980. {
  1981. int r;
  1982. vcpu_load(vcpu);
  1983. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1984. vcpu_put(vcpu);
  1985. return r;
  1986. }
  1987. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  1988. unsigned long address,
  1989. int *type)
  1990. {
  1991. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1992. unsigned long pgoff;
  1993. struct page *page;
  1994. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1995. if (pgoff == 0)
  1996. page = virt_to_page(vcpu->run);
  1997. else if (pgoff == KVM_PIO_PAGE_OFFSET)
  1998. page = virt_to_page(vcpu->pio_data);
  1999. else
  2000. return NOPAGE_SIGBUS;
  2001. get_page(page);
  2002. if (type != NULL)
  2003. *type = VM_FAULT_MINOR;
  2004. return page;
  2005. }
  2006. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  2007. .nopage = kvm_vcpu_nopage,
  2008. };
  2009. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  2010. {
  2011. vma->vm_ops = &kvm_vcpu_vm_ops;
  2012. return 0;
  2013. }
  2014. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  2015. {
  2016. struct kvm_vcpu *vcpu = filp->private_data;
  2017. fput(vcpu->kvm->filp);
  2018. return 0;
  2019. }
  2020. static struct file_operations kvm_vcpu_fops = {
  2021. .release = kvm_vcpu_release,
  2022. .unlocked_ioctl = kvm_vcpu_ioctl,
  2023. .compat_ioctl = kvm_vcpu_ioctl,
  2024. .mmap = kvm_vcpu_mmap,
  2025. };
  2026. /*
  2027. * Allocates an inode for the vcpu.
  2028. */
  2029. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  2030. {
  2031. int fd, r;
  2032. struct inode *inode;
  2033. struct file *file;
  2034. r = anon_inode_getfd(&fd, &inode, &file,
  2035. "kvm-vcpu", &kvm_vcpu_fops, vcpu);
  2036. if (r)
  2037. return r;
  2038. atomic_inc(&vcpu->kvm->filp->f_count);
  2039. return fd;
  2040. }
  2041. /*
  2042. * Creates some virtual cpus. Good luck creating more than one.
  2043. */
  2044. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  2045. {
  2046. int r;
  2047. struct kvm_vcpu *vcpu;
  2048. if (!valid_vcpu(n))
  2049. return -EINVAL;
  2050. vcpu = kvm_arch_ops->vcpu_create(kvm, n);
  2051. if (IS_ERR(vcpu))
  2052. return PTR_ERR(vcpu);
  2053. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  2054. vcpu_load(vcpu);
  2055. r = kvm_mmu_setup(vcpu);
  2056. vcpu_put(vcpu);
  2057. if (r < 0)
  2058. goto free_vcpu;
  2059. mutex_lock(&kvm->lock);
  2060. if (kvm->vcpus[n]) {
  2061. r = -EEXIST;
  2062. mutex_unlock(&kvm->lock);
  2063. goto mmu_unload;
  2064. }
  2065. kvm->vcpus[n] = vcpu;
  2066. mutex_unlock(&kvm->lock);
  2067. /* Now it's all set up, let userspace reach it */
  2068. r = create_vcpu_fd(vcpu);
  2069. if (r < 0)
  2070. goto unlink;
  2071. return r;
  2072. unlink:
  2073. mutex_lock(&kvm->lock);
  2074. kvm->vcpus[n] = NULL;
  2075. mutex_unlock(&kvm->lock);
  2076. mmu_unload:
  2077. vcpu_load(vcpu);
  2078. kvm_mmu_unload(vcpu);
  2079. vcpu_put(vcpu);
  2080. free_vcpu:
  2081. kvm_arch_ops->vcpu_free(vcpu);
  2082. return r;
  2083. }
  2084. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  2085. {
  2086. u64 efer;
  2087. int i;
  2088. struct kvm_cpuid_entry *e, *entry;
  2089. rdmsrl(MSR_EFER, efer);
  2090. entry = NULL;
  2091. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  2092. e = &vcpu->cpuid_entries[i];
  2093. if (e->function == 0x80000001) {
  2094. entry = e;
  2095. break;
  2096. }
  2097. }
  2098. if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
  2099. entry->edx &= ~(1 << 20);
  2100. printk(KERN_INFO "kvm: guest NX capability removed\n");
  2101. }
  2102. }
  2103. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  2104. struct kvm_cpuid *cpuid,
  2105. struct kvm_cpuid_entry __user *entries)
  2106. {
  2107. int r;
  2108. r = -E2BIG;
  2109. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  2110. goto out;
  2111. r = -EFAULT;
  2112. if (copy_from_user(&vcpu->cpuid_entries, entries,
  2113. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  2114. goto out;
  2115. vcpu->cpuid_nent = cpuid->nent;
  2116. cpuid_fix_nx_cap(vcpu);
  2117. return 0;
  2118. out:
  2119. return r;
  2120. }
  2121. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2122. {
  2123. if (sigset) {
  2124. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2125. vcpu->sigset_active = 1;
  2126. vcpu->sigset = *sigset;
  2127. } else
  2128. vcpu->sigset_active = 0;
  2129. return 0;
  2130. }
  2131. /*
  2132. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2133. * we have asm/x86/processor.h
  2134. */
  2135. struct fxsave {
  2136. u16 cwd;
  2137. u16 swd;
  2138. u16 twd;
  2139. u16 fop;
  2140. u64 rip;
  2141. u64 rdp;
  2142. u32 mxcsr;
  2143. u32 mxcsr_mask;
  2144. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2145. #ifdef CONFIG_X86_64
  2146. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2147. #else
  2148. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2149. #endif
  2150. };
  2151. static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2152. {
  2153. struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
  2154. vcpu_load(vcpu);
  2155. memcpy(fpu->fpr, fxsave->st_space, 128);
  2156. fpu->fcw = fxsave->cwd;
  2157. fpu->fsw = fxsave->swd;
  2158. fpu->ftwx = fxsave->twd;
  2159. fpu->last_opcode = fxsave->fop;
  2160. fpu->last_ip = fxsave->rip;
  2161. fpu->last_dp = fxsave->rdp;
  2162. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2163. vcpu_put(vcpu);
  2164. return 0;
  2165. }
  2166. static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2167. {
  2168. struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
  2169. vcpu_load(vcpu);
  2170. memcpy(fxsave->st_space, fpu->fpr, 128);
  2171. fxsave->cwd = fpu->fcw;
  2172. fxsave->swd = fpu->fsw;
  2173. fxsave->twd = fpu->ftwx;
  2174. fxsave->fop = fpu->last_opcode;
  2175. fxsave->rip = fpu->last_ip;
  2176. fxsave->rdp = fpu->last_dp;
  2177. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2178. vcpu_put(vcpu);
  2179. return 0;
  2180. }
  2181. static long kvm_vcpu_ioctl(struct file *filp,
  2182. unsigned int ioctl, unsigned long arg)
  2183. {
  2184. struct kvm_vcpu *vcpu = filp->private_data;
  2185. void __user *argp = (void __user *)arg;
  2186. int r = -EINVAL;
  2187. switch (ioctl) {
  2188. case KVM_RUN:
  2189. r = -EINVAL;
  2190. if (arg)
  2191. goto out;
  2192. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  2193. break;
  2194. case KVM_GET_REGS: {
  2195. struct kvm_regs kvm_regs;
  2196. memset(&kvm_regs, 0, sizeof kvm_regs);
  2197. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  2198. if (r)
  2199. goto out;
  2200. r = -EFAULT;
  2201. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  2202. goto out;
  2203. r = 0;
  2204. break;
  2205. }
  2206. case KVM_SET_REGS: {
  2207. struct kvm_regs kvm_regs;
  2208. r = -EFAULT;
  2209. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  2210. goto out;
  2211. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  2212. if (r)
  2213. goto out;
  2214. r = 0;
  2215. break;
  2216. }
  2217. case KVM_GET_SREGS: {
  2218. struct kvm_sregs kvm_sregs;
  2219. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  2220. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  2221. if (r)
  2222. goto out;
  2223. r = -EFAULT;
  2224. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  2225. goto out;
  2226. r = 0;
  2227. break;
  2228. }
  2229. case KVM_SET_SREGS: {
  2230. struct kvm_sregs kvm_sregs;
  2231. r = -EFAULT;
  2232. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  2233. goto out;
  2234. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  2235. if (r)
  2236. goto out;
  2237. r = 0;
  2238. break;
  2239. }
  2240. case KVM_TRANSLATE: {
  2241. struct kvm_translation tr;
  2242. r = -EFAULT;
  2243. if (copy_from_user(&tr, argp, sizeof tr))
  2244. goto out;
  2245. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  2246. if (r)
  2247. goto out;
  2248. r = -EFAULT;
  2249. if (copy_to_user(argp, &tr, sizeof tr))
  2250. goto out;
  2251. r = 0;
  2252. break;
  2253. }
  2254. case KVM_INTERRUPT: {
  2255. struct kvm_interrupt irq;
  2256. r = -EFAULT;
  2257. if (copy_from_user(&irq, argp, sizeof irq))
  2258. goto out;
  2259. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2260. if (r)
  2261. goto out;
  2262. r = 0;
  2263. break;
  2264. }
  2265. case KVM_DEBUG_GUEST: {
  2266. struct kvm_debug_guest dbg;
  2267. r = -EFAULT;
  2268. if (copy_from_user(&dbg, argp, sizeof dbg))
  2269. goto out;
  2270. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  2271. if (r)
  2272. goto out;
  2273. r = 0;
  2274. break;
  2275. }
  2276. case KVM_GET_MSRS:
  2277. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2278. break;
  2279. case KVM_SET_MSRS:
  2280. r = msr_io(vcpu, argp, do_set_msr, 0);
  2281. break;
  2282. case KVM_SET_CPUID: {
  2283. struct kvm_cpuid __user *cpuid_arg = argp;
  2284. struct kvm_cpuid cpuid;
  2285. r = -EFAULT;
  2286. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2287. goto out;
  2288. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2289. if (r)
  2290. goto out;
  2291. break;
  2292. }
  2293. case KVM_SET_SIGNAL_MASK: {
  2294. struct kvm_signal_mask __user *sigmask_arg = argp;
  2295. struct kvm_signal_mask kvm_sigmask;
  2296. sigset_t sigset, *p;
  2297. p = NULL;
  2298. if (argp) {
  2299. r = -EFAULT;
  2300. if (copy_from_user(&kvm_sigmask, argp,
  2301. sizeof kvm_sigmask))
  2302. goto out;
  2303. r = -EINVAL;
  2304. if (kvm_sigmask.len != sizeof sigset)
  2305. goto out;
  2306. r = -EFAULT;
  2307. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2308. sizeof sigset))
  2309. goto out;
  2310. p = &sigset;
  2311. }
  2312. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2313. break;
  2314. }
  2315. case KVM_GET_FPU: {
  2316. struct kvm_fpu fpu;
  2317. memset(&fpu, 0, sizeof fpu);
  2318. r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
  2319. if (r)
  2320. goto out;
  2321. r = -EFAULT;
  2322. if (copy_to_user(argp, &fpu, sizeof fpu))
  2323. goto out;
  2324. r = 0;
  2325. break;
  2326. }
  2327. case KVM_SET_FPU: {
  2328. struct kvm_fpu fpu;
  2329. r = -EFAULT;
  2330. if (copy_from_user(&fpu, argp, sizeof fpu))
  2331. goto out;
  2332. r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
  2333. if (r)
  2334. goto out;
  2335. r = 0;
  2336. break;
  2337. }
  2338. default:
  2339. ;
  2340. }
  2341. out:
  2342. return r;
  2343. }
  2344. static long kvm_vm_ioctl(struct file *filp,
  2345. unsigned int ioctl, unsigned long arg)
  2346. {
  2347. struct kvm *kvm = filp->private_data;
  2348. void __user *argp = (void __user *)arg;
  2349. int r = -EINVAL;
  2350. switch (ioctl) {
  2351. case KVM_CREATE_VCPU:
  2352. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2353. if (r < 0)
  2354. goto out;
  2355. break;
  2356. case KVM_SET_MEMORY_REGION: {
  2357. struct kvm_memory_region kvm_mem;
  2358. r = -EFAULT;
  2359. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2360. goto out;
  2361. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  2362. if (r)
  2363. goto out;
  2364. break;
  2365. }
  2366. case KVM_GET_DIRTY_LOG: {
  2367. struct kvm_dirty_log log;
  2368. r = -EFAULT;
  2369. if (copy_from_user(&log, argp, sizeof log))
  2370. goto out;
  2371. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2372. if (r)
  2373. goto out;
  2374. break;
  2375. }
  2376. case KVM_SET_MEMORY_ALIAS: {
  2377. struct kvm_memory_alias alias;
  2378. r = -EFAULT;
  2379. if (copy_from_user(&alias, argp, sizeof alias))
  2380. goto out;
  2381. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  2382. if (r)
  2383. goto out;
  2384. break;
  2385. }
  2386. default:
  2387. ;
  2388. }
  2389. out:
  2390. return r;
  2391. }
  2392. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  2393. unsigned long address,
  2394. int *type)
  2395. {
  2396. struct kvm *kvm = vma->vm_file->private_data;
  2397. unsigned long pgoff;
  2398. struct page *page;
  2399. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2400. page = gfn_to_page(kvm, pgoff);
  2401. if (!page)
  2402. return NOPAGE_SIGBUS;
  2403. get_page(page);
  2404. if (type != NULL)
  2405. *type = VM_FAULT_MINOR;
  2406. return page;
  2407. }
  2408. static struct vm_operations_struct kvm_vm_vm_ops = {
  2409. .nopage = kvm_vm_nopage,
  2410. };
  2411. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2412. {
  2413. vma->vm_ops = &kvm_vm_vm_ops;
  2414. return 0;
  2415. }
  2416. static struct file_operations kvm_vm_fops = {
  2417. .release = kvm_vm_release,
  2418. .unlocked_ioctl = kvm_vm_ioctl,
  2419. .compat_ioctl = kvm_vm_ioctl,
  2420. .mmap = kvm_vm_mmap,
  2421. };
  2422. static int kvm_dev_ioctl_create_vm(void)
  2423. {
  2424. int fd, r;
  2425. struct inode *inode;
  2426. struct file *file;
  2427. struct kvm *kvm;
  2428. kvm = kvm_create_vm();
  2429. if (IS_ERR(kvm))
  2430. return PTR_ERR(kvm);
  2431. r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
  2432. if (r) {
  2433. kvm_destroy_vm(kvm);
  2434. return r;
  2435. }
  2436. kvm->filp = file;
  2437. return fd;
  2438. }
  2439. static long kvm_dev_ioctl(struct file *filp,
  2440. unsigned int ioctl, unsigned long arg)
  2441. {
  2442. void __user *argp = (void __user *)arg;
  2443. long r = -EINVAL;
  2444. switch (ioctl) {
  2445. case KVM_GET_API_VERSION:
  2446. r = -EINVAL;
  2447. if (arg)
  2448. goto out;
  2449. r = KVM_API_VERSION;
  2450. break;
  2451. case KVM_CREATE_VM:
  2452. r = -EINVAL;
  2453. if (arg)
  2454. goto out;
  2455. r = kvm_dev_ioctl_create_vm();
  2456. break;
  2457. case KVM_GET_MSR_INDEX_LIST: {
  2458. struct kvm_msr_list __user *user_msr_list = argp;
  2459. struct kvm_msr_list msr_list;
  2460. unsigned n;
  2461. r = -EFAULT;
  2462. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2463. goto out;
  2464. n = msr_list.nmsrs;
  2465. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2466. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2467. goto out;
  2468. r = -E2BIG;
  2469. if (n < num_msrs_to_save)
  2470. goto out;
  2471. r = -EFAULT;
  2472. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2473. num_msrs_to_save * sizeof(u32)))
  2474. goto out;
  2475. if (copy_to_user(user_msr_list->indices
  2476. + num_msrs_to_save * sizeof(u32),
  2477. &emulated_msrs,
  2478. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2479. goto out;
  2480. r = 0;
  2481. break;
  2482. }
  2483. case KVM_CHECK_EXTENSION:
  2484. /*
  2485. * No extensions defined at present.
  2486. */
  2487. r = 0;
  2488. break;
  2489. case KVM_GET_VCPU_MMAP_SIZE:
  2490. r = -EINVAL;
  2491. if (arg)
  2492. goto out;
  2493. r = 2 * PAGE_SIZE;
  2494. break;
  2495. default:
  2496. ;
  2497. }
  2498. out:
  2499. return r;
  2500. }
  2501. static struct file_operations kvm_chardev_ops = {
  2502. .open = kvm_dev_open,
  2503. .release = kvm_dev_release,
  2504. .unlocked_ioctl = kvm_dev_ioctl,
  2505. .compat_ioctl = kvm_dev_ioctl,
  2506. };
  2507. static struct miscdevice kvm_dev = {
  2508. KVM_MINOR,
  2509. "kvm",
  2510. &kvm_chardev_ops,
  2511. };
  2512. /*
  2513. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2514. * cached on it.
  2515. */
  2516. static void decache_vcpus_on_cpu(int cpu)
  2517. {
  2518. struct kvm *vm;
  2519. struct kvm_vcpu *vcpu;
  2520. int i;
  2521. spin_lock(&kvm_lock);
  2522. list_for_each_entry(vm, &vm_list, vm_list)
  2523. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2524. vcpu = vm->vcpus[i];
  2525. if (!vcpu)
  2526. continue;
  2527. /*
  2528. * If the vcpu is locked, then it is running on some
  2529. * other cpu and therefore it is not cached on the
  2530. * cpu in question.
  2531. *
  2532. * If it's not locked, check the last cpu it executed
  2533. * on.
  2534. */
  2535. if (mutex_trylock(&vcpu->mutex)) {
  2536. if (vcpu->cpu == cpu) {
  2537. kvm_arch_ops->vcpu_decache(vcpu);
  2538. vcpu->cpu = -1;
  2539. }
  2540. mutex_unlock(&vcpu->mutex);
  2541. }
  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);