blob: f032806a212f9d8e02faa7823f65262813d82015 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/miscdevice.h>
27#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/debugfs.h>
30#include <linux/highmem.h>
31#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080032#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080033#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040034#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030035#include <linux/cpumask.h>
36#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040037#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030038#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050039#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020040#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050041#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050042#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080043#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030044#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020045#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020046#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010047#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Avi Kivitye4956062007-06-28 14:15:57 -040050#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/io.h>
52#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020053#include <asm/pgtable.h>
Alexander Grafc8240bd2009-10-30 05:47:26 +000054#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Laurent Vivier5f94c172008-05-30 16:05:54 +020056#include "coalesced_mmio.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020057
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
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020089static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
90
Hannes Edere8ba5d32008-11-28 17:02:42 +010091static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030092
Marcelo Tosatti54dee992009-06-11 12:07:44 -030093static bool largepages_enabled = true;
94
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +080095inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030096{
Joerg Roedelfc5659c2009-02-18 14:08:58 +010097 if (pfn_valid(pfn)) {
98 struct page *page = compound_head(pfn_to_page(pfn));
99 return PageReserved(page);
100 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300101
102 return true;
103}
104
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105/*
106 * Switches to specified vcpu, until a matching vcpu_put()
107 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200108void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109{
Avi Kivity15ad7142007-07-11 18:17:21 +0300110 int cpu;
111
Avi Kivitybccf2152007-02-21 18:04:26 +0200112 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300113 cpu = get_cpu();
114 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200115 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300116 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200117}
118
Carsten Otte313a3dc2007-10-11 19:16:52 +0200119void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120{
Avi Kivity15ad7142007-07-11 18:17:21 +0300121 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200122 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300123 preempt_notifier_unregister(&vcpu->preempt_notifier);
124 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 mutex_unlock(&vcpu->mutex);
126}
127
Avi Kivityd9e368d2007-06-07 19:18:30 +0300128static void ack_flush(void *_completed)
129{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300130}
131
Rusty Russell49846892008-12-08 20:26:24 +1030132static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300133{
Avi Kivity597a5f52008-07-20 14:24:22 +0300134 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030135 cpumask_var_t cpus;
136 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300137 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300138
Li Zefan79f55992009-06-15 14:58:26 +0800139 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030140
Avi Kivity70e335e2010-02-18 11:25:22 +0200141 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200142 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300143 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030144 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300145 continue;
146 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030147 if (cpus != NULL && cpu != -1 && cpu != me)
148 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300149 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030150 if (unlikely(cpus == NULL))
151 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
152 else if (!cpumask_empty(cpus))
153 smp_call_function_many(cpus, ack_flush, NULL, 1);
154 else
155 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200156 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030157 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030158 return called;
159}
160
161void kvm_flush_remote_tlbs(struct kvm *kvm)
162{
163 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
164 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300165}
166
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500167void kvm_reload_remote_mmus(struct kvm *kvm)
168{
Rusty Russell49846892008-12-08 20:26:24 +1030169 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500170}
171
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000172int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
173{
174 struct page *page;
175 int r;
176
177 mutex_init(&vcpu->mutex);
178 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000179 vcpu->kvm = kvm;
180 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300181 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000182
183 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
184 if (!page) {
185 r = -ENOMEM;
186 goto fail;
187 }
188 vcpu->run = page_address(page);
189
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800190 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800192 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000193 return 0;
194
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000195fail_free_run:
196 free_page((unsigned long)vcpu->run);
197fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000198 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000199}
200EXPORT_SYMBOL_GPL(kvm_vcpu_init);
201
202void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
203{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800204 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000205 free_page((unsigned long)vcpu->run);
206}
207EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
208
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200209#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
210static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
211{
212 return container_of(mn, struct kvm, mmu_notifier);
213}
214
215static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
216 struct mm_struct *mm,
217 unsigned long address)
218{
219 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200220 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200221
222 /*
223 * When ->invalidate_page runs, the linux pte has been zapped
224 * already but the page is still allocated until
225 * ->invalidate_page returns. So if we increase the sequence
226 * here the kvm page fault will notice if the spte can't be
227 * established because the page is going to be freed. If
228 * instead the kvm page fault establishes the spte before
229 * ->invalidate_page runs, kvm_unmap_hva will release it
230 * before returning.
231 *
232 * The sequence increase only need to be seen at spin_unlock
233 * time, and not at spin_lock time.
234 *
235 * Increasing the sequence after the spin_unlock would be
236 * unsafe because the kvm page fault could then establish the
237 * pte after kvm_unmap_hva returned, without noticing the page
238 * is going to be freed.
239 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200240 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200241 spin_lock(&kvm->mmu_lock);
242 kvm->mmu_notifier_seq++;
243 need_tlb_flush = kvm_unmap_hva(kvm, address);
244 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200245 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200246
247 /* we've to flush the tlb before the pages can be freed */
248 if (need_tlb_flush)
249 kvm_flush_remote_tlbs(kvm);
250
251}
252
Izik Eidus3da0dd42009-09-23 21:47:18 +0300253static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
254 struct mm_struct *mm,
255 unsigned long address,
256 pte_t pte)
257{
258 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200259 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300260
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200261 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300262 spin_lock(&kvm->mmu_lock);
263 kvm->mmu_notifier_seq++;
264 kvm_set_spte_hva(kvm, address, pte);
265 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200266 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300267}
268
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200269static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
270 struct mm_struct *mm,
271 unsigned long start,
272 unsigned long end)
273{
274 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200275 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200276
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200277 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200278 spin_lock(&kvm->mmu_lock);
279 /*
280 * The count increase must become visible at unlock time as no
281 * spte can be established without taking the mmu_lock and
282 * count is also read inside the mmu_lock critical section.
283 */
284 kvm->mmu_notifier_count++;
285 for (; start < end; start += PAGE_SIZE)
286 need_tlb_flush |= kvm_unmap_hva(kvm, start);
287 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200288 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200289
290 /* we've to flush the tlb before the pages can be freed */
291 if (need_tlb_flush)
292 kvm_flush_remote_tlbs(kvm);
293}
294
295static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
296 struct mm_struct *mm,
297 unsigned long start,
298 unsigned long end)
299{
300 struct kvm *kvm = mmu_notifier_to_kvm(mn);
301
302 spin_lock(&kvm->mmu_lock);
303 /*
304 * This sequence increase will notify the kvm page fault that
305 * the page that is going to be mapped in the spte could have
306 * been freed.
307 */
308 kvm->mmu_notifier_seq++;
309 /*
310 * The above sequence increase must be visible before the
311 * below count decrease but both values are read by the kvm
312 * page fault under mmu_lock spinlock so we don't need to add
313 * a smb_wmb() here in between the two.
314 */
315 kvm->mmu_notifier_count--;
316 spin_unlock(&kvm->mmu_lock);
317
318 BUG_ON(kvm->mmu_notifier_count < 0);
319}
320
321static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
322 struct mm_struct *mm,
323 unsigned long address)
324{
325 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200326 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200327
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200328 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200329 spin_lock(&kvm->mmu_lock);
330 young = kvm_age_hva(kvm, address);
331 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200332 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200333
334 if (young)
335 kvm_flush_remote_tlbs(kvm);
336
337 return young;
338}
339
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100340static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
341 struct mm_struct *mm)
342{
343 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800344 int idx;
345
346 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100347 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800348 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100349}
350
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200351static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
352 .invalidate_page = kvm_mmu_notifier_invalidate_page,
353 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
354 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
355 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300356 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100357 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200358};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200359
360static int kvm_init_mmu_notifier(struct kvm *kvm)
361{
362 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
363 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
364}
365
366#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
367
368static int kvm_init_mmu_notifier(struct kvm *kvm)
369{
370 return 0;
371}
372
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200373#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
374
Avi Kivityf17abe92007-02-21 19:28:04 +0200375static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200377 int r = 0, i;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800378 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800380 if (IS_ERR(kvm))
381 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200382
383 r = hardware_enable_all();
384 if (r)
385 goto out_err_nodisable;
386
Avi Kivity75858a82009-01-04 17:10:50 +0200387#ifdef CONFIG_HAVE_KVM_IRQCHIP
388 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300389 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200390#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200392 r = -ENOMEM;
393 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
394 if (!kvm->memslots)
395 goto out_err;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200396 if (init_srcu_struct(&kvm->srcu))
397 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200398 for (i = 0; i < KVM_NR_BUSES; i++) {
399 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
400 GFP_KERNEL);
401 if (!kvm->buses[i]) {
402 cleanup_srcu_struct(&kvm->srcu);
403 goto out_err;
404 }
405 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200406
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200407 r = kvm_init_mmu_notifier(kvm);
Avi Kivity283d0c62009-12-20 14:25:19 +0200408 if (r) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200409 cleanup_srcu_struct(&kvm->srcu);
Avi Kivity283d0c62009-12-20 14:25:19 +0200410 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200411 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200412
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200413 kvm->mm = current->mm;
414 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500415 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200416 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400417 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800418 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300419 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200420 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300421 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000422 spin_lock(&kvm_lock);
423 list_add(&kvm->vm_list, &vm_list);
424 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800425out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200426 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200427
428out_err:
429 hardware_disable_all();
430out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200431 for (i = 0; i < KVM_NR_BUSES; i++)
432 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200433 kfree(kvm->memslots);
Alexander Graf10474ae2009-09-15 11:37:46 +0200434 kfree(kvm);
435 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200436}
437
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438/*
439 * Free any memory in @free but not in @dont.
440 */
441static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
442 struct kvm_memory_slot *dont)
443{
Joerg Roedelec04b262009-06-19 15:16:23 +0200444 int i;
445
Izik Eidus290fc382007-09-27 14:11:22 +0200446 if (!dont || free->rmap != dont->rmap)
447 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448
449 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
450 vfree(free->dirty_bitmap);
451
Joerg Roedelec04b262009-06-19 15:16:23 +0200452
453 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
454 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
455 vfree(free->lpage_info[i]);
456 free->lpage_info[i] = NULL;
457 }
458 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000461 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500462 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463}
464
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800465void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466{
467 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200468 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200470 for (i = 0; i < slots->nmemslots; ++i)
471 kvm_free_physmem_slot(&slots->memslots[i], NULL);
472
473 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474}
475
Avi Kivityf17abe92007-02-21 19:28:04 +0200476static void kvm_destroy_vm(struct kvm *kvm)
477{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200478 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200479 struct mm_struct *mm = kvm->mm;
480
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800481 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800482 spin_lock(&kvm_lock);
483 list_del(&kvm->vm_list);
484 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200485 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200486 for (i = 0; i < KVM_NR_BUSES; i++)
487 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200488 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200489#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
490 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200491#else
492 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200493#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800494 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200495 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200496 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200497}
498
Izik Eidusd39f13b2008-03-30 16:01:25 +0300499void kvm_get_kvm(struct kvm *kvm)
500{
501 atomic_inc(&kvm->users_count);
502}
503EXPORT_SYMBOL_GPL(kvm_get_kvm);
504
505void kvm_put_kvm(struct kvm *kvm)
506{
507 if (atomic_dec_and_test(&kvm->users_count))
508 kvm_destroy_vm(kvm);
509}
510EXPORT_SYMBOL_GPL(kvm_put_kvm);
511
512
Avi Kivityf17abe92007-02-21 19:28:04 +0200513static int kvm_vm_release(struct inode *inode, struct file *filp)
514{
515 struct kvm *kvm = filp->private_data;
516
Gregory Haskins721eecb2009-05-20 10:30:49 -0400517 kvm_irqfd_release(kvm);
518
Izik Eidusd39f13b2008-03-30 16:01:25 +0300519 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 return 0;
521}
522
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 * Allocate some memory and give it an address in the guest physical address
525 * space.
526 *
527 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800528 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500529 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800531int __kvm_set_memory_region(struct kvm *kvm,
532 struct kvm_userspace_memory_region *mem,
533 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200535 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200537 unsigned long npages;
538 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 struct kvm_memory_slot *memslot;
540 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200541 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542
543 r = -EINVAL;
544 /* General sanity checks */
545 if (mem->memory_size & (PAGE_SIZE - 1))
546 goto out;
547 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
548 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800549 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600550 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200551 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 goto out;
553 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
554 goto out;
555
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200556 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
558 npages = mem->memory_size >> PAGE_SHIFT;
559
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900560 r = -EINVAL;
561 if (npages > KVM_MEM_MAX_NR_PAGES)
562 goto out;
563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 if (!npages)
565 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
566
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 new = old = *memslot;
568
569 new.base_gfn = base_gfn;
570 new.npages = npages;
571 new.flags = mem->flags;
572
573 /* Disallow changing a memory slot's size. */
574 r = -EINVAL;
575 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800576 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577
578 /* Check for overlaps */
579 r = -EEXIST;
580 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200581 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200583 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 continue;
585 if (!((base_gfn + npages <= s->base_gfn) ||
586 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800587 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 /* Free page dirty bitmap if unneeded */
591 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000592 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593
594 r = -ENOMEM;
595
596 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200597#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500598 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400599 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200600
601 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800602 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200603
Izik Eidus290fc382007-09-27 14:11:22 +0200604 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500605
Izik Eidus80b14b52007-10-25 11:54:04 +0200606 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200607 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200609 if (!npages)
610 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300611
Joerg Roedelec04b262009-06-19 15:16:23 +0200612 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200613 unsigned long ugfn;
614 unsigned long j;
615 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200616 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300617
Joerg Roedelec04b262009-06-19 15:16:23 +0200618 /* Avoid unused variable warning if no large pages */
619 (void)level;
620
621 if (new.lpage_info[i])
622 continue;
623
624 lpages = 1 + (base_gfn + npages - 1) /
625 KVM_PAGES_PER_HPAGE(level);
626 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
627
628 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
629
630 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300631 goto out_free;
632
Joerg Roedelec04b262009-06-19 15:16:23 +0200633 memset(new.lpage_info[i], 0,
634 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300635
Joerg Roedelec04b262009-06-19 15:16:23 +0200636 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
637 new.lpage_info[i][0].write_count = 1;
638 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
639 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300640 ugfn = new.userspace_addr >> PAGE_SHIFT;
641 /*
642 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300643 * other, or if explicitly asked to, disable large page
644 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300645 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200646 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300647 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200648 for (j = 0; j < lpages; ++j)
649 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300650 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651
Joerg Roedelec04b262009-06-19 15:16:23 +0200652skip_lpage:
653
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 /* Allocate page dirty bitmap if needed */
655 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900656 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
658 new.dirty_bitmap = vmalloc(dirty_bytes);
659 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800660 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 memset(new.dirty_bitmap, 0, dirty_bytes);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200662 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300663 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200664 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200666#else /* not defined CONFIG_S390 */
667 new.user_alloc = user_alloc;
668 if (user_alloc)
669 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200670#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200672 if (!npages) {
673 r = -ENOMEM;
674 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
675 if (!slots)
676 goto out_free;
677 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
678 if (mem->slot >= slots->nmemslots)
679 slots->nmemslots = mem->slot + 1;
680 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
681
682 old_memslots = kvm->memslots;
683 rcu_assign_pointer(kvm->memslots, slots);
684 synchronize_srcu_expedited(&kvm->srcu);
685 /* From this point no new shadow pages pointing to a deleted
686 * memslot will be created.
687 *
688 * validation of sp->gfn happens in:
689 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
690 * - kvm_is_visible_gfn (mmu_check_roots)
691 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300692 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200693 kfree(old_memslots);
694 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300695
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200696 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
697 if (r)
698 goto out_free;
699
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200700#ifdef CONFIG_DMAR
701 /* map the pages in iommu page table */
702 if (npages) {
703 r = kvm_iommu_map_pages(kvm, &new);
704 if (r)
705 goto out_free;
706 }
707#endif
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200708
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200709 r = -ENOMEM;
710 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
711 if (!slots)
712 goto out_free;
713 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
714 if (mem->slot >= slots->nmemslots)
715 slots->nmemslots = mem->slot + 1;
716
717 /* actual memory is freed via old in kvm_free_physmem_slot below */
718 if (!npages) {
719 new.rmap = NULL;
720 new.dirty_bitmap = NULL;
721 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
722 new.lpage_info[i] = NULL;
723 }
724
725 slots->memslots[mem->slot] = new;
726 old_memslots = kvm->memslots;
727 rcu_assign_pointer(kvm->memslots, slots);
728 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200730 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800731
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200732 kvm_free_physmem_slot(&old, &new);
733 kfree(old_memslots);
734
735 if (flush_shadow)
736 kvm_arch_flush_shadow(kvm);
737
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 return 0;
739
Sheng Yangf78e0e22007-10-29 09:40:42 +0800740out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 kvm_free_physmem_slot(&new, &old);
742out:
743 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200744
745}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800746EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
747
748int kvm_set_memory_region(struct kvm *kvm,
749 struct kvm_userspace_memory_region *mem,
750 int user_alloc)
751{
752 int r;
753
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200754 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800755 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200756 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800757 return r;
758}
Izik Eidus210c7c42007-10-24 23:52:57 +0200759EXPORT_SYMBOL_GPL(kvm_set_memory_region);
760
Carsten Otte1fe779f2007-10-29 16:08:35 +0100761int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
762 struct
763 kvm_userspace_memory_region *mem,
764 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200765{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200766 if (mem->slot >= KVM_MEMORY_SLOTS)
767 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200768 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769}
770
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800771int kvm_get_dirty_log(struct kvm *kvm,
772 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773{
774 struct kvm_memory_slot *memslot;
775 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900776 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 unsigned long any = 0;
778
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 r = -EINVAL;
780 if (log->slot >= KVM_MEMORY_SLOTS)
781 goto out;
782
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200783 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 r = -ENOENT;
785 if (!memslot->dirty_bitmap)
786 goto out;
787
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900788 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789
Uri Lublincd1a4a92007-02-22 16:43:09 +0200790 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 any = memslot->dirty_bitmap[i];
792
793 r = -EFAULT;
794 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
795 goto out;
796
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800797 if (any)
798 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799
800 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 return r;
803}
804
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300805void kvm_disable_largepages(void)
806{
807 largepages_enabled = false;
808}
809EXPORT_SYMBOL_GPL(kvm_disable_largepages);
810
Izik Eiduscea7bb22007-10-17 19:17:48 +0200811int is_error_page(struct page *page)
812{
813 return page == bad_page;
814}
815EXPORT_SYMBOL_GPL(is_error_page);
816
Anthony Liguori35149e22008-04-02 14:46:56 -0500817int is_error_pfn(pfn_t pfn)
818{
819 return pfn == bad_pfn;
820}
821EXPORT_SYMBOL_GPL(is_error_pfn);
822
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200823static inline unsigned long bad_hva(void)
824{
825 return PAGE_OFFSET;
826}
827
828int kvm_is_error_hva(unsigned long addr)
829{
830 return addr == bad_hva();
831}
832EXPORT_SYMBOL_GPL(kvm_is_error_hva);
833
Izik Eidus28430992008-10-03 17:40:32 +0300834struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835{
836 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800837 struct kvm_memslots *slots = kvm_memslots(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200839 for (i = 0; i < slots->nmemslots; ++i) {
840 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
842 if (gfn >= memslot->base_gfn
843 && gfn < memslot->base_gfn + memslot->npages)
844 return memslot;
845 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000846 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847}
Izik Eidus28430992008-10-03 17:40:32 +0300848EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300849
850struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
851{
852 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300853 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300854}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855
Izik Eiduse0d62c72007-10-24 23:57:46 +0200856int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
857{
858 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800859 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200860
Marcelo Tosattia983fb22009-12-23 14:35:23 -0200861 gfn = unalias_gfn_instantiation(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200862 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200863 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200864
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200865 if (memslot->flags & KVM_MEMSLOT_INVALID)
866 continue;
867
Izik Eiduse0d62c72007-10-24 23:57:46 +0200868 if (gfn >= memslot->base_gfn
869 && gfn < memslot->base_gfn + memslot->npages)
870 return 1;
871 }
872 return 0;
873}
874EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
875
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100876unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
877{
878 struct vm_area_struct *vma;
879 unsigned long addr, size;
880
881 size = PAGE_SIZE;
882
883 addr = gfn_to_hva(kvm, gfn);
884 if (kvm_is_error_hva(addr))
885 return PAGE_SIZE;
886
887 down_read(&current->mm->mmap_sem);
888 vma = find_vma(current->mm, addr);
889 if (!vma)
890 goto out;
891
892 size = vma_kernel_pagesize(vma);
893
894out:
895 up_read(&current->mm->mmap_sem);
896
897 return size;
898}
899
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200900int memslot_id(struct kvm *kvm, gfn_t gfn)
901{
902 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800903 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200904 struct kvm_memory_slot *memslot = NULL;
905
906 gfn = unalias_gfn(kvm, gfn);
907 for (i = 0; i < slots->nmemslots; ++i) {
908 memslot = &slots->memslots[i];
909
910 if (gfn >= memslot->base_gfn
911 && gfn < memslot->base_gfn + memslot->npages)
912 break;
913 }
914
915 return memslot - slots->memslots;
916}
917
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900918static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
919{
920 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
921}
922
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300923unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200924{
925 struct kvm_memory_slot *slot;
926
Marcelo Tosattia983fb22009-12-23 14:35:23 -0200927 gfn = unalias_gfn_instantiation(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300928 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200929 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200930 return bad_hva();
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900931 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200932}
Sheng Yang0d150292008-04-25 21:44:50 +0800933EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200934
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200935static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300936{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500937 struct page *page[1];
938 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500939 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300940
Avi Kivity60395222007-10-21 11:03:36 +0200941 might_sleep();
942
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300943 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200944
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500945 if (unlikely(npages != 1)) {
946 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500947
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300948 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500949 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300950
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500951 if (vma == NULL || addr < vma->vm_start ||
952 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300953 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500954 get_page(bad_page);
955 return page_to_pfn(bad_page);
956 }
957
958 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300959 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800960 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500961 } else
962 pfn = page_to_pfn(page[0]);
963
964 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500965}
966
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200967pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
968{
969 unsigned long addr;
970
971 addr = gfn_to_hva(kvm, gfn);
972 if (kvm_is_error_hva(addr)) {
973 get_page(bad_page);
974 return page_to_pfn(bad_page);
975 }
976
977 return hva_to_pfn(kvm, addr);
978}
Anthony Liguori35149e22008-04-02 14:46:56 -0500979EXPORT_SYMBOL_GPL(gfn_to_pfn);
980
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200981pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
982 struct kvm_memory_slot *slot, gfn_t gfn)
983{
984 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
985 return hva_to_pfn(kvm, addr);
986}
987
Anthony Liguori35149e22008-04-02 14:46:56 -0500988struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
989{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500990 pfn_t pfn;
991
992 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800993 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500994 return pfn_to_page(pfn);
995
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800996 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500997
998 get_page(bad_page);
999 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001000}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001001
Avi Kivity954bbbc22007-03-30 14:02:32 +03001002EXPORT_SYMBOL_GPL(gfn_to_page);
1003
Izik Eidusb4231d62007-11-20 11:49:33 +02001004void kvm_release_page_clean(struct page *page)
1005{
Anthony Liguori35149e22008-04-02 14:46:56 -05001006 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001007}
1008EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1009
Anthony Liguori35149e22008-04-02 14:46:56 -05001010void kvm_release_pfn_clean(pfn_t pfn)
1011{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001012 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001013 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001014}
1015EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1016
Izik Eidusb4231d62007-11-20 11:49:33 +02001017void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001018{
Anthony Liguori35149e22008-04-02 14:46:56 -05001019 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001020}
Izik Eidusb4231d62007-11-20 11:49:33 +02001021EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001022
Anthony Liguori35149e22008-04-02 14:46:56 -05001023void kvm_release_pfn_dirty(pfn_t pfn)
1024{
1025 kvm_set_pfn_dirty(pfn);
1026 kvm_release_pfn_clean(pfn);
1027}
1028EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1029
1030void kvm_set_page_dirty(struct page *page)
1031{
1032 kvm_set_pfn_dirty(page_to_pfn(page));
1033}
1034EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1035
1036void kvm_set_pfn_dirty(pfn_t pfn)
1037{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001038 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001039 struct page *page = pfn_to_page(pfn);
1040 if (!PageReserved(page))
1041 SetPageDirty(page);
1042 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001043}
1044EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1045
1046void kvm_set_pfn_accessed(pfn_t pfn)
1047{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001048 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001049 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001050}
1051EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1052
1053void kvm_get_pfn(pfn_t pfn)
1054{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001055 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001056 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001057}
1058EXPORT_SYMBOL_GPL(kvm_get_pfn);
1059
Izik Eidus195aefd2007-10-01 22:14:18 +02001060static int next_segment(unsigned long len, int offset)
1061{
1062 if (len > PAGE_SIZE - offset)
1063 return PAGE_SIZE - offset;
1064 else
1065 return len;
1066}
1067
1068int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1069 int len)
1070{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001071 int r;
1072 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001073
Izik Eiduse0506bc2007-11-11 22:10:22 +02001074 addr = gfn_to_hva(kvm, gfn);
1075 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001076 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001077 r = copy_from_user(data, (void __user *)addr + offset, len);
1078 if (r)
1079 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001080 return 0;
1081}
1082EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1083
1084int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1085{
1086 gfn_t gfn = gpa >> PAGE_SHIFT;
1087 int seg;
1088 int offset = offset_in_page(gpa);
1089 int ret;
1090
1091 while ((seg = next_segment(len, offset)) != 0) {
1092 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1093 if (ret < 0)
1094 return ret;
1095 offset = 0;
1096 len -= seg;
1097 data += seg;
1098 ++gfn;
1099 }
1100 return 0;
1101}
1102EXPORT_SYMBOL_GPL(kvm_read_guest);
1103
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001104int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1105 unsigned long len)
1106{
1107 int r;
1108 unsigned long addr;
1109 gfn_t gfn = gpa >> PAGE_SHIFT;
1110 int offset = offset_in_page(gpa);
1111
1112 addr = gfn_to_hva(kvm, gfn);
1113 if (kvm_is_error_hva(addr))
1114 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001115 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001116 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001117 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001118 if (r)
1119 return -EFAULT;
1120 return 0;
1121}
1122EXPORT_SYMBOL(kvm_read_guest_atomic);
1123
Izik Eidus195aefd2007-10-01 22:14:18 +02001124int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1125 int offset, int len)
1126{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001127 int r;
1128 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001129
Izik Eiduse0506bc2007-11-11 22:10:22 +02001130 addr = gfn_to_hva(kvm, gfn);
1131 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001132 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001133 r = copy_to_user((void __user *)addr + offset, data, len);
1134 if (r)
1135 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001136 mark_page_dirty(kvm, gfn);
1137 return 0;
1138}
1139EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1140
1141int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1142 unsigned long len)
1143{
1144 gfn_t gfn = gpa >> PAGE_SHIFT;
1145 int seg;
1146 int offset = offset_in_page(gpa);
1147 int ret;
1148
1149 while ((seg = next_segment(len, offset)) != 0) {
1150 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1151 if (ret < 0)
1152 return ret;
1153 offset = 0;
1154 len -= seg;
1155 data += seg;
1156 ++gfn;
1157 }
1158 return 0;
1159}
1160
1161int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1162{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001163 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001164}
1165EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1166
1167int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1168{
1169 gfn_t gfn = gpa >> PAGE_SHIFT;
1170 int seg;
1171 int offset = offset_in_page(gpa);
1172 int ret;
1173
1174 while ((seg = next_segment(len, offset)) != 0) {
1175 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1176 if (ret < 0)
1177 return ret;
1178 offset = 0;
1179 len -= seg;
1180 ++gfn;
1181 }
1182 return 0;
1183}
1184EXPORT_SYMBOL_GPL(kvm_clear_guest);
1185
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1187{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001188 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
Uri Lublin3b6fff12007-10-30 10:42:09 +02001190 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001191 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001192 if (memslot && memslot->dirty_bitmap) {
1193 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001195 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 }
1197}
1198
Eddie Dongb6958ce2007-07-18 12:15:21 +03001199/*
1200 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1201 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001202void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001203{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001204 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001205
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001206 for (;;) {
1207 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001208
Gleb Natapova1b37102009-07-09 15:33:52 +03001209 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001210 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001211 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001212 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001213 if (kvm_cpu_has_pending_timer(vcpu))
1214 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001215 if (signal_pending(current))
1216 break;
1217
Eddie Dongb6958ce2007-07-18 12:15:21 +03001218 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001219 }
1220
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001221 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001222}
1223
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224void kvm_resched(struct kvm_vcpu *vcpu)
1225{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001226 if (!need_resched())
1227 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230EXPORT_SYMBOL_GPL(kvm_resched);
1231
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001232void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1233{
1234 ktime_t expires;
1235 DEFINE_WAIT(wait);
1236
1237 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1238
1239 /* Sleep for 100 us, and hope lock-holder got scheduled */
1240 expires = ktime_add_ns(ktime_get(), 100000UL);
1241 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1242
1243 finish_wait(&vcpu->wq, &wait);
1244}
1245EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1246
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001247static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001248{
1249 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001250 struct page *page;
1251
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001252 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001253 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001254#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001255 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001256 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001257#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001258#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1259 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1260 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1261#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001262 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001263 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001264 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001265 vmf->page = page;
1266 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001267}
1268
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001269static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001270 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001271};
1272
1273static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1274{
1275 vma->vm_ops = &kvm_vcpu_vm_ops;
1276 return 0;
1277}
1278
Avi Kivitybccf2152007-02-21 18:04:26 +02001279static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1280{
1281 struct kvm_vcpu *vcpu = filp->private_data;
1282
Al Viro66c0b392008-04-19 20:33:56 +01001283 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001284 return 0;
1285}
1286
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001287static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001288 .release = kvm_vcpu_release,
1289 .unlocked_ioctl = kvm_vcpu_ioctl,
1290 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001291 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001292};
1293
1294/*
1295 * Allocates an inode for the vcpu.
1296 */
1297static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1298{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001299 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001300}
1301
Avi Kivityc5ea7662007-02-20 18:41:05 +02001302/*
1303 * Creates some virtual cpus. Good luck creating more than one.
1304 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001305static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001306{
1307 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001308 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001309
Gleb Natapov73880c82009-06-09 15:56:28 +03001310 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001311 if (IS_ERR(vcpu))
1312 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001313
Avi Kivity15ad7142007-07-11 18:17:21 +03001314 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1315
Avi Kivity26e52152007-11-20 15:30:24 +02001316 r = kvm_arch_vcpu_setup(vcpu);
1317 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001318 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001319
Shaohua Li11ec2802007-07-23 14:51:37 +08001320 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001321 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1322 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001323 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001324 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001325
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001326 kvm_for_each_vcpu(r, v, kvm)
1327 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001328 r = -EEXIST;
1329 goto vcpu_destroy;
1330 }
1331
1332 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001333
1334 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001335 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001336 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001337 if (r < 0) {
1338 kvm_put_kvm(kvm);
1339 goto vcpu_destroy;
1340 }
1341
1342 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1343 smp_wmb();
1344 atomic_inc(&kvm->online_vcpus);
1345
1346#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1347 if (kvm->bsp_vcpu_id == id)
1348 kvm->bsp_vcpu = vcpu;
1349#endif
1350 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001351 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001352
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001353vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001354 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001355 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001356 return r;
1357}
1358
Avi Kivity1961d272007-03-05 19:46:05 +02001359static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1360{
1361 if (sigset) {
1362 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1363 vcpu->sigset_active = 1;
1364 vcpu->sigset = *sigset;
1365 } else
1366 vcpu->sigset_active = 0;
1367 return 0;
1368}
1369
Avi Kivitybccf2152007-02-21 18:04:26 +02001370static long kvm_vcpu_ioctl(struct file *filp,
1371 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372{
Avi Kivitybccf2152007-02-21 18:04:26 +02001373 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001374 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001375 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001376 struct kvm_fpu *fpu = NULL;
1377 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001379 if (vcpu->kvm->mm != current->mm)
1380 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001382 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001383 r = -EINVAL;
1384 if (arg)
1385 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001386 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001389 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001391 r = -ENOMEM;
1392 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1393 if (!kvm_regs)
1394 goto out;
1395 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001397 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001399 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1400 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001402out_free1:
1403 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 break;
1405 }
1406 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001407 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001409 r = -ENOMEM;
1410 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1411 if (!kvm_regs)
1412 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001414 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1415 goto out_free2;
1416 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001418 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001420out_free2:
1421 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 break;
1423 }
1424 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001425 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1426 r = -ENOMEM;
1427 if (!kvm_sregs)
1428 goto out;
1429 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430 if (r)
1431 goto out;
1432 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001433 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434 goto out;
1435 r = 0;
1436 break;
1437 }
1438 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001439 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1440 r = -ENOMEM;
1441 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001443 r = -EFAULT;
1444 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1445 goto out;
1446 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 if (r)
1448 goto out;
1449 r = 0;
1450 break;
1451 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001452 case KVM_GET_MP_STATE: {
1453 struct kvm_mp_state mp_state;
1454
1455 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1456 if (r)
1457 goto out;
1458 r = -EFAULT;
1459 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1460 goto out;
1461 r = 0;
1462 break;
1463 }
1464 case KVM_SET_MP_STATE: {
1465 struct kvm_mp_state mp_state;
1466
1467 r = -EFAULT;
1468 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1469 goto out;
1470 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1471 if (r)
1472 goto out;
1473 r = 0;
1474 break;
1475 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 case KVM_TRANSLATE: {
1477 struct kvm_translation tr;
1478
1479 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001480 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001482 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 if (r)
1484 goto out;
1485 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001486 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 goto out;
1488 r = 0;
1489 break;
1490 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001491 case KVM_SET_GUEST_DEBUG: {
1492 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493
1494 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001495 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001497 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 if (r)
1499 goto out;
1500 r = 0;
1501 break;
1502 }
Avi Kivity1961d272007-03-05 19:46:05 +02001503 case KVM_SET_SIGNAL_MASK: {
1504 struct kvm_signal_mask __user *sigmask_arg = argp;
1505 struct kvm_signal_mask kvm_sigmask;
1506 sigset_t sigset, *p;
1507
1508 p = NULL;
1509 if (argp) {
1510 r = -EFAULT;
1511 if (copy_from_user(&kvm_sigmask, argp,
1512 sizeof kvm_sigmask))
1513 goto out;
1514 r = -EINVAL;
1515 if (kvm_sigmask.len != sizeof sigset)
1516 goto out;
1517 r = -EFAULT;
1518 if (copy_from_user(&sigset, sigmask_arg->sigset,
1519 sizeof sigset))
1520 goto out;
1521 p = &sigset;
1522 }
1523 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1524 break;
1525 }
Avi Kivityb8836732007-04-01 16:34:31 +03001526 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001527 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1528 r = -ENOMEM;
1529 if (!fpu)
1530 goto out;
1531 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001532 if (r)
1533 goto out;
1534 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001535 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001536 goto out;
1537 r = 0;
1538 break;
1539 }
1540 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001541 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1542 r = -ENOMEM;
1543 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001544 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001545 r = -EFAULT;
1546 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1547 goto out;
1548 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001549 if (r)
1550 goto out;
1551 r = 0;
1552 break;
1553 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001554 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001555 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001556 }
1557out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001558 kfree(fpu);
1559 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001560 return r;
1561}
1562
1563static long kvm_vm_ioctl(struct file *filp,
1564 unsigned int ioctl, unsigned long arg)
1565{
1566 struct kvm *kvm = filp->private_data;
1567 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001568 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001569
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001570 if (kvm->mm != current->mm)
1571 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001572 switch (ioctl) {
1573 case KVM_CREATE_VCPU:
1574 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1575 if (r < 0)
1576 goto out;
1577 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001578 case KVM_SET_USER_MEMORY_REGION: {
1579 struct kvm_userspace_memory_region kvm_userspace_mem;
1580
1581 r = -EFAULT;
1582 if (copy_from_user(&kvm_userspace_mem, argp,
1583 sizeof kvm_userspace_mem))
1584 goto out;
1585
1586 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587 if (r)
1588 goto out;
1589 break;
1590 }
1591 case KVM_GET_DIRTY_LOG: {
1592 struct kvm_dirty_log log;
1593
1594 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001595 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001597 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598 if (r)
1599 goto out;
1600 break;
1601 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001602#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1603 case KVM_REGISTER_COALESCED_MMIO: {
1604 struct kvm_coalesced_mmio_zone zone;
1605 r = -EFAULT;
1606 if (copy_from_user(&zone, argp, sizeof zone))
1607 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001608 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1609 if (r)
1610 goto out;
1611 r = 0;
1612 break;
1613 }
1614 case KVM_UNREGISTER_COALESCED_MMIO: {
1615 struct kvm_coalesced_mmio_zone zone;
1616 r = -EFAULT;
1617 if (copy_from_user(&zone, argp, sizeof zone))
1618 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001619 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1620 if (r)
1621 goto out;
1622 r = 0;
1623 break;
1624 }
1625#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001626 case KVM_IRQFD: {
1627 struct kvm_irqfd data;
1628
1629 r = -EFAULT;
1630 if (copy_from_user(&data, argp, sizeof data))
1631 goto out;
1632 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1633 break;
1634 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001635 case KVM_IOEVENTFD: {
1636 struct kvm_ioeventfd data;
1637
1638 r = -EFAULT;
1639 if (copy_from_user(&data, argp, sizeof data))
1640 goto out;
1641 r = kvm_ioeventfd(kvm, &data);
1642 break;
1643 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001644#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1645 case KVM_SET_BOOT_CPU_ID:
1646 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001647 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001648 if (atomic_read(&kvm->online_vcpus) != 0)
1649 r = -EBUSY;
1650 else
1651 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001652 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001653 break;
1654#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001655 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001656 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001657 if (r == -ENOTTY)
1658 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001659 }
1660out:
1661 return r;
1662}
1663
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001664#ifdef CONFIG_COMPAT
1665struct compat_kvm_dirty_log {
1666 __u32 slot;
1667 __u32 padding1;
1668 union {
1669 compat_uptr_t dirty_bitmap; /* one bit per page */
1670 __u64 padding2;
1671 };
1672};
1673
1674static long kvm_vm_compat_ioctl(struct file *filp,
1675 unsigned int ioctl, unsigned long arg)
1676{
1677 struct kvm *kvm = filp->private_data;
1678 int r;
1679
1680 if (kvm->mm != current->mm)
1681 return -EIO;
1682 switch (ioctl) {
1683 case KVM_GET_DIRTY_LOG: {
1684 struct compat_kvm_dirty_log compat_log;
1685 struct kvm_dirty_log log;
1686
1687 r = -EFAULT;
1688 if (copy_from_user(&compat_log, (void __user *)arg,
1689 sizeof(compat_log)))
1690 goto out;
1691 log.slot = compat_log.slot;
1692 log.padding1 = compat_log.padding1;
1693 log.padding2 = compat_log.padding2;
1694 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1695
1696 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1697 if (r)
1698 goto out;
1699 break;
1700 }
1701 default:
1702 r = kvm_vm_ioctl(filp, ioctl, arg);
1703 }
1704
1705out:
1706 return r;
1707}
1708#endif
1709
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001710static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001711{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001712 struct page *page[1];
1713 unsigned long addr;
1714 int npages;
1715 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001716 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001717
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001718 addr = gfn_to_hva(kvm, gfn);
1719 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001720 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001721
1722 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1723 NULL);
1724 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001725 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001726
1727 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001728 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001729}
1730
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001731static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001732 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001733};
1734
1735static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1736{
1737 vma->vm_ops = &kvm_vm_vm_ops;
1738 return 0;
1739}
1740
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001741static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001742 .release = kvm_vm_release,
1743 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001744#ifdef CONFIG_COMPAT
1745 .compat_ioctl = kvm_vm_compat_ioctl,
1746#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001747 .mmap = kvm_vm_mmap,
1748};
1749
1750static int kvm_dev_ioctl_create_vm(void)
1751{
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001752 int fd, r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001753 struct kvm *kvm;
1754
Avi Kivityf17abe92007-02-21 19:28:04 +02001755 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001756 if (IS_ERR(kvm))
1757 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001758#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1759 r = kvm_coalesced_mmio_init(kvm);
1760 if (r < 0) {
1761 kvm_put_kvm(kvm);
1762 return r;
1763 }
1764#endif
Roland Dreier628ff7c2009-12-18 09:41:24 -08001765 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
Al Viro2030a422008-02-23 06:46:49 -05001766 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001767 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001768
Avi Kivityf17abe92007-02-21 19:28:04 +02001769 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001770}
1771
Avi Kivity1a811b62008-12-08 18:25:27 +02001772static long kvm_dev_ioctl_check_extension_generic(long arg)
1773{
1774 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001775 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001776 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001777 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001778#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1779 case KVM_CAP_SET_BOOT_CPU_ID:
1780#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001781 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001782 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001783#ifdef CONFIG_HAVE_KVM_IRQCHIP
1784 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001785 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001786#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001787 default:
1788 break;
1789 }
1790 return kvm_dev_ioctl_check_extension(arg);
1791}
1792
Avi Kivityf17abe92007-02-21 19:28:04 +02001793static long kvm_dev_ioctl(struct file *filp,
1794 unsigned int ioctl, unsigned long arg)
1795{
Avi Kivity07c45a32007-03-07 13:05:38 +02001796 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001797
1798 switch (ioctl) {
1799 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001800 r = -EINVAL;
1801 if (arg)
1802 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001803 r = KVM_API_VERSION;
1804 break;
1805 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001806 r = -EINVAL;
1807 if (arg)
1808 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001809 r = kvm_dev_ioctl_create_vm();
1810 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001811 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001812 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001813 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001814 case KVM_GET_VCPU_MMAP_SIZE:
1815 r = -EINVAL;
1816 if (arg)
1817 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001818 r = PAGE_SIZE; /* struct kvm_run */
1819#ifdef CONFIG_X86
1820 r += PAGE_SIZE; /* pio data page */
1821#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001822#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1823 r += PAGE_SIZE; /* coalesced mmio ring page */
1824#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001825 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001826 case KVM_TRACE_ENABLE:
1827 case KVM_TRACE_PAUSE:
1828 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001829 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001830 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001832 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833 }
1834out:
1835 return r;
1836}
1837
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 .unlocked_ioctl = kvm_dev_ioctl,
1840 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841};
1842
1843static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001844 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 "kvm",
1846 &kvm_chardev_ops,
1847};
1848
Avi Kivity1b6c0162007-05-24 13:03:52 +03001849static void hardware_enable(void *junk)
1850{
1851 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02001852 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03001853
Rusty Russell7f59f492008-12-07 21:25:45 +10301854 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001855 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02001856
Rusty Russell7f59f492008-12-07 21:25:45 +10301857 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02001858
1859 r = kvm_arch_hardware_enable(NULL);
1860
1861 if (r) {
1862 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1863 atomic_inc(&hardware_enable_failed);
1864 printk(KERN_INFO "kvm: enabling virtualization on "
1865 "CPU%d failed\n", cpu);
1866 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03001867}
1868
1869static void hardware_disable(void *junk)
1870{
1871 int cpu = raw_smp_processor_id();
1872
Rusty Russell7f59f492008-12-07 21:25:45 +10301873 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001874 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301875 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001876 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001877}
1878
Alexander Graf10474ae2009-09-15 11:37:46 +02001879static void hardware_disable_all_nolock(void)
1880{
1881 BUG_ON(!kvm_usage_count);
1882
1883 kvm_usage_count--;
1884 if (!kvm_usage_count)
1885 on_each_cpu(hardware_disable, NULL, 1);
1886}
1887
1888static void hardware_disable_all(void)
1889{
1890 spin_lock(&kvm_lock);
1891 hardware_disable_all_nolock();
1892 spin_unlock(&kvm_lock);
1893}
1894
1895static int hardware_enable_all(void)
1896{
1897 int r = 0;
1898
1899 spin_lock(&kvm_lock);
1900
1901 kvm_usage_count++;
1902 if (kvm_usage_count == 1) {
1903 atomic_set(&hardware_enable_failed, 0);
1904 on_each_cpu(hardware_enable, NULL, 1);
1905
1906 if (atomic_read(&hardware_enable_failed)) {
1907 hardware_disable_all_nolock();
1908 r = -EBUSY;
1909 }
1910 }
1911
1912 spin_unlock(&kvm_lock);
1913
1914 return r;
1915}
1916
Avi Kivity774c47f2007-02-12 00:54:47 -08001917static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1918 void *v)
1919{
1920 int cpu = (long)v;
1921
Alexander Graf10474ae2009-09-15 11:37:46 +02001922 if (!kvm_usage_count)
1923 return NOTIFY_OK;
1924
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001925 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001926 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001927 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001928 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1929 cpu);
1930 hardware_disable(NULL);
1931 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001932 case CPU_ONLINE:
1933 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1934 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001935 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001936 break;
1937 }
1938 return NOTIFY_OK;
1939}
1940
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001941
1942asmlinkage void kvm_handle_fault_on_reboot(void)
1943{
1944 if (kvm_rebooting)
1945 /* spin while reset goes on */
1946 while (true)
1947 ;
1948 /* Fault while not rebooting. We want the trace. */
1949 BUG();
1950}
1951EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1952
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001953static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001954 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001955{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001956 /*
1957 * Some (well, at least mine) BIOSes hang on reboot if
1958 * in vmx root mode.
1959 *
1960 * And Intel TXT required VMX off for all cpu when system shutdown.
1961 */
1962 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1963 kvm_rebooting = true;
1964 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001965 return NOTIFY_OK;
1966}
1967
1968static struct notifier_block kvm_reboot_notifier = {
1969 .notifier_call = kvm_reboot,
1970 .priority = 0,
1971};
1972
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001973static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001974{
1975 int i;
1976
1977 for (i = 0; i < bus->dev_count; i++) {
1978 struct kvm_io_device *pos = bus->devs[i];
1979
1980 kvm_iodevice_destructor(pos);
1981 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001982 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001983}
1984
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001985/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001986int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001987 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001988{
1989 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001990 struct kvm_io_bus *bus;
1991
1992 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001993 for (i = 0; i < bus->dev_count; i++)
1994 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1995 return 0;
1996 return -EOPNOTSUPP;
1997}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001998
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001999/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002000int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2001 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002002{
2003 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002004 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002005
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002006 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002007 for (i = 0; i < bus->dev_count; i++)
2008 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2009 return 0;
2010 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002011}
2012
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002013/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002014int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2015 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002016{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002017 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002018
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002019 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002020 if (bus->dev_count > NR_IOBUS_DEVS-1)
2021 return -ENOSPC;
2022
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002023 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2024 if (!new_bus)
2025 return -ENOMEM;
2026 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2027 new_bus->devs[new_bus->dev_count++] = dev;
2028 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2029 synchronize_srcu_expedited(&kvm->srcu);
2030 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002031
2032 return 0;
2033}
2034
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002035/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002036int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2037 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002038{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002039 int i, r;
2040 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002041
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002042 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2043 if (!new_bus)
2044 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002045
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002046 bus = kvm->buses[bus_idx];
2047 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2048
2049 r = -ENOENT;
2050 for (i = 0; i < new_bus->dev_count; i++)
2051 if (new_bus->devs[i] == dev) {
2052 r = 0;
2053 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002054 break;
2055 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002056
2057 if (r) {
2058 kfree(new_bus);
2059 return r;
2060 }
2061
2062 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2063 synchronize_srcu_expedited(&kvm->srcu);
2064 kfree(bus);
2065 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002066}
2067
Avi Kivity774c47f2007-02-12 00:54:47 -08002068static struct notifier_block kvm_cpu_notifier = {
2069 .notifier_call = kvm_cpu_hotplug,
2070 .priority = 20, /* must be > scheduler priority */
2071};
2072
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002073static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002074{
2075 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002076 struct kvm *kvm;
2077
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002078 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002079 spin_lock(&kvm_lock);
2080 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002081 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002082 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002083 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002084}
2085
2086DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2087
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002088static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002089{
2090 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002091 struct kvm *kvm;
2092 struct kvm_vcpu *vcpu;
2093 int i;
2094
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002095 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002096 spin_lock(&kvm_lock);
2097 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002098 kvm_for_each_vcpu(i, vcpu, kvm)
2099 *val += *(u32 *)((void *)vcpu + offset);
2100
Avi Kivity1165f5f2007-04-19 17:27:43 +03002101 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002102 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002103}
2104
Avi Kivityba1389b2007-11-18 16:24:12 +02002105DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2106
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002107static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002108 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2109 [KVM_STAT_VM] = &vm_stat_fops,
2110};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002111
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002112static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113{
2114 struct kvm_stats_debugfs_item *p;
2115
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002116 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002118 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002119 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002120 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121}
2122
2123static void kvm_exit_debug(void)
2124{
2125 struct kvm_stats_debugfs_item *p;
2126
2127 for (p = debugfs_entries; p->name; ++p)
2128 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002129 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130}
2131
Avi Kivity59ae6c62007-02-12 00:54:48 -08002132static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2133{
Alexander Graf10474ae2009-09-15 11:37:46 +02002134 if (kvm_usage_count)
2135 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002136 return 0;
2137}
2138
2139static int kvm_resume(struct sys_device *dev)
2140{
Alexander Graf10474ae2009-09-15 11:37:46 +02002141 if (kvm_usage_count)
2142 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002143 return 0;
2144}
2145
2146static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002147 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002148 .suspend = kvm_suspend,
2149 .resume = kvm_resume,
2150};
2151
2152static struct sys_device kvm_sysdev = {
2153 .id = 0,
2154 .cls = &kvm_sysdev_class,
2155};
2156
Izik Eiduscea7bb22007-10-17 19:17:48 +02002157struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002158pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159
Avi Kivity15ad7142007-07-11 18:17:21 +03002160static inline
2161struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2162{
2163 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2164}
2165
2166static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2167{
2168 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2169
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002170 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002171}
2172
2173static void kvm_sched_out(struct preempt_notifier *pn,
2174 struct task_struct *next)
2175{
2176 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2177
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002178 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002179}
2180
Avi Kivity0ee75be2010-04-28 15:39:01 +03002181int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002182 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183{
2184 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002185 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002187 r = kvm_arch_init(opaque);
2188 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002189 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002190
2191 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2192
2193 if (bad_page == NULL) {
2194 r = -ENOMEM;
2195 goto out;
2196 }
2197
Anthony Liguori35149e22008-04-02 14:46:56 -05002198 bad_pfn = page_to_pfn(bad_page);
2199
Avi Kivity8437a612009-06-06 14:52:35 -07002200 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302201 r = -ENOMEM;
2202 goto out_free_0;
2203 }
2204
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002205 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302207 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208
Yang, Sheng002c7f72007-07-31 14:23:01 +03002209 for_each_online_cpu(cpu) {
2210 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002211 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002212 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002213 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002214 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002215 }
2216
Avi Kivity774c47f2007-02-12 00:54:47 -08002217 r = register_cpu_notifier(&kvm_cpu_notifier);
2218 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002219 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 register_reboot_notifier(&kvm_reboot_notifier);
2221
Avi Kivity59ae6c62007-02-12 00:54:48 -08002222 r = sysdev_class_register(&kvm_sysdev_class);
2223 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002224 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002225
2226 r = sysdev_register(&kvm_sysdev);
2227 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002228 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002229
Rusty Russellc16f8622007-07-30 21:12:19 +10002230 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002231 if (!vcpu_align)
2232 vcpu_align = __alignof__(struct kvm_vcpu);
2233 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002234 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002235 if (!kvm_vcpu_cache) {
2236 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002237 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002238 }
2239
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002241 kvm_vm_fops.owner = module;
2242 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243
2244 r = misc_register(&kvm_dev);
2245 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002246 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 goto out_free;
2248 }
2249
Avi Kivity15ad7142007-07-11 18:17:21 +03002250 kvm_preempt_ops.sched_in = kvm_sched_in;
2251 kvm_preempt_ops.sched_out = kvm_sched_out;
2252
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002253 kvm_init_debug();
2254
Avi Kivityc7addb92007-09-16 18:58:32 +02002255 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256
2257out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002258 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002259out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002260 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002261out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002262 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002263out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002265 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002266out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002267out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002268 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302269out_free_0a:
2270 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002271out_free_0:
2272 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002273out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002274 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002275out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 return r;
2277}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002278EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002280void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002282 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002284 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002285 sysdev_unregister(&kvm_sysdev);
2286 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002288 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002289 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002290 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002291 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302292 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002293 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002295EXPORT_SYMBOL_GPL(kvm_exit);