blob: 4e1bd948847038e4483def237d5b5abefc8c4538 [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;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500294 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200295 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500296 else {
297 down_write(&current->mm->mmap_sem);
298 new.userspace_addr = do_mmap(NULL, 0,
299 npages * PAGE_SIZE,
300 PROT_READ | PROT_WRITE,
301 MAP_SHARED | MAP_ANONYMOUS,
302 0);
303 up_write(&current->mm->mmap_sem);
304
305 if (IS_ERR((void *)new.userspace_addr))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800306 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200308 } else {
309 if (!old.user_alloc && old.rmap) {
310 int ret;
311
312 down_write(&current->mm->mmap_sem);
313 ret = do_munmap(current->mm, old.userspace_addr,
314 old.npages * PAGE_SIZE);
315 up_write(&current->mm->mmap_sem);
316 if (ret < 0)
317 printk(KERN_WARNING
318 "kvm_vm_ioctl_set_memory_region: "
319 "failed to munmap memory\n");
320 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321 }
322
323 /* Allocate page dirty bitmap if needed */
324 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
325 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
326
327 new.dirty_bitmap = vmalloc(dirty_bytes);
328 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800329 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 memset(new.dirty_bitmap, 0, dirty_bytes);
331 }
332
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 if (mem->slot >= kvm->nmemslots)
334 kvm->nmemslots = mem->slot + 1;
335
Izik Eidus82ce2c92007-10-02 18:52:55 +0200336 if (!kvm->n_requested_mmu_pages) {
337 unsigned int n_pages;
338
339 if (npages) {
340 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
341 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
342 n_pages);
343 } else {
344 unsigned int nr_mmu_pages;
345
346 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
347 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
348 nr_mmu_pages = max(nr_mmu_pages,
349 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
350 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
351 }
352 }
353
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355
Avi Kivity90cb0522007-07-17 13:04:56 +0300356 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
357 kvm_flush_remote_tlbs(kvm);
358
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 kvm_free_physmem_slot(&old, &new);
360 return 0;
361
Sheng Yangf78e0e22007-10-29 09:40:42 +0800362out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 kvm_free_physmem_slot(&new, &old);
364out:
365 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200366
367}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800368EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
369
370int kvm_set_memory_region(struct kvm *kvm,
371 struct kvm_userspace_memory_region *mem,
372 int user_alloc)
373{
374 int r;
375
376 mutex_lock(&kvm->lock);
377 r = __kvm_set_memory_region(kvm, mem, user_alloc);
378 mutex_unlock(&kvm->lock);
379 return r;
380}
Izik Eidus210c7c42007-10-24 23:52:57 +0200381EXPORT_SYMBOL_GPL(kvm_set_memory_region);
382
Carsten Otte1fe779f2007-10-29 16:08:35 +0100383int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
384 struct
385 kvm_userspace_memory_region *mem,
386 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200387{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200388 if (mem->slot >= KVM_MEMORY_SLOTS)
389 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200390 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391}
392
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800393int kvm_get_dirty_log(struct kvm *kvm,
394 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395{
396 struct kvm_memory_slot *memslot;
397 int r, i;
398 int n;
399 unsigned long any = 0;
400
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 r = -EINVAL;
402 if (log->slot >= KVM_MEMORY_SLOTS)
403 goto out;
404
405 memslot = &kvm->memslots[log->slot];
406 r = -ENOENT;
407 if (!memslot->dirty_bitmap)
408 goto out;
409
Uri Lublincd1a4a92007-02-22 16:43:09 +0200410 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411
Uri Lublincd1a4a92007-02-22 16:43:09 +0200412 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 any = memslot->dirty_bitmap[i];
414
415 r = -EFAULT;
416 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
417 goto out;
418
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800419 if (any)
420 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421
422 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 return r;
425}
426
Izik Eiduscea7bb22007-10-17 19:17:48 +0200427int is_error_page(struct page *page)
428{
429 return page == bad_page;
430}
431EXPORT_SYMBOL_GPL(is_error_page);
432
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200433static inline unsigned long bad_hva(void)
434{
435 return PAGE_OFFSET;
436}
437
438int kvm_is_error_hva(unsigned long addr)
439{
440 return addr == bad_hva();
441}
442EXPORT_SYMBOL_GPL(kvm_is_error_hva);
443
Izik Eidus290fc382007-09-27 14:11:22 +0200444gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300445{
446 int i;
447 struct kvm_mem_alias *alias;
448
449 for (i = 0; i < kvm->naliases; ++i) {
450 alias = &kvm->aliases[i];
451 if (gfn >= alias->base_gfn
452 && gfn < alias->base_gfn + alias->npages)
453 return alias->target_gfn + gfn - alias->base_gfn;
454 }
455 return gfn;
456}
457
458static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459{
460 int i;
461
462 for (i = 0; i < kvm->nmemslots; ++i) {
463 struct kvm_memory_slot *memslot = &kvm->memslots[i];
464
465 if (gfn >= memslot->base_gfn
466 && gfn < memslot->base_gfn + memslot->npages)
467 return memslot;
468 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000469 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470}
Avi Kivitye8207542007-03-30 16:54:30 +0300471
472struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
473{
474 gfn = unalias_gfn(kvm, gfn);
475 return __gfn_to_memslot(kvm, gfn);
476}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477
Izik Eiduse0d62c72007-10-24 23:57:46 +0200478int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
479{
480 int i;
481
482 gfn = unalias_gfn(kvm, gfn);
483 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
484 struct kvm_memory_slot *memslot = &kvm->memslots[i];
485
486 if (gfn >= memslot->base_gfn
487 && gfn < memslot->base_gfn + memslot->npages)
488 return 1;
489 }
490 return 0;
491}
492EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
493
Izik Eidus539cb662007-11-11 22:05:04 +0200494static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
495{
496 struct kvm_memory_slot *slot;
497
498 gfn = unalias_gfn(kvm, gfn);
499 slot = __gfn_to_memslot(kvm, gfn);
500 if (!slot)
501 return bad_hva();
502 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
503}
504
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500505/*
506 * Requires current->mm->mmap_sem to be held
507 */
508static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300509{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500510 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200511 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500512 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300513
Avi Kivity60395222007-10-21 11:03:36 +0200514 might_sleep();
515
Izik Eidus539cb662007-11-11 22:05:04 +0200516 addr = gfn_to_hva(kvm, gfn);
517 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200518 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200519 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200520 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200521
Izik Eidus539cb662007-11-11 22:05:04 +0200522 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
523 NULL);
524
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500525 if (npages != 1) {
526 get_page(bad_page);
527 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200528 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500529
530 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300531}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500532
533struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
534{
535 struct page *page;
536
537 down_read(&current->mm->mmap_sem);
538 page = __gfn_to_page(kvm, gfn);
539 up_read(&current->mm->mmap_sem);
540
541 return page;
542}
543
Avi Kivity954bbbc2007-03-30 14:02:32 +0300544EXPORT_SYMBOL_GPL(gfn_to_page);
545
Izik Eidus8a7ae052007-10-18 11:09:33 +0200546void kvm_release_page(struct page *page)
547{
548 if (!PageReserved(page))
549 SetPageDirty(page);
550 put_page(page);
551}
552EXPORT_SYMBOL_GPL(kvm_release_page);
553
Izik Eidus195aefd2007-10-01 22:14:18 +0200554static int next_segment(unsigned long len, int offset)
555{
556 if (len > PAGE_SIZE - offset)
557 return PAGE_SIZE - offset;
558 else
559 return len;
560}
561
562int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
563 int len)
564{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200565 int r;
566 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200567
Izik Eiduse0506bc2007-11-11 22:10:22 +0200568 addr = gfn_to_hva(kvm, gfn);
569 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200570 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200571 r = copy_from_user(data, (void __user *)addr + offset, len);
572 if (r)
573 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200574 return 0;
575}
576EXPORT_SYMBOL_GPL(kvm_read_guest_page);
577
578int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
579{
580 gfn_t gfn = gpa >> PAGE_SHIFT;
581 int seg;
582 int offset = offset_in_page(gpa);
583 int ret;
584
585 while ((seg = next_segment(len, offset)) != 0) {
586 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
587 if (ret < 0)
588 return ret;
589 offset = 0;
590 len -= seg;
591 data += seg;
592 ++gfn;
593 }
594 return 0;
595}
596EXPORT_SYMBOL_GPL(kvm_read_guest);
597
598int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
599 int offset, int len)
600{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200601 int r;
602 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200603
Izik Eiduse0506bc2007-11-11 22:10:22 +0200604 addr = gfn_to_hva(kvm, gfn);
605 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200606 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200607 r = copy_to_user((void __user *)addr + offset, data, len);
608 if (r)
609 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200610 mark_page_dirty(kvm, gfn);
611 return 0;
612}
613EXPORT_SYMBOL_GPL(kvm_write_guest_page);
614
615int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
616 unsigned long len)
617{
618 gfn_t gfn = gpa >> PAGE_SHIFT;
619 int seg;
620 int offset = offset_in_page(gpa);
621 int ret;
622
623 while ((seg = next_segment(len, offset)) != 0) {
624 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
625 if (ret < 0)
626 return ret;
627 offset = 0;
628 len -= seg;
629 data += seg;
630 ++gfn;
631 }
632 return 0;
633}
634
635int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
636{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200637 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200638}
639EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
640
641int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
642{
643 gfn_t gfn = gpa >> PAGE_SHIFT;
644 int seg;
645 int offset = offset_in_page(gpa);
646 int ret;
647
648 while ((seg = next_segment(len, offset)) != 0) {
649 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
650 if (ret < 0)
651 return ret;
652 offset = 0;
653 len -= seg;
654 ++gfn;
655 }
656 return 0;
657}
658EXPORT_SYMBOL_GPL(kvm_clear_guest);
659
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
661{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300662 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663
Uri Lublin3b6fff12007-10-30 10:42:09 +0200664 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000665 memslot = __gfn_to_memslot(kvm, gfn);
666 if (memslot && memslot->dirty_bitmap) {
667 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668
Rusty Russell7e9d6192007-07-31 20:41:14 +1000669 /* avoid RMW */
670 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
671 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 }
673}
674
Eddie Dongb6958ce2007-07-18 12:15:21 +0300675/*
676 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
677 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500678void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300679{
680 DECLARE_WAITQUEUE(wait, current);
681
682 add_wait_queue(&vcpu->wq, &wait);
683
684 /*
685 * We will block until either an interrupt or a signal wakes us up
686 */
He, Qingc5ec1532007-09-03 17:07:41 +0300687 while (!kvm_cpu_has_interrupt(vcpu)
688 && !signal_pending(current)
689 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
690 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300691 set_current_state(TASK_INTERRUPTIBLE);
692 vcpu_put(vcpu);
693 schedule();
694 vcpu_load(vcpu);
695 }
696
He, Qingc5ec1532007-09-03 17:07:41 +0300697 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300698 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300699}
700
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701void kvm_resched(struct kvm_vcpu *vcpu)
702{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300703 if (!need_resched())
704 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706}
707EXPORT_SYMBOL_GPL(kvm_resched);
708
Avi Kivitybccf2152007-02-21 18:04:26 +0200709static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
710 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 if (irq->irq < 0 || irq->irq >= 256)
713 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300714 if (irqchip_in_kernel(vcpu->kvm))
715 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200716 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717
718 set_bit(irq->irq, vcpu->irq_pending);
719 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
720
721 vcpu_put(vcpu);
722
723 return 0;
724}
725
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200726static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
727 unsigned long address,
728 int *type)
729{
730 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
731 unsigned long pgoff;
732 struct page *page;
733
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200734 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +0200735 if (pgoff == 0)
736 page = virt_to_page(vcpu->run);
737 else if (pgoff == KVM_PIO_PAGE_OFFSET)
738 page = virt_to_page(vcpu->pio_data);
739 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200740 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200741 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300742 if (type != NULL)
743 *type = VM_FAULT_MINOR;
744
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200745 return page;
746}
747
748static struct vm_operations_struct kvm_vcpu_vm_ops = {
749 .nopage = kvm_vcpu_nopage,
750};
751
752static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
753{
754 vma->vm_ops = &kvm_vcpu_vm_ops;
755 return 0;
756}
757
Avi Kivitybccf2152007-02-21 18:04:26 +0200758static int kvm_vcpu_release(struct inode *inode, struct file *filp)
759{
760 struct kvm_vcpu *vcpu = filp->private_data;
761
762 fput(vcpu->kvm->filp);
763 return 0;
764}
765
766static struct file_operations kvm_vcpu_fops = {
767 .release = kvm_vcpu_release,
768 .unlocked_ioctl = kvm_vcpu_ioctl,
769 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200770 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200771};
772
773/*
774 * Allocates an inode for the vcpu.
775 */
776static int create_vcpu_fd(struct kvm_vcpu *vcpu)
777{
778 int fd, r;
779 struct inode *inode;
780 struct file *file;
781
Avi Kivityd6d28162007-06-28 08:38:16 -0400782 r = anon_inode_getfd(&fd, &inode, &file,
783 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
784 if (r)
785 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200786 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200787 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200788}
789
Avi Kivityc5ea7662007-02-20 18:41:05 +0200790/*
791 * Creates some virtual cpus. Good luck creating more than one.
792 */
793static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
794{
795 int r;
796 struct kvm_vcpu *vcpu;
797
Avi Kivityc5ea7662007-02-20 18:41:05 +0200798 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000799 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200800
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800801 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000802 if (IS_ERR(vcpu))
803 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200804
Avi Kivity15ad7142007-07-11 18:17:21 +0300805 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
806
Shaohua Li11ec2802007-07-23 14:51:37 +0800807 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000808 if (kvm->vcpus[n]) {
809 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800810 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800811 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000812 }
813 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800814 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000815
816 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200817 r = create_vcpu_fd(vcpu);
818 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000819 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200820 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200821
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000822unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800823 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000824 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800825 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800826vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600827 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200828 return r;
829}
830
Avi Kivity1961d272007-03-05 19:46:05 +0200831static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
832{
833 if (sigset) {
834 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
835 vcpu->sigset_active = 1;
836 vcpu->sigset = *sigset;
837 } else
838 vcpu->sigset_active = 0;
839 return 0;
840}
841
Avi Kivitybccf2152007-02-21 18:04:26 +0200842static long kvm_vcpu_ioctl(struct file *filp,
843 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844{
Avi Kivitybccf2152007-02-21 18:04:26 +0200845 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000846 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200847 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
849 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200850 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200851 r = -EINVAL;
852 if (arg)
853 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500854 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 case KVM_GET_REGS: {
857 struct kvm_regs kvm_regs;
858
Avi Kivitybccf2152007-02-21 18:04:26 +0200859 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500860 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 if (r)
862 goto out;
863 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000864 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 goto out;
866 r = 0;
867 break;
868 }
869 case KVM_SET_REGS: {
870 struct kvm_regs kvm_regs;
871
872 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000873 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500875 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 if (r)
877 goto out;
878 r = 0;
879 break;
880 }
881 case KVM_GET_SREGS: {
882 struct kvm_sregs kvm_sregs;
883
Avi Kivitybccf2152007-02-21 18:04:26 +0200884 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500885 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 if (r)
887 goto out;
888 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000889 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 goto out;
891 r = 0;
892 break;
893 }
894 case KVM_SET_SREGS: {
895 struct kvm_sregs kvm_sregs;
896
897 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000898 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500900 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 if (r)
902 goto out;
903 r = 0;
904 break;
905 }
906 case KVM_TRANSLATE: {
907 struct kvm_translation tr;
908
909 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000910 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800912 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 if (r)
914 goto out;
915 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000916 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 goto out;
918 r = 0;
919 break;
920 }
921 case KVM_INTERRUPT: {
922 struct kvm_interrupt irq;
923
924 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000925 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +0200927 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 if (r)
929 goto out;
930 r = 0;
931 break;
932 }
933 case KVM_DEBUG_GUEST: {
934 struct kvm_debug_guest dbg;
935
936 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000937 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500939 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 if (r)
941 goto out;
942 r = 0;
943 break;
944 }
Avi Kivity1961d272007-03-05 19:46:05 +0200945 case KVM_SET_SIGNAL_MASK: {
946 struct kvm_signal_mask __user *sigmask_arg = argp;
947 struct kvm_signal_mask kvm_sigmask;
948 sigset_t sigset, *p;
949
950 p = NULL;
951 if (argp) {
952 r = -EFAULT;
953 if (copy_from_user(&kvm_sigmask, argp,
954 sizeof kvm_sigmask))
955 goto out;
956 r = -EINVAL;
957 if (kvm_sigmask.len != sizeof sigset)
958 goto out;
959 r = -EFAULT;
960 if (copy_from_user(&sigset, sigmask_arg->sigset,
961 sizeof sigset))
962 goto out;
963 p = &sigset;
964 }
965 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
966 break;
967 }
Avi Kivityb8836732007-04-01 16:34:31 +0300968 case KVM_GET_FPU: {
969 struct kvm_fpu fpu;
970
971 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500972 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300973 if (r)
974 goto out;
975 r = -EFAULT;
976 if (copy_to_user(argp, &fpu, sizeof fpu))
977 goto out;
978 r = 0;
979 break;
980 }
981 case KVM_SET_FPU: {
982 struct kvm_fpu fpu;
983
984 r = -EFAULT;
985 if (copy_from_user(&fpu, argp, sizeof fpu))
986 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500987 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300988 if (r)
989 goto out;
990 r = 0;
991 break;
992 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200993 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200994 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200995 }
996out:
997 return r;
998}
999
1000static long kvm_vm_ioctl(struct file *filp,
1001 unsigned int ioctl, unsigned long arg)
1002{
1003 struct kvm *kvm = filp->private_data;
1004 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001005 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001006
1007 switch (ioctl) {
1008 case KVM_CREATE_VCPU:
1009 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1010 if (r < 0)
1011 goto out;
1012 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001013 case KVM_SET_USER_MEMORY_REGION: {
1014 struct kvm_userspace_memory_region kvm_userspace_mem;
1015
1016 r = -EFAULT;
1017 if (copy_from_user(&kvm_userspace_mem, argp,
1018 sizeof kvm_userspace_mem))
1019 goto out;
1020
1021 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 if (r)
1023 goto out;
1024 break;
1025 }
1026 case KVM_GET_DIRTY_LOG: {
1027 struct kvm_dirty_log log;
1028
1029 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001030 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001032 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 if (r)
1034 goto out;
1035 break;
1036 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001037 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001038 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001039 }
1040out:
1041 return r;
1042}
1043
1044static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
1045 unsigned long address,
1046 int *type)
1047{
1048 struct kvm *kvm = vma->vm_file->private_data;
1049 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001050 struct page *page;
1051
Avi Kivityf17abe92007-02-21 19:28:04 +02001052 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001053 if (!kvm_is_visible_gfn(kvm, pgoff))
1054 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001055 /* current->mm->mmap_sem is already held so call lockless version */
1056 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001057 if (is_error_page(page)) {
1058 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02001059 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001060 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03001061 if (type != NULL)
1062 *type = VM_FAULT_MINOR;
1063
Avi Kivityf17abe92007-02-21 19:28:04 +02001064 return page;
1065}
1066
1067static struct vm_operations_struct kvm_vm_vm_ops = {
1068 .nopage = kvm_vm_nopage,
1069};
1070
1071static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1072{
1073 vma->vm_ops = &kvm_vm_vm_ops;
1074 return 0;
1075}
1076
1077static struct file_operations kvm_vm_fops = {
1078 .release = kvm_vm_release,
1079 .unlocked_ioctl = kvm_vm_ioctl,
1080 .compat_ioctl = kvm_vm_ioctl,
1081 .mmap = kvm_vm_mmap,
1082};
1083
1084static int kvm_dev_ioctl_create_vm(void)
1085{
1086 int fd, r;
1087 struct inode *inode;
1088 struct file *file;
1089 struct kvm *kvm;
1090
Avi Kivityf17abe92007-02-21 19:28:04 +02001091 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001092 if (IS_ERR(kvm))
1093 return PTR_ERR(kvm);
1094 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1095 if (r) {
1096 kvm_destroy_vm(kvm);
1097 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001098 }
1099
Avi Kivitybccf2152007-02-21 18:04:26 +02001100 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001101
Avi Kivityf17abe92007-02-21 19:28:04 +02001102 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001103}
1104
1105static long kvm_dev_ioctl(struct file *filp,
1106 unsigned int ioctl, unsigned long arg)
1107{
1108 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001109 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001110
1111 switch (ioctl) {
1112 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001113 r = -EINVAL;
1114 if (arg)
1115 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001116 r = KVM_API_VERSION;
1117 break;
1118 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001119 r = -EINVAL;
1120 if (arg)
1121 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001122 r = kvm_dev_ioctl_create_vm();
1123 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001124 case KVM_CHECK_EXTENSION:
1125 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001126 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001127 case KVM_GET_VCPU_MMAP_SIZE:
1128 r = -EINVAL;
1129 if (arg)
1130 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001131 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001134 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 }
1136out:
1137 return r;
1138}
1139
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 .unlocked_ioctl = kvm_dev_ioctl,
1142 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143};
1144
1145static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001146 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 "kvm",
1148 &kvm_chardev_ops,
1149};
1150
Avi Kivity1b6c0162007-05-24 13:03:52 +03001151static void hardware_enable(void *junk)
1152{
1153 int cpu = raw_smp_processor_id();
1154
1155 if (cpu_isset(cpu, cpus_hardware_enabled))
1156 return;
1157 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001158 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001159}
1160
1161static void hardware_disable(void *junk)
1162{
1163 int cpu = raw_smp_processor_id();
1164
1165 if (!cpu_isset(cpu, cpus_hardware_enabled))
1166 return;
1167 cpu_clear(cpu, cpus_hardware_enabled);
1168 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001169 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001170}
1171
Avi Kivity774c47f2007-02-12 00:54:47 -08001172static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1173 void *v)
1174{
1175 int cpu = (long)v;
1176
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001177 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001178 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001179 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001180 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1181 cpu);
1182 hardware_disable(NULL);
1183 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001184 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001185 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1186 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001187 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001188 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001189 case CPU_ONLINE:
1190 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1191 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001192 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001193 break;
1194 }
1195 return NOTIFY_OK;
1196}
1197
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001198static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001199 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001200{
1201 if (val == SYS_RESTART) {
1202 /*
1203 * Some (well, at least mine) BIOSes hang on reboot if
1204 * in vmx root mode.
1205 */
1206 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1207 on_each_cpu(hardware_disable, NULL, 0, 1);
1208 }
1209 return NOTIFY_OK;
1210}
1211
1212static struct notifier_block kvm_reboot_notifier = {
1213 .notifier_call = kvm_reboot,
1214 .priority = 0,
1215};
1216
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001217void kvm_io_bus_init(struct kvm_io_bus *bus)
1218{
1219 memset(bus, 0, sizeof(*bus));
1220}
1221
1222void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1223{
1224 int i;
1225
1226 for (i = 0; i < bus->dev_count; i++) {
1227 struct kvm_io_device *pos = bus->devs[i];
1228
1229 kvm_iodevice_destructor(pos);
1230 }
1231}
1232
1233struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1234{
1235 int i;
1236
1237 for (i = 0; i < bus->dev_count; i++) {
1238 struct kvm_io_device *pos = bus->devs[i];
1239
1240 if (pos->in_range(pos, addr))
1241 return pos;
1242 }
1243
1244 return NULL;
1245}
1246
1247void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1248{
1249 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1250
1251 bus->devs[bus->dev_count++] = dev;
1252}
1253
Avi Kivity774c47f2007-02-12 00:54:47 -08001254static struct notifier_block kvm_cpu_notifier = {
1255 .notifier_call = kvm_cpu_hotplug,
1256 .priority = 20, /* must be > scheduler priority */
1257};
1258
Avi Kivityba1389b2007-11-18 16:24:12 +02001259static u64 vm_stat_get(void *_offset)
1260{
1261 unsigned offset = (long)_offset;
1262 u64 total = 0;
1263 struct kvm *kvm;
1264
1265 spin_lock(&kvm_lock);
1266 list_for_each_entry(kvm, &vm_list, vm_list)
1267 total += *(u32 *)((void *)kvm + offset);
1268 spin_unlock(&kvm_lock);
1269 return total;
1270}
1271
1272DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1273
1274static u64 vcpu_stat_get(void *_offset)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001275{
1276 unsigned offset = (long)_offset;
1277 u64 total = 0;
1278 struct kvm *kvm;
1279 struct kvm_vcpu *vcpu;
1280 int i;
1281
1282 spin_lock(&kvm_lock);
1283 list_for_each_entry(kvm, &vm_list, vm_list)
1284 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001285 vcpu = kvm->vcpus[i];
1286 if (vcpu)
1287 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001288 }
1289 spin_unlock(&kvm_lock);
1290 return total;
1291}
1292
Avi Kivityba1389b2007-11-18 16:24:12 +02001293DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1294
1295static struct file_operations *stat_fops[] = {
1296 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1297 [KVM_STAT_VM] = &vm_stat_fops,
1298};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001299
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001300static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301{
1302 struct kvm_stats_debugfs_item *p;
1303
Al Viro8b6d44c2007-02-09 16:38:40 +00001304 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001306 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1307 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001308 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309}
1310
1311static void kvm_exit_debug(void)
1312{
1313 struct kvm_stats_debugfs_item *p;
1314
1315 for (p = debugfs_entries; p->name; ++p)
1316 debugfs_remove(p->dentry);
1317 debugfs_remove(debugfs_dir);
1318}
1319
Avi Kivity59ae6c62007-02-12 00:54:48 -08001320static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1321{
Avi Kivity4267c412007-05-24 13:09:41 +03001322 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001323 return 0;
1324}
1325
1326static int kvm_resume(struct sys_device *dev)
1327{
Avi Kivity4267c412007-05-24 13:09:41 +03001328 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001329 return 0;
1330}
1331
1332static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001333 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001334 .suspend = kvm_suspend,
1335 .resume = kvm_resume,
1336};
1337
1338static struct sys_device kvm_sysdev = {
1339 .id = 0,
1340 .cls = &kvm_sysdev_class,
1341};
1342
Izik Eiduscea7bb22007-10-17 19:17:48 +02001343struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344
Avi Kivity15ad7142007-07-11 18:17:21 +03001345static inline
1346struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1347{
1348 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1349}
1350
1351static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1352{
1353 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1354
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001355 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001356}
1357
1358static void kvm_sched_out(struct preempt_notifier *pn,
1359 struct task_struct *next)
1360{
1361 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1362
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001363 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001364}
1365
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001366int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001367 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368{
1369 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001370 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001372 kvm_init_debug();
1373
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001374 r = kvm_arch_init(opaque);
1375 if (r)
1376 goto out4;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001377
1378 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1379
1380 if (bad_page == NULL) {
1381 r = -ENOMEM;
1382 goto out;
1383 }
1384
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001385 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02001387 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Yang, Sheng002c7f72007-07-31 14:23:01 +03001389 for_each_online_cpu(cpu) {
1390 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001391 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001392 &r, 0, 1);
1393 if (r < 0)
1394 goto out_free_0;
1395 }
1396
Avi Kivity1b6c0162007-05-24 13:03:52 +03001397 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001398 r = register_cpu_notifier(&kvm_cpu_notifier);
1399 if (r)
1400 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 register_reboot_notifier(&kvm_reboot_notifier);
1402
Avi Kivity59ae6c62007-02-12 00:54:48 -08001403 r = sysdev_class_register(&kvm_sysdev_class);
1404 if (r)
1405 goto out_free_2;
1406
1407 r = sysdev_register(&kvm_sysdev);
1408 if (r)
1409 goto out_free_3;
1410
Rusty Russellc16f8622007-07-30 21:12:19 +10001411 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1412 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001413 __alignof__(struct kvm_vcpu),
1414 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001415 if (!kvm_vcpu_cache) {
1416 r = -ENOMEM;
1417 goto out_free_4;
1418 }
1419
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 kvm_chardev_ops.owner = module;
1421
1422 r = misc_register(&kvm_dev);
1423 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001424 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 goto out_free;
1426 }
1427
Avi Kivity15ad7142007-07-11 18:17:21 +03001428 kvm_preempt_ops.sched_in = kvm_sched_in;
1429 kvm_preempt_ops.sched_out = kvm_sched_out;
1430
Avi Kivityc7addb92007-09-16 18:58:32 +02001431 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
1433out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001434 kmem_cache_destroy(kvm_vcpu_cache);
1435out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001436 sysdev_unregister(&kvm_sysdev);
1437out_free_3:
1438 sysdev_class_unregister(&kvm_sysdev_class);
1439out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001441 unregister_cpu_notifier(&kvm_cpu_notifier);
1442out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001443 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001444out_free_0:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001445 kvm_arch_hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02001446out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001447 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001448 kvm_exit_debug();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001449out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 return r;
1451}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001452EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001454void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455{
1456 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001457 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001458 sysdev_unregister(&kvm_sysdev);
1459 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001461 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001462 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001463 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001464 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001466 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001468EXPORT_SYMBOL_GPL(kvm_exit);