blob: 659bc12ad16ac1a9a06235bdd5a12bc238ae416f [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020046#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020047#include <linux/srcu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
Avi Kivitye4956062007-06-28 14:15:57 -040049#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040050#include <asm/io.h>
51#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020052#include <asm/pgtable.h>
Alexander Grafc8240bd2009-10-30 05:47:26 +000053#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Laurent Vivier5f94c172008-05-30 16:05:54 +020055#include "coalesced_mmio.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#define CREATE_TRACE_POINTS
58#include <trace/events/kvm.h>
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
Marcelo Tosattifa40a822009-06-04 15:08:24 -030063/*
64 * Ordering of locks:
65 *
Sheng Yangfae3a352009-12-15 10:28:07 +080066 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030067 */
68
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080069DEFINE_SPINLOCK(kvm_lock);
70LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080071
Rusty Russell7f59f492008-12-07 21:25:45 +103072static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020073static int kvm_usage_count = 0;
74static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030075
Rusty Russellc16f8622007-07-30 21:12:19 +100076struct kmem_cache *kvm_vcpu_cache;
77EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030078
Avi Kivity15ad7142007-07-11 18:17:21 +030079static __read_mostly struct preempt_ops kvm_preempt_ops;
80
Hollis Blanchard76f7c872008-04-15 16:05:42 -050081struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivitybccf2152007-02-21 18:04:26 +020083static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
84 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020085static int hardware_enable_all(void);
86static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020087
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020088static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
89
Hannes Edere8ba5d32008-11-28 17:02:42 +010090static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030091
Marcelo Tosatti54dee992009-06-11 12:07:44 -030092static bool largepages_enabled = true;
93
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +080094inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +030095{
Joerg Roedelfc5659c2009-02-18 14:08:58 +010096 if (pfn_valid(pfn)) {
97 struct page *page = compound_head(pfn_to_page(pfn));
98 return PageReserved(page);
99 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300100
101 return true;
102}
103
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104/*
105 * Switches to specified vcpu, until a matching vcpu_put()
106 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200107void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108{
Avi Kivity15ad7142007-07-11 18:17:21 +0300109 int cpu;
110
Avi Kivitybccf2152007-02-21 18:04:26 +0200111 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300112 cpu = get_cpu();
113 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200114 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300115 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200116}
117
Carsten Otte313a3dc2007-10-11 19:16:52 +0200118void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119{
Avi Kivity15ad7142007-07-11 18:17:21 +0300120 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200121 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300122 preempt_notifier_unregister(&vcpu->preempt_notifier);
123 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 mutex_unlock(&vcpu->mutex);
125}
126
Avi Kivityd9e368d2007-06-07 19:18:30 +0300127static void ack_flush(void *_completed)
128{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300129}
130
Rusty Russell49846892008-12-08 20:26:24 +1030131static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300132{
Avi Kivity597a5f52008-07-20 14:24:22 +0300133 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030134 cpumask_var_t cpus;
135 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300136 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300137
Li Zefan79f55992009-06-15 14:58:26 +0800138 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030139
Marcelo Tosatti84261922009-06-17 10:53:47 -0300140 spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200141 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300142 kvm_for_each_vcpu(i, vcpu, kvm) {
Rusty Russell49846892008-12-08 20:26:24 +1030143 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300144 continue;
145 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030146 if (cpus != NULL && cpu != -1 && cpu != me)
147 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300148 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030149 if (unlikely(cpus == NULL))
150 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
151 else if (!cpumask_empty(cpus))
152 smp_call_function_many(cpus, ack_flush, NULL, 1);
153 else
154 called = false;
Marcelo Tosatti84261922009-06-17 10:53:47 -0300155 spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030156 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030157 return called;
158}
159
160void kvm_flush_remote_tlbs(struct kvm *kvm)
161{
162 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
163 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300164}
165
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500166void kvm_reload_remote_mmus(struct kvm *kvm)
167{
Rusty Russell49846892008-12-08 20:26:24 +1030168 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500169}
170
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000171int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
172{
173 struct page *page;
174 int r;
175
176 mutex_init(&vcpu->mutex);
177 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000178 vcpu->kvm = kvm;
179 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300180 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000181
182 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
183 if (!page) {
184 r = -ENOMEM;
185 goto fail;
186 }
187 vcpu->run = page_address(page);
188
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800189 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000190 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800191 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000192 return 0;
193
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000194fail_free_run:
195 free_page((unsigned long)vcpu->run);
196fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000197 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000198}
199EXPORT_SYMBOL_GPL(kvm_vcpu_init);
200
201void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
202{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800203 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000204 free_page((unsigned long)vcpu->run);
205}
206EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
207
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200208#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
209static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
210{
211 return container_of(mn, struct kvm, mmu_notifier);
212}
213
214static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
215 struct mm_struct *mm,
216 unsigned long address)
217{
218 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200219 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200220
221 /*
222 * When ->invalidate_page runs, the linux pte has been zapped
223 * already but the page is still allocated until
224 * ->invalidate_page returns. So if we increase the sequence
225 * here the kvm page fault will notice if the spte can't be
226 * established because the page is going to be freed. If
227 * instead the kvm page fault establishes the spte before
228 * ->invalidate_page runs, kvm_unmap_hva will release it
229 * before returning.
230 *
231 * The sequence increase only need to be seen at spin_unlock
232 * time, and not at spin_lock time.
233 *
234 * Increasing the sequence after the spin_unlock would be
235 * unsafe because the kvm page fault could then establish the
236 * pte after kvm_unmap_hva returned, without noticing the page
237 * is going to be freed.
238 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200239 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200240 spin_lock(&kvm->mmu_lock);
241 kvm->mmu_notifier_seq++;
242 need_tlb_flush = kvm_unmap_hva(kvm, address);
243 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200244 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200245
246 /* we've to flush the tlb before the pages can be freed */
247 if (need_tlb_flush)
248 kvm_flush_remote_tlbs(kvm);
249
250}
251
Izik Eidus3da0dd42009-09-23 21:47:18 +0300252static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
253 struct mm_struct *mm,
254 unsigned long address,
255 pte_t pte)
256{
257 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200258 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300259
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200260 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300261 spin_lock(&kvm->mmu_lock);
262 kvm->mmu_notifier_seq++;
263 kvm_set_spte_hva(kvm, address, pte);
264 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200265 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300266}
267
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200268static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
269 struct mm_struct *mm,
270 unsigned long start,
271 unsigned long end)
272{
273 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200274 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200275
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200276 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200277 spin_lock(&kvm->mmu_lock);
278 /*
279 * The count increase must become visible at unlock time as no
280 * spte can be established without taking the mmu_lock and
281 * count is also read inside the mmu_lock critical section.
282 */
283 kvm->mmu_notifier_count++;
284 for (; start < end; start += PAGE_SIZE)
285 need_tlb_flush |= kvm_unmap_hva(kvm, start);
286 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200287 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200288
289 /* we've to flush the tlb before the pages can be freed */
290 if (need_tlb_flush)
291 kvm_flush_remote_tlbs(kvm);
292}
293
294static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
295 struct mm_struct *mm,
296 unsigned long start,
297 unsigned long end)
298{
299 struct kvm *kvm = mmu_notifier_to_kvm(mn);
300
301 spin_lock(&kvm->mmu_lock);
302 /*
303 * This sequence increase will notify the kvm page fault that
304 * the page that is going to be mapped in the spte could have
305 * been freed.
306 */
307 kvm->mmu_notifier_seq++;
308 /*
309 * The above sequence increase must be visible before the
310 * below count decrease but both values are read by the kvm
311 * page fault under mmu_lock spinlock so we don't need to add
312 * a smb_wmb() here in between the two.
313 */
314 kvm->mmu_notifier_count--;
315 spin_unlock(&kvm->mmu_lock);
316
317 BUG_ON(kvm->mmu_notifier_count < 0);
318}
319
320static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
321 struct mm_struct *mm,
322 unsigned long address)
323{
324 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200325 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200326
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200327 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200328 spin_lock(&kvm->mmu_lock);
329 young = kvm_age_hva(kvm, address);
330 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200331 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200332
333 if (young)
334 kvm_flush_remote_tlbs(kvm);
335
336 return young;
337}
338
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100339static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
340 struct mm_struct *mm)
341{
342 struct kvm *kvm = mmu_notifier_to_kvm(mn);
343 kvm_arch_flush_shadow(kvm);
344}
345
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200346static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
347 .invalidate_page = kvm_mmu_notifier_invalidate_page,
348 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
349 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
350 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300351 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100352 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200353};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200354
355static int kvm_init_mmu_notifier(struct kvm *kvm)
356{
357 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
358 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
359}
360
361#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
362
363static int kvm_init_mmu_notifier(struct kvm *kvm)
364{
365 return 0;
366}
367
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200368#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
369
Avi Kivityf17abe92007-02-21 19:28:04 +0200370static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200372 int r = 0, i;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800373 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200374#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
375 struct page *page;
376#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800378 if (IS_ERR(kvm))
379 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200380
381 r = hardware_enable_all();
382 if (r)
383 goto out_err_nodisable;
384
Avi Kivity75858a82009-01-04 17:10:50 +0200385#ifdef CONFIG_HAVE_KVM_IRQCHIP
386 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300387 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200388#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200390 r = -ENOMEM;
391 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
392 if (!kvm->memslots)
393 goto out_err;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200394 if (init_srcu_struct(&kvm->srcu))
395 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200396 for (i = 0; i < KVM_NR_BUSES; i++) {
397 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
398 GFP_KERNEL);
399 if (!kvm->buses[i]) {
400 cleanup_srcu_struct(&kvm->srcu);
401 goto out_err;
402 }
403 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200404
Laurent Vivier5f94c172008-05-30 16:05:54 +0200405#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
406 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200407 if (!page) {
408 cleanup_srcu_struct(&kvm->srcu);
Alexander Graf10474ae2009-09-15 11:37:46 +0200409 goto out_err;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200410 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200411
Laurent Vivier5f94c172008-05-30 16:05:54 +0200412 kvm->coalesced_mmio_ring =
413 (struct kvm_coalesced_mmio_ring *)page_address(page);
414#endif
415
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200416 r = kvm_init_mmu_notifier(kvm);
Avi Kivity283d0c62009-12-20 14:25:19 +0200417 if (r) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200418 cleanup_srcu_struct(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200419#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
Avi Kivity283d0c62009-12-20 14:25:19 +0200420 put_page(page);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200421#endif
Avi Kivity283d0c62009-12-20 14:25:19 +0200422 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200423 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200424
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200425 kvm->mm = current->mm;
426 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500427 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti84261922009-06-17 10:53:47 -0300428 spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400429 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800430 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300431 mutex_init(&kvm->irq_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200432 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300433 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000434 spin_lock(&kvm_lock);
435 list_add(&kvm->vm_list, &vm_list);
436 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200437#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
438 kvm_coalesced_mmio_init(kvm);
439#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800440out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200441 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200442
443out_err:
444 hardware_disable_all();
445out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200446 for (i = 0; i < KVM_NR_BUSES; i++)
447 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200448 kfree(kvm->memslots);
Alexander Graf10474ae2009-09-15 11:37:46 +0200449 kfree(kvm);
450 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200451}
452
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453/*
454 * Free any memory in @free but not in @dont.
455 */
456static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
457 struct kvm_memory_slot *dont)
458{
Joerg Roedelec04b262009-06-19 15:16:23 +0200459 int i;
460
Izik Eidus290fc382007-09-27 14:11:22 +0200461 if (!dont || free->rmap != dont->rmap)
462 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463
464 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
465 vfree(free->dirty_bitmap);
466
Joerg Roedelec04b262009-06-19 15:16:23 +0200467
468 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
469 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
470 vfree(free->lpage_info[i]);
471 free->lpage_info[i] = NULL;
472 }
473 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000476 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500477 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478}
479
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800480void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481{
482 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200483 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200485 for (i = 0; i < slots->nmemslots; ++i)
486 kvm_free_physmem_slot(&slots->memslots[i], NULL);
487
488 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489}
490
Avi Kivityf17abe92007-02-21 19:28:04 +0200491static void kvm_destroy_vm(struct kvm *kvm)
492{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200493 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200494 struct mm_struct *mm = kvm->mm;
495
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800496 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800497 spin_lock(&kvm_lock);
498 list_del(&kvm->vm_list);
499 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200500 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200501 for (i = 0; i < KVM_NR_BUSES; i++)
502 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200503 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200504#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
505 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200506#else
507 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200508#endif
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200509 cleanup_srcu_struct(&kvm->srcu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800510 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200511 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200512 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200513}
514
Izik Eidusd39f13b2008-03-30 16:01:25 +0300515void kvm_get_kvm(struct kvm *kvm)
516{
517 atomic_inc(&kvm->users_count);
518}
519EXPORT_SYMBOL_GPL(kvm_get_kvm);
520
521void kvm_put_kvm(struct kvm *kvm)
522{
523 if (atomic_dec_and_test(&kvm->users_count))
524 kvm_destroy_vm(kvm);
525}
526EXPORT_SYMBOL_GPL(kvm_put_kvm);
527
528
Avi Kivityf17abe92007-02-21 19:28:04 +0200529static int kvm_vm_release(struct inode *inode, struct file *filp)
530{
531 struct kvm *kvm = filp->private_data;
532
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400533 kvm_irqfd_release(kvm);
534
Izik Eidusd39f13b2008-03-30 16:01:25 +0300535 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 return 0;
537}
538
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540 * Allocate some memory and give it an address in the guest physical address
541 * space.
542 *
543 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800544 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500545 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800547int __kvm_set_memory_region(struct kvm *kvm,
548 struct kvm_userspace_memory_region *mem,
549 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200551 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200553 unsigned long npages;
554 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 struct kvm_memory_slot *memslot;
556 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200557 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
559 r = -EINVAL;
560 /* General sanity checks */
561 if (mem->memory_size & (PAGE_SIZE - 1))
562 goto out;
563 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
564 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800565 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600566 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200567 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568 goto out;
569 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
570 goto out;
571
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200572 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
574 npages = mem->memory_size >> PAGE_SHIFT;
575
576 if (!npages)
577 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
578
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 new = old = *memslot;
580
581 new.base_gfn = base_gfn;
582 new.npages = npages;
583 new.flags = mem->flags;
584
585 /* Disallow changing a memory slot's size. */
586 r = -EINVAL;
587 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800588 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589
590 /* Check for overlaps */
591 r = -EEXIST;
592 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200593 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200595 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 continue;
597 if (!((base_gfn + npages <= s->base_gfn) ||
598 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800599 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601
Avi Kivity6aa8b732006-12-10 02:21:36 -0800602 /* Free page dirty bitmap if unneeded */
603 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000604 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605
606 r = -ENOMEM;
607
608 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200609#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500610 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400611 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200612
613 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800614 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200615
Izik Eidus290fc382007-09-27 14:11:22 +0200616 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500617
Izik Eidus80b14b52007-10-25 11:54:04 +0200618 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200619 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200621 if (!npages)
622 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300623
Joerg Roedelec04b262009-06-19 15:16:23 +0200624 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200625 unsigned long ugfn;
626 unsigned long j;
627 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200628 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300629
Joerg Roedelec04b262009-06-19 15:16:23 +0200630 /* Avoid unused variable warning if no large pages */
631 (void)level;
632
633 if (new.lpage_info[i])
634 continue;
635
636 lpages = 1 + (base_gfn + npages - 1) /
637 KVM_PAGES_PER_HPAGE(level);
638 lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
639
640 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
641
642 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300643 goto out_free;
644
Joerg Roedelec04b262009-06-19 15:16:23 +0200645 memset(new.lpage_info[i], 0,
646 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300647
Joerg Roedelec04b262009-06-19 15:16:23 +0200648 if (base_gfn % KVM_PAGES_PER_HPAGE(level))
649 new.lpage_info[i][0].write_count = 1;
650 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
651 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300652 ugfn = new.userspace_addr >> PAGE_SHIFT;
653 /*
654 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300655 * other, or if explicitly asked to, disable large page
656 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300657 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200658 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300659 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200660 for (j = 0; j < lpages; ++j)
661 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300662 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663
Joerg Roedelec04b262009-06-19 15:16:23 +0200664skip_lpage:
665
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 /* Allocate page dirty bitmap if needed */
667 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
668 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
669
670 new.dirty_bitmap = vmalloc(dirty_bytes);
671 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800672 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 memset(new.dirty_bitmap, 0, dirty_bytes);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200674 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300675 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200676 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200678#else /* not defined CONFIG_S390 */
679 new.user_alloc = user_alloc;
680 if (user_alloc)
681 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200682#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200684 if (!npages) {
685 r = -ENOMEM;
686 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
687 if (!slots)
688 goto out_free;
689 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
690 if (mem->slot >= slots->nmemslots)
691 slots->nmemslots = mem->slot + 1;
692 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
693
694 old_memslots = kvm->memslots;
695 rcu_assign_pointer(kvm->memslots, slots);
696 synchronize_srcu_expedited(&kvm->srcu);
697 /* From this point no new shadow pages pointing to a deleted
698 * memslot will be created.
699 *
700 * validation of sp->gfn happens in:
701 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
702 * - kvm_is_visible_gfn (mmu_check_roots)
703 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300704 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200705 kfree(old_memslots);
706 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300707
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200708 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
709 if (r)
710 goto out_free;
711
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200712#ifdef CONFIG_DMAR
713 /* map the pages in iommu page table */
714 if (npages) {
715 r = kvm_iommu_map_pages(kvm, &new);
716 if (r)
717 goto out_free;
718 }
719#endif
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200720
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200721 r = -ENOMEM;
722 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
723 if (!slots)
724 goto out_free;
725 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
726 if (mem->slot >= slots->nmemslots)
727 slots->nmemslots = mem->slot + 1;
728
729 /* actual memory is freed via old in kvm_free_physmem_slot below */
730 if (!npages) {
731 new.rmap = NULL;
732 new.dirty_bitmap = NULL;
733 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
734 new.lpage_info[i] = NULL;
735 }
736
737 slots->memslots[mem->slot] = new;
738 old_memslots = kvm->memslots;
739 rcu_assign_pointer(kvm->memslots, slots);
740 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200742 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800743
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200744 kvm_free_physmem_slot(&old, &new);
745 kfree(old_memslots);
746
747 if (flush_shadow)
748 kvm_arch_flush_shadow(kvm);
749
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 return 0;
751
Sheng Yangf78e0e22007-10-29 09:40:42 +0800752out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 kvm_free_physmem_slot(&new, &old);
754out:
755 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200756
757}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800758EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
759
760int kvm_set_memory_region(struct kvm *kvm,
761 struct kvm_userspace_memory_region *mem,
762 int user_alloc)
763{
764 int r;
765
Izik Eidus72dc67a2008-02-10 18:04:15 +0200766 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800767 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200768 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800769 return r;
770}
Izik Eidus210c7c42007-10-24 23:52:57 +0200771EXPORT_SYMBOL_GPL(kvm_set_memory_region);
772
Carsten Otte1fe779f2007-10-29 16:08:35 +0100773int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
774 struct
775 kvm_userspace_memory_region *mem,
776 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200777{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200778 if (mem->slot >= KVM_MEMORY_SLOTS)
779 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200780 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781}
782
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800783int kvm_get_dirty_log(struct kvm *kvm,
784 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785{
786 struct kvm_memory_slot *memslot;
787 int r, i;
788 int n;
789 unsigned long any = 0;
790
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 r = -EINVAL;
792 if (log->slot >= KVM_MEMORY_SLOTS)
793 goto out;
794
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200795 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 r = -ENOENT;
797 if (!memslot->dirty_bitmap)
798 goto out;
799
Uri Lublincd1a4a92007-02-22 16:43:09 +0200800 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801
Uri Lublincd1a4a92007-02-22 16:43:09 +0200802 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 any = memslot->dirty_bitmap[i];
804
805 r = -EFAULT;
806 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
807 goto out;
808
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800809 if (any)
810 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
812 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 return r;
815}
816
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300817void kvm_disable_largepages(void)
818{
819 largepages_enabled = false;
820}
821EXPORT_SYMBOL_GPL(kvm_disable_largepages);
822
Izik Eiduscea7bb22007-10-17 19:17:48 +0200823int is_error_page(struct page *page)
824{
825 return page == bad_page;
826}
827EXPORT_SYMBOL_GPL(is_error_page);
828
Anthony Liguori35149e22008-04-02 14:46:56 -0500829int is_error_pfn(pfn_t pfn)
830{
831 return pfn == bad_pfn;
832}
833EXPORT_SYMBOL_GPL(is_error_pfn);
834
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200835static inline unsigned long bad_hva(void)
836{
837 return PAGE_OFFSET;
838}
839
840int kvm_is_error_hva(unsigned long addr)
841{
842 return addr == bad_hva();
843}
844EXPORT_SYMBOL_GPL(kvm_is_error_hva);
845
Izik Eidus28430992008-10-03 17:40:32 +0300846struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847{
848 int i;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200849 struct kvm_memslots *slots = rcu_dereference(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200851 for (i = 0; i < slots->nmemslots; ++i) {
852 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853
854 if (gfn >= memslot->base_gfn
855 && gfn < memslot->base_gfn + memslot->npages)
856 return memslot;
857 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000858 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859}
Izik Eidus28430992008-10-03 17:40:32 +0300860EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300861
862struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
863{
864 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300865 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300866}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867
Izik Eiduse0d62c72007-10-24 23:57:46 +0200868int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
869{
870 int i;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200871 struct kvm_memslots *slots = rcu_dereference(kvm->memslots);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200872
Marcelo Tosattia983fb22009-12-23 14:35:23 -0200873 gfn = unalias_gfn_instantiation(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200874 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200875 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200876
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200877 if (memslot->flags & KVM_MEMSLOT_INVALID)
878 continue;
879
Izik Eiduse0d62c72007-10-24 23:57:46 +0200880 if (gfn >= memslot->base_gfn
881 && gfn < memslot->base_gfn + memslot->npages)
882 return 1;
883 }
884 return 0;
885}
886EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
887
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200888int memslot_id(struct kvm *kvm, gfn_t gfn)
889{
890 int i;
891 struct kvm_memslots *slots = rcu_dereference(kvm->memslots);
892 struct kvm_memory_slot *memslot = NULL;
893
894 gfn = unalias_gfn(kvm, gfn);
895 for (i = 0; i < slots->nmemslots; ++i) {
896 memslot = &slots->memslots[i];
897
898 if (gfn >= memslot->base_gfn
899 && gfn < memslot->base_gfn + memslot->npages)
900 break;
901 }
902
903 return memslot - slots->memslots;
904}
905
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300906unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200907{
908 struct kvm_memory_slot *slot;
909
Marcelo Tosattia983fb22009-12-23 14:35:23 -0200910 gfn = unalias_gfn_instantiation(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300911 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200912 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200913 return bad_hva();
914 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
915}
Sheng Yang0d150292008-04-25 21:44:50 +0800916EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200917
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200918static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300919{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500920 struct page *page[1];
921 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500922 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300923
Avi Kivity60395222007-10-21 11:03:36 +0200924 might_sleep();
925
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300926 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200927
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500928 if (unlikely(npages != 1)) {
929 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500930
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300931 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500932 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300933
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500934 if (vma == NULL || addr < vma->vm_start ||
935 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300936 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500937 get_page(bad_page);
938 return page_to_pfn(bad_page);
939 }
940
941 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300942 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800943 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500944 } else
945 pfn = page_to_pfn(page[0]);
946
947 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500948}
949
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200950pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
951{
952 unsigned long addr;
953
954 addr = gfn_to_hva(kvm, gfn);
955 if (kvm_is_error_hva(addr)) {
956 get_page(bad_page);
957 return page_to_pfn(bad_page);
958 }
959
960 return hva_to_pfn(kvm, addr);
961}
Anthony Liguori35149e22008-04-02 14:46:56 -0500962EXPORT_SYMBOL_GPL(gfn_to_pfn);
963
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200964static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
965{
966 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
967}
968
969pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
970 struct kvm_memory_slot *slot, gfn_t gfn)
971{
972 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
973 return hva_to_pfn(kvm, addr);
974}
975
Anthony Liguori35149e22008-04-02 14:46:56 -0500976struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
977{
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500978 pfn_t pfn;
979
980 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800981 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500982 return pfn_to_page(pfn);
983
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800984 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500985
986 get_page(bad_page);
987 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300988}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500989
Avi Kivity954bbbc2007-03-30 14:02:32 +0300990EXPORT_SYMBOL_GPL(gfn_to_page);
991
Izik Eidusb4231d62007-11-20 11:49:33 +0200992void kvm_release_page_clean(struct page *page)
993{
Anthony Liguori35149e22008-04-02 14:46:56 -0500994 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +0200995}
996EXPORT_SYMBOL_GPL(kvm_release_page_clean);
997
Anthony Liguori35149e22008-04-02 14:46:56 -0500998void kvm_release_pfn_clean(pfn_t pfn)
999{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001000 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001001 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001002}
1003EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1004
Izik Eidusb4231d62007-11-20 11:49:33 +02001005void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001006{
Anthony Liguori35149e22008-04-02 14:46:56 -05001007 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001008}
Izik Eidusb4231d62007-11-20 11:49:33 +02001009EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001010
Anthony Liguori35149e22008-04-02 14:46:56 -05001011void kvm_release_pfn_dirty(pfn_t pfn)
1012{
1013 kvm_set_pfn_dirty(pfn);
1014 kvm_release_pfn_clean(pfn);
1015}
1016EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1017
1018void kvm_set_page_dirty(struct page *page)
1019{
1020 kvm_set_pfn_dirty(page_to_pfn(page));
1021}
1022EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1023
1024void kvm_set_pfn_dirty(pfn_t pfn)
1025{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001026 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001027 struct page *page = pfn_to_page(pfn);
1028 if (!PageReserved(page))
1029 SetPageDirty(page);
1030 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001031}
1032EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1033
1034void kvm_set_pfn_accessed(pfn_t pfn)
1035{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001036 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001037 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001038}
1039EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1040
1041void kvm_get_pfn(pfn_t pfn)
1042{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001043 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001044 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001045}
1046EXPORT_SYMBOL_GPL(kvm_get_pfn);
1047
Izik Eidus195aefd2007-10-01 22:14:18 +02001048static int next_segment(unsigned long len, int offset)
1049{
1050 if (len > PAGE_SIZE - offset)
1051 return PAGE_SIZE - offset;
1052 else
1053 return len;
1054}
1055
1056int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1057 int len)
1058{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001059 int r;
1060 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001061
Izik Eiduse0506bc2007-11-11 22:10:22 +02001062 addr = gfn_to_hva(kvm, gfn);
1063 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001064 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001065 r = copy_from_user(data, (void __user *)addr + offset, len);
1066 if (r)
1067 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001068 return 0;
1069}
1070EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1071
1072int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1073{
1074 gfn_t gfn = gpa >> PAGE_SHIFT;
1075 int seg;
1076 int offset = offset_in_page(gpa);
1077 int ret;
1078
1079 while ((seg = next_segment(len, offset)) != 0) {
1080 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1081 if (ret < 0)
1082 return ret;
1083 offset = 0;
1084 len -= seg;
1085 data += seg;
1086 ++gfn;
1087 }
1088 return 0;
1089}
1090EXPORT_SYMBOL_GPL(kvm_read_guest);
1091
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001092int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1093 unsigned long len)
1094{
1095 int r;
1096 unsigned long addr;
1097 gfn_t gfn = gpa >> PAGE_SHIFT;
1098 int offset = offset_in_page(gpa);
1099
1100 addr = gfn_to_hva(kvm, gfn);
1101 if (kvm_is_error_hva(addr))
1102 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001103 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001104 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001105 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001106 if (r)
1107 return -EFAULT;
1108 return 0;
1109}
1110EXPORT_SYMBOL(kvm_read_guest_atomic);
1111
Izik Eidus195aefd2007-10-01 22:14:18 +02001112int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1113 int offset, int len)
1114{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001115 int r;
1116 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001117
Izik Eiduse0506bc2007-11-11 22:10:22 +02001118 addr = gfn_to_hva(kvm, gfn);
1119 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001120 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001121 r = copy_to_user((void __user *)addr + offset, data, len);
1122 if (r)
1123 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001124 mark_page_dirty(kvm, gfn);
1125 return 0;
1126}
1127EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1128
1129int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1130 unsigned long len)
1131{
1132 gfn_t gfn = gpa >> PAGE_SHIFT;
1133 int seg;
1134 int offset = offset_in_page(gpa);
1135 int ret;
1136
1137 while ((seg = next_segment(len, offset)) != 0) {
1138 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1139 if (ret < 0)
1140 return ret;
1141 offset = 0;
1142 len -= seg;
1143 data += seg;
1144 ++gfn;
1145 }
1146 return 0;
1147}
1148
1149int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1150{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001151 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001152}
1153EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1154
1155int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1156{
1157 gfn_t gfn = gpa >> PAGE_SHIFT;
1158 int seg;
1159 int offset = offset_in_page(gpa);
1160 int ret;
1161
1162 while ((seg = next_segment(len, offset)) != 0) {
1163 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1164 if (ret < 0)
1165 return ret;
1166 offset = 0;
1167 len -= seg;
1168 ++gfn;
1169 }
1170 return 0;
1171}
1172EXPORT_SYMBOL_GPL(kvm_clear_guest);
1173
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1175{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001176 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177
Uri Lublin3b6fff12007-10-30 10:42:09 +02001178 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001179 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001180 if (memslot && memslot->dirty_bitmap) {
1181 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182
Rusty Russell7e9d6192007-07-31 20:41:14 +10001183 /* avoid RMW */
Alexander Grafc8240bd2009-10-30 05:47:26 +00001184 if (!generic_test_le_bit(rel_gfn, memslot->dirty_bitmap))
1185 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 }
1187}
1188
Eddie Dongb6958ce2007-07-18 12:15:21 +03001189/*
1190 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1191 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001192void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001193{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001194 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001195
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001196 for (;;) {
1197 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001198
Gleb Natapova1b37102009-07-09 15:33:52 +03001199 if (kvm_arch_vcpu_runnable(vcpu)) {
Marcelo Tosattid7690172008-09-08 15:23:48 -03001200 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001201 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001202 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001203 if (kvm_cpu_has_pending_timer(vcpu))
1204 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001205 if (signal_pending(current))
1206 break;
1207
Eddie Dongb6958ce2007-07-18 12:15:21 +03001208 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001209 }
1210
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001211 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001212}
1213
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214void kvm_resched(struct kvm_vcpu *vcpu)
1215{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001216 if (!need_resched())
1217 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219}
1220EXPORT_SYMBOL_GPL(kvm_resched);
1221
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001222void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1223{
1224 ktime_t expires;
1225 DEFINE_WAIT(wait);
1226
1227 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1228
1229 /* Sleep for 100 us, and hope lock-holder got scheduled */
1230 expires = ktime_add_ns(ktime_get(), 100000UL);
1231 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1232
1233 finish_wait(&vcpu->wq, &wait);
1234}
1235EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1236
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001237static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001238{
1239 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001240 struct page *page;
1241
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001242 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001243 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001244#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001245 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001246 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001247#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001248#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1249 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1250 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1251#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001252 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001253 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001254 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001255 vmf->page = page;
1256 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001257}
1258
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001259static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001260 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001261};
1262
1263static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1264{
1265 vma->vm_ops = &kvm_vcpu_vm_ops;
1266 return 0;
1267}
1268
Avi Kivitybccf2152007-02-21 18:04:26 +02001269static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1270{
1271 struct kvm_vcpu *vcpu = filp->private_data;
1272
Al Viro66c0b392008-04-19 20:33:56 +01001273 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001274 return 0;
1275}
1276
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001277static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001278 .release = kvm_vcpu_release,
1279 .unlocked_ioctl = kvm_vcpu_ioctl,
1280 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001281 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001282};
1283
1284/*
1285 * Allocates an inode for the vcpu.
1286 */
1287static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1288{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001289 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001290}
1291
Avi Kivityc5ea7662007-02-20 18:41:05 +02001292/*
1293 * Creates some virtual cpus. Good luck creating more than one.
1294 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001295static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001296{
1297 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001298 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001299
Gleb Natapov73880c82009-06-09 15:56:28 +03001300 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001301 if (IS_ERR(vcpu))
1302 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001303
Avi Kivity15ad7142007-07-11 18:17:21 +03001304 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1305
Avi Kivity26e52152007-11-20 15:30:24 +02001306 r = kvm_arch_vcpu_setup(vcpu);
1307 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001308 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001309
Shaohua Li11ec2802007-07-23 14:51:37 +08001310 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001311 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1312 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001313 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001314 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001315
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001316 kvm_for_each_vcpu(r, v, kvm)
1317 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001318 r = -EEXIST;
1319 goto vcpu_destroy;
1320 }
1321
1322 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001323
1324 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001325 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001326 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001327 if (r < 0) {
1328 kvm_put_kvm(kvm);
1329 goto vcpu_destroy;
1330 }
1331
1332 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1333 smp_wmb();
1334 atomic_inc(&kvm->online_vcpus);
1335
1336#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1337 if (kvm->bsp_vcpu_id == id)
1338 kvm->bsp_vcpu = vcpu;
1339#endif
1340 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001341 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001342
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001343vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001344 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001345 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001346 return r;
1347}
1348
Avi Kivity1961d272007-03-05 19:46:05 +02001349static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1350{
1351 if (sigset) {
1352 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1353 vcpu->sigset_active = 1;
1354 vcpu->sigset = *sigset;
1355 } else
1356 vcpu->sigset_active = 0;
1357 return 0;
1358}
1359
Avi Kivitybccf2152007-02-21 18:04:26 +02001360static long kvm_vcpu_ioctl(struct file *filp,
1361 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362{
Avi Kivitybccf2152007-02-21 18:04:26 +02001363 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001364 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001365 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001366 struct kvm_fpu *fpu = NULL;
1367 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001369 if (vcpu->kvm->mm != current->mm)
1370 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001372 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001373 r = -EINVAL;
1374 if (arg)
1375 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001376 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001379 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001381 r = -ENOMEM;
1382 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1383 if (!kvm_regs)
1384 goto out;
1385 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001387 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001389 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1390 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001392out_free1:
1393 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 break;
1395 }
1396 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001397 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001399 r = -ENOMEM;
1400 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1401 if (!kvm_regs)
1402 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001404 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1405 goto out_free2;
1406 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001408 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001410out_free2:
1411 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 break;
1413 }
1414 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001415 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1416 r = -ENOMEM;
1417 if (!kvm_sregs)
1418 goto out;
1419 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 if (r)
1421 goto out;
1422 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001423 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 goto out;
1425 r = 0;
1426 break;
1427 }
1428 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001429 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1430 r = -ENOMEM;
1431 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001433 r = -EFAULT;
1434 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1435 goto out;
1436 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 if (r)
1438 goto out;
1439 r = 0;
1440 break;
1441 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001442 case KVM_GET_MP_STATE: {
1443 struct kvm_mp_state mp_state;
1444
1445 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1446 if (r)
1447 goto out;
1448 r = -EFAULT;
1449 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1450 goto out;
1451 r = 0;
1452 break;
1453 }
1454 case KVM_SET_MP_STATE: {
1455 struct kvm_mp_state mp_state;
1456
1457 r = -EFAULT;
1458 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1459 goto out;
1460 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1461 if (r)
1462 goto out;
1463 r = 0;
1464 break;
1465 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 case KVM_TRANSLATE: {
1467 struct kvm_translation tr;
1468
1469 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001470 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001472 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 if (r)
1474 goto out;
1475 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001476 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 goto out;
1478 r = 0;
1479 break;
1480 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001481 case KVM_SET_GUEST_DEBUG: {
1482 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483
1484 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001485 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001487 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 if (r)
1489 goto out;
1490 r = 0;
1491 break;
1492 }
Avi Kivity1961d272007-03-05 19:46:05 +02001493 case KVM_SET_SIGNAL_MASK: {
1494 struct kvm_signal_mask __user *sigmask_arg = argp;
1495 struct kvm_signal_mask kvm_sigmask;
1496 sigset_t sigset, *p;
1497
1498 p = NULL;
1499 if (argp) {
1500 r = -EFAULT;
1501 if (copy_from_user(&kvm_sigmask, argp,
1502 sizeof kvm_sigmask))
1503 goto out;
1504 r = -EINVAL;
1505 if (kvm_sigmask.len != sizeof sigset)
1506 goto out;
1507 r = -EFAULT;
1508 if (copy_from_user(&sigset, sigmask_arg->sigset,
1509 sizeof sigset))
1510 goto out;
1511 p = &sigset;
1512 }
1513 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1514 break;
1515 }
Avi Kivityb8836732007-04-01 16:34:31 +03001516 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001517 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1518 r = -ENOMEM;
1519 if (!fpu)
1520 goto out;
1521 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001522 if (r)
1523 goto out;
1524 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001525 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001526 goto out;
1527 r = 0;
1528 break;
1529 }
1530 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001531 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1532 r = -ENOMEM;
1533 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001534 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001535 r = -EFAULT;
1536 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1537 goto out;
1538 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001539 if (r)
1540 goto out;
1541 r = 0;
1542 break;
1543 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001544 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001545 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001546 }
1547out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001548 kfree(fpu);
1549 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001550 return r;
1551}
1552
1553static long kvm_vm_ioctl(struct file *filp,
1554 unsigned int ioctl, unsigned long arg)
1555{
1556 struct kvm *kvm = filp->private_data;
1557 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001558 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001559
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001560 if (kvm->mm != current->mm)
1561 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001562 switch (ioctl) {
1563 case KVM_CREATE_VCPU:
1564 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1565 if (r < 0)
1566 goto out;
1567 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001568 case KVM_SET_USER_MEMORY_REGION: {
1569 struct kvm_userspace_memory_region kvm_userspace_mem;
1570
1571 r = -EFAULT;
1572 if (copy_from_user(&kvm_userspace_mem, argp,
1573 sizeof kvm_userspace_mem))
1574 goto out;
1575
1576 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 if (r)
1578 goto out;
1579 break;
1580 }
1581 case KVM_GET_DIRTY_LOG: {
1582 struct kvm_dirty_log log;
1583
1584 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001585 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001587 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 if (r)
1589 goto out;
1590 break;
1591 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001592#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1593 case KVM_REGISTER_COALESCED_MMIO: {
1594 struct kvm_coalesced_mmio_zone zone;
1595 r = -EFAULT;
1596 if (copy_from_user(&zone, argp, sizeof zone))
1597 goto out;
1598 r = -ENXIO;
1599 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1600 if (r)
1601 goto out;
1602 r = 0;
1603 break;
1604 }
1605 case KVM_UNREGISTER_COALESCED_MMIO: {
1606 struct kvm_coalesced_mmio_zone zone;
1607 r = -EFAULT;
1608 if (copy_from_user(&zone, argp, sizeof zone))
1609 goto out;
1610 r = -ENXIO;
1611 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1612 if (r)
1613 goto out;
1614 r = 0;
1615 break;
1616 }
1617#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001618 case KVM_IRQFD: {
1619 struct kvm_irqfd data;
1620
1621 r = -EFAULT;
1622 if (copy_from_user(&data, argp, sizeof data))
1623 goto out;
1624 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1625 break;
1626 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001627 case KVM_IOEVENTFD: {
1628 struct kvm_ioeventfd data;
1629
1630 r = -EFAULT;
1631 if (copy_from_user(&data, argp, sizeof data))
1632 goto out;
1633 r = kvm_ioeventfd(kvm, &data);
1634 break;
1635 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001636#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1637 case KVM_SET_BOOT_CPU_ID:
1638 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001639 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001640 if (atomic_read(&kvm->online_vcpus) != 0)
1641 r = -EBUSY;
1642 else
1643 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001644 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001645 break;
1646#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001647 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001648 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001649 if (r == -ENOTTY)
1650 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001651 }
1652out:
1653 return r;
1654}
1655
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001656#ifdef CONFIG_COMPAT
1657struct compat_kvm_dirty_log {
1658 __u32 slot;
1659 __u32 padding1;
1660 union {
1661 compat_uptr_t dirty_bitmap; /* one bit per page */
1662 __u64 padding2;
1663 };
1664};
1665
1666static long kvm_vm_compat_ioctl(struct file *filp,
1667 unsigned int ioctl, unsigned long arg)
1668{
1669 struct kvm *kvm = filp->private_data;
1670 int r;
1671
1672 if (kvm->mm != current->mm)
1673 return -EIO;
1674 switch (ioctl) {
1675 case KVM_GET_DIRTY_LOG: {
1676 struct compat_kvm_dirty_log compat_log;
1677 struct kvm_dirty_log log;
1678
1679 r = -EFAULT;
1680 if (copy_from_user(&compat_log, (void __user *)arg,
1681 sizeof(compat_log)))
1682 goto out;
1683 log.slot = compat_log.slot;
1684 log.padding1 = compat_log.padding1;
1685 log.padding2 = compat_log.padding2;
1686 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1687
1688 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1689 if (r)
1690 goto out;
1691 break;
1692 }
1693 default:
1694 r = kvm_vm_ioctl(filp, ioctl, arg);
1695 }
1696
1697out:
1698 return r;
1699}
1700#endif
1701
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001702static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001703{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001704 struct page *page[1];
1705 unsigned long addr;
1706 int npages;
1707 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001708 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001709
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001710 addr = gfn_to_hva(kvm, gfn);
1711 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001712 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001713
1714 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1715 NULL);
1716 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001717 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001718
1719 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001720 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001721}
1722
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001723static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001724 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001725};
1726
1727static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1728{
1729 vma->vm_ops = &kvm_vm_vm_ops;
1730 return 0;
1731}
1732
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001733static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001734 .release = kvm_vm_release,
1735 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001736#ifdef CONFIG_COMPAT
1737 .compat_ioctl = kvm_vm_compat_ioctl,
1738#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001739 .mmap = kvm_vm_mmap,
1740};
1741
1742static int kvm_dev_ioctl_create_vm(void)
1743{
Al Viro2030a422008-02-23 06:46:49 -05001744 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001745 struct kvm *kvm;
1746
Avi Kivityf17abe92007-02-21 19:28:04 +02001747 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001748 if (IS_ERR(kvm))
1749 return PTR_ERR(kvm);
Roland Dreier628ff7c2009-12-18 09:41:24 -08001750 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
Al Viro2030a422008-02-23 06:46:49 -05001751 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001752 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001753
Avi Kivityf17abe92007-02-21 19:28:04 +02001754 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001755}
1756
Avi Kivity1a811b62008-12-08 18:25:27 +02001757static long kvm_dev_ioctl_check_extension_generic(long arg)
1758{
1759 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001760 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001761 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001762 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001763#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1764 case KVM_CAP_SET_BOOT_CPU_ID:
1765#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001766 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001767 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001768#ifdef CONFIG_HAVE_KVM_IRQCHIP
1769 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001770 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001771#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001772 default:
1773 break;
1774 }
1775 return kvm_dev_ioctl_check_extension(arg);
1776}
1777
Avi Kivityf17abe92007-02-21 19:28:04 +02001778static long kvm_dev_ioctl(struct file *filp,
1779 unsigned int ioctl, unsigned long arg)
1780{
Avi Kivity07c45a32007-03-07 13:05:38 +02001781 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001782
1783 switch (ioctl) {
1784 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001785 r = -EINVAL;
1786 if (arg)
1787 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001788 r = KVM_API_VERSION;
1789 break;
1790 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001791 r = -EINVAL;
1792 if (arg)
1793 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001794 r = kvm_dev_ioctl_create_vm();
1795 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001796 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001797 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001798 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001799 case KVM_GET_VCPU_MMAP_SIZE:
1800 r = -EINVAL;
1801 if (arg)
1802 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001803 r = PAGE_SIZE; /* struct kvm_run */
1804#ifdef CONFIG_X86
1805 r += PAGE_SIZE; /* pio data page */
1806#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001807#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1808 r += PAGE_SIZE; /* coalesced mmio ring page */
1809#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001810 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001811 case KVM_TRACE_ENABLE:
1812 case KVM_TRACE_PAUSE:
1813 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001814 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001815 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001817 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 }
1819out:
1820 return r;
1821}
1822
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 .unlocked_ioctl = kvm_dev_ioctl,
1825 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826};
1827
1828static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001829 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 "kvm",
1831 &kvm_chardev_ops,
1832};
1833
Avi Kivity1b6c0162007-05-24 13:03:52 +03001834static void hardware_enable(void *junk)
1835{
1836 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02001837 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03001838
Rusty Russell7f59f492008-12-07 21:25:45 +10301839 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001840 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02001841
Rusty Russell7f59f492008-12-07 21:25:45 +10301842 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02001843
1844 r = kvm_arch_hardware_enable(NULL);
1845
1846 if (r) {
1847 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1848 atomic_inc(&hardware_enable_failed);
1849 printk(KERN_INFO "kvm: enabling virtualization on "
1850 "CPU%d failed\n", cpu);
1851 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03001852}
1853
1854static void hardware_disable(void *junk)
1855{
1856 int cpu = raw_smp_processor_id();
1857
Rusty Russell7f59f492008-12-07 21:25:45 +10301858 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001859 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301860 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001861 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001862}
1863
Alexander Graf10474ae2009-09-15 11:37:46 +02001864static void hardware_disable_all_nolock(void)
1865{
1866 BUG_ON(!kvm_usage_count);
1867
1868 kvm_usage_count--;
1869 if (!kvm_usage_count)
1870 on_each_cpu(hardware_disable, NULL, 1);
1871}
1872
1873static void hardware_disable_all(void)
1874{
1875 spin_lock(&kvm_lock);
1876 hardware_disable_all_nolock();
1877 spin_unlock(&kvm_lock);
1878}
1879
1880static int hardware_enable_all(void)
1881{
1882 int r = 0;
1883
1884 spin_lock(&kvm_lock);
1885
1886 kvm_usage_count++;
1887 if (kvm_usage_count == 1) {
1888 atomic_set(&hardware_enable_failed, 0);
1889 on_each_cpu(hardware_enable, NULL, 1);
1890
1891 if (atomic_read(&hardware_enable_failed)) {
1892 hardware_disable_all_nolock();
1893 r = -EBUSY;
1894 }
1895 }
1896
1897 spin_unlock(&kvm_lock);
1898
1899 return r;
1900}
1901
Avi Kivity774c47f2007-02-12 00:54:47 -08001902static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1903 void *v)
1904{
1905 int cpu = (long)v;
1906
Alexander Graf10474ae2009-09-15 11:37:46 +02001907 if (!kvm_usage_count)
1908 return NOTIFY_OK;
1909
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001910 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001911 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001912 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001913 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1914 cpu);
1915 hardware_disable(NULL);
1916 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001917 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001918 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1919 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001920 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001921 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001922 case CPU_ONLINE:
1923 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1924 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001925 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001926 break;
1927 }
1928 return NOTIFY_OK;
1929}
1930
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001931
1932asmlinkage void kvm_handle_fault_on_reboot(void)
1933{
1934 if (kvm_rebooting)
1935 /* spin while reset goes on */
1936 while (true)
1937 ;
1938 /* Fault while not rebooting. We want the trace. */
1939 BUG();
1940}
1941EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1942
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001943static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001944 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001945{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001946 /*
1947 * Some (well, at least mine) BIOSes hang on reboot if
1948 * in vmx root mode.
1949 *
1950 * And Intel TXT required VMX off for all cpu when system shutdown.
1951 */
1952 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1953 kvm_rebooting = true;
1954 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001955 return NOTIFY_OK;
1956}
1957
1958static struct notifier_block kvm_reboot_notifier = {
1959 .notifier_call = kvm_reboot,
1960 .priority = 0,
1961};
1962
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001963static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001964{
1965 int i;
1966
1967 for (i = 0; i < bus->dev_count; i++) {
1968 struct kvm_io_device *pos = bus->devs[i];
1969
1970 kvm_iodevice_destructor(pos);
1971 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001972 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001973}
1974
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001975/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001976int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001977 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001978{
1979 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001980 struct kvm_io_bus *bus = rcu_dereference(kvm->buses[bus_idx]);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001981 for (i = 0; i < bus->dev_count; i++)
1982 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1983 return 0;
1984 return -EOPNOTSUPP;
1985}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001986
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001987/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001988int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
1989 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001990{
1991 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02001992 struct kvm_io_bus *bus = rcu_dereference(kvm->buses[bus_idx]);
1993
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03001994 for (i = 0; i < bus->dev_count; i++)
1995 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
1996 return 0;
1997 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001998}
1999
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002000/* Caller must have write lock on slots_lock. */
2001int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2002 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002003{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002004 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002005
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002006 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002007 if (bus->dev_count > NR_IOBUS_DEVS-1)
2008 return -ENOSPC;
2009
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002010 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2011 if (!new_bus)
2012 return -ENOMEM;
2013 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2014 new_bus->devs[new_bus->dev_count++] = dev;
2015 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2016 synchronize_srcu_expedited(&kvm->srcu);
2017 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002018
2019 return 0;
2020}
2021
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002022/* Caller must have write lock on slots_lock. */
2023int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2024 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002025{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002026 int i, r;
2027 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002028
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002029 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2030 if (!new_bus)
2031 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002032
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002033 bus = kvm->buses[bus_idx];
2034 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2035
2036 r = -ENOENT;
2037 for (i = 0; i < new_bus->dev_count; i++)
2038 if (new_bus->devs[i] == dev) {
2039 r = 0;
2040 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002041 break;
2042 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002043
2044 if (r) {
2045 kfree(new_bus);
2046 return r;
2047 }
2048
2049 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2050 synchronize_srcu_expedited(&kvm->srcu);
2051 kfree(bus);
2052 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002053}
2054
Avi Kivity774c47f2007-02-12 00:54:47 -08002055static struct notifier_block kvm_cpu_notifier = {
2056 .notifier_call = kvm_cpu_hotplug,
2057 .priority = 20, /* must be > scheduler priority */
2058};
2059
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002060static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002061{
2062 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002063 struct kvm *kvm;
2064
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002065 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002066 spin_lock(&kvm_lock);
2067 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002068 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002069 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002070 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002071}
2072
2073DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2074
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002075static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002076{
2077 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002078 struct kvm *kvm;
2079 struct kvm_vcpu *vcpu;
2080 int i;
2081
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002082 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002083 spin_lock(&kvm_lock);
2084 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002085 kvm_for_each_vcpu(i, vcpu, kvm)
2086 *val += *(u32 *)((void *)vcpu + offset);
2087
Avi Kivity1165f5f2007-04-19 17:27:43 +03002088 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002089 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002090}
2091
Avi Kivityba1389b2007-11-18 16:24:12 +02002092DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2093
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002094static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002095 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2096 [KVM_STAT_VM] = &vm_stat_fops,
2097};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002098
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002099static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100{
2101 struct kvm_stats_debugfs_item *p;
2102
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002103 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002105 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002106 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002107 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108}
2109
2110static void kvm_exit_debug(void)
2111{
2112 struct kvm_stats_debugfs_item *p;
2113
2114 for (p = debugfs_entries; p->name; ++p)
2115 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002116 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117}
2118
Avi Kivity59ae6c62007-02-12 00:54:48 -08002119static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2120{
Alexander Graf10474ae2009-09-15 11:37:46 +02002121 if (kvm_usage_count)
2122 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002123 return 0;
2124}
2125
2126static int kvm_resume(struct sys_device *dev)
2127{
Alexander Graf10474ae2009-09-15 11:37:46 +02002128 if (kvm_usage_count)
2129 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002130 return 0;
2131}
2132
2133static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01002134 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002135 .suspend = kvm_suspend,
2136 .resume = kvm_resume,
2137};
2138
2139static struct sys_device kvm_sysdev = {
2140 .id = 0,
2141 .cls = &kvm_sysdev_class,
2142};
2143
Izik Eiduscea7bb22007-10-17 19:17:48 +02002144struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002145pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146
Avi Kivity15ad7142007-07-11 18:17:21 +03002147static inline
2148struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2149{
2150 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2151}
2152
2153static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2154{
2155 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2156
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002157 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002158}
2159
2160static void kvm_sched_out(struct preempt_notifier *pn,
2161 struct task_struct *next)
2162{
2163 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2164
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002165 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002166}
2167
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002168int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002169 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
2171 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002172 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002174 r = kvm_arch_init(opaque);
2175 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002176 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002177
2178 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2179
2180 if (bad_page == NULL) {
2181 r = -ENOMEM;
2182 goto out;
2183 }
2184
Anthony Liguori35149e22008-04-02 14:46:56 -05002185 bad_pfn = page_to_pfn(bad_page);
2186
Avi Kivity8437a6172009-06-06 14:52:35 -07002187 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302188 r = -ENOMEM;
2189 goto out_free_0;
2190 }
2191
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002192 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302194 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195
Yang, Sheng002c7f72007-07-31 14:23:01 +03002196 for_each_online_cpu(cpu) {
2197 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002198 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002199 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002200 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002201 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002202 }
2203
Avi Kivity774c47f2007-02-12 00:54:47 -08002204 r = register_cpu_notifier(&kvm_cpu_notifier);
2205 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002206 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 register_reboot_notifier(&kvm_reboot_notifier);
2208
Avi Kivity59ae6c62007-02-12 00:54:48 -08002209 r = sysdev_class_register(&kvm_sysdev_class);
2210 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002211 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002212
2213 r = sysdev_register(&kvm_sysdev);
2214 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002215 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002216
Rusty Russellc16f8622007-07-30 21:12:19 +10002217 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2218 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002219 __alignof__(struct kvm_vcpu),
2220 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002221 if (!kvm_vcpu_cache) {
2222 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002223 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002224 }
2225
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002227 kvm_vm_fops.owner = module;
2228 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229
2230 r = misc_register(&kvm_dev);
2231 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002232 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 goto out_free;
2234 }
2235
Avi Kivity15ad7142007-07-11 18:17:21 +03002236 kvm_preempt_ops.sched_in = kvm_sched_in;
2237 kvm_preempt_ops.sched_out = kvm_sched_out;
2238
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002239 kvm_init_debug();
2240
Avi Kivityc7addb92007-09-16 18:58:32 +02002241 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242
2243out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002244 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002245out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002246 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002247out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002248 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002249out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002251 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002252out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002253out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002254 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302255out_free_0a:
2256 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002257out_free_0:
2258 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002259out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002260 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002261out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 return r;
2263}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002264EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002266void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002268 tracepoint_synchronize_unregister();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002269 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002271 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002272 sysdev_unregister(&kvm_sysdev);
2273 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002275 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002276 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002277 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002278 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302279 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002280 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002282EXPORT_SYMBOL_GPL(kvm_exit);