blob: 4286d4766510ee1696d8997fd21c70ff42c9838d [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
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800383static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
384 struct mm_struct *mm,
385 unsigned long address)
386{
387 struct kvm *kvm = mmu_notifier_to_kvm(mn);
388 int young, idx;
389
390 idx = srcu_read_lock(&kvm->srcu);
391 spin_lock(&kvm->mmu_lock);
392 young = kvm_test_age_hva(kvm, address);
393 spin_unlock(&kvm->mmu_lock);
394 srcu_read_unlock(&kvm->srcu, idx);
395
396 return young;
397}
398
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100399static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
400 struct mm_struct *mm)
401{
402 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800403 int idx;
404
405 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100406 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800407 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100408}
409
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200410static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
411 .invalidate_page = kvm_mmu_notifier_invalidate_page,
412 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
413 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
414 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800415 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300416 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100417 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200418};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200419
420static int kvm_init_mmu_notifier(struct kvm *kvm)
421{
422 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
423 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
424}
425
426#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
427
428static int kvm_init_mmu_notifier(struct kvm *kvm)
429{
430 return 0;
431}
432
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200433#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
434
Avi Kivityf17abe92007-02-21 19:28:04 +0200435static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100437 int r, i;
438 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100440 if (!kvm)
441 return ERR_PTR(-ENOMEM);
442
443 r = kvm_arch_init_vm(kvm);
444 if (r)
445 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200446
447 r = hardware_enable_all();
448 if (r)
449 goto out_err_nodisable;
450
Avi Kivity75858a82009-01-04 17:10:50 +0200451#ifdef CONFIG_HAVE_KVM_IRQCHIP
452 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300453 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200454#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200456 r = -ENOMEM;
457 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
458 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100459 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200460 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100461 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200462 for (i = 0; i < KVM_NR_BUSES; i++) {
463 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
464 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100465 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200466 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200467 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200468
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200469 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100470 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200471 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200472
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200473 kvm->mm = current->mm;
474 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500475 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200476 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400477 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800478 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300479 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200480 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300481 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000482 spin_lock(&kvm_lock);
483 list_add(&kvm->vm_list, &vm_list);
484 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100485
Avi Kivityf17abe92007-02-21 19:28:04 +0200486 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200487
488out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100489 cleanup_srcu_struct(&kvm->srcu);
490out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200491 hardware_disable_all();
492out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200493 for (i = 0; i < KVM_NR_BUSES; i++)
494 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200495 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100496 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200497 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200498}
499
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900500static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
501{
502 if (!memslot->dirty_bitmap)
503 return;
504
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900505 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
506 vfree(memslot->dirty_bitmap_head);
507 else
508 kfree(memslot->dirty_bitmap_head);
509
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900510 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900511 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900512}
513
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514/*
515 * Free any memory in @free but not in @dont.
516 */
517static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
518 struct kvm_memory_slot *dont)
519{
Joerg Roedelec04b262009-06-19 15:16:23 +0200520 int i;
521
Izik Eidus290fc382007-09-27 14:11:22 +0200522 if (!dont || free->rmap != dont->rmap)
523 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524
525 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900526 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527
Joerg Roedelec04b262009-06-19 15:16:23 +0200528
529 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
530 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
531 vfree(free->lpage_info[i]);
532 free->lpage_info[i] = NULL;
533 }
534 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300535
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500537 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538}
539
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800540void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541{
542 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200543 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200545 for (i = 0; i < slots->nmemslots; ++i)
546 kvm_free_physmem_slot(&slots->memslots[i], NULL);
547
548 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549}
550
Avi Kivityf17abe92007-02-21 19:28:04 +0200551static void kvm_destroy_vm(struct kvm *kvm)
552{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200553 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200554 struct mm_struct *mm = kvm->mm;
555
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800556 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800557 spin_lock(&kvm_lock);
558 list_del(&kvm->vm_list);
559 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200560 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200561 for (i = 0; i < KVM_NR_BUSES; i++)
562 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200563 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200564#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
565 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200566#else
567 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200568#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800569 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100570 kvm_free_physmem(kvm);
571 cleanup_srcu_struct(&kvm->srcu);
572 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200573 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200574 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200575}
576
Izik Eidusd39f13b2008-03-30 16:01:25 +0300577void kvm_get_kvm(struct kvm *kvm)
578{
579 atomic_inc(&kvm->users_count);
580}
581EXPORT_SYMBOL_GPL(kvm_get_kvm);
582
583void kvm_put_kvm(struct kvm *kvm)
584{
585 if (atomic_dec_and_test(&kvm->users_count))
586 kvm_destroy_vm(kvm);
587}
588EXPORT_SYMBOL_GPL(kvm_put_kvm);
589
590
Avi Kivityf17abe92007-02-21 19:28:04 +0200591static int kvm_vm_release(struct inode *inode, struct file *filp)
592{
593 struct kvm *kvm = filp->private_data;
594
Gregory Haskins721eecb2009-05-20 10:30:49 -0400595 kvm_irqfd_release(kvm);
596
Izik Eidusd39f13b2008-03-30 16:01:25 +0300597 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598 return 0;
599}
600
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900601/*
602 * Allocation size is twice as large as the actual dirty bitmap size.
603 * This makes it possible to do double buffering: see x86's
604 * kvm_vm_ioctl_get_dirty_log().
605 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900606static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
607{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900608 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900609
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900610 if (dirty_bytes > PAGE_SIZE)
611 memslot->dirty_bitmap = vzalloc(dirty_bytes);
612 else
613 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
614
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900615 if (!memslot->dirty_bitmap)
616 return -ENOMEM;
617
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900618 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900619 return 0;
620}
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 * Allocate some memory and give it an address in the guest physical address
624 * space.
625 *
626 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800627 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500628 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800630int __kvm_set_memory_region(struct kvm *kvm,
631 struct kvm_userspace_memory_region *mem,
632 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200634 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200636 unsigned long npages;
637 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 struct kvm_memory_slot *memslot;
639 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200640 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641
642 r = -EINVAL;
643 /* General sanity checks */
644 if (mem->memory_size & (PAGE_SIZE - 1))
645 goto out;
646 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
647 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800648 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600649 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200650 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651 goto out;
652 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
653 goto out;
654
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200655 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
657 npages = mem->memory_size >> PAGE_SHIFT;
658
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900659 r = -EINVAL;
660 if (npages > KVM_MEM_MAX_NR_PAGES)
661 goto out;
662
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 if (!npages)
664 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
665
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 new = old = *memslot;
667
Avi Kivitye36d96f2010-06-21 10:56:36 +0300668 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 new.base_gfn = base_gfn;
670 new.npages = npages;
671 new.flags = mem->flags;
672
673 /* Disallow changing a memory slot's size. */
674 r = -EINVAL;
675 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800676 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677
678 /* Check for overlaps */
679 r = -EEXIST;
680 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200681 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200683 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 continue;
685 if (!((base_gfn + npages <= s->base_gfn) ||
686 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800687 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 /* Free page dirty bitmap if unneeded */
691 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000692 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693
694 r = -ENOMEM;
695
696 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200697#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500698 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900699 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200700
701 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800702 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200703
Izik Eidus80b14b52007-10-25 11:54:04 +0200704 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200705 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200707 if (!npages)
708 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300709
Joerg Roedelec04b262009-06-19 15:16:23 +0200710 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200711 unsigned long ugfn;
712 unsigned long j;
713 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200714 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300715
Joerg Roedelec04b262009-06-19 15:16:23 +0200716 /* Avoid unused variable warning if no large pages */
717 (void)level;
718
719 if (new.lpage_info[i])
720 continue;
721
Joerg Roedel82855412010-07-01 16:00:11 +0200722 lpages = 1 + ((base_gfn + npages - 1)
723 >> KVM_HPAGE_GFN_SHIFT(level));
724 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200725
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900726 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200727
728 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300729 goto out_free;
730
Joerg Roedel82855412010-07-01 16:00:11 +0200731 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200732 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200733 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200734 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300735 ugfn = new.userspace_addr >> PAGE_SHIFT;
736 /*
737 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300738 * other, or if explicitly asked to, disable large page
739 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300740 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200741 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300742 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200743 for (j = 0; j < lpages; ++j)
744 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300745 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746
Joerg Roedelec04b262009-06-19 15:16:23 +0200747skip_lpage:
748
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 /* Allocate page dirty bitmap if needed */
750 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900751 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800752 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200753 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300754 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200755 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200757#else /* not defined CONFIG_S390 */
758 new.user_alloc = user_alloc;
759 if (user_alloc)
760 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200761#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200763 if (!npages) {
764 r = -ENOMEM;
765 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
766 if (!slots)
767 goto out_free;
768 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
769 if (mem->slot >= slots->nmemslots)
770 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200771 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200772 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
773
774 old_memslots = kvm->memslots;
775 rcu_assign_pointer(kvm->memslots, slots);
776 synchronize_srcu_expedited(&kvm->srcu);
777 /* From this point no new shadow pages pointing to a deleted
778 * memslot will be created.
779 *
780 * validation of sp->gfn happens in:
781 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
782 * - kvm_is_visible_gfn (mmu_check_roots)
783 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300784 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200785 kfree(old_memslots);
786 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300787
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200788 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
789 if (r)
790 goto out_free;
791
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200792 /* map the pages in iommu page table */
793 if (npages) {
794 r = kvm_iommu_map_pages(kvm, &new);
795 if (r)
796 goto out_free;
797 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200798
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200799 r = -ENOMEM;
800 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
801 if (!slots)
802 goto out_free;
803 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
804 if (mem->slot >= slots->nmemslots)
805 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200806 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200807
808 /* actual memory is freed via old in kvm_free_physmem_slot below */
809 if (!npages) {
810 new.rmap = NULL;
811 new.dirty_bitmap = NULL;
812 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
813 new.lpage_info[i] = NULL;
814 }
815
816 slots->memslots[mem->slot] = new;
817 old_memslots = kvm->memslots;
818 rcu_assign_pointer(kvm->memslots, slots);
819 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200821 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800822
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200823 kvm_free_physmem_slot(&old, &new);
824 kfree(old_memslots);
825
826 if (flush_shadow)
827 kvm_arch_flush_shadow(kvm);
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 return 0;
830
Sheng Yangf78e0e22007-10-29 09:40:42 +0800831out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 kvm_free_physmem_slot(&new, &old);
833out:
834 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200835
836}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800837EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
838
839int kvm_set_memory_region(struct kvm *kvm,
840 struct kvm_userspace_memory_region *mem,
841 int user_alloc)
842{
843 int r;
844
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200845 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800846 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200847 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800848 return r;
849}
Izik Eidus210c7c42007-10-24 23:52:57 +0200850EXPORT_SYMBOL_GPL(kvm_set_memory_region);
851
Carsten Otte1fe779f2007-10-29 16:08:35 +0100852int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
853 struct
854 kvm_userspace_memory_region *mem,
855 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200856{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200857 if (mem->slot >= KVM_MEMORY_SLOTS)
858 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200859 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860}
861
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800862int kvm_get_dirty_log(struct kvm *kvm,
863 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864{
865 struct kvm_memory_slot *memslot;
866 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900867 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 unsigned long any = 0;
869
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 r = -EINVAL;
871 if (log->slot >= KVM_MEMORY_SLOTS)
872 goto out;
873
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200874 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 r = -ENOENT;
876 if (!memslot->dirty_bitmap)
877 goto out;
878
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900879 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880
Uri Lublincd1a4a92007-02-22 16:43:09 +0200881 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 any = memslot->dirty_bitmap[i];
883
884 r = -EFAULT;
885 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
886 goto out;
887
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800888 if (any)
889 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890
891 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 return r;
894}
895
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300896void kvm_disable_largepages(void)
897{
898 largepages_enabled = false;
899}
900EXPORT_SYMBOL_GPL(kvm_disable_largepages);
901
Izik Eiduscea7bb22007-10-17 19:17:48 +0200902int is_error_page(struct page *page)
903{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300904 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200905}
906EXPORT_SYMBOL_GPL(is_error_page);
907
Anthony Liguori35149e22008-04-02 14:46:56 -0500908int is_error_pfn(pfn_t pfn)
909{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300910 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500911}
912EXPORT_SYMBOL_GPL(is_error_pfn);
913
Huang Yingbf998152010-05-31 14:28:19 +0800914int is_hwpoison_pfn(pfn_t pfn)
915{
916 return pfn == hwpoison_pfn;
917}
918EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
919
Gleb Natapovedba23e2010-07-07 20:16:45 +0300920int is_fault_pfn(pfn_t pfn)
921{
922 return pfn == fault_pfn;
923}
924EXPORT_SYMBOL_GPL(is_fault_pfn);
925
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200926static inline unsigned long bad_hva(void)
927{
928 return PAGE_OFFSET;
929}
930
931int kvm_is_error_hva(unsigned long addr)
932{
933 return addr == bad_hva();
934}
935EXPORT_SYMBOL_GPL(kvm_is_error_hva);
936
Gleb Natapov49c77542010-10-18 15:22:23 +0200937static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
938 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939{
940 int i;
941
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200942 for (i = 0; i < slots->nmemslots; ++i) {
943 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
945 if (gfn >= memslot->base_gfn
946 && gfn < memslot->base_gfn + memslot->npages)
947 return memslot;
948 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000949 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950}
Gleb Natapov49c77542010-10-18 15:22:23 +0200951
952struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
953{
954 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
955}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300956EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957
Izik Eiduse0d62c72007-10-24 23:57:46 +0200958int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
959{
960 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800961 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200962
Izik Eiduse0d62c72007-10-24 23:57:46 +0200963 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200964 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200965
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200966 if (memslot->flags & KVM_MEMSLOT_INVALID)
967 continue;
968
Izik Eiduse0d62c72007-10-24 23:57:46 +0200969 if (gfn >= memslot->base_gfn
970 && gfn < memslot->base_gfn + memslot->npages)
971 return 1;
972 }
973 return 0;
974}
975EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
976
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100977unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
978{
979 struct vm_area_struct *vma;
980 unsigned long addr, size;
981
982 size = PAGE_SIZE;
983
984 addr = gfn_to_hva(kvm, gfn);
985 if (kvm_is_error_hva(addr))
986 return PAGE_SIZE;
987
988 down_read(&current->mm->mmap_sem);
989 vma = find_vma(current->mm, addr);
990 if (!vma)
991 goto out;
992
993 size = vma_kernel_pagesize(vma);
994
995out:
996 up_read(&current->mm->mmap_sem);
997
998 return size;
999}
1000
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001001int memslot_id(struct kvm *kvm, gfn_t gfn)
1002{
1003 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001004 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001005 struct kvm_memory_slot *memslot = NULL;
1006
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001007 for (i = 0; i < slots->nmemslots; ++i) {
1008 memslot = &slots->memslots[i];
1009
1010 if (gfn >= memslot->base_gfn
1011 && gfn < memslot->base_gfn + memslot->npages)
1012 break;
1013 }
1014
1015 return memslot - slots->memslots;
1016}
1017
Gleb Natapov49c77542010-10-18 15:22:23 +02001018static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001019 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001020{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001021 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001022 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001023
1024 if (nr_pages)
1025 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1026
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001027 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001028}
Xiao Guangrong48987782010-08-22 19:11:43 +08001029
1030unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1031{
Gleb Natapov49c77542010-10-18 15:22:23 +02001032 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001033}
Sheng Yang0d150292008-04-25 21:44:50 +08001034EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001035
Gleb Natapov80300892010-10-19 18:13:41 +02001036static pfn_t get_fault_pfn(void)
1037{
1038 get_page(fault_page);
1039 return fault_pfn;
1040}
1041
Gleb Natapovaf585b92010-10-14 11:22:46 +02001042static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001043 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001044{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001045 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001046 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001047 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001048
Gleb Natapovaf585b92010-10-14 11:22:46 +02001049 /* we can do it either atomically or asynchronously, not both */
1050 BUG_ON(atomic && async);
1051
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001052 BUG_ON(!write_fault && !writable);
1053
1054 if (writable)
1055 *writable = true;
1056
Gleb Natapovaf585b92010-10-14 11:22:46 +02001057 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001058 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001059
1060 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001061 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001062
1063 if (writable)
1064 *writable = write_fault;
1065
1066 npages = get_user_pages_fast(addr, 1, write_fault, page);
1067
1068 /* map read fault as writable if possible */
1069 if (unlikely(!write_fault) && npages == 1) {
1070 struct page *wpage[1];
1071
1072 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1073 if (npages == 1) {
1074 *writable = true;
1075 put_page(page[0]);
1076 page[0] = wpage[0];
1077 }
1078 npages = 1;
1079 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001080 }
Izik Eidus539cb662007-11-11 22:05:04 +02001081
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001082 if (unlikely(npages != 1)) {
1083 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001084
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001085 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001086 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001087
Huang Yingbbeb3402010-06-22 14:23:11 +08001088 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001089 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001090 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001091 get_page(hwpoison_page);
1092 return page_to_pfn(hwpoison_page);
1093 }
1094
Gleb Natapov80300892010-10-19 18:13:41 +02001095 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001096
Gleb Natapov80300892010-10-19 18:13:41 +02001097 if (vma == NULL)
1098 pfn = get_fault_pfn();
1099 else if ((vma->vm_flags & VM_PFNMAP)) {
1100 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1101 vma->vm_pgoff;
1102 BUG_ON(!kvm_is_mmio_pfn(pfn));
1103 } else {
1104 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001105 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001106 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001107 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001108 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001109 } else
1110 pfn = page_to_pfn(page[0]);
1111
1112 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001113}
1114
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001115pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1116{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001117 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001118}
1119EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1120
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001121static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1122 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001123{
1124 unsigned long addr;
1125
Gleb Natapovaf585b92010-10-14 11:22:46 +02001126 if (async)
1127 *async = false;
1128
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001129 addr = gfn_to_hva(kvm, gfn);
1130 if (kvm_is_error_hva(addr)) {
1131 get_page(bad_page);
1132 return page_to_pfn(bad_page);
1133 }
1134
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001135 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001136}
1137
1138pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1139{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001140 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001141}
1142EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1143
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001144pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1145 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001146{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001147 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001148}
1149EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1150
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001151pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1152{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001153 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001154}
Anthony Liguori35149e22008-04-02 14:46:56 -05001155EXPORT_SYMBOL_GPL(gfn_to_pfn);
1156
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001157pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1158 bool *writable)
1159{
1160 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1161}
1162EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1163
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001164pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1165 struct kvm_memory_slot *slot, gfn_t gfn)
1166{
1167 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001168 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001169}
1170
Xiao Guangrong48987782010-08-22 19:11:43 +08001171int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1172 int nr_pages)
1173{
1174 unsigned long addr;
1175 gfn_t entry;
1176
Gleb Natapov49c77542010-10-18 15:22:23 +02001177 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001178 if (kvm_is_error_hva(addr))
1179 return -1;
1180
1181 if (entry < nr_pages)
1182 return 0;
1183
1184 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1185}
1186EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1187
Anthony Liguori35149e22008-04-02 14:46:56 -05001188struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1189{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001190 pfn_t pfn;
1191
1192 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001193 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001194 return pfn_to_page(pfn);
1195
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001196 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001197
1198 get_page(bad_page);
1199 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001200}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001201
Avi Kivity954bbbc22007-03-30 14:02:32 +03001202EXPORT_SYMBOL_GPL(gfn_to_page);
1203
Izik Eidusb4231d62007-11-20 11:49:33 +02001204void kvm_release_page_clean(struct page *page)
1205{
Anthony Liguori35149e22008-04-02 14:46:56 -05001206 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001207}
1208EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1209
Anthony Liguori35149e22008-04-02 14:46:56 -05001210void kvm_release_pfn_clean(pfn_t pfn)
1211{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001212 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001213 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001214}
1215EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1216
Izik Eidusb4231d62007-11-20 11:49:33 +02001217void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001218{
Anthony Liguori35149e22008-04-02 14:46:56 -05001219 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001220}
Izik Eidusb4231d62007-11-20 11:49:33 +02001221EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001222
Anthony Liguori35149e22008-04-02 14:46:56 -05001223void kvm_release_pfn_dirty(pfn_t pfn)
1224{
1225 kvm_set_pfn_dirty(pfn);
1226 kvm_release_pfn_clean(pfn);
1227}
1228EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1229
1230void kvm_set_page_dirty(struct page *page)
1231{
1232 kvm_set_pfn_dirty(page_to_pfn(page));
1233}
1234EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1235
1236void kvm_set_pfn_dirty(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 struct page *page = pfn_to_page(pfn);
1240 if (!PageReserved(page))
1241 SetPageDirty(page);
1242 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001243}
1244EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1245
1246void kvm_set_pfn_accessed(pfn_t pfn)
1247{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001248 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001249 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001250}
1251EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1252
1253void kvm_get_pfn(pfn_t pfn)
1254{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001255 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001256 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001257}
1258EXPORT_SYMBOL_GPL(kvm_get_pfn);
1259
Izik Eidus195aefd2007-10-01 22:14:18 +02001260static int next_segment(unsigned long len, int offset)
1261{
1262 if (len > PAGE_SIZE - offset)
1263 return PAGE_SIZE - offset;
1264 else
1265 return len;
1266}
1267
1268int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1269 int len)
1270{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001271 int r;
1272 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001273
Izik Eiduse0506bc2007-11-11 22:10:22 +02001274 addr = gfn_to_hva(kvm, gfn);
1275 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001276 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001277 r = copy_from_user(data, (void __user *)addr + offset, len);
1278 if (r)
1279 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001280 return 0;
1281}
1282EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1283
1284int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1285{
1286 gfn_t gfn = gpa >> PAGE_SHIFT;
1287 int seg;
1288 int offset = offset_in_page(gpa);
1289 int ret;
1290
1291 while ((seg = next_segment(len, offset)) != 0) {
1292 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1293 if (ret < 0)
1294 return ret;
1295 offset = 0;
1296 len -= seg;
1297 data += seg;
1298 ++gfn;
1299 }
1300 return 0;
1301}
1302EXPORT_SYMBOL_GPL(kvm_read_guest);
1303
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001304int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1305 unsigned long len)
1306{
1307 int r;
1308 unsigned long addr;
1309 gfn_t gfn = gpa >> PAGE_SHIFT;
1310 int offset = offset_in_page(gpa);
1311
1312 addr = gfn_to_hva(kvm, gfn);
1313 if (kvm_is_error_hva(addr))
1314 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001315 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001316 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001317 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001318 if (r)
1319 return -EFAULT;
1320 return 0;
1321}
1322EXPORT_SYMBOL(kvm_read_guest_atomic);
1323
Izik Eidus195aefd2007-10-01 22:14:18 +02001324int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1325 int offset, int len)
1326{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001327 int r;
1328 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001329
Izik Eiduse0506bc2007-11-11 22:10:22 +02001330 addr = gfn_to_hva(kvm, gfn);
1331 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001332 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001333 r = copy_to_user((void __user *)addr + offset, data, len);
1334 if (r)
1335 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001336 mark_page_dirty(kvm, gfn);
1337 return 0;
1338}
1339EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1340
1341int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1342 unsigned long len)
1343{
1344 gfn_t gfn = gpa >> PAGE_SHIFT;
1345 int seg;
1346 int offset = offset_in_page(gpa);
1347 int ret;
1348
1349 while ((seg = next_segment(len, offset)) != 0) {
1350 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1351 if (ret < 0)
1352 return ret;
1353 offset = 0;
1354 len -= seg;
1355 data += seg;
1356 ++gfn;
1357 }
1358 return 0;
1359}
1360
Gleb Natapov49c77542010-10-18 15:22:23 +02001361int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1362 gpa_t gpa)
1363{
1364 struct kvm_memslots *slots = kvm_memslots(kvm);
1365 int offset = offset_in_page(gpa);
1366 gfn_t gfn = gpa >> PAGE_SHIFT;
1367
1368 ghc->gpa = gpa;
1369 ghc->generation = slots->generation;
1370 ghc->memslot = __gfn_to_memslot(slots, gfn);
1371 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1372 if (!kvm_is_error_hva(ghc->hva))
1373 ghc->hva += offset;
1374 else
1375 return -EFAULT;
1376
1377 return 0;
1378}
1379EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1380
1381int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1382 void *data, unsigned long len)
1383{
1384 struct kvm_memslots *slots = kvm_memslots(kvm);
1385 int r;
1386
1387 if (slots->generation != ghc->generation)
1388 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1389
1390 if (kvm_is_error_hva(ghc->hva))
1391 return -EFAULT;
1392
1393 r = copy_to_user((void __user *)ghc->hva, data, len);
1394 if (r)
1395 return -EFAULT;
1396 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1397
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1401
Izik Eidus195aefd2007-10-01 22:14:18 +02001402int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1403{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001404 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1405 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001406}
1407EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1408
1409int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1410{
1411 gfn_t gfn = gpa >> PAGE_SHIFT;
1412 int seg;
1413 int offset = offset_in_page(gpa);
1414 int ret;
1415
1416 while ((seg = next_segment(len, offset)) != 0) {
1417 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1418 if (ret < 0)
1419 return ret;
1420 offset = 0;
1421 len -= seg;
1422 ++gfn;
1423 }
1424 return 0;
1425}
1426EXPORT_SYMBOL_GPL(kvm_clear_guest);
1427
Gleb Natapov49c77542010-10-18 15:22:23 +02001428void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1429 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001431 if (memslot && memslot->dirty_bitmap) {
1432 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001434 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 }
1436}
1437
Gleb Natapov49c77542010-10-18 15:22:23 +02001438void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1439{
1440 struct kvm_memory_slot *memslot;
1441
1442 memslot = gfn_to_memslot(kvm, gfn);
1443 mark_page_dirty_in_slot(kvm, memslot, gfn);
1444}
1445
Eddie Dongb6958ce2007-07-18 12:15:21 +03001446/*
1447 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1448 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001449void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001450{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001451 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001452
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001453 for (;;) {
1454 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001455
Gleb Natapova1b37102009-07-09 15:33:52 +03001456 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001457 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001458 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001459 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001460 if (kvm_cpu_has_pending_timer(vcpu))
1461 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001462 if (signal_pending(current))
1463 break;
1464
Eddie Dongb6958ce2007-07-18 12:15:21 +03001465 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001466 }
1467
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001468 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001469}
1470
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471void kvm_resched(struct kvm_vcpu *vcpu)
1472{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001473 if (!need_resched())
1474 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476}
1477EXPORT_SYMBOL_GPL(kvm_resched);
1478
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001479void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1480{
1481 ktime_t expires;
1482 DEFINE_WAIT(wait);
1483
1484 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1485
1486 /* Sleep for 100 us, and hope lock-holder got scheduled */
1487 expires = ktime_add_ns(ktime_get(), 100000UL);
1488 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1489
1490 finish_wait(&vcpu->wq, &wait);
1491}
1492EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1493
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001494static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001495{
1496 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001497 struct page *page;
1498
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001499 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001500 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001501#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001502 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001503 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001504#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001505#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1506 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1507 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1508#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001509 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001510 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001511 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001512 vmf->page = page;
1513 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001514}
1515
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001516static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001517 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001518};
1519
1520static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1521{
1522 vma->vm_ops = &kvm_vcpu_vm_ops;
1523 return 0;
1524}
1525
Avi Kivitybccf2152007-02-21 18:04:26 +02001526static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1527{
1528 struct kvm_vcpu *vcpu = filp->private_data;
1529
Al Viro66c0b392008-04-19 20:33:56 +01001530 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001531 return 0;
1532}
1533
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001534static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001535 .release = kvm_vcpu_release,
1536 .unlocked_ioctl = kvm_vcpu_ioctl,
1537 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001538 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001539 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001540};
1541
1542/*
1543 * Allocates an inode for the vcpu.
1544 */
1545static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1546{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001547 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001548}
1549
Avi Kivityc5ea7662007-02-20 18:41:05 +02001550/*
1551 * Creates some virtual cpus. Good luck creating more than one.
1552 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001553static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001554{
1555 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001556 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001557
Gleb Natapov73880c82009-06-09 15:56:28 +03001558 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001559 if (IS_ERR(vcpu))
1560 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001561
Avi Kivity15ad7142007-07-11 18:17:21 +03001562 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1563
Avi Kivity26e52152007-11-20 15:30:24 +02001564 r = kvm_arch_vcpu_setup(vcpu);
1565 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001566 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001567
Shaohua Li11ec2802007-07-23 14:51:37 +08001568 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001569 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1570 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001571 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001572 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001573
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001574 kvm_for_each_vcpu(r, v, kvm)
1575 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001576 r = -EEXIST;
1577 goto vcpu_destroy;
1578 }
1579
1580 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001581
1582 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001583 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001584 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001585 if (r < 0) {
1586 kvm_put_kvm(kvm);
1587 goto vcpu_destroy;
1588 }
1589
1590 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1591 smp_wmb();
1592 atomic_inc(&kvm->online_vcpus);
1593
1594#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1595 if (kvm->bsp_vcpu_id == id)
1596 kvm->bsp_vcpu = vcpu;
1597#endif
1598 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001599 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001600
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001601vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001602 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001603 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001604 return r;
1605}
1606
Avi Kivity1961d272007-03-05 19:46:05 +02001607static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1608{
1609 if (sigset) {
1610 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1611 vcpu->sigset_active = 1;
1612 vcpu->sigset = *sigset;
1613 } else
1614 vcpu->sigset_active = 0;
1615 return 0;
1616}
1617
Avi Kivitybccf2152007-02-21 18:04:26 +02001618static long kvm_vcpu_ioctl(struct file *filp,
1619 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620{
Avi Kivitybccf2152007-02-21 18:04:26 +02001621 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001622 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001623 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001624 struct kvm_fpu *fpu = NULL;
1625 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001627 if (vcpu->kvm->mm != current->mm)
1628 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001629
1630#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1631 /*
1632 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1633 * so vcpu_load() would break it.
1634 */
1635 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1636 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1637#endif
1638
1639
1640 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001642 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001643 r = -EINVAL;
1644 if (arg)
1645 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001646 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001647 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001650 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001652 r = -ENOMEM;
1653 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1654 if (!kvm_regs)
1655 goto out;
1656 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001658 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001660 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1661 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001663out_free1:
1664 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 break;
1666 }
1667 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001668 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001670 r = -ENOMEM;
1671 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1672 if (!kvm_regs)
1673 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001675 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1676 goto out_free2;
1677 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001679 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001681out_free2:
1682 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 break;
1684 }
1685 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001686 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1687 r = -ENOMEM;
1688 if (!kvm_sregs)
1689 goto out;
1690 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 if (r)
1692 goto out;
1693 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001694 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695 goto out;
1696 r = 0;
1697 break;
1698 }
1699 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001700 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1701 r = -ENOMEM;
1702 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001704 r = -EFAULT;
1705 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1706 goto out;
1707 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 if (r)
1709 goto out;
1710 r = 0;
1711 break;
1712 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001713 case KVM_GET_MP_STATE: {
1714 struct kvm_mp_state mp_state;
1715
1716 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1717 if (r)
1718 goto out;
1719 r = -EFAULT;
1720 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1721 goto out;
1722 r = 0;
1723 break;
1724 }
1725 case KVM_SET_MP_STATE: {
1726 struct kvm_mp_state mp_state;
1727
1728 r = -EFAULT;
1729 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1730 goto out;
1731 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1732 if (r)
1733 goto out;
1734 r = 0;
1735 break;
1736 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737 case KVM_TRANSLATE: {
1738 struct kvm_translation tr;
1739
1740 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001741 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001743 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 if (r)
1745 goto out;
1746 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001747 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 goto out;
1749 r = 0;
1750 break;
1751 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001752 case KVM_SET_GUEST_DEBUG: {
1753 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754
1755 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001756 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001758 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 if (r)
1760 goto out;
1761 r = 0;
1762 break;
1763 }
Avi Kivity1961d272007-03-05 19:46:05 +02001764 case KVM_SET_SIGNAL_MASK: {
1765 struct kvm_signal_mask __user *sigmask_arg = argp;
1766 struct kvm_signal_mask kvm_sigmask;
1767 sigset_t sigset, *p;
1768
1769 p = NULL;
1770 if (argp) {
1771 r = -EFAULT;
1772 if (copy_from_user(&kvm_sigmask, argp,
1773 sizeof kvm_sigmask))
1774 goto out;
1775 r = -EINVAL;
1776 if (kvm_sigmask.len != sizeof sigset)
1777 goto out;
1778 r = -EFAULT;
1779 if (copy_from_user(&sigset, sigmask_arg->sigset,
1780 sizeof sigset))
1781 goto out;
1782 p = &sigset;
1783 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001784 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001785 break;
1786 }
Avi Kivityb8836732007-04-01 16:34:31 +03001787 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001788 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1789 r = -ENOMEM;
1790 if (!fpu)
1791 goto out;
1792 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001793 if (r)
1794 goto out;
1795 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001796 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001797 goto out;
1798 r = 0;
1799 break;
1800 }
1801 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001802 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1803 r = -ENOMEM;
1804 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001805 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001806 r = -EFAULT;
1807 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1808 goto out;
1809 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001810 if (r)
1811 goto out;
1812 r = 0;
1813 break;
1814 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001815 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001816 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001817 }
1818out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001819 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001820 kfree(fpu);
1821 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001822 return r;
1823}
1824
1825static long kvm_vm_ioctl(struct file *filp,
1826 unsigned int ioctl, unsigned long arg)
1827{
1828 struct kvm *kvm = filp->private_data;
1829 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001830 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001831
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001832 if (kvm->mm != current->mm)
1833 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001834 switch (ioctl) {
1835 case KVM_CREATE_VCPU:
1836 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1837 if (r < 0)
1838 goto out;
1839 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001840 case KVM_SET_USER_MEMORY_REGION: {
1841 struct kvm_userspace_memory_region kvm_userspace_mem;
1842
1843 r = -EFAULT;
1844 if (copy_from_user(&kvm_userspace_mem, argp,
1845 sizeof kvm_userspace_mem))
1846 goto out;
1847
1848 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849 if (r)
1850 goto out;
1851 break;
1852 }
1853 case KVM_GET_DIRTY_LOG: {
1854 struct kvm_dirty_log log;
1855
1856 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001857 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001859 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860 if (r)
1861 goto out;
1862 break;
1863 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001864#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1865 case KVM_REGISTER_COALESCED_MMIO: {
1866 struct kvm_coalesced_mmio_zone zone;
1867 r = -EFAULT;
1868 if (copy_from_user(&zone, argp, sizeof zone))
1869 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001870 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1871 if (r)
1872 goto out;
1873 r = 0;
1874 break;
1875 }
1876 case KVM_UNREGISTER_COALESCED_MMIO: {
1877 struct kvm_coalesced_mmio_zone zone;
1878 r = -EFAULT;
1879 if (copy_from_user(&zone, argp, sizeof zone))
1880 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001881 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1882 if (r)
1883 goto out;
1884 r = 0;
1885 break;
1886 }
1887#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001888 case KVM_IRQFD: {
1889 struct kvm_irqfd data;
1890
1891 r = -EFAULT;
1892 if (copy_from_user(&data, argp, sizeof data))
1893 goto out;
1894 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1895 break;
1896 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001897 case KVM_IOEVENTFD: {
1898 struct kvm_ioeventfd data;
1899
1900 r = -EFAULT;
1901 if (copy_from_user(&data, argp, sizeof data))
1902 goto out;
1903 r = kvm_ioeventfd(kvm, &data);
1904 break;
1905 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001906#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1907 case KVM_SET_BOOT_CPU_ID:
1908 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001909 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001910 if (atomic_read(&kvm->online_vcpus) != 0)
1911 r = -EBUSY;
1912 else
1913 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001914 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001915 break;
1916#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001917 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001918 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001919 if (r == -ENOTTY)
1920 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001921 }
1922out:
1923 return r;
1924}
1925
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001926#ifdef CONFIG_COMPAT
1927struct compat_kvm_dirty_log {
1928 __u32 slot;
1929 __u32 padding1;
1930 union {
1931 compat_uptr_t dirty_bitmap; /* one bit per page */
1932 __u64 padding2;
1933 };
1934};
1935
1936static long kvm_vm_compat_ioctl(struct file *filp,
1937 unsigned int ioctl, unsigned long arg)
1938{
1939 struct kvm *kvm = filp->private_data;
1940 int r;
1941
1942 if (kvm->mm != current->mm)
1943 return -EIO;
1944 switch (ioctl) {
1945 case KVM_GET_DIRTY_LOG: {
1946 struct compat_kvm_dirty_log compat_log;
1947 struct kvm_dirty_log log;
1948
1949 r = -EFAULT;
1950 if (copy_from_user(&compat_log, (void __user *)arg,
1951 sizeof(compat_log)))
1952 goto out;
1953 log.slot = compat_log.slot;
1954 log.padding1 = compat_log.padding1;
1955 log.padding2 = compat_log.padding2;
1956 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1957
1958 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1959 if (r)
1960 goto out;
1961 break;
1962 }
1963 default:
1964 r = kvm_vm_ioctl(filp, ioctl, arg);
1965 }
1966
1967out:
1968 return r;
1969}
1970#endif
1971
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001972static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001973{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001974 struct page *page[1];
1975 unsigned long addr;
1976 int npages;
1977 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001978 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001979
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001980 addr = gfn_to_hva(kvm, gfn);
1981 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001982 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001983
1984 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1985 NULL);
1986 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001987 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001988
1989 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001990 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001991}
1992
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001993static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001994 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001995};
1996
1997static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1998{
1999 vma->vm_ops = &kvm_vm_vm_ops;
2000 return 0;
2001}
2002
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002003static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002004 .release = kvm_vm_release,
2005 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002006#ifdef CONFIG_COMPAT
2007 .compat_ioctl = kvm_vm_compat_ioctl,
2008#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002009 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002010 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002011};
2012
2013static int kvm_dev_ioctl_create_vm(void)
2014{
Heiko Carstensaac87632010-10-27 17:22:10 +02002015 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002016 struct kvm *kvm;
2017
Avi Kivityf17abe92007-02-21 19:28:04 +02002018 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002019 if (IS_ERR(kvm))
2020 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002021#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2022 r = kvm_coalesced_mmio_init(kvm);
2023 if (r < 0) {
2024 kvm_put_kvm(kvm);
2025 return r;
2026 }
2027#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002028 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2029 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002030 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002031
Heiko Carstensaac87632010-10-27 17:22:10 +02002032 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002033}
2034
Avi Kivity1a811b62008-12-08 18:25:27 +02002035static long kvm_dev_ioctl_check_extension_generic(long arg)
2036{
2037 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002038 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002039 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002040 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002041#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2042 case KVM_CAP_SET_BOOT_CPU_ID:
2043#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002044 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002045 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002046#ifdef CONFIG_HAVE_KVM_IRQCHIP
2047 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002048 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002049#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002050 default:
2051 break;
2052 }
2053 return kvm_dev_ioctl_check_extension(arg);
2054}
2055
Avi Kivityf17abe92007-02-21 19:28:04 +02002056static long kvm_dev_ioctl(struct file *filp,
2057 unsigned int ioctl, unsigned long arg)
2058{
Avi Kivity07c45a32007-03-07 13:05:38 +02002059 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002060
2061 switch (ioctl) {
2062 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002063 r = -EINVAL;
2064 if (arg)
2065 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002066 r = KVM_API_VERSION;
2067 break;
2068 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002069 r = -EINVAL;
2070 if (arg)
2071 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002072 r = kvm_dev_ioctl_create_vm();
2073 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002074 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002075 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002076 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002077 case KVM_GET_VCPU_MMAP_SIZE:
2078 r = -EINVAL;
2079 if (arg)
2080 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002081 r = PAGE_SIZE; /* struct kvm_run */
2082#ifdef CONFIG_X86
2083 r += PAGE_SIZE; /* pio data page */
2084#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002085#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2086 r += PAGE_SIZE; /* coalesced mmio ring page */
2087#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002088 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002089 case KVM_TRACE_ENABLE:
2090 case KVM_TRACE_PAUSE:
2091 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002092 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002093 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002095 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 }
2097out:
2098 return r;
2099}
2100
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 .unlocked_ioctl = kvm_dev_ioctl,
2103 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002104 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105};
2106
2107static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002108 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 "kvm",
2110 &kvm_chardev_ops,
2111};
2112
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002113static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002114{
2115 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002116 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002117
Rusty Russell7f59f492008-12-07 21:25:45 +10302118 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002119 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002120
Rusty Russell7f59f492008-12-07 21:25:45 +10302121 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002122
2123 r = kvm_arch_hardware_enable(NULL);
2124
2125 if (r) {
2126 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2127 atomic_inc(&hardware_enable_failed);
2128 printk(KERN_INFO "kvm: enabling virtualization on "
2129 "CPU%d failed\n", cpu);
2130 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002131}
2132
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002133static void hardware_enable(void *junk)
2134{
2135 spin_lock(&kvm_lock);
2136 hardware_enable_nolock(junk);
2137 spin_unlock(&kvm_lock);
2138}
2139
2140static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002141{
2142 int cpu = raw_smp_processor_id();
2143
Rusty Russell7f59f492008-12-07 21:25:45 +10302144 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002145 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302146 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002147 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002148}
2149
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002150static void hardware_disable(void *junk)
2151{
2152 spin_lock(&kvm_lock);
2153 hardware_disable_nolock(junk);
2154 spin_unlock(&kvm_lock);
2155}
2156
Alexander Graf10474ae2009-09-15 11:37:46 +02002157static void hardware_disable_all_nolock(void)
2158{
2159 BUG_ON(!kvm_usage_count);
2160
2161 kvm_usage_count--;
2162 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002163 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002164}
2165
2166static void hardware_disable_all(void)
2167{
2168 spin_lock(&kvm_lock);
2169 hardware_disable_all_nolock();
2170 spin_unlock(&kvm_lock);
2171}
2172
2173static int hardware_enable_all(void)
2174{
2175 int r = 0;
2176
2177 spin_lock(&kvm_lock);
2178
2179 kvm_usage_count++;
2180 if (kvm_usage_count == 1) {
2181 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002182 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002183
2184 if (atomic_read(&hardware_enable_failed)) {
2185 hardware_disable_all_nolock();
2186 r = -EBUSY;
2187 }
2188 }
2189
2190 spin_unlock(&kvm_lock);
2191
2192 return r;
2193}
2194
Avi Kivity774c47f2007-02-12 00:54:47 -08002195static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2196 void *v)
2197{
2198 int cpu = (long)v;
2199
Alexander Graf10474ae2009-09-15 11:37:46 +02002200 if (!kvm_usage_count)
2201 return NOTIFY_OK;
2202
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002203 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002204 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002205 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002206 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2207 cpu);
2208 hardware_disable(NULL);
2209 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002210 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002211 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2212 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002213 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002214 break;
2215 }
2216 return NOTIFY_OK;
2217}
2218
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002219
Avi Kivityb7c41452010-12-02 17:52:50 +02002220asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002221{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002222 /* Fault while not rebooting. We want the trace. */
2223 BUG();
2224}
Avi Kivityb7c41452010-12-02 17:52:50 +02002225EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002226
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002227static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002228 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002229{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002230 /*
2231 * Some (well, at least mine) BIOSes hang on reboot if
2232 * in vmx root mode.
2233 *
2234 * And Intel TXT required VMX off for all cpu when system shutdown.
2235 */
2236 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2237 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002238 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002239 return NOTIFY_OK;
2240}
2241
2242static struct notifier_block kvm_reboot_notifier = {
2243 .notifier_call = kvm_reboot,
2244 .priority = 0,
2245};
2246
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002247static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002248{
2249 int i;
2250
2251 for (i = 0; i < bus->dev_count; i++) {
2252 struct kvm_io_device *pos = bus->devs[i];
2253
2254 kvm_iodevice_destructor(pos);
2255 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002256 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002257}
2258
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002259/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002260int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002261 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002262{
2263 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002264 struct kvm_io_bus *bus;
2265
2266 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002267 for (i = 0; i < bus->dev_count; i++)
2268 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2269 return 0;
2270 return -EOPNOTSUPP;
2271}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002272
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002273/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002274int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2275 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002276{
2277 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002278 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002279
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002280 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002281 for (i = 0; i < bus->dev_count; i++)
2282 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2283 return 0;
2284 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002285}
2286
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002287/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002288int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2289 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002290{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002291 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002292
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002293 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002294 if (bus->dev_count > NR_IOBUS_DEVS-1)
2295 return -ENOSPC;
2296
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002297 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2298 if (!new_bus)
2299 return -ENOMEM;
2300 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2301 new_bus->devs[new_bus->dev_count++] = dev;
2302 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2303 synchronize_srcu_expedited(&kvm->srcu);
2304 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002305
2306 return 0;
2307}
2308
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002309/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002310int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2311 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002312{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002313 int i, r;
2314 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002315
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002316 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2317 if (!new_bus)
2318 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002319
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002320 bus = kvm->buses[bus_idx];
2321 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2322
2323 r = -ENOENT;
2324 for (i = 0; i < new_bus->dev_count; i++)
2325 if (new_bus->devs[i] == dev) {
2326 r = 0;
2327 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002328 break;
2329 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002330
2331 if (r) {
2332 kfree(new_bus);
2333 return r;
2334 }
2335
2336 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2337 synchronize_srcu_expedited(&kvm->srcu);
2338 kfree(bus);
2339 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002340}
2341
Avi Kivity774c47f2007-02-12 00:54:47 -08002342static struct notifier_block kvm_cpu_notifier = {
2343 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002344};
2345
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002346static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002347{
2348 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002349 struct kvm *kvm;
2350
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002351 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002352 spin_lock(&kvm_lock);
2353 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002354 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002355 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002356 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002357}
2358
2359DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2360
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002361static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002362{
2363 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002364 struct kvm *kvm;
2365 struct kvm_vcpu *vcpu;
2366 int i;
2367
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002368 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002369 spin_lock(&kvm_lock);
2370 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002371 kvm_for_each_vcpu(i, vcpu, kvm)
2372 *val += *(u32 *)((void *)vcpu + offset);
2373
Avi Kivity1165f5f2007-04-19 17:27:43 +03002374 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002375 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002376}
2377
Avi Kivityba1389b2007-11-18 16:24:12 +02002378DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2379
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002380static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002381 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2382 [KVM_STAT_VM] = &vm_stat_fops,
2383};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002384
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002385static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386{
2387 struct kvm_stats_debugfs_item *p;
2388
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002389 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002391 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002392 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002393 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394}
2395
2396static void kvm_exit_debug(void)
2397{
2398 struct kvm_stats_debugfs_item *p;
2399
2400 for (p = debugfs_entries; p->name; ++p)
2401 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002402 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403}
2404
Avi Kivity59ae6c62007-02-12 00:54:48 -08002405static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2406{
Alexander Graf10474ae2009-09-15 11:37:46 +02002407 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002408 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002409 return 0;
2410}
2411
2412static int kvm_resume(struct sys_device *dev)
2413{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002414 if (kvm_usage_count) {
2415 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002416 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002417 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002418 return 0;
2419}
2420
2421static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002422 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002423 .suspend = kvm_suspend,
2424 .resume = kvm_resume,
2425};
2426
2427static struct sys_device kvm_sysdev = {
2428 .id = 0,
2429 .cls = &kvm_sysdev_class,
2430};
2431
Izik Eiduscea7bb22007-10-17 19:17:48 +02002432struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002433pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434
Avi Kivity15ad7142007-07-11 18:17:21 +03002435static inline
2436struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2437{
2438 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2439}
2440
2441static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2442{
2443 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2444
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002445 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002446}
2447
2448static void kvm_sched_out(struct preempt_notifier *pn,
2449 struct task_struct *next)
2450{
2451 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2452
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002453 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002454}
2455
Avi Kivity0ee75be2010-04-28 15:39:01 +03002456int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002457 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458{
2459 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002460 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002462 r = kvm_arch_init(opaque);
2463 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002464 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002465
2466 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2467
2468 if (bad_page == NULL) {
2469 r = -ENOMEM;
2470 goto out;
2471 }
2472
Anthony Liguori35149e22008-04-02 14:46:56 -05002473 bad_pfn = page_to_pfn(bad_page);
2474
Huang Yingbf998152010-05-31 14:28:19 +08002475 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2476
2477 if (hwpoison_page == NULL) {
2478 r = -ENOMEM;
2479 goto out_free_0;
2480 }
2481
2482 hwpoison_pfn = page_to_pfn(hwpoison_page);
2483
Gleb Natapovedba23e2010-07-07 20:16:45 +03002484 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2485
2486 if (fault_page == NULL) {
2487 r = -ENOMEM;
2488 goto out_free_0;
2489 }
2490
2491 fault_pfn = page_to_pfn(fault_page);
2492
Avi Kivity8437a612009-06-06 14:52:35 -07002493 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302494 r = -ENOMEM;
2495 goto out_free_0;
2496 }
2497
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002498 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302500 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501
Yang, Sheng002c7f72007-07-31 14:23:01 +03002502 for_each_online_cpu(cpu) {
2503 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002504 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002505 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002506 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002507 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002508 }
2509
Avi Kivity774c47f2007-02-12 00:54:47 -08002510 r = register_cpu_notifier(&kvm_cpu_notifier);
2511 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002512 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 register_reboot_notifier(&kvm_reboot_notifier);
2514
Avi Kivity59ae6c62007-02-12 00:54:48 -08002515 r = sysdev_class_register(&kvm_sysdev_class);
2516 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002517 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002518
2519 r = sysdev_register(&kvm_sysdev);
2520 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002521 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002522
Rusty Russellc16f8622007-07-30 21:12:19 +10002523 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002524 if (!vcpu_align)
2525 vcpu_align = __alignof__(struct kvm_vcpu);
2526 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002527 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002528 if (!kvm_vcpu_cache) {
2529 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002530 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002531 }
2532
Gleb Natapovaf585b92010-10-14 11:22:46 +02002533 r = kvm_async_pf_init();
2534 if (r)
2535 goto out_free;
2536
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002538 kvm_vm_fops.owner = module;
2539 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540
2541 r = misc_register(&kvm_dev);
2542 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002543 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002544 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 }
2546
Avi Kivity15ad7142007-07-11 18:17:21 +03002547 kvm_preempt_ops.sched_in = kvm_sched_in;
2548 kvm_preempt_ops.sched_out = kvm_sched_out;
2549
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002550 kvm_init_debug();
2551
Avi Kivityc7addb92007-09-16 18:58:32 +02002552 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553
Gleb Natapovaf585b92010-10-14 11:22:46 +02002554out_unreg:
2555 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002557 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002558out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002559 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002560out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002561 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002562out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002564 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002565out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002566out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002567 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302568out_free_0a:
2569 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002570out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002571 if (fault_page)
2572 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002573 if (hwpoison_page)
2574 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002575 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002576out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002577 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002578out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579 return r;
2580}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002581EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002583void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002585 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002587 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002588 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002589 sysdev_unregister(&kvm_sysdev);
2590 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002592 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002593 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002594 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002595 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302596 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002597 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002598 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002600EXPORT_SYMBOL_GPL(kvm_exit);