blob: d5911d9895c3d6867d8e289dc520b8a6a6a52c62 [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
50MODULE_AUTHOR("Qumranet");
51MODULE_LICENSE("GPL");
52
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080053DEFINE_SPINLOCK(kvm_lock);
54LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080055
Avi Kivity1b6c0162007-05-24 13:03:52 +030056static cpumask_t cpus_hardware_enabled;
57
Rusty Russellc16f8622007-07-30 21:12:19 +100058struct kmem_cache *kvm_vcpu_cache;
59EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030060
Avi Kivity15ad7142007-07-11 18:17:21 +030061static __read_mostly struct preempt_ops kvm_preempt_ops;
62
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -040063struct dentry *debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080064
Avi Kivitybccf2152007-02-21 18:04:26 +020065static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
66 unsigned long arg);
67
James Morris5aacf0c2006-12-22 01:04:55 -080068static inline int valid_vcpu(int n)
69{
70 return likely(n >= 0 && n < KVM_MAX_VCPUS);
71}
72
Avi Kivity6aa8b732006-12-10 02:21:36 -080073/*
74 * Switches to specified vcpu, until a matching vcpu_put()
75 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020076void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077{
Avi Kivity15ad7142007-07-11 18:17:21 +030078 int cpu;
79
Avi Kivitybccf2152007-02-21 18:04:26 +020080 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030081 cpu = get_cpu();
82 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020083 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030084 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020085}
86
Carsten Otte313a3dc2007-10-11 19:16:52 +020087void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080088{
Avi Kivity15ad7142007-07-11 18:17:21 +030089 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020090 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030091 preempt_notifier_unregister(&vcpu->preempt_notifier);
92 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080093 mutex_unlock(&vcpu->mutex);
94}
95
Avi Kivityd9e368d2007-06-07 19:18:30 +030096static void ack_flush(void *_completed)
97{
Avi Kivityd9e368d2007-06-07 19:18:30 +030098}
99
100void kvm_flush_remote_tlbs(struct kvm *kvm)
101{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200102 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300103 cpumask_t cpus;
104 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300105
Avi Kivityd9e368d2007-06-07 19:18:30 +0300106 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000107 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
108 vcpu = kvm->vcpus[i];
109 if (!vcpu)
110 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200111 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300112 continue;
113 cpu = vcpu->cpu;
114 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200115 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300116 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200117 if (cpus_empty(cpus))
118 return;
119 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200120 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300121}
122
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500123void kvm_reload_remote_mmus(struct kvm *kvm)
124{
125 int i, cpu;
126 cpumask_t cpus;
127 struct kvm_vcpu *vcpu;
128
129 cpus_clear(cpus);
130 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
131 vcpu = kvm->vcpus[i];
132 if (!vcpu)
133 continue;
134 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
135 continue;
136 cpu = vcpu->cpu;
137 if (cpu != -1 && cpu != raw_smp_processor_id())
138 cpu_set(cpu, cpus);
139 }
140 if (cpus_empty(cpus))
141 return;
142 smp_call_function_mask(cpus, ack_flush, NULL, 1);
143}
144
145
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000146int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
147{
148 struct page *page;
149 int r;
150
151 mutex_init(&vcpu->mutex);
152 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000153 vcpu->kvm = kvm;
154 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300155 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000156
157 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
158 if (!page) {
159 r = -ENOMEM;
160 goto fail;
161 }
162 vcpu->run = page_address(page);
163
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800164 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000165 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800166 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000167 return 0;
168
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000169fail_free_run:
170 free_page((unsigned long)vcpu->run);
171fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000172 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000173}
174EXPORT_SYMBOL_GPL(kvm_vcpu_init);
175
176void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
177{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800178 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000179 free_page((unsigned long)vcpu->run);
180}
181EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
182
Avi Kivityf17abe92007-02-21 19:28:04 +0200183static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800185 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800187 if (IS_ERR(kvm))
188 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200190 kvm->mm = current->mm;
191 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500192 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300193 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800194 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400195 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200196 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300197 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000198 spin_lock(&kvm_lock);
199 list_add(&kvm->vm_list, &vm_list);
200 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800201out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200202 return kvm;
203}
204
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205/*
206 * Free any memory in @free but not in @dont.
207 */
208static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
209 struct kvm_memory_slot *dont)
210{
Izik Eidus290fc382007-09-27 14:11:22 +0200211 if (!dont || free->rmap != dont->rmap)
212 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213
214 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
215 vfree(free->dirty_bitmap);
216
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300217 if (!dont || free->lpage_info != dont->lpage_info)
218 vfree(free->lpage_info);
219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000221 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500222 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300223 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224}
225
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800226void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227{
228 int i;
229
230 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000231 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232}
233
Avi Kivityf17abe92007-02-21 19:28:04 +0200234static void kvm_destroy_vm(struct kvm *kvm)
235{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200236 struct mm_struct *mm = kvm->mm;
237
Avi Kivity133de902007-02-12 00:54:44 -0800238 spin_lock(&kvm_lock);
239 list_del(&kvm->vm_list);
240 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300241 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400242 kvm_io_bus_destroy(&kvm->mmio_bus);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800243 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200244 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200245}
246
Izik Eidusd39f13b2008-03-30 16:01:25 +0300247void kvm_get_kvm(struct kvm *kvm)
248{
249 atomic_inc(&kvm->users_count);
250}
251EXPORT_SYMBOL_GPL(kvm_get_kvm);
252
253void kvm_put_kvm(struct kvm *kvm)
254{
255 if (atomic_dec_and_test(&kvm->users_count))
256 kvm_destroy_vm(kvm);
257}
258EXPORT_SYMBOL_GPL(kvm_put_kvm);
259
260
Avi Kivityf17abe92007-02-21 19:28:04 +0200261static int kvm_vm_release(struct inode *inode, struct file *filp)
262{
263 struct kvm *kvm = filp->private_data;
264
Izik Eidusd39f13b2008-03-30 16:01:25 +0300265 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266 return 0;
267}
268
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270 * Allocate some memory and give it an address in the guest physical address
271 * space.
272 *
273 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800274 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500275 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800277int __kvm_set_memory_region(struct kvm *kvm,
278 struct kvm_userspace_memory_region *mem,
279 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280{
281 int r;
282 gfn_t base_gfn;
283 unsigned long npages;
284 unsigned long i;
285 struct kvm_memory_slot *memslot;
286 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287
288 r = -EINVAL;
289 /* General sanity checks */
290 if (mem->memory_size & (PAGE_SIZE - 1))
291 goto out;
292 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
293 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200294 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 goto out;
296 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
297 goto out;
298
299 memslot = &kvm->memslots[mem->slot];
300 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
301 npages = mem->memory_size >> PAGE_SHIFT;
302
303 if (!npages)
304 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306 new = old = *memslot;
307
308 new.base_gfn = base_gfn;
309 new.npages = npages;
310 new.flags = mem->flags;
311
312 /* Disallow changing a memory slot's size. */
313 r = -EINVAL;
314 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800315 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316
317 /* Check for overlaps */
318 r = -EEXIST;
319 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
320 struct kvm_memory_slot *s = &kvm->memslots[i];
321
322 if (s == memslot)
323 continue;
324 if (!((base_gfn + npages <= s->base_gfn) ||
325 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800326 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329 /* Free page dirty bitmap if unneeded */
330 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000331 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332
333 r = -ENOMEM;
334
335 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500336 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400337 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200338
339 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800340 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200341
Izik Eidus290fc382007-09-27 14:11:22 +0200342 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500343
Izik Eidus80b14b52007-10-25 11:54:04 +0200344 new.user_alloc = user_alloc;
Zhang Xiantao0de10342007-11-20 16:25:04 +0800345 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300347 if (npages && !new.lpage_info) {
348 int largepages = npages / KVM_PAGES_PER_HPAGE;
349 if (npages % KVM_PAGES_PER_HPAGE)
350 largepages++;
351 if (base_gfn % KVM_PAGES_PER_HPAGE)
352 largepages++;
353
354 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
355
356 if (!new.lpage_info)
357 goto out_free;
358
359 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
360
361 if (base_gfn % KVM_PAGES_PER_HPAGE)
362 new.lpage_info[0].write_count = 1;
363 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
364 new.lpage_info[largepages-1].write_count = 1;
365 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366
367 /* Allocate page dirty bitmap if needed */
368 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
369 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
370
371 new.dirty_bitmap = vmalloc(dirty_bytes);
372 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800373 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374 memset(new.dirty_bitmap, 0, dirty_bytes);
375 }
376
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 if (mem->slot >= kvm->nmemslots)
378 kvm->nmemslots = mem->slot + 1;
379
380 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381
Zhang Xiantao0de10342007-11-20 16:25:04 +0800382 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
383 if (r) {
384 *memslot = old;
385 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800386 }
387
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 kvm_free_physmem_slot(&old, &new);
389 return 0;
390
Sheng Yangf78e0e22007-10-29 09:40:42 +0800391out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392 kvm_free_physmem_slot(&new, &old);
393out:
394 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200395
396}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800397EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
398
399int kvm_set_memory_region(struct kvm *kvm,
400 struct kvm_userspace_memory_region *mem,
401 int user_alloc)
402{
403 int r;
404
Izik Eidus72dc67a2008-02-10 18:04:15 +0200405 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800406 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200407 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800408 return r;
409}
Izik Eidus210c7c42007-10-24 23:52:57 +0200410EXPORT_SYMBOL_GPL(kvm_set_memory_region);
411
Carsten Otte1fe779f2007-10-29 16:08:35 +0100412int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
413 struct
414 kvm_userspace_memory_region *mem,
415 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200416{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200417 if (mem->slot >= KVM_MEMORY_SLOTS)
418 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200419 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800422int kvm_get_dirty_log(struct kvm *kvm,
423 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424{
425 struct kvm_memory_slot *memslot;
426 int r, i;
427 int n;
428 unsigned long any = 0;
429
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 r = -EINVAL;
431 if (log->slot >= KVM_MEMORY_SLOTS)
432 goto out;
433
434 memslot = &kvm->memslots[log->slot];
435 r = -ENOENT;
436 if (!memslot->dirty_bitmap)
437 goto out;
438
Uri Lublincd1a4a92007-02-22 16:43:09 +0200439 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440
Uri Lublincd1a4a92007-02-22 16:43:09 +0200441 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 any = memslot->dirty_bitmap[i];
443
444 r = -EFAULT;
445 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
446 goto out;
447
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800448 if (any)
449 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450
451 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453 return r;
454}
455
Izik Eiduscea7bb22007-10-17 19:17:48 +0200456int is_error_page(struct page *page)
457{
458 return page == bad_page;
459}
460EXPORT_SYMBOL_GPL(is_error_page);
461
Anthony Liguori35149e22008-04-02 14:46:56 -0500462int is_error_pfn(pfn_t pfn)
463{
464 return pfn == bad_pfn;
465}
466EXPORT_SYMBOL_GPL(is_error_pfn);
467
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200468static inline unsigned long bad_hva(void)
469{
470 return PAGE_OFFSET;
471}
472
473int kvm_is_error_hva(unsigned long addr)
474{
475 return addr == bad_hva();
476}
477EXPORT_SYMBOL_GPL(kvm_is_error_hva);
478
Avi Kivitye8207542007-03-30 16:54:30 +0300479static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480{
481 int i;
482
483 for (i = 0; i < kvm->nmemslots; ++i) {
484 struct kvm_memory_slot *memslot = &kvm->memslots[i];
485
486 if (gfn >= memslot->base_gfn
487 && gfn < memslot->base_gfn + memslot->npages)
488 return memslot;
489 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000490 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491}
Avi Kivitye8207542007-03-30 16:54:30 +0300492
493struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
494{
495 gfn = unalias_gfn(kvm, gfn);
496 return __gfn_to_memslot(kvm, gfn);
497}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498
Izik Eiduse0d62c72007-10-24 23:57:46 +0200499int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
500{
501 int i;
502
503 gfn = unalias_gfn(kvm, gfn);
504 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
505 struct kvm_memory_slot *memslot = &kvm->memslots[i];
506
507 if (gfn >= memslot->base_gfn
508 && gfn < memslot->base_gfn + memslot->npages)
509 return 1;
510 }
511 return 0;
512}
513EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
514
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300515unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200516{
517 struct kvm_memory_slot *slot;
518
519 gfn = unalias_gfn(kvm, gfn);
520 slot = __gfn_to_memslot(kvm, gfn);
521 if (!slot)
522 return bad_hva();
523 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
524}
525
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500526/*
527 * Requires current->mm->mmap_sem to be held
528 */
Anthony Liguori35149e22008-04-02 14:46:56 -0500529pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300530{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500531 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200532 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500533 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300534
Avi Kivity60395222007-10-21 11:03:36 +0200535 might_sleep();
536
Izik Eidus539cb662007-11-11 22:05:04 +0200537 addr = gfn_to_hva(kvm, gfn);
538 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200539 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500540 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200541 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200542
Izik Eidus539cb662007-11-11 22:05:04 +0200543 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
544 NULL);
545
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500546 if (npages != 1) {
547 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500548 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200549 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500550
Anthony Liguori35149e22008-04-02 14:46:56 -0500551 return page_to_pfn(page[0]);
552}
553
554EXPORT_SYMBOL_GPL(gfn_to_pfn);
555
556struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
557{
558 return pfn_to_page(gfn_to_pfn(kvm, gfn));
Avi Kivity954bbbc2007-03-30 14:02:32 +0300559}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500560
Avi Kivity954bbbc2007-03-30 14:02:32 +0300561EXPORT_SYMBOL_GPL(gfn_to_page);
562
Izik Eidusb4231d62007-11-20 11:49:33 +0200563void kvm_release_page_clean(struct page *page)
564{
Anthony Liguori35149e22008-04-02 14:46:56 -0500565 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200566}
567EXPORT_SYMBOL_GPL(kvm_release_page_clean);
568
Anthony Liguori35149e22008-04-02 14:46:56 -0500569void kvm_release_pfn_clean(pfn_t pfn)
570{
571 put_page(pfn_to_page(pfn));
572}
573EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
574
Izik Eidusb4231d62007-11-20 11:49:33 +0200575void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200576{
Anthony Liguori35149e22008-04-02 14:46:56 -0500577 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200578}
Izik Eidusb4231d62007-11-20 11:49:33 +0200579EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200580
Anthony Liguori35149e22008-04-02 14:46:56 -0500581void kvm_release_pfn_dirty(pfn_t pfn)
582{
583 kvm_set_pfn_dirty(pfn);
584 kvm_release_pfn_clean(pfn);
585}
586EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
587
588void kvm_set_page_dirty(struct page *page)
589{
590 kvm_set_pfn_dirty(page_to_pfn(page));
591}
592EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
593
594void kvm_set_pfn_dirty(pfn_t pfn)
595{
596 struct page *page = pfn_to_page(pfn);
597 if (!PageReserved(page))
598 SetPageDirty(page);
599}
600EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
601
602void kvm_set_pfn_accessed(pfn_t pfn)
603{
604 mark_page_accessed(pfn_to_page(pfn));
605}
606EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
607
608void kvm_get_pfn(pfn_t pfn)
609{
610 get_page(pfn_to_page(pfn));
611}
612EXPORT_SYMBOL_GPL(kvm_get_pfn);
613
Izik Eidus195aefd2007-10-01 22:14:18 +0200614static int next_segment(unsigned long len, int offset)
615{
616 if (len > PAGE_SIZE - offset)
617 return PAGE_SIZE - offset;
618 else
619 return len;
620}
621
622int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
623 int len)
624{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200625 int r;
626 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200627
Izik Eiduse0506bc2007-11-11 22:10:22 +0200628 addr = gfn_to_hva(kvm, gfn);
629 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200630 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200631 r = copy_from_user(data, (void __user *)addr + offset, len);
632 if (r)
633 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200634 return 0;
635}
636EXPORT_SYMBOL_GPL(kvm_read_guest_page);
637
638int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
639{
640 gfn_t gfn = gpa >> PAGE_SHIFT;
641 int seg;
642 int offset = offset_in_page(gpa);
643 int ret;
644
645 while ((seg = next_segment(len, offset)) != 0) {
646 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
647 if (ret < 0)
648 return ret;
649 offset = 0;
650 len -= seg;
651 data += seg;
652 ++gfn;
653 }
654 return 0;
655}
656EXPORT_SYMBOL_GPL(kvm_read_guest);
657
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500658int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
659 unsigned long len)
660{
661 int r;
662 unsigned long addr;
663 gfn_t gfn = gpa >> PAGE_SHIFT;
664 int offset = offset_in_page(gpa);
665
666 addr = gfn_to_hva(kvm, gfn);
667 if (kvm_is_error_hva(addr))
668 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100669 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500670 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100671 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500672 if (r)
673 return -EFAULT;
674 return 0;
675}
676EXPORT_SYMBOL(kvm_read_guest_atomic);
677
Izik Eidus195aefd2007-10-01 22:14:18 +0200678int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
679 int offset, int len)
680{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200681 int r;
682 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200683
Izik Eiduse0506bc2007-11-11 22:10:22 +0200684 addr = gfn_to_hva(kvm, gfn);
685 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200686 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200687 r = copy_to_user((void __user *)addr + offset, data, len);
688 if (r)
689 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200690 mark_page_dirty(kvm, gfn);
691 return 0;
692}
693EXPORT_SYMBOL_GPL(kvm_write_guest_page);
694
695int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
696 unsigned long len)
697{
698 gfn_t gfn = gpa >> PAGE_SHIFT;
699 int seg;
700 int offset = offset_in_page(gpa);
701 int ret;
702
703 while ((seg = next_segment(len, offset)) != 0) {
704 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
705 if (ret < 0)
706 return ret;
707 offset = 0;
708 len -= seg;
709 data += seg;
710 ++gfn;
711 }
712 return 0;
713}
714
715int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
716{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200717 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200718}
719EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
720
721int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
722{
723 gfn_t gfn = gpa >> PAGE_SHIFT;
724 int seg;
725 int offset = offset_in_page(gpa);
726 int ret;
727
728 while ((seg = next_segment(len, offset)) != 0) {
729 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
730 if (ret < 0)
731 return ret;
732 offset = 0;
733 len -= seg;
734 ++gfn;
735 }
736 return 0;
737}
738EXPORT_SYMBOL_GPL(kvm_clear_guest);
739
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
741{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300742 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743
Uri Lublin3b6fff12007-10-30 10:42:09 +0200744 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000745 memslot = __gfn_to_memslot(kvm, gfn);
746 if (memslot && memslot->dirty_bitmap) {
747 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748
Rusty Russell7e9d6192007-07-31 20:41:14 +1000749 /* avoid RMW */
750 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
751 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 }
753}
754
Eddie Dongb6958ce2007-07-18 12:15:21 +0300755/*
756 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
757 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500758void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300759{
760 DECLARE_WAITQUEUE(wait, current);
761
762 add_wait_queue(&vcpu->wq, &wait);
763
764 /*
765 * We will block until either an interrupt or a signal wakes us up
766 */
He, Qingc5ec1532007-09-03 17:07:41 +0300767 while (!kvm_cpu_has_interrupt(vcpu)
768 && !signal_pending(current)
Hollis Blanchard53e0aa72007-12-03 16:15:26 -0600769 && !kvm_arch_vcpu_runnable(vcpu)) {
Eddie Dongb6958ce2007-07-18 12:15:21 +0300770 set_current_state(TASK_INTERRUPTIBLE);
771 vcpu_put(vcpu);
772 schedule();
773 vcpu_load(vcpu);
774 }
775
He, Qingc5ec1532007-09-03 17:07:41 +0300776 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300777 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300778}
779
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780void kvm_resched(struct kvm_vcpu *vcpu)
781{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300782 if (!need_resched())
783 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785}
786EXPORT_SYMBOL_GPL(kvm_resched);
787
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100788static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200789{
790 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200791 struct page *page;
792
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100793 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +0200794 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +0200795#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100796 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800797 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +0200798#endif
Avi Kivity039576c2007-03-20 12:46:50 +0200799 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100800 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200801 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100802 vmf->page = page;
803 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200804}
805
806static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +1100807 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200808};
809
810static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
811{
812 vma->vm_ops = &kvm_vcpu_vm_ops;
813 return 0;
814}
815
Avi Kivitybccf2152007-02-21 18:04:26 +0200816static int kvm_vcpu_release(struct inode *inode, struct file *filp)
817{
818 struct kvm_vcpu *vcpu = filp->private_data;
819
820 fput(vcpu->kvm->filp);
821 return 0;
822}
823
Jan Engelhardt5c502742008-01-22 20:46:14 +0100824static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +0200825 .release = kvm_vcpu_release,
826 .unlocked_ioctl = kvm_vcpu_ioctl,
827 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200828 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +0200829};
830
831/*
832 * Allocates an inode for the vcpu.
833 */
834static int create_vcpu_fd(struct kvm_vcpu *vcpu)
835{
836 int fd, r;
837 struct inode *inode;
838 struct file *file;
839
Avi Kivityd6d28162007-06-28 08:38:16 -0400840 r = anon_inode_getfd(&fd, &inode, &file,
841 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
842 if (r)
843 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +0200844 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +0200845 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +0200846}
847
Avi Kivityc5ea7662007-02-20 18:41:05 +0200848/*
849 * Creates some virtual cpus. Good luck creating more than one.
850 */
851static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
852{
853 int r;
854 struct kvm_vcpu *vcpu;
855
Avi Kivityc5ea7662007-02-20 18:41:05 +0200856 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000857 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200858
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800859 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000860 if (IS_ERR(vcpu))
861 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200862
Avi Kivity15ad7142007-07-11 18:17:21 +0300863 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
864
Avi Kivity26e52152007-11-20 15:30:24 +0200865 r = kvm_arch_vcpu_setup(vcpu);
866 if (r)
867 goto vcpu_destroy;
868
Shaohua Li11ec2802007-07-23 14:51:37 +0800869 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000870 if (kvm->vcpus[n]) {
871 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +0800872 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800873 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000874 }
875 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +0800876 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000877
878 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +0200879 r = create_vcpu_fd(vcpu);
880 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000881 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +0200882 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +0200883
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000884unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +0800885 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000886 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +0800887 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800888vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -0600889 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +0200890 return r;
891}
892
Avi Kivity1961d272007-03-05 19:46:05 +0200893static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
894{
895 if (sigset) {
896 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
897 vcpu->sigset_active = 1;
898 vcpu->sigset = *sigset;
899 } else
900 vcpu->sigset_active = 0;
901 return 0;
902}
903
Avi Kivitybccf2152007-02-21 18:04:26 +0200904static long kvm_vcpu_ioctl(struct file *filp,
905 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906{
Avi Kivitybccf2152007-02-21 18:04:26 +0200907 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +0000908 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200909 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200911 if (vcpu->kvm->mm != current->mm)
912 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200914 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +0200915 r = -EINVAL;
916 if (arg)
917 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500918 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800921 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800923 r = -ENOMEM;
924 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
925 if (!kvm_regs)
926 goto out;
927 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800929 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800931 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
932 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800934out_free1:
935 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 break;
937 }
938 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800939 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800941 r = -ENOMEM;
942 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
943 if (!kvm_regs)
944 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800946 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
947 goto out_free2;
948 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800950 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +0800952out_free2:
953 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 break;
955 }
956 case KVM_GET_SREGS: {
957 struct kvm_sregs kvm_sregs;
958
Avi Kivitybccf2152007-02-21 18:04:26 +0200959 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500960 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 if (r)
962 goto out;
963 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000964 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 goto out;
966 r = 0;
967 break;
968 }
969 case KVM_SET_SREGS: {
970 struct kvm_sregs kvm_sregs;
971
972 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000973 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -0500975 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 if (r)
977 goto out;
978 r = 0;
979 break;
980 }
981 case KVM_TRANSLATE: {
982 struct kvm_translation tr;
983
984 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000985 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +0800987 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 if (r)
989 goto out;
990 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +0000991 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 goto out;
993 r = 0;
994 break;
995 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 case KVM_DEBUG_GUEST: {
997 struct kvm_debug_guest dbg;
998
999 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001000 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001002 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 if (r)
1004 goto out;
1005 r = 0;
1006 break;
1007 }
Avi Kivity1961d272007-03-05 19:46:05 +02001008 case KVM_SET_SIGNAL_MASK: {
1009 struct kvm_signal_mask __user *sigmask_arg = argp;
1010 struct kvm_signal_mask kvm_sigmask;
1011 sigset_t sigset, *p;
1012
1013 p = NULL;
1014 if (argp) {
1015 r = -EFAULT;
1016 if (copy_from_user(&kvm_sigmask, argp,
1017 sizeof kvm_sigmask))
1018 goto out;
1019 r = -EINVAL;
1020 if (kvm_sigmask.len != sizeof sigset)
1021 goto out;
1022 r = -EFAULT;
1023 if (copy_from_user(&sigset, sigmask_arg->sigset,
1024 sizeof sigset))
1025 goto out;
1026 p = &sigset;
1027 }
1028 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1029 break;
1030 }
Avi Kivityb8836732007-04-01 16:34:31 +03001031 case KVM_GET_FPU: {
1032 struct kvm_fpu fpu;
1033
1034 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05001035 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001036 if (r)
1037 goto out;
1038 r = -EFAULT;
1039 if (copy_to_user(argp, &fpu, sizeof fpu))
1040 goto out;
1041 r = 0;
1042 break;
1043 }
1044 case KVM_SET_FPU: {
1045 struct kvm_fpu fpu;
1046
1047 r = -EFAULT;
1048 if (copy_from_user(&fpu, argp, sizeof fpu))
1049 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -05001050 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001051 if (r)
1052 goto out;
1053 r = 0;
1054 break;
1055 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001056 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001057 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001058 }
1059out:
1060 return r;
1061}
1062
1063static long kvm_vm_ioctl(struct file *filp,
1064 unsigned int ioctl, unsigned long arg)
1065{
1066 struct kvm *kvm = filp->private_data;
1067 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001068 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001069
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001070 if (kvm->mm != current->mm)
1071 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001072 switch (ioctl) {
1073 case KVM_CREATE_VCPU:
1074 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1075 if (r < 0)
1076 goto out;
1077 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001078 case KVM_SET_USER_MEMORY_REGION: {
1079 struct kvm_userspace_memory_region kvm_userspace_mem;
1080
1081 r = -EFAULT;
1082 if (copy_from_user(&kvm_userspace_mem, argp,
1083 sizeof kvm_userspace_mem))
1084 goto out;
1085
1086 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 if (r)
1088 goto out;
1089 break;
1090 }
1091 case KVM_GET_DIRTY_LOG: {
1092 struct kvm_dirty_log log;
1093
1094 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001095 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001097 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 if (r)
1099 goto out;
1100 break;
1101 }
Avi Kivityf17abe92007-02-21 19:28:04 +02001102 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001103 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001104 }
1105out:
1106 return r;
1107}
1108
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001109static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001110{
1111 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001112 struct page *page;
1113
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001114 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1115 return VM_FAULT_SIGBUS;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001116 page = gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001117 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001118 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001119 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001120 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001121 vmf->page = page;
1122 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001123}
1124
1125static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001126 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001127};
1128
1129static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1130{
1131 vma->vm_ops = &kvm_vm_vm_ops;
1132 return 0;
1133}
1134
Jan Engelhardt5c502742008-01-22 20:46:14 +01001135static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001136 .release = kvm_vm_release,
1137 .unlocked_ioctl = kvm_vm_ioctl,
1138 .compat_ioctl = kvm_vm_ioctl,
1139 .mmap = kvm_vm_mmap,
1140};
1141
1142static int kvm_dev_ioctl_create_vm(void)
1143{
1144 int fd, r;
1145 struct inode *inode;
1146 struct file *file;
1147 struct kvm *kvm;
1148
Avi Kivityf17abe92007-02-21 19:28:04 +02001149 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001150 if (IS_ERR(kvm))
1151 return PTR_ERR(kvm);
1152 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1153 if (r) {
1154 kvm_destroy_vm(kvm);
1155 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001156 }
1157
Avi Kivitybccf2152007-02-21 18:04:26 +02001158 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02001159
Avi Kivityf17abe92007-02-21 19:28:04 +02001160 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001161}
1162
1163static long kvm_dev_ioctl(struct file *filp,
1164 unsigned int ioctl, unsigned long arg)
1165{
1166 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02001167 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001168
1169 switch (ioctl) {
1170 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001171 r = -EINVAL;
1172 if (arg)
1173 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001174 r = KVM_API_VERSION;
1175 break;
1176 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001177 r = -EINVAL;
1178 if (arg)
1179 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001180 r = kvm_dev_ioctl_create_vm();
1181 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001182 case KVM_CHECK_EXTENSION:
1183 r = kvm_dev_ioctl_check_extension((long)argp);
Avi Kivity5d308f42007-03-01 17:56:20 +02001184 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001185 case KVM_GET_VCPU_MMAP_SIZE:
1186 r = -EINVAL;
1187 if (arg)
1188 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001189 r = PAGE_SIZE; /* struct kvm_run */
1190#ifdef CONFIG_X86
1191 r += PAGE_SIZE; /* pio data page */
1192#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001193 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001194 case KVM_TRACE_ENABLE:
1195 case KVM_TRACE_PAUSE:
1196 case KVM_TRACE_DISABLE:
1197 r = kvm_trace_ioctl(ioctl, arg);
1198 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001200 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201 }
1202out:
1203 return r;
1204}
1205
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207 .unlocked_ioctl = kvm_dev_ioctl,
1208 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209};
1210
1211static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001212 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 "kvm",
1214 &kvm_chardev_ops,
1215};
1216
Avi Kivity1b6c0162007-05-24 13:03:52 +03001217static void hardware_enable(void *junk)
1218{
1219 int cpu = raw_smp_processor_id();
1220
1221 if (cpu_isset(cpu, cpus_hardware_enabled))
1222 return;
1223 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001224 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001225}
1226
1227static void hardware_disable(void *junk)
1228{
1229 int cpu = raw_smp_processor_id();
1230
1231 if (!cpu_isset(cpu, cpus_hardware_enabled))
1232 return;
1233 cpu_clear(cpu, cpus_hardware_enabled);
1234 decache_vcpus_on_cpu(cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001235 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001236}
1237
Avi Kivity774c47f2007-02-12 00:54:47 -08001238static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1239 void *v)
1240{
1241 int cpu = (long)v;
1242
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001243 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001244 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001245 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001246 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1247 cpu);
1248 hardware_disable(NULL);
1249 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001250 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001251 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1252 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001253 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001254 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001255 case CPU_ONLINE:
1256 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1257 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001258 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001259 break;
1260 }
1261 return NOTIFY_OK;
1262}
1263
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001264static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001265 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001266{
1267 if (val == SYS_RESTART) {
1268 /*
1269 * Some (well, at least mine) BIOSes hang on reboot if
1270 * in vmx root mode.
1271 */
1272 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1273 on_each_cpu(hardware_disable, NULL, 0, 1);
1274 }
1275 return NOTIFY_OK;
1276}
1277
1278static struct notifier_block kvm_reboot_notifier = {
1279 .notifier_call = kvm_reboot,
1280 .priority = 0,
1281};
1282
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001283void kvm_io_bus_init(struct kvm_io_bus *bus)
1284{
1285 memset(bus, 0, sizeof(*bus));
1286}
1287
1288void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1289{
1290 int i;
1291
1292 for (i = 0; i < bus->dev_count; i++) {
1293 struct kvm_io_device *pos = bus->devs[i];
1294
1295 kvm_iodevice_destructor(pos);
1296 }
1297}
1298
1299struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
1300{
1301 int i;
1302
1303 for (i = 0; i < bus->dev_count; i++) {
1304 struct kvm_io_device *pos = bus->devs[i];
1305
1306 if (pos->in_range(pos, addr))
1307 return pos;
1308 }
1309
1310 return NULL;
1311}
1312
1313void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1314{
1315 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1316
1317 bus->devs[bus->dev_count++] = dev;
1318}
1319
Avi Kivity774c47f2007-02-12 00:54:47 -08001320static struct notifier_block kvm_cpu_notifier = {
1321 .notifier_call = kvm_cpu_hotplug,
1322 .priority = 20, /* must be > scheduler priority */
1323};
1324
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001325static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001326{
1327 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001328 struct kvm *kvm;
1329
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001330 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001331 spin_lock(&kvm_lock);
1332 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001333 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001334 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001335 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001336}
1337
1338DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1339
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001340static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001341{
1342 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001343 struct kvm *kvm;
1344 struct kvm_vcpu *vcpu;
1345 int i;
1346
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001347 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001348 spin_lock(&kvm_lock);
1349 list_for_each_entry(kvm, &vm_list, vm_list)
1350 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001351 vcpu = kvm->vcpus[i];
1352 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001353 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001354 }
1355 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001356 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001357}
1358
Avi Kivityba1389b2007-11-18 16:24:12 +02001359DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1360
1361static struct file_operations *stat_fops[] = {
1362 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1363 [KVM_STAT_VM] = &vm_stat_fops,
1364};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001365
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001366static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367{
1368 struct kvm_stats_debugfs_item *p;
1369
Al Viro8b6d44c2007-02-09 16:38:40 +00001370 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001372 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
1373 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001374 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375}
1376
1377static void kvm_exit_debug(void)
1378{
1379 struct kvm_stats_debugfs_item *p;
1380
1381 for (p = debugfs_entries; p->name; ++p)
1382 debugfs_remove(p->dentry);
1383 debugfs_remove(debugfs_dir);
1384}
1385
Avi Kivity59ae6c62007-02-12 00:54:48 -08001386static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1387{
Avi Kivity4267c412007-05-24 13:09:41 +03001388 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001389 return 0;
1390}
1391
1392static int kvm_resume(struct sys_device *dev)
1393{
Avi Kivity4267c412007-05-24 13:09:41 +03001394 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001395 return 0;
1396}
1397
1398static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001399 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001400 .suspend = kvm_suspend,
1401 .resume = kvm_resume,
1402};
1403
1404static struct sys_device kvm_sysdev = {
1405 .id = 0,
1406 .cls = &kvm_sysdev_class,
1407};
1408
Izik Eiduscea7bb22007-10-17 19:17:48 +02001409struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001410pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
Avi Kivity15ad7142007-07-11 18:17:21 +03001412static inline
1413struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1414{
1415 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1416}
1417
1418static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1419{
1420 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1421
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001422 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001423}
1424
1425static void kvm_sched_out(struct preempt_notifier *pn,
1426 struct task_struct *next)
1427{
1428 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1429
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001430 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001431}
1432
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001433int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001434 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435{
1436 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001437 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001439 kvm_init_debug();
1440
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001441 r = kvm_arch_init(opaque);
1442 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001443 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001444
1445 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1446
1447 if (bad_page == NULL) {
1448 r = -ENOMEM;
1449 goto out;
1450 }
1451
Anthony Liguori35149e22008-04-02 14:46:56 -05001452 bad_pfn = page_to_pfn(bad_page);
1453
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001454 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001456 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457
Yang, Sheng002c7f72007-07-31 14:23:01 +03001458 for_each_online_cpu(cpu) {
1459 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001460 kvm_arch_check_processor_compat,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001461 &r, 0, 1);
1462 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001463 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001464 }
1465
Avi Kivity1b6c0162007-05-24 13:03:52 +03001466 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001467 r = register_cpu_notifier(&kvm_cpu_notifier);
1468 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001469 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 register_reboot_notifier(&kvm_reboot_notifier);
1471
Avi Kivity59ae6c62007-02-12 00:54:48 -08001472 r = sysdev_class_register(&kvm_sysdev_class);
1473 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001474 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001475
1476 r = sysdev_register(&kvm_sysdev);
1477 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001478 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001479
Rusty Russellc16f8622007-07-30 21:12:19 +10001480 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1481 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001482 __alignof__(struct kvm_vcpu),
1483 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001484 if (!kvm_vcpu_cache) {
1485 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001486 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001487 }
1488
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 kvm_chardev_ops.owner = module;
1490
1491 r = misc_register(&kvm_dev);
1492 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001493 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 goto out_free;
1495 }
1496
Avi Kivity15ad7142007-07-11 18:17:21 +03001497 kvm_preempt_ops.sched_in = kvm_sched_in;
1498 kvm_preempt_ops.sched_out = kvm_sched_out;
1499
Avi Kivityc7addb92007-09-16 18:58:32 +02001500 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501
1502out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001503 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001504out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001505 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001506out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001507 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001508out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001510 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001511out_free_2:
Avi Kivity1b6c0162007-05-24 13:03:52 +03001512 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001513out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001514 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001515out_free_0:
1516 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001517out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001518 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001519 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001520out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 return r;
1522}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001523EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001525void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001527 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001529 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001530 sysdev_unregister(&kvm_sysdev);
1531 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001533 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001534 on_each_cpu(hardware_disable, NULL, 0, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001535 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001536 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001538 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001540EXPORT_SYMBOL_GPL(kvm_exit);