blob: 38e4d2c34ac1201f9006a568da36847dd46e2738 [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>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/io.h>
49#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020050#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Laurent Vivier5f94c172008-05-30 16:05:54 +020052#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53#include "coalesced_mmio.h"
54#endif
55
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#define CREATE_TRACE_POINTS
57#include <trace/events/kvm.h>
58
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Marcelo Tosattifa40a822009-06-04 15:08:24 -030062/*
63 * Ordering of locks:
64 *
Michael S. Tsirkin22fc0292009-06-29 22:24:45 +030065 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030066 */
67
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080068DEFINE_SPINLOCK(kvm_lock);
69LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080070
Rusty Russell7f59f492008-12-07 21:25:45 +103071static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030072
Rusty Russellc16f8622007-07-30 21:12:19 +100073struct kmem_cache *kvm_vcpu_cache;
74EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030075
Avi Kivity15ad7142007-07-11 18:17:21 +030076static __read_mostly struct preempt_ops kvm_preempt_ops;
77
Hollis Blanchard76f7c872008-04-15 16:05:42 -050078struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080079
Avi Kivitybccf2152007-02-21 18:04:26 +020080static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
81 unsigned long arg);
82
Hannes Edere8ba5d32008-11-28 17:02:42 +010083static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030084
Marcelo Tosatti54dee992009-06-11 12:07:44 -030085static bool largepages_enabled = true;
86
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +080087inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030088{
Joerg Roedelfc5659c2009-02-18 14:08:58 +010089 if (pfn_valid(pfn)) {
90 struct page *page = compound_head(pfn_to_page(pfn));
91 return PageReserved(page);
92 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030093
94 return true;
95}
96
Avi Kivity6aa8b732006-12-10 02:21:36 -080097/*
98 * Switches to specified vcpu, until a matching vcpu_put()
99 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200100void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101{
Avi Kivity15ad7142007-07-11 18:17:21 +0300102 int cpu;
103
Avi Kivitybccf2152007-02-21 18:04:26 +0200104 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300105 cpu = get_cpu();
106 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200107 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300108 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200109}
110
Carsten Otte313a3dc2007-10-11 19:16:52 +0200111void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112{
Avi Kivity15ad7142007-07-11 18:17:21 +0300113 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200114 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300115 preempt_notifier_unregister(&vcpu->preempt_notifier);
116 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117 mutex_unlock(&vcpu->mutex);
118}
119
Avi Kivityd9e368d2007-06-07 19:18:30 +0300120static void ack_flush(void *_completed)
121{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300122}
123
Rusty Russell49846892008-12-08 20:26:24 +1030124static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300125{
Avi Kivity597a5f52008-07-20 14:24:22 +0300126 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030127 cpumask_var_t cpus;
128 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300129 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300130
Li Zefan79f55992009-06-15 14:58:26 +0800131 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030132
Marcelo Tosatti84261922009-06-17 10:53:47 -0300133 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200134 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300135 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030136 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300137 continue;
138 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030139 if (cpus != NULL && cpu != -1 && cpu != me)
140 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300141 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030142 if (unlikely(cpus == NULL))
143 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
144 else if (!cpumask_empty(cpus))
145 smp_call_function_many(cpus, ack_flush, NULL, 1);
146 else
147 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300148 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030149 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030150 return called;
151}
152
153void kvm_flush_remote_tlbs(struct kvm *kvm)
154{
155 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
156 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300157}
158
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500159void kvm_reload_remote_mmus(struct kvm *kvm)
160{
Rusty Russell49846892008-12-08 20:26:24 +1030161 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500162}
163
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000164int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
165{
166 struct page *page;
167 int r;
168
169 mutex_init(&vcpu->mutex);
170 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000171 vcpu->kvm = kvm;
172 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300173 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000174
175 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
176 if (!page) {
177 r = -ENOMEM;
178 goto fail;
179 }
180 vcpu->run = page_address(page);
181
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800182 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000183 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800184 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000185 return 0;
186
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000187fail_free_run:
188 free_page((unsigned long)vcpu->run);
189fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000190 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191}
192EXPORT_SYMBOL_GPL(kvm_vcpu_init);
193
194void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
195{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800196 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000197 free_page((unsigned long)vcpu->run);
198}
199EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
200
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200201#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
202static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
203{
204 return container_of(mn, struct kvm, mmu_notifier);
205}
206
207static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
208 struct mm_struct *mm,
209 unsigned long address)
210{
211 struct kvm *kvm = mmu_notifier_to_kvm(mn);
212 int need_tlb_flush;
213
214 /*
215 * When ->invalidate_page runs, the linux pte has been zapped
216 * already but the page is still allocated until
217 * ->invalidate_page returns. So if we increase the sequence
218 * here the kvm page fault will notice if the spte can't be
219 * established because the page is going to be freed. If
220 * instead the kvm page fault establishes the spte before
221 * ->invalidate_page runs, kvm_unmap_hva will release it
222 * before returning.
223 *
224 * The sequence increase only need to be seen at spin_unlock
225 * time, and not at spin_lock time.
226 *
227 * Increasing the sequence after the spin_unlock would be
228 * unsafe because the kvm page fault could then establish the
229 * pte after kvm_unmap_hva returned, without noticing the page
230 * is going to be freed.
231 */
232 spin_lock(&kvm->mmu_lock);
233 kvm->mmu_notifier_seq++;
234 need_tlb_flush = kvm_unmap_hva(kvm, address);
235 spin_unlock(&kvm->mmu_lock);
236
237 /* we've to flush the tlb before the pages can be freed */
238 if (need_tlb_flush)
239 kvm_flush_remote_tlbs(kvm);
240
241}
242
Izik Eidus3da0dd42009-09-23 21:47:18 +0300243static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
244 struct mm_struct *mm,
245 unsigned long address,
246 pte_t pte)
247{
248 struct kvm *kvm = mmu_notifier_to_kvm(mn);
249
250 spin_lock(&kvm->mmu_lock);
251 kvm->mmu_notifier_seq++;
252 kvm_set_spte_hva(kvm, address, pte);
253 spin_unlock(&kvm->mmu_lock);
254}
255
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200256static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
257 struct mm_struct *mm,
258 unsigned long start,
259 unsigned long end)
260{
261 struct kvm *kvm = mmu_notifier_to_kvm(mn);
262 int need_tlb_flush = 0;
263
264 spin_lock(&kvm->mmu_lock);
265 /*
266 * The count increase must become visible at unlock time as no
267 * spte can be established without taking the mmu_lock and
268 * count is also read inside the mmu_lock critical section.
269 */
270 kvm->mmu_notifier_count++;
271 for (; start < end; start += PAGE_SIZE)
272 need_tlb_flush |= kvm_unmap_hva(kvm, start);
273 spin_unlock(&kvm->mmu_lock);
274
275 /* we've to flush the tlb before the pages can be freed */
276 if (need_tlb_flush)
277 kvm_flush_remote_tlbs(kvm);
278}
279
280static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
281 struct mm_struct *mm,
282 unsigned long start,
283 unsigned long end)
284{
285 struct kvm *kvm = mmu_notifier_to_kvm(mn);
286
287 spin_lock(&kvm->mmu_lock);
288 /*
289 * This sequence increase will notify the kvm page fault that
290 * the page that is going to be mapped in the spte could have
291 * been freed.
292 */
293 kvm->mmu_notifier_seq++;
294 /*
295 * The above sequence increase must be visible before the
296 * below count decrease but both values are read by the kvm
297 * page fault under mmu_lock spinlock so we don't need to add
298 * a smb_wmb() here in between the two.
299 */
300 kvm->mmu_notifier_count--;
301 spin_unlock(&kvm->mmu_lock);
302
303 BUG_ON(kvm->mmu_notifier_count < 0);
304}
305
306static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
307 struct mm_struct *mm,
308 unsigned long address)
309{
310 struct kvm *kvm = mmu_notifier_to_kvm(mn);
311 int young;
312
313 spin_lock(&kvm->mmu_lock);
314 young = kvm_age_hva(kvm, address);
315 spin_unlock(&kvm->mmu_lock);
316
317 if (young)
318 kvm_flush_remote_tlbs(kvm);
319
320 return young;
321}
322
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100323static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
324 struct mm_struct *mm)
325{
326 struct kvm *kvm = mmu_notifier_to_kvm(mn);
327 kvm_arch_flush_shadow(kvm);
328}
329
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200330static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
331 .invalidate_page = kvm_mmu_notifier_invalidate_page,
332 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
333 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
334 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300335 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100336 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200337};
338#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
339
Avi Kivityf17abe92007-02-21 19:28:04 +0200340static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800342 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200343#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
344 struct page *page;
345#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800347 if (IS_ERR(kvm))
348 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200349#ifdef CONFIG_HAVE_KVM_IRQCHIP
350 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300351 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200352#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353
Laurent Vivier5f94c172008-05-30 16:05:54 +0200354#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
355 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
356 if (!page) {
357 kfree(kvm);
358 return ERR_PTR(-ENOMEM);
359 }
360 kvm->coalesced_mmio_ring =
361 (struct kvm_coalesced_mmio_ring *)page_address(page);
362#endif
363
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200364#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
365 {
366 int err;
367 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
368 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
369 if (err) {
370#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
371 put_page(page);
372#endif
373 kfree(kvm);
374 return ERR_PTR(err);
375 }
376 }
377#endif
378
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200379 kvm->mm = current->mm;
380 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500381 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300382 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300383 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400384 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800385 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300386 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400387 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200388 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300389 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000390 spin_lock(&kvm_lock);
391 list_add(&kvm->vm_list, &vm_list);
392 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200393#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
394 kvm_coalesced_mmio_init(kvm);
395#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800396out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200397 return kvm;
398}
399
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400/*
401 * Free any memory in @free but not in @dont.
402 */
403static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
404 struct kvm_memory_slot *dont)
405{
Joerg Roedelec04b262009-06-19 15:16:23 +0200406 int i;
407
Izik Eidus290fc382007-09-27 14:11:22 +0200408 if (!dont || free->rmap != dont->rmap)
409 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410
411 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
412 vfree(free->dirty_bitmap);
413
Joerg Roedelec04b262009-06-19 15:16:23 +0200414
415 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
416 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
417 vfree(free->lpage_info[i]);
418 free->lpage_info[i] = NULL;
419 }
420 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000423 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500424 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425}
426
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800427void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428{
429 int i;
430
431 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000432 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433}
434
Avi Kivityf17abe92007-02-21 19:28:04 +0200435static void kvm_destroy_vm(struct kvm *kvm)
436{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200437 struct mm_struct *mm = kvm->mm;
438
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800439 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800440 spin_lock(&kvm_lock);
441 list_del(&kvm->vm_list);
442 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200443 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +0300444 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400445 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200446#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
447 if (kvm->coalesced_mmio_ring != NULL)
448 free_page((unsigned long)kvm->coalesced_mmio_ring);
449#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200450#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
451 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200452#else
453 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200454#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800455 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200456 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200457}
458
Izik Eidusd39f13b2008-03-30 16:01:25 +0300459void kvm_get_kvm(struct kvm *kvm)
460{
461 atomic_inc(&kvm->users_count);
462}
463EXPORT_SYMBOL_GPL(kvm_get_kvm);
464
465void kvm_put_kvm(struct kvm *kvm)
466{
467 if (atomic_dec_and_test(&kvm->users_count))
468 kvm_destroy_vm(kvm);
469}
470EXPORT_SYMBOL_GPL(kvm_put_kvm);
471
472
Avi Kivityf17abe92007-02-21 19:28:04 +0200473static int kvm_vm_release(struct inode *inode, struct file *filp)
474{
475 struct kvm *kvm = filp->private_data;
476
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400477 kvm_irqfd_release(kvm);
478
Izik Eidusd39f13b2008-03-30 16:01:25 +0300479 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 return 0;
481}
482
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 * Allocate some memory and give it an address in the guest physical address
485 * space.
486 *
487 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800488 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500489 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800491int __kvm_set_memory_region(struct kvm *kvm,
492 struct kvm_userspace_memory_region *mem,
493 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494{
495 int r;
496 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200497 unsigned long npages;
498 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 struct kvm_memory_slot *memslot;
500 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501
502 r = -EINVAL;
503 /* General sanity checks */
504 if (mem->memory_size & (PAGE_SIZE - 1))
505 goto out;
506 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
507 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800508 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600509 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200510 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 goto out;
512 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
513 goto out;
514
515 memslot = &kvm->memslots[mem->slot];
516 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
517 npages = mem->memory_size >> PAGE_SHIFT;
518
519 if (!npages)
520 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
521
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 new = old = *memslot;
523
524 new.base_gfn = base_gfn;
525 new.npages = npages;
526 new.flags = mem->flags;
527
528 /* Disallow changing a memory slot's size. */
529 r = -EINVAL;
530 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800531 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532
533 /* Check for overlaps */
534 r = -EEXIST;
535 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
536 struct kvm_memory_slot *s = &kvm->memslots[i];
537
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200538 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 continue;
540 if (!((base_gfn + npages <= s->base_gfn) ||
541 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800542 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 /* Free page dirty bitmap if unneeded */
546 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000547 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548
549 r = -ENOMEM;
550
551 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200552#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500553 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400554 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200555
556 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800557 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200558
Izik Eidus290fc382007-09-27 14:11:22 +0200559 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500560
Izik Eidus80b14b52007-10-25 11:54:04 +0200561 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200562 /*
563 * hva_to_rmmap() serialzies with the mmu_lock and to be
564 * safe it has to ignore memslots with !user_alloc &&
565 * !userspace_addr.
566 */
567 if (user_alloc)
568 new.userspace_addr = mem->userspace_addr;
569 else
570 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200572 if (!npages)
573 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300574
Joerg Roedelec04b262009-06-19 15:16:23 +0200575 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200576 unsigned long ugfn;
577 unsigned long j;
578 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200579 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300580
Joerg Roedelec04b262009-06-19 15:16:23 +0200581 /* Avoid unused variable warning if no large pages */
582 (void)level;
583
584 if (new.lpage_info[i])
585 continue;
586
587 lpages = 1 + (base_gfn + npages - 1) /
588 KVM_PAGES_PER_HPAGE(level);
589 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
590
591 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
592
593 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300594 goto out_free;
595
Joerg Roedelec04b262009-06-19 15:16:23 +0200596 memset(new.lpage_info[i], 0,
597 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300598
Joerg Roedelec04b262009-06-19 15:16:23 +0200599 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
600 new.lpage_info[i][0].write_count = 1;
601 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
602 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300603 ugfn = new.userspace_addr >> PAGE_SHIFT;
604 /*
605 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300606 * other, or if explicitly asked to, disable large page
607 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300608 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200609 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300610 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200611 for (j = 0; j < lpages; ++j)
612 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300613 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614
Joerg Roedelec04b262009-06-19 15:16:23 +0200615skip_lpage:
616
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617 /* Allocate page dirty bitmap if needed */
618 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
619 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
620
621 new.dirty_bitmap = vmalloc(dirty_bytes);
622 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800623 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +0300625 if (old.npages)
626 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200628#else /* not defined CONFIG_S390 */
629 new.user_alloc = user_alloc;
630 if (user_alloc)
631 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200632#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300634 if (!npages)
635 kvm_arch_flush_shadow(kvm);
636
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200637 spin_lock(&kvm->mmu_lock);
638 if (mem->slot >= kvm->nmemslots)
639 kvm->nmemslots = mem->slot + 1;
640
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200642 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643
Zhang Xiantao0de10342007-11-20 16:25:04 +0800644 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
645 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200646 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800647 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200648 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800649 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800650 }
651
Glauber Costa6f897242008-12-03 13:40:51 -0200652 kvm_free_physmem_slot(&old, npages ? &new : NULL);
653 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300654 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -0200655 if (!npages)
656 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300657 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800658#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300659 /* map the pages in iommu page table */
660 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
661 if (r)
662 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800663#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 return 0;
665
Sheng Yangf78e0e22007-10-29 09:40:42 +0800666out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 kvm_free_physmem_slot(&new, &old);
668out:
669 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200670
671}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800672EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
673
674int kvm_set_memory_region(struct kvm *kvm,
675 struct kvm_userspace_memory_region *mem,
676 int user_alloc)
677{
678 int r;
679
Izik Eidus72dc67a2008-02-10 18:04:15 +0200680 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800681 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200682 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800683 return r;
684}
Izik Eidus210c7c42007-10-24 23:52:57 +0200685EXPORT_SYMBOL_GPL(kvm_set_memory_region);
686
Carsten Otte1fe779f2007-10-29 16:08:35 +0100687int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
688 struct
689 kvm_userspace_memory_region *mem,
690 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200691{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200692 if (mem->slot >= KVM_MEMORY_SLOTS)
693 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200694 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695}
696
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800697int kvm_get_dirty_log(struct kvm *kvm,
698 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
700 struct kvm_memory_slot *memslot;
701 int r, i;
702 int n;
703 unsigned long any = 0;
704
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705 r = -EINVAL;
706 if (log->slot >= KVM_MEMORY_SLOTS)
707 goto out;
708
709 memslot = &kvm->memslots[log->slot];
710 r = -ENOENT;
711 if (!memslot->dirty_bitmap)
712 goto out;
713
Uri Lublincd1a4a92007-02-22 16:43:09 +0200714 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715
Uri Lublincd1a4a92007-02-22 16:43:09 +0200716 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 any = memslot->dirty_bitmap[i];
718
719 r = -EFAULT;
720 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
721 goto out;
722
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800723 if (any)
724 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725
726 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 return r;
729}
730
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300731void kvm_disable_largepages(void)
732{
733 largepages_enabled = false;
734}
735EXPORT_SYMBOL_GPL(kvm_disable_largepages);
736
Izik Eiduscea7bb22007-10-17 19:17:48 +0200737int is_error_page(struct page *page)
738{
739 return page == bad_page;
740}
741EXPORT_SYMBOL_GPL(is_error_page);
742
Anthony Liguori35149e22008-04-02 14:46:56 -0500743int is_error_pfn(pfn_t pfn)
744{
745 return pfn == bad_pfn;
746}
747EXPORT_SYMBOL_GPL(is_error_pfn);
748
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200749static inline unsigned long bad_hva(void)
750{
751 return PAGE_OFFSET;
752}
753
754int kvm_is_error_hva(unsigned long addr)
755{
756 return addr == bad_hva();
757}
758EXPORT_SYMBOL_GPL(kvm_is_error_hva);
759
Izik Eidus28430992008-10-03 17:40:32 +0300760struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761{
762 int i;
763
764 for (i = 0; i < kvm->nmemslots; ++i) {
765 struct kvm_memory_slot *memslot = &kvm->memslots[i];
766
767 if (gfn >= memslot->base_gfn
768 && gfn < memslot->base_gfn + memslot->npages)
769 return memslot;
770 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000771 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772}
Izik Eidus28430992008-10-03 17:40:32 +0300773EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300774
775struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
776{
777 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300778 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300779}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
Izik Eiduse0d62c72007-10-24 23:57:46 +0200781int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
782{
783 int i;
784
785 gfn = unalias_gfn(kvm, gfn);
786 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
787 struct kvm_memory_slot *memslot = &kvm->memslots[i];
788
789 if (gfn >= memslot->base_gfn
790 && gfn < memslot->base_gfn + memslot->npages)
791 return 1;
792 }
793 return 0;
794}
795EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
796
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300797unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200798{
799 struct kvm_memory_slot *slot;
800
801 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300802 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200803 if (!slot)
804 return bad_hva();
805 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
806}
Sheng Yang0d150292008-04-25 21:44:50 +0800807EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200808
Anthony Liguori35149e22008-04-02 14:46:56 -0500809pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300810{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500811 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200812 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500813 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500814 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300815
Avi Kivity60395222007-10-21 11:03:36 +0200816 might_sleep();
817
Izik Eidus539cb662007-11-11 22:05:04 +0200818 addr = gfn_to_hva(kvm, gfn);
819 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200820 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500821 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200822 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200823
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300824 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200825
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500826 if (unlikely(npages != 1)) {
827 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500828
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300829 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500830 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300831
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500832 if (vma == NULL || addr < vma->vm_start ||
833 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300834 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500835 get_page(bad_page);
836 return page_to_pfn(bad_page);
837 }
838
839 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300840 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800841 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500842 } else
843 pfn = page_to_pfn(page[0]);
844
845 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500846}
847
848EXPORT_SYMBOL_GPL(gfn_to_pfn);
849
850struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
851{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500852 pfn_t pfn;
853
854 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800855 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500856 return pfn_to_page(pfn);
857
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800858 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500859
860 get_page(bad_page);
861 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300862}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500863
Avi Kivity954bbbc2007-03-30 14:02:32 +0300864EXPORT_SYMBOL_GPL(gfn_to_page);
865
Izik Eidusb4231d62007-11-20 11:49:33 +0200866void kvm_release_page_clean(struct page *page)
867{
Anthony Liguori35149e22008-04-02 14:46:56 -0500868 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200869}
870EXPORT_SYMBOL_GPL(kvm_release_page_clean);
871
Anthony Liguori35149e22008-04-02 14:46:56 -0500872void kvm_release_pfn_clean(pfn_t pfn)
873{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800874 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500875 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500876}
877EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
878
Izik Eidusb4231d62007-11-20 11:49:33 +0200879void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200880{
Anthony Liguori35149e22008-04-02 14:46:56 -0500881 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200882}
Izik Eidusb4231d62007-11-20 11:49:33 +0200883EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200884
Anthony Liguori35149e22008-04-02 14:46:56 -0500885void kvm_release_pfn_dirty(pfn_t pfn)
886{
887 kvm_set_pfn_dirty(pfn);
888 kvm_release_pfn_clean(pfn);
889}
890EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
891
892void kvm_set_page_dirty(struct page *page)
893{
894 kvm_set_pfn_dirty(page_to_pfn(page));
895}
896EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
897
898void kvm_set_pfn_dirty(pfn_t pfn)
899{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800900 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500901 struct page *page = pfn_to_page(pfn);
902 if (!PageReserved(page))
903 SetPageDirty(page);
904 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500905}
906EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
907
908void kvm_set_pfn_accessed(pfn_t pfn)
909{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800910 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500911 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500912}
913EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
914
915void kvm_get_pfn(pfn_t pfn)
916{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800917 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500918 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500919}
920EXPORT_SYMBOL_GPL(kvm_get_pfn);
921
Izik Eidus195aefd2007-10-01 22:14:18 +0200922static int next_segment(unsigned long len, int offset)
923{
924 if (len > PAGE_SIZE - offset)
925 return PAGE_SIZE - offset;
926 else
927 return len;
928}
929
930int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
931 int len)
932{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200933 int r;
934 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200935
Izik Eiduse0506bc2007-11-11 22:10:22 +0200936 addr = gfn_to_hva(kvm, gfn);
937 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200938 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200939 r = copy_from_user(data, (void __user *)addr + offset, len);
940 if (r)
941 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200942 return 0;
943}
944EXPORT_SYMBOL_GPL(kvm_read_guest_page);
945
946int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
947{
948 gfn_t gfn = gpa >> PAGE_SHIFT;
949 int seg;
950 int offset = offset_in_page(gpa);
951 int ret;
952
953 while ((seg = next_segment(len, offset)) != 0) {
954 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
955 if (ret < 0)
956 return ret;
957 offset = 0;
958 len -= seg;
959 data += seg;
960 ++gfn;
961 }
962 return 0;
963}
964EXPORT_SYMBOL_GPL(kvm_read_guest);
965
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500966int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
967 unsigned long len)
968{
969 int r;
970 unsigned long addr;
971 gfn_t gfn = gpa >> PAGE_SHIFT;
972 int offset = offset_in_page(gpa);
973
974 addr = gfn_to_hva(kvm, gfn);
975 if (kvm_is_error_hva(addr))
976 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100977 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500978 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100979 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500980 if (r)
981 return -EFAULT;
982 return 0;
983}
984EXPORT_SYMBOL(kvm_read_guest_atomic);
985
Izik Eidus195aefd2007-10-01 22:14:18 +0200986int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
987 int offset, int len)
988{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200989 int r;
990 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200991
Izik Eiduse0506bc2007-11-11 22:10:22 +0200992 addr = gfn_to_hva(kvm, gfn);
993 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200994 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200995 r = copy_to_user((void __user *)addr + offset, data, len);
996 if (r)
997 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200998 mark_page_dirty(kvm, gfn);
999 return 0;
1000}
1001EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1002
1003int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1004 unsigned long len)
1005{
1006 gfn_t gfn = gpa >> PAGE_SHIFT;
1007 int seg;
1008 int offset = offset_in_page(gpa);
1009 int ret;
1010
1011 while ((seg = next_segment(len, offset)) != 0) {
1012 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1013 if (ret < 0)
1014 return ret;
1015 offset = 0;
1016 len -= seg;
1017 data += seg;
1018 ++gfn;
1019 }
1020 return 0;
1021}
1022
1023int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1024{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001025 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001026}
1027EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1028
1029int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1030{
1031 gfn_t gfn = gpa >> PAGE_SHIFT;
1032 int seg;
1033 int offset = offset_in_page(gpa);
1034 int ret;
1035
1036 while ((seg = next_segment(len, offset)) != 0) {
1037 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1038 if (ret < 0)
1039 return ret;
1040 offset = 0;
1041 len -= seg;
1042 ++gfn;
1043 }
1044 return 0;
1045}
1046EXPORT_SYMBOL_GPL(kvm_clear_guest);
1047
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1049{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001050 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Uri Lublin3b6fff12007-10-30 10:42:09 +02001052 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001053 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001054 if (memslot && memslot->dirty_bitmap) {
1055 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056
Rusty Russell7e9d6192007-07-31 20:41:14 +10001057 /* avoid RMW */
1058 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1059 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 }
1061}
1062
Eddie Dongb6958ce2007-07-18 12:15:21 +03001063/*
1064 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1065 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001066void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001067{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001068 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001069
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001070 for (;;) {
1071 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001072
Gleb Natapova1b37102009-07-09 15:33:52 +03001073 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001074 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001075 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001076 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001077 if (kvm_cpu_has_pending_timer(vcpu))
1078 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001079 if (signal_pending(current))
1080 break;
1081
Eddie Dongb6958ce2007-07-18 12:15:21 +03001082 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001083 }
1084
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001085 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001086}
1087
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088void kvm_resched(struct kvm_vcpu *vcpu)
1089{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001090 if (!need_resched())
1091 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093}
1094EXPORT_SYMBOL_GPL(kvm_resched);
1095
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001096static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001097{
1098 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001099 struct page *page;
1100
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001101 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001102 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001103#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001104 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001105 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001106#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001107#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1108 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1109 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1110#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001111 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001112 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001113 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001114 vmf->page = page;
1115 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001116}
1117
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001118static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001119 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001120};
1121
1122static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1123{
1124 vma->vm_ops = &kvm_vcpu_vm_ops;
1125 return 0;
1126}
1127
Avi Kivitybccf2152007-02-21 18:04:26 +02001128static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1129{
1130 struct kvm_vcpu *vcpu = filp->private_data;
1131
Al Viro66c0b392008-04-19 20:33:56 +01001132 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001133 return 0;
1134}
1135
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001136static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001137 .release = kvm_vcpu_release,
1138 .unlocked_ioctl = kvm_vcpu_ioctl,
1139 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001140 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001141};
1142
1143/*
1144 * Allocates an inode for the vcpu.
1145 */
1146static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1147{
Gleb Natapov73880c82009-06-09 15:56:28 +03001148 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001149}
1150
Avi Kivityc5ea7662007-02-20 18:41:05 +02001151/*
1152 * Creates some virtual cpus. Good luck creating more than one.
1153 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001154static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001155{
1156 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001157 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001158
Gleb Natapov73880c82009-06-09 15:56:28 +03001159 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001160 if (IS_ERR(vcpu))
1161 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001162
Avi Kivity15ad7142007-07-11 18:17:21 +03001163 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1164
Avi Kivity26e52152007-11-20 15:30:24 +02001165 r = kvm_arch_vcpu_setup(vcpu);
1166 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001167 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001168
Shaohua Li11ec2802007-07-23 14:51:37 +08001169 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001170 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1171 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001172 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001173 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001174
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001175 kvm_for_each_vcpu(r, v, kvm)
1176 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001177 r = -EEXIST;
1178 goto vcpu_destroy;
1179 }
1180
1181 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001182
1183 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001184 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001185 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001186 if (r < 0) {
1187 kvm_put_kvm(kvm);
1188 goto vcpu_destroy;
1189 }
1190
1191 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1192 smp_wmb();
1193 atomic_inc(&kvm->online_vcpus);
1194
1195#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1196 if (kvm->bsp_vcpu_id == id)
1197 kvm->bsp_vcpu = vcpu;
1198#endif
1199 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001200 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001201
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001202vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001203 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001204 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001205 return r;
1206}
1207
Avi Kivity1961d272007-03-05 19:46:05 +02001208static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1209{
1210 if (sigset) {
1211 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1212 vcpu->sigset_active = 1;
1213 vcpu->sigset = *sigset;
1214 } else
1215 vcpu->sigset_active = 0;
1216 return 0;
1217}
1218
Avi Kivitybccf2152007-02-21 18:04:26 +02001219static long kvm_vcpu_ioctl(struct file *filp,
1220 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221{
Avi Kivitybccf2152007-02-21 18:04:26 +02001222 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001223 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001224 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001225 struct kvm_fpu *fpu = NULL;
1226 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001228 if (vcpu->kvm->mm != current->mm)
1229 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001231 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001232 r = -EINVAL;
1233 if (arg)
1234 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001235 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001238 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001240 r = -ENOMEM;
1241 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1242 if (!kvm_regs)
1243 goto out;
1244 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001246 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001248 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1249 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001251out_free1:
1252 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253 break;
1254 }
1255 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001256 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001258 r = -ENOMEM;
1259 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1260 if (!kvm_regs)
1261 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001263 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1264 goto out_free2;
1265 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001267 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001269out_free2:
1270 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271 break;
1272 }
1273 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001274 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1275 r = -ENOMEM;
1276 if (!kvm_sregs)
1277 goto out;
1278 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 if (r)
1280 goto out;
1281 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001282 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 goto out;
1284 r = 0;
1285 break;
1286 }
1287 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001288 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1289 r = -ENOMEM;
1290 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001292 r = -EFAULT;
1293 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1294 goto out;
1295 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 if (r)
1297 goto out;
1298 r = 0;
1299 break;
1300 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001301 case KVM_GET_MP_STATE: {
1302 struct kvm_mp_state mp_state;
1303
1304 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1305 if (r)
1306 goto out;
1307 r = -EFAULT;
1308 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1309 goto out;
1310 r = 0;
1311 break;
1312 }
1313 case KVM_SET_MP_STATE: {
1314 struct kvm_mp_state mp_state;
1315
1316 r = -EFAULT;
1317 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1318 goto out;
1319 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1320 if (r)
1321 goto out;
1322 r = 0;
1323 break;
1324 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 case KVM_TRANSLATE: {
1326 struct kvm_translation tr;
1327
1328 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001329 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001331 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 if (r)
1333 goto out;
1334 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001335 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 goto out;
1337 r = 0;
1338 break;
1339 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001340 case KVM_SET_GUEST_DEBUG: {
1341 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342
1343 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001344 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001346 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 if (r)
1348 goto out;
1349 r = 0;
1350 break;
1351 }
Avi Kivity1961d272007-03-05 19:46:05 +02001352 case KVM_SET_SIGNAL_MASK: {
1353 struct kvm_signal_mask __user *sigmask_arg = argp;
1354 struct kvm_signal_mask kvm_sigmask;
1355 sigset_t sigset, *p;
1356
1357 p = NULL;
1358 if (argp) {
1359 r = -EFAULT;
1360 if (copy_from_user(&kvm_sigmask, argp,
1361 sizeof kvm_sigmask))
1362 goto out;
1363 r = -EINVAL;
1364 if (kvm_sigmask.len != sizeof sigset)
1365 goto out;
1366 r = -EFAULT;
1367 if (copy_from_user(&sigset, sigmask_arg->sigset,
1368 sizeof sigset))
1369 goto out;
1370 p = &sigset;
1371 }
1372 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1373 break;
1374 }
Avi Kivityb8836732007-04-01 16:34:31 +03001375 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001376 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1377 r = -ENOMEM;
1378 if (!fpu)
1379 goto out;
1380 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001381 if (r)
1382 goto out;
1383 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001384 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001385 goto out;
1386 r = 0;
1387 break;
1388 }
1389 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001390 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1391 r = -ENOMEM;
1392 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001393 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001394 r = -EFAULT;
1395 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1396 goto out;
1397 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001398 if (r)
1399 goto out;
1400 r = 0;
1401 break;
1402 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001403 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001404 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001405 }
1406out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001407 kfree(fpu);
1408 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001409 return r;
1410}
1411
1412static long kvm_vm_ioctl(struct file *filp,
1413 unsigned int ioctl, unsigned long arg)
1414{
1415 struct kvm *kvm = filp->private_data;
1416 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001417 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001418
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001419 if (kvm->mm != current->mm)
1420 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001421 switch (ioctl) {
1422 case KVM_CREATE_VCPU:
1423 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1424 if (r < 0)
1425 goto out;
1426 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001427 case KVM_SET_USER_MEMORY_REGION: {
1428 struct kvm_userspace_memory_region kvm_userspace_mem;
1429
1430 r = -EFAULT;
1431 if (copy_from_user(&kvm_userspace_mem, argp,
1432 sizeof kvm_userspace_mem))
1433 goto out;
1434
1435 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 if (r)
1437 goto out;
1438 break;
1439 }
1440 case KVM_GET_DIRTY_LOG: {
1441 struct kvm_dirty_log log;
1442
1443 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001444 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001446 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 if (r)
1448 goto out;
1449 break;
1450 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001451#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1452 case KVM_REGISTER_COALESCED_MMIO: {
1453 struct kvm_coalesced_mmio_zone zone;
1454 r = -EFAULT;
1455 if (copy_from_user(&zone, argp, sizeof zone))
1456 goto out;
1457 r = -ENXIO;
1458 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1459 if (r)
1460 goto out;
1461 r = 0;
1462 break;
1463 }
1464 case KVM_UNREGISTER_COALESCED_MMIO: {
1465 struct kvm_coalesced_mmio_zone zone;
1466 r = -EFAULT;
1467 if (copy_from_user(&zone, argp, sizeof zone))
1468 goto out;
1469 r = -ENXIO;
1470 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1471 if (r)
1472 goto out;
1473 r = 0;
1474 break;
1475 }
1476#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001477 case KVM_IRQFD: {
1478 struct kvm_irqfd data;
1479
1480 r = -EFAULT;
1481 if (copy_from_user(&data, argp, sizeof data))
1482 goto out;
1483 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1484 break;
1485 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001486 case KVM_IOEVENTFD: {
1487 struct kvm_ioeventfd data;
1488
1489 r = -EFAULT;
1490 if (copy_from_user(&data, argp, sizeof data))
1491 goto out;
1492 r = kvm_ioeventfd(kvm, &data);
1493 break;
1494 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001495#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1496 case KVM_SET_BOOT_CPU_ID:
1497 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001498 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001499 if (atomic_read(&kvm->online_vcpus) != 0)
1500 r = -EBUSY;
1501 else
1502 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001503 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001504 break;
1505#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001506 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001507 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001508 if (r == -ENOTTY)
1509 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001510 }
1511out:
1512 return r;
1513}
1514
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001515static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001516{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001517 struct page *page[1];
1518 unsigned long addr;
1519 int npages;
1520 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001521 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001522
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001523 addr = gfn_to_hva(kvm, gfn);
1524 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001525 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001526
1527 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1528 NULL);
1529 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001530 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001531
1532 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001533 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001534}
1535
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001536static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001537 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001538};
1539
1540static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1541{
1542 vma->vm_ops = &kvm_vm_vm_ops;
1543 return 0;
1544}
1545
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001546static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001547 .release = kvm_vm_release,
1548 .unlocked_ioctl = kvm_vm_ioctl,
1549 .compat_ioctl = kvm_vm_ioctl,
1550 .mmap = kvm_vm_mmap,
1551};
1552
1553static int kvm_dev_ioctl_create_vm(void)
1554{
Al Viro2030a422008-02-23 06:46:49 -05001555 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001556 struct kvm *kvm;
1557
Avi Kivityf17abe92007-02-21 19:28:04 +02001558 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001559 if (IS_ERR(kvm))
1560 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001561 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001562 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001563 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001564
Avi Kivityf17abe92007-02-21 19:28:04 +02001565 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001566}
1567
Avi Kivity1a811b62008-12-08 18:25:27 +02001568static long kvm_dev_ioctl_check_extension_generic(long arg)
1569{
1570 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001571 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001572 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001573 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001574#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1575 case KVM_CAP_SET_BOOT_CPU_ID:
1576#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001577 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001578#ifdef CONFIG_HAVE_KVM_IRQCHIP
1579 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001580 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001581#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001582 default:
1583 break;
1584 }
1585 return kvm_dev_ioctl_check_extension(arg);
1586}
1587
Avi Kivityf17abe92007-02-21 19:28:04 +02001588static long kvm_dev_ioctl(struct file *filp,
1589 unsigned int ioctl, unsigned long arg)
1590{
Avi Kivity07c45a32007-03-07 13:05:38 +02001591 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001592
1593 switch (ioctl) {
1594 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001595 r = -EINVAL;
1596 if (arg)
1597 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001598 r = KVM_API_VERSION;
1599 break;
1600 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001601 r = -EINVAL;
1602 if (arg)
1603 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001604 r = kvm_dev_ioctl_create_vm();
1605 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001606 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001607 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001608 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001609 case KVM_GET_VCPU_MMAP_SIZE:
1610 r = -EINVAL;
1611 if (arg)
1612 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001613 r = PAGE_SIZE; /* struct kvm_run */
1614#ifdef CONFIG_X86
1615 r += PAGE_SIZE; /* pio data page */
1616#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001617#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1618 r += PAGE_SIZE; /* coalesced mmio ring page */
1619#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001620 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001621 case KVM_TRACE_ENABLE:
1622 case KVM_TRACE_PAUSE:
1623 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001624 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001625 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001627 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 }
1629out:
1630 return r;
1631}
1632
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634 .unlocked_ioctl = kvm_dev_ioctl,
1635 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636};
1637
1638static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001639 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 "kvm",
1641 &kvm_chardev_ops,
1642};
1643
Avi Kivity1b6c0162007-05-24 13:03:52 +03001644static void hardware_enable(void *junk)
1645{
1646 int cpu = raw_smp_processor_id();
1647
Rusty Russell7f59f492008-12-07 21:25:45 +10301648 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001649 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301650 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001651 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001652}
1653
1654static void hardware_disable(void *junk)
1655{
1656 int cpu = raw_smp_processor_id();
1657
Rusty Russell7f59f492008-12-07 21:25:45 +10301658 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001659 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301660 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001661 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001662}
1663
Avi Kivity774c47f2007-02-12 00:54:47 -08001664static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1665 void *v)
1666{
1667 int cpu = (long)v;
1668
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001669 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001670 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001671 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001672 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1673 cpu);
1674 hardware_disable(NULL);
1675 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001676 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001677 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1678 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001679 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001680 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001681 case CPU_ONLINE:
1682 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1683 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001684 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001685 break;
1686 }
1687 return NOTIFY_OK;
1688}
1689
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001690
1691asmlinkage void kvm_handle_fault_on_reboot(void)
1692{
1693 if (kvm_rebooting)
1694 /* spin while reset goes on */
1695 while (true)
1696 ;
1697 /* Fault while not rebooting. We want the trace. */
1698 BUG();
1699}
1700EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1701
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001702static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001703 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001704{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001705 /*
1706 * Some (well, at least mine) BIOSes hang on reboot if
1707 * in vmx root mode.
1708 *
1709 * And Intel TXT required VMX off for all cpu when system shutdown.
1710 */
1711 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1712 kvm_rebooting = true;
1713 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001714 return NOTIFY_OK;
1715}
1716
1717static struct notifier_block kvm_reboot_notifier = {
1718 .notifier_call = kvm_reboot,
1719 .priority = 0,
1720};
1721
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001722void kvm_io_bus_init(struct kvm_io_bus *bus)
1723{
1724 memset(bus, 0, sizeof(*bus));
1725}
1726
1727void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1728{
1729 int i;
1730
1731 for (i = 0; i < bus->dev_count; i++) {
1732 struct kvm_io_device *pos = bus->devs[i];
1733
1734 kvm_iodevice_destructor(pos);
1735 }
1736}
1737
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001738/* kvm_io_bus_write - called under kvm->slots_lock */
1739int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
1740 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001741{
1742 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001743 for (i = 0; i < bus->dev_count; i++)
1744 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1745 return 0;
1746 return -EOPNOTSUPP;
1747}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001748
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001749/* kvm_io_bus_read - called under kvm->slots_lock */
1750int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
1751{
1752 int i;
1753 for (i = 0; i < bus->dev_count; i++)
1754 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
1755 return 0;
1756 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001757}
1758
Gregory Haskins090b7af2009-07-07 17:08:44 -04001759int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001760 struct kvm_io_device *dev)
1761{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001762 int ret;
1763
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001764 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04001765 ret = __kvm_io_bus_register_dev(bus, dev);
1766 up_write(&kvm->slots_lock);
1767
1768 return ret;
1769}
1770
1771/* An unlocked version. Caller must have write lock on slots_lock. */
1772int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
1773 struct kvm_io_device *dev)
1774{
1775 if (bus->dev_count > NR_IOBUS_DEVS-1)
1776 return -ENOSPC;
1777
1778 bus->devs[bus->dev_count++] = dev;
1779
1780 return 0;
1781}
1782
1783void kvm_io_bus_unregister_dev(struct kvm *kvm,
1784 struct kvm_io_bus *bus,
1785 struct kvm_io_device *dev)
1786{
1787 down_write(&kvm->slots_lock);
1788 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001789 up_write(&kvm->slots_lock);
1790}
1791
1792/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04001793void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
1794 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001795{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001796 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001797
Gregory Haskins090b7af2009-07-07 17:08:44 -04001798 for (i = 0; i < bus->dev_count; i++)
1799 if (bus->devs[i] == dev) {
1800 bus->devs[i] = bus->devs[--bus->dev_count];
1801 break;
1802 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001803}
1804
Avi Kivity774c47f2007-02-12 00:54:47 -08001805static struct notifier_block kvm_cpu_notifier = {
1806 .notifier_call = kvm_cpu_hotplug,
1807 .priority = 20, /* must be > scheduler priority */
1808};
1809
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001810static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001811{
1812 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001813 struct kvm *kvm;
1814
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001815 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001816 spin_lock(&kvm_lock);
1817 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001818 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001819 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001820 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001821}
1822
1823DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1824
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001825static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001826{
1827 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001828 struct kvm *kvm;
1829 struct kvm_vcpu *vcpu;
1830 int i;
1831
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001832 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001833 spin_lock(&kvm_lock);
1834 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001835 kvm_for_each_vcpu(i, vcpu, kvm)
1836 *val += *(u32 *)((void *)vcpu + offset);
1837
Avi Kivity1165f5f2007-04-19 17:27:43 +03001838 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001839 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001840}
1841
Avi Kivityba1389b2007-11-18 16:24:12 +02001842DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1843
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001844static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02001845 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1846 [KVM_STAT_VM] = &vm_stat_fops,
1847};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001848
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001849static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850{
1851 struct kvm_stats_debugfs_item *p;
1852
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001853 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001855 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001856 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001857 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858}
1859
1860static void kvm_exit_debug(void)
1861{
1862 struct kvm_stats_debugfs_item *p;
1863
1864 for (p = debugfs_entries; p->name; ++p)
1865 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001866 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867}
1868
Avi Kivity59ae6c62007-02-12 00:54:48 -08001869static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1870{
Avi Kivity4267c412007-05-24 13:09:41 +03001871 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001872 return 0;
1873}
1874
1875static int kvm_resume(struct sys_device *dev)
1876{
Avi Kivity4267c412007-05-24 13:09:41 +03001877 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001878 return 0;
1879}
1880
1881static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001882 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001883 .suspend = kvm_suspend,
1884 .resume = kvm_resume,
1885};
1886
1887static struct sys_device kvm_sysdev = {
1888 .id = 0,
1889 .cls = &kvm_sysdev_class,
1890};
1891
Izik Eiduscea7bb22007-10-17 19:17:48 +02001892struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001893pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894
Avi Kivity15ad7142007-07-11 18:17:21 +03001895static inline
1896struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1897{
1898 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1899}
1900
1901static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1902{
1903 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1904
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001905 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001906}
1907
1908static void kvm_sched_out(struct preempt_notifier *pn,
1909 struct task_struct *next)
1910{
1911 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1912
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001913 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001914}
1915
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001916int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001917 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918{
1919 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001920 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001922 r = kvm_arch_init(opaque);
1923 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001924 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001925
1926 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1927
1928 if (bad_page == NULL) {
1929 r = -ENOMEM;
1930 goto out;
1931 }
1932
Anthony Liguori35149e22008-04-02 14:46:56 -05001933 bad_pfn = page_to_pfn(bad_page);
1934
Avi Kivity8437a6172009-06-06 14:52:35 -07001935 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10301936 r = -ENOMEM;
1937 goto out_free_0;
1938 }
1939
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001940 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10301942 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
Yang, Sheng002c7f72007-07-31 14:23:01 +03001944 for_each_online_cpu(cpu) {
1945 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001946 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02001947 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03001948 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001949 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001950 }
1951
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001952 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001953 r = register_cpu_notifier(&kvm_cpu_notifier);
1954 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001955 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 register_reboot_notifier(&kvm_reboot_notifier);
1957
Avi Kivity59ae6c62007-02-12 00:54:48 -08001958 r = sysdev_class_register(&kvm_sysdev_class);
1959 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001960 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001961
1962 r = sysdev_register(&kvm_sysdev);
1963 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08001964 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08001965
Rusty Russellc16f8622007-07-30 21:12:19 +10001966 /* A kmem cache lets us meet the alignment requirements of fx_save. */
1967 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08001968 __alignof__(struct kvm_vcpu),
1969 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10001970 if (!kvm_vcpu_cache) {
1971 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08001972 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10001973 }
1974
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001976 kvm_vm_fops.owner = module;
1977 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978
1979 r = misc_register(&kvm_dev);
1980 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001981 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 goto out_free;
1983 }
1984
Avi Kivity15ad7142007-07-11 18:17:21 +03001985 kvm_preempt_ops.sched_in = kvm_sched_in;
1986 kvm_preempt_ops.sched_out = kvm_sched_out;
1987
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07001988 kvm_init_debug();
1989
Avi Kivityc7addb92007-09-16 18:58:32 +02001990 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991
1992out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10001993 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001994out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001995 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001996out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08001997 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08001998out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002000 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002001out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002002 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002003out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002004 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302005out_free_0a:
2006 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002007out_free_0:
2008 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002009out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002010 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002011out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 return r;
2013}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002014EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002016void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002018 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002019 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002021 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002022 sysdev_unregister(&kvm_sysdev);
2023 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002025 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002026 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002027 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002028 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302029 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002030 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002032EXPORT_SYMBOL_GPL(kvm_exit);