blob: 3946025f3d37c60d431b7f822de78e9f8af2c833 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
50MODULE_AUTHOR("Qumranet");
51MODULE_LICENSE("GPL");
52
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080053DEFINE_SPINLOCK(kvm_lock);
54LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080055
Avi Kivity1b6c0162007-05-24 13:03:52 +030056static cpumask_t cpus_hardware_enabled;
57
Rusty Russellc16f8622007-07-30 21:12:19 +100058struct kmem_cache *kvm_vcpu_cache;
59EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030060
Avi Kivity15ad7142007-07-11 18:17:21 +030061static __read_mostly struct preempt_ops kvm_preempt_ops;
62
Avi Kivity6aa8b732006-12-10 02:21:36 -080063static struct dentry *debugfs_dir;
64
Avi Kivitybccf2152007-02-21 18:04:26 +020065static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
66 unsigned long arg);
67
James Morris5aacf0c2006-12-22 01:04:55 -080068static inline int valid_vcpu(int n)
69{
70 return likely(n >= 0 && n < KVM_MAX_VCPUS);
71}
72
Avi Kivity6aa8b732006-12-10 02:21:36 -080073/*
74 * Switches to specified vcpu, until a matching vcpu_put()
75 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020076void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077{
Avi Kivity15ad7142007-07-11 18:17:21 +030078 int cpu;
79
Avi Kivitybccf2152007-02-21 18:04:26 +020080 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030081 cpu = get_cpu();
82 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020083 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030084 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020085}
86
Carsten Otte313a3dc2007-10-11 19:16:52 +020087void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080088{
Avi Kivity15ad7142007-07-11 18:17:21 +030089 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020090 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030091 preempt_notifier_unregister(&vcpu->preempt_notifier);
92 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080093 mutex_unlock(&vcpu->mutex);
94}
95
Avi Kivityd9e368d2007-06-07 19:18:30 +030096static void ack_flush(void *_completed)
97{
Avi Kivityd9e368d2007-06-07 19:18:30 +030098}
99
100void kvm_flush_remote_tlbs(struct kvm *kvm)
101{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200102 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300103 cpumask_t cpus;
104 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300105
Avi Kivityd9e368d2007-06-07 19:18:30 +0300106 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000107 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
108 vcpu = kvm->vcpus[i];
109 if (!vcpu)
110 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200111 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300112 continue;
113 cpu = vcpu->cpu;
114 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200115 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300116 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200117 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300118}
119
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000120int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
121{
122 struct page *page;
123 int r;
124
125 mutex_init(&vcpu->mutex);
126 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000127 vcpu->kvm = kvm;
128 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300129 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000130
131 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
132 if (!page) {
133 r = -ENOMEM;
134 goto fail;
135 }
136 vcpu->run = page_address(page);
137
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800138 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000139 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800140 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000141 return 0;
142
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000143fail_free_run:
144 free_page((unsigned long)vcpu->run);
145fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000146 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000147}
148EXPORT_SYMBOL_GPL(kvm_vcpu_init);
149
150void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
151{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800152 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000153 free_page((unsigned long)vcpu->run);
154}
155EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
156
Avi Kivityf17abe92007-02-21 19:28:04 +0200157static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158{
159 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160
161 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200162 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163
Eddie Dong74906342007-06-19 18:05:03 +0300164 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800165 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 INIT_LIST_HEAD(&kvm->active_mmu_pages);
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);
Avi Kivityf17abe92007-02-21 19:28:04 +0200171 return kvm;
172}
173
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174/*
175 * Free any memory in @free but not in @dont.
176 */
177static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
178 struct kvm_memory_slot *dont)
179{
Izik Eidus290fc382007-09-27 14:11:22 +0200180 if (!dont || free->rmap != dont->rmap)
181 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182
183 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
184 vfree(free->dirty_bitmap);
185
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000187 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500188 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189}
190
191static void kvm_free_physmem(struct kvm *kvm)
192{
193 int i;
194
195 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000196 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197}
198
Avi Kivity7b53aa52007-06-05 12:17:03 +0300199static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
200{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300201 vcpu_load(vcpu);
202 kvm_mmu_unload(vcpu);
203 vcpu_put(vcpu);
204}
205
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206static void kvm_free_vcpus(struct kvm *kvm)
207{
208 unsigned int i;
209
Avi Kivity7b53aa52007-06-05 12:17:03 +0300210 /*
211 * Unpin any mmu pages first.
212 */
213 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000214 if (kvm->vcpus[i])
215 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
216 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
217 if (kvm->vcpus[i]) {
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800218 kvm_arch_vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000219 kvm->vcpus[i] = NULL;
220 }
221 }
222
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223}
224
Avi Kivityf17abe92007-02-21 19:28:04 +0200225static void kvm_destroy_vm(struct kvm *kvm)
226{
Avi Kivity133de902007-02-12 00:54:44 -0800227 spin_lock(&kvm_lock);
228 list_del(&kvm->vm_list);
229 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300230 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400231 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300232 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300233 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 kvm_free_vcpus(kvm);
235 kvm_free_physmem(kvm);
236 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200237}
238
239static int kvm_vm_release(struct inode *inode, struct file *filp)
240{
241 struct kvm *kvm = filp->private_data;
242
243 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 return 0;
245}
246
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 * Allocate some memory and give it an address in the guest physical address
249 * space.
250 *
251 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800252 *
253 * Must be called holding kvm->lock.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800255int __kvm_set_memory_region(struct kvm *kvm,
256 struct kvm_userspace_memory_region *mem,
257 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258{
259 int r;
260 gfn_t base_gfn;
261 unsigned long npages;
262 unsigned long i;
263 struct kvm_memory_slot *memslot;
264 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265
266 r = -EINVAL;
267 /* General sanity checks */
268 if (mem->memory_size & (PAGE_SIZE - 1))
269 goto out;
270 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
271 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200272 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273 goto out;
274 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
275 goto out;
276
277 memslot = &kvm->memslots[mem->slot];
278 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
279 npages = mem->memory_size >> PAGE_SHIFT;
280
281 if (!npages)
282 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
283
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 new = old = *memslot;
285
286 new.base_gfn = base_gfn;
287 new.npages = npages;
288 new.flags = mem->flags;
289
290 /* Disallow changing a memory slot's size. */
291 r = -EINVAL;
292 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800293 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294
295 /* Check for overlaps */
296 r = -EEXIST;
297 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
298 struct kvm_memory_slot *s = &kvm->memslots[i];
299
300 if (s == memslot)
301 continue;
302 if (!((base_gfn + npages <= s->base_gfn) ||
303 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800304 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 /* Free page dirty bitmap if unneeded */
308 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000309 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310
311 r = -ENOMEM;
312
313 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500314 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400315 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200316
317 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800318 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200319
Izik Eidus290fc382007-09-27 14:11:22 +0200320 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500321
Izik Eidus80b14b52007-10-25 11:54:04 +0200322 new.user_alloc = user_alloc;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500323 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200324 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500325 else {
326 down_write(&current->mm->mmap_sem);
327 new.userspace_addr = do_mmap(NULL, 0,
328 npages * PAGE_SIZE,
329 PROT_READ | PROT_WRITE,
330 MAP_SHARED | MAP_ANONYMOUS,
331 0);
332 up_write(&current->mm->mmap_sem);
333
334 if (IS_ERR((void *)new.userspace_addr))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800335 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200337 } else {
338 if (!old.user_alloc && old.rmap) {
339 int ret;
340
341 down_write(&current->mm->mmap_sem);
342 ret = do_munmap(current->mm, old.userspace_addr,
343 old.npages * PAGE_SIZE);
344 up_write(&current->mm->mmap_sem);
345 if (ret < 0)
346 printk(KERN_WARNING
347 "kvm_vm_ioctl_set_memory_region: "
348 "failed to munmap memory\n");
349 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350 }
351
352 /* Allocate page dirty bitmap if needed */
353 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
354 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
355
356 new.dirty_bitmap = vmalloc(dirty_bytes);
357 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800358 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 memset(new.dirty_bitmap, 0, dirty_bytes);
360 }
361
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 if (mem->slot >= kvm->nmemslots)
363 kvm->nmemslots = mem->slot + 1;
364
Izik Eidus82ce2c92007-10-02 18:52:55 +0200365 if (!kvm->n_requested_mmu_pages) {
366 unsigned int n_pages;
367
368 if (npages) {
369 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
370 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
371 n_pages);
372 } else {
373 unsigned int nr_mmu_pages;
374
375 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
376 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
377 nr_mmu_pages = max(nr_mmu_pages,
378 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
379 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
380 }
381 }
382
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384
Avi Kivity90cb0522007-07-17 13:04:56 +0300385 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
386 kvm_flush_remote_tlbs(kvm);
387
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 kvm_free_physmem_slot(&old, &new);
389 return 0;
390
Sheng Yangf78e0e22007-10-29 09:40:42 +0800391out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392 kvm_free_physmem_slot(&new, &old);
393out:
394 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200395
396}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800397EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
398
399int kvm_set_memory_region(struct kvm *kvm,
400 struct kvm_userspace_memory_region *mem,
401 int user_alloc)
402{
403 int r;
404
405 mutex_lock(&kvm->lock);
406 r = __kvm_set_memory_region(kvm, mem, user_alloc);
407 mutex_unlock(&kvm->lock);
408 return r;
409}
Izik Eidus210c7c42007-10-24 23:52:57 +0200410EXPORT_SYMBOL_GPL(kvm_set_memory_region);
411
Carsten Otte1fe779f2007-10-29 16:08:35 +0100412int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
413 struct
414 kvm_userspace_memory_region *mem,
415 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200416{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200417 if (mem->slot >= KVM_MEMORY_SLOTS)
418 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200419 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
422/*
423 * Get (and clear) the dirty memory log for a memory slot.
424 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200425static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
426 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427{
428 struct kvm_memory_slot *memslot;
429 int r, i;
430 int n;
431 unsigned long any = 0;
432
Shaohua Li11ec2802007-07-23 14:51:37 +0800433 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 r = -EINVAL;
436 if (log->slot >= KVM_MEMORY_SLOTS)
437 goto out;
438
439 memslot = &kvm->memslots[log->slot];
440 r = -ENOENT;
441 if (!memslot->dirty_bitmap)
442 goto out;
443
Uri Lublincd1a4a92007-02-22 16:43:09 +0200444 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445
Uri Lublincd1a4a92007-02-22 16:43:09 +0200446 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 any = memslot->dirty_bitmap[i];
448
449 r = -EFAULT;
450 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
451 goto out;
452
Rusty Russell39214912007-07-31 19:57:47 +1000453 /* If nothing is dirty, don't bother messing with page tables. */
454 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000455 kvm_mmu_slot_remove_write_access(kvm, log->slot);
456 kvm_flush_remote_tlbs(kvm);
457 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000458 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459
460 r = 0;
461
462out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800463 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 return r;
465}
466
Izik Eiduscea7bb22007-10-17 19:17:48 +0200467int is_error_page(struct page *page)
468{
469 return page == bad_page;
470}
471EXPORT_SYMBOL_GPL(is_error_page);
472
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200473static inline unsigned long bad_hva(void)
474{
475 return PAGE_OFFSET;
476}
477
478int kvm_is_error_hva(unsigned long addr)
479{
480 return addr == bad_hva();
481}
482EXPORT_SYMBOL_GPL(kvm_is_error_hva);
483
Izik Eidus290fc382007-09-27 14:11:22 +0200484gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300485{
486 int i;
487 struct kvm_mem_alias *alias;
488
489 for (i = 0; i < kvm->naliases; ++i) {
490 alias = &kvm->aliases[i];
491 if (gfn >= alias->base_gfn
492 && gfn < alias->base_gfn + alias->npages)
493 return alias->target_gfn + gfn - alias->base_gfn;
494 }
495 return gfn;
496}
497
498static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499{
500 int i;
501
502 for (i = 0; i < kvm->nmemslots; ++i) {
503 struct kvm_memory_slot *memslot = &kvm->memslots[i];
504
505 if (gfn >= memslot->base_gfn
506 && gfn < memslot->base_gfn + memslot->npages)
507 return memslot;
508 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000509 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510}
Avi Kivitye8207542007-03-30 16:54:30 +0300511
512struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
513{
514 gfn = unalias_gfn(kvm, gfn);
515 return __gfn_to_memslot(kvm, gfn);
516}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517
Izik Eiduse0d62c72007-10-24 23:57:46 +0200518int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
519{
520 int i;
521
522 gfn = unalias_gfn(kvm, gfn);
523 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
524 struct kvm_memory_slot *memslot = &kvm->memslots[i];
525
526 if (gfn >= memslot->base_gfn
527 && gfn < memslot->base_gfn + memslot->npages)
528 return 1;
529 }
530 return 0;
531}
532EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
533
Izik Eidus539cb662007-11-11 22:05:04 +0200534static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
535{
536 struct kvm_memory_slot *slot;
537
538 gfn = unalias_gfn(kvm, gfn);
539 slot = __gfn_to_memslot(kvm, gfn);
540 if (!slot)
541 return bad_hva();
542 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
543}
544
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500545/*
546 * Requires current->mm->mmap_sem to be held
547 */
548static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300549{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500550 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200551 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500552 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300553
Avi Kivity60395222007-10-21 11:03:36 +0200554 might_sleep();
555
Izik Eidus539cb662007-11-11 22:05:04 +0200556 addr = gfn_to_hva(kvm, gfn);
557 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200558 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200559 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200560 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200561
Izik Eidus539cb662007-11-11 22:05:04 +0200562 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
563 NULL);
564
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500565 if (npages != 1) {
566 get_page(bad_page);
567 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200568 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500569
570 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300571}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500572
573struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
574{
575 struct page *page;
576
577 down_read(&current->mm->mmap_sem);
578 page = __gfn_to_page(kvm, gfn);
579 up_read(&current->mm->mmap_sem);
580
581 return page;
582}
583
Avi Kivity954bbbc2007-03-30 14:02:32 +0300584EXPORT_SYMBOL_GPL(gfn_to_page);
585
Izik Eidus8a7ae052007-10-18 11:09:33 +0200586void kvm_release_page(struct page *page)
587{
588 if (!PageReserved(page))
589 SetPageDirty(page);
590 put_page(page);
591}
592EXPORT_SYMBOL_GPL(kvm_release_page);
593
Izik Eidus195aefd2007-10-01 22:14:18 +0200594static int next_segment(unsigned long len, int offset)
595{
596 if (len > PAGE_SIZE - offset)
597 return PAGE_SIZE - offset;
598 else
599 return len;
600}
601
602int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
603 int len)
604{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200605 int r;
606 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200607
Izik Eiduse0506bc2007-11-11 22:10:22 +0200608 addr = gfn_to_hva(kvm, gfn);
609 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200610 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200611 r = copy_from_user(data, (void __user *)addr + offset, len);
612 if (r)
613 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200614 return 0;
615}
616EXPORT_SYMBOL_GPL(kvm_read_guest_page);
617
618int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
619{
620 gfn_t gfn = gpa >> PAGE_SHIFT;
621 int seg;
622 int offset = offset_in_page(gpa);
623 int ret;
624
625 while ((seg = next_segment(len, offset)) != 0) {
626 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
627 if (ret < 0)
628 return ret;
629 offset = 0;
630 len -= seg;
631 data += seg;
632 ++gfn;
633 }
634 return 0;
635}
636EXPORT_SYMBOL_GPL(kvm_read_guest);
637
638int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
639 int offset, int len)
640{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200641 int r;
642 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200643
Izik Eiduse0506bc2007-11-11 22:10:22 +0200644 addr = gfn_to_hva(kvm, gfn);
645 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200646 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200647 r = copy_to_user((void __user *)addr + offset, data, len);
648 if (r)
649 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200650 mark_page_dirty(kvm, gfn);
651 return 0;
652}
653EXPORT_SYMBOL_GPL(kvm_write_guest_page);
654
655int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
656 unsigned long len)
657{
658 gfn_t gfn = gpa >> PAGE_SHIFT;
659 int seg;
660 int offset = offset_in_page(gpa);
661 int ret;
662
663 while ((seg = next_segment(len, offset)) != 0) {
664 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
665 if (ret < 0)
666 return ret;
667 offset = 0;
668 len -= seg;
669 data += seg;
670 ++gfn;
671 }
672 return 0;
673}
674
675int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
676{
677 void *page_virt;
678 struct page *page;
679
680 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200681 if (is_error_page(page)) {
682 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200683 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200684 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200685 page_virt = kmap_atomic(page, KM_USER0);
686
687 memset(page_virt + offset, 0, len);
688
689 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200690 kvm_release_page(page);
Dor Laor12264762007-11-07 16:20:06 +0200691 mark_page_dirty(kvm, gfn);
Izik Eidus195aefd2007-10-01 22:14:18 +0200692 return 0;
693}
694EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
695
696int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
697{
698 gfn_t gfn = gpa >> PAGE_SHIFT;
699 int seg;
700 int offset = offset_in_page(gpa);
701 int ret;
702
703 while ((seg = next_segment(len, offset)) != 0) {
704 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
705 if (ret < 0)
706 return ret;
707 offset = 0;
708 len -= seg;
709 ++gfn;
710 }
711 return 0;
712}
713EXPORT_SYMBOL_GPL(kvm_clear_guest);
714
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
716{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300717 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718
Uri Lublin3b6fff12007-10-30 10:42:09 +0200719 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000720 memslot = __gfn_to_memslot(kvm, gfn);
721 if (memslot && memslot->dirty_bitmap) {
722 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723
Rusty Russell7e9d6192007-07-31 20:41:14 +1000724 /* avoid RMW */
725 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
726 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727 }
728}
729
Eddie Dongb6958ce2007-07-18 12:15:21 +0300730/*
731 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
732 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500733void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300734{
735 DECLARE_WAITQUEUE(wait, current);
736
737 add_wait_queue(&vcpu->wq, &wait);
738
739 /*
740 * We will block until either an interrupt or a signal wakes us up
741 */
He, Qingc5ec1532007-09-03 17:07:41 +0300742 while (!kvm_cpu_has_interrupt(vcpu)
743 && !signal_pending(current)
744 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
745 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300746 set_current_state(TASK_INTERRUPTIBLE);
747 vcpu_put(vcpu);
748 schedule();
749 vcpu_load(vcpu);
750 }
751
He, Qingc5ec1532007-09-03 17:07:41 +0300752 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300753 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300754}
755
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756void kvm_resched(struct kvm_vcpu *vcpu)
757{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300758 if (!need_resched())
759 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761}
762EXPORT_SYMBOL_GPL(kvm_resched);
763
Avi Kivitybccf2152007-02-21 18:04:26 +0200764static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
765 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 if (irq->irq < 0 || irq->irq >= 256)
768 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300769 if (irqchip_in_kernel(vcpu->kvm))
770 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200771 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772
773 set_bit(irq->irq, vcpu->irq_pending);
774 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
775
776 vcpu_put(vcpu);
777
778 return 0;
779}
780
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200781static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
782 unsigned long address,
783 int *type)
784{
785 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
786 unsigned long pgoff;
787 struct page *page;
788
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200789 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +0200790 if (pgoff == 0)
791 page = virt_to_page(vcpu->run);
792 else if (pgoff == KVM_PIO_PAGE_OFFSET)
793 page = virt_to_page(vcpu->pio_data);
794 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200795 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200796 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300797 if (type != NULL)
798 *type = VM_FAULT_MINOR;
799
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200800 return page;
801}
802
803static struct vm_operations_struct kvm_vcpu_vm_ops = {
804 .nopage = kvm_vcpu_nopage,
805};
806
807static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
808{
809 vma->vm_ops = &kvm_vcpu_vm_ops;
810 return 0;
811}
812
Avi Kivitybccf2152007-02-21 18:04:26 +0200813static int kvm_vcpu_release(struct inode *inode, struct file *filp)
814{
815 struct kvm_vcpu *vcpu = filp->private_data;
816
817 fput(vcpu->kvm->filp);
818 return 0;
819}
820
821static struct file_operations kvm_vcpu_fops = {
822 .release = kvm_vcpu_release,
823 .unlocked_ioctl = kvm_vcpu_ioctl,
824 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200825 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200826};
827
828/*
829 * Allocates an inode for the vcpu.
830 */
831static int create_vcpu_fd(struct kvm_vcpu *vcpu)
832{
833 int fd, r;
834 struct inode *inode;
835 struct file *file;
836
Avi Kivityd6d28162007-06-28 08:38:16 -0400837 r = anon_inode_getfd(&fd, &inode, &file,
838 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
839 if (r)
840 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200841 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200842 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200843}
844
Avi Kivityc5ea7662007-02-20 18:41:05 +0200845/*
846 * Creates some virtual cpus. Good luck creating more than one.
847 */
848static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
849{
850 int r;
851 struct kvm_vcpu *vcpu;
852
Avi Kivityc5ea7662007-02-20 18:41:05 +0200853 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000854 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200855
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800856 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000857 if (IS_ERR(vcpu))
858 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200859
Avi Kivity15ad7142007-07-11 18:17:21 +0300860 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
861
Shaohua Li11ec2802007-07-23 14:51:37 +0800862 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000863 if (kvm->vcpus[n]) {
864 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800865 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800866 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000867 }
868 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800869 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000870
871 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200872 r = create_vcpu_fd(vcpu);
873 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000874 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200875 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200876
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000877unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800878 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000879 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800880 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800881vcpu_destroy:
882 kvm_arch_vcpu_destory(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200883 return r;
884}
885
Avi Kivity1961d272007-03-05 19:46:05 +0200886static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
887{
888 if (sigset) {
889 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
890 vcpu->sigset_active = 1;
891 vcpu->sigset = *sigset;
892 } else
893 vcpu->sigset_active = 0;
894 return 0;
895}
896
Avi Kivitybccf2152007-02-21 18:04:26 +0200897static long kvm_vcpu_ioctl(struct file *filp,
898 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899{
Avi Kivitybccf2152007-02-21 18:04:26 +0200900 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000901 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200902 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903
904 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200905 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200906 r = -EINVAL;
907 if (arg)
908 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500909 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 case KVM_GET_REGS: {
912 struct kvm_regs kvm_regs;
913
Avi Kivitybccf2152007-02-21 18:04:26 +0200914 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500915 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 if (r)
917 goto out;
918 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000919 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 goto out;
921 r = 0;
922 break;
923 }
924 case KVM_SET_REGS: {
925 struct kvm_regs kvm_regs;
926
927 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000928 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500930 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 if (r)
932 goto out;
933 r = 0;
934 break;
935 }
936 case KVM_GET_SREGS: {
937 struct kvm_sregs kvm_sregs;
938
Avi Kivitybccf2152007-02-21 18:04:26 +0200939 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500940 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 if (r)
942 goto out;
943 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000944 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 goto out;
946 r = 0;
947 break;
948 }
949 case KVM_SET_SREGS: {
950 struct kvm_sregs kvm_sregs;
951
952 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000953 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500955 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 if (r)
957 goto out;
958 r = 0;
959 break;
960 }
961 case KVM_TRANSLATE: {
962 struct kvm_translation tr;
963
964 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000965 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800967 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 if (r)
969 goto out;
970 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000971 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 goto out;
973 r = 0;
974 break;
975 }
976 case KVM_INTERRUPT: {
977 struct kvm_interrupt irq;
978
979 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000980 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +0200982 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 if (r)
984 goto out;
985 r = 0;
986 break;
987 }
988 case KVM_DEBUG_GUEST: {
989 struct kvm_debug_guest dbg;
990
991 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000992 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500994 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 if (r)
996 goto out;
997 r = 0;
998 break;
999 }
Avi Kivity1961d272007-03-05 19:46:05 +02001000 case KVM_SET_SIGNAL_MASK: {
1001 struct kvm_signal_mask __user *sigmask_arg = argp;
1002 struct kvm_signal_mask kvm_sigmask;
1003 sigset_t sigset, *p;
1004
1005 p = NULL;
1006 if (argp) {
1007 r = -EFAULT;
1008 if (copy_from_user(&kvm_sigmask, argp,
1009 sizeof kvm_sigmask))
1010 goto out;
1011 r = -EINVAL;
1012 if (kvm_sigmask.len != sizeof sigset)
1013 goto out;
1014 r = -EFAULT;
1015 if (copy_from_user(&sigset, sigmask_arg->sigset,
1016 sizeof sigset))
1017 goto out;
1018 p = &sigset;
1019 }
1020 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1021 break;
1022 }
Avi Kivityb8836732007-04-01 16:34:31 +03001023 case KVM_GET_FPU: {
1024 struct kvm_fpu fpu;
1025
1026 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05001027 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001028 if (r)
1029 goto out;
1030 r = -EFAULT;
1031 if (copy_to_user(argp, &fpu, sizeof fpu))
1032 goto out;
1033 r = 0;
1034 break;
1035 }
1036 case KVM_SET_FPU: {
1037 struct kvm_fpu fpu;
1038
1039 r = -EFAULT;
1040 if (copy_from_user(&fpu, argp, sizeof fpu))
1041 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -05001042 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001043 if (r)
1044 goto out;
1045 r = 0;
1046 break;
1047 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001048 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001049 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001050 }
1051out:
1052 return r;
1053}
1054
1055static long kvm_vm_ioctl(struct file *filp,
1056 unsigned int ioctl, unsigned long arg)
1057{
1058 struct kvm *kvm = filp->private_data;
1059 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001060 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001061
1062 switch (ioctl) {
1063 case KVM_CREATE_VCPU:
1064 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1065 if (r < 0)
1066 goto out;
1067 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001068 case KVM_SET_USER_MEMORY_REGION: {
1069 struct kvm_userspace_memory_region kvm_userspace_mem;
1070
1071 r = -EFAULT;
1072 if (copy_from_user(&kvm_userspace_mem, argp,
1073 sizeof kvm_userspace_mem))
1074 goto out;
1075
1076 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 if (r)
1078 goto out;
1079 break;
1080 }
1081 case KVM_GET_DIRTY_LOG: {
1082 struct kvm_dirty_log log;
1083
1084 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001085 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001087 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 if (r)
1089 goto out;
1090 break;
1091 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001092 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001093 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001094 }
1095out:
1096 return r;
1097}
1098
1099static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
1100 unsigned long address,
1101 int *type)
1102{
1103 struct kvm *kvm = vma->vm_file->private_data;
1104 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001105 struct page *page;
1106
Avi Kivityf17abe92007-02-21 19:28:04 +02001107 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001108 if (!kvm_is_visible_gfn(kvm, pgoff))
1109 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001110 /* current->mm->mmap_sem is already held so call lockless version */
1111 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001112 if (is_error_page(page)) {
1113 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02001114 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001115 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03001116 if (type != NULL)
1117 *type = VM_FAULT_MINOR;
1118
Avi Kivityf17abe92007-02-21 19:28:04 +02001119 return page;
1120}
1121
1122static struct vm_operations_struct kvm_vm_vm_ops = {
1123 .nopage = kvm_vm_nopage,
1124};
1125
1126static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1127{
1128 vma->vm_ops = &kvm_vm_vm_ops;
1129 return 0;
1130}
1131
1132static struct file_operations kvm_vm_fops = {
1133 .release = kvm_vm_release,
1134 .unlocked_ioctl = kvm_vm_ioctl,
1135 .compat_ioctl = kvm_vm_ioctl,
1136 .mmap = kvm_vm_mmap,
1137};
1138
1139static int kvm_dev_ioctl_create_vm(void)
1140{
1141 int fd, r;
1142 struct inode *inode;
1143 struct file *file;
1144 struct kvm *kvm;
1145
Avi Kivityf17abe92007-02-21 19:28:04 +02001146 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001147 if (IS_ERR(kvm))
1148 return PTR_ERR(kvm);
1149 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1150 if (r) {
1151 kvm_destroy_vm(kvm);
1152 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001153 }
1154
Avi Kivitybccf2152007-02-21 18:04:26 +02001155 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001156
Avi Kivityf17abe92007-02-21 19:28:04 +02001157 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001158}
1159
1160static long kvm_dev_ioctl(struct file *filp,
1161 unsigned int ioctl, unsigned long arg)
1162{
1163 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001164 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001165
1166 switch (ioctl) {
1167 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001168 r = -EINVAL;
1169 if (arg)
1170 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001171 r = KVM_API_VERSION;
1172 break;
1173 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001174 r = -EINVAL;
1175 if (arg)
1176 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001177 r = kvm_dev_ioctl_create_vm();
1178 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001179 case KVM_CHECK_EXTENSION:
1180 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001181 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001182 case KVM_GET_VCPU_MMAP_SIZE:
1183 r = -EINVAL;
1184 if (arg)
1185 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001186 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001187 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001189 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 }
1191out:
1192 return r;
1193}
1194
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 .unlocked_ioctl = kvm_dev_ioctl,
1197 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198};
1199
1200static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001201 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202 "kvm",
1203 &kvm_chardev_ops,
1204};
1205
Avi Kivity1b6c0162007-05-24 13:03:52 +03001206static void hardware_enable(void *junk)
1207{
1208 int cpu = raw_smp_processor_id();
1209
1210 if (cpu_isset(cpu, cpus_hardware_enabled))
1211 return;
1212 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001213 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001214}
1215
1216static void hardware_disable(void *junk)
1217{
1218 int cpu = raw_smp_processor_id();
1219
1220 if (!cpu_isset(cpu, cpus_hardware_enabled))
1221 return;
1222 cpu_clear(cpu, cpus_hardware_enabled);
1223 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001224 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001225}
1226
Avi Kivity774c47f2007-02-12 00:54:47 -08001227static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1228 void *v)
1229{
1230 int cpu = (long)v;
1231
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001232 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001233 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001234 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001235 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1236 cpu);
1237 hardware_disable(NULL);
1238 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001239 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001240 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1241 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001242 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001243 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001244 case CPU_ONLINE:
1245 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1246 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001247 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001248 break;
1249 }
1250 return NOTIFY_OK;
1251}
1252
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001253static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001254 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001255{
1256 if (val == SYS_RESTART) {
1257 /*
1258 * Some (well, at least mine) BIOSes hang on reboot if
1259 * in vmx root mode.
1260 */
1261 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1262 on_each_cpu(hardware_disable, NULL, 0, 1);
1263 }
1264 return NOTIFY_OK;
1265}
1266
1267static struct notifier_block kvm_reboot_notifier = {
1268 .notifier_call = kvm_reboot,
1269 .priority = 0,
1270};
1271
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001272void kvm_io_bus_init(struct kvm_io_bus *bus)
1273{
1274 memset(bus, 0, sizeof(*bus));
1275}
1276
1277void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1278{
1279 int i;
1280
1281 for (i = 0; i < bus->dev_count; i++) {
1282 struct kvm_io_device *pos = bus->devs[i];
1283
1284 kvm_iodevice_destructor(pos);
1285 }
1286}
1287
1288struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1289{
1290 int i;
1291
1292 for (i = 0; i < bus->dev_count; i++) {
1293 struct kvm_io_device *pos = bus->devs[i];
1294
1295 if (pos->in_range(pos, addr))
1296 return pos;
1297 }
1298
1299 return NULL;
1300}
1301
1302void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1303{
1304 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1305
1306 bus->devs[bus->dev_count++] = dev;
1307}
1308
Avi Kivity774c47f2007-02-12 00:54:47 -08001309static struct notifier_block kvm_cpu_notifier = {
1310 .notifier_call = kvm_cpu_hotplug,
1311 .priority = 20, /* must be > scheduler priority */
1312};
1313
Avi Kivity1165f5f2007-04-19 17:27:43 +03001314static u64 stat_get(void *_offset)
1315{
1316 unsigned offset = (long)_offset;
1317 u64 total = 0;
1318 struct kvm *kvm;
1319 struct kvm_vcpu *vcpu;
1320 int i;
1321
1322 spin_lock(&kvm_lock);
1323 list_for_each_entry(kvm, &vm_list, vm_list)
1324 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001325 vcpu = kvm->vcpus[i];
1326 if (vcpu)
1327 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001328 }
1329 spin_unlock(&kvm_lock);
1330 return total;
1331}
1332
Rusty Russell3dea7ca2007-08-01 10:12:22 +10001333DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03001334
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335static __init void kvm_init_debug(void)
1336{
1337 struct kvm_stats_debugfs_item *p;
1338
Al Viro8b6d44c2007-02-09 16:38:40 +00001339 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001341 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1342 (void *)(long)p->offset,
1343 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
1346static void kvm_exit_debug(void)
1347{
1348 struct kvm_stats_debugfs_item *p;
1349
1350 for (p = debugfs_entries; p->name; ++p)
1351 debugfs_remove(p->dentry);
1352 debugfs_remove(debugfs_dir);
1353}
1354
Avi Kivity59ae6c62007-02-12 00:54:48 -08001355static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1356{
Avi Kivity4267c412007-05-24 13:09:41 +03001357 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001358 return 0;
1359}
1360
1361static int kvm_resume(struct sys_device *dev)
1362{
Avi Kivity4267c412007-05-24 13:09:41 +03001363 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001364 return 0;
1365}
1366
1367static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001368 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001369 .suspend = kvm_suspend,
1370 .resume = kvm_resume,
1371};
1372
1373static struct sys_device kvm_sysdev = {
1374 .id = 0,
1375 .cls = &kvm_sysdev_class,
1376};
1377
Izik Eiduscea7bb22007-10-17 19:17:48 +02001378struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379
Avi Kivity15ad7142007-07-11 18:17:21 +03001380static inline
1381struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1382{
1383 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1384}
1385
1386static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1387{
1388 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1389
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001390 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001391}
1392
1393static void kvm_sched_out(struct preempt_notifier *pn,
1394 struct task_struct *next)
1395{
1396 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1397
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001398 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001399}
1400
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001401int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001402 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403{
1404 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001405 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001407 r = kvm_mmu_module_init();
1408 if (r)
1409 goto out4;
1410
1411 kvm_init_debug();
1412
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001413 r = kvm_arch_init(opaque);
1414 if (r)
1415 goto out4;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001416
1417 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1418
1419 if (bad_page == NULL) {
1420 r = -ENOMEM;
1421 goto out;
1422 }
1423
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001424 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02001426 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427
Yang, Sheng002c7f72007-07-31 14:23:01 +03001428 for_each_online_cpu(cpu) {
1429 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001430 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001431 &r, 0, 1);
1432 if (r < 0)
1433 goto out_free_0;
1434 }
1435
Avi Kivity1b6c0162007-05-24 13:03:52 +03001436 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001437 r = register_cpu_notifier(&kvm_cpu_notifier);
1438 if (r)
1439 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 register_reboot_notifier(&kvm_reboot_notifier);
1441
Avi Kivity59ae6c62007-02-12 00:54:48 -08001442 r = sysdev_class_register(&kvm_sysdev_class);
1443 if (r)
1444 goto out_free_2;
1445
1446 r = sysdev_register(&kvm_sysdev);
1447 if (r)
1448 goto out_free_3;
1449
Rusty Russellc16f8622007-07-30 21:12:19 +10001450 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1451 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
1452 __alignof__(struct kvm_vcpu), 0, 0);
1453 if (!kvm_vcpu_cache) {
1454 r = -ENOMEM;
1455 goto out_free_4;
1456 }
1457
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 kvm_chardev_ops.owner = module;
1459
1460 r = misc_register(&kvm_dev);
1461 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001462 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463 goto out_free;
1464 }
1465
Avi Kivity15ad7142007-07-11 18:17:21 +03001466 kvm_preempt_ops.sched_in = kvm_sched_in;
1467 kvm_preempt_ops.sched_out = kvm_sched_out;
1468
Avi Kivityc7addb92007-09-16 18:58:32 +02001469 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
1470
1471 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472
1473out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001474 kmem_cache_destroy(kvm_vcpu_cache);
1475out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001476 sysdev_unregister(&kvm_sysdev);
1477out_free_3:
1478 sysdev_class_unregister(&kvm_sysdev_class);
1479out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001481 unregister_cpu_notifier(&kvm_cpu_notifier);
1482out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001483 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001484out_free_0:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001485 kvm_arch_hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02001486out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001487 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001488 kvm_exit_debug();
1489 kvm_mmu_module_exit();
1490out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 return r;
1492}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001493EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001495void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496{
1497 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001498 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001499 sysdev_unregister(&kvm_sysdev);
1500 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001502 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001503 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001504 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001505 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001507 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001508 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001510EXPORT_SYMBOL_GPL(kvm_exit);