blob: 058366f480dc69e47a4112e9adc45eb57f67cf2b [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"
Avi Kivity6aa8b732006-12-10 02:21:36 -080021
22#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/percpu.h>
26#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/miscdevice.h>
29#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/debugfs.h>
32#include <linux/highmem.h>
33#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080034#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080035#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040036#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030037#include <linux/cpumask.h>
38#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040039#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030040#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050041#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020042#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050043#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/io.h>
47#include <asm/uaccess.h>
48#include <asm/desc.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020049#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
51MODULE_AUTHOR("Qumranet");
52MODULE_LICENSE("GPL");
53
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080054DEFINE_SPINLOCK(kvm_lock);
55LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080056
Avi Kivity1b6c0162007-05-24 13:03:52 +030057static cpumask_t cpus_hardware_enabled;
58
Rusty Russellc16f8622007-07-30 21:12:19 +100059struct kmem_cache *kvm_vcpu_cache;
60EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030061
Avi Kivity15ad7142007-07-11 18:17:21 +030062static __read_mostly struct preempt_ops kvm_preempt_ops;
63
Avi Kivity6aa8b732006-12-10 02:21:36 -080064static struct dentry *debugfs_dir;
65
Avi Kivitybccf2152007-02-21 18:04:26 +020066static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
67 unsigned long arg);
68
James Morris5aacf0c2006-12-22 01:04:55 -080069static inline int valid_vcpu(int n)
70{
71 return likely(n >= 0 && n < KVM_MAX_VCPUS);
72}
73
Avi Kivity6aa8b732006-12-10 02:21:36 -080074/*
75 * Switches to specified vcpu, until a matching vcpu_put()
76 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020077void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080078{
Avi Kivity15ad7142007-07-11 18:17:21 +030079 int cpu;
80
Avi Kivitybccf2152007-02-21 18:04:26 +020081 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030082 cpu = get_cpu();
83 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020084 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030085 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020086}
87
Carsten Otte313a3dc2007-10-11 19:16:52 +020088void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089{
Avi Kivity15ad7142007-07-11 18:17:21 +030090 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020091 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030092 preempt_notifier_unregister(&vcpu->preempt_notifier);
93 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080094 mutex_unlock(&vcpu->mutex);
95}
96
Avi Kivityd9e368d2007-06-07 19:18:30 +030097static void ack_flush(void *_completed)
98{
Avi Kivityd9e368d2007-06-07 19:18:30 +030099}
100
101void kvm_flush_remote_tlbs(struct kvm *kvm)
102{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200103 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300104 cpumask_t cpus;
105 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300106
Avi Kivityd9e368d2007-06-07 19:18:30 +0300107 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000108 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
109 vcpu = kvm->vcpus[i];
110 if (!vcpu)
111 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200112 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300113 continue;
114 cpu = vcpu->cpu;
115 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200116 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300117 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200118 if (cpus_empty(cpus))
119 return;
120 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200121 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300122}
123
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000124int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
125{
126 struct page *page;
127 int r;
128
129 mutex_init(&vcpu->mutex);
130 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000131 vcpu->kvm = kvm;
132 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300133 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000134
135 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
136 if (!page) {
137 r = -ENOMEM;
138 goto fail;
139 }
140 vcpu->run = page_address(page);
141
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800142 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000143 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800144 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000145 return 0;
146
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000147fail_free_run:
148 free_page((unsigned long)vcpu->run);
149fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000150 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000151}
152EXPORT_SYMBOL_GPL(kvm_vcpu_init);
153
154void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
155{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800156 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000157 free_page((unsigned long)vcpu->run);
158}
159EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
160
Avi Kivityf17abe92007-02-21 19:28:04 +0200161static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800163 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800165 if (IS_ERR(kvm))
166 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200168 kvm->mm = current->mm;
169 atomic_inc(&kvm->mm->mm_count);
Eddie Dong74906342007-06-19 18:05:03 +0300170 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800171 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400172 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000173 spin_lock(&kvm_lock);
174 list_add(&kvm->vm_list, &vm_list);
175 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800176out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200177 return kvm;
178}
179
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180/*
181 * Free any memory in @free but not in @dont.
182 */
183static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
184 struct kvm_memory_slot *dont)
185{
Izik Eidus290fc382007-09-27 14:11:22 +0200186 if (!dont || free->rmap != dont->rmap)
187 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188
189 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
190 vfree(free->dirty_bitmap);
191
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000193 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500194 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195}
196
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800197void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198{
199 int i;
200
201 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000202 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203}
204
Avi Kivityf17abe92007-02-21 19:28:04 +0200205static void kvm_destroy_vm(struct kvm *kvm)
206{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200207 struct mm_struct *mm = kvm->mm;
208
Avi Kivity133de902007-02-12 00:54:44 -0800209 spin_lock(&kvm_lock);
210 list_del(&kvm->vm_list);
211 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300212 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400213 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800214 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200215 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200216}
217
218static int kvm_vm_release(struct inode *inode, struct file *filp)
219{
220 struct kvm *kvm = filp->private_data;
221
222 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 return 0;
224}
225
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 * Allocate some memory and give it an address in the guest physical address
228 * space.
229 *
230 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800231 *
232 * Must be called holding kvm->lock.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800234int __kvm_set_memory_region(struct kvm *kvm,
235 struct kvm_userspace_memory_region *mem,
236 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237{
238 int r;
239 gfn_t base_gfn;
240 unsigned long npages;
241 unsigned long i;
242 struct kvm_memory_slot *memslot;
243 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244
245 r = -EINVAL;
246 /* General sanity checks */
247 if (mem->memory_size & (PAGE_SIZE - 1))
248 goto out;
249 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
250 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200251 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252 goto out;
253 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
254 goto out;
255
256 memslot = &kvm->memslots[mem->slot];
257 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
258 npages = mem->memory_size >> PAGE_SHIFT;
259
260 if (!npages)
261 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
262
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 new = old = *memslot;
264
265 new.base_gfn = base_gfn;
266 new.npages = npages;
267 new.flags = mem->flags;
268
269 /* Disallow changing a memory slot's size. */
270 r = -EINVAL;
271 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800272 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273
274 /* Check for overlaps */
275 r = -EEXIST;
276 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
277 struct kvm_memory_slot *s = &kvm->memslots[i];
278
279 if (s == memslot)
280 continue;
281 if (!((base_gfn + npages <= s->base_gfn) ||
282 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800283 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 /* Free page dirty bitmap if unneeded */
287 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000288 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289
290 r = -ENOMEM;
291
292 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500293 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400294 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200295
296 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800297 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200298
Izik Eidus290fc382007-09-27 14:11:22 +0200299 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500300
Izik Eidus80b14b52007-10-25 11:54:04 +0200301 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800302 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 }
304
305 /* Allocate page dirty bitmap if needed */
306 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
307 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
308
309 new.dirty_bitmap = vmalloc(dirty_bytes);
310 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800311 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312 memset(new.dirty_bitmap, 0, dirty_bytes);
313 }
314
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 if (mem->slot >= kvm->nmemslots)
316 kvm->nmemslots = mem->slot + 1;
317
318 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319
Zhang Xiantao0de10342007-11-20 16:25:04 +0800320 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
321 if (r) {
322 *memslot = old;
323 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800324 }
325
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 kvm_free_physmem_slot(&old, &new);
327 return 0;
328
Sheng Yangf78e0e22007-10-29 09:40:42 +0800329out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 kvm_free_physmem_slot(&new, &old);
331out:
332 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200333
334}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800335EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
336
337int kvm_set_memory_region(struct kvm *kvm,
338 struct kvm_userspace_memory_region *mem,
339 int user_alloc)
340{
341 int r;
342
343 mutex_lock(&kvm->lock);
344 r = __kvm_set_memory_region(kvm, mem, user_alloc);
345 mutex_unlock(&kvm->lock);
346 return r;
347}
Izik Eidus210c7c42007-10-24 23:52:57 +0200348EXPORT_SYMBOL_GPL(kvm_set_memory_region);
349
Carsten Otte1fe779f2007-10-29 16:08:35 +0100350int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
351 struct
352 kvm_userspace_memory_region *mem,
353 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200354{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200355 if (mem->slot >= KVM_MEMORY_SLOTS)
356 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200357 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358}
359
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800360int kvm_get_dirty_log(struct kvm *kvm,
361 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362{
363 struct kvm_memory_slot *memslot;
364 int r, i;
365 int n;
366 unsigned long any = 0;
367
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 r = -EINVAL;
369 if (log->slot >= KVM_MEMORY_SLOTS)
370 goto out;
371
372 memslot = &kvm->memslots[log->slot];
373 r = -ENOENT;
374 if (!memslot->dirty_bitmap)
375 goto out;
376
Uri Lublincd1a4a92007-02-22 16:43:09 +0200377 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378
Uri Lublincd1a4a92007-02-22 16:43:09 +0200379 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 any = memslot->dirty_bitmap[i];
381
382 r = -EFAULT;
383 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
384 goto out;
385
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800386 if (any)
387 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388
389 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 return r;
392}
393
Izik Eiduscea7bb22007-10-17 19:17:48 +0200394int is_error_page(struct page *page)
395{
396 return page == bad_page;
397}
398EXPORT_SYMBOL_GPL(is_error_page);
399
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200400static inline unsigned long bad_hva(void)
401{
402 return PAGE_OFFSET;
403}
404
405int kvm_is_error_hva(unsigned long addr)
406{
407 return addr == bad_hva();
408}
409EXPORT_SYMBOL_GPL(kvm_is_error_hva);
410
Avi Kivitye8207542007-03-30 16:54:30 +0300411static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412{
413 int i;
414
415 for (i = 0; i < kvm->nmemslots; ++i) {
416 struct kvm_memory_slot *memslot = &kvm->memslots[i];
417
418 if (gfn >= memslot->base_gfn
419 && gfn < memslot->base_gfn + memslot->npages)
420 return memslot;
421 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000422 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423}
Avi Kivitye8207542007-03-30 16:54:30 +0300424
425struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
426{
427 gfn = unalias_gfn(kvm, gfn);
428 return __gfn_to_memslot(kvm, gfn);
429}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430
Izik Eiduse0d62c72007-10-24 23:57:46 +0200431int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
432{
433 int i;
434
435 gfn = unalias_gfn(kvm, gfn);
436 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
437 struct kvm_memory_slot *memslot = &kvm->memslots[i];
438
439 if (gfn >= memslot->base_gfn
440 && gfn < memslot->base_gfn + memslot->npages)
441 return 1;
442 }
443 return 0;
444}
445EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
446
Izik Eidus539cb662007-11-11 22:05:04 +0200447static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
448{
449 struct kvm_memory_slot *slot;
450
451 gfn = unalias_gfn(kvm, gfn);
452 slot = __gfn_to_memslot(kvm, gfn);
453 if (!slot)
454 return bad_hva();
455 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
456}
457
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500458/*
459 * Requires current->mm->mmap_sem to be held
460 */
461static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300462{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500463 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200464 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500465 int npages;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300466
Avi Kivity60395222007-10-21 11:03:36 +0200467 might_sleep();
468
Izik Eidus539cb662007-11-11 22:05:04 +0200469 addr = gfn_to_hva(kvm, gfn);
470 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200471 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200472 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200473 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200474
Izik Eidus539cb662007-11-11 22:05:04 +0200475 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
476 NULL);
477
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500478 if (npages != 1) {
479 get_page(bad_page);
480 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200481 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500482
483 return page[0];
Avi Kivity954bbbc22007-03-30 14:02:32 +0300484}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500485
486struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
487{
488 struct page *page;
489
490 down_read(&current->mm->mmap_sem);
491 page = __gfn_to_page(kvm, gfn);
492 up_read(&current->mm->mmap_sem);
493
494 return page;
495}
496
Avi Kivity954bbbc22007-03-30 14:02:32 +0300497EXPORT_SYMBOL_GPL(gfn_to_page);
498
Izik Eidusb4231d62007-11-20 11:49:33 +0200499void kvm_release_page_clean(struct page *page)
500{
501 put_page(page);
502}
503EXPORT_SYMBOL_GPL(kvm_release_page_clean);
504
505void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200506{
507 if (!PageReserved(page))
508 SetPageDirty(page);
509 put_page(page);
510}
Izik Eidusb4231d62007-11-20 11:49:33 +0200511EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200512
Izik Eidus195aefd2007-10-01 22:14:18 +0200513static int next_segment(unsigned long len, int offset)
514{
515 if (len > PAGE_SIZE - offset)
516 return PAGE_SIZE - offset;
517 else
518 return len;
519}
520
521int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
522 int len)
523{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200524 int r;
525 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200526
Izik Eiduse0506bc2007-11-11 22:10:22 +0200527 addr = gfn_to_hva(kvm, gfn);
528 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200529 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200530 r = copy_from_user(data, (void __user *)addr + offset, len);
531 if (r)
532 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200533 return 0;
534}
535EXPORT_SYMBOL_GPL(kvm_read_guest_page);
536
537int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
538{
539 gfn_t gfn = gpa >> PAGE_SHIFT;
540 int seg;
541 int offset = offset_in_page(gpa);
542 int ret;
543
544 while ((seg = next_segment(len, offset)) != 0) {
545 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
546 if (ret < 0)
547 return ret;
548 offset = 0;
549 len -= seg;
550 data += seg;
551 ++gfn;
552 }
553 return 0;
554}
555EXPORT_SYMBOL_GPL(kvm_read_guest);
556
557int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
558 int offset, int len)
559{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200560 int r;
561 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200562
Izik Eiduse0506bc2007-11-11 22:10:22 +0200563 addr = gfn_to_hva(kvm, gfn);
564 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200565 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200566 r = copy_to_user((void __user *)addr + offset, data, len);
567 if (r)
568 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200569 mark_page_dirty(kvm, gfn);
570 return 0;
571}
572EXPORT_SYMBOL_GPL(kvm_write_guest_page);
573
574int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
575 unsigned long len)
576{
577 gfn_t gfn = gpa >> PAGE_SHIFT;
578 int seg;
579 int offset = offset_in_page(gpa);
580 int ret;
581
582 while ((seg = next_segment(len, offset)) != 0) {
583 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
584 if (ret < 0)
585 return ret;
586 offset = 0;
587 len -= seg;
588 data += seg;
589 ++gfn;
590 }
591 return 0;
592}
593
594int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
595{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200596 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200597}
598EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
599
600int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
601{
602 gfn_t gfn = gpa >> PAGE_SHIFT;
603 int seg;
604 int offset = offset_in_page(gpa);
605 int ret;
606
607 while ((seg = next_segment(len, offset)) != 0) {
608 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
609 if (ret < 0)
610 return ret;
611 offset = 0;
612 len -= seg;
613 ++gfn;
614 }
615 return 0;
616}
617EXPORT_SYMBOL_GPL(kvm_clear_guest);
618
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
620{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300621 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622
Uri Lublin3b6fff12007-10-30 10:42:09 +0200623 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000624 memslot = __gfn_to_memslot(kvm, gfn);
625 if (memslot && memslot->dirty_bitmap) {
626 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627
Rusty Russell7e9d6192007-07-31 20:41:14 +1000628 /* avoid RMW */
629 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
630 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 }
632}
633
Eddie Dongb6958ce2007-07-18 12:15:21 +0300634/*
635 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
636 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500637void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300638{
639 DECLARE_WAITQUEUE(wait, current);
640
641 add_wait_queue(&vcpu->wq, &wait);
642
643 /*
644 * We will block until either an interrupt or a signal wakes us up
645 */
He, Qingc5ec1532007-09-03 17:07:41 +0300646 while (!kvm_cpu_has_interrupt(vcpu)
647 && !signal_pending(current)
648 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
649 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300650 set_current_state(TASK_INTERRUPTIBLE);
651 vcpu_put(vcpu);
652 schedule();
653 vcpu_load(vcpu);
654 }
655
He, Qingc5ec1532007-09-03 17:07:41 +0300656 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300657 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300658}
659
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660void kvm_resched(struct kvm_vcpu *vcpu)
661{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300662 if (!need_resched())
663 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665}
666EXPORT_SYMBOL_GPL(kvm_resched);
667
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200668static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
669 unsigned long address,
670 int *type)
671{
672 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
673 unsigned long pgoff;
674 struct page *page;
675
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200676 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +0200677 if (pgoff == 0)
678 page = virt_to_page(vcpu->run);
679 else if (pgoff == KVM_PIO_PAGE_OFFSET)
680 page = virt_to_page(vcpu->pio_data);
681 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200682 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200683 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300684 if (type != NULL)
685 *type = VM_FAULT_MINOR;
686
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200687 return page;
688}
689
690static struct vm_operations_struct kvm_vcpu_vm_ops = {
691 .nopage = kvm_vcpu_nopage,
692};
693
694static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
695{
696 vma->vm_ops = &kvm_vcpu_vm_ops;
697 return 0;
698}
699
Avi Kivitybccf2152007-02-21 18:04:26 +0200700static int kvm_vcpu_release(struct inode *inode, struct file *filp)
701{
702 struct kvm_vcpu *vcpu = filp->private_data;
703
704 fput(vcpu->kvm->filp);
705 return 0;
706}
707
708static struct file_operations kvm_vcpu_fops = {
709 .release = kvm_vcpu_release,
710 .unlocked_ioctl = kvm_vcpu_ioctl,
711 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200712 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200713};
714
715/*
716 * Allocates an inode for the vcpu.
717 */
718static int create_vcpu_fd(struct kvm_vcpu *vcpu)
719{
720 int fd, r;
721 struct inode *inode;
722 struct file *file;
723
Avi Kivityd6d28162007-06-28 08:38:16 -0400724 r = anon_inode_getfd(&fd, &inode, &file,
725 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
726 if (r)
727 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200728 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200729 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200730}
731
Avi Kivityc5ea7662007-02-20 18:41:05 +0200732/*
733 * Creates some virtual cpus. Good luck creating more than one.
734 */
735static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
736{
737 int r;
738 struct kvm_vcpu *vcpu;
739
Avi Kivityc5ea7662007-02-20 18:41:05 +0200740 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000741 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200742
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800743 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000744 if (IS_ERR(vcpu))
745 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200746
Avi Kivity15ad7142007-07-11 18:17:21 +0300747 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
748
Avi Kivity26e52152007-11-20 15:30:24 +0200749 r = kvm_arch_vcpu_setup(vcpu);
750 if (r)
751 goto vcpu_destroy;
752
Shaohua Li11ec2802007-07-23 14:51:37 +0800753 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000754 if (kvm->vcpus[n]) {
755 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800756 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800757 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000758 }
759 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800760 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000761
762 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200763 r = create_vcpu_fd(vcpu);
764 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000765 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200766 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200767
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000768unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800769 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000770 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800771 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800772vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600773 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200774 return r;
775}
776
Avi Kivity1961d272007-03-05 19:46:05 +0200777static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
778{
779 if (sigset) {
780 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
781 vcpu->sigset_active = 1;
782 vcpu->sigset = *sigset;
783 } else
784 vcpu->sigset_active = 0;
785 return 0;
786}
787
Avi Kivitybccf2152007-02-21 18:04:26 +0200788static long kvm_vcpu_ioctl(struct file *filp,
789 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790{
Avi Kivitybccf2152007-02-21 18:04:26 +0200791 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +0000792 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200793 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200795 if (vcpu->kvm->mm != current->mm)
796 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200798 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200799 r = -EINVAL;
800 if (arg)
801 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500802 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 case KVM_GET_REGS: {
805 struct kvm_regs kvm_regs;
806
Avi Kivitybccf2152007-02-21 18:04:26 +0200807 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500808 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 if (r)
810 goto out;
811 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000812 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 goto out;
814 r = 0;
815 break;
816 }
817 case KVM_SET_REGS: {
818 struct kvm_regs kvm_regs;
819
820 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000821 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500823 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 if (r)
825 goto out;
826 r = 0;
827 break;
828 }
829 case KVM_GET_SREGS: {
830 struct kvm_sregs kvm_sregs;
831
Avi Kivitybccf2152007-02-21 18:04:26 +0200832 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500833 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 if (r)
835 goto out;
836 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000837 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 goto out;
839 r = 0;
840 break;
841 }
842 case KVM_SET_SREGS: {
843 struct kvm_sregs kvm_sregs;
844
845 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000846 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500848 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 if (r)
850 goto out;
851 r = 0;
852 break;
853 }
854 case KVM_TRANSLATE: {
855 struct kvm_translation tr;
856
857 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000858 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800860 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 if (r)
862 goto out;
863 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000864 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 goto out;
866 r = 0;
867 break;
868 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 case KVM_DEBUG_GUEST: {
870 struct kvm_debug_guest dbg;
871
872 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000873 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500875 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 if (r)
877 goto out;
878 r = 0;
879 break;
880 }
Avi Kivity1961d272007-03-05 19:46:05 +0200881 case KVM_SET_SIGNAL_MASK: {
882 struct kvm_signal_mask __user *sigmask_arg = argp;
883 struct kvm_signal_mask kvm_sigmask;
884 sigset_t sigset, *p;
885
886 p = NULL;
887 if (argp) {
888 r = -EFAULT;
889 if (copy_from_user(&kvm_sigmask, argp,
890 sizeof kvm_sigmask))
891 goto out;
892 r = -EINVAL;
893 if (kvm_sigmask.len != sizeof sigset)
894 goto out;
895 r = -EFAULT;
896 if (copy_from_user(&sigset, sigmask_arg->sigset,
897 sizeof sigset))
898 goto out;
899 p = &sigset;
900 }
901 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
902 break;
903 }
Avi Kivityb8836732007-04-01 16:34:31 +0300904 case KVM_GET_FPU: {
905 struct kvm_fpu fpu;
906
907 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500908 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300909 if (r)
910 goto out;
911 r = -EFAULT;
912 if (copy_to_user(argp, &fpu, sizeof fpu))
913 goto out;
914 r = 0;
915 break;
916 }
917 case KVM_SET_FPU: {
918 struct kvm_fpu fpu;
919
920 r = -EFAULT;
921 if (copy_from_user(&fpu, argp, sizeof fpu))
922 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500923 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300924 if (r)
925 goto out;
926 r = 0;
927 break;
928 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200929 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200930 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200931 }
932out:
933 return r;
934}
935
936static long kvm_vm_ioctl(struct file *filp,
937 unsigned int ioctl, unsigned long arg)
938{
939 struct kvm *kvm = filp->private_data;
940 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +0100941 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200942
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200943 if (kvm->mm != current->mm)
944 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200945 switch (ioctl) {
946 case KVM_CREATE_VCPU:
947 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
948 if (r < 0)
949 goto out;
950 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200951 case KVM_SET_USER_MEMORY_REGION: {
952 struct kvm_userspace_memory_region kvm_userspace_mem;
953
954 r = -EFAULT;
955 if (copy_from_user(&kvm_userspace_mem, argp,
956 sizeof kvm_userspace_mem))
957 goto out;
958
959 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 if (r)
961 goto out;
962 break;
963 }
964 case KVM_GET_DIRTY_LOG: {
965 struct kvm_dirty_log log;
966
967 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000968 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200970 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 if (r)
972 goto out;
973 break;
974 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200975 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +0100976 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +0200977 }
978out:
979 return r;
980}
981
982static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
983 unsigned long address,
984 int *type)
985{
986 struct kvm *kvm = vma->vm_file->private_data;
987 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +0200988 struct page *page;
989
Avi Kivityf17abe92007-02-21 19:28:04 +0200990 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200991 if (!kvm_is_visible_gfn(kvm, pgoff))
992 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500993 /* current->mm->mmap_sem is already held so call lockless version */
994 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200995 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200996 kvm_release_page_clean(page);
Avi Kivityf17abe92007-02-21 19:28:04 +0200997 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200998 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300999 if (type != NULL)
1000 *type = VM_FAULT_MINOR;
1001
Avi Kivityf17abe92007-02-21 19:28:04 +02001002 return page;
1003}
1004
1005static struct vm_operations_struct kvm_vm_vm_ops = {
1006 .nopage = kvm_vm_nopage,
1007};
1008
1009static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1010{
1011 vma->vm_ops = &kvm_vm_vm_ops;
1012 return 0;
1013}
1014
1015static struct file_operations kvm_vm_fops = {
1016 .release = kvm_vm_release,
1017 .unlocked_ioctl = kvm_vm_ioctl,
1018 .compat_ioctl = kvm_vm_ioctl,
1019 .mmap = kvm_vm_mmap,
1020};
1021
1022static int kvm_dev_ioctl_create_vm(void)
1023{
1024 int fd, r;
1025 struct inode *inode;
1026 struct file *file;
1027 struct kvm *kvm;
1028
Avi Kivityf17abe92007-02-21 19:28:04 +02001029 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001030 if (IS_ERR(kvm))
1031 return PTR_ERR(kvm);
1032 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1033 if (r) {
1034 kvm_destroy_vm(kvm);
1035 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001036 }
1037
Avi Kivitybccf2152007-02-21 18:04:26 +02001038 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001039
Avi Kivityf17abe92007-02-21 19:28:04 +02001040 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001041}
1042
1043static long kvm_dev_ioctl(struct file *filp,
1044 unsigned int ioctl, unsigned long arg)
1045{
1046 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001047 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001048
1049 switch (ioctl) {
1050 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001051 r = -EINVAL;
1052 if (arg)
1053 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001054 r = KVM_API_VERSION;
1055 break;
1056 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001057 r = -EINVAL;
1058 if (arg)
1059 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001060 r = kvm_dev_ioctl_create_vm();
1061 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001062 case KVM_CHECK_EXTENSION:
1063 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001064 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001065 case KVM_GET_VCPU_MMAP_SIZE:
1066 r = -EINVAL;
1067 if (arg)
1068 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001069 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001070 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001072 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 }
1074out:
1075 return r;
1076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 .unlocked_ioctl = kvm_dev_ioctl,
1080 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081};
1082
1083static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001084 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 "kvm",
1086 &kvm_chardev_ops,
1087};
1088
Avi Kivity1b6c0162007-05-24 13:03:52 +03001089static void hardware_enable(void *junk)
1090{
1091 int cpu = raw_smp_processor_id();
1092
1093 if (cpu_isset(cpu, cpus_hardware_enabled))
1094 return;
1095 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001096 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001097}
1098
1099static void hardware_disable(void *junk)
1100{
1101 int cpu = raw_smp_processor_id();
1102
1103 if (!cpu_isset(cpu, cpus_hardware_enabled))
1104 return;
1105 cpu_clear(cpu, cpus_hardware_enabled);
1106 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001107 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001108}
1109
Avi Kivity774c47f2007-02-12 00:54:47 -08001110static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1111 void *v)
1112{
1113 int cpu = (long)v;
1114
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001115 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001116 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001117 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001118 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1119 cpu);
1120 hardware_disable(NULL);
1121 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001122 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001123 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1124 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001125 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001126 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001127 case CPU_ONLINE:
1128 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1129 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001130 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001131 break;
1132 }
1133 return NOTIFY_OK;
1134}
1135
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001136static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001137 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001138{
1139 if (val == SYS_RESTART) {
1140 /*
1141 * Some (well, at least mine) BIOSes hang on reboot if
1142 * in vmx root mode.
1143 */
1144 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1145 on_each_cpu(hardware_disable, NULL, 0, 1);
1146 }
1147 return NOTIFY_OK;
1148}
1149
1150static struct notifier_block kvm_reboot_notifier = {
1151 .notifier_call = kvm_reboot,
1152 .priority = 0,
1153};
1154
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001155void kvm_io_bus_init(struct kvm_io_bus *bus)
1156{
1157 memset(bus, 0, sizeof(*bus));
1158}
1159
1160void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1161{
1162 int i;
1163
1164 for (i = 0; i < bus->dev_count; i++) {
1165 struct kvm_io_device *pos = bus->devs[i];
1166
1167 kvm_iodevice_destructor(pos);
1168 }
1169}
1170
1171struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1172{
1173 int i;
1174
1175 for (i = 0; i < bus->dev_count; i++) {
1176 struct kvm_io_device *pos = bus->devs[i];
1177
1178 if (pos->in_range(pos, addr))
1179 return pos;
1180 }
1181
1182 return NULL;
1183}
1184
1185void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1186{
1187 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1188
1189 bus->devs[bus->dev_count++] = dev;
1190}
1191
Avi Kivity774c47f2007-02-12 00:54:47 -08001192static struct notifier_block kvm_cpu_notifier = {
1193 .notifier_call = kvm_cpu_hotplug,
1194 .priority = 20, /* must be > scheduler priority */
1195};
1196
Avi Kivityba1389b2007-11-18 16:24:12 +02001197static u64 vm_stat_get(void *_offset)
1198{
1199 unsigned offset = (long)_offset;
1200 u64 total = 0;
1201 struct kvm *kvm;
1202
1203 spin_lock(&kvm_lock);
1204 list_for_each_entry(kvm, &vm_list, vm_list)
1205 total += *(u32 *)((void *)kvm + offset);
1206 spin_unlock(&kvm_lock);
1207 return total;
1208}
1209
1210DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1211
1212static u64 vcpu_stat_get(void *_offset)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001213{
1214 unsigned offset = (long)_offset;
1215 u64 total = 0;
1216 struct kvm *kvm;
1217 struct kvm_vcpu *vcpu;
1218 int i;
1219
1220 spin_lock(&kvm_lock);
1221 list_for_each_entry(kvm, &vm_list, vm_list)
1222 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001223 vcpu = kvm->vcpus[i];
1224 if (vcpu)
1225 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001226 }
1227 spin_unlock(&kvm_lock);
1228 return total;
1229}
1230
Avi Kivityba1389b2007-11-18 16:24:12 +02001231DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1232
1233static struct file_operations *stat_fops[] = {
1234 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1235 [KVM_STAT_VM] = &vm_stat_fops,
1236};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001237
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001238static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239{
1240 struct kvm_stats_debugfs_item *p;
1241
Al Viro8b6d44c2007-02-09 16:38:40 +00001242 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001244 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1245 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001246 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247}
1248
1249static void kvm_exit_debug(void)
1250{
1251 struct kvm_stats_debugfs_item *p;
1252
1253 for (p = debugfs_entries; p->name; ++p)
1254 debugfs_remove(p->dentry);
1255 debugfs_remove(debugfs_dir);
1256}
1257
Avi Kivity59ae6c62007-02-12 00:54:48 -08001258static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1259{
Avi Kivity4267c412007-05-24 13:09:41 +03001260 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001261 return 0;
1262}
1263
1264static int kvm_resume(struct sys_device *dev)
1265{
Avi Kivity4267c412007-05-24 13:09:41 +03001266 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001267 return 0;
1268}
1269
1270static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001271 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001272 .suspend = kvm_suspend,
1273 .resume = kvm_resume,
1274};
1275
1276static struct sys_device kvm_sysdev = {
1277 .id = 0,
1278 .cls = &kvm_sysdev_class,
1279};
1280
Izik Eiduscea7bb22007-10-17 19:17:48 +02001281struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Avi Kivity15ad7142007-07-11 18:17:21 +03001283static inline
1284struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1285{
1286 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1287}
1288
1289static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1290{
1291 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1292
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001293 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001294}
1295
1296static void kvm_sched_out(struct preempt_notifier *pn,
1297 struct task_struct *next)
1298{
1299 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1300
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001301 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001302}
1303
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001304int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001305 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306{
1307 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001308 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001310 kvm_init_debug();
1311
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001312 r = kvm_arch_init(opaque);
1313 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001314 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001315
1316 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1317
1318 if (bad_page == NULL) {
1319 r = -ENOMEM;
1320 goto out;
1321 }
1322
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001323 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001325 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Yang, Sheng002c7f72007-07-31 14:23:01 +03001327 for_each_online_cpu(cpu) {
1328 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001329 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001330 &r, 0, 1);
1331 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001332 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001333 }
1334
Avi Kivity1b6c0162007-05-24 13:03:52 +03001335 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001336 r = register_cpu_notifier(&kvm_cpu_notifier);
1337 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001338 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 register_reboot_notifier(&kvm_reboot_notifier);
1340
Avi Kivity59ae6c62007-02-12 00:54:48 -08001341 r = sysdev_class_register(&kvm_sysdev_class);
1342 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001343 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001344
1345 r = sysdev_register(&kvm_sysdev);
1346 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001347 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001348
Rusty Russellc16f8622007-07-30 21:12:19 +10001349 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1350 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001351 __alignof__(struct kvm_vcpu),
1352 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001353 if (!kvm_vcpu_cache) {
1354 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001355 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001356 }
1357
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 kvm_chardev_ops.owner = module;
1359
1360 r = misc_register(&kvm_dev);
1361 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001362 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 goto out_free;
1364 }
1365
Avi Kivity15ad7142007-07-11 18:17:21 +03001366 kvm_preempt_ops.sched_in = kvm_sched_in;
1367 kvm_preempt_ops.sched_out = kvm_sched_out;
1368
Avi Kivityc7addb92007-09-16 18:58:32 +02001369 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370
1371out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001372 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001373out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001374 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001375out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001376 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001377out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001379 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001380out_free_2:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001381 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001382out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001383 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001384out_free_0:
1385 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001386out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001387 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001388 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001389out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 return r;
1391}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001392EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001394void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395{
1396 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001397 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001398 sysdev_unregister(&kvm_sysdev);
1399 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001401 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001402 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001403 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001404 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001406 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001408EXPORT_SYMBOL_GPL(kvm_exit);