blob: 7939b5c9a4b4c20d89f84a1e4cbc6a765097172a [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 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200118 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300119}
120
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000121int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
122{
123 struct page *page;
124 int r;
125
126 mutex_init(&vcpu->mutex);
127 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000128 vcpu->kvm = kvm;
129 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300130 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000131
132 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
133 if (!page) {
134 r = -ENOMEM;
135 goto fail;
136 }
137 vcpu->run = page_address(page);
138
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800139 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000140 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800141 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000142 return 0;
143
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000144fail_free_run:
145 free_page((unsigned long)vcpu->run);
146fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000147 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000148}
149EXPORT_SYMBOL_GPL(kvm_vcpu_init);
150
151void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
152{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800153 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000154 free_page((unsigned long)vcpu->run);
155}
156EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
157
Avi Kivityf17abe92007-02-21 19:28:04 +0200158static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800160 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800162 if (IS_ERR(kvm))
163 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164
Eddie Dong74906342007-06-19 18:05:03 +0300165 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800166 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400167 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000168 spin_lock(&kvm_lock);
169 list_add(&kvm->vm_list, &vm_list);
170 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800171out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200172 return kvm;
173}
174
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175/*
176 * Free any memory in @free but not in @dont.
177 */
178static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
179 struct kvm_memory_slot *dont)
180{
Izik Eidus290fc382007-09-27 14:11:22 +0200181 if (!dont || free->rmap != dont->rmap)
182 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183
184 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
185 vfree(free->dirty_bitmap);
186
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000188 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500189 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190}
191
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800192void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193{
194 int i;
195
196 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000197 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198}
199
Avi Kivityf17abe92007-02-21 19:28:04 +0200200static void kvm_destroy_vm(struct kvm *kvm)
201{
Avi Kivity133de902007-02-12 00:54:44 -0800202 spin_lock(&kvm_lock);
203 list_del(&kvm->vm_list);
204 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300205 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400206 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800207 kvm_arch_destroy_vm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200208}
209
210static int kvm_vm_release(struct inode *inode, struct file *filp)
211{
212 struct kvm *kvm = filp->private_data;
213
214 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 return 0;
216}
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 * Allocate some memory and give it an address in the guest physical address
220 * space.
221 *
222 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800223 *
224 * Must be called holding kvm->lock.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800226int __kvm_set_memory_region(struct kvm *kvm,
227 struct kvm_userspace_memory_region *mem,
228 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229{
230 int r;
231 gfn_t base_gfn;
232 unsigned long npages;
233 unsigned long i;
234 struct kvm_memory_slot *memslot;
235 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236
237 r = -EINVAL;
238 /* General sanity checks */
239 if (mem->memory_size & (PAGE_SIZE - 1))
240 goto out;
241 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
242 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200243 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 goto out;
245 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
246 goto out;
247
248 memslot = &kvm->memslots[mem->slot];
249 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
250 npages = mem->memory_size >> PAGE_SHIFT;
251
252 if (!npages)
253 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 new = old = *memslot;
256
257 new.base_gfn = base_gfn;
258 new.npages = npages;
259 new.flags = mem->flags;
260
261 /* Disallow changing a memory slot's size. */
262 r = -EINVAL;
263 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800264 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265
266 /* Check for overlaps */
267 r = -EEXIST;
268 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
269 struct kvm_memory_slot *s = &kvm->memslots[i];
270
271 if (s == memslot)
272 continue;
273 if (!((base_gfn + npages <= s->base_gfn) ||
274 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800275 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 /* Free page dirty bitmap if unneeded */
279 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000280 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281
282 r = -ENOMEM;
283
284 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500285 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400286 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200287
288 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800289 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200290
Izik Eidus290fc382007-09-27 14:11:22 +0200291 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500292
Izik Eidus80b14b52007-10-25 11:54:04 +0200293 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800294 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 }
296
297 /* Allocate page dirty bitmap if needed */
298 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
299 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
300
301 new.dirty_bitmap = vmalloc(dirty_bytes);
302 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800303 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 memset(new.dirty_bitmap, 0, dirty_bytes);
305 }
306
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 if (mem->slot >= kvm->nmemslots)
308 kvm->nmemslots = mem->slot + 1;
309
310 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311
Zhang Xiantao0de10342007-11-20 16:25:04 +0800312 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
313 if (r) {
314 *memslot = old;
315 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800316 }
317
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318 kvm_free_physmem_slot(&old, &new);
319 return 0;
320
Sheng Yangf78e0e22007-10-29 09:40:42 +0800321out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 kvm_free_physmem_slot(&new, &old);
323out:
324 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200325
326}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800327EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
328
329int kvm_set_memory_region(struct kvm *kvm,
330 struct kvm_userspace_memory_region *mem,
331 int user_alloc)
332{
333 int r;
334
335 mutex_lock(&kvm->lock);
336 r = __kvm_set_memory_region(kvm, mem, user_alloc);
337 mutex_unlock(&kvm->lock);
338 return r;
339}
Izik Eidus210c7c42007-10-24 23:52:57 +0200340EXPORT_SYMBOL_GPL(kvm_set_memory_region);
341
Carsten Otte1fe779f2007-10-29 16:08:35 +0100342int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
343 struct
344 kvm_userspace_memory_region *mem,
345 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200346{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200347 if (mem->slot >= KVM_MEMORY_SLOTS)
348 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200349 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350}
351
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800352int kvm_get_dirty_log(struct kvm *kvm,
353 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354{
355 struct kvm_memory_slot *memslot;
356 int r, i;
357 int n;
358 unsigned long any = 0;
359
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 r = -EINVAL;
361 if (log->slot >= KVM_MEMORY_SLOTS)
362 goto out;
363
364 memslot = &kvm->memslots[log->slot];
365 r = -ENOENT;
366 if (!memslot->dirty_bitmap)
367 goto out;
368
Uri Lublincd1a4a92007-02-22 16:43:09 +0200369 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370
Uri Lublincd1a4a92007-02-22 16:43:09 +0200371 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372 any = memslot->dirty_bitmap[i];
373
374 r = -EFAULT;
375 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
376 goto out;
377
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800378 if (any)
379 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380
381 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 return r;
384}
385
Izik Eiduscea7bb22007-10-17 19:17:48 +0200386int is_error_page(struct page *page)
387{
388 return page == bad_page;
389}
390EXPORT_SYMBOL_GPL(is_error_page);
391
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200392static inline unsigned long bad_hva(void)
393{
394 return PAGE_OFFSET;
395}
396
397int kvm_is_error_hva(unsigned long addr)
398{
399 return addr == bad_hva();
400}
401EXPORT_SYMBOL_GPL(kvm_is_error_hva);
402
Izik Eidus290fc382007-09-27 14:11:22 +0200403gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300404{
405 int i;
406 struct kvm_mem_alias *alias;
407
408 for (i = 0; i < kvm->naliases; ++i) {
409 alias = &kvm->aliases[i];
410 if (gfn >= alias->base_gfn
411 && gfn < alias->base_gfn + alias->npages)
412 return alias->target_gfn + gfn - alias->base_gfn;
413 }
414 return gfn;
415}
416
417static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418{
419 int i;
420
421 for (i = 0; i < kvm->nmemslots; ++i) {
422 struct kvm_memory_slot *memslot = &kvm->memslots[i];
423
424 if (gfn >= memslot->base_gfn
425 && gfn < memslot->base_gfn + memslot->npages)
426 return memslot;
427 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000428 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429}
Avi Kivitye8207542007-03-30 16:54:30 +0300430
431struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
432{
433 gfn = unalias_gfn(kvm, gfn);
434 return __gfn_to_memslot(kvm, gfn);
435}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436
Izik Eiduse0d62c72007-10-24 23:57:46 +0200437int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
438{
439 int i;
440
441 gfn = unalias_gfn(kvm, gfn);
442 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
443 struct kvm_memory_slot *memslot = &kvm->memslots[i];
444
445 if (gfn >= memslot->base_gfn
446 && gfn < memslot->base_gfn + memslot->npages)
447 return 1;
448 }
449 return 0;
450}
451EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
452
Izik Eidus539cb662007-11-11 22:05:04 +0200453static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
454{
455 struct kvm_memory_slot *slot;
456
457 gfn = unalias_gfn(kvm, gfn);
458 slot = __gfn_to_memslot(kvm, gfn);
459 if (!slot)
460 return bad_hva();
461 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
462}
463
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500464/*
465 * Requires current->mm->mmap_sem to be held
466 */
467static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300468{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500469 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200470 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500471 int npages;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300472
Avi Kivity60395222007-10-21 11:03:36 +0200473 might_sleep();
474
Izik Eidus539cb662007-11-11 22:05:04 +0200475 addr = gfn_to_hva(kvm, gfn);
476 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200477 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200478 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200479 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200480
Izik Eidus539cb662007-11-11 22:05:04 +0200481 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
482 NULL);
483
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500484 if (npages != 1) {
485 get_page(bad_page);
486 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200487 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500488
489 return page[0];
Avi Kivity954bbbc22007-03-30 14:02:32 +0300490}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500491
492struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
493{
494 struct page *page;
495
496 down_read(&current->mm->mmap_sem);
497 page = __gfn_to_page(kvm, gfn);
498 up_read(&current->mm->mmap_sem);
499
500 return page;
501}
502
Avi Kivity954bbbc22007-03-30 14:02:32 +0300503EXPORT_SYMBOL_GPL(gfn_to_page);
504
Izik Eidusb4231d62007-11-20 11:49:33 +0200505void kvm_release_page_clean(struct page *page)
506{
507 put_page(page);
508}
509EXPORT_SYMBOL_GPL(kvm_release_page_clean);
510
511void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200512{
513 if (!PageReserved(page))
514 SetPageDirty(page);
515 put_page(page);
516}
Izik Eidusb4231d62007-11-20 11:49:33 +0200517EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200518
Izik Eidus195aefd2007-10-01 22:14:18 +0200519static int next_segment(unsigned long len, int offset)
520{
521 if (len > PAGE_SIZE - offset)
522 return PAGE_SIZE - offset;
523 else
524 return len;
525}
526
527int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
528 int len)
529{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200530 int r;
531 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200532
Izik Eiduse0506bc2007-11-11 22:10:22 +0200533 addr = gfn_to_hva(kvm, gfn);
534 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200535 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200536 r = copy_from_user(data, (void __user *)addr + offset, len);
537 if (r)
538 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200539 return 0;
540}
541EXPORT_SYMBOL_GPL(kvm_read_guest_page);
542
543int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
544{
545 gfn_t gfn = gpa >> PAGE_SHIFT;
546 int seg;
547 int offset = offset_in_page(gpa);
548 int ret;
549
550 while ((seg = next_segment(len, offset)) != 0) {
551 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
552 if (ret < 0)
553 return ret;
554 offset = 0;
555 len -= seg;
556 data += seg;
557 ++gfn;
558 }
559 return 0;
560}
561EXPORT_SYMBOL_GPL(kvm_read_guest);
562
563int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
564 int offset, int len)
565{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200566 int r;
567 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200568
Izik Eiduse0506bc2007-11-11 22:10:22 +0200569 addr = gfn_to_hva(kvm, gfn);
570 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200571 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200572 r = copy_to_user((void __user *)addr + offset, data, len);
573 if (r)
574 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200575 mark_page_dirty(kvm, gfn);
576 return 0;
577}
578EXPORT_SYMBOL_GPL(kvm_write_guest_page);
579
580int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
581 unsigned long len)
582{
583 gfn_t gfn = gpa >> PAGE_SHIFT;
584 int seg;
585 int offset = offset_in_page(gpa);
586 int ret;
587
588 while ((seg = next_segment(len, offset)) != 0) {
589 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
590 if (ret < 0)
591 return ret;
592 offset = 0;
593 len -= seg;
594 data += seg;
595 ++gfn;
596 }
597 return 0;
598}
599
600int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
601{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200602 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200603}
604EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
605
606int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
607{
608 gfn_t gfn = gpa >> PAGE_SHIFT;
609 int seg;
610 int offset = offset_in_page(gpa);
611 int ret;
612
613 while ((seg = next_segment(len, offset)) != 0) {
614 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
615 if (ret < 0)
616 return ret;
617 offset = 0;
618 len -= seg;
619 ++gfn;
620 }
621 return 0;
622}
623EXPORT_SYMBOL_GPL(kvm_clear_guest);
624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
626{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300627 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628
Uri Lublin3b6fff12007-10-30 10:42:09 +0200629 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000630 memslot = __gfn_to_memslot(kvm, gfn);
631 if (memslot && memslot->dirty_bitmap) {
632 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633
Rusty Russell7e9d6192007-07-31 20:41:14 +1000634 /* avoid RMW */
635 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
636 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 }
638}
639
Eddie Dongb6958ce2007-07-18 12:15:21 +0300640/*
641 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
642 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500643void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300644{
645 DECLARE_WAITQUEUE(wait, current);
646
647 add_wait_queue(&vcpu->wq, &wait);
648
649 /*
650 * We will block until either an interrupt or a signal wakes us up
651 */
He, Qingc5ec1532007-09-03 17:07:41 +0300652 while (!kvm_cpu_has_interrupt(vcpu)
653 && !signal_pending(current)
654 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
655 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300656 set_current_state(TASK_INTERRUPTIBLE);
657 vcpu_put(vcpu);
658 schedule();
659 vcpu_load(vcpu);
660 }
661
He, Qingc5ec1532007-09-03 17:07:41 +0300662 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300663 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300664}
665
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666void kvm_resched(struct kvm_vcpu *vcpu)
667{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300668 if (!need_resched())
669 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671}
672EXPORT_SYMBOL_GPL(kvm_resched);
673
Avi Kivitybccf2152007-02-21 18:04:26 +0200674static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
675 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 if (irq->irq < 0 || irq->irq >= 256)
678 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300679 if (irqchip_in_kernel(vcpu->kvm))
680 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200681 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
683 set_bit(irq->irq, vcpu->irq_pending);
684 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
685
686 vcpu_put(vcpu);
687
688 return 0;
689}
690
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200691static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
692 unsigned long address,
693 int *type)
694{
695 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
696 unsigned long pgoff;
697 struct page *page;
698
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200699 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +0200700 if (pgoff == 0)
701 page = virt_to_page(vcpu->run);
702 else if (pgoff == KVM_PIO_PAGE_OFFSET)
703 page = virt_to_page(vcpu->pio_data);
704 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200705 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200706 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300707 if (type != NULL)
708 *type = VM_FAULT_MINOR;
709
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200710 return page;
711}
712
713static struct vm_operations_struct kvm_vcpu_vm_ops = {
714 .nopage = kvm_vcpu_nopage,
715};
716
717static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
718{
719 vma->vm_ops = &kvm_vcpu_vm_ops;
720 return 0;
721}
722
Avi Kivitybccf2152007-02-21 18:04:26 +0200723static int kvm_vcpu_release(struct inode *inode, struct file *filp)
724{
725 struct kvm_vcpu *vcpu = filp->private_data;
726
727 fput(vcpu->kvm->filp);
728 return 0;
729}
730
731static struct file_operations kvm_vcpu_fops = {
732 .release = kvm_vcpu_release,
733 .unlocked_ioctl = kvm_vcpu_ioctl,
734 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200735 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200736};
737
738/*
739 * Allocates an inode for the vcpu.
740 */
741static int create_vcpu_fd(struct kvm_vcpu *vcpu)
742{
743 int fd, r;
744 struct inode *inode;
745 struct file *file;
746
Avi Kivityd6d28162007-06-28 08:38:16 -0400747 r = anon_inode_getfd(&fd, &inode, &file,
748 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
749 if (r)
750 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200751 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200752 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200753}
754
Avi Kivityc5ea7662007-02-20 18:41:05 +0200755/*
756 * Creates some virtual cpus. Good luck creating more than one.
757 */
758static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
759{
760 int r;
761 struct kvm_vcpu *vcpu;
762
Avi Kivityc5ea7662007-02-20 18:41:05 +0200763 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000764 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200765
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800766 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000767 if (IS_ERR(vcpu))
768 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200769
Avi Kivity15ad7142007-07-11 18:17:21 +0300770 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
771
Avi Kivity26e52152007-11-20 15:30:24 +0200772 r = kvm_arch_vcpu_setup(vcpu);
773 if (r)
774 goto vcpu_destroy;
775
Shaohua Li11ec2802007-07-23 14:51:37 +0800776 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000777 if (kvm->vcpus[n]) {
778 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800779 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800780 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781 }
782 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800783 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000784
785 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200786 r = create_vcpu_fd(vcpu);
787 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000788 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200789 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200790
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000791unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800792 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000793 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800794 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800795vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600796 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200797 return r;
798}
799
Avi Kivity1961d272007-03-05 19:46:05 +0200800static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
801{
802 if (sigset) {
803 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
804 vcpu->sigset_active = 1;
805 vcpu->sigset = *sigset;
806 } else
807 vcpu->sigset_active = 0;
808 return 0;
809}
810
Avi Kivitybccf2152007-02-21 18:04:26 +0200811static long kvm_vcpu_ioctl(struct file *filp,
812 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813{
Avi Kivitybccf2152007-02-21 18:04:26 +0200814 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +0000815 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200816 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817
818 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200819 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200820 r = -EINVAL;
821 if (arg)
822 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500823 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 case KVM_GET_REGS: {
826 struct kvm_regs kvm_regs;
827
Avi Kivitybccf2152007-02-21 18:04:26 +0200828 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500829 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 if (r)
831 goto out;
832 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000833 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 goto out;
835 r = 0;
836 break;
837 }
838 case KVM_SET_REGS: {
839 struct kvm_regs kvm_regs;
840
841 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000842 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500844 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 if (r)
846 goto out;
847 r = 0;
848 break;
849 }
850 case KVM_GET_SREGS: {
851 struct kvm_sregs kvm_sregs;
852
Avi Kivitybccf2152007-02-21 18:04:26 +0200853 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500854 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 if (r)
856 goto out;
857 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000858 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 goto out;
860 r = 0;
861 break;
862 }
863 case KVM_SET_SREGS: {
864 struct kvm_sregs kvm_sregs;
865
866 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000867 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500869 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 if (r)
871 goto out;
872 r = 0;
873 break;
874 }
875 case KVM_TRANSLATE: {
876 struct kvm_translation tr;
877
878 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000879 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800881 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 if (r)
883 goto out;
884 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000885 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 goto out;
887 r = 0;
888 break;
889 }
890 case KVM_INTERRUPT: {
891 struct kvm_interrupt irq;
892
893 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000894 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +0200896 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 if (r)
898 goto out;
899 r = 0;
900 break;
901 }
902 case KVM_DEBUG_GUEST: {
903 struct kvm_debug_guest dbg;
904
905 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000906 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500908 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 if (r)
910 goto out;
911 r = 0;
912 break;
913 }
Avi Kivity1961d272007-03-05 19:46:05 +0200914 case KVM_SET_SIGNAL_MASK: {
915 struct kvm_signal_mask __user *sigmask_arg = argp;
916 struct kvm_signal_mask kvm_sigmask;
917 sigset_t sigset, *p;
918
919 p = NULL;
920 if (argp) {
921 r = -EFAULT;
922 if (copy_from_user(&kvm_sigmask, argp,
923 sizeof kvm_sigmask))
924 goto out;
925 r = -EINVAL;
926 if (kvm_sigmask.len != sizeof sigset)
927 goto out;
928 r = -EFAULT;
929 if (copy_from_user(&sigset, sigmask_arg->sigset,
930 sizeof sigset))
931 goto out;
932 p = &sigset;
933 }
934 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
935 break;
936 }
Avi Kivityb8836732007-04-01 16:34:31 +0300937 case KVM_GET_FPU: {
938 struct kvm_fpu fpu;
939
940 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500941 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300942 if (r)
943 goto out;
944 r = -EFAULT;
945 if (copy_to_user(argp, &fpu, sizeof fpu))
946 goto out;
947 r = 0;
948 break;
949 }
950 case KVM_SET_FPU: {
951 struct kvm_fpu fpu;
952
953 r = -EFAULT;
954 if (copy_from_user(&fpu, argp, sizeof fpu))
955 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500956 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300957 if (r)
958 goto out;
959 r = 0;
960 break;
961 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200962 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200963 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200964 }
965out:
966 return r;
967}
968
969static long kvm_vm_ioctl(struct file *filp,
970 unsigned int ioctl, unsigned long arg)
971{
972 struct kvm *kvm = filp->private_data;
973 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +0100974 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200975
976 switch (ioctl) {
977 case KVM_CREATE_VCPU:
978 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
979 if (r < 0)
980 goto out;
981 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200982 case KVM_SET_USER_MEMORY_REGION: {
983 struct kvm_userspace_memory_region kvm_userspace_mem;
984
985 r = -EFAULT;
986 if (copy_from_user(&kvm_userspace_mem, argp,
987 sizeof kvm_userspace_mem))
988 goto out;
989
990 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 if (r)
992 goto out;
993 break;
994 }
995 case KVM_GET_DIRTY_LOG: {
996 struct kvm_dirty_log log;
997
998 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +0000999 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001001 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 if (r)
1003 goto out;
1004 break;
1005 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001006 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001007 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001008 }
1009out:
1010 return r;
1011}
1012
1013static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
1014 unsigned long address,
1015 int *type)
1016{
1017 struct kvm *kvm = vma->vm_file->private_data;
1018 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001019 struct page *page;
1020
Avi Kivityf17abe92007-02-21 19:28:04 +02001021 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001022 if (!kvm_is_visible_gfn(kvm, pgoff))
1023 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001024 /* current->mm->mmap_sem is already held so call lockless version */
1025 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001026 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001027 kvm_release_page_clean(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02001028 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001029 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03001030 if (type != NULL)
1031 *type = VM_FAULT_MINOR;
1032
Avi Kivityf17abe92007-02-21 19:28:04 +02001033 return page;
1034}
1035
1036static struct vm_operations_struct kvm_vm_vm_ops = {
1037 .nopage = kvm_vm_nopage,
1038};
1039
1040static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1041{
1042 vma->vm_ops = &kvm_vm_vm_ops;
1043 return 0;
1044}
1045
1046static struct file_operations kvm_vm_fops = {
1047 .release = kvm_vm_release,
1048 .unlocked_ioctl = kvm_vm_ioctl,
1049 .compat_ioctl = kvm_vm_ioctl,
1050 .mmap = kvm_vm_mmap,
1051};
1052
1053static int kvm_dev_ioctl_create_vm(void)
1054{
1055 int fd, r;
1056 struct inode *inode;
1057 struct file *file;
1058 struct kvm *kvm;
1059
Avi Kivityf17abe92007-02-21 19:28:04 +02001060 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001061 if (IS_ERR(kvm))
1062 return PTR_ERR(kvm);
1063 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1064 if (r) {
1065 kvm_destroy_vm(kvm);
1066 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001067 }
1068
Avi Kivitybccf2152007-02-21 18:04:26 +02001069 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001070
Avi Kivityf17abe92007-02-21 19:28:04 +02001071 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001072}
1073
1074static long kvm_dev_ioctl(struct file *filp,
1075 unsigned int ioctl, unsigned long arg)
1076{
1077 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001078 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001079
1080 switch (ioctl) {
1081 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001082 r = -EINVAL;
1083 if (arg)
1084 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001085 r = KVM_API_VERSION;
1086 break;
1087 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001088 r = -EINVAL;
1089 if (arg)
1090 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001091 r = kvm_dev_ioctl_create_vm();
1092 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001093 case KVM_CHECK_EXTENSION:
1094 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001095 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001096 case KVM_GET_VCPU_MMAP_SIZE:
1097 r = -EINVAL;
1098 if (arg)
1099 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001100 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001101 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001103 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 }
1105out:
1106 return r;
1107}
1108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 .unlocked_ioctl = kvm_dev_ioctl,
1111 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112};
1113
1114static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001115 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 "kvm",
1117 &kvm_chardev_ops,
1118};
1119
Avi Kivity1b6c0162007-05-24 13:03:52 +03001120static void hardware_enable(void *junk)
1121{
1122 int cpu = raw_smp_processor_id();
1123
1124 if (cpu_isset(cpu, cpus_hardware_enabled))
1125 return;
1126 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001127 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001128}
1129
1130static void hardware_disable(void *junk)
1131{
1132 int cpu = raw_smp_processor_id();
1133
1134 if (!cpu_isset(cpu, cpus_hardware_enabled))
1135 return;
1136 cpu_clear(cpu, cpus_hardware_enabled);
1137 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001138 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001139}
1140
Avi Kivity774c47f2007-02-12 00:54:47 -08001141static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1142 void *v)
1143{
1144 int cpu = (long)v;
1145
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001146 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001147 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001148 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001149 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1150 cpu);
1151 hardware_disable(NULL);
1152 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001153 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001154 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1155 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001156 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001157 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001158 case CPU_ONLINE:
1159 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1160 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001161 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001162 break;
1163 }
1164 return NOTIFY_OK;
1165}
1166
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001167static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001168 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001169{
1170 if (val == SYS_RESTART) {
1171 /*
1172 * Some (well, at least mine) BIOSes hang on reboot if
1173 * in vmx root mode.
1174 */
1175 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1176 on_each_cpu(hardware_disable, NULL, 0, 1);
1177 }
1178 return NOTIFY_OK;
1179}
1180
1181static struct notifier_block kvm_reboot_notifier = {
1182 .notifier_call = kvm_reboot,
1183 .priority = 0,
1184};
1185
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001186void kvm_io_bus_init(struct kvm_io_bus *bus)
1187{
1188 memset(bus, 0, sizeof(*bus));
1189}
1190
1191void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1192{
1193 int i;
1194
1195 for (i = 0; i < bus->dev_count; i++) {
1196 struct kvm_io_device *pos = bus->devs[i];
1197
1198 kvm_iodevice_destructor(pos);
1199 }
1200}
1201
1202struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1203{
1204 int i;
1205
1206 for (i = 0; i < bus->dev_count; i++) {
1207 struct kvm_io_device *pos = bus->devs[i];
1208
1209 if (pos->in_range(pos, addr))
1210 return pos;
1211 }
1212
1213 return NULL;
1214}
1215
1216void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1217{
1218 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1219
1220 bus->devs[bus->dev_count++] = dev;
1221}
1222
Avi Kivity774c47f2007-02-12 00:54:47 -08001223static struct notifier_block kvm_cpu_notifier = {
1224 .notifier_call = kvm_cpu_hotplug,
1225 .priority = 20, /* must be > scheduler priority */
1226};
1227
Avi Kivityba1389b2007-11-18 16:24:12 +02001228static u64 vm_stat_get(void *_offset)
1229{
1230 unsigned offset = (long)_offset;
1231 u64 total = 0;
1232 struct kvm *kvm;
1233
1234 spin_lock(&kvm_lock);
1235 list_for_each_entry(kvm, &vm_list, vm_list)
1236 total += *(u32 *)((void *)kvm + offset);
1237 spin_unlock(&kvm_lock);
1238 return total;
1239}
1240
1241DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1242
1243static u64 vcpu_stat_get(void *_offset)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001244{
1245 unsigned offset = (long)_offset;
1246 u64 total = 0;
1247 struct kvm *kvm;
1248 struct kvm_vcpu *vcpu;
1249 int i;
1250
1251 spin_lock(&kvm_lock);
1252 list_for_each_entry(kvm, &vm_list, vm_list)
1253 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001254 vcpu = kvm->vcpus[i];
1255 if (vcpu)
1256 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001257 }
1258 spin_unlock(&kvm_lock);
1259 return total;
1260}
1261
Avi Kivityba1389b2007-11-18 16:24:12 +02001262DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1263
1264static struct file_operations *stat_fops[] = {
1265 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1266 [KVM_STAT_VM] = &vm_stat_fops,
1267};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001268
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001269static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270{
1271 struct kvm_stats_debugfs_item *p;
1272
Al Viro8b6d44c2007-02-09 16:38:40 +00001273 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001275 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1276 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001277 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
1280static void kvm_exit_debug(void)
1281{
1282 struct kvm_stats_debugfs_item *p;
1283
1284 for (p = debugfs_entries; p->name; ++p)
1285 debugfs_remove(p->dentry);
1286 debugfs_remove(debugfs_dir);
1287}
1288
Avi Kivity59ae6c62007-02-12 00:54:48 -08001289static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1290{
Avi Kivity4267c412007-05-24 13:09:41 +03001291 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001292 return 0;
1293}
1294
1295static int kvm_resume(struct sys_device *dev)
1296{
Avi Kivity4267c412007-05-24 13:09:41 +03001297 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001298 return 0;
1299}
1300
1301static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001302 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001303 .suspend = kvm_suspend,
1304 .resume = kvm_resume,
1305};
1306
1307static struct sys_device kvm_sysdev = {
1308 .id = 0,
1309 .cls = &kvm_sysdev_class,
1310};
1311
Izik Eiduscea7bb22007-10-17 19:17:48 +02001312struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313
Avi Kivity15ad7142007-07-11 18:17:21 +03001314static inline
1315struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1316{
1317 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1318}
1319
1320static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1321{
1322 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1323
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001324 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001325}
1326
1327static void kvm_sched_out(struct preempt_notifier *pn,
1328 struct task_struct *next)
1329{
1330 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1331
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001332 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001333}
1334
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001335int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001336 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337{
1338 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001339 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001341 kvm_init_debug();
1342
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001343 r = kvm_arch_init(opaque);
1344 if (r)
1345 goto out4;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001346
1347 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1348
1349 if (bad_page == NULL) {
1350 r = -ENOMEM;
1351 goto out;
1352 }
1353
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001354 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02001356 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357
Yang, Sheng002c7f72007-07-31 14:23:01 +03001358 for_each_online_cpu(cpu) {
1359 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001360 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001361 &r, 0, 1);
1362 if (r < 0)
1363 goto out_free_0;
1364 }
1365
Avi Kivity1b6c0162007-05-24 13:03:52 +03001366 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001367 r = register_cpu_notifier(&kvm_cpu_notifier);
1368 if (r)
1369 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 register_reboot_notifier(&kvm_reboot_notifier);
1371
Avi Kivity59ae6c62007-02-12 00:54:48 -08001372 r = sysdev_class_register(&kvm_sysdev_class);
1373 if (r)
1374 goto out_free_2;
1375
1376 r = sysdev_register(&kvm_sysdev);
1377 if (r)
1378 goto out_free_3;
1379
Rusty Russellc16f8622007-07-30 21:12:19 +10001380 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1381 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001382 __alignof__(struct kvm_vcpu),
1383 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001384 if (!kvm_vcpu_cache) {
1385 r = -ENOMEM;
1386 goto out_free_4;
1387 }
1388
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 kvm_chardev_ops.owner = module;
1390
1391 r = misc_register(&kvm_dev);
1392 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001393 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 goto out_free;
1395 }
1396
Avi Kivity15ad7142007-07-11 18:17:21 +03001397 kvm_preempt_ops.sched_in = kvm_sched_in;
1398 kvm_preempt_ops.sched_out = kvm_sched_out;
1399
Avi Kivityc7addb92007-09-16 18:58:32 +02001400 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
1402out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001403 kmem_cache_destroy(kvm_vcpu_cache);
1404out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001405 sysdev_unregister(&kvm_sysdev);
1406out_free_3:
1407 sysdev_class_unregister(&kvm_sysdev_class);
1408out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001410 unregister_cpu_notifier(&kvm_cpu_notifier);
1411out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001412 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001413out_free_0:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001414 kvm_arch_hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02001415out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001416 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001417 kvm_exit_debug();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001418out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 return r;
1420}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001421EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001423void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424{
1425 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001426 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001427 sysdev_unregister(&kvm_sysdev);
1428 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001430 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001431 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001432 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001433 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001435 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001437EXPORT_SYMBOL_GPL(kvm_exit);