blob: 7dd9b0b85e4eea1aafaff116e993e6392c9cc173 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/io.h>
47#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020048#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Laurent Vivier5f94c172008-05-30 16:05:54 +020050#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51#include "coalesced_mmio.h"
52#endif
53
Avi Kivity6aa8b732006-12-10 02:21:36 -080054MODULE_AUTHOR("Qumranet");
55MODULE_LICENSE("GPL");
56
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080057DEFINE_SPINLOCK(kvm_lock);
58LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080059
Avi Kivity1b6c0162007-05-24 13:03:52 +030060static cpumask_t cpus_hardware_enabled;
61
Rusty Russellc16f8622007-07-30 21:12:19 +100062struct kmem_cache *kvm_vcpu_cache;
63EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030064
Avi Kivity15ad7142007-07-11 18:17:21 +030065static __read_mostly struct preempt_ops kvm_preempt_ops;
66
Hollis Blanchard76f7c872008-04-15 16:05:42 -050067struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivitybccf2152007-02-21 18:04:26 +020069static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
70 unsigned long arg);
71
Avi Kivity4ecac3f2008-05-13 13:23:38 +030072bool kvm_rebooting;
73
James Morris5aacf0c2006-12-22 01:04:55 -080074static inline int valid_vcpu(int n)
75{
76 return likely(n >= 0 && n < KVM_MAX_VCPUS);
77}
78
Avi Kivity6aa8b732006-12-10 02:21:36 -080079/*
80 * Switches to specified vcpu, until a matching vcpu_put()
81 */
Carsten Otte313a3dc2007-10-11 19:16:52 +020082void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080083{
Avi Kivity15ad7142007-07-11 18:17:21 +030084 int cpu;
85
Avi Kivitybccf2152007-02-21 18:04:26 +020086 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +030087 cpu = get_cpu();
88 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +020089 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030090 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +020091}
92
Carsten Otte313a3dc2007-10-11 19:16:52 +020093void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094{
Avi Kivity15ad7142007-07-11 18:17:21 +030095 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +020096 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030097 preempt_notifier_unregister(&vcpu->preempt_notifier);
98 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -080099 mutex_unlock(&vcpu->mutex);
100}
101
Avi Kivityd9e368d2007-06-07 19:18:30 +0300102static void ack_flush(void *_completed)
103{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300104}
105
106void kvm_flush_remote_tlbs(struct kvm *kvm)
107{
Avi Kivity597a5f52008-07-20 14:24:22 +0300108 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300109 cpumask_t cpus;
110 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300111
Avi Kivity597a5f52008-07-20 14:24:22 +0300112 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300113 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000114 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
115 vcpu = kvm->vcpus[i];
116 if (!vcpu)
117 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200118 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300119 continue;
120 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300121 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200122 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300123 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200124 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300125 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200126 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200127 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300128out:
129 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300130}
131
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500132void kvm_reload_remote_mmus(struct kvm *kvm)
133{
Avi Kivity597a5f52008-07-20 14:24:22 +0300134 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500135 cpumask_t cpus;
136 struct kvm_vcpu *vcpu;
137
Avi Kivity597a5f52008-07-20 14:24:22 +0300138 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500139 cpus_clear(cpus);
140 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
141 vcpu = kvm->vcpus[i];
142 if (!vcpu)
143 continue;
144 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
145 continue;
146 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300147 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500148 cpu_set(cpu, cpus);
149 }
150 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300151 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500152 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300153out:
154 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500155}
156
157
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000158int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
159{
160 struct page *page;
161 int r;
162
163 mutex_init(&vcpu->mutex);
164 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000165 vcpu->kvm = kvm;
166 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300167 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000168
169 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
170 if (!page) {
171 r = -ENOMEM;
172 goto fail;
173 }
174 vcpu->run = page_address(page);
175
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800176 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000177 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800178 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000179 return 0;
180
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000181fail_free_run:
182 free_page((unsigned long)vcpu->run);
183fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000184 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000185}
186EXPORT_SYMBOL_GPL(kvm_vcpu_init);
187
188void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
189{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800190 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191 free_page((unsigned long)vcpu->run);
192}
193EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
194
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200195#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
196static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
197{
198 return container_of(mn, struct kvm, mmu_notifier);
199}
200
201static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
202 struct mm_struct *mm,
203 unsigned long address)
204{
205 struct kvm *kvm = mmu_notifier_to_kvm(mn);
206 int need_tlb_flush;
207
208 /*
209 * When ->invalidate_page runs, the linux pte has been zapped
210 * already but the page is still allocated until
211 * ->invalidate_page returns. So if we increase the sequence
212 * here the kvm page fault will notice if the spte can't be
213 * established because the page is going to be freed. If
214 * instead the kvm page fault establishes the spte before
215 * ->invalidate_page runs, kvm_unmap_hva will release it
216 * before returning.
217 *
218 * The sequence increase only need to be seen at spin_unlock
219 * time, and not at spin_lock time.
220 *
221 * Increasing the sequence after the spin_unlock would be
222 * unsafe because the kvm page fault could then establish the
223 * pte after kvm_unmap_hva returned, without noticing the page
224 * is going to be freed.
225 */
226 spin_lock(&kvm->mmu_lock);
227 kvm->mmu_notifier_seq++;
228 need_tlb_flush = kvm_unmap_hva(kvm, address);
229 spin_unlock(&kvm->mmu_lock);
230
231 /* we've to flush the tlb before the pages can be freed */
232 if (need_tlb_flush)
233 kvm_flush_remote_tlbs(kvm);
234
235}
236
237static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
238 struct mm_struct *mm,
239 unsigned long start,
240 unsigned long end)
241{
242 struct kvm *kvm = mmu_notifier_to_kvm(mn);
243 int need_tlb_flush = 0;
244
245 spin_lock(&kvm->mmu_lock);
246 /*
247 * The count increase must become visible at unlock time as no
248 * spte can be established without taking the mmu_lock and
249 * count is also read inside the mmu_lock critical section.
250 */
251 kvm->mmu_notifier_count++;
252 for (; start < end; start += PAGE_SIZE)
253 need_tlb_flush |= kvm_unmap_hva(kvm, start);
254 spin_unlock(&kvm->mmu_lock);
255
256 /* we've to flush the tlb before the pages can be freed */
257 if (need_tlb_flush)
258 kvm_flush_remote_tlbs(kvm);
259}
260
261static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
262 struct mm_struct *mm,
263 unsigned long start,
264 unsigned long end)
265{
266 struct kvm *kvm = mmu_notifier_to_kvm(mn);
267
268 spin_lock(&kvm->mmu_lock);
269 /*
270 * This sequence increase will notify the kvm page fault that
271 * the page that is going to be mapped in the spte could have
272 * been freed.
273 */
274 kvm->mmu_notifier_seq++;
275 /*
276 * The above sequence increase must be visible before the
277 * below count decrease but both values are read by the kvm
278 * page fault under mmu_lock spinlock so we don't need to add
279 * a smb_wmb() here in between the two.
280 */
281 kvm->mmu_notifier_count--;
282 spin_unlock(&kvm->mmu_lock);
283
284 BUG_ON(kvm->mmu_notifier_count < 0);
285}
286
287static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
288 struct mm_struct *mm,
289 unsigned long address)
290{
291 struct kvm *kvm = mmu_notifier_to_kvm(mn);
292 int young;
293
294 spin_lock(&kvm->mmu_lock);
295 young = kvm_age_hva(kvm, address);
296 spin_unlock(&kvm->mmu_lock);
297
298 if (young)
299 kvm_flush_remote_tlbs(kvm);
300
301 return young;
302}
303
304static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
305 .invalidate_page = kvm_mmu_notifier_invalidate_page,
306 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
307 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
308 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
309};
310#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
311
Avi Kivityf17abe92007-02-21 19:28:04 +0200312static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800314 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200315#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
316 struct page *page;
317#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800319 if (IS_ERR(kvm))
320 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321
Laurent Vivier5f94c172008-05-30 16:05:54 +0200322#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
323 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
324 if (!page) {
325 kfree(kvm);
326 return ERR_PTR(-ENOMEM);
327 }
328 kvm->coalesced_mmio_ring =
329 (struct kvm_coalesced_mmio_ring *)page_address(page);
330#endif
331
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200332#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
333 {
334 int err;
335 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
336 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
337 if (err) {
338#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
339 put_page(page);
340#endif
341 kfree(kvm);
342 return ERR_PTR(err);
343 }
344 }
345#endif
346
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200347 kvm->mm = current->mm;
348 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500349 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300350 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800351 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400352 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200353 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300354 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000355 spin_lock(&kvm_lock);
356 list_add(&kvm->vm_list, &vm_list);
357 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200358#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
359 kvm_coalesced_mmio_init(kvm);
360#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800361out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200362 return kvm;
363}
364
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365/*
366 * Free any memory in @free but not in @dont.
367 */
368static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
369 struct kvm_memory_slot *dont)
370{
Izik Eidus290fc382007-09-27 14:11:22 +0200371 if (!dont || free->rmap != dont->rmap)
372 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373
374 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
375 vfree(free->dirty_bitmap);
376
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300377 if (!dont || free->lpage_info != dont->lpage_info)
378 vfree(free->lpage_info);
379
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000381 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500382 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300383 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384}
385
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800386void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387{
388 int i;
389
390 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000391 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392}
393
Avi Kivityf17abe92007-02-21 19:28:04 +0200394static void kvm_destroy_vm(struct kvm *kvm)
395{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200396 struct mm_struct *mm = kvm->mm;
397
Avi Kivity133de902007-02-12 00:54:44 -0800398 spin_lock(&kvm_lock);
399 list_del(&kvm->vm_list);
400 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300401 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400402 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200403#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
404 if (kvm->coalesced_mmio_ring != NULL)
405 free_page((unsigned long)kvm->coalesced_mmio_ring);
406#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200407#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
408 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
409#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800410 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200411 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200412}
413
Izik Eidusd39f13b2008-03-30 16:01:25 +0300414void kvm_get_kvm(struct kvm *kvm)
415{
416 atomic_inc(&kvm->users_count);
417}
418EXPORT_SYMBOL_GPL(kvm_get_kvm);
419
420void kvm_put_kvm(struct kvm *kvm)
421{
422 if (atomic_dec_and_test(&kvm->users_count))
423 kvm_destroy_vm(kvm);
424}
425EXPORT_SYMBOL_GPL(kvm_put_kvm);
426
427
Avi Kivityf17abe92007-02-21 19:28:04 +0200428static int kvm_vm_release(struct inode *inode, struct file *filp)
429{
430 struct kvm *kvm = filp->private_data;
431
Izik Eidusd39f13b2008-03-30 16:01:25 +0300432 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 return 0;
434}
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 * Allocate some memory and give it an address in the guest physical address
438 * space.
439 *
440 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800441 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500442 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800444int __kvm_set_memory_region(struct kvm *kvm,
445 struct kvm_userspace_memory_region *mem,
446 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447{
448 int r;
449 gfn_t base_gfn;
450 unsigned long npages;
451 unsigned long i;
452 struct kvm_memory_slot *memslot;
453 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
455 r = -EINVAL;
456 /* General sanity checks */
457 if (mem->memory_size & (PAGE_SIZE - 1))
458 goto out;
459 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
460 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200461 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 goto out;
463 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
464 goto out;
465
466 memslot = &kvm->memslots[mem->slot];
467 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
468 npages = mem->memory_size >> PAGE_SHIFT;
469
470 if (!npages)
471 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
472
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 new = old = *memslot;
474
475 new.base_gfn = base_gfn;
476 new.npages = npages;
477 new.flags = mem->flags;
478
479 /* Disallow changing a memory slot's size. */
480 r = -EINVAL;
481 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800482 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483
484 /* Check for overlaps */
485 r = -EEXIST;
486 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
487 struct kvm_memory_slot *s = &kvm->memslots[i];
488
489 if (s == memslot)
490 continue;
491 if (!((base_gfn + npages <= s->base_gfn) ||
492 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800493 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 /* Free page dirty bitmap if unneeded */
497 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000498 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499
500 r = -ENOMEM;
501
502 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200503#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500504 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400505 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200506
507 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800508 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200509
Izik Eidus290fc382007-09-27 14:11:22 +0200510 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500511
Izik Eidus80b14b52007-10-25 11:54:04 +0200512 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200513 /*
514 * hva_to_rmmap() serialzies with the mmu_lock and to be
515 * safe it has to ignore memslots with !user_alloc &&
516 * !userspace_addr.
517 */
518 if (user_alloc)
519 new.userspace_addr = mem->userspace_addr;
520 else
521 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300523 if (npages && !new.lpage_info) {
524 int largepages = npages / KVM_PAGES_PER_HPAGE;
525 if (npages % KVM_PAGES_PER_HPAGE)
526 largepages++;
527 if (base_gfn % KVM_PAGES_PER_HPAGE)
528 largepages++;
529
530 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
531
532 if (!new.lpage_info)
533 goto out_free;
534
535 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
536
537 if (base_gfn % KVM_PAGES_PER_HPAGE)
538 new.lpage_info[0].write_count = 1;
539 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
540 new.lpage_info[largepages-1].write_count = 1;
541 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542
543 /* Allocate page dirty bitmap if needed */
544 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
545 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
546
547 new.dirty_bitmap = vmalloc(dirty_bytes);
548 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800549 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550 memset(new.dirty_bitmap, 0, dirty_bytes);
551 }
Carsten Otteeff01142008-06-27 15:05:31 +0200552#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300554 if (!npages)
555 kvm_arch_flush_shadow(kvm);
556
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200557 spin_lock(&kvm->mmu_lock);
558 if (mem->slot >= kvm->nmemslots)
559 kvm->nmemslots = mem->slot + 1;
560
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200562 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563
Zhang Xiantao0de10342007-11-20 16:25:04 +0800564 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
565 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200566 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800567 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200568 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800569 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800570 }
571
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 kvm_free_physmem_slot(&old, &new);
573 return 0;
574
Sheng Yangf78e0e22007-10-29 09:40:42 +0800575out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576 kvm_free_physmem_slot(&new, &old);
577out:
578 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200579
580}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800581EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
582
583int kvm_set_memory_region(struct kvm *kvm,
584 struct kvm_userspace_memory_region *mem,
585 int user_alloc)
586{
587 int r;
588
Izik Eidus72dc67a2008-02-10 18:04:15 +0200589 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800590 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200591 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800592 return r;
593}
Izik Eidus210c7c42007-10-24 23:52:57 +0200594EXPORT_SYMBOL_GPL(kvm_set_memory_region);
595
Carsten Otte1fe779f2007-10-29 16:08:35 +0100596int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
597 struct
598 kvm_userspace_memory_region *mem,
599 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200600{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200601 if (mem->slot >= KVM_MEMORY_SLOTS)
602 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200603 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604}
605
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800606int kvm_get_dirty_log(struct kvm *kvm,
607 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608{
609 struct kvm_memory_slot *memslot;
610 int r, i;
611 int n;
612 unsigned long any = 0;
613
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614 r = -EINVAL;
615 if (log->slot >= KVM_MEMORY_SLOTS)
616 goto out;
617
618 memslot = &kvm->memslots[log->slot];
619 r = -ENOENT;
620 if (!memslot->dirty_bitmap)
621 goto out;
622
Uri Lublincd1a4a92007-02-22 16:43:09 +0200623 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624
Uri Lublincd1a4a92007-02-22 16:43:09 +0200625 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 any = memslot->dirty_bitmap[i];
627
628 r = -EFAULT;
629 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
630 goto out;
631
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800632 if (any)
633 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634
635 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 return r;
638}
639
Izik Eiduscea7bb22007-10-17 19:17:48 +0200640int is_error_page(struct page *page)
641{
642 return page == bad_page;
643}
644EXPORT_SYMBOL_GPL(is_error_page);
645
Anthony Liguori35149e22008-04-02 14:46:56 -0500646int is_error_pfn(pfn_t pfn)
647{
648 return pfn == bad_pfn;
649}
650EXPORT_SYMBOL_GPL(is_error_pfn);
651
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200652static inline unsigned long bad_hva(void)
653{
654 return PAGE_OFFSET;
655}
656
657int kvm_is_error_hva(unsigned long addr)
658{
659 return addr == bad_hva();
660}
661EXPORT_SYMBOL_GPL(kvm_is_error_hva);
662
Avi Kivitye8207542007-03-30 16:54:30 +0300663static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664{
665 int i;
666
667 for (i = 0; i < kvm->nmemslots; ++i) {
668 struct kvm_memory_slot *memslot = &kvm->memslots[i];
669
670 if (gfn >= memslot->base_gfn
671 && gfn < memslot->base_gfn + memslot->npages)
672 return memslot;
673 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000674 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675}
Avi Kivitye8207542007-03-30 16:54:30 +0300676
677struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
678{
679 gfn = unalias_gfn(kvm, gfn);
680 return __gfn_to_memslot(kvm, gfn);
681}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Izik Eiduse0d62c72007-10-24 23:57:46 +0200683int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
684{
685 int i;
686
687 gfn = unalias_gfn(kvm, gfn);
688 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
689 struct kvm_memory_slot *memslot = &kvm->memslots[i];
690
691 if (gfn >= memslot->base_gfn
692 && gfn < memslot->base_gfn + memslot->npages)
693 return 1;
694 }
695 return 0;
696}
697EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
698
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300699unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200700{
701 struct kvm_memory_slot *slot;
702
703 gfn = unalias_gfn(kvm, gfn);
704 slot = __gfn_to_memslot(kvm, gfn);
705 if (!slot)
706 return bad_hva();
707 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
708}
Sheng Yang0d150292008-04-25 21:44:50 +0800709EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200710
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500711/*
712 * Requires current->mm->mmap_sem to be held
713 */
Anthony Liguori35149e22008-04-02 14:46:56 -0500714pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300715{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500716 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200717 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500718 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500719 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300720
Avi Kivity60395222007-10-21 11:03:36 +0200721 might_sleep();
722
Izik Eidus539cb662007-11-11 22:05:04 +0200723 addr = gfn_to_hva(kvm, gfn);
724 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200725 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500726 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200727 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200728
Izik Eidus539cb662007-11-11 22:05:04 +0200729 npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page,
730 NULL);
731
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500732 if (unlikely(npages != 1)) {
733 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500734
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500735 vma = find_vma(current->mm, addr);
736 if (vma == NULL || addr < vma->vm_start ||
737 !(vma->vm_flags & VM_PFNMAP)) {
738 get_page(bad_page);
739 return page_to_pfn(bad_page);
740 }
741
742 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
743 BUG_ON(pfn_valid(pfn));
744 } else
745 pfn = page_to_pfn(page[0]);
746
747 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500748}
749
750EXPORT_SYMBOL_GPL(gfn_to_pfn);
751
752struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
753{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500754 pfn_t pfn;
755
756 pfn = gfn_to_pfn(kvm, gfn);
757 if (pfn_valid(pfn))
758 return pfn_to_page(pfn);
759
760 WARN_ON(!pfn_valid(pfn));
761
762 get_page(bad_page);
763 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300764}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500765
Avi Kivity954bbbc2007-03-30 14:02:32 +0300766EXPORT_SYMBOL_GPL(gfn_to_page);
767
Izik Eidusb4231d62007-11-20 11:49:33 +0200768void kvm_release_page_clean(struct page *page)
769{
Anthony Liguori35149e22008-04-02 14:46:56 -0500770 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200771}
772EXPORT_SYMBOL_GPL(kvm_release_page_clean);
773
Anthony Liguori35149e22008-04-02 14:46:56 -0500774void kvm_release_pfn_clean(pfn_t pfn)
775{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500776 if (pfn_valid(pfn))
777 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500778}
779EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
780
Izik Eidusb4231d62007-11-20 11:49:33 +0200781void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200782{
Anthony Liguori35149e22008-04-02 14:46:56 -0500783 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200784}
Izik Eidusb4231d62007-11-20 11:49:33 +0200785EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200786
Anthony Liguori35149e22008-04-02 14:46:56 -0500787void kvm_release_pfn_dirty(pfn_t pfn)
788{
789 kvm_set_pfn_dirty(pfn);
790 kvm_release_pfn_clean(pfn);
791}
792EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
793
794void kvm_set_page_dirty(struct page *page)
795{
796 kvm_set_pfn_dirty(page_to_pfn(page));
797}
798EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
799
800void kvm_set_pfn_dirty(pfn_t pfn)
801{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500802 if (pfn_valid(pfn)) {
803 struct page *page = pfn_to_page(pfn);
804 if (!PageReserved(page))
805 SetPageDirty(page);
806 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500807}
808EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
809
810void kvm_set_pfn_accessed(pfn_t pfn)
811{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500812 if (pfn_valid(pfn))
813 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500814}
815EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
816
817void kvm_get_pfn(pfn_t pfn)
818{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500819 if (pfn_valid(pfn))
820 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500821}
822EXPORT_SYMBOL_GPL(kvm_get_pfn);
823
Izik Eidus195aefd2007-10-01 22:14:18 +0200824static int next_segment(unsigned long len, int offset)
825{
826 if (len > PAGE_SIZE - offset)
827 return PAGE_SIZE - offset;
828 else
829 return len;
830}
831
832int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
833 int len)
834{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200835 int r;
836 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200837
Izik Eiduse0506bc2007-11-11 22:10:22 +0200838 addr = gfn_to_hva(kvm, gfn);
839 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200840 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200841 r = copy_from_user(data, (void __user *)addr + offset, len);
842 if (r)
843 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200844 return 0;
845}
846EXPORT_SYMBOL_GPL(kvm_read_guest_page);
847
848int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
849{
850 gfn_t gfn = gpa >> PAGE_SHIFT;
851 int seg;
852 int offset = offset_in_page(gpa);
853 int ret;
854
855 while ((seg = next_segment(len, offset)) != 0) {
856 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
857 if (ret < 0)
858 return ret;
859 offset = 0;
860 len -= seg;
861 data += seg;
862 ++gfn;
863 }
864 return 0;
865}
866EXPORT_SYMBOL_GPL(kvm_read_guest);
867
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500868int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
869 unsigned long len)
870{
871 int r;
872 unsigned long addr;
873 gfn_t gfn = gpa >> PAGE_SHIFT;
874 int offset = offset_in_page(gpa);
875
876 addr = gfn_to_hva(kvm, gfn);
877 if (kvm_is_error_hva(addr))
878 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100879 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500880 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100881 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500882 if (r)
883 return -EFAULT;
884 return 0;
885}
886EXPORT_SYMBOL(kvm_read_guest_atomic);
887
Izik Eidus195aefd2007-10-01 22:14:18 +0200888int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
889 int offset, int len)
890{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200891 int r;
892 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200893
Izik Eiduse0506bc2007-11-11 22:10:22 +0200894 addr = gfn_to_hva(kvm, gfn);
895 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200896 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200897 r = copy_to_user((void __user *)addr + offset, data, len);
898 if (r)
899 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200900 mark_page_dirty(kvm, gfn);
901 return 0;
902}
903EXPORT_SYMBOL_GPL(kvm_write_guest_page);
904
905int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
906 unsigned long len)
907{
908 gfn_t gfn = gpa >> PAGE_SHIFT;
909 int seg;
910 int offset = offset_in_page(gpa);
911 int ret;
912
913 while ((seg = next_segment(len, offset)) != 0) {
914 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
915 if (ret < 0)
916 return ret;
917 offset = 0;
918 len -= seg;
919 data += seg;
920 ++gfn;
921 }
922 return 0;
923}
924
925int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
926{
Izik Eidus3e021bf2007-11-19 11:16:57 +0200927 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +0200928}
929EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
930
931int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
932{
933 gfn_t gfn = gpa >> PAGE_SHIFT;
934 int seg;
935 int offset = offset_in_page(gpa);
936 int ret;
937
938 while ((seg = next_segment(len, offset)) != 0) {
939 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
940 if (ret < 0)
941 return ret;
942 offset = 0;
943 len -= seg;
944 ++gfn;
945 }
946 return 0;
947}
948EXPORT_SYMBOL_GPL(kvm_clear_guest);
949
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
951{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300952 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953
Uri Lublin3b6fff12007-10-30 10:42:09 +0200954 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +1000955 memslot = __gfn_to_memslot(kvm, gfn);
956 if (memslot && memslot->dirty_bitmap) {
957 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958
Rusty Russell7e9d6192007-07-31 20:41:14 +1000959 /* avoid RMW */
960 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
961 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 }
963}
964
Eddie Dongb6958ce2007-07-18 12:15:21 +0300965/*
966 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
967 */
Hollis Blanchard8776e512007-10-31 17:24:24 -0500968void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +0300969{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300970 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300971
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300972 for (;;) {
973 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300974
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300975 if (kvm_cpu_has_interrupt(vcpu))
976 break;
977 if (kvm_cpu_has_pending_timer(vcpu))
978 break;
979 if (kvm_arch_vcpu_runnable(vcpu))
980 break;
981 if (signal_pending(current))
982 break;
983
Eddie Dongb6958ce2007-07-18 12:15:21 +0300984 vcpu_put(vcpu);
985 schedule();
986 vcpu_load(vcpu);
987 }
988
Marcelo Tosattie5c239c2008-05-08 19:47:01 -0300989 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +0300990}
991
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992void kvm_resched(struct kvm_vcpu *vcpu)
993{
Yaozu Dong3fca0362007-04-25 16:49:19 +0300994 if (!need_resched())
995 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997}
998EXPORT_SYMBOL_GPL(kvm_resched);
999
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001000static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001001{
1002 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001003 struct page *page;
1004
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001005 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001006 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001007#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001008 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001009 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001010#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001011#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1012 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1013 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1014#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001015 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001016 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001017 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001018 vmf->page = page;
1019 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001020}
1021
1022static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001023 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001024};
1025
1026static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1027{
1028 vma->vm_ops = &kvm_vcpu_vm_ops;
1029 return 0;
1030}
1031
Avi Kivitybccf2152007-02-21 18:04:26 +02001032static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1033{
1034 struct kvm_vcpu *vcpu = filp->private_data;
1035
Al Viro66c0b392008-04-19 20:33:56 +01001036 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001037 return 0;
1038}
1039
Jan Engelhardt5c502742008-01-22 20:46:14 +01001040static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001041 .release = kvm_vcpu_release,
1042 .unlocked_ioctl = kvm_vcpu_ioctl,
1043 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001044 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001045};
1046
1047/*
1048 * Allocates an inode for the vcpu.
1049 */
1050static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1051{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001052 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001053 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001054 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001055 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001056}
1057
Avi Kivityc5ea7662007-02-20 18:41:05 +02001058/*
1059 * Creates some virtual cpus. Good luck creating more than one.
1060 */
1061static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1062{
1063 int r;
1064 struct kvm_vcpu *vcpu;
1065
Avi Kivityc5ea7662007-02-20 18:41:05 +02001066 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001067 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001068
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001069 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001070 if (IS_ERR(vcpu))
1071 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001072
Avi Kivity15ad7142007-07-11 18:17:21 +03001073 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1074
Avi Kivity26e52152007-11-20 15:30:24 +02001075 r = kvm_arch_vcpu_setup(vcpu);
1076 if (r)
1077 goto vcpu_destroy;
1078
Shaohua Li11ec2802007-07-23 14:51:37 +08001079 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001080 if (kvm->vcpus[n]) {
1081 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08001082 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001083 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001084 }
1085 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001086 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001087
1088 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001089 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001090 r = create_vcpu_fd(vcpu);
1091 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001092 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001093 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001094
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001095unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001096 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001097 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08001098 mutex_unlock(&kvm->lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001099vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001100 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001101 return r;
1102}
1103
Avi Kivity1961d272007-03-05 19:46:05 +02001104static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1105{
1106 if (sigset) {
1107 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1108 vcpu->sigset_active = 1;
1109 vcpu->sigset = *sigset;
1110 } else
1111 vcpu->sigset_active = 0;
1112 return 0;
1113}
1114
Avi Kivitybccf2152007-02-21 18:04:26 +02001115static long kvm_vcpu_ioctl(struct file *filp,
1116 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117{
Avi Kivitybccf2152007-02-21 18:04:26 +02001118 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001119 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001120 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001122 if (vcpu->kvm->mm != current->mm)
1123 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001125 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001126 r = -EINVAL;
1127 if (arg)
1128 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001129 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001132 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001134 r = -ENOMEM;
1135 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1136 if (!kvm_regs)
1137 goto out;
1138 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001140 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001142 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1143 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001145out_free1:
1146 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 break;
1148 }
1149 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001150 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001152 r = -ENOMEM;
1153 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1154 if (!kvm_regs)
1155 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001157 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1158 goto out_free2;
1159 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001161 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001163out_free2:
1164 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 break;
1166 }
1167 case KVM_GET_SREGS: {
1168 struct kvm_sregs kvm_sregs;
1169
Avi Kivitybccf2152007-02-21 18:04:26 +02001170 memset(&kvm_sregs, 0, sizeof kvm_sregs);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001171 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 if (r)
1173 goto out;
1174 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001175 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 goto out;
1177 r = 0;
1178 break;
1179 }
1180 case KVM_SET_SREGS: {
1181 struct kvm_sregs kvm_sregs;
1182
1183 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001184 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001186 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 if (r)
1188 goto out;
1189 r = 0;
1190 break;
1191 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001192 case KVM_GET_MP_STATE: {
1193 struct kvm_mp_state mp_state;
1194
1195 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1196 if (r)
1197 goto out;
1198 r = -EFAULT;
1199 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1200 goto out;
1201 r = 0;
1202 break;
1203 }
1204 case KVM_SET_MP_STATE: {
1205 struct kvm_mp_state mp_state;
1206
1207 r = -EFAULT;
1208 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1209 goto out;
1210 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1211 if (r)
1212 goto out;
1213 r = 0;
1214 break;
1215 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 case KVM_TRANSLATE: {
1217 struct kvm_translation tr;
1218
1219 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001220 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001222 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 if (r)
1224 goto out;
1225 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001226 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227 goto out;
1228 r = 0;
1229 break;
1230 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 case KVM_DEBUG_GUEST: {
1232 struct kvm_debug_guest dbg;
1233
1234 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001235 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001237 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 if (r)
1239 goto out;
1240 r = 0;
1241 break;
1242 }
Avi Kivity1961d272007-03-05 19:46:05 +02001243 case KVM_SET_SIGNAL_MASK: {
1244 struct kvm_signal_mask __user *sigmask_arg = argp;
1245 struct kvm_signal_mask kvm_sigmask;
1246 sigset_t sigset, *p;
1247
1248 p = NULL;
1249 if (argp) {
1250 r = -EFAULT;
1251 if (copy_from_user(&kvm_sigmask, argp,
1252 sizeof kvm_sigmask))
1253 goto out;
1254 r = -EINVAL;
1255 if (kvm_sigmask.len != sizeof sigset)
1256 goto out;
1257 r = -EFAULT;
1258 if (copy_from_user(&sigset, sigmask_arg->sigset,
1259 sizeof sigset))
1260 goto out;
1261 p = &sigset;
1262 }
1263 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1264 break;
1265 }
Avi Kivityb8836732007-04-01 16:34:31 +03001266 case KVM_GET_FPU: {
1267 struct kvm_fpu fpu;
1268
1269 memset(&fpu, 0, sizeof fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05001270 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001271 if (r)
1272 goto out;
1273 r = -EFAULT;
1274 if (copy_to_user(argp, &fpu, sizeof fpu))
1275 goto out;
1276 r = 0;
1277 break;
1278 }
1279 case KVM_SET_FPU: {
1280 struct kvm_fpu fpu;
1281
1282 r = -EFAULT;
1283 if (copy_from_user(&fpu, argp, sizeof fpu))
1284 goto out;
Hollis Blanchardd0752062007-10-31 17:24:25 -05001285 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001286 if (r)
1287 goto out;
1288 r = 0;
1289 break;
1290 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001291 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001292 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001293 }
1294out:
1295 return r;
1296}
1297
1298static long kvm_vm_ioctl(struct file *filp,
1299 unsigned int ioctl, unsigned long arg)
1300{
1301 struct kvm *kvm = filp->private_data;
1302 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001303 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001304
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001305 if (kvm->mm != current->mm)
1306 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001307 switch (ioctl) {
1308 case KVM_CREATE_VCPU:
1309 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1310 if (r < 0)
1311 goto out;
1312 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001313 case KVM_SET_USER_MEMORY_REGION: {
1314 struct kvm_userspace_memory_region kvm_userspace_mem;
1315
1316 r = -EFAULT;
1317 if (copy_from_user(&kvm_userspace_mem, argp,
1318 sizeof kvm_userspace_mem))
1319 goto out;
1320
1321 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 if (r)
1323 goto out;
1324 break;
1325 }
1326 case KVM_GET_DIRTY_LOG: {
1327 struct kvm_dirty_log log;
1328
1329 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001330 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001332 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 if (r)
1334 goto out;
1335 break;
1336 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001337#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1338 case KVM_REGISTER_COALESCED_MMIO: {
1339 struct kvm_coalesced_mmio_zone zone;
1340 r = -EFAULT;
1341 if (copy_from_user(&zone, argp, sizeof zone))
1342 goto out;
1343 r = -ENXIO;
1344 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1345 if (r)
1346 goto out;
1347 r = 0;
1348 break;
1349 }
1350 case KVM_UNREGISTER_COALESCED_MMIO: {
1351 struct kvm_coalesced_mmio_zone zone;
1352 r = -EFAULT;
1353 if (copy_from_user(&zone, argp, sizeof zone))
1354 goto out;
1355 r = -ENXIO;
1356 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1357 if (r)
1358 goto out;
1359 r = 0;
1360 break;
1361 }
1362#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001363 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001364 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001365 }
1366out:
1367 return r;
1368}
1369
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001370static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001371{
1372 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001373 struct page *page;
1374
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001375 if (!kvm_is_visible_gfn(kvm, vmf->pgoff))
1376 return VM_FAULT_SIGBUS;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001377 page = gfn_to_page(kvm, vmf->pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001378 if (is_error_page(page)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001379 kvm_release_page_clean(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001380 return VM_FAULT_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001381 }
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001382 vmf->page = page;
1383 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001384}
1385
1386static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001387 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001388};
1389
1390static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1391{
1392 vma->vm_ops = &kvm_vm_vm_ops;
1393 return 0;
1394}
1395
Jan Engelhardt5c502742008-01-22 20:46:14 +01001396static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001397 .release = kvm_vm_release,
1398 .unlocked_ioctl = kvm_vm_ioctl,
1399 .compat_ioctl = kvm_vm_ioctl,
1400 .mmap = kvm_vm_mmap,
1401};
1402
1403static int kvm_dev_ioctl_create_vm(void)
1404{
Al Viro2030a422008-02-23 06:46:49 -05001405 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001406 struct kvm *kvm;
1407
Avi Kivityf17abe92007-02-21 19:28:04 +02001408 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001409 if (IS_ERR(kvm))
1410 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001411 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001412 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001413 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001414
Avi Kivityf17abe92007-02-21 19:28:04 +02001415 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001416}
1417
1418static long kvm_dev_ioctl(struct file *filp,
1419 unsigned int ioctl, unsigned long arg)
1420{
Avi Kivity07c45a32007-03-07 13:05:38 +02001421 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001422
1423 switch (ioctl) {
1424 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001425 r = -EINVAL;
1426 if (arg)
1427 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001428 r = KVM_API_VERSION;
1429 break;
1430 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001431 r = -EINVAL;
1432 if (arg)
1433 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001434 r = kvm_dev_ioctl_create_vm();
1435 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001436 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001437 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001438 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001439 case KVM_GET_VCPU_MMAP_SIZE:
1440 r = -EINVAL;
1441 if (arg)
1442 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001443 r = PAGE_SIZE; /* struct kvm_run */
1444#ifdef CONFIG_X86
1445 r += PAGE_SIZE; /* pio data page */
1446#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001447#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1448 r += PAGE_SIZE; /* coalesced mmio ring page */
1449#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001450 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001451 case KVM_TRACE_ENABLE:
1452 case KVM_TRACE_PAUSE:
1453 case KVM_TRACE_DISABLE:
1454 r = kvm_trace_ioctl(ioctl, arg);
1455 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001457 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 }
1459out:
1460 return r;
1461}
1462
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464 .unlocked_ioctl = kvm_dev_ioctl,
1465 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466};
1467
1468static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001469 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 "kvm",
1471 &kvm_chardev_ops,
1472};
1473
Avi Kivity1b6c0162007-05-24 13:03:52 +03001474static void hardware_enable(void *junk)
1475{
1476 int cpu = raw_smp_processor_id();
1477
1478 if (cpu_isset(cpu, cpus_hardware_enabled))
1479 return;
1480 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001481 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001482}
1483
1484static void hardware_disable(void *junk)
1485{
1486 int cpu = raw_smp_processor_id();
1487
1488 if (!cpu_isset(cpu, cpus_hardware_enabled))
1489 return;
1490 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001491 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001492}
1493
Avi Kivity774c47f2007-02-12 00:54:47 -08001494static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1495 void *v)
1496{
1497 int cpu = (long)v;
1498
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001499 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001500 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001501 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03001502 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1503 cpu);
1504 hardware_disable(NULL);
1505 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001506 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001507 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1508 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001509 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001510 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001511 case CPU_ONLINE:
1512 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1513 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001514 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001515 break;
1516 }
1517 return NOTIFY_OK;
1518}
1519
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001520
1521asmlinkage void kvm_handle_fault_on_reboot(void)
1522{
1523 if (kvm_rebooting)
1524 /* spin while reset goes on */
1525 while (true)
1526 ;
1527 /* Fault while not rebooting. We want the trace. */
1528 BUG();
1529}
1530EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1531
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001532static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001533 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001534{
1535 if (val == SYS_RESTART) {
1536 /*
1537 * Some (well, at least mine) BIOSes hang on reboot if
1538 * in vmx root mode.
1539 */
1540 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001541 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001542 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001543 }
1544 return NOTIFY_OK;
1545}
1546
1547static struct notifier_block kvm_reboot_notifier = {
1548 .notifier_call = kvm_reboot,
1549 .priority = 0,
1550};
1551
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001552void kvm_io_bus_init(struct kvm_io_bus *bus)
1553{
1554 memset(bus, 0, sizeof(*bus));
1555}
1556
1557void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1558{
1559 int i;
1560
1561 for (i = 0; i < bus->dev_count; i++) {
1562 struct kvm_io_device *pos = bus->devs[i];
1563
1564 kvm_iodevice_destructor(pos);
1565 }
1566}
1567
Laurent Vivier92760492008-05-30 16:05:53 +02001568struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1569 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001570{
1571 int i;
1572
1573 for (i = 0; i < bus->dev_count; i++) {
1574 struct kvm_io_device *pos = bus->devs[i];
1575
Laurent Vivier92760492008-05-30 16:05:53 +02001576 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001577 return pos;
1578 }
1579
1580 return NULL;
1581}
1582
1583void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1584{
1585 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1586
1587 bus->devs[bus->dev_count++] = dev;
1588}
1589
Avi Kivity774c47f2007-02-12 00:54:47 -08001590static struct notifier_block kvm_cpu_notifier = {
1591 .notifier_call = kvm_cpu_hotplug,
1592 .priority = 20, /* must be > scheduler priority */
1593};
1594
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001595static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001596{
1597 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001598 struct kvm *kvm;
1599
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001600 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001601 spin_lock(&kvm_lock);
1602 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001603 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001604 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001605 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001606}
1607
1608DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1609
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001610static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001611{
1612 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001613 struct kvm *kvm;
1614 struct kvm_vcpu *vcpu;
1615 int i;
1616
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001617 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001618 spin_lock(&kvm_lock);
1619 list_for_each_entry(kvm, &vm_list, vm_list)
1620 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001621 vcpu = kvm->vcpus[i];
1622 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001623 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001624 }
1625 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001626 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001627}
1628
Avi Kivityba1389b2007-11-18 16:24:12 +02001629DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1630
1631static struct file_operations *stat_fops[] = {
1632 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1633 [KVM_STAT_VM] = &vm_stat_fops,
1634};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001635
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001636static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637{
1638 struct kvm_stats_debugfs_item *p;
1639
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001640 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001642 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001643 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001644 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
1646
1647static void kvm_exit_debug(void)
1648{
1649 struct kvm_stats_debugfs_item *p;
1650
1651 for (p = debugfs_entries; p->name; ++p)
1652 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001653 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654}
1655
Avi Kivity59ae6c62007-02-12 00:54:48 -08001656static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1657{
Avi Kivity4267c412007-05-24 13:09:41 +03001658 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001659 return 0;
1660}
1661
1662static int kvm_resume(struct sys_device *dev)
1663{
Avi Kivity4267c412007-05-24 13:09:41 +03001664 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001665 return 0;
1666}
1667
1668static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001669 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001670 .suspend = kvm_suspend,
1671 .resume = kvm_resume,
1672};
1673
1674static struct sys_device kvm_sysdev = {
1675 .id = 0,
1676 .cls = &kvm_sysdev_class,
1677};
1678
Izik Eiduscea7bb22007-10-17 19:17:48 +02001679struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001680pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681
Avi Kivity15ad7142007-07-11 18:17:21 +03001682static inline
1683struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1684{
1685 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1686}
1687
1688static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1689{
1690 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1691
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001692 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001693}
1694
1695static void kvm_sched_out(struct preempt_notifier *pn,
1696 struct task_struct *next)
1697{
1698 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1699
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001700 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001701}
1702
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001703int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001704 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705{
1706 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001707 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001709 kvm_init_debug();
1710
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001711 r = kvm_arch_init(opaque);
1712 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001713 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001714
1715 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1716
1717 if (bad_page == NULL) {
1718 r = -ENOMEM;
1719 goto out;
1720 }
1721
Anthony Liguori35149e22008-04-02 14:46:56 -05001722 bad_pfn = page_to_pfn(bad_page);
1723
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001724 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001726 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727
Yang, Sheng002c7f72007-07-31 14:23:01 +03001728 for_each_online_cpu(cpu) {
1729 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001730 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02001731 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001732 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001733 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001734 }
1735
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001736 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001737 r = register_cpu_notifier(&kvm_cpu_notifier);
1738 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001739 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 register_reboot_notifier(&kvm_reboot_notifier);
1741
Avi Kivity59ae6c62007-02-12 00:54:48 -08001742 r = sysdev_class_register(&kvm_sysdev_class);
1743 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001744 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001745
1746 r = sysdev_register(&kvm_sysdev);
1747 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001748 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001749
Rusty Russellc16f8622007-07-30 21:12:19 +10001750 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1751 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001752 __alignof__(struct kvm_vcpu),
1753 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001754 if (!kvm_vcpu_cache) {
1755 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001756 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001757 }
1758
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 kvm_chardev_ops.owner = module;
1760
1761 r = misc_register(&kvm_dev);
1762 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001763 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764 goto out_free;
1765 }
1766
Avi Kivity15ad7142007-07-11 18:17:21 +03001767 kvm_preempt_ops.sched_in = kvm_sched_in;
1768 kvm_preempt_ops.sched_out = kvm_sched_out;
1769
Avi Kivityc7addb92007-09-16 18:58:32 +02001770 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771
1772out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001773 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001774out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001775 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001776out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001777 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001778out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08001780 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001781out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001782 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001783out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001784 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001785out_free_0:
1786 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02001787out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001788 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001789 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08001790out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 return r;
1792}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001793EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001795void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001797 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10001799 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001800 sysdev_unregister(&kvm_sysdev);
1801 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001803 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001804 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001805 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001806 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02001808 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001810EXPORT_SYMBOL_GPL(kvm_exit);