kvm_main.c 44 KB

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