kvm_main.c 48 KB

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