blob: c782f27265de00de1fa65e05301a39bd04c35998 [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 Kivity04d2cc72007-09-10 18:10:54 +0300764/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 * Translate a guest virtual address to a guest physical address.
766 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200767static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
768 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769{
770 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 gpa_t gpa;
772
Avi Kivitybccf2152007-02-21 18:04:26 +0200773 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800774 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
776 tr->physical_address = gpa;
777 tr->valid = gpa != UNMAPPED_GVA;
778 tr->writeable = 1;
779 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +0800780 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 vcpu_put(vcpu);
782
783 return 0;
784}
785
Avi Kivitybccf2152007-02-21 18:04:26 +0200786static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
787 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 if (irq->irq < 0 || irq->irq >= 256)
790 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300791 if (irqchip_in_kernel(vcpu->kvm))
792 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200793 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794
795 set_bit(irq->irq, vcpu->irq_pending);
796 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
797
798 vcpu_put(vcpu);
799
800 return 0;
801}
802
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200803static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
804 unsigned long address,
805 int *type)
806{
807 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
808 unsigned long pgoff;
809 struct page *page;
810
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200811 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +0200812 if (pgoff == 0)
813 page = virt_to_page(vcpu->run);
814 else if (pgoff == KVM_PIO_PAGE_OFFSET)
815 page = virt_to_page(vcpu->pio_data);
816 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200817 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200818 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +0300819 if (type != NULL)
820 *type = VM_FAULT_MINOR;
821
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200822 return page;
823}
824
825static struct vm_operations_struct kvm_vcpu_vm_ops = {
826 .nopage = kvm_vcpu_nopage,
827};
828
829static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
830{
831 vma->vm_ops = &kvm_vcpu_vm_ops;
832 return 0;
833}
834
Avi Kivitybccf2152007-02-21 18:04:26 +0200835static int kvm_vcpu_release(struct inode *inode, struct file *filp)
836{
837 struct kvm_vcpu *vcpu = filp->private_data;
838
839 fput(vcpu->kvm->filp);
840 return 0;
841}
842
843static struct file_operations kvm_vcpu_fops = {
844 .release = kvm_vcpu_release,
845 .unlocked_ioctl = kvm_vcpu_ioctl,
846 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200847 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200848};
849
850/*
851 * Allocates an inode for the vcpu.
852 */
853static int create_vcpu_fd(struct kvm_vcpu *vcpu)
854{
855 int fd, r;
856 struct inode *inode;
857 struct file *file;
858
Avi Kivityd6d28162007-06-28 08:38:16 -0400859 r = anon_inode_getfd(&fd, &inode, &file,
860 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
861 if (r)
862 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200863 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200864 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200865}
866
Avi Kivityc5ea7662007-02-20 18:41:05 +0200867/*
868 * Creates some virtual cpus. Good luck creating more than one.
869 */
870static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
871{
872 int r;
873 struct kvm_vcpu *vcpu;
874
Avi Kivityc5ea7662007-02-20 18:41:05 +0200875 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000876 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200877
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800878 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000879 if (IS_ERR(vcpu))
880 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200881
Avi Kivity15ad7142007-07-11 18:17:21 +0300882 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
883
Shaohua Li11ec2802007-07-23 14:51:37 +0800884 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000885 if (kvm->vcpus[n]) {
886 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800887 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800888 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000889 }
890 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800891 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000892
893 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200894 r = create_vcpu_fd(vcpu);
895 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000896 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200897 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200898
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000899unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800900 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000901 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800902 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800903vcpu_destroy:
904 kvm_arch_vcpu_destory(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200905 return r;
906}
907
Avi Kivity1961d272007-03-05 19:46:05 +0200908static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
909{
910 if (sigset) {
911 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
912 vcpu->sigset_active = 1;
913 vcpu->sigset = *sigset;
914 } else
915 vcpu->sigset_active = 0;
916 return 0;
917}
918
Avi Kivitybccf2152007-02-21 18:04:26 +0200919static long kvm_vcpu_ioctl(struct file *filp,
920 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921{
Avi Kivitybccf2152007-02-21 18:04:26 +0200922 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000923 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200924 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
926 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200927 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200928 r = -EINVAL;
929 if (arg)
930 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500931 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 case KVM_GET_REGS: {
934 struct kvm_regs kvm_regs;
935
Avi Kivitybccf2152007-02-21 18:04:26 +0200936 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500937 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 if (r)
939 goto out;
940 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000941 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 goto out;
943 r = 0;
944 break;
945 }
946 case KVM_SET_REGS: {
947 struct kvm_regs kvm_regs;
948
949 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000950 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500952 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 if (r)
954 goto out;
955 r = 0;
956 break;
957 }
958 case KVM_GET_SREGS: {
959 struct kvm_sregs kvm_sregs;
960
Avi Kivitybccf2152007-02-21 18:04:26 +0200961 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500962 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 if (r)
964 goto out;
965 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000966 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 goto out;
968 r = 0;
969 break;
970 }
971 case KVM_SET_SREGS: {
972 struct kvm_sregs kvm_sregs;
973
974 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000975 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500977 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 if (r)
979 goto out;
980 r = 0;
981 break;
982 }
983 case KVM_TRANSLATE: {
984 struct kvm_translation tr;
985
986 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000987 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +0200989 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 if (r)
991 goto out;
992 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000993 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 goto out;
995 r = 0;
996 break;
997 }
998 case KVM_INTERRUPT: {
999 struct kvm_interrupt irq;
1000
1001 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001002 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02001004 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 if (r)
1006 goto out;
1007 r = 0;
1008 break;
1009 }
1010 case KVM_DEBUG_GUEST: {
1011 struct kvm_debug_guest dbg;
1012
1013 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001014 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001016 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 if (r)
1018 goto out;
1019 r = 0;
1020 break;
1021 }
Avi Kivity1961d272007-03-05 19:46:05 +02001022 case KVM_SET_SIGNAL_MASK: {
1023 struct kvm_signal_mask __user *sigmask_arg = argp;
1024 struct kvm_signal_mask kvm_sigmask;
1025 sigset_t sigset, *p;
1026
1027 p = NULL;
1028 if (argp) {
1029 r = -EFAULT;
1030 if (copy_from_user(&kvm_sigmask, argp,
1031 sizeof kvm_sigmask))
1032 goto out;
1033 r = -EINVAL;
1034 if (kvm_sigmask.len != sizeof sigset)
1035 goto out;
1036 r = -EFAULT;
1037 if (copy_from_user(&sigset, sigmask_arg->sigset,
1038 sizeof sigset))
1039 goto out;
1040 p = &sigset;
1041 }
1042 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1043 break;
1044 }
Avi Kivityb8836732007-04-01 16:34:31 +03001045 case KVM_GET_FPU: {
1046 struct kvm_fpu fpu;
1047
1048 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05001049 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001050 if (r)
1051 goto out;
1052 r = -EFAULT;
1053 if (copy_to_user(argp, &fpu, sizeof fpu))
1054 goto out;
1055 r = 0;
1056 break;
1057 }
1058 case KVM_SET_FPU: {
1059 struct kvm_fpu fpu;
1060
1061 r = -EFAULT;
1062 if (copy_from_user(&fpu, argp, sizeof fpu))
1063 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -05001064 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001065 if (r)
1066 goto out;
1067 r = 0;
1068 break;
1069 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001070 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001071 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001072 }
1073out:
1074 return r;
1075}
1076
1077static long kvm_vm_ioctl(struct file *filp,
1078 unsigned int ioctl, unsigned long arg)
1079{
1080 struct kvm *kvm = filp->private_data;
1081 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001082 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001083
1084 switch (ioctl) {
1085 case KVM_CREATE_VCPU:
1086 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1087 if (r < 0)
1088 goto out;
1089 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001090 case KVM_SET_USER_MEMORY_REGION: {
1091 struct kvm_userspace_memory_region kvm_userspace_mem;
1092
1093 r = -EFAULT;
1094 if (copy_from_user(&kvm_userspace_mem, argp,
1095 sizeof kvm_userspace_mem))
1096 goto out;
1097
1098 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 if (r)
1100 goto out;
1101 break;
1102 }
1103 case KVM_GET_DIRTY_LOG: {
1104 struct kvm_dirty_log log;
1105
1106 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001107 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001109 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 if (r)
1111 goto out;
1112 break;
1113 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001114 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001115 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001116 }
1117out:
1118 return r;
1119}
1120
1121static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
1122 unsigned long address,
1123 int *type)
1124{
1125 struct kvm *kvm = vma->vm_file->private_data;
1126 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001127 struct page *page;
1128
Avi Kivityf17abe92007-02-21 19:28:04 +02001129 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02001130 if (!kvm_is_visible_gfn(kvm, pgoff))
1131 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001132 /* current->mm->mmap_sem is already held so call lockless version */
1133 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001134 if (is_error_page(page)) {
1135 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02001136 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001137 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03001138 if (type != NULL)
1139 *type = VM_FAULT_MINOR;
1140
Avi Kivityf17abe92007-02-21 19:28:04 +02001141 return page;
1142}
1143
1144static struct vm_operations_struct kvm_vm_vm_ops = {
1145 .nopage = kvm_vm_nopage,
1146};
1147
1148static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1149{
1150 vma->vm_ops = &kvm_vm_vm_ops;
1151 return 0;
1152}
1153
1154static struct file_operations kvm_vm_fops = {
1155 .release = kvm_vm_release,
1156 .unlocked_ioctl = kvm_vm_ioctl,
1157 .compat_ioctl = kvm_vm_ioctl,
1158 .mmap = kvm_vm_mmap,
1159};
1160
1161static int kvm_dev_ioctl_create_vm(void)
1162{
1163 int fd, r;
1164 struct inode *inode;
1165 struct file *file;
1166 struct kvm *kvm;
1167
Avi Kivityf17abe92007-02-21 19:28:04 +02001168 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001169 if (IS_ERR(kvm))
1170 return PTR_ERR(kvm);
1171 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1172 if (r) {
1173 kvm_destroy_vm(kvm);
1174 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001175 }
1176
Avi Kivitybccf2152007-02-21 18:04:26 +02001177 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001178
Avi Kivityf17abe92007-02-21 19:28:04 +02001179 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001180}
1181
1182static long kvm_dev_ioctl(struct file *filp,
1183 unsigned int ioctl, unsigned long arg)
1184{
1185 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001186 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001187
1188 switch (ioctl) {
1189 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001190 r = -EINVAL;
1191 if (arg)
1192 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001193 r = KVM_API_VERSION;
1194 break;
1195 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001196 r = -EINVAL;
1197 if (arg)
1198 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001199 r = kvm_dev_ioctl_create_vm();
1200 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001201 case KVM_CHECK_EXTENSION:
1202 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001203 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001204 case KVM_GET_VCPU_MMAP_SIZE:
1205 r = -EINVAL;
1206 if (arg)
1207 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02001208 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02001209 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001211 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 }
1213out:
1214 return r;
1215}
1216
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 .unlocked_ioctl = kvm_dev_ioctl,
1219 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220};
1221
1222static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001223 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 "kvm",
1225 &kvm_chardev_ops,
1226};
1227
Avi Kivity1b6c0162007-05-24 13:03:52 +03001228static void hardware_enable(void *junk)
1229{
1230 int cpu = raw_smp_processor_id();
1231
1232 if (cpu_isset(cpu, cpus_hardware_enabled))
1233 return;
1234 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001235 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001236}
1237
1238static void hardware_disable(void *junk)
1239{
1240 int cpu = raw_smp_processor_id();
1241
1242 if (!cpu_isset(cpu, cpus_hardware_enabled))
1243 return;
1244 cpu_clear(cpu, cpus_hardware_enabled);
1245 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001246 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001247}
1248
Avi Kivity774c47f2007-02-12 00:54:47 -08001249static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1250 void *v)
1251{
1252 int cpu = (long)v;
1253
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001254 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001255 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001256 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001257 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1258 cpu);
1259 hardware_disable(NULL);
1260 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001261 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001262 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1263 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001264 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001265 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001266 case CPU_ONLINE:
1267 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1268 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001269 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001270 break;
1271 }
1272 return NOTIFY_OK;
1273}
1274
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001275static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001276 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001277{
1278 if (val == SYS_RESTART) {
1279 /*
1280 * Some (well, at least mine) BIOSes hang on reboot if
1281 * in vmx root mode.
1282 */
1283 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1284 on_each_cpu(hardware_disable, NULL, 0, 1);
1285 }
1286 return NOTIFY_OK;
1287}
1288
1289static struct notifier_block kvm_reboot_notifier = {
1290 .notifier_call = kvm_reboot,
1291 .priority = 0,
1292};
1293
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001294void kvm_io_bus_init(struct kvm_io_bus *bus)
1295{
1296 memset(bus, 0, sizeof(*bus));
1297}
1298
1299void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1300{
1301 int i;
1302
1303 for (i = 0; i < bus->dev_count; i++) {
1304 struct kvm_io_device *pos = bus->devs[i];
1305
1306 kvm_iodevice_destructor(pos);
1307 }
1308}
1309
1310struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1311{
1312 int i;
1313
1314 for (i = 0; i < bus->dev_count; i++) {
1315 struct kvm_io_device *pos = bus->devs[i];
1316
1317 if (pos->in_range(pos, addr))
1318 return pos;
1319 }
1320
1321 return NULL;
1322}
1323
1324void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1325{
1326 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1327
1328 bus->devs[bus->dev_count++] = dev;
1329}
1330
Avi Kivity774c47f2007-02-12 00:54:47 -08001331static struct notifier_block kvm_cpu_notifier = {
1332 .notifier_call = kvm_cpu_hotplug,
1333 .priority = 20, /* must be > scheduler priority */
1334};
1335
Avi Kivity1165f5f2007-04-19 17:27:43 +03001336static u64 stat_get(void *_offset)
1337{
1338 unsigned offset = (long)_offset;
1339 u64 total = 0;
1340 struct kvm *kvm;
1341 struct kvm_vcpu *vcpu;
1342 int i;
1343
1344 spin_lock(&kvm_lock);
1345 list_for_each_entry(kvm, &vm_list, vm_list)
1346 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001347 vcpu = kvm->vcpus[i];
1348 if (vcpu)
1349 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001350 }
1351 spin_unlock(&kvm_lock);
1352 return total;
1353}
1354
Rusty Russell3dea7ca2007-08-01 10:12:22 +10001355DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03001356
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357static __init void kvm_init_debug(void)
1358{
1359 struct kvm_stats_debugfs_item *p;
1360
Al Viro8b6d44c2007-02-09 16:38:40 +00001361 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001363 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1364 (void *)(long)p->offset,
1365 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
1368static void kvm_exit_debug(void)
1369{
1370 struct kvm_stats_debugfs_item *p;
1371
1372 for (p = debugfs_entries; p->name; ++p)
1373 debugfs_remove(p->dentry);
1374 debugfs_remove(debugfs_dir);
1375}
1376
Avi Kivity59ae6c62007-02-12 00:54:48 -08001377static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1378{
Avi Kivity4267c412007-05-24 13:09:41 +03001379 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001380 return 0;
1381}
1382
1383static int kvm_resume(struct sys_device *dev)
1384{
Avi Kivity4267c412007-05-24 13:09:41 +03001385 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001386 return 0;
1387}
1388
1389static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001390 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001391 .suspend = kvm_suspend,
1392 .resume = kvm_resume,
1393};
1394
1395static struct sys_device kvm_sysdev = {
1396 .id = 0,
1397 .cls = &kvm_sysdev_class,
1398};
1399
Izik Eiduscea7bb22007-10-17 19:17:48 +02001400struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
Avi Kivity15ad7142007-07-11 18:17:21 +03001402static inline
1403struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1404{
1405 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1406}
1407
1408static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1409{
1410 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1411
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001412 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001413}
1414
1415static void kvm_sched_out(struct preempt_notifier *pn,
1416 struct task_struct *next)
1417{
1418 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1419
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001420 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001421}
1422
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001423int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001424 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425{
1426 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001427 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001429 r = kvm_mmu_module_init();
1430 if (r)
1431 goto out4;
1432
1433 kvm_init_debug();
1434
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001435 r = kvm_arch_init(opaque);
1436 if (r)
1437 goto out4;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001438
1439 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1440
1441 if (bad_page == NULL) {
1442 r = -ENOMEM;
1443 goto out;
1444 }
1445
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001446 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02001448 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449
Yang, Sheng002c7f72007-07-31 14:23:01 +03001450 for_each_online_cpu(cpu) {
1451 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001452 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001453 &r, 0, 1);
1454 if (r < 0)
1455 goto out_free_0;
1456 }
1457
Avi Kivity1b6c0162007-05-24 13:03:52 +03001458 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001459 r = register_cpu_notifier(&kvm_cpu_notifier);
1460 if (r)
1461 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 register_reboot_notifier(&kvm_reboot_notifier);
1463
Avi Kivity59ae6c62007-02-12 00:54:48 -08001464 r = sysdev_class_register(&kvm_sysdev_class);
1465 if (r)
1466 goto out_free_2;
1467
1468 r = sysdev_register(&kvm_sysdev);
1469 if (r)
1470 goto out_free_3;
1471
Rusty Russellc16f8622007-07-30 21:12:19 +10001472 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1473 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
1474 __alignof__(struct kvm_vcpu), 0, 0);
1475 if (!kvm_vcpu_cache) {
1476 r = -ENOMEM;
1477 goto out_free_4;
1478 }
1479
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 kvm_chardev_ops.owner = module;
1481
1482 r = misc_register(&kvm_dev);
1483 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001484 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 goto out_free;
1486 }
1487
Avi Kivity15ad7142007-07-11 18:17:21 +03001488 kvm_preempt_ops.sched_in = kvm_sched_in;
1489 kvm_preempt_ops.sched_out = kvm_sched_out;
1490
Avi Kivityc7addb92007-09-16 18:58:32 +02001491 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
1492
1493 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
1495out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001496 kmem_cache_destroy(kvm_vcpu_cache);
1497out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001498 sysdev_unregister(&kvm_sysdev);
1499out_free_3:
1500 sysdev_class_unregister(&kvm_sysdev_class);
1501out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001503 unregister_cpu_notifier(&kvm_cpu_notifier);
1504out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001505 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001506out_free_0:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001507 kvm_arch_hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02001508out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001509 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001510 kvm_exit_debug();
1511 kvm_mmu_module_exit();
1512out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 return r;
1514}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001515EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001517void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518{
1519 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001520 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001521 sysdev_unregister(&kvm_sysdev);
1522 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001524 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001525 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001526 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001527 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001529 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001530 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001532EXPORT_SYMBOL_GPL(kvm_exit);