blob: 5eb96c7c8d7a608f9968a086e6b6ab71bc16655a [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
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030079static inline int is_mmio_pfn(pfn_t pfn)
80{
81 if (pfn_valid(pfn))
82 return PageReserved(pfn_to_page(pfn));
83
84 return true;
85}
86
Avi Kivity6aa8b732006-12-10 02:21:36 -080087/*
88 * Switches to specified vcpu, until a matching vcpu_put()
89 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020090void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080091{
Avi Kivity15ad7142007-07-11 18:17:21 +030092 int cpu;
93
Avi Kivitybccf2152007-02-21 18:04:26 +020094 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030095 cpu = get_cpu();
96 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020097 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030098 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020099}
100
Carsten Otte313a3dc2007-10-11 19:16:52 +0200101void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102{
Avi Kivity15ad7142007-07-11 18:17:21 +0300103 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200104 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300105 preempt_notifier_unregister(&vcpu->preempt_notifier);
106 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107 mutex_unlock(&vcpu->mutex);
108}
109
Avi Kivityd9e368d2007-06-07 19:18:30 +0300110static void ack_flush(void *_completed)
111{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300112}
113
114void kvm_flush_remote_tlbs(struct kvm *kvm)
115{
Avi Kivity597a5f52008-07-20 14:24:22 +0300116 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300117 cpumask_t cpus;
118 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300119
Avi Kivity597a5f52008-07-20 14:24:22 +0300120 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300121 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000122 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
123 vcpu = kvm->vcpus[i];
124 if (!vcpu)
125 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200126 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300127 continue;
128 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300129 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200130 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300131 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200132 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300133 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200134 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200135 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300136out:
137 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300138}
139
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500140void kvm_reload_remote_mmus(struct kvm *kvm)
141{
Avi Kivity597a5f52008-07-20 14:24:22 +0300142 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500143 cpumask_t cpus;
144 struct kvm_vcpu *vcpu;
145
Avi Kivity597a5f52008-07-20 14:24:22 +0300146 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500147 cpus_clear(cpus);
148 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
149 vcpu = kvm->vcpus[i];
150 if (!vcpu)
151 continue;
152 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
153 continue;
154 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300155 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500156 cpu_set(cpu, cpus);
157 }
158 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300159 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500160 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300161out:
162 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500163}
164
165
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000166int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
167{
168 struct page *page;
169 int r;
170
171 mutex_init(&vcpu->mutex);
172 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000173 vcpu->kvm = kvm;
174 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300175 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000176
177 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
178 if (!page) {
179 r = -ENOMEM;
180 goto fail;
181 }
182 vcpu->run = page_address(page);
183
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800184 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000185 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800186 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000187 return 0;
188
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000189fail_free_run:
190 free_page((unsigned long)vcpu->run);
191fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000192 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000193}
194EXPORT_SYMBOL_GPL(kvm_vcpu_init);
195
196void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
197{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800198 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000199 free_page((unsigned long)vcpu->run);
200}
201EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
202
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200203#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
204static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
205{
206 return container_of(mn, struct kvm, mmu_notifier);
207}
208
209static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
210 struct mm_struct *mm,
211 unsigned long address)
212{
213 struct kvm *kvm = mmu_notifier_to_kvm(mn);
214 int need_tlb_flush;
215
216 /*
217 * When ->invalidate_page runs, the linux pte has been zapped
218 * already but the page is still allocated until
219 * ->invalidate_page returns. So if we increase the sequence
220 * here the kvm page fault will notice if the spte can't be
221 * established because the page is going to be freed. If
222 * instead the kvm page fault establishes the spte before
223 * ->invalidate_page runs, kvm_unmap_hva will release it
224 * before returning.
225 *
226 * The sequence increase only need to be seen at spin_unlock
227 * time, and not at spin_lock time.
228 *
229 * Increasing the sequence after the spin_unlock would be
230 * unsafe because the kvm page fault could then establish the
231 * pte after kvm_unmap_hva returned, without noticing the page
232 * is going to be freed.
233 */
234 spin_lock(&kvm->mmu_lock);
235 kvm->mmu_notifier_seq++;
236 need_tlb_flush = kvm_unmap_hva(kvm, address);
237 spin_unlock(&kvm->mmu_lock);
238
239 /* we've to flush the tlb before the pages can be freed */
240 if (need_tlb_flush)
241 kvm_flush_remote_tlbs(kvm);
242
243}
244
245static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
246 struct mm_struct *mm,
247 unsigned long start,
248 unsigned long end)
249{
250 struct kvm *kvm = mmu_notifier_to_kvm(mn);
251 int need_tlb_flush = 0;
252
253 spin_lock(&kvm->mmu_lock);
254 /*
255 * The count increase must become visible at unlock time as no
256 * spte can be established without taking the mmu_lock and
257 * count is also read inside the mmu_lock critical section.
258 */
259 kvm->mmu_notifier_count++;
260 for (; start < end; start += PAGE_SIZE)
261 need_tlb_flush |= kvm_unmap_hva(kvm, start);
262 spin_unlock(&kvm->mmu_lock);
263
264 /* we've to flush the tlb before the pages can be freed */
265 if (need_tlb_flush)
266 kvm_flush_remote_tlbs(kvm);
267}
268
269static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
270 struct mm_struct *mm,
271 unsigned long start,
272 unsigned long end)
273{
274 struct kvm *kvm = mmu_notifier_to_kvm(mn);
275
276 spin_lock(&kvm->mmu_lock);
277 /*
278 * This sequence increase will notify the kvm page fault that
279 * the page that is going to be mapped in the spte could have
280 * been freed.
281 */
282 kvm->mmu_notifier_seq++;
283 /*
284 * The above sequence increase must be visible before the
285 * below count decrease but both values are read by the kvm
286 * page fault under mmu_lock spinlock so we don't need to add
287 * a smb_wmb() here in between the two.
288 */
289 kvm->mmu_notifier_count--;
290 spin_unlock(&kvm->mmu_lock);
291
292 BUG_ON(kvm->mmu_notifier_count < 0);
293}
294
295static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
296 struct mm_struct *mm,
297 unsigned long address)
298{
299 struct kvm *kvm = mmu_notifier_to_kvm(mn);
300 int young;
301
302 spin_lock(&kvm->mmu_lock);
303 young = kvm_age_hva(kvm, address);
304 spin_unlock(&kvm->mmu_lock);
305
306 if (young)
307 kvm_flush_remote_tlbs(kvm);
308
309 return young;
310}
311
312static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
313 .invalidate_page = kvm_mmu_notifier_invalidate_page,
314 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
315 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
316 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
317};
318#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
319
Avi Kivityf17abe92007-02-21 19:28:04 +0200320static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800322 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200323#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
324 struct page *page;
325#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800327 if (IS_ERR(kvm))
328 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329
Laurent Vivier5f94c172008-05-30 16:05:54 +0200330#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
331 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
332 if (!page) {
333 kfree(kvm);
334 return ERR_PTR(-ENOMEM);
335 }
336 kvm->coalesced_mmio_ring =
337 (struct kvm_coalesced_mmio_ring *)page_address(page);
338#endif
339
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200340#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
341 {
342 int err;
343 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
344 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
345 if (err) {
346#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
347 put_page(page);
348#endif
349 kfree(kvm);
350 return ERR_PTR(err);
351 }
352 }
353#endif
354
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200355 kvm->mm = current->mm;
356 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500357 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300358 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800359 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400360 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200361 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300362 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000363 spin_lock(&kvm_lock);
364 list_add(&kvm->vm_list, &vm_list);
365 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200366#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
367 kvm_coalesced_mmio_init(kvm);
368#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800369out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200370 return kvm;
371}
372
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373/*
374 * Free any memory in @free but not in @dont.
375 */
376static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
377 struct kvm_memory_slot *dont)
378{
Izik Eidus290fc382007-09-27 14:11:22 +0200379 if (!dont || free->rmap != dont->rmap)
380 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381
382 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
383 vfree(free->dirty_bitmap);
384
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300385 if (!dont || free->lpage_info != dont->lpage_info)
386 vfree(free->lpage_info);
387
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000389 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500390 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300391 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392}
393
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800394void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395{
396 int i;
397
398 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000399 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400}
401
Avi Kivityf17abe92007-02-21 19:28:04 +0200402static void kvm_destroy_vm(struct kvm *kvm)
403{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200404 struct mm_struct *mm = kvm->mm;
405
Avi Kivity133de902007-02-12 00:54:44 -0800406 spin_lock(&kvm_lock);
407 list_del(&kvm->vm_list);
408 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300409 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400410 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200411#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
412 if (kvm->coalesced_mmio_ring != NULL)
413 free_page((unsigned long)kvm->coalesced_mmio_ring);
414#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200415#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
416 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
417#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800418 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200419 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200420}
421
Izik Eidusd39f13b2008-03-30 16:01:25 +0300422void kvm_get_kvm(struct kvm *kvm)
423{
424 atomic_inc(&kvm->users_count);
425}
426EXPORT_SYMBOL_GPL(kvm_get_kvm);
427
428void kvm_put_kvm(struct kvm *kvm)
429{
430 if (atomic_dec_and_test(&kvm->users_count))
431 kvm_destroy_vm(kvm);
432}
433EXPORT_SYMBOL_GPL(kvm_put_kvm);
434
435
Avi Kivityf17abe92007-02-21 19:28:04 +0200436static int kvm_vm_release(struct inode *inode, struct file *filp)
437{
438 struct kvm *kvm = filp->private_data;
439
Izik Eidusd39f13b2008-03-30 16:01:25 +0300440 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 return 0;
442}
443
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 * Allocate some memory and give it an address in the guest physical address
446 * space.
447 *
448 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800449 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500450 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800452int __kvm_set_memory_region(struct kvm *kvm,
453 struct kvm_userspace_memory_region *mem,
454 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455{
456 int r;
457 gfn_t base_gfn;
458 unsigned long npages;
459 unsigned long i;
460 struct kvm_memory_slot *memslot;
461 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462
463 r = -EINVAL;
464 /* General sanity checks */
465 if (mem->memory_size & (PAGE_SIZE - 1))
466 goto out;
467 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
468 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200469 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 goto out;
471 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
472 goto out;
473
474 memslot = &kvm->memslots[mem->slot];
475 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
476 npages = mem->memory_size >> PAGE_SHIFT;
477
478 if (!npages)
479 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
480
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 new = old = *memslot;
482
483 new.base_gfn = base_gfn;
484 new.npages = npages;
485 new.flags = mem->flags;
486
487 /* Disallow changing a memory slot's size. */
488 r = -EINVAL;
489 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800490 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491
492 /* Check for overlaps */
493 r = -EEXIST;
494 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
495 struct kvm_memory_slot *s = &kvm->memslots[i];
496
497 if (s == memslot)
498 continue;
499 if (!((base_gfn + npages <= s->base_gfn) ||
500 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800501 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 /* Free page dirty bitmap if unneeded */
505 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000506 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507
508 r = -ENOMEM;
509
510 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200511#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500512 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400513 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200514
515 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800516 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200517
Izik Eidus290fc382007-09-27 14:11:22 +0200518 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500519
Izik Eidus80b14b52007-10-25 11:54:04 +0200520 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200521 /*
522 * hva_to_rmmap() serialzies with the mmu_lock and to be
523 * safe it has to ignore memslots with !user_alloc &&
524 * !userspace_addr.
525 */
526 if (user_alloc)
527 new.userspace_addr = mem->userspace_addr;
528 else
529 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300531 if (npages && !new.lpage_info) {
532 int largepages = npages / KVM_PAGES_PER_HPAGE;
533 if (npages % KVM_PAGES_PER_HPAGE)
534 largepages++;
535 if (base_gfn % KVM_PAGES_PER_HPAGE)
536 largepages++;
537
538 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
539
540 if (!new.lpage_info)
541 goto out_free;
542
543 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
544
545 if (base_gfn % KVM_PAGES_PER_HPAGE)
546 new.lpage_info[0].write_count = 1;
547 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
548 new.lpage_info[largepages-1].write_count = 1;
549 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550
551 /* Allocate page dirty bitmap if needed */
552 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
553 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
554
555 new.dirty_bitmap = vmalloc(dirty_bytes);
556 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800557 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 memset(new.dirty_bitmap, 0, dirty_bytes);
559 }
Carsten Otteeff01142008-06-27 15:05:31 +0200560#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300562 if (!npages)
563 kvm_arch_flush_shadow(kvm);
564
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200565 spin_lock(&kvm->mmu_lock);
566 if (mem->slot >= kvm->nmemslots)
567 kvm->nmemslots = mem->slot + 1;
568
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200570 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571
Zhang Xiantao0de10342007-11-20 16:25:04 +0800572 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
573 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200574 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800575 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200576 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800577 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800578 }
579
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 kvm_free_physmem_slot(&old, &new);
581 return 0;
582
Sheng Yangf78e0e22007-10-29 09:40:42 +0800583out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 kvm_free_physmem_slot(&new, &old);
585out:
586 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200587
588}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800589EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
590
591int kvm_set_memory_region(struct kvm *kvm,
592 struct kvm_userspace_memory_region *mem,
593 int user_alloc)
594{
595 int r;
596
Izik Eidus72dc67a2008-02-10 18:04:15 +0200597 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800598 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200599 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800600 return r;
601}
Izik Eidus210c7c42007-10-24 23:52:57 +0200602EXPORT_SYMBOL_GPL(kvm_set_memory_region);
603
Carsten Otte1fe779f2007-10-29 16:08:35 +0100604int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
605 struct
606 kvm_userspace_memory_region *mem,
607 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200608{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200609 if (mem->slot >= KVM_MEMORY_SLOTS)
610 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200611 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612}
613
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800614int kvm_get_dirty_log(struct kvm *kvm,
615 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616{
617 struct kvm_memory_slot *memslot;
618 int r, i;
619 int n;
620 unsigned long any = 0;
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 r = -EINVAL;
623 if (log->slot >= KVM_MEMORY_SLOTS)
624 goto out;
625
626 memslot = &kvm->memslots[log->slot];
627 r = -ENOENT;
628 if (!memslot->dirty_bitmap)
629 goto out;
630
Uri Lublincd1a4a92007-02-22 16:43:09 +0200631 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632
Uri Lublincd1a4a92007-02-22 16:43:09 +0200633 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 any = memslot->dirty_bitmap[i];
635
636 r = -EFAULT;
637 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
638 goto out;
639
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800640 if (any)
641 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
643 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645 return r;
646}
647
Izik Eiduscea7bb22007-10-17 19:17:48 +0200648int is_error_page(struct page *page)
649{
650 return page == bad_page;
651}
652EXPORT_SYMBOL_GPL(is_error_page);
653
Anthony Liguori35149e22008-04-02 14:46:56 -0500654int is_error_pfn(pfn_t pfn)
655{
656 return pfn == bad_pfn;
657}
658EXPORT_SYMBOL_GPL(is_error_pfn);
659
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200660static inline unsigned long bad_hva(void)
661{
662 return PAGE_OFFSET;
663}
664
665int kvm_is_error_hva(unsigned long addr)
666{
667 return addr == bad_hva();
668}
669EXPORT_SYMBOL_GPL(kvm_is_error_hva);
670
Avi Kivitye8207542007-03-30 16:54:30 +0300671static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672{
673 int i;
674
675 for (i = 0; i < kvm->nmemslots; ++i) {
676 struct kvm_memory_slot *memslot = &kvm->memslots[i];
677
678 if (gfn >= memslot->base_gfn
679 && gfn < memslot->base_gfn + memslot->npages)
680 return memslot;
681 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000682 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683}
Avi Kivitye8207542007-03-30 16:54:30 +0300684
685struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
686{
687 gfn = unalias_gfn(kvm, gfn);
688 return __gfn_to_memslot(kvm, gfn);
689}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690
Izik Eiduse0d62c72007-10-24 23:57:46 +0200691int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
692{
693 int i;
694
695 gfn = unalias_gfn(kvm, gfn);
696 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
697 struct kvm_memory_slot *memslot = &kvm->memslots[i];
698
699 if (gfn >= memslot->base_gfn
700 && gfn < memslot->base_gfn + memslot->npages)
701 return 1;
702 }
703 return 0;
704}
705EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
706
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300707unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200708{
709 struct kvm_memory_slot *slot;
710
711 gfn = unalias_gfn(kvm, gfn);
712 slot = __gfn_to_memslot(kvm, gfn);
713 if (!slot)
714 return bad_hva();
715 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
716}
Sheng Yang0d150292008-04-25 21:44:50 +0800717EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200718
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500719/*
720 * Requires current->mm->mmap_sem to be held
721 */
Anthony Liguori35149e22008-04-02 14:46:56 -0500722pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300723{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500724 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200725 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500726 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500727 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300728
Avi Kivity60395222007-10-21 11:03:36 +0200729 might_sleep();
730
Izik Eidus539cb662007-11-11 22:05:04 +0200731 addr = gfn_to_hva(kvm, gfn);
732 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200733 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500734 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200735 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200736
Izik Eidus539cb662007-11-11 22:05:04 +0200737 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
738 NULL);
739
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500740 if (unlikely(npages != 1)) {
741 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500742
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500743 vma = find_vma(current->mm, addr);
744 if (vma == NULL || addr < vma->vm_start ||
745 !(vma->vm_flags & VM_PFNMAP)) {
746 get_page(bad_page);
747 return page_to_pfn(bad_page);
748 }
749
750 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300751 BUG_ON(!is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500752 } else
753 pfn = page_to_pfn(page[0]);
754
755 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500756}
757
758EXPORT_SYMBOL_GPL(gfn_to_pfn);
759
760struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
761{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500762 pfn_t pfn;
763
764 pfn = gfn_to_pfn(kvm, gfn);
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300765 if (!is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500766 return pfn_to_page(pfn);
767
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300768 WARN_ON(is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500769
770 get_page(bad_page);
771 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300772}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500773
Avi Kivity954bbbc2007-03-30 14:02:32 +0300774EXPORT_SYMBOL_GPL(gfn_to_page);
775
Izik Eidusb4231d62007-11-20 11:49:33 +0200776void kvm_release_page_clean(struct page *page)
777{
Anthony Liguori35149e22008-04-02 14:46:56 -0500778 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200779}
780EXPORT_SYMBOL_GPL(kvm_release_page_clean);
781
Anthony Liguori35149e22008-04-02 14:46:56 -0500782void kvm_release_pfn_clean(pfn_t pfn)
783{
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300784 if (!is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500785 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500786}
787EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
788
Izik Eidusb4231d62007-11-20 11:49:33 +0200789void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200790{
Anthony Liguori35149e22008-04-02 14:46:56 -0500791 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200792}
Izik Eidusb4231d62007-11-20 11:49:33 +0200793EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200794
Anthony Liguori35149e22008-04-02 14:46:56 -0500795void kvm_release_pfn_dirty(pfn_t pfn)
796{
797 kvm_set_pfn_dirty(pfn);
798 kvm_release_pfn_clean(pfn);
799}
800EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
801
802void kvm_set_page_dirty(struct page *page)
803{
804 kvm_set_pfn_dirty(page_to_pfn(page));
805}
806EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
807
808void kvm_set_pfn_dirty(pfn_t pfn)
809{
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300810 if (!is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500811 struct page *page = pfn_to_page(pfn);
812 if (!PageReserved(page))
813 SetPageDirty(page);
814 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500815}
816EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
817
818void kvm_set_pfn_accessed(pfn_t pfn)
819{
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300820 if (!is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500821 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500822}
823EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
824
825void kvm_get_pfn(pfn_t pfn)
826{
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300827 if (!is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500828 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500829}
830EXPORT_SYMBOL_GPL(kvm_get_pfn);
831
Izik Eidus195aefd2007-10-01 22:14:18 +0200832static int next_segment(unsigned long len, int offset)
833{
834 if (len > PAGE_SIZE - offset)
835 return PAGE_SIZE - offset;
836 else
837 return len;
838}
839
840int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
841 int len)
842{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200843 int r;
844 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200845
Izik Eiduse0506bc2007-11-11 22:10:22 +0200846 addr = gfn_to_hva(kvm, gfn);
847 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200848 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200849 r = copy_from_user(data, (void __user *)addr + offset, len);
850 if (r)
851 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200852 return 0;
853}
854EXPORT_SYMBOL_GPL(kvm_read_guest_page);
855
856int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
857{
858 gfn_t gfn = gpa >> PAGE_SHIFT;
859 int seg;
860 int offset = offset_in_page(gpa);
861 int ret;
862
863 while ((seg = next_segment(len, offset)) != 0) {
864 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
865 if (ret < 0)
866 return ret;
867 offset = 0;
868 len -= seg;
869 data += seg;
870 ++gfn;
871 }
872 return 0;
873}
874EXPORT_SYMBOL_GPL(kvm_read_guest);
875
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500876int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
877 unsigned long len)
878{
879 int r;
880 unsigned long addr;
881 gfn_t gfn = gpa >> PAGE_SHIFT;
882 int offset = offset_in_page(gpa);
883
884 addr = gfn_to_hva(kvm, gfn);
885 if (kvm_is_error_hva(addr))
886 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100887 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500888 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100889 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500890 if (r)
891 return -EFAULT;
892 return 0;
893}
894EXPORT_SYMBOL(kvm_read_guest_atomic);
895
Izik Eidus195aefd2007-10-01 22:14:18 +0200896int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
897 int offset, int len)
898{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200899 int r;
900 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200901
Izik Eiduse0506bc2007-11-11 22:10:22 +0200902 addr = gfn_to_hva(kvm, gfn);
903 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200904 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200905 r = copy_to_user((void __user *)addr + offset, data, len);
906 if (r)
907 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200908 mark_page_dirty(kvm, gfn);
909 return 0;
910}
911EXPORT_SYMBOL_GPL(kvm_write_guest_page);
912
913int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
914 unsigned long len)
915{
916 gfn_t gfn = gpa >> PAGE_SHIFT;
917 int seg;
918 int offset = offset_in_page(gpa);
919 int ret;
920
921 while ((seg = next_segment(len, offset)) != 0) {
922 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
923 if (ret < 0)
924 return ret;
925 offset = 0;
926 len -= seg;
927 data += seg;
928 ++gfn;
929 }
930 return 0;
931}
932
933int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
934{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200935 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200936}
937EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
938
939int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
940{
941 gfn_t gfn = gpa >> PAGE_SHIFT;
942 int seg;
943 int offset = offset_in_page(gpa);
944 int ret;
945
946 while ((seg = next_segment(len, offset)) != 0) {
947 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
948 if (ret < 0)
949 return ret;
950 offset = 0;
951 len -= seg;
952 ++gfn;
953 }
954 return 0;
955}
956EXPORT_SYMBOL_GPL(kvm_clear_guest);
957
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
959{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300960 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961
Uri Lublin3b6fff12007-10-30 10:42:09 +0200962 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000963 memslot = __gfn_to_memslot(kvm, gfn);
964 if (memslot && memslot->dirty_bitmap) {
965 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966
Rusty Russell7e9d6192007-07-31 20:41:14 +1000967 /* avoid RMW */
968 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
969 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 }
971}
972
Eddie Dongb6958ce2007-07-18 12:15:21 +0300973/*
974 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
975 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500976void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300977{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300978 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300979
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300980 for (;;) {
981 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300982
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300983 if (kvm_cpu_has_interrupt(vcpu))
984 break;
985 if (kvm_cpu_has_pending_timer(vcpu))
986 break;
987 if (kvm_arch_vcpu_runnable(vcpu))
988 break;
989 if (signal_pending(current))
990 break;
991
Eddie Dongb6958ce2007-07-18 12:15:21 +0300992 vcpu_put(vcpu);
993 schedule();
994 vcpu_load(vcpu);
995 }
996
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300997 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300998}
999
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000void kvm_resched(struct kvm_vcpu *vcpu)
1001{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001002 if (!need_resched())
1003 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005}
1006EXPORT_SYMBOL_GPL(kvm_resched);
1007
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001008static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001009{
1010 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001011 struct page *page;
1012
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001013 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001014 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001015#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001016 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001017 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001018#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001019#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1020 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1021 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1022#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001023 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001024 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001025 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001026 vmf->page = page;
1027 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001028}
1029
1030static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001031 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001032};
1033
1034static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1035{
1036 vma->vm_ops = &kvm_vcpu_vm_ops;
1037 return 0;
1038}
1039
Avi Kivitybccf2152007-02-21 18:04:26 +02001040static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1041{
1042 struct kvm_vcpu *vcpu = filp->private_data;
1043
Al Viro66c0b392008-04-19 20:33:56 +01001044 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001045 return 0;
1046}
1047
Jan Engelhardt5c502742008-01-22 20:46:14 +01001048static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001049 .release = kvm_vcpu_release,
1050 .unlocked_ioctl = kvm_vcpu_ioctl,
1051 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001052 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001053};
1054
1055/*
1056 * Allocates an inode for the vcpu.
1057 */
1058static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1059{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001060 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001061 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001062 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001063 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001064}
1065
Avi Kivityc5ea7662007-02-20 18:41:05 +02001066/*
1067 * Creates some virtual cpus. Good luck creating more than one.
1068 */
1069static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1070{
1071 int r;
1072 struct kvm_vcpu *vcpu;
1073
Avi Kivityc5ea7662007-02-20 18:41:05 +02001074 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001075 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001076
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001077 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001078 if (IS_ERR(vcpu))
1079 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001080
Avi Kivity15ad7142007-07-11 18:17:21 +03001081 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1082
Avi Kivity26e52152007-11-20 15:30:24 +02001083 r = kvm_arch_vcpu_setup(vcpu);
1084 if (r)
1085 goto vcpu_destroy;
1086
Shaohua Li11ec2802007-07-23 14:51:37 +08001087 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001088 if (kvm->vcpus[n]) {
1089 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08001090 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001091 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001092 }
1093 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001094 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001095
1096 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001097 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001098 r = create_vcpu_fd(vcpu);
1099 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001100 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001101 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001102
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001103unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001104 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001105 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08001106 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001107vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001108 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001109 return r;
1110}
1111
Avi Kivity1961d272007-03-05 19:46:05 +02001112static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1113{
1114 if (sigset) {
1115 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1116 vcpu->sigset_active = 1;
1117 vcpu->sigset = *sigset;
1118 } else
1119 vcpu->sigset_active = 0;
1120 return 0;
1121}
1122
Avi Kivitybccf2152007-02-21 18:04:26 +02001123static long kvm_vcpu_ioctl(struct file *filp,
1124 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125{
Avi Kivitybccf2152007-02-21 18:04:26 +02001126 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001127 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001128 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001130 if (vcpu->kvm->mm != current->mm)
1131 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001133 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001134 r = -EINVAL;
1135 if (arg)
1136 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001137 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001140 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001142 r = -ENOMEM;
1143 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1144 if (!kvm_regs)
1145 goto out;
1146 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001148 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001150 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1151 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001153out_free1:
1154 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 break;
1156 }
1157 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001158 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001160 r = -ENOMEM;
1161 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1162 if (!kvm_regs)
1163 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001165 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1166 goto out_free2;
1167 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001169 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001171out_free2:
1172 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 break;
1174 }
1175 case KVM_GET_SREGS: {
1176 struct kvm_sregs kvm_sregs;
1177
Avi Kivitybccf2152007-02-21 18:04:26 +02001178 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001179 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 if (r)
1181 goto out;
1182 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001183 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 goto out;
1185 r = 0;
1186 break;
1187 }
1188 case KVM_SET_SREGS: {
1189 struct kvm_sregs kvm_sregs;
1190
1191 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001192 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001194 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 if (r)
1196 goto out;
1197 r = 0;
1198 break;
1199 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001200 case KVM_GET_MP_STATE: {
1201 struct kvm_mp_state mp_state;
1202
1203 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1204 if (r)
1205 goto out;
1206 r = -EFAULT;
1207 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1208 goto out;
1209 r = 0;
1210 break;
1211 }
1212 case KVM_SET_MP_STATE: {
1213 struct kvm_mp_state mp_state;
1214
1215 r = -EFAULT;
1216 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1217 goto out;
1218 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1219 if (r)
1220 goto out;
1221 r = 0;
1222 break;
1223 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 case KVM_TRANSLATE: {
1225 struct kvm_translation tr;
1226
1227 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001228 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001230 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 if (r)
1232 goto out;
1233 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001234 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 goto out;
1236 r = 0;
1237 break;
1238 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239 case KVM_DEBUG_GUEST: {
1240 struct kvm_debug_guest dbg;
1241
1242 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001243 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001245 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246 if (r)
1247 goto out;
1248 r = 0;
1249 break;
1250 }
Avi Kivity1961d272007-03-05 19:46:05 +02001251 case KVM_SET_SIGNAL_MASK: {
1252 struct kvm_signal_mask __user *sigmask_arg = argp;
1253 struct kvm_signal_mask kvm_sigmask;
1254 sigset_t sigset, *p;
1255
1256 p = NULL;
1257 if (argp) {
1258 r = -EFAULT;
1259 if (copy_from_user(&kvm_sigmask, argp,
1260 sizeof kvm_sigmask))
1261 goto out;
1262 r = -EINVAL;
1263 if (kvm_sigmask.len != sizeof sigset)
1264 goto out;
1265 r = -EFAULT;
1266 if (copy_from_user(&sigset, sigmask_arg->sigset,
1267 sizeof sigset))
1268 goto out;
1269 p = &sigset;
1270 }
1271 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1272 break;
1273 }
Avi Kivityb8836732007-04-01 16:34:31 +03001274 case KVM_GET_FPU: {
1275 struct kvm_fpu fpu;
1276
1277 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05001278 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001279 if (r)
1280 goto out;
1281 r = -EFAULT;
1282 if (copy_to_user(argp, &fpu, sizeof fpu))
1283 goto out;
1284 r = 0;
1285 break;
1286 }
1287 case KVM_SET_FPU: {
1288 struct kvm_fpu fpu;
1289
1290 r = -EFAULT;
1291 if (copy_from_user(&fpu, argp, sizeof fpu))
1292 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -05001293 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001294 if (r)
1295 goto out;
1296 r = 0;
1297 break;
1298 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001299 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001300 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001301 }
1302out:
1303 return r;
1304}
1305
1306static long kvm_vm_ioctl(struct file *filp,
1307 unsigned int ioctl, unsigned long arg)
1308{
1309 struct kvm *kvm = filp->private_data;
1310 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001311 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001312
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001313 if (kvm->mm != current->mm)
1314 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001315 switch (ioctl) {
1316 case KVM_CREATE_VCPU:
1317 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1318 if (r < 0)
1319 goto out;
1320 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001321 case KVM_SET_USER_MEMORY_REGION: {
1322 struct kvm_userspace_memory_region kvm_userspace_mem;
1323
1324 r = -EFAULT;
1325 if (copy_from_user(&kvm_userspace_mem, argp,
1326 sizeof kvm_userspace_mem))
1327 goto out;
1328
1329 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330 if (r)
1331 goto out;
1332 break;
1333 }
1334 case KVM_GET_DIRTY_LOG: {
1335 struct kvm_dirty_log log;
1336
1337 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001338 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001340 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 if (r)
1342 goto out;
1343 break;
1344 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001345#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1346 case KVM_REGISTER_COALESCED_MMIO: {
1347 struct kvm_coalesced_mmio_zone zone;
1348 r = -EFAULT;
1349 if (copy_from_user(&zone, argp, sizeof zone))
1350 goto out;
1351 r = -ENXIO;
1352 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1353 if (r)
1354 goto out;
1355 r = 0;
1356 break;
1357 }
1358 case KVM_UNREGISTER_COALESCED_MMIO: {
1359 struct kvm_coalesced_mmio_zone zone;
1360 r = -EFAULT;
1361 if (copy_from_user(&zone, argp, sizeof zone))
1362 goto out;
1363 r = -ENXIO;
1364 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1365 if (r)
1366 goto out;
1367 r = 0;
1368 break;
1369 }
1370#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001371 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001372 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001373 }
1374out:
1375 return r;
1376}
1377
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001378static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001379{
1380 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001381 struct page *page;
1382
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001383 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1384 return VM_FAULT_SIGBUS;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001385 page = gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001386 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001387 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001388 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001389 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001390 vmf->page = page;
1391 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001392}
1393
1394static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001395 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001396};
1397
1398static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1399{
1400 vma->vm_ops = &kvm_vm_vm_ops;
1401 return 0;
1402}
1403
Jan Engelhardt5c502742008-01-22 20:46:14 +01001404static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001405 .release = kvm_vm_release,
1406 .unlocked_ioctl = kvm_vm_ioctl,
1407 .compat_ioctl = kvm_vm_ioctl,
1408 .mmap = kvm_vm_mmap,
1409};
1410
1411static int kvm_dev_ioctl_create_vm(void)
1412{
Al Viro2030a422008-02-23 06:46:49 -05001413 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001414 struct kvm *kvm;
1415
Avi Kivityf17abe92007-02-21 19:28:04 +02001416 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001417 if (IS_ERR(kvm))
1418 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001419 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001420 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001421 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001422
Avi Kivityf17abe92007-02-21 19:28:04 +02001423 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001424}
1425
1426static long kvm_dev_ioctl(struct file *filp,
1427 unsigned int ioctl, unsigned long arg)
1428{
Avi Kivity07c45a32007-03-07 13:05:38 +02001429 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001430
1431 switch (ioctl) {
1432 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001433 r = -EINVAL;
1434 if (arg)
1435 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001436 r = KVM_API_VERSION;
1437 break;
1438 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001439 r = -EINVAL;
1440 if (arg)
1441 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001442 r = kvm_dev_ioctl_create_vm();
1443 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001444 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001445 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001446 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001447 case KVM_GET_VCPU_MMAP_SIZE:
1448 r = -EINVAL;
1449 if (arg)
1450 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001451 r = PAGE_SIZE; /* struct kvm_run */
1452#ifdef CONFIG_X86
1453 r += PAGE_SIZE; /* pio data page */
1454#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001455#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1456 r += PAGE_SIZE; /* coalesced mmio ring page */
1457#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001458 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001459 case KVM_TRACE_ENABLE:
1460 case KVM_TRACE_PAUSE:
1461 case KVM_TRACE_DISABLE:
1462 r = kvm_trace_ioctl(ioctl, arg);
1463 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001465 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 }
1467out:
1468 return r;
1469}
1470
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 .unlocked_ioctl = kvm_dev_ioctl,
1473 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474};
1475
1476static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001477 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 "kvm",
1479 &kvm_chardev_ops,
1480};
1481
Avi Kivity1b6c0162007-05-24 13:03:52 +03001482static void hardware_enable(void *junk)
1483{
1484 int cpu = raw_smp_processor_id();
1485
1486 if (cpu_isset(cpu, cpus_hardware_enabled))
1487 return;
1488 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001489 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001490}
1491
1492static void hardware_disable(void *junk)
1493{
1494 int cpu = raw_smp_processor_id();
1495
1496 if (!cpu_isset(cpu, cpus_hardware_enabled))
1497 return;
1498 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001499 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001500}
1501
Avi Kivity774c47f2007-02-12 00:54:47 -08001502static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1503 void *v)
1504{
1505 int cpu = (long)v;
1506
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001507 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001508 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001509 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001510 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1511 cpu);
1512 hardware_disable(NULL);
1513 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001514 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001515 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1516 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001517 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001518 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001519 case CPU_ONLINE:
1520 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1521 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001522 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001523 break;
1524 }
1525 return NOTIFY_OK;
1526}
1527
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001528
1529asmlinkage void kvm_handle_fault_on_reboot(void)
1530{
1531 if (kvm_rebooting)
1532 /* spin while reset goes on */
1533 while (true)
1534 ;
1535 /* Fault while not rebooting. We want the trace. */
1536 BUG();
1537}
1538EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1539
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001540static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001541 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001542{
1543 if (val == SYS_RESTART) {
1544 /*
1545 * Some (well, at least mine) BIOSes hang on reboot if
1546 * in vmx root mode.
1547 */
1548 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001549 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001550 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001551 }
1552 return NOTIFY_OK;
1553}
1554
1555static struct notifier_block kvm_reboot_notifier = {
1556 .notifier_call = kvm_reboot,
1557 .priority = 0,
1558};
1559
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001560void kvm_io_bus_init(struct kvm_io_bus *bus)
1561{
1562 memset(bus, 0, sizeof(*bus));
1563}
1564
1565void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1566{
1567 int i;
1568
1569 for (i = 0; i < bus->dev_count; i++) {
1570 struct kvm_io_device *pos = bus->devs[i];
1571
1572 kvm_iodevice_destructor(pos);
1573 }
1574}
1575
Laurent Vivier92760492008-05-30 16:05:53 +02001576struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1577 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001578{
1579 int i;
1580
1581 for (i = 0; i < bus->dev_count; i++) {
1582 struct kvm_io_device *pos = bus->devs[i];
1583
Laurent Vivier92760492008-05-30 16:05:53 +02001584 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001585 return pos;
1586 }
1587
1588 return NULL;
1589}
1590
1591void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1592{
1593 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1594
1595 bus->devs[bus->dev_count++] = dev;
1596}
1597
Avi Kivity774c47f2007-02-12 00:54:47 -08001598static struct notifier_block kvm_cpu_notifier = {
1599 .notifier_call = kvm_cpu_hotplug,
1600 .priority = 20, /* must be > scheduler priority */
1601};
1602
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001603static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001604{
1605 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001606 struct kvm *kvm;
1607
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001608 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001609 spin_lock(&kvm_lock);
1610 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001611 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001612 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001613 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001614}
1615
1616DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1617
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001618static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001619{
1620 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001621 struct kvm *kvm;
1622 struct kvm_vcpu *vcpu;
1623 int i;
1624
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001625 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001626 spin_lock(&kvm_lock);
1627 list_for_each_entry(kvm, &vm_list, vm_list)
1628 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001629 vcpu = kvm->vcpus[i];
1630 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001631 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001632 }
1633 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001634 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001635}
1636
Avi Kivityba1389b2007-11-18 16:24:12 +02001637DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1638
1639static struct file_operations *stat_fops[] = {
1640 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1641 [KVM_STAT_VM] = &vm_stat_fops,
1642};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001643
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001644static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645{
1646 struct kvm_stats_debugfs_item *p;
1647
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001648 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001650 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001651 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001652 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
1655static void kvm_exit_debug(void)
1656{
1657 struct kvm_stats_debugfs_item *p;
1658
1659 for (p = debugfs_entries; p->name; ++p)
1660 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001661 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662}
1663
Avi Kivity59ae6c62007-02-12 00:54:48 -08001664static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1665{
Avi Kivity4267c412007-05-24 13:09:41 +03001666 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001667 return 0;
1668}
1669
1670static int kvm_resume(struct sys_device *dev)
1671{
Avi Kivity4267c412007-05-24 13:09:41 +03001672 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001673 return 0;
1674}
1675
1676static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001677 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001678 .suspend = kvm_suspend,
1679 .resume = kvm_resume,
1680};
1681
1682static struct sys_device kvm_sysdev = {
1683 .id = 0,
1684 .cls = &kvm_sysdev_class,
1685};
1686
Izik Eiduscea7bb22007-10-17 19:17:48 +02001687struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001688pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689
Avi Kivity15ad7142007-07-11 18:17:21 +03001690static inline
1691struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1692{
1693 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1694}
1695
1696static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1697{
1698 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1699
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001700 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001701}
1702
1703static void kvm_sched_out(struct preempt_notifier *pn,
1704 struct task_struct *next)
1705{
1706 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1707
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001708 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001709}
1710
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001711int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001712 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713{
1714 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001715 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001717 kvm_init_debug();
1718
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001719 r = kvm_arch_init(opaque);
1720 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001721 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001722
1723 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1724
1725 if (bad_page == NULL) {
1726 r = -ENOMEM;
1727 goto out;
1728 }
1729
Anthony Liguori35149e22008-04-02 14:46:56 -05001730 bad_pfn = page_to_pfn(bad_page);
1731
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001732 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001734 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735
Yang, Sheng002c7f72007-07-31 14:23:01 +03001736 for_each_online_cpu(cpu) {
1737 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001738 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02001739 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001740 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001741 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001742 }
1743
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001744 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001745 r = register_cpu_notifier(&kvm_cpu_notifier);
1746 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001747 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 register_reboot_notifier(&kvm_reboot_notifier);
1749
Avi Kivity59ae6c62007-02-12 00:54:48 -08001750 r = sysdev_class_register(&kvm_sysdev_class);
1751 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001752 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001753
1754 r = sysdev_register(&kvm_sysdev);
1755 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001756 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001757
Rusty Russellc16f8622007-07-30 21:12:19 +10001758 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1759 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001760 __alignof__(struct kvm_vcpu),
1761 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001762 if (!kvm_vcpu_cache) {
1763 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001764 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001765 }
1766
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 kvm_chardev_ops.owner = module;
1768
1769 r = misc_register(&kvm_dev);
1770 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001771 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 goto out_free;
1773 }
1774
Avi Kivity15ad7142007-07-11 18:17:21 +03001775 kvm_preempt_ops.sched_in = kvm_sched_in;
1776 kvm_preempt_ops.sched_out = kvm_sched_out;
1777
Avi Kivityc7addb92007-09-16 18:58:32 +02001778 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779
1780out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001781 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001782out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001783 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001784out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001785 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001786out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001788 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001789out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001790 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001791out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001792 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001793out_free_0:
1794 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001795out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001796 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001797 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001798out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799 return r;
1800}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001801EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001803void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001805 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001807 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001808 sysdev_unregister(&kvm_sysdev);
1809 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001811 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001812 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001813 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001814 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001816 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001818EXPORT_SYMBOL_GPL(kvm_exit);