blob: 5156d458a84d53b79b36cec3b7d121583320988f [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{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100386 int r, i;
387 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100389 if (!kvm)
390 return ERR_PTR(-ENOMEM);
391
392 r = kvm_arch_init_vm(kvm);
393 if (r)
394 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200395
396 r = hardware_enable_all();
397 if (r)
398 goto out_err_nodisable;
399
Avi Kivity75858a82009-01-04 17:10:50 +0200400#ifdef CONFIG_HAVE_KVM_IRQCHIP
401 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300402 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200403#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200405 r = -ENOMEM;
406 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
407 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100408 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200409 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100410 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200411 for (i = 0; i < KVM_NR_BUSES; i++) {
412 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
413 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100414 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200415 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200416 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200417
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200418 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100419 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200420 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200421
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200422 kvm->mm = current->mm;
423 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500424 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200425 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400426 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800427 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300428 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200429 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300430 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000431 spin_lock(&kvm_lock);
432 list_add(&kvm->vm_list, &vm_list);
433 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100434
Avi Kivityf17abe92007-02-21 19:28:04 +0200435 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200436
437out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100438 cleanup_srcu_struct(&kvm->srcu);
439out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200440 hardware_disable_all();
441out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200442 for (i = 0; i < KVM_NR_BUSES; i++)
443 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200444 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100445 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200446 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200447}
448
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900449static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
450{
451 if (!memslot->dirty_bitmap)
452 return;
453
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900454 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
455 vfree(memslot->dirty_bitmap_head);
456 else
457 kfree(memslot->dirty_bitmap_head);
458
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900459 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900460 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900461}
462
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463/*
464 * Free any memory in @free but not in @dont.
465 */
466static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
467 struct kvm_memory_slot *dont)
468{
Joerg Roedelec04b262009-06-19 15:16:23 +0200469 int i;
470
Izik Eidus290fc382007-09-27 14:11:22 +0200471 if (!dont || free->rmap != dont->rmap)
472 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473
474 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900475 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476
Joerg Roedelec04b262009-06-19 15:16:23 +0200477
478 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
479 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
480 vfree(free->lpage_info[i]);
481 free->lpage_info[i] = NULL;
482 }
483 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300484
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500486 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487}
488
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800489void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490{
491 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200492 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200494 for (i = 0; i < slots->nmemslots; ++i)
495 kvm_free_physmem_slot(&slots->memslots[i], NULL);
496
497 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498}
499
Avi Kivityf17abe92007-02-21 19:28:04 +0200500static void kvm_destroy_vm(struct kvm *kvm)
501{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200502 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200503 struct mm_struct *mm = kvm->mm;
504
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800505 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800506 spin_lock(&kvm_lock);
507 list_del(&kvm->vm_list);
508 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200509 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200510 for (i = 0; i < KVM_NR_BUSES; i++)
511 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200512 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200513#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
514 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200515#else
516 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200517#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800518 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100519 kvm_free_physmem(kvm);
520 cleanup_srcu_struct(&kvm->srcu);
521 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200522 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200523 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200524}
525
Izik Eidusd39f13b2008-03-30 16:01:25 +0300526void kvm_get_kvm(struct kvm *kvm)
527{
528 atomic_inc(&kvm->users_count);
529}
530EXPORT_SYMBOL_GPL(kvm_get_kvm);
531
532void kvm_put_kvm(struct kvm *kvm)
533{
534 if (atomic_dec_and_test(&kvm->users_count))
535 kvm_destroy_vm(kvm);
536}
537EXPORT_SYMBOL_GPL(kvm_put_kvm);
538
539
Avi Kivityf17abe92007-02-21 19:28:04 +0200540static int kvm_vm_release(struct inode *inode, struct file *filp)
541{
542 struct kvm *kvm = filp->private_data;
543
Gregory Haskins721eecb2009-05-20 10:30:49 -0400544 kvm_irqfd_release(kvm);
545
Izik Eidusd39f13b2008-03-30 16:01:25 +0300546 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 return 0;
548}
549
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900550/*
551 * Allocation size is twice as large as the actual dirty bitmap size.
552 * This makes it possible to do double buffering: see x86's
553 * kvm_vm_ioctl_get_dirty_log().
554 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900555static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
556{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900557 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900558
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900559 if (dirty_bytes > PAGE_SIZE)
560 memslot->dirty_bitmap = vzalloc(dirty_bytes);
561 else
562 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
563
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900564 if (!memslot->dirty_bitmap)
565 return -ENOMEM;
566
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900567 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900568 return 0;
569}
570
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 * Allocate some memory and give it an address in the guest physical address
573 * space.
574 *
575 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800576 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500577 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800579int __kvm_set_memory_region(struct kvm *kvm,
580 struct kvm_userspace_memory_region *mem,
581 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200583 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200585 unsigned long npages;
586 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 struct kvm_memory_slot *memslot;
588 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200589 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590
591 r = -EINVAL;
592 /* General sanity checks */
593 if (mem->memory_size & (PAGE_SIZE - 1))
594 goto out;
595 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
596 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800597 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600598 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200599 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 goto out;
601 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
602 goto out;
603
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200604 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
606 npages = mem->memory_size >> PAGE_SHIFT;
607
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900608 r = -EINVAL;
609 if (npages > KVM_MEM_MAX_NR_PAGES)
610 goto out;
611
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 if (!npages)
613 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
614
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 new = old = *memslot;
616
Avi Kivitye36d96f2010-06-21 10:56:36 +0300617 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 new.base_gfn = base_gfn;
619 new.npages = npages;
620 new.flags = mem->flags;
621
622 /* Disallow changing a memory slot's size. */
623 r = -EINVAL;
624 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800625 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626
627 /* Check for overlaps */
628 r = -EEXIST;
629 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200630 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200632 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 continue;
634 if (!((base_gfn + npages <= s->base_gfn) ||
635 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800636 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 /* Free page dirty bitmap if unneeded */
640 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000641 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
643 r = -ENOMEM;
644
645 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200646#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500647 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900648 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200649
650 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800651 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200652
Izik Eidus80b14b52007-10-25 11:54:04 +0200653 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200654 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200656 if (!npages)
657 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300658
Joerg Roedelec04b262009-06-19 15:16:23 +0200659 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200660 unsigned long ugfn;
661 unsigned long j;
662 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200663 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300664
Joerg Roedelec04b262009-06-19 15:16:23 +0200665 /* Avoid unused variable warning if no large pages */
666 (void)level;
667
668 if (new.lpage_info[i])
669 continue;
670
Joerg Roedel82855412010-07-01 16:00:11 +0200671 lpages = 1 + ((base_gfn + npages - 1)
672 >> KVM_HPAGE_GFN_SHIFT(level));
673 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200674
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900675 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200676
677 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300678 goto out_free;
679
Joerg Roedel82855412010-07-01 16:00:11 +0200680 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200681 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200682 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200683 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300684 ugfn = new.userspace_addr >> PAGE_SHIFT;
685 /*
686 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300687 * other, or if explicitly asked to, disable large page
688 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300689 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200690 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300691 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200692 for (j = 0; j < lpages; ++j)
693 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300694 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695
Joerg Roedelec04b262009-06-19 15:16:23 +0200696skip_lpage:
697
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 /* Allocate page dirty bitmap if needed */
699 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900700 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800701 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200702 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300703 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200704 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200706#else /* not defined CONFIG_S390 */
707 new.user_alloc = user_alloc;
708 if (user_alloc)
709 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200710#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200712 if (!npages) {
713 r = -ENOMEM;
714 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
715 if (!slots)
716 goto out_free;
717 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
718 if (mem->slot >= slots->nmemslots)
719 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200720 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200721 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
722
723 old_memslots = kvm->memslots;
724 rcu_assign_pointer(kvm->memslots, slots);
725 synchronize_srcu_expedited(&kvm->srcu);
726 /* From this point no new shadow pages pointing to a deleted
727 * memslot will be created.
728 *
729 * validation of sp->gfn happens in:
730 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
731 * - kvm_is_visible_gfn (mmu_check_roots)
732 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300733 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200734 kfree(old_memslots);
735 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300736
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200737 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
738 if (r)
739 goto out_free;
740
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200741 /* map the pages in iommu page table */
742 if (npages) {
743 r = kvm_iommu_map_pages(kvm, &new);
744 if (r)
745 goto out_free;
746 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200747
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200748 r = -ENOMEM;
749 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
750 if (!slots)
751 goto out_free;
752 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
753 if (mem->slot >= slots->nmemslots)
754 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200755 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200756
757 /* actual memory is freed via old in kvm_free_physmem_slot below */
758 if (!npages) {
759 new.rmap = NULL;
760 new.dirty_bitmap = NULL;
761 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
762 new.lpage_info[i] = NULL;
763 }
764
765 slots->memslots[mem->slot] = new;
766 old_memslots = kvm->memslots;
767 rcu_assign_pointer(kvm->memslots, slots);
768 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200770 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800771
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200772 kvm_free_physmem_slot(&old, &new);
773 kfree(old_memslots);
774
775 if (flush_shadow)
776 kvm_arch_flush_shadow(kvm);
777
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 return 0;
779
Sheng Yangf78e0e22007-10-29 09:40:42 +0800780out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 kvm_free_physmem_slot(&new, &old);
782out:
783 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200784
785}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800786EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
787
788int kvm_set_memory_region(struct kvm *kvm,
789 struct kvm_userspace_memory_region *mem,
790 int user_alloc)
791{
792 int r;
793
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200794 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800795 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200796 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800797 return r;
798}
Izik Eidus210c7c42007-10-24 23:52:57 +0200799EXPORT_SYMBOL_GPL(kvm_set_memory_region);
800
Carsten Otte1fe779f2007-10-29 16:08:35 +0100801int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
802 struct
803 kvm_userspace_memory_region *mem,
804 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200805{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200806 if (mem->slot >= KVM_MEMORY_SLOTS)
807 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200808 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809}
810
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800811int kvm_get_dirty_log(struct kvm *kvm,
812 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813{
814 struct kvm_memory_slot *memslot;
815 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900816 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 unsigned long any = 0;
818
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 r = -EINVAL;
820 if (log->slot >= KVM_MEMORY_SLOTS)
821 goto out;
822
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200823 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 r = -ENOENT;
825 if (!memslot->dirty_bitmap)
826 goto out;
827
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900828 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829
Uri Lublincd1a4a92007-02-22 16:43:09 +0200830 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831 any = memslot->dirty_bitmap[i];
832
833 r = -EFAULT;
834 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
835 goto out;
836
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800837 if (any)
838 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
840 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 return r;
843}
844
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300845void kvm_disable_largepages(void)
846{
847 largepages_enabled = false;
848}
849EXPORT_SYMBOL_GPL(kvm_disable_largepages);
850
Izik Eiduscea7bb22007-10-17 19:17:48 +0200851int is_error_page(struct page *page)
852{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300853 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200854}
855EXPORT_SYMBOL_GPL(is_error_page);
856
Anthony Liguori35149e22008-04-02 14:46:56 -0500857int is_error_pfn(pfn_t pfn)
858{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300859 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500860}
861EXPORT_SYMBOL_GPL(is_error_pfn);
862
Huang Yingbf998152010-05-31 14:28:19 +0800863int is_hwpoison_pfn(pfn_t pfn)
864{
865 return pfn == hwpoison_pfn;
866}
867EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
868
Gleb Natapovedba23e2010-07-07 20:16:45 +0300869int is_fault_pfn(pfn_t pfn)
870{
871 return pfn == fault_pfn;
872}
873EXPORT_SYMBOL_GPL(is_fault_pfn);
874
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200875static inline unsigned long bad_hva(void)
876{
877 return PAGE_OFFSET;
878}
879
880int kvm_is_error_hva(unsigned long addr)
881{
882 return addr == bad_hva();
883}
884EXPORT_SYMBOL_GPL(kvm_is_error_hva);
885
Gleb Natapov49c77542010-10-18 15:22:23 +0200886static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
887 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888{
889 int i;
890
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200891 for (i = 0; i < slots->nmemslots; ++i) {
892 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893
894 if (gfn >= memslot->base_gfn
895 && gfn < memslot->base_gfn + memslot->npages)
896 return memslot;
897 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000898 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899}
Gleb Natapov49c77542010-10-18 15:22:23 +0200900
901struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
902{
903 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
904}
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300905EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906
Izik Eiduse0d62c72007-10-24 23:57:46 +0200907int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
908{
909 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800910 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200911
Izik Eiduse0d62c72007-10-24 23:57:46 +0200912 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200913 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200914
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200915 if (memslot->flags & KVM_MEMSLOT_INVALID)
916 continue;
917
Izik Eiduse0d62c72007-10-24 23:57:46 +0200918 if (gfn >= memslot->base_gfn
919 && gfn < memslot->base_gfn + memslot->npages)
920 return 1;
921 }
922 return 0;
923}
924EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
925
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100926unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
927{
928 struct vm_area_struct *vma;
929 unsigned long addr, size;
930
931 size = PAGE_SIZE;
932
933 addr = gfn_to_hva(kvm, gfn);
934 if (kvm_is_error_hva(addr))
935 return PAGE_SIZE;
936
937 down_read(&current->mm->mmap_sem);
938 vma = find_vma(current->mm, addr);
939 if (!vma)
940 goto out;
941
942 size = vma_kernel_pagesize(vma);
943
944out:
945 up_read(&current->mm->mmap_sem);
946
947 return size;
948}
949
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200950int memslot_id(struct kvm *kvm, gfn_t gfn)
951{
952 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800953 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200954 struct kvm_memory_slot *memslot = NULL;
955
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200956 for (i = 0; i < slots->nmemslots; ++i) {
957 memslot = &slots->memslots[i];
958
959 if (gfn >= memslot->base_gfn
960 && gfn < memslot->base_gfn + memslot->npages)
961 break;
962 }
963
964 return memslot - slots->memslots;
965}
966
Gleb Natapov49c77542010-10-18 15:22:23 +0200967static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +0800968 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +0200969{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200970 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200971 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +0800972
973 if (nr_pages)
974 *nr_pages = slot->npages - (gfn - slot->base_gfn);
975
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900976 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200977}
Xiao Guangrong48987782010-08-22 19:11:43 +0800978
979unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
980{
Gleb Natapov49c77542010-10-18 15:22:23 +0200981 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +0800982}
Sheng Yang0d150292008-04-25 21:44:50 +0800983EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200984
Gleb Natapov80300892010-10-19 18:13:41 +0200985static pfn_t get_fault_pfn(void)
986{
987 get_page(fault_page);
988 return fault_pfn;
989}
990
Gleb Natapovaf585b92010-10-14 11:22:46 +0200991static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200992 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300993{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500994 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +0200995 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500996 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300997
Gleb Natapovaf585b92010-10-14 11:22:46 +0200998 /* we can do it either atomically or asynchronously, not both */
999 BUG_ON(atomic && async);
1000
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001001 BUG_ON(!write_fault && !writable);
1002
1003 if (writable)
1004 *writable = true;
1005
Gleb Natapovaf585b92010-10-14 11:22:46 +02001006 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001007 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001008
1009 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001010 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001011
1012 if (writable)
1013 *writable = write_fault;
1014
1015 npages = get_user_pages_fast(addr, 1, write_fault, page);
1016
1017 /* map read fault as writable if possible */
1018 if (unlikely(!write_fault) && npages == 1) {
1019 struct page *wpage[1];
1020
1021 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1022 if (npages == 1) {
1023 *writable = true;
1024 put_page(page[0]);
1025 page[0] = wpage[0];
1026 }
1027 npages = 1;
1028 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001029 }
Izik Eidus539cb662007-11-11 22:05:04 +02001030
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001031 if (unlikely(npages != 1)) {
1032 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001033
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001034 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001035 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001036
Huang Yingbbeb3402010-06-22 14:23:11 +08001037 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001038 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001039 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001040 get_page(hwpoison_page);
1041 return page_to_pfn(hwpoison_page);
1042 }
1043
Gleb Natapov80300892010-10-19 18:13:41 +02001044 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001045
Gleb Natapov80300892010-10-19 18:13:41 +02001046 if (vma == NULL)
1047 pfn = get_fault_pfn();
1048 else if ((vma->vm_flags & VM_PFNMAP)) {
1049 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1050 vma->vm_pgoff;
1051 BUG_ON(!kvm_is_mmio_pfn(pfn));
1052 } else {
1053 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001054 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001055 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001056 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001057 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001058 } else
1059 pfn = page_to_pfn(page[0]);
1060
1061 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001062}
1063
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001064pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1065{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001066 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001067}
1068EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1069
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001070static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1071 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001072{
1073 unsigned long addr;
1074
Gleb Natapovaf585b92010-10-14 11:22:46 +02001075 if (async)
1076 *async = false;
1077
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001078 addr = gfn_to_hva(kvm, gfn);
1079 if (kvm_is_error_hva(addr)) {
1080 get_page(bad_page);
1081 return page_to_pfn(bad_page);
1082 }
1083
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001084 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001085}
1086
1087pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1088{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001089 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001090}
1091EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1092
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001093pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1094 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001095{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001096 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001097}
1098EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1099
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001100pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1101{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001102 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001103}
Anthony Liguori35149e22008-04-02 14:46:56 -05001104EXPORT_SYMBOL_GPL(gfn_to_pfn);
1105
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001106pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1107 bool *writable)
1108{
1109 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1110}
1111EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1112
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001113pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1114 struct kvm_memory_slot *slot, gfn_t gfn)
1115{
1116 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001117 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001118}
1119
Xiao Guangrong48987782010-08-22 19:11:43 +08001120int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1121 int nr_pages)
1122{
1123 unsigned long addr;
1124 gfn_t entry;
1125
Gleb Natapov49c77542010-10-18 15:22:23 +02001126 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001127 if (kvm_is_error_hva(addr))
1128 return -1;
1129
1130 if (entry < nr_pages)
1131 return 0;
1132
1133 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1134}
1135EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1136
Anthony Liguori35149e22008-04-02 14:46:56 -05001137struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1138{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001139 pfn_t pfn;
1140
1141 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001142 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001143 return pfn_to_page(pfn);
1144
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001145 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001146
1147 get_page(bad_page);
1148 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001149}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001150
Avi Kivity954bbbc2007-03-30 14:02:32 +03001151EXPORT_SYMBOL_GPL(gfn_to_page);
1152
Izik Eidusb4231d62007-11-20 11:49:33 +02001153void kvm_release_page_clean(struct page *page)
1154{
Anthony Liguori35149e22008-04-02 14:46:56 -05001155 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001156}
1157EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1158
Anthony Liguori35149e22008-04-02 14:46:56 -05001159void kvm_release_pfn_clean(pfn_t pfn)
1160{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001161 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001162 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001163}
1164EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1165
Izik Eidusb4231d62007-11-20 11:49:33 +02001166void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001167{
Anthony Liguori35149e22008-04-02 14:46:56 -05001168 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001169}
Izik Eidusb4231d62007-11-20 11:49:33 +02001170EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001171
Anthony Liguori35149e22008-04-02 14:46:56 -05001172void kvm_release_pfn_dirty(pfn_t pfn)
1173{
1174 kvm_set_pfn_dirty(pfn);
1175 kvm_release_pfn_clean(pfn);
1176}
1177EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1178
1179void kvm_set_page_dirty(struct page *page)
1180{
1181 kvm_set_pfn_dirty(page_to_pfn(page));
1182}
1183EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1184
1185void kvm_set_pfn_dirty(pfn_t pfn)
1186{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001187 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001188 struct page *page = pfn_to_page(pfn);
1189 if (!PageReserved(page))
1190 SetPageDirty(page);
1191 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001192}
1193EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1194
1195void kvm_set_pfn_accessed(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 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001199}
1200EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1201
1202void kvm_get_pfn(pfn_t pfn)
1203{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001204 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001205 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001206}
1207EXPORT_SYMBOL_GPL(kvm_get_pfn);
1208
Izik Eidus195aefd2007-10-01 22:14:18 +02001209static int next_segment(unsigned long len, int offset)
1210{
1211 if (len > PAGE_SIZE - offset)
1212 return PAGE_SIZE - offset;
1213 else
1214 return len;
1215}
1216
1217int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1218 int len)
1219{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001220 int r;
1221 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001222
Izik Eiduse0506bc2007-11-11 22:10:22 +02001223 addr = gfn_to_hva(kvm, gfn);
1224 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001225 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001226 r = copy_from_user(data, (void __user *)addr + offset, len);
1227 if (r)
1228 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001229 return 0;
1230}
1231EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1232
1233int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1234{
1235 gfn_t gfn = gpa >> PAGE_SHIFT;
1236 int seg;
1237 int offset = offset_in_page(gpa);
1238 int ret;
1239
1240 while ((seg = next_segment(len, offset)) != 0) {
1241 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1242 if (ret < 0)
1243 return ret;
1244 offset = 0;
1245 len -= seg;
1246 data += seg;
1247 ++gfn;
1248 }
1249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(kvm_read_guest);
1252
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001253int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1254 unsigned long len)
1255{
1256 int r;
1257 unsigned long addr;
1258 gfn_t gfn = gpa >> PAGE_SHIFT;
1259 int offset = offset_in_page(gpa);
1260
1261 addr = gfn_to_hva(kvm, gfn);
1262 if (kvm_is_error_hva(addr))
1263 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001264 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001265 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001266 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001267 if (r)
1268 return -EFAULT;
1269 return 0;
1270}
1271EXPORT_SYMBOL(kvm_read_guest_atomic);
1272
Izik Eidus195aefd2007-10-01 22:14:18 +02001273int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1274 int offset, int len)
1275{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001276 int r;
1277 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001278
Izik Eiduse0506bc2007-11-11 22:10:22 +02001279 addr = gfn_to_hva(kvm, gfn);
1280 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001281 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001282 r = copy_to_user((void __user *)addr + offset, data, len);
1283 if (r)
1284 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001285 mark_page_dirty(kvm, gfn);
1286 return 0;
1287}
1288EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1289
1290int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1291 unsigned long len)
1292{
1293 gfn_t gfn = gpa >> PAGE_SHIFT;
1294 int seg;
1295 int offset = offset_in_page(gpa);
1296 int ret;
1297
1298 while ((seg = next_segment(len, offset)) != 0) {
1299 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1300 if (ret < 0)
1301 return ret;
1302 offset = 0;
1303 len -= seg;
1304 data += seg;
1305 ++gfn;
1306 }
1307 return 0;
1308}
1309
Gleb Natapov49c77542010-10-18 15:22:23 +02001310int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1311 gpa_t gpa)
1312{
1313 struct kvm_memslots *slots = kvm_memslots(kvm);
1314 int offset = offset_in_page(gpa);
1315 gfn_t gfn = gpa >> PAGE_SHIFT;
1316
1317 ghc->gpa = gpa;
1318 ghc->generation = slots->generation;
1319 ghc->memslot = __gfn_to_memslot(slots, gfn);
1320 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1321 if (!kvm_is_error_hva(ghc->hva))
1322 ghc->hva += offset;
1323 else
1324 return -EFAULT;
1325
1326 return 0;
1327}
1328EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1329
1330int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1331 void *data, unsigned long len)
1332{
1333 struct kvm_memslots *slots = kvm_memslots(kvm);
1334 int r;
1335
1336 if (slots->generation != ghc->generation)
1337 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1338
1339 if (kvm_is_error_hva(ghc->hva))
1340 return -EFAULT;
1341
1342 r = copy_to_user((void __user *)ghc->hva, data, len);
1343 if (r)
1344 return -EFAULT;
1345 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1346
1347 return 0;
1348}
1349EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1350
Izik Eidus195aefd2007-10-01 22:14:18 +02001351int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1352{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001353 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1354 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001355}
1356EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1357
1358int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1359{
1360 gfn_t gfn = gpa >> PAGE_SHIFT;
1361 int seg;
1362 int offset = offset_in_page(gpa);
1363 int ret;
1364
1365 while ((seg = next_segment(len, offset)) != 0) {
1366 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1367 if (ret < 0)
1368 return ret;
1369 offset = 0;
1370 len -= seg;
1371 ++gfn;
1372 }
1373 return 0;
1374}
1375EXPORT_SYMBOL_GPL(kvm_clear_guest);
1376
Gleb Natapov49c77542010-10-18 15:22:23 +02001377void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1378 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001380 if (memslot && memslot->dirty_bitmap) {
1381 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001383 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 }
1385}
1386
Gleb Natapov49c77542010-10-18 15:22:23 +02001387void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1388{
1389 struct kvm_memory_slot *memslot;
1390
1391 memslot = gfn_to_memslot(kvm, gfn);
1392 mark_page_dirty_in_slot(kvm, memslot, gfn);
1393}
1394
Eddie Dongb6958ce2007-07-18 12:15:21 +03001395/*
1396 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1397 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001398void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001399{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001400 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001401
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001402 for (;;) {
1403 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001404
Gleb Natapova1b37102009-07-09 15:33:52 +03001405 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001406 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001407 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001408 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001409 if (kvm_cpu_has_pending_timer(vcpu))
1410 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001411 if (signal_pending(current))
1412 break;
1413
Eddie Dongb6958ce2007-07-18 12:15:21 +03001414 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001415 }
1416
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001417 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001418}
1419
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420void kvm_resched(struct kvm_vcpu *vcpu)
1421{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001422 if (!need_resched())
1423 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425}
1426EXPORT_SYMBOL_GPL(kvm_resched);
1427
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001428void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1429{
1430 ktime_t expires;
1431 DEFINE_WAIT(wait);
1432
1433 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1434
1435 /* Sleep for 100 us, and hope lock-holder got scheduled */
1436 expires = ktime_add_ns(ktime_get(), 100000UL);
1437 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1438
1439 finish_wait(&vcpu->wq, &wait);
1440}
1441EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1442
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001443static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001444{
1445 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001446 struct page *page;
1447
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001448 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001449 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001450#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001451 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001452 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001453#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001454#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1455 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1456 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1457#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001458 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001459 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001460 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001461 vmf->page = page;
1462 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001463}
1464
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001465static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001466 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001467};
1468
1469static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1470{
1471 vma->vm_ops = &kvm_vcpu_vm_ops;
1472 return 0;
1473}
1474
Avi Kivitybccf2152007-02-21 18:04:26 +02001475static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1476{
1477 struct kvm_vcpu *vcpu = filp->private_data;
1478
Al Viro66c0b392008-04-19 20:33:56 +01001479 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001480 return 0;
1481}
1482
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001483static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001484 .release = kvm_vcpu_release,
1485 .unlocked_ioctl = kvm_vcpu_ioctl,
1486 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001487 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001488 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001489};
1490
1491/*
1492 * Allocates an inode for the vcpu.
1493 */
1494static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1495{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001496 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001497}
1498
Avi Kivityc5ea7662007-02-20 18:41:05 +02001499/*
1500 * Creates some virtual cpus. Good luck creating more than one.
1501 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001502static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001503{
1504 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001505 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001506
Gleb Natapov73880c82009-06-09 15:56:28 +03001507 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001508 if (IS_ERR(vcpu))
1509 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001510
Avi Kivity15ad7142007-07-11 18:17:21 +03001511 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1512
Avi Kivity26e52152007-11-20 15:30:24 +02001513 r = kvm_arch_vcpu_setup(vcpu);
1514 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001515 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001516
Shaohua Li11ec2802007-07-23 14:51:37 +08001517 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001518 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1519 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001520 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001521 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001522
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001523 kvm_for_each_vcpu(r, v, kvm)
1524 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001525 r = -EEXIST;
1526 goto vcpu_destroy;
1527 }
1528
1529 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001530
1531 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001532 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001533 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001534 if (r < 0) {
1535 kvm_put_kvm(kvm);
1536 goto vcpu_destroy;
1537 }
1538
1539 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1540 smp_wmb();
1541 atomic_inc(&kvm->online_vcpus);
1542
1543#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1544 if (kvm->bsp_vcpu_id == id)
1545 kvm->bsp_vcpu = vcpu;
1546#endif
1547 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001548 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001549
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001550vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001551 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001552 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001553 return r;
1554}
1555
Avi Kivity1961d272007-03-05 19:46:05 +02001556static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1557{
1558 if (sigset) {
1559 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1560 vcpu->sigset_active = 1;
1561 vcpu->sigset = *sigset;
1562 } else
1563 vcpu->sigset_active = 0;
1564 return 0;
1565}
1566
Avi Kivitybccf2152007-02-21 18:04:26 +02001567static long kvm_vcpu_ioctl(struct file *filp,
1568 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569{
Avi Kivitybccf2152007-02-21 18:04:26 +02001570 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001571 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001572 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001573 struct kvm_fpu *fpu = NULL;
1574 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001576 if (vcpu->kvm->mm != current->mm)
1577 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001578
1579#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1580 /*
1581 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1582 * so vcpu_load() would break it.
1583 */
1584 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1585 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1586#endif
1587
1588
1589 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001591 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001592 r = -EINVAL;
1593 if (arg)
1594 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001595 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001596 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001599 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001601 r = -ENOMEM;
1602 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1603 if (!kvm_regs)
1604 goto out;
1605 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001607 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001609 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1610 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001612out_free1:
1613 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 break;
1615 }
1616 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001617 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001619 r = -ENOMEM;
1620 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1621 if (!kvm_regs)
1622 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001624 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1625 goto out_free2;
1626 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001628 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001630out_free2:
1631 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 break;
1633 }
1634 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001635 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1636 r = -ENOMEM;
1637 if (!kvm_sregs)
1638 goto out;
1639 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 if (r)
1641 goto out;
1642 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001643 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 goto out;
1645 r = 0;
1646 break;
1647 }
1648 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001649 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1650 r = -ENOMEM;
1651 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001653 r = -EFAULT;
1654 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1655 goto out;
1656 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 if (r)
1658 goto out;
1659 r = 0;
1660 break;
1661 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001662 case KVM_GET_MP_STATE: {
1663 struct kvm_mp_state mp_state;
1664
1665 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1666 if (r)
1667 goto out;
1668 r = -EFAULT;
1669 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1670 goto out;
1671 r = 0;
1672 break;
1673 }
1674 case KVM_SET_MP_STATE: {
1675 struct kvm_mp_state mp_state;
1676
1677 r = -EFAULT;
1678 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1679 goto out;
1680 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1681 if (r)
1682 goto out;
1683 r = 0;
1684 break;
1685 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 case KVM_TRANSLATE: {
1687 struct kvm_translation tr;
1688
1689 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001690 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001692 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 if (r)
1694 goto out;
1695 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001696 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 goto out;
1698 r = 0;
1699 break;
1700 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001701 case KVM_SET_GUEST_DEBUG: {
1702 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703
1704 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001705 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001707 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 if (r)
1709 goto out;
1710 r = 0;
1711 break;
1712 }
Avi Kivity1961d272007-03-05 19:46:05 +02001713 case KVM_SET_SIGNAL_MASK: {
1714 struct kvm_signal_mask __user *sigmask_arg = argp;
1715 struct kvm_signal_mask kvm_sigmask;
1716 sigset_t sigset, *p;
1717
1718 p = NULL;
1719 if (argp) {
1720 r = -EFAULT;
1721 if (copy_from_user(&kvm_sigmask, argp,
1722 sizeof kvm_sigmask))
1723 goto out;
1724 r = -EINVAL;
1725 if (kvm_sigmask.len != sizeof sigset)
1726 goto out;
1727 r = -EFAULT;
1728 if (copy_from_user(&sigset, sigmask_arg->sigset,
1729 sizeof sigset))
1730 goto out;
1731 p = &sigset;
1732 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001733 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001734 break;
1735 }
Avi Kivityb8836732007-04-01 16:34:31 +03001736 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001737 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1738 r = -ENOMEM;
1739 if (!fpu)
1740 goto out;
1741 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001742 if (r)
1743 goto out;
1744 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001745 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001746 goto out;
1747 r = 0;
1748 break;
1749 }
1750 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001751 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1752 r = -ENOMEM;
1753 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001754 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001755 r = -EFAULT;
1756 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1757 goto out;
1758 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001759 if (r)
1760 goto out;
1761 r = 0;
1762 break;
1763 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001764 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001765 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001766 }
1767out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001768 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001769 kfree(fpu);
1770 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001771 return r;
1772}
1773
1774static long kvm_vm_ioctl(struct file *filp,
1775 unsigned int ioctl, unsigned long arg)
1776{
1777 struct kvm *kvm = filp->private_data;
1778 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001779 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001780
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001781 if (kvm->mm != current->mm)
1782 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001783 switch (ioctl) {
1784 case KVM_CREATE_VCPU:
1785 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1786 if (r < 0)
1787 goto out;
1788 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001789 case KVM_SET_USER_MEMORY_REGION: {
1790 struct kvm_userspace_memory_region kvm_userspace_mem;
1791
1792 r = -EFAULT;
1793 if (copy_from_user(&kvm_userspace_mem, argp,
1794 sizeof kvm_userspace_mem))
1795 goto out;
1796
1797 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 if (r)
1799 goto out;
1800 break;
1801 }
1802 case KVM_GET_DIRTY_LOG: {
1803 struct kvm_dirty_log log;
1804
1805 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001806 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001808 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 if (r)
1810 goto out;
1811 break;
1812 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001813#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1814 case KVM_REGISTER_COALESCED_MMIO: {
1815 struct kvm_coalesced_mmio_zone zone;
1816 r = -EFAULT;
1817 if (copy_from_user(&zone, argp, sizeof zone))
1818 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001819 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1820 if (r)
1821 goto out;
1822 r = 0;
1823 break;
1824 }
1825 case KVM_UNREGISTER_COALESCED_MMIO: {
1826 struct kvm_coalesced_mmio_zone zone;
1827 r = -EFAULT;
1828 if (copy_from_user(&zone, argp, sizeof zone))
1829 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001830 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1831 if (r)
1832 goto out;
1833 r = 0;
1834 break;
1835 }
1836#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001837 case KVM_IRQFD: {
1838 struct kvm_irqfd data;
1839
1840 r = -EFAULT;
1841 if (copy_from_user(&data, argp, sizeof data))
1842 goto out;
1843 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1844 break;
1845 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001846 case KVM_IOEVENTFD: {
1847 struct kvm_ioeventfd data;
1848
1849 r = -EFAULT;
1850 if (copy_from_user(&data, argp, sizeof data))
1851 goto out;
1852 r = kvm_ioeventfd(kvm, &data);
1853 break;
1854 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001855#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1856 case KVM_SET_BOOT_CPU_ID:
1857 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001858 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001859 if (atomic_read(&kvm->online_vcpus) != 0)
1860 r = -EBUSY;
1861 else
1862 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001863 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001864 break;
1865#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001866 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001867 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001868 if (r == -ENOTTY)
1869 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001870 }
1871out:
1872 return r;
1873}
1874
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001875#ifdef CONFIG_COMPAT
1876struct compat_kvm_dirty_log {
1877 __u32 slot;
1878 __u32 padding1;
1879 union {
1880 compat_uptr_t dirty_bitmap; /* one bit per page */
1881 __u64 padding2;
1882 };
1883};
1884
1885static long kvm_vm_compat_ioctl(struct file *filp,
1886 unsigned int ioctl, unsigned long arg)
1887{
1888 struct kvm *kvm = filp->private_data;
1889 int r;
1890
1891 if (kvm->mm != current->mm)
1892 return -EIO;
1893 switch (ioctl) {
1894 case KVM_GET_DIRTY_LOG: {
1895 struct compat_kvm_dirty_log compat_log;
1896 struct kvm_dirty_log log;
1897
1898 r = -EFAULT;
1899 if (copy_from_user(&compat_log, (void __user *)arg,
1900 sizeof(compat_log)))
1901 goto out;
1902 log.slot = compat_log.slot;
1903 log.padding1 = compat_log.padding1;
1904 log.padding2 = compat_log.padding2;
1905 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1906
1907 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1908 if (r)
1909 goto out;
1910 break;
1911 }
1912 default:
1913 r = kvm_vm_ioctl(filp, ioctl, arg);
1914 }
1915
1916out:
1917 return r;
1918}
1919#endif
1920
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001921static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001922{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001923 struct page *page[1];
1924 unsigned long addr;
1925 int npages;
1926 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001927 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001928
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001929 addr = gfn_to_hva(kvm, gfn);
1930 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001931 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001932
1933 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1934 NULL);
1935 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001936 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001937
1938 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001939 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001940}
1941
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001942static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001943 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001944};
1945
1946static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1947{
1948 vma->vm_ops = &kvm_vm_vm_ops;
1949 return 0;
1950}
1951
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001952static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001953 .release = kvm_vm_release,
1954 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001955#ifdef CONFIG_COMPAT
1956 .compat_ioctl = kvm_vm_compat_ioctl,
1957#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001958 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001959 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001960};
1961
1962static int kvm_dev_ioctl_create_vm(void)
1963{
Heiko Carstensaac87632010-10-27 17:22:10 +02001964 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001965 struct kvm *kvm;
1966
Avi Kivityf17abe92007-02-21 19:28:04 +02001967 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001968 if (IS_ERR(kvm))
1969 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001970#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1971 r = kvm_coalesced_mmio_init(kvm);
1972 if (r < 0) {
1973 kvm_put_kvm(kvm);
1974 return r;
1975 }
1976#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02001977 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1978 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001979 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001980
Heiko Carstensaac87632010-10-27 17:22:10 +02001981 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001982}
1983
Avi Kivity1a811b62008-12-08 18:25:27 +02001984static long kvm_dev_ioctl_check_extension_generic(long arg)
1985{
1986 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001987 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001988 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001989 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001990#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1991 case KVM_CAP_SET_BOOT_CPU_ID:
1992#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001993 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001994 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001995#ifdef CONFIG_HAVE_KVM_IRQCHIP
1996 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001997 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001998#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001999 default:
2000 break;
2001 }
2002 return kvm_dev_ioctl_check_extension(arg);
2003}
2004
Avi Kivityf17abe92007-02-21 19:28:04 +02002005static long kvm_dev_ioctl(struct file *filp,
2006 unsigned int ioctl, unsigned long arg)
2007{
Avi Kivity07c45a32007-03-07 13:05:38 +02002008 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002009
2010 switch (ioctl) {
2011 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002012 r = -EINVAL;
2013 if (arg)
2014 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002015 r = KVM_API_VERSION;
2016 break;
2017 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002018 r = -EINVAL;
2019 if (arg)
2020 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002021 r = kvm_dev_ioctl_create_vm();
2022 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002023 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002024 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002025 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002026 case KVM_GET_VCPU_MMAP_SIZE:
2027 r = -EINVAL;
2028 if (arg)
2029 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002030 r = PAGE_SIZE; /* struct kvm_run */
2031#ifdef CONFIG_X86
2032 r += PAGE_SIZE; /* pio data page */
2033#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002034#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2035 r += PAGE_SIZE; /* coalesced mmio ring page */
2036#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002037 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002038 case KVM_TRACE_ENABLE:
2039 case KVM_TRACE_PAUSE:
2040 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002041 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002042 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002044 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045 }
2046out:
2047 return r;
2048}
2049
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 .unlocked_ioctl = kvm_dev_ioctl,
2052 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002053 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054};
2055
2056static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002057 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058 "kvm",
2059 &kvm_chardev_ops,
2060};
2061
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002062static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002063{
2064 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002065 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002066
Rusty Russell7f59f492008-12-07 21:25:45 +10302067 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002068 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002069
Rusty Russell7f59f492008-12-07 21:25:45 +10302070 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002071
2072 r = kvm_arch_hardware_enable(NULL);
2073
2074 if (r) {
2075 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2076 atomic_inc(&hardware_enable_failed);
2077 printk(KERN_INFO "kvm: enabling virtualization on "
2078 "CPU%d failed\n", cpu);
2079 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002080}
2081
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002082static void hardware_enable(void *junk)
2083{
2084 spin_lock(&kvm_lock);
2085 hardware_enable_nolock(junk);
2086 spin_unlock(&kvm_lock);
2087}
2088
2089static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002090{
2091 int cpu = raw_smp_processor_id();
2092
Rusty Russell7f59f492008-12-07 21:25:45 +10302093 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002094 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302095 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002096 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002097}
2098
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002099static void hardware_disable(void *junk)
2100{
2101 spin_lock(&kvm_lock);
2102 hardware_disable_nolock(junk);
2103 spin_unlock(&kvm_lock);
2104}
2105
Alexander Graf10474ae2009-09-15 11:37:46 +02002106static void hardware_disable_all_nolock(void)
2107{
2108 BUG_ON(!kvm_usage_count);
2109
2110 kvm_usage_count--;
2111 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002112 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002113}
2114
2115static void hardware_disable_all(void)
2116{
2117 spin_lock(&kvm_lock);
2118 hardware_disable_all_nolock();
2119 spin_unlock(&kvm_lock);
2120}
2121
2122static int hardware_enable_all(void)
2123{
2124 int r = 0;
2125
2126 spin_lock(&kvm_lock);
2127
2128 kvm_usage_count++;
2129 if (kvm_usage_count == 1) {
2130 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002131 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002132
2133 if (atomic_read(&hardware_enable_failed)) {
2134 hardware_disable_all_nolock();
2135 r = -EBUSY;
2136 }
2137 }
2138
2139 spin_unlock(&kvm_lock);
2140
2141 return r;
2142}
2143
Avi Kivity774c47f2007-02-12 00:54:47 -08002144static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2145 void *v)
2146{
2147 int cpu = (long)v;
2148
Alexander Graf10474ae2009-09-15 11:37:46 +02002149 if (!kvm_usage_count)
2150 return NOTIFY_OK;
2151
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002152 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002153 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002154 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03002155 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2156 cpu);
2157 hardware_disable(NULL);
2158 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002159 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002160 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2161 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002162 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002163 break;
2164 }
2165 return NOTIFY_OK;
2166}
2167
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002168
2169asmlinkage void kvm_handle_fault_on_reboot(void)
2170{
Avi Kivityca242ac2010-09-21 19:59:43 +02002171 if (kvm_rebooting) {
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002172 /* spin while reset goes on */
Avi Kivityca242ac2010-09-21 19:59:43 +02002173 local_irq_enable();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002174 while (true)
Avi Kivity624d84c2010-09-21 19:59:44 +02002175 cpu_relax();
Avi Kivityca242ac2010-09-21 19:59:43 +02002176 }
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002177 /* Fault while not rebooting. We want the trace. */
2178 BUG();
2179}
2180EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2181
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002182static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002183 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002184{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002185 /*
2186 * Some (well, at least mine) BIOSes hang on reboot if
2187 * in vmx root mode.
2188 *
2189 * And Intel TXT required VMX off for all cpu when system shutdown.
2190 */
2191 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2192 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002193 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002194 return NOTIFY_OK;
2195}
2196
2197static struct notifier_block kvm_reboot_notifier = {
2198 .notifier_call = kvm_reboot,
2199 .priority = 0,
2200};
2201
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002202static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002203{
2204 int i;
2205
2206 for (i = 0; i < bus->dev_count; i++) {
2207 struct kvm_io_device *pos = bus->devs[i];
2208
2209 kvm_iodevice_destructor(pos);
2210 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002211 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002212}
2213
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002214/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002215int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002216 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002217{
2218 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002219 struct kvm_io_bus *bus;
2220
2221 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002222 for (i = 0; i < bus->dev_count; i++)
2223 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2224 return 0;
2225 return -EOPNOTSUPP;
2226}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002227
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002228/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002229int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2230 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002231{
2232 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002233 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002234
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002235 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002236 for (i = 0; i < bus->dev_count; i++)
2237 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2238 return 0;
2239 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002240}
2241
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002242/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002243int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2244 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002245{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002246 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002247
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002248 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002249 if (bus->dev_count > NR_IOBUS_DEVS-1)
2250 return -ENOSPC;
2251
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002252 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2253 if (!new_bus)
2254 return -ENOMEM;
2255 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2256 new_bus->devs[new_bus->dev_count++] = dev;
2257 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2258 synchronize_srcu_expedited(&kvm->srcu);
2259 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002260
2261 return 0;
2262}
2263
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002264/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002265int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2266 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002267{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002268 int i, r;
2269 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002270
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002271 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2272 if (!new_bus)
2273 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002274
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002275 bus = kvm->buses[bus_idx];
2276 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2277
2278 r = -ENOENT;
2279 for (i = 0; i < new_bus->dev_count; i++)
2280 if (new_bus->devs[i] == dev) {
2281 r = 0;
2282 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002283 break;
2284 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002285
2286 if (r) {
2287 kfree(new_bus);
2288 return r;
2289 }
2290
2291 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2292 synchronize_srcu_expedited(&kvm->srcu);
2293 kfree(bus);
2294 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002295}
2296
Avi Kivity774c47f2007-02-12 00:54:47 -08002297static struct notifier_block kvm_cpu_notifier = {
2298 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002299};
2300
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002301static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002302{
2303 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002304 struct kvm *kvm;
2305
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002306 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002307 spin_lock(&kvm_lock);
2308 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002309 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002310 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002311 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002312}
2313
2314DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2315
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002316static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002317{
2318 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002319 struct kvm *kvm;
2320 struct kvm_vcpu *vcpu;
2321 int i;
2322
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002323 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002324 spin_lock(&kvm_lock);
2325 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002326 kvm_for_each_vcpu(i, vcpu, kvm)
2327 *val += *(u32 *)((void *)vcpu + offset);
2328
Avi Kivity1165f5f2007-04-19 17:27:43 +03002329 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002330 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002331}
2332
Avi Kivityba1389b2007-11-18 16:24:12 +02002333DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2334
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002335static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002336 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2337 [KVM_STAT_VM] = &vm_stat_fops,
2338};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002339
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002340static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341{
2342 struct kvm_stats_debugfs_item *p;
2343
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002344 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002346 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002347 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002348 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349}
2350
2351static void kvm_exit_debug(void)
2352{
2353 struct kvm_stats_debugfs_item *p;
2354
2355 for (p = debugfs_entries; p->name; ++p)
2356 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002357 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358}
2359
Avi Kivity59ae6c62007-02-12 00:54:48 -08002360static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2361{
Alexander Graf10474ae2009-09-15 11:37:46 +02002362 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002363 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002364 return 0;
2365}
2366
2367static int kvm_resume(struct sys_device *dev)
2368{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002369 if (kvm_usage_count) {
2370 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002371 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002372 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002373 return 0;
2374}
2375
2376static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002377 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002378 .suspend = kvm_suspend,
2379 .resume = kvm_resume,
2380};
2381
2382static struct sys_device kvm_sysdev = {
2383 .id = 0,
2384 .cls = &kvm_sysdev_class,
2385};
2386
Izik Eiduscea7bb22007-10-17 19:17:48 +02002387struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002388pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389
Avi Kivity15ad7142007-07-11 18:17:21 +03002390static inline
2391struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2392{
2393 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2394}
2395
2396static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2397{
2398 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2399
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002400 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002401}
2402
2403static void kvm_sched_out(struct preempt_notifier *pn,
2404 struct task_struct *next)
2405{
2406 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2407
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002408 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002409}
2410
Avi Kivity0ee75be2010-04-28 15:39:01 +03002411int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002412 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413{
2414 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002415 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002417 r = kvm_arch_init(opaque);
2418 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002419 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002420
2421 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2422
2423 if (bad_page == NULL) {
2424 r = -ENOMEM;
2425 goto out;
2426 }
2427
Anthony Liguori35149e22008-04-02 14:46:56 -05002428 bad_pfn = page_to_pfn(bad_page);
2429
Huang Yingbf998152010-05-31 14:28:19 +08002430 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2431
2432 if (hwpoison_page == NULL) {
2433 r = -ENOMEM;
2434 goto out_free_0;
2435 }
2436
2437 hwpoison_pfn = page_to_pfn(hwpoison_page);
2438
Gleb Natapovedba23e2010-07-07 20:16:45 +03002439 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2440
2441 if (fault_page == NULL) {
2442 r = -ENOMEM;
2443 goto out_free_0;
2444 }
2445
2446 fault_pfn = page_to_pfn(fault_page);
2447
Avi Kivity8437a612009-06-06 14:52:35 -07002448 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302449 r = -ENOMEM;
2450 goto out_free_0;
2451 }
2452
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002453 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302455 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456
Yang, Sheng002c7f72007-07-31 14:23:01 +03002457 for_each_online_cpu(cpu) {
2458 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002459 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002460 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002461 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002462 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002463 }
2464
Avi Kivity774c47f2007-02-12 00:54:47 -08002465 r = register_cpu_notifier(&kvm_cpu_notifier);
2466 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002467 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 register_reboot_notifier(&kvm_reboot_notifier);
2469
Avi Kivity59ae6c62007-02-12 00:54:48 -08002470 r = sysdev_class_register(&kvm_sysdev_class);
2471 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002472 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002473
2474 r = sysdev_register(&kvm_sysdev);
2475 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002476 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002477
Rusty Russellc16f8622007-07-30 21:12:19 +10002478 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002479 if (!vcpu_align)
2480 vcpu_align = __alignof__(struct kvm_vcpu);
2481 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002482 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002483 if (!kvm_vcpu_cache) {
2484 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002485 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002486 }
2487
Gleb Natapovaf585b92010-10-14 11:22:46 +02002488 r = kvm_async_pf_init();
2489 if (r)
2490 goto out_free;
2491
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002493 kvm_vm_fops.owner = module;
2494 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495
2496 r = misc_register(&kvm_dev);
2497 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002498 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002499 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 }
2501
Avi Kivity15ad7142007-07-11 18:17:21 +03002502 kvm_preempt_ops.sched_in = kvm_sched_in;
2503 kvm_preempt_ops.sched_out = kvm_sched_out;
2504
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002505 kvm_init_debug();
2506
Avi Kivityc7addb92007-09-16 18:58:32 +02002507 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508
Gleb Natapovaf585b92010-10-14 11:22:46 +02002509out_unreg:
2510 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002512 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002513out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002514 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002515out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002516 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002517out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002519 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002520out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002521out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002522 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302523out_free_0a:
2524 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002525out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002526 if (fault_page)
2527 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002528 if (hwpoison_page)
2529 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002530 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002531out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002532 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002533out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 return r;
2535}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002536EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002538void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002540 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002542 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002543 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002544 sysdev_unregister(&kvm_sysdev);
2545 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002547 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002548 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002549 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002550 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302551 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002552 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002553 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002555EXPORT_SYMBOL_GPL(kvm_exit);