blob: ae2a1bf640bc9e453c98ffdf979846b05129e6f6 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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
18#include "kvm.h"
Hollis Blancharde2174022007-12-03 15:30:24 -060019#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
21#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
Avi Kivitye4956062007-06-28 14:15:57 -040044#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/io.h>
46#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020047#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
49MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080052DEFINE_SPINLOCK(kvm_lock);
53LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080054
Avi Kivity1b6c0162007-05-24 13:03:52 +030055static cpumask_t cpus_hardware_enabled;
56
Rusty Russellc16f8622007-07-30 21:12:19 +100057struct kmem_cache *kvm_vcpu_cache;
58EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030059
Avi Kivity15ad7142007-07-11 18:17:21 +030060static __read_mostly struct preempt_ops kvm_preempt_ops;
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062static struct dentry *debugfs_dir;
63
Avi Kivitybccf2152007-02-21 18:04:26 +020064static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
65 unsigned long arg);
66
James Morris5aacf0c2006-12-22 01:04:55 -080067static inline int valid_vcpu(int n)
68{
69 return likely(n >= 0 && n < KVM_MAX_VCPUS);
70}
71
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/*
73 * Switches to specified vcpu, until a matching vcpu_put()
74 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020075void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076{
Avi Kivity15ad7142007-07-11 18:17:21 +030077 int cpu;
78
Avi Kivitybccf2152007-02-21 18:04:26 +020079 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030080 cpu = get_cpu();
81 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020082 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030083 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020084}
85
Carsten Otte313a3dc2007-10-11 19:16:52 +020086void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087{
Avi Kivity15ad7142007-07-11 18:17:21 +030088 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020089 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030090 preempt_notifier_unregister(&vcpu->preempt_notifier);
91 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080092 mutex_unlock(&vcpu->mutex);
93}
94
Avi Kivityd9e368d2007-06-07 19:18:30 +030095static void ack_flush(void *_completed)
96{
Avi Kivityd9e368d2007-06-07 19:18:30 +030097}
98
99void kvm_flush_remote_tlbs(struct kvm *kvm)
100{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200101 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300102 cpumask_t cpus;
103 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300104
Avi Kivityd9e368d2007-06-07 19:18:30 +0300105 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000106 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
107 vcpu = kvm->vcpus[i];
108 if (!vcpu)
109 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200110 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300111 continue;
112 cpu = vcpu->cpu;
113 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200114 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300115 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200116 if (cpus_empty(cpus))
117 return;
118 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200119 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300120}
121
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000122int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
123{
124 struct page *page;
125 int r;
126
127 mutex_init(&vcpu->mutex);
128 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000129 vcpu->kvm = kvm;
130 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300131 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000132
133 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
134 if (!page) {
135 r = -ENOMEM;
136 goto fail;
137 }
138 vcpu->run = page_address(page);
139
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800140 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000141 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800142 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000143 return 0;
144
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000145fail_free_run:
146 free_page((unsigned long)vcpu->run);
147fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000148 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000149}
150EXPORT_SYMBOL_GPL(kvm_vcpu_init);
151
152void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
153{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800154 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000155 free_page((unsigned long)vcpu->run);
156}
157EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
158
Avi Kivityf17abe92007-02-21 19:28:04 +0200159static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800161 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800163 if (IS_ERR(kvm))
164 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200166 kvm->mm = current->mm;
167 atomic_inc(&kvm->mm->mm_count);
Eddie Dong74906342007-06-19 18:05:03 +0300168 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800169 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400170 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000171 spin_lock(&kvm_lock);
172 list_add(&kvm->vm_list, &vm_list);
173 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800174out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200175 return kvm;
176}
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Free any memory in @free but not in @dont.
180 */
181static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
182 struct kvm_memory_slot *dont)
183{
Izik Eidus290fc382007-09-27 14:11:22 +0200184 if (!dont || free->rmap != dont->rmap)
185 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186
187 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
188 vfree(free->dirty_bitmap);
189
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000191 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500192 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193}
194
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800195void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196{
197 int i;
198
199 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000200 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201}
202
Avi Kivityf17abe92007-02-21 19:28:04 +0200203static void kvm_destroy_vm(struct kvm *kvm)
204{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200205 struct mm_struct *mm = kvm->mm;
206
Avi Kivity133de902007-02-12 00:54:44 -0800207 spin_lock(&kvm_lock);
208 list_del(&kvm->vm_list);
209 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300210 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400211 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800212 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200213 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200214}
215
216static int kvm_vm_release(struct inode *inode, struct file *filp)
217{
218 struct kvm *kvm = filp->private_data;
219
220 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 return 0;
222}
223
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 * Allocate some memory and give it an address in the guest physical address
226 * space.
227 *
228 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800229 *
230 * Must be called holding kvm->lock.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800232int __kvm_set_memory_region(struct kvm *kvm,
233 struct kvm_userspace_memory_region *mem,
234 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235{
236 int r;
237 gfn_t base_gfn;
238 unsigned long npages;
239 unsigned long i;
240 struct kvm_memory_slot *memslot;
241 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242
243 r = -EINVAL;
244 /* General sanity checks */
245 if (mem->memory_size & (PAGE_SIZE - 1))
246 goto out;
247 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
248 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200249 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 goto out;
251 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
252 goto out;
253
254 memslot = &kvm->memslots[mem->slot];
255 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
256 npages = mem->memory_size >> PAGE_SHIFT;
257
258 if (!npages)
259 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
260
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261 new = old = *memslot;
262
263 new.base_gfn = base_gfn;
264 new.npages = npages;
265 new.flags = mem->flags;
266
267 /* Disallow changing a memory slot's size. */
268 r = -EINVAL;
269 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800270 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271
272 /* Check for overlaps */
273 r = -EEXIST;
274 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
275 struct kvm_memory_slot *s = &kvm->memslots[i];
276
277 if (s == memslot)
278 continue;
279 if (!((base_gfn + npages <= s->base_gfn) ||
280 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800281 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 /* Free page dirty bitmap if unneeded */
285 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000286 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287
288 r = -ENOMEM;
289
290 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500291 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400292 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200293
294 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800295 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200296
Izik Eidus290fc382007-09-27 14:11:22 +0200297 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500298
Izik Eidus80b14b52007-10-25 11:54:04 +0200299 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800300 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301 }
302
303 /* Allocate page dirty bitmap if needed */
304 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
305 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
306
307 new.dirty_bitmap = vmalloc(dirty_bytes);
308 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800309 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310 memset(new.dirty_bitmap, 0, dirty_bytes);
311 }
312
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 if (mem->slot >= kvm->nmemslots)
314 kvm->nmemslots = mem->slot + 1;
315
316 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317
Zhang Xiantao0de10342007-11-20 16:25:04 +0800318 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
319 if (r) {
320 *memslot = old;
321 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800322 }
323
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324 kvm_free_physmem_slot(&old, &new);
325 return 0;
326
Sheng Yangf78e0e22007-10-29 09:40:42 +0800327out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 kvm_free_physmem_slot(&new, &old);
329out:
330 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200331
332}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800333EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
334
335int kvm_set_memory_region(struct kvm *kvm,
336 struct kvm_userspace_memory_region *mem,
337 int user_alloc)
338{
339 int r;
340
341 mutex_lock(&kvm->lock);
342 r = __kvm_set_memory_region(kvm, mem, user_alloc);
343 mutex_unlock(&kvm->lock);
344 return r;
345}
Izik Eidus210c7c42007-10-24 23:52:57 +0200346EXPORT_SYMBOL_GPL(kvm_set_memory_region);
347
Carsten Otte1fe779f2007-10-29 16:08:35 +0100348int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
349 struct
350 kvm_userspace_memory_region *mem,
351 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200352{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200353 if (mem->slot >= KVM_MEMORY_SLOTS)
354 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200355 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356}
357
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800358int kvm_get_dirty_log(struct kvm *kvm,
359 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360{
361 struct kvm_memory_slot *memslot;
362 int r, i;
363 int n;
364 unsigned long any = 0;
365
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 r = -EINVAL;
367 if (log->slot >= KVM_MEMORY_SLOTS)
368 goto out;
369
370 memslot = &kvm->memslots[log->slot];
371 r = -ENOENT;
372 if (!memslot->dirty_bitmap)
373 goto out;
374
Uri Lublincd1a4a92007-02-22 16:43:09 +0200375 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376
Uri Lublincd1a4a92007-02-22 16:43:09 +0200377 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 any = memslot->dirty_bitmap[i];
379
380 r = -EFAULT;
381 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
382 goto out;
383
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800384 if (any)
385 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
387 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389 return r;
390}
391
Izik Eiduscea7bb22007-10-17 19:17:48 +0200392int is_error_page(struct page *page)
393{
394 return page == bad_page;
395}
396EXPORT_SYMBOL_GPL(is_error_page);
397
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200398static inline unsigned long bad_hva(void)
399{
400 return PAGE_OFFSET;
401}
402
403int kvm_is_error_hva(unsigned long addr)
404{
405 return addr == bad_hva();
406}
407EXPORT_SYMBOL_GPL(kvm_is_error_hva);
408
Avi Kivitye8207542007-03-30 16:54:30 +0300409static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410{
411 int i;
412
413 for (i = 0; i < kvm->nmemslots; ++i) {
414 struct kvm_memory_slot *memslot = &kvm->memslots[i];
415
416 if (gfn >= memslot->base_gfn
417 && gfn < memslot->base_gfn + memslot->npages)
418 return memslot;
419 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000420 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421}
Avi Kivitye8207542007-03-30 16:54:30 +0300422
423struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
424{
425 gfn = unalias_gfn(kvm, gfn);
426 return __gfn_to_memslot(kvm, gfn);
427}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428
Izik Eiduse0d62c72007-10-24 23:57:46 +0200429int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
430{
431 int i;
432
433 gfn = unalias_gfn(kvm, gfn);
434 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
435 struct kvm_memory_slot *memslot = &kvm->memslots[i];
436
437 if (gfn >= memslot->base_gfn
438 && gfn < memslot->base_gfn + memslot->npages)
439 return 1;
440 }
441 return 0;
442}
443EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
444
Izik Eidus539cb662007-11-11 22:05:04 +0200445static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
446{
447 struct kvm_memory_slot *slot;
448
449 gfn = unalias_gfn(kvm, gfn);
450 slot = __gfn_to_memslot(kvm, gfn);
451 if (!slot)
452 return bad_hva();
453 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
454}
455
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500456/*
457 * Requires current->mm->mmap_sem to be held
458 */
459static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300460{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500461 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200462 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500463 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300464
Avi Kivity60395222007-10-21 11:03:36 +0200465 might_sleep();
466
Izik Eidus539cb662007-11-11 22:05:04 +0200467 addr = gfn_to_hva(kvm, gfn);
468 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200469 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200470 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200471 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200472
Izik Eidus539cb662007-11-11 22:05:04 +0200473 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
474 NULL);
475
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500476 if (npages != 1) {
477 get_page(bad_page);
478 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200479 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500480
481 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300482}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500483
484struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
485{
486 struct page *page;
487
488 down_read(&current->mm->mmap_sem);
489 page = __gfn_to_page(kvm, gfn);
490 up_read(&current->mm->mmap_sem);
491
492 return page;
493}
494
Avi Kivity954bbbc2007-03-30 14:02:32 +0300495EXPORT_SYMBOL_GPL(gfn_to_page);
496
Izik Eidusb4231d62007-11-20 11:49:33 +0200497void kvm_release_page_clean(struct page *page)
498{
499 put_page(page);
500}
501EXPORT_SYMBOL_GPL(kvm_release_page_clean);
502
503void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200504{
505 if (!PageReserved(page))
506 SetPageDirty(page);
507 put_page(page);
508}
Izik Eidusb4231d62007-11-20 11:49:33 +0200509EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200510
Izik Eidus195aefd2007-10-01 22:14:18 +0200511static int next_segment(unsigned long len, int offset)
512{
513 if (len > PAGE_SIZE - offset)
514 return PAGE_SIZE - offset;
515 else
516 return len;
517}
518
519int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
520 int len)
521{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200522 int r;
523 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200524
Izik Eiduse0506bc2007-11-11 22:10:22 +0200525 addr = gfn_to_hva(kvm, gfn);
526 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200527 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200528 r = copy_from_user(data, (void __user *)addr + offset, len);
529 if (r)
530 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200531 return 0;
532}
533EXPORT_SYMBOL_GPL(kvm_read_guest_page);
534
535int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
536{
537 gfn_t gfn = gpa >> PAGE_SHIFT;
538 int seg;
539 int offset = offset_in_page(gpa);
540 int ret;
541
542 while ((seg = next_segment(len, offset)) != 0) {
543 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
544 if (ret < 0)
545 return ret;
546 offset = 0;
547 len -= seg;
548 data += seg;
549 ++gfn;
550 }
551 return 0;
552}
553EXPORT_SYMBOL_GPL(kvm_read_guest);
554
555int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
556 int offset, int len)
557{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200558 int r;
559 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200560
Izik Eiduse0506bc2007-11-11 22:10:22 +0200561 addr = gfn_to_hva(kvm, gfn);
562 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200563 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200564 r = copy_to_user((void __user *)addr + offset, data, len);
565 if (r)
566 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200567 mark_page_dirty(kvm, gfn);
568 return 0;
569}
570EXPORT_SYMBOL_GPL(kvm_write_guest_page);
571
572int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
573 unsigned long len)
574{
575 gfn_t gfn = gpa >> PAGE_SHIFT;
576 int seg;
577 int offset = offset_in_page(gpa);
578 int ret;
579
580 while ((seg = next_segment(len, offset)) != 0) {
581 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
582 if (ret < 0)
583 return ret;
584 offset = 0;
585 len -= seg;
586 data += seg;
587 ++gfn;
588 }
589 return 0;
590}
591
592int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
593{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200594 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200595}
596EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
597
598int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
599{
600 gfn_t gfn = gpa >> PAGE_SHIFT;
601 int seg;
602 int offset = offset_in_page(gpa);
603 int ret;
604
605 while ((seg = next_segment(len, offset)) != 0) {
606 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
607 if (ret < 0)
608 return ret;
609 offset = 0;
610 len -= seg;
611 ++gfn;
612 }
613 return 0;
614}
615EXPORT_SYMBOL_GPL(kvm_clear_guest);
616
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
618{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300619 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620
Uri Lublin3b6fff12007-10-30 10:42:09 +0200621 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000622 memslot = __gfn_to_memslot(kvm, gfn);
623 if (memslot && memslot->dirty_bitmap) {
624 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625
Rusty Russell7e9d6192007-07-31 20:41:14 +1000626 /* avoid RMW */
627 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
628 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 }
630}
631
Eddie Dongb6958ce2007-07-18 12:15:21 +0300632/*
633 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
634 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500635void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300636{
637 DECLARE_WAITQUEUE(wait, current);
638
639 add_wait_queue(&vcpu->wq, &wait);
640
641 /*
642 * We will block until either an interrupt or a signal wakes us up
643 */
He, Qingc5ec1532007-09-03 17:07:41 +0300644 while (!kvm_cpu_has_interrupt(vcpu)
645 && !signal_pending(current)
Hollis Blanchard53e0aa72007-12-03 16:15:26 -0600646 && !kvm_arch_vcpu_runnable(vcpu)) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300647 set_current_state(TASK_INTERRUPTIBLE);
648 vcpu_put(vcpu);
649 schedule();
650 vcpu_load(vcpu);
651 }
652
He, Qingc5ec1532007-09-03 17:07:41 +0300653 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300654 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300655}
656
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657void kvm_resched(struct kvm_vcpu *vcpu)
658{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300659 if (!need_resched())
660 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662}
663EXPORT_SYMBOL_GPL(kvm_resched);
664
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100665static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200666{
667 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200668 struct page *page;
669
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100670 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +0200671 page = virt_to_page(vcpu->run);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100672 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800673 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity039576c2007-03-20 12:46:50 +0200674 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100675 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200676 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100677 vmf->page = page;
678 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200679}
680
681static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100682 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200683};
684
685static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
686{
687 vma->vm_ops = &kvm_vcpu_vm_ops;
688 return 0;
689}
690
Avi Kivitybccf2152007-02-21 18:04:26 +0200691static int kvm_vcpu_release(struct inode *inode, struct file *filp)
692{
693 struct kvm_vcpu *vcpu = filp->private_data;
694
695 fput(vcpu->kvm->filp);
696 return 0;
697}
698
699static struct file_operations kvm_vcpu_fops = {
700 .release = kvm_vcpu_release,
701 .unlocked_ioctl = kvm_vcpu_ioctl,
702 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200703 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200704};
705
706/*
707 * Allocates an inode for the vcpu.
708 */
709static int create_vcpu_fd(struct kvm_vcpu *vcpu)
710{
711 int fd, r;
712 struct inode *inode;
713 struct file *file;
714
Avi Kivityd6d28162007-06-28 08:38:16 -0400715 r = anon_inode_getfd(&fd, &inode, &file,
716 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
717 if (r)
718 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200719 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200720 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200721}
722
Avi Kivityc5ea7662007-02-20 18:41:05 +0200723/*
724 * Creates some virtual cpus. Good luck creating more than one.
725 */
726static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
727{
728 int r;
729 struct kvm_vcpu *vcpu;
730
Avi Kivityc5ea7662007-02-20 18:41:05 +0200731 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000732 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200733
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800734 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000735 if (IS_ERR(vcpu))
736 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200737
Avi Kivity15ad7142007-07-11 18:17:21 +0300738 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
739
Avi Kivity26e52152007-11-20 15:30:24 +0200740 r = kvm_arch_vcpu_setup(vcpu);
741 if (r)
742 goto vcpu_destroy;
743
Shaohua Li11ec2802007-07-23 14:51:37 +0800744 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000745 if (kvm->vcpus[n]) {
746 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800747 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800748 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000749 }
750 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800751 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000752
753 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200754 r = create_vcpu_fd(vcpu);
755 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000756 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200757 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200758
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000759unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800760 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000761 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800762 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800763vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600764 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200765 return r;
766}
767
Avi Kivity1961d272007-03-05 19:46:05 +0200768static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
769{
770 if (sigset) {
771 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
772 vcpu->sigset_active = 1;
773 vcpu->sigset = *sigset;
774 } else
775 vcpu->sigset_active = 0;
776 return 0;
777}
778
Avi Kivitybccf2152007-02-21 18:04:26 +0200779static long kvm_vcpu_ioctl(struct file *filp,
780 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781{
Avi Kivitybccf2152007-02-21 18:04:26 +0200782 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000783 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200784 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200786 if (vcpu->kvm->mm != current->mm)
787 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200789 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200790 r = -EINVAL;
791 if (arg)
792 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500793 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 case KVM_GET_REGS: {
796 struct kvm_regs kvm_regs;
797
Avi Kivitybccf2152007-02-21 18:04:26 +0200798 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500799 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 if (r)
801 goto out;
802 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000803 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 goto out;
805 r = 0;
806 break;
807 }
808 case KVM_SET_REGS: {
809 struct kvm_regs kvm_regs;
810
811 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000812 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500814 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 if (r)
816 goto out;
817 r = 0;
818 break;
819 }
820 case KVM_GET_SREGS: {
821 struct kvm_sregs kvm_sregs;
822
Avi Kivitybccf2152007-02-21 18:04:26 +0200823 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500824 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 if (r)
826 goto out;
827 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000828 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 goto out;
830 r = 0;
831 break;
832 }
833 case KVM_SET_SREGS: {
834 struct kvm_sregs kvm_sregs;
835
836 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000837 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500839 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 if (r)
841 goto out;
842 r = 0;
843 break;
844 }
845 case KVM_TRANSLATE: {
846 struct kvm_translation tr;
847
848 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000849 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800851 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 if (r)
853 goto out;
854 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000855 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 goto out;
857 r = 0;
858 break;
859 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 case KVM_DEBUG_GUEST: {
861 struct kvm_debug_guest dbg;
862
863 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000864 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500866 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 if (r)
868 goto out;
869 r = 0;
870 break;
871 }
Avi Kivity1961d272007-03-05 19:46:05 +0200872 case KVM_SET_SIGNAL_MASK: {
873 struct kvm_signal_mask __user *sigmask_arg = argp;
874 struct kvm_signal_mask kvm_sigmask;
875 sigset_t sigset, *p;
876
877 p = NULL;
878 if (argp) {
879 r = -EFAULT;
880 if (copy_from_user(&kvm_sigmask, argp,
881 sizeof kvm_sigmask))
882 goto out;
883 r = -EINVAL;
884 if (kvm_sigmask.len != sizeof sigset)
885 goto out;
886 r = -EFAULT;
887 if (copy_from_user(&sigset, sigmask_arg->sigset,
888 sizeof sigset))
889 goto out;
890 p = &sigset;
891 }
892 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
893 break;
894 }
Avi Kivityb8836732007-04-01 16:34:31 +0300895 case KVM_GET_FPU: {
896 struct kvm_fpu fpu;
897
898 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500899 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300900 if (r)
901 goto out;
902 r = -EFAULT;
903 if (copy_to_user(argp, &fpu, sizeof fpu))
904 goto out;
905 r = 0;
906 break;
907 }
908 case KVM_SET_FPU: {
909 struct kvm_fpu fpu;
910
911 r = -EFAULT;
912 if (copy_from_user(&fpu, argp, sizeof fpu))
913 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500914 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300915 if (r)
916 goto out;
917 r = 0;
918 break;
919 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200920 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200921 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200922 }
923out:
924 return r;
925}
926
927static long kvm_vm_ioctl(struct file *filp,
928 unsigned int ioctl, unsigned long arg)
929{
930 struct kvm *kvm = filp->private_data;
931 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +0100932 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200933
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200934 if (kvm->mm != current->mm)
935 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200936 switch (ioctl) {
937 case KVM_CREATE_VCPU:
938 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
939 if (r < 0)
940 goto out;
941 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200942 case KVM_SET_USER_MEMORY_REGION: {
943 struct kvm_userspace_memory_region kvm_userspace_mem;
944
945 r = -EFAULT;
946 if (copy_from_user(&kvm_userspace_mem, argp,
947 sizeof kvm_userspace_mem))
948 goto out;
949
950 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 if (r)
952 goto out;
953 break;
954 }
955 case KVM_GET_DIRTY_LOG: {
956 struct kvm_dirty_log log;
957
958 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000959 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200961 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 if (r)
963 goto out;
964 break;
965 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200966 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +0100967 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +0200968 }
969out:
970 return r;
971}
972
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100973static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +0200974{
975 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +0200976 struct page *page;
977
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100978 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
979 return VM_FAULT_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500980 /* current->mm->mmap_sem is already held so call lockless version */
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100981 page = __gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200982 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200983 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100984 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200985 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100986 vmf->page = page;
987 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +0200988}
989
990static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100991 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +0200992};
993
994static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
995{
996 vma->vm_ops = &kvm_vm_vm_ops;
997 return 0;
998}
999
1000static struct file_operations kvm_vm_fops = {
1001 .release = kvm_vm_release,
1002 .unlocked_ioctl = kvm_vm_ioctl,
1003 .compat_ioctl = kvm_vm_ioctl,
1004 .mmap = kvm_vm_mmap,
1005};
1006
1007static int kvm_dev_ioctl_create_vm(void)
1008{
1009 int fd, r;
1010 struct inode *inode;
1011 struct file *file;
1012 struct kvm *kvm;
1013
Avi Kivityf17abe92007-02-21 19:28:04 +02001014 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001015 if (IS_ERR(kvm))
1016 return PTR_ERR(kvm);
1017 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1018 if (r) {
1019 kvm_destroy_vm(kvm);
1020 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001021 }
1022
Avi Kivitybccf2152007-02-21 18:04:26 +02001023 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001024
Avi Kivityf17abe92007-02-21 19:28:04 +02001025 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001026}
1027
1028static long kvm_dev_ioctl(struct file *filp,
1029 unsigned int ioctl, unsigned long arg)
1030{
1031 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001032 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001033
1034 switch (ioctl) {
1035 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001036 r = -EINVAL;
1037 if (arg)
1038 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001039 r = KVM_API_VERSION;
1040 break;
1041 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001042 r = -EINVAL;
1043 if (arg)
1044 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001045 r = kvm_dev_ioctl_create_vm();
1046 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001047 case KVM_CHECK_EXTENSION:
1048 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001049 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001050 case KVM_GET_VCPU_MMAP_SIZE:
1051 r = -EINVAL;
1052 if (arg)
1053 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001054 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001055 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001057 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 }
1059out:
1060 return r;
1061}
1062
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 .unlocked_ioctl = kvm_dev_ioctl,
1065 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066};
1067
1068static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001069 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 "kvm",
1071 &kvm_chardev_ops,
1072};
1073
Avi Kivity1b6c0162007-05-24 13:03:52 +03001074static void hardware_enable(void *junk)
1075{
1076 int cpu = raw_smp_processor_id();
1077
1078 if (cpu_isset(cpu, cpus_hardware_enabled))
1079 return;
1080 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001081 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001082}
1083
1084static void hardware_disable(void *junk)
1085{
1086 int cpu = raw_smp_processor_id();
1087
1088 if (!cpu_isset(cpu, cpus_hardware_enabled))
1089 return;
1090 cpu_clear(cpu, cpus_hardware_enabled);
1091 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001092 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001093}
1094
Avi Kivity774c47f2007-02-12 00:54:47 -08001095static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1096 void *v)
1097{
1098 int cpu = (long)v;
1099
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001100 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001101 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001102 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03001103 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1104 cpu);
1105 hardware_disable(NULL);
1106 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001107 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001108 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1109 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001110 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001111 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001112 case CPU_ONLINE:
1113 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1114 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001115 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001116 break;
1117 }
1118 return NOTIFY_OK;
1119}
1120
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001121static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001122 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001123{
1124 if (val == SYS_RESTART) {
1125 /*
1126 * Some (well, at least mine) BIOSes hang on reboot if
1127 * in vmx root mode.
1128 */
1129 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1130 on_each_cpu(hardware_disable, NULL, 0, 1);
1131 }
1132 return NOTIFY_OK;
1133}
1134
1135static struct notifier_block kvm_reboot_notifier = {
1136 .notifier_call = kvm_reboot,
1137 .priority = 0,
1138};
1139
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001140void kvm_io_bus_init(struct kvm_io_bus *bus)
1141{
1142 memset(bus, 0, sizeof(*bus));
1143}
1144
1145void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1146{
1147 int i;
1148
1149 for (i = 0; i < bus->dev_count; i++) {
1150 struct kvm_io_device *pos = bus->devs[i];
1151
1152 kvm_iodevice_destructor(pos);
1153 }
1154}
1155
1156struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1157{
1158 int i;
1159
1160 for (i = 0; i < bus->dev_count; i++) {
1161 struct kvm_io_device *pos = bus->devs[i];
1162
1163 if (pos->in_range(pos, addr))
1164 return pos;
1165 }
1166
1167 return NULL;
1168}
1169
1170void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1171{
1172 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1173
1174 bus->devs[bus->dev_count++] = dev;
1175}
1176
Avi Kivity774c47f2007-02-12 00:54:47 -08001177static struct notifier_block kvm_cpu_notifier = {
1178 .notifier_call = kvm_cpu_hotplug,
1179 .priority = 20, /* must be > scheduler priority */
1180};
1181
Avi Kivityba1389b2007-11-18 16:24:12 +02001182static u64 vm_stat_get(void *_offset)
1183{
1184 unsigned offset = (long)_offset;
1185 u64 total = 0;
1186 struct kvm *kvm;
1187
1188 spin_lock(&kvm_lock);
1189 list_for_each_entry(kvm, &vm_list, vm_list)
1190 total += *(u32 *)((void *)kvm + offset);
1191 spin_unlock(&kvm_lock);
1192 return total;
1193}
1194
1195DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1196
1197static u64 vcpu_stat_get(void *_offset)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001198{
1199 unsigned offset = (long)_offset;
1200 u64 total = 0;
1201 struct kvm *kvm;
1202 struct kvm_vcpu *vcpu;
1203 int i;
1204
1205 spin_lock(&kvm_lock);
1206 list_for_each_entry(kvm, &vm_list, vm_list)
1207 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001208 vcpu = kvm->vcpus[i];
1209 if (vcpu)
1210 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001211 }
1212 spin_unlock(&kvm_lock);
1213 return total;
1214}
1215
Avi Kivityba1389b2007-11-18 16:24:12 +02001216DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1217
1218static struct file_operations *stat_fops[] = {
1219 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1220 [KVM_STAT_VM] = &vm_stat_fops,
1221};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001222
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001223static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224{
1225 struct kvm_stats_debugfs_item *p;
1226
Al Viro8b6d44c2007-02-09 16:38:40 +00001227 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001229 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1230 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001231 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232}
1233
1234static void kvm_exit_debug(void)
1235{
1236 struct kvm_stats_debugfs_item *p;
1237
1238 for (p = debugfs_entries; p->name; ++p)
1239 debugfs_remove(p->dentry);
1240 debugfs_remove(debugfs_dir);
1241}
1242
Avi Kivity59ae6c62007-02-12 00:54:48 -08001243static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1244{
Avi Kivity4267c412007-05-24 13:09:41 +03001245 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001246 return 0;
1247}
1248
1249static int kvm_resume(struct sys_device *dev)
1250{
Avi Kivity4267c412007-05-24 13:09:41 +03001251 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001252 return 0;
1253}
1254
1255static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001256 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001257 .suspend = kvm_suspend,
1258 .resume = kvm_resume,
1259};
1260
1261static struct sys_device kvm_sysdev = {
1262 .id = 0,
1263 .cls = &kvm_sysdev_class,
1264};
1265
Izik Eiduscea7bb22007-10-17 19:17:48 +02001266struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267
Avi Kivity15ad7142007-07-11 18:17:21 +03001268static inline
1269struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1270{
1271 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1272}
1273
1274static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1275{
1276 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1277
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001278 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001279}
1280
1281static void kvm_sched_out(struct preempt_notifier *pn,
1282 struct task_struct *next)
1283{
1284 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1285
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001286 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001287}
1288
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001289int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001290 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291{
1292 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001293 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001295 kvm_init_debug();
1296
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001297 r = kvm_arch_init(opaque);
1298 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001299 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001300
1301 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1302
1303 if (bad_page == NULL) {
1304 r = -ENOMEM;
1305 goto out;
1306 }
1307
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001308 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001310 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311
Yang, Sheng002c7f72007-07-31 14:23:01 +03001312 for_each_online_cpu(cpu) {
1313 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001314 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001315 &r, 0, 1);
1316 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001317 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001318 }
1319
Avi Kivity1b6c0162007-05-24 13:03:52 +03001320 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001321 r = register_cpu_notifier(&kvm_cpu_notifier);
1322 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001323 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 register_reboot_notifier(&kvm_reboot_notifier);
1325
Avi Kivity59ae6c62007-02-12 00:54:48 -08001326 r = sysdev_class_register(&kvm_sysdev_class);
1327 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001328 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001329
1330 r = sysdev_register(&kvm_sysdev);
1331 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001332 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001333
Rusty Russellc16f8622007-07-30 21:12:19 +10001334 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1335 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001336 __alignof__(struct kvm_vcpu),
1337 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001338 if (!kvm_vcpu_cache) {
1339 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001340 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001341 }
1342
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 kvm_chardev_ops.owner = module;
1344
1345 r = misc_register(&kvm_dev);
1346 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001347 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 goto out_free;
1349 }
1350
Avi Kivity15ad7142007-07-11 18:17:21 +03001351 kvm_preempt_ops.sched_in = kvm_sched_in;
1352 kvm_preempt_ops.sched_out = kvm_sched_out;
1353
Avi Kivityc7addb92007-09-16 18:58:32 +02001354 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355
1356out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001357 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001358out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001359 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001360out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001361 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001362out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001364 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001365out_free_2:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001366 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001367out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001368 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001369out_free_0:
1370 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001371out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001372 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001373 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001374out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 return r;
1376}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001377EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001379void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380{
1381 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001382 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001383 sysdev_unregister(&kvm_sysdev);
1384 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001386 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001387 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001388 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001389 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001391 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001393EXPORT_SYMBOL_GPL(kvm_exit);