blob: 85ab7db0d366091575039041ce735760e48e16b6 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Hollis Blancharde2174022007-12-03 15:30:24 -060019#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/percpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020046#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020047#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010048#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040052#include <asm/io.h>
53#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020054#include <asm/pgtable.h>
Alexander Grafc8240bd2009-10-30 05:47:26 +000055#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Laurent Vivier5f94c172008-05-30 16:05:54 +020057#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020058#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020059
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060#define CREATE_TRACE_POINTS
61#include <trace/events/kvm.h>
62
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Marcelo Tosattifa40a822009-06-04 15:08:24 -030066/*
67 * Ordering of locks:
68 *
Sheng Yangfae3a352009-12-15 10:28:07 +080069 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030070 */
71
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072DEFINE_SPINLOCK(kvm_lock);
73LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080074
Rusty Russell7f59f492008-12-07 21:25:45 +103075static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020076static int kvm_usage_count = 0;
77static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030078
Rusty Russellc16f8622007-07-30 21:12:19 +100079struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030081
Avi Kivity15ad7142007-07-11 18:17:21 +030082static __read_mostly struct preempt_ops kvm_preempt_ops;
83
Hollis Blanchard76f7c872008-04-15 16:05:42 -050084struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivitybccf2152007-02-21 18:04:26 +020086static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020088static int hardware_enable_all(void);
89static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020090
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020091static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
92
Avi Kivityb7c41452010-12-02 17:52:50 +020093bool kvm_rebooting;
94EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +030095
Marcelo Tosatti54dee992009-06-11 12:07:44 -030096static bool largepages_enabled = true;
97
Gleb Natapovfa7bff82010-07-07 20:16:44 +030098static struct page *hwpoison_page;
99static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +0800100
Gleb Natapovedba23e2010-07-07 20:16:45 +0300101static struct page *fault_page;
102static pfn_t fault_pfn;
103
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800104inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300105{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100106 if (pfn_valid(pfn)) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800107 struct page *head;
108 struct page *tail = pfn_to_page(pfn);
109 head = compound_head(tail);
110 if (head != tail) {
111 smp_rmb();
112 /*
113 * head may be a dangling pointer.
114 * __split_huge_page_refcount clears PageTail
115 * before overwriting first_page, so if
116 * PageTail is still there it means the head
117 * pointer isn't dangling.
118 */
119 if (PageTail(tail)) {
120 /*
121 * the "head" is not a dangling
122 * pointer but the hugepage may have
123 * been splitted from under us (and we
124 * may not hold a reference count on
125 * the head page so it can be reused
126 * before we run PageReferenced), so
127 * we've to recheck PageTail before
128 * returning what we just read.
129 */
130 int reserved = PageReserved(head);
131 smp_rmb();
132 if (PageTail(tail))
133 return reserved;
134 }
135 }
136 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100137 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300138
139 return true;
140}
141
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142/*
143 * Switches to specified vcpu, until a matching vcpu_put()
144 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200145void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146{
Avi Kivity15ad7142007-07-11 18:17:21 +0300147 int cpu;
148
Avi Kivitybccf2152007-02-21 18:04:26 +0200149 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300150 cpu = get_cpu();
151 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200152 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300153 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200154}
155
Carsten Otte313a3dc2007-10-11 19:16:52 +0200156void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800157{
Avi Kivity15ad7142007-07-11 18:17:21 +0300158 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200159 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300160 preempt_notifier_unregister(&vcpu->preempt_notifier);
161 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 mutex_unlock(&vcpu->mutex);
163}
164
Avi Kivityd9e368d2007-06-07 19:18:30 +0300165static void ack_flush(void *_completed)
166{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300167}
168
Rusty Russell49846892008-12-08 20:26:24 +1030169static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300170{
Avi Kivity597a5f52008-07-20 14:24:22 +0300171 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030172 cpumask_var_t cpus;
173 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300174 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300175
Li Zefan79f55992009-06-15 14:58:26 +0800176 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030177
Avi Kivity70e335e2010-02-18 11:25:22 +0200178 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200179 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300180 kvm_for_each_vcpu(i, vcpu, kvm) {
Avi Kivitya8eeb042010-05-10 12:34:53 +0300181 if (kvm_make_check_request(req, vcpu))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300182 continue;
183 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030184 if (cpus != NULL && cpu != -1 && cpu != me)
185 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300186 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030187 if (unlikely(cpus == NULL))
188 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
189 else if (!cpumask_empty(cpus))
190 smp_call_function_many(cpus, ack_flush, NULL, 1);
191 else
192 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200193 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030194 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030195 return called;
196}
197
198void kvm_flush_remote_tlbs(struct kvm *kvm)
199{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800200 int dirty_count = kvm->tlbs_dirty;
201
202 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030203 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
204 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800205 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300206}
207
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500208void kvm_reload_remote_mmus(struct kvm *kvm)
209{
Rusty Russell49846892008-12-08 20:26:24 +1030210 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500211}
212
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000213int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
214{
215 struct page *page;
216 int r;
217
218 mutex_init(&vcpu->mutex);
219 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000220 vcpu->kvm = kvm;
221 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300222 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200223 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000224
225 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
226 if (!page) {
227 r = -ENOMEM;
228 goto fail;
229 }
230 vcpu->run = page_address(page);
231
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800232 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000233 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800234 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000235 return 0;
236
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000237fail_free_run:
238 free_page((unsigned long)vcpu->run);
239fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000240 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000241}
242EXPORT_SYMBOL_GPL(kvm_vcpu_init);
243
244void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
245{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800246 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000247 free_page((unsigned long)vcpu->run);
248}
249EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
250
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200251#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
252static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
253{
254 return container_of(mn, struct kvm, mmu_notifier);
255}
256
257static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
258 struct mm_struct *mm,
259 unsigned long address)
260{
261 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200262 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200263
264 /*
265 * When ->invalidate_page runs, the linux pte has been zapped
266 * already but the page is still allocated until
267 * ->invalidate_page returns. So if we increase the sequence
268 * here the kvm page fault will notice if the spte can't be
269 * established because the page is going to be freed. If
270 * instead the kvm page fault establishes the spte before
271 * ->invalidate_page runs, kvm_unmap_hva will release it
272 * before returning.
273 *
274 * The sequence increase only need to be seen at spin_unlock
275 * time, and not at spin_lock time.
276 *
277 * Increasing the sequence after the spin_unlock would be
278 * unsafe because the kvm page fault could then establish the
279 * pte after kvm_unmap_hva returned, without noticing the page
280 * is going to be freed.
281 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200282 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200283 spin_lock(&kvm->mmu_lock);
284 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800285 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200286 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200287 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200288
289 /* we've to flush the tlb before the pages can be freed */
290 if (need_tlb_flush)
291 kvm_flush_remote_tlbs(kvm);
292
293}
294
Izik Eidus3da0dd42009-09-23 21:47:18 +0300295static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
296 struct mm_struct *mm,
297 unsigned long address,
298 pte_t pte)
299{
300 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200301 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300302
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200303 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300304 spin_lock(&kvm->mmu_lock);
305 kvm->mmu_notifier_seq++;
306 kvm_set_spte_hva(kvm, address, pte);
307 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200308 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300309}
310
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200311static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
312 struct mm_struct *mm,
313 unsigned long start,
314 unsigned long end)
315{
316 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200317 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200318
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200319 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200320 spin_lock(&kvm->mmu_lock);
321 /*
322 * The count increase must become visible at unlock time as no
323 * spte can be established without taking the mmu_lock and
324 * count is also read inside the mmu_lock critical section.
325 */
326 kvm->mmu_notifier_count++;
327 for (; start < end; start += PAGE_SIZE)
328 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800329 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200330 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200331 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200332
333 /* we've to flush the tlb before the pages can be freed */
334 if (need_tlb_flush)
335 kvm_flush_remote_tlbs(kvm);
336}
337
338static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
339 struct mm_struct *mm,
340 unsigned long start,
341 unsigned long end)
342{
343 struct kvm *kvm = mmu_notifier_to_kvm(mn);
344
345 spin_lock(&kvm->mmu_lock);
346 /*
347 * This sequence increase will notify the kvm page fault that
348 * the page that is going to be mapped in the spte could have
349 * been freed.
350 */
351 kvm->mmu_notifier_seq++;
352 /*
353 * The above sequence increase must be visible before the
354 * below count decrease but both values are read by the kvm
355 * page fault under mmu_lock spinlock so we don't need to add
356 * a smb_wmb() here in between the two.
357 */
358 kvm->mmu_notifier_count--;
359 spin_unlock(&kvm->mmu_lock);
360
361 BUG_ON(kvm->mmu_notifier_count < 0);
362}
363
364static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
365 struct mm_struct *mm,
366 unsigned long address)
367{
368 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200369 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200370
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200371 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200372 spin_lock(&kvm->mmu_lock);
373 young = kvm_age_hva(kvm, address);
374 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200375 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200376
377 if (young)
378 kvm_flush_remote_tlbs(kvm);
379
380 return young;
381}
382
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100383static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
384 struct mm_struct *mm)
385{
386 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800387 int idx;
388
389 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100390 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800391 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100392}
393
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200394static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
395 .invalidate_page = kvm_mmu_notifier_invalidate_page,
396 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
397 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
398 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300399 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100400 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200401};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200402
403static int kvm_init_mmu_notifier(struct kvm *kvm)
404{
405 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
406 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
407}
408
409#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
410
411static int kvm_init_mmu_notifier(struct kvm *kvm)
412{
413 return 0;
414}
415
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200416#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
417
Avi Kivityf17abe92007-02-21 19:28:04 +0200418static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100420 int r, i;
421 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100423 if (!kvm)
424 return ERR_PTR(-ENOMEM);
425
426 r = kvm_arch_init_vm(kvm);
427 if (r)
428 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200429
430 r = hardware_enable_all();
431 if (r)
432 goto out_err_nodisable;
433
Avi Kivity75858a82009-01-04 17:10:50 +0200434#ifdef CONFIG_HAVE_KVM_IRQCHIP
435 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300436 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200437#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200439 r = -ENOMEM;
440 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
441 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100442 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200443 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100444 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200445 for (i = 0; i < KVM_NR_BUSES; i++) {
446 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
447 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100448 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200449 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200450 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200451
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200452 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100453 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200454 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200455
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200456 kvm->mm = current->mm;
457 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500458 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200459 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400460 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800461 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300462 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200463 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300464 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000465 spin_lock(&kvm_lock);
466 list_add(&kvm->vm_list, &vm_list);
467 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100468
Avi Kivityf17abe92007-02-21 19:28:04 +0200469 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200470
471out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100472 cleanup_srcu_struct(&kvm->srcu);
473out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200474 hardware_disable_all();
475out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200476 for (i = 0; i < KVM_NR_BUSES; i++)
477 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200478 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100479 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200480 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200481}
482
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900483static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
484{
485 if (!memslot->dirty_bitmap)
486 return;
487
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900488 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
489 vfree(memslot->dirty_bitmap_head);
490 else
491 kfree(memslot->dirty_bitmap_head);
492
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900493 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900494 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900495}
496
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497/*
498 * Free any memory in @free but not in @dont.
499 */
500static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
501 struct kvm_memory_slot *dont)
502{
Joerg Roedelec04b262009-06-19 15:16:23 +0200503 int i;
504
Izik Eidus290fc382007-09-27 14:11:22 +0200505 if (!dont || free->rmap != dont->rmap)
506 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507
508 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900509 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510
Joerg Roedelec04b262009-06-19 15:16:23 +0200511
512 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
513 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
514 vfree(free->lpage_info[i]);
515 free->lpage_info[i] = NULL;
516 }
517 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300518
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500520 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521}
522
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800523void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524{
525 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200526 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200528 for (i = 0; i < slots->nmemslots; ++i)
529 kvm_free_physmem_slot(&slots->memslots[i], NULL);
530
531 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532}
533
Avi Kivityf17abe92007-02-21 19:28:04 +0200534static void kvm_destroy_vm(struct kvm *kvm)
535{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200536 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200537 struct mm_struct *mm = kvm->mm;
538
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800539 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800540 spin_lock(&kvm_lock);
541 list_del(&kvm->vm_list);
542 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200543 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200544 for (i = 0; i < KVM_NR_BUSES; i++)
545 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200546 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200547#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
548 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200549#else
550 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200551#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800552 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100553 kvm_free_physmem(kvm);
554 cleanup_srcu_struct(&kvm->srcu);
555 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200556 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200557 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200558}
559
Izik Eidusd39f13b2008-03-30 16:01:25 +0300560void kvm_get_kvm(struct kvm *kvm)
561{
562 atomic_inc(&kvm->users_count);
563}
564EXPORT_SYMBOL_GPL(kvm_get_kvm);
565
566void kvm_put_kvm(struct kvm *kvm)
567{
568 if (atomic_dec_and_test(&kvm->users_count))
569 kvm_destroy_vm(kvm);
570}
571EXPORT_SYMBOL_GPL(kvm_put_kvm);
572
573
Avi Kivityf17abe92007-02-21 19:28:04 +0200574static int kvm_vm_release(struct inode *inode, struct file *filp)
575{
576 struct kvm *kvm = filp->private_data;
577
Gregory Haskins721eecb2009-05-20 10:30:49 -0400578 kvm_irqfd_release(kvm);
579
Izik Eidusd39f13b2008-03-30 16:01:25 +0300580 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 return 0;
582}
583
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900584/*
585 * Allocation size is twice as large as the actual dirty bitmap size.
586 * This makes it possible to do double buffering: see x86's
587 * kvm_vm_ioctl_get_dirty_log().
588 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900589static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
590{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900591 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900592
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900593 if (dirty_bytes > PAGE_SIZE)
594 memslot->dirty_bitmap = vzalloc(dirty_bytes);
595 else
596 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
597
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900598 if (!memslot->dirty_bitmap)
599 return -ENOMEM;
600
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900601 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900602 return 0;
603}
604
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606 * Allocate some memory and give it an address in the guest physical address
607 * space.
608 *
609 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800610 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500611 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800613int __kvm_set_memory_region(struct kvm *kvm,
614 struct kvm_userspace_memory_region *mem,
615 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200617 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200619 unsigned long npages;
620 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621 struct kvm_memory_slot *memslot;
622 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200623 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624
625 r = -EINVAL;
626 /* General sanity checks */
627 if (mem->memory_size & (PAGE_SIZE - 1))
628 goto out;
629 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
630 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800631 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600632 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200633 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 goto out;
635 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
636 goto out;
637
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200638 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
640 npages = mem->memory_size >> PAGE_SHIFT;
641
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900642 r = -EINVAL;
643 if (npages > KVM_MEM_MAX_NR_PAGES)
644 goto out;
645
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 if (!npages)
647 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
648
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 new = old = *memslot;
650
Avi Kivitye36d96f2010-06-21 10:56:36 +0300651 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 new.base_gfn = base_gfn;
653 new.npages = npages;
654 new.flags = mem->flags;
655
656 /* Disallow changing a memory slot's size. */
657 r = -EINVAL;
658 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800659 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660
661 /* Check for overlaps */
662 r = -EEXIST;
663 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200664 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200666 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 continue;
668 if (!((base_gfn + npages <= s->base_gfn) ||
669 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800670 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 /* Free page dirty bitmap if unneeded */
674 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000675 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676
677 r = -ENOMEM;
678
679 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200680#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500681 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900682 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200683
684 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800685 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200686
Izik Eidus80b14b52007-10-25 11:54:04 +0200687 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200688 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200690 if (!npages)
691 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300692
Joerg Roedelec04b262009-06-19 15:16:23 +0200693 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200694 unsigned long ugfn;
695 unsigned long j;
696 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200697 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300698
Joerg Roedelec04b262009-06-19 15:16:23 +0200699 /* Avoid unused variable warning if no large pages */
700 (void)level;
701
702 if (new.lpage_info[i])
703 continue;
704
Joerg Roedel82855412010-07-01 16:00:11 +0200705 lpages = 1 + ((base_gfn + npages - 1)
706 >> KVM_HPAGE_GFN_SHIFT(level));
707 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200708
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900709 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200710
711 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300712 goto out_free;
713
Joerg Roedel82855412010-07-01 16:00:11 +0200714 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200715 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200716 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200717 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300718 ugfn = new.userspace_addr >> PAGE_SHIFT;
719 /*
720 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300721 * other, or if explicitly asked to, disable large page
722 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300723 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200724 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300725 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200726 for (j = 0; j < lpages; ++j)
727 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300728 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
Joerg Roedelec04b262009-06-19 15:16:23 +0200730skip_lpage:
731
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 /* Allocate page dirty bitmap if needed */
733 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900734 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800735 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200736 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300737 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200738 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200740#else /* not defined CONFIG_S390 */
741 new.user_alloc = user_alloc;
742 if (user_alloc)
743 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200744#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200746 if (!npages) {
747 r = -ENOMEM;
748 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
749 if (!slots)
750 goto out_free;
751 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
752 if (mem->slot >= slots->nmemslots)
753 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200754 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200755 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
756
757 old_memslots = kvm->memslots;
758 rcu_assign_pointer(kvm->memslots, slots);
759 synchronize_srcu_expedited(&kvm->srcu);
760 /* From this point no new shadow pages pointing to a deleted
761 * memslot will be created.
762 *
763 * validation of sp->gfn happens in:
764 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
765 * - kvm_is_visible_gfn (mmu_check_roots)
766 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300767 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200768 kfree(old_memslots);
769 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300770
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200771 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
772 if (r)
773 goto out_free;
774
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200775 /* map the pages in iommu page table */
776 if (npages) {
777 r = kvm_iommu_map_pages(kvm, &new);
778 if (r)
779 goto out_free;
780 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200781
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200782 r = -ENOMEM;
783 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
784 if (!slots)
785 goto out_free;
786 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
787 if (mem->slot >= slots->nmemslots)
788 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200789 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200790
791 /* actual memory is freed via old in kvm_free_physmem_slot below */
792 if (!npages) {
793 new.rmap = NULL;
794 new.dirty_bitmap = NULL;
795 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
796 new.lpage_info[i] = NULL;
797 }
798
799 slots->memslots[mem->slot] = new;
800 old_memslots = kvm->memslots;
801 rcu_assign_pointer(kvm->memslots, slots);
802 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200804 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800805
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200806 kvm_free_physmem_slot(&old, &new);
807 kfree(old_memslots);
808
809 if (flush_shadow)
810 kvm_arch_flush_shadow(kvm);
811
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 return 0;
813
Sheng Yangf78e0e22007-10-29 09:40:42 +0800814out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 kvm_free_physmem_slot(&new, &old);
816out:
817 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200818
819}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800820EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
821
822int kvm_set_memory_region(struct kvm *kvm,
823 struct kvm_userspace_memory_region *mem,
824 int user_alloc)
825{
826 int r;
827
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200828 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800829 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200830 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800831 return r;
832}
Izik Eidus210c7c42007-10-24 23:52:57 +0200833EXPORT_SYMBOL_GPL(kvm_set_memory_region);
834
Carsten Otte1fe779f2007-10-29 16:08:35 +0100835int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
836 struct
837 kvm_userspace_memory_region *mem,
838 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200839{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200840 if (mem->slot >= KVM_MEMORY_SLOTS)
841 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200842 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843}
844
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800845int kvm_get_dirty_log(struct kvm *kvm,
846 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847{
848 struct kvm_memory_slot *memslot;
849 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900850 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 unsigned long any = 0;
852
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 r = -EINVAL;
854 if (log->slot >= KVM_MEMORY_SLOTS)
855 goto out;
856
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200857 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 r = -ENOENT;
859 if (!memslot->dirty_bitmap)
860 goto out;
861
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900862 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
Uri Lublincd1a4a92007-02-22 16:43:09 +0200864 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 any = memslot->dirty_bitmap[i];
866
867 r = -EFAULT;
868 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
869 goto out;
870
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800871 if (any)
872 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873
874 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 return r;
877}
878
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300879void kvm_disable_largepages(void)
880{
881 largepages_enabled = false;
882}
883EXPORT_SYMBOL_GPL(kvm_disable_largepages);
884
Izik Eiduscea7bb22007-10-17 19:17:48 +0200885int is_error_page(struct page *page)
886{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300887 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200888}
889EXPORT_SYMBOL_GPL(is_error_page);
890
Anthony Liguori35149e22008-04-02 14:46:56 -0500891int is_error_pfn(pfn_t pfn)
892{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300893 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500894}
895EXPORT_SYMBOL_GPL(is_error_pfn);
896
Huang Yingbf998152010-05-31 14:28:19 +0800897int is_hwpoison_pfn(pfn_t pfn)
898{
899 return pfn == hwpoison_pfn;
900}
901EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
902
Gleb Natapovedba23e2010-07-07 20:16:45 +0300903int is_fault_pfn(pfn_t pfn)
904{
905 return pfn == fault_pfn;
906}
907EXPORT_SYMBOL_GPL(is_fault_pfn);
908
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200909static inline unsigned long bad_hva(void)
910{
911 return PAGE_OFFSET;
912}
913
914int kvm_is_error_hva(unsigned long addr)
915{
916 return addr == bad_hva();
917}
918EXPORT_SYMBOL_GPL(kvm_is_error_hva);
919
Gleb Natapov49c77542010-10-18 15:22:23 +0200920static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
921 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922{
923 int i;
924
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200925 for (i = 0; i < slots->nmemslots; ++i) {
926 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
928 if (gfn >= memslot->base_gfn
929 && gfn < memslot->base_gfn + memslot->npages)
930 return memslot;
931 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000932 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933}
Gleb Natapov49c77542010-10-18 15:22:23 +0200934
935struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
936{
937 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
938}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300939EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Izik Eiduse0d62c72007-10-24 23:57:46 +0200941int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
942{
943 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800944 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200945
Izik Eiduse0d62c72007-10-24 23:57:46 +0200946 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200947 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200948
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200949 if (memslot->flags & KVM_MEMSLOT_INVALID)
950 continue;
951
Izik Eiduse0d62c72007-10-24 23:57:46 +0200952 if (gfn >= memslot->base_gfn
953 && gfn < memslot->base_gfn + memslot->npages)
954 return 1;
955 }
956 return 0;
957}
958EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
959
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100960unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
961{
962 struct vm_area_struct *vma;
963 unsigned long addr, size;
964
965 size = PAGE_SIZE;
966
967 addr = gfn_to_hva(kvm, gfn);
968 if (kvm_is_error_hva(addr))
969 return PAGE_SIZE;
970
971 down_read(&current->mm->mmap_sem);
972 vma = find_vma(current->mm, addr);
973 if (!vma)
974 goto out;
975
976 size = vma_kernel_pagesize(vma);
977
978out:
979 up_read(&current->mm->mmap_sem);
980
981 return size;
982}
983
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200984int memslot_id(struct kvm *kvm, gfn_t gfn)
985{
986 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800987 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200988 struct kvm_memory_slot *memslot = NULL;
989
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200990 for (i = 0; i < slots->nmemslots; ++i) {
991 memslot = &slots->memslots[i];
992
993 if (gfn >= memslot->base_gfn
994 && gfn < memslot->base_gfn + memslot->npages)
995 break;
996 }
997
998 return memslot - slots->memslots;
999}
1000
Gleb Natapov49c77542010-10-18 15:22:23 +02001001static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001002 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001003{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001004 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001005 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001006
1007 if (nr_pages)
1008 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1009
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001010 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001011}
Xiao Guangrong48987782010-08-22 19:11:43 +08001012
1013unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1014{
Gleb Natapov49c77542010-10-18 15:22:23 +02001015 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001016}
Sheng Yang0d150292008-04-25 21:44:50 +08001017EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001018
Gleb Natapov80300892010-10-19 18:13:41 +02001019static pfn_t get_fault_pfn(void)
1020{
1021 get_page(fault_page);
1022 return fault_pfn;
1023}
1024
Gleb Natapovaf585b92010-10-14 11:22:46 +02001025static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001026 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001027{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001028 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001029 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001030 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001031
Gleb Natapovaf585b92010-10-14 11:22:46 +02001032 /* we can do it either atomically or asynchronously, not both */
1033 BUG_ON(atomic && async);
1034
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001035 BUG_ON(!write_fault && !writable);
1036
1037 if (writable)
1038 *writable = true;
1039
Gleb Natapovaf585b92010-10-14 11:22:46 +02001040 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001041 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001042
1043 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001044 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001045
1046 if (writable)
1047 *writable = write_fault;
1048
1049 npages = get_user_pages_fast(addr, 1, write_fault, page);
1050
1051 /* map read fault as writable if possible */
1052 if (unlikely(!write_fault) && npages == 1) {
1053 struct page *wpage[1];
1054
1055 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1056 if (npages == 1) {
1057 *writable = true;
1058 put_page(page[0]);
1059 page[0] = wpage[0];
1060 }
1061 npages = 1;
1062 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001063 }
Izik Eidus539cb662007-11-11 22:05:04 +02001064
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001065 if (unlikely(npages != 1)) {
1066 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001067
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001068 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001069 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001070
Huang Yingbbeb3402010-06-22 14:23:11 +08001071 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001072 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001073 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001074 get_page(hwpoison_page);
1075 return page_to_pfn(hwpoison_page);
1076 }
1077
Gleb Natapov80300892010-10-19 18:13:41 +02001078 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001079
Gleb Natapov80300892010-10-19 18:13:41 +02001080 if (vma == NULL)
1081 pfn = get_fault_pfn();
1082 else if ((vma->vm_flags & VM_PFNMAP)) {
1083 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1084 vma->vm_pgoff;
1085 BUG_ON(!kvm_is_mmio_pfn(pfn));
1086 } else {
1087 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001088 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001089 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001090 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001091 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001092 } else
1093 pfn = page_to_pfn(page[0]);
1094
1095 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001096}
1097
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001098pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1099{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001100 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001101}
1102EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1103
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001104static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1105 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001106{
1107 unsigned long addr;
1108
Gleb Natapovaf585b92010-10-14 11:22:46 +02001109 if (async)
1110 *async = false;
1111
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001112 addr = gfn_to_hva(kvm, gfn);
1113 if (kvm_is_error_hva(addr)) {
1114 get_page(bad_page);
1115 return page_to_pfn(bad_page);
1116 }
1117
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001118 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001119}
1120
1121pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1122{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001123 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001124}
1125EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1126
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001127pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1128 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001129{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001130 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001131}
1132EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1133
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001134pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1135{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001136 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001137}
Anthony Liguori35149e22008-04-02 14:46:56 -05001138EXPORT_SYMBOL_GPL(gfn_to_pfn);
1139
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001140pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1141 bool *writable)
1142{
1143 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1144}
1145EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1146
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001147pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1148 struct kvm_memory_slot *slot, gfn_t gfn)
1149{
1150 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001151 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001152}
1153
Xiao Guangrong48987782010-08-22 19:11:43 +08001154int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1155 int nr_pages)
1156{
1157 unsigned long addr;
1158 gfn_t entry;
1159
Gleb Natapov49c77542010-10-18 15:22:23 +02001160 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001161 if (kvm_is_error_hva(addr))
1162 return -1;
1163
1164 if (entry < nr_pages)
1165 return 0;
1166
1167 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1168}
1169EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1170
Anthony Liguori35149e22008-04-02 14:46:56 -05001171struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1172{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001173 pfn_t pfn;
1174
1175 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001176 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001177 return pfn_to_page(pfn);
1178
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001179 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001180
1181 get_page(bad_page);
1182 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001183}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001184
Avi Kivity954bbbc22007-03-30 14:02:32 +03001185EXPORT_SYMBOL_GPL(gfn_to_page);
1186
Izik Eidusb4231d62007-11-20 11:49:33 +02001187void kvm_release_page_clean(struct page *page)
1188{
Anthony Liguori35149e22008-04-02 14:46:56 -05001189 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001190}
1191EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1192
Anthony Liguori35149e22008-04-02 14:46:56 -05001193void kvm_release_pfn_clean(pfn_t pfn)
1194{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001195 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001196 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001197}
1198EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1199
Izik Eidusb4231d62007-11-20 11:49:33 +02001200void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001201{
Anthony Liguori35149e22008-04-02 14:46:56 -05001202 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001203}
Izik Eidusb4231d62007-11-20 11:49:33 +02001204EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001205
Anthony Liguori35149e22008-04-02 14:46:56 -05001206void kvm_release_pfn_dirty(pfn_t pfn)
1207{
1208 kvm_set_pfn_dirty(pfn);
1209 kvm_release_pfn_clean(pfn);
1210}
1211EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1212
1213void kvm_set_page_dirty(struct page *page)
1214{
1215 kvm_set_pfn_dirty(page_to_pfn(page));
1216}
1217EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1218
1219void kvm_set_pfn_dirty(pfn_t pfn)
1220{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001221 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001222 struct page *page = pfn_to_page(pfn);
1223 if (!PageReserved(page))
1224 SetPageDirty(page);
1225 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001226}
1227EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1228
1229void kvm_set_pfn_accessed(pfn_t pfn)
1230{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001231 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001232 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001233}
1234EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1235
1236void kvm_get_pfn(pfn_t pfn)
1237{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001238 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001239 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001240}
1241EXPORT_SYMBOL_GPL(kvm_get_pfn);
1242
Izik Eidus195aefd2007-10-01 22:14:18 +02001243static int next_segment(unsigned long len, int offset)
1244{
1245 if (len > PAGE_SIZE - offset)
1246 return PAGE_SIZE - offset;
1247 else
1248 return len;
1249}
1250
1251int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1252 int len)
1253{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001254 int r;
1255 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001256
Izik Eiduse0506bc2007-11-11 22:10:22 +02001257 addr = gfn_to_hva(kvm, gfn);
1258 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001259 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001260 r = copy_from_user(data, (void __user *)addr + offset, len);
1261 if (r)
1262 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001263 return 0;
1264}
1265EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1266
1267int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1268{
1269 gfn_t gfn = gpa >> PAGE_SHIFT;
1270 int seg;
1271 int offset = offset_in_page(gpa);
1272 int ret;
1273
1274 while ((seg = next_segment(len, offset)) != 0) {
1275 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1276 if (ret < 0)
1277 return ret;
1278 offset = 0;
1279 len -= seg;
1280 data += seg;
1281 ++gfn;
1282 }
1283 return 0;
1284}
1285EXPORT_SYMBOL_GPL(kvm_read_guest);
1286
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001287int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1288 unsigned long len)
1289{
1290 int r;
1291 unsigned long addr;
1292 gfn_t gfn = gpa >> PAGE_SHIFT;
1293 int offset = offset_in_page(gpa);
1294
1295 addr = gfn_to_hva(kvm, gfn);
1296 if (kvm_is_error_hva(addr))
1297 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001298 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001299 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001300 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001301 if (r)
1302 return -EFAULT;
1303 return 0;
1304}
1305EXPORT_SYMBOL(kvm_read_guest_atomic);
1306
Izik Eidus195aefd2007-10-01 22:14:18 +02001307int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1308 int offset, int len)
1309{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001310 int r;
1311 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001312
Izik Eiduse0506bc2007-11-11 22:10:22 +02001313 addr = gfn_to_hva(kvm, gfn);
1314 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001315 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001316 r = copy_to_user((void __user *)addr + offset, data, len);
1317 if (r)
1318 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001319 mark_page_dirty(kvm, gfn);
1320 return 0;
1321}
1322EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1323
1324int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1325 unsigned long len)
1326{
1327 gfn_t gfn = gpa >> PAGE_SHIFT;
1328 int seg;
1329 int offset = offset_in_page(gpa);
1330 int ret;
1331
1332 while ((seg = next_segment(len, offset)) != 0) {
1333 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1334 if (ret < 0)
1335 return ret;
1336 offset = 0;
1337 len -= seg;
1338 data += seg;
1339 ++gfn;
1340 }
1341 return 0;
1342}
1343
Gleb Natapov49c77542010-10-18 15:22:23 +02001344int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1345 gpa_t gpa)
1346{
1347 struct kvm_memslots *slots = kvm_memslots(kvm);
1348 int offset = offset_in_page(gpa);
1349 gfn_t gfn = gpa >> PAGE_SHIFT;
1350
1351 ghc->gpa = gpa;
1352 ghc->generation = slots->generation;
1353 ghc->memslot = __gfn_to_memslot(slots, gfn);
1354 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1355 if (!kvm_is_error_hva(ghc->hva))
1356 ghc->hva += offset;
1357 else
1358 return -EFAULT;
1359
1360 return 0;
1361}
1362EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1363
1364int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1365 void *data, unsigned long len)
1366{
1367 struct kvm_memslots *slots = kvm_memslots(kvm);
1368 int r;
1369
1370 if (slots->generation != ghc->generation)
1371 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1372
1373 if (kvm_is_error_hva(ghc->hva))
1374 return -EFAULT;
1375
1376 r = copy_to_user((void __user *)ghc->hva, data, len);
1377 if (r)
1378 return -EFAULT;
1379 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1380
1381 return 0;
1382}
1383EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1384
Izik Eidus195aefd2007-10-01 22:14:18 +02001385int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1386{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001387 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1388 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001389}
1390EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1391
1392int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1393{
1394 gfn_t gfn = gpa >> PAGE_SHIFT;
1395 int seg;
1396 int offset = offset_in_page(gpa);
1397 int ret;
1398
1399 while ((seg = next_segment(len, offset)) != 0) {
1400 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1401 if (ret < 0)
1402 return ret;
1403 offset = 0;
1404 len -= seg;
1405 ++gfn;
1406 }
1407 return 0;
1408}
1409EXPORT_SYMBOL_GPL(kvm_clear_guest);
1410
Gleb Natapov49c77542010-10-18 15:22:23 +02001411void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1412 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001414 if (memslot && memslot->dirty_bitmap) {
1415 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001417 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 }
1419}
1420
Gleb Natapov49c77542010-10-18 15:22:23 +02001421void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1422{
1423 struct kvm_memory_slot *memslot;
1424
1425 memslot = gfn_to_memslot(kvm, gfn);
1426 mark_page_dirty_in_slot(kvm, memslot, gfn);
1427}
1428
Eddie Dongb6958ce2007-07-18 12:15:21 +03001429/*
1430 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1431 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001432void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001433{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001434 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001435
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001436 for (;;) {
1437 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001438
Gleb Natapova1b37102009-07-09 15:33:52 +03001439 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001440 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001441 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001442 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001443 if (kvm_cpu_has_pending_timer(vcpu))
1444 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001445 if (signal_pending(current))
1446 break;
1447
Eddie Dongb6958ce2007-07-18 12:15:21 +03001448 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001449 }
1450
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001451 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001452}
1453
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454void kvm_resched(struct kvm_vcpu *vcpu)
1455{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001456 if (!need_resched())
1457 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459}
1460EXPORT_SYMBOL_GPL(kvm_resched);
1461
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001462void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1463{
1464 ktime_t expires;
1465 DEFINE_WAIT(wait);
1466
1467 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1468
1469 /* Sleep for 100 us, and hope lock-holder got scheduled */
1470 expires = ktime_add_ns(ktime_get(), 100000UL);
1471 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1472
1473 finish_wait(&vcpu->wq, &wait);
1474}
1475EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1476
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001477static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001478{
1479 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001480 struct page *page;
1481
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001482 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001483 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001484#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001485 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001486 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001487#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001488#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1489 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1490 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1491#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001492 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001493 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001494 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001495 vmf->page = page;
1496 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001497}
1498
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001499static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001500 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001501};
1502
1503static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1504{
1505 vma->vm_ops = &kvm_vcpu_vm_ops;
1506 return 0;
1507}
1508
Avi Kivitybccf2152007-02-21 18:04:26 +02001509static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1510{
1511 struct kvm_vcpu *vcpu = filp->private_data;
1512
Al Viro66c0b392008-04-19 20:33:56 +01001513 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001514 return 0;
1515}
1516
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001517static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001518 .release = kvm_vcpu_release,
1519 .unlocked_ioctl = kvm_vcpu_ioctl,
1520 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001521 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001522 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001523};
1524
1525/*
1526 * Allocates an inode for the vcpu.
1527 */
1528static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1529{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001530 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001531}
1532
Avi Kivityc5ea7662007-02-20 18:41:05 +02001533/*
1534 * Creates some virtual cpus. Good luck creating more than one.
1535 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001536static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001537{
1538 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001539 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001540
Gleb Natapov73880c82009-06-09 15:56:28 +03001541 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001542 if (IS_ERR(vcpu))
1543 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001544
Avi Kivity15ad7142007-07-11 18:17:21 +03001545 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1546
Avi Kivity26e52152007-11-20 15:30:24 +02001547 r = kvm_arch_vcpu_setup(vcpu);
1548 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001549 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001550
Shaohua Li11ec2802007-07-23 14:51:37 +08001551 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001552 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1553 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001554 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001555 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001556
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001557 kvm_for_each_vcpu(r, v, kvm)
1558 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001559 r = -EEXIST;
1560 goto vcpu_destroy;
1561 }
1562
1563 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001564
1565 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001566 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001567 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001568 if (r < 0) {
1569 kvm_put_kvm(kvm);
1570 goto vcpu_destroy;
1571 }
1572
1573 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1574 smp_wmb();
1575 atomic_inc(&kvm->online_vcpus);
1576
1577#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1578 if (kvm->bsp_vcpu_id == id)
1579 kvm->bsp_vcpu = vcpu;
1580#endif
1581 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001582 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001583
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001584vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001585 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001586 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001587 return r;
1588}
1589
Avi Kivity1961d272007-03-05 19:46:05 +02001590static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1591{
1592 if (sigset) {
1593 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1594 vcpu->sigset_active = 1;
1595 vcpu->sigset = *sigset;
1596 } else
1597 vcpu->sigset_active = 0;
1598 return 0;
1599}
1600
Avi Kivitybccf2152007-02-21 18:04:26 +02001601static long kvm_vcpu_ioctl(struct file *filp,
1602 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603{
Avi Kivitybccf2152007-02-21 18:04:26 +02001604 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001605 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001606 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001607 struct kvm_fpu *fpu = NULL;
1608 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001610 if (vcpu->kvm->mm != current->mm)
1611 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001612
1613#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1614 /*
1615 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1616 * so vcpu_load() would break it.
1617 */
1618 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1619 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1620#endif
1621
1622
1623 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001625 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001626 r = -EINVAL;
1627 if (arg)
1628 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001629 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001630 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001633 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001635 r = -ENOMEM;
1636 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1637 if (!kvm_regs)
1638 goto out;
1639 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001641 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001643 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1644 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001646out_free1:
1647 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 break;
1649 }
1650 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001651 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001653 r = -ENOMEM;
1654 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1655 if (!kvm_regs)
1656 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001658 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1659 goto out_free2;
1660 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001662 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001664out_free2:
1665 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666 break;
1667 }
1668 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001669 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1670 r = -ENOMEM;
1671 if (!kvm_sregs)
1672 goto out;
1673 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 if (r)
1675 goto out;
1676 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001677 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 goto out;
1679 r = 0;
1680 break;
1681 }
1682 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001683 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1684 r = -ENOMEM;
1685 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001687 r = -EFAULT;
1688 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1689 goto out;
1690 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 if (r)
1692 goto out;
1693 r = 0;
1694 break;
1695 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001696 case KVM_GET_MP_STATE: {
1697 struct kvm_mp_state mp_state;
1698
1699 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1700 if (r)
1701 goto out;
1702 r = -EFAULT;
1703 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1704 goto out;
1705 r = 0;
1706 break;
1707 }
1708 case KVM_SET_MP_STATE: {
1709 struct kvm_mp_state mp_state;
1710
1711 r = -EFAULT;
1712 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1713 goto out;
1714 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1715 if (r)
1716 goto out;
1717 r = 0;
1718 break;
1719 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 case KVM_TRANSLATE: {
1721 struct kvm_translation tr;
1722
1723 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001724 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001726 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727 if (r)
1728 goto out;
1729 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001730 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 goto out;
1732 r = 0;
1733 break;
1734 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001735 case KVM_SET_GUEST_DEBUG: {
1736 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737
1738 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001739 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001741 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742 if (r)
1743 goto out;
1744 r = 0;
1745 break;
1746 }
Avi Kivity1961d272007-03-05 19:46:05 +02001747 case KVM_SET_SIGNAL_MASK: {
1748 struct kvm_signal_mask __user *sigmask_arg = argp;
1749 struct kvm_signal_mask kvm_sigmask;
1750 sigset_t sigset, *p;
1751
1752 p = NULL;
1753 if (argp) {
1754 r = -EFAULT;
1755 if (copy_from_user(&kvm_sigmask, argp,
1756 sizeof kvm_sigmask))
1757 goto out;
1758 r = -EINVAL;
1759 if (kvm_sigmask.len != sizeof sigset)
1760 goto out;
1761 r = -EFAULT;
1762 if (copy_from_user(&sigset, sigmask_arg->sigset,
1763 sizeof sigset))
1764 goto out;
1765 p = &sigset;
1766 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001767 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001768 break;
1769 }
Avi Kivityb8836732007-04-01 16:34:31 +03001770 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001771 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1772 r = -ENOMEM;
1773 if (!fpu)
1774 goto out;
1775 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001776 if (r)
1777 goto out;
1778 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001779 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001780 goto out;
1781 r = 0;
1782 break;
1783 }
1784 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001785 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1786 r = -ENOMEM;
1787 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001788 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001789 r = -EFAULT;
1790 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1791 goto out;
1792 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001793 if (r)
1794 goto out;
1795 r = 0;
1796 break;
1797 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001798 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001799 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001800 }
1801out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001802 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001803 kfree(fpu);
1804 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001805 return r;
1806}
1807
1808static long kvm_vm_ioctl(struct file *filp,
1809 unsigned int ioctl, unsigned long arg)
1810{
1811 struct kvm *kvm = filp->private_data;
1812 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001813 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001814
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001815 if (kvm->mm != current->mm)
1816 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001817 switch (ioctl) {
1818 case KVM_CREATE_VCPU:
1819 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1820 if (r < 0)
1821 goto out;
1822 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001823 case KVM_SET_USER_MEMORY_REGION: {
1824 struct kvm_userspace_memory_region kvm_userspace_mem;
1825
1826 r = -EFAULT;
1827 if (copy_from_user(&kvm_userspace_mem, argp,
1828 sizeof kvm_userspace_mem))
1829 goto out;
1830
1831 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832 if (r)
1833 goto out;
1834 break;
1835 }
1836 case KVM_GET_DIRTY_LOG: {
1837 struct kvm_dirty_log log;
1838
1839 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001840 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001842 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 if (r)
1844 goto out;
1845 break;
1846 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001847#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1848 case KVM_REGISTER_COALESCED_MMIO: {
1849 struct kvm_coalesced_mmio_zone zone;
1850 r = -EFAULT;
1851 if (copy_from_user(&zone, argp, sizeof zone))
1852 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001853 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1854 if (r)
1855 goto out;
1856 r = 0;
1857 break;
1858 }
1859 case KVM_UNREGISTER_COALESCED_MMIO: {
1860 struct kvm_coalesced_mmio_zone zone;
1861 r = -EFAULT;
1862 if (copy_from_user(&zone, argp, sizeof zone))
1863 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001864 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1865 if (r)
1866 goto out;
1867 r = 0;
1868 break;
1869 }
1870#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001871 case KVM_IRQFD: {
1872 struct kvm_irqfd data;
1873
1874 r = -EFAULT;
1875 if (copy_from_user(&data, argp, sizeof data))
1876 goto out;
1877 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1878 break;
1879 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001880 case KVM_IOEVENTFD: {
1881 struct kvm_ioeventfd data;
1882
1883 r = -EFAULT;
1884 if (copy_from_user(&data, argp, sizeof data))
1885 goto out;
1886 r = kvm_ioeventfd(kvm, &data);
1887 break;
1888 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001889#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1890 case KVM_SET_BOOT_CPU_ID:
1891 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001892 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001893 if (atomic_read(&kvm->online_vcpus) != 0)
1894 r = -EBUSY;
1895 else
1896 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001897 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001898 break;
1899#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001900 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001901 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001902 if (r == -ENOTTY)
1903 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001904 }
1905out:
1906 return r;
1907}
1908
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001909#ifdef CONFIG_COMPAT
1910struct compat_kvm_dirty_log {
1911 __u32 slot;
1912 __u32 padding1;
1913 union {
1914 compat_uptr_t dirty_bitmap; /* one bit per page */
1915 __u64 padding2;
1916 };
1917};
1918
1919static long kvm_vm_compat_ioctl(struct file *filp,
1920 unsigned int ioctl, unsigned long arg)
1921{
1922 struct kvm *kvm = filp->private_data;
1923 int r;
1924
1925 if (kvm->mm != current->mm)
1926 return -EIO;
1927 switch (ioctl) {
1928 case KVM_GET_DIRTY_LOG: {
1929 struct compat_kvm_dirty_log compat_log;
1930 struct kvm_dirty_log log;
1931
1932 r = -EFAULT;
1933 if (copy_from_user(&compat_log, (void __user *)arg,
1934 sizeof(compat_log)))
1935 goto out;
1936 log.slot = compat_log.slot;
1937 log.padding1 = compat_log.padding1;
1938 log.padding2 = compat_log.padding2;
1939 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1940
1941 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1942 if (r)
1943 goto out;
1944 break;
1945 }
1946 default:
1947 r = kvm_vm_ioctl(filp, ioctl, arg);
1948 }
1949
1950out:
1951 return r;
1952}
1953#endif
1954
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001955static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001956{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001957 struct page *page[1];
1958 unsigned long addr;
1959 int npages;
1960 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001961 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001962
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001963 addr = gfn_to_hva(kvm, gfn);
1964 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001965 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001966
1967 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1968 NULL);
1969 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001970 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001971
1972 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001973 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001974}
1975
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001976static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001977 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001978};
1979
1980static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1981{
1982 vma->vm_ops = &kvm_vm_vm_ops;
1983 return 0;
1984}
1985
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001986static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001987 .release = kvm_vm_release,
1988 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001989#ifdef CONFIG_COMPAT
1990 .compat_ioctl = kvm_vm_compat_ioctl,
1991#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001992 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001993 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001994};
1995
1996static int kvm_dev_ioctl_create_vm(void)
1997{
Heiko Carstensaac87632010-10-27 17:22:10 +02001998 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001999 struct kvm *kvm;
2000
Avi Kivityf17abe92007-02-21 19:28:04 +02002001 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002002 if (IS_ERR(kvm))
2003 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002004#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2005 r = kvm_coalesced_mmio_init(kvm);
2006 if (r < 0) {
2007 kvm_put_kvm(kvm);
2008 return r;
2009 }
2010#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002011 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2012 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002013 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002014
Heiko Carstensaac87632010-10-27 17:22:10 +02002015 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002016}
2017
Avi Kivity1a811b62008-12-08 18:25:27 +02002018static long kvm_dev_ioctl_check_extension_generic(long arg)
2019{
2020 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002021 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002022 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002023 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002024#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2025 case KVM_CAP_SET_BOOT_CPU_ID:
2026#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002027 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002028 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002029#ifdef CONFIG_HAVE_KVM_IRQCHIP
2030 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002031 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002032#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002033 default:
2034 break;
2035 }
2036 return kvm_dev_ioctl_check_extension(arg);
2037}
2038
Avi Kivityf17abe92007-02-21 19:28:04 +02002039static long kvm_dev_ioctl(struct file *filp,
2040 unsigned int ioctl, unsigned long arg)
2041{
Avi Kivity07c45a32007-03-07 13:05:38 +02002042 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002043
2044 switch (ioctl) {
2045 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002046 r = -EINVAL;
2047 if (arg)
2048 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002049 r = KVM_API_VERSION;
2050 break;
2051 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002052 r = -EINVAL;
2053 if (arg)
2054 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002055 r = kvm_dev_ioctl_create_vm();
2056 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002057 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002058 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002059 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002060 case KVM_GET_VCPU_MMAP_SIZE:
2061 r = -EINVAL;
2062 if (arg)
2063 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002064 r = PAGE_SIZE; /* struct kvm_run */
2065#ifdef CONFIG_X86
2066 r += PAGE_SIZE; /* pio data page */
2067#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002068#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2069 r += PAGE_SIZE; /* coalesced mmio ring page */
2070#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002071 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002072 case KVM_TRACE_ENABLE:
2073 case KVM_TRACE_PAUSE:
2074 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002075 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002076 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002078 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 }
2080out:
2081 return r;
2082}
2083
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 .unlocked_ioctl = kvm_dev_ioctl,
2086 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002087 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088};
2089
2090static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002091 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 "kvm",
2093 &kvm_chardev_ops,
2094};
2095
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002096static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002097{
2098 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002099 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002100
Rusty Russell7f59f492008-12-07 21:25:45 +10302101 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002102 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002103
Rusty Russell7f59f492008-12-07 21:25:45 +10302104 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002105
2106 r = kvm_arch_hardware_enable(NULL);
2107
2108 if (r) {
2109 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2110 atomic_inc(&hardware_enable_failed);
2111 printk(KERN_INFO "kvm: enabling virtualization on "
2112 "CPU%d failed\n", cpu);
2113 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002114}
2115
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002116static void hardware_enable(void *junk)
2117{
2118 spin_lock(&kvm_lock);
2119 hardware_enable_nolock(junk);
2120 spin_unlock(&kvm_lock);
2121}
2122
2123static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002124{
2125 int cpu = raw_smp_processor_id();
2126
Rusty Russell7f59f492008-12-07 21:25:45 +10302127 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002128 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302129 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002130 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002131}
2132
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002133static void hardware_disable(void *junk)
2134{
2135 spin_lock(&kvm_lock);
2136 hardware_disable_nolock(junk);
2137 spin_unlock(&kvm_lock);
2138}
2139
Alexander Graf10474ae2009-09-15 11:37:46 +02002140static void hardware_disable_all_nolock(void)
2141{
2142 BUG_ON(!kvm_usage_count);
2143
2144 kvm_usage_count--;
2145 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002146 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002147}
2148
2149static void hardware_disable_all(void)
2150{
2151 spin_lock(&kvm_lock);
2152 hardware_disable_all_nolock();
2153 spin_unlock(&kvm_lock);
2154}
2155
2156static int hardware_enable_all(void)
2157{
2158 int r = 0;
2159
2160 spin_lock(&kvm_lock);
2161
2162 kvm_usage_count++;
2163 if (kvm_usage_count == 1) {
2164 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002165 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002166
2167 if (atomic_read(&hardware_enable_failed)) {
2168 hardware_disable_all_nolock();
2169 r = -EBUSY;
2170 }
2171 }
2172
2173 spin_unlock(&kvm_lock);
2174
2175 return r;
2176}
2177
Avi Kivity774c47f2007-02-12 00:54:47 -08002178static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2179 void *v)
2180{
2181 int cpu = (long)v;
2182
Alexander Graf10474ae2009-09-15 11:37:46 +02002183 if (!kvm_usage_count)
2184 return NOTIFY_OK;
2185
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002186 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002187 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002188 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002189 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2190 cpu);
2191 hardware_disable(NULL);
2192 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002193 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002194 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2195 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002196 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002197 break;
2198 }
2199 return NOTIFY_OK;
2200}
2201
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002202
Avi Kivityb7c41452010-12-02 17:52:50 +02002203asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002204{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002205 /* Fault while not rebooting. We want the trace. */
2206 BUG();
2207}
Avi Kivityb7c41452010-12-02 17:52:50 +02002208EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002209
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002210static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002211 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002212{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002213 /*
2214 * Some (well, at least mine) BIOSes hang on reboot if
2215 * in vmx root mode.
2216 *
2217 * And Intel TXT required VMX off for all cpu when system shutdown.
2218 */
2219 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2220 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002221 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002222 return NOTIFY_OK;
2223}
2224
2225static struct notifier_block kvm_reboot_notifier = {
2226 .notifier_call = kvm_reboot,
2227 .priority = 0,
2228};
2229
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002230static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002231{
2232 int i;
2233
2234 for (i = 0; i < bus->dev_count; i++) {
2235 struct kvm_io_device *pos = bus->devs[i];
2236
2237 kvm_iodevice_destructor(pos);
2238 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002239 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002240}
2241
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002242/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002243int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002244 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002245{
2246 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002247 struct kvm_io_bus *bus;
2248
2249 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002250 for (i = 0; i < bus->dev_count; i++)
2251 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2252 return 0;
2253 return -EOPNOTSUPP;
2254}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002255
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002256/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002257int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2258 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002259{
2260 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002261 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002262
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002263 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002264 for (i = 0; i < bus->dev_count; i++)
2265 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2266 return 0;
2267 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002268}
2269
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002270/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002271int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2272 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002273{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002274 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002275
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002276 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002277 if (bus->dev_count > NR_IOBUS_DEVS-1)
2278 return -ENOSPC;
2279
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002280 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2281 if (!new_bus)
2282 return -ENOMEM;
2283 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2284 new_bus->devs[new_bus->dev_count++] = dev;
2285 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2286 synchronize_srcu_expedited(&kvm->srcu);
2287 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002288
2289 return 0;
2290}
2291
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002292/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002293int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2294 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002295{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002296 int i, r;
2297 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002298
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002299 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2300 if (!new_bus)
2301 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002302
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002303 bus = kvm->buses[bus_idx];
2304 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2305
2306 r = -ENOENT;
2307 for (i = 0; i < new_bus->dev_count; i++)
2308 if (new_bus->devs[i] == dev) {
2309 r = 0;
2310 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002311 break;
2312 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002313
2314 if (r) {
2315 kfree(new_bus);
2316 return r;
2317 }
2318
2319 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2320 synchronize_srcu_expedited(&kvm->srcu);
2321 kfree(bus);
2322 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002323}
2324
Avi Kivity774c47f2007-02-12 00:54:47 -08002325static struct notifier_block kvm_cpu_notifier = {
2326 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002327};
2328
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002329static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002330{
2331 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002332 struct kvm *kvm;
2333
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002334 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002335 spin_lock(&kvm_lock);
2336 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002337 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002338 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002339 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002340}
2341
2342DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2343
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002344static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002345{
2346 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002347 struct kvm *kvm;
2348 struct kvm_vcpu *vcpu;
2349 int i;
2350
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002351 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002352 spin_lock(&kvm_lock);
2353 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002354 kvm_for_each_vcpu(i, vcpu, kvm)
2355 *val += *(u32 *)((void *)vcpu + offset);
2356
Avi Kivity1165f5f2007-04-19 17:27:43 +03002357 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002358 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002359}
2360
Avi Kivityba1389b2007-11-18 16:24:12 +02002361DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2362
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002363static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002364 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2365 [KVM_STAT_VM] = &vm_stat_fops,
2366};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002367
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002368static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369{
2370 struct kvm_stats_debugfs_item *p;
2371
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002372 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002374 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002375 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002376 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377}
2378
2379static void kvm_exit_debug(void)
2380{
2381 struct kvm_stats_debugfs_item *p;
2382
2383 for (p = debugfs_entries; p->name; ++p)
2384 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002385 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Avi Kivity59ae6c62007-02-12 00:54:48 -08002388static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2389{
Alexander Graf10474ae2009-09-15 11:37:46 +02002390 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002391 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002392 return 0;
2393}
2394
2395static int kvm_resume(struct sys_device *dev)
2396{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002397 if (kvm_usage_count) {
2398 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002399 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002400 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002401 return 0;
2402}
2403
2404static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002405 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002406 .suspend = kvm_suspend,
2407 .resume = kvm_resume,
2408};
2409
2410static struct sys_device kvm_sysdev = {
2411 .id = 0,
2412 .cls = &kvm_sysdev_class,
2413};
2414
Izik Eiduscea7bb22007-10-17 19:17:48 +02002415struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002416pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417
Avi Kivity15ad7142007-07-11 18:17:21 +03002418static inline
2419struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2420{
2421 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2422}
2423
2424static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2425{
2426 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2427
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002428 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002429}
2430
2431static void kvm_sched_out(struct preempt_notifier *pn,
2432 struct task_struct *next)
2433{
2434 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2435
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002436 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002437}
2438
Avi Kivity0ee75be2010-04-28 15:39:01 +03002439int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002440 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441{
2442 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002443 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002445 r = kvm_arch_init(opaque);
2446 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002447 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002448
2449 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2450
2451 if (bad_page == NULL) {
2452 r = -ENOMEM;
2453 goto out;
2454 }
2455
Anthony Liguori35149e22008-04-02 14:46:56 -05002456 bad_pfn = page_to_pfn(bad_page);
2457
Huang Yingbf998152010-05-31 14:28:19 +08002458 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2459
2460 if (hwpoison_page == NULL) {
2461 r = -ENOMEM;
2462 goto out_free_0;
2463 }
2464
2465 hwpoison_pfn = page_to_pfn(hwpoison_page);
2466
Gleb Natapovedba23e2010-07-07 20:16:45 +03002467 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2468
2469 if (fault_page == NULL) {
2470 r = -ENOMEM;
2471 goto out_free_0;
2472 }
2473
2474 fault_pfn = page_to_pfn(fault_page);
2475
Avi Kivity8437a612009-06-06 14:52:35 -07002476 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302477 r = -ENOMEM;
2478 goto out_free_0;
2479 }
2480
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002481 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302483 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484
Yang, Sheng002c7f72007-07-31 14:23:01 +03002485 for_each_online_cpu(cpu) {
2486 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002487 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002488 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002489 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002490 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002491 }
2492
Avi Kivity774c47f2007-02-12 00:54:47 -08002493 r = register_cpu_notifier(&kvm_cpu_notifier);
2494 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002495 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 register_reboot_notifier(&kvm_reboot_notifier);
2497
Avi Kivity59ae6c62007-02-12 00:54:48 -08002498 r = sysdev_class_register(&kvm_sysdev_class);
2499 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002500 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002501
2502 r = sysdev_register(&kvm_sysdev);
2503 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002504 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002505
Rusty Russellc16f8622007-07-30 21:12:19 +10002506 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002507 if (!vcpu_align)
2508 vcpu_align = __alignof__(struct kvm_vcpu);
2509 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002510 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002511 if (!kvm_vcpu_cache) {
2512 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002513 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002514 }
2515
Gleb Natapovaf585b92010-10-14 11:22:46 +02002516 r = kvm_async_pf_init();
2517 if (r)
2518 goto out_free;
2519
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002521 kvm_vm_fops.owner = module;
2522 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523
2524 r = misc_register(&kvm_dev);
2525 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002526 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002527 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 }
2529
Avi Kivity15ad7142007-07-11 18:17:21 +03002530 kvm_preempt_ops.sched_in = kvm_sched_in;
2531 kvm_preempt_ops.sched_out = kvm_sched_out;
2532
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002533 kvm_init_debug();
2534
Avi Kivityc7addb92007-09-16 18:58:32 +02002535 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536
Gleb Natapovaf585b92010-10-14 11:22:46 +02002537out_unreg:
2538 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002540 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002541out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002542 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002543out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002544 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002545out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002547 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002548out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002549out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002550 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302551out_free_0a:
2552 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002553out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002554 if (fault_page)
2555 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002556 if (hwpoison_page)
2557 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002558 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002559out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002560 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002561out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 return r;
2563}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002564EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002566void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002568 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002570 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002571 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002572 sysdev_unregister(&kvm_sysdev);
2573 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002575 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002576 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002577 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002578 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302579 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002580 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002581 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002583EXPORT_SYMBOL_GPL(kvm_exit);