kvm_main.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  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 <linux/kvm.h>
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <asm/processor.h>
  22. #include <linux/percpu.h>
  23. #include <linux/gfp.h>
  24. #include <asm/msr.h>
  25. #include <linux/mm.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/reboot.h>
  30. #include <asm/io.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/highmem.h>
  33. #include <linux/file.h>
  34. #include <asm/desc.h>
  35. #include "x86_emulate.h"
  36. #include "segment_descriptor.h"
  37. MODULE_AUTHOR("Qumranet");
  38. MODULE_LICENSE("GPL");
  39. struct kvm_arch_ops *kvm_arch_ops;
  40. struct kvm_stat kvm_stat;
  41. EXPORT_SYMBOL_GPL(kvm_stat);
  42. static struct kvm_stats_debugfs_item {
  43. const char *name;
  44. u32 *data;
  45. struct dentry *dentry;
  46. } debugfs_entries[] = {
  47. { "pf_fixed", &kvm_stat.pf_fixed },
  48. { "pf_guest", &kvm_stat.pf_guest },
  49. { "tlb_flush", &kvm_stat.tlb_flush },
  50. { "invlpg", &kvm_stat.invlpg },
  51. { "exits", &kvm_stat.exits },
  52. { "io_exits", &kvm_stat.io_exits },
  53. { "mmio_exits", &kvm_stat.mmio_exits },
  54. { "signal_exits", &kvm_stat.signal_exits },
  55. { "irq_window", &kvm_stat.irq_window_exits },
  56. { "halt_exits", &kvm_stat.halt_exits },
  57. { "request_irq", &kvm_stat.request_irq_exits },
  58. { "irq_exits", &kvm_stat.irq_exits },
  59. { 0, 0 }
  60. };
  61. static struct dentry *debugfs_dir;
  62. #define MAX_IO_MSRS 256
  63. #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
  64. #define LMSW_GUEST_MASK 0x0eULL
  65. #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
  66. #define CR8_RESEVED_BITS (~0x0fULL)
  67. #define EFER_RESERVED_BITS 0xfffffffffffff2fe
  68. #ifdef CONFIG_X86_64
  69. // LDT or TSS descriptor in the GDT. 16 bytes.
  70. struct segment_descriptor_64 {
  71. struct segment_descriptor s;
  72. u32 base_higher;
  73. u32 pad_zero;
  74. };
  75. #endif
  76. unsigned long segment_base(u16 selector)
  77. {
  78. struct descriptor_table gdt;
  79. struct segment_descriptor *d;
  80. unsigned long table_base;
  81. typedef unsigned long ul;
  82. unsigned long v;
  83. if (selector == 0)
  84. return 0;
  85. asm ("sgdt %0" : "=m"(gdt));
  86. table_base = gdt.base;
  87. if (selector & 4) { /* from ldt */
  88. u16 ldt_selector;
  89. asm ("sldt %0" : "=g"(ldt_selector));
  90. table_base = segment_base(ldt_selector);
  91. }
  92. d = (struct segment_descriptor *)(table_base + (selector & ~7));
  93. v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
  94. #ifdef CONFIG_X86_64
  95. if (d->system == 0
  96. && (d->type == 2 || d->type == 9 || d->type == 11))
  97. v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
  98. #endif
  99. return v;
  100. }
  101. EXPORT_SYMBOL_GPL(segment_base);
  102. static inline int valid_vcpu(int n)
  103. {
  104. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  105. }
  106. int kvm_read_guest(struct kvm_vcpu *vcpu,
  107. gva_t addr,
  108. unsigned long size,
  109. void *dest)
  110. {
  111. unsigned char *host_buf = dest;
  112. unsigned long req_size = size;
  113. while (size) {
  114. hpa_t paddr;
  115. unsigned now;
  116. unsigned offset;
  117. hva_t guest_buf;
  118. paddr = gva_to_hpa(vcpu, addr);
  119. if (is_error_hpa(paddr))
  120. break;
  121. guest_buf = (hva_t)kmap_atomic(
  122. pfn_to_page(paddr >> PAGE_SHIFT),
  123. KM_USER0);
  124. offset = addr & ~PAGE_MASK;
  125. guest_buf |= offset;
  126. now = min(size, PAGE_SIZE - offset);
  127. memcpy(host_buf, (void*)guest_buf, now);
  128. host_buf += now;
  129. addr += now;
  130. size -= now;
  131. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  132. }
  133. return req_size - size;
  134. }
  135. EXPORT_SYMBOL_GPL(kvm_read_guest);
  136. int kvm_write_guest(struct kvm_vcpu *vcpu,
  137. gva_t addr,
  138. unsigned long size,
  139. void *data)
  140. {
  141. unsigned char *host_buf = data;
  142. unsigned long req_size = size;
  143. while (size) {
  144. hpa_t paddr;
  145. unsigned now;
  146. unsigned offset;
  147. hva_t guest_buf;
  148. paddr = gva_to_hpa(vcpu, addr);
  149. if (is_error_hpa(paddr))
  150. break;
  151. guest_buf = (hva_t)kmap_atomic(
  152. pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
  153. offset = addr & ~PAGE_MASK;
  154. guest_buf |= offset;
  155. now = min(size, PAGE_SIZE - offset);
  156. memcpy((void*)guest_buf, host_buf, now);
  157. host_buf += now;
  158. addr += now;
  159. size -= now;
  160. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  161. }
  162. return req_size - size;
  163. }
  164. EXPORT_SYMBOL_GPL(kvm_write_guest);
  165. static int vcpu_slot(struct kvm_vcpu *vcpu)
  166. {
  167. return vcpu - vcpu->kvm->vcpus;
  168. }
  169. /*
  170. * Switches to specified vcpu, until a matching vcpu_put()
  171. */
  172. static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot)
  173. {
  174. struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot];
  175. mutex_lock(&vcpu->mutex);
  176. if (unlikely(!vcpu->vmcs)) {
  177. mutex_unlock(&vcpu->mutex);
  178. return 0;
  179. }
  180. return kvm_arch_ops->vcpu_load(vcpu);
  181. }
  182. static void vcpu_put(struct kvm_vcpu *vcpu)
  183. {
  184. kvm_arch_ops->vcpu_put(vcpu);
  185. mutex_unlock(&vcpu->mutex);
  186. }
  187. static int kvm_dev_open(struct inode *inode, struct file *filp)
  188. {
  189. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  190. int i;
  191. if (!kvm)
  192. return -ENOMEM;
  193. spin_lock_init(&kvm->lock);
  194. INIT_LIST_HEAD(&kvm->active_mmu_pages);
  195. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  196. struct kvm_vcpu *vcpu = &kvm->vcpus[i];
  197. mutex_init(&vcpu->mutex);
  198. vcpu->mmu.root_hpa = INVALID_PAGE;
  199. INIT_LIST_HEAD(&vcpu->free_pages);
  200. }
  201. filp->private_data = kvm;
  202. return 0;
  203. }
  204. /*
  205. * Free any memory in @free but not in @dont.
  206. */
  207. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  208. struct kvm_memory_slot *dont)
  209. {
  210. int i;
  211. if (!dont || free->phys_mem != dont->phys_mem)
  212. if (free->phys_mem) {
  213. for (i = 0; i < free->npages; ++i)
  214. if (free->phys_mem[i])
  215. __free_page(free->phys_mem[i]);
  216. vfree(free->phys_mem);
  217. }
  218. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  219. vfree(free->dirty_bitmap);
  220. free->phys_mem = 0;
  221. free->npages = 0;
  222. free->dirty_bitmap = 0;
  223. }
  224. static void kvm_free_physmem(struct kvm *kvm)
  225. {
  226. int i;
  227. for (i = 0; i < kvm->nmemslots; ++i)
  228. kvm_free_physmem_slot(&kvm->memslots[i], 0);
  229. }
  230. static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
  231. {
  232. kvm_arch_ops->vcpu_free(vcpu);
  233. kvm_mmu_destroy(vcpu);
  234. }
  235. static void kvm_free_vcpus(struct kvm *kvm)
  236. {
  237. unsigned int i;
  238. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  239. kvm_free_vcpu(&kvm->vcpus[i]);
  240. }
  241. static int kvm_dev_release(struct inode *inode, struct file *filp)
  242. {
  243. struct kvm *kvm = filp->private_data;
  244. kvm_free_vcpus(kvm);
  245. kvm_free_physmem(kvm);
  246. kfree(kvm);
  247. return 0;
  248. }
  249. static void inject_gp(struct kvm_vcpu *vcpu)
  250. {
  251. kvm_arch_ops->inject_gp(vcpu, 0);
  252. }
  253. /*
  254. * Load the pae pdptrs. Return true is they are all valid.
  255. */
  256. static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  257. {
  258. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  259. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  260. int i;
  261. u64 pdpte;
  262. u64 *pdpt;
  263. int ret;
  264. struct kvm_memory_slot *memslot;
  265. spin_lock(&vcpu->kvm->lock);
  266. memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
  267. /* FIXME: !memslot - emulate? 0xff? */
  268. pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
  269. ret = 1;
  270. for (i = 0; i < 4; ++i) {
  271. pdpte = pdpt[offset + i];
  272. if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
  273. ret = 0;
  274. goto out;
  275. }
  276. }
  277. for (i = 0; i < 4; ++i)
  278. vcpu->pdptrs[i] = pdpt[offset + i];
  279. out:
  280. kunmap_atomic(pdpt, KM_USER0);
  281. spin_unlock(&vcpu->kvm->lock);
  282. return ret;
  283. }
  284. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  285. {
  286. if (cr0 & CR0_RESEVED_BITS) {
  287. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  288. cr0, vcpu->cr0);
  289. inject_gp(vcpu);
  290. return;
  291. }
  292. if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
  293. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  294. inject_gp(vcpu);
  295. return;
  296. }
  297. if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
  298. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  299. "and a clear PE flag\n");
  300. inject_gp(vcpu);
  301. return;
  302. }
  303. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  304. #ifdef CONFIG_X86_64
  305. if ((vcpu->shadow_efer & EFER_LME)) {
  306. int cs_db, cs_l;
  307. if (!is_pae(vcpu)) {
  308. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  309. "in long mode while PAE is disabled\n");
  310. inject_gp(vcpu);
  311. return;
  312. }
  313. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  314. if (cs_l) {
  315. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  316. "in long mode while CS.L == 1\n");
  317. inject_gp(vcpu);
  318. return;
  319. }
  320. } else
  321. #endif
  322. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
  323. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  324. "reserved bits\n");
  325. inject_gp(vcpu);
  326. return;
  327. }
  328. }
  329. kvm_arch_ops->set_cr0(vcpu, cr0);
  330. vcpu->cr0 = cr0;
  331. spin_lock(&vcpu->kvm->lock);
  332. kvm_mmu_reset_context(vcpu);
  333. spin_unlock(&vcpu->kvm->lock);
  334. return;
  335. }
  336. EXPORT_SYMBOL_GPL(set_cr0);
  337. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  338. {
  339. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  340. set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
  341. }
  342. EXPORT_SYMBOL_GPL(lmsw);
  343. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  344. {
  345. if (cr4 & CR4_RESEVED_BITS) {
  346. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  347. inject_gp(vcpu);
  348. return;
  349. }
  350. if (is_long_mode(vcpu)) {
  351. if (!(cr4 & CR4_PAE_MASK)) {
  352. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  353. "in long mode\n");
  354. inject_gp(vcpu);
  355. return;
  356. }
  357. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
  358. && !load_pdptrs(vcpu, vcpu->cr3)) {
  359. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  360. inject_gp(vcpu);
  361. }
  362. if (cr4 & CR4_VMXE_MASK) {
  363. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  364. inject_gp(vcpu);
  365. return;
  366. }
  367. kvm_arch_ops->set_cr4(vcpu, cr4);
  368. spin_lock(&vcpu->kvm->lock);
  369. kvm_mmu_reset_context(vcpu);
  370. spin_unlock(&vcpu->kvm->lock);
  371. }
  372. EXPORT_SYMBOL_GPL(set_cr4);
  373. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  374. {
  375. if (is_long_mode(vcpu)) {
  376. if ( cr3 & CR3_L_MODE_RESEVED_BITS) {
  377. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  378. inject_gp(vcpu);
  379. return;
  380. }
  381. } else {
  382. if (cr3 & CR3_RESEVED_BITS) {
  383. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  384. inject_gp(vcpu);
  385. return;
  386. }
  387. if (is_paging(vcpu) && is_pae(vcpu) &&
  388. !load_pdptrs(vcpu, cr3)) {
  389. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  390. "reserved bits\n");
  391. inject_gp(vcpu);
  392. return;
  393. }
  394. }
  395. vcpu->cr3 = cr3;
  396. spin_lock(&vcpu->kvm->lock);
  397. vcpu->mmu.new_cr3(vcpu);
  398. spin_unlock(&vcpu->kvm->lock);
  399. }
  400. EXPORT_SYMBOL_GPL(set_cr3);
  401. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  402. {
  403. if ( cr8 & CR8_RESEVED_BITS) {
  404. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  405. inject_gp(vcpu);
  406. return;
  407. }
  408. vcpu->cr8 = cr8;
  409. }
  410. EXPORT_SYMBOL_GPL(set_cr8);
  411. void fx_init(struct kvm_vcpu *vcpu)
  412. {
  413. struct __attribute__ ((__packed__)) fx_image_s {
  414. u16 control; //fcw
  415. u16 status; //fsw
  416. u16 tag; // ftw
  417. u16 opcode; //fop
  418. u64 ip; // fpu ip
  419. u64 operand;// fpu dp
  420. u32 mxcsr;
  421. u32 mxcsr_mask;
  422. } *fx_image;
  423. fx_save(vcpu->host_fx_image);
  424. fpu_init();
  425. fx_save(vcpu->guest_fx_image);
  426. fx_restore(vcpu->host_fx_image);
  427. fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
  428. fx_image->mxcsr = 0x1f80;
  429. memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
  430. 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
  431. }
  432. EXPORT_SYMBOL_GPL(fx_init);
  433. /*
  434. * Creates some virtual cpus. Good luck creating more than one.
  435. */
  436. static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n)
  437. {
  438. int r;
  439. struct kvm_vcpu *vcpu;
  440. r = -EINVAL;
  441. if (!valid_vcpu(n))
  442. goto out;
  443. vcpu = &kvm->vcpus[n];
  444. mutex_lock(&vcpu->mutex);
  445. if (vcpu->vmcs) {
  446. mutex_unlock(&vcpu->mutex);
  447. return -EEXIST;
  448. }
  449. vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
  450. FX_IMAGE_ALIGN);
  451. vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
  452. vcpu->cpu = -1; /* First load will set up TR */
  453. vcpu->kvm = kvm;
  454. r = kvm_arch_ops->vcpu_create(vcpu);
  455. if (r < 0)
  456. goto out_free_vcpus;
  457. r = kvm_mmu_create(vcpu);
  458. if (r < 0)
  459. goto out_free_vcpus;
  460. kvm_arch_ops->vcpu_load(vcpu);
  461. r = kvm_mmu_setup(vcpu);
  462. if (r >= 0)
  463. r = kvm_arch_ops->vcpu_setup(vcpu);
  464. vcpu_put(vcpu);
  465. if (r < 0)
  466. goto out_free_vcpus;
  467. return 0;
  468. out_free_vcpus:
  469. kvm_free_vcpu(vcpu);
  470. mutex_unlock(&vcpu->mutex);
  471. out:
  472. return r;
  473. }
  474. /*
  475. * Allocate some memory and give it an address in the guest physical address
  476. * space.
  477. *
  478. * Discontiguous memory is allowed, mostly for framebuffers.
  479. */
  480. static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm,
  481. struct kvm_memory_region *mem)
  482. {
  483. int r;
  484. gfn_t base_gfn;
  485. unsigned long npages;
  486. unsigned long i;
  487. struct kvm_memory_slot *memslot;
  488. struct kvm_memory_slot old, new;
  489. int memory_config_version;
  490. r = -EINVAL;
  491. /* General sanity checks */
  492. if (mem->memory_size & (PAGE_SIZE - 1))
  493. goto out;
  494. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  495. goto out;
  496. if (mem->slot >= KVM_MEMORY_SLOTS)
  497. goto out;
  498. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  499. goto out;
  500. memslot = &kvm->memslots[mem->slot];
  501. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  502. npages = mem->memory_size >> PAGE_SHIFT;
  503. if (!npages)
  504. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  505. raced:
  506. spin_lock(&kvm->lock);
  507. memory_config_version = kvm->memory_config_version;
  508. new = old = *memslot;
  509. new.base_gfn = base_gfn;
  510. new.npages = npages;
  511. new.flags = mem->flags;
  512. /* Disallow changing a memory slot's size. */
  513. r = -EINVAL;
  514. if (npages && old.npages && npages != old.npages)
  515. goto out_unlock;
  516. /* Check for overlaps */
  517. r = -EEXIST;
  518. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  519. struct kvm_memory_slot *s = &kvm->memslots[i];
  520. if (s == memslot)
  521. continue;
  522. if (!((base_gfn + npages <= s->base_gfn) ||
  523. (base_gfn >= s->base_gfn + s->npages)))
  524. goto out_unlock;
  525. }
  526. /*
  527. * Do memory allocations outside lock. memory_config_version will
  528. * detect any races.
  529. */
  530. spin_unlock(&kvm->lock);
  531. /* Deallocate if slot is being removed */
  532. if (!npages)
  533. new.phys_mem = 0;
  534. /* Free page dirty bitmap if unneeded */
  535. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  536. new.dirty_bitmap = 0;
  537. r = -ENOMEM;
  538. /* Allocate if a slot is being created */
  539. if (npages && !new.phys_mem) {
  540. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  541. if (!new.phys_mem)
  542. goto out_free;
  543. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  544. for (i = 0; i < npages; ++i) {
  545. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  546. | __GFP_ZERO);
  547. if (!new.phys_mem[i])
  548. goto out_free;
  549. new.phys_mem[i]->private = 0;
  550. }
  551. }
  552. /* Allocate page dirty bitmap if needed */
  553. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  554. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  555. new.dirty_bitmap = vmalloc(dirty_bytes);
  556. if (!new.dirty_bitmap)
  557. goto out_free;
  558. memset(new.dirty_bitmap, 0, dirty_bytes);
  559. }
  560. spin_lock(&kvm->lock);
  561. if (memory_config_version != kvm->memory_config_version) {
  562. spin_unlock(&kvm->lock);
  563. kvm_free_physmem_slot(&new, &old);
  564. goto raced;
  565. }
  566. r = -EAGAIN;
  567. if (kvm->busy)
  568. goto out_unlock;
  569. if (mem->slot >= kvm->nmemslots)
  570. kvm->nmemslots = mem->slot + 1;
  571. *memslot = new;
  572. ++kvm->memory_config_version;
  573. spin_unlock(&kvm->lock);
  574. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  575. struct kvm_vcpu *vcpu;
  576. vcpu = vcpu_load(kvm, i);
  577. if (!vcpu)
  578. continue;
  579. kvm_mmu_reset_context(vcpu);
  580. vcpu_put(vcpu);
  581. }
  582. kvm_free_physmem_slot(&old, &new);
  583. return 0;
  584. out_unlock:
  585. spin_unlock(&kvm->lock);
  586. out_free:
  587. kvm_free_physmem_slot(&new, &old);
  588. out:
  589. return r;
  590. }
  591. /*
  592. * Get (and clear) the dirty memory log for a memory slot.
  593. */
  594. static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm,
  595. struct kvm_dirty_log *log)
  596. {
  597. struct kvm_memory_slot *memslot;
  598. int r, i;
  599. int n;
  600. unsigned long any = 0;
  601. spin_lock(&kvm->lock);
  602. /*
  603. * Prevent changes to guest memory configuration even while the lock
  604. * is not taken.
  605. */
  606. ++kvm->busy;
  607. spin_unlock(&kvm->lock);
  608. r = -EINVAL;
  609. if (log->slot >= KVM_MEMORY_SLOTS)
  610. goto out;
  611. memslot = &kvm->memslots[log->slot];
  612. r = -ENOENT;
  613. if (!memslot->dirty_bitmap)
  614. goto out;
  615. n = ALIGN(memslot->npages, 8) / 8;
  616. for (i = 0; !any && i < n; ++i)
  617. any = memslot->dirty_bitmap[i];
  618. r = -EFAULT;
  619. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  620. goto out;
  621. if (any) {
  622. spin_lock(&kvm->lock);
  623. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  624. spin_unlock(&kvm->lock);
  625. memset(memslot->dirty_bitmap, 0, n);
  626. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  627. struct kvm_vcpu *vcpu = vcpu_load(kvm, i);
  628. if (!vcpu)
  629. continue;
  630. kvm_arch_ops->tlb_flush(vcpu);
  631. vcpu_put(vcpu);
  632. }
  633. }
  634. r = 0;
  635. out:
  636. spin_lock(&kvm->lock);
  637. --kvm->busy;
  638. spin_unlock(&kvm->lock);
  639. return r;
  640. }
  641. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  642. {
  643. int i;
  644. for (i = 0; i < kvm->nmemslots; ++i) {
  645. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  646. if (gfn >= memslot->base_gfn
  647. && gfn < memslot->base_gfn + memslot->npages)
  648. return memslot;
  649. }
  650. return 0;
  651. }
  652. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  653. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  654. {
  655. int i;
  656. struct kvm_memory_slot *memslot = 0;
  657. unsigned long rel_gfn;
  658. for (i = 0; i < kvm->nmemslots; ++i) {
  659. memslot = &kvm->memslots[i];
  660. if (gfn >= memslot->base_gfn
  661. && gfn < memslot->base_gfn + memslot->npages) {
  662. if (!memslot || !memslot->dirty_bitmap)
  663. return;
  664. rel_gfn = gfn - memslot->base_gfn;
  665. /* avoid RMW */
  666. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  667. set_bit(rel_gfn, memslot->dirty_bitmap);
  668. return;
  669. }
  670. }
  671. }
  672. static int emulator_read_std(unsigned long addr,
  673. unsigned long *val,
  674. unsigned int bytes,
  675. struct x86_emulate_ctxt *ctxt)
  676. {
  677. struct kvm_vcpu *vcpu = ctxt->vcpu;
  678. void *data = val;
  679. while (bytes) {
  680. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  681. unsigned offset = addr & (PAGE_SIZE-1);
  682. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  683. unsigned long pfn;
  684. struct kvm_memory_slot *memslot;
  685. void *page;
  686. if (gpa == UNMAPPED_GVA)
  687. return X86EMUL_PROPAGATE_FAULT;
  688. pfn = gpa >> PAGE_SHIFT;
  689. memslot = gfn_to_memslot(vcpu->kvm, pfn);
  690. if (!memslot)
  691. return X86EMUL_UNHANDLEABLE;
  692. page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
  693. memcpy(data, page + offset, tocopy);
  694. kunmap_atomic(page, KM_USER0);
  695. bytes -= tocopy;
  696. data += tocopy;
  697. addr += tocopy;
  698. }
  699. return X86EMUL_CONTINUE;
  700. }
  701. static int emulator_write_std(unsigned long addr,
  702. unsigned long val,
  703. unsigned int bytes,
  704. struct x86_emulate_ctxt *ctxt)
  705. {
  706. printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
  707. addr, bytes);
  708. return X86EMUL_UNHANDLEABLE;
  709. }
  710. static int emulator_read_emulated(unsigned long addr,
  711. unsigned long *val,
  712. unsigned int bytes,
  713. struct x86_emulate_ctxt *ctxt)
  714. {
  715. struct kvm_vcpu *vcpu = ctxt->vcpu;
  716. if (vcpu->mmio_read_completed) {
  717. memcpy(val, vcpu->mmio_data, bytes);
  718. vcpu->mmio_read_completed = 0;
  719. return X86EMUL_CONTINUE;
  720. } else if (emulator_read_std(addr, val, bytes, ctxt)
  721. == X86EMUL_CONTINUE)
  722. return X86EMUL_CONTINUE;
  723. else {
  724. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  725. if (gpa == UNMAPPED_GVA)
  726. return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT;
  727. vcpu->mmio_needed = 1;
  728. vcpu->mmio_phys_addr = gpa;
  729. vcpu->mmio_size = bytes;
  730. vcpu->mmio_is_write = 0;
  731. return X86EMUL_UNHANDLEABLE;
  732. }
  733. }
  734. static int emulator_write_emulated(unsigned long addr,
  735. unsigned long val,
  736. unsigned int bytes,
  737. struct x86_emulate_ctxt *ctxt)
  738. {
  739. struct kvm_vcpu *vcpu = ctxt->vcpu;
  740. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  741. if (gpa == UNMAPPED_GVA)
  742. return X86EMUL_PROPAGATE_FAULT;
  743. vcpu->mmio_needed = 1;
  744. vcpu->mmio_phys_addr = gpa;
  745. vcpu->mmio_size = bytes;
  746. vcpu->mmio_is_write = 1;
  747. memcpy(vcpu->mmio_data, &val, bytes);
  748. return X86EMUL_CONTINUE;
  749. }
  750. static int emulator_cmpxchg_emulated(unsigned long addr,
  751. unsigned long old,
  752. unsigned long new,
  753. unsigned int bytes,
  754. struct x86_emulate_ctxt *ctxt)
  755. {
  756. static int reported;
  757. if (!reported) {
  758. reported = 1;
  759. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  760. }
  761. return emulator_write_emulated(addr, new, bytes, ctxt);
  762. }
  763. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  764. {
  765. return kvm_arch_ops->get_segment_base(vcpu, seg);
  766. }
  767. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  768. {
  769. spin_lock(&vcpu->kvm->lock);
  770. vcpu->mmu.inval_page(vcpu, address);
  771. spin_unlock(&vcpu->kvm->lock);
  772. kvm_arch_ops->invlpg(vcpu, address);
  773. return X86EMUL_CONTINUE;
  774. }
  775. int emulate_clts(struct kvm_vcpu *vcpu)
  776. {
  777. unsigned long cr0;
  778. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  779. cr0 = vcpu->cr0 & ~CR0_TS_MASK;
  780. kvm_arch_ops->set_cr0(vcpu, cr0);
  781. return X86EMUL_CONTINUE;
  782. }
  783. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  784. {
  785. struct kvm_vcpu *vcpu = ctxt->vcpu;
  786. switch (dr) {
  787. case 0 ... 3:
  788. *dest = kvm_arch_ops->get_dr(vcpu, dr);
  789. return X86EMUL_CONTINUE;
  790. default:
  791. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  792. __FUNCTION__, dr);
  793. return X86EMUL_UNHANDLEABLE;
  794. }
  795. }
  796. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  797. {
  798. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  799. int exception;
  800. kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  801. if (exception) {
  802. /* FIXME: better handling */
  803. return X86EMUL_UNHANDLEABLE;
  804. }
  805. return X86EMUL_CONTINUE;
  806. }
  807. static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
  808. {
  809. static int reported;
  810. u8 opcodes[4];
  811. unsigned long rip = ctxt->vcpu->rip;
  812. unsigned long rip_linear;
  813. rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
  814. if (reported)
  815. return;
  816. emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
  817. printk(KERN_ERR "emulation failed but !mmio_needed?"
  818. " rip %lx %02x %02x %02x %02x\n",
  819. rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  820. reported = 1;
  821. }
  822. struct x86_emulate_ops emulate_ops = {
  823. .read_std = emulator_read_std,
  824. .write_std = emulator_write_std,
  825. .read_emulated = emulator_read_emulated,
  826. .write_emulated = emulator_write_emulated,
  827. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  828. };
  829. int emulate_instruction(struct kvm_vcpu *vcpu,
  830. struct kvm_run *run,
  831. unsigned long cr2,
  832. u16 error_code)
  833. {
  834. struct x86_emulate_ctxt emulate_ctxt;
  835. int r;
  836. int cs_db, cs_l;
  837. kvm_arch_ops->cache_regs(vcpu);
  838. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  839. emulate_ctxt.vcpu = vcpu;
  840. emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
  841. emulate_ctxt.cr2 = cr2;
  842. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  843. ? X86EMUL_MODE_REAL : cs_l
  844. ? X86EMUL_MODE_PROT64 : cs_db
  845. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  846. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  847. emulate_ctxt.cs_base = 0;
  848. emulate_ctxt.ds_base = 0;
  849. emulate_ctxt.es_base = 0;
  850. emulate_ctxt.ss_base = 0;
  851. } else {
  852. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  853. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  854. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  855. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  856. }
  857. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  858. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  859. vcpu->mmio_is_write = 0;
  860. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  861. if ((r || vcpu->mmio_is_write) && run) {
  862. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  863. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  864. run->mmio.len = vcpu->mmio_size;
  865. run->mmio.is_write = vcpu->mmio_is_write;
  866. }
  867. if (r) {
  868. if (!vcpu->mmio_needed) {
  869. report_emulation_failure(&emulate_ctxt);
  870. return EMULATE_FAIL;
  871. }
  872. return EMULATE_DO_MMIO;
  873. }
  874. kvm_arch_ops->decache_regs(vcpu);
  875. kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  876. if (vcpu->mmio_is_write)
  877. return EMULATE_DO_MMIO;
  878. return EMULATE_DONE;
  879. }
  880. EXPORT_SYMBOL_GPL(emulate_instruction);
  881. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  882. {
  883. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  884. }
  885. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  886. {
  887. struct descriptor_table dt = { limit, base };
  888. kvm_arch_ops->set_gdt(vcpu, &dt);
  889. }
  890. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  891. {
  892. struct descriptor_table dt = { limit, base };
  893. kvm_arch_ops->set_idt(vcpu, &dt);
  894. }
  895. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  896. unsigned long *rflags)
  897. {
  898. lmsw(vcpu, msw);
  899. *rflags = kvm_arch_ops->get_rflags(vcpu);
  900. }
  901. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  902. {
  903. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  904. switch (cr) {
  905. case 0:
  906. return vcpu->cr0;
  907. case 2:
  908. return vcpu->cr2;
  909. case 3:
  910. return vcpu->cr3;
  911. case 4:
  912. return vcpu->cr4;
  913. default:
  914. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  915. return 0;
  916. }
  917. }
  918. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  919. unsigned long *rflags)
  920. {
  921. switch (cr) {
  922. case 0:
  923. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  924. *rflags = kvm_arch_ops->get_rflags(vcpu);
  925. break;
  926. case 2:
  927. vcpu->cr2 = val;
  928. break;
  929. case 3:
  930. set_cr3(vcpu, val);
  931. break;
  932. case 4:
  933. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  934. break;
  935. default:
  936. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  937. }
  938. }
  939. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  940. {
  941. u64 data;
  942. switch (msr) {
  943. case 0xc0010010: /* SYSCFG */
  944. case 0xc0010015: /* HWCR */
  945. case MSR_IA32_PLATFORM_ID:
  946. case MSR_IA32_P5_MC_ADDR:
  947. case MSR_IA32_P5_MC_TYPE:
  948. case MSR_IA32_MC0_CTL:
  949. case MSR_IA32_MCG_STATUS:
  950. case MSR_IA32_MCG_CAP:
  951. case MSR_IA32_MC0_MISC:
  952. case MSR_IA32_MC0_MISC+4:
  953. case MSR_IA32_MC0_MISC+8:
  954. case MSR_IA32_MC0_MISC+12:
  955. case MSR_IA32_MC0_MISC+16:
  956. case MSR_IA32_UCODE_REV:
  957. case MSR_IA32_PERF_STATUS:
  958. /* MTRR registers */
  959. case 0xfe:
  960. case 0x200 ... 0x2ff:
  961. data = 0;
  962. break;
  963. case 0xcd: /* fsb frequency */
  964. data = 3;
  965. break;
  966. case MSR_IA32_APICBASE:
  967. data = vcpu->apic_base;
  968. break;
  969. #ifdef CONFIG_X86_64
  970. case MSR_EFER:
  971. data = vcpu->shadow_efer;
  972. break;
  973. #endif
  974. default:
  975. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  976. return 1;
  977. }
  978. *pdata = data;
  979. return 0;
  980. }
  981. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  982. /*
  983. * Reads an msr value (of 'msr_index') into 'pdata'.
  984. * Returns 0 on success, non-0 otherwise.
  985. * Assumes vcpu_load() was already called.
  986. */
  987. static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  988. {
  989. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  990. }
  991. #ifdef CONFIG_X86_64
  992. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  993. {
  994. if (efer & EFER_RESERVED_BITS) {
  995. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  996. efer);
  997. inject_gp(vcpu);
  998. return;
  999. }
  1000. if (is_paging(vcpu)
  1001. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1002. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1003. inject_gp(vcpu);
  1004. return;
  1005. }
  1006. kvm_arch_ops->set_efer(vcpu, efer);
  1007. efer &= ~EFER_LMA;
  1008. efer |= vcpu->shadow_efer & EFER_LMA;
  1009. vcpu->shadow_efer = efer;
  1010. }
  1011. #endif
  1012. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1013. {
  1014. switch (msr) {
  1015. #ifdef CONFIG_X86_64
  1016. case MSR_EFER:
  1017. set_efer(vcpu, data);
  1018. break;
  1019. #endif
  1020. case MSR_IA32_MC0_STATUS:
  1021. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1022. __FUNCTION__, data);
  1023. break;
  1024. case MSR_IA32_UCODE_REV:
  1025. case MSR_IA32_UCODE_WRITE:
  1026. case 0x200 ... 0x2ff: /* MTRRs */
  1027. break;
  1028. case MSR_IA32_APICBASE:
  1029. vcpu->apic_base = data;
  1030. break;
  1031. default:
  1032. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1033. return 1;
  1034. }
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1038. /*
  1039. * Writes msr value into into the appropriate "register".
  1040. * Returns 0 on success, non-0 otherwise.
  1041. * Assumes vcpu_load() was already called.
  1042. */
  1043. static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1044. {
  1045. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1046. }
  1047. void kvm_resched(struct kvm_vcpu *vcpu)
  1048. {
  1049. vcpu_put(vcpu);
  1050. cond_resched();
  1051. /* Cannot fail - no vcpu unplug yet. */
  1052. vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
  1053. }
  1054. EXPORT_SYMBOL_GPL(kvm_resched);
  1055. void load_msrs(struct vmx_msr_entry *e, int n)
  1056. {
  1057. int i;
  1058. for (i = 0; i < n; ++i)
  1059. wrmsrl(e[i].index, e[i].data);
  1060. }
  1061. EXPORT_SYMBOL_GPL(load_msrs);
  1062. void save_msrs(struct vmx_msr_entry *e, int n)
  1063. {
  1064. int i;
  1065. for (i = 0; i < n; ++i)
  1066. rdmsrl(e[i].index, e[i].data);
  1067. }
  1068. EXPORT_SYMBOL_GPL(save_msrs);
  1069. static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
  1070. {
  1071. struct kvm_vcpu *vcpu;
  1072. int r;
  1073. if (!valid_vcpu(kvm_run->vcpu))
  1074. return -EINVAL;
  1075. vcpu = vcpu_load(kvm, kvm_run->vcpu);
  1076. if (!vcpu)
  1077. return -ENOENT;
  1078. if (kvm_run->emulated) {
  1079. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1080. kvm_run->emulated = 0;
  1081. }
  1082. if (kvm_run->mmio_completed) {
  1083. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1084. vcpu->mmio_read_completed = 1;
  1085. }
  1086. vcpu->mmio_needed = 0;
  1087. r = kvm_arch_ops->run(vcpu, kvm_run);
  1088. vcpu_put(vcpu);
  1089. return r;
  1090. }
  1091. static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
  1092. {
  1093. struct kvm_vcpu *vcpu;
  1094. if (!valid_vcpu(regs->vcpu))
  1095. return -EINVAL;
  1096. vcpu = vcpu_load(kvm, regs->vcpu);
  1097. if (!vcpu)
  1098. return -ENOENT;
  1099. kvm_arch_ops->cache_regs(vcpu);
  1100. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1101. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1102. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1103. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1104. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1105. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1106. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1107. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1108. #ifdef CONFIG_X86_64
  1109. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1110. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1111. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1112. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1113. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1114. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1115. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1116. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1117. #endif
  1118. regs->rip = vcpu->rip;
  1119. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1120. /*
  1121. * Don't leak debug flags in case they were set for guest debugging
  1122. */
  1123. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1124. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1125. vcpu_put(vcpu);
  1126. return 0;
  1127. }
  1128. static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs)
  1129. {
  1130. struct kvm_vcpu *vcpu;
  1131. if (!valid_vcpu(regs->vcpu))
  1132. return -EINVAL;
  1133. vcpu = vcpu_load(kvm, regs->vcpu);
  1134. if (!vcpu)
  1135. return -ENOENT;
  1136. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1137. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1138. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1139. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1140. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1141. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1142. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1143. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1144. #ifdef CONFIG_X86_64
  1145. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1146. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1147. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1148. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1149. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1150. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1151. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1152. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1153. #endif
  1154. vcpu->rip = regs->rip;
  1155. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1156. kvm_arch_ops->decache_regs(vcpu);
  1157. vcpu_put(vcpu);
  1158. return 0;
  1159. }
  1160. static void get_segment(struct kvm_vcpu *vcpu,
  1161. struct kvm_segment *var, int seg)
  1162. {
  1163. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1164. }
  1165. static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
  1166. {
  1167. struct kvm_vcpu *vcpu;
  1168. struct descriptor_table dt;
  1169. if (!valid_vcpu(sregs->vcpu))
  1170. return -EINVAL;
  1171. vcpu = vcpu_load(kvm, sregs->vcpu);
  1172. if (!vcpu)
  1173. return -ENOENT;
  1174. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1175. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1176. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1177. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1178. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1179. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1180. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1181. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1182. kvm_arch_ops->get_idt(vcpu, &dt);
  1183. sregs->idt.limit = dt.limit;
  1184. sregs->idt.base = dt.base;
  1185. kvm_arch_ops->get_gdt(vcpu, &dt);
  1186. sregs->gdt.limit = dt.limit;
  1187. sregs->gdt.base = dt.base;
  1188. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1189. sregs->cr0 = vcpu->cr0;
  1190. sregs->cr2 = vcpu->cr2;
  1191. sregs->cr3 = vcpu->cr3;
  1192. sregs->cr4 = vcpu->cr4;
  1193. sregs->cr8 = vcpu->cr8;
  1194. sregs->efer = vcpu->shadow_efer;
  1195. sregs->apic_base = vcpu->apic_base;
  1196. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1197. sizeof sregs->interrupt_bitmap);
  1198. vcpu_put(vcpu);
  1199. return 0;
  1200. }
  1201. static void set_segment(struct kvm_vcpu *vcpu,
  1202. struct kvm_segment *var, int seg)
  1203. {
  1204. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1205. }
  1206. static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
  1207. {
  1208. struct kvm_vcpu *vcpu;
  1209. int mmu_reset_needed = 0;
  1210. int i;
  1211. struct descriptor_table dt;
  1212. if (!valid_vcpu(sregs->vcpu))
  1213. return -EINVAL;
  1214. vcpu = vcpu_load(kvm, sregs->vcpu);
  1215. if (!vcpu)
  1216. return -ENOENT;
  1217. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1218. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1219. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1220. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1221. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1222. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1223. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1224. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1225. dt.limit = sregs->idt.limit;
  1226. dt.base = sregs->idt.base;
  1227. kvm_arch_ops->set_idt(vcpu, &dt);
  1228. dt.limit = sregs->gdt.limit;
  1229. dt.base = sregs->gdt.base;
  1230. kvm_arch_ops->set_gdt(vcpu, &dt);
  1231. vcpu->cr2 = sregs->cr2;
  1232. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1233. vcpu->cr3 = sregs->cr3;
  1234. vcpu->cr8 = sregs->cr8;
  1235. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1236. #ifdef CONFIG_X86_64
  1237. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1238. #endif
  1239. vcpu->apic_base = sregs->apic_base;
  1240. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1241. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1242. kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
  1243. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1244. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1245. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1246. load_pdptrs(vcpu, vcpu->cr3);
  1247. if (mmu_reset_needed)
  1248. kvm_mmu_reset_context(vcpu);
  1249. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1250. sizeof vcpu->irq_pending);
  1251. vcpu->irq_summary = 0;
  1252. for (i = 0; i < NR_IRQ_WORDS; ++i)
  1253. if (vcpu->irq_pending[i])
  1254. __set_bit(i, &vcpu->irq_summary);
  1255. vcpu_put(vcpu);
  1256. return 0;
  1257. }
  1258. /*
  1259. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1260. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1261. *
  1262. * This list is modified at module load time to reflect the
  1263. * capabilities of the host cpu.
  1264. */
  1265. static u32 msrs_to_save[] = {
  1266. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1267. MSR_K6_STAR,
  1268. #ifdef CONFIG_X86_64
  1269. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1270. #endif
  1271. MSR_IA32_TIME_STAMP_COUNTER,
  1272. };
  1273. static unsigned num_msrs_to_save;
  1274. static __init void kvm_init_msr_list(void)
  1275. {
  1276. u32 dummy[2];
  1277. unsigned i, j;
  1278. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1279. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1280. continue;
  1281. if (j < i)
  1282. msrs_to_save[j] = msrs_to_save[i];
  1283. j++;
  1284. }
  1285. num_msrs_to_save = j;
  1286. }
  1287. /*
  1288. * Adapt set_msr() to msr_io()'s calling convention
  1289. */
  1290. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1291. {
  1292. return set_msr(vcpu, index, *data);
  1293. }
  1294. /*
  1295. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1296. *
  1297. * @return number of msrs set successfully.
  1298. */
  1299. static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
  1300. struct kvm_msr_entry *entries,
  1301. int (*do_msr)(struct kvm_vcpu *vcpu,
  1302. unsigned index, u64 *data))
  1303. {
  1304. struct kvm_vcpu *vcpu;
  1305. int i;
  1306. if (!valid_vcpu(msrs->vcpu))
  1307. return -EINVAL;
  1308. vcpu = vcpu_load(kvm, msrs->vcpu);
  1309. if (!vcpu)
  1310. return -ENOENT;
  1311. for (i = 0; i < msrs->nmsrs; ++i)
  1312. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1313. break;
  1314. vcpu_put(vcpu);
  1315. return i;
  1316. }
  1317. /*
  1318. * Read or write a bunch of msrs. Parameters are user addresses.
  1319. *
  1320. * @return number of msrs set successfully.
  1321. */
  1322. static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
  1323. int (*do_msr)(struct kvm_vcpu *vcpu,
  1324. unsigned index, u64 *data),
  1325. int writeback)
  1326. {
  1327. struct kvm_msrs msrs;
  1328. struct kvm_msr_entry *entries;
  1329. int r, n;
  1330. unsigned size;
  1331. r = -EFAULT;
  1332. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1333. goto out;
  1334. r = -E2BIG;
  1335. if (msrs.nmsrs >= MAX_IO_MSRS)
  1336. goto out;
  1337. r = -ENOMEM;
  1338. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1339. entries = vmalloc(size);
  1340. if (!entries)
  1341. goto out;
  1342. r = -EFAULT;
  1343. if (copy_from_user(entries, user_msrs->entries, size))
  1344. goto out_free;
  1345. r = n = __msr_io(kvm, &msrs, entries, do_msr);
  1346. if (r < 0)
  1347. goto out_free;
  1348. r = -EFAULT;
  1349. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1350. goto out_free;
  1351. r = n;
  1352. out_free:
  1353. vfree(entries);
  1354. out:
  1355. return r;
  1356. }
  1357. /*
  1358. * Translate a guest virtual address to a guest physical address.
  1359. */
  1360. static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr)
  1361. {
  1362. unsigned long vaddr = tr->linear_address;
  1363. struct kvm_vcpu *vcpu;
  1364. gpa_t gpa;
  1365. vcpu = vcpu_load(kvm, tr->vcpu);
  1366. if (!vcpu)
  1367. return -ENOENT;
  1368. spin_lock(&kvm->lock);
  1369. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1370. tr->physical_address = gpa;
  1371. tr->valid = gpa != UNMAPPED_GVA;
  1372. tr->writeable = 1;
  1373. tr->usermode = 0;
  1374. spin_unlock(&kvm->lock);
  1375. vcpu_put(vcpu);
  1376. return 0;
  1377. }
  1378. static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
  1379. {
  1380. struct kvm_vcpu *vcpu;
  1381. if (!valid_vcpu(irq->vcpu))
  1382. return -EINVAL;
  1383. if (irq->irq < 0 || irq->irq >= 256)
  1384. return -EINVAL;
  1385. vcpu = vcpu_load(kvm, irq->vcpu);
  1386. if (!vcpu)
  1387. return -ENOENT;
  1388. set_bit(irq->irq, vcpu->irq_pending);
  1389. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1390. vcpu_put(vcpu);
  1391. return 0;
  1392. }
  1393. static int kvm_dev_ioctl_debug_guest(struct kvm *kvm,
  1394. struct kvm_debug_guest *dbg)
  1395. {
  1396. struct kvm_vcpu *vcpu;
  1397. int r;
  1398. if (!valid_vcpu(dbg->vcpu))
  1399. return -EINVAL;
  1400. vcpu = vcpu_load(kvm, dbg->vcpu);
  1401. if (!vcpu)
  1402. return -ENOENT;
  1403. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1404. vcpu_put(vcpu);
  1405. return r;
  1406. }
  1407. static long kvm_dev_ioctl(struct file *filp,
  1408. unsigned int ioctl, unsigned long arg)
  1409. {
  1410. struct kvm *kvm = filp->private_data;
  1411. int r = -EINVAL;
  1412. switch (ioctl) {
  1413. case KVM_GET_API_VERSION:
  1414. r = KVM_API_VERSION;
  1415. break;
  1416. case KVM_CREATE_VCPU: {
  1417. r = kvm_dev_ioctl_create_vcpu(kvm, arg);
  1418. if (r)
  1419. goto out;
  1420. break;
  1421. }
  1422. case KVM_RUN: {
  1423. struct kvm_run kvm_run;
  1424. r = -EFAULT;
  1425. if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run))
  1426. goto out;
  1427. r = kvm_dev_ioctl_run(kvm, &kvm_run);
  1428. if (r < 0 && r != -EINTR)
  1429. goto out;
  1430. if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run)) {
  1431. r = -EFAULT;
  1432. goto out;
  1433. }
  1434. break;
  1435. }
  1436. case KVM_GET_REGS: {
  1437. struct kvm_regs kvm_regs;
  1438. r = -EFAULT;
  1439. if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
  1440. goto out;
  1441. r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs);
  1442. if (r)
  1443. goto out;
  1444. r = -EFAULT;
  1445. if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs))
  1446. goto out;
  1447. r = 0;
  1448. break;
  1449. }
  1450. case KVM_SET_REGS: {
  1451. struct kvm_regs kvm_regs;
  1452. r = -EFAULT;
  1453. if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
  1454. goto out;
  1455. r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs);
  1456. if (r)
  1457. goto out;
  1458. r = 0;
  1459. break;
  1460. }
  1461. case KVM_GET_SREGS: {
  1462. struct kvm_sregs kvm_sregs;
  1463. r = -EFAULT;
  1464. if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
  1465. goto out;
  1466. r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs);
  1467. if (r)
  1468. goto out;
  1469. r = -EFAULT;
  1470. if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs))
  1471. goto out;
  1472. r = 0;
  1473. break;
  1474. }
  1475. case KVM_SET_SREGS: {
  1476. struct kvm_sregs kvm_sregs;
  1477. r = -EFAULT;
  1478. if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
  1479. goto out;
  1480. r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs);
  1481. if (r)
  1482. goto out;
  1483. r = 0;
  1484. break;
  1485. }
  1486. case KVM_TRANSLATE: {
  1487. struct kvm_translation tr;
  1488. r = -EFAULT;
  1489. if (copy_from_user(&tr, (void *)arg, sizeof tr))
  1490. goto out;
  1491. r = kvm_dev_ioctl_translate(kvm, &tr);
  1492. if (r)
  1493. goto out;
  1494. r = -EFAULT;
  1495. if (copy_to_user((void *)arg, &tr, sizeof tr))
  1496. goto out;
  1497. r = 0;
  1498. break;
  1499. }
  1500. case KVM_INTERRUPT: {
  1501. struct kvm_interrupt irq;
  1502. r = -EFAULT;
  1503. if (copy_from_user(&irq, (void *)arg, sizeof irq))
  1504. goto out;
  1505. r = kvm_dev_ioctl_interrupt(kvm, &irq);
  1506. if (r)
  1507. goto out;
  1508. r = 0;
  1509. break;
  1510. }
  1511. case KVM_DEBUG_GUEST: {
  1512. struct kvm_debug_guest dbg;
  1513. r = -EFAULT;
  1514. if (copy_from_user(&dbg, (void *)arg, sizeof dbg))
  1515. goto out;
  1516. r = kvm_dev_ioctl_debug_guest(kvm, &dbg);
  1517. if (r)
  1518. goto out;
  1519. r = 0;
  1520. break;
  1521. }
  1522. case KVM_SET_MEMORY_REGION: {
  1523. struct kvm_memory_region kvm_mem;
  1524. r = -EFAULT;
  1525. if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem))
  1526. goto out;
  1527. r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem);
  1528. if (r)
  1529. goto out;
  1530. break;
  1531. }
  1532. case KVM_GET_DIRTY_LOG: {
  1533. struct kvm_dirty_log log;
  1534. r = -EFAULT;
  1535. if (copy_from_user(&log, (void *)arg, sizeof log))
  1536. goto out;
  1537. r = kvm_dev_ioctl_get_dirty_log(kvm, &log);
  1538. if (r)
  1539. goto out;
  1540. break;
  1541. }
  1542. case KVM_GET_MSRS:
  1543. r = msr_io(kvm, (void __user *)arg, get_msr, 1);
  1544. break;
  1545. case KVM_SET_MSRS:
  1546. r = msr_io(kvm, (void __user *)arg, do_set_msr, 0);
  1547. break;
  1548. case KVM_GET_MSR_INDEX_LIST: {
  1549. struct kvm_msr_list __user *user_msr_list = (void __user *)arg;
  1550. struct kvm_msr_list msr_list;
  1551. unsigned n;
  1552. r = -EFAULT;
  1553. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1554. goto out;
  1555. n = msr_list.nmsrs;
  1556. msr_list.nmsrs = num_msrs_to_save;
  1557. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1558. goto out;
  1559. r = -E2BIG;
  1560. if (n < num_msrs_to_save)
  1561. goto out;
  1562. r = -EFAULT;
  1563. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1564. num_msrs_to_save * sizeof(u32)))
  1565. goto out;
  1566. r = 0;
  1567. }
  1568. default:
  1569. ;
  1570. }
  1571. out:
  1572. return r;
  1573. }
  1574. static struct page *kvm_dev_nopage(struct vm_area_struct *vma,
  1575. unsigned long address,
  1576. int *type)
  1577. {
  1578. struct kvm *kvm = vma->vm_file->private_data;
  1579. unsigned long pgoff;
  1580. struct kvm_memory_slot *slot;
  1581. struct page *page;
  1582. *type = VM_FAULT_MINOR;
  1583. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1584. slot = gfn_to_memslot(kvm, pgoff);
  1585. if (!slot)
  1586. return NOPAGE_SIGBUS;
  1587. page = gfn_to_page(slot, pgoff);
  1588. if (!page)
  1589. return NOPAGE_SIGBUS;
  1590. get_page(page);
  1591. return page;
  1592. }
  1593. static struct vm_operations_struct kvm_dev_vm_ops = {
  1594. .nopage = kvm_dev_nopage,
  1595. };
  1596. static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma)
  1597. {
  1598. vma->vm_ops = &kvm_dev_vm_ops;
  1599. return 0;
  1600. }
  1601. static struct file_operations kvm_chardev_ops = {
  1602. .open = kvm_dev_open,
  1603. .release = kvm_dev_release,
  1604. .unlocked_ioctl = kvm_dev_ioctl,
  1605. .compat_ioctl = kvm_dev_ioctl,
  1606. .mmap = kvm_dev_mmap,
  1607. };
  1608. static struct miscdevice kvm_dev = {
  1609. MISC_DYNAMIC_MINOR,
  1610. "kvm",
  1611. &kvm_chardev_ops,
  1612. };
  1613. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  1614. void *v)
  1615. {
  1616. if (val == SYS_RESTART) {
  1617. /*
  1618. * Some (well, at least mine) BIOSes hang on reboot if
  1619. * in vmx root mode.
  1620. */
  1621. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  1622. on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
  1623. }
  1624. return NOTIFY_OK;
  1625. }
  1626. static struct notifier_block kvm_reboot_notifier = {
  1627. .notifier_call = kvm_reboot,
  1628. .priority = 0,
  1629. };
  1630. static __init void kvm_init_debug(void)
  1631. {
  1632. struct kvm_stats_debugfs_item *p;
  1633. debugfs_dir = debugfs_create_dir("kvm", 0);
  1634. for (p = debugfs_entries; p->name; ++p)
  1635. p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
  1636. p->data);
  1637. }
  1638. static void kvm_exit_debug(void)
  1639. {
  1640. struct kvm_stats_debugfs_item *p;
  1641. for (p = debugfs_entries; p->name; ++p)
  1642. debugfs_remove(p->dentry);
  1643. debugfs_remove(debugfs_dir);
  1644. }
  1645. hpa_t bad_page_address;
  1646. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
  1647. {
  1648. int r;
  1649. if (kvm_arch_ops) {
  1650. printk(KERN_ERR "kvm: already loaded the other module\n");
  1651. return -EEXIST;
  1652. }
  1653. if (!ops->cpu_has_kvm_support()) {
  1654. printk(KERN_ERR "kvm: no hardware support\n");
  1655. return -EOPNOTSUPP;
  1656. }
  1657. if (ops->disabled_by_bios()) {
  1658. printk(KERN_ERR "kvm: disabled by bios\n");
  1659. return -EOPNOTSUPP;
  1660. }
  1661. kvm_arch_ops = ops;
  1662. r = kvm_arch_ops->hardware_setup();
  1663. if (r < 0)
  1664. return r;
  1665. on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1);
  1666. register_reboot_notifier(&kvm_reboot_notifier);
  1667. kvm_chardev_ops.owner = module;
  1668. r = misc_register(&kvm_dev);
  1669. if (r) {
  1670. printk (KERN_ERR "kvm: misc device register failed\n");
  1671. goto out_free;
  1672. }
  1673. return r;
  1674. out_free:
  1675. unregister_reboot_notifier(&kvm_reboot_notifier);
  1676. on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
  1677. kvm_arch_ops->hardware_unsetup();
  1678. return r;
  1679. }
  1680. void kvm_exit_arch(void)
  1681. {
  1682. misc_deregister(&kvm_dev);
  1683. unregister_reboot_notifier(&kvm_reboot_notifier);
  1684. on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
  1685. kvm_arch_ops->hardware_unsetup();
  1686. kvm_arch_ops = NULL;
  1687. }
  1688. static __init int kvm_init(void)
  1689. {
  1690. static struct page *bad_page;
  1691. int r = 0;
  1692. kvm_init_debug();
  1693. kvm_init_msr_list();
  1694. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  1695. r = -ENOMEM;
  1696. goto out;
  1697. }
  1698. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  1699. memset(__va(bad_page_address), 0, PAGE_SIZE);
  1700. return r;
  1701. out:
  1702. kvm_exit_debug();
  1703. return r;
  1704. }
  1705. static __exit void kvm_exit(void)
  1706. {
  1707. kvm_exit_debug();
  1708. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  1709. }
  1710. module_init(kvm_init)
  1711. module_exit(kvm_exit)
  1712. EXPORT_SYMBOL_GPL(kvm_init_arch);
  1713. EXPORT_SYMBOL_GPL(kvm_exit_arch);