blob: 7f686251f71109e0dd9fd7c19194862b20b458c3 [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Hollis Blancharde2174022007-12-03 15:30:24 -060019#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/percpu.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>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010048#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040052#include <asm/io.h>
53#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020054#include <asm/pgtable.h>
Alexander Grafc8240bd2009-10-30 05:47:26 +000055#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Laurent Vivier5f94c172008-05-30 16:05:54 +020057#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020058#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020059
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060#define CREATE_TRACE_POINTS
61#include <trace/events/kvm.h>
62
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Marcelo Tosattifa40a822009-06-04 15:08:24 -030066/*
67 * Ordering of locks:
68 *
Sheng Yangfae3a352009-12-15 10:28:07 +080069 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030070 */
71
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072DEFINE_SPINLOCK(kvm_lock);
73LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080074
Rusty Russell7f59f492008-12-07 21:25:45 +103075static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020076static int kvm_usage_count = 0;
77static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030078
Rusty Russellc16f8622007-07-30 21:12:19 +100079struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030081
Avi Kivity15ad7142007-07-11 18:17:21 +030082static __read_mostly struct preempt_ops kvm_preempt_ops;
83
Hollis Blanchard76f7c872008-04-15 16:05:42 -050084struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivitybccf2152007-02-21 18:04:26 +020086static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020088static int hardware_enable_all(void);
89static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020090
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020091static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
92
Avi Kivityb7c41452010-12-02 17:52:50 +020093bool kvm_rebooting;
94EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +030095
Marcelo Tosatti54dee992009-06-11 12:07:44 -030096static bool largepages_enabled = true;
97
Gleb Natapovfa7bff82010-07-07 20:16:44 +030098static struct page *hwpoison_page;
99static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +0800100
Gleb Natapovedba23e2010-07-07 20:16:45 +0300101static struct page *fault_page;
102static pfn_t fault_pfn;
103
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800104inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300105{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100106 if (pfn_valid(pfn)) {
107 struct page *page = compound_head(pfn_to_page(pfn));
108 return PageReserved(page);
109 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300110
111 return true;
112}
113
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114/*
115 * Switches to specified vcpu, until a matching vcpu_put()
116 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200117void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118{
Avi Kivity15ad7142007-07-11 18:17:21 +0300119 int cpu;
120
Avi Kivitybccf2152007-02-21 18:04:26 +0200121 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300122 cpu = get_cpu();
123 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200124 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300125 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200126}
127
Carsten Otte313a3dc2007-10-11 19:16:52 +0200128void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129{
Avi Kivity15ad7142007-07-11 18:17:21 +0300130 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200131 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300132 preempt_notifier_unregister(&vcpu->preempt_notifier);
133 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 mutex_unlock(&vcpu->mutex);
135}
136
Avi Kivityd9e368d2007-06-07 19:18:30 +0300137static void ack_flush(void *_completed)
138{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300139}
140
Rusty Russell49846892008-12-08 20:26:24 +1030141static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300142{
Avi Kivity597a5f52008-07-20 14:24:22 +0300143 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030144 cpumask_var_t cpus;
145 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300146 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300147
Li Zefan79f55992009-06-15 14:58:26 +0800148 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030149
Avi Kivity70e335e2010-02-18 11:25:22 +0200150 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200151 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300152 kvm_for_each_vcpu(i, vcpu, kvm) {
Avi Kivitya8eeb042010-05-10 12:34:53 +0300153 if (kvm_make_check_request(req, vcpu))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300154 continue;
155 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030156 if (cpus != NULL && cpu != -1 && cpu != me)
157 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300158 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030159 if (unlikely(cpus == NULL))
160 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
161 else if (!cpumask_empty(cpus))
162 smp_call_function_many(cpus, ack_flush, NULL, 1);
163 else
164 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200165 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030166 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030167 return called;
168}
169
170void kvm_flush_remote_tlbs(struct kvm *kvm)
171{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800172 int dirty_count = kvm->tlbs_dirty;
173
174 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030175 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
176 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800177 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300178}
179
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500180void kvm_reload_remote_mmus(struct kvm *kvm)
181{
Rusty Russell49846892008-12-08 20:26:24 +1030182 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500183}
184
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000185int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
186{
187 struct page *page;
188 int r;
189
190 mutex_init(&vcpu->mutex);
191 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000192 vcpu->kvm = kvm;
193 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300194 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200195 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000196
197 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
198 if (!page) {
199 r = -ENOMEM;
200 goto fail;
201 }
202 vcpu->run = page_address(page);
203
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800204 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000205 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800206 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000207 return 0;
208
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000209fail_free_run:
210 free_page((unsigned long)vcpu->run);
211fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000212 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000213}
214EXPORT_SYMBOL_GPL(kvm_vcpu_init);
215
216void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
217{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800218 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000219 free_page((unsigned long)vcpu->run);
220}
221EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
222
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200223#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
224static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
225{
226 return container_of(mn, struct kvm, mmu_notifier);
227}
228
229static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
230 struct mm_struct *mm,
231 unsigned long address)
232{
233 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200234 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200235
236 /*
237 * When ->invalidate_page runs, the linux pte has been zapped
238 * already but the page is still allocated until
239 * ->invalidate_page returns. So if we increase the sequence
240 * here the kvm page fault will notice if the spte can't be
241 * established because the page is going to be freed. If
242 * instead the kvm page fault establishes the spte before
243 * ->invalidate_page runs, kvm_unmap_hva will release it
244 * before returning.
245 *
246 * The sequence increase only need to be seen at spin_unlock
247 * time, and not at spin_lock time.
248 *
249 * Increasing the sequence after the spin_unlock would be
250 * unsafe because the kvm page fault could then establish the
251 * pte after kvm_unmap_hva returned, without noticing the page
252 * is going to be freed.
253 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200254 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200255 spin_lock(&kvm->mmu_lock);
256 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800257 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200258 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200259 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200260
261 /* we've to flush the tlb before the pages can be freed */
262 if (need_tlb_flush)
263 kvm_flush_remote_tlbs(kvm);
264
265}
266
Izik Eidus3da0dd42009-09-23 21:47:18 +0300267static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
268 struct mm_struct *mm,
269 unsigned long address,
270 pte_t pte)
271{
272 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200273 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300274
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200275 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300276 spin_lock(&kvm->mmu_lock);
277 kvm->mmu_notifier_seq++;
278 kvm_set_spte_hva(kvm, address, pte);
279 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200280 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300281}
282
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200283static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
284 struct mm_struct *mm,
285 unsigned long start,
286 unsigned long end)
287{
288 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200289 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200290
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200291 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200292 spin_lock(&kvm->mmu_lock);
293 /*
294 * The count increase must become visible at unlock time as no
295 * spte can be established without taking the mmu_lock and
296 * count is also read inside the mmu_lock critical section.
297 */
298 kvm->mmu_notifier_count++;
299 for (; start < end; start += PAGE_SIZE)
300 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800301 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200302 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200303 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200304
305 /* we've to flush the tlb before the pages can be freed */
306 if (need_tlb_flush)
307 kvm_flush_remote_tlbs(kvm);
308}
309
310static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
311 struct mm_struct *mm,
312 unsigned long start,
313 unsigned long end)
314{
315 struct kvm *kvm = mmu_notifier_to_kvm(mn);
316
317 spin_lock(&kvm->mmu_lock);
318 /*
319 * This sequence increase will notify the kvm page fault that
320 * the page that is going to be mapped in the spte could have
321 * been freed.
322 */
323 kvm->mmu_notifier_seq++;
324 /*
325 * The above sequence increase must be visible before the
326 * below count decrease but both values are read by the kvm
327 * page fault under mmu_lock spinlock so we don't need to add
328 * a smb_wmb() here in between the two.
329 */
330 kvm->mmu_notifier_count--;
331 spin_unlock(&kvm->mmu_lock);
332
333 BUG_ON(kvm->mmu_notifier_count < 0);
334}
335
336static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
337 struct mm_struct *mm,
338 unsigned long address)
339{
340 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200341 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200342
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200343 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200344 spin_lock(&kvm->mmu_lock);
345 young = kvm_age_hva(kvm, address);
346 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200347 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200348
349 if (young)
350 kvm_flush_remote_tlbs(kvm);
351
352 return young;
353}
354
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100355static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
356 struct mm_struct *mm)
357{
358 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800359 int idx;
360
361 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100362 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800363 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100364}
365
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200366static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
367 .invalidate_page = kvm_mmu_notifier_invalidate_page,
368 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
369 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
370 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300371 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100372 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200373};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200374
375static int kvm_init_mmu_notifier(struct kvm *kvm)
376{
377 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
378 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
379}
380
381#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
382
383static int kvm_init_mmu_notifier(struct kvm *kvm)
384{
385 return 0;
386}
387
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200388#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
389
Avi Kivityf17abe92007-02-21 19:28:04 +0200390static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100392 int r, i;
393 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100395 if (!kvm)
396 return ERR_PTR(-ENOMEM);
397
398 r = kvm_arch_init_vm(kvm);
399 if (r)
400 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200401
402 r = hardware_enable_all();
403 if (r)
404 goto out_err_nodisable;
405
Avi Kivity75858a82009-01-04 17:10:50 +0200406#ifdef CONFIG_HAVE_KVM_IRQCHIP
407 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300408 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200409#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200411 r = -ENOMEM;
412 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
413 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100414 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200415 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100416 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200417 for (i = 0; i < KVM_NR_BUSES; i++) {
418 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
419 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100420 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200421 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200422 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200423
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200424 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100425 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200426 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200427
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200428 kvm->mm = current->mm;
429 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500430 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200431 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400432 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800433 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300434 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200435 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300436 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000437 spin_lock(&kvm_lock);
438 list_add(&kvm->vm_list, &vm_list);
439 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100440
Avi Kivityf17abe92007-02-21 19:28:04 +0200441 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200442
443out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100444 cleanup_srcu_struct(&kvm->srcu);
445out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200446 hardware_disable_all();
447out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200448 for (i = 0; i < KVM_NR_BUSES; i++)
449 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200450 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100451 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200452 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200453}
454
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900455static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
456{
457 if (!memslot->dirty_bitmap)
458 return;
459
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900460 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
461 vfree(memslot->dirty_bitmap_head);
462 else
463 kfree(memslot->dirty_bitmap_head);
464
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900465 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900466 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469/*
470 * Free any memory in @free but not in @dont.
471 */
472static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
473 struct kvm_memory_slot *dont)
474{
Joerg Roedelec04b262009-06-19 15:16:23 +0200475 int i;
476
Izik Eidus290fc382007-09-27 14:11:22 +0200477 if (!dont || free->rmap != dont->rmap)
478 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479
480 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900481 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482
Joerg Roedelec04b262009-06-19 15:16:23 +0200483
484 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
485 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
486 vfree(free->lpage_info[i]);
487 free->lpage_info[i] = NULL;
488 }
489 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300490
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500492 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493}
494
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800495void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496{
497 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200498 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200500 for (i = 0; i < slots->nmemslots; ++i)
501 kvm_free_physmem_slot(&slots->memslots[i], NULL);
502
503 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504}
505
Avi Kivityf17abe92007-02-21 19:28:04 +0200506static void kvm_destroy_vm(struct kvm *kvm)
507{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200508 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200509 struct mm_struct *mm = kvm->mm;
510
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800511 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800512 spin_lock(&kvm_lock);
513 list_del(&kvm->vm_list);
514 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200515 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200516 for (i = 0; i < KVM_NR_BUSES; i++)
517 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200518 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200519#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
520 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200521#else
522 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200523#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800524 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100525 kvm_free_physmem(kvm);
526 cleanup_srcu_struct(&kvm->srcu);
527 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200528 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200529 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200530}
531
Izik Eidusd39f13b2008-03-30 16:01:25 +0300532void kvm_get_kvm(struct kvm *kvm)
533{
534 atomic_inc(&kvm->users_count);
535}
536EXPORT_SYMBOL_GPL(kvm_get_kvm);
537
538void kvm_put_kvm(struct kvm *kvm)
539{
540 if (atomic_dec_and_test(&kvm->users_count))
541 kvm_destroy_vm(kvm);
542}
543EXPORT_SYMBOL_GPL(kvm_put_kvm);
544
545
Avi Kivityf17abe92007-02-21 19:28:04 +0200546static int kvm_vm_release(struct inode *inode, struct file *filp)
547{
548 struct kvm *kvm = filp->private_data;
549
Gregory Haskins721eecb2009-05-20 10:30:49 -0400550 kvm_irqfd_release(kvm);
551
Izik Eidusd39f13b2008-03-30 16:01:25 +0300552 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553 return 0;
554}
555
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900556/*
557 * Allocation size is twice as large as the actual dirty bitmap size.
558 * This makes it possible to do double buffering: see x86's
559 * kvm_vm_ioctl_get_dirty_log().
560 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900561static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
562{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900563 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900564
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900565 if (dirty_bytes > PAGE_SIZE)
566 memslot->dirty_bitmap = vzalloc(dirty_bytes);
567 else
568 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
569
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900570 if (!memslot->dirty_bitmap)
571 return -ENOMEM;
572
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900573 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900574 return 0;
575}
576
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 * Allocate some memory and give it an address in the guest physical address
579 * space.
580 *
581 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800582 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500583 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800585int __kvm_set_memory_region(struct kvm *kvm,
586 struct kvm_userspace_memory_region *mem,
587 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200589 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200591 unsigned long npages;
592 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 struct kvm_memory_slot *memslot;
594 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200595 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596
597 r = -EINVAL;
598 /* General sanity checks */
599 if (mem->memory_size & (PAGE_SIZE - 1))
600 goto out;
601 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
602 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800603 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600604 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200605 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606 goto out;
607 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
608 goto out;
609
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200610 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
612 npages = mem->memory_size >> PAGE_SHIFT;
613
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900614 r = -EINVAL;
615 if (npages > KVM_MEM_MAX_NR_PAGES)
616 goto out;
617
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 if (!npages)
619 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
620
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621 new = old = *memslot;
622
Avi Kivitye36d96f2010-06-21 10:56:36 +0300623 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 new.base_gfn = base_gfn;
625 new.npages = npages;
626 new.flags = mem->flags;
627
628 /* Disallow changing a memory slot's size. */
629 r = -EINVAL;
630 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800631 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632
633 /* Check for overlaps */
634 r = -EEXIST;
635 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200636 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200638 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 continue;
640 if (!((base_gfn + npages <= s->base_gfn) ||
641 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800642 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645 /* Free page dirty bitmap if unneeded */
646 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000647 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648
649 r = -ENOMEM;
650
651 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200652#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500653 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900654 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200655
656 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800657 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200658
Izik Eidus80b14b52007-10-25 11:54:04 +0200659 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200660 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200662 if (!npages)
663 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300664
Joerg Roedelec04b262009-06-19 15:16:23 +0200665 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200666 unsigned long ugfn;
667 unsigned long j;
668 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200669 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300670
Joerg Roedelec04b262009-06-19 15:16:23 +0200671 /* Avoid unused variable warning if no large pages */
672 (void)level;
673
674 if (new.lpage_info[i])
675 continue;
676
Joerg Roedel82855412010-07-01 16:00:11 +0200677 lpages = 1 + ((base_gfn + npages - 1)
678 >> KVM_HPAGE_GFN_SHIFT(level));
679 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200680
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900681 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200682
683 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300684 goto out_free;
685
Joerg Roedel82855412010-07-01 16:00:11 +0200686 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200687 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200688 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200689 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300690 ugfn = new.userspace_addr >> PAGE_SHIFT;
691 /*
692 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300693 * other, or if explicitly asked to, disable large page
694 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300695 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200696 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300697 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200698 for (j = 0; j < lpages; ++j)
699 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300700 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701
Joerg Roedelec04b262009-06-19 15:16:23 +0200702skip_lpage:
703
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 /* Allocate page dirty bitmap if needed */
705 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900706 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800707 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200708 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300709 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200710 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200712#else /* not defined CONFIG_S390 */
713 new.user_alloc = user_alloc;
714 if (user_alloc)
715 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200716#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200718 if (!npages) {
719 r = -ENOMEM;
720 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
721 if (!slots)
722 goto out_free;
723 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
724 if (mem->slot >= slots->nmemslots)
725 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200726 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200727 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
728
729 old_memslots = kvm->memslots;
730 rcu_assign_pointer(kvm->memslots, slots);
731 synchronize_srcu_expedited(&kvm->srcu);
732 /* From this point no new shadow pages pointing to a deleted
733 * memslot will be created.
734 *
735 * validation of sp->gfn happens in:
736 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
737 * - kvm_is_visible_gfn (mmu_check_roots)
738 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300739 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200740 kfree(old_memslots);
741 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300742
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200743 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
744 if (r)
745 goto out_free;
746
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200747 /* map the pages in iommu page table */
748 if (npages) {
749 r = kvm_iommu_map_pages(kvm, &new);
750 if (r)
751 goto out_free;
752 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200753
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200754 r = -ENOMEM;
755 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
756 if (!slots)
757 goto out_free;
758 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
759 if (mem->slot >= slots->nmemslots)
760 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200761 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200762
763 /* actual memory is freed via old in kvm_free_physmem_slot below */
764 if (!npages) {
765 new.rmap = NULL;
766 new.dirty_bitmap = NULL;
767 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
768 new.lpage_info[i] = NULL;
769 }
770
771 slots->memslots[mem->slot] = new;
772 old_memslots = kvm->memslots;
773 rcu_assign_pointer(kvm->memslots, slots);
774 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200776 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800777
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200778 kvm_free_physmem_slot(&old, &new);
779 kfree(old_memslots);
780
781 if (flush_shadow)
782 kvm_arch_flush_shadow(kvm);
783
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 return 0;
785
Sheng Yangf78e0e22007-10-29 09:40:42 +0800786out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 kvm_free_physmem_slot(&new, &old);
788out:
789 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200790
791}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800792EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
793
794int kvm_set_memory_region(struct kvm *kvm,
795 struct kvm_userspace_memory_region *mem,
796 int user_alloc)
797{
798 int r;
799
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200800 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800801 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200802 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800803 return r;
804}
Izik Eidus210c7c42007-10-24 23:52:57 +0200805EXPORT_SYMBOL_GPL(kvm_set_memory_region);
806
Carsten Otte1fe779f2007-10-29 16:08:35 +0100807int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
808 struct
809 kvm_userspace_memory_region *mem,
810 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200811{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200812 if (mem->slot >= KVM_MEMORY_SLOTS)
813 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200814 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815}
816
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800817int kvm_get_dirty_log(struct kvm *kvm,
818 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819{
820 struct kvm_memory_slot *memslot;
821 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900822 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 unsigned long any = 0;
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 r = -EINVAL;
826 if (log->slot >= KVM_MEMORY_SLOTS)
827 goto out;
828
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200829 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 r = -ENOENT;
831 if (!memslot->dirty_bitmap)
832 goto out;
833
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900834 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835
Uri Lublincd1a4a92007-02-22 16:43:09 +0200836 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 any = memslot->dirty_bitmap[i];
838
839 r = -EFAULT;
840 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
841 goto out;
842
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800843 if (any)
844 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845
846 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 return r;
849}
850
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300851void kvm_disable_largepages(void)
852{
853 largepages_enabled = false;
854}
855EXPORT_SYMBOL_GPL(kvm_disable_largepages);
856
Izik Eiduscea7bb22007-10-17 19:17:48 +0200857int is_error_page(struct page *page)
858{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300859 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200860}
861EXPORT_SYMBOL_GPL(is_error_page);
862
Anthony Liguori35149e22008-04-02 14:46:56 -0500863int is_error_pfn(pfn_t pfn)
864{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300865 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500866}
867EXPORT_SYMBOL_GPL(is_error_pfn);
868
Huang Yingbf998152010-05-31 14:28:19 +0800869int is_hwpoison_pfn(pfn_t pfn)
870{
871 return pfn == hwpoison_pfn;
872}
873EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
874
Gleb Natapovedba23e2010-07-07 20:16:45 +0300875int is_fault_pfn(pfn_t pfn)
876{
877 return pfn == fault_pfn;
878}
879EXPORT_SYMBOL_GPL(is_fault_pfn);
880
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200881static inline unsigned long bad_hva(void)
882{
883 return PAGE_OFFSET;
884}
885
886int kvm_is_error_hva(unsigned long addr)
887{
888 return addr == bad_hva();
889}
890EXPORT_SYMBOL_GPL(kvm_is_error_hva);
891
Gleb Natapov49c77542010-10-18 15:22:23 +0200892static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
893 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894{
895 int i;
896
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200897 for (i = 0; i < slots->nmemslots; ++i) {
898 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899
900 if (gfn >= memslot->base_gfn
901 && gfn < memslot->base_gfn + memslot->npages)
902 return memslot;
903 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000904 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905}
Gleb Natapov49c77542010-10-18 15:22:23 +0200906
907struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
908{
909 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
910}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300911EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Izik Eiduse0d62c72007-10-24 23:57:46 +0200913int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
914{
915 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800916 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200917
Izik Eiduse0d62c72007-10-24 23:57:46 +0200918 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200919 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200920
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200921 if (memslot->flags & KVM_MEMSLOT_INVALID)
922 continue;
923
Izik Eiduse0d62c72007-10-24 23:57:46 +0200924 if (gfn >= memslot->base_gfn
925 && gfn < memslot->base_gfn + memslot->npages)
926 return 1;
927 }
928 return 0;
929}
930EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
931
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100932unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
933{
934 struct vm_area_struct *vma;
935 unsigned long addr, size;
936
937 size = PAGE_SIZE;
938
939 addr = gfn_to_hva(kvm, gfn);
940 if (kvm_is_error_hva(addr))
941 return PAGE_SIZE;
942
943 down_read(&current->mm->mmap_sem);
944 vma = find_vma(current->mm, addr);
945 if (!vma)
946 goto out;
947
948 size = vma_kernel_pagesize(vma);
949
950out:
951 up_read(&current->mm->mmap_sem);
952
953 return size;
954}
955
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200956int memslot_id(struct kvm *kvm, gfn_t gfn)
957{
958 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800959 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200960 struct kvm_memory_slot *memslot = NULL;
961
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200962 for (i = 0; i < slots->nmemslots; ++i) {
963 memslot = &slots->memslots[i];
964
965 if (gfn >= memslot->base_gfn
966 && gfn < memslot->base_gfn + memslot->npages)
967 break;
968 }
969
970 return memslot - slots->memslots;
971}
972
Gleb Natapov49c77542010-10-18 15:22:23 +0200973static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +0800974 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +0200975{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200976 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200977 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +0800978
979 if (nr_pages)
980 *nr_pages = slot->npages - (gfn - slot->base_gfn);
981
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900982 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200983}
Xiao Guangrong48987782010-08-22 19:11:43 +0800984
985unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
986{
Gleb Natapov49c77542010-10-18 15:22:23 +0200987 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +0800988}
Sheng Yang0d150292008-04-25 21:44:50 +0800989EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200990
Gleb Natapov80300892010-10-19 18:13:41 +0200991static pfn_t get_fault_pfn(void)
992{
993 get_page(fault_page);
994 return fault_pfn;
995}
996
Gleb Natapovaf585b92010-10-14 11:22:46 +0200997static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200998 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300999{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001000 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001001 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001002 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001003
Gleb Natapovaf585b92010-10-14 11:22:46 +02001004 /* we can do it either atomically or asynchronously, not both */
1005 BUG_ON(atomic && async);
1006
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001007 BUG_ON(!write_fault && !writable);
1008
1009 if (writable)
1010 *writable = true;
1011
Gleb Natapovaf585b92010-10-14 11:22:46 +02001012 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001013 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001014
1015 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001016 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001017
1018 if (writable)
1019 *writable = write_fault;
1020
1021 npages = get_user_pages_fast(addr, 1, write_fault, page);
1022
1023 /* map read fault as writable if possible */
1024 if (unlikely(!write_fault) && npages == 1) {
1025 struct page *wpage[1];
1026
1027 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1028 if (npages == 1) {
1029 *writable = true;
1030 put_page(page[0]);
1031 page[0] = wpage[0];
1032 }
1033 npages = 1;
1034 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001035 }
Izik Eidus539cb662007-11-11 22:05:04 +02001036
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001037 if (unlikely(npages != 1)) {
1038 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001039
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001040 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001041 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001042
Huang Yingbbeb3402010-06-22 14:23:11 +08001043 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001044 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001045 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001046 get_page(hwpoison_page);
1047 return page_to_pfn(hwpoison_page);
1048 }
1049
Gleb Natapov80300892010-10-19 18:13:41 +02001050 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001051
Gleb Natapov80300892010-10-19 18:13:41 +02001052 if (vma == NULL)
1053 pfn = get_fault_pfn();
1054 else if ((vma->vm_flags & VM_PFNMAP)) {
1055 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1056 vma->vm_pgoff;
1057 BUG_ON(!kvm_is_mmio_pfn(pfn));
1058 } else {
1059 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001060 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001061 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001062 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001063 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001064 } else
1065 pfn = page_to_pfn(page[0]);
1066
1067 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001068}
1069
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001070pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1071{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001072 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001073}
1074EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1075
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001076static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1077 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001078{
1079 unsigned long addr;
1080
Gleb Natapovaf585b92010-10-14 11:22:46 +02001081 if (async)
1082 *async = false;
1083
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001084 addr = gfn_to_hva(kvm, gfn);
1085 if (kvm_is_error_hva(addr)) {
1086 get_page(bad_page);
1087 return page_to_pfn(bad_page);
1088 }
1089
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001090 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001091}
1092
1093pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1094{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001095 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001096}
1097EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1098
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001099pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1100 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001101{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001102 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001103}
1104EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1105
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001106pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1107{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001108 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001109}
Anthony Liguori35149e22008-04-02 14:46:56 -05001110EXPORT_SYMBOL_GPL(gfn_to_pfn);
1111
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001112pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1113 bool *writable)
1114{
1115 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1116}
1117EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1118
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001119pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1120 struct kvm_memory_slot *slot, gfn_t gfn)
1121{
1122 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001123 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001124}
1125
Xiao Guangrong48987782010-08-22 19:11:43 +08001126int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1127 int nr_pages)
1128{
1129 unsigned long addr;
1130 gfn_t entry;
1131
Gleb Natapov49c77542010-10-18 15:22:23 +02001132 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001133 if (kvm_is_error_hva(addr))
1134 return -1;
1135
1136 if (entry < nr_pages)
1137 return 0;
1138
1139 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1140}
1141EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1142
Anthony Liguori35149e22008-04-02 14:46:56 -05001143struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1144{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001145 pfn_t pfn;
1146
1147 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001148 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001149 return pfn_to_page(pfn);
1150
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001151 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001152
1153 get_page(bad_page);
1154 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001155}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001156
Avi Kivity954bbbc22007-03-30 14:02:32 +03001157EXPORT_SYMBOL_GPL(gfn_to_page);
1158
Izik Eidusb4231d62007-11-20 11:49:33 +02001159void kvm_release_page_clean(struct page *page)
1160{
Anthony Liguori35149e22008-04-02 14:46:56 -05001161 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001162}
1163EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1164
Anthony Liguori35149e22008-04-02 14:46:56 -05001165void kvm_release_pfn_clean(pfn_t pfn)
1166{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001167 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001168 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001169}
1170EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1171
Izik Eidusb4231d62007-11-20 11:49:33 +02001172void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001173{
Anthony Liguori35149e22008-04-02 14:46:56 -05001174 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001175}
Izik Eidusb4231d62007-11-20 11:49:33 +02001176EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001177
Anthony Liguori35149e22008-04-02 14:46:56 -05001178void kvm_release_pfn_dirty(pfn_t pfn)
1179{
1180 kvm_set_pfn_dirty(pfn);
1181 kvm_release_pfn_clean(pfn);
1182}
1183EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1184
1185void kvm_set_page_dirty(struct page *page)
1186{
1187 kvm_set_pfn_dirty(page_to_pfn(page));
1188}
1189EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1190
1191void kvm_set_pfn_dirty(pfn_t pfn)
1192{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001193 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001194 struct page *page = pfn_to_page(pfn);
1195 if (!PageReserved(page))
1196 SetPageDirty(page);
1197 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001198}
1199EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1200
1201void kvm_set_pfn_accessed(pfn_t pfn)
1202{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001203 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001204 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001205}
1206EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1207
1208void kvm_get_pfn(pfn_t pfn)
1209{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001210 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001211 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001212}
1213EXPORT_SYMBOL_GPL(kvm_get_pfn);
1214
Izik Eidus195aefd2007-10-01 22:14:18 +02001215static int next_segment(unsigned long len, int offset)
1216{
1217 if (len > PAGE_SIZE - offset)
1218 return PAGE_SIZE - offset;
1219 else
1220 return len;
1221}
1222
1223int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1224 int len)
1225{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001226 int r;
1227 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001228
Izik Eiduse0506bc2007-11-11 22:10:22 +02001229 addr = gfn_to_hva(kvm, gfn);
1230 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001231 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001232 r = copy_from_user(data, (void __user *)addr + offset, len);
1233 if (r)
1234 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001235 return 0;
1236}
1237EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1238
1239int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1240{
1241 gfn_t gfn = gpa >> PAGE_SHIFT;
1242 int seg;
1243 int offset = offset_in_page(gpa);
1244 int ret;
1245
1246 while ((seg = next_segment(len, offset)) != 0) {
1247 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1248 if (ret < 0)
1249 return ret;
1250 offset = 0;
1251 len -= seg;
1252 data += seg;
1253 ++gfn;
1254 }
1255 return 0;
1256}
1257EXPORT_SYMBOL_GPL(kvm_read_guest);
1258
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001259int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1260 unsigned long len)
1261{
1262 int r;
1263 unsigned long addr;
1264 gfn_t gfn = gpa >> PAGE_SHIFT;
1265 int offset = offset_in_page(gpa);
1266
1267 addr = gfn_to_hva(kvm, gfn);
1268 if (kvm_is_error_hva(addr))
1269 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001270 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001271 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001272 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001273 if (r)
1274 return -EFAULT;
1275 return 0;
1276}
1277EXPORT_SYMBOL(kvm_read_guest_atomic);
1278
Izik Eidus195aefd2007-10-01 22:14:18 +02001279int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1280 int offset, int len)
1281{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001282 int r;
1283 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001284
Izik Eiduse0506bc2007-11-11 22:10:22 +02001285 addr = gfn_to_hva(kvm, gfn);
1286 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001287 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001288 r = copy_to_user((void __user *)addr + offset, data, len);
1289 if (r)
1290 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001291 mark_page_dirty(kvm, gfn);
1292 return 0;
1293}
1294EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1295
1296int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1297 unsigned long len)
1298{
1299 gfn_t gfn = gpa >> PAGE_SHIFT;
1300 int seg;
1301 int offset = offset_in_page(gpa);
1302 int ret;
1303
1304 while ((seg = next_segment(len, offset)) != 0) {
1305 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1306 if (ret < 0)
1307 return ret;
1308 offset = 0;
1309 len -= seg;
1310 data += seg;
1311 ++gfn;
1312 }
1313 return 0;
1314}
1315
Gleb Natapov49c77542010-10-18 15:22:23 +02001316int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1317 gpa_t gpa)
1318{
1319 struct kvm_memslots *slots = kvm_memslots(kvm);
1320 int offset = offset_in_page(gpa);
1321 gfn_t gfn = gpa >> PAGE_SHIFT;
1322
1323 ghc->gpa = gpa;
1324 ghc->generation = slots->generation;
1325 ghc->memslot = __gfn_to_memslot(slots, gfn);
1326 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1327 if (!kvm_is_error_hva(ghc->hva))
1328 ghc->hva += offset;
1329 else
1330 return -EFAULT;
1331
1332 return 0;
1333}
1334EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1335
1336int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1337 void *data, unsigned long len)
1338{
1339 struct kvm_memslots *slots = kvm_memslots(kvm);
1340 int r;
1341
1342 if (slots->generation != ghc->generation)
1343 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1344
1345 if (kvm_is_error_hva(ghc->hva))
1346 return -EFAULT;
1347
1348 r = copy_to_user((void __user *)ghc->hva, data, len);
1349 if (r)
1350 return -EFAULT;
1351 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1352
1353 return 0;
1354}
1355EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1356
Izik Eidus195aefd2007-10-01 22:14:18 +02001357int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1358{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001359 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1360 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001361}
1362EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1363
1364int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1365{
1366 gfn_t gfn = gpa >> PAGE_SHIFT;
1367 int seg;
1368 int offset = offset_in_page(gpa);
1369 int ret;
1370
1371 while ((seg = next_segment(len, offset)) != 0) {
1372 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1373 if (ret < 0)
1374 return ret;
1375 offset = 0;
1376 len -= seg;
1377 ++gfn;
1378 }
1379 return 0;
1380}
1381EXPORT_SYMBOL_GPL(kvm_clear_guest);
1382
Gleb Natapov49c77542010-10-18 15:22:23 +02001383void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1384 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001386 if (memslot && memslot->dirty_bitmap) {
1387 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001389 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 }
1391}
1392
Gleb Natapov49c77542010-10-18 15:22:23 +02001393void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1394{
1395 struct kvm_memory_slot *memslot;
1396
1397 memslot = gfn_to_memslot(kvm, gfn);
1398 mark_page_dirty_in_slot(kvm, memslot, gfn);
1399}
1400
Eddie Dongb6958ce2007-07-18 12:15:21 +03001401/*
1402 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1403 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001404void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001405{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001406 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001407
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001408 for (;;) {
1409 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001410
Gleb Natapova1b37102009-07-09 15:33:52 +03001411 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001412 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001413 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001414 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001415 if (kvm_cpu_has_pending_timer(vcpu))
1416 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001417 if (signal_pending(current))
1418 break;
1419
Eddie Dongb6958ce2007-07-18 12:15:21 +03001420 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001421 }
1422
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001423 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001424}
1425
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426void kvm_resched(struct kvm_vcpu *vcpu)
1427{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001428 if (!need_resched())
1429 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431}
1432EXPORT_SYMBOL_GPL(kvm_resched);
1433
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001434void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1435{
1436 ktime_t expires;
1437 DEFINE_WAIT(wait);
1438
1439 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1440
1441 /* Sleep for 100 us, and hope lock-holder got scheduled */
1442 expires = ktime_add_ns(ktime_get(), 100000UL);
1443 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1444
1445 finish_wait(&vcpu->wq, &wait);
1446}
1447EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1448
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001449static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001450{
1451 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001452 struct page *page;
1453
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001454 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001455 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001456#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001457 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001458 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001459#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001460#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1461 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1462 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1463#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001464 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001465 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001466 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001467 vmf->page = page;
1468 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001469}
1470
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001471static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001472 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001473};
1474
1475static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1476{
1477 vma->vm_ops = &kvm_vcpu_vm_ops;
1478 return 0;
1479}
1480
Avi Kivitybccf2152007-02-21 18:04:26 +02001481static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1482{
1483 struct kvm_vcpu *vcpu = filp->private_data;
1484
Al Viro66c0b392008-04-19 20:33:56 +01001485 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001486 return 0;
1487}
1488
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001489static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001490 .release = kvm_vcpu_release,
1491 .unlocked_ioctl = kvm_vcpu_ioctl,
1492 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001493 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001494 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001495};
1496
1497/*
1498 * Allocates an inode for the vcpu.
1499 */
1500static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1501{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001502 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001503}
1504
Avi Kivityc5ea7662007-02-20 18:41:05 +02001505/*
1506 * Creates some virtual cpus. Good luck creating more than one.
1507 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001508static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001509{
1510 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001511 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001512
Gleb Natapov73880c82009-06-09 15:56:28 +03001513 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001514 if (IS_ERR(vcpu))
1515 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001516
Avi Kivity15ad7142007-07-11 18:17:21 +03001517 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1518
Avi Kivity26e52152007-11-20 15:30:24 +02001519 r = kvm_arch_vcpu_setup(vcpu);
1520 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001521 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001522
Shaohua Li11ec2802007-07-23 14:51:37 +08001523 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001524 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1525 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001526 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001527 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001528
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001529 kvm_for_each_vcpu(r, v, kvm)
1530 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001531 r = -EEXIST;
1532 goto vcpu_destroy;
1533 }
1534
1535 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001536
1537 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001538 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001539 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001540 if (r < 0) {
1541 kvm_put_kvm(kvm);
1542 goto vcpu_destroy;
1543 }
1544
1545 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1546 smp_wmb();
1547 atomic_inc(&kvm->online_vcpus);
1548
1549#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1550 if (kvm->bsp_vcpu_id == id)
1551 kvm->bsp_vcpu = vcpu;
1552#endif
1553 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001554 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001555
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001556vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001557 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001558 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001559 return r;
1560}
1561
Avi Kivity1961d272007-03-05 19:46:05 +02001562static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1563{
1564 if (sigset) {
1565 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1566 vcpu->sigset_active = 1;
1567 vcpu->sigset = *sigset;
1568 } else
1569 vcpu->sigset_active = 0;
1570 return 0;
1571}
1572
Avi Kivitybccf2152007-02-21 18:04:26 +02001573static long kvm_vcpu_ioctl(struct file *filp,
1574 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575{
Avi Kivitybccf2152007-02-21 18:04:26 +02001576 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001577 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001578 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001579 struct kvm_fpu *fpu = NULL;
1580 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001582 if (vcpu->kvm->mm != current->mm)
1583 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001584
1585#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1586 /*
1587 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1588 * so vcpu_load() would break it.
1589 */
1590 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1591 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1592#endif
1593
1594
1595 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001597 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001598 r = -EINVAL;
1599 if (arg)
1600 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001601 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001602 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001605 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001607 r = -ENOMEM;
1608 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1609 if (!kvm_regs)
1610 goto out;
1611 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001613 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001615 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1616 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001618out_free1:
1619 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620 break;
1621 }
1622 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001623 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001625 r = -ENOMEM;
1626 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1627 if (!kvm_regs)
1628 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001630 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1631 goto out_free2;
1632 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001634 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001636out_free2:
1637 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638 break;
1639 }
1640 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001641 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1642 r = -ENOMEM;
1643 if (!kvm_sregs)
1644 goto out;
1645 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 if (r)
1647 goto out;
1648 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001649 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650 goto out;
1651 r = 0;
1652 break;
1653 }
1654 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001655 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1656 r = -ENOMEM;
1657 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001659 r = -EFAULT;
1660 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1661 goto out;
1662 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 if (r)
1664 goto out;
1665 r = 0;
1666 break;
1667 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001668 case KVM_GET_MP_STATE: {
1669 struct kvm_mp_state mp_state;
1670
1671 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1672 if (r)
1673 goto out;
1674 r = -EFAULT;
1675 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1676 goto out;
1677 r = 0;
1678 break;
1679 }
1680 case KVM_SET_MP_STATE: {
1681 struct kvm_mp_state mp_state;
1682
1683 r = -EFAULT;
1684 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1685 goto out;
1686 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1687 if (r)
1688 goto out;
1689 r = 0;
1690 break;
1691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 case KVM_TRANSLATE: {
1693 struct kvm_translation tr;
1694
1695 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001696 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001698 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 if (r)
1700 goto out;
1701 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001702 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 goto out;
1704 r = 0;
1705 break;
1706 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001707 case KVM_SET_GUEST_DEBUG: {
1708 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709
1710 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001711 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001713 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 if (r)
1715 goto out;
1716 r = 0;
1717 break;
1718 }
Avi Kivity1961d272007-03-05 19:46:05 +02001719 case KVM_SET_SIGNAL_MASK: {
1720 struct kvm_signal_mask __user *sigmask_arg = argp;
1721 struct kvm_signal_mask kvm_sigmask;
1722 sigset_t sigset, *p;
1723
1724 p = NULL;
1725 if (argp) {
1726 r = -EFAULT;
1727 if (copy_from_user(&kvm_sigmask, argp,
1728 sizeof kvm_sigmask))
1729 goto out;
1730 r = -EINVAL;
1731 if (kvm_sigmask.len != sizeof sigset)
1732 goto out;
1733 r = -EFAULT;
1734 if (copy_from_user(&sigset, sigmask_arg->sigset,
1735 sizeof sigset))
1736 goto out;
1737 p = &sigset;
1738 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001739 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001740 break;
1741 }
Avi Kivityb8836732007-04-01 16:34:31 +03001742 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001743 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1744 r = -ENOMEM;
1745 if (!fpu)
1746 goto out;
1747 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001748 if (r)
1749 goto out;
1750 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001751 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001752 goto out;
1753 r = 0;
1754 break;
1755 }
1756 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001757 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1758 r = -ENOMEM;
1759 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001760 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001761 r = -EFAULT;
1762 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1763 goto out;
1764 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001765 if (r)
1766 goto out;
1767 r = 0;
1768 break;
1769 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001770 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001771 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001772 }
1773out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001774 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001775 kfree(fpu);
1776 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001777 return r;
1778}
1779
1780static long kvm_vm_ioctl(struct file *filp,
1781 unsigned int ioctl, unsigned long arg)
1782{
1783 struct kvm *kvm = filp->private_data;
1784 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001785 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001786
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001787 if (kvm->mm != current->mm)
1788 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001789 switch (ioctl) {
1790 case KVM_CREATE_VCPU:
1791 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1792 if (r < 0)
1793 goto out;
1794 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001795 case KVM_SET_USER_MEMORY_REGION: {
1796 struct kvm_userspace_memory_region kvm_userspace_mem;
1797
1798 r = -EFAULT;
1799 if (copy_from_user(&kvm_userspace_mem, argp,
1800 sizeof kvm_userspace_mem))
1801 goto out;
1802
1803 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 if (r)
1805 goto out;
1806 break;
1807 }
1808 case KVM_GET_DIRTY_LOG: {
1809 struct kvm_dirty_log log;
1810
1811 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001812 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001814 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 if (r)
1816 goto out;
1817 break;
1818 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001819#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1820 case KVM_REGISTER_COALESCED_MMIO: {
1821 struct kvm_coalesced_mmio_zone zone;
1822 r = -EFAULT;
1823 if (copy_from_user(&zone, argp, sizeof zone))
1824 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001825 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1826 if (r)
1827 goto out;
1828 r = 0;
1829 break;
1830 }
1831 case KVM_UNREGISTER_COALESCED_MMIO: {
1832 struct kvm_coalesced_mmio_zone zone;
1833 r = -EFAULT;
1834 if (copy_from_user(&zone, argp, sizeof zone))
1835 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001836 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1837 if (r)
1838 goto out;
1839 r = 0;
1840 break;
1841 }
1842#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001843 case KVM_IRQFD: {
1844 struct kvm_irqfd data;
1845
1846 r = -EFAULT;
1847 if (copy_from_user(&data, argp, sizeof data))
1848 goto out;
1849 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1850 break;
1851 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001852 case KVM_IOEVENTFD: {
1853 struct kvm_ioeventfd data;
1854
1855 r = -EFAULT;
1856 if (copy_from_user(&data, argp, sizeof data))
1857 goto out;
1858 r = kvm_ioeventfd(kvm, &data);
1859 break;
1860 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001861#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1862 case KVM_SET_BOOT_CPU_ID:
1863 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001864 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001865 if (atomic_read(&kvm->online_vcpus) != 0)
1866 r = -EBUSY;
1867 else
1868 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001869 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001870 break;
1871#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001872 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001873 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001874 if (r == -ENOTTY)
1875 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001876 }
1877out:
1878 return r;
1879}
1880
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001881#ifdef CONFIG_COMPAT
1882struct compat_kvm_dirty_log {
1883 __u32 slot;
1884 __u32 padding1;
1885 union {
1886 compat_uptr_t dirty_bitmap; /* one bit per page */
1887 __u64 padding2;
1888 };
1889};
1890
1891static long kvm_vm_compat_ioctl(struct file *filp,
1892 unsigned int ioctl, unsigned long arg)
1893{
1894 struct kvm *kvm = filp->private_data;
1895 int r;
1896
1897 if (kvm->mm != current->mm)
1898 return -EIO;
1899 switch (ioctl) {
1900 case KVM_GET_DIRTY_LOG: {
1901 struct compat_kvm_dirty_log compat_log;
1902 struct kvm_dirty_log log;
1903
1904 r = -EFAULT;
1905 if (copy_from_user(&compat_log, (void __user *)arg,
1906 sizeof(compat_log)))
1907 goto out;
1908 log.slot = compat_log.slot;
1909 log.padding1 = compat_log.padding1;
1910 log.padding2 = compat_log.padding2;
1911 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1912
1913 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1914 if (r)
1915 goto out;
1916 break;
1917 }
1918 default:
1919 r = kvm_vm_ioctl(filp, ioctl, arg);
1920 }
1921
1922out:
1923 return r;
1924}
1925#endif
1926
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001927static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001928{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001929 struct page *page[1];
1930 unsigned long addr;
1931 int npages;
1932 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001933 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001934
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001935 addr = gfn_to_hva(kvm, gfn);
1936 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001937 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001938
1939 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1940 NULL);
1941 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001942 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001943
1944 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001945 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001946}
1947
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001948static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001949 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001950};
1951
1952static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1953{
1954 vma->vm_ops = &kvm_vm_vm_ops;
1955 return 0;
1956}
1957
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001958static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001959 .release = kvm_vm_release,
1960 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001961#ifdef CONFIG_COMPAT
1962 .compat_ioctl = kvm_vm_compat_ioctl,
1963#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001964 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001965 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001966};
1967
1968static int kvm_dev_ioctl_create_vm(void)
1969{
Heiko Carstensaac87632010-10-27 17:22:10 +02001970 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001971 struct kvm *kvm;
1972
Avi Kivityf17abe92007-02-21 19:28:04 +02001973 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001974 if (IS_ERR(kvm))
1975 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001976#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1977 r = kvm_coalesced_mmio_init(kvm);
1978 if (r < 0) {
1979 kvm_put_kvm(kvm);
1980 return r;
1981 }
1982#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02001983 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1984 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001985 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001986
Heiko Carstensaac87632010-10-27 17:22:10 +02001987 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001988}
1989
Avi Kivity1a811b62008-12-08 18:25:27 +02001990static long kvm_dev_ioctl_check_extension_generic(long arg)
1991{
1992 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001993 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001994 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001995 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001996#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1997 case KVM_CAP_SET_BOOT_CPU_ID:
1998#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001999 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002000 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002001#ifdef CONFIG_HAVE_KVM_IRQCHIP
2002 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002003 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002004#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002005 default:
2006 break;
2007 }
2008 return kvm_dev_ioctl_check_extension(arg);
2009}
2010
Avi Kivityf17abe92007-02-21 19:28:04 +02002011static long kvm_dev_ioctl(struct file *filp,
2012 unsigned int ioctl, unsigned long arg)
2013{
Avi Kivity07c45a32007-03-07 13:05:38 +02002014 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002015
2016 switch (ioctl) {
2017 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002018 r = -EINVAL;
2019 if (arg)
2020 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002021 r = KVM_API_VERSION;
2022 break;
2023 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002024 r = -EINVAL;
2025 if (arg)
2026 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002027 r = kvm_dev_ioctl_create_vm();
2028 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002029 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002030 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002031 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002032 case KVM_GET_VCPU_MMAP_SIZE:
2033 r = -EINVAL;
2034 if (arg)
2035 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002036 r = PAGE_SIZE; /* struct kvm_run */
2037#ifdef CONFIG_X86
2038 r += PAGE_SIZE; /* pio data page */
2039#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002040#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2041 r += PAGE_SIZE; /* coalesced mmio ring page */
2042#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002043 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002044 case KVM_TRACE_ENABLE:
2045 case KVM_TRACE_PAUSE:
2046 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002047 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002048 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002050 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 }
2052out:
2053 return r;
2054}
2055
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057 .unlocked_ioctl = kvm_dev_ioctl,
2058 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002059 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060};
2061
2062static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002063 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 "kvm",
2065 &kvm_chardev_ops,
2066};
2067
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002068static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002069{
2070 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002071 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002072
Rusty Russell7f59f492008-12-07 21:25:45 +10302073 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002074 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002075
Rusty Russell7f59f492008-12-07 21:25:45 +10302076 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002077
2078 r = kvm_arch_hardware_enable(NULL);
2079
2080 if (r) {
2081 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2082 atomic_inc(&hardware_enable_failed);
2083 printk(KERN_INFO "kvm: enabling virtualization on "
2084 "CPU%d failed\n", cpu);
2085 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002086}
2087
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002088static void hardware_enable(void *junk)
2089{
2090 spin_lock(&kvm_lock);
2091 hardware_enable_nolock(junk);
2092 spin_unlock(&kvm_lock);
2093}
2094
2095static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002096{
2097 int cpu = raw_smp_processor_id();
2098
Rusty Russell7f59f492008-12-07 21:25:45 +10302099 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002100 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302101 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002102 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002103}
2104
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002105static void hardware_disable(void *junk)
2106{
2107 spin_lock(&kvm_lock);
2108 hardware_disable_nolock(junk);
2109 spin_unlock(&kvm_lock);
2110}
2111
Alexander Graf10474ae2009-09-15 11:37:46 +02002112static void hardware_disable_all_nolock(void)
2113{
2114 BUG_ON(!kvm_usage_count);
2115
2116 kvm_usage_count--;
2117 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002118 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002119}
2120
2121static void hardware_disable_all(void)
2122{
2123 spin_lock(&kvm_lock);
2124 hardware_disable_all_nolock();
2125 spin_unlock(&kvm_lock);
2126}
2127
2128static int hardware_enable_all(void)
2129{
2130 int r = 0;
2131
2132 spin_lock(&kvm_lock);
2133
2134 kvm_usage_count++;
2135 if (kvm_usage_count == 1) {
2136 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002137 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002138
2139 if (atomic_read(&hardware_enable_failed)) {
2140 hardware_disable_all_nolock();
2141 r = -EBUSY;
2142 }
2143 }
2144
2145 spin_unlock(&kvm_lock);
2146
2147 return r;
2148}
2149
Avi Kivity774c47f2007-02-12 00:54:47 -08002150static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2151 void *v)
2152{
2153 int cpu = (long)v;
2154
Alexander Graf10474ae2009-09-15 11:37:46 +02002155 if (!kvm_usage_count)
2156 return NOTIFY_OK;
2157
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002158 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002159 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002160 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002161 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2162 cpu);
2163 hardware_disable(NULL);
2164 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002165 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002166 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2167 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002168 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002169 break;
2170 }
2171 return NOTIFY_OK;
2172}
2173
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002174
Avi Kivityb7c41452010-12-02 17:52:50 +02002175asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002176{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002177 /* Fault while not rebooting. We want the trace. */
2178 BUG();
2179}
Avi Kivityb7c41452010-12-02 17:52:50 +02002180EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002181
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002182static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002183 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002184{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002185 /*
2186 * Some (well, at least mine) BIOSes hang on reboot if
2187 * in vmx root mode.
2188 *
2189 * And Intel TXT required VMX off for all cpu when system shutdown.
2190 */
2191 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2192 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002193 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002194 return NOTIFY_OK;
2195}
2196
2197static struct notifier_block kvm_reboot_notifier = {
2198 .notifier_call = kvm_reboot,
2199 .priority = 0,
2200};
2201
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002202static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002203{
2204 int i;
2205
2206 for (i = 0; i < bus->dev_count; i++) {
2207 struct kvm_io_device *pos = bus->devs[i];
2208
2209 kvm_iodevice_destructor(pos);
2210 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002211 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002212}
2213
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002214/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002215int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002216 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002217{
2218 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002219 struct kvm_io_bus *bus;
2220
2221 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002222 for (i = 0; i < bus->dev_count; i++)
2223 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2224 return 0;
2225 return -EOPNOTSUPP;
2226}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002227
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002228/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002229int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2230 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002231{
2232 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002233 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002234
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002235 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002236 for (i = 0; i < bus->dev_count; i++)
2237 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2238 return 0;
2239 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002240}
2241
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002242/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002243int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2244 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002245{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002246 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002247
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002248 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002249 if (bus->dev_count > NR_IOBUS_DEVS-1)
2250 return -ENOSPC;
2251
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002252 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2253 if (!new_bus)
2254 return -ENOMEM;
2255 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2256 new_bus->devs[new_bus->dev_count++] = dev;
2257 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2258 synchronize_srcu_expedited(&kvm->srcu);
2259 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002260
2261 return 0;
2262}
2263
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002264/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002265int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2266 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002267{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002268 int i, r;
2269 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002270
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002271 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2272 if (!new_bus)
2273 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002274
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002275 bus = kvm->buses[bus_idx];
2276 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2277
2278 r = -ENOENT;
2279 for (i = 0; i < new_bus->dev_count; i++)
2280 if (new_bus->devs[i] == dev) {
2281 r = 0;
2282 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002283 break;
2284 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002285
2286 if (r) {
2287 kfree(new_bus);
2288 return r;
2289 }
2290
2291 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2292 synchronize_srcu_expedited(&kvm->srcu);
2293 kfree(bus);
2294 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002295}
2296
Avi Kivity774c47f2007-02-12 00:54:47 -08002297static struct notifier_block kvm_cpu_notifier = {
2298 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002299};
2300
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002301static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002302{
2303 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002304 struct kvm *kvm;
2305
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002306 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002307 spin_lock(&kvm_lock);
2308 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002309 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002310 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002311 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002312}
2313
2314DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2315
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002316static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002317{
2318 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002319 struct kvm *kvm;
2320 struct kvm_vcpu *vcpu;
2321 int i;
2322
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002323 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002324 spin_lock(&kvm_lock);
2325 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002326 kvm_for_each_vcpu(i, vcpu, kvm)
2327 *val += *(u32 *)((void *)vcpu + offset);
2328
Avi Kivity1165f5f2007-04-19 17:27:43 +03002329 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002330 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002331}
2332
Avi Kivityba1389b2007-11-18 16:24:12 +02002333DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2334
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002335static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002336 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2337 [KVM_STAT_VM] = &vm_stat_fops,
2338};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002339
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002340static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341{
2342 struct kvm_stats_debugfs_item *p;
2343
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002344 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002346 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002347 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002348 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349}
2350
2351static void kvm_exit_debug(void)
2352{
2353 struct kvm_stats_debugfs_item *p;
2354
2355 for (p = debugfs_entries; p->name; ++p)
2356 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002357 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358}
2359
Avi Kivity59ae6c62007-02-12 00:54:48 -08002360static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2361{
Alexander Graf10474ae2009-09-15 11:37:46 +02002362 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002363 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002364 return 0;
2365}
2366
2367static int kvm_resume(struct sys_device *dev)
2368{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002369 if (kvm_usage_count) {
2370 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002371 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002372 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002373 return 0;
2374}
2375
2376static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002377 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002378 .suspend = kvm_suspend,
2379 .resume = kvm_resume,
2380};
2381
2382static struct sys_device kvm_sysdev = {
2383 .id = 0,
2384 .cls = &kvm_sysdev_class,
2385};
2386
Izik Eiduscea7bb22007-10-17 19:17:48 +02002387struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002388pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389
Avi Kivity15ad7142007-07-11 18:17:21 +03002390static inline
2391struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2392{
2393 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2394}
2395
2396static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2397{
2398 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2399
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002400 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002401}
2402
2403static void kvm_sched_out(struct preempt_notifier *pn,
2404 struct task_struct *next)
2405{
2406 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2407
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002408 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002409}
2410
Avi Kivity0ee75be2010-04-28 15:39:01 +03002411int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002412 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413{
2414 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002415 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002417 r = kvm_arch_init(opaque);
2418 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002419 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002420
2421 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2422
2423 if (bad_page == NULL) {
2424 r = -ENOMEM;
2425 goto out;
2426 }
2427
Anthony Liguori35149e22008-04-02 14:46:56 -05002428 bad_pfn = page_to_pfn(bad_page);
2429
Huang Yingbf998152010-05-31 14:28:19 +08002430 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2431
2432 if (hwpoison_page == NULL) {
2433 r = -ENOMEM;
2434 goto out_free_0;
2435 }
2436
2437 hwpoison_pfn = page_to_pfn(hwpoison_page);
2438
Gleb Natapovedba23e2010-07-07 20:16:45 +03002439 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2440
2441 if (fault_page == NULL) {
2442 r = -ENOMEM;
2443 goto out_free_0;
2444 }
2445
2446 fault_pfn = page_to_pfn(fault_page);
2447
Avi Kivity8437a612009-06-06 14:52:35 -07002448 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302449 r = -ENOMEM;
2450 goto out_free_0;
2451 }
2452
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002453 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302455 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456
Yang, Sheng002c7f72007-07-31 14:23:01 +03002457 for_each_online_cpu(cpu) {
2458 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002459 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002460 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002461 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002462 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002463 }
2464
Avi Kivity774c47f2007-02-12 00:54:47 -08002465 r = register_cpu_notifier(&kvm_cpu_notifier);
2466 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002467 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 register_reboot_notifier(&kvm_reboot_notifier);
2469
Avi Kivity59ae6c62007-02-12 00:54:48 -08002470 r = sysdev_class_register(&kvm_sysdev_class);
2471 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002472 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002473
2474 r = sysdev_register(&kvm_sysdev);
2475 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002476 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002477
Rusty Russellc16f8622007-07-30 21:12:19 +10002478 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002479 if (!vcpu_align)
2480 vcpu_align = __alignof__(struct kvm_vcpu);
2481 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002482 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002483 if (!kvm_vcpu_cache) {
2484 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002485 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002486 }
2487
Gleb Natapovaf585b92010-10-14 11:22:46 +02002488 r = kvm_async_pf_init();
2489 if (r)
2490 goto out_free;
2491
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002493 kvm_vm_fops.owner = module;
2494 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495
2496 r = misc_register(&kvm_dev);
2497 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002498 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002499 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 }
2501
Avi Kivity15ad7142007-07-11 18:17:21 +03002502 kvm_preempt_ops.sched_in = kvm_sched_in;
2503 kvm_preempt_ops.sched_out = kvm_sched_out;
2504
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002505 kvm_init_debug();
2506
Avi Kivityc7addb92007-09-16 18:58:32 +02002507 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508
Gleb Natapovaf585b92010-10-14 11:22:46 +02002509out_unreg:
2510 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002512 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002513out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002514 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002515out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002516 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002517out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002519 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002520out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002521out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002522 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302523out_free_0a:
2524 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002525out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002526 if (fault_page)
2527 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002528 if (hwpoison_page)
2529 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002530 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002531out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002532 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002533out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 return r;
2535}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002536EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002538void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002540 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002542 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002543 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002544 sysdev_unregister(&kvm_sysdev);
2545 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002547 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002548 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002549 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002550 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302551 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002552 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002553 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002555EXPORT_SYMBOL_GPL(kvm_exit);