blob: fce0578eab0efb5b9d6e58b5cd74708f96207b62 [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{
171 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
172 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300173}
174
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500175void kvm_reload_remote_mmus(struct kvm *kvm)
176{
Rusty Russell49846892008-12-08 20:26:24 +1030177 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500178}
179
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000180int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
181{
182 struct page *page;
183 int r;
184
185 mutex_init(&vcpu->mutex);
186 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000187 vcpu->kvm = kvm;
188 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300189 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200190 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191
192 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
193 if (!page) {
194 r = -ENOMEM;
195 goto fail;
196 }
197 vcpu->run = page_address(page);
198
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800199 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000200 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800201 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000202 return 0;
203
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000204fail_free_run:
205 free_page((unsigned long)vcpu->run);
206fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000207 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000208}
209EXPORT_SYMBOL_GPL(kvm_vcpu_init);
210
211void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
212{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800213 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000214 free_page((unsigned long)vcpu->run);
215}
216EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
217
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200218#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
219static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
220{
221 return container_of(mn, struct kvm, mmu_notifier);
222}
223
224static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
225 struct mm_struct *mm,
226 unsigned long address)
227{
228 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200229 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200230
231 /*
232 * When ->invalidate_page runs, the linux pte has been zapped
233 * already but the page is still allocated until
234 * ->invalidate_page returns. So if we increase the sequence
235 * here the kvm page fault will notice if the spte can't be
236 * established because the page is going to be freed. If
237 * instead the kvm page fault establishes the spte before
238 * ->invalidate_page runs, kvm_unmap_hva will release it
239 * before returning.
240 *
241 * The sequence increase only need to be seen at spin_unlock
242 * time, and not at spin_lock time.
243 *
244 * Increasing the sequence after the spin_unlock would be
245 * unsafe because the kvm page fault could then establish the
246 * pte after kvm_unmap_hva returned, without noticing the page
247 * is going to be freed.
248 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200249 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200250 spin_lock(&kvm->mmu_lock);
251 kvm->mmu_notifier_seq++;
252 need_tlb_flush = kvm_unmap_hva(kvm, address);
253 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200254 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200255
256 /* we've to flush the tlb before the pages can be freed */
257 if (need_tlb_flush)
258 kvm_flush_remote_tlbs(kvm);
259
260}
261
Izik Eidus3da0dd42009-09-23 21:47:18 +0300262static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
263 struct mm_struct *mm,
264 unsigned long address,
265 pte_t pte)
266{
267 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200268 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300269
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200270 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300271 spin_lock(&kvm->mmu_lock);
272 kvm->mmu_notifier_seq++;
273 kvm_set_spte_hva(kvm, address, pte);
274 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200275 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300276}
277
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200278static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
279 struct mm_struct *mm,
280 unsigned long start,
281 unsigned long end)
282{
283 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200284 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200285
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200286 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200287 spin_lock(&kvm->mmu_lock);
288 /*
289 * The count increase must become visible at unlock time as no
290 * spte can be established without taking the mmu_lock and
291 * count is also read inside the mmu_lock critical section.
292 */
293 kvm->mmu_notifier_count++;
294 for (; start < end; start += PAGE_SIZE)
295 need_tlb_flush |= kvm_unmap_hva(kvm, start);
296 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200297 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200298
299 /* we've to flush the tlb before the pages can be freed */
300 if (need_tlb_flush)
301 kvm_flush_remote_tlbs(kvm);
302}
303
304static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
305 struct mm_struct *mm,
306 unsigned long start,
307 unsigned long end)
308{
309 struct kvm *kvm = mmu_notifier_to_kvm(mn);
310
311 spin_lock(&kvm->mmu_lock);
312 /*
313 * This sequence increase will notify the kvm page fault that
314 * the page that is going to be mapped in the spte could have
315 * been freed.
316 */
317 kvm->mmu_notifier_seq++;
318 /*
319 * The above sequence increase must be visible before the
320 * below count decrease but both values are read by the kvm
321 * page fault under mmu_lock spinlock so we don't need to add
322 * a smb_wmb() here in between the two.
323 */
324 kvm->mmu_notifier_count--;
325 spin_unlock(&kvm->mmu_lock);
326
327 BUG_ON(kvm->mmu_notifier_count < 0);
328}
329
330static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
331 struct mm_struct *mm,
332 unsigned long address)
333{
334 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200335 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200336
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200337 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200338 spin_lock(&kvm->mmu_lock);
339 young = kvm_age_hva(kvm, address);
340 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200341 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200342
343 if (young)
344 kvm_flush_remote_tlbs(kvm);
345
346 return young;
347}
348
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100349static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
350 struct mm_struct *mm)
351{
352 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800353 int idx;
354
355 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100356 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800357 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100358}
359
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200360static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
361 .invalidate_page = kvm_mmu_notifier_invalidate_page,
362 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
363 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
364 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300365 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100366 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200367};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200368
369static int kvm_init_mmu_notifier(struct kvm *kvm)
370{
371 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
372 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
373}
374
375#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
376
377static int kvm_init_mmu_notifier(struct kvm *kvm)
378{
379 return 0;
380}
381
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200382#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
383
Avi Kivityf17abe92007-02-21 19:28:04 +0200384static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200386 int r = 0, i;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800387 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800389 if (IS_ERR(kvm))
390 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200391
392 r = hardware_enable_all();
393 if (r)
394 goto out_err_nodisable;
395
Avi Kivity75858a82009-01-04 17:10:50 +0200396#ifdef CONFIG_HAVE_KVM_IRQCHIP
397 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300398 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200399#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200401 r = -ENOMEM;
402 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
403 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100404 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200405 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100406 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200407 for (i = 0; i < KVM_NR_BUSES; i++) {
408 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
409 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100410 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200411 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200412 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200413
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200414 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100415 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200416 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200417
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200418 kvm->mm = current->mm;
419 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500420 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200421 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400422 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800423 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300424 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200425 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300426 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000427 spin_lock(&kvm_lock);
428 list_add(&kvm->vm_list, &vm_list);
429 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800430out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200431 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200432
433out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100434 cleanup_srcu_struct(&kvm->srcu);
435out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200436 hardware_disable_all();
437out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200438 for (i = 0; i < KVM_NR_BUSES; i++)
439 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200440 kfree(kvm->memslots);
Alexander Graf10474ae2009-09-15 11:37:46 +0200441 kfree(kvm);
442 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200443}
444
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900445static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
446{
447 if (!memslot->dirty_bitmap)
448 return;
449
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900450 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
451 vfree(memslot->dirty_bitmap_head);
452 else
453 kfree(memslot->dirty_bitmap_head);
454
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900455 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900456 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900457}
458
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459/*
460 * Free any memory in @free but not in @dont.
461 */
462static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
463 struct kvm_memory_slot *dont)
464{
Joerg Roedelec04b262009-06-19 15:16:23 +0200465 int i;
466
Izik Eidus290fc382007-09-27 14:11:22 +0200467 if (!dont || free->rmap != dont->rmap)
468 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469
470 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900471 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472
Joerg Roedelec04b262009-06-19 15:16:23 +0200473
474 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
475 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
476 vfree(free->lpage_info[i]);
477 free->lpage_info[i] = NULL;
478 }
479 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300480
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500482 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483}
484
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800485void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486{
487 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200488 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200490 for (i = 0; i < slots->nmemslots; ++i)
491 kvm_free_physmem_slot(&slots->memslots[i], NULL);
492
493 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494}
495
Avi Kivityf17abe92007-02-21 19:28:04 +0200496static void kvm_destroy_vm(struct kvm *kvm)
497{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200498 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200499 struct mm_struct *mm = kvm->mm;
500
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800501 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800502 spin_lock(&kvm_lock);
503 list_del(&kvm->vm_list);
504 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200505 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200506 for (i = 0; i < KVM_NR_BUSES; i++)
507 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200508 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200509#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
510 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200511#else
512 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200513#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800514 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200515 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200516 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200517}
518
Izik Eidusd39f13b2008-03-30 16:01:25 +0300519void kvm_get_kvm(struct kvm *kvm)
520{
521 atomic_inc(&kvm->users_count);
522}
523EXPORT_SYMBOL_GPL(kvm_get_kvm);
524
525void kvm_put_kvm(struct kvm *kvm)
526{
527 if (atomic_dec_and_test(&kvm->users_count))
528 kvm_destroy_vm(kvm);
529}
530EXPORT_SYMBOL_GPL(kvm_put_kvm);
531
532
Avi Kivityf17abe92007-02-21 19:28:04 +0200533static int kvm_vm_release(struct inode *inode, struct file *filp)
534{
535 struct kvm *kvm = filp->private_data;
536
Gregory Haskins721eecb2009-05-20 10:30:49 -0400537 kvm_irqfd_release(kvm);
538
Izik Eidusd39f13b2008-03-30 16:01:25 +0300539 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540 return 0;
541}
542
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900543/*
544 * Allocation size is twice as large as the actual dirty bitmap size.
545 * This makes it possible to do double buffering: see x86's
546 * kvm_vm_ioctl_get_dirty_log().
547 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900548static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
549{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900550 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900551
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900552 if (dirty_bytes > PAGE_SIZE)
553 memslot->dirty_bitmap = vzalloc(dirty_bytes);
554 else
555 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
556
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900557 if (!memslot->dirty_bitmap)
558 return -ENOMEM;
559
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900560 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900561 return 0;
562}
563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 * Allocate some memory and give it an address in the guest physical address
566 * space.
567 *
568 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800569 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500570 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800572int __kvm_set_memory_region(struct kvm *kvm,
573 struct kvm_userspace_memory_region *mem,
574 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200576 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200578 unsigned long npages;
579 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 struct kvm_memory_slot *memslot;
581 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200582 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583
584 r = -EINVAL;
585 /* General sanity checks */
586 if (mem->memory_size & (PAGE_SIZE - 1))
587 goto out;
588 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
589 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800590 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600591 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200592 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 goto out;
594 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
595 goto out;
596
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200597 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
599 npages = mem->memory_size >> PAGE_SHIFT;
600
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900601 r = -EINVAL;
602 if (npages > KVM_MEM_MAX_NR_PAGES)
603 goto out;
604
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 if (!npages)
606 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
607
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608 new = old = *memslot;
609
Avi Kivitye36d96f2010-06-21 10:56:36 +0300610 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611 new.base_gfn = base_gfn;
612 new.npages = npages;
613 new.flags = mem->flags;
614
615 /* Disallow changing a memory slot's size. */
616 r = -EINVAL;
617 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800618 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619
620 /* Check for overlaps */
621 r = -EEXIST;
622 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200623 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200625 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 continue;
627 if (!((base_gfn + npages <= s->base_gfn) ||
628 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800629 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 /* Free page dirty bitmap if unneeded */
633 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000634 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635
636 r = -ENOMEM;
637
638 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200639#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500640 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900641 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200642
643 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800644 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200645
Izik Eidus80b14b52007-10-25 11:54:04 +0200646 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200647 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200649 if (!npages)
650 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300651
Joerg Roedelec04b262009-06-19 15:16:23 +0200652 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200653 unsigned long ugfn;
654 unsigned long j;
655 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200656 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300657
Joerg Roedelec04b262009-06-19 15:16:23 +0200658 /* Avoid unused variable warning if no large pages */
659 (void)level;
660
661 if (new.lpage_info[i])
662 continue;
663
Joerg Roedel82855412010-07-01 16:00:11 +0200664 lpages = 1 + ((base_gfn + npages - 1)
665 >> KVM_HPAGE_GFN_SHIFT(level));
666 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200667
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900668 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200669
670 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300671 goto out_free;
672
Joerg Roedel82855412010-07-01 16:00:11 +0200673 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200674 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200675 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200676 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300677 ugfn = new.userspace_addr >> PAGE_SHIFT;
678 /*
679 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300680 * other, or if explicitly asked to, disable large page
681 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300682 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200683 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300684 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200685 for (j = 0; j < lpages; ++j)
686 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300687 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688
Joerg Roedelec04b262009-06-19 15:16:23 +0200689skip_lpage:
690
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 /* Allocate page dirty bitmap if needed */
692 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900693 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800694 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200695 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300696 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200697 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200699#else /* not defined CONFIG_S390 */
700 new.user_alloc = user_alloc;
701 if (user_alloc)
702 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200703#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200705 if (!npages) {
706 r = -ENOMEM;
707 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
708 if (!slots)
709 goto out_free;
710 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
711 if (mem->slot >= slots->nmemslots)
712 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200713 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200714 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
715
716 old_memslots = kvm->memslots;
717 rcu_assign_pointer(kvm->memslots, slots);
718 synchronize_srcu_expedited(&kvm->srcu);
719 /* From this point no new shadow pages pointing to a deleted
720 * memslot will be created.
721 *
722 * validation of sp->gfn happens in:
723 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
724 * - kvm_is_visible_gfn (mmu_check_roots)
725 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300726 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200727 kfree(old_memslots);
728 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300729
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200730 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
731 if (r)
732 goto out_free;
733
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200734 /* map the pages in iommu page table */
735 if (npages) {
736 r = kvm_iommu_map_pages(kvm, &new);
737 if (r)
738 goto out_free;
739 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200740
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200741 r = -ENOMEM;
742 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
743 if (!slots)
744 goto out_free;
745 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
746 if (mem->slot >= slots->nmemslots)
747 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200748 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200749
750 /* actual memory is freed via old in kvm_free_physmem_slot below */
751 if (!npages) {
752 new.rmap = NULL;
753 new.dirty_bitmap = NULL;
754 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
755 new.lpage_info[i] = NULL;
756 }
757
758 slots->memslots[mem->slot] = new;
759 old_memslots = kvm->memslots;
760 rcu_assign_pointer(kvm->memslots, slots);
761 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200763 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800764
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200765 kvm_free_physmem_slot(&old, &new);
766 kfree(old_memslots);
767
768 if (flush_shadow)
769 kvm_arch_flush_shadow(kvm);
770
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 return 0;
772
Sheng Yangf78e0e22007-10-29 09:40:42 +0800773out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 kvm_free_physmem_slot(&new, &old);
775out:
776 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200777
778}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800779EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
780
781int kvm_set_memory_region(struct kvm *kvm,
782 struct kvm_userspace_memory_region *mem,
783 int user_alloc)
784{
785 int r;
786
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200787 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800788 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200789 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800790 return r;
791}
Izik Eidus210c7c42007-10-24 23:52:57 +0200792EXPORT_SYMBOL_GPL(kvm_set_memory_region);
793
Carsten Otte1fe779f2007-10-29 16:08:35 +0100794int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
795 struct
796 kvm_userspace_memory_region *mem,
797 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200798{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200799 if (mem->slot >= KVM_MEMORY_SLOTS)
800 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200801 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802}
803
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800804int kvm_get_dirty_log(struct kvm *kvm,
805 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806{
807 struct kvm_memory_slot *memslot;
808 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900809 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 unsigned long any = 0;
811
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 r = -EINVAL;
813 if (log->slot >= KVM_MEMORY_SLOTS)
814 goto out;
815
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200816 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 r = -ENOENT;
818 if (!memslot->dirty_bitmap)
819 goto out;
820
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900821 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
Uri Lublincd1a4a92007-02-22 16:43:09 +0200823 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 any = memslot->dirty_bitmap[i];
825
826 r = -EFAULT;
827 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
828 goto out;
829
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800830 if (any)
831 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
833 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 return r;
836}
837
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300838void kvm_disable_largepages(void)
839{
840 largepages_enabled = false;
841}
842EXPORT_SYMBOL_GPL(kvm_disable_largepages);
843
Izik Eiduscea7bb22007-10-17 19:17:48 +0200844int is_error_page(struct page *page)
845{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300846 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200847}
848EXPORT_SYMBOL_GPL(is_error_page);
849
Anthony Liguori35149e22008-04-02 14:46:56 -0500850int is_error_pfn(pfn_t pfn)
851{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300852 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500853}
854EXPORT_SYMBOL_GPL(is_error_pfn);
855
Huang Yingbf998152010-05-31 14:28:19 +0800856int is_hwpoison_pfn(pfn_t pfn)
857{
858 return pfn == hwpoison_pfn;
859}
860EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
861
Gleb Natapovedba23e2010-07-07 20:16:45 +0300862int is_fault_pfn(pfn_t pfn)
863{
864 return pfn == fault_pfn;
865}
866EXPORT_SYMBOL_GPL(is_fault_pfn);
867
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200868static inline unsigned long bad_hva(void)
869{
870 return PAGE_OFFSET;
871}
872
873int kvm_is_error_hva(unsigned long addr)
874{
875 return addr == bad_hva();
876}
877EXPORT_SYMBOL_GPL(kvm_is_error_hva);
878
Gleb Natapov49c77542010-10-18 15:22:23 +0200879static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
880 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881{
882 int i;
883
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200884 for (i = 0; i < slots->nmemslots; ++i) {
885 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886
887 if (gfn >= memslot->base_gfn
888 && gfn < memslot->base_gfn + memslot->npages)
889 return memslot;
890 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000891 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892}
Gleb Natapov49c77542010-10-18 15:22:23 +0200893
894struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
895{
896 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
897}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300898EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899
Izik Eiduse0d62c72007-10-24 23:57:46 +0200900int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
901{
902 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800903 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200904
Izik Eiduse0d62c72007-10-24 23:57:46 +0200905 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200906 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200907
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200908 if (memslot->flags & KVM_MEMSLOT_INVALID)
909 continue;
910
Izik Eiduse0d62c72007-10-24 23:57:46 +0200911 if (gfn >= memslot->base_gfn
912 && gfn < memslot->base_gfn + memslot->npages)
913 return 1;
914 }
915 return 0;
916}
917EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
918
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100919unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
920{
921 struct vm_area_struct *vma;
922 unsigned long addr, size;
923
924 size = PAGE_SIZE;
925
926 addr = gfn_to_hva(kvm, gfn);
927 if (kvm_is_error_hva(addr))
928 return PAGE_SIZE;
929
930 down_read(&current->mm->mmap_sem);
931 vma = find_vma(current->mm, addr);
932 if (!vma)
933 goto out;
934
935 size = vma_kernel_pagesize(vma);
936
937out:
938 up_read(&current->mm->mmap_sem);
939
940 return size;
941}
942
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200943int memslot_id(struct kvm *kvm, gfn_t gfn)
944{
945 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800946 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200947 struct kvm_memory_slot *memslot = NULL;
948
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200949 for (i = 0; i < slots->nmemslots; ++i) {
950 memslot = &slots->memslots[i];
951
952 if (gfn >= memslot->base_gfn
953 && gfn < memslot->base_gfn + memslot->npages)
954 break;
955 }
956
957 return memslot - slots->memslots;
958}
959
Gleb Natapov49c77542010-10-18 15:22:23 +0200960static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +0800961 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +0200962{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200963 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200964 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +0800965
966 if (nr_pages)
967 *nr_pages = slot->npages - (gfn - slot->base_gfn);
968
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900969 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200970}
Xiao Guangrong48987782010-08-22 19:11:43 +0800971
972unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
973{
Gleb Natapov49c77542010-10-18 15:22:23 +0200974 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +0800975}
Sheng Yang0d150292008-04-25 21:44:50 +0800976EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200977
Gleb Natapov80300892010-10-19 18:13:41 +0200978static pfn_t get_fault_pfn(void)
979{
980 get_page(fault_page);
981 return fault_pfn;
982}
983
Gleb Natapovaf585b92010-10-14 11:22:46 +0200984static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200985 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300986{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500987 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +0200988 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500989 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300990
Gleb Natapovaf585b92010-10-14 11:22:46 +0200991 /* we can do it either atomically or asynchronously, not both */
992 BUG_ON(atomic && async);
993
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200994 BUG_ON(!write_fault && !writable);
995
996 if (writable)
997 *writable = true;
998
Gleb Natapovaf585b92010-10-14 11:22:46 +0200999 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001000 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001001
1002 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001003 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001004
1005 if (writable)
1006 *writable = write_fault;
1007
1008 npages = get_user_pages_fast(addr, 1, write_fault, page);
1009
1010 /* map read fault as writable if possible */
1011 if (unlikely(!write_fault) && npages == 1) {
1012 struct page *wpage[1];
1013
1014 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1015 if (npages == 1) {
1016 *writable = true;
1017 put_page(page[0]);
1018 page[0] = wpage[0];
1019 }
1020 npages = 1;
1021 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001022 }
Izik Eidus539cb662007-11-11 22:05:04 +02001023
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001024 if (unlikely(npages != 1)) {
1025 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001026
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001027 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001028 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001029
Huang Yingbbeb3402010-06-22 14:23:11 +08001030 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001031 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001032 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001033 get_page(hwpoison_page);
1034 return page_to_pfn(hwpoison_page);
1035 }
1036
Gleb Natapov80300892010-10-19 18:13:41 +02001037 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001038
Gleb Natapov80300892010-10-19 18:13:41 +02001039 if (vma == NULL)
1040 pfn = get_fault_pfn();
1041 else if ((vma->vm_flags & VM_PFNMAP)) {
1042 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1043 vma->vm_pgoff;
1044 BUG_ON(!kvm_is_mmio_pfn(pfn));
1045 } else {
1046 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001047 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001048 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001049 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001050 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001051 } else
1052 pfn = page_to_pfn(page[0]);
1053
1054 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001055}
1056
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001057pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1058{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001059 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001060}
1061EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1062
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001063static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1064 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001065{
1066 unsigned long addr;
1067
Gleb Natapovaf585b92010-10-14 11:22:46 +02001068 if (async)
1069 *async = false;
1070
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001071 addr = gfn_to_hva(kvm, gfn);
1072 if (kvm_is_error_hva(addr)) {
1073 get_page(bad_page);
1074 return page_to_pfn(bad_page);
1075 }
1076
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001077 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001078}
1079
1080pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1081{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001082 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001083}
1084EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1085
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001086pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1087 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001088{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001089 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001090}
1091EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1092
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001093pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1094{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001095 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001096}
Anthony Liguori35149e22008-04-02 14:46:56 -05001097EXPORT_SYMBOL_GPL(gfn_to_pfn);
1098
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001099pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1100 bool *writable)
1101{
1102 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1103}
1104EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1105
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001106pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1107 struct kvm_memory_slot *slot, gfn_t gfn)
1108{
1109 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001110 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001111}
1112
Xiao Guangrong48987782010-08-22 19:11:43 +08001113int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1114 int nr_pages)
1115{
1116 unsigned long addr;
1117 gfn_t entry;
1118
Gleb Natapov49c77542010-10-18 15:22:23 +02001119 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001120 if (kvm_is_error_hva(addr))
1121 return -1;
1122
1123 if (entry < nr_pages)
1124 return 0;
1125
1126 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1127}
1128EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1129
Anthony Liguori35149e22008-04-02 14:46:56 -05001130struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1131{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001132 pfn_t pfn;
1133
1134 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001135 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001136 return pfn_to_page(pfn);
1137
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001138 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001139
1140 get_page(bad_page);
1141 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001142}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001143
Avi Kivity954bbbc22007-03-30 14:02:32 +03001144EXPORT_SYMBOL_GPL(gfn_to_page);
1145
Izik Eidusb4231d62007-11-20 11:49:33 +02001146void kvm_release_page_clean(struct page *page)
1147{
Anthony Liguori35149e22008-04-02 14:46:56 -05001148 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001149}
1150EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1151
Anthony Liguori35149e22008-04-02 14:46:56 -05001152void kvm_release_pfn_clean(pfn_t pfn)
1153{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001154 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001155 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001156}
1157EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1158
Izik Eidusb4231d62007-11-20 11:49:33 +02001159void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001160{
Anthony Liguori35149e22008-04-02 14:46:56 -05001161 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001162}
Izik Eidusb4231d62007-11-20 11:49:33 +02001163EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001164
Anthony Liguori35149e22008-04-02 14:46:56 -05001165void kvm_release_pfn_dirty(pfn_t pfn)
1166{
1167 kvm_set_pfn_dirty(pfn);
1168 kvm_release_pfn_clean(pfn);
1169}
1170EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1171
1172void kvm_set_page_dirty(struct page *page)
1173{
1174 kvm_set_pfn_dirty(page_to_pfn(page));
1175}
1176EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1177
1178void kvm_set_pfn_dirty(pfn_t pfn)
1179{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001180 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001181 struct page *page = pfn_to_page(pfn);
1182 if (!PageReserved(page))
1183 SetPageDirty(page);
1184 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001185}
1186EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1187
1188void kvm_set_pfn_accessed(pfn_t pfn)
1189{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001190 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001191 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001192}
1193EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1194
1195void kvm_get_pfn(pfn_t pfn)
1196{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001197 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001198 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001199}
1200EXPORT_SYMBOL_GPL(kvm_get_pfn);
1201
Izik Eidus195aefd2007-10-01 22:14:18 +02001202static int next_segment(unsigned long len, int offset)
1203{
1204 if (len > PAGE_SIZE - offset)
1205 return PAGE_SIZE - offset;
1206 else
1207 return len;
1208}
1209
1210int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1211 int len)
1212{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001213 int r;
1214 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001215
Izik Eiduse0506bc2007-11-11 22:10:22 +02001216 addr = gfn_to_hva(kvm, gfn);
1217 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001218 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001219 r = copy_from_user(data, (void __user *)addr + offset, len);
1220 if (r)
1221 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001222 return 0;
1223}
1224EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1225
1226int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1227{
1228 gfn_t gfn = gpa >> PAGE_SHIFT;
1229 int seg;
1230 int offset = offset_in_page(gpa);
1231 int ret;
1232
1233 while ((seg = next_segment(len, offset)) != 0) {
1234 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1235 if (ret < 0)
1236 return ret;
1237 offset = 0;
1238 len -= seg;
1239 data += seg;
1240 ++gfn;
1241 }
1242 return 0;
1243}
1244EXPORT_SYMBOL_GPL(kvm_read_guest);
1245
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001246int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1247 unsigned long len)
1248{
1249 int r;
1250 unsigned long addr;
1251 gfn_t gfn = gpa >> PAGE_SHIFT;
1252 int offset = offset_in_page(gpa);
1253
1254 addr = gfn_to_hva(kvm, gfn);
1255 if (kvm_is_error_hva(addr))
1256 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001257 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001258 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001259 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001260 if (r)
1261 return -EFAULT;
1262 return 0;
1263}
1264EXPORT_SYMBOL(kvm_read_guest_atomic);
1265
Izik Eidus195aefd2007-10-01 22:14:18 +02001266int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1267 int offset, int len)
1268{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001269 int r;
1270 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001271
Izik Eiduse0506bc2007-11-11 22:10:22 +02001272 addr = gfn_to_hva(kvm, gfn);
1273 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001274 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001275 r = copy_to_user((void __user *)addr + offset, data, len);
1276 if (r)
1277 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001278 mark_page_dirty(kvm, gfn);
1279 return 0;
1280}
1281EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1282
1283int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1284 unsigned long len)
1285{
1286 gfn_t gfn = gpa >> PAGE_SHIFT;
1287 int seg;
1288 int offset = offset_in_page(gpa);
1289 int ret;
1290
1291 while ((seg = next_segment(len, offset)) != 0) {
1292 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1293 if (ret < 0)
1294 return ret;
1295 offset = 0;
1296 len -= seg;
1297 data += seg;
1298 ++gfn;
1299 }
1300 return 0;
1301}
1302
Gleb Natapov49c77542010-10-18 15:22:23 +02001303int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1304 gpa_t gpa)
1305{
1306 struct kvm_memslots *slots = kvm_memslots(kvm);
1307 int offset = offset_in_page(gpa);
1308 gfn_t gfn = gpa >> PAGE_SHIFT;
1309
1310 ghc->gpa = gpa;
1311 ghc->generation = slots->generation;
1312 ghc->memslot = __gfn_to_memslot(slots, gfn);
1313 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1314 if (!kvm_is_error_hva(ghc->hva))
1315 ghc->hva += offset;
1316 else
1317 return -EFAULT;
1318
1319 return 0;
1320}
1321EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1322
1323int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1324 void *data, unsigned long len)
1325{
1326 struct kvm_memslots *slots = kvm_memslots(kvm);
1327 int r;
1328
1329 if (slots->generation != ghc->generation)
1330 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1331
1332 if (kvm_is_error_hva(ghc->hva))
1333 return -EFAULT;
1334
1335 r = copy_to_user((void __user *)ghc->hva, data, len);
1336 if (r)
1337 return -EFAULT;
1338 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1339
1340 return 0;
1341}
1342EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1343
Izik Eidus195aefd2007-10-01 22:14:18 +02001344int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1345{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001346 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1347 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001348}
1349EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1350
1351int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1352{
1353 gfn_t gfn = gpa >> PAGE_SHIFT;
1354 int seg;
1355 int offset = offset_in_page(gpa);
1356 int ret;
1357
1358 while ((seg = next_segment(len, offset)) != 0) {
1359 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1360 if (ret < 0)
1361 return ret;
1362 offset = 0;
1363 len -= seg;
1364 ++gfn;
1365 }
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(kvm_clear_guest);
1369
Gleb Natapov49c77542010-10-18 15:22:23 +02001370void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1371 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001373 if (memslot && memslot->dirty_bitmap) {
1374 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001376 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377 }
1378}
1379
Gleb Natapov49c77542010-10-18 15:22:23 +02001380void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1381{
1382 struct kvm_memory_slot *memslot;
1383
1384 memslot = gfn_to_memslot(kvm, gfn);
1385 mark_page_dirty_in_slot(kvm, memslot, gfn);
1386}
1387
Eddie Dongb6958ce2007-07-18 12:15:21 +03001388/*
1389 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1390 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001391void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001392{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001393 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001394
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001395 for (;;) {
1396 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001397
Gleb Natapova1b37102009-07-09 15:33:52 +03001398 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001399 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001400 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001401 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001402 if (kvm_cpu_has_pending_timer(vcpu))
1403 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001404 if (signal_pending(current))
1405 break;
1406
Eddie Dongb6958ce2007-07-18 12:15:21 +03001407 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001408 }
1409
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001410 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001411}
1412
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413void kvm_resched(struct kvm_vcpu *vcpu)
1414{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001415 if (!need_resched())
1416 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418}
1419EXPORT_SYMBOL_GPL(kvm_resched);
1420
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001421void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1422{
1423 ktime_t expires;
1424 DEFINE_WAIT(wait);
1425
1426 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1427
1428 /* Sleep for 100 us, and hope lock-holder got scheduled */
1429 expires = ktime_add_ns(ktime_get(), 100000UL);
1430 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1431
1432 finish_wait(&vcpu->wq, &wait);
1433}
1434EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1435
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001436static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001437{
1438 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001439 struct page *page;
1440
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001441 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001442 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001443#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001444 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001445 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001446#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001447#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1448 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1449 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1450#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001451 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001452 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001453 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001454 vmf->page = page;
1455 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001456}
1457
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001458static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001459 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001460};
1461
1462static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1463{
1464 vma->vm_ops = &kvm_vcpu_vm_ops;
1465 return 0;
1466}
1467
Avi Kivitybccf2152007-02-21 18:04:26 +02001468static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1469{
1470 struct kvm_vcpu *vcpu = filp->private_data;
1471
Al Viro66c0b392008-04-19 20:33:56 +01001472 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001473 return 0;
1474}
1475
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001476static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001477 .release = kvm_vcpu_release,
1478 .unlocked_ioctl = kvm_vcpu_ioctl,
1479 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001480 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001481 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001482};
1483
1484/*
1485 * Allocates an inode for the vcpu.
1486 */
1487static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1488{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001489 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001490}
1491
Avi Kivityc5ea7662007-02-20 18:41:05 +02001492/*
1493 * Creates some virtual cpus. Good luck creating more than one.
1494 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001495static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001496{
1497 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001498 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001499
Gleb Natapov73880c82009-06-09 15:56:28 +03001500 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001501 if (IS_ERR(vcpu))
1502 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001503
Avi Kivity15ad7142007-07-11 18:17:21 +03001504 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1505
Avi Kivity26e52152007-11-20 15:30:24 +02001506 r = kvm_arch_vcpu_setup(vcpu);
1507 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001508 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001509
Shaohua Li11ec2802007-07-23 14:51:37 +08001510 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001511 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1512 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001513 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001514 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001515
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001516 kvm_for_each_vcpu(r, v, kvm)
1517 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001518 r = -EEXIST;
1519 goto vcpu_destroy;
1520 }
1521
1522 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001523
1524 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001525 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001526 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001527 if (r < 0) {
1528 kvm_put_kvm(kvm);
1529 goto vcpu_destroy;
1530 }
1531
1532 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1533 smp_wmb();
1534 atomic_inc(&kvm->online_vcpus);
1535
1536#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1537 if (kvm->bsp_vcpu_id == id)
1538 kvm->bsp_vcpu = vcpu;
1539#endif
1540 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001541 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001542
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001543vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001544 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001545 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001546 return r;
1547}
1548
Avi Kivity1961d272007-03-05 19:46:05 +02001549static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1550{
1551 if (sigset) {
1552 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1553 vcpu->sigset_active = 1;
1554 vcpu->sigset = *sigset;
1555 } else
1556 vcpu->sigset_active = 0;
1557 return 0;
1558}
1559
Avi Kivitybccf2152007-02-21 18:04:26 +02001560static long kvm_vcpu_ioctl(struct file *filp,
1561 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562{
Avi Kivitybccf2152007-02-21 18:04:26 +02001563 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001564 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001565 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001566 struct kvm_fpu *fpu = NULL;
1567 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001569 if (vcpu->kvm->mm != current->mm)
1570 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001571
1572#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1573 /*
1574 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1575 * so vcpu_load() would break it.
1576 */
1577 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1578 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1579#endif
1580
1581
1582 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001584 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001585 r = -EINVAL;
1586 if (arg)
1587 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001588 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001589 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001592 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001594 r = -ENOMEM;
1595 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1596 if (!kvm_regs)
1597 goto out;
1598 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001600 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001602 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1603 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001605out_free1:
1606 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 break;
1608 }
1609 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001610 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001612 r = -ENOMEM;
1613 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1614 if (!kvm_regs)
1615 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001617 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1618 goto out_free2;
1619 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001621 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001623out_free2:
1624 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 break;
1626 }
1627 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001628 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1629 r = -ENOMEM;
1630 if (!kvm_sregs)
1631 goto out;
1632 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 if (r)
1634 goto out;
1635 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001636 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 goto out;
1638 r = 0;
1639 break;
1640 }
1641 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001642 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1643 r = -ENOMEM;
1644 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001646 r = -EFAULT;
1647 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1648 goto out;
1649 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650 if (r)
1651 goto out;
1652 r = 0;
1653 break;
1654 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001655 case KVM_GET_MP_STATE: {
1656 struct kvm_mp_state mp_state;
1657
1658 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1659 if (r)
1660 goto out;
1661 r = -EFAULT;
1662 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1663 goto out;
1664 r = 0;
1665 break;
1666 }
1667 case KVM_SET_MP_STATE: {
1668 struct kvm_mp_state mp_state;
1669
1670 r = -EFAULT;
1671 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1672 goto out;
1673 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1674 if (r)
1675 goto out;
1676 r = 0;
1677 break;
1678 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 case KVM_TRANSLATE: {
1680 struct kvm_translation tr;
1681
1682 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001683 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001685 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 if (r)
1687 goto out;
1688 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001689 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 goto out;
1691 r = 0;
1692 break;
1693 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001694 case KVM_SET_GUEST_DEBUG: {
1695 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696
1697 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001698 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001700 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 if (r)
1702 goto out;
1703 r = 0;
1704 break;
1705 }
Avi Kivity1961d272007-03-05 19:46:05 +02001706 case KVM_SET_SIGNAL_MASK: {
1707 struct kvm_signal_mask __user *sigmask_arg = argp;
1708 struct kvm_signal_mask kvm_sigmask;
1709 sigset_t sigset, *p;
1710
1711 p = NULL;
1712 if (argp) {
1713 r = -EFAULT;
1714 if (copy_from_user(&kvm_sigmask, argp,
1715 sizeof kvm_sigmask))
1716 goto out;
1717 r = -EINVAL;
1718 if (kvm_sigmask.len != sizeof sigset)
1719 goto out;
1720 r = -EFAULT;
1721 if (copy_from_user(&sigset, sigmask_arg->sigset,
1722 sizeof sigset))
1723 goto out;
1724 p = &sigset;
1725 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001726 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001727 break;
1728 }
Avi Kivityb8836732007-04-01 16:34:31 +03001729 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001730 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1731 r = -ENOMEM;
1732 if (!fpu)
1733 goto out;
1734 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001735 if (r)
1736 goto out;
1737 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001738 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001739 goto out;
1740 r = 0;
1741 break;
1742 }
1743 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001744 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1745 r = -ENOMEM;
1746 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001747 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001748 r = -EFAULT;
1749 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1750 goto out;
1751 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001752 if (r)
1753 goto out;
1754 r = 0;
1755 break;
1756 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001757 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001758 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001759 }
1760out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001761 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001762 kfree(fpu);
1763 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001764 return r;
1765}
1766
1767static long kvm_vm_ioctl(struct file *filp,
1768 unsigned int ioctl, unsigned long arg)
1769{
1770 struct kvm *kvm = filp->private_data;
1771 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001772 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001773
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001774 if (kvm->mm != current->mm)
1775 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001776 switch (ioctl) {
1777 case KVM_CREATE_VCPU:
1778 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1779 if (r < 0)
1780 goto out;
1781 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001782 case KVM_SET_USER_MEMORY_REGION: {
1783 struct kvm_userspace_memory_region kvm_userspace_mem;
1784
1785 r = -EFAULT;
1786 if (copy_from_user(&kvm_userspace_mem, argp,
1787 sizeof kvm_userspace_mem))
1788 goto out;
1789
1790 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 if (r)
1792 goto out;
1793 break;
1794 }
1795 case KVM_GET_DIRTY_LOG: {
1796 struct kvm_dirty_log log;
1797
1798 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001799 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001801 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 if (r)
1803 goto out;
1804 break;
1805 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001806#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1807 case KVM_REGISTER_COALESCED_MMIO: {
1808 struct kvm_coalesced_mmio_zone zone;
1809 r = -EFAULT;
1810 if (copy_from_user(&zone, argp, sizeof zone))
1811 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001812 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1813 if (r)
1814 goto out;
1815 r = 0;
1816 break;
1817 }
1818 case KVM_UNREGISTER_COALESCED_MMIO: {
1819 struct kvm_coalesced_mmio_zone zone;
1820 r = -EFAULT;
1821 if (copy_from_user(&zone, argp, sizeof zone))
1822 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001823 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1824 if (r)
1825 goto out;
1826 r = 0;
1827 break;
1828 }
1829#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001830 case KVM_IRQFD: {
1831 struct kvm_irqfd data;
1832
1833 r = -EFAULT;
1834 if (copy_from_user(&data, argp, sizeof data))
1835 goto out;
1836 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1837 break;
1838 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001839 case KVM_IOEVENTFD: {
1840 struct kvm_ioeventfd data;
1841
1842 r = -EFAULT;
1843 if (copy_from_user(&data, argp, sizeof data))
1844 goto out;
1845 r = kvm_ioeventfd(kvm, &data);
1846 break;
1847 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001848#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1849 case KVM_SET_BOOT_CPU_ID:
1850 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001851 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001852 if (atomic_read(&kvm->online_vcpus) != 0)
1853 r = -EBUSY;
1854 else
1855 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001856 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001857 break;
1858#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001859 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001860 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001861 if (r == -ENOTTY)
1862 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001863 }
1864out:
1865 return r;
1866}
1867
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001868#ifdef CONFIG_COMPAT
1869struct compat_kvm_dirty_log {
1870 __u32 slot;
1871 __u32 padding1;
1872 union {
1873 compat_uptr_t dirty_bitmap; /* one bit per page */
1874 __u64 padding2;
1875 };
1876};
1877
1878static long kvm_vm_compat_ioctl(struct file *filp,
1879 unsigned int ioctl, unsigned long arg)
1880{
1881 struct kvm *kvm = filp->private_data;
1882 int r;
1883
1884 if (kvm->mm != current->mm)
1885 return -EIO;
1886 switch (ioctl) {
1887 case KVM_GET_DIRTY_LOG: {
1888 struct compat_kvm_dirty_log compat_log;
1889 struct kvm_dirty_log log;
1890
1891 r = -EFAULT;
1892 if (copy_from_user(&compat_log, (void __user *)arg,
1893 sizeof(compat_log)))
1894 goto out;
1895 log.slot = compat_log.slot;
1896 log.padding1 = compat_log.padding1;
1897 log.padding2 = compat_log.padding2;
1898 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1899
1900 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1901 if (r)
1902 goto out;
1903 break;
1904 }
1905 default:
1906 r = kvm_vm_ioctl(filp, ioctl, arg);
1907 }
1908
1909out:
1910 return r;
1911}
1912#endif
1913
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001914static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001915{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001916 struct page *page[1];
1917 unsigned long addr;
1918 int npages;
1919 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001920 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001921
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001922 addr = gfn_to_hva(kvm, gfn);
1923 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001924 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001925
1926 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1927 NULL);
1928 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001929 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001930
1931 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001932 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001933}
1934
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001935static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001936 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001937};
1938
1939static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1940{
1941 vma->vm_ops = &kvm_vm_vm_ops;
1942 return 0;
1943}
1944
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001945static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001946 .release = kvm_vm_release,
1947 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001948#ifdef CONFIG_COMPAT
1949 .compat_ioctl = kvm_vm_compat_ioctl,
1950#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001951 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001952 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001953};
1954
1955static int kvm_dev_ioctl_create_vm(void)
1956{
Heiko Carstensaac87632010-10-27 17:22:10 +02001957 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001958 struct kvm *kvm;
1959
Avi Kivityf17abe92007-02-21 19:28:04 +02001960 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001961 if (IS_ERR(kvm))
1962 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001963#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1964 r = kvm_coalesced_mmio_init(kvm);
1965 if (r < 0) {
1966 kvm_put_kvm(kvm);
1967 return r;
1968 }
1969#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02001970 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1971 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001972 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001973
Heiko Carstensaac87632010-10-27 17:22:10 +02001974 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001975}
1976
Avi Kivity1a811b62008-12-08 18:25:27 +02001977static long kvm_dev_ioctl_check_extension_generic(long arg)
1978{
1979 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001980 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001981 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001982 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001983#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1984 case KVM_CAP_SET_BOOT_CPU_ID:
1985#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001986 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001987 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001988#ifdef CONFIG_HAVE_KVM_IRQCHIP
1989 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001990 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001991#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001992 default:
1993 break;
1994 }
1995 return kvm_dev_ioctl_check_extension(arg);
1996}
1997
Avi Kivityf17abe92007-02-21 19:28:04 +02001998static long kvm_dev_ioctl(struct file *filp,
1999 unsigned int ioctl, unsigned long arg)
2000{
Avi Kivity07c45a32007-03-07 13:05:38 +02002001 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002002
2003 switch (ioctl) {
2004 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002005 r = -EINVAL;
2006 if (arg)
2007 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002008 r = KVM_API_VERSION;
2009 break;
2010 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002011 r = -EINVAL;
2012 if (arg)
2013 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002014 r = kvm_dev_ioctl_create_vm();
2015 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002016 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002017 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002018 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002019 case KVM_GET_VCPU_MMAP_SIZE:
2020 r = -EINVAL;
2021 if (arg)
2022 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002023 r = PAGE_SIZE; /* struct kvm_run */
2024#ifdef CONFIG_X86
2025 r += PAGE_SIZE; /* pio data page */
2026#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002027#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2028 r += PAGE_SIZE; /* coalesced mmio ring page */
2029#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002030 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002031 case KVM_TRACE_ENABLE:
2032 case KVM_TRACE_PAUSE:
2033 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002034 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002035 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002037 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002038 }
2039out:
2040 return r;
2041}
2042
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 .unlocked_ioctl = kvm_dev_ioctl,
2045 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002046 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047};
2048
2049static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002050 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 "kvm",
2052 &kvm_chardev_ops,
2053};
2054
Avi Kivity1b6c0162007-05-24 13:03:52 +03002055static void hardware_enable(void *junk)
2056{
2057 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002058 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002059
Rusty Russell7f59f492008-12-07 21:25:45 +10302060 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002061 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002062
Rusty Russell7f59f492008-12-07 21:25:45 +10302063 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002064
2065 r = kvm_arch_hardware_enable(NULL);
2066
2067 if (r) {
2068 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2069 atomic_inc(&hardware_enable_failed);
2070 printk(KERN_INFO "kvm: enabling virtualization on "
2071 "CPU%d failed\n", cpu);
2072 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002073}
2074
2075static void hardware_disable(void *junk)
2076{
2077 int cpu = raw_smp_processor_id();
2078
Rusty Russell7f59f492008-12-07 21:25:45 +10302079 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002080 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302081 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002082 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002083}
2084
Alexander Graf10474ae2009-09-15 11:37:46 +02002085static void hardware_disable_all_nolock(void)
2086{
2087 BUG_ON(!kvm_usage_count);
2088
2089 kvm_usage_count--;
2090 if (!kvm_usage_count)
2091 on_each_cpu(hardware_disable, NULL, 1);
2092}
2093
2094static void hardware_disable_all(void)
2095{
2096 spin_lock(&kvm_lock);
2097 hardware_disable_all_nolock();
2098 spin_unlock(&kvm_lock);
2099}
2100
2101static int hardware_enable_all(void)
2102{
2103 int r = 0;
2104
2105 spin_lock(&kvm_lock);
2106
2107 kvm_usage_count++;
2108 if (kvm_usage_count == 1) {
2109 atomic_set(&hardware_enable_failed, 0);
2110 on_each_cpu(hardware_enable, NULL, 1);
2111
2112 if (atomic_read(&hardware_enable_failed)) {
2113 hardware_disable_all_nolock();
2114 r = -EBUSY;
2115 }
2116 }
2117
2118 spin_unlock(&kvm_lock);
2119
2120 return r;
2121}
2122
Avi Kivity774c47f2007-02-12 00:54:47 -08002123static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2124 void *v)
2125{
2126 int cpu = (long)v;
2127
Alexander Graf10474ae2009-09-15 11:37:46 +02002128 if (!kvm_usage_count)
2129 return NOTIFY_OK;
2130
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002131 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002132 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002133 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002134 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2135 cpu);
2136 hardware_disable(NULL);
2137 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002138 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002139 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2140 cpu);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002141 spin_lock(&kvm_lock);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002142 hardware_enable(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002143 spin_unlock(&kvm_lock);
Avi Kivity774c47f2007-02-12 00:54:47 -08002144 break;
2145 }
2146 return NOTIFY_OK;
2147}
2148
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002149
2150asmlinkage void kvm_handle_fault_on_reboot(void)
2151{
Avi Kivityca242ac2010-09-21 19:59:43 +02002152 if (kvm_rebooting) {
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002153 /* spin while reset goes on */
Avi Kivityca242ac2010-09-21 19:59:43 +02002154 local_irq_enable();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002155 while (true)
Avi Kivity624d84c2010-09-21 19:59:44 +02002156 cpu_relax();
Avi Kivityca242ac2010-09-21 19:59:43 +02002157 }
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002158 /* Fault while not rebooting. We want the trace. */
2159 BUG();
2160}
2161EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2162
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002163static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002164 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002165{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002166 /*
2167 * Some (well, at least mine) BIOSes hang on reboot if
2168 * in vmx root mode.
2169 *
2170 * And Intel TXT required VMX off for all cpu when system shutdown.
2171 */
2172 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2173 kvm_rebooting = true;
2174 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002175 return NOTIFY_OK;
2176}
2177
2178static struct notifier_block kvm_reboot_notifier = {
2179 .notifier_call = kvm_reboot,
2180 .priority = 0,
2181};
2182
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002183static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002184{
2185 int i;
2186
2187 for (i = 0; i < bus->dev_count; i++) {
2188 struct kvm_io_device *pos = bus->devs[i];
2189
2190 kvm_iodevice_destructor(pos);
2191 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002192 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002193}
2194
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002195/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002196int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002197 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002198{
2199 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002200 struct kvm_io_bus *bus;
2201
2202 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002203 for (i = 0; i < bus->dev_count; i++)
2204 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2205 return 0;
2206 return -EOPNOTSUPP;
2207}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002208
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002209/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002210int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2211 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002212{
2213 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002214 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002215
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002216 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002217 for (i = 0; i < bus->dev_count; i++)
2218 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2219 return 0;
2220 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002221}
2222
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002223/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002224int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2225 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002226{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002227 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002228
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002229 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002230 if (bus->dev_count > NR_IOBUS_DEVS-1)
2231 return -ENOSPC;
2232
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002233 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2234 if (!new_bus)
2235 return -ENOMEM;
2236 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2237 new_bus->devs[new_bus->dev_count++] = dev;
2238 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2239 synchronize_srcu_expedited(&kvm->srcu);
2240 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002241
2242 return 0;
2243}
2244
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002245/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002246int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2247 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002248{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002249 int i, r;
2250 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002251
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002252 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2253 if (!new_bus)
2254 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002255
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002256 bus = kvm->buses[bus_idx];
2257 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2258
2259 r = -ENOENT;
2260 for (i = 0; i < new_bus->dev_count; i++)
2261 if (new_bus->devs[i] == dev) {
2262 r = 0;
2263 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002264 break;
2265 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002266
2267 if (r) {
2268 kfree(new_bus);
2269 return r;
2270 }
2271
2272 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2273 synchronize_srcu_expedited(&kvm->srcu);
2274 kfree(bus);
2275 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002276}
2277
Avi Kivity774c47f2007-02-12 00:54:47 -08002278static struct notifier_block kvm_cpu_notifier = {
2279 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002280};
2281
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002282static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002283{
2284 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002285 struct kvm *kvm;
2286
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002287 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002288 spin_lock(&kvm_lock);
2289 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002290 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002291 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002292 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002293}
2294
2295DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2296
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002297static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002298{
2299 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002300 struct kvm *kvm;
2301 struct kvm_vcpu *vcpu;
2302 int i;
2303
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002304 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002305 spin_lock(&kvm_lock);
2306 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002307 kvm_for_each_vcpu(i, vcpu, kvm)
2308 *val += *(u32 *)((void *)vcpu + offset);
2309
Avi Kivity1165f5f2007-04-19 17:27:43 +03002310 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002311 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002312}
2313
Avi Kivityba1389b2007-11-18 16:24:12 +02002314DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2315
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002316static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002317 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2318 [KVM_STAT_VM] = &vm_stat_fops,
2319};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002320
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002321static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322{
2323 struct kvm_stats_debugfs_item *p;
2324
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002325 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002327 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002328 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002329 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
2332static void kvm_exit_debug(void)
2333{
2334 struct kvm_stats_debugfs_item *p;
2335
2336 for (p = debugfs_entries; p->name; ++p)
2337 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002338 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339}
2340
Avi Kivity59ae6c62007-02-12 00:54:48 -08002341static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2342{
Alexander Graf10474ae2009-09-15 11:37:46 +02002343 if (kvm_usage_count)
2344 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002345 return 0;
2346}
2347
2348static int kvm_resume(struct sys_device *dev)
2349{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002350 if (kvm_usage_count) {
2351 WARN_ON(spin_is_locked(&kvm_lock));
Alexander Graf10474ae2009-09-15 11:37:46 +02002352 hardware_enable(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002353 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002354 return 0;
2355}
2356
2357static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002358 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002359 .suspend = kvm_suspend,
2360 .resume = kvm_resume,
2361};
2362
2363static struct sys_device kvm_sysdev = {
2364 .id = 0,
2365 .cls = &kvm_sysdev_class,
2366};
2367
Izik Eiduscea7bb22007-10-17 19:17:48 +02002368struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002369pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
Avi Kivity15ad7142007-07-11 18:17:21 +03002371static inline
2372struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2373{
2374 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2375}
2376
2377static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2378{
2379 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2380
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002381 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002382}
2383
2384static void kvm_sched_out(struct preempt_notifier *pn,
2385 struct task_struct *next)
2386{
2387 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2388
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002389 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002390}
2391
Avi Kivity0ee75be2010-04-28 15:39:01 +03002392int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002393 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394{
2395 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002396 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002398 r = kvm_arch_init(opaque);
2399 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002400 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002401
2402 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2403
2404 if (bad_page == NULL) {
2405 r = -ENOMEM;
2406 goto out;
2407 }
2408
Anthony Liguori35149e22008-04-02 14:46:56 -05002409 bad_pfn = page_to_pfn(bad_page);
2410
Huang Yingbf998152010-05-31 14:28:19 +08002411 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2412
2413 if (hwpoison_page == NULL) {
2414 r = -ENOMEM;
2415 goto out_free_0;
2416 }
2417
2418 hwpoison_pfn = page_to_pfn(hwpoison_page);
2419
Gleb Natapovedba23e2010-07-07 20:16:45 +03002420 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2421
2422 if (fault_page == NULL) {
2423 r = -ENOMEM;
2424 goto out_free_0;
2425 }
2426
2427 fault_pfn = page_to_pfn(fault_page);
2428
Avi Kivity8437a612009-06-06 14:52:35 -07002429 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302430 r = -ENOMEM;
2431 goto out_free_0;
2432 }
2433
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002434 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302436 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437
Yang, Sheng002c7f72007-07-31 14:23:01 +03002438 for_each_online_cpu(cpu) {
2439 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002440 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002441 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002442 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002443 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002444 }
2445
Avi Kivity774c47f2007-02-12 00:54:47 -08002446 r = register_cpu_notifier(&kvm_cpu_notifier);
2447 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002448 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 register_reboot_notifier(&kvm_reboot_notifier);
2450
Avi Kivity59ae6c62007-02-12 00:54:48 -08002451 r = sysdev_class_register(&kvm_sysdev_class);
2452 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002453 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002454
2455 r = sysdev_register(&kvm_sysdev);
2456 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002457 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002458
Rusty Russellc16f8622007-07-30 21:12:19 +10002459 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002460 if (!vcpu_align)
2461 vcpu_align = __alignof__(struct kvm_vcpu);
2462 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002463 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002464 if (!kvm_vcpu_cache) {
2465 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002466 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002467 }
2468
Gleb Natapovaf585b92010-10-14 11:22:46 +02002469 r = kvm_async_pf_init();
2470 if (r)
2471 goto out_free;
2472
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002474 kvm_vm_fops.owner = module;
2475 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476
2477 r = misc_register(&kvm_dev);
2478 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002479 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002480 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 }
2482
Avi Kivity15ad7142007-07-11 18:17:21 +03002483 kvm_preempt_ops.sched_in = kvm_sched_in;
2484 kvm_preempt_ops.sched_out = kvm_sched_out;
2485
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002486 kvm_init_debug();
2487
Avi Kivityc7addb92007-09-16 18:58:32 +02002488 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489
Gleb Natapovaf585b92010-10-14 11:22:46 +02002490out_unreg:
2491 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002493 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002494out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002495 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002496out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002497 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002498out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002500 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002501out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002502out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002503 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302504out_free_0a:
2505 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002506out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002507 if (fault_page)
2508 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002509 if (hwpoison_page)
2510 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002511 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002512out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002513 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002514out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 return r;
2516}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002517EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002519void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002521 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002523 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002524 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002525 sysdev_unregister(&kvm_sysdev);
2526 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002528 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002529 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002530 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002531 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302532 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002533 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002534 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002536EXPORT_SYMBOL_GPL(kvm_exit);