blob: 880370caf9edbb3a256f143c79c86c1c12a4b8b9 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Hollis Blancharde2174022007-12-03 15:30:24 -060019#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/percpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020046#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020047#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010048#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040052#include <asm/io.h>
53#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020054#include <asm/pgtable.h>
Alexander Grafc8240bd2009-10-30 05:47:26 +000055#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Laurent Vivier5f94c172008-05-30 16:05:54 +020057#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020058#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020059
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060#define CREATE_TRACE_POINTS
61#include <trace/events/kvm.h>
62
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Marcelo Tosattifa40a822009-06-04 15:08:24 -030066/*
67 * Ordering of locks:
68 *
Sheng Yangfae3a352009-12-15 10:28:07 +080069 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030070 */
71
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072DEFINE_SPINLOCK(kvm_lock);
73LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080074
Rusty Russell7f59f492008-12-07 21:25:45 +103075static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020076static int kvm_usage_count = 0;
77static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030078
Rusty Russellc16f8622007-07-30 21:12:19 +100079struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030081
Avi Kivity15ad7142007-07-11 18:17:21 +030082static __read_mostly struct preempt_ops kvm_preempt_ops;
83
Hollis Blanchard76f7c872008-04-15 16:05:42 -050084struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivitybccf2152007-02-21 18:04:26 +020086static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020088static int hardware_enable_all(void);
89static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020090
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020091static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
92
Hannes Edere8ba5d32008-11-28 17:02:42 +010093static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030094
Marcelo Tosatti54dee992009-06-11 12:07:44 -030095static bool largepages_enabled = true;
96
Gleb Natapovfa7bff82010-07-07 20:16:44 +030097static struct page *hwpoison_page;
98static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +080099
Gleb Natapovedba23e2010-07-07 20:16:45 +0300100static struct page *fault_page;
101static pfn_t fault_pfn;
102
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800103inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300104{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100105 if (pfn_valid(pfn)) {
106 struct page *page = compound_head(pfn_to_page(pfn));
107 return PageReserved(page);
108 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300109
110 return true;
111}
112
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113/*
114 * Switches to specified vcpu, until a matching vcpu_put()
115 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200116void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117{
Avi Kivity15ad7142007-07-11 18:17:21 +0300118 int cpu;
119
Avi Kivitybccf2152007-02-21 18:04:26 +0200120 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300121 cpu = get_cpu();
122 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200123 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300124 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200125}
126
Carsten Otte313a3dc2007-10-11 19:16:52 +0200127void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128{
Avi Kivity15ad7142007-07-11 18:17:21 +0300129 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200130 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300131 preempt_notifier_unregister(&vcpu->preempt_notifier);
132 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133 mutex_unlock(&vcpu->mutex);
134}
135
Avi Kivityd9e368d2007-06-07 19:18:30 +0300136static void ack_flush(void *_completed)
137{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300138}
139
Rusty Russell49846892008-12-08 20:26:24 +1030140static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300141{
Avi Kivity597a5f52008-07-20 14:24:22 +0300142 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030143 cpumask_var_t cpus;
144 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300145 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300146
Li Zefan79f55992009-06-15 14:58:26 +0800147 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030148
Avi Kivity70e335e2010-02-18 11:25:22 +0200149 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200150 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300151 kvm_for_each_vcpu(i, vcpu, kvm) {
Avi Kivitya8eeb042010-05-10 12:34:53 +0300152 if (kvm_make_check_request(req, vcpu))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300153 continue;
154 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030155 if (cpus != NULL && cpu != -1 && cpu != me)
156 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300157 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030158 if (unlikely(cpus == NULL))
159 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
160 else if (!cpumask_empty(cpus))
161 smp_call_function_many(cpus, ack_flush, NULL, 1);
162 else
163 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200164 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030165 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030166 return called;
167}
168
169void kvm_flush_remote_tlbs(struct kvm *kvm)
170{
171 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
172 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300173}
174
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500175void kvm_reload_remote_mmus(struct kvm *kvm)
176{
Rusty Russell49846892008-12-08 20:26:24 +1030177 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500178}
179
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000180int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
181{
182 struct page *page;
183 int r;
184
185 mutex_init(&vcpu->mutex);
186 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000187 vcpu->kvm = kvm;
188 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300189 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200190 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000191
192 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
193 if (!page) {
194 r = -ENOMEM;
195 goto fail;
196 }
197 vcpu->run = page_address(page);
198
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800199 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000200 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800201 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000202 return 0;
203
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000204fail_free_run:
205 free_page((unsigned long)vcpu->run);
206fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000207 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000208}
209EXPORT_SYMBOL_GPL(kvm_vcpu_init);
210
211void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
212{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800213 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000214 free_page((unsigned long)vcpu->run);
215}
216EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
217
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200218#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
219static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
220{
221 return container_of(mn, struct kvm, mmu_notifier);
222}
223
224static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
225 struct mm_struct *mm,
226 unsigned long address)
227{
228 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200229 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200230
231 /*
232 * When ->invalidate_page runs, the linux pte has been zapped
233 * already but the page is still allocated until
234 * ->invalidate_page returns. So if we increase the sequence
235 * here the kvm page fault will notice if the spte can't be
236 * established because the page is going to be freed. If
237 * instead the kvm page fault establishes the spte before
238 * ->invalidate_page runs, kvm_unmap_hva will release it
239 * before returning.
240 *
241 * The sequence increase only need to be seen at spin_unlock
242 * time, and not at spin_lock time.
243 *
244 * Increasing the sequence after the spin_unlock would be
245 * unsafe because the kvm page fault could then establish the
246 * pte after kvm_unmap_hva returned, without noticing the page
247 * is going to be freed.
248 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200249 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200250 spin_lock(&kvm->mmu_lock);
251 kvm->mmu_notifier_seq++;
252 need_tlb_flush = kvm_unmap_hva(kvm, address);
253 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200254 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200255
256 /* we've to flush the tlb before the pages can be freed */
257 if (need_tlb_flush)
258 kvm_flush_remote_tlbs(kvm);
259
260}
261
Izik Eidus3da0dd42009-09-23 21:47:18 +0300262static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
263 struct mm_struct *mm,
264 unsigned long address,
265 pte_t pte)
266{
267 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200268 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300269
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200270 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300271 spin_lock(&kvm->mmu_lock);
272 kvm->mmu_notifier_seq++;
273 kvm_set_spte_hva(kvm, address, pte);
274 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200275 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300276}
277
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200278static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
279 struct mm_struct *mm,
280 unsigned long start,
281 unsigned long end)
282{
283 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200284 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200285
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200286 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200287 spin_lock(&kvm->mmu_lock);
288 /*
289 * The count increase must become visible at unlock time as no
290 * spte can be established without taking the mmu_lock and
291 * count is also read inside the mmu_lock critical section.
292 */
293 kvm->mmu_notifier_count++;
294 for (; start < end; start += PAGE_SIZE)
295 need_tlb_flush |= kvm_unmap_hva(kvm, start);
296 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200297 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200298
299 /* we've to flush the tlb before the pages can be freed */
300 if (need_tlb_flush)
301 kvm_flush_remote_tlbs(kvm);
302}
303
304static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
305 struct mm_struct *mm,
306 unsigned long start,
307 unsigned long end)
308{
309 struct kvm *kvm = mmu_notifier_to_kvm(mn);
310
311 spin_lock(&kvm->mmu_lock);
312 /*
313 * This sequence increase will notify the kvm page fault that
314 * the page that is going to be mapped in the spte could have
315 * been freed.
316 */
317 kvm->mmu_notifier_seq++;
318 /*
319 * The above sequence increase must be visible before the
320 * below count decrease but both values are read by the kvm
321 * page fault under mmu_lock spinlock so we don't need to add
322 * a smb_wmb() here in between the two.
323 */
324 kvm->mmu_notifier_count--;
325 spin_unlock(&kvm->mmu_lock);
326
327 BUG_ON(kvm->mmu_notifier_count < 0);
328}
329
330static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
331 struct mm_struct *mm,
332 unsigned long address)
333{
334 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200335 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200336
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200337 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200338 spin_lock(&kvm->mmu_lock);
339 young = kvm_age_hva(kvm, address);
340 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200341 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200342
343 if (young)
344 kvm_flush_remote_tlbs(kvm);
345
346 return young;
347}
348
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100349static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
350 struct mm_struct *mm)
351{
352 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800353 int idx;
354
355 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100356 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800357 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100358}
359
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200360static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
361 .invalidate_page = kvm_mmu_notifier_invalidate_page,
362 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
363 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
364 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300365 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100366 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200367};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200368
369static int kvm_init_mmu_notifier(struct kvm *kvm)
370{
371 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
372 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
373}
374
375#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
376
377static int kvm_init_mmu_notifier(struct kvm *kvm)
378{
379 return 0;
380}
381
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200382#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
383
Avi Kivityf17abe92007-02-21 19:28:04 +0200384static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200386 int r = 0, i;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800387 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800389 if (IS_ERR(kvm))
390 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200391
392 r = hardware_enable_all();
393 if (r)
394 goto out_err_nodisable;
395
Avi Kivity75858a82009-01-04 17:10:50 +0200396#ifdef CONFIG_HAVE_KVM_IRQCHIP
397 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300398 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200399#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200401 r = -ENOMEM;
402 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
403 if (!kvm->memslots)
404 goto out_err;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200405 if (init_srcu_struct(&kvm->srcu))
406 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200407 for (i = 0; i < KVM_NR_BUSES; i++) {
408 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
409 GFP_KERNEL);
410 if (!kvm->buses[i]) {
411 cleanup_srcu_struct(&kvm->srcu);
412 goto out_err;
413 }
414 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200415
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200416 r = kvm_init_mmu_notifier(kvm);
Avi Kivity283d0c62009-12-20 14:25:19 +0200417 if (r) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200418 cleanup_srcu_struct(&kvm->srcu);
Avi Kivity283d0c62009-12-20 14:25:19 +0200419 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200420 }
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);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800434out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200435 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200436
437out_err:
438 hardware_disable_all();
439out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200440 for (i = 0; i < KVM_NR_BUSES; i++)
441 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200442 kfree(kvm->memslots);
Alexander Graf10474ae2009-09-15 11:37:46 +0200443 kfree(kvm);
444 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200445}
446
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447/*
448 * Free any memory in @free but not in @dont.
449 */
450static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
451 struct kvm_memory_slot *dont)
452{
Joerg Roedelec04b262009-06-19 15:16:23 +0200453 int i;
454
Izik Eidus290fc382007-09-27 14:11:22 +0200455 if (!dont || free->rmap != dont->rmap)
456 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
458 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
459 vfree(free->dirty_bitmap);
460
Joerg Roedelec04b262009-06-19 15:16:23 +0200461
462 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
463 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
464 vfree(free->lpage_info[i]);
465 free->lpage_info[i] = NULL;
466 }
467 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000470 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500471 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472}
473
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800474void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475{
476 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200477 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200479 for (i = 0; i < slots->nmemslots; ++i)
480 kvm_free_physmem_slot(&slots->memslots[i], NULL);
481
482 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483}
484
Avi Kivityf17abe92007-02-21 19:28:04 +0200485static void kvm_destroy_vm(struct kvm *kvm)
486{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200487 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200488 struct mm_struct *mm = kvm->mm;
489
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800490 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800491 spin_lock(&kvm_lock);
492 list_del(&kvm->vm_list);
493 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200494 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200495 for (i = 0; i < KVM_NR_BUSES; i++)
496 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200497 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200498#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
499 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200500#else
501 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200502#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800503 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200504 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200505 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200506}
507
Izik Eidusd39f13b2008-03-30 16:01:25 +0300508void kvm_get_kvm(struct kvm *kvm)
509{
510 atomic_inc(&kvm->users_count);
511}
512EXPORT_SYMBOL_GPL(kvm_get_kvm);
513
514void kvm_put_kvm(struct kvm *kvm)
515{
516 if (atomic_dec_and_test(&kvm->users_count))
517 kvm_destroy_vm(kvm);
518}
519EXPORT_SYMBOL_GPL(kvm_put_kvm);
520
521
Avi Kivityf17abe92007-02-21 19:28:04 +0200522static int kvm_vm_release(struct inode *inode, struct file *filp)
523{
524 struct kvm *kvm = filp->private_data;
525
Gregory Haskins721eecb2009-05-20 10:30:49 -0400526 kvm_irqfd_release(kvm);
527
Izik Eidusd39f13b2008-03-30 16:01:25 +0300528 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529 return 0;
530}
531
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 * Allocate some memory and give it an address in the guest physical address
534 * space.
535 *
536 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800537 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500538 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800540int __kvm_set_memory_region(struct kvm *kvm,
541 struct kvm_userspace_memory_region *mem,
542 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200544 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200546 unsigned long npages;
547 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 struct kvm_memory_slot *memslot;
549 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200550 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551
552 r = -EINVAL;
553 /* General sanity checks */
554 if (mem->memory_size & (PAGE_SIZE - 1))
555 goto out;
556 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
557 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800558 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600559 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200560 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 goto out;
562 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
563 goto out;
564
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200565 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
567 npages = mem->memory_size >> PAGE_SHIFT;
568
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900569 r = -EINVAL;
570 if (npages > KVM_MEM_MAX_NR_PAGES)
571 goto out;
572
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 if (!npages)
574 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
575
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576 new = old = *memslot;
577
Avi Kivitye36d96f2010-06-21 10:56:36 +0300578 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 new.base_gfn = base_gfn;
580 new.npages = npages;
581 new.flags = mem->flags;
582
583 /* Disallow changing a memory slot's size. */
584 r = -EINVAL;
585 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800586 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587
588 /* Check for overlaps */
589 r = -EEXIST;
590 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200591 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200593 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 continue;
595 if (!((base_gfn + npages <= s->base_gfn) ||
596 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800597 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 /* Free page dirty bitmap if unneeded */
601 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000602 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603
604 r = -ENOMEM;
605
606 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200607#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500608 if (npages && !new.rmap) {
Lai Jiangshan3bd89002010-06-02 17:01:23 +0800609 new.rmap = vmalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200610
611 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800612 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200613
Izik Eidus290fc382007-09-27 14:11:22 +0200614 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500615
Izik Eidus80b14b52007-10-25 11:54:04 +0200616 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200617 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200619 if (!npages)
620 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300621
Joerg Roedelec04b262009-06-19 15:16:23 +0200622 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200623 unsigned long ugfn;
624 unsigned long j;
625 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200626 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300627
Joerg Roedelec04b262009-06-19 15:16:23 +0200628 /* Avoid unused variable warning if no large pages */
629 (void)level;
630
631 if (new.lpage_info[i])
632 continue;
633
Joerg Roedel82855412010-07-01 16:00:11 +0200634 lpages = 1 + ((base_gfn + npages - 1)
635 >> KVM_HPAGE_GFN_SHIFT(level));
636 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200637
638 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
639
640 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300641 goto out_free;
642
Joerg Roedelec04b262009-06-19 15:16:23 +0200643 memset(new.lpage_info[i], 0,
644 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300645
Joerg Roedel82855412010-07-01 16:00:11 +0200646 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200647 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200648 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200649 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300650 ugfn = new.userspace_addr >> PAGE_SHIFT;
651 /*
652 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300653 * other, or if explicitly asked to, disable large page
654 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300655 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200656 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300657 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200658 for (j = 0; j < lpages; ++j)
659 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300660 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661
Joerg Roedelec04b262009-06-19 15:16:23 +0200662skip_lpage:
663
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 /* Allocate page dirty bitmap if needed */
665 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900666 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667
668 new.dirty_bitmap = vmalloc(dirty_bytes);
669 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800670 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 memset(new.dirty_bitmap, 0, dirty_bytes);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200672 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300673 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200674 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200676#else /* not defined CONFIG_S390 */
677 new.user_alloc = user_alloc;
678 if (user_alloc)
679 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200680#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200682 if (!npages) {
683 r = -ENOMEM;
684 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
685 if (!slots)
686 goto out_free;
687 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
688 if (mem->slot >= slots->nmemslots)
689 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200690 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200691 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
692
693 old_memslots = kvm->memslots;
694 rcu_assign_pointer(kvm->memslots, slots);
695 synchronize_srcu_expedited(&kvm->srcu);
696 /* From this point no new shadow pages pointing to a deleted
697 * memslot will be created.
698 *
699 * validation of sp->gfn happens in:
700 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
701 * - kvm_is_visible_gfn (mmu_check_roots)
702 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300703 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200704 kfree(old_memslots);
705 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300706
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200707 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
708 if (r)
709 goto out_free;
710
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200711 /* map the pages in iommu page table */
712 if (npages) {
713 r = kvm_iommu_map_pages(kvm, &new);
714 if (r)
715 goto out_free;
716 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200717
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200718 r = -ENOMEM;
719 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
720 if (!slots)
721 goto out_free;
722 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
723 if (mem->slot >= slots->nmemslots)
724 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200725 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200726
727 /* actual memory is freed via old in kvm_free_physmem_slot below */
728 if (!npages) {
729 new.rmap = NULL;
730 new.dirty_bitmap = NULL;
731 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
732 new.lpage_info[i] = NULL;
733 }
734
735 slots->memslots[mem->slot] = new;
736 old_memslots = kvm->memslots;
737 rcu_assign_pointer(kvm->memslots, slots);
738 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200740 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800741
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200742 kvm_free_physmem_slot(&old, &new);
743 kfree(old_memslots);
744
745 if (flush_shadow)
746 kvm_arch_flush_shadow(kvm);
747
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 return 0;
749
Sheng Yangf78e0e22007-10-29 09:40:42 +0800750out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 kvm_free_physmem_slot(&new, &old);
752out:
753 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200754
755}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800756EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
757
758int kvm_set_memory_region(struct kvm *kvm,
759 struct kvm_userspace_memory_region *mem,
760 int user_alloc)
761{
762 int r;
763
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200764 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800765 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200766 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800767 return r;
768}
Izik Eidus210c7c42007-10-24 23:52:57 +0200769EXPORT_SYMBOL_GPL(kvm_set_memory_region);
770
Carsten Otte1fe779f2007-10-29 16:08:35 +0100771int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
772 struct
773 kvm_userspace_memory_region *mem,
774 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200775{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200776 if (mem->slot >= KVM_MEMORY_SLOTS)
777 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200778 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779}
780
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800781int kvm_get_dirty_log(struct kvm *kvm,
782 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783{
784 struct kvm_memory_slot *memslot;
785 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900786 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 unsigned long any = 0;
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 r = -EINVAL;
790 if (log->slot >= KVM_MEMORY_SLOTS)
791 goto out;
792
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200793 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 r = -ENOENT;
795 if (!memslot->dirty_bitmap)
796 goto out;
797
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900798 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799
Uri Lublincd1a4a92007-02-22 16:43:09 +0200800 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 any = memslot->dirty_bitmap[i];
802
803 r = -EFAULT;
804 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
805 goto out;
806
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800807 if (any)
808 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809
810 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 return r;
813}
814
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300815void kvm_disable_largepages(void)
816{
817 largepages_enabled = false;
818}
819EXPORT_SYMBOL_GPL(kvm_disable_largepages);
820
Izik Eiduscea7bb22007-10-17 19:17:48 +0200821int is_error_page(struct page *page)
822{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300823 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200824}
825EXPORT_SYMBOL_GPL(is_error_page);
826
Anthony Liguori35149e22008-04-02 14:46:56 -0500827int is_error_pfn(pfn_t pfn)
828{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300829 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500830}
831EXPORT_SYMBOL_GPL(is_error_pfn);
832
Huang Yingbf998152010-05-31 14:28:19 +0800833int is_hwpoison_pfn(pfn_t pfn)
834{
835 return pfn == hwpoison_pfn;
836}
837EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
838
Gleb Natapovedba23e2010-07-07 20:16:45 +0300839int is_fault_pfn(pfn_t pfn)
840{
841 return pfn == fault_pfn;
842}
843EXPORT_SYMBOL_GPL(is_fault_pfn);
844
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200845static inline unsigned long bad_hva(void)
846{
847 return PAGE_OFFSET;
848}
849
850int kvm_is_error_hva(unsigned long addr)
851{
852 return addr == bad_hva();
853}
854EXPORT_SYMBOL_GPL(kvm_is_error_hva);
855
Gleb Natapov49c77542010-10-18 15:22:23 +0200856static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
857 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858{
859 int i;
860
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200861 for (i = 0; i < slots->nmemslots; ++i) {
862 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
864 if (gfn >= memslot->base_gfn
865 && gfn < memslot->base_gfn + memslot->npages)
866 return memslot;
867 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000868 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869}
Gleb Natapov49c77542010-10-18 15:22:23 +0200870
871struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
872{
873 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
874}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300875EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876
Izik Eiduse0d62c72007-10-24 23:57:46 +0200877int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
878{
879 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800880 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200881
Izik Eiduse0d62c72007-10-24 23:57:46 +0200882 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200883 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200884
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200885 if (memslot->flags & KVM_MEMSLOT_INVALID)
886 continue;
887
Izik Eiduse0d62c72007-10-24 23:57:46 +0200888 if (gfn >= memslot->base_gfn
889 && gfn < memslot->base_gfn + memslot->npages)
890 return 1;
891 }
892 return 0;
893}
894EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
895
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100896unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
897{
898 struct vm_area_struct *vma;
899 unsigned long addr, size;
900
901 size = PAGE_SIZE;
902
903 addr = gfn_to_hva(kvm, gfn);
904 if (kvm_is_error_hva(addr))
905 return PAGE_SIZE;
906
907 down_read(&current->mm->mmap_sem);
908 vma = find_vma(current->mm, addr);
909 if (!vma)
910 goto out;
911
912 size = vma_kernel_pagesize(vma);
913
914out:
915 up_read(&current->mm->mmap_sem);
916
917 return size;
918}
919
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200920int memslot_id(struct kvm *kvm, gfn_t gfn)
921{
922 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800923 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200924 struct kvm_memory_slot *memslot = NULL;
925
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200926 for (i = 0; i < slots->nmemslots; ++i) {
927 memslot = &slots->memslots[i];
928
929 if (gfn >= memslot->base_gfn
930 && gfn < memslot->base_gfn + memslot->npages)
931 break;
932 }
933
934 return memslot - slots->memslots;
935}
936
Gleb Natapov49c77542010-10-18 15:22:23 +0200937static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +0800938 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +0200939{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200940 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200941 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +0800942
943 if (nr_pages)
944 *nr_pages = slot->npages - (gfn - slot->base_gfn);
945
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900946 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200947}
Xiao Guangrong48987782010-08-22 19:11:43 +0800948
949unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
950{
Gleb Natapov49c77542010-10-18 15:22:23 +0200951 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +0800952}
Sheng Yang0d150292008-04-25 21:44:50 +0800953EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200954
Gleb Natapov80300892010-10-19 18:13:41 +0200955static pfn_t get_fault_pfn(void)
956{
957 get_page(fault_page);
958 return fault_pfn;
959}
960
Gleb Natapovaf585b92010-10-14 11:22:46 +0200961static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200962 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +0300963{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500964 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +0200965 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500966 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300967
Gleb Natapovaf585b92010-10-14 11:22:46 +0200968 /* we can do it either atomically or asynchronously, not both */
969 BUG_ON(atomic && async);
970
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200971 BUG_ON(!write_fault && !writable);
972
973 if (writable)
974 *writable = true;
975
Gleb Natapovaf585b92010-10-14 11:22:46 +0200976 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +0800977 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200978
979 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +0800980 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -0200981
982 if (writable)
983 *writable = write_fault;
984
985 npages = get_user_pages_fast(addr, 1, write_fault, page);
986
987 /* map read fault as writable if possible */
988 if (unlikely(!write_fault) && npages == 1) {
989 struct page *wpage[1];
990
991 npages = __get_user_pages_fast(addr, 1, 1, wpage);
992 if (npages == 1) {
993 *writable = true;
994 put_page(page[0]);
995 page[0] = wpage[0];
996 }
997 npages = 1;
998 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +0800999 }
Izik Eidus539cb662007-11-11 22:05:04 +02001000
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001001 if (unlikely(npages != 1)) {
1002 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001003
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001004 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001005 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001006
Huang Yingbbeb3402010-06-22 14:23:11 +08001007 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001008 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001009 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001010 get_page(hwpoison_page);
1011 return page_to_pfn(hwpoison_page);
1012 }
1013
Gleb Natapov80300892010-10-19 18:13:41 +02001014 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001015
Gleb Natapov80300892010-10-19 18:13:41 +02001016 if (vma == NULL)
1017 pfn = get_fault_pfn();
1018 else if ((vma->vm_flags & VM_PFNMAP)) {
1019 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1020 vma->vm_pgoff;
1021 BUG_ON(!kvm_is_mmio_pfn(pfn));
1022 } else {
1023 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001024 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001025 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001026 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001027 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001028 } else
1029 pfn = page_to_pfn(page[0]);
1030
1031 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001032}
1033
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001034pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1035{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001036 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001037}
1038EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1039
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001040static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1041 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001042{
1043 unsigned long addr;
1044
Gleb Natapovaf585b92010-10-14 11:22:46 +02001045 if (async)
1046 *async = false;
1047
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001048 addr = gfn_to_hva(kvm, gfn);
1049 if (kvm_is_error_hva(addr)) {
1050 get_page(bad_page);
1051 return page_to_pfn(bad_page);
1052 }
1053
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001054 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001055}
1056
1057pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1058{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001059 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001060}
1061EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1062
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001063pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1064 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001065{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001066 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001067}
1068EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1069
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001070pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1071{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001072 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001073}
Anthony Liguori35149e22008-04-02 14:46:56 -05001074EXPORT_SYMBOL_GPL(gfn_to_pfn);
1075
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001076pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1077 bool *writable)
1078{
1079 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1080}
1081EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1082
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001083pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1084 struct kvm_memory_slot *slot, gfn_t gfn)
1085{
1086 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001087 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001088}
1089
Xiao Guangrong48987782010-08-22 19:11:43 +08001090int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1091 int nr_pages)
1092{
1093 unsigned long addr;
1094 gfn_t entry;
1095
Gleb Natapov49c77542010-10-18 15:22:23 +02001096 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001097 if (kvm_is_error_hva(addr))
1098 return -1;
1099
1100 if (entry < nr_pages)
1101 return 0;
1102
1103 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1104}
1105EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1106
Anthony Liguori35149e22008-04-02 14:46:56 -05001107struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1108{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001109 pfn_t pfn;
1110
1111 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001112 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001113 return pfn_to_page(pfn);
1114
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001115 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001116
1117 get_page(bad_page);
1118 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001119}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001120
Avi Kivity954bbbc22007-03-30 14:02:32 +03001121EXPORT_SYMBOL_GPL(gfn_to_page);
1122
Izik Eidusb4231d62007-11-20 11:49:33 +02001123void kvm_release_page_clean(struct page *page)
1124{
Anthony Liguori35149e22008-04-02 14:46:56 -05001125 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001126}
1127EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1128
Anthony Liguori35149e22008-04-02 14:46:56 -05001129void kvm_release_pfn_clean(pfn_t pfn)
1130{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001131 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001132 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001133}
1134EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1135
Izik Eidusb4231d62007-11-20 11:49:33 +02001136void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001137{
Anthony Liguori35149e22008-04-02 14:46:56 -05001138 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001139}
Izik Eidusb4231d62007-11-20 11:49:33 +02001140EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001141
Anthony Liguori35149e22008-04-02 14:46:56 -05001142void kvm_release_pfn_dirty(pfn_t pfn)
1143{
1144 kvm_set_pfn_dirty(pfn);
1145 kvm_release_pfn_clean(pfn);
1146}
1147EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1148
1149void kvm_set_page_dirty(struct page *page)
1150{
1151 kvm_set_pfn_dirty(page_to_pfn(page));
1152}
1153EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1154
1155void kvm_set_pfn_dirty(pfn_t pfn)
1156{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001157 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001158 struct page *page = pfn_to_page(pfn);
1159 if (!PageReserved(page))
1160 SetPageDirty(page);
1161 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001162}
1163EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1164
1165void kvm_set_pfn_accessed(pfn_t pfn)
1166{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001167 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001168 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001169}
1170EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1171
1172void kvm_get_pfn(pfn_t pfn)
1173{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001174 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001175 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001176}
1177EXPORT_SYMBOL_GPL(kvm_get_pfn);
1178
Izik Eidus195aefd2007-10-01 22:14:18 +02001179static int next_segment(unsigned long len, int offset)
1180{
1181 if (len > PAGE_SIZE - offset)
1182 return PAGE_SIZE - offset;
1183 else
1184 return len;
1185}
1186
1187int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1188 int len)
1189{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001190 int r;
1191 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001192
Izik Eiduse0506bc2007-11-11 22:10:22 +02001193 addr = gfn_to_hva(kvm, gfn);
1194 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001195 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001196 r = copy_from_user(data, (void __user *)addr + offset, len);
1197 if (r)
1198 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001199 return 0;
1200}
1201EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1202
1203int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1204{
1205 gfn_t gfn = gpa >> PAGE_SHIFT;
1206 int seg;
1207 int offset = offset_in_page(gpa);
1208 int ret;
1209
1210 while ((seg = next_segment(len, offset)) != 0) {
1211 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1212 if (ret < 0)
1213 return ret;
1214 offset = 0;
1215 len -= seg;
1216 data += seg;
1217 ++gfn;
1218 }
1219 return 0;
1220}
1221EXPORT_SYMBOL_GPL(kvm_read_guest);
1222
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001223int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1224 unsigned long len)
1225{
1226 int r;
1227 unsigned long addr;
1228 gfn_t gfn = gpa >> PAGE_SHIFT;
1229 int offset = offset_in_page(gpa);
1230
1231 addr = gfn_to_hva(kvm, gfn);
1232 if (kvm_is_error_hva(addr))
1233 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001234 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001235 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001236 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001237 if (r)
1238 return -EFAULT;
1239 return 0;
1240}
1241EXPORT_SYMBOL(kvm_read_guest_atomic);
1242
Izik Eidus195aefd2007-10-01 22:14:18 +02001243int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1244 int offset, int len)
1245{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001246 int r;
1247 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001248
Izik Eiduse0506bc2007-11-11 22:10:22 +02001249 addr = gfn_to_hva(kvm, gfn);
1250 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001251 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001252 r = copy_to_user((void __user *)addr + offset, data, len);
1253 if (r)
1254 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001255 mark_page_dirty(kvm, gfn);
1256 return 0;
1257}
1258EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1259
1260int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1261 unsigned long len)
1262{
1263 gfn_t gfn = gpa >> PAGE_SHIFT;
1264 int seg;
1265 int offset = offset_in_page(gpa);
1266 int ret;
1267
1268 while ((seg = next_segment(len, offset)) != 0) {
1269 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1270 if (ret < 0)
1271 return ret;
1272 offset = 0;
1273 len -= seg;
1274 data += seg;
1275 ++gfn;
1276 }
1277 return 0;
1278}
1279
Gleb Natapov49c77542010-10-18 15:22:23 +02001280int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1281 gpa_t gpa)
1282{
1283 struct kvm_memslots *slots = kvm_memslots(kvm);
1284 int offset = offset_in_page(gpa);
1285 gfn_t gfn = gpa >> PAGE_SHIFT;
1286
1287 ghc->gpa = gpa;
1288 ghc->generation = slots->generation;
1289 ghc->memslot = __gfn_to_memslot(slots, gfn);
1290 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1291 if (!kvm_is_error_hva(ghc->hva))
1292 ghc->hva += offset;
1293 else
1294 return -EFAULT;
1295
1296 return 0;
1297}
1298EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1299
1300int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1301 void *data, unsigned long len)
1302{
1303 struct kvm_memslots *slots = kvm_memslots(kvm);
1304 int r;
1305
1306 if (slots->generation != ghc->generation)
1307 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1308
1309 if (kvm_is_error_hva(ghc->hva))
1310 return -EFAULT;
1311
1312 r = copy_to_user((void __user *)ghc->hva, data, len);
1313 if (r)
1314 return -EFAULT;
1315 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1316
1317 return 0;
1318}
1319EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1320
Izik Eidus195aefd2007-10-01 22:14:18 +02001321int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1322{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001323 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001324}
1325EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1326
1327int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1328{
1329 gfn_t gfn = gpa >> PAGE_SHIFT;
1330 int seg;
1331 int offset = offset_in_page(gpa);
1332 int ret;
1333
1334 while ((seg = next_segment(len, offset)) != 0) {
1335 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1336 if (ret < 0)
1337 return ret;
1338 offset = 0;
1339 len -= seg;
1340 ++gfn;
1341 }
1342 return 0;
1343}
1344EXPORT_SYMBOL_GPL(kvm_clear_guest);
1345
Gleb Natapov49c77542010-10-18 15:22:23 +02001346void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1347 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001349 if (memslot && memslot->dirty_bitmap) {
1350 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001352 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 }
1354}
1355
Gleb Natapov49c77542010-10-18 15:22:23 +02001356void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1357{
1358 struct kvm_memory_slot *memslot;
1359
1360 memslot = gfn_to_memslot(kvm, gfn);
1361 mark_page_dirty_in_slot(kvm, memslot, gfn);
1362}
1363
Eddie Dongb6958ce2007-07-18 12:15:21 +03001364/*
1365 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1366 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001367void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001368{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001369 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001370
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001371 for (;;) {
1372 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001373
Gleb Natapova1b37102009-07-09 15:33:52 +03001374 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001375 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001376 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001377 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001378 if (kvm_cpu_has_pending_timer(vcpu))
1379 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001380 if (signal_pending(current))
1381 break;
1382
Eddie Dongb6958ce2007-07-18 12:15:21 +03001383 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001384 }
1385
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001386 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001387}
1388
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389void kvm_resched(struct kvm_vcpu *vcpu)
1390{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001391 if (!need_resched())
1392 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394}
1395EXPORT_SYMBOL_GPL(kvm_resched);
1396
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001397void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1398{
1399 ktime_t expires;
1400 DEFINE_WAIT(wait);
1401
1402 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1403
1404 /* Sleep for 100 us, and hope lock-holder got scheduled */
1405 expires = ktime_add_ns(ktime_get(), 100000UL);
1406 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1407
1408 finish_wait(&vcpu->wq, &wait);
1409}
1410EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1411
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001412static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001413{
1414 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001415 struct page *page;
1416
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001417 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001418 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001419#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001420 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001421 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001422#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001423#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1424 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1425 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1426#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001427 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001428 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001429 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001430 vmf->page = page;
1431 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001432}
1433
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001434static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001435 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001436};
1437
1438static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1439{
1440 vma->vm_ops = &kvm_vcpu_vm_ops;
1441 return 0;
1442}
1443
Avi Kivitybccf2152007-02-21 18:04:26 +02001444static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1445{
1446 struct kvm_vcpu *vcpu = filp->private_data;
1447
Al Viro66c0b392008-04-19 20:33:56 +01001448 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001449 return 0;
1450}
1451
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001452static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001453 .release = kvm_vcpu_release,
1454 .unlocked_ioctl = kvm_vcpu_ioctl,
1455 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001456 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001457 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001458};
1459
1460/*
1461 * Allocates an inode for the vcpu.
1462 */
1463static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1464{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001465 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001466}
1467
Avi Kivityc5ea7662007-02-20 18:41:05 +02001468/*
1469 * Creates some virtual cpus. Good luck creating more than one.
1470 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001471static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001472{
1473 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001474 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001475
Gleb Natapov73880c82009-06-09 15:56:28 +03001476 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001477 if (IS_ERR(vcpu))
1478 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001479
Avi Kivity15ad7142007-07-11 18:17:21 +03001480 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1481
Avi Kivity26e52152007-11-20 15:30:24 +02001482 r = kvm_arch_vcpu_setup(vcpu);
1483 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001484 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001485
Shaohua Li11ec2802007-07-23 14:51:37 +08001486 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001487 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1488 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001489 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001490 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001491
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001492 kvm_for_each_vcpu(r, v, kvm)
1493 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001494 r = -EEXIST;
1495 goto vcpu_destroy;
1496 }
1497
1498 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001499
1500 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001501 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001502 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001503 if (r < 0) {
1504 kvm_put_kvm(kvm);
1505 goto vcpu_destroy;
1506 }
1507
1508 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1509 smp_wmb();
1510 atomic_inc(&kvm->online_vcpus);
1511
1512#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1513 if (kvm->bsp_vcpu_id == id)
1514 kvm->bsp_vcpu = vcpu;
1515#endif
1516 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001517 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001518
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001519vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001520 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001521 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001522 return r;
1523}
1524
Avi Kivity1961d272007-03-05 19:46:05 +02001525static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1526{
1527 if (sigset) {
1528 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1529 vcpu->sigset_active = 1;
1530 vcpu->sigset = *sigset;
1531 } else
1532 vcpu->sigset_active = 0;
1533 return 0;
1534}
1535
Avi Kivitybccf2152007-02-21 18:04:26 +02001536static long kvm_vcpu_ioctl(struct file *filp,
1537 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538{
Avi Kivitybccf2152007-02-21 18:04:26 +02001539 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001540 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001541 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001542 struct kvm_fpu *fpu = NULL;
1543 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001545 if (vcpu->kvm->mm != current->mm)
1546 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001547
1548#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1549 /*
1550 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1551 * so vcpu_load() would break it.
1552 */
1553 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1554 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1555#endif
1556
1557
1558 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001560 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001561 r = -EINVAL;
1562 if (arg)
1563 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001564 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001565 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001568 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001570 r = -ENOMEM;
1571 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1572 if (!kvm_regs)
1573 goto out;
1574 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001576 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001578 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1579 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001581out_free1:
1582 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 break;
1584 }
1585 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001586 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001588 r = -ENOMEM;
1589 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1590 if (!kvm_regs)
1591 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001593 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1594 goto out_free2;
1595 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001597 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001599out_free2:
1600 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601 break;
1602 }
1603 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001604 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1605 r = -ENOMEM;
1606 if (!kvm_sregs)
1607 goto out;
1608 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609 if (r)
1610 goto out;
1611 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001612 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 goto out;
1614 r = 0;
1615 break;
1616 }
1617 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001618 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1619 r = -ENOMEM;
1620 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001622 r = -EFAULT;
1623 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1624 goto out;
1625 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 if (r)
1627 goto out;
1628 r = 0;
1629 break;
1630 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001631 case KVM_GET_MP_STATE: {
1632 struct kvm_mp_state mp_state;
1633
1634 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1635 if (r)
1636 goto out;
1637 r = -EFAULT;
1638 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1639 goto out;
1640 r = 0;
1641 break;
1642 }
1643 case KVM_SET_MP_STATE: {
1644 struct kvm_mp_state mp_state;
1645
1646 r = -EFAULT;
1647 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1648 goto out;
1649 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1650 if (r)
1651 goto out;
1652 r = 0;
1653 break;
1654 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 case KVM_TRANSLATE: {
1656 struct kvm_translation tr;
1657
1658 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001659 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001661 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 if (r)
1663 goto out;
1664 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001665 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666 goto out;
1667 r = 0;
1668 break;
1669 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001670 case KVM_SET_GUEST_DEBUG: {
1671 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672
1673 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001674 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001676 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 if (r)
1678 goto out;
1679 r = 0;
1680 break;
1681 }
Avi Kivity1961d272007-03-05 19:46:05 +02001682 case KVM_SET_SIGNAL_MASK: {
1683 struct kvm_signal_mask __user *sigmask_arg = argp;
1684 struct kvm_signal_mask kvm_sigmask;
1685 sigset_t sigset, *p;
1686
1687 p = NULL;
1688 if (argp) {
1689 r = -EFAULT;
1690 if (copy_from_user(&kvm_sigmask, argp,
1691 sizeof kvm_sigmask))
1692 goto out;
1693 r = -EINVAL;
1694 if (kvm_sigmask.len != sizeof sigset)
1695 goto out;
1696 r = -EFAULT;
1697 if (copy_from_user(&sigset, sigmask_arg->sigset,
1698 sizeof sigset))
1699 goto out;
1700 p = &sigset;
1701 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001702 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001703 break;
1704 }
Avi Kivityb8836732007-04-01 16:34:31 +03001705 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001706 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1707 r = -ENOMEM;
1708 if (!fpu)
1709 goto out;
1710 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001711 if (r)
1712 goto out;
1713 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001714 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001715 goto out;
1716 r = 0;
1717 break;
1718 }
1719 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001720 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1721 r = -ENOMEM;
1722 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001723 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001724 r = -EFAULT;
1725 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1726 goto out;
1727 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001728 if (r)
1729 goto out;
1730 r = 0;
1731 break;
1732 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001733 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001734 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001735 }
1736out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001737 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001738 kfree(fpu);
1739 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001740 return r;
1741}
1742
1743static long kvm_vm_ioctl(struct file *filp,
1744 unsigned int ioctl, unsigned long arg)
1745{
1746 struct kvm *kvm = filp->private_data;
1747 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001748 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001749
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001750 if (kvm->mm != current->mm)
1751 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001752 switch (ioctl) {
1753 case KVM_CREATE_VCPU:
1754 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1755 if (r < 0)
1756 goto out;
1757 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001758 case KVM_SET_USER_MEMORY_REGION: {
1759 struct kvm_userspace_memory_region kvm_userspace_mem;
1760
1761 r = -EFAULT;
1762 if (copy_from_user(&kvm_userspace_mem, argp,
1763 sizeof kvm_userspace_mem))
1764 goto out;
1765
1766 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 if (r)
1768 goto out;
1769 break;
1770 }
1771 case KVM_GET_DIRTY_LOG: {
1772 struct kvm_dirty_log log;
1773
1774 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001775 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001777 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 if (r)
1779 goto out;
1780 break;
1781 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001782#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1783 case KVM_REGISTER_COALESCED_MMIO: {
1784 struct kvm_coalesced_mmio_zone zone;
1785 r = -EFAULT;
1786 if (copy_from_user(&zone, argp, sizeof zone))
1787 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001788 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1789 if (r)
1790 goto out;
1791 r = 0;
1792 break;
1793 }
1794 case KVM_UNREGISTER_COALESCED_MMIO: {
1795 struct kvm_coalesced_mmio_zone zone;
1796 r = -EFAULT;
1797 if (copy_from_user(&zone, argp, sizeof zone))
1798 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001799 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1800 if (r)
1801 goto out;
1802 r = 0;
1803 break;
1804 }
1805#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001806 case KVM_IRQFD: {
1807 struct kvm_irqfd data;
1808
1809 r = -EFAULT;
1810 if (copy_from_user(&data, argp, sizeof data))
1811 goto out;
1812 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1813 break;
1814 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001815 case KVM_IOEVENTFD: {
1816 struct kvm_ioeventfd data;
1817
1818 r = -EFAULT;
1819 if (copy_from_user(&data, argp, sizeof data))
1820 goto out;
1821 r = kvm_ioeventfd(kvm, &data);
1822 break;
1823 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001824#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1825 case KVM_SET_BOOT_CPU_ID:
1826 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001827 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001828 if (atomic_read(&kvm->online_vcpus) != 0)
1829 r = -EBUSY;
1830 else
1831 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001832 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001833 break;
1834#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001835 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001836 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001837 if (r == -ENOTTY)
1838 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001839 }
1840out:
1841 return r;
1842}
1843
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001844#ifdef CONFIG_COMPAT
1845struct compat_kvm_dirty_log {
1846 __u32 slot;
1847 __u32 padding1;
1848 union {
1849 compat_uptr_t dirty_bitmap; /* one bit per page */
1850 __u64 padding2;
1851 };
1852};
1853
1854static long kvm_vm_compat_ioctl(struct file *filp,
1855 unsigned int ioctl, unsigned long arg)
1856{
1857 struct kvm *kvm = filp->private_data;
1858 int r;
1859
1860 if (kvm->mm != current->mm)
1861 return -EIO;
1862 switch (ioctl) {
1863 case KVM_GET_DIRTY_LOG: {
1864 struct compat_kvm_dirty_log compat_log;
1865 struct kvm_dirty_log log;
1866
1867 r = -EFAULT;
1868 if (copy_from_user(&compat_log, (void __user *)arg,
1869 sizeof(compat_log)))
1870 goto out;
1871 log.slot = compat_log.slot;
1872 log.padding1 = compat_log.padding1;
1873 log.padding2 = compat_log.padding2;
1874 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1875
1876 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1877 if (r)
1878 goto out;
1879 break;
1880 }
1881 default:
1882 r = kvm_vm_ioctl(filp, ioctl, arg);
1883 }
1884
1885out:
1886 return r;
1887}
1888#endif
1889
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001890static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001891{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001892 struct page *page[1];
1893 unsigned long addr;
1894 int npages;
1895 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001896 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001897
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001898 addr = gfn_to_hva(kvm, gfn);
1899 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001900 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001901
1902 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1903 NULL);
1904 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001905 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001906
1907 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001908 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001909}
1910
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001911static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001912 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001913};
1914
1915static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1916{
1917 vma->vm_ops = &kvm_vm_vm_ops;
1918 return 0;
1919}
1920
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001921static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001922 .release = kvm_vm_release,
1923 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001924#ifdef CONFIG_COMPAT
1925 .compat_ioctl = kvm_vm_compat_ioctl,
1926#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001927 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001928 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001929};
1930
1931static int kvm_dev_ioctl_create_vm(void)
1932{
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001933 int fd, r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001934 struct kvm *kvm;
1935
Avi Kivityf17abe92007-02-21 19:28:04 +02001936 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001937 if (IS_ERR(kvm))
1938 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001939#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1940 r = kvm_coalesced_mmio_init(kvm);
1941 if (r < 0) {
1942 kvm_put_kvm(kvm);
1943 return r;
1944 }
1945#endif
Roland Dreier628ff7c2009-12-18 09:41:24 -08001946 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
Al Viro2030a422008-02-23 06:46:49 -05001947 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001948 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001949
Avi Kivityf17abe92007-02-21 19:28:04 +02001950 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001951}
1952
Avi Kivity1a811b62008-12-08 18:25:27 +02001953static long kvm_dev_ioctl_check_extension_generic(long arg)
1954{
1955 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001956 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001957 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001958 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001959#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1960 case KVM_CAP_SET_BOOT_CPU_ID:
1961#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001962 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001963 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001964#ifdef CONFIG_HAVE_KVM_IRQCHIP
1965 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001966 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001967#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001968 default:
1969 break;
1970 }
1971 return kvm_dev_ioctl_check_extension(arg);
1972}
1973
Avi Kivityf17abe92007-02-21 19:28:04 +02001974static long kvm_dev_ioctl(struct file *filp,
1975 unsigned int ioctl, unsigned long arg)
1976{
Avi Kivity07c45a32007-03-07 13:05:38 +02001977 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001978
1979 switch (ioctl) {
1980 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001981 r = -EINVAL;
1982 if (arg)
1983 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001984 r = KVM_API_VERSION;
1985 break;
1986 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001987 r = -EINVAL;
1988 if (arg)
1989 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001990 r = kvm_dev_ioctl_create_vm();
1991 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001992 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001993 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001994 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001995 case KVM_GET_VCPU_MMAP_SIZE:
1996 r = -EINVAL;
1997 if (arg)
1998 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001999 r = PAGE_SIZE; /* struct kvm_run */
2000#ifdef CONFIG_X86
2001 r += PAGE_SIZE; /* pio data page */
2002#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002003#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2004 r += PAGE_SIZE; /* coalesced mmio ring page */
2005#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002006 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002007 case KVM_TRACE_ENABLE:
2008 case KVM_TRACE_PAUSE:
2009 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002010 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002013 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 }
2015out:
2016 return r;
2017}
2018
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020 .unlocked_ioctl = kvm_dev_ioctl,
2021 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002022 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023};
2024
2025static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002026 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 "kvm",
2028 &kvm_chardev_ops,
2029};
2030
Avi Kivity1b6c0162007-05-24 13:03:52 +03002031static void hardware_enable(void *junk)
2032{
2033 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002034 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002035
Rusty Russell7f59f492008-12-07 21:25:45 +10302036 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002037 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002038
Rusty Russell7f59f492008-12-07 21:25:45 +10302039 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002040
2041 r = kvm_arch_hardware_enable(NULL);
2042
2043 if (r) {
2044 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2045 atomic_inc(&hardware_enable_failed);
2046 printk(KERN_INFO "kvm: enabling virtualization on "
2047 "CPU%d failed\n", cpu);
2048 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002049}
2050
2051static void hardware_disable(void *junk)
2052{
2053 int cpu = raw_smp_processor_id();
2054
Rusty Russell7f59f492008-12-07 21:25:45 +10302055 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002056 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302057 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002058 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002059}
2060
Alexander Graf10474ae2009-09-15 11:37:46 +02002061static void hardware_disable_all_nolock(void)
2062{
2063 BUG_ON(!kvm_usage_count);
2064
2065 kvm_usage_count--;
2066 if (!kvm_usage_count)
2067 on_each_cpu(hardware_disable, NULL, 1);
2068}
2069
2070static void hardware_disable_all(void)
2071{
2072 spin_lock(&kvm_lock);
2073 hardware_disable_all_nolock();
2074 spin_unlock(&kvm_lock);
2075}
2076
2077static int hardware_enable_all(void)
2078{
2079 int r = 0;
2080
2081 spin_lock(&kvm_lock);
2082
2083 kvm_usage_count++;
2084 if (kvm_usage_count == 1) {
2085 atomic_set(&hardware_enable_failed, 0);
2086 on_each_cpu(hardware_enable, NULL, 1);
2087
2088 if (atomic_read(&hardware_enable_failed)) {
2089 hardware_disable_all_nolock();
2090 r = -EBUSY;
2091 }
2092 }
2093
2094 spin_unlock(&kvm_lock);
2095
2096 return r;
2097}
2098
Avi Kivity774c47f2007-02-12 00:54:47 -08002099static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2100 void *v)
2101{
2102 int cpu = (long)v;
2103
Alexander Graf10474ae2009-09-15 11:37:46 +02002104 if (!kvm_usage_count)
2105 return NOTIFY_OK;
2106
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002107 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002108 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002109 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002110 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2111 cpu);
2112 hardware_disable(NULL);
2113 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002114 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002115 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2116 cpu);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002117 spin_lock(&kvm_lock);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002118 hardware_enable(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002119 spin_unlock(&kvm_lock);
Avi Kivity774c47f2007-02-12 00:54:47 -08002120 break;
2121 }
2122 return NOTIFY_OK;
2123}
2124
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002125
2126asmlinkage void kvm_handle_fault_on_reboot(void)
2127{
Avi Kivityca242ac2010-09-21 19:59:43 +02002128 if (kvm_rebooting) {
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002129 /* spin while reset goes on */
Avi Kivityca242ac2010-09-21 19:59:43 +02002130 local_irq_enable();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002131 while (true)
Avi Kivity624d84c2010-09-21 19:59:44 +02002132 cpu_relax();
Avi Kivityca242ac2010-09-21 19:59:43 +02002133 }
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002134 /* Fault while not rebooting. We want the trace. */
2135 BUG();
2136}
2137EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2138
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002139static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002140 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002141{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002142 /*
2143 * Some (well, at least mine) BIOSes hang on reboot if
2144 * in vmx root mode.
2145 *
2146 * And Intel TXT required VMX off for all cpu when system shutdown.
2147 */
2148 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2149 kvm_rebooting = true;
2150 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002151 return NOTIFY_OK;
2152}
2153
2154static struct notifier_block kvm_reboot_notifier = {
2155 .notifier_call = kvm_reboot,
2156 .priority = 0,
2157};
2158
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002159static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002160{
2161 int i;
2162
2163 for (i = 0; i < bus->dev_count; i++) {
2164 struct kvm_io_device *pos = bus->devs[i];
2165
2166 kvm_iodevice_destructor(pos);
2167 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002168 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002169}
2170
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002171/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002172int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002173 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002174{
2175 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002176 struct kvm_io_bus *bus;
2177
2178 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002179 for (i = 0; i < bus->dev_count; i++)
2180 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2181 return 0;
2182 return -EOPNOTSUPP;
2183}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002184
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002185/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002186int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2187 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002188{
2189 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002190 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002191
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002192 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002193 for (i = 0; i < bus->dev_count; i++)
2194 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2195 return 0;
2196 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002197}
2198
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002199/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002200int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2201 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002202{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002203 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002204
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002205 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002206 if (bus->dev_count > NR_IOBUS_DEVS-1)
2207 return -ENOSPC;
2208
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002209 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2210 if (!new_bus)
2211 return -ENOMEM;
2212 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2213 new_bus->devs[new_bus->dev_count++] = dev;
2214 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2215 synchronize_srcu_expedited(&kvm->srcu);
2216 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002217
2218 return 0;
2219}
2220
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002221/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002222int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2223 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002224{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002225 int i, r;
2226 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002227
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002228 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2229 if (!new_bus)
2230 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002231
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002232 bus = kvm->buses[bus_idx];
2233 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2234
2235 r = -ENOENT;
2236 for (i = 0; i < new_bus->dev_count; i++)
2237 if (new_bus->devs[i] == dev) {
2238 r = 0;
2239 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002240 break;
2241 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002242
2243 if (r) {
2244 kfree(new_bus);
2245 return r;
2246 }
2247
2248 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2249 synchronize_srcu_expedited(&kvm->srcu);
2250 kfree(bus);
2251 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002252}
2253
Avi Kivity774c47f2007-02-12 00:54:47 -08002254static struct notifier_block kvm_cpu_notifier = {
2255 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002256};
2257
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002258static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002259{
2260 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002261 struct kvm *kvm;
2262
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002263 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002264 spin_lock(&kvm_lock);
2265 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002266 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002267 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002268 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002269}
2270
2271DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2272
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002273static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002274{
2275 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002276 struct kvm *kvm;
2277 struct kvm_vcpu *vcpu;
2278 int i;
2279
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002280 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002281 spin_lock(&kvm_lock);
2282 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002283 kvm_for_each_vcpu(i, vcpu, kvm)
2284 *val += *(u32 *)((void *)vcpu + offset);
2285
Avi Kivity1165f5f2007-04-19 17:27:43 +03002286 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002287 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002288}
2289
Avi Kivityba1389b2007-11-18 16:24:12 +02002290DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2291
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002292static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002293 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2294 [KVM_STAT_VM] = &vm_stat_fops,
2295};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002296
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002297static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298{
2299 struct kvm_stats_debugfs_item *p;
2300
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002301 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002303 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002304 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002305 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306}
2307
2308static void kvm_exit_debug(void)
2309{
2310 struct kvm_stats_debugfs_item *p;
2311
2312 for (p = debugfs_entries; p->name; ++p)
2313 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002314 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315}
2316
Avi Kivity59ae6c62007-02-12 00:54:48 -08002317static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2318{
Alexander Graf10474ae2009-09-15 11:37:46 +02002319 if (kvm_usage_count)
2320 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002321 return 0;
2322}
2323
2324static int kvm_resume(struct sys_device *dev)
2325{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002326 if (kvm_usage_count) {
2327 WARN_ON(spin_is_locked(&kvm_lock));
Alexander Graf10474ae2009-09-15 11:37:46 +02002328 hardware_enable(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002329 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002330 return 0;
2331}
2332
2333static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002334 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002335 .suspend = kvm_suspend,
2336 .resume = kvm_resume,
2337};
2338
2339static struct sys_device kvm_sysdev = {
2340 .id = 0,
2341 .cls = &kvm_sysdev_class,
2342};
2343
Izik Eiduscea7bb22007-10-17 19:17:48 +02002344struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002345pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
Avi Kivity15ad7142007-07-11 18:17:21 +03002347static inline
2348struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2349{
2350 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2351}
2352
2353static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2354{
2355 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2356
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002357 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002358}
2359
2360static void kvm_sched_out(struct preempt_notifier *pn,
2361 struct task_struct *next)
2362{
2363 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2364
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002365 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002366}
2367
Avi Kivity0ee75be2010-04-28 15:39:01 +03002368int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002369 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
2371 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002372 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002374 r = kvm_arch_init(opaque);
2375 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002376 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002377
2378 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2379
2380 if (bad_page == NULL) {
2381 r = -ENOMEM;
2382 goto out;
2383 }
2384
Anthony Liguori35149e22008-04-02 14:46:56 -05002385 bad_pfn = page_to_pfn(bad_page);
2386
Huang Yingbf998152010-05-31 14:28:19 +08002387 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2388
2389 if (hwpoison_page == NULL) {
2390 r = -ENOMEM;
2391 goto out_free_0;
2392 }
2393
2394 hwpoison_pfn = page_to_pfn(hwpoison_page);
2395
Gleb Natapovedba23e2010-07-07 20:16:45 +03002396 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2397
2398 if (fault_page == NULL) {
2399 r = -ENOMEM;
2400 goto out_free_0;
2401 }
2402
2403 fault_pfn = page_to_pfn(fault_page);
2404
Avi Kivity8437a612009-06-06 14:52:35 -07002405 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302406 r = -ENOMEM;
2407 goto out_free_0;
2408 }
2409
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002410 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302412 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413
Yang, Sheng002c7f72007-07-31 14:23:01 +03002414 for_each_online_cpu(cpu) {
2415 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002416 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002417 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002418 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002419 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002420 }
2421
Avi Kivity774c47f2007-02-12 00:54:47 -08002422 r = register_cpu_notifier(&kvm_cpu_notifier);
2423 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002424 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 register_reboot_notifier(&kvm_reboot_notifier);
2426
Avi Kivity59ae6c62007-02-12 00:54:48 -08002427 r = sysdev_class_register(&kvm_sysdev_class);
2428 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002429 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002430
2431 r = sysdev_register(&kvm_sysdev);
2432 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002433 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002434
Rusty Russellc16f8622007-07-30 21:12:19 +10002435 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002436 if (!vcpu_align)
2437 vcpu_align = __alignof__(struct kvm_vcpu);
2438 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002439 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002440 if (!kvm_vcpu_cache) {
2441 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002442 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002443 }
2444
Gleb Natapovaf585b92010-10-14 11:22:46 +02002445 r = kvm_async_pf_init();
2446 if (r)
2447 goto out_free;
2448
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002450 kvm_vm_fops.owner = module;
2451 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452
2453 r = misc_register(&kvm_dev);
2454 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002455 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002456 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457 }
2458
Avi Kivity15ad7142007-07-11 18:17:21 +03002459 kvm_preempt_ops.sched_in = kvm_sched_in;
2460 kvm_preempt_ops.sched_out = kvm_sched_out;
2461
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002462 kvm_init_debug();
2463
Avi Kivityc7addb92007-09-16 18:58:32 +02002464 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465
Gleb Natapovaf585b92010-10-14 11:22:46 +02002466out_unreg:
2467 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002469 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002470out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002471 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002472out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002473 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002474out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002476 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002477out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002478out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002479 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302480out_free_0a:
2481 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002482out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002483 if (fault_page)
2484 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002485 if (hwpoison_page)
2486 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002487 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002488out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002489 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002490out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 return r;
2492}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002493EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002495void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002497 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002499 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002500 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002501 sysdev_unregister(&kvm_sysdev);
2502 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002504 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002505 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002506 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002507 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302508 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002509 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002510 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002512EXPORT_SYMBOL_GPL(kvm_exit);