kvm_main.c 71 KB

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