blob: a845890b680062365930b89a63ba4536c0fa6670 [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>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.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>
Izik Eidus3e021bf2007-11-19 11:16:57 +020048#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Laurent Vivier5f94c172008-05-30 16:05:54 +020050#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51#include "coalesced_mmio.h"
52#endif
53
Avi Kivity6aa8b732006-12-10 02:21:36 -080054MODULE_AUTHOR("Qumranet");
55MODULE_LICENSE("GPL");
56
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080057DEFINE_SPINLOCK(kvm_lock);
58LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080059
Avi Kivity1b6c0162007-05-24 13:03:52 +030060static cpumask_t cpus_hardware_enabled;
61
Rusty Russellc16f8622007-07-30 21:12:19 +100062struct kmem_cache *kvm_vcpu_cache;
63EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030064
Avi Kivity15ad7142007-07-11 18:17:21 +030065static __read_mostly struct preempt_ops kvm_preempt_ops;
66
Hollis Blanchard76f7c872008-04-15 16:05:42 -050067struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivitybccf2152007-02-21 18:04:26 +020069static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
70 unsigned long arg);
71
Avi Kivity4ecac3f2008-05-13 13:23:38 +030072bool kvm_rebooting;
73
James Morris5aacf0c2006-12-22 01:04:55 -080074static inline int valid_vcpu(int n)
75{
76 return likely(n >= 0 && n < KVM_MAX_VCPUS);
77}
78
Avi Kivity6aa8b732006-12-10 02:21:36 -080079/*
80 * Switches to specified vcpu, until a matching vcpu_put()
81 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020082void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080083{
Avi Kivity15ad7142007-07-11 18:17:21 +030084 int cpu;
85
Avi Kivitybccf2152007-02-21 18:04:26 +020086 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030087 cpu = get_cpu();
88 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020089 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030090 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020091}
92
Carsten Otte313a3dc2007-10-11 19:16:52 +020093void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094{
Avi Kivity15ad7142007-07-11 18:17:21 +030095 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020096 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030097 preempt_notifier_unregister(&vcpu->preempt_notifier);
98 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 mutex_unlock(&vcpu->mutex);
100}
101
Avi Kivityd9e368d2007-06-07 19:18:30 +0300102static void ack_flush(void *_completed)
103{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300104}
105
106void kvm_flush_remote_tlbs(struct kvm *kvm)
107{
Avi Kivity597a5f52008-07-20 14:24:22 +0300108 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300109 cpumask_t cpus;
110 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300111
Avi Kivity597a5f52008-07-20 14:24:22 +0300112 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300113 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000114 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
115 vcpu = kvm->vcpus[i];
116 if (!vcpu)
117 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200118 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300119 continue;
120 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300121 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200122 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300123 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200124 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300125 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200126 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200127 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300128out:
129 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300130}
131
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500132void kvm_reload_remote_mmus(struct kvm *kvm)
133{
Avi Kivity597a5f52008-07-20 14:24:22 +0300134 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500135 cpumask_t cpus;
136 struct kvm_vcpu *vcpu;
137
Avi Kivity597a5f52008-07-20 14:24:22 +0300138 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500139 cpus_clear(cpus);
140 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
141 vcpu = kvm->vcpus[i];
142 if (!vcpu)
143 continue;
144 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
145 continue;
146 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300147 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500148 cpu_set(cpu, cpus);
149 }
150 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300151 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500152 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300153out:
154 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500155}
156
157
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000158int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
159{
160 struct page *page;
161 int r;
162
163 mutex_init(&vcpu->mutex);
164 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000165 vcpu->kvm = kvm;
166 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300167 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000168
169 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
170 if (!page) {
171 r = -ENOMEM;
172 goto fail;
173 }
174 vcpu->run = page_address(page);
175
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800176 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000177 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800178 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000179 return 0;
180
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000181fail_free_run:
182 free_page((unsigned long)vcpu->run);
183fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000184 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000185}
186EXPORT_SYMBOL_GPL(kvm_vcpu_init);
187
188void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
189{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800190 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191 free_page((unsigned long)vcpu->run);
192}
193EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
194
Avi Kivityf17abe92007-02-21 19:28:04 +0200195static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800197 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200198#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
199 struct page *page;
200#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800202 if (IS_ERR(kvm))
203 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204
Laurent Vivier5f94c172008-05-30 16:05:54 +0200205#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
206 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
207 if (!page) {
208 kfree(kvm);
209 return ERR_PTR(-ENOMEM);
210 }
211 kvm->coalesced_mmio_ring =
212 (struct kvm_coalesced_mmio_ring *)page_address(page);
213#endif
214
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200215 kvm->mm = current->mm;
216 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500217 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300218 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800219 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400220 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200221 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300222 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000223 spin_lock(&kvm_lock);
224 list_add(&kvm->vm_list, &vm_list);
225 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200226#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
227 kvm_coalesced_mmio_init(kvm);
228#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800229out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200230 return kvm;
231}
232
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233/*
234 * Free any memory in @free but not in @dont.
235 */
236static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
237 struct kvm_memory_slot *dont)
238{
Izik Eidus290fc382007-09-27 14:11:22 +0200239 if (!dont || free->rmap != dont->rmap)
240 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241
242 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
243 vfree(free->dirty_bitmap);
244
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300245 if (!dont || free->lpage_info != dont->lpage_info)
246 vfree(free->lpage_info);
247
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000249 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500250 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300251 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252}
253
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800254void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255{
256 int i;
257
258 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000259 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260}
261
Avi Kivityf17abe92007-02-21 19:28:04 +0200262static void kvm_destroy_vm(struct kvm *kvm)
263{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200264 struct mm_struct *mm = kvm->mm;
265
Avi Kivity133de902007-02-12 00:54:44 -0800266 spin_lock(&kvm_lock);
267 list_del(&kvm->vm_list);
268 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300269 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400270 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200271#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
272 if (kvm->coalesced_mmio_ring != NULL)
273 free_page((unsigned long)kvm->coalesced_mmio_ring);
274#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800275 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200276 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200277}
278
Izik Eidusd39f13b2008-03-30 16:01:25 +0300279void kvm_get_kvm(struct kvm *kvm)
280{
281 atomic_inc(&kvm->users_count);
282}
283EXPORT_SYMBOL_GPL(kvm_get_kvm);
284
285void kvm_put_kvm(struct kvm *kvm)
286{
287 if (atomic_dec_and_test(&kvm->users_count))
288 kvm_destroy_vm(kvm);
289}
290EXPORT_SYMBOL_GPL(kvm_put_kvm);
291
292
Avi Kivityf17abe92007-02-21 19:28:04 +0200293static int kvm_vm_release(struct inode *inode, struct file *filp)
294{
295 struct kvm *kvm = filp->private_data;
296
Izik Eidusd39f13b2008-03-30 16:01:25 +0300297 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 return 0;
299}
300
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 * Allocate some memory and give it an address in the guest physical address
303 * space.
304 *
305 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800306 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500307 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800309int __kvm_set_memory_region(struct kvm *kvm,
310 struct kvm_userspace_memory_region *mem,
311 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312{
313 int r;
314 gfn_t base_gfn;
315 unsigned long npages;
316 unsigned long i;
317 struct kvm_memory_slot *memslot;
318 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319
320 r = -EINVAL;
321 /* General sanity checks */
322 if (mem->memory_size & (PAGE_SIZE - 1))
323 goto out;
324 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
325 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200326 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 goto out;
328 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
329 goto out;
330
331 memslot = &kvm->memslots[mem->slot];
332 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
333 npages = mem->memory_size >> PAGE_SHIFT;
334
335 if (!npages)
336 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
337
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 new = old = *memslot;
339
340 new.base_gfn = base_gfn;
341 new.npages = npages;
342 new.flags = mem->flags;
343
344 /* Disallow changing a memory slot's size. */
345 r = -EINVAL;
346 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800347 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348
349 /* Check for overlaps */
350 r = -EEXIST;
351 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
352 struct kvm_memory_slot *s = &kvm->memslots[i];
353
354 if (s == memslot)
355 continue;
356 if (!((base_gfn + npages <= s->base_gfn) ||
357 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800358 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 /* Free page dirty bitmap if unneeded */
362 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000363 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364
365 r = -ENOMEM;
366
367 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200368#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500369 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400370 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200371
372 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800373 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200374
Izik Eidus290fc382007-09-27 14:11:22 +0200375 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500376
Izik Eidus80b14b52007-10-25 11:54:04 +0200377 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800378 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300380 if (npages && !new.lpage_info) {
381 int largepages = npages / KVM_PAGES_PER_HPAGE;
382 if (npages % KVM_PAGES_PER_HPAGE)
383 largepages++;
384 if (base_gfn % KVM_PAGES_PER_HPAGE)
385 largepages++;
386
387 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
388
389 if (!new.lpage_info)
390 goto out_free;
391
392 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
393
394 if (base_gfn % KVM_PAGES_PER_HPAGE)
395 new.lpage_info[0].write_count = 1;
396 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
397 new.lpage_info[largepages-1].write_count = 1;
398 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399
400 /* Allocate page dirty bitmap if needed */
401 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
402 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
403
404 new.dirty_bitmap = vmalloc(dirty_bytes);
405 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800406 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 memset(new.dirty_bitmap, 0, dirty_bytes);
408 }
Carsten Otteeff01142008-06-27 15:05:31 +0200409#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 if (mem->slot >= kvm->nmemslots)
412 kvm->nmemslots = mem->slot + 1;
413
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300414 if (!npages)
415 kvm_arch_flush_shadow(kvm);
416
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Zhang Xiantao0de10342007-11-20 16:25:04 +0800419 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
420 if (r) {
421 *memslot = old;
422 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800423 }
424
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 kvm_free_physmem_slot(&old, &new);
426 return 0;
427
Sheng Yangf78e0e22007-10-29 09:40:42 +0800428out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 kvm_free_physmem_slot(&new, &old);
430out:
431 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200432
433}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800434EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
435
436int kvm_set_memory_region(struct kvm *kvm,
437 struct kvm_userspace_memory_region *mem,
438 int user_alloc)
439{
440 int r;
441
Izik Eidus72dc67a2008-02-10 18:04:15 +0200442 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800443 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200444 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800445 return r;
446}
Izik Eidus210c7c42007-10-24 23:52:57 +0200447EXPORT_SYMBOL_GPL(kvm_set_memory_region);
448
Carsten Otte1fe779f2007-10-29 16:08:35 +0100449int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
450 struct
451 kvm_userspace_memory_region *mem,
452 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200453{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200454 if (mem->slot >= KVM_MEMORY_SLOTS)
455 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200456 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457}
458
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800459int kvm_get_dirty_log(struct kvm *kvm,
460 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461{
462 struct kvm_memory_slot *memslot;
463 int r, i;
464 int n;
465 unsigned long any = 0;
466
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 r = -EINVAL;
468 if (log->slot >= KVM_MEMORY_SLOTS)
469 goto out;
470
471 memslot = &kvm->memslots[log->slot];
472 r = -ENOENT;
473 if (!memslot->dirty_bitmap)
474 goto out;
475
Uri Lublincd1a4a92007-02-22 16:43:09 +0200476 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477
Uri Lublincd1a4a92007-02-22 16:43:09 +0200478 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 any = memslot->dirty_bitmap[i];
480
481 r = -EFAULT;
482 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
483 goto out;
484
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800485 if (any)
486 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487
488 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 return r;
491}
492
Izik Eiduscea7bb22007-10-17 19:17:48 +0200493int is_error_page(struct page *page)
494{
495 return page == bad_page;
496}
497EXPORT_SYMBOL_GPL(is_error_page);
498
Anthony Liguori35149e22008-04-02 14:46:56 -0500499int is_error_pfn(pfn_t pfn)
500{
501 return pfn == bad_pfn;
502}
503EXPORT_SYMBOL_GPL(is_error_pfn);
504
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200505static inline unsigned long bad_hva(void)
506{
507 return PAGE_OFFSET;
508}
509
510int kvm_is_error_hva(unsigned long addr)
511{
512 return addr == bad_hva();
513}
514EXPORT_SYMBOL_GPL(kvm_is_error_hva);
515
Avi Kivitye8207542007-03-30 16:54:30 +0300516static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517{
518 int i;
519
520 for (i = 0; i < kvm->nmemslots; ++i) {
521 struct kvm_memory_slot *memslot = &kvm->memslots[i];
522
523 if (gfn >= memslot->base_gfn
524 && gfn < memslot->base_gfn + memslot->npages)
525 return memslot;
526 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000527 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528}
Avi Kivitye8207542007-03-30 16:54:30 +0300529
530struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
531{
532 gfn = unalias_gfn(kvm, gfn);
533 return __gfn_to_memslot(kvm, gfn);
534}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535
Izik Eiduse0d62c72007-10-24 23:57:46 +0200536int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
537{
538 int i;
539
540 gfn = unalias_gfn(kvm, gfn);
541 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
542 struct kvm_memory_slot *memslot = &kvm->memslots[i];
543
544 if (gfn >= memslot->base_gfn
545 && gfn < memslot->base_gfn + memslot->npages)
546 return 1;
547 }
548 return 0;
549}
550EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
551
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300552unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200553{
554 struct kvm_memory_slot *slot;
555
556 gfn = unalias_gfn(kvm, gfn);
557 slot = __gfn_to_memslot(kvm, gfn);
558 if (!slot)
559 return bad_hva();
560 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
561}
Sheng Yang0d150292008-04-25 21:44:50 +0800562EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200563
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500564/*
565 * Requires current->mm->mmap_sem to be held
566 */
Anthony Liguori35149e22008-04-02 14:46:56 -0500567pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300568{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500569 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200570 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500571 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500572 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300573
Avi Kivity60395222007-10-21 11:03:36 +0200574 might_sleep();
575
Izik Eidus539cb662007-11-11 22:05:04 +0200576 addr = gfn_to_hva(kvm, gfn);
577 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200578 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500579 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200580 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200581
Izik Eidus539cb662007-11-11 22:05:04 +0200582 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
583 NULL);
584
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500585 if (unlikely(npages != 1)) {
586 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500587
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500588 vma = find_vma(current->mm, addr);
589 if (vma == NULL || addr < vma->vm_start ||
590 !(vma->vm_flags & VM_PFNMAP)) {
591 get_page(bad_page);
592 return page_to_pfn(bad_page);
593 }
594
595 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
596 BUG_ON(pfn_valid(pfn));
597 } else
598 pfn = page_to_pfn(page[0]);
599
600 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500601}
602
603EXPORT_SYMBOL_GPL(gfn_to_pfn);
604
605struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
606{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500607 pfn_t pfn;
608
609 pfn = gfn_to_pfn(kvm, gfn);
610 if (pfn_valid(pfn))
611 return pfn_to_page(pfn);
612
613 WARN_ON(!pfn_valid(pfn));
614
615 get_page(bad_page);
616 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300617}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500618
Avi Kivity954bbbc2007-03-30 14:02:32 +0300619EXPORT_SYMBOL_GPL(gfn_to_page);
620
Izik Eidusb4231d62007-11-20 11:49:33 +0200621void kvm_release_page_clean(struct page *page)
622{
Anthony Liguori35149e22008-04-02 14:46:56 -0500623 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200624}
625EXPORT_SYMBOL_GPL(kvm_release_page_clean);
626
Anthony Liguori35149e22008-04-02 14:46:56 -0500627void kvm_release_pfn_clean(pfn_t pfn)
628{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500629 if (pfn_valid(pfn))
630 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500631}
632EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
633
Izik Eidusb4231d62007-11-20 11:49:33 +0200634void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200635{
Anthony Liguori35149e22008-04-02 14:46:56 -0500636 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200637}
Izik Eidusb4231d62007-11-20 11:49:33 +0200638EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200639
Anthony Liguori35149e22008-04-02 14:46:56 -0500640void kvm_release_pfn_dirty(pfn_t pfn)
641{
642 kvm_set_pfn_dirty(pfn);
643 kvm_release_pfn_clean(pfn);
644}
645EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
646
647void kvm_set_page_dirty(struct page *page)
648{
649 kvm_set_pfn_dirty(page_to_pfn(page));
650}
651EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
652
653void kvm_set_pfn_dirty(pfn_t pfn)
654{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500655 if (pfn_valid(pfn)) {
656 struct page *page = pfn_to_page(pfn);
657 if (!PageReserved(page))
658 SetPageDirty(page);
659 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500660}
661EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
662
663void kvm_set_pfn_accessed(pfn_t pfn)
664{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500665 if (pfn_valid(pfn))
666 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500667}
668EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
669
670void kvm_get_pfn(pfn_t pfn)
671{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500672 if (pfn_valid(pfn))
673 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500674}
675EXPORT_SYMBOL_GPL(kvm_get_pfn);
676
Izik Eidus195aefd2007-10-01 22:14:18 +0200677static int next_segment(unsigned long len, int offset)
678{
679 if (len > PAGE_SIZE - offset)
680 return PAGE_SIZE - offset;
681 else
682 return len;
683}
684
685int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
686 int len)
687{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200688 int r;
689 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200690
Izik Eiduse0506bc2007-11-11 22:10:22 +0200691 addr = gfn_to_hva(kvm, gfn);
692 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200693 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200694 r = copy_from_user(data, (void __user *)addr + offset, len);
695 if (r)
696 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200697 return 0;
698}
699EXPORT_SYMBOL_GPL(kvm_read_guest_page);
700
701int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
702{
703 gfn_t gfn = gpa >> PAGE_SHIFT;
704 int seg;
705 int offset = offset_in_page(gpa);
706 int ret;
707
708 while ((seg = next_segment(len, offset)) != 0) {
709 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
710 if (ret < 0)
711 return ret;
712 offset = 0;
713 len -= seg;
714 data += seg;
715 ++gfn;
716 }
717 return 0;
718}
719EXPORT_SYMBOL_GPL(kvm_read_guest);
720
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500721int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
722 unsigned long len)
723{
724 int r;
725 unsigned long addr;
726 gfn_t gfn = gpa >> PAGE_SHIFT;
727 int offset = offset_in_page(gpa);
728
729 addr = gfn_to_hva(kvm, gfn);
730 if (kvm_is_error_hva(addr))
731 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100732 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500733 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100734 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500735 if (r)
736 return -EFAULT;
737 return 0;
738}
739EXPORT_SYMBOL(kvm_read_guest_atomic);
740
Izik Eidus195aefd2007-10-01 22:14:18 +0200741int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
742 int offset, int len)
743{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200744 int r;
745 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200746
Izik Eiduse0506bc2007-11-11 22:10:22 +0200747 addr = gfn_to_hva(kvm, gfn);
748 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200749 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200750 r = copy_to_user((void __user *)addr + offset, data, len);
751 if (r)
752 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200753 mark_page_dirty(kvm, gfn);
754 return 0;
755}
756EXPORT_SYMBOL_GPL(kvm_write_guest_page);
757
758int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
759 unsigned long len)
760{
761 gfn_t gfn = gpa >> PAGE_SHIFT;
762 int seg;
763 int offset = offset_in_page(gpa);
764 int ret;
765
766 while ((seg = next_segment(len, offset)) != 0) {
767 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
768 if (ret < 0)
769 return ret;
770 offset = 0;
771 len -= seg;
772 data += seg;
773 ++gfn;
774 }
775 return 0;
776}
777
778int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
779{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200780 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200781}
782EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
783
784int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
785{
786 gfn_t gfn = gpa >> PAGE_SHIFT;
787 int seg;
788 int offset = offset_in_page(gpa);
789 int ret;
790
791 while ((seg = next_segment(len, offset)) != 0) {
792 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
793 if (ret < 0)
794 return ret;
795 offset = 0;
796 len -= seg;
797 ++gfn;
798 }
799 return 0;
800}
801EXPORT_SYMBOL_GPL(kvm_clear_guest);
802
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
804{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300805 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806
Uri Lublin3b6fff12007-10-30 10:42:09 +0200807 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000808 memslot = __gfn_to_memslot(kvm, gfn);
809 if (memslot && memslot->dirty_bitmap) {
810 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
Rusty Russell7e9d6192007-07-31 20:41:14 +1000812 /* avoid RMW */
813 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
814 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 }
816}
817
Eddie Dongb6958ce2007-07-18 12:15:21 +0300818/*
819 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
820 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500821void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300822{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300823 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300824
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300825 for (;;) {
826 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300827
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300828 if (kvm_cpu_has_interrupt(vcpu))
829 break;
830 if (kvm_cpu_has_pending_timer(vcpu))
831 break;
832 if (kvm_arch_vcpu_runnable(vcpu))
833 break;
834 if (signal_pending(current))
835 break;
836
Eddie Dongb6958ce2007-07-18 12:15:21 +0300837 vcpu_put(vcpu);
838 schedule();
839 vcpu_load(vcpu);
840 }
841
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300842 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300843}
844
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845void kvm_resched(struct kvm_vcpu *vcpu)
846{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300847 if (!need_resched())
848 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850}
851EXPORT_SYMBOL_GPL(kvm_resched);
852
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100853static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200854{
855 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200856 struct page *page;
857
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100858 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +0200859 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +0200860#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100861 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800862 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +0200863#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +0200864#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
865 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
866 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
867#endif
Avi Kivity039576c2007-03-20 12:46:50 +0200868 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100869 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200870 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100871 vmf->page = page;
872 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200873}
874
875static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100876 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200877};
878
879static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
880{
881 vma->vm_ops = &kvm_vcpu_vm_ops;
882 return 0;
883}
884
Avi Kivitybccf2152007-02-21 18:04:26 +0200885static int kvm_vcpu_release(struct inode *inode, struct file *filp)
886{
887 struct kvm_vcpu *vcpu = filp->private_data;
888
Al Viro66c0b392008-04-19 20:33:56 +0100889 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +0200890 return 0;
891}
892
Jan Engelhardt5c502742008-01-22 20:46:14 +0100893static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +0200894 .release = kvm_vcpu_release,
895 .unlocked_ioctl = kvm_vcpu_ioctl,
896 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200897 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200898};
899
900/*
901 * Allocates an inode for the vcpu.
902 */
903static int create_vcpu_fd(struct kvm_vcpu *vcpu)
904{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -0700905 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -0500906 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +0100907 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +0200908 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200909}
910
Avi Kivityc5ea7662007-02-20 18:41:05 +0200911/*
912 * Creates some virtual cpus. Good luck creating more than one.
913 */
914static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
915{
916 int r;
917 struct kvm_vcpu *vcpu;
918
Avi Kivityc5ea7662007-02-20 18:41:05 +0200919 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000920 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200921
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800922 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000923 if (IS_ERR(vcpu))
924 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200925
Avi Kivity15ad7142007-07-11 18:17:21 +0300926 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
927
Avi Kivity26e52152007-11-20 15:30:24 +0200928 r = kvm_arch_vcpu_setup(vcpu);
929 if (r)
930 goto vcpu_destroy;
931
Shaohua Li11ec2802007-07-23 14:51:37 +0800932 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000933 if (kvm->vcpus[n]) {
934 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800935 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800936 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000937 }
938 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800939 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000940
941 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +0100942 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +0200943 r = create_vcpu_fd(vcpu);
944 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000945 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200946 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200947
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000948unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800949 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000950 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800951 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800952vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600953 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200954 return r;
955}
956
Avi Kivity1961d272007-03-05 19:46:05 +0200957static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
958{
959 if (sigset) {
960 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
961 vcpu->sigset_active = 1;
962 vcpu->sigset = *sigset;
963 } else
964 vcpu->sigset_active = 0;
965 return 0;
966}
967
Avi Kivitybccf2152007-02-21 18:04:26 +0200968static long kvm_vcpu_ioctl(struct file *filp,
969 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970{
Avi Kivitybccf2152007-02-21 18:04:26 +0200971 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000972 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200973 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200975 if (vcpu->kvm->mm != current->mm)
976 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200978 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200979 r = -EINVAL;
980 if (arg)
981 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500982 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800985 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800987 r = -ENOMEM;
988 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
989 if (!kvm_regs)
990 goto out;
991 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800993 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800995 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
996 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800998out_free1:
999 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 break;
1001 }
1002 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001003 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001005 r = -ENOMEM;
1006 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1007 if (!kvm_regs)
1008 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001010 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1011 goto out_free2;
1012 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001014 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001016out_free2:
1017 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 break;
1019 }
1020 case KVM_GET_SREGS: {
1021 struct kvm_sregs kvm_sregs;
1022
Avi Kivitybccf2152007-02-21 18:04:26 +02001023 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001024 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 if (r)
1026 goto out;
1027 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001028 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 goto out;
1030 r = 0;
1031 break;
1032 }
1033 case KVM_SET_SREGS: {
1034 struct kvm_sregs kvm_sregs;
1035
1036 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001037 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001039 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 if (r)
1041 goto out;
1042 r = 0;
1043 break;
1044 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001045 case KVM_GET_MP_STATE: {
1046 struct kvm_mp_state mp_state;
1047
1048 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1049 if (r)
1050 goto out;
1051 r = -EFAULT;
1052 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1053 goto out;
1054 r = 0;
1055 break;
1056 }
1057 case KVM_SET_MP_STATE: {
1058 struct kvm_mp_state mp_state;
1059
1060 r = -EFAULT;
1061 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1062 goto out;
1063 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1064 if (r)
1065 goto out;
1066 r = 0;
1067 break;
1068 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 case KVM_TRANSLATE: {
1070 struct kvm_translation tr;
1071
1072 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001073 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001075 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 if (r)
1077 goto out;
1078 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001079 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 goto out;
1081 r = 0;
1082 break;
1083 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 case KVM_DEBUG_GUEST: {
1085 struct kvm_debug_guest dbg;
1086
1087 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001088 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001090 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 if (r)
1092 goto out;
1093 r = 0;
1094 break;
1095 }
Avi Kivity1961d272007-03-05 19:46:05 +02001096 case KVM_SET_SIGNAL_MASK: {
1097 struct kvm_signal_mask __user *sigmask_arg = argp;
1098 struct kvm_signal_mask kvm_sigmask;
1099 sigset_t sigset, *p;
1100
1101 p = NULL;
1102 if (argp) {
1103 r = -EFAULT;
1104 if (copy_from_user(&kvm_sigmask, argp,
1105 sizeof kvm_sigmask))
1106 goto out;
1107 r = -EINVAL;
1108 if (kvm_sigmask.len != sizeof sigset)
1109 goto out;
1110 r = -EFAULT;
1111 if (copy_from_user(&sigset, sigmask_arg->sigset,
1112 sizeof sigset))
1113 goto out;
1114 p = &sigset;
1115 }
1116 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1117 break;
1118 }
Avi Kivityb8836732007-04-01 16:34:31 +03001119 case KVM_GET_FPU: {
1120 struct kvm_fpu fpu;
1121
1122 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05001123 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001124 if (r)
1125 goto out;
1126 r = -EFAULT;
1127 if (copy_to_user(argp, &fpu, sizeof fpu))
1128 goto out;
1129 r = 0;
1130 break;
1131 }
1132 case KVM_SET_FPU: {
1133 struct kvm_fpu fpu;
1134
1135 r = -EFAULT;
1136 if (copy_from_user(&fpu, argp, sizeof fpu))
1137 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -05001138 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001139 if (r)
1140 goto out;
1141 r = 0;
1142 break;
1143 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001144 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001145 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001146 }
1147out:
1148 return r;
1149}
1150
1151static long kvm_vm_ioctl(struct file *filp,
1152 unsigned int ioctl, unsigned long arg)
1153{
1154 struct kvm *kvm = filp->private_data;
1155 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001156 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001157
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001158 if (kvm->mm != current->mm)
1159 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001160 switch (ioctl) {
1161 case KVM_CREATE_VCPU:
1162 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1163 if (r < 0)
1164 goto out;
1165 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001166 case KVM_SET_USER_MEMORY_REGION: {
1167 struct kvm_userspace_memory_region kvm_userspace_mem;
1168
1169 r = -EFAULT;
1170 if (copy_from_user(&kvm_userspace_mem, argp,
1171 sizeof kvm_userspace_mem))
1172 goto out;
1173
1174 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 if (r)
1176 goto out;
1177 break;
1178 }
1179 case KVM_GET_DIRTY_LOG: {
1180 struct kvm_dirty_log log;
1181
1182 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001183 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001185 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 if (r)
1187 goto out;
1188 break;
1189 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001190#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1191 case KVM_REGISTER_COALESCED_MMIO: {
1192 struct kvm_coalesced_mmio_zone zone;
1193 r = -EFAULT;
1194 if (copy_from_user(&zone, argp, sizeof zone))
1195 goto out;
1196 r = -ENXIO;
1197 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1198 if (r)
1199 goto out;
1200 r = 0;
1201 break;
1202 }
1203 case KVM_UNREGISTER_COALESCED_MMIO: {
1204 struct kvm_coalesced_mmio_zone zone;
1205 r = -EFAULT;
1206 if (copy_from_user(&zone, argp, sizeof zone))
1207 goto out;
1208 r = -ENXIO;
1209 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1210 if (r)
1211 goto out;
1212 r = 0;
1213 break;
1214 }
1215#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001216 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001217 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001218 }
1219out:
1220 return r;
1221}
1222
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001223static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001224{
1225 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001226 struct page *page;
1227
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001228 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1229 return VM_FAULT_SIGBUS;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001230 page = gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001231 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001232 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001233 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001234 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001235 vmf->page = page;
1236 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001237}
1238
1239static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001240 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001241};
1242
1243static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1244{
1245 vma->vm_ops = &kvm_vm_vm_ops;
1246 return 0;
1247}
1248
Jan Engelhardt5c502742008-01-22 20:46:14 +01001249static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001250 .release = kvm_vm_release,
1251 .unlocked_ioctl = kvm_vm_ioctl,
1252 .compat_ioctl = kvm_vm_ioctl,
1253 .mmap = kvm_vm_mmap,
1254};
1255
1256static int kvm_dev_ioctl_create_vm(void)
1257{
Al Viro2030a422008-02-23 06:46:49 -05001258 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001259 struct kvm *kvm;
1260
Avi Kivityf17abe92007-02-21 19:28:04 +02001261 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001262 if (IS_ERR(kvm))
1263 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001264 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001265 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001266 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001267
Avi Kivityf17abe92007-02-21 19:28:04 +02001268 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001269}
1270
1271static long kvm_dev_ioctl(struct file *filp,
1272 unsigned int ioctl, unsigned long arg)
1273{
Avi Kivity07c45a32007-03-07 13:05:38 +02001274 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001275
1276 switch (ioctl) {
1277 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001278 r = -EINVAL;
1279 if (arg)
1280 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001281 r = KVM_API_VERSION;
1282 break;
1283 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001284 r = -EINVAL;
1285 if (arg)
1286 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001287 r = kvm_dev_ioctl_create_vm();
1288 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001289 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001290 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001291 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001292 case KVM_GET_VCPU_MMAP_SIZE:
1293 r = -EINVAL;
1294 if (arg)
1295 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001296 r = PAGE_SIZE; /* struct kvm_run */
1297#ifdef CONFIG_X86
1298 r += PAGE_SIZE; /* pio data page */
1299#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001300#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1301 r += PAGE_SIZE; /* coalesced mmio ring page */
1302#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001303 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001304 case KVM_TRACE_ENABLE:
1305 case KVM_TRACE_PAUSE:
1306 case KVM_TRACE_DISABLE:
1307 r = kvm_trace_ioctl(ioctl, arg);
1308 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001310 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311 }
1312out:
1313 return r;
1314}
1315
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317 .unlocked_ioctl = kvm_dev_ioctl,
1318 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319};
1320
1321static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001322 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 "kvm",
1324 &kvm_chardev_ops,
1325};
1326
Avi Kivity1b6c0162007-05-24 13:03:52 +03001327static void hardware_enable(void *junk)
1328{
1329 int cpu = raw_smp_processor_id();
1330
1331 if (cpu_isset(cpu, cpus_hardware_enabled))
1332 return;
1333 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001334 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001335}
1336
1337static void hardware_disable(void *junk)
1338{
1339 int cpu = raw_smp_processor_id();
1340
1341 if (!cpu_isset(cpu, cpus_hardware_enabled))
1342 return;
1343 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001344 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001345}
1346
Avi Kivity774c47f2007-02-12 00:54:47 -08001347static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1348 void *v)
1349{
1350 int cpu = (long)v;
1351
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001352 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001353 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001354 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03001355 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1356 cpu);
1357 hardware_disable(NULL);
1358 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001359 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001360 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1361 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001362 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001363 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001364 case CPU_ONLINE:
1365 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1366 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001367 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001368 break;
1369 }
1370 return NOTIFY_OK;
1371}
1372
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001373
1374asmlinkage void kvm_handle_fault_on_reboot(void)
1375{
1376 if (kvm_rebooting)
1377 /* spin while reset goes on */
1378 while (true)
1379 ;
1380 /* Fault while not rebooting. We want the trace. */
1381 BUG();
1382}
1383EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1384
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001385static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001386 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001387{
1388 if (val == SYS_RESTART) {
1389 /*
1390 * Some (well, at least mine) BIOSes hang on reboot if
1391 * in vmx root mode.
1392 */
1393 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001394 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001395 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001396 }
1397 return NOTIFY_OK;
1398}
1399
1400static struct notifier_block kvm_reboot_notifier = {
1401 .notifier_call = kvm_reboot,
1402 .priority = 0,
1403};
1404
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001405void kvm_io_bus_init(struct kvm_io_bus *bus)
1406{
1407 memset(bus, 0, sizeof(*bus));
1408}
1409
1410void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1411{
1412 int i;
1413
1414 for (i = 0; i < bus->dev_count; i++) {
1415 struct kvm_io_device *pos = bus->devs[i];
1416
1417 kvm_iodevice_destructor(pos);
1418 }
1419}
1420
Laurent Vivier92760492008-05-30 16:05:53 +02001421struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1422 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001423{
1424 int i;
1425
1426 for (i = 0; i < bus->dev_count; i++) {
1427 struct kvm_io_device *pos = bus->devs[i];
1428
Laurent Vivier92760492008-05-30 16:05:53 +02001429 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001430 return pos;
1431 }
1432
1433 return NULL;
1434}
1435
1436void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1437{
1438 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1439
1440 bus->devs[bus->dev_count++] = dev;
1441}
1442
Avi Kivity774c47f2007-02-12 00:54:47 -08001443static struct notifier_block kvm_cpu_notifier = {
1444 .notifier_call = kvm_cpu_hotplug,
1445 .priority = 20, /* must be > scheduler priority */
1446};
1447
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001448static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001449{
1450 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001451 struct kvm *kvm;
1452
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001453 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001454 spin_lock(&kvm_lock);
1455 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001456 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001457 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001458 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001459}
1460
1461DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1462
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001463static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001464{
1465 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001466 struct kvm *kvm;
1467 struct kvm_vcpu *vcpu;
1468 int i;
1469
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001470 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001471 spin_lock(&kvm_lock);
1472 list_for_each_entry(kvm, &vm_list, vm_list)
1473 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001474 vcpu = kvm->vcpus[i];
1475 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001476 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001477 }
1478 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001479 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001480}
1481
Avi Kivityba1389b2007-11-18 16:24:12 +02001482DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1483
1484static struct file_operations *stat_fops[] = {
1485 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1486 [KVM_STAT_VM] = &vm_stat_fops,
1487};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001488
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001489static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490{
1491 struct kvm_stats_debugfs_item *p;
1492
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001493 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001495 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001496 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001497 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498}
1499
1500static void kvm_exit_debug(void)
1501{
1502 struct kvm_stats_debugfs_item *p;
1503
1504 for (p = debugfs_entries; p->name; ++p)
1505 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001506 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507}
1508
Avi Kivity59ae6c62007-02-12 00:54:48 -08001509static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1510{
Avi Kivity4267c412007-05-24 13:09:41 +03001511 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001512 return 0;
1513}
1514
1515static int kvm_resume(struct sys_device *dev)
1516{
Avi Kivity4267c412007-05-24 13:09:41 +03001517 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001518 return 0;
1519}
1520
1521static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001522 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001523 .suspend = kvm_suspend,
1524 .resume = kvm_resume,
1525};
1526
1527static struct sys_device kvm_sysdev = {
1528 .id = 0,
1529 .cls = &kvm_sysdev_class,
1530};
1531
Izik Eiduscea7bb22007-10-17 19:17:48 +02001532struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001533pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534
Avi Kivity15ad7142007-07-11 18:17:21 +03001535static inline
1536struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1537{
1538 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1539}
1540
1541static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1542{
1543 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1544
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001545 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001546}
1547
1548static void kvm_sched_out(struct preempt_notifier *pn,
1549 struct task_struct *next)
1550{
1551 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1552
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001553 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001554}
1555
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001556int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001557 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558{
1559 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001560 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001562 kvm_init_debug();
1563
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001564 r = kvm_arch_init(opaque);
1565 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001566 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001567
1568 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1569
1570 if (bad_page == NULL) {
1571 r = -ENOMEM;
1572 goto out;
1573 }
1574
Anthony Liguori35149e22008-04-02 14:46:56 -05001575 bad_pfn = page_to_pfn(bad_page);
1576
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001577 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001579 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580
Yang, Sheng002c7f72007-07-31 14:23:01 +03001581 for_each_online_cpu(cpu) {
1582 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001583 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02001584 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001585 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001586 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001587 }
1588
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001589 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001590 r = register_cpu_notifier(&kvm_cpu_notifier);
1591 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001592 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 register_reboot_notifier(&kvm_reboot_notifier);
1594
Avi Kivity59ae6c62007-02-12 00:54:48 -08001595 r = sysdev_class_register(&kvm_sysdev_class);
1596 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001597 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001598
1599 r = sysdev_register(&kvm_sysdev);
1600 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001601 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001602
Rusty Russellc16f8622007-07-30 21:12:19 +10001603 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1604 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001605 __alignof__(struct kvm_vcpu),
1606 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001607 if (!kvm_vcpu_cache) {
1608 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001609 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001610 }
1611
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612 kvm_chardev_ops.owner = module;
1613
1614 r = misc_register(&kvm_dev);
1615 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001616 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617 goto out_free;
1618 }
1619
Avi Kivity15ad7142007-07-11 18:17:21 +03001620 kvm_preempt_ops.sched_in = kvm_sched_in;
1621 kvm_preempt_ops.sched_out = kvm_sched_out;
1622
Avi Kivityc7addb92007-09-16 18:58:32 +02001623 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624
1625out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001626 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001627out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001628 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001629out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001630 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001631out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001633 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001634out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001635 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001636out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001637 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001638out_free_0:
1639 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001640out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001641 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001642 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001643out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 return r;
1645}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001646EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001648void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001650 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001652 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001653 sysdev_unregister(&kvm_sysdev);
1654 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001656 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001657 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001658 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001659 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001661 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001663EXPORT_SYMBOL_GPL(kvm_exit);