blob: a944be392d6e9420aa38d833cbc736bf9f07d22e [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#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55#include "coalesced_mmio.h"
56#endif
57
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#define CREATE_TRACE_POINTS
59#include <trace/events/kvm.h>
60
Avi Kivity6aa8b732006-12-10 02:21:36 -080061MODULE_AUTHOR("Qumranet");
62MODULE_LICENSE("GPL");
63
Marcelo Tosattifa40a822009-06-04 15:08:24 -030064/*
65 * Ordering of locks:
66 *
Sheng Yangfae3a352009-12-15 10:28:07 +080067 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030068 */
69
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080070DEFINE_SPINLOCK(kvm_lock);
71LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080072
Rusty Russell7f59f492008-12-07 21:25:45 +103073static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020074static int kvm_usage_count = 0;
75static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030076
Rusty Russellc16f8622007-07-30 21:12:19 +100077struct kmem_cache *kvm_vcpu_cache;
78EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030079
Avi Kivity15ad7142007-07-11 18:17:21 +030080static __read_mostly struct preempt_ops kvm_preempt_ops;
81
Hollis Blanchard76f7c872008-04-15 16:05:42 -050082struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080083
Avi Kivitybccf2152007-02-21 18:04:26 +020084static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
85 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020086static int hardware_enable_all(void);
87static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020088
Hannes Edere8ba5d32008-11-28 17:02:42 +010089static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030090
Marcelo Tosatti54dee992009-06-11 12:07:44 -030091static bool largepages_enabled = true;
92
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +080093inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030094{
Joerg Roedelfc5659c2009-02-18 14:08:58 +010095 if (pfn_valid(pfn)) {
96 struct page *page = compound_head(pfn_to_page(pfn));
97 return PageReserved(page);
98 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030099
100 return true;
101}
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103/*
104 * Switches to specified vcpu, until a matching vcpu_put()
105 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200106void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107{
Avi Kivity15ad7142007-07-11 18:17:21 +0300108 int cpu;
109
Avi Kivitybccf2152007-02-21 18:04:26 +0200110 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300111 cpu = get_cpu();
112 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200113 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300114 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200115}
116
Carsten Otte313a3dc2007-10-11 19:16:52 +0200117void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118{
Avi Kivity15ad7142007-07-11 18:17:21 +0300119 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200120 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300121 preempt_notifier_unregister(&vcpu->preempt_notifier);
122 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 mutex_unlock(&vcpu->mutex);
124}
125
Avi Kivityd9e368d2007-06-07 19:18:30 +0300126static void ack_flush(void *_completed)
127{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300128}
129
Rusty Russell49846892008-12-08 20:26:24 +1030130static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300131{
Avi Kivity597a5f52008-07-20 14:24:22 +0300132 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030133 cpumask_var_t cpus;
134 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300135 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300136
Li Zefan79f55992009-06-15 14:58:26 +0800137 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030138
Marcelo Tosatti84261922009-06-17 10:53:47 -0300139 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200140 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300141 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030142 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300143 continue;
144 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030145 if (cpus != NULL && cpu != -1 && cpu != me)
146 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300147 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030148 if (unlikely(cpus == NULL))
149 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
150 else if (!cpumask_empty(cpus))
151 smp_call_function_many(cpus, ack_flush, NULL, 1);
152 else
153 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300154 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030155 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030156 return called;
157}
158
159void kvm_flush_remote_tlbs(struct kvm *kvm)
160{
161 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
162 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300163}
164
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500165void kvm_reload_remote_mmus(struct kvm *kvm)
166{
Rusty Russell49846892008-12-08 20:26:24 +1030167 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500168}
169
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000170int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
171{
172 struct page *page;
173 int r;
174
175 mutex_init(&vcpu->mutex);
176 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000177 vcpu->kvm = kvm;
178 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300179 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000180
181 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
182 if (!page) {
183 r = -ENOMEM;
184 goto fail;
185 }
186 vcpu->run = page_address(page);
187
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800188 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000189 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800190 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191 return 0;
192
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000193fail_free_run:
194 free_page((unsigned long)vcpu->run);
195fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000196 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000197}
198EXPORT_SYMBOL_GPL(kvm_vcpu_init);
199
200void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
201{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800202 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000203 free_page((unsigned long)vcpu->run);
204}
205EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
206
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200207#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
208static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
209{
210 return container_of(mn, struct kvm, mmu_notifier);
211}
212
213static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
214 struct mm_struct *mm,
215 unsigned long address)
216{
217 struct kvm *kvm = mmu_notifier_to_kvm(mn);
218 int need_tlb_flush;
219
220 /*
221 * When ->invalidate_page runs, the linux pte has been zapped
222 * already but the page is still allocated until
223 * ->invalidate_page returns. So if we increase the sequence
224 * here the kvm page fault will notice if the spte can't be
225 * established because the page is going to be freed. If
226 * instead the kvm page fault establishes the spte before
227 * ->invalidate_page runs, kvm_unmap_hva will release it
228 * before returning.
229 *
230 * The sequence increase only need to be seen at spin_unlock
231 * time, and not at spin_lock time.
232 *
233 * Increasing the sequence after the spin_unlock would be
234 * unsafe because the kvm page fault could then establish the
235 * pte after kvm_unmap_hva returned, without noticing the page
236 * is going to be freed.
237 */
238 spin_lock(&kvm->mmu_lock);
239 kvm->mmu_notifier_seq++;
240 need_tlb_flush = kvm_unmap_hva(kvm, address);
241 spin_unlock(&kvm->mmu_lock);
242
243 /* we've to flush the tlb before the pages can be freed */
244 if (need_tlb_flush)
245 kvm_flush_remote_tlbs(kvm);
246
247}
248
Izik Eidus3da0dd42009-09-23 21:47:18 +0300249static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
250 struct mm_struct *mm,
251 unsigned long address,
252 pte_t pte)
253{
254 struct kvm *kvm = mmu_notifier_to_kvm(mn);
255
256 spin_lock(&kvm->mmu_lock);
257 kvm->mmu_notifier_seq++;
258 kvm_set_spte_hva(kvm, address, pte);
259 spin_unlock(&kvm->mmu_lock);
260}
261
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200262static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
263 struct mm_struct *mm,
264 unsigned long start,
265 unsigned long end)
266{
267 struct kvm *kvm = mmu_notifier_to_kvm(mn);
268 int need_tlb_flush = 0;
269
270 spin_lock(&kvm->mmu_lock);
271 /*
272 * The count increase must become visible at unlock time as no
273 * spte can be established without taking the mmu_lock and
274 * count is also read inside the mmu_lock critical section.
275 */
276 kvm->mmu_notifier_count++;
277 for (; start < end; start += PAGE_SIZE)
278 need_tlb_flush |= kvm_unmap_hva(kvm, start);
279 spin_unlock(&kvm->mmu_lock);
280
281 /* we've to flush the tlb before the pages can be freed */
282 if (need_tlb_flush)
283 kvm_flush_remote_tlbs(kvm);
284}
285
286static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
287 struct mm_struct *mm,
288 unsigned long start,
289 unsigned long end)
290{
291 struct kvm *kvm = mmu_notifier_to_kvm(mn);
292
293 spin_lock(&kvm->mmu_lock);
294 /*
295 * This sequence increase will notify the kvm page fault that
296 * the page that is going to be mapped in the spte could have
297 * been freed.
298 */
299 kvm->mmu_notifier_seq++;
300 /*
301 * The above sequence increase must be visible before the
302 * below count decrease but both values are read by the kvm
303 * page fault under mmu_lock spinlock so we don't need to add
304 * a smb_wmb() here in between the two.
305 */
306 kvm->mmu_notifier_count--;
307 spin_unlock(&kvm->mmu_lock);
308
309 BUG_ON(kvm->mmu_notifier_count < 0);
310}
311
312static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
313 struct mm_struct *mm,
314 unsigned long address)
315{
316 struct kvm *kvm = mmu_notifier_to_kvm(mn);
317 int young;
318
319 spin_lock(&kvm->mmu_lock);
320 young = kvm_age_hva(kvm, address);
321 spin_unlock(&kvm->mmu_lock);
322
323 if (young)
324 kvm_flush_remote_tlbs(kvm);
325
326 return young;
327}
328
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100329static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
330 struct mm_struct *mm)
331{
332 struct kvm *kvm = mmu_notifier_to_kvm(mn);
333 kvm_arch_flush_shadow(kvm);
334}
335
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200336static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
337 .invalidate_page = kvm_mmu_notifier_invalidate_page,
338 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
339 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
340 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300341 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100342 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200343};
344#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
345
Avi Kivityf17abe92007-02-21 19:28:04 +0200346static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347{
Alexander Graf10474ae2009-09-15 11:37:46 +0200348 int r = 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800349 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200350#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
351 struct page *page;
352#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800354 if (IS_ERR(kvm))
355 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200356
357 r = hardware_enable_all();
358 if (r)
359 goto out_err_nodisable;
360
Avi Kivity75858a82009-01-04 17:10:50 +0200361#ifdef CONFIG_HAVE_KVM_IRQCHIP
362 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300363 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200364#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365
Laurent Vivier5f94c172008-05-30 16:05:54 +0200366#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
367 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
368 if (!page) {
Alexander Graf10474ae2009-09-15 11:37:46 +0200369 r = -ENOMEM;
370 goto out_err;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200371 }
372 kvm->coalesced_mmio_ring =
373 (struct kvm_coalesced_mmio_ring *)page_address(page);
374#endif
375
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200376#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
377 {
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200378 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
Alexander Graf10474ae2009-09-15 11:37:46 +0200379 r = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
380 if (r) {
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200381#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
382 put_page(page);
383#endif
Alexander Graf10474ae2009-09-15 11:37:46 +0200384 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200385 }
386 }
387#endif
388
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200389 kvm->mm = current->mm;
390 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500391 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300392 spin_lock_init(&kvm->requests_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300393 kvm_io_bus_init(&kvm->pio_bus);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400394 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800395 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300396 mutex_init(&kvm->irq_lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400397 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200398 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300399 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000400 spin_lock(&kvm_lock);
401 list_add(&kvm->vm_list, &vm_list);
402 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200403#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
404 kvm_coalesced_mmio_init(kvm);
405#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800406out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200407 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200408
Heiko Carstensb4329db02009-12-18 09:41:43 +0100409#if defined(KVM_COALESCED_MMIO_PAGE_OFFSET) || \
410 (defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER))
Alexander Graf10474ae2009-09-15 11:37:46 +0200411out_err:
412 hardware_disable_all();
Heiko Carstensb4329db02009-12-18 09:41:43 +0100413#endif
Alexander Graf10474ae2009-09-15 11:37:46 +0200414out_err_nodisable:
415 kfree(kvm);
416 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200417}
418
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419/*
420 * Free any memory in @free but not in @dont.
421 */
422static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
423 struct kvm_memory_slot *dont)
424{
Joerg Roedelec04b262009-06-19 15:16:23 +0200425 int i;
426
Izik Eidus290fc382007-09-27 14:11:22 +0200427 if (!dont || free->rmap != dont->rmap)
428 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429
430 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
431 vfree(free->dirty_bitmap);
432
Joerg Roedelec04b262009-06-19 15:16:23 +0200433
434 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
435 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
436 vfree(free->lpage_info[i]);
437 free->lpage_info[i] = NULL;
438 }
439 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300440
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000442 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500443 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444}
445
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800446void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447{
448 int i;
449
450 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000451 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452}
453
Avi Kivityf17abe92007-02-21 19:28:04 +0200454static void kvm_destroy_vm(struct kvm *kvm)
455{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200456 struct mm_struct *mm = kvm->mm;
457
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800458 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800459 spin_lock(&kvm_lock);
460 list_del(&kvm->vm_list);
461 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200462 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +0300463 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400464 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200465#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
466 if (kvm->coalesced_mmio_ring != NULL)
467 free_page((unsigned long)kvm->coalesced_mmio_ring);
468#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200469#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
470 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200471#else
472 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200473#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800474 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200475 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200476 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200477}
478
Izik Eidusd39f13b2008-03-30 16:01:25 +0300479void kvm_get_kvm(struct kvm *kvm)
480{
481 atomic_inc(&kvm->users_count);
482}
483EXPORT_SYMBOL_GPL(kvm_get_kvm);
484
485void kvm_put_kvm(struct kvm *kvm)
486{
487 if (atomic_dec_and_test(&kvm->users_count))
488 kvm_destroy_vm(kvm);
489}
490EXPORT_SYMBOL_GPL(kvm_put_kvm);
491
492
Avi Kivityf17abe92007-02-21 19:28:04 +0200493static int kvm_vm_release(struct inode *inode, struct file *filp)
494{
495 struct kvm *kvm = filp->private_data;
496
Gregory Haskins721eecb2009-05-20 10:30:49 -0400497 kvm_irqfd_release(kvm);
498
Izik Eidusd39f13b2008-03-30 16:01:25 +0300499 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 return 0;
501}
502
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 * Allocate some memory and give it an address in the guest physical address
505 * space.
506 *
507 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800508 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500509 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800511int __kvm_set_memory_region(struct kvm *kvm,
512 struct kvm_userspace_memory_region *mem,
513 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514{
515 int r;
516 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200517 unsigned long npages;
518 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 struct kvm_memory_slot *memslot;
520 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521
522 r = -EINVAL;
523 /* General sanity checks */
524 if (mem->memory_size & (PAGE_SIZE - 1))
525 goto out;
526 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
527 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800528 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600529 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200530 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531 goto out;
532 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
533 goto out;
534
535 memslot = &kvm->memslots[mem->slot];
536 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
537 npages = mem->memory_size >> PAGE_SHIFT;
538
539 if (!npages)
540 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
541
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 new = old = *memslot;
543
544 new.base_gfn = base_gfn;
545 new.npages = npages;
546 new.flags = mem->flags;
547
548 /* Disallow changing a memory slot's size. */
549 r = -EINVAL;
550 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800551 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552
553 /* Check for overlaps */
554 r = -EEXIST;
555 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
556 struct kvm_memory_slot *s = &kvm->memslots[i];
557
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200558 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 continue;
560 if (!((base_gfn + npages <= s->base_gfn) ||
561 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800562 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 /* Free page dirty bitmap if unneeded */
566 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000567 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568
569 r = -ENOMEM;
570
571 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200572#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500573 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400574 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200575
576 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800577 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200578
Izik Eidus290fc382007-09-27 14:11:22 +0200579 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500580
Izik Eidus80b14b52007-10-25 11:54:04 +0200581 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200582 /*
583 * hva_to_rmmap() serialzies with the mmu_lock and to be
584 * safe it has to ignore memslots with !user_alloc &&
585 * !userspace_addr.
586 */
587 if (user_alloc)
588 new.userspace_addr = mem->userspace_addr;
589 else
590 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200592 if (!npages)
593 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300594
Joerg Roedelec04b262009-06-19 15:16:23 +0200595 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200596 unsigned long ugfn;
597 unsigned long j;
598 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200599 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300600
Joerg Roedelec04b262009-06-19 15:16:23 +0200601 /* Avoid unused variable warning if no large pages */
602 (void)level;
603
604 if (new.lpage_info[i])
605 continue;
606
607 lpages = 1 + (base_gfn + npages - 1) /
608 KVM_PAGES_PER_HPAGE(level);
609 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
610
611 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
612
613 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300614 goto out_free;
615
Joerg Roedelec04b262009-06-19 15:16:23 +0200616 memset(new.lpage_info[i], 0,
617 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300618
Joerg Roedelec04b262009-06-19 15:16:23 +0200619 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
620 new.lpage_info[i][0].write_count = 1;
621 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
622 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300623 ugfn = new.userspace_addr >> PAGE_SHIFT;
624 /*
625 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300626 * other, or if explicitly asked to, disable large page
627 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300628 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200629 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300630 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200631 for (j = 0; j < lpages; ++j)
632 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300633 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634
Joerg Roedelec04b262009-06-19 15:16:23 +0200635skip_lpage:
636
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 /* Allocate page dirty bitmap if needed */
638 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
639 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
640
641 new.dirty_bitmap = vmalloc(dirty_bytes);
642 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800643 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644 memset(new.dirty_bitmap, 0, dirty_bytes);
Izik Eiduse2445842009-06-10 19:23:24 +0300645 if (old.npages)
646 kvm_arch_flush_shadow(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200648#else /* not defined CONFIG_S390 */
649 new.user_alloc = user_alloc;
650 if (user_alloc)
651 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200652#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300654 if (!npages)
655 kvm_arch_flush_shadow(kvm);
656
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200657 spin_lock(&kvm->mmu_lock);
658 if (mem->slot >= kvm->nmemslots)
659 kvm->nmemslots = mem->slot + 1;
660
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200662 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663
Zhang Xiantao0de10342007-11-20 16:25:04 +0800664 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
665 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200666 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800667 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200668 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800669 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800670 }
671
Glauber Costa6f897242008-12-03 13:40:51 -0200672 kvm_free_physmem_slot(&old, npages ? &new : NULL);
673 /* Slot deletion case: we have to update the current slot */
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300674 spin_lock(&kvm->mmu_lock);
Glauber Costa6f897242008-12-03 13:40:51 -0200675 if (!npages)
676 *memslot = old;
Marcelo Tosattib43b1902009-05-12 18:55:44 -0300677 spin_unlock(&kvm->mmu_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800678#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300679 /* map the pages in iommu page table */
680 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
681 if (r)
682 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800683#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 return 0;
685
Sheng Yangf78e0e22007-10-29 09:40:42 +0800686out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 kvm_free_physmem_slot(&new, &old);
688out:
689 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200690
691}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800692EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
693
694int kvm_set_memory_region(struct kvm *kvm,
695 struct kvm_userspace_memory_region *mem,
696 int user_alloc)
697{
698 int r;
699
Izik Eidus72dc67a2008-02-10 18:04:15 +0200700 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800701 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200702 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800703 return r;
704}
Izik Eidus210c7c42007-10-24 23:52:57 +0200705EXPORT_SYMBOL_GPL(kvm_set_memory_region);
706
Carsten Otte1fe779f2007-10-29 16:08:35 +0100707int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
708 struct
709 kvm_userspace_memory_region *mem,
710 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200711{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200712 if (mem->slot >= KVM_MEMORY_SLOTS)
713 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200714 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715}
716
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800717int kvm_get_dirty_log(struct kvm *kvm,
718 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719{
720 struct kvm_memory_slot *memslot;
721 int r, i;
722 int n;
723 unsigned long any = 0;
724
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 r = -EINVAL;
726 if (log->slot >= KVM_MEMORY_SLOTS)
727 goto out;
728
729 memslot = &kvm->memslots[log->slot];
730 r = -ENOENT;
731 if (!memslot->dirty_bitmap)
732 goto out;
733
Uri Lublincd1a4a92007-02-22 16:43:09 +0200734 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
Uri Lublincd1a4a92007-02-22 16:43:09 +0200736 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 any = memslot->dirty_bitmap[i];
738
739 r = -EFAULT;
740 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
741 goto out;
742
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800743 if (any)
744 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745
746 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 return r;
749}
750
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300751void kvm_disable_largepages(void)
752{
753 largepages_enabled = false;
754}
755EXPORT_SYMBOL_GPL(kvm_disable_largepages);
756
Izik Eiduscea7bb22007-10-17 19:17:48 +0200757int is_error_page(struct page *page)
758{
759 return page == bad_page;
760}
761EXPORT_SYMBOL_GPL(is_error_page);
762
Anthony Liguori35149e22008-04-02 14:46:56 -0500763int is_error_pfn(pfn_t pfn)
764{
765 return pfn == bad_pfn;
766}
767EXPORT_SYMBOL_GPL(is_error_pfn);
768
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200769static inline unsigned long bad_hva(void)
770{
771 return PAGE_OFFSET;
772}
773
774int kvm_is_error_hva(unsigned long addr)
775{
776 return addr == bad_hva();
777}
778EXPORT_SYMBOL_GPL(kvm_is_error_hva);
779
Izik Eidus28430992008-10-03 17:40:32 +0300780struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781{
782 int i;
783
784 for (i = 0; i < kvm->nmemslots; ++i) {
785 struct kvm_memory_slot *memslot = &kvm->memslots[i];
786
787 if (gfn >= memslot->base_gfn
788 && gfn < memslot->base_gfn + memslot->npages)
789 return memslot;
790 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000791 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792}
Izik Eidus28430992008-10-03 17:40:32 +0300793EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300794
795struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
796{
797 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300798 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300799}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800
Izik Eiduse0d62c72007-10-24 23:57:46 +0200801int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
802{
803 int i;
804
805 gfn = unalias_gfn(kvm, gfn);
806 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
807 struct kvm_memory_slot *memslot = &kvm->memslots[i];
808
809 if (gfn >= memslot->base_gfn
810 && gfn < memslot->base_gfn + memslot->npages)
811 return 1;
812 }
813 return 0;
814}
815EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
816
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300817unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200818{
819 struct kvm_memory_slot *slot;
820
821 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300822 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200823 if (!slot)
824 return bad_hva();
825 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
826}
Sheng Yang0d150292008-04-25 21:44:50 +0800827EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200828
Anthony Liguori35149e22008-04-02 14:46:56 -0500829pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300830{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500831 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200832 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500833 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500834 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300835
Avi Kivity60395222007-10-21 11:03:36 +0200836 might_sleep();
837
Izik Eidus539cb662007-11-11 22:05:04 +0200838 addr = gfn_to_hva(kvm, gfn);
839 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200840 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500841 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200842 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200843
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300844 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200845
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500846 if (unlikely(npages != 1)) {
847 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500848
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300849 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500850 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300851
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500852 if (vma == NULL || addr < vma->vm_start ||
853 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300854 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500855 get_page(bad_page);
856 return page_to_pfn(bad_page);
857 }
858
859 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300860 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800861 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500862 } else
863 pfn = page_to_pfn(page[0]);
864
865 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500866}
867
868EXPORT_SYMBOL_GPL(gfn_to_pfn);
869
870struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
871{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500872 pfn_t pfn;
873
874 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800875 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500876 return pfn_to_page(pfn);
877
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800878 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500879
880 get_page(bad_page);
881 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300882}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500883
Avi Kivity954bbbc2007-03-30 14:02:32 +0300884EXPORT_SYMBOL_GPL(gfn_to_page);
885
Izik Eidusb4231d62007-11-20 11:49:33 +0200886void kvm_release_page_clean(struct page *page)
887{
Anthony Liguori35149e22008-04-02 14:46:56 -0500888 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200889}
890EXPORT_SYMBOL_GPL(kvm_release_page_clean);
891
Anthony Liguori35149e22008-04-02 14:46:56 -0500892void kvm_release_pfn_clean(pfn_t pfn)
893{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800894 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500895 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500896}
897EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
898
Izik Eidusb4231d62007-11-20 11:49:33 +0200899void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200900{
Anthony Liguori35149e22008-04-02 14:46:56 -0500901 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200902}
Izik Eidusb4231d62007-11-20 11:49:33 +0200903EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200904
Anthony Liguori35149e22008-04-02 14:46:56 -0500905void kvm_release_pfn_dirty(pfn_t pfn)
906{
907 kvm_set_pfn_dirty(pfn);
908 kvm_release_pfn_clean(pfn);
909}
910EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
911
912void kvm_set_page_dirty(struct page *page)
913{
914 kvm_set_pfn_dirty(page_to_pfn(page));
915}
916EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
917
918void kvm_set_pfn_dirty(pfn_t pfn)
919{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800920 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500921 struct page *page = pfn_to_page(pfn);
922 if (!PageReserved(page))
923 SetPageDirty(page);
924 }
Anthony Liguori35149e22008-04-02 14:46:56 -0500925}
926EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
927
928void kvm_set_pfn_accessed(pfn_t pfn)
929{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800930 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500931 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500932}
933EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
934
935void kvm_get_pfn(pfn_t pfn)
936{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800937 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500938 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -0500939}
940EXPORT_SYMBOL_GPL(kvm_get_pfn);
941
Izik Eidus195aefd2007-10-01 22:14:18 +0200942static int next_segment(unsigned long len, int offset)
943{
944 if (len > PAGE_SIZE - offset)
945 return PAGE_SIZE - offset;
946 else
947 return len;
948}
949
950int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
951 int len)
952{
Izik Eiduse0506bc2007-11-11 22:10:22 +0200953 int r;
954 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +0200955
Izik Eiduse0506bc2007-11-11 22:10:22 +0200956 addr = gfn_to_hva(kvm, gfn);
957 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +0200958 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +0200959 r = copy_from_user(data, (void __user *)addr + offset, len);
960 if (r)
961 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200962 return 0;
963}
964EXPORT_SYMBOL_GPL(kvm_read_guest_page);
965
966int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
967{
968 gfn_t gfn = gpa >> PAGE_SHIFT;
969 int seg;
970 int offset = offset_in_page(gpa);
971 int ret;
972
973 while ((seg = next_segment(len, offset)) != 0) {
974 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
975 if (ret < 0)
976 return ret;
977 offset = 0;
978 len -= seg;
979 data += seg;
980 ++gfn;
981 }
982 return 0;
983}
984EXPORT_SYMBOL_GPL(kvm_read_guest);
985
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500986int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
987 unsigned long len)
988{
989 int r;
990 unsigned long addr;
991 gfn_t gfn = gpa >> PAGE_SHIFT;
992 int offset = offset_in_page(gpa);
993
994 addr = gfn_to_hva(kvm, gfn);
995 if (kvm_is_error_hva(addr))
996 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100997 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -0500998 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +0100999 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001000 if (r)
1001 return -EFAULT;
1002 return 0;
1003}
1004EXPORT_SYMBOL(kvm_read_guest_atomic);
1005
Izik Eidus195aefd2007-10-01 22:14:18 +02001006int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1007 int offset, int len)
1008{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001009 int r;
1010 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001011
Izik Eiduse0506bc2007-11-11 22:10:22 +02001012 addr = gfn_to_hva(kvm, gfn);
1013 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001014 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001015 r = copy_to_user((void __user *)addr + offset, data, len);
1016 if (r)
1017 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001018 mark_page_dirty(kvm, gfn);
1019 return 0;
1020}
1021EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1022
1023int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1024 unsigned long len)
1025{
1026 gfn_t gfn = gpa >> PAGE_SHIFT;
1027 int seg;
1028 int offset = offset_in_page(gpa);
1029 int ret;
1030
1031 while ((seg = next_segment(len, offset)) != 0) {
1032 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1033 if (ret < 0)
1034 return ret;
1035 offset = 0;
1036 len -= seg;
1037 data += seg;
1038 ++gfn;
1039 }
1040 return 0;
1041}
1042
1043int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1044{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001045 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001046}
1047EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1048
1049int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1050{
1051 gfn_t gfn = gpa >> PAGE_SHIFT;
1052 int seg;
1053 int offset = offset_in_page(gpa);
1054 int ret;
1055
1056 while ((seg = next_segment(len, offset)) != 0) {
1057 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1058 if (ret < 0)
1059 return ret;
1060 offset = 0;
1061 len -= seg;
1062 ++gfn;
1063 }
1064 return 0;
1065}
1066EXPORT_SYMBOL_GPL(kvm_clear_guest);
1067
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1069{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001070 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071
Uri Lublin3b6fff12007-10-30 10:42:09 +02001072 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001073 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001074 if (memslot && memslot->dirty_bitmap) {
1075 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076
Rusty Russell7e9d6192007-07-31 20:41:14 +10001077 /* avoid RMW */
Alexander Grafc8240bd2009-10-30 05:47:26 +00001078 if (!generic_test_le_bit(rel_gfn, memslot->dirty_bitmap))
1079 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 }
1081}
1082
Eddie Dongb6958ce2007-07-18 12:15:21 +03001083/*
1084 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1085 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001086void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001087{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001088 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001089
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001090 for (;;) {
1091 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001092
Gleb Natapova1b37102009-07-09 15:33:52 +03001093 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001094 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001095 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001096 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001097 if (kvm_cpu_has_pending_timer(vcpu))
1098 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001099 if (signal_pending(current))
1100 break;
1101
Eddie Dongb6958ce2007-07-18 12:15:21 +03001102 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001103 }
1104
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001105 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001106}
1107
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108void kvm_resched(struct kvm_vcpu *vcpu)
1109{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001110 if (!need_resched())
1111 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113}
1114EXPORT_SYMBOL_GPL(kvm_resched);
1115
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001116void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1117{
1118 ktime_t expires;
1119 DEFINE_WAIT(wait);
1120
1121 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1122
1123 /* Sleep for 100 us, and hope lock-holder got scheduled */
1124 expires = ktime_add_ns(ktime_get(), 100000UL);
1125 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1126
1127 finish_wait(&vcpu->wq, &wait);
1128}
1129EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1130
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001131static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001132{
1133 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001134 struct page *page;
1135
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001136 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001137 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001138#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001139 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001140 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001141#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001142#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1143 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1144 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1145#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001146 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001147 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001148 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001149 vmf->page = page;
1150 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001151}
1152
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001153static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001154 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001155};
1156
1157static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1158{
1159 vma->vm_ops = &kvm_vcpu_vm_ops;
1160 return 0;
1161}
1162
Avi Kivitybccf2152007-02-21 18:04:26 +02001163static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1164{
1165 struct kvm_vcpu *vcpu = filp->private_data;
1166
Al Viro66c0b392008-04-19 20:33:56 +01001167 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001168 return 0;
1169}
1170
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001171static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001172 .release = kvm_vcpu_release,
1173 .unlocked_ioctl = kvm_vcpu_ioctl,
1174 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001175 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001176};
1177
1178/*
1179 * Allocates an inode for the vcpu.
1180 */
1181static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1182{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001183 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001184}
1185
Avi Kivityc5ea7662007-02-20 18:41:05 +02001186/*
1187 * Creates some virtual cpus. Good luck creating more than one.
1188 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001189static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001190{
1191 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001192 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001193
Gleb Natapov73880c82009-06-09 15:56:28 +03001194 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001195 if (IS_ERR(vcpu))
1196 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001197
Avi Kivity15ad7142007-07-11 18:17:21 +03001198 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1199
Avi Kivity26e52152007-11-20 15:30:24 +02001200 r = kvm_arch_vcpu_setup(vcpu);
1201 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001202 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001203
Shaohua Li11ec2802007-07-23 14:51:37 +08001204 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001205 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1206 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001207 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001208 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001209
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001210 kvm_for_each_vcpu(r, v, kvm)
1211 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001212 r = -EEXIST;
1213 goto vcpu_destroy;
1214 }
1215
1216 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001217
1218 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001219 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001220 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001221 if (r < 0) {
1222 kvm_put_kvm(kvm);
1223 goto vcpu_destroy;
1224 }
1225
1226 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1227 smp_wmb();
1228 atomic_inc(&kvm->online_vcpus);
1229
1230#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1231 if (kvm->bsp_vcpu_id == id)
1232 kvm->bsp_vcpu = vcpu;
1233#endif
1234 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001235 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001236
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001237vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001238 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001239 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001240 return r;
1241}
1242
Avi Kivity1961d272007-03-05 19:46:05 +02001243static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1244{
1245 if (sigset) {
1246 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1247 vcpu->sigset_active = 1;
1248 vcpu->sigset = *sigset;
1249 } else
1250 vcpu->sigset_active = 0;
1251 return 0;
1252}
1253
Avi Kivitybccf2152007-02-21 18:04:26 +02001254static long kvm_vcpu_ioctl(struct file *filp,
1255 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256{
Avi Kivitybccf2152007-02-21 18:04:26 +02001257 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001258 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001259 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001260 struct kvm_fpu *fpu = NULL;
1261 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001263 if (vcpu->kvm->mm != current->mm)
1264 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001266 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001267 r = -EINVAL;
1268 if (arg)
1269 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001270 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001273 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001275 r = -ENOMEM;
1276 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1277 if (!kvm_regs)
1278 goto out;
1279 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001281 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001283 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1284 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001286out_free1:
1287 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288 break;
1289 }
1290 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001291 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001293 r = -ENOMEM;
1294 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1295 if (!kvm_regs)
1296 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001298 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1299 goto out_free2;
1300 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001302 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001304out_free2:
1305 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 break;
1307 }
1308 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001309 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1310 r = -ENOMEM;
1311 if (!kvm_sregs)
1312 goto out;
1313 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 if (r)
1315 goto out;
1316 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001317 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 goto out;
1319 r = 0;
1320 break;
1321 }
1322 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001323 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1324 r = -ENOMEM;
1325 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001327 r = -EFAULT;
1328 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1329 goto out;
1330 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 if (r)
1332 goto out;
1333 r = 0;
1334 break;
1335 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001336 case KVM_GET_MP_STATE: {
1337 struct kvm_mp_state mp_state;
1338
1339 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1340 if (r)
1341 goto out;
1342 r = -EFAULT;
1343 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1344 goto out;
1345 r = 0;
1346 break;
1347 }
1348 case KVM_SET_MP_STATE: {
1349 struct kvm_mp_state mp_state;
1350
1351 r = -EFAULT;
1352 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1353 goto out;
1354 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1355 if (r)
1356 goto out;
1357 r = 0;
1358 break;
1359 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 case KVM_TRANSLATE: {
1361 struct kvm_translation tr;
1362
1363 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001364 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001366 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 if (r)
1368 goto out;
1369 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001370 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 goto out;
1372 r = 0;
1373 break;
1374 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001375 case KVM_SET_GUEST_DEBUG: {
1376 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
1378 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001379 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001381 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 if (r)
1383 goto out;
1384 r = 0;
1385 break;
1386 }
Avi Kivity1961d272007-03-05 19:46:05 +02001387 case KVM_SET_SIGNAL_MASK: {
1388 struct kvm_signal_mask __user *sigmask_arg = argp;
1389 struct kvm_signal_mask kvm_sigmask;
1390 sigset_t sigset, *p;
1391
1392 p = NULL;
1393 if (argp) {
1394 r = -EFAULT;
1395 if (copy_from_user(&kvm_sigmask, argp,
1396 sizeof kvm_sigmask))
1397 goto out;
1398 r = -EINVAL;
1399 if (kvm_sigmask.len != sizeof sigset)
1400 goto out;
1401 r = -EFAULT;
1402 if (copy_from_user(&sigset, sigmask_arg->sigset,
1403 sizeof sigset))
1404 goto out;
1405 p = &sigset;
1406 }
1407 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1408 break;
1409 }
Avi Kivityb8836732007-04-01 16:34:31 +03001410 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001411 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1412 r = -ENOMEM;
1413 if (!fpu)
1414 goto out;
1415 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001416 if (r)
1417 goto out;
1418 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001419 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001420 goto out;
1421 r = 0;
1422 break;
1423 }
1424 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001425 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1426 r = -ENOMEM;
1427 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001428 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001429 r = -EFAULT;
1430 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1431 goto out;
1432 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001433 if (r)
1434 goto out;
1435 r = 0;
1436 break;
1437 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001438 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001439 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001440 }
1441out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001442 kfree(fpu);
1443 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001444 return r;
1445}
1446
1447static long kvm_vm_ioctl(struct file *filp,
1448 unsigned int ioctl, unsigned long arg)
1449{
1450 struct kvm *kvm = filp->private_data;
1451 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001452 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001453
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001454 if (kvm->mm != current->mm)
1455 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001456 switch (ioctl) {
1457 case KVM_CREATE_VCPU:
1458 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1459 if (r < 0)
1460 goto out;
1461 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001462 case KVM_SET_USER_MEMORY_REGION: {
1463 struct kvm_userspace_memory_region kvm_userspace_mem;
1464
1465 r = -EFAULT;
1466 if (copy_from_user(&kvm_userspace_mem, argp,
1467 sizeof kvm_userspace_mem))
1468 goto out;
1469
1470 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 if (r)
1472 goto out;
1473 break;
1474 }
1475 case KVM_GET_DIRTY_LOG: {
1476 struct kvm_dirty_log log;
1477
1478 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001479 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001481 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 if (r)
1483 goto out;
1484 break;
1485 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001486#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1487 case KVM_REGISTER_COALESCED_MMIO: {
1488 struct kvm_coalesced_mmio_zone zone;
1489 r = -EFAULT;
1490 if (copy_from_user(&zone, argp, sizeof zone))
1491 goto out;
1492 r = -ENXIO;
1493 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1494 if (r)
1495 goto out;
1496 r = 0;
1497 break;
1498 }
1499 case KVM_UNREGISTER_COALESCED_MMIO: {
1500 struct kvm_coalesced_mmio_zone zone;
1501 r = -EFAULT;
1502 if (copy_from_user(&zone, argp, sizeof zone))
1503 goto out;
1504 r = -ENXIO;
1505 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1506 if (r)
1507 goto out;
1508 r = 0;
1509 break;
1510 }
1511#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001512 case KVM_IRQFD: {
1513 struct kvm_irqfd data;
1514
1515 r = -EFAULT;
1516 if (copy_from_user(&data, argp, sizeof data))
1517 goto out;
1518 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1519 break;
1520 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001521 case KVM_IOEVENTFD: {
1522 struct kvm_ioeventfd data;
1523
1524 r = -EFAULT;
1525 if (copy_from_user(&data, argp, sizeof data))
1526 goto out;
1527 r = kvm_ioeventfd(kvm, &data);
1528 break;
1529 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001530#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1531 case KVM_SET_BOOT_CPU_ID:
1532 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001533 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001534 if (atomic_read(&kvm->online_vcpus) != 0)
1535 r = -EBUSY;
1536 else
1537 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001538 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001539 break;
1540#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001541 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001542 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001543 if (r == -ENOTTY)
1544 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001545 }
1546out:
1547 return r;
1548}
1549
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001550#ifdef CONFIG_COMPAT
1551struct compat_kvm_dirty_log {
1552 __u32 slot;
1553 __u32 padding1;
1554 union {
1555 compat_uptr_t dirty_bitmap; /* one bit per page */
1556 __u64 padding2;
1557 };
1558};
1559
1560static long kvm_vm_compat_ioctl(struct file *filp,
1561 unsigned int ioctl, unsigned long arg)
1562{
1563 struct kvm *kvm = filp->private_data;
1564 int r;
1565
1566 if (kvm->mm != current->mm)
1567 return -EIO;
1568 switch (ioctl) {
1569 case KVM_GET_DIRTY_LOG: {
1570 struct compat_kvm_dirty_log compat_log;
1571 struct kvm_dirty_log log;
1572
1573 r = -EFAULT;
1574 if (copy_from_user(&compat_log, (void __user *)arg,
1575 sizeof(compat_log)))
1576 goto out;
1577 log.slot = compat_log.slot;
1578 log.padding1 = compat_log.padding1;
1579 log.padding2 = compat_log.padding2;
1580 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1581
1582 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1583 if (r)
1584 goto out;
1585 break;
1586 }
1587 default:
1588 r = kvm_vm_ioctl(filp, ioctl, arg);
1589 }
1590
1591out:
1592 return r;
1593}
1594#endif
1595
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001596static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001597{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001598 struct page *page[1];
1599 unsigned long addr;
1600 int npages;
1601 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001602 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001603
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001604 addr = gfn_to_hva(kvm, gfn);
1605 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001606 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001607
1608 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1609 NULL);
1610 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001611 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001612
1613 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001614 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001615}
1616
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001617static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001618 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001619};
1620
1621static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1622{
1623 vma->vm_ops = &kvm_vm_vm_ops;
1624 return 0;
1625}
1626
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001627static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001628 .release = kvm_vm_release,
1629 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001630#ifdef CONFIG_COMPAT
1631 .compat_ioctl = kvm_vm_compat_ioctl,
1632#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001633 .mmap = kvm_vm_mmap,
1634};
1635
1636static int kvm_dev_ioctl_create_vm(void)
1637{
Al Viro2030a422008-02-23 06:46:49 -05001638 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001639 struct kvm *kvm;
1640
Avi Kivityf17abe92007-02-21 19:28:04 +02001641 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001642 if (IS_ERR(kvm))
1643 return PTR_ERR(kvm);
Roland Dreier628ff7c2009-12-18 09:41:24 -08001644 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
Al Viro2030a422008-02-23 06:46:49 -05001645 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001646 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001647
Avi Kivityf17abe92007-02-21 19:28:04 +02001648 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001649}
1650
Avi Kivity1a811b62008-12-08 18:25:27 +02001651static long kvm_dev_ioctl_check_extension_generic(long arg)
1652{
1653 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001654 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001655 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001656 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001657#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1658 case KVM_CAP_SET_BOOT_CPU_ID:
1659#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001660 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001661 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001662#ifdef CONFIG_HAVE_KVM_IRQCHIP
1663 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001664 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001665#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001666 default:
1667 break;
1668 }
1669 return kvm_dev_ioctl_check_extension(arg);
1670}
1671
Avi Kivityf17abe92007-02-21 19:28:04 +02001672static long kvm_dev_ioctl(struct file *filp,
1673 unsigned int ioctl, unsigned long arg)
1674{
Avi Kivity07c45a32007-03-07 13:05:38 +02001675 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001676
1677 switch (ioctl) {
1678 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001679 r = -EINVAL;
1680 if (arg)
1681 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001682 r = KVM_API_VERSION;
1683 break;
1684 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001685 r = -EINVAL;
1686 if (arg)
1687 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001688 r = kvm_dev_ioctl_create_vm();
1689 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001690 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001691 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001692 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001693 case KVM_GET_VCPU_MMAP_SIZE:
1694 r = -EINVAL;
1695 if (arg)
1696 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001697 r = PAGE_SIZE; /* struct kvm_run */
1698#ifdef CONFIG_X86
1699 r += PAGE_SIZE; /* pio data page */
1700#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001701#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1702 r += PAGE_SIZE; /* coalesced mmio ring page */
1703#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001704 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001705 case KVM_TRACE_ENABLE:
1706 case KVM_TRACE_PAUSE:
1707 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001708 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001709 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001710 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001711 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 }
1713out:
1714 return r;
1715}
1716
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 .unlocked_ioctl = kvm_dev_ioctl,
1719 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720};
1721
1722static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001723 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 "kvm",
1725 &kvm_chardev_ops,
1726};
1727
Avi Kivity1b6c0162007-05-24 13:03:52 +03001728static void hardware_enable(void *junk)
1729{
1730 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02001731 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03001732
Rusty Russell7f59f492008-12-07 21:25:45 +10301733 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001734 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02001735
Rusty Russell7f59f492008-12-07 21:25:45 +10301736 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02001737
1738 r = kvm_arch_hardware_enable(NULL);
1739
1740 if (r) {
1741 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1742 atomic_inc(&hardware_enable_failed);
1743 printk(KERN_INFO "kvm: enabling virtualization on "
1744 "CPU%d failed\n", cpu);
1745 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03001746}
1747
1748static void hardware_disable(void *junk)
1749{
1750 int cpu = raw_smp_processor_id();
1751
Rusty Russell7f59f492008-12-07 21:25:45 +10301752 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001753 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301754 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001755 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001756}
1757
Alexander Graf10474ae2009-09-15 11:37:46 +02001758static void hardware_disable_all_nolock(void)
1759{
1760 BUG_ON(!kvm_usage_count);
1761
1762 kvm_usage_count--;
1763 if (!kvm_usage_count)
1764 on_each_cpu(hardware_disable, NULL, 1);
1765}
1766
1767static void hardware_disable_all(void)
1768{
1769 spin_lock(&kvm_lock);
1770 hardware_disable_all_nolock();
1771 spin_unlock(&kvm_lock);
1772}
1773
1774static int hardware_enable_all(void)
1775{
1776 int r = 0;
1777
1778 spin_lock(&kvm_lock);
1779
1780 kvm_usage_count++;
1781 if (kvm_usage_count == 1) {
1782 atomic_set(&hardware_enable_failed, 0);
1783 on_each_cpu(hardware_enable, NULL, 1);
1784
1785 if (atomic_read(&hardware_enable_failed)) {
1786 hardware_disable_all_nolock();
1787 r = -EBUSY;
1788 }
1789 }
1790
1791 spin_unlock(&kvm_lock);
1792
1793 return r;
1794}
1795
Avi Kivity774c47f2007-02-12 00:54:47 -08001796static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1797 void *v)
1798{
1799 int cpu = (long)v;
1800
Alexander Graf10474ae2009-09-15 11:37:46 +02001801 if (!kvm_usage_count)
1802 return NOTIFY_OK;
1803
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001804 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001805 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001806 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03001807 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1808 cpu);
1809 hardware_disable(NULL);
1810 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001811 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001812 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1813 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001814 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001815 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001816 case CPU_ONLINE:
1817 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1818 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001819 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001820 break;
1821 }
1822 return NOTIFY_OK;
1823}
1824
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001825
1826asmlinkage void kvm_handle_fault_on_reboot(void)
1827{
1828 if (kvm_rebooting)
1829 /* spin while reset goes on */
1830 while (true)
1831 ;
1832 /* Fault while not rebooting. We want the trace. */
1833 BUG();
1834}
1835EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1836
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001837static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001838 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001839{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001840 /*
1841 * Some (well, at least mine) BIOSes hang on reboot if
1842 * in vmx root mode.
1843 *
1844 * And Intel TXT required VMX off for all cpu when system shutdown.
1845 */
1846 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1847 kvm_rebooting = true;
1848 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001849 return NOTIFY_OK;
1850}
1851
1852static struct notifier_block kvm_reboot_notifier = {
1853 .notifier_call = kvm_reboot,
1854 .priority = 0,
1855};
1856
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001857void kvm_io_bus_init(struct kvm_io_bus *bus)
1858{
1859 memset(bus, 0, sizeof(*bus));
1860}
1861
1862void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1863{
1864 int i;
1865
1866 for (i = 0; i < bus->dev_count; i++) {
1867 struct kvm_io_device *pos = bus->devs[i];
1868
1869 kvm_iodevice_destructor(pos);
1870 }
1871}
1872
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001873/* kvm_io_bus_write - called under kvm->slots_lock */
1874int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
1875 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001876{
1877 int i;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001878 for (i = 0; i < bus->dev_count; i++)
1879 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1880 return 0;
1881 return -EOPNOTSUPP;
1882}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001883
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001884/* kvm_io_bus_read - called under kvm->slots_lock */
1885int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
1886{
1887 int i;
1888 for (i = 0; i < bus->dev_count; i++)
1889 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
1890 return 0;
1891 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001892}
1893
Gregory Haskins090b7af2009-07-07 17:08:44 -04001894int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001895 struct kvm_io_device *dev)
1896{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001897 int ret;
1898
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001899 down_write(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -04001900 ret = __kvm_io_bus_register_dev(bus, dev);
1901 up_write(&kvm->slots_lock);
1902
1903 return ret;
1904}
1905
1906/* An unlocked version. Caller must have write lock on slots_lock. */
1907int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
1908 struct kvm_io_device *dev)
1909{
1910 if (bus->dev_count > NR_IOBUS_DEVS-1)
1911 return -ENOSPC;
1912
1913 bus->devs[bus->dev_count++] = dev;
1914
1915 return 0;
1916}
1917
1918void kvm_io_bus_unregister_dev(struct kvm *kvm,
1919 struct kvm_io_bus *bus,
1920 struct kvm_io_device *dev)
1921{
1922 down_write(&kvm->slots_lock);
1923 __kvm_io_bus_unregister_dev(bus, dev);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03001924 up_write(&kvm->slots_lock);
1925}
1926
1927/* An unlocked version. Caller must have write lock on slots_lock. */
Gregory Haskins090b7af2009-07-07 17:08:44 -04001928void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
1929 struct kvm_io_device *dev)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001930{
Gregory Haskins090b7af2009-07-07 17:08:44 -04001931 int i;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001932
Gregory Haskins090b7af2009-07-07 17:08:44 -04001933 for (i = 0; i < bus->dev_count; i++)
1934 if (bus->devs[i] == dev) {
1935 bus->devs[i] = bus->devs[--bus->dev_count];
1936 break;
1937 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001938}
1939
Avi Kivity774c47f2007-02-12 00:54:47 -08001940static struct notifier_block kvm_cpu_notifier = {
1941 .notifier_call = kvm_cpu_hotplug,
1942 .priority = 20, /* must be > scheduler priority */
1943};
1944
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001945static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001946{
1947 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001948 struct kvm *kvm;
1949
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001950 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001951 spin_lock(&kvm_lock);
1952 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001953 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001954 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001955 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001956}
1957
1958DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1959
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001960static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001961{
1962 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001963 struct kvm *kvm;
1964 struct kvm_vcpu *vcpu;
1965 int i;
1966
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001967 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001968 spin_lock(&kvm_lock);
1969 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001970 kvm_for_each_vcpu(i, vcpu, kvm)
1971 *val += *(u32 *)((void *)vcpu + offset);
1972
Avi Kivity1165f5f2007-04-19 17:27:43 +03001973 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001974 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001975}
1976
Avi Kivityba1389b2007-11-18 16:24:12 +02001977DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1978
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001979static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02001980 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1981 [KVM_STAT_VM] = &vm_stat_fops,
1982};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001983
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001984static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985{
1986 struct kvm_stats_debugfs_item *p;
1987
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001988 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001990 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001991 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001992 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993}
1994
1995static void kvm_exit_debug(void)
1996{
1997 struct kvm_stats_debugfs_item *p;
1998
1999 for (p = debugfs_entries; p->name; ++p)
2000 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002001 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002}
2003
Avi Kivity59ae6c62007-02-12 00:54:48 -08002004static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2005{
Alexander Graf10474ae2009-09-15 11:37:46 +02002006 if (kvm_usage_count)
2007 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002008 return 0;
2009}
2010
2011static int kvm_resume(struct sys_device *dev)
2012{
Alexander Graf10474ae2009-09-15 11:37:46 +02002013 if (kvm_usage_count)
2014 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002015 return 0;
2016}
2017
2018static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002019 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002020 .suspend = kvm_suspend,
2021 .resume = kvm_resume,
2022};
2023
2024static struct sys_device kvm_sysdev = {
2025 .id = 0,
2026 .cls = &kvm_sysdev_class,
2027};
2028
Izik Eiduscea7bb22007-10-17 19:17:48 +02002029struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002030pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031
Avi Kivity15ad7142007-07-11 18:17:21 +03002032static inline
2033struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2034{
2035 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2036}
2037
2038static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2039{
2040 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2041
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002042 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002043}
2044
2045static void kvm_sched_out(struct preempt_notifier *pn,
2046 struct task_struct *next)
2047{
2048 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2049
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002050 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002051}
2052
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002053int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002054 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055{
2056 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002057 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002059 r = kvm_arch_init(opaque);
2060 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002061 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002062
2063 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2064
2065 if (bad_page == NULL) {
2066 r = -ENOMEM;
2067 goto out;
2068 }
2069
Anthony Liguori35149e22008-04-02 14:46:56 -05002070 bad_pfn = page_to_pfn(bad_page);
2071
Avi Kivity8437a612009-06-06 14:52:35 -07002072 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302073 r = -ENOMEM;
2074 goto out_free_0;
2075 }
2076
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002077 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302079 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002080
Yang, Sheng002c7f72007-07-31 14:23:01 +03002081 for_each_online_cpu(cpu) {
2082 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002083 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002084 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002085 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002086 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002087 }
2088
Avi Kivity774c47f2007-02-12 00:54:47 -08002089 r = register_cpu_notifier(&kvm_cpu_notifier);
2090 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002091 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 register_reboot_notifier(&kvm_reboot_notifier);
2093
Avi Kivity59ae6c62007-02-12 00:54:48 -08002094 r = sysdev_class_register(&kvm_sysdev_class);
2095 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002096 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002097
2098 r = sysdev_register(&kvm_sysdev);
2099 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002100 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002101
Rusty Russellc16f8622007-07-30 21:12:19 +10002102 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2103 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002104 __alignof__(struct kvm_vcpu),
2105 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002106 if (!kvm_vcpu_cache) {
2107 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002108 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002109 }
2110
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002112 kvm_vm_fops.owner = module;
2113 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114
2115 r = misc_register(&kvm_dev);
2116 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002117 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 goto out_free;
2119 }
2120
Avi Kivity15ad7142007-07-11 18:17:21 +03002121 kvm_preempt_ops.sched_in = kvm_sched_in;
2122 kvm_preempt_ops.sched_out = kvm_sched_out;
2123
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002124 kvm_init_debug();
2125
Avi Kivityc7addb92007-09-16 18:58:32 +02002126 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127
2128out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002129 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002130out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002131 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002132out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002133 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002134out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002136 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002137out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002138out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002139 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302140out_free_0a:
2141 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002142out_free_0:
2143 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002144out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002145 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002146out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 return r;
2148}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002149EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002151void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002153 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002154 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002156 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002157 sysdev_unregister(&kvm_sysdev);
2158 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002160 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002161 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002162 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002163 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302164 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002165 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002167EXPORT_SYMBOL_GPL(kvm_exit);