blob: 14b376ead3da0ec54cb9466563df8e951d73ec93 [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"
Carsten Otte043405e2007-10-10 17:16:19 +020019#include "x86.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030020#include "irq.h"
Hollis Blancharde2174022007-12-03 15:30:24 -060021#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
23#include <linux/kvm.h>
24#include <linux/module.h>
25#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/percpu.h>
27#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/debugfs.h>
33#include <linux/highmem.h>
34#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080035#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080036#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040037#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030038#include <linux/cpumask.h>
39#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040040#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030041#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050042#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020043#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050044#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080045
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/io.h>
48#include <asm/uaccess.h>
49#include <asm/desc.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020050#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
52MODULE_AUTHOR("Qumranet");
53MODULE_LICENSE("GPL");
54
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080055DEFINE_SPINLOCK(kvm_lock);
56LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080057
Avi Kivity1b6c0162007-05-24 13:03:52 +030058static cpumask_t cpus_hardware_enabled;
59
Rusty Russellc16f8622007-07-30 21:12:19 +100060struct kmem_cache *kvm_vcpu_cache;
61EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030062
Avi Kivity15ad7142007-07-11 18:17:21 +030063static __read_mostly struct preempt_ops kvm_preempt_ops;
64
Avi Kivity6aa8b732006-12-10 02:21:36 -080065static struct dentry *debugfs_dir;
66
Avi Kivitybccf2152007-02-21 18:04:26 +020067static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
68 unsigned long arg);
69
James Morris5aacf0c2006-12-22 01:04:55 -080070static inline int valid_vcpu(int n)
71{
72 return likely(n >= 0 && n < KVM_MAX_VCPUS);
73}
74
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/*
76 * Switches to specified vcpu, until a matching vcpu_put()
77 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020078void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080079{
Avi Kivity15ad7142007-07-11 18:17:21 +030080 int cpu;
81
Avi Kivitybccf2152007-02-21 18:04:26 +020082 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030083 cpu = get_cpu();
84 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020085 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030086 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020087}
88
Carsten Otte313a3dc2007-10-11 19:16:52 +020089void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080090{
Avi Kivity15ad7142007-07-11 18:17:21 +030091 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020092 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030093 preempt_notifier_unregister(&vcpu->preempt_notifier);
94 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080095 mutex_unlock(&vcpu->mutex);
96}
97
Avi Kivityd9e368d2007-06-07 19:18:30 +030098static void ack_flush(void *_completed)
99{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300100}
101
102void kvm_flush_remote_tlbs(struct kvm *kvm)
103{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200104 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300105 cpumask_t cpus;
106 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300107
Avi Kivityd9e368d2007-06-07 19:18:30 +0300108 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000109 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
110 vcpu = kvm->vcpus[i];
111 if (!vcpu)
112 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200113 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300114 continue;
115 cpu = vcpu->cpu;
116 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200117 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300118 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200119 if (cpus_empty(cpus))
120 return;
121 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200122 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300123}
124
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000125int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
126{
127 struct page *page;
128 int r;
129
130 mutex_init(&vcpu->mutex);
131 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000132 vcpu->kvm = kvm;
133 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300134 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000135
136 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
137 if (!page) {
138 r = -ENOMEM;
139 goto fail;
140 }
141 vcpu->run = page_address(page);
142
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800143 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000144 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800145 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000146 return 0;
147
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000148fail_free_run:
149 free_page((unsigned long)vcpu->run);
150fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000151 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000152}
153EXPORT_SYMBOL_GPL(kvm_vcpu_init);
154
155void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
156{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800157 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000158 free_page((unsigned long)vcpu->run);
159}
160EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
161
Avi Kivityf17abe92007-02-21 19:28:04 +0200162static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800164 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800166 if (IS_ERR(kvm))
167 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200169 kvm->mm = current->mm;
170 atomic_inc(&kvm->mm->mm_count);
Eddie Dong74906342007-06-19 18:05:03 +0300171 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800172 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400173 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000174 spin_lock(&kvm_lock);
175 list_add(&kvm->vm_list, &vm_list);
176 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800177out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200178 return kvm;
179}
180
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181/*
182 * Free any memory in @free but not in @dont.
183 */
184static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
185 struct kvm_memory_slot *dont)
186{
Izik Eidus290fc382007-09-27 14:11:22 +0200187 if (!dont || free->rmap != dont->rmap)
188 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189
190 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
191 vfree(free->dirty_bitmap);
192
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000194 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500195 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196}
197
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800198void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199{
200 int i;
201
202 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000203 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204}
205
Avi Kivityf17abe92007-02-21 19:28:04 +0200206static void kvm_destroy_vm(struct kvm *kvm)
207{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200208 struct mm_struct *mm = kvm->mm;
209
Avi Kivity133de902007-02-12 00:54:44 -0800210 spin_lock(&kvm_lock);
211 list_del(&kvm->vm_list);
212 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300213 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400214 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800215 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200216 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200217}
218
219static int kvm_vm_release(struct inode *inode, struct file *filp)
220{
221 struct kvm *kvm = filp->private_data;
222
223 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 return 0;
225}
226
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 * Allocate some memory and give it an address in the guest physical address
229 * space.
230 *
231 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800232 *
233 * Must be called holding kvm->lock.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800235int __kvm_set_memory_region(struct kvm *kvm,
236 struct kvm_userspace_memory_region *mem,
237 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238{
239 int r;
240 gfn_t base_gfn;
241 unsigned long npages;
242 unsigned long i;
243 struct kvm_memory_slot *memslot;
244 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245
246 r = -EINVAL;
247 /* General sanity checks */
248 if (mem->memory_size & (PAGE_SIZE - 1))
249 goto out;
250 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
251 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200252 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253 goto out;
254 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
255 goto out;
256
257 memslot = &kvm->memslots[mem->slot];
258 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
259 npages = mem->memory_size >> PAGE_SHIFT;
260
261 if (!npages)
262 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
263
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264 new = old = *memslot;
265
266 new.base_gfn = base_gfn;
267 new.npages = npages;
268 new.flags = mem->flags;
269
270 /* Disallow changing a memory slot's size. */
271 r = -EINVAL;
272 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800273 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274
275 /* Check for overlaps */
276 r = -EEXIST;
277 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
278 struct kvm_memory_slot *s = &kvm->memslots[i];
279
280 if (s == memslot)
281 continue;
282 if (!((base_gfn + npages <= s->base_gfn) ||
283 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800284 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 /* Free page dirty bitmap if unneeded */
288 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000289 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291 r = -ENOMEM;
292
293 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500294 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400295 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200296
297 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800298 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200299
Izik Eidus290fc382007-09-27 14:11:22 +0200300 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500301
Izik Eidus80b14b52007-10-25 11:54:04 +0200302 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800303 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 }
305
306 /* Allocate page dirty bitmap if needed */
307 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
308 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
309
310 new.dirty_bitmap = vmalloc(dirty_bytes);
311 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800312 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 memset(new.dirty_bitmap, 0, dirty_bytes);
314 }
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 if (mem->slot >= kvm->nmemslots)
317 kvm->nmemslots = mem->slot + 1;
318
319 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320
Zhang Xiantao0de10342007-11-20 16:25:04 +0800321 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
322 if (r) {
323 *memslot = old;
324 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800325 }
326
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 kvm_free_physmem_slot(&old, &new);
328 return 0;
329
Sheng Yangf78e0e22007-10-29 09:40:42 +0800330out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331 kvm_free_physmem_slot(&new, &old);
332out:
333 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200334
335}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800336EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
337
338int kvm_set_memory_region(struct kvm *kvm,
339 struct kvm_userspace_memory_region *mem,
340 int user_alloc)
341{
342 int r;
343
344 mutex_lock(&kvm->lock);
345 r = __kvm_set_memory_region(kvm, mem, user_alloc);
346 mutex_unlock(&kvm->lock);
347 return r;
348}
Izik Eidus210c7c42007-10-24 23:52:57 +0200349EXPORT_SYMBOL_GPL(kvm_set_memory_region);
350
Carsten Otte1fe779f2007-10-29 16:08:35 +0100351int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
352 struct
353 kvm_userspace_memory_region *mem,
354 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200355{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200356 if (mem->slot >= KVM_MEMORY_SLOTS)
357 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200358 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359}
360
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800361int kvm_get_dirty_log(struct kvm *kvm,
362 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363{
364 struct kvm_memory_slot *memslot;
365 int r, i;
366 int n;
367 unsigned long any = 0;
368
Avi Kivity6aa8b732006-12-10 02:21:36 -0800369 r = -EINVAL;
370 if (log->slot >= KVM_MEMORY_SLOTS)
371 goto out;
372
373 memslot = &kvm->memslots[log->slot];
374 r = -ENOENT;
375 if (!memslot->dirty_bitmap)
376 goto out;
377
Uri Lublincd1a4a92007-02-22 16:43:09 +0200378 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
Uri Lublincd1a4a92007-02-22 16:43:09 +0200380 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 any = memslot->dirty_bitmap[i];
382
383 r = -EFAULT;
384 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
385 goto out;
386
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800387 if (any)
388 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389
390 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392 return r;
393}
394
Izik Eiduscea7bb22007-10-17 19:17:48 +0200395int is_error_page(struct page *page)
396{
397 return page == bad_page;
398}
399EXPORT_SYMBOL_GPL(is_error_page);
400
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200401static inline unsigned long bad_hva(void)
402{
403 return PAGE_OFFSET;
404}
405
406int kvm_is_error_hva(unsigned long addr)
407{
408 return addr == bad_hva();
409}
410EXPORT_SYMBOL_GPL(kvm_is_error_hva);
411
Avi Kivitye8207542007-03-30 16:54:30 +0300412static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413{
414 int i;
415
416 for (i = 0; i < kvm->nmemslots; ++i) {
417 struct kvm_memory_slot *memslot = &kvm->memslots[i];
418
419 if (gfn >= memslot->base_gfn
420 && gfn < memslot->base_gfn + memslot->npages)
421 return memslot;
422 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000423 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
Avi Kivitye8207542007-03-30 16:54:30 +0300425
426struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
427{
428 gfn = unalias_gfn(kvm, gfn);
429 return __gfn_to_memslot(kvm, gfn);
430}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431
Izik Eiduse0d62c72007-10-24 23:57:46 +0200432int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
433{
434 int i;
435
436 gfn = unalias_gfn(kvm, gfn);
437 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
438 struct kvm_memory_slot *memslot = &kvm->memslots[i];
439
440 if (gfn >= memslot->base_gfn
441 && gfn < memslot->base_gfn + memslot->npages)
442 return 1;
443 }
444 return 0;
445}
446EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
447
Izik Eidus539cb662007-11-11 22:05:04 +0200448static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
449{
450 struct kvm_memory_slot *slot;
451
452 gfn = unalias_gfn(kvm, gfn);
453 slot = __gfn_to_memslot(kvm, gfn);
454 if (!slot)
455 return bad_hva();
456 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
457}
458
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500459/*
460 * Requires current->mm->mmap_sem to be held
461 */
462static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300463{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500464 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200465 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500466 int npages;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300467
Avi Kivity60395222007-10-21 11:03:36 +0200468 might_sleep();
469
Izik Eidus539cb662007-11-11 22:05:04 +0200470 addr = gfn_to_hva(kvm, gfn);
471 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200472 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200473 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200474 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200475
Izik Eidus539cb662007-11-11 22:05:04 +0200476 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
477 NULL);
478
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500479 if (npages != 1) {
480 get_page(bad_page);
481 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200482 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500483
484 return page[0];
Avi Kivity954bbbc22007-03-30 14:02:32 +0300485}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500486
487struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
488{
489 struct page *page;
490
491 down_read(&current->mm->mmap_sem);
492 page = __gfn_to_page(kvm, gfn);
493 up_read(&current->mm->mmap_sem);
494
495 return page;
496}
497
Avi Kivity954bbbc22007-03-30 14:02:32 +0300498EXPORT_SYMBOL_GPL(gfn_to_page);
499
Izik Eidusb4231d62007-11-20 11:49:33 +0200500void kvm_release_page_clean(struct page *page)
501{
502 put_page(page);
503}
504EXPORT_SYMBOL_GPL(kvm_release_page_clean);
505
506void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200507{
508 if (!PageReserved(page))
509 SetPageDirty(page);
510 put_page(page);
511}
Izik Eidusb4231d62007-11-20 11:49:33 +0200512EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200513
Izik Eidus195aefd2007-10-01 22:14:18 +0200514static int next_segment(unsigned long len, int offset)
515{
516 if (len > PAGE_SIZE - offset)
517 return PAGE_SIZE - offset;
518 else
519 return len;
520}
521
522int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
523 int len)
524{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200525 int r;
526 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200527
Izik Eiduse0506bc2007-11-11 22:10:22 +0200528 addr = gfn_to_hva(kvm, gfn);
529 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200530 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200531 r = copy_from_user(data, (void __user *)addr + offset, len);
532 if (r)
533 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200534 return 0;
535}
536EXPORT_SYMBOL_GPL(kvm_read_guest_page);
537
538int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
539{
540 gfn_t gfn = gpa >> PAGE_SHIFT;
541 int seg;
542 int offset = offset_in_page(gpa);
543 int ret;
544
545 while ((seg = next_segment(len, offset)) != 0) {
546 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
547 if (ret < 0)
548 return ret;
549 offset = 0;
550 len -= seg;
551 data += seg;
552 ++gfn;
553 }
554 return 0;
555}
556EXPORT_SYMBOL_GPL(kvm_read_guest);
557
558int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
559 int offset, int len)
560{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200561 int r;
562 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200563
Izik Eiduse0506bc2007-11-11 22:10:22 +0200564 addr = gfn_to_hva(kvm, gfn);
565 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200566 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200567 r = copy_to_user((void __user *)addr + offset, data, len);
568 if (r)
569 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200570 mark_page_dirty(kvm, gfn);
571 return 0;
572}
573EXPORT_SYMBOL_GPL(kvm_write_guest_page);
574
575int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
576 unsigned long len)
577{
578 gfn_t gfn = gpa >> PAGE_SHIFT;
579 int seg;
580 int offset = offset_in_page(gpa);
581 int ret;
582
583 while ((seg = next_segment(len, offset)) != 0) {
584 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
585 if (ret < 0)
586 return ret;
587 offset = 0;
588 len -= seg;
589 data += seg;
590 ++gfn;
591 }
592 return 0;
593}
594
595int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
596{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200597 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200598}
599EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
600
601int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
602{
603 gfn_t gfn = gpa >> PAGE_SHIFT;
604 int seg;
605 int offset = offset_in_page(gpa);
606 int ret;
607
608 while ((seg = next_segment(len, offset)) != 0) {
609 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
610 if (ret < 0)
611 return ret;
612 offset = 0;
613 len -= seg;
614 ++gfn;
615 }
616 return 0;
617}
618EXPORT_SYMBOL_GPL(kvm_clear_guest);
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
621{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300622 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Uri Lublin3b6fff12007-10-30 10:42:09 +0200624 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000625 memslot = __gfn_to_memslot(kvm, gfn);
626 if (memslot && memslot->dirty_bitmap) {
627 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628
Rusty Russell7e9d6192007-07-31 20:41:14 +1000629 /* avoid RMW */
630 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
631 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 }
633}
634
Eddie Dongb6958ce2007-07-18 12:15:21 +0300635/*
636 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
637 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500638void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300639{
640 DECLARE_WAITQUEUE(wait, current);
641
642 add_wait_queue(&vcpu->wq, &wait);
643
644 /*
645 * We will block until either an interrupt or a signal wakes us up
646 */
He, Qingc5ec1532007-09-03 17:07:41 +0300647 while (!kvm_cpu_has_interrupt(vcpu)
648 && !signal_pending(current)
649 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
650 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300651 set_current_state(TASK_INTERRUPTIBLE);
652 vcpu_put(vcpu);
653 schedule();
654 vcpu_load(vcpu);
655 }
656
He, Qingc5ec1532007-09-03 17:07:41 +0300657 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300658 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300659}
660
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661void kvm_resched(struct kvm_vcpu *vcpu)
662{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300663 if (!need_resched())
664 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666}
667EXPORT_SYMBOL_GPL(kvm_resched);
668
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200669static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
670 unsigned long address,
671 int *type)
672{
673 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
674 unsigned long pgoff;
675 struct page *page;
676
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200677 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +0200678 if (pgoff == 0)
679 page = virt_to_page(vcpu->run);
680 else if (pgoff == KVM_PIO_PAGE_OFFSET)
681 page = virt_to_page(vcpu->pio_data);
682 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200683 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200684 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300685 if (type != NULL)
686 *type = VM_FAULT_MINOR;
687
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200688 return page;
689}
690
691static struct vm_operations_struct kvm_vcpu_vm_ops = {
692 .nopage = kvm_vcpu_nopage,
693};
694
695static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
696{
697 vma->vm_ops = &kvm_vcpu_vm_ops;
698 return 0;
699}
700
Avi Kivitybccf2152007-02-21 18:04:26 +0200701static int kvm_vcpu_release(struct inode *inode, struct file *filp)
702{
703 struct kvm_vcpu *vcpu = filp->private_data;
704
705 fput(vcpu->kvm->filp);
706 return 0;
707}
708
709static struct file_operations kvm_vcpu_fops = {
710 .release = kvm_vcpu_release,
711 .unlocked_ioctl = kvm_vcpu_ioctl,
712 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200713 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200714};
715
716/*
717 * Allocates an inode for the vcpu.
718 */
719static int create_vcpu_fd(struct kvm_vcpu *vcpu)
720{
721 int fd, r;
722 struct inode *inode;
723 struct file *file;
724
Avi Kivityd6d28162007-06-28 08:38:16 -0400725 r = anon_inode_getfd(&fd, &inode, &file,
726 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
727 if (r)
728 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200729 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200730 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200731}
732
Avi Kivityc5ea7662007-02-20 18:41:05 +0200733/*
734 * Creates some virtual cpus. Good luck creating more than one.
735 */
736static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
737{
738 int r;
739 struct kvm_vcpu *vcpu;
740
Avi Kivityc5ea7662007-02-20 18:41:05 +0200741 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000742 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200743
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800744 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000745 if (IS_ERR(vcpu))
746 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200747
Avi Kivity15ad7142007-07-11 18:17:21 +0300748 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
749
Avi Kivity26e52152007-11-20 15:30:24 +0200750 r = kvm_arch_vcpu_setup(vcpu);
751 if (r)
752 goto vcpu_destroy;
753
Shaohua Li11ec2802007-07-23 14:51:37 +0800754 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000755 if (kvm->vcpus[n]) {
756 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800757 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800758 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000759 }
760 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800761 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000762
763 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200764 r = create_vcpu_fd(vcpu);
765 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000766 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200767 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200768
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000769unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800770 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000771 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800772 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800773vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600774 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200775 return r;
776}
777
Avi Kivity1961d272007-03-05 19:46:05 +0200778static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
779{
780 if (sigset) {
781 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
782 vcpu->sigset_active = 1;
783 vcpu->sigset = *sigset;
784 } else
785 vcpu->sigset_active = 0;
786 return 0;
787}
788
Avi Kivitybccf2152007-02-21 18:04:26 +0200789static long kvm_vcpu_ioctl(struct file *filp,
790 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791{
Avi Kivitybccf2152007-02-21 18:04:26 +0200792 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +0000793 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200794 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200796 if (vcpu->kvm->mm != current->mm)
797 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200799 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200800 r = -EINVAL;
801 if (arg)
802 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500803 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 case KVM_GET_REGS: {
806 struct kvm_regs kvm_regs;
807
Avi Kivitybccf2152007-02-21 18:04:26 +0200808 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500809 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 if (r)
811 goto out;
812 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000813 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 goto out;
815 r = 0;
816 break;
817 }
818 case KVM_SET_REGS: {
819 struct kvm_regs kvm_regs;
820
821 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000822 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500824 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 if (r)
826 goto out;
827 r = 0;
828 break;
829 }
830 case KVM_GET_SREGS: {
831 struct kvm_sregs kvm_sregs;
832
Avi Kivitybccf2152007-02-21 18:04:26 +0200833 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500834 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 if (r)
836 goto out;
837 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000838 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 goto out;
840 r = 0;
841 break;
842 }
843 case KVM_SET_SREGS: {
844 struct kvm_sregs kvm_sregs;
845
846 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000847 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500849 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 if (r)
851 goto out;
852 r = 0;
853 break;
854 }
855 case KVM_TRANSLATE: {
856 struct kvm_translation tr;
857
858 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000859 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800861 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 if (r)
863 goto out;
864 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000865 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 goto out;
867 r = 0;
868 break;
869 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 case KVM_DEBUG_GUEST: {
871 struct kvm_debug_guest dbg;
872
873 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000874 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500876 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 if (r)
878 goto out;
879 r = 0;
880 break;
881 }
Avi Kivity1961d272007-03-05 19:46:05 +0200882 case KVM_SET_SIGNAL_MASK: {
883 struct kvm_signal_mask __user *sigmask_arg = argp;
884 struct kvm_signal_mask kvm_sigmask;
885 sigset_t sigset, *p;
886
887 p = NULL;
888 if (argp) {
889 r = -EFAULT;
890 if (copy_from_user(&kvm_sigmask, argp,
891 sizeof kvm_sigmask))
892 goto out;
893 r = -EINVAL;
894 if (kvm_sigmask.len != sizeof sigset)
895 goto out;
896 r = -EFAULT;
897 if (copy_from_user(&sigset, sigmask_arg->sigset,
898 sizeof sigset))
899 goto out;
900 p = &sigset;
901 }
902 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
903 break;
904 }
Avi Kivityb8836732007-04-01 16:34:31 +0300905 case KVM_GET_FPU: {
906 struct kvm_fpu fpu;
907
908 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500909 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300910 if (r)
911 goto out;
912 r = -EFAULT;
913 if (copy_to_user(argp, &fpu, sizeof fpu))
914 goto out;
915 r = 0;
916 break;
917 }
918 case KVM_SET_FPU: {
919 struct kvm_fpu fpu;
920
921 r = -EFAULT;
922 if (copy_from_user(&fpu, argp, sizeof fpu))
923 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500924 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300925 if (r)
926 goto out;
927 r = 0;
928 break;
929 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200930 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200931 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200932 }
933out:
934 return r;
935}
936
937static long kvm_vm_ioctl(struct file *filp,
938 unsigned int ioctl, unsigned long arg)
939{
940 struct kvm *kvm = filp->private_data;
941 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +0100942 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200943
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200944 if (kvm->mm != current->mm)
945 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200946 switch (ioctl) {
947 case KVM_CREATE_VCPU:
948 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
949 if (r < 0)
950 goto out;
951 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200952 case KVM_SET_USER_MEMORY_REGION: {
953 struct kvm_userspace_memory_region kvm_userspace_mem;
954
955 r = -EFAULT;
956 if (copy_from_user(&kvm_userspace_mem, argp,
957 sizeof kvm_userspace_mem))
958 goto out;
959
960 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 if (r)
962 goto out;
963 break;
964 }
965 case KVM_GET_DIRTY_LOG: {
966 struct kvm_dirty_log log;
967
968 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000969 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200971 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 if (r)
973 goto out;
974 break;
975 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200976 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +0100977 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +0200978 }
979out:
980 return r;
981}
982
983static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
984 unsigned long address,
985 int *type)
986{
987 struct kvm *kvm = vma->vm_file->private_data;
988 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +0200989 struct page *page;
990
Avi Kivityf17abe92007-02-21 19:28:04 +0200991 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200992 if (!kvm_is_visible_gfn(kvm, pgoff))
993 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500994 /* current->mm->mmap_sem is already held so call lockless version */
995 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200996 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200997 kvm_release_page_clean(page);
Avi Kivityf17abe92007-02-21 19:28:04 +0200998 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200999 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03001000 if (type != NULL)
1001 *type = VM_FAULT_MINOR;
1002
Avi Kivityf17abe92007-02-21 19:28:04 +02001003 return page;
1004}
1005
1006static struct vm_operations_struct kvm_vm_vm_ops = {
1007 .nopage = kvm_vm_nopage,
1008};
1009
1010static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1011{
1012 vma->vm_ops = &kvm_vm_vm_ops;
1013 return 0;
1014}
1015
1016static struct file_operations kvm_vm_fops = {
1017 .release = kvm_vm_release,
1018 .unlocked_ioctl = kvm_vm_ioctl,
1019 .compat_ioctl = kvm_vm_ioctl,
1020 .mmap = kvm_vm_mmap,
1021};
1022
1023static int kvm_dev_ioctl_create_vm(void)
1024{
1025 int fd, r;
1026 struct inode *inode;
1027 struct file *file;
1028 struct kvm *kvm;
1029
Avi Kivityf17abe92007-02-21 19:28:04 +02001030 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001031 if (IS_ERR(kvm))
1032 return PTR_ERR(kvm);
1033 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1034 if (r) {
1035 kvm_destroy_vm(kvm);
1036 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001037 }
1038
Avi Kivitybccf2152007-02-21 18:04:26 +02001039 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001040
Avi Kivityf17abe92007-02-21 19:28:04 +02001041 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001042}
1043
1044static long kvm_dev_ioctl(struct file *filp,
1045 unsigned int ioctl, unsigned long arg)
1046{
1047 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001048 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001049
1050 switch (ioctl) {
1051 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001052 r = -EINVAL;
1053 if (arg)
1054 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001055 r = KVM_API_VERSION;
1056 break;
1057 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001058 r = -EINVAL;
1059 if (arg)
1060 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001061 r = kvm_dev_ioctl_create_vm();
1062 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001063 case KVM_CHECK_EXTENSION:
1064 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001065 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001066 case KVM_GET_VCPU_MMAP_SIZE:
1067 r = -EINVAL;
1068 if (arg)
1069 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001070 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001071 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001073 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 }
1075out:
1076 return r;
1077}
1078
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 .unlocked_ioctl = kvm_dev_ioctl,
1081 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082};
1083
1084static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001085 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 "kvm",
1087 &kvm_chardev_ops,
1088};
1089
Avi Kivity1b6c0162007-05-24 13:03:52 +03001090static void hardware_enable(void *junk)
1091{
1092 int cpu = raw_smp_processor_id();
1093
1094 if (cpu_isset(cpu, cpus_hardware_enabled))
1095 return;
1096 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001097 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001098}
1099
1100static void hardware_disable(void *junk)
1101{
1102 int cpu = raw_smp_processor_id();
1103
1104 if (!cpu_isset(cpu, cpus_hardware_enabled))
1105 return;
1106 cpu_clear(cpu, cpus_hardware_enabled);
1107 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001108 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001109}
1110
Avi Kivity774c47f2007-02-12 00:54:47 -08001111static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1112 void *v)
1113{
1114 int cpu = (long)v;
1115
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001116 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001117 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001118 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001119 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1120 cpu);
1121 hardware_disable(NULL);
1122 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001123 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001124 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1125 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001126 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001127 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001128 case CPU_ONLINE:
1129 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1130 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001131 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001132 break;
1133 }
1134 return NOTIFY_OK;
1135}
1136
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001137static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001138 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001139{
1140 if (val == SYS_RESTART) {
1141 /*
1142 * Some (well, at least mine) BIOSes hang on reboot if
1143 * in vmx root mode.
1144 */
1145 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1146 on_each_cpu(hardware_disable, NULL, 0, 1);
1147 }
1148 return NOTIFY_OK;
1149}
1150
1151static struct notifier_block kvm_reboot_notifier = {
1152 .notifier_call = kvm_reboot,
1153 .priority = 0,
1154};
1155
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001156void kvm_io_bus_init(struct kvm_io_bus *bus)
1157{
1158 memset(bus, 0, sizeof(*bus));
1159}
1160
1161void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1162{
1163 int i;
1164
1165 for (i = 0; i < bus->dev_count; i++) {
1166 struct kvm_io_device *pos = bus->devs[i];
1167
1168 kvm_iodevice_destructor(pos);
1169 }
1170}
1171
1172struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1173{
1174 int i;
1175
1176 for (i = 0; i < bus->dev_count; i++) {
1177 struct kvm_io_device *pos = bus->devs[i];
1178
1179 if (pos->in_range(pos, addr))
1180 return pos;
1181 }
1182
1183 return NULL;
1184}
1185
1186void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1187{
1188 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1189
1190 bus->devs[bus->dev_count++] = dev;
1191}
1192
Avi Kivity774c47f2007-02-12 00:54:47 -08001193static struct notifier_block kvm_cpu_notifier = {
1194 .notifier_call = kvm_cpu_hotplug,
1195 .priority = 20, /* must be > scheduler priority */
1196};
1197
Avi Kivityba1389b2007-11-18 16:24:12 +02001198static u64 vm_stat_get(void *_offset)
1199{
1200 unsigned offset = (long)_offset;
1201 u64 total = 0;
1202 struct kvm *kvm;
1203
1204 spin_lock(&kvm_lock);
1205 list_for_each_entry(kvm, &vm_list, vm_list)
1206 total += *(u32 *)((void *)kvm + offset);
1207 spin_unlock(&kvm_lock);
1208 return total;
1209}
1210
1211DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1212
1213static u64 vcpu_stat_get(void *_offset)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001214{
1215 unsigned offset = (long)_offset;
1216 u64 total = 0;
1217 struct kvm *kvm;
1218 struct kvm_vcpu *vcpu;
1219 int i;
1220
1221 spin_lock(&kvm_lock);
1222 list_for_each_entry(kvm, &vm_list, vm_list)
1223 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001224 vcpu = kvm->vcpus[i];
1225 if (vcpu)
1226 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001227 }
1228 spin_unlock(&kvm_lock);
1229 return total;
1230}
1231
Avi Kivityba1389b2007-11-18 16:24:12 +02001232DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1233
1234static struct file_operations *stat_fops[] = {
1235 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1236 [KVM_STAT_VM] = &vm_stat_fops,
1237};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001238
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001239static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240{
1241 struct kvm_stats_debugfs_item *p;
1242
Al Viro8b6d44c2007-02-09 16:38:40 +00001243 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001245 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1246 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001247 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248}
1249
1250static void kvm_exit_debug(void)
1251{
1252 struct kvm_stats_debugfs_item *p;
1253
1254 for (p = debugfs_entries; p->name; ++p)
1255 debugfs_remove(p->dentry);
1256 debugfs_remove(debugfs_dir);
1257}
1258
Avi Kivity59ae6c62007-02-12 00:54:48 -08001259static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1260{
Avi Kivity4267c412007-05-24 13:09:41 +03001261 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001262 return 0;
1263}
1264
1265static int kvm_resume(struct sys_device *dev)
1266{
Avi Kivity4267c412007-05-24 13:09:41 +03001267 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001268 return 0;
1269}
1270
1271static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001272 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001273 .suspend = kvm_suspend,
1274 .resume = kvm_resume,
1275};
1276
1277static struct sys_device kvm_sysdev = {
1278 .id = 0,
1279 .cls = &kvm_sysdev_class,
1280};
1281
Izik Eiduscea7bb22007-10-17 19:17:48 +02001282struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283
Avi Kivity15ad7142007-07-11 18:17:21 +03001284static inline
1285struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1286{
1287 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1288}
1289
1290static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1291{
1292 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1293
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001294 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001295}
1296
1297static void kvm_sched_out(struct preempt_notifier *pn,
1298 struct task_struct *next)
1299{
1300 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1301
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001302 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001303}
1304
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001305int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001306 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
1308 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001309 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001311 kvm_init_debug();
1312
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001313 r = kvm_arch_init(opaque);
1314 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001315 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001316
1317 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1318
1319 if (bad_page == NULL) {
1320 r = -ENOMEM;
1321 goto out;
1322 }
1323
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001324 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001326 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
Yang, Sheng002c7f72007-07-31 14:23:01 +03001328 for_each_online_cpu(cpu) {
1329 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001330 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001331 &r, 0, 1);
1332 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001333 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001334 }
1335
Avi Kivity1b6c0162007-05-24 13:03:52 +03001336 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001337 r = register_cpu_notifier(&kvm_cpu_notifier);
1338 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001339 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 register_reboot_notifier(&kvm_reboot_notifier);
1341
Avi Kivity59ae6c62007-02-12 00:54:48 -08001342 r = sysdev_class_register(&kvm_sysdev_class);
1343 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001344 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001345
1346 r = sysdev_register(&kvm_sysdev);
1347 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001348 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001349
Rusty Russellc16f8622007-07-30 21:12:19 +10001350 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1351 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001352 __alignof__(struct kvm_vcpu),
1353 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001354 if (!kvm_vcpu_cache) {
1355 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001356 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001357 }
1358
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 kvm_chardev_ops.owner = module;
1360
1361 r = misc_register(&kvm_dev);
1362 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001363 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 goto out_free;
1365 }
1366
Avi Kivity15ad7142007-07-11 18:17:21 +03001367 kvm_preempt_ops.sched_in = kvm_sched_in;
1368 kvm_preempt_ops.sched_out = kvm_sched_out;
1369
Avi Kivityc7addb92007-09-16 18:58:32 +02001370 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371
1372out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001373 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001374out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001375 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001376out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001377 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001378out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001380 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001381out_free_2:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001382 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001383out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001384 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001385out_free_0:
1386 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001387out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001388 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001389 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001390out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 return r;
1392}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001393EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001395void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396{
1397 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001398 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001399 sysdev_unregister(&kvm_sysdev);
1400 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001402 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001403 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001404 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001405 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001407 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001409EXPORT_SYMBOL_GPL(kvm_exit);