blob: f92ba138007ace7a7669492d9852055322e69b64 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
Laurent Vivier5f94c172008-05-30 16:05:54 +020053#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
54#include "coalesced_mmio.h"
55#endif
56
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#define CREATE_TRACE_POINTS
58#include <trace/events/kvm.h>
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
Marcelo Tosattifa40a822009-06-04 15:08:24 -030063/*
64 * Ordering of locks:
65 *
Michael S. Tsirkin22fc0292009-06-29 22:24:45 +030066 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030067 */
68
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080069DEFINE_SPINLOCK(kvm_lock);
70LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080071
Rusty Russell7f59f492008-12-07 21:25:45 +103072static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020073static int kvm_usage_count = 0;
74static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030075
Rusty Russellc16f8622007-07-30 21:12:19 +100076struct kmem_cache *kvm_vcpu_cache;
77EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030078
Avi Kivity15ad7142007-07-11 18:17:21 +030079static __read_mostly struct preempt_ops kvm_preempt_ops;
80
Hollis Blanchard76f7c872008-04-15 16:05:42 -050081struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivitybccf2152007-02-21 18:04:26 +020083static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
84 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020085static int hardware_enable_all(void);
86static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020087
Hannes Edere8ba5d32008-11-28 17:02:42 +010088static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030089
Marcelo Tosatti54dee992009-06-11 12:07:44 -030090static bool largepages_enabled = true;
91
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +080092inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030093{
Joerg Roedelfc5659c2009-02-18 14:08:58 +010094 if (pfn_valid(pfn)) {
95 struct page *page = compound_head(pfn_to_page(pfn));
96 return PageReserved(page);
97 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030098
99 return true;
100}
101
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102/*
103 * Switches to specified vcpu, until a matching vcpu_put()
104 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200105void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106{
Avi Kivity15ad7142007-07-11 18:17:21 +0300107 int cpu;
108
Avi Kivitybccf2152007-02-21 18:04:26 +0200109 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300110 cpu = get_cpu();
111 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200112 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300113 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200114}
115
Carsten Otte313a3dc2007-10-11 19:16:52 +0200116void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117{
Avi Kivity15ad7142007-07-11 18:17:21 +0300118 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200119 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300120 preempt_notifier_unregister(&vcpu->preempt_notifier);
121 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122 mutex_unlock(&vcpu->mutex);
123}
124
Avi Kivityd9e368d2007-06-07 19:18:30 +0300125static void ack_flush(void *_completed)
126{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300127}
128
Rusty Russell49846892008-12-08 20:26:24 +1030129static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300130{
Avi Kivity597a5f52008-07-20 14:24:22 +0300131 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030132 cpumask_var_t cpus;
133 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300134 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300135
Li Zefan79f55992009-06-15 14:58:26 +0800136 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030137
Marcelo Tosatti84261922009-06-17 10:53:47 -0300138 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200139 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300140 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030141 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300142 continue;
143 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030144 if (cpus != NULL && cpu != -1 && cpu != me)
145 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300146 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030147 if (unlikely(cpus == NULL))
148 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
149 else if (!cpumask_empty(cpus))
150 smp_call_function_many(cpus, ack_flush, NULL, 1);
151 else
152 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300153 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030154 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030155 return called;
156}
157
158void kvm_flush_remote_tlbs(struct kvm *kvm)
159{
160 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
161 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300162}
163
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500164void kvm_reload_remote_mmus(struct kvm *kvm)
165{
Rusty Russell49846892008-12-08 20:26:24 +1030166 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500167}
168
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000169int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
170{
171 struct page *page;
172 int r;
173
174 mutex_init(&vcpu->mutex);
175 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000176 vcpu->kvm = kvm;
177 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300178 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000179
180 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
181 if (!page) {
182 r = -ENOMEM;
183 goto fail;
184 }
185 vcpu->run = page_address(page);
186
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800187 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000188 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800189 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000190 return 0;
191
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000192fail_free_run:
193 free_page((unsigned long)vcpu->run);
194fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000195 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000196}
197EXPORT_SYMBOL_GPL(kvm_vcpu_init);
198
199void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
200{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800201 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000202 free_page((unsigned long)vcpu->run);
203}
204EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
205
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200206#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
207static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
208{
209 return container_of(mn, struct kvm, mmu_notifier);
210}
211
212static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
213 struct mm_struct *mm,
214 unsigned long address)
215{
216 struct kvm *kvm = mmu_notifier_to_kvm(mn);
217 int need_tlb_flush;
218
219 /*
220 * When ->invalidate_page runs, the linux pte has been zapped
221 * already but the page is still allocated until
222 * ->invalidate_page returns. So if we increase the sequence
223 * here the kvm page fault will notice if the spte can't be
224 * established because the page is going to be freed. If
225 * instead the kvm page fault establishes the spte before
226 * ->invalidate_page runs, kvm_unmap_hva will release it
227 * before returning.
228 *
229 * The sequence increase only need to be seen at spin_unlock
230 * time, and not at spin_lock time.
231 *
232 * Increasing the sequence after the spin_unlock would be
233 * unsafe because the kvm page fault could then establish the
234 * pte after kvm_unmap_hva returned, without noticing the page
235 * is going to be freed.
236 */
237 spin_lock(&kvm->mmu_lock);
238 kvm->mmu_notifier_seq++;
239 need_tlb_flush = kvm_unmap_hva(kvm, address);
240 spin_unlock(&kvm->mmu_lock);
241
242 /* we've to flush the tlb before the pages can be freed */
243 if (need_tlb_flush)
244 kvm_flush_remote_tlbs(kvm);
245
246}
247
Izik Eidus3da0dd42009-09-23 21:47:18 +0300248static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
249 struct mm_struct *mm,
250 unsigned long address,
251 pte_t pte)
252{
253 struct kvm *kvm = mmu_notifier_to_kvm(mn);
254
255 spin_lock(&kvm->mmu_lock);
256 kvm->mmu_notifier_seq++;
257 kvm_set_spte_hva(kvm, address, pte);
258 spin_unlock(&kvm->mmu_lock);
259}
260
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200261static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
262 struct mm_struct *mm,
263 unsigned long start,
264 unsigned long end)
265{
266 struct kvm *kvm = mmu_notifier_to_kvm(mn);
267 int need_tlb_flush = 0;
268
269 spin_lock(&kvm->mmu_lock);
270 /*
271 * The count increase must become visible at unlock time as no
272 * spte can be established without taking the mmu_lock and
273 * count is also read inside the mmu_lock critical section.
274 */
275 kvm->mmu_notifier_count++;
276 for (; start < end; start += PAGE_SIZE)
277 need_tlb_flush |= kvm_unmap_hva(kvm, start);
278 spin_unlock(&kvm->mmu_lock);
279
280 /* we've to flush the tlb before the pages can be freed */
281 if (need_tlb_flush)
282 kvm_flush_remote_tlbs(kvm);
283}
284
285static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
286 struct mm_struct *mm,
287 unsigned long start,
288 unsigned long end)
289{
290 struct kvm *kvm = mmu_notifier_to_kvm(mn);
291
292 spin_lock(&kvm->mmu_lock);
293 /*
294 * This sequence increase will notify the kvm page fault that
295 * the page that is going to be mapped in the spte could have
296 * been freed.
297 */
298 kvm->mmu_notifier_seq++;
299 /*
300 * The above sequence increase must be visible before the
301 * below count decrease but both values are read by the kvm
302 * page fault under mmu_lock spinlock so we don't need to add
303 * a smb_wmb() here in between the two.
304 */
305 kvm->mmu_notifier_count--;
306 spin_unlock(&kvm->mmu_lock);
307
308 BUG_ON(kvm->mmu_notifier_count < 0);
309}
310
311static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
312 struct mm_struct *mm,
313 unsigned long address)
314{
315 struct kvm *kvm = mmu_notifier_to_kvm(mn);
316 int young;
317
318 spin_lock(&kvm->mmu_lock);
319 young = kvm_age_hva(kvm, address);
320 spin_unlock(&kvm->mmu_lock);
321
322 if (young)
323 kvm_flush_remote_tlbs(kvm);
324
325 return young;
326}
327
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100328static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
329 struct mm_struct *mm)
330{
331 struct kvm *kvm = mmu_notifier_to_kvm(mn);
332 kvm_arch_flush_shadow(kvm);
333}
334
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200335static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
336 .invalidate_page = kvm_mmu_notifier_invalidate_page,
337 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
338 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
339 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300340 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100341 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200342};
343#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
344
Avi Kivityf17abe92007-02-21 19:28:04 +0200345static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346{
Alexander Graf10474ae2009-09-15 11:37:46 +0200347 int r = 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800348 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200349#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
350 struct page *page;
351#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800353 if (IS_ERR(kvm))
354 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200355
356 r = hardware_enable_all();
357 if (r)
358 goto out_err_nodisable;
359
Avi Kivity75858a82009-01-04 17:10:50 +0200360#ifdef CONFIG_HAVE_KVM_IRQCHIP
361 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300362 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200363#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364
Laurent Vivier5f94c172008-05-30 16:05:54 +0200365#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
366 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
367 if (!page) {
Alexander Graf10474ae2009-09-15 11:37:46 +0200368 r = -ENOMEM;
369 goto out_err;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200370 }
371 kvm->coalesced_mmio_ring =
372 (struct kvm_coalesced_mmio_ring *)page_address(page);
373#endif
374
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200375#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
376 {
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200377 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
Alexander Graf10474ae2009-09-15 11:37:46 +0200378 r = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
379 if (r) {
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200380#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
381 put_page(page);
382#endif
Alexander Graf10474ae2009-09-15 11:37:46 +0200383 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200384 }
385 }
386#endif
387
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200388 kvm->mm = current->mm;
389 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500390 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300391 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300392 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400393 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800394 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300395 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400396 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200397 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300398 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000399 spin_lock(&kvm_lock);
400 list_add(&kvm->vm_list, &vm_list);
401 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200402#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
403 kvm_coalesced_mmio_init(kvm);
404#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800405out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200406 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200407
408out_err:
409 hardware_disable_all();
410out_err_nodisable:
411 kfree(kvm);
412 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200413}
414
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415/*
416 * Free any memory in @free but not in @dont.
417 */
418static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
419 struct kvm_memory_slot *dont)
420{
Joerg Roedelec04b262009-06-19 15:16:23 +0200421 int i;
422
Izik Eidus290fc382007-09-27 14:11:22 +0200423 if (!dont || free->rmap != dont->rmap)
424 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425
426 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
427 vfree(free->dirty_bitmap);
428
Joerg Roedelec04b262009-06-19 15:16:23 +0200429
430 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
431 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
432 vfree(free->lpage_info[i]);
433 free->lpage_info[i] = NULL;
434 }
435 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000438 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500439 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440}
441
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800442void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443{
444 int i;
445
446 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000447 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448}
449
Avi Kivityf17abe92007-02-21 19:28:04 +0200450static void kvm_destroy_vm(struct kvm *kvm)
451{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200452 struct mm_struct *mm = kvm->mm;
453
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800454 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800455 spin_lock(&kvm_lock);
456 list_del(&kvm->vm_list);
457 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200458 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +0300459 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400460 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200461#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
462 if (kvm->coalesced_mmio_ring != NULL)
463 free_page((unsigned long)kvm->coalesced_mmio_ring);
464#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200465#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
466 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200467#else
468 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200469#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800470 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200471 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200472 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200473}
474
Izik Eidusd39f13b2008-03-30 16:01:25 +0300475void kvm_get_kvm(struct kvm *kvm)
476{
477 atomic_inc(&kvm->users_count);
478}
479EXPORT_SYMBOL_GPL(kvm_get_kvm);
480
481void kvm_put_kvm(struct kvm *kvm)
482{
483 if (atomic_dec_and_test(&kvm->users_count))
484 kvm_destroy_vm(kvm);
485}
486EXPORT_SYMBOL_GPL(kvm_put_kvm);
487
488
Avi Kivityf17abe92007-02-21 19:28:04 +0200489static int kvm_vm_release(struct inode *inode, struct file *filp)
490{
491 struct kvm *kvm = filp->private_data;
492
Gregory Haskins721eecb2009-05-20 10:30:49 -0400493 kvm_irqfd_release(kvm);
494
Izik Eidusd39f13b2008-03-30 16:01:25 +0300495 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 return 0;
497}
498
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 * Allocate some memory and give it an address in the guest physical address
501 * space.
502 *
503 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800504 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500505 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800507int __kvm_set_memory_region(struct kvm *kvm,
508 struct kvm_userspace_memory_region *mem,
509 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510{
511 int r;
512 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200513 unsigned long npages;
514 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 struct kvm_memory_slot *memslot;
516 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517
518 r = -EINVAL;
519 /* General sanity checks */
520 if (mem->memory_size & (PAGE_SIZE - 1))
521 goto out;
522 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
523 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800524 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600525 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200526 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 goto out;
528 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
529 goto out;
530
531 memslot = &kvm->memslots[mem->slot];
532 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
533 npages = mem->memory_size >> PAGE_SHIFT;
534
535 if (!npages)
536 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
537
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 new = old = *memslot;
539
540 new.base_gfn = base_gfn;
541 new.npages = npages;
542 new.flags = mem->flags;
543
544 /* Disallow changing a memory slot's size. */
545 r = -EINVAL;
546 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800547 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548
549 /* Check for overlaps */
550 r = -EEXIST;
551 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
552 struct kvm_memory_slot *s = &kvm->memslots[i];
553
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200554 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 continue;
556 if (!((base_gfn + npages <= s->base_gfn) ||
557 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800558 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 /* Free page dirty bitmap if unneeded */
562 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000563 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564
565 r = -ENOMEM;
566
567 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200568#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500569 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400570 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200571
572 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800573 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200574
Izik Eidus290fc382007-09-27 14:11:22 +0200575 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500576
Izik Eidus80b14b52007-10-25 11:54:04 +0200577 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200578 /*
579 * hva_to_rmmap() serialzies with the mmu_lock and to be
580 * safe it has to ignore memslots with !user_alloc &&
581 * !userspace_addr.
582 */
583 if (user_alloc)
584 new.userspace_addr = mem->userspace_addr;
585 else
586 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200588 if (!npages)
589 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300590
Joerg Roedelec04b262009-06-19 15:16:23 +0200591 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200592 unsigned long ugfn;
593 unsigned long j;
594 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200595 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300596
Joerg Roedelec04b262009-06-19 15:16:23 +0200597 /* Avoid unused variable warning if no large pages */
598 (void)level;
599
600 if (new.lpage_info[i])
601 continue;
602
603 lpages = 1 + (base_gfn + npages - 1) /
604 KVM_PAGES_PER_HPAGE(level);
605 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
606
607 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
608
609 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300610 goto out_free;
611
Joerg Roedelec04b262009-06-19 15:16:23 +0200612 memset(new.lpage_info[i], 0,
613 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300614
Joerg Roedelec04b262009-06-19 15:16:23 +0200615 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
616 new.lpage_info[i][0].write_count = 1;
617 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
618 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300619 ugfn = new.userspace_addr >> PAGE_SHIFT;
620 /*
621 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300622 * other, or if explicitly asked to, disable large page
623 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300624 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200625 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300626 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200627 for (j = 0; j < lpages; ++j)
628 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300629 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630
Joerg Roedelec04b262009-06-19 15:16:23 +0200631skip_lpage:
632
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 /* Allocate page dirty bitmap if needed */
634 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
635 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
636
637 new.dirty_bitmap = vmalloc(dirty_bytes);
638 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800639 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +0300641 if (old.npages)
642 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200644#else /* not defined CONFIG_S390 */
645 new.user_alloc = user_alloc;
646 if (user_alloc)
647 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200648#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300650 if (!npages)
651 kvm_arch_flush_shadow(kvm);
652
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200653 spin_lock(&kvm->mmu_lock);
654 if (mem->slot >= kvm->nmemslots)
655 kvm->nmemslots = mem->slot + 1;
656
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200658 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Zhang Xiantao0de10342007-11-20 16:25:04 +0800660 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
661 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200662 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800663 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200664 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800665 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800666 }
667
Glauber Costa6f897242008-12-03 13:40:51 -0200668 kvm_free_physmem_slot(&old, npages ? &new : NULL);
669 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300670 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -0200671 if (!npages)
672 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300673 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800674#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300675 /* map the pages in iommu page table */
676 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
677 if (r)
678 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800679#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 return 0;
681
Sheng Yangf78e0e22007-10-29 09:40:42 +0800682out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683 kvm_free_physmem_slot(&new, &old);
684out:
685 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200686
687}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800688EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
689
690int kvm_set_memory_region(struct kvm *kvm,
691 struct kvm_userspace_memory_region *mem,
692 int user_alloc)
693{
694 int r;
695
Izik Eidus72dc67a2008-02-10 18:04:15 +0200696 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800697 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200698 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800699 return r;
700}
Izik Eidus210c7c42007-10-24 23:52:57 +0200701EXPORT_SYMBOL_GPL(kvm_set_memory_region);
702
Carsten Otte1fe779f2007-10-29 16:08:35 +0100703int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
704 struct
705 kvm_userspace_memory_region *mem,
706 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200707{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200708 if (mem->slot >= KVM_MEMORY_SLOTS)
709 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200710 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711}
712
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800713int kvm_get_dirty_log(struct kvm *kvm,
714 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715{
716 struct kvm_memory_slot *memslot;
717 int r, i;
718 int n;
719 unsigned long any = 0;
720
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 r = -EINVAL;
722 if (log->slot >= KVM_MEMORY_SLOTS)
723 goto out;
724
725 memslot = &kvm->memslots[log->slot];
726 r = -ENOENT;
727 if (!memslot->dirty_bitmap)
728 goto out;
729
Uri Lublincd1a4a92007-02-22 16:43:09 +0200730 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
Uri Lublincd1a4a92007-02-22 16:43:09 +0200732 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 any = memslot->dirty_bitmap[i];
734
735 r = -EFAULT;
736 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
737 goto out;
738
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800739 if (any)
740 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741
742 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 return r;
745}
746
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300747void kvm_disable_largepages(void)
748{
749 largepages_enabled = false;
750}
751EXPORT_SYMBOL_GPL(kvm_disable_largepages);
752
Izik Eiduscea7bb22007-10-17 19:17:48 +0200753int is_error_page(struct page *page)
754{
755 return page == bad_page;
756}
757EXPORT_SYMBOL_GPL(is_error_page);
758
Anthony Liguori35149e22008-04-02 14:46:56 -0500759int is_error_pfn(pfn_t pfn)
760{
761 return pfn == bad_pfn;
762}
763EXPORT_SYMBOL_GPL(is_error_pfn);
764
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200765static inline unsigned long bad_hva(void)
766{
767 return PAGE_OFFSET;
768}
769
770int kvm_is_error_hva(unsigned long addr)
771{
772 return addr == bad_hva();
773}
774EXPORT_SYMBOL_GPL(kvm_is_error_hva);
775
Izik Eidus28430992008-10-03 17:40:32 +0300776struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777{
778 int i;
779
780 for (i = 0; i < kvm->nmemslots; ++i) {
781 struct kvm_memory_slot *memslot = &kvm->memslots[i];
782
783 if (gfn >= memslot->base_gfn
784 && gfn < memslot->base_gfn + memslot->npages)
785 return memslot;
786 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000787 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788}
Izik Eidus28430992008-10-03 17:40:32 +0300789EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300790
791struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
792{
793 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300794 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300795}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796
Izik Eiduse0d62c72007-10-24 23:57:46 +0200797int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
798{
799 int i;
800
801 gfn = unalias_gfn(kvm, gfn);
802 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
803 struct kvm_memory_slot *memslot = &kvm->memslots[i];
804
805 if (gfn >= memslot->base_gfn
806 && gfn < memslot->base_gfn + memslot->npages)
807 return 1;
808 }
809 return 0;
810}
811EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
812
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300813unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200814{
815 struct kvm_memory_slot *slot;
816
817 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300818 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200819 if (!slot)
820 return bad_hva();
821 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
822}
Sheng Yang0d150292008-04-25 21:44:50 +0800823EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200824
Anthony Liguori35149e22008-04-02 14:46:56 -0500825pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300826{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500827 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200828 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500829 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500830 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300831
Avi Kivity60395222007-10-21 11:03:36 +0200832 might_sleep();
833
Izik Eidus539cb662007-11-11 22:05:04 +0200834 addr = gfn_to_hva(kvm, gfn);
835 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200836 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500837 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200838 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200839
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300840 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200841
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500842 if (unlikely(npages != 1)) {
843 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500844
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300845 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500846 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300847
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500848 if (vma == NULL || addr < vma->vm_start ||
849 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300850 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500851 get_page(bad_page);
852 return page_to_pfn(bad_page);
853 }
854
855 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300856 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800857 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500858 } else
859 pfn = page_to_pfn(page[0]);
860
861 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500862}
863
864EXPORT_SYMBOL_GPL(gfn_to_pfn);
865
866struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
867{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500868 pfn_t pfn;
869
870 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800871 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500872 return pfn_to_page(pfn);
873
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800874 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500875
876 get_page(bad_page);
877 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300878}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500879
Avi Kivity954bbbc2007-03-30 14:02:32 +0300880EXPORT_SYMBOL_GPL(gfn_to_page);
881
Izik Eidusb4231d62007-11-20 11:49:33 +0200882void kvm_release_page_clean(struct page *page)
883{
Anthony Liguori35149e22008-04-02 14:46:56 -0500884 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200885}
886EXPORT_SYMBOL_GPL(kvm_release_page_clean);
887
Anthony Liguori35149e22008-04-02 14:46:56 -0500888void kvm_release_pfn_clean(pfn_t pfn)
889{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800890 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500891 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500892}
893EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
894
Izik Eidusb4231d62007-11-20 11:49:33 +0200895void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200896{
Anthony Liguori35149e22008-04-02 14:46:56 -0500897 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200898}
Izik Eidusb4231d62007-11-20 11:49:33 +0200899EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200900
Anthony Liguori35149e22008-04-02 14:46:56 -0500901void kvm_release_pfn_dirty(pfn_t pfn)
902{
903 kvm_set_pfn_dirty(pfn);
904 kvm_release_pfn_clean(pfn);
905}
906EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
907
908void kvm_set_page_dirty(struct page *page)
909{
910 kvm_set_pfn_dirty(page_to_pfn(page));
911}
912EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
913
914void kvm_set_pfn_dirty(pfn_t pfn)
915{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800916 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500917 struct page *page = pfn_to_page(pfn);
918 if (!PageReserved(page))
919 SetPageDirty(page);
920 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500921}
922EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
923
924void kvm_set_pfn_accessed(pfn_t pfn)
925{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800926 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500927 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500928}
929EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
930
931void kvm_get_pfn(pfn_t pfn)
932{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800933 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500934 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500935}
936EXPORT_SYMBOL_GPL(kvm_get_pfn);
937
Izik Eidus195aefd2007-10-01 22:14:18 +0200938static int next_segment(unsigned long len, int offset)
939{
940 if (len > PAGE_SIZE - offset)
941 return PAGE_SIZE - offset;
942 else
943 return len;
944}
945
946int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
947 int len)
948{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200949 int r;
950 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200951
Izik Eiduse0506bc2007-11-11 22:10:22 +0200952 addr = gfn_to_hva(kvm, gfn);
953 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200954 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200955 r = copy_from_user(data, (void __user *)addr + offset, len);
956 if (r)
957 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200958 return 0;
959}
960EXPORT_SYMBOL_GPL(kvm_read_guest_page);
961
962int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
963{
964 gfn_t gfn = gpa >> PAGE_SHIFT;
965 int seg;
966 int offset = offset_in_page(gpa);
967 int ret;
968
969 while ((seg = next_segment(len, offset)) != 0) {
970 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
971 if (ret < 0)
972 return ret;
973 offset = 0;
974 len -= seg;
975 data += seg;
976 ++gfn;
977 }
978 return 0;
979}
980EXPORT_SYMBOL_GPL(kvm_read_guest);
981
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500982int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
983 unsigned long len)
984{
985 int r;
986 unsigned long addr;
987 gfn_t gfn = gpa >> PAGE_SHIFT;
988 int offset = offset_in_page(gpa);
989
990 addr = gfn_to_hva(kvm, gfn);
991 if (kvm_is_error_hva(addr))
992 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100993 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500994 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100995 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500996 if (r)
997 return -EFAULT;
998 return 0;
999}
1000EXPORT_SYMBOL(kvm_read_guest_atomic);
1001
Izik Eidus195aefd2007-10-01 22:14:18 +02001002int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1003 int offset, int len)
1004{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001005 int r;
1006 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001007
Izik Eiduse0506bc2007-11-11 22:10:22 +02001008 addr = gfn_to_hva(kvm, gfn);
1009 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001010 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001011 r = copy_to_user((void __user *)addr + offset, data, len);
1012 if (r)
1013 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001014 mark_page_dirty(kvm, gfn);
1015 return 0;
1016}
1017EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1018
1019int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1020 unsigned long len)
1021{
1022 gfn_t gfn = gpa >> PAGE_SHIFT;
1023 int seg;
1024 int offset = offset_in_page(gpa);
1025 int ret;
1026
1027 while ((seg = next_segment(len, offset)) != 0) {
1028 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1029 if (ret < 0)
1030 return ret;
1031 offset = 0;
1032 len -= seg;
1033 data += seg;
1034 ++gfn;
1035 }
1036 return 0;
1037}
1038
1039int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1040{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001041 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001042}
1043EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1044
1045int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1046{
1047 gfn_t gfn = gpa >> PAGE_SHIFT;
1048 int seg;
1049 int offset = offset_in_page(gpa);
1050 int ret;
1051
1052 while ((seg = next_segment(len, offset)) != 0) {
1053 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1054 if (ret < 0)
1055 return ret;
1056 offset = 0;
1057 len -= seg;
1058 ++gfn;
1059 }
1060 return 0;
1061}
1062EXPORT_SYMBOL_GPL(kvm_clear_guest);
1063
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1065{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001066 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067
Uri Lublin3b6fff12007-10-30 10:42:09 +02001068 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001069 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001070 if (memslot && memslot->dirty_bitmap) {
1071 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072
Rusty Russell7e9d6192007-07-31 20:41:14 +10001073 /* avoid RMW */
1074 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1075 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 }
1077}
1078
Eddie Dongb6958ce2007-07-18 12:15:21 +03001079/*
1080 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1081 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001082void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001083{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001084 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001085
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001086 for (;;) {
1087 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001088
Gleb Natapova1b37102009-07-09 15:33:52 +03001089 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001090 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001091 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001092 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001093 if (kvm_cpu_has_pending_timer(vcpu))
1094 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001095 if (signal_pending(current))
1096 break;
1097
Eddie Dongb6958ce2007-07-18 12:15:21 +03001098 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001099 }
1100
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001101 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001102}
1103
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104void kvm_resched(struct kvm_vcpu *vcpu)
1105{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001106 if (!need_resched())
1107 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109}
1110EXPORT_SYMBOL_GPL(kvm_resched);
1111
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001112void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1113{
1114 ktime_t expires;
1115 DEFINE_WAIT(wait);
1116
1117 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1118
1119 /* Sleep for 100 us, and hope lock-holder got scheduled */
1120 expires = ktime_add_ns(ktime_get(), 100000UL);
1121 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1122
1123 finish_wait(&vcpu->wq, &wait);
1124}
1125EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1126
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001127static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001128{
1129 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001130 struct page *page;
1131
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001132 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001133 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001134#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001135 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001136 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001137#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001138#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1139 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1140 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1141#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001142 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001143 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001144 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001145 vmf->page = page;
1146 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001147}
1148
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001149static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001150 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001151};
1152
1153static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1154{
1155 vma->vm_ops = &kvm_vcpu_vm_ops;
1156 return 0;
1157}
1158
Avi Kivitybccf2152007-02-21 18:04:26 +02001159static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1160{
1161 struct kvm_vcpu *vcpu = filp->private_data;
1162
Al Viro66c0b392008-04-19 20:33:56 +01001163 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001164 return 0;
1165}
1166
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001167static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001168 .release = kvm_vcpu_release,
1169 .unlocked_ioctl = kvm_vcpu_ioctl,
1170 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001171 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001172};
1173
1174/*
1175 * Allocates an inode for the vcpu.
1176 */
1177static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1178{
Gleb Natapov73880c82009-06-09 15:56:28 +03001179 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Avi Kivitybccf2152007-02-21 18:04:26 +02001180}
1181
Avi Kivityc5ea7662007-02-20 18:41:05 +02001182/*
1183 * Creates some virtual cpus. Good luck creating more than one.
1184 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001185static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001186{
1187 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001188 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001189
Gleb Natapov73880c82009-06-09 15:56:28 +03001190 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001191 if (IS_ERR(vcpu))
1192 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001193
Avi Kivity15ad7142007-07-11 18:17:21 +03001194 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1195
Avi Kivity26e52152007-11-20 15:30:24 +02001196 r = kvm_arch_vcpu_setup(vcpu);
1197 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001198 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001199
Shaohua Li11ec2802007-07-23 14:51:37 +08001200 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001201 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1202 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001203 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001204 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001205
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001206 kvm_for_each_vcpu(r, v, kvm)
1207 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001208 r = -EEXIST;
1209 goto vcpu_destroy;
1210 }
1211
1212 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001213
1214 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001215 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001216 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001217 if (r < 0) {
1218 kvm_put_kvm(kvm);
1219 goto vcpu_destroy;
1220 }
1221
1222 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1223 smp_wmb();
1224 atomic_inc(&kvm->online_vcpus);
1225
1226#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1227 if (kvm->bsp_vcpu_id == id)
1228 kvm->bsp_vcpu = vcpu;
1229#endif
1230 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001231 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001232
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001233vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001234 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001235 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001236 return r;
1237}
1238
Avi Kivity1961d272007-03-05 19:46:05 +02001239static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1240{
1241 if (sigset) {
1242 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1243 vcpu->sigset_active = 1;
1244 vcpu->sigset = *sigset;
1245 } else
1246 vcpu->sigset_active = 0;
1247 return 0;
1248}
1249
Avi Kivitybccf2152007-02-21 18:04:26 +02001250static long kvm_vcpu_ioctl(struct file *filp,
1251 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252{
Avi Kivitybccf2152007-02-21 18:04:26 +02001253 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001254 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001255 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001256 struct kvm_fpu *fpu = NULL;
1257 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001259 if (vcpu->kvm->mm != current->mm)
1260 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001262 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001263 r = -EINVAL;
1264 if (arg)
1265 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001266 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001269 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001271 r = -ENOMEM;
1272 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1273 if (!kvm_regs)
1274 goto out;
1275 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001277 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001279 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1280 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001282out_free1:
1283 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 break;
1285 }
1286 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001287 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001289 r = -ENOMEM;
1290 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1291 if (!kvm_regs)
1292 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001294 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1295 goto out_free2;
1296 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001298 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001300out_free2:
1301 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 break;
1303 }
1304 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001305 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1306 r = -ENOMEM;
1307 if (!kvm_sregs)
1308 goto out;
1309 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 if (r)
1311 goto out;
1312 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001313 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 goto out;
1315 r = 0;
1316 break;
1317 }
1318 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001319 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1320 r = -ENOMEM;
1321 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001323 r = -EFAULT;
1324 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1325 goto out;
1326 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 if (r)
1328 goto out;
1329 r = 0;
1330 break;
1331 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001332 case KVM_GET_MP_STATE: {
1333 struct kvm_mp_state mp_state;
1334
1335 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1336 if (r)
1337 goto out;
1338 r = -EFAULT;
1339 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1340 goto out;
1341 r = 0;
1342 break;
1343 }
1344 case KVM_SET_MP_STATE: {
1345 struct kvm_mp_state mp_state;
1346
1347 r = -EFAULT;
1348 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1349 goto out;
1350 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1351 if (r)
1352 goto out;
1353 r = 0;
1354 break;
1355 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 case KVM_TRANSLATE: {
1357 struct kvm_translation tr;
1358
1359 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001360 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001362 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 if (r)
1364 goto out;
1365 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001366 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 goto out;
1368 r = 0;
1369 break;
1370 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001371 case KVM_SET_GUEST_DEBUG: {
1372 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373
1374 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001375 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001377 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 if (r)
1379 goto out;
1380 r = 0;
1381 break;
1382 }
Avi Kivity1961d272007-03-05 19:46:05 +02001383 case KVM_SET_SIGNAL_MASK: {
1384 struct kvm_signal_mask __user *sigmask_arg = argp;
1385 struct kvm_signal_mask kvm_sigmask;
1386 sigset_t sigset, *p;
1387
1388 p = NULL;
1389 if (argp) {
1390 r = -EFAULT;
1391 if (copy_from_user(&kvm_sigmask, argp,
1392 sizeof kvm_sigmask))
1393 goto out;
1394 r = -EINVAL;
1395 if (kvm_sigmask.len != sizeof sigset)
1396 goto out;
1397 r = -EFAULT;
1398 if (copy_from_user(&sigset, sigmask_arg->sigset,
1399 sizeof sigset))
1400 goto out;
1401 p = &sigset;
1402 }
1403 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1404 break;
1405 }
Avi Kivityb8836732007-04-01 16:34:31 +03001406 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001407 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1408 r = -ENOMEM;
1409 if (!fpu)
1410 goto out;
1411 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001412 if (r)
1413 goto out;
1414 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001415 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001416 goto out;
1417 r = 0;
1418 break;
1419 }
1420 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001421 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1422 r = -ENOMEM;
1423 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001424 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001425 r = -EFAULT;
1426 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1427 goto out;
1428 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001429 if (r)
1430 goto out;
1431 r = 0;
1432 break;
1433 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001434 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001435 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001436 }
1437out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001438 kfree(fpu);
1439 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001440 return r;
1441}
1442
1443static long kvm_vm_ioctl(struct file *filp,
1444 unsigned int ioctl, unsigned long arg)
1445{
1446 struct kvm *kvm = filp->private_data;
1447 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001448 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001449
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001450 if (kvm->mm != current->mm)
1451 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001452 switch (ioctl) {
1453 case KVM_CREATE_VCPU:
1454 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1455 if (r < 0)
1456 goto out;
1457 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001458 case KVM_SET_USER_MEMORY_REGION: {
1459 struct kvm_userspace_memory_region kvm_userspace_mem;
1460
1461 r = -EFAULT;
1462 if (copy_from_user(&kvm_userspace_mem, argp,
1463 sizeof kvm_userspace_mem))
1464 goto out;
1465
1466 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 if (r)
1468 goto out;
1469 break;
1470 }
1471 case KVM_GET_DIRTY_LOG: {
1472 struct kvm_dirty_log log;
1473
1474 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001475 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001477 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 if (r)
1479 goto out;
1480 break;
1481 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001482#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1483 case KVM_REGISTER_COALESCED_MMIO: {
1484 struct kvm_coalesced_mmio_zone zone;
1485 r = -EFAULT;
1486 if (copy_from_user(&zone, argp, sizeof zone))
1487 goto out;
1488 r = -ENXIO;
1489 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1490 if (r)
1491 goto out;
1492 r = 0;
1493 break;
1494 }
1495 case KVM_UNREGISTER_COALESCED_MMIO: {
1496 struct kvm_coalesced_mmio_zone zone;
1497 r = -EFAULT;
1498 if (copy_from_user(&zone, argp, sizeof zone))
1499 goto out;
1500 r = -ENXIO;
1501 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1502 if (r)
1503 goto out;
1504 r = 0;
1505 break;
1506 }
1507#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001508 case KVM_IRQFD: {
1509 struct kvm_irqfd data;
1510
1511 r = -EFAULT;
1512 if (copy_from_user(&data, argp, sizeof data))
1513 goto out;
1514 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1515 break;
1516 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001517 case KVM_IOEVENTFD: {
1518 struct kvm_ioeventfd data;
1519
1520 r = -EFAULT;
1521 if (copy_from_user(&data, argp, sizeof data))
1522 goto out;
1523 r = kvm_ioeventfd(kvm, &data);
1524 break;
1525 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001526#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1527 case KVM_SET_BOOT_CPU_ID:
1528 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001529 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001530 if (atomic_read(&kvm->online_vcpus) != 0)
1531 r = -EBUSY;
1532 else
1533 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001534 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001535 break;
1536#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001537 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001538 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001539 if (r == -ENOTTY)
1540 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001541 }
1542out:
1543 return r;
1544}
1545
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001546#ifdef CONFIG_COMPAT
1547struct compat_kvm_dirty_log {
1548 __u32 slot;
1549 __u32 padding1;
1550 union {
1551 compat_uptr_t dirty_bitmap; /* one bit per page */
1552 __u64 padding2;
1553 };
1554};
1555
1556static long kvm_vm_compat_ioctl(struct file *filp,
1557 unsigned int ioctl, unsigned long arg)
1558{
1559 struct kvm *kvm = filp->private_data;
1560 int r;
1561
1562 if (kvm->mm != current->mm)
1563 return -EIO;
1564 switch (ioctl) {
1565 case KVM_GET_DIRTY_LOG: {
1566 struct compat_kvm_dirty_log compat_log;
1567 struct kvm_dirty_log log;
1568
1569 r = -EFAULT;
1570 if (copy_from_user(&compat_log, (void __user *)arg,
1571 sizeof(compat_log)))
1572 goto out;
1573 log.slot = compat_log.slot;
1574 log.padding1 = compat_log.padding1;
1575 log.padding2 = compat_log.padding2;
1576 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1577
1578 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1579 if (r)
1580 goto out;
1581 break;
1582 }
1583 default:
1584 r = kvm_vm_ioctl(filp, ioctl, arg);
1585 }
1586
1587out:
1588 return r;
1589}
1590#endif
1591
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001592static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001593{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001594 struct page *page[1];
1595 unsigned long addr;
1596 int npages;
1597 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001598 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001599
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001600 addr = gfn_to_hva(kvm, gfn);
1601 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001602 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001603
1604 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1605 NULL);
1606 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001607 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001608
1609 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001610 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001611}
1612
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001613static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001614 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001615};
1616
1617static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1618{
1619 vma->vm_ops = &kvm_vm_vm_ops;
1620 return 0;
1621}
1622
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001623static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001624 .release = kvm_vm_release,
1625 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001626#ifdef CONFIG_COMPAT
1627 .compat_ioctl = kvm_vm_compat_ioctl,
1628#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001629 .mmap = kvm_vm_mmap,
1630};
1631
1632static int kvm_dev_ioctl_create_vm(void)
1633{
Al Viro2030a422008-02-23 06:46:49 -05001634 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001635 struct kvm *kvm;
1636
Avi Kivityf17abe92007-02-21 19:28:04 +02001637 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001638 if (IS_ERR(kvm))
1639 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001640 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001641 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001642 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001643
Avi Kivityf17abe92007-02-21 19:28:04 +02001644 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001645}
1646
Avi Kivity1a811b62008-12-08 18:25:27 +02001647static long kvm_dev_ioctl_check_extension_generic(long arg)
1648{
1649 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001650 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001651 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001652 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001653#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1654 case KVM_CAP_SET_BOOT_CPU_ID:
1655#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001656 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001657 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001658#ifdef CONFIG_HAVE_KVM_IRQCHIP
1659 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001660 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001661#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001662 default:
1663 break;
1664 }
1665 return kvm_dev_ioctl_check_extension(arg);
1666}
1667
Avi Kivityf17abe92007-02-21 19:28:04 +02001668static long kvm_dev_ioctl(struct file *filp,
1669 unsigned int ioctl, unsigned long arg)
1670{
Avi Kivity07c45a32007-03-07 13:05:38 +02001671 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001672
1673 switch (ioctl) {
1674 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001675 r = -EINVAL;
1676 if (arg)
1677 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001678 r = KVM_API_VERSION;
1679 break;
1680 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001681 r = -EINVAL;
1682 if (arg)
1683 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001684 r = kvm_dev_ioctl_create_vm();
1685 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001686 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001687 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001688 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001689 case KVM_GET_VCPU_MMAP_SIZE:
1690 r = -EINVAL;
1691 if (arg)
1692 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001693 r = PAGE_SIZE; /* struct kvm_run */
1694#ifdef CONFIG_X86
1695 r += PAGE_SIZE; /* pio data page */
1696#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001697#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1698 r += PAGE_SIZE; /* coalesced mmio ring page */
1699#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001700 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001701 case KVM_TRACE_ENABLE:
1702 case KVM_TRACE_PAUSE:
1703 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001704 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001705 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001707 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 }
1709out:
1710 return r;
1711}
1712
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 .unlocked_ioctl = kvm_dev_ioctl,
1715 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716};
1717
1718static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001719 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 "kvm",
1721 &kvm_chardev_ops,
1722};
1723
Avi Kivity1b6c0162007-05-24 13:03:52 +03001724static void hardware_enable(void *junk)
1725{
1726 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02001727 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03001728
Rusty Russell7f59f492008-12-07 21:25:45 +10301729 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001730 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02001731
Rusty Russell7f59f492008-12-07 21:25:45 +10301732 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02001733
1734 r = kvm_arch_hardware_enable(NULL);
1735
1736 if (r) {
1737 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1738 atomic_inc(&hardware_enable_failed);
1739 printk(KERN_INFO "kvm: enabling virtualization on "
1740 "CPU%d failed\n", cpu);
1741 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03001742}
1743
1744static void hardware_disable(void *junk)
1745{
1746 int cpu = raw_smp_processor_id();
1747
Rusty Russell7f59f492008-12-07 21:25:45 +10301748 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001749 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301750 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001751 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001752}
1753
Alexander Graf10474ae2009-09-15 11:37:46 +02001754static void hardware_disable_all_nolock(void)
1755{
1756 BUG_ON(!kvm_usage_count);
1757
1758 kvm_usage_count--;
1759 if (!kvm_usage_count)
1760 on_each_cpu(hardware_disable, NULL, 1);
1761}
1762
1763static void hardware_disable_all(void)
1764{
1765 spin_lock(&kvm_lock);
1766 hardware_disable_all_nolock();
1767 spin_unlock(&kvm_lock);
1768}
1769
1770static int hardware_enable_all(void)
1771{
1772 int r = 0;
1773
1774 spin_lock(&kvm_lock);
1775
1776 kvm_usage_count++;
1777 if (kvm_usage_count == 1) {
1778 atomic_set(&hardware_enable_failed, 0);
1779 on_each_cpu(hardware_enable, NULL, 1);
1780
1781 if (atomic_read(&hardware_enable_failed)) {
1782 hardware_disable_all_nolock();
1783 r = -EBUSY;
1784 }
1785 }
1786
1787 spin_unlock(&kvm_lock);
1788
1789 return r;
1790}
1791
Avi Kivity774c47f2007-02-12 00:54:47 -08001792static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1793 void *v)
1794{
1795 int cpu = (long)v;
1796
Alexander Graf10474ae2009-09-15 11:37:46 +02001797 if (!kvm_usage_count)
1798 return NOTIFY_OK;
1799
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001800 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001801 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001802 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03001803 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1804 cpu);
1805 hardware_disable(NULL);
1806 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001807 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001808 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1809 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001810 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001811 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001812 case CPU_ONLINE:
1813 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1814 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001815 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001816 break;
1817 }
1818 return NOTIFY_OK;
1819}
1820
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001821
1822asmlinkage void kvm_handle_fault_on_reboot(void)
1823{
1824 if (kvm_rebooting)
1825 /* spin while reset goes on */
1826 while (true)
1827 ;
1828 /* Fault while not rebooting. We want the trace. */
1829 BUG();
1830}
1831EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1832
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001833static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001834 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001835{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001836 /*
1837 * Some (well, at least mine) BIOSes hang on reboot if
1838 * in vmx root mode.
1839 *
1840 * And Intel TXT required VMX off for all cpu when system shutdown.
1841 */
1842 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1843 kvm_rebooting = true;
1844 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001845 return NOTIFY_OK;
1846}
1847
1848static struct notifier_block kvm_reboot_notifier = {
1849 .notifier_call = kvm_reboot,
1850 .priority = 0,
1851};
1852
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001853void kvm_io_bus_init(struct kvm_io_bus *bus)
1854{
1855 memset(bus, 0, sizeof(*bus));
1856}
1857
1858void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1859{
1860 int i;
1861
1862 for (i = 0; i < bus->dev_count; i++) {
1863 struct kvm_io_device *pos = bus->devs[i];
1864
1865 kvm_iodevice_destructor(pos);
1866 }
1867}
1868
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001869/* kvm_io_bus_write - called under kvm->slots_lock */
1870int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
1871 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001872{
1873 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001874 for (i = 0; i < bus->dev_count; i++)
1875 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1876 return 0;
1877 return -EOPNOTSUPP;
1878}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001879
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001880/* kvm_io_bus_read - called under kvm->slots_lock */
1881int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
1882{
1883 int i;
1884 for (i = 0; i < bus->dev_count; i++)
1885 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
1886 return 0;
1887 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001888}
1889
Gregory Haskins090b7af2009-07-07 17:08:44 -04001890int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001891 struct kvm_io_device *dev)
1892{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001893 int ret;
1894
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001895 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04001896 ret = __kvm_io_bus_register_dev(bus, dev);
1897 up_write(&kvm->slots_lock);
1898
1899 return ret;
1900}
1901
1902/* An unlocked version. Caller must have write lock on slots_lock. */
1903int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
1904 struct kvm_io_device *dev)
1905{
1906 if (bus->dev_count > NR_IOBUS_DEVS-1)
1907 return -ENOSPC;
1908
1909 bus->devs[bus->dev_count++] = dev;
1910
1911 return 0;
1912}
1913
1914void kvm_io_bus_unregister_dev(struct kvm *kvm,
1915 struct kvm_io_bus *bus,
1916 struct kvm_io_device *dev)
1917{
1918 down_write(&kvm->slots_lock);
1919 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001920 up_write(&kvm->slots_lock);
1921}
1922
1923/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04001924void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
1925 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001926{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001927 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001928
Gregory Haskins090b7af2009-07-07 17:08:44 -04001929 for (i = 0; i < bus->dev_count; i++)
1930 if (bus->devs[i] == dev) {
1931 bus->devs[i] = bus->devs[--bus->dev_count];
1932 break;
1933 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001934}
1935
Avi Kivity774c47f2007-02-12 00:54:47 -08001936static struct notifier_block kvm_cpu_notifier = {
1937 .notifier_call = kvm_cpu_hotplug,
1938 .priority = 20, /* must be > scheduler priority */
1939};
1940
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001941static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001942{
1943 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001944 struct kvm *kvm;
1945
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001946 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001947 spin_lock(&kvm_lock);
1948 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001949 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001950 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001951 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001952}
1953
1954DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1955
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001956static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001957{
1958 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001959 struct kvm *kvm;
1960 struct kvm_vcpu *vcpu;
1961 int i;
1962
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001963 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001964 spin_lock(&kvm_lock);
1965 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001966 kvm_for_each_vcpu(i, vcpu, kvm)
1967 *val += *(u32 *)((void *)vcpu + offset);
1968
Avi Kivity1165f5f2007-04-19 17:27:43 +03001969 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001970 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001971}
1972
Avi Kivityba1389b2007-11-18 16:24:12 +02001973DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1974
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001975static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02001976 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1977 [KVM_STAT_VM] = &vm_stat_fops,
1978};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001979
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001980static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981{
1982 struct kvm_stats_debugfs_item *p;
1983
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001984 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001986 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001987 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001988 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989}
1990
1991static void kvm_exit_debug(void)
1992{
1993 struct kvm_stats_debugfs_item *p;
1994
1995 for (p = debugfs_entries; p->name; ++p)
1996 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001997 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998}
1999
Avi Kivity59ae6c62007-02-12 00:54:48 -08002000static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2001{
Alexander Graf10474ae2009-09-15 11:37:46 +02002002 if (kvm_usage_count)
2003 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002004 return 0;
2005}
2006
2007static int kvm_resume(struct sys_device *dev)
2008{
Alexander Graf10474ae2009-09-15 11:37:46 +02002009 if (kvm_usage_count)
2010 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002011 return 0;
2012}
2013
2014static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002015 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002016 .suspend = kvm_suspend,
2017 .resume = kvm_resume,
2018};
2019
2020static struct sys_device kvm_sysdev = {
2021 .id = 0,
2022 .cls = &kvm_sysdev_class,
2023};
2024
Izik Eiduscea7bb22007-10-17 19:17:48 +02002025struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002026pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027
Avi Kivity15ad7142007-07-11 18:17:21 +03002028static inline
2029struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2030{
2031 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2032}
2033
2034static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2035{
2036 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2037
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002038 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002039}
2040
2041static void kvm_sched_out(struct preempt_notifier *pn,
2042 struct task_struct *next)
2043{
2044 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2045
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002046 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002047}
2048
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002049int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002050 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051{
2052 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002053 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002055 r = kvm_arch_init(opaque);
2056 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002057 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002058
2059 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2060
2061 if (bad_page == NULL) {
2062 r = -ENOMEM;
2063 goto out;
2064 }
2065
Anthony Liguori35149e22008-04-02 14:46:56 -05002066 bad_pfn = page_to_pfn(bad_page);
2067
Avi Kivity8437a612009-06-06 14:52:35 -07002068 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302069 r = -ENOMEM;
2070 goto out_free_0;
2071 }
2072
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002073 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302075 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076
Yang, Sheng002c7f72007-07-31 14:23:01 +03002077 for_each_online_cpu(cpu) {
2078 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002079 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002080 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002081 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002082 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002083 }
2084
Avi Kivity774c47f2007-02-12 00:54:47 -08002085 r = register_cpu_notifier(&kvm_cpu_notifier);
2086 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002087 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 register_reboot_notifier(&kvm_reboot_notifier);
2089
Avi Kivity59ae6c62007-02-12 00:54:48 -08002090 r = sysdev_class_register(&kvm_sysdev_class);
2091 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002092 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002093
2094 r = sysdev_register(&kvm_sysdev);
2095 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002096 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002097
Rusty Russellc16f8622007-07-30 21:12:19 +10002098 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2099 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002100 __alignof__(struct kvm_vcpu),
2101 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002102 if (!kvm_vcpu_cache) {
2103 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002104 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002105 }
2106
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002108 kvm_vm_fops.owner = module;
2109 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110
2111 r = misc_register(&kvm_dev);
2112 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002113 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 goto out_free;
2115 }
2116
Avi Kivity15ad7142007-07-11 18:17:21 +03002117 kvm_preempt_ops.sched_in = kvm_sched_in;
2118 kvm_preempt_ops.sched_out = kvm_sched_out;
2119
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002120 kvm_init_debug();
2121
Avi Kivityc7addb92007-09-16 18:58:32 +02002122 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123
2124out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002125 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002126out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002127 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002128out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002129 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002130out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002132 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002133out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002134out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002135 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302136out_free_0a:
2137 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002138out_free_0:
2139 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002140out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002141 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002142out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 return r;
2144}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002145EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002147void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002149 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002150 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002152 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002153 sysdev_unregister(&kvm_sysdev);
2154 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002156 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002157 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002158 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002159 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302160 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002161 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002163EXPORT_SYMBOL_GPL(kvm_exit);