blob: bc23b8e0609b58c5e71f47ca6ced85131763a6ae [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>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020046#include <linux/compat.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080047
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040049#include <asm/io.h>
50#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020051#include <asm/pgtable.h>
Alexander Grafc8240bd2009-10-30 05:47:26 +000052#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080053
Laurent Vivier5f94c172008-05-30 16:05:54 +020054#include "coalesced_mmio.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020055
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 *
Sheng Yangfae3a352009-12-15 10:28:07 +080065 * kvm->lock --> kvm->slots_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;
Alexander Graf10474ae2009-09-15 11:37:46 +020072static int kvm_usage_count = 0;
73static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030074
Rusty Russellc16f8622007-07-30 21:12:19 +100075struct kmem_cache *kvm_vcpu_cache;
76EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030077
Avi Kivity15ad7142007-07-11 18:17:21 +030078static __read_mostly struct preempt_ops kvm_preempt_ops;
79
Hollis Blanchard76f7c872008-04-15 16:05:42 -050080struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
Avi Kivitybccf2152007-02-21 18:04:26 +020082static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
83 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020084static int hardware_enable_all(void);
85static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020086
Hannes Edere8ba5d32008-11-28 17:02:42 +010087static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030088
Marcelo Tosatti54dee992009-06-11 12:07:44 -030089static bool largepages_enabled = true;
90
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +080091inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030092{
Joerg Roedelfc5659c2009-02-18 14:08:58 +010093 if (pfn_valid(pfn)) {
94 struct page *page = compound_head(pfn_to_page(pfn));
95 return PageReserved(page);
96 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030097
98 return true;
99}
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101/*
102 * Switches to specified vcpu, until a matching vcpu_put()
103 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200104void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105{
Avi Kivity15ad7142007-07-11 18:17:21 +0300106 int cpu;
107
Avi Kivitybccf2152007-02-21 18:04:26 +0200108 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300109 cpu = get_cpu();
110 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200111 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300112 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200113}
114
Carsten Otte313a3dc2007-10-11 19:16:52 +0200115void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116{
Avi Kivity15ad7142007-07-11 18:17:21 +0300117 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200118 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300119 preempt_notifier_unregister(&vcpu->preempt_notifier);
120 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 mutex_unlock(&vcpu->mutex);
122}
123
Avi Kivityd9e368d2007-06-07 19:18:30 +0300124static void ack_flush(void *_completed)
125{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300126}
127
Rusty Russell49846892008-12-08 20:26:24 +1030128static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300129{
Avi Kivity597a5f52008-07-20 14:24:22 +0300130 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030131 cpumask_var_t cpus;
132 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300133 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300134
Li Zefan79f55992009-06-15 14:58:26 +0800135 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030136
Marcelo Tosatti84261922009-06-17 10:53:47 -0300137 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200138 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300139 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030140 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300141 continue;
142 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030143 if (cpus != NULL && cpu != -1 && cpu != me)
144 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300145 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030146 if (unlikely(cpus == NULL))
147 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
148 else if (!cpumask_empty(cpus))
149 smp_call_function_many(cpus, ack_flush, NULL, 1);
150 else
151 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300152 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030153 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030154 return called;
155}
156
157void kvm_flush_remote_tlbs(struct kvm *kvm)
158{
159 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
160 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300161}
162
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500163void kvm_reload_remote_mmus(struct kvm *kvm)
164{
Rusty Russell49846892008-12-08 20:26:24 +1030165 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500166}
167
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000168int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
169{
170 struct page *page;
171 int r;
172
173 mutex_init(&vcpu->mutex);
174 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000175 vcpu->kvm = kvm;
176 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300177 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000178
179 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
180 if (!page) {
181 r = -ENOMEM;
182 goto fail;
183 }
184 vcpu->run = page_address(page);
185
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800186 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000187 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800188 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000189 return 0;
190
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191fail_free_run:
192 free_page((unsigned long)vcpu->run);
193fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000194 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000195}
196EXPORT_SYMBOL_GPL(kvm_vcpu_init);
197
198void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
199{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800200 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000201 free_page((unsigned long)vcpu->run);
202}
203EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
204
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200205#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
206static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
207{
208 return container_of(mn, struct kvm, mmu_notifier);
209}
210
211static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
212 struct mm_struct *mm,
213 unsigned long address)
214{
215 struct kvm *kvm = mmu_notifier_to_kvm(mn);
216 int need_tlb_flush;
217
218 /*
219 * When ->invalidate_page runs, the linux pte has been zapped
220 * already but the page is still allocated until
221 * ->invalidate_page returns. So if we increase the sequence
222 * here the kvm page fault will notice if the spte can't be
223 * established because the page is going to be freed. If
224 * instead the kvm page fault establishes the spte before
225 * ->invalidate_page runs, kvm_unmap_hva will release it
226 * before returning.
227 *
228 * The sequence increase only need to be seen at spin_unlock
229 * time, and not at spin_lock time.
230 *
231 * Increasing the sequence after the spin_unlock would be
232 * unsafe because the kvm page fault could then establish the
233 * pte after kvm_unmap_hva returned, without noticing the page
234 * is going to be freed.
235 */
236 spin_lock(&kvm->mmu_lock);
237 kvm->mmu_notifier_seq++;
238 need_tlb_flush = kvm_unmap_hva(kvm, address);
239 spin_unlock(&kvm->mmu_lock);
240
241 /* we've to flush the tlb before the pages can be freed */
242 if (need_tlb_flush)
243 kvm_flush_remote_tlbs(kvm);
244
245}
246
Izik Eidus3da0dd42009-09-23 21:47:18 +0300247static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
248 struct mm_struct *mm,
249 unsigned long address,
250 pte_t pte)
251{
252 struct kvm *kvm = mmu_notifier_to_kvm(mn);
253
254 spin_lock(&kvm->mmu_lock);
255 kvm->mmu_notifier_seq++;
256 kvm_set_spte_hva(kvm, address, pte);
257 spin_unlock(&kvm->mmu_lock);
258}
259
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200260static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
261 struct mm_struct *mm,
262 unsigned long start,
263 unsigned long end)
264{
265 struct kvm *kvm = mmu_notifier_to_kvm(mn);
266 int need_tlb_flush = 0;
267
268 spin_lock(&kvm->mmu_lock);
269 /*
270 * The count increase must become visible at unlock time as no
271 * spte can be established without taking the mmu_lock and
272 * count is also read inside the mmu_lock critical section.
273 */
274 kvm->mmu_notifier_count++;
275 for (; start < end; start += PAGE_SIZE)
276 need_tlb_flush |= kvm_unmap_hva(kvm, start);
277 spin_unlock(&kvm->mmu_lock);
278
279 /* we've to flush the tlb before the pages can be freed */
280 if (need_tlb_flush)
281 kvm_flush_remote_tlbs(kvm);
282}
283
284static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
285 struct mm_struct *mm,
286 unsigned long start,
287 unsigned long end)
288{
289 struct kvm *kvm = mmu_notifier_to_kvm(mn);
290
291 spin_lock(&kvm->mmu_lock);
292 /*
293 * This sequence increase will notify the kvm page fault that
294 * the page that is going to be mapped in the spte could have
295 * been freed.
296 */
297 kvm->mmu_notifier_seq++;
298 /*
299 * The above sequence increase must be visible before the
300 * below count decrease but both values are read by the kvm
301 * page fault under mmu_lock spinlock so we don't need to add
302 * a smb_wmb() here in between the two.
303 */
304 kvm->mmu_notifier_count--;
305 spin_unlock(&kvm->mmu_lock);
306
307 BUG_ON(kvm->mmu_notifier_count < 0);
308}
309
310static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
311 struct mm_struct *mm,
312 unsigned long address)
313{
314 struct kvm *kvm = mmu_notifier_to_kvm(mn);
315 int young;
316
317 spin_lock(&kvm->mmu_lock);
318 young = kvm_age_hva(kvm, address);
319 spin_unlock(&kvm->mmu_lock);
320
321 if (young)
322 kvm_flush_remote_tlbs(kvm);
323
324 return young;
325}
326
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100327static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
328 struct mm_struct *mm)
329{
330 struct kvm *kvm = mmu_notifier_to_kvm(mn);
331 kvm_arch_flush_shadow(kvm);
332}
333
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200334static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
335 .invalidate_page = kvm_mmu_notifier_invalidate_page,
336 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
337 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
338 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300339 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100340 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200341};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200342
343static int kvm_init_mmu_notifier(struct kvm *kvm)
344{
345 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
346 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
347}
348
349#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
350
351static int kvm_init_mmu_notifier(struct kvm *kvm)
352{
353 return 0;
354}
355
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200356#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
357
Avi Kivityf17abe92007-02-21 19:28:04 +0200358static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359{
Alexander Graf10474ae2009-09-15 11:37:46 +0200360 int r = 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800361 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200362#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
363 struct page *page;
364#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800366 if (IS_ERR(kvm))
367 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200368
369 r = hardware_enable_all();
370 if (r)
371 goto out_err_nodisable;
372
Avi Kivity75858a82009-01-04 17:10:50 +0200373#ifdef CONFIG_HAVE_KVM_IRQCHIP
374 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300375 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200376#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377
Laurent Vivier5f94c172008-05-30 16:05:54 +0200378#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
379 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
380 if (!page) {
Alexander Graf10474ae2009-09-15 11:37:46 +0200381 r = -ENOMEM;
382 goto out_err;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200383 }
384 kvm->coalesced_mmio_ring =
385 (struct kvm_coalesced_mmio_ring *)page_address(page);
386#endif
387
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200388 r = kvm_init_mmu_notifier(kvm);
Avi Kivity283d0c62009-12-20 14:25:19 +0200389 if (r) {
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200390#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
Avi Kivity283d0c62009-12-20 14:25:19 +0200391 put_page(page);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200392#endif
Avi Kivity283d0c62009-12-20 14:25:19 +0200393 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200394 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200395
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200396 kvm->mm = current->mm;
397 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500398 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300399 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300400 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400401 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800402 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300403 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400404 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200405 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300406 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000407 spin_lock(&kvm_lock);
408 list_add(&kvm->vm_list, &vm_list);
409 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200410#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
411 kvm_coalesced_mmio_init(kvm);
412#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800413out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200414 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200415
416out_err:
417 hardware_disable_all();
418out_err_nodisable:
419 kfree(kvm);
420 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200421}
422
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423/*
424 * Free any memory in @free but not in @dont.
425 */
426static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
427 struct kvm_memory_slot *dont)
428{
Joerg Roedelec04b262009-06-19 15:16:23 +0200429 int i;
430
Izik Eidus290fc382007-09-27 14:11:22 +0200431 if (!dont || free->rmap != dont->rmap)
432 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433
434 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
435 vfree(free->dirty_bitmap);
436
Joerg Roedelec04b262009-06-19 15:16:23 +0200437
438 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
439 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
440 vfree(free->lpage_info[i]);
441 free->lpage_info[i] = NULL;
442 }
443 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000446 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500447 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448}
449
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800450void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451{
452 int i;
453
454 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000455 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456}
457
Avi Kivityf17abe92007-02-21 19:28:04 +0200458static void kvm_destroy_vm(struct kvm *kvm)
459{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200460 struct mm_struct *mm = kvm->mm;
461
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800462 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800463 spin_lock(&kvm_lock);
464 list_del(&kvm->vm_list);
465 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200466 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +0300467 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400468 kvm_io_bus_destroy(&kvm->mmio_bus);
Avi Kivity980da6c2009-12-20 15:13:43 +0200469 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200470#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
471 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200472#else
473 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200474#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800475 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200476 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200477 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200478}
479
Izik Eidusd39f13b2008-03-30 16:01:25 +0300480void kvm_get_kvm(struct kvm *kvm)
481{
482 atomic_inc(&kvm->users_count);
483}
484EXPORT_SYMBOL_GPL(kvm_get_kvm);
485
486void kvm_put_kvm(struct kvm *kvm)
487{
488 if (atomic_dec_and_test(&kvm->users_count))
489 kvm_destroy_vm(kvm);
490}
491EXPORT_SYMBOL_GPL(kvm_put_kvm);
492
493
Avi Kivityf17abe92007-02-21 19:28:04 +0200494static int kvm_vm_release(struct inode *inode, struct file *filp)
495{
496 struct kvm *kvm = filp->private_data;
497
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400498 kvm_irqfd_release(kvm);
499
Izik Eidusd39f13b2008-03-30 16:01:25 +0300500 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 return 0;
502}
503
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 * Allocate some memory and give it an address in the guest physical address
506 * space.
507 *
508 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800509 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500510 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800512int __kvm_set_memory_region(struct kvm *kvm,
513 struct kvm_userspace_memory_region *mem,
514 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515{
516 int r;
517 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200518 unsigned long npages;
519 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 struct kvm_memory_slot *memslot;
521 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522
523 r = -EINVAL;
524 /* General sanity checks */
525 if (mem->memory_size & (PAGE_SIZE - 1))
526 goto out;
527 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
528 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800529 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600530 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200531 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532 goto out;
533 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
534 goto out;
535
536 memslot = &kvm->memslots[mem->slot];
537 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
538 npages = mem->memory_size >> PAGE_SHIFT;
539
540 if (!npages)
541 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
542
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 new = old = *memslot;
544
545 new.base_gfn = base_gfn;
546 new.npages = npages;
547 new.flags = mem->flags;
548
549 /* Disallow changing a memory slot's size. */
550 r = -EINVAL;
551 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800552 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553
554 /* Check for overlaps */
555 r = -EEXIST;
556 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
557 struct kvm_memory_slot *s = &kvm->memslots[i];
558
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200559 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560 continue;
561 if (!((base_gfn + npages <= s->base_gfn) ||
562 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800563 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 /* Free page dirty bitmap if unneeded */
567 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000568 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569
570 r = -ENOMEM;
571
572 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200573#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500574 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400575 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200576
577 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800578 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200579
Izik Eidus290fc382007-09-27 14:11:22 +0200580 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500581
Izik Eidus80b14b52007-10-25 11:54:04 +0200582 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200583 /*
584 * hva_to_rmmap() serialzies with the mmu_lock and to be
585 * safe it has to ignore memslots with !user_alloc &&
586 * !userspace_addr.
587 */
588 if (user_alloc)
589 new.userspace_addr = mem->userspace_addr;
590 else
591 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200593 if (!npages)
594 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300595
Joerg Roedelec04b262009-06-19 15:16:23 +0200596 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200597 unsigned long ugfn;
598 unsigned long j;
599 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200600 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300601
Joerg Roedelec04b262009-06-19 15:16:23 +0200602 /* Avoid unused variable warning if no large pages */
603 (void)level;
604
605 if (new.lpage_info[i])
606 continue;
607
608 lpages = 1 + (base_gfn + npages - 1) /
609 KVM_PAGES_PER_HPAGE(level);
610 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
611
612 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
613
614 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300615 goto out_free;
616
Joerg Roedelec04b262009-06-19 15:16:23 +0200617 memset(new.lpage_info[i], 0,
618 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300619
Joerg Roedelec04b262009-06-19 15:16:23 +0200620 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
621 new.lpage_info[i][0].write_count = 1;
622 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
623 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300624 ugfn = new.userspace_addr >> PAGE_SHIFT;
625 /*
626 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300627 * other, or if explicitly asked to, disable large page
628 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300629 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200630 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300631 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200632 for (j = 0; j < lpages; ++j)
633 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300634 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635
Joerg Roedelec04b262009-06-19 15:16:23 +0200636skip_lpage:
637
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 /* Allocate page dirty bitmap if needed */
639 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
640 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
641
642 new.dirty_bitmap = vmalloc(dirty_bytes);
643 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800644 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +0300646 if (old.npages)
647 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200649#else /* not defined CONFIG_S390 */
650 new.user_alloc = user_alloc;
651 if (user_alloc)
652 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200653#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300655 if (!npages)
656 kvm_arch_flush_shadow(kvm);
657
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200658 spin_lock(&kvm->mmu_lock);
659 if (mem->slot >= kvm->nmemslots)
660 kvm->nmemslots = mem->slot + 1;
661
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200663 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664
Zhang Xiantao0de10342007-11-20 16:25:04 +0800665 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
666 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200667 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800668 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200669 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800670 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800671 }
672
Glauber Costa6f897242008-12-03 13:40:51 -0200673 kvm_free_physmem_slot(&old, npages ? &new : NULL);
674 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300675 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -0200676 if (!npages)
677 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300678 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800679#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300680 /* map the pages in iommu page table */
681 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
682 if (r)
683 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800684#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 return 0;
686
Sheng Yangf78e0e22007-10-29 09:40:42 +0800687out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 kvm_free_physmem_slot(&new, &old);
689out:
690 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200691
692}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800693EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
694
695int kvm_set_memory_region(struct kvm *kvm,
696 struct kvm_userspace_memory_region *mem,
697 int user_alloc)
698{
699 int r;
700
Izik Eidus72dc67a2008-02-10 18:04:15 +0200701 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800702 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200703 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800704 return r;
705}
Izik Eidus210c7c42007-10-24 23:52:57 +0200706EXPORT_SYMBOL_GPL(kvm_set_memory_region);
707
Carsten Otte1fe779f2007-10-29 16:08:35 +0100708int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
709 struct
710 kvm_userspace_memory_region *mem,
711 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200712{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200713 if (mem->slot >= KVM_MEMORY_SLOTS)
714 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200715 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716}
717
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800718int kvm_get_dirty_log(struct kvm *kvm,
719 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720{
721 struct kvm_memory_slot *memslot;
722 int r, i;
723 int n;
724 unsigned long any = 0;
725
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 r = -EINVAL;
727 if (log->slot >= KVM_MEMORY_SLOTS)
728 goto out;
729
730 memslot = &kvm->memslots[log->slot];
731 r = -ENOENT;
732 if (!memslot->dirty_bitmap)
733 goto out;
734
Uri Lublincd1a4a92007-02-22 16:43:09 +0200735 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736
Uri Lublincd1a4a92007-02-22 16:43:09 +0200737 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 any = memslot->dirty_bitmap[i];
739
740 r = -EFAULT;
741 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
742 goto out;
743
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800744 if (any)
745 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746
747 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 return r;
750}
751
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300752void kvm_disable_largepages(void)
753{
754 largepages_enabled = false;
755}
756EXPORT_SYMBOL_GPL(kvm_disable_largepages);
757
Izik Eiduscea7bb22007-10-17 19:17:48 +0200758int is_error_page(struct page *page)
759{
760 return page == bad_page;
761}
762EXPORT_SYMBOL_GPL(is_error_page);
763
Anthony Liguori35149e22008-04-02 14:46:56 -0500764int is_error_pfn(pfn_t pfn)
765{
766 return pfn == bad_pfn;
767}
768EXPORT_SYMBOL_GPL(is_error_pfn);
769
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200770static inline unsigned long bad_hva(void)
771{
772 return PAGE_OFFSET;
773}
774
775int kvm_is_error_hva(unsigned long addr)
776{
777 return addr == bad_hva();
778}
779EXPORT_SYMBOL_GPL(kvm_is_error_hva);
780
Izik Eidus28430992008-10-03 17:40:32 +0300781struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782{
783 int i;
784
785 for (i = 0; i < kvm->nmemslots; ++i) {
786 struct kvm_memory_slot *memslot = &kvm->memslots[i];
787
788 if (gfn >= memslot->base_gfn
789 && gfn < memslot->base_gfn + memslot->npages)
790 return memslot;
791 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000792 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793}
Izik Eidus28430992008-10-03 17:40:32 +0300794EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300795
796struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
797{
798 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300799 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300800}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801
Izik Eiduse0d62c72007-10-24 23:57:46 +0200802int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
803{
804 int i;
805
806 gfn = unalias_gfn(kvm, gfn);
807 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
808 struct kvm_memory_slot *memslot = &kvm->memslots[i];
809
810 if (gfn >= memslot->base_gfn
811 && gfn < memslot->base_gfn + memslot->npages)
812 return 1;
813 }
814 return 0;
815}
816EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
817
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300818unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200819{
820 struct kvm_memory_slot *slot;
821
822 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300823 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200824 if (!slot)
825 return bad_hva();
826 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
827}
Sheng Yang0d150292008-04-25 21:44:50 +0800828EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200829
Anthony Liguori35149e22008-04-02 14:46:56 -0500830pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300831{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500832 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200833 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500834 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500835 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300836
Avi Kivity60395222007-10-21 11:03:36 +0200837 might_sleep();
838
Izik Eidus539cb662007-11-11 22:05:04 +0200839 addr = gfn_to_hva(kvm, gfn);
840 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200841 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500842 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200843 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200844
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300845 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200846
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500847 if (unlikely(npages != 1)) {
848 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500849
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300850 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500851 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300852
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500853 if (vma == NULL || addr < vma->vm_start ||
854 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300855 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500856 get_page(bad_page);
857 return page_to_pfn(bad_page);
858 }
859
860 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300861 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800862 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500863 } else
864 pfn = page_to_pfn(page[0]);
865
866 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500867}
868
869EXPORT_SYMBOL_GPL(gfn_to_pfn);
870
871struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
872{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500873 pfn_t pfn;
874
875 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800876 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500877 return pfn_to_page(pfn);
878
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800879 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500880
881 get_page(bad_page);
882 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300883}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500884
Avi Kivity954bbbc2007-03-30 14:02:32 +0300885EXPORT_SYMBOL_GPL(gfn_to_page);
886
Izik Eidusb4231d62007-11-20 11:49:33 +0200887void kvm_release_page_clean(struct page *page)
888{
Anthony Liguori35149e22008-04-02 14:46:56 -0500889 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200890}
891EXPORT_SYMBOL_GPL(kvm_release_page_clean);
892
Anthony Liguori35149e22008-04-02 14:46:56 -0500893void kvm_release_pfn_clean(pfn_t pfn)
894{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800895 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500896 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500897}
898EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
899
Izik Eidusb4231d62007-11-20 11:49:33 +0200900void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200901{
Anthony Liguori35149e22008-04-02 14:46:56 -0500902 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200903}
Izik Eidusb4231d62007-11-20 11:49:33 +0200904EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200905
Anthony Liguori35149e22008-04-02 14:46:56 -0500906void kvm_release_pfn_dirty(pfn_t pfn)
907{
908 kvm_set_pfn_dirty(pfn);
909 kvm_release_pfn_clean(pfn);
910}
911EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
912
913void kvm_set_page_dirty(struct page *page)
914{
915 kvm_set_pfn_dirty(page_to_pfn(page));
916}
917EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
918
919void kvm_set_pfn_dirty(pfn_t pfn)
920{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800921 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500922 struct page *page = pfn_to_page(pfn);
923 if (!PageReserved(page))
924 SetPageDirty(page);
925 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500926}
927EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
928
929void kvm_set_pfn_accessed(pfn_t pfn)
930{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800931 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500932 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500933}
934EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
935
936void kvm_get_pfn(pfn_t pfn)
937{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800938 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500939 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500940}
941EXPORT_SYMBOL_GPL(kvm_get_pfn);
942
Izik Eidus195aefd2007-10-01 22:14:18 +0200943static int next_segment(unsigned long len, int offset)
944{
945 if (len > PAGE_SIZE - offset)
946 return PAGE_SIZE - offset;
947 else
948 return len;
949}
950
951int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
952 int len)
953{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200954 int r;
955 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200956
Izik Eiduse0506bc2007-11-11 22:10:22 +0200957 addr = gfn_to_hva(kvm, gfn);
958 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200959 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200960 r = copy_from_user(data, (void __user *)addr + offset, len);
961 if (r)
962 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200963 return 0;
964}
965EXPORT_SYMBOL_GPL(kvm_read_guest_page);
966
967int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
968{
969 gfn_t gfn = gpa >> PAGE_SHIFT;
970 int seg;
971 int offset = offset_in_page(gpa);
972 int ret;
973
974 while ((seg = next_segment(len, offset)) != 0) {
975 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
976 if (ret < 0)
977 return ret;
978 offset = 0;
979 len -= seg;
980 data += seg;
981 ++gfn;
982 }
983 return 0;
984}
985EXPORT_SYMBOL_GPL(kvm_read_guest);
986
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500987int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
988 unsigned long len)
989{
990 int r;
991 unsigned long addr;
992 gfn_t gfn = gpa >> PAGE_SHIFT;
993 int offset = offset_in_page(gpa);
994
995 addr = gfn_to_hva(kvm, gfn);
996 if (kvm_is_error_hva(addr))
997 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100998 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500999 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001000 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001001 if (r)
1002 return -EFAULT;
1003 return 0;
1004}
1005EXPORT_SYMBOL(kvm_read_guest_atomic);
1006
Izik Eidus195aefd2007-10-01 22:14:18 +02001007int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1008 int offset, int len)
1009{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001010 int r;
1011 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001012
Izik Eiduse0506bc2007-11-11 22:10:22 +02001013 addr = gfn_to_hva(kvm, gfn);
1014 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001015 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001016 r = copy_to_user((void __user *)addr + offset, data, len);
1017 if (r)
1018 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001019 mark_page_dirty(kvm, gfn);
1020 return 0;
1021}
1022EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1023
1024int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1025 unsigned long len)
1026{
1027 gfn_t gfn = gpa >> PAGE_SHIFT;
1028 int seg;
1029 int offset = offset_in_page(gpa);
1030 int ret;
1031
1032 while ((seg = next_segment(len, offset)) != 0) {
1033 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1034 if (ret < 0)
1035 return ret;
1036 offset = 0;
1037 len -= seg;
1038 data += seg;
1039 ++gfn;
1040 }
1041 return 0;
1042}
1043
1044int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1045{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001046 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001047}
1048EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1049
1050int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1051{
1052 gfn_t gfn = gpa >> PAGE_SHIFT;
1053 int seg;
1054 int offset = offset_in_page(gpa);
1055 int ret;
1056
1057 while ((seg = next_segment(len, offset)) != 0) {
1058 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1059 if (ret < 0)
1060 return ret;
1061 offset = 0;
1062 len -= seg;
1063 ++gfn;
1064 }
1065 return 0;
1066}
1067EXPORT_SYMBOL_GPL(kvm_clear_guest);
1068
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1070{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001071 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072
Uri Lublin3b6fff12007-10-30 10:42:09 +02001073 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001074 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001075 if (memslot && memslot->dirty_bitmap) {
1076 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077
Rusty Russell7e9d6192007-07-31 20:41:14 +10001078 /* avoid RMW */
Alexander Grafc8240bd2009-10-30 05:47:26 +00001079 if (!generic_test_le_bit(rel_gfn, memslot->dirty_bitmap))
1080 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 }
1082}
1083
Eddie Dongb6958ce2007-07-18 12:15:21 +03001084/*
1085 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1086 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001087void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001088{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001089 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001090
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001091 for (;;) {
1092 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001093
Gleb Natapova1b37102009-07-09 15:33:52 +03001094 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001095 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001096 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001097 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001098 if (kvm_cpu_has_pending_timer(vcpu))
1099 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001100 if (signal_pending(current))
1101 break;
1102
Eddie Dongb6958ce2007-07-18 12:15:21 +03001103 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001104 }
1105
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001106 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001107}
1108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109void kvm_resched(struct kvm_vcpu *vcpu)
1110{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001111 if (!need_resched())
1112 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114}
1115EXPORT_SYMBOL_GPL(kvm_resched);
1116
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001117void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1118{
1119 ktime_t expires;
1120 DEFINE_WAIT(wait);
1121
1122 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1123
1124 /* Sleep for 100 us, and hope lock-holder got scheduled */
1125 expires = ktime_add_ns(ktime_get(), 100000UL);
1126 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1127
1128 finish_wait(&vcpu->wq, &wait);
1129}
1130EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1131
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001132static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001133{
1134 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001135 struct page *page;
1136
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001137 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001138 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001139#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001140 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001141 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001142#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001143#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1144 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1145 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1146#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001147 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001148 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001149 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001150 vmf->page = page;
1151 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001152}
1153
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001154static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001155 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001156};
1157
1158static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1159{
1160 vma->vm_ops = &kvm_vcpu_vm_ops;
1161 return 0;
1162}
1163
Avi Kivitybccf2152007-02-21 18:04:26 +02001164static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1165{
1166 struct kvm_vcpu *vcpu = filp->private_data;
1167
Al Viro66c0b392008-04-19 20:33:56 +01001168 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001169 return 0;
1170}
1171
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001172static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001173 .release = kvm_vcpu_release,
1174 .unlocked_ioctl = kvm_vcpu_ioctl,
1175 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001176 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001177};
1178
1179/*
1180 * Allocates an inode for the vcpu.
1181 */
1182static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1183{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001184 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001185}
1186
Avi Kivityc5ea7662007-02-20 18:41:05 +02001187/*
1188 * Creates some virtual cpus. Good luck creating more than one.
1189 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001190static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001191{
1192 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001193 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001194
Gleb Natapov73880c82009-06-09 15:56:28 +03001195 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001196 if (IS_ERR(vcpu))
1197 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001198
Avi Kivity15ad7142007-07-11 18:17:21 +03001199 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1200
Avi Kivity26e52152007-11-20 15:30:24 +02001201 r = kvm_arch_vcpu_setup(vcpu);
1202 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001203 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001204
Shaohua Li11ec2802007-07-23 14:51:37 +08001205 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001206 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1207 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001208 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001209 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001210
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001211 kvm_for_each_vcpu(r, v, kvm)
1212 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001213 r = -EEXIST;
1214 goto vcpu_destroy;
1215 }
1216
1217 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001218
1219 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001220 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001221 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001222 if (r < 0) {
1223 kvm_put_kvm(kvm);
1224 goto vcpu_destroy;
1225 }
1226
1227 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1228 smp_wmb();
1229 atomic_inc(&kvm->online_vcpus);
1230
1231#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1232 if (kvm->bsp_vcpu_id == id)
1233 kvm->bsp_vcpu = vcpu;
1234#endif
1235 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001236 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001237
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001238vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001239 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001240 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001241 return r;
1242}
1243
Avi Kivity1961d272007-03-05 19:46:05 +02001244static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1245{
1246 if (sigset) {
1247 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1248 vcpu->sigset_active = 1;
1249 vcpu->sigset = *sigset;
1250 } else
1251 vcpu->sigset_active = 0;
1252 return 0;
1253}
1254
Avi Kivitybccf2152007-02-21 18:04:26 +02001255static long kvm_vcpu_ioctl(struct file *filp,
1256 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257{
Avi Kivitybccf2152007-02-21 18:04:26 +02001258 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001259 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001260 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001261 struct kvm_fpu *fpu = NULL;
1262 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001264 if (vcpu->kvm->mm != current->mm)
1265 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001267 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001268 r = -EINVAL;
1269 if (arg)
1270 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001271 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001274 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001276 r = -ENOMEM;
1277 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1278 if (!kvm_regs)
1279 goto out;
1280 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001282 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001284 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1285 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001287out_free1:
1288 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 break;
1290 }
1291 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001292 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001294 r = -ENOMEM;
1295 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1296 if (!kvm_regs)
1297 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001299 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1300 goto out_free2;
1301 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001303 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001305out_free2:
1306 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 break;
1308 }
1309 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001310 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1311 r = -ENOMEM;
1312 if (!kvm_sregs)
1313 goto out;
1314 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 if (r)
1316 goto out;
1317 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001318 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 goto out;
1320 r = 0;
1321 break;
1322 }
1323 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001324 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1325 r = -ENOMEM;
1326 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001328 r = -EFAULT;
1329 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1330 goto out;
1331 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 if (r)
1333 goto out;
1334 r = 0;
1335 break;
1336 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001337 case KVM_GET_MP_STATE: {
1338 struct kvm_mp_state mp_state;
1339
1340 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1341 if (r)
1342 goto out;
1343 r = -EFAULT;
1344 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1345 goto out;
1346 r = 0;
1347 break;
1348 }
1349 case KVM_SET_MP_STATE: {
1350 struct kvm_mp_state mp_state;
1351
1352 r = -EFAULT;
1353 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1354 goto out;
1355 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1356 if (r)
1357 goto out;
1358 r = 0;
1359 break;
1360 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 case KVM_TRANSLATE: {
1362 struct kvm_translation tr;
1363
1364 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001365 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001367 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 if (r)
1369 goto out;
1370 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001371 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 goto out;
1373 r = 0;
1374 break;
1375 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001376 case KVM_SET_GUEST_DEBUG: {
1377 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
1379 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001380 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001382 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 if (r)
1384 goto out;
1385 r = 0;
1386 break;
1387 }
Avi Kivity1961d272007-03-05 19:46:05 +02001388 case KVM_SET_SIGNAL_MASK: {
1389 struct kvm_signal_mask __user *sigmask_arg = argp;
1390 struct kvm_signal_mask kvm_sigmask;
1391 sigset_t sigset, *p;
1392
1393 p = NULL;
1394 if (argp) {
1395 r = -EFAULT;
1396 if (copy_from_user(&kvm_sigmask, argp,
1397 sizeof kvm_sigmask))
1398 goto out;
1399 r = -EINVAL;
1400 if (kvm_sigmask.len != sizeof sigset)
1401 goto out;
1402 r = -EFAULT;
1403 if (copy_from_user(&sigset, sigmask_arg->sigset,
1404 sizeof sigset))
1405 goto out;
1406 p = &sigset;
1407 }
1408 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1409 break;
1410 }
Avi Kivityb8836732007-04-01 16:34:31 +03001411 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001412 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1413 r = -ENOMEM;
1414 if (!fpu)
1415 goto out;
1416 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001417 if (r)
1418 goto out;
1419 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001420 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001421 goto out;
1422 r = 0;
1423 break;
1424 }
1425 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001426 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1427 r = -ENOMEM;
1428 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001429 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001430 r = -EFAULT;
1431 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1432 goto out;
1433 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001434 if (r)
1435 goto out;
1436 r = 0;
1437 break;
1438 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001439 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001440 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001441 }
1442out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001443 kfree(fpu);
1444 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001445 return r;
1446}
1447
1448static long kvm_vm_ioctl(struct file *filp,
1449 unsigned int ioctl, unsigned long arg)
1450{
1451 struct kvm *kvm = filp->private_data;
1452 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001453 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001454
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001455 if (kvm->mm != current->mm)
1456 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001457 switch (ioctl) {
1458 case KVM_CREATE_VCPU:
1459 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1460 if (r < 0)
1461 goto out;
1462 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001463 case KVM_SET_USER_MEMORY_REGION: {
1464 struct kvm_userspace_memory_region kvm_userspace_mem;
1465
1466 r = -EFAULT;
1467 if (copy_from_user(&kvm_userspace_mem, argp,
1468 sizeof kvm_userspace_mem))
1469 goto out;
1470
1471 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 if (r)
1473 goto out;
1474 break;
1475 }
1476 case KVM_GET_DIRTY_LOG: {
1477 struct kvm_dirty_log log;
1478
1479 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001480 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001482 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 if (r)
1484 goto out;
1485 break;
1486 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001487#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1488 case KVM_REGISTER_COALESCED_MMIO: {
1489 struct kvm_coalesced_mmio_zone zone;
1490 r = -EFAULT;
1491 if (copy_from_user(&zone, argp, sizeof zone))
1492 goto out;
1493 r = -ENXIO;
1494 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1495 if (r)
1496 goto out;
1497 r = 0;
1498 break;
1499 }
1500 case KVM_UNREGISTER_COALESCED_MMIO: {
1501 struct kvm_coalesced_mmio_zone zone;
1502 r = -EFAULT;
1503 if (copy_from_user(&zone, argp, sizeof zone))
1504 goto out;
1505 r = -ENXIO;
1506 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1507 if (r)
1508 goto out;
1509 r = 0;
1510 break;
1511 }
1512#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001513 case KVM_IRQFD: {
1514 struct kvm_irqfd data;
1515
1516 r = -EFAULT;
1517 if (copy_from_user(&data, argp, sizeof data))
1518 goto out;
1519 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1520 break;
1521 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001522 case KVM_IOEVENTFD: {
1523 struct kvm_ioeventfd data;
1524
1525 r = -EFAULT;
1526 if (copy_from_user(&data, argp, sizeof data))
1527 goto out;
1528 r = kvm_ioeventfd(kvm, &data);
1529 break;
1530 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001531#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1532 case KVM_SET_BOOT_CPU_ID:
1533 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001534 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001535 if (atomic_read(&kvm->online_vcpus) != 0)
1536 r = -EBUSY;
1537 else
1538 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001539 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001540 break;
1541#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001542 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001543 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001544 if (r == -ENOTTY)
1545 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001546 }
1547out:
1548 return r;
1549}
1550
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001551#ifdef CONFIG_COMPAT
1552struct compat_kvm_dirty_log {
1553 __u32 slot;
1554 __u32 padding1;
1555 union {
1556 compat_uptr_t dirty_bitmap; /* one bit per page */
1557 __u64 padding2;
1558 };
1559};
1560
1561static long kvm_vm_compat_ioctl(struct file *filp,
1562 unsigned int ioctl, unsigned long arg)
1563{
1564 struct kvm *kvm = filp->private_data;
1565 int r;
1566
1567 if (kvm->mm != current->mm)
1568 return -EIO;
1569 switch (ioctl) {
1570 case KVM_GET_DIRTY_LOG: {
1571 struct compat_kvm_dirty_log compat_log;
1572 struct kvm_dirty_log log;
1573
1574 r = -EFAULT;
1575 if (copy_from_user(&compat_log, (void __user *)arg,
1576 sizeof(compat_log)))
1577 goto out;
1578 log.slot = compat_log.slot;
1579 log.padding1 = compat_log.padding1;
1580 log.padding2 = compat_log.padding2;
1581 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1582
1583 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1584 if (r)
1585 goto out;
1586 break;
1587 }
1588 default:
1589 r = kvm_vm_ioctl(filp, ioctl, arg);
1590 }
1591
1592out:
1593 return r;
1594}
1595#endif
1596
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001597static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001598{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001599 struct page *page[1];
1600 unsigned long addr;
1601 int npages;
1602 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001603 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001604
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001605 addr = gfn_to_hva(kvm, gfn);
1606 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001607 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001608
1609 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1610 NULL);
1611 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001612 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001613
1614 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001615 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001616}
1617
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001618static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001619 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001620};
1621
1622static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1623{
1624 vma->vm_ops = &kvm_vm_vm_ops;
1625 return 0;
1626}
1627
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001628static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001629 .release = kvm_vm_release,
1630 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001631#ifdef CONFIG_COMPAT
1632 .compat_ioctl = kvm_vm_compat_ioctl,
1633#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001634 .mmap = kvm_vm_mmap,
1635};
1636
1637static int kvm_dev_ioctl_create_vm(void)
1638{
Al Viro2030a422008-02-23 06:46:49 -05001639 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001640 struct kvm *kvm;
1641
Avi Kivityf17abe92007-02-21 19:28:04 +02001642 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001643 if (IS_ERR(kvm))
1644 return PTR_ERR(kvm);
Roland Dreier628ff7c2009-12-18 09:41:24 -08001645 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
Al Viro2030a422008-02-23 06:46:49 -05001646 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001647 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001648
Avi Kivityf17abe92007-02-21 19:28:04 +02001649 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001650}
1651
Avi Kivity1a811b62008-12-08 18:25:27 +02001652static long kvm_dev_ioctl_check_extension_generic(long arg)
1653{
1654 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001655 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001656 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001657 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001658#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1659 case KVM_CAP_SET_BOOT_CPU_ID:
1660#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001661 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001662 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001663#ifdef CONFIG_HAVE_KVM_IRQCHIP
1664 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001665 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001666#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001667 default:
1668 break;
1669 }
1670 return kvm_dev_ioctl_check_extension(arg);
1671}
1672
Avi Kivityf17abe92007-02-21 19:28:04 +02001673static long kvm_dev_ioctl(struct file *filp,
1674 unsigned int ioctl, unsigned long arg)
1675{
Avi Kivity07c45a32007-03-07 13:05:38 +02001676 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001677
1678 switch (ioctl) {
1679 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001680 r = -EINVAL;
1681 if (arg)
1682 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001683 r = KVM_API_VERSION;
1684 break;
1685 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001686 r = -EINVAL;
1687 if (arg)
1688 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001689 r = kvm_dev_ioctl_create_vm();
1690 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001691 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001692 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001693 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001694 case KVM_GET_VCPU_MMAP_SIZE:
1695 r = -EINVAL;
1696 if (arg)
1697 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001698 r = PAGE_SIZE; /* struct kvm_run */
1699#ifdef CONFIG_X86
1700 r += PAGE_SIZE; /* pio data page */
1701#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001702#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1703 r += PAGE_SIZE; /* coalesced mmio ring page */
1704#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001705 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001706 case KVM_TRACE_ENABLE:
1707 case KVM_TRACE_PAUSE:
1708 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001709 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001710 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001712 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 }
1714out:
1715 return r;
1716}
1717
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719 .unlocked_ioctl = kvm_dev_ioctl,
1720 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721};
1722
1723static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001724 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 "kvm",
1726 &kvm_chardev_ops,
1727};
1728
Avi Kivity1b6c0162007-05-24 13:03:52 +03001729static void hardware_enable(void *junk)
1730{
1731 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02001732 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03001733
Rusty Russell7f59f492008-12-07 21:25:45 +10301734 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001735 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02001736
Rusty Russell7f59f492008-12-07 21:25:45 +10301737 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02001738
1739 r = kvm_arch_hardware_enable(NULL);
1740
1741 if (r) {
1742 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1743 atomic_inc(&hardware_enable_failed);
1744 printk(KERN_INFO "kvm: enabling virtualization on "
1745 "CPU%d failed\n", cpu);
1746 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03001747}
1748
1749static void hardware_disable(void *junk)
1750{
1751 int cpu = raw_smp_processor_id();
1752
Rusty Russell7f59f492008-12-07 21:25:45 +10301753 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001754 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301755 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001756 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001757}
1758
Alexander Graf10474ae2009-09-15 11:37:46 +02001759static void hardware_disable_all_nolock(void)
1760{
1761 BUG_ON(!kvm_usage_count);
1762
1763 kvm_usage_count--;
1764 if (!kvm_usage_count)
1765 on_each_cpu(hardware_disable, NULL, 1);
1766}
1767
1768static void hardware_disable_all(void)
1769{
1770 spin_lock(&kvm_lock);
1771 hardware_disable_all_nolock();
1772 spin_unlock(&kvm_lock);
1773}
1774
1775static int hardware_enable_all(void)
1776{
1777 int r = 0;
1778
1779 spin_lock(&kvm_lock);
1780
1781 kvm_usage_count++;
1782 if (kvm_usage_count == 1) {
1783 atomic_set(&hardware_enable_failed, 0);
1784 on_each_cpu(hardware_enable, NULL, 1);
1785
1786 if (atomic_read(&hardware_enable_failed)) {
1787 hardware_disable_all_nolock();
1788 r = -EBUSY;
1789 }
1790 }
1791
1792 spin_unlock(&kvm_lock);
1793
1794 return r;
1795}
1796
Avi Kivity774c47f2007-02-12 00:54:47 -08001797static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1798 void *v)
1799{
1800 int cpu = (long)v;
1801
Alexander Graf10474ae2009-09-15 11:37:46 +02001802 if (!kvm_usage_count)
1803 return NOTIFY_OK;
1804
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001805 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001806 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001807 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001808 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1809 cpu);
1810 hardware_disable(NULL);
1811 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001812 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001813 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1814 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001815 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001816 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001817 case CPU_ONLINE:
1818 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1819 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001820 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001821 break;
1822 }
1823 return NOTIFY_OK;
1824}
1825
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001826
1827asmlinkage void kvm_handle_fault_on_reboot(void)
1828{
1829 if (kvm_rebooting)
1830 /* spin while reset goes on */
1831 while (true)
1832 ;
1833 /* Fault while not rebooting. We want the trace. */
1834 BUG();
1835}
1836EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1837
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001838static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001839 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001840{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001841 /*
1842 * Some (well, at least mine) BIOSes hang on reboot if
1843 * in vmx root mode.
1844 *
1845 * And Intel TXT required VMX off for all cpu when system shutdown.
1846 */
1847 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1848 kvm_rebooting = true;
1849 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001850 return NOTIFY_OK;
1851}
1852
1853static struct notifier_block kvm_reboot_notifier = {
1854 .notifier_call = kvm_reboot,
1855 .priority = 0,
1856};
1857
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001858void kvm_io_bus_init(struct kvm_io_bus *bus)
1859{
1860 memset(bus, 0, sizeof(*bus));
1861}
1862
1863void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1864{
1865 int i;
1866
1867 for (i = 0; i < bus->dev_count; i++) {
1868 struct kvm_io_device *pos = bus->devs[i];
1869
1870 kvm_iodevice_destructor(pos);
1871 }
1872}
1873
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001874/* kvm_io_bus_write - called under kvm->slots_lock */
1875int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
1876 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001877{
1878 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001879 for (i = 0; i < bus->dev_count; i++)
1880 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1881 return 0;
1882 return -EOPNOTSUPP;
1883}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001884
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001885/* kvm_io_bus_read - called under kvm->slots_lock */
1886int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
1887{
1888 int i;
1889 for (i = 0; i < bus->dev_count; i++)
1890 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
1891 return 0;
1892 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001893}
1894
Gregory Haskins090b7af2009-07-07 17:08:44 -04001895int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001896 struct kvm_io_device *dev)
1897{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001898 int ret;
1899
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001900 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04001901 ret = __kvm_io_bus_register_dev(bus, dev);
1902 up_write(&kvm->slots_lock);
1903
1904 return ret;
1905}
1906
1907/* An unlocked version. Caller must have write lock on slots_lock. */
1908int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
1909 struct kvm_io_device *dev)
1910{
1911 if (bus->dev_count > NR_IOBUS_DEVS-1)
1912 return -ENOSPC;
1913
1914 bus->devs[bus->dev_count++] = dev;
1915
1916 return 0;
1917}
1918
1919void kvm_io_bus_unregister_dev(struct kvm *kvm,
1920 struct kvm_io_bus *bus,
1921 struct kvm_io_device *dev)
1922{
1923 down_write(&kvm->slots_lock);
1924 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001925 up_write(&kvm->slots_lock);
1926}
1927
1928/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04001929void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
1930 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001931{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001932 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001933
Gregory Haskins090b7af2009-07-07 17:08:44 -04001934 for (i = 0; i < bus->dev_count; i++)
1935 if (bus->devs[i] == dev) {
1936 bus->devs[i] = bus->devs[--bus->dev_count];
1937 break;
1938 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001939}
1940
Avi Kivity774c47f2007-02-12 00:54:47 -08001941static struct notifier_block kvm_cpu_notifier = {
1942 .notifier_call = kvm_cpu_hotplug,
1943 .priority = 20, /* must be > scheduler priority */
1944};
1945
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001946static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001947{
1948 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001949 struct kvm *kvm;
1950
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001951 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001952 spin_lock(&kvm_lock);
1953 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001954 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001955 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001956 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001957}
1958
1959DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1960
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001961static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001962{
1963 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001964 struct kvm *kvm;
1965 struct kvm_vcpu *vcpu;
1966 int i;
1967
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001968 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001969 spin_lock(&kvm_lock);
1970 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001971 kvm_for_each_vcpu(i, vcpu, kvm)
1972 *val += *(u32 *)((void *)vcpu + offset);
1973
Avi Kivity1165f5f2007-04-19 17:27:43 +03001974 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001975 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001976}
1977
Avi Kivityba1389b2007-11-18 16:24:12 +02001978DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1979
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001980static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02001981 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1982 [KVM_STAT_VM] = &vm_stat_fops,
1983};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001984
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001985static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986{
1987 struct kvm_stats_debugfs_item *p;
1988
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001989 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001991 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001992 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001993 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001994}
1995
1996static void kvm_exit_debug(void)
1997{
1998 struct kvm_stats_debugfs_item *p;
1999
2000 for (p = debugfs_entries; p->name; ++p)
2001 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002002 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003}
2004
Avi Kivity59ae6c62007-02-12 00:54:48 -08002005static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2006{
Alexander Graf10474ae2009-09-15 11:37:46 +02002007 if (kvm_usage_count)
2008 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002009 return 0;
2010}
2011
2012static int kvm_resume(struct sys_device *dev)
2013{
Alexander Graf10474ae2009-09-15 11:37:46 +02002014 if (kvm_usage_count)
2015 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002016 return 0;
2017}
2018
2019static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002020 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002021 .suspend = kvm_suspend,
2022 .resume = kvm_resume,
2023};
2024
2025static struct sys_device kvm_sysdev = {
2026 .id = 0,
2027 .cls = &kvm_sysdev_class,
2028};
2029
Izik Eiduscea7bb22007-10-17 19:17:48 +02002030struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002031pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032
Avi Kivity15ad7142007-07-11 18:17:21 +03002033static inline
2034struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2035{
2036 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2037}
2038
2039static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2040{
2041 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2042
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002043 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002044}
2045
2046static void kvm_sched_out(struct preempt_notifier *pn,
2047 struct task_struct *next)
2048{
2049 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2050
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002051 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002052}
2053
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002054int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002055 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056{
2057 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002058 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002060 r = kvm_arch_init(opaque);
2061 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002062 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002063
2064 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2065
2066 if (bad_page == NULL) {
2067 r = -ENOMEM;
2068 goto out;
2069 }
2070
Anthony Liguori35149e22008-04-02 14:46:56 -05002071 bad_pfn = page_to_pfn(bad_page);
2072
Avi Kivity8437a6172009-06-06 14:52:35 -07002073 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302074 r = -ENOMEM;
2075 goto out_free_0;
2076 }
2077
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002078 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302080 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081
Yang, Sheng002c7f72007-07-31 14:23:01 +03002082 for_each_online_cpu(cpu) {
2083 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002084 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002085 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002086 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002087 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002088 }
2089
Avi Kivity774c47f2007-02-12 00:54:47 -08002090 r = register_cpu_notifier(&kvm_cpu_notifier);
2091 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002092 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 register_reboot_notifier(&kvm_reboot_notifier);
2094
Avi Kivity59ae6c62007-02-12 00:54:48 -08002095 r = sysdev_class_register(&kvm_sysdev_class);
2096 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002097 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002098
2099 r = sysdev_register(&kvm_sysdev);
2100 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002101 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002102
Rusty Russellc16f8622007-07-30 21:12:19 +10002103 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2104 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002105 __alignof__(struct kvm_vcpu),
2106 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002107 if (!kvm_vcpu_cache) {
2108 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002109 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002110 }
2111
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002113 kvm_vm_fops.owner = module;
2114 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
2116 r = misc_register(&kvm_dev);
2117 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002118 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 goto out_free;
2120 }
2121
Avi Kivity15ad7142007-07-11 18:17:21 +03002122 kvm_preempt_ops.sched_in = kvm_sched_in;
2123 kvm_preempt_ops.sched_out = kvm_sched_out;
2124
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002125 kvm_init_debug();
2126
Avi Kivityc7addb92007-09-16 18:58:32 +02002127 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128
2129out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002130 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002131out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002132 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002133out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002134 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002135out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002137 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002138out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002139out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002140 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302141out_free_0a:
2142 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002143out_free_0:
2144 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002145out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002146 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002147out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 return r;
2149}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002150EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002152void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002154 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002155 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002157 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002158 sysdev_unregister(&kvm_sysdev);
2159 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002161 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002162 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002163 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002164 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302165 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002166 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002168EXPORT_SYMBOL_GPL(kvm_exit);