blob: c41eb57ce29b8c7ba6ffd2b78175b90ad8254bae [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
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
Avi Kivitye4956062007-06-28 14:15:57 -040044#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/io.h>
46#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020047#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
49MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080052DEFINE_SPINLOCK(kvm_lock);
53LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080054
Avi Kivity1b6c0162007-05-24 13:03:52 +030055static cpumask_t cpus_hardware_enabled;
56
Rusty Russellc16f8622007-07-30 21:12:19 +100057struct kmem_cache *kvm_vcpu_cache;
58EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030059
Avi Kivity15ad7142007-07-11 18:17:21 +030060static __read_mostly struct preempt_ops kvm_preempt_ops;
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062static struct dentry *debugfs_dir;
63
Avi Kivitybccf2152007-02-21 18:04:26 +020064static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
65 unsigned long arg);
66
James Morris5aacf0c2006-12-22 01:04:55 -080067static inline int valid_vcpu(int n)
68{
69 return likely(n >= 0 && n < KVM_MAX_VCPUS);
70}
71
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/*
73 * Switches to specified vcpu, until a matching vcpu_put()
74 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020075void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076{
Avi Kivity15ad7142007-07-11 18:17:21 +030077 int cpu;
78
Avi Kivitybccf2152007-02-21 18:04:26 +020079 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030080 cpu = get_cpu();
81 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020082 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030083 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020084}
85
Carsten Otte313a3dc2007-10-11 19:16:52 +020086void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087{
Avi Kivity15ad7142007-07-11 18:17:21 +030088 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020089 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030090 preempt_notifier_unregister(&vcpu->preempt_notifier);
91 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080092 mutex_unlock(&vcpu->mutex);
93}
94
Avi Kivityd9e368d2007-06-07 19:18:30 +030095static void ack_flush(void *_completed)
96{
Avi Kivityd9e368d2007-06-07 19:18:30 +030097}
98
99void kvm_flush_remote_tlbs(struct kvm *kvm)
100{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200101 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300102 cpumask_t cpus;
103 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300104
Avi Kivityd9e368d2007-06-07 19:18:30 +0300105 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000106 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
107 vcpu = kvm->vcpus[i];
108 if (!vcpu)
109 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200110 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300111 continue;
112 cpu = vcpu->cpu;
113 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200114 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300115 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200116 if (cpus_empty(cpus))
117 return;
118 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200119 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300120}
121
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500122void kvm_reload_remote_mmus(struct kvm *kvm)
123{
124 int i, cpu;
125 cpumask_t cpus;
126 struct kvm_vcpu *vcpu;
127
128 cpus_clear(cpus);
129 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
130 vcpu = kvm->vcpus[i];
131 if (!vcpu)
132 continue;
133 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
134 continue;
135 cpu = vcpu->cpu;
136 if (cpu != -1 && cpu != raw_smp_processor_id())
137 cpu_set(cpu, cpus);
138 }
139 if (cpus_empty(cpus))
140 return;
141 smp_call_function_mask(cpus, ack_flush, NULL, 1);
142}
143
144
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000145int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
146{
147 struct page *page;
148 int r;
149
150 mutex_init(&vcpu->mutex);
151 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000152 vcpu->kvm = kvm;
153 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300154 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000155
156 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
157 if (!page) {
158 r = -ENOMEM;
159 goto fail;
160 }
161 vcpu->run = page_address(page);
162
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800163 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000164 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800165 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000166 return 0;
167
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000168fail_free_run:
169 free_page((unsigned long)vcpu->run);
170fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000171 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000172}
173EXPORT_SYMBOL_GPL(kvm_vcpu_init);
174
175void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
176{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800177 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000178 free_page((unsigned long)vcpu->run);
179}
180EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
181
Avi Kivityf17abe92007-02-21 19:28:04 +0200182static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800184 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800186 if (IS_ERR(kvm))
187 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200189 kvm->mm = current->mm;
190 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500191 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300192 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800193 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400194 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200195 init_rwsem(&kvm->slots_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000196 spin_lock(&kvm_lock);
197 list_add(&kvm->vm_list, &vm_list);
198 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800199out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200200 return kvm;
201}
202
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203/*
204 * Free any memory in @free but not in @dont.
205 */
206static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
207 struct kvm_memory_slot *dont)
208{
Izik Eidus290fc382007-09-27 14:11:22 +0200209 if (!dont || free->rmap != dont->rmap)
210 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211
212 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
213 vfree(free->dirty_bitmap);
214
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000216 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500217 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218}
219
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800220void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221{
222 int i;
223
224 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000225 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226}
227
Avi Kivityf17abe92007-02-21 19:28:04 +0200228static void kvm_destroy_vm(struct kvm *kvm)
229{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200230 struct mm_struct *mm = kvm->mm;
231
Avi Kivity133de902007-02-12 00:54:44 -0800232 spin_lock(&kvm_lock);
233 list_del(&kvm->vm_list);
234 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300235 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400236 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800237 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200238 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200239}
240
241static int kvm_vm_release(struct inode *inode, struct file *filp)
242{
243 struct kvm *kvm = filp->private_data;
244
245 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 return 0;
247}
248
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 * Allocate some memory and give it an address in the guest physical address
251 * space.
252 *
253 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800254 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500255 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800257int __kvm_set_memory_region(struct kvm *kvm,
258 struct kvm_userspace_memory_region *mem,
259 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260{
261 int r;
262 gfn_t base_gfn;
263 unsigned long npages;
264 unsigned long i;
265 struct kvm_memory_slot *memslot;
266 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267
268 r = -EINVAL;
269 /* General sanity checks */
270 if (mem->memory_size & (PAGE_SIZE - 1))
271 goto out;
272 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
273 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200274 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275 goto out;
276 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
277 goto out;
278
279 memslot = &kvm->memslots[mem->slot];
280 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
281 npages = mem->memory_size >> PAGE_SHIFT;
282
283 if (!npages)
284 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
285
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 new = old = *memslot;
287
288 new.base_gfn = base_gfn;
289 new.npages = npages;
290 new.flags = mem->flags;
291
292 /* Disallow changing a memory slot's size. */
293 r = -EINVAL;
294 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800295 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296
297 /* Check for overlaps */
298 r = -EEXIST;
299 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
300 struct kvm_memory_slot *s = &kvm->memslots[i];
301
302 if (s == memslot)
303 continue;
304 if (!((base_gfn + npages <= s->base_gfn) ||
305 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800306 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309 /* Free page dirty bitmap if unneeded */
310 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000311 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313 r = -ENOMEM;
314
315 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500316 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400317 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200318
319 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800320 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200321
Izik Eidus290fc382007-09-27 14:11:22 +0200322 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500323
Izik Eidus80b14b52007-10-25 11:54:04 +0200324 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800325 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 }
327
328 /* Allocate page dirty bitmap if needed */
329 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
330 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
331
332 new.dirty_bitmap = vmalloc(dirty_bytes);
333 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800334 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335 memset(new.dirty_bitmap, 0, dirty_bytes);
336 }
337
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 if (mem->slot >= kvm->nmemslots)
339 kvm->nmemslots = mem->slot + 1;
340
341 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342
Zhang Xiantao0de10342007-11-20 16:25:04 +0800343 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
344 if (r) {
345 *memslot = old;
346 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800347 }
348
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349 kvm_free_physmem_slot(&old, &new);
350 return 0;
351
Sheng Yangf78e0e22007-10-29 09:40:42 +0800352out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353 kvm_free_physmem_slot(&new, &old);
354out:
355 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200356
357}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800358EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
359
360int kvm_set_memory_region(struct kvm *kvm,
361 struct kvm_userspace_memory_region *mem,
362 int user_alloc)
363{
364 int r;
365
Izik Eidus72dc67a2008-02-10 18:04:15 +0200366 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800367 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200368 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800369 return r;
370}
Izik Eidus210c7c42007-10-24 23:52:57 +0200371EXPORT_SYMBOL_GPL(kvm_set_memory_region);
372
Carsten Otte1fe779f2007-10-29 16:08:35 +0100373int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
374 struct
375 kvm_userspace_memory_region *mem,
376 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200377{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200378 if (mem->slot >= KVM_MEMORY_SLOTS)
379 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200380 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381}
382
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800383int kvm_get_dirty_log(struct kvm *kvm,
384 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385{
386 struct kvm_memory_slot *memslot;
387 int r, i;
388 int n;
389 unsigned long any = 0;
390
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 r = -EINVAL;
392 if (log->slot >= KVM_MEMORY_SLOTS)
393 goto out;
394
395 memslot = &kvm->memslots[log->slot];
396 r = -ENOENT;
397 if (!memslot->dirty_bitmap)
398 goto out;
399
Uri Lublincd1a4a92007-02-22 16:43:09 +0200400 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401
Uri Lublincd1a4a92007-02-22 16:43:09 +0200402 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 any = memslot->dirty_bitmap[i];
404
405 r = -EFAULT;
406 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
407 goto out;
408
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800409 if (any)
410 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411
412 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return r;
415}
416
Izik Eiduscea7bb22007-10-17 19:17:48 +0200417int is_error_page(struct page *page)
418{
419 return page == bad_page;
420}
421EXPORT_SYMBOL_GPL(is_error_page);
422
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200423static inline unsigned long bad_hva(void)
424{
425 return PAGE_OFFSET;
426}
427
428int kvm_is_error_hva(unsigned long addr)
429{
430 return addr == bad_hva();
431}
432EXPORT_SYMBOL_GPL(kvm_is_error_hva);
433
Avi Kivitye8207542007-03-30 16:54:30 +0300434static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435{
436 int i;
437
438 for (i = 0; i < kvm->nmemslots; ++i) {
439 struct kvm_memory_slot *memslot = &kvm->memslots[i];
440
441 if (gfn >= memslot->base_gfn
442 && gfn < memslot->base_gfn + memslot->npages)
443 return memslot;
444 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000445 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446}
Avi Kivitye8207542007-03-30 16:54:30 +0300447
448struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
449{
450 gfn = unalias_gfn(kvm, gfn);
451 return __gfn_to_memslot(kvm, gfn);
452}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
Izik Eiduse0d62c72007-10-24 23:57:46 +0200454int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
455{
456 int i;
457
458 gfn = unalias_gfn(kvm, gfn);
459 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
460 struct kvm_memory_slot *memslot = &kvm->memslots[i];
461
462 if (gfn >= memslot->base_gfn
463 && gfn < memslot->base_gfn + memslot->npages)
464 return 1;
465 }
466 return 0;
467}
468EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
469
Izik Eidus539cb662007-11-11 22:05:04 +0200470static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
471{
472 struct kvm_memory_slot *slot;
473
474 gfn = unalias_gfn(kvm, gfn);
475 slot = __gfn_to_memslot(kvm, gfn);
476 if (!slot)
477 return bad_hva();
478 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
479}
480
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500481/*
482 * Requires current->mm->mmap_sem to be held
483 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500484struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300485{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500486 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200487 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500488 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300489
Avi Kivity60395222007-10-21 11:03:36 +0200490 might_sleep();
491
Izik Eidus539cb662007-11-11 22:05:04 +0200492 addr = gfn_to_hva(kvm, gfn);
493 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200494 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200495 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200496 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200497
Izik Eidus539cb662007-11-11 22:05:04 +0200498 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
499 NULL);
500
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500501 if (npages != 1) {
502 get_page(bad_page);
503 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200504 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500505
506 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300507}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500508
Avi Kivity954bbbc2007-03-30 14:02:32 +0300509EXPORT_SYMBOL_GPL(gfn_to_page);
510
Izik Eidusb4231d62007-11-20 11:49:33 +0200511void kvm_release_page_clean(struct page *page)
512{
513 put_page(page);
514}
515EXPORT_SYMBOL_GPL(kvm_release_page_clean);
516
517void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200518{
519 if (!PageReserved(page))
520 SetPageDirty(page);
521 put_page(page);
522}
Izik Eidusb4231d62007-11-20 11:49:33 +0200523EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200524
Izik Eidus195aefd2007-10-01 22:14:18 +0200525static int next_segment(unsigned long len, int offset)
526{
527 if (len > PAGE_SIZE - offset)
528 return PAGE_SIZE - offset;
529 else
530 return len;
531}
532
533int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
534 int len)
535{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200536 int r;
537 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200538
Izik Eiduse0506bc2007-11-11 22:10:22 +0200539 addr = gfn_to_hva(kvm, gfn);
540 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200541 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200542 r = copy_from_user(data, (void __user *)addr + offset, len);
543 if (r)
544 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200545 return 0;
546}
547EXPORT_SYMBOL_GPL(kvm_read_guest_page);
548
549int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
550{
551 gfn_t gfn = gpa >> PAGE_SHIFT;
552 int seg;
553 int offset = offset_in_page(gpa);
554 int ret;
555
556 while ((seg = next_segment(len, offset)) != 0) {
557 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
558 if (ret < 0)
559 return ret;
560 offset = 0;
561 len -= seg;
562 data += seg;
563 ++gfn;
564 }
565 return 0;
566}
567EXPORT_SYMBOL_GPL(kvm_read_guest);
568
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500569int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
570 unsigned long len)
571{
572 int r;
573 unsigned long addr;
574 gfn_t gfn = gpa >> PAGE_SHIFT;
575 int offset = offset_in_page(gpa);
576
577 addr = gfn_to_hva(kvm, gfn);
578 if (kvm_is_error_hva(addr))
579 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100580 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500581 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100582 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500583 if (r)
584 return -EFAULT;
585 return 0;
586}
587EXPORT_SYMBOL(kvm_read_guest_atomic);
588
Izik Eidus195aefd2007-10-01 22:14:18 +0200589int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
590 int offset, int len)
591{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200592 int r;
593 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200594
Izik Eiduse0506bc2007-11-11 22:10:22 +0200595 addr = gfn_to_hva(kvm, gfn);
596 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200597 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200598 r = copy_to_user((void __user *)addr + offset, data, len);
599 if (r)
600 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200601 mark_page_dirty(kvm, gfn);
602 return 0;
603}
604EXPORT_SYMBOL_GPL(kvm_write_guest_page);
605
606int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
607 unsigned long len)
608{
609 gfn_t gfn = gpa >> PAGE_SHIFT;
610 int seg;
611 int offset = offset_in_page(gpa);
612 int ret;
613
614 while ((seg = next_segment(len, offset)) != 0) {
615 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
616 if (ret < 0)
617 return ret;
618 offset = 0;
619 len -= seg;
620 data += seg;
621 ++gfn;
622 }
623 return 0;
624}
625
626int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
627{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200628 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200629}
630EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
631
632int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
633{
634 gfn_t gfn = gpa >> PAGE_SHIFT;
635 int seg;
636 int offset = offset_in_page(gpa);
637 int ret;
638
639 while ((seg = next_segment(len, offset)) != 0) {
640 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
641 if (ret < 0)
642 return ret;
643 offset = 0;
644 len -= seg;
645 ++gfn;
646 }
647 return 0;
648}
649EXPORT_SYMBOL_GPL(kvm_clear_guest);
650
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
652{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300653 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654
Uri Lublin3b6fff12007-10-30 10:42:09 +0200655 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000656 memslot = __gfn_to_memslot(kvm, gfn);
657 if (memslot && memslot->dirty_bitmap) {
658 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Rusty Russell7e9d6192007-07-31 20:41:14 +1000660 /* avoid RMW */
661 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
662 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 }
664}
665
Eddie Dongb6958ce2007-07-18 12:15:21 +0300666/*
667 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
668 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500669void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300670{
671 DECLARE_WAITQUEUE(wait, current);
672
673 add_wait_queue(&vcpu->wq, &wait);
674
675 /*
676 * We will block until either an interrupt or a signal wakes us up
677 */
He, Qingc5ec1532007-09-03 17:07:41 +0300678 while (!kvm_cpu_has_interrupt(vcpu)
679 && !signal_pending(current)
Hollis Blanchard53e0aa72007-12-03 16:15:26 -0600680 && !kvm_arch_vcpu_runnable(vcpu)) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300681 set_current_state(TASK_INTERRUPTIBLE);
682 vcpu_put(vcpu);
683 schedule();
684 vcpu_load(vcpu);
685 }
686
He, Qingc5ec1532007-09-03 17:07:41 +0300687 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300688 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300689}
690
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691void kvm_resched(struct kvm_vcpu *vcpu)
692{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300693 if (!need_resched())
694 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696}
697EXPORT_SYMBOL_GPL(kvm_resched);
698
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100699static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200700{
701 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200702 struct page *page;
703
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100704 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +0200705 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +0200706#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100707 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800708 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +0200709#endif
Avi Kivity039576c2007-03-20 12:46:50 +0200710 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100711 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200712 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100713 vmf->page = page;
714 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200715}
716
717static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100718 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200719};
720
721static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
722{
723 vma->vm_ops = &kvm_vcpu_vm_ops;
724 return 0;
725}
726
Avi Kivitybccf2152007-02-21 18:04:26 +0200727static int kvm_vcpu_release(struct inode *inode, struct file *filp)
728{
729 struct kvm_vcpu *vcpu = filp->private_data;
730
731 fput(vcpu->kvm->filp);
732 return 0;
733}
734
Jan Engelhardt5c502742008-01-22 20:46:14 +0100735static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +0200736 .release = kvm_vcpu_release,
737 .unlocked_ioctl = kvm_vcpu_ioctl,
738 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200739 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200740};
741
742/*
743 * Allocates an inode for the vcpu.
744 */
745static int create_vcpu_fd(struct kvm_vcpu *vcpu)
746{
747 int fd, r;
748 struct inode *inode;
749 struct file *file;
750
Avi Kivityd6d28162007-06-28 08:38:16 -0400751 r = anon_inode_getfd(&fd, &inode, &file,
752 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
753 if (r)
754 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200755 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200756 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200757}
758
Avi Kivityc5ea7662007-02-20 18:41:05 +0200759/*
760 * Creates some virtual cpus. Good luck creating more than one.
761 */
762static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
763{
764 int r;
765 struct kvm_vcpu *vcpu;
766
Avi Kivityc5ea7662007-02-20 18:41:05 +0200767 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000768 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200769
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800770 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000771 if (IS_ERR(vcpu))
772 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200773
Avi Kivity15ad7142007-07-11 18:17:21 +0300774 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
775
Avi Kivity26e52152007-11-20 15:30:24 +0200776 r = kvm_arch_vcpu_setup(vcpu);
777 if (r)
778 goto vcpu_destroy;
779
Shaohua Li11ec2802007-07-23 14:51:37 +0800780 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781 if (kvm->vcpus[n]) {
782 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800783 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800784 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000785 }
786 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800787 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000788
789 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200790 r = create_vcpu_fd(vcpu);
791 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000792 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200793 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200794
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000795unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800796 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000797 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800798 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800799vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600800 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200801 return r;
802}
803
Avi Kivity1961d272007-03-05 19:46:05 +0200804static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
805{
806 if (sigset) {
807 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
808 vcpu->sigset_active = 1;
809 vcpu->sigset = *sigset;
810 } else
811 vcpu->sigset_active = 0;
812 return 0;
813}
814
Avi Kivitybccf2152007-02-21 18:04:26 +0200815static long kvm_vcpu_ioctl(struct file *filp,
816 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817{
Avi Kivitybccf2152007-02-21 18:04:26 +0200818 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000819 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200820 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200822 if (vcpu->kvm->mm != current->mm)
823 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200825 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200826 r = -EINVAL;
827 if (arg)
828 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500829 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831 case KVM_GET_REGS: {
832 struct kvm_regs kvm_regs;
833
Avi Kivitybccf2152007-02-21 18:04:26 +0200834 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500835 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 if (r)
837 goto out;
838 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000839 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 goto out;
841 r = 0;
842 break;
843 }
844 case KVM_SET_REGS: {
845 struct kvm_regs kvm_regs;
846
847 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000848 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500850 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 if (r)
852 goto out;
853 r = 0;
854 break;
855 }
856 case KVM_GET_SREGS: {
857 struct kvm_sregs kvm_sregs;
858
Avi Kivitybccf2152007-02-21 18:04:26 +0200859 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500860 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
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_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 goto out;
866 r = 0;
867 break;
868 }
869 case KVM_SET_SREGS: {
870 struct kvm_sregs kvm_sregs;
871
872 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000873 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500875 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 if (r)
877 goto out;
878 r = 0;
879 break;
880 }
881 case KVM_TRANSLATE: {
882 struct kvm_translation tr;
883
884 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000885 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800887 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 if (r)
889 goto out;
890 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000891 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 goto out;
893 r = 0;
894 break;
895 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 case KVM_DEBUG_GUEST: {
897 struct kvm_debug_guest dbg;
898
899 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000900 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500902 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 if (r)
904 goto out;
905 r = 0;
906 break;
907 }
Avi Kivity1961d272007-03-05 19:46:05 +0200908 case KVM_SET_SIGNAL_MASK: {
909 struct kvm_signal_mask __user *sigmask_arg = argp;
910 struct kvm_signal_mask kvm_sigmask;
911 sigset_t sigset, *p;
912
913 p = NULL;
914 if (argp) {
915 r = -EFAULT;
916 if (copy_from_user(&kvm_sigmask, argp,
917 sizeof kvm_sigmask))
918 goto out;
919 r = -EINVAL;
920 if (kvm_sigmask.len != sizeof sigset)
921 goto out;
922 r = -EFAULT;
923 if (copy_from_user(&sigset, sigmask_arg->sigset,
924 sizeof sigset))
925 goto out;
926 p = &sigset;
927 }
928 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
929 break;
930 }
Avi Kivityb8836732007-04-01 16:34:31 +0300931 case KVM_GET_FPU: {
932 struct kvm_fpu fpu;
933
934 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500935 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300936 if (r)
937 goto out;
938 r = -EFAULT;
939 if (copy_to_user(argp, &fpu, sizeof fpu))
940 goto out;
941 r = 0;
942 break;
943 }
944 case KVM_SET_FPU: {
945 struct kvm_fpu fpu;
946
947 r = -EFAULT;
948 if (copy_from_user(&fpu, argp, sizeof fpu))
949 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500950 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300951 if (r)
952 goto out;
953 r = 0;
954 break;
955 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200956 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200957 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200958 }
959out:
960 return r;
961}
962
963static long kvm_vm_ioctl(struct file *filp,
964 unsigned int ioctl, unsigned long arg)
965{
966 struct kvm *kvm = filp->private_data;
967 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +0100968 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200969
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200970 if (kvm->mm != current->mm)
971 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200972 switch (ioctl) {
973 case KVM_CREATE_VCPU:
974 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
975 if (r < 0)
976 goto out;
977 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200978 case KVM_SET_USER_MEMORY_REGION: {
979 struct kvm_userspace_memory_region kvm_userspace_mem;
980
981 r = -EFAULT;
982 if (copy_from_user(&kvm_userspace_mem, argp,
983 sizeof kvm_userspace_mem))
984 goto out;
985
986 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 if (r)
988 goto out;
989 break;
990 }
991 case KVM_GET_DIRTY_LOG: {
992 struct kvm_dirty_log log;
993
994 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000995 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200997 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 if (r)
999 goto out;
1000 break;
1001 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001002 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001003 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001004 }
1005out:
1006 return r;
1007}
1008
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001009static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001010{
1011 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001012 struct page *page;
1013
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001014 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1015 return VM_FAULT_SIGBUS;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001016 page = gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001017 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001018 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001019 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001020 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001021 vmf->page = page;
1022 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001023}
1024
1025static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001026 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001027};
1028
1029static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1030{
1031 vma->vm_ops = &kvm_vm_vm_ops;
1032 return 0;
1033}
1034
Jan Engelhardt5c502742008-01-22 20:46:14 +01001035static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001036 .release = kvm_vm_release,
1037 .unlocked_ioctl = kvm_vm_ioctl,
1038 .compat_ioctl = kvm_vm_ioctl,
1039 .mmap = kvm_vm_mmap,
1040};
1041
1042static int kvm_dev_ioctl_create_vm(void)
1043{
1044 int fd, r;
1045 struct inode *inode;
1046 struct file *file;
1047 struct kvm *kvm;
1048
Avi Kivityf17abe92007-02-21 19:28:04 +02001049 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001050 if (IS_ERR(kvm))
1051 return PTR_ERR(kvm);
1052 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1053 if (r) {
1054 kvm_destroy_vm(kvm);
1055 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001056 }
1057
Avi Kivitybccf2152007-02-21 18:04:26 +02001058 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001059
Avi Kivityf17abe92007-02-21 19:28:04 +02001060 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001061}
1062
1063static long kvm_dev_ioctl(struct file *filp,
1064 unsigned int ioctl, unsigned long arg)
1065{
1066 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001067 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001068
1069 switch (ioctl) {
1070 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001071 r = -EINVAL;
1072 if (arg)
1073 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001074 r = KVM_API_VERSION;
1075 break;
1076 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001077 r = -EINVAL;
1078 if (arg)
1079 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001080 r = kvm_dev_ioctl_create_vm();
1081 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001082 case KVM_CHECK_EXTENSION:
1083 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001084 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001085 case KVM_GET_VCPU_MMAP_SIZE:
1086 r = -EINVAL;
1087 if (arg)
1088 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001089 r = PAGE_SIZE; /* struct kvm_run */
1090#ifdef CONFIG_X86
1091 r += PAGE_SIZE; /* pio data page */
1092#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001093 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001095 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 }
1097out:
1098 return r;
1099}
1100
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 .unlocked_ioctl = kvm_dev_ioctl,
1103 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104};
1105
1106static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001107 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 "kvm",
1109 &kvm_chardev_ops,
1110};
1111
Avi Kivity1b6c0162007-05-24 13:03:52 +03001112static void hardware_enable(void *junk)
1113{
1114 int cpu = raw_smp_processor_id();
1115
1116 if (cpu_isset(cpu, cpus_hardware_enabled))
1117 return;
1118 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001119 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001120}
1121
1122static void hardware_disable(void *junk)
1123{
1124 int cpu = raw_smp_processor_id();
1125
1126 if (!cpu_isset(cpu, cpus_hardware_enabled))
1127 return;
1128 cpu_clear(cpu, cpus_hardware_enabled);
1129 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001130 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001131}
1132
Avi Kivity774c47f2007-02-12 00:54:47 -08001133static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1134 void *v)
1135{
1136 int cpu = (long)v;
1137
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001138 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001139 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001140 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001141 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1142 cpu);
1143 hardware_disable(NULL);
1144 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001145 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001146 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1147 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001148 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001149 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001150 case CPU_ONLINE:
1151 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1152 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001153 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001154 break;
1155 }
1156 return NOTIFY_OK;
1157}
1158
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001159static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001160 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001161{
1162 if (val == SYS_RESTART) {
1163 /*
1164 * Some (well, at least mine) BIOSes hang on reboot if
1165 * in vmx root mode.
1166 */
1167 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1168 on_each_cpu(hardware_disable, NULL, 0, 1);
1169 }
1170 return NOTIFY_OK;
1171}
1172
1173static struct notifier_block kvm_reboot_notifier = {
1174 .notifier_call = kvm_reboot,
1175 .priority = 0,
1176};
1177
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001178void kvm_io_bus_init(struct kvm_io_bus *bus)
1179{
1180 memset(bus, 0, sizeof(*bus));
1181}
1182
1183void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1184{
1185 int i;
1186
1187 for (i = 0; i < bus->dev_count; i++) {
1188 struct kvm_io_device *pos = bus->devs[i];
1189
1190 kvm_iodevice_destructor(pos);
1191 }
1192}
1193
1194struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1195{
1196 int i;
1197
1198 for (i = 0; i < bus->dev_count; i++) {
1199 struct kvm_io_device *pos = bus->devs[i];
1200
1201 if (pos->in_range(pos, addr))
1202 return pos;
1203 }
1204
1205 return NULL;
1206}
1207
1208void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1209{
1210 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1211
1212 bus->devs[bus->dev_count++] = dev;
1213}
1214
Avi Kivity774c47f2007-02-12 00:54:47 -08001215static struct notifier_block kvm_cpu_notifier = {
1216 .notifier_call = kvm_cpu_hotplug,
1217 .priority = 20, /* must be > scheduler priority */
1218};
1219
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001220static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001221{
1222 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001223 struct kvm *kvm;
1224
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001225 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001226 spin_lock(&kvm_lock);
1227 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001228 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001229 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001230 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001231}
1232
1233DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1234
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001235static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001236{
1237 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001238 struct kvm *kvm;
1239 struct kvm_vcpu *vcpu;
1240 int i;
1241
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001242 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001243 spin_lock(&kvm_lock);
1244 list_for_each_entry(kvm, &vm_list, vm_list)
1245 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001246 vcpu = kvm->vcpus[i];
1247 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001248 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001249 }
1250 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001251 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001252}
1253
Avi Kivityba1389b2007-11-18 16:24:12 +02001254DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1255
1256static struct file_operations *stat_fops[] = {
1257 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1258 [KVM_STAT_VM] = &vm_stat_fops,
1259};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001260
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001261static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262{
1263 struct kvm_stats_debugfs_item *p;
1264
Al Viro8b6d44c2007-02-09 16:38:40 +00001265 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001267 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1268 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001269 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270}
1271
1272static void kvm_exit_debug(void)
1273{
1274 struct kvm_stats_debugfs_item *p;
1275
1276 for (p = debugfs_entries; p->name; ++p)
1277 debugfs_remove(p->dentry);
1278 debugfs_remove(debugfs_dir);
1279}
1280
Avi Kivity59ae6c62007-02-12 00:54:48 -08001281static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1282{
Avi Kivity4267c412007-05-24 13:09:41 +03001283 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001284 return 0;
1285}
1286
1287static int kvm_resume(struct sys_device *dev)
1288{
Avi Kivity4267c412007-05-24 13:09:41 +03001289 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001290 return 0;
1291}
1292
1293static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001294 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001295 .suspend = kvm_suspend,
1296 .resume = kvm_resume,
1297};
1298
1299static struct sys_device kvm_sysdev = {
1300 .id = 0,
1301 .cls = &kvm_sysdev_class,
1302};
1303
Izik Eiduscea7bb22007-10-17 19:17:48 +02001304struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305
Avi Kivity15ad7142007-07-11 18:17:21 +03001306static inline
1307struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1308{
1309 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1310}
1311
1312static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1313{
1314 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1315
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001316 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001317}
1318
1319static void kvm_sched_out(struct preempt_notifier *pn,
1320 struct task_struct *next)
1321{
1322 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1323
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001324 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001325}
1326
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001327int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001328 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329{
1330 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001331 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001333 kvm_init_debug();
1334
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001335 r = kvm_arch_init(opaque);
1336 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001337 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001338
1339 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1340
1341 if (bad_page == NULL) {
1342 r = -ENOMEM;
1343 goto out;
1344 }
1345
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001346 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001348 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349
Yang, Sheng002c7f72007-07-31 14:23:01 +03001350 for_each_online_cpu(cpu) {
1351 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001352 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001353 &r, 0, 1);
1354 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001355 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001356 }
1357
Avi Kivity1b6c0162007-05-24 13:03:52 +03001358 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001359 r = register_cpu_notifier(&kvm_cpu_notifier);
1360 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001361 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 register_reboot_notifier(&kvm_reboot_notifier);
1363
Avi Kivity59ae6c62007-02-12 00:54:48 -08001364 r = sysdev_class_register(&kvm_sysdev_class);
1365 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001366 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001367
1368 r = sysdev_register(&kvm_sysdev);
1369 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001370 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001371
Rusty Russellc16f8622007-07-30 21:12:19 +10001372 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1373 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001374 __alignof__(struct kvm_vcpu),
1375 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001376 if (!kvm_vcpu_cache) {
1377 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001378 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001379 }
1380
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 kvm_chardev_ops.owner = module;
1382
1383 r = misc_register(&kvm_dev);
1384 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001385 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 goto out_free;
1387 }
1388
Avi Kivity15ad7142007-07-11 18:17:21 +03001389 kvm_preempt_ops.sched_in = kvm_sched_in;
1390 kvm_preempt_ops.sched_out = kvm_sched_out;
1391
Avi Kivityc7addb92007-09-16 18:58:32 +02001392 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
1394out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001395 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001396out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001397 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001398out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001399 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001400out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001402 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001403out_free_2:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001404 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001405out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001406 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001407out_free_0:
1408 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001409out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001410 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001411 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001412out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 return r;
1414}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001415EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001417void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418{
1419 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001420 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001421 sysdev_unregister(&kvm_sysdev);
1422 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001424 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001425 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001426 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001427 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001429 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001431EXPORT_SYMBOL_GPL(kvm_exit);