kvm_main.c 49 KB

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