blob: 31db9b4d3016e91843aa94abf631edf3ebe864dc [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
Avi Kivitye4956062007-06-28 14:15:57 -040044#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/io.h>
46#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020047#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
49MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080052DEFINE_SPINLOCK(kvm_lock);
53LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080054
Avi Kivity1b6c0162007-05-24 13:03:52 +030055static cpumask_t cpus_hardware_enabled;
56
Rusty Russellc16f8622007-07-30 21:12:19 +100057struct kmem_cache *kvm_vcpu_cache;
58EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030059
Avi Kivity15ad7142007-07-11 18:17:21 +030060static __read_mostly struct preempt_ops kvm_preempt_ops;
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062static struct dentry *debugfs_dir;
63
Avi Kivitybccf2152007-02-21 18:04:26 +020064static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
65 unsigned long arg);
66
James Morris5aacf0c2006-12-22 01:04:55 -080067static inline int valid_vcpu(int n)
68{
69 return likely(n >= 0 && n < KVM_MAX_VCPUS);
70}
71
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/*
73 * Switches to specified vcpu, until a matching vcpu_put()
74 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020075void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076{
Avi Kivity15ad7142007-07-11 18:17:21 +030077 int cpu;
78
Avi Kivitybccf2152007-02-21 18:04:26 +020079 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030080 cpu = get_cpu();
81 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020082 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030083 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020084}
85
Carsten Otte313a3dc2007-10-11 19:16:52 +020086void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087{
Avi Kivity15ad7142007-07-11 18:17:21 +030088 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020089 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030090 preempt_notifier_unregister(&vcpu->preempt_notifier);
91 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080092 mutex_unlock(&vcpu->mutex);
93}
94
Avi Kivityd9e368d2007-06-07 19:18:30 +030095static void ack_flush(void *_completed)
96{
Avi Kivityd9e368d2007-06-07 19:18:30 +030097}
98
99void kvm_flush_remote_tlbs(struct kvm *kvm)
100{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200101 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300102 cpumask_t cpus;
103 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300104
Avi Kivityd9e368d2007-06-07 19:18:30 +0300105 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000106 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
107 vcpu = kvm->vcpus[i];
108 if (!vcpu)
109 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200110 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300111 continue;
112 cpu = vcpu->cpu;
113 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200114 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300115 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200116 if (cpus_empty(cpus))
117 return;
118 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200119 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300120}
121
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500122void kvm_reload_remote_mmus(struct kvm *kvm)
123{
124 int i, cpu;
125 cpumask_t cpus;
126 struct kvm_vcpu *vcpu;
127
128 cpus_clear(cpus);
129 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
130 vcpu = kvm->vcpus[i];
131 if (!vcpu)
132 continue;
133 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
134 continue;
135 cpu = vcpu->cpu;
136 if (cpu != -1 && cpu != raw_smp_processor_id())
137 cpu_set(cpu, cpus);
138 }
139 if (cpus_empty(cpus))
140 return;
141 smp_call_function_mask(cpus, ack_flush, NULL, 1);
142}
143
144
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000145int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
146{
147 struct page *page;
148 int r;
149
150 mutex_init(&vcpu->mutex);
151 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000152 vcpu->kvm = kvm;
153 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300154 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000155
156 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
157 if (!page) {
158 r = -ENOMEM;
159 goto fail;
160 }
161 vcpu->run = page_address(page);
162
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800163 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000164 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800165 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000166 return 0;
167
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000168fail_free_run:
169 free_page((unsigned long)vcpu->run);
170fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000171 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000172}
173EXPORT_SYMBOL_GPL(kvm_vcpu_init);
174
175void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
176{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800177 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000178 free_page((unsigned long)vcpu->run);
179}
180EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
181
Avi Kivityf17abe92007-02-21 19:28:04 +0200182static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800184 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800186 if (IS_ERR(kvm))
187 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200189 kvm->mm = current->mm;
190 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500191 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300192 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800193 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400194 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200195 init_rwsem(&kvm->slots_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000196 spin_lock(&kvm_lock);
197 list_add(&kvm->vm_list, &vm_list);
198 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800199out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200200 return kvm;
201}
202
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203/*
204 * Free any memory in @free but not in @dont.
205 */
206static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
207 struct kvm_memory_slot *dont)
208{
Izik Eidus290fc382007-09-27 14:11:22 +0200209 if (!dont || free->rmap != dont->rmap)
210 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211
212 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
213 vfree(free->dirty_bitmap);
214
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300215 if (!dont || free->lpage_info != dont->lpage_info)
216 vfree(free->lpage_info);
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000219 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500220 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300221 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222}
223
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800224void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225{
226 int i;
227
228 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000229 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230}
231
Avi Kivityf17abe92007-02-21 19:28:04 +0200232static void kvm_destroy_vm(struct kvm *kvm)
233{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200234 struct mm_struct *mm = kvm->mm;
235
Avi Kivity133de902007-02-12 00:54:44 -0800236 spin_lock(&kvm_lock);
237 list_del(&kvm->vm_list);
238 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300239 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400240 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800241 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200242 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200243}
244
245static int kvm_vm_release(struct inode *inode, struct file *filp)
246{
247 struct kvm *kvm = filp->private_data;
248
249 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 return 0;
251}
252
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254 * Allocate some memory and give it an address in the guest physical address
255 * space.
256 *
257 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800258 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500259 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800261int __kvm_set_memory_region(struct kvm *kvm,
262 struct kvm_userspace_memory_region *mem,
263 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264{
265 int r;
266 gfn_t base_gfn;
267 unsigned long npages;
268 unsigned long i;
269 struct kvm_memory_slot *memslot;
270 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271
272 r = -EINVAL;
273 /* General sanity checks */
274 if (mem->memory_size & (PAGE_SIZE - 1))
275 goto out;
276 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
277 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200278 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 goto out;
280 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
281 goto out;
282
283 memslot = &kvm->memslots[mem->slot];
284 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
285 npages = mem->memory_size >> PAGE_SHIFT;
286
287 if (!npages)
288 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
289
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290 new = old = *memslot;
291
292 new.base_gfn = base_gfn;
293 new.npages = npages;
294 new.flags = mem->flags;
295
296 /* Disallow changing a memory slot's size. */
297 r = -EINVAL;
298 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800299 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300
301 /* Check for overlaps */
302 r = -EEXIST;
303 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
304 struct kvm_memory_slot *s = &kvm->memslots[i];
305
306 if (s == memslot)
307 continue;
308 if (!((base_gfn + npages <= s->base_gfn) ||
309 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800310 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 /* Free page dirty bitmap if unneeded */
314 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000315 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316
317 r = -ENOMEM;
318
319 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500320 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400321 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200322
323 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800324 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200325
Izik Eidus290fc382007-09-27 14:11:22 +0200326 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500327
Izik Eidus80b14b52007-10-25 11:54:04 +0200328 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800329 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300331 if (npages && !new.lpage_info) {
332 int largepages = npages / KVM_PAGES_PER_HPAGE;
333 if (npages % KVM_PAGES_PER_HPAGE)
334 largepages++;
335 if (base_gfn % KVM_PAGES_PER_HPAGE)
336 largepages++;
337
338 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
339
340 if (!new.lpage_info)
341 goto out_free;
342
343 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
344
345 if (base_gfn % KVM_PAGES_PER_HPAGE)
346 new.lpage_info[0].write_count = 1;
347 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
348 new.lpage_info[largepages-1].write_count = 1;
349 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350
351 /* Allocate page dirty bitmap if needed */
352 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
353 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
354
355 new.dirty_bitmap = vmalloc(dirty_bytes);
356 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800357 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 memset(new.dirty_bitmap, 0, dirty_bytes);
359 }
360
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 if (mem->slot >= kvm->nmemslots)
362 kvm->nmemslots = mem->slot + 1;
363
364 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365
Zhang Xiantao0de10342007-11-20 16:25:04 +0800366 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
367 if (r) {
368 *memslot = old;
369 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800370 }
371
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372 kvm_free_physmem_slot(&old, &new);
373 return 0;
374
Sheng Yangf78e0e22007-10-29 09:40:42 +0800375out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376 kvm_free_physmem_slot(&new, &old);
377out:
378 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200379
380}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800381EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
382
383int kvm_set_memory_region(struct kvm *kvm,
384 struct kvm_userspace_memory_region *mem,
385 int user_alloc)
386{
387 int r;
388
Izik Eidus72dc67a2008-02-10 18:04:15 +0200389 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800390 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200391 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800392 return r;
393}
Izik Eidus210c7c42007-10-24 23:52:57 +0200394EXPORT_SYMBOL_GPL(kvm_set_memory_region);
395
Carsten Otte1fe779f2007-10-29 16:08:35 +0100396int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
397 struct
398 kvm_userspace_memory_region *mem,
399 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200400{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200401 if (mem->slot >= KVM_MEMORY_SLOTS)
402 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200403 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404}
405
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800406int kvm_get_dirty_log(struct kvm *kvm,
407 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408{
409 struct kvm_memory_slot *memslot;
410 int r, i;
411 int n;
412 unsigned long any = 0;
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 r = -EINVAL;
415 if (log->slot >= KVM_MEMORY_SLOTS)
416 goto out;
417
418 memslot = &kvm->memslots[log->slot];
419 r = -ENOENT;
420 if (!memslot->dirty_bitmap)
421 goto out;
422
Uri Lublincd1a4a92007-02-22 16:43:09 +0200423 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
Uri Lublincd1a4a92007-02-22 16:43:09 +0200425 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 any = memslot->dirty_bitmap[i];
427
428 r = -EFAULT;
429 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
430 goto out;
431
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800432 if (any)
433 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434
435 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 return r;
438}
439
Izik Eiduscea7bb22007-10-17 19:17:48 +0200440int is_error_page(struct page *page)
441{
442 return page == bad_page;
443}
444EXPORT_SYMBOL_GPL(is_error_page);
445
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200446static inline unsigned long bad_hva(void)
447{
448 return PAGE_OFFSET;
449}
450
451int kvm_is_error_hva(unsigned long addr)
452{
453 return addr == bad_hva();
454}
455EXPORT_SYMBOL_GPL(kvm_is_error_hva);
456
Avi Kivitye8207542007-03-30 16:54:30 +0300457static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458{
459 int i;
460
461 for (i = 0; i < kvm->nmemslots; ++i) {
462 struct kvm_memory_slot *memslot = &kvm->memslots[i];
463
464 if (gfn >= memslot->base_gfn
465 && gfn < memslot->base_gfn + memslot->npages)
466 return memslot;
467 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000468 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469}
Avi Kivitye8207542007-03-30 16:54:30 +0300470
471struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
472{
473 gfn = unalias_gfn(kvm, gfn);
474 return __gfn_to_memslot(kvm, gfn);
475}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476
Izik Eiduse0d62c72007-10-24 23:57:46 +0200477int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
478{
479 int i;
480
481 gfn = unalias_gfn(kvm, gfn);
482 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
483 struct kvm_memory_slot *memslot = &kvm->memslots[i];
484
485 if (gfn >= memslot->base_gfn
486 && gfn < memslot->base_gfn + memslot->npages)
487 return 1;
488 }
489 return 0;
490}
491EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
492
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200494{
495 struct kvm_memory_slot *slot;
496
497 gfn = unalias_gfn(kvm, gfn);
498 slot = __gfn_to_memslot(kvm, gfn);
499 if (!slot)
500 return bad_hva();
501 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
502}
503
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500504/*
505 * Requires current->mm->mmap_sem to be held
506 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500507struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300508{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500509 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200510 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500511 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300512
Avi Kivity60395222007-10-21 11:03:36 +0200513 might_sleep();
514
Izik Eidus539cb662007-11-11 22:05:04 +0200515 addr = gfn_to_hva(kvm, gfn);
516 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200517 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200518 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200519 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200520
Izik Eidus539cb662007-11-11 22:05:04 +0200521 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
522 NULL);
523
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500524 if (npages != 1) {
525 get_page(bad_page);
526 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200527 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500528
529 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300530}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500531
Avi Kivity954bbbc2007-03-30 14:02:32 +0300532EXPORT_SYMBOL_GPL(gfn_to_page);
533
Izik Eidusb4231d62007-11-20 11:49:33 +0200534void kvm_release_page_clean(struct page *page)
535{
536 put_page(page);
537}
538EXPORT_SYMBOL_GPL(kvm_release_page_clean);
539
540void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200541{
542 if (!PageReserved(page))
543 SetPageDirty(page);
544 put_page(page);
545}
Izik Eidusb4231d62007-11-20 11:49:33 +0200546EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200547
Izik Eidus195aefd2007-10-01 22:14:18 +0200548static int next_segment(unsigned long len, int offset)
549{
550 if (len > PAGE_SIZE - offset)
551 return PAGE_SIZE - offset;
552 else
553 return len;
554}
555
556int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
557 int len)
558{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200559 int r;
560 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200561
Izik Eiduse0506bc2007-11-11 22:10:22 +0200562 addr = gfn_to_hva(kvm, gfn);
563 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200564 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200565 r = copy_from_user(data, (void __user *)addr + offset, len);
566 if (r)
567 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200568 return 0;
569}
570EXPORT_SYMBOL_GPL(kvm_read_guest_page);
571
572int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
573{
574 gfn_t gfn = gpa >> PAGE_SHIFT;
575 int seg;
576 int offset = offset_in_page(gpa);
577 int ret;
578
579 while ((seg = next_segment(len, offset)) != 0) {
580 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
581 if (ret < 0)
582 return ret;
583 offset = 0;
584 len -= seg;
585 data += seg;
586 ++gfn;
587 }
588 return 0;
589}
590EXPORT_SYMBOL_GPL(kvm_read_guest);
591
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500592int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
593 unsigned long len)
594{
595 int r;
596 unsigned long addr;
597 gfn_t gfn = gpa >> PAGE_SHIFT;
598 int offset = offset_in_page(gpa);
599
600 addr = gfn_to_hva(kvm, gfn);
601 if (kvm_is_error_hva(addr))
602 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100603 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500604 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100605 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500606 if (r)
607 return -EFAULT;
608 return 0;
609}
610EXPORT_SYMBOL(kvm_read_guest_atomic);
611
Izik Eidus195aefd2007-10-01 22:14:18 +0200612int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
613 int offset, int len)
614{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200615 int r;
616 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200617
Izik Eiduse0506bc2007-11-11 22:10:22 +0200618 addr = gfn_to_hva(kvm, gfn);
619 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200620 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200621 r = copy_to_user((void __user *)addr + offset, data, len);
622 if (r)
623 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200624 mark_page_dirty(kvm, gfn);
625 return 0;
626}
627EXPORT_SYMBOL_GPL(kvm_write_guest_page);
628
629int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
630 unsigned long len)
631{
632 gfn_t gfn = gpa >> PAGE_SHIFT;
633 int seg;
634 int offset = offset_in_page(gpa);
635 int ret;
636
637 while ((seg = next_segment(len, offset)) != 0) {
638 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
639 if (ret < 0)
640 return ret;
641 offset = 0;
642 len -= seg;
643 data += seg;
644 ++gfn;
645 }
646 return 0;
647}
648
649int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
650{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200651 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200652}
653EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
654
655int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
656{
657 gfn_t gfn = gpa >> PAGE_SHIFT;
658 int seg;
659 int offset = offset_in_page(gpa);
660 int ret;
661
662 while ((seg = next_segment(len, offset)) != 0) {
663 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
664 if (ret < 0)
665 return ret;
666 offset = 0;
667 len -= seg;
668 ++gfn;
669 }
670 return 0;
671}
672EXPORT_SYMBOL_GPL(kvm_clear_guest);
673
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
675{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300676 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677
Uri Lublin3b6fff12007-10-30 10:42:09 +0200678 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000679 memslot = __gfn_to_memslot(kvm, gfn);
680 if (memslot && memslot->dirty_bitmap) {
681 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Rusty Russell7e9d6192007-07-31 20:41:14 +1000683 /* avoid RMW */
684 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
685 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 }
687}
688
Eddie Dongb6958ce2007-07-18 12:15:21 +0300689/*
690 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
691 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500692void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300693{
694 DECLARE_WAITQUEUE(wait, current);
695
696 add_wait_queue(&vcpu->wq, &wait);
697
698 /*
699 * We will block until either an interrupt or a signal wakes us up
700 */
He, Qingc5ec1532007-09-03 17:07:41 +0300701 while (!kvm_cpu_has_interrupt(vcpu)
702 && !signal_pending(current)
Hollis Blanchard53e0aa72007-12-03 16:15:26 -0600703 && !kvm_arch_vcpu_runnable(vcpu)) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300704 set_current_state(TASK_INTERRUPTIBLE);
705 vcpu_put(vcpu);
706 schedule();
707 vcpu_load(vcpu);
708 }
709
He, Qingc5ec1532007-09-03 17:07:41 +0300710 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300711 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300712}
713
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714void kvm_resched(struct kvm_vcpu *vcpu)
715{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300716 if (!need_resched())
717 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719}
720EXPORT_SYMBOL_GPL(kvm_resched);
721
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100722static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200723{
724 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200725 struct page *page;
726
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100727 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +0200728 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +0200729#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100730 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800731 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +0200732#endif
Avi Kivity039576c2007-03-20 12:46:50 +0200733 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100734 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200735 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100736 vmf->page = page;
737 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200738}
739
740static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100741 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200742};
743
744static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
745{
746 vma->vm_ops = &kvm_vcpu_vm_ops;
747 return 0;
748}
749
Avi Kivitybccf2152007-02-21 18:04:26 +0200750static int kvm_vcpu_release(struct inode *inode, struct file *filp)
751{
752 struct kvm_vcpu *vcpu = filp->private_data;
753
754 fput(vcpu->kvm->filp);
755 return 0;
756}
757
Jan Engelhardt5c502742008-01-22 20:46:14 +0100758static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +0200759 .release = kvm_vcpu_release,
760 .unlocked_ioctl = kvm_vcpu_ioctl,
761 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200762 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200763};
764
765/*
766 * Allocates an inode for the vcpu.
767 */
768static int create_vcpu_fd(struct kvm_vcpu *vcpu)
769{
770 int fd, r;
771 struct inode *inode;
772 struct file *file;
773
Avi Kivityd6d28162007-06-28 08:38:16 -0400774 r = anon_inode_getfd(&fd, &inode, &file,
775 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
776 if (r)
777 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200778 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200779 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200780}
781
Avi Kivityc5ea7662007-02-20 18:41:05 +0200782/*
783 * Creates some virtual cpus. Good luck creating more than one.
784 */
785static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
786{
787 int r;
788 struct kvm_vcpu *vcpu;
789
Avi Kivityc5ea7662007-02-20 18:41:05 +0200790 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000791 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200792
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800793 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000794 if (IS_ERR(vcpu))
795 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200796
Avi Kivity15ad7142007-07-11 18:17:21 +0300797 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
798
Avi Kivity26e52152007-11-20 15:30:24 +0200799 r = kvm_arch_vcpu_setup(vcpu);
800 if (r)
801 goto vcpu_destroy;
802
Shaohua Li11ec2802007-07-23 14:51:37 +0800803 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000804 if (kvm->vcpus[n]) {
805 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800806 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800807 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000808 }
809 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800810 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000811
812 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200813 r = create_vcpu_fd(vcpu);
814 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000815 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200816 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200817
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000818unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800819 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000820 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800821 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800822vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600823 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200824 return r;
825}
826
Avi Kivity1961d272007-03-05 19:46:05 +0200827static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
828{
829 if (sigset) {
830 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
831 vcpu->sigset_active = 1;
832 vcpu->sigset = *sigset;
833 } else
834 vcpu->sigset_active = 0;
835 return 0;
836}
837
Avi Kivitybccf2152007-02-21 18:04:26 +0200838static long kvm_vcpu_ioctl(struct file *filp,
839 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840{
Avi Kivitybccf2152007-02-21 18:04:26 +0200841 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000842 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200843 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200845 if (vcpu->kvm->mm != current->mm)
846 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200848 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200849 r = -EINVAL;
850 if (arg)
851 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500852 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 case KVM_GET_REGS: {
855 struct kvm_regs kvm_regs;
856
Avi Kivitybccf2152007-02-21 18:04:26 +0200857 memset(&kvm_regs, 0, sizeof kvm_regs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500858 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 if (r)
860 goto out;
861 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000862 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 goto out;
864 r = 0;
865 break;
866 }
867 case KVM_SET_REGS: {
868 struct kvm_regs kvm_regs;
869
870 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000871 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500873 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 if (r)
875 goto out;
876 r = 0;
877 break;
878 }
879 case KVM_GET_SREGS: {
880 struct kvm_sregs kvm_sregs;
881
Avi Kivitybccf2152007-02-21 18:04:26 +0200882 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500883 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 if (r)
885 goto out;
886 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000887 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 goto out;
889 r = 0;
890 break;
891 }
892 case KVM_SET_SREGS: {
893 struct kvm_sregs kvm_sregs;
894
895 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000896 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500898 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 if (r)
900 goto out;
901 r = 0;
902 break;
903 }
904 case KVM_TRANSLATE: {
905 struct kvm_translation tr;
906
907 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000908 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800910 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 if (r)
912 goto out;
913 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000914 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 goto out;
916 r = 0;
917 break;
918 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 case KVM_DEBUG_GUEST: {
920 struct kvm_debug_guest dbg;
921
922 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000923 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500925 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 if (r)
927 goto out;
928 r = 0;
929 break;
930 }
Avi Kivity1961d272007-03-05 19:46:05 +0200931 case KVM_SET_SIGNAL_MASK: {
932 struct kvm_signal_mask __user *sigmask_arg = argp;
933 struct kvm_signal_mask kvm_sigmask;
934 sigset_t sigset, *p;
935
936 p = NULL;
937 if (argp) {
938 r = -EFAULT;
939 if (copy_from_user(&kvm_sigmask, argp,
940 sizeof kvm_sigmask))
941 goto out;
942 r = -EINVAL;
943 if (kvm_sigmask.len != sizeof sigset)
944 goto out;
945 r = -EFAULT;
946 if (copy_from_user(&sigset, sigmask_arg->sigset,
947 sizeof sigset))
948 goto out;
949 p = &sigset;
950 }
951 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
952 break;
953 }
Avi Kivityb8836732007-04-01 16:34:31 +0300954 case KVM_GET_FPU: {
955 struct kvm_fpu fpu;
956
957 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -0500958 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300959 if (r)
960 goto out;
961 r = -EFAULT;
962 if (copy_to_user(argp, &fpu, sizeof fpu))
963 goto out;
964 r = 0;
965 break;
966 }
967 case KVM_SET_FPU: {
968 struct kvm_fpu fpu;
969
970 r = -EFAULT;
971 if (copy_from_user(&fpu, argp, sizeof fpu))
972 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -0500973 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +0300974 if (r)
975 goto out;
976 r = 0;
977 break;
978 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200979 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +0200980 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +0200981 }
982out:
983 return r;
984}
985
986static long kvm_vm_ioctl(struct file *filp,
987 unsigned int ioctl, unsigned long arg)
988{
989 struct kvm *kvm = filp->private_data;
990 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +0100991 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200992
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200993 if (kvm->mm != current->mm)
994 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +0200995 switch (ioctl) {
996 case KVM_CREATE_VCPU:
997 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
998 if (r < 0)
999 goto out;
1000 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001001 case KVM_SET_USER_MEMORY_REGION: {
1002 struct kvm_userspace_memory_region kvm_userspace_mem;
1003
1004 r = -EFAULT;
1005 if (copy_from_user(&kvm_userspace_mem, argp,
1006 sizeof kvm_userspace_mem))
1007 goto out;
1008
1009 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 if (r)
1011 goto out;
1012 break;
1013 }
1014 case KVM_GET_DIRTY_LOG: {
1015 struct kvm_dirty_log log;
1016
1017 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001018 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001020 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 if (r)
1022 goto out;
1023 break;
1024 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001025 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001026 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001027 }
1028out:
1029 return r;
1030}
1031
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001032static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001033{
1034 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001035 struct page *page;
1036
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001037 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1038 return VM_FAULT_SIGBUS;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001039 page = gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001040 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001041 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001042 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001043 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001044 vmf->page = page;
1045 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001046}
1047
1048static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001049 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001050};
1051
1052static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1053{
1054 vma->vm_ops = &kvm_vm_vm_ops;
1055 return 0;
1056}
1057
Jan Engelhardt5c502742008-01-22 20:46:14 +01001058static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001059 .release = kvm_vm_release,
1060 .unlocked_ioctl = kvm_vm_ioctl,
1061 .compat_ioctl = kvm_vm_ioctl,
1062 .mmap = kvm_vm_mmap,
1063};
1064
1065static int kvm_dev_ioctl_create_vm(void)
1066{
1067 int fd, r;
1068 struct inode *inode;
1069 struct file *file;
1070 struct kvm *kvm;
1071
Avi Kivityf17abe92007-02-21 19:28:04 +02001072 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001073 if (IS_ERR(kvm))
1074 return PTR_ERR(kvm);
1075 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1076 if (r) {
1077 kvm_destroy_vm(kvm);
1078 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001079 }
1080
Avi Kivitybccf2152007-02-21 18:04:26 +02001081 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001082
Avi Kivityf17abe92007-02-21 19:28:04 +02001083 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001084}
1085
1086static long kvm_dev_ioctl(struct file *filp,
1087 unsigned int ioctl, unsigned long arg)
1088{
1089 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001090 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001091
1092 switch (ioctl) {
1093 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001094 r = -EINVAL;
1095 if (arg)
1096 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001097 r = KVM_API_VERSION;
1098 break;
1099 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001100 r = -EINVAL;
1101 if (arg)
1102 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001103 r = kvm_dev_ioctl_create_vm();
1104 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001105 case KVM_CHECK_EXTENSION:
1106 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001107 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001108 case KVM_GET_VCPU_MMAP_SIZE:
1109 r = -EINVAL;
1110 if (arg)
1111 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001112 r = PAGE_SIZE; /* struct kvm_run */
1113#ifdef CONFIG_X86
1114 r += PAGE_SIZE; /* pio data page */
1115#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001116 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001118 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 }
1120out:
1121 return r;
1122}
1123
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 .unlocked_ioctl = kvm_dev_ioctl,
1126 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127};
1128
1129static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001130 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 "kvm",
1132 &kvm_chardev_ops,
1133};
1134
Avi Kivity1b6c0162007-05-24 13:03:52 +03001135static void hardware_enable(void *junk)
1136{
1137 int cpu = raw_smp_processor_id();
1138
1139 if (cpu_isset(cpu, cpus_hardware_enabled))
1140 return;
1141 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001142 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001143}
1144
1145static void hardware_disable(void *junk)
1146{
1147 int cpu = raw_smp_processor_id();
1148
1149 if (!cpu_isset(cpu, cpus_hardware_enabled))
1150 return;
1151 cpu_clear(cpu, cpus_hardware_enabled);
1152 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001153 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001154}
1155
Avi Kivity774c47f2007-02-12 00:54:47 -08001156static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1157 void *v)
1158{
1159 int cpu = (long)v;
1160
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001161 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001162 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001163 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001164 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1165 cpu);
1166 hardware_disable(NULL);
1167 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001168 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001169 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1170 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001171 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001172 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001173 case CPU_ONLINE:
1174 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1175 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001176 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001177 break;
1178 }
1179 return NOTIFY_OK;
1180}
1181
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001182static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001183 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001184{
1185 if (val == SYS_RESTART) {
1186 /*
1187 * Some (well, at least mine) BIOSes hang on reboot if
1188 * in vmx root mode.
1189 */
1190 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1191 on_each_cpu(hardware_disable, NULL, 0, 1);
1192 }
1193 return NOTIFY_OK;
1194}
1195
1196static struct notifier_block kvm_reboot_notifier = {
1197 .notifier_call = kvm_reboot,
1198 .priority = 0,
1199};
1200
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001201void kvm_io_bus_init(struct kvm_io_bus *bus)
1202{
1203 memset(bus, 0, sizeof(*bus));
1204}
1205
1206void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1207{
1208 int i;
1209
1210 for (i = 0; i < bus->dev_count; i++) {
1211 struct kvm_io_device *pos = bus->devs[i];
1212
1213 kvm_iodevice_destructor(pos);
1214 }
1215}
1216
1217struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1218{
1219 int i;
1220
1221 for (i = 0; i < bus->dev_count; i++) {
1222 struct kvm_io_device *pos = bus->devs[i];
1223
1224 if (pos->in_range(pos, addr))
1225 return pos;
1226 }
1227
1228 return NULL;
1229}
1230
1231void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1232{
1233 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1234
1235 bus->devs[bus->dev_count++] = dev;
1236}
1237
Avi Kivity774c47f2007-02-12 00:54:47 -08001238static struct notifier_block kvm_cpu_notifier = {
1239 .notifier_call = kvm_cpu_hotplug,
1240 .priority = 20, /* must be > scheduler priority */
1241};
1242
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001243static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001244{
1245 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001246 struct kvm *kvm;
1247
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001248 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001249 spin_lock(&kvm_lock);
1250 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001251 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001252 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001253 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001254}
1255
1256DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1257
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001258static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001259{
1260 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001261 struct kvm *kvm;
1262 struct kvm_vcpu *vcpu;
1263 int i;
1264
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001265 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001266 spin_lock(&kvm_lock);
1267 list_for_each_entry(kvm, &vm_list, vm_list)
1268 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001269 vcpu = kvm->vcpus[i];
1270 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001271 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001272 }
1273 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001274 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001275}
1276
Avi Kivityba1389b2007-11-18 16:24:12 +02001277DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1278
1279static struct file_operations *stat_fops[] = {
1280 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1281 [KVM_STAT_VM] = &vm_stat_fops,
1282};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001283
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001284static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285{
1286 struct kvm_stats_debugfs_item *p;
1287
Al Viro8b6d44c2007-02-09 16:38:40 +00001288 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001290 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1291 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001292 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293}
1294
1295static void kvm_exit_debug(void)
1296{
1297 struct kvm_stats_debugfs_item *p;
1298
1299 for (p = debugfs_entries; p->name; ++p)
1300 debugfs_remove(p->dentry);
1301 debugfs_remove(debugfs_dir);
1302}
1303
Avi Kivity59ae6c62007-02-12 00:54:48 -08001304static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1305{
Avi Kivity4267c412007-05-24 13:09:41 +03001306 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001307 return 0;
1308}
1309
1310static int kvm_resume(struct sys_device *dev)
1311{
Avi Kivity4267c412007-05-24 13:09:41 +03001312 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001313 return 0;
1314}
1315
1316static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01001317 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001318 .suspend = kvm_suspend,
1319 .resume = kvm_resume,
1320};
1321
1322static struct sys_device kvm_sysdev = {
1323 .id = 0,
1324 .cls = &kvm_sysdev_class,
1325};
1326
Izik Eiduscea7bb22007-10-17 19:17:48 +02001327struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328
Avi Kivity15ad7142007-07-11 18:17:21 +03001329static inline
1330struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1331{
1332 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1333}
1334
1335static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1336{
1337 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1338
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001339 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001340}
1341
1342static void kvm_sched_out(struct preempt_notifier *pn,
1343 struct task_struct *next)
1344{
1345 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1346
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001347 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001348}
1349
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001350int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001351 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352{
1353 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001354 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001356 kvm_init_debug();
1357
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001358 r = kvm_arch_init(opaque);
1359 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001360 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001361
1362 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1363
1364 if (bad_page == NULL) {
1365 r = -ENOMEM;
1366 goto out;
1367 }
1368
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001369 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001371 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372
Yang, Sheng002c7f72007-07-31 14:23:01 +03001373 for_each_online_cpu(cpu) {
1374 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001375 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001376 &r, 0, 1);
1377 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001378 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001379 }
1380
Avi Kivity1b6c0162007-05-24 13:03:52 +03001381 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001382 r = register_cpu_notifier(&kvm_cpu_notifier);
1383 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001384 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 register_reboot_notifier(&kvm_reboot_notifier);
1386
Avi Kivity59ae6c62007-02-12 00:54:48 -08001387 r = sysdev_class_register(&kvm_sysdev_class);
1388 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001389 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001390
1391 r = sysdev_register(&kvm_sysdev);
1392 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001393 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001394
Rusty Russellc16f8622007-07-30 21:12:19 +10001395 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1396 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001397 __alignof__(struct kvm_vcpu),
1398 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001399 if (!kvm_vcpu_cache) {
1400 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001401 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001402 }
1403
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 kvm_chardev_ops.owner = module;
1405
1406 r = misc_register(&kvm_dev);
1407 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001408 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 goto out_free;
1410 }
1411
Avi Kivity15ad7142007-07-11 18:17:21 +03001412 kvm_preempt_ops.sched_in = kvm_sched_in;
1413 kvm_preempt_ops.sched_out = kvm_sched_out;
1414
Avi Kivityc7addb92007-09-16 18:58:32 +02001415 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
1417out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001418 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001419out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001420 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001421out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001422 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001423out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001425 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001426out_free_2:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001427 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001428out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001429 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001430out_free_0:
1431 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001432out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001433 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001434 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001435out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 return r;
1437}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001438EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001440void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441{
1442 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001443 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001444 sysdev_unregister(&kvm_sysdev);
1445 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001447 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001448 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001449 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001450 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001452 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001454EXPORT_SYMBOL_GPL(kvm_exit);