kvm_main.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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 "iodev.h"
  18. #include <linux/kvm_host.h>
  19. #include <linux/kvm.h>
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/percpu.h>
  23. #include <linux/gfp.h>
  24. #include <linux/mm.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/reboot.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/highmem.h>
  30. #include <linux/file.h>
  31. #include <linux/sysdev.h>
  32. #include <linux/cpu.h>
  33. #include <linux/sched.h>
  34. #include <linux/cpumask.h>
  35. #include <linux/smp.h>
  36. #include <linux/anon_inodes.h>
  37. #include <linux/profile.h>
  38. #include <linux/kvm_para.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/mman.h>
  41. #include <asm/processor.h>
  42. #include <asm/io.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/pgtable.h>
  45. MODULE_AUTHOR("Qumranet");
  46. MODULE_LICENSE("GPL");
  47. DEFINE_SPINLOCK(kvm_lock);
  48. LIST_HEAD(vm_list);
  49. static cpumask_t cpus_hardware_enabled;
  50. struct kmem_cache *kvm_vcpu_cache;
  51. EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  52. static __read_mostly struct preempt_ops kvm_preempt_ops;
  53. static struct dentry *debugfs_dir;
  54. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  55. unsigned long arg);
  56. static inline int valid_vcpu(int n)
  57. {
  58. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  59. }
  60. /*
  61. * Switches to specified vcpu, until a matching vcpu_put()
  62. */
  63. void vcpu_load(struct kvm_vcpu *vcpu)
  64. {
  65. int cpu;
  66. mutex_lock(&vcpu->mutex);
  67. cpu = get_cpu();
  68. preempt_notifier_register(&vcpu->preempt_notifier);
  69. kvm_arch_vcpu_load(vcpu, cpu);
  70. put_cpu();
  71. }
  72. void vcpu_put(struct kvm_vcpu *vcpu)
  73. {
  74. preempt_disable();
  75. kvm_arch_vcpu_put(vcpu);
  76. preempt_notifier_unregister(&vcpu->preempt_notifier);
  77. preempt_enable();
  78. mutex_unlock(&vcpu->mutex);
  79. }
  80. static void ack_flush(void *_completed)
  81. {
  82. }
  83. void kvm_flush_remote_tlbs(struct kvm *kvm)
  84. {
  85. int i, cpu;
  86. cpumask_t cpus;
  87. struct kvm_vcpu *vcpu;
  88. cpus_clear(cpus);
  89. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  90. vcpu = kvm->vcpus[i];
  91. if (!vcpu)
  92. continue;
  93. if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  94. continue;
  95. cpu = vcpu->cpu;
  96. if (cpu != -1 && cpu != raw_smp_processor_id())
  97. cpu_set(cpu, cpus);
  98. }
  99. if (cpus_empty(cpus))
  100. return;
  101. ++kvm->stat.remote_tlb_flush;
  102. smp_call_function_mask(cpus, ack_flush, NULL, 1);
  103. }
  104. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  105. {
  106. struct page *page;
  107. int r;
  108. mutex_init(&vcpu->mutex);
  109. vcpu->cpu = -1;
  110. vcpu->kvm = kvm;
  111. vcpu->vcpu_id = id;
  112. init_waitqueue_head(&vcpu->wq);
  113. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  114. if (!page) {
  115. r = -ENOMEM;
  116. goto fail;
  117. }
  118. vcpu->run = page_address(page);
  119. r = kvm_arch_vcpu_init(vcpu);
  120. if (r < 0)
  121. goto fail_free_run;
  122. return 0;
  123. fail_free_run:
  124. free_page((unsigned long)vcpu->run);
  125. fail:
  126. return r;
  127. }
  128. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  129. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  130. {
  131. kvm_arch_vcpu_uninit(vcpu);
  132. free_page((unsigned long)vcpu->run);
  133. }
  134. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  135. static struct kvm *kvm_create_vm(void)
  136. {
  137. struct kvm *kvm = kvm_arch_create_vm();
  138. if (IS_ERR(kvm))
  139. goto out;
  140. kvm->mm = current->mm;
  141. atomic_inc(&kvm->mm->mm_count);
  142. spin_lock_init(&kvm->mmu_lock);
  143. kvm_io_bus_init(&kvm->pio_bus);
  144. mutex_init(&kvm->lock);
  145. kvm_io_bus_init(&kvm->mmio_bus);
  146. init_rwsem(&kvm->slots_lock);
  147. spin_lock(&kvm_lock);
  148. list_add(&kvm->vm_list, &vm_list);
  149. spin_unlock(&kvm_lock);
  150. out:
  151. return kvm;
  152. }
  153. /*
  154. * Free any memory in @free but not in @dont.
  155. */
  156. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  157. struct kvm_memory_slot *dont)
  158. {
  159. if (!dont || free->rmap != dont->rmap)
  160. vfree(free->rmap);
  161. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  162. vfree(free->dirty_bitmap);
  163. free->npages = 0;
  164. free->dirty_bitmap = NULL;
  165. free->rmap = NULL;
  166. }
  167. void kvm_free_physmem(struct kvm *kvm)
  168. {
  169. int i;
  170. for (i = 0; i < kvm->nmemslots; ++i)
  171. kvm_free_physmem_slot(&kvm->memslots[i], NULL);
  172. }
  173. static void kvm_destroy_vm(struct kvm *kvm)
  174. {
  175. struct mm_struct *mm = kvm->mm;
  176. spin_lock(&kvm_lock);
  177. list_del(&kvm->vm_list);
  178. spin_unlock(&kvm_lock);
  179. kvm_io_bus_destroy(&kvm->pio_bus);
  180. kvm_io_bus_destroy(&kvm->mmio_bus);
  181. kvm_arch_destroy_vm(kvm);
  182. mmdrop(mm);
  183. }
  184. static int kvm_vm_release(struct inode *inode, struct file *filp)
  185. {
  186. struct kvm *kvm = filp->private_data;
  187. kvm_destroy_vm(kvm);
  188. return 0;
  189. }
  190. /*
  191. * Allocate some memory and give it an address in the guest physical address
  192. * space.
  193. *
  194. * Discontiguous memory is allowed, mostly for framebuffers.
  195. *
  196. * Must be called holding mmap_sem for write.
  197. */
  198. int __kvm_set_memory_region(struct kvm *kvm,
  199. struct kvm_userspace_memory_region *mem,
  200. int user_alloc)
  201. {
  202. int r;
  203. gfn_t base_gfn;
  204. unsigned long npages;
  205. unsigned long i;
  206. struct kvm_memory_slot *memslot;
  207. struct kvm_memory_slot old, new;
  208. r = -EINVAL;
  209. /* General sanity checks */
  210. if (mem->memory_size & (PAGE_SIZE - 1))
  211. goto out;
  212. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  213. goto out;
  214. if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  215. goto out;
  216. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  217. goto out;
  218. memslot = &kvm->memslots[mem->slot];
  219. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  220. npages = mem->memory_size >> PAGE_SHIFT;
  221. if (!npages)
  222. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  223. new = old = *memslot;
  224. new.base_gfn = base_gfn;
  225. new.npages = npages;
  226. new.flags = mem->flags;
  227. /* Disallow changing a memory slot's size. */
  228. r = -EINVAL;
  229. if (npages && old.npages && npages != old.npages)
  230. goto out_free;
  231. /* Check for overlaps */
  232. r = -EEXIST;
  233. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  234. struct kvm_memory_slot *s = &kvm->memslots[i];
  235. if (s == memslot)
  236. continue;
  237. if (!((base_gfn + npages <= s->base_gfn) ||
  238. (base_gfn >= s->base_gfn + s->npages)))
  239. goto out_free;
  240. }
  241. /* Free page dirty bitmap if unneeded */
  242. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  243. new.dirty_bitmap = NULL;
  244. r = -ENOMEM;
  245. /* Allocate if a slot is being created */
  246. if (npages && !new.rmap) {
  247. new.rmap = vmalloc(npages * sizeof(struct page *));
  248. if (!new.rmap)
  249. goto out_free;
  250. memset(new.rmap, 0, npages * sizeof(*new.rmap));
  251. new.user_alloc = user_alloc;
  252. new.userspace_addr = mem->userspace_addr;
  253. }
  254. /* Allocate page dirty bitmap if needed */
  255. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  256. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  257. new.dirty_bitmap = vmalloc(dirty_bytes);
  258. if (!new.dirty_bitmap)
  259. goto out_free;
  260. memset(new.dirty_bitmap, 0, dirty_bytes);
  261. }
  262. if (mem->slot >= kvm->nmemslots)
  263. kvm->nmemslots = mem->slot + 1;
  264. *memslot = new;
  265. r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
  266. if (r) {
  267. *memslot = old;
  268. goto out_free;
  269. }
  270. kvm_free_physmem_slot(&old, &new);
  271. return 0;
  272. out_free:
  273. kvm_free_physmem_slot(&new, &old);
  274. out:
  275. return r;
  276. }
  277. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  278. int kvm_set_memory_region(struct kvm *kvm,
  279. struct kvm_userspace_memory_region *mem,
  280. int user_alloc)
  281. {
  282. int r;
  283. down_write(&kvm->slots_lock);
  284. r = __kvm_set_memory_region(kvm, mem, user_alloc);
  285. up_write(&kvm->slots_lock);
  286. return r;
  287. }
  288. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  289. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  290. struct
  291. kvm_userspace_memory_region *mem,
  292. int user_alloc)
  293. {
  294. if (mem->slot >= KVM_MEMORY_SLOTS)
  295. return -EINVAL;
  296. return kvm_set_memory_region(kvm, mem, user_alloc);
  297. }
  298. int kvm_get_dirty_log(struct kvm *kvm,
  299. struct kvm_dirty_log *log, int *is_dirty)
  300. {
  301. struct kvm_memory_slot *memslot;
  302. int r, i;
  303. int n;
  304. unsigned long any = 0;
  305. r = -EINVAL;
  306. if (log->slot >= KVM_MEMORY_SLOTS)
  307. goto out;
  308. memslot = &kvm->memslots[log->slot];
  309. r = -ENOENT;
  310. if (!memslot->dirty_bitmap)
  311. goto out;
  312. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  313. for (i = 0; !any && i < n/sizeof(long); ++i)
  314. any = memslot->dirty_bitmap[i];
  315. r = -EFAULT;
  316. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  317. goto out;
  318. if (any)
  319. *is_dirty = 1;
  320. r = 0;
  321. out:
  322. return r;
  323. }
  324. int is_error_page(struct page *page)
  325. {
  326. return page == bad_page;
  327. }
  328. EXPORT_SYMBOL_GPL(is_error_page);
  329. static inline unsigned long bad_hva(void)
  330. {
  331. return PAGE_OFFSET;
  332. }
  333. int kvm_is_error_hva(unsigned long addr)
  334. {
  335. return addr == bad_hva();
  336. }
  337. EXPORT_SYMBOL_GPL(kvm_is_error_hva);
  338. static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  339. {
  340. int i;
  341. for (i = 0; i < kvm->nmemslots; ++i) {
  342. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  343. if (gfn >= memslot->base_gfn
  344. && gfn < memslot->base_gfn + memslot->npages)
  345. return memslot;
  346. }
  347. return NULL;
  348. }
  349. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  350. {
  351. gfn = unalias_gfn(kvm, gfn);
  352. return __gfn_to_memslot(kvm, gfn);
  353. }
  354. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  355. {
  356. int i;
  357. gfn = unalias_gfn(kvm, gfn);
  358. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  359. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  360. if (gfn >= memslot->base_gfn
  361. && gfn < memslot->base_gfn + memslot->npages)
  362. return 1;
  363. }
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  367. static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  368. {
  369. struct kvm_memory_slot *slot;
  370. gfn = unalias_gfn(kvm, gfn);
  371. slot = __gfn_to_memslot(kvm, gfn);
  372. if (!slot)
  373. return bad_hva();
  374. return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
  375. }
  376. /*
  377. * Requires current->mm->mmap_sem to be held
  378. */
  379. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  380. {
  381. struct page *page[1];
  382. unsigned long addr;
  383. int npages;
  384. might_sleep();
  385. addr = gfn_to_hva(kvm, gfn);
  386. if (kvm_is_error_hva(addr)) {
  387. get_page(bad_page);
  388. return bad_page;
  389. }
  390. npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
  391. NULL);
  392. if (npages != 1) {
  393. get_page(bad_page);
  394. return bad_page;
  395. }
  396. return page[0];
  397. }
  398. EXPORT_SYMBOL_GPL(gfn_to_page);
  399. void kvm_release_page_clean(struct page *page)
  400. {
  401. put_page(page);
  402. }
  403. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  404. void kvm_release_page_dirty(struct page *page)
  405. {
  406. if (!PageReserved(page))
  407. SetPageDirty(page);
  408. put_page(page);
  409. }
  410. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  411. static int next_segment(unsigned long len, int offset)
  412. {
  413. if (len > PAGE_SIZE - offset)
  414. return PAGE_SIZE - offset;
  415. else
  416. return len;
  417. }
  418. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  419. int len)
  420. {
  421. int r;
  422. unsigned long addr;
  423. addr = gfn_to_hva(kvm, gfn);
  424. if (kvm_is_error_hva(addr))
  425. return -EFAULT;
  426. r = copy_from_user(data, (void __user *)addr + offset, len);
  427. if (r)
  428. return -EFAULT;
  429. return 0;
  430. }
  431. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  432. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  433. {
  434. gfn_t gfn = gpa >> PAGE_SHIFT;
  435. int seg;
  436. int offset = offset_in_page(gpa);
  437. int ret;
  438. while ((seg = next_segment(len, offset)) != 0) {
  439. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  440. if (ret < 0)
  441. return ret;
  442. offset = 0;
  443. len -= seg;
  444. data += seg;
  445. ++gfn;
  446. }
  447. return 0;
  448. }
  449. EXPORT_SYMBOL_GPL(kvm_read_guest);
  450. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  451. unsigned long len)
  452. {
  453. int r;
  454. unsigned long addr;
  455. gfn_t gfn = gpa >> PAGE_SHIFT;
  456. int offset = offset_in_page(gpa);
  457. addr = gfn_to_hva(kvm, gfn);
  458. if (kvm_is_error_hva(addr))
  459. return -EFAULT;
  460. r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
  461. if (r)
  462. return -EFAULT;
  463. return 0;
  464. }
  465. EXPORT_SYMBOL(kvm_read_guest_atomic);
  466. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  467. int offset, int len)
  468. {
  469. int r;
  470. unsigned long addr;
  471. addr = gfn_to_hva(kvm, gfn);
  472. if (kvm_is_error_hva(addr))
  473. return -EFAULT;
  474. r = copy_to_user((void __user *)addr + offset, data, len);
  475. if (r)
  476. return -EFAULT;
  477. mark_page_dirty(kvm, gfn);
  478. return 0;
  479. }
  480. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  481. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  482. unsigned long len)
  483. {
  484. gfn_t gfn = gpa >> PAGE_SHIFT;
  485. int seg;
  486. int offset = offset_in_page(gpa);
  487. int ret;
  488. while ((seg = next_segment(len, offset)) != 0) {
  489. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  490. if (ret < 0)
  491. return ret;
  492. offset = 0;
  493. len -= seg;
  494. data += seg;
  495. ++gfn;
  496. }
  497. return 0;
  498. }
  499. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  500. {
  501. return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
  502. }
  503. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  504. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  505. {
  506. gfn_t gfn = gpa >> PAGE_SHIFT;
  507. int seg;
  508. int offset = offset_in_page(gpa);
  509. int ret;
  510. while ((seg = next_segment(len, offset)) != 0) {
  511. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  512. if (ret < 0)
  513. return ret;
  514. offset = 0;
  515. len -= seg;
  516. ++gfn;
  517. }
  518. return 0;
  519. }
  520. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  521. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  522. {
  523. struct kvm_memory_slot *memslot;
  524. gfn = unalias_gfn(kvm, gfn);
  525. memslot = __gfn_to_memslot(kvm, gfn);
  526. if (memslot && memslot->dirty_bitmap) {
  527. unsigned long rel_gfn = gfn - memslot->base_gfn;
  528. /* avoid RMW */
  529. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  530. set_bit(rel_gfn, memslot->dirty_bitmap);
  531. }
  532. }
  533. /*
  534. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  535. */
  536. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  537. {
  538. DECLARE_WAITQUEUE(wait, current);
  539. add_wait_queue(&vcpu->wq, &wait);
  540. /*
  541. * We will block until either an interrupt or a signal wakes us up
  542. */
  543. while (!kvm_cpu_has_interrupt(vcpu)
  544. && !signal_pending(current)
  545. && !kvm_arch_vcpu_runnable(vcpu)) {
  546. set_current_state(TASK_INTERRUPTIBLE);
  547. vcpu_put(vcpu);
  548. schedule();
  549. vcpu_load(vcpu);
  550. }
  551. __set_current_state(TASK_RUNNING);
  552. remove_wait_queue(&vcpu->wq, &wait);
  553. }
  554. void kvm_resched(struct kvm_vcpu *vcpu)
  555. {
  556. if (!need_resched())
  557. return;
  558. cond_resched();
  559. }
  560. EXPORT_SYMBOL_GPL(kvm_resched);
  561. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  562. {
  563. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  564. struct page *page;
  565. if (vmf->pgoff == 0)
  566. page = virt_to_page(vcpu->run);
  567. #ifdef CONFIG_X86
  568. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  569. page = virt_to_page(vcpu->arch.pio_data);
  570. #endif
  571. else
  572. return VM_FAULT_SIGBUS;
  573. get_page(page);
  574. vmf->page = page;
  575. return 0;
  576. }
  577. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  578. .fault = kvm_vcpu_fault,
  579. };
  580. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  581. {
  582. vma->vm_ops = &kvm_vcpu_vm_ops;
  583. return 0;
  584. }
  585. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  586. {
  587. struct kvm_vcpu *vcpu = filp->private_data;
  588. fput(vcpu->kvm->filp);
  589. return 0;
  590. }
  591. static const struct file_operations kvm_vcpu_fops = {
  592. .release = kvm_vcpu_release,
  593. .unlocked_ioctl = kvm_vcpu_ioctl,
  594. .compat_ioctl = kvm_vcpu_ioctl,
  595. .mmap = kvm_vcpu_mmap,
  596. };
  597. /*
  598. * Allocates an inode for the vcpu.
  599. */
  600. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  601. {
  602. int fd, r;
  603. struct inode *inode;
  604. struct file *file;
  605. r = anon_inode_getfd(&fd, &inode, &file,
  606. "kvm-vcpu", &kvm_vcpu_fops, vcpu);
  607. if (r)
  608. return r;
  609. atomic_inc(&vcpu->kvm->filp->f_count);
  610. return fd;
  611. }
  612. /*
  613. * Creates some virtual cpus. Good luck creating more than one.
  614. */
  615. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  616. {
  617. int r;
  618. struct kvm_vcpu *vcpu;
  619. if (!valid_vcpu(n))
  620. return -EINVAL;
  621. vcpu = kvm_arch_vcpu_create(kvm, n);
  622. if (IS_ERR(vcpu))
  623. return PTR_ERR(vcpu);
  624. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  625. r = kvm_arch_vcpu_setup(vcpu);
  626. if (r)
  627. goto vcpu_destroy;
  628. mutex_lock(&kvm->lock);
  629. if (kvm->vcpus[n]) {
  630. r = -EEXIST;
  631. mutex_unlock(&kvm->lock);
  632. goto vcpu_destroy;
  633. }
  634. kvm->vcpus[n] = vcpu;
  635. mutex_unlock(&kvm->lock);
  636. /* Now it's all set up, let userspace reach it */
  637. r = create_vcpu_fd(vcpu);
  638. if (r < 0)
  639. goto unlink;
  640. return r;
  641. unlink:
  642. mutex_lock(&kvm->lock);
  643. kvm->vcpus[n] = NULL;
  644. mutex_unlock(&kvm->lock);
  645. vcpu_destroy:
  646. kvm_arch_vcpu_destroy(vcpu);
  647. return r;
  648. }
  649. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  650. {
  651. if (sigset) {
  652. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  653. vcpu->sigset_active = 1;
  654. vcpu->sigset = *sigset;
  655. } else
  656. vcpu->sigset_active = 0;
  657. return 0;
  658. }
  659. static long kvm_vcpu_ioctl(struct file *filp,
  660. unsigned int ioctl, unsigned long arg)
  661. {
  662. struct kvm_vcpu *vcpu = filp->private_data;
  663. void __user *argp = (void __user *)arg;
  664. int r;
  665. if (vcpu->kvm->mm != current->mm)
  666. return -EIO;
  667. switch (ioctl) {
  668. case KVM_RUN:
  669. r = -EINVAL;
  670. if (arg)
  671. goto out;
  672. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  673. break;
  674. case KVM_GET_REGS: {
  675. struct kvm_regs kvm_regs;
  676. memset(&kvm_regs, 0, sizeof kvm_regs);
  677. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  678. if (r)
  679. goto out;
  680. r = -EFAULT;
  681. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  682. goto out;
  683. r = 0;
  684. break;
  685. }
  686. case KVM_SET_REGS: {
  687. struct kvm_regs kvm_regs;
  688. r = -EFAULT;
  689. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  690. goto out;
  691. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  692. if (r)
  693. goto out;
  694. r = 0;
  695. break;
  696. }
  697. case KVM_GET_SREGS: {
  698. struct kvm_sregs kvm_sregs;
  699. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  700. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  701. if (r)
  702. goto out;
  703. r = -EFAULT;
  704. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  705. goto out;
  706. r = 0;
  707. break;
  708. }
  709. case KVM_SET_SREGS: {
  710. struct kvm_sregs kvm_sregs;
  711. r = -EFAULT;
  712. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  713. goto out;
  714. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  715. if (r)
  716. goto out;
  717. r = 0;
  718. break;
  719. }
  720. case KVM_TRANSLATE: {
  721. struct kvm_translation tr;
  722. r = -EFAULT;
  723. if (copy_from_user(&tr, argp, sizeof tr))
  724. goto out;
  725. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  726. if (r)
  727. goto out;
  728. r = -EFAULT;
  729. if (copy_to_user(argp, &tr, sizeof tr))
  730. goto out;
  731. r = 0;
  732. break;
  733. }
  734. case KVM_DEBUG_GUEST: {
  735. struct kvm_debug_guest dbg;
  736. r = -EFAULT;
  737. if (copy_from_user(&dbg, argp, sizeof dbg))
  738. goto out;
  739. r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
  740. if (r)
  741. goto out;
  742. r = 0;
  743. break;
  744. }
  745. case KVM_SET_SIGNAL_MASK: {
  746. struct kvm_signal_mask __user *sigmask_arg = argp;
  747. struct kvm_signal_mask kvm_sigmask;
  748. sigset_t sigset, *p;
  749. p = NULL;
  750. if (argp) {
  751. r = -EFAULT;
  752. if (copy_from_user(&kvm_sigmask, argp,
  753. sizeof kvm_sigmask))
  754. goto out;
  755. r = -EINVAL;
  756. if (kvm_sigmask.len != sizeof sigset)
  757. goto out;
  758. r = -EFAULT;
  759. if (copy_from_user(&sigset, sigmask_arg->sigset,
  760. sizeof sigset))
  761. goto out;
  762. p = &sigset;
  763. }
  764. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  765. break;
  766. }
  767. case KVM_GET_FPU: {
  768. struct kvm_fpu fpu;
  769. memset(&fpu, 0, sizeof fpu);
  770. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
  771. if (r)
  772. goto out;
  773. r = -EFAULT;
  774. if (copy_to_user(argp, &fpu, sizeof fpu))
  775. goto out;
  776. r = 0;
  777. break;
  778. }
  779. case KVM_SET_FPU: {
  780. struct kvm_fpu fpu;
  781. r = -EFAULT;
  782. if (copy_from_user(&fpu, argp, sizeof fpu))
  783. goto out;
  784. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
  785. if (r)
  786. goto out;
  787. r = 0;
  788. break;
  789. }
  790. default:
  791. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  792. }
  793. out:
  794. return r;
  795. }
  796. static long kvm_vm_ioctl(struct file *filp,
  797. unsigned int ioctl, unsigned long arg)
  798. {
  799. struct kvm *kvm = filp->private_data;
  800. void __user *argp = (void __user *)arg;
  801. int r;
  802. if (kvm->mm != current->mm)
  803. return -EIO;
  804. switch (ioctl) {
  805. case KVM_CREATE_VCPU:
  806. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  807. if (r < 0)
  808. goto out;
  809. break;
  810. case KVM_SET_USER_MEMORY_REGION: {
  811. struct kvm_userspace_memory_region kvm_userspace_mem;
  812. r = -EFAULT;
  813. if (copy_from_user(&kvm_userspace_mem, argp,
  814. sizeof kvm_userspace_mem))
  815. goto out;
  816. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
  817. if (r)
  818. goto out;
  819. break;
  820. }
  821. case KVM_GET_DIRTY_LOG: {
  822. struct kvm_dirty_log log;
  823. r = -EFAULT;
  824. if (copy_from_user(&log, argp, sizeof log))
  825. goto out;
  826. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  827. if (r)
  828. goto out;
  829. break;
  830. }
  831. default:
  832. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  833. }
  834. out:
  835. return r;
  836. }
  837. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  838. {
  839. struct kvm *kvm = vma->vm_file->private_data;
  840. struct page *page;
  841. if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
  842. return VM_FAULT_SIGBUS;
  843. page = gfn_to_page(kvm, vmf->pgoff);
  844. if (is_error_page(page)) {
  845. kvm_release_page_clean(page);
  846. return VM_FAULT_SIGBUS;
  847. }
  848. vmf->page = page;
  849. return 0;
  850. }
  851. static struct vm_operations_struct kvm_vm_vm_ops = {
  852. .fault = kvm_vm_fault,
  853. };
  854. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  855. {
  856. vma->vm_ops = &kvm_vm_vm_ops;
  857. return 0;
  858. }
  859. static const struct file_operations kvm_vm_fops = {
  860. .release = kvm_vm_release,
  861. .unlocked_ioctl = kvm_vm_ioctl,
  862. .compat_ioctl = kvm_vm_ioctl,
  863. .mmap = kvm_vm_mmap,
  864. };
  865. static int kvm_dev_ioctl_create_vm(void)
  866. {
  867. int fd, r;
  868. struct inode *inode;
  869. struct file *file;
  870. struct kvm *kvm;
  871. kvm = kvm_create_vm();
  872. if (IS_ERR(kvm))
  873. return PTR_ERR(kvm);
  874. r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
  875. if (r) {
  876. kvm_destroy_vm(kvm);
  877. return r;
  878. }
  879. kvm->filp = file;
  880. return fd;
  881. }
  882. static long kvm_dev_ioctl(struct file *filp,
  883. unsigned int ioctl, unsigned long arg)
  884. {
  885. void __user *argp = (void __user *)arg;
  886. long r = -EINVAL;
  887. switch (ioctl) {
  888. case KVM_GET_API_VERSION:
  889. r = -EINVAL;
  890. if (arg)
  891. goto out;
  892. r = KVM_API_VERSION;
  893. break;
  894. case KVM_CREATE_VM:
  895. r = -EINVAL;
  896. if (arg)
  897. goto out;
  898. r = kvm_dev_ioctl_create_vm();
  899. break;
  900. case KVM_CHECK_EXTENSION:
  901. r = kvm_dev_ioctl_check_extension((long)argp);
  902. break;
  903. case KVM_GET_VCPU_MMAP_SIZE:
  904. r = -EINVAL;
  905. if (arg)
  906. goto out;
  907. r = PAGE_SIZE; /* struct kvm_run */
  908. #ifdef CONFIG_X86
  909. r += PAGE_SIZE; /* pio data page */
  910. #endif
  911. break;
  912. default:
  913. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  914. }
  915. out:
  916. return r;
  917. }
  918. static struct file_operations kvm_chardev_ops = {
  919. .unlocked_ioctl = kvm_dev_ioctl,
  920. .compat_ioctl = kvm_dev_ioctl,
  921. };
  922. static struct miscdevice kvm_dev = {
  923. KVM_MINOR,
  924. "kvm",
  925. &kvm_chardev_ops,
  926. };
  927. static void hardware_enable(void *junk)
  928. {
  929. int cpu = raw_smp_processor_id();
  930. if (cpu_isset(cpu, cpus_hardware_enabled))
  931. return;
  932. cpu_set(cpu, cpus_hardware_enabled);
  933. kvm_arch_hardware_enable(NULL);
  934. }
  935. static void hardware_disable(void *junk)
  936. {
  937. int cpu = raw_smp_processor_id();
  938. if (!cpu_isset(cpu, cpus_hardware_enabled))
  939. return;
  940. cpu_clear(cpu, cpus_hardware_enabled);
  941. decache_vcpus_on_cpu(cpu);
  942. kvm_arch_hardware_disable(NULL);
  943. }
  944. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  945. void *v)
  946. {
  947. int cpu = (long)v;
  948. val &= ~CPU_TASKS_FROZEN;
  949. switch (val) {
  950. case CPU_DYING:
  951. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  952. cpu);
  953. hardware_disable(NULL);
  954. break;
  955. case CPU_UP_CANCELED:
  956. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  957. cpu);
  958. smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
  959. break;
  960. case CPU_ONLINE:
  961. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  962. cpu);
  963. smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
  964. break;
  965. }
  966. return NOTIFY_OK;
  967. }
  968. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  969. void *v)
  970. {
  971. if (val == SYS_RESTART) {
  972. /*
  973. * Some (well, at least mine) BIOSes hang on reboot if
  974. * in vmx root mode.
  975. */
  976. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  977. on_each_cpu(hardware_disable, NULL, 0, 1);
  978. }
  979. return NOTIFY_OK;
  980. }
  981. static struct notifier_block kvm_reboot_notifier = {
  982. .notifier_call = kvm_reboot,
  983. .priority = 0,
  984. };
  985. void kvm_io_bus_init(struct kvm_io_bus *bus)
  986. {
  987. memset(bus, 0, sizeof(*bus));
  988. }
  989. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  990. {
  991. int i;
  992. for (i = 0; i < bus->dev_count; i++) {
  993. struct kvm_io_device *pos = bus->devs[i];
  994. kvm_iodevice_destructor(pos);
  995. }
  996. }
  997. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
  998. {
  999. int i;
  1000. for (i = 0; i < bus->dev_count; i++) {
  1001. struct kvm_io_device *pos = bus->devs[i];
  1002. if (pos->in_range(pos, addr))
  1003. return pos;
  1004. }
  1005. return NULL;
  1006. }
  1007. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  1008. {
  1009. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  1010. bus->devs[bus->dev_count++] = dev;
  1011. }
  1012. static struct notifier_block kvm_cpu_notifier = {
  1013. .notifier_call = kvm_cpu_hotplug,
  1014. .priority = 20, /* must be > scheduler priority */
  1015. };
  1016. static int vm_stat_get(void *_offset, u64 *val)
  1017. {
  1018. unsigned offset = (long)_offset;
  1019. struct kvm *kvm;
  1020. *val = 0;
  1021. spin_lock(&kvm_lock);
  1022. list_for_each_entry(kvm, &vm_list, vm_list)
  1023. *val += *(u32 *)((void *)kvm + offset);
  1024. spin_unlock(&kvm_lock);
  1025. return 0;
  1026. }
  1027. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  1028. static int vcpu_stat_get(void *_offset, u64 *val)
  1029. {
  1030. unsigned offset = (long)_offset;
  1031. struct kvm *kvm;
  1032. struct kvm_vcpu *vcpu;
  1033. int i;
  1034. *val = 0;
  1035. spin_lock(&kvm_lock);
  1036. list_for_each_entry(kvm, &vm_list, vm_list)
  1037. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  1038. vcpu = kvm->vcpus[i];
  1039. if (vcpu)
  1040. *val += *(u32 *)((void *)vcpu + offset);
  1041. }
  1042. spin_unlock(&kvm_lock);
  1043. return 0;
  1044. }
  1045. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  1046. static struct file_operations *stat_fops[] = {
  1047. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  1048. [KVM_STAT_VM] = &vm_stat_fops,
  1049. };
  1050. static void kvm_init_debug(void)
  1051. {
  1052. struct kvm_stats_debugfs_item *p;
  1053. debugfs_dir = debugfs_create_dir("kvm", NULL);
  1054. for (p = debugfs_entries; p->name; ++p)
  1055. p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
  1056. (void *)(long)p->offset,
  1057. stat_fops[p->kind]);
  1058. }
  1059. static void kvm_exit_debug(void)
  1060. {
  1061. struct kvm_stats_debugfs_item *p;
  1062. for (p = debugfs_entries; p->name; ++p)
  1063. debugfs_remove(p->dentry);
  1064. debugfs_remove(debugfs_dir);
  1065. }
  1066. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  1067. {
  1068. hardware_disable(NULL);
  1069. return 0;
  1070. }
  1071. static int kvm_resume(struct sys_device *dev)
  1072. {
  1073. hardware_enable(NULL);
  1074. return 0;
  1075. }
  1076. static struct sysdev_class kvm_sysdev_class = {
  1077. .name = "kvm",
  1078. .suspend = kvm_suspend,
  1079. .resume = kvm_resume,
  1080. };
  1081. static struct sys_device kvm_sysdev = {
  1082. .id = 0,
  1083. .cls = &kvm_sysdev_class,
  1084. };
  1085. struct page *bad_page;
  1086. static inline
  1087. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  1088. {
  1089. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  1090. }
  1091. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  1092. {
  1093. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  1094. kvm_arch_vcpu_load(vcpu, cpu);
  1095. }
  1096. static void kvm_sched_out(struct preempt_notifier *pn,
  1097. struct task_struct *next)
  1098. {
  1099. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  1100. kvm_arch_vcpu_put(vcpu);
  1101. }
  1102. int kvm_init(void *opaque, unsigned int vcpu_size,
  1103. struct module *module)
  1104. {
  1105. int r;
  1106. int cpu;
  1107. kvm_init_debug();
  1108. r = kvm_arch_init(opaque);
  1109. if (r)
  1110. goto out_fail;
  1111. bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1112. if (bad_page == NULL) {
  1113. r = -ENOMEM;
  1114. goto out;
  1115. }
  1116. r = kvm_arch_hardware_setup();
  1117. if (r < 0)
  1118. goto out_free_0;
  1119. for_each_online_cpu(cpu) {
  1120. smp_call_function_single(cpu,
  1121. kvm_arch_check_processor_compat,
  1122. &r, 0, 1);
  1123. if (r < 0)
  1124. goto out_free_1;
  1125. }
  1126. on_each_cpu(hardware_enable, NULL, 0, 1);
  1127. r = register_cpu_notifier(&kvm_cpu_notifier);
  1128. if (r)
  1129. goto out_free_2;
  1130. register_reboot_notifier(&kvm_reboot_notifier);
  1131. r = sysdev_class_register(&kvm_sysdev_class);
  1132. if (r)
  1133. goto out_free_3;
  1134. r = sysdev_register(&kvm_sysdev);
  1135. if (r)
  1136. goto out_free_4;
  1137. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  1138. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
  1139. __alignof__(struct kvm_vcpu),
  1140. 0, NULL);
  1141. if (!kvm_vcpu_cache) {
  1142. r = -ENOMEM;
  1143. goto out_free_5;
  1144. }
  1145. kvm_chardev_ops.owner = module;
  1146. r = misc_register(&kvm_dev);
  1147. if (r) {
  1148. printk(KERN_ERR "kvm: misc device register failed\n");
  1149. goto out_free;
  1150. }
  1151. kvm_preempt_ops.sched_in = kvm_sched_in;
  1152. kvm_preempt_ops.sched_out = kvm_sched_out;
  1153. return 0;
  1154. out_free:
  1155. kmem_cache_destroy(kvm_vcpu_cache);
  1156. out_free_5:
  1157. sysdev_unregister(&kvm_sysdev);
  1158. out_free_4:
  1159. sysdev_class_unregister(&kvm_sysdev_class);
  1160. out_free_3:
  1161. unregister_reboot_notifier(&kvm_reboot_notifier);
  1162. unregister_cpu_notifier(&kvm_cpu_notifier);
  1163. out_free_2:
  1164. on_each_cpu(hardware_disable, NULL, 0, 1);
  1165. out_free_1:
  1166. kvm_arch_hardware_unsetup();
  1167. out_free_0:
  1168. __free_page(bad_page);
  1169. out:
  1170. kvm_arch_exit();
  1171. kvm_exit_debug();
  1172. out_fail:
  1173. return r;
  1174. }
  1175. EXPORT_SYMBOL_GPL(kvm_init);
  1176. void kvm_exit(void)
  1177. {
  1178. misc_deregister(&kvm_dev);
  1179. kmem_cache_destroy(kvm_vcpu_cache);
  1180. sysdev_unregister(&kvm_sysdev);
  1181. sysdev_class_unregister(&kvm_sysdev_class);
  1182. unregister_reboot_notifier(&kvm_reboot_notifier);
  1183. unregister_cpu_notifier(&kvm_cpu_notifier);
  1184. on_each_cpu(hardware_disable, NULL, 0, 1);
  1185. kvm_arch_hardware_unsetup();
  1186. kvm_arch_exit();
  1187. kvm_exit_debug();
  1188. __free_page(bad_page);
  1189. }
  1190. EXPORT_SYMBOL_GPL(kvm_exit);