blob: ee99b77e445104cfb24b1e442e800ac4f32b0614 [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
Hannes Edere8ba5d32008-11-28 17:02:42 +010093static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030094
Marcelo Tosatti54dee992009-06-11 12:07:44 -030095static bool largepages_enabled = true;
96
Gleb Natapovfa7bff82010-07-07 20:16:44 +030097static struct page *hwpoison_page;
98static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +080099
Gleb Natapovedba23e2010-07-07 20:16:45 +0300100static struct page *fault_page;
101static pfn_t fault_pfn;
102
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800103inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300104{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100105 if (pfn_valid(pfn)) {
106 struct page *page = compound_head(pfn_to_page(pfn));
107 return PageReserved(page);
108 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300109
110 return true;
111}
112
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113/*
114 * Switches to specified vcpu, until a matching vcpu_put()
115 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200116void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117{
Avi Kivity15ad7142007-07-11 18:17:21 +0300118 int cpu;
119
Avi Kivitybccf2152007-02-21 18:04:26 +0200120 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300121 cpu = get_cpu();
122 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200123 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300124 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200125}
126
Carsten Otte313a3dc2007-10-11 19:16:52 +0200127void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128{
Avi Kivity15ad7142007-07-11 18:17:21 +0300129 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200130 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300131 preempt_notifier_unregister(&vcpu->preempt_notifier);
132 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 mutex_unlock(&vcpu->mutex);
134}
135
Avi Kivityd9e368d2007-06-07 19:18:30 +0300136static void ack_flush(void *_completed)
137{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300138}
139
Rusty Russell49846892008-12-08 20:26:24 +1030140static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300141{
Avi Kivity597a5f52008-07-20 14:24:22 +0300142 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030143 cpumask_var_t cpus;
144 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300145 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300146
Li Zefan79f55992009-06-15 14:58:26 +0800147 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030148
Avi Kivity70e335e2010-02-18 11:25:22 +0200149 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200150 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300151 kvm_for_each_vcpu(i, vcpu, kvm) {
Avi Kivitya8eeb042010-05-10 12:34:53 +0300152 if (kvm_make_check_request(req, vcpu))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300153 continue;
154 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030155 if (cpus != NULL && cpu != -1 && cpu != me)
156 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300157 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030158 if (unlikely(cpus == NULL))
159 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
160 else if (!cpumask_empty(cpus))
161 smp_call_function_many(cpus, ack_flush, NULL, 1);
162 else
163 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200164 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030165 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030166 return called;
167}
168
169void kvm_flush_remote_tlbs(struct kvm *kvm)
170{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800171 int dirty_count = kvm->tlbs_dirty;
172
173 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030174 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
175 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800176 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300177}
178
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500179void kvm_reload_remote_mmus(struct kvm *kvm)
180{
Rusty Russell49846892008-12-08 20:26:24 +1030181 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500182}
183
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000184int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
185{
186 struct page *page;
187 int r;
188
189 mutex_init(&vcpu->mutex);
190 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191 vcpu->kvm = kvm;
192 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300193 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200194 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000195
196 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
197 if (!page) {
198 r = -ENOMEM;
199 goto fail;
200 }
201 vcpu->run = page_address(page);
202
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800203 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000204 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800205 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000206 return 0;
207
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000208fail_free_run:
209 free_page((unsigned long)vcpu->run);
210fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000211 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212}
213EXPORT_SYMBOL_GPL(kvm_vcpu_init);
214
215void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
216{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800217 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000218 free_page((unsigned long)vcpu->run);
219}
220EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
221
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200222#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
223static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
224{
225 return container_of(mn, struct kvm, mmu_notifier);
226}
227
228static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
229 struct mm_struct *mm,
230 unsigned long address)
231{
232 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200233 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200234
235 /*
236 * When ->invalidate_page runs, the linux pte has been zapped
237 * already but the page is still allocated until
238 * ->invalidate_page returns. So if we increase the sequence
239 * here the kvm page fault will notice if the spte can't be
240 * established because the page is going to be freed. If
241 * instead the kvm page fault establishes the spte before
242 * ->invalidate_page runs, kvm_unmap_hva will release it
243 * before returning.
244 *
245 * The sequence increase only need to be seen at spin_unlock
246 * time, and not at spin_lock time.
247 *
248 * Increasing the sequence after the spin_unlock would be
249 * unsafe because the kvm page fault could then establish the
250 * pte after kvm_unmap_hva returned, without noticing the page
251 * is going to be freed.
252 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200253 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200254 spin_lock(&kvm->mmu_lock);
255 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800256 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200257 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200258 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200259
260 /* we've to flush the tlb before the pages can be freed */
261 if (need_tlb_flush)
262 kvm_flush_remote_tlbs(kvm);
263
264}
265
Izik Eidus3da0dd42009-09-23 21:47:18 +0300266static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
267 struct mm_struct *mm,
268 unsigned long address,
269 pte_t pte)
270{
271 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200272 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300273
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200274 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300275 spin_lock(&kvm->mmu_lock);
276 kvm->mmu_notifier_seq++;
277 kvm_set_spte_hva(kvm, address, pte);
278 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200279 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300280}
281
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200282static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
283 struct mm_struct *mm,
284 unsigned long start,
285 unsigned long end)
286{
287 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200288 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200289
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200290 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200291 spin_lock(&kvm->mmu_lock);
292 /*
293 * The count increase must become visible at unlock time as no
294 * spte can be established without taking the mmu_lock and
295 * count is also read inside the mmu_lock critical section.
296 */
297 kvm->mmu_notifier_count++;
298 for (; start < end; start += PAGE_SIZE)
299 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800300 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200301 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200302 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200303
304 /* we've to flush the tlb before the pages can be freed */
305 if (need_tlb_flush)
306 kvm_flush_remote_tlbs(kvm);
307}
308
309static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
310 struct mm_struct *mm,
311 unsigned long start,
312 unsigned long end)
313{
314 struct kvm *kvm = mmu_notifier_to_kvm(mn);
315
316 spin_lock(&kvm->mmu_lock);
317 /*
318 * This sequence increase will notify the kvm page fault that
319 * the page that is going to be mapped in the spte could have
320 * been freed.
321 */
322 kvm->mmu_notifier_seq++;
323 /*
324 * The above sequence increase must be visible before the
325 * below count decrease but both values are read by the kvm
326 * page fault under mmu_lock spinlock so we don't need to add
327 * a smb_wmb() here in between the two.
328 */
329 kvm->mmu_notifier_count--;
330 spin_unlock(&kvm->mmu_lock);
331
332 BUG_ON(kvm->mmu_notifier_count < 0);
333}
334
335static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
336 struct mm_struct *mm,
337 unsigned long address)
338{
339 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200340 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200341
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200342 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200343 spin_lock(&kvm->mmu_lock);
344 young = kvm_age_hva(kvm, address);
345 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200346 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200347
348 if (young)
349 kvm_flush_remote_tlbs(kvm);
350
351 return young;
352}
353
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100354static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
355 struct mm_struct *mm)
356{
357 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800358 int idx;
359
360 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100361 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800362 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100363}
364
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200365static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
366 .invalidate_page = kvm_mmu_notifier_invalidate_page,
367 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
368 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
369 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300370 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100371 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200372};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200373
374static int kvm_init_mmu_notifier(struct kvm *kvm)
375{
376 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
377 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
378}
379
380#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
381
382static int kvm_init_mmu_notifier(struct kvm *kvm)
383{
384 return 0;
385}
386
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200387#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
388
Avi Kivityf17abe92007-02-21 19:28:04 +0200389static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100391 int r, i;
392 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100394 if (!kvm)
395 return ERR_PTR(-ENOMEM);
396
397 r = kvm_arch_init_vm(kvm);
398 if (r)
399 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200400
401 r = hardware_enable_all();
402 if (r)
403 goto out_err_nodisable;
404
Avi Kivity75858a82009-01-04 17:10:50 +0200405#ifdef CONFIG_HAVE_KVM_IRQCHIP
406 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300407 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200408#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200410 r = -ENOMEM;
411 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
412 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100413 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200414 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100415 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200416 for (i = 0; i < KVM_NR_BUSES; i++) {
417 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
418 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100419 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200420 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200421 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200422
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200423 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100424 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200425 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200426
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200427 kvm->mm = current->mm;
428 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500429 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200430 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400431 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800432 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300433 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200434 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300435 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000436 spin_lock(&kvm_lock);
437 list_add(&kvm->vm_list, &vm_list);
438 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100439
Avi Kivityf17abe92007-02-21 19:28:04 +0200440 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200441
442out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100443 cleanup_srcu_struct(&kvm->srcu);
444out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200445 hardware_disable_all();
446out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200447 for (i = 0; i < KVM_NR_BUSES; i++)
448 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200449 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100450 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200451 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200452}
453
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900454static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
455{
456 if (!memslot->dirty_bitmap)
457 return;
458
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900459 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
460 vfree(memslot->dirty_bitmap_head);
461 else
462 kfree(memslot->dirty_bitmap_head);
463
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900464 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900465 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900466}
467
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468/*
469 * Free any memory in @free but not in @dont.
470 */
471static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
472 struct kvm_memory_slot *dont)
473{
Joerg Roedelec04b262009-06-19 15:16:23 +0200474 int i;
475
Izik Eidus290fc382007-09-27 14:11:22 +0200476 if (!dont || free->rmap != dont->rmap)
477 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
479 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900480 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481
Joerg Roedelec04b262009-06-19 15:16:23 +0200482
483 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
484 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
485 vfree(free->lpage_info[i]);
486 free->lpage_info[i] = NULL;
487 }
488 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300489
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500491 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492}
493
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800494void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495{
496 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200497 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200499 for (i = 0; i < slots->nmemslots; ++i)
500 kvm_free_physmem_slot(&slots->memslots[i], NULL);
501
502 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503}
504
Avi Kivityf17abe92007-02-21 19:28:04 +0200505static void kvm_destroy_vm(struct kvm *kvm)
506{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200507 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200508 struct mm_struct *mm = kvm->mm;
509
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800510 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800511 spin_lock(&kvm_lock);
512 list_del(&kvm->vm_list);
513 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200514 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200515 for (i = 0; i < KVM_NR_BUSES; i++)
516 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200517 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200518#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
519 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200520#else
521 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200522#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800523 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100524 kvm_free_physmem(kvm);
525 cleanup_srcu_struct(&kvm->srcu);
526 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200527 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200528 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200529}
530
Izik Eidusd39f13b2008-03-30 16:01:25 +0300531void kvm_get_kvm(struct kvm *kvm)
532{
533 atomic_inc(&kvm->users_count);
534}
535EXPORT_SYMBOL_GPL(kvm_get_kvm);
536
537void kvm_put_kvm(struct kvm *kvm)
538{
539 if (atomic_dec_and_test(&kvm->users_count))
540 kvm_destroy_vm(kvm);
541}
542EXPORT_SYMBOL_GPL(kvm_put_kvm);
543
544
Avi Kivityf17abe92007-02-21 19:28:04 +0200545static int kvm_vm_release(struct inode *inode, struct file *filp)
546{
547 struct kvm *kvm = filp->private_data;
548
Gregory Haskins721eecb2009-05-20 10:30:49 -0400549 kvm_irqfd_release(kvm);
550
Izik Eidusd39f13b2008-03-30 16:01:25 +0300551 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 return 0;
553}
554
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900555/*
556 * Allocation size is twice as large as the actual dirty bitmap size.
557 * This makes it possible to do double buffering: see x86's
558 * kvm_vm_ioctl_get_dirty_log().
559 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900560static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
561{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900562 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900563
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900564 if (dirty_bytes > PAGE_SIZE)
565 memslot->dirty_bitmap = vzalloc(dirty_bytes);
566 else
567 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
568
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900569 if (!memslot->dirty_bitmap)
570 return -ENOMEM;
571
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900572 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900573 return 0;
574}
575
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 * Allocate some memory and give it an address in the guest physical address
578 * space.
579 *
580 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800581 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500582 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800584int __kvm_set_memory_region(struct kvm *kvm,
585 struct kvm_userspace_memory_region *mem,
586 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200588 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200590 unsigned long npages;
591 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 struct kvm_memory_slot *memslot;
593 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200594 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595
596 r = -EINVAL;
597 /* General sanity checks */
598 if (mem->memory_size & (PAGE_SIZE - 1))
599 goto out;
600 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
601 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800602 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600603 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200604 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 goto out;
606 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
607 goto out;
608
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200609 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
611 npages = mem->memory_size >> PAGE_SHIFT;
612
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900613 r = -EINVAL;
614 if (npages > KVM_MEM_MAX_NR_PAGES)
615 goto out;
616
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617 if (!npages)
618 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620 new = old = *memslot;
621
Avi Kivitye36d96f2010-06-21 10:56:36 +0300622 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 new.base_gfn = base_gfn;
624 new.npages = npages;
625 new.flags = mem->flags;
626
627 /* Disallow changing a memory slot's size. */
628 r = -EINVAL;
629 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800630 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
632 /* Check for overlaps */
633 r = -EEXIST;
634 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200635 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200637 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 continue;
639 if (!((base_gfn + npages <= s->base_gfn) ||
640 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800641 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644 /* Free page dirty bitmap if unneeded */
645 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000646 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647
648 r = -ENOMEM;
649
650 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200651#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500652 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900653 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200654
655 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800656 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200657
Izik Eidus80b14b52007-10-25 11:54:04 +0200658 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200659 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200661 if (!npages)
662 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300663
Joerg Roedelec04b262009-06-19 15:16:23 +0200664 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200665 unsigned long ugfn;
666 unsigned long j;
667 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200668 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300669
Joerg Roedelec04b262009-06-19 15:16:23 +0200670 /* Avoid unused variable warning if no large pages */
671 (void)level;
672
673 if (new.lpage_info[i])
674 continue;
675
Joerg Roedel82855412010-07-01 16:00:11 +0200676 lpages = 1 + ((base_gfn + npages - 1)
677 >> KVM_HPAGE_GFN_SHIFT(level));
678 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200679
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900680 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200681
682 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300683 goto out_free;
684
Joerg Roedel82855412010-07-01 16:00:11 +0200685 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200686 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200687 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200688 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300689 ugfn = new.userspace_addr >> PAGE_SHIFT;
690 /*
691 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300692 * other, or if explicitly asked to, disable large page
693 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300694 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200695 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300696 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200697 for (j = 0; j < lpages; ++j)
698 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700
Joerg Roedelec04b262009-06-19 15:16:23 +0200701skip_lpage:
702
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703 /* Allocate page dirty bitmap if needed */
704 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900705 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800706 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200707 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300708 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200709 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200711#else /* not defined CONFIG_S390 */
712 new.user_alloc = user_alloc;
713 if (user_alloc)
714 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200715#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200717 if (!npages) {
718 r = -ENOMEM;
719 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
720 if (!slots)
721 goto out_free;
722 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
723 if (mem->slot >= slots->nmemslots)
724 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200725 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200726 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
727
728 old_memslots = kvm->memslots;
729 rcu_assign_pointer(kvm->memslots, slots);
730 synchronize_srcu_expedited(&kvm->srcu);
731 /* From this point no new shadow pages pointing to a deleted
732 * memslot will be created.
733 *
734 * validation of sp->gfn happens in:
735 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
736 * - kvm_is_visible_gfn (mmu_check_roots)
737 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300738 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200739 kfree(old_memslots);
740 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300741
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200742 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
743 if (r)
744 goto out_free;
745
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200746 /* map the pages in iommu page table */
747 if (npages) {
748 r = kvm_iommu_map_pages(kvm, &new);
749 if (r)
750 goto out_free;
751 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200752
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200753 r = -ENOMEM;
754 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
755 if (!slots)
756 goto out_free;
757 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
758 if (mem->slot >= slots->nmemslots)
759 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200760 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200761
762 /* actual memory is freed via old in kvm_free_physmem_slot below */
763 if (!npages) {
764 new.rmap = NULL;
765 new.dirty_bitmap = NULL;
766 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
767 new.lpage_info[i] = NULL;
768 }
769
770 slots->memslots[mem->slot] = new;
771 old_memslots = kvm->memslots;
772 rcu_assign_pointer(kvm->memslots, slots);
773 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200775 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800776
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200777 kvm_free_physmem_slot(&old, &new);
778 kfree(old_memslots);
779
780 if (flush_shadow)
781 kvm_arch_flush_shadow(kvm);
782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 return 0;
784
Sheng Yangf78e0e22007-10-29 09:40:42 +0800785out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 kvm_free_physmem_slot(&new, &old);
787out:
788 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200789
790}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800791EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
792
793int kvm_set_memory_region(struct kvm *kvm,
794 struct kvm_userspace_memory_region *mem,
795 int user_alloc)
796{
797 int r;
798
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200799 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800800 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200801 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800802 return r;
803}
Izik Eidus210c7c42007-10-24 23:52:57 +0200804EXPORT_SYMBOL_GPL(kvm_set_memory_region);
805
Carsten Otte1fe779f2007-10-29 16:08:35 +0100806int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
807 struct
808 kvm_userspace_memory_region *mem,
809 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200810{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200811 if (mem->slot >= KVM_MEMORY_SLOTS)
812 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200813 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814}
815
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800816int kvm_get_dirty_log(struct kvm *kvm,
817 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818{
819 struct kvm_memory_slot *memslot;
820 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900821 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 unsigned long any = 0;
823
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 r = -EINVAL;
825 if (log->slot >= KVM_MEMORY_SLOTS)
826 goto out;
827
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200828 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 r = -ENOENT;
830 if (!memslot->dirty_bitmap)
831 goto out;
832
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900833 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834
Uri Lublincd1a4a92007-02-22 16:43:09 +0200835 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 any = memslot->dirty_bitmap[i];
837
838 r = -EFAULT;
839 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
840 goto out;
841
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800842 if (any)
843 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
845 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 return r;
848}
849
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300850void kvm_disable_largepages(void)
851{
852 largepages_enabled = false;
853}
854EXPORT_SYMBOL_GPL(kvm_disable_largepages);
855
Izik Eiduscea7bb22007-10-17 19:17:48 +0200856int is_error_page(struct page *page)
857{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300858 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200859}
860EXPORT_SYMBOL_GPL(is_error_page);
861
Anthony Liguori35149e22008-04-02 14:46:56 -0500862int is_error_pfn(pfn_t pfn)
863{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300864 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500865}
866EXPORT_SYMBOL_GPL(is_error_pfn);
867
Huang Yingbf998152010-05-31 14:28:19 +0800868int is_hwpoison_pfn(pfn_t pfn)
869{
870 return pfn == hwpoison_pfn;
871}
872EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
873
Gleb Natapovedba23e2010-07-07 20:16:45 +0300874int is_fault_pfn(pfn_t pfn)
875{
876 return pfn == fault_pfn;
877}
878EXPORT_SYMBOL_GPL(is_fault_pfn);
879
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200880static inline unsigned long bad_hva(void)
881{
882 return PAGE_OFFSET;
883}
884
885int kvm_is_error_hva(unsigned long addr)
886{
887 return addr == bad_hva();
888}
889EXPORT_SYMBOL_GPL(kvm_is_error_hva);
890
Gleb Natapov49c77542010-10-18 15:22:23 +0200891static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
892 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893{
894 int i;
895
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200896 for (i = 0; i < slots->nmemslots; ++i) {
897 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898
899 if (gfn >= memslot->base_gfn
900 && gfn < memslot->base_gfn + memslot->npages)
901 return memslot;
902 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000903 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904}
Gleb Natapov49c77542010-10-18 15:22:23 +0200905
906struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
907{
908 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
909}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300910EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911
Izik Eiduse0d62c72007-10-24 23:57:46 +0200912int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
913{
914 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800915 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200916
Izik Eiduse0d62c72007-10-24 23:57:46 +0200917 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200918 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200919
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200920 if (memslot->flags & KVM_MEMSLOT_INVALID)
921 continue;
922
Izik Eiduse0d62c72007-10-24 23:57:46 +0200923 if (gfn >= memslot->base_gfn
924 && gfn < memslot->base_gfn + memslot->npages)
925 return 1;
926 }
927 return 0;
928}
929EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
930
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100931unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
932{
933 struct vm_area_struct *vma;
934 unsigned long addr, size;
935
936 size = PAGE_SIZE;
937
938 addr = gfn_to_hva(kvm, gfn);
939 if (kvm_is_error_hva(addr))
940 return PAGE_SIZE;
941
942 down_read(&current->mm->mmap_sem);
943 vma = find_vma(current->mm, addr);
944 if (!vma)
945 goto out;
946
947 size = vma_kernel_pagesize(vma);
948
949out:
950 up_read(&current->mm->mmap_sem);
951
952 return size;
953}
954
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200955int memslot_id(struct kvm *kvm, gfn_t gfn)
956{
957 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800958 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200959 struct kvm_memory_slot *memslot = NULL;
960
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200961 for (i = 0; i < slots->nmemslots; ++i) {
962 memslot = &slots->memslots[i];
963
964 if (gfn >= memslot->base_gfn
965 && gfn < memslot->base_gfn + memslot->npages)
966 break;
967 }
968
969 return memslot - slots->memslots;
970}
971
Gleb Natapov49c77542010-10-18 15:22:23 +0200972static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +0800973 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +0200974{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200975 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200976 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +0800977
978 if (nr_pages)
979 *nr_pages = slot->npages - (gfn - slot->base_gfn);
980
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900981 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200982}
Xiao Guangrong48987782010-08-22 19:11:43 +0800983
984unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
985{
Gleb Natapov49c77542010-10-18 15:22:23 +0200986 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +0800987}
Sheng Yang0d150292008-04-25 21:44:50 +0800988EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200989
Gleb Natapov80300892010-10-19 18:13:41 +0200990static pfn_t get_fault_pfn(void)
991{
992 get_page(fault_page);
993 return fault_pfn;
994}
995
Gleb Natapovaf585b92010-10-14 11:22:46 +0200996static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200997 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300998{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500999 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001000 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001001 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001002
Gleb Natapovaf585b92010-10-14 11:22:46 +02001003 /* we can do it either atomically or asynchronously, not both */
1004 BUG_ON(atomic && async);
1005
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001006 BUG_ON(!write_fault && !writable);
1007
1008 if (writable)
1009 *writable = true;
1010
Gleb Natapovaf585b92010-10-14 11:22:46 +02001011 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001012 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001013
1014 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001015 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001016
1017 if (writable)
1018 *writable = write_fault;
1019
1020 npages = get_user_pages_fast(addr, 1, write_fault, page);
1021
1022 /* map read fault as writable if possible */
1023 if (unlikely(!write_fault) && npages == 1) {
1024 struct page *wpage[1];
1025
1026 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1027 if (npages == 1) {
1028 *writable = true;
1029 put_page(page[0]);
1030 page[0] = wpage[0];
1031 }
1032 npages = 1;
1033 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001034 }
Izik Eidus539cb662007-11-11 22:05:04 +02001035
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001036 if (unlikely(npages != 1)) {
1037 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001038
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001039 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001040 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001041
Huang Yingbbeb3402010-06-22 14:23:11 +08001042 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001043 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001044 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001045 get_page(hwpoison_page);
1046 return page_to_pfn(hwpoison_page);
1047 }
1048
Gleb Natapov80300892010-10-19 18:13:41 +02001049 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001050
Gleb Natapov80300892010-10-19 18:13:41 +02001051 if (vma == NULL)
1052 pfn = get_fault_pfn();
1053 else if ((vma->vm_flags & VM_PFNMAP)) {
1054 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1055 vma->vm_pgoff;
1056 BUG_ON(!kvm_is_mmio_pfn(pfn));
1057 } else {
1058 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001059 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001060 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001061 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001062 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001063 } else
1064 pfn = page_to_pfn(page[0]);
1065
1066 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001067}
1068
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001069pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1070{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001071 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001072}
1073EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1074
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001075static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1076 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001077{
1078 unsigned long addr;
1079
Gleb Natapovaf585b92010-10-14 11:22:46 +02001080 if (async)
1081 *async = false;
1082
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001083 addr = gfn_to_hva(kvm, gfn);
1084 if (kvm_is_error_hva(addr)) {
1085 get_page(bad_page);
1086 return page_to_pfn(bad_page);
1087 }
1088
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001089 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001090}
1091
1092pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1093{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001094 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001095}
1096EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1097
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001098pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1099 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001100{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001101 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001102}
1103EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1104
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001105pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1106{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001107 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001108}
Anthony Liguori35149e22008-04-02 14:46:56 -05001109EXPORT_SYMBOL_GPL(gfn_to_pfn);
1110
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001111pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1112 bool *writable)
1113{
1114 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1115}
1116EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1117
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001118pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1119 struct kvm_memory_slot *slot, gfn_t gfn)
1120{
1121 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001122 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001123}
1124
Xiao Guangrong48987782010-08-22 19:11:43 +08001125int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1126 int nr_pages)
1127{
1128 unsigned long addr;
1129 gfn_t entry;
1130
Gleb Natapov49c77542010-10-18 15:22:23 +02001131 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001132 if (kvm_is_error_hva(addr))
1133 return -1;
1134
1135 if (entry < nr_pages)
1136 return 0;
1137
1138 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1139}
1140EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1141
Anthony Liguori35149e22008-04-02 14:46:56 -05001142struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1143{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001144 pfn_t pfn;
1145
1146 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001147 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001148 return pfn_to_page(pfn);
1149
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001150 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001151
1152 get_page(bad_page);
1153 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001154}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001155
Avi Kivity954bbbc22007-03-30 14:02:32 +03001156EXPORT_SYMBOL_GPL(gfn_to_page);
1157
Izik Eidusb4231d62007-11-20 11:49:33 +02001158void kvm_release_page_clean(struct page *page)
1159{
Anthony Liguori35149e22008-04-02 14:46:56 -05001160 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001161}
1162EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1163
Anthony Liguori35149e22008-04-02 14:46:56 -05001164void kvm_release_pfn_clean(pfn_t pfn)
1165{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001166 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001167 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001168}
1169EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1170
Izik Eidusb4231d62007-11-20 11:49:33 +02001171void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001172{
Anthony Liguori35149e22008-04-02 14:46:56 -05001173 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001174}
Izik Eidusb4231d62007-11-20 11:49:33 +02001175EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001176
Anthony Liguori35149e22008-04-02 14:46:56 -05001177void kvm_release_pfn_dirty(pfn_t pfn)
1178{
1179 kvm_set_pfn_dirty(pfn);
1180 kvm_release_pfn_clean(pfn);
1181}
1182EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1183
1184void kvm_set_page_dirty(struct page *page)
1185{
1186 kvm_set_pfn_dirty(page_to_pfn(page));
1187}
1188EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1189
1190void kvm_set_pfn_dirty(pfn_t pfn)
1191{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001192 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001193 struct page *page = pfn_to_page(pfn);
1194 if (!PageReserved(page))
1195 SetPageDirty(page);
1196 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001197}
1198EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1199
1200void kvm_set_pfn_accessed(pfn_t pfn)
1201{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001202 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001203 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001204}
1205EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1206
1207void kvm_get_pfn(pfn_t pfn)
1208{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001209 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001210 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001211}
1212EXPORT_SYMBOL_GPL(kvm_get_pfn);
1213
Izik Eidus195aefd2007-10-01 22:14:18 +02001214static int next_segment(unsigned long len, int offset)
1215{
1216 if (len > PAGE_SIZE - offset)
1217 return PAGE_SIZE - offset;
1218 else
1219 return len;
1220}
1221
1222int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1223 int len)
1224{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001225 int r;
1226 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001227
Izik Eiduse0506bc2007-11-11 22:10:22 +02001228 addr = gfn_to_hva(kvm, gfn);
1229 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001230 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001231 r = copy_from_user(data, (void __user *)addr + offset, len);
1232 if (r)
1233 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001234 return 0;
1235}
1236EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1237
1238int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1239{
1240 gfn_t gfn = gpa >> PAGE_SHIFT;
1241 int seg;
1242 int offset = offset_in_page(gpa);
1243 int ret;
1244
1245 while ((seg = next_segment(len, offset)) != 0) {
1246 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1247 if (ret < 0)
1248 return ret;
1249 offset = 0;
1250 len -= seg;
1251 data += seg;
1252 ++gfn;
1253 }
1254 return 0;
1255}
1256EXPORT_SYMBOL_GPL(kvm_read_guest);
1257
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001258int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1259 unsigned long len)
1260{
1261 int r;
1262 unsigned long addr;
1263 gfn_t gfn = gpa >> PAGE_SHIFT;
1264 int offset = offset_in_page(gpa);
1265
1266 addr = gfn_to_hva(kvm, gfn);
1267 if (kvm_is_error_hva(addr))
1268 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001269 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001270 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001271 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001272 if (r)
1273 return -EFAULT;
1274 return 0;
1275}
1276EXPORT_SYMBOL(kvm_read_guest_atomic);
1277
Izik Eidus195aefd2007-10-01 22:14:18 +02001278int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1279 int offset, int len)
1280{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001281 int r;
1282 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001283
Izik Eiduse0506bc2007-11-11 22:10:22 +02001284 addr = gfn_to_hva(kvm, gfn);
1285 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001286 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001287 r = copy_to_user((void __user *)addr + offset, data, len);
1288 if (r)
1289 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001290 mark_page_dirty(kvm, gfn);
1291 return 0;
1292}
1293EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1294
1295int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1296 unsigned long len)
1297{
1298 gfn_t gfn = gpa >> PAGE_SHIFT;
1299 int seg;
1300 int offset = offset_in_page(gpa);
1301 int ret;
1302
1303 while ((seg = next_segment(len, offset)) != 0) {
1304 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1305 if (ret < 0)
1306 return ret;
1307 offset = 0;
1308 len -= seg;
1309 data += seg;
1310 ++gfn;
1311 }
1312 return 0;
1313}
1314
Gleb Natapov49c77542010-10-18 15:22:23 +02001315int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1316 gpa_t gpa)
1317{
1318 struct kvm_memslots *slots = kvm_memslots(kvm);
1319 int offset = offset_in_page(gpa);
1320 gfn_t gfn = gpa >> PAGE_SHIFT;
1321
1322 ghc->gpa = gpa;
1323 ghc->generation = slots->generation;
1324 ghc->memslot = __gfn_to_memslot(slots, gfn);
1325 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1326 if (!kvm_is_error_hva(ghc->hva))
1327 ghc->hva += offset;
1328 else
1329 return -EFAULT;
1330
1331 return 0;
1332}
1333EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1334
1335int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1336 void *data, unsigned long len)
1337{
1338 struct kvm_memslots *slots = kvm_memslots(kvm);
1339 int r;
1340
1341 if (slots->generation != ghc->generation)
1342 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1343
1344 if (kvm_is_error_hva(ghc->hva))
1345 return -EFAULT;
1346
1347 r = copy_to_user((void __user *)ghc->hva, data, len);
1348 if (r)
1349 return -EFAULT;
1350 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1351
1352 return 0;
1353}
1354EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1355
Izik Eidus195aefd2007-10-01 22:14:18 +02001356int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1357{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001358 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1359 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001360}
1361EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1362
1363int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1364{
1365 gfn_t gfn = gpa >> PAGE_SHIFT;
1366 int seg;
1367 int offset = offset_in_page(gpa);
1368 int ret;
1369
1370 while ((seg = next_segment(len, offset)) != 0) {
1371 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1372 if (ret < 0)
1373 return ret;
1374 offset = 0;
1375 len -= seg;
1376 ++gfn;
1377 }
1378 return 0;
1379}
1380EXPORT_SYMBOL_GPL(kvm_clear_guest);
1381
Gleb Natapov49c77542010-10-18 15:22:23 +02001382void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1383 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001385 if (memslot && memslot->dirty_bitmap) {
1386 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001388 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 }
1390}
1391
Gleb Natapov49c77542010-10-18 15:22:23 +02001392void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1393{
1394 struct kvm_memory_slot *memslot;
1395
1396 memslot = gfn_to_memslot(kvm, gfn);
1397 mark_page_dirty_in_slot(kvm, memslot, gfn);
1398}
1399
Eddie Dongb6958ce2007-07-18 12:15:21 +03001400/*
1401 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1402 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001403void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001404{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001405 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001406
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001407 for (;;) {
1408 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001409
Gleb Natapova1b37102009-07-09 15:33:52 +03001410 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001411 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001412 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001413 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001414 if (kvm_cpu_has_pending_timer(vcpu))
1415 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001416 if (signal_pending(current))
1417 break;
1418
Eddie Dongb6958ce2007-07-18 12:15:21 +03001419 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001420 }
1421
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001422 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001423}
1424
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425void kvm_resched(struct kvm_vcpu *vcpu)
1426{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001427 if (!need_resched())
1428 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430}
1431EXPORT_SYMBOL_GPL(kvm_resched);
1432
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001433void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1434{
1435 ktime_t expires;
1436 DEFINE_WAIT(wait);
1437
1438 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1439
1440 /* Sleep for 100 us, and hope lock-holder got scheduled */
1441 expires = ktime_add_ns(ktime_get(), 100000UL);
1442 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1443
1444 finish_wait(&vcpu->wq, &wait);
1445}
1446EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1447
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001448static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001449{
1450 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001451 struct page *page;
1452
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001453 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001454 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001455#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001456 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001457 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001458#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001459#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1460 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1461 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1462#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001463 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001464 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001465 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001466 vmf->page = page;
1467 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001468}
1469
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001470static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001471 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001472};
1473
1474static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1475{
1476 vma->vm_ops = &kvm_vcpu_vm_ops;
1477 return 0;
1478}
1479
Avi Kivitybccf2152007-02-21 18:04:26 +02001480static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1481{
1482 struct kvm_vcpu *vcpu = filp->private_data;
1483
Al Viro66c0b392008-04-19 20:33:56 +01001484 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001485 return 0;
1486}
1487
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001488static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001489 .release = kvm_vcpu_release,
1490 .unlocked_ioctl = kvm_vcpu_ioctl,
1491 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001492 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001493 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001494};
1495
1496/*
1497 * Allocates an inode for the vcpu.
1498 */
1499static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1500{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001501 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001502}
1503
Avi Kivityc5ea7662007-02-20 18:41:05 +02001504/*
1505 * Creates some virtual cpus. Good luck creating more than one.
1506 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001507static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001508{
1509 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001510 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001511
Gleb Natapov73880c82009-06-09 15:56:28 +03001512 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001513 if (IS_ERR(vcpu))
1514 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001515
Avi Kivity15ad7142007-07-11 18:17:21 +03001516 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1517
Avi Kivity26e52152007-11-20 15:30:24 +02001518 r = kvm_arch_vcpu_setup(vcpu);
1519 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001520 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001521
Shaohua Li11ec2802007-07-23 14:51:37 +08001522 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001523 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1524 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001525 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001526 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001527
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001528 kvm_for_each_vcpu(r, v, kvm)
1529 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001530 r = -EEXIST;
1531 goto vcpu_destroy;
1532 }
1533
1534 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001535
1536 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001537 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001538 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001539 if (r < 0) {
1540 kvm_put_kvm(kvm);
1541 goto vcpu_destroy;
1542 }
1543
1544 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1545 smp_wmb();
1546 atomic_inc(&kvm->online_vcpus);
1547
1548#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1549 if (kvm->bsp_vcpu_id == id)
1550 kvm->bsp_vcpu = vcpu;
1551#endif
1552 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001553 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001554
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001555vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001556 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001557 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001558 return r;
1559}
1560
Avi Kivity1961d272007-03-05 19:46:05 +02001561static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1562{
1563 if (sigset) {
1564 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1565 vcpu->sigset_active = 1;
1566 vcpu->sigset = *sigset;
1567 } else
1568 vcpu->sigset_active = 0;
1569 return 0;
1570}
1571
Avi Kivitybccf2152007-02-21 18:04:26 +02001572static long kvm_vcpu_ioctl(struct file *filp,
1573 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574{
Avi Kivitybccf2152007-02-21 18:04:26 +02001575 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001576 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001577 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001578 struct kvm_fpu *fpu = NULL;
1579 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001581 if (vcpu->kvm->mm != current->mm)
1582 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001583
1584#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1585 /*
1586 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1587 * so vcpu_load() would break it.
1588 */
1589 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1590 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1591#endif
1592
1593
1594 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001596 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001597 r = -EINVAL;
1598 if (arg)
1599 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001600 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001601 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001604 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001606 r = -ENOMEM;
1607 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1608 if (!kvm_regs)
1609 goto out;
1610 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001612 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001614 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1615 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001617out_free1:
1618 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 break;
1620 }
1621 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001622 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001624 r = -ENOMEM;
1625 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1626 if (!kvm_regs)
1627 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001629 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1630 goto out_free2;
1631 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001633 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001635out_free2:
1636 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 break;
1638 }
1639 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001640 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1641 r = -ENOMEM;
1642 if (!kvm_sregs)
1643 goto out;
1644 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645 if (r)
1646 goto out;
1647 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001648 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 goto out;
1650 r = 0;
1651 break;
1652 }
1653 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001654 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1655 r = -ENOMEM;
1656 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001658 r = -EFAULT;
1659 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1660 goto out;
1661 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 if (r)
1663 goto out;
1664 r = 0;
1665 break;
1666 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001667 case KVM_GET_MP_STATE: {
1668 struct kvm_mp_state mp_state;
1669
1670 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1671 if (r)
1672 goto out;
1673 r = -EFAULT;
1674 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1675 goto out;
1676 r = 0;
1677 break;
1678 }
1679 case KVM_SET_MP_STATE: {
1680 struct kvm_mp_state mp_state;
1681
1682 r = -EFAULT;
1683 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1684 goto out;
1685 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1686 if (r)
1687 goto out;
1688 r = 0;
1689 break;
1690 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 case KVM_TRANSLATE: {
1692 struct kvm_translation tr;
1693
1694 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001695 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001697 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 if (r)
1699 goto out;
1700 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001701 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 goto out;
1703 r = 0;
1704 break;
1705 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001706 case KVM_SET_GUEST_DEBUG: {
1707 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708
1709 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001710 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001712 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 if (r)
1714 goto out;
1715 r = 0;
1716 break;
1717 }
Avi Kivity1961d272007-03-05 19:46:05 +02001718 case KVM_SET_SIGNAL_MASK: {
1719 struct kvm_signal_mask __user *sigmask_arg = argp;
1720 struct kvm_signal_mask kvm_sigmask;
1721 sigset_t sigset, *p;
1722
1723 p = NULL;
1724 if (argp) {
1725 r = -EFAULT;
1726 if (copy_from_user(&kvm_sigmask, argp,
1727 sizeof kvm_sigmask))
1728 goto out;
1729 r = -EINVAL;
1730 if (kvm_sigmask.len != sizeof sigset)
1731 goto out;
1732 r = -EFAULT;
1733 if (copy_from_user(&sigset, sigmask_arg->sigset,
1734 sizeof sigset))
1735 goto out;
1736 p = &sigset;
1737 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001738 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001739 break;
1740 }
Avi Kivityb8836732007-04-01 16:34:31 +03001741 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001742 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1743 r = -ENOMEM;
1744 if (!fpu)
1745 goto out;
1746 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001747 if (r)
1748 goto out;
1749 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001750 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001751 goto out;
1752 r = 0;
1753 break;
1754 }
1755 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001756 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1757 r = -ENOMEM;
1758 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001759 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001760 r = -EFAULT;
1761 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1762 goto out;
1763 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001764 if (r)
1765 goto out;
1766 r = 0;
1767 break;
1768 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001769 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001770 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001771 }
1772out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001773 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001774 kfree(fpu);
1775 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001776 return r;
1777}
1778
1779static long kvm_vm_ioctl(struct file *filp,
1780 unsigned int ioctl, unsigned long arg)
1781{
1782 struct kvm *kvm = filp->private_data;
1783 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001784 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001785
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001786 if (kvm->mm != current->mm)
1787 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001788 switch (ioctl) {
1789 case KVM_CREATE_VCPU:
1790 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1791 if (r < 0)
1792 goto out;
1793 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001794 case KVM_SET_USER_MEMORY_REGION: {
1795 struct kvm_userspace_memory_region kvm_userspace_mem;
1796
1797 r = -EFAULT;
1798 if (copy_from_user(&kvm_userspace_mem, argp,
1799 sizeof kvm_userspace_mem))
1800 goto out;
1801
1802 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 if (r)
1804 goto out;
1805 break;
1806 }
1807 case KVM_GET_DIRTY_LOG: {
1808 struct kvm_dirty_log log;
1809
1810 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001811 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001813 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 if (r)
1815 goto out;
1816 break;
1817 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001818#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1819 case KVM_REGISTER_COALESCED_MMIO: {
1820 struct kvm_coalesced_mmio_zone zone;
1821 r = -EFAULT;
1822 if (copy_from_user(&zone, argp, sizeof zone))
1823 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001824 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1825 if (r)
1826 goto out;
1827 r = 0;
1828 break;
1829 }
1830 case KVM_UNREGISTER_COALESCED_MMIO: {
1831 struct kvm_coalesced_mmio_zone zone;
1832 r = -EFAULT;
1833 if (copy_from_user(&zone, argp, sizeof zone))
1834 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001835 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1836 if (r)
1837 goto out;
1838 r = 0;
1839 break;
1840 }
1841#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001842 case KVM_IRQFD: {
1843 struct kvm_irqfd data;
1844
1845 r = -EFAULT;
1846 if (copy_from_user(&data, argp, sizeof data))
1847 goto out;
1848 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1849 break;
1850 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001851 case KVM_IOEVENTFD: {
1852 struct kvm_ioeventfd data;
1853
1854 r = -EFAULT;
1855 if (copy_from_user(&data, argp, sizeof data))
1856 goto out;
1857 r = kvm_ioeventfd(kvm, &data);
1858 break;
1859 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001860#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1861 case KVM_SET_BOOT_CPU_ID:
1862 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001863 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001864 if (atomic_read(&kvm->online_vcpus) != 0)
1865 r = -EBUSY;
1866 else
1867 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001868 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001869 break;
1870#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001871 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001872 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001873 if (r == -ENOTTY)
1874 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001875 }
1876out:
1877 return r;
1878}
1879
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001880#ifdef CONFIG_COMPAT
1881struct compat_kvm_dirty_log {
1882 __u32 slot;
1883 __u32 padding1;
1884 union {
1885 compat_uptr_t dirty_bitmap; /* one bit per page */
1886 __u64 padding2;
1887 };
1888};
1889
1890static long kvm_vm_compat_ioctl(struct file *filp,
1891 unsigned int ioctl, unsigned long arg)
1892{
1893 struct kvm *kvm = filp->private_data;
1894 int r;
1895
1896 if (kvm->mm != current->mm)
1897 return -EIO;
1898 switch (ioctl) {
1899 case KVM_GET_DIRTY_LOG: {
1900 struct compat_kvm_dirty_log compat_log;
1901 struct kvm_dirty_log log;
1902
1903 r = -EFAULT;
1904 if (copy_from_user(&compat_log, (void __user *)arg,
1905 sizeof(compat_log)))
1906 goto out;
1907 log.slot = compat_log.slot;
1908 log.padding1 = compat_log.padding1;
1909 log.padding2 = compat_log.padding2;
1910 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1911
1912 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1913 if (r)
1914 goto out;
1915 break;
1916 }
1917 default:
1918 r = kvm_vm_ioctl(filp, ioctl, arg);
1919 }
1920
1921out:
1922 return r;
1923}
1924#endif
1925
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001926static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001927{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001928 struct page *page[1];
1929 unsigned long addr;
1930 int npages;
1931 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001932 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001933
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001934 addr = gfn_to_hva(kvm, gfn);
1935 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001936 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001937
1938 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1939 NULL);
1940 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001941 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001942
1943 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001944 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001945}
1946
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001947static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001948 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001949};
1950
1951static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1952{
1953 vma->vm_ops = &kvm_vm_vm_ops;
1954 return 0;
1955}
1956
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001957static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001958 .release = kvm_vm_release,
1959 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001960#ifdef CONFIG_COMPAT
1961 .compat_ioctl = kvm_vm_compat_ioctl,
1962#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001963 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001964 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001965};
1966
1967static int kvm_dev_ioctl_create_vm(void)
1968{
Heiko Carstensaac87632010-10-27 17:22:10 +02001969 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001970 struct kvm *kvm;
1971
Avi Kivityf17abe92007-02-21 19:28:04 +02001972 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001973 if (IS_ERR(kvm))
1974 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001975#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1976 r = kvm_coalesced_mmio_init(kvm);
1977 if (r < 0) {
1978 kvm_put_kvm(kvm);
1979 return r;
1980 }
1981#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02001982 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1983 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001984 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001985
Heiko Carstensaac87632010-10-27 17:22:10 +02001986 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001987}
1988
Avi Kivity1a811b62008-12-08 18:25:27 +02001989static long kvm_dev_ioctl_check_extension_generic(long arg)
1990{
1991 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001992 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001993 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001994 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001995#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1996 case KVM_CAP_SET_BOOT_CPU_ID:
1997#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001998 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001999 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002000#ifdef CONFIG_HAVE_KVM_IRQCHIP
2001 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002002 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002003#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002004 default:
2005 break;
2006 }
2007 return kvm_dev_ioctl_check_extension(arg);
2008}
2009
Avi Kivityf17abe92007-02-21 19:28:04 +02002010static long kvm_dev_ioctl(struct file *filp,
2011 unsigned int ioctl, unsigned long arg)
2012{
Avi Kivity07c45a32007-03-07 13:05:38 +02002013 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002014
2015 switch (ioctl) {
2016 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002017 r = -EINVAL;
2018 if (arg)
2019 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002020 r = KVM_API_VERSION;
2021 break;
2022 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002023 r = -EINVAL;
2024 if (arg)
2025 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002026 r = kvm_dev_ioctl_create_vm();
2027 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002028 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002029 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002030 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002031 case KVM_GET_VCPU_MMAP_SIZE:
2032 r = -EINVAL;
2033 if (arg)
2034 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002035 r = PAGE_SIZE; /* struct kvm_run */
2036#ifdef CONFIG_X86
2037 r += PAGE_SIZE; /* pio data page */
2038#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002039#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2040 r += PAGE_SIZE; /* coalesced mmio ring page */
2041#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002042 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002043 case KVM_TRACE_ENABLE:
2044 case KVM_TRACE_PAUSE:
2045 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002046 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002047 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002049 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050 }
2051out:
2052 return r;
2053}
2054
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 .unlocked_ioctl = kvm_dev_ioctl,
2057 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002058 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059};
2060
2061static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002062 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 "kvm",
2064 &kvm_chardev_ops,
2065};
2066
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002067static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002068{
2069 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002070 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002071
Rusty Russell7f59f492008-12-07 21:25:45 +10302072 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002073 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002074
Rusty Russell7f59f492008-12-07 21:25:45 +10302075 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002076
2077 r = kvm_arch_hardware_enable(NULL);
2078
2079 if (r) {
2080 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2081 atomic_inc(&hardware_enable_failed);
2082 printk(KERN_INFO "kvm: enabling virtualization on "
2083 "CPU%d failed\n", cpu);
2084 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002085}
2086
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002087static void hardware_enable(void *junk)
2088{
2089 spin_lock(&kvm_lock);
2090 hardware_enable_nolock(junk);
2091 spin_unlock(&kvm_lock);
2092}
2093
2094static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002095{
2096 int cpu = raw_smp_processor_id();
2097
Rusty Russell7f59f492008-12-07 21:25:45 +10302098 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002099 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302100 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002101 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002102}
2103
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002104static void hardware_disable(void *junk)
2105{
2106 spin_lock(&kvm_lock);
2107 hardware_disable_nolock(junk);
2108 spin_unlock(&kvm_lock);
2109}
2110
Alexander Graf10474ae2009-09-15 11:37:46 +02002111static void hardware_disable_all_nolock(void)
2112{
2113 BUG_ON(!kvm_usage_count);
2114
2115 kvm_usage_count--;
2116 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002117 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002118}
2119
2120static void hardware_disable_all(void)
2121{
2122 spin_lock(&kvm_lock);
2123 hardware_disable_all_nolock();
2124 spin_unlock(&kvm_lock);
2125}
2126
2127static int hardware_enable_all(void)
2128{
2129 int r = 0;
2130
2131 spin_lock(&kvm_lock);
2132
2133 kvm_usage_count++;
2134 if (kvm_usage_count == 1) {
2135 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002136 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002137
2138 if (atomic_read(&hardware_enable_failed)) {
2139 hardware_disable_all_nolock();
2140 r = -EBUSY;
2141 }
2142 }
2143
2144 spin_unlock(&kvm_lock);
2145
2146 return r;
2147}
2148
Avi Kivity774c47f2007-02-12 00:54:47 -08002149static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2150 void *v)
2151{
2152 int cpu = (long)v;
2153
Alexander Graf10474ae2009-09-15 11:37:46 +02002154 if (!kvm_usage_count)
2155 return NOTIFY_OK;
2156
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002157 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002158 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002159 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002160 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2161 cpu);
2162 hardware_disable(NULL);
2163 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002164 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002165 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2166 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002167 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002168 break;
2169 }
2170 return NOTIFY_OK;
2171}
2172
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002173
2174asmlinkage void kvm_handle_fault_on_reboot(void)
2175{
Avi Kivityca242ac2010-09-21 19:59:43 +02002176 if (kvm_rebooting) {
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002177 /* spin while reset goes on */
Avi Kivityca242ac2010-09-21 19:59:43 +02002178 local_irq_enable();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002179 while (true)
Avi Kivity624d84c2010-09-21 19:59:44 +02002180 cpu_relax();
Avi Kivityca242ac2010-09-21 19:59:43 +02002181 }
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002182 /* Fault while not rebooting. We want the trace. */
2183 BUG();
2184}
2185EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2186
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002187static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002188 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002189{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002190 /*
2191 * Some (well, at least mine) BIOSes hang on reboot if
2192 * in vmx root mode.
2193 *
2194 * And Intel TXT required VMX off for all cpu when system shutdown.
2195 */
2196 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2197 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002198 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002199 return NOTIFY_OK;
2200}
2201
2202static struct notifier_block kvm_reboot_notifier = {
2203 .notifier_call = kvm_reboot,
2204 .priority = 0,
2205};
2206
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002207static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002208{
2209 int i;
2210
2211 for (i = 0; i < bus->dev_count; i++) {
2212 struct kvm_io_device *pos = bus->devs[i];
2213
2214 kvm_iodevice_destructor(pos);
2215 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002216 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002217}
2218
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002219/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002220int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002221 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002222{
2223 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002224 struct kvm_io_bus *bus;
2225
2226 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002227 for (i = 0; i < bus->dev_count; i++)
2228 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2229 return 0;
2230 return -EOPNOTSUPP;
2231}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002232
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002233/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002234int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2235 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002236{
2237 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002238 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002239
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002240 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002241 for (i = 0; i < bus->dev_count; i++)
2242 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2243 return 0;
2244 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002245}
2246
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002247/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002248int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2249 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002250{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002251 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002252
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002253 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002254 if (bus->dev_count > NR_IOBUS_DEVS-1)
2255 return -ENOSPC;
2256
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002257 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2258 if (!new_bus)
2259 return -ENOMEM;
2260 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2261 new_bus->devs[new_bus->dev_count++] = dev;
2262 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2263 synchronize_srcu_expedited(&kvm->srcu);
2264 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002265
2266 return 0;
2267}
2268
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002269/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002270int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2271 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002272{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002273 int i, r;
2274 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002275
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002276 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2277 if (!new_bus)
2278 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002279
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002280 bus = kvm->buses[bus_idx];
2281 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2282
2283 r = -ENOENT;
2284 for (i = 0; i < new_bus->dev_count; i++)
2285 if (new_bus->devs[i] == dev) {
2286 r = 0;
2287 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002288 break;
2289 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002290
2291 if (r) {
2292 kfree(new_bus);
2293 return r;
2294 }
2295
2296 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2297 synchronize_srcu_expedited(&kvm->srcu);
2298 kfree(bus);
2299 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002300}
2301
Avi Kivity774c47f2007-02-12 00:54:47 -08002302static struct notifier_block kvm_cpu_notifier = {
2303 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002304};
2305
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002306static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002307{
2308 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002309 struct kvm *kvm;
2310
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002311 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002312 spin_lock(&kvm_lock);
2313 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002314 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002315 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002316 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002317}
2318
2319DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2320
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002321static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002322{
2323 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002324 struct kvm *kvm;
2325 struct kvm_vcpu *vcpu;
2326 int i;
2327
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002328 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002329 spin_lock(&kvm_lock);
2330 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002331 kvm_for_each_vcpu(i, vcpu, kvm)
2332 *val += *(u32 *)((void *)vcpu + offset);
2333
Avi Kivity1165f5f2007-04-19 17:27:43 +03002334 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002335 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002336}
2337
Avi Kivityba1389b2007-11-18 16:24:12 +02002338DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2339
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002340static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002341 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2342 [KVM_STAT_VM] = &vm_stat_fops,
2343};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002344
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002345static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346{
2347 struct kvm_stats_debugfs_item *p;
2348
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002349 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002351 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002352 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002353 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354}
2355
2356static void kvm_exit_debug(void)
2357{
2358 struct kvm_stats_debugfs_item *p;
2359
2360 for (p = debugfs_entries; p->name; ++p)
2361 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002362 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363}
2364
Avi Kivity59ae6c62007-02-12 00:54:48 -08002365static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2366{
Alexander Graf10474ae2009-09-15 11:37:46 +02002367 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002368 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002369 return 0;
2370}
2371
2372static int kvm_resume(struct sys_device *dev)
2373{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002374 if (kvm_usage_count) {
2375 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002376 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002377 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002378 return 0;
2379}
2380
2381static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002382 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002383 .suspend = kvm_suspend,
2384 .resume = kvm_resume,
2385};
2386
2387static struct sys_device kvm_sysdev = {
2388 .id = 0,
2389 .cls = &kvm_sysdev_class,
2390};
2391
Izik Eiduscea7bb22007-10-17 19:17:48 +02002392struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002393pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394
Avi Kivity15ad7142007-07-11 18:17:21 +03002395static inline
2396struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2397{
2398 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2399}
2400
2401static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2402{
2403 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2404
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002405 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002406}
2407
2408static void kvm_sched_out(struct preempt_notifier *pn,
2409 struct task_struct *next)
2410{
2411 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2412
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002413 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002414}
2415
Avi Kivity0ee75be2010-04-28 15:39:01 +03002416int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002417 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418{
2419 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002420 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002422 r = kvm_arch_init(opaque);
2423 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002424 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002425
2426 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2427
2428 if (bad_page == NULL) {
2429 r = -ENOMEM;
2430 goto out;
2431 }
2432
Anthony Liguori35149e22008-04-02 14:46:56 -05002433 bad_pfn = page_to_pfn(bad_page);
2434
Huang Yingbf998152010-05-31 14:28:19 +08002435 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2436
2437 if (hwpoison_page == NULL) {
2438 r = -ENOMEM;
2439 goto out_free_0;
2440 }
2441
2442 hwpoison_pfn = page_to_pfn(hwpoison_page);
2443
Gleb Natapovedba23e2010-07-07 20:16:45 +03002444 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2445
2446 if (fault_page == NULL) {
2447 r = -ENOMEM;
2448 goto out_free_0;
2449 }
2450
2451 fault_pfn = page_to_pfn(fault_page);
2452
Avi Kivity8437a612009-06-06 14:52:35 -07002453 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302454 r = -ENOMEM;
2455 goto out_free_0;
2456 }
2457
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002458 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302460 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461
Yang, Sheng002c7f72007-07-31 14:23:01 +03002462 for_each_online_cpu(cpu) {
2463 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002464 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002465 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002466 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002467 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002468 }
2469
Avi Kivity774c47f2007-02-12 00:54:47 -08002470 r = register_cpu_notifier(&kvm_cpu_notifier);
2471 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002472 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 register_reboot_notifier(&kvm_reboot_notifier);
2474
Avi Kivity59ae6c62007-02-12 00:54:48 -08002475 r = sysdev_class_register(&kvm_sysdev_class);
2476 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002477 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002478
2479 r = sysdev_register(&kvm_sysdev);
2480 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002481 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002482
Rusty Russellc16f8622007-07-30 21:12:19 +10002483 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002484 if (!vcpu_align)
2485 vcpu_align = __alignof__(struct kvm_vcpu);
2486 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002487 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002488 if (!kvm_vcpu_cache) {
2489 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002490 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002491 }
2492
Gleb Natapovaf585b92010-10-14 11:22:46 +02002493 r = kvm_async_pf_init();
2494 if (r)
2495 goto out_free;
2496
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002498 kvm_vm_fops.owner = module;
2499 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500
2501 r = misc_register(&kvm_dev);
2502 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002503 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002504 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 }
2506
Avi Kivity15ad7142007-07-11 18:17:21 +03002507 kvm_preempt_ops.sched_in = kvm_sched_in;
2508 kvm_preempt_ops.sched_out = kvm_sched_out;
2509
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002510 kvm_init_debug();
2511
Avi Kivityc7addb92007-09-16 18:58:32 +02002512 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513
Gleb Natapovaf585b92010-10-14 11:22:46 +02002514out_unreg:
2515 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002517 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002518out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002519 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002520out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002521 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002522out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002524 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002525out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002526out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002527 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302528out_free_0a:
2529 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002530out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002531 if (fault_page)
2532 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002533 if (hwpoison_page)
2534 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002535 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002536out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002537 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002538out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539 return r;
2540}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002541EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002543void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002545 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002547 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002548 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002549 sysdev_unregister(&kvm_sysdev);
2550 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002552 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002553 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002554 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002555 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302556 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002557 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002558 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002560EXPORT_SYMBOL_GPL(kvm_exit);