blob: fd67bcde998095186356ba8e84d3241d7910a52f [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 Arcangeli22e5c472011-01-13 15:47:20 -0800107 int reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800108 struct page *tail = pfn_to_page(pfn);
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800109 struct page *head = compound_trans_head(tail);
110 reserved = PageReserved(head);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800111 if (head != tail) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800112 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800113 * "head" is not a dangling pointer
114 * (compound_trans_head takes care of that)
115 * but the hugepage may have been splitted
116 * from under us (and we may not hold a
117 * reference count on the head page so it can
118 * be reused before we run PageReferenced), so
119 * we've to check PageTail before returning
120 * what we just read.
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800121 */
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800122 smp_rmb();
123 if (PageTail(tail))
124 return reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800125 }
126 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100127 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300128
129 return true;
130}
131
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132/*
133 * Switches to specified vcpu, until a matching vcpu_put()
134 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200135void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136{
Avi Kivity15ad7142007-07-11 18:17:21 +0300137 int cpu;
138
Avi Kivitybccf2152007-02-21 18:04:26 +0200139 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300140 cpu = get_cpu();
141 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200142 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300143 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200144}
145
Carsten Otte313a3dc2007-10-11 19:16:52 +0200146void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147{
Avi Kivity15ad7142007-07-11 18:17:21 +0300148 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200149 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300150 preempt_notifier_unregister(&vcpu->preempt_notifier);
151 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152 mutex_unlock(&vcpu->mutex);
153}
154
Avi Kivityd9e368d2007-06-07 19:18:30 +0300155static void ack_flush(void *_completed)
156{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300157}
158
Rusty Russell49846892008-12-08 20:26:24 +1030159static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300160{
Avi Kivity597a5f52008-07-20 14:24:22 +0300161 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030162 cpumask_var_t cpus;
163 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300164 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300165
Li Zefan79f55992009-06-15 14:58:26 +0800166 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030167
Avi Kivity70e335e2010-02-18 11:25:22 +0200168 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200169 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300170 kvm_for_each_vcpu(i, vcpu, kvm) {
Avi Kivitya8eeb042010-05-10 12:34:53 +0300171 if (kvm_make_check_request(req, vcpu))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300172 continue;
173 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030174 if (cpus != NULL && cpu != -1 && cpu != me)
175 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300176 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030177 if (unlikely(cpus == NULL))
178 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
179 else if (!cpumask_empty(cpus))
180 smp_call_function_many(cpus, ack_flush, NULL, 1);
181 else
182 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200183 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030184 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030185 return called;
186}
187
188void kvm_flush_remote_tlbs(struct kvm *kvm)
189{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800190 int dirty_count = kvm->tlbs_dirty;
191
192 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030193 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
194 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800195 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300196}
197
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500198void kvm_reload_remote_mmus(struct kvm *kvm)
199{
Rusty Russell49846892008-12-08 20:26:24 +1030200 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500201}
202
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000203int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
204{
205 struct page *page;
206 int r;
207
208 mutex_init(&vcpu->mutex);
209 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000210 vcpu->kvm = kvm;
211 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300212 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200213 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000214
215 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
216 if (!page) {
217 r = -ENOMEM;
218 goto fail;
219 }
220 vcpu->run = page_address(page);
221
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800222 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000223 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800224 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225 return 0;
226
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000227fail_free_run:
228 free_page((unsigned long)vcpu->run);
229fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000230 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000231}
232EXPORT_SYMBOL_GPL(kvm_vcpu_init);
233
234void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
235{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800236 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000237 free_page((unsigned long)vcpu->run);
238}
239EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
240
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200241#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
242static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
243{
244 return container_of(mn, struct kvm, mmu_notifier);
245}
246
247static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
248 struct mm_struct *mm,
249 unsigned long address)
250{
251 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200252 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200253
254 /*
255 * When ->invalidate_page runs, the linux pte has been zapped
256 * already but the page is still allocated until
257 * ->invalidate_page returns. So if we increase the sequence
258 * here the kvm page fault will notice if the spte can't be
259 * established because the page is going to be freed. If
260 * instead the kvm page fault establishes the spte before
261 * ->invalidate_page runs, kvm_unmap_hva will release it
262 * before returning.
263 *
264 * The sequence increase only need to be seen at spin_unlock
265 * time, and not at spin_lock time.
266 *
267 * Increasing the sequence after the spin_unlock would be
268 * unsafe because the kvm page fault could then establish the
269 * pte after kvm_unmap_hva returned, without noticing the page
270 * is going to be freed.
271 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200272 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200273 spin_lock(&kvm->mmu_lock);
274 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800275 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200276 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200277 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200278
279 /* we've to flush the tlb before the pages can be freed */
280 if (need_tlb_flush)
281 kvm_flush_remote_tlbs(kvm);
282
283}
284
Izik Eidus3da0dd42009-09-23 21:47:18 +0300285static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
286 struct mm_struct *mm,
287 unsigned long address,
288 pte_t pte)
289{
290 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200291 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300292
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200293 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300294 spin_lock(&kvm->mmu_lock);
295 kvm->mmu_notifier_seq++;
296 kvm_set_spte_hva(kvm, address, pte);
297 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200298 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300299}
300
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200301static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
302 struct mm_struct *mm,
303 unsigned long start,
304 unsigned long end)
305{
306 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200307 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200308
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200309 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200310 spin_lock(&kvm->mmu_lock);
311 /*
312 * The count increase must become visible at unlock time as no
313 * spte can be established without taking the mmu_lock and
314 * count is also read inside the mmu_lock critical section.
315 */
316 kvm->mmu_notifier_count++;
317 for (; start < end; start += PAGE_SIZE)
318 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800319 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200320 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200321 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200322
323 /* we've to flush the tlb before the pages can be freed */
324 if (need_tlb_flush)
325 kvm_flush_remote_tlbs(kvm);
326}
327
328static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
329 struct mm_struct *mm,
330 unsigned long start,
331 unsigned long end)
332{
333 struct kvm *kvm = mmu_notifier_to_kvm(mn);
334
335 spin_lock(&kvm->mmu_lock);
336 /*
337 * This sequence increase will notify the kvm page fault that
338 * the page that is going to be mapped in the spte could have
339 * been freed.
340 */
341 kvm->mmu_notifier_seq++;
342 /*
343 * The above sequence increase must be visible before the
344 * below count decrease but both values are read by the kvm
345 * page fault under mmu_lock spinlock so we don't need to add
346 * a smb_wmb() here in between the two.
347 */
348 kvm->mmu_notifier_count--;
349 spin_unlock(&kvm->mmu_lock);
350
351 BUG_ON(kvm->mmu_notifier_count < 0);
352}
353
354static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
355 struct mm_struct *mm,
356 unsigned long address)
357{
358 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200359 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200360
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200361 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200362 spin_lock(&kvm->mmu_lock);
363 young = kvm_age_hva(kvm, address);
364 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200365 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200366
367 if (young)
368 kvm_flush_remote_tlbs(kvm);
369
370 return young;
371}
372
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800373static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
374 struct mm_struct *mm,
375 unsigned long address)
376{
377 struct kvm *kvm = mmu_notifier_to_kvm(mn);
378 int young, idx;
379
380 idx = srcu_read_lock(&kvm->srcu);
381 spin_lock(&kvm->mmu_lock);
382 young = kvm_test_age_hva(kvm, address);
383 spin_unlock(&kvm->mmu_lock);
384 srcu_read_unlock(&kvm->srcu, idx);
385
386 return young;
387}
388
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100389static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
390 struct mm_struct *mm)
391{
392 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800393 int idx;
394
395 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100396 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800397 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100398}
399
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200400static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
401 .invalidate_page = kvm_mmu_notifier_invalidate_page,
402 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
403 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
404 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800405 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300406 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100407 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200408};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200409
410static int kvm_init_mmu_notifier(struct kvm *kvm)
411{
412 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
413 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
414}
415
416#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
417
418static int kvm_init_mmu_notifier(struct kvm *kvm)
419{
420 return 0;
421}
422
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200423#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
424
Avi Kivityf17abe92007-02-21 19:28:04 +0200425static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100427 int r, i;
428 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100430 if (!kvm)
431 return ERR_PTR(-ENOMEM);
432
433 r = kvm_arch_init_vm(kvm);
434 if (r)
435 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200436
437 r = hardware_enable_all();
438 if (r)
439 goto out_err_nodisable;
440
Avi Kivity75858a82009-01-04 17:10:50 +0200441#ifdef CONFIG_HAVE_KVM_IRQCHIP
442 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300443 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200444#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200446 r = -ENOMEM;
447 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
448 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100449 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200450 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100451 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200452 for (i = 0; i < KVM_NR_BUSES; i++) {
453 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
454 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100455 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200456 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200457 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200458
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200459 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100460 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200461 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200462
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200463 kvm->mm = current->mm;
464 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500465 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200466 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400467 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800468 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300469 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200470 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300471 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000472 spin_lock(&kvm_lock);
473 list_add(&kvm->vm_list, &vm_list);
474 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100475
Avi Kivityf17abe92007-02-21 19:28:04 +0200476 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200477
478out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100479 cleanup_srcu_struct(&kvm->srcu);
480out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200481 hardware_disable_all();
482out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200483 for (i = 0; i < KVM_NR_BUSES; i++)
484 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200485 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100486 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200487 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200488}
489
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900490static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
491{
492 if (!memslot->dirty_bitmap)
493 return;
494
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900495 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
496 vfree(memslot->dirty_bitmap_head);
497 else
498 kfree(memslot->dirty_bitmap_head);
499
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900500 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900501 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900502}
503
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504/*
505 * Free any memory in @free but not in @dont.
506 */
507static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
508 struct kvm_memory_slot *dont)
509{
Joerg Roedelec04b262009-06-19 15:16:23 +0200510 int i;
511
Izik Eidus290fc382007-09-27 14:11:22 +0200512 if (!dont || free->rmap != dont->rmap)
513 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514
515 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900516 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517
Joerg Roedelec04b262009-06-19 15:16:23 +0200518
519 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
520 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
521 vfree(free->lpage_info[i]);
522 free->lpage_info[i] = NULL;
523 }
524 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300525
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500527 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528}
529
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800530void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531{
532 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200533 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200535 for (i = 0; i < slots->nmemslots; ++i)
536 kvm_free_physmem_slot(&slots->memslots[i], NULL);
537
538 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539}
540
Avi Kivityf17abe92007-02-21 19:28:04 +0200541static void kvm_destroy_vm(struct kvm *kvm)
542{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200543 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200544 struct mm_struct *mm = kvm->mm;
545
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800546 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800547 spin_lock(&kvm_lock);
548 list_del(&kvm->vm_list);
549 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200550 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200551 for (i = 0; i < KVM_NR_BUSES; i++)
552 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200553 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200554#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
555 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200556#else
557 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200558#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800559 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100560 kvm_free_physmem(kvm);
561 cleanup_srcu_struct(&kvm->srcu);
562 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200563 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200564 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200565}
566
Izik Eidusd39f13b2008-03-30 16:01:25 +0300567void kvm_get_kvm(struct kvm *kvm)
568{
569 atomic_inc(&kvm->users_count);
570}
571EXPORT_SYMBOL_GPL(kvm_get_kvm);
572
573void kvm_put_kvm(struct kvm *kvm)
574{
575 if (atomic_dec_and_test(&kvm->users_count))
576 kvm_destroy_vm(kvm);
577}
578EXPORT_SYMBOL_GPL(kvm_put_kvm);
579
580
Avi Kivityf17abe92007-02-21 19:28:04 +0200581static int kvm_vm_release(struct inode *inode, struct file *filp)
582{
583 struct kvm *kvm = filp->private_data;
584
Gregory Haskins721eecb2009-05-20 10:30:49 -0400585 kvm_irqfd_release(kvm);
586
Izik Eidusd39f13b2008-03-30 16:01:25 +0300587 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 return 0;
589}
590
Heiko Carstensd48ead82011-01-17 21:21:08 +0100591#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900592/*
593 * Allocation size is twice as large as the actual dirty bitmap size.
594 * This makes it possible to do double buffering: see x86's
595 * kvm_vm_ioctl_get_dirty_log().
596 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900597static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
598{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900599 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900600
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900601 if (dirty_bytes > PAGE_SIZE)
602 memslot->dirty_bitmap = vzalloc(dirty_bytes);
603 else
604 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
605
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900606 if (!memslot->dirty_bitmap)
607 return -ENOMEM;
608
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900609 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900610 return 0;
611}
Heiko Carstensd48ead82011-01-17 21:21:08 +0100612#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900613
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 * Allocate some memory and give it an address in the guest physical address
616 * space.
617 *
618 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800619 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500620 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800622int __kvm_set_memory_region(struct kvm *kvm,
623 struct kvm_userspace_memory_region *mem,
624 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625{
Avi Kivity8234b222010-12-27 12:08:45 +0200626 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200628 unsigned long npages;
629 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630 struct kvm_memory_slot *memslot;
631 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200632 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633
634 r = -EINVAL;
635 /* General sanity checks */
636 if (mem->memory_size & (PAGE_SIZE - 1))
637 goto out;
638 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
639 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800640 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600641 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200642 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 goto out;
644 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
645 goto out;
646
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200647 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
649 npages = mem->memory_size >> PAGE_SHIFT;
650
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900651 r = -EINVAL;
652 if (npages > KVM_MEM_MAX_NR_PAGES)
653 goto out;
654
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 if (!npages)
656 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
657
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 new = old = *memslot;
659
Avi Kivitye36d96f2010-06-21 10:56:36 +0300660 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 new.base_gfn = base_gfn;
662 new.npages = npages;
663 new.flags = mem->flags;
664
665 /* Disallow changing a memory slot's size. */
666 r = -EINVAL;
667 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800668 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669
670 /* Check for overlaps */
671 r = -EEXIST;
672 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200673 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200675 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 continue;
677 if (!((base_gfn + npages <= s->base_gfn) ||
678 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800679 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682 /* Free page dirty bitmap if unneeded */
683 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000684 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685
686 r = -ENOMEM;
687
688 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200689#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500690 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900691 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200692
693 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800694 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200695
Izik Eidus80b14b52007-10-25 11:54:04 +0200696 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200697 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200699 if (!npages)
700 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300701
Joerg Roedelec04b262009-06-19 15:16:23 +0200702 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200703 unsigned long ugfn;
704 unsigned long j;
705 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200706 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300707
Joerg Roedelec04b262009-06-19 15:16:23 +0200708 /* Avoid unused variable warning if no large pages */
709 (void)level;
710
711 if (new.lpage_info[i])
712 continue;
713
Joerg Roedel82855412010-07-01 16:00:11 +0200714 lpages = 1 + ((base_gfn + npages - 1)
715 >> KVM_HPAGE_GFN_SHIFT(level));
716 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200717
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900718 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200719
720 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300721 goto out_free;
722
Joerg Roedel82855412010-07-01 16:00:11 +0200723 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200724 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200725 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200726 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300727 ugfn = new.userspace_addr >> PAGE_SHIFT;
728 /*
729 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300730 * other, or if explicitly asked to, disable large page
731 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300732 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200733 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300734 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200735 for (j = 0; j < lpages; ++j)
736 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300737 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738
Joerg Roedelec04b262009-06-19 15:16:23 +0200739skip_lpage:
740
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 /* Allocate page dirty bitmap if needed */
742 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900743 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800744 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200745 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200747#else /* not defined CONFIG_S390 */
748 new.user_alloc = user_alloc;
749 if (user_alloc)
750 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200751#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200753 if (!npages) {
754 r = -ENOMEM;
755 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
756 if (!slots)
757 goto out_free;
758 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
759 if (mem->slot >= slots->nmemslots)
760 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200761 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200762 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
763
764 old_memslots = kvm->memslots;
765 rcu_assign_pointer(kvm->memslots, slots);
766 synchronize_srcu_expedited(&kvm->srcu);
767 /* From this point no new shadow pages pointing to a deleted
768 * memslot will be created.
769 *
770 * validation of sp->gfn happens in:
771 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
772 * - kvm_is_visible_gfn (mmu_check_roots)
773 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300774 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200775 kfree(old_memslots);
776 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300777
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200778 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
779 if (r)
780 goto out_free;
781
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200782 /* map the pages in iommu page table */
783 if (npages) {
784 r = kvm_iommu_map_pages(kvm, &new);
785 if (r)
786 goto out_free;
787 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200788
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200789 r = -ENOMEM;
790 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
791 if (!slots)
792 goto out_free;
793 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
794 if (mem->slot >= slots->nmemslots)
795 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200796 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200797
798 /* actual memory is freed via old in kvm_free_physmem_slot below */
799 if (!npages) {
800 new.rmap = NULL;
801 new.dirty_bitmap = NULL;
802 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
803 new.lpage_info[i] = NULL;
804 }
805
806 slots->memslots[mem->slot] = new;
807 old_memslots = kvm->memslots;
808 rcu_assign_pointer(kvm->memslots, slots);
809 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200811 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800812
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200813 kvm_free_physmem_slot(&old, &new);
814 kfree(old_memslots);
815
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 return 0;
817
Sheng Yangf78e0e22007-10-29 09:40:42 +0800818out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 kvm_free_physmem_slot(&new, &old);
820out:
821 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200822
823}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800824EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
825
826int kvm_set_memory_region(struct kvm *kvm,
827 struct kvm_userspace_memory_region *mem,
828 int user_alloc)
829{
830 int r;
831
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200832 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800833 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200834 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800835 return r;
836}
Izik Eidus210c7c42007-10-24 23:52:57 +0200837EXPORT_SYMBOL_GPL(kvm_set_memory_region);
838
Carsten Otte1fe779f2007-10-29 16:08:35 +0100839int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
840 struct
841 kvm_userspace_memory_region *mem,
842 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200843{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200844 if (mem->slot >= KVM_MEMORY_SLOTS)
845 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200846 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847}
848
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800849int kvm_get_dirty_log(struct kvm *kvm,
850 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851{
852 struct kvm_memory_slot *memslot;
853 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900854 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 unsigned long any = 0;
856
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857 r = -EINVAL;
858 if (log->slot >= KVM_MEMORY_SLOTS)
859 goto out;
860
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200861 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 r = -ENOENT;
863 if (!memslot->dirty_bitmap)
864 goto out;
865
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900866 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867
Uri Lublincd1a4a92007-02-22 16:43:09 +0200868 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 any = memslot->dirty_bitmap[i];
870
871 r = -EFAULT;
872 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
873 goto out;
874
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800875 if (any)
876 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
878 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 return r;
881}
882
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300883void kvm_disable_largepages(void)
884{
885 largepages_enabled = false;
886}
887EXPORT_SYMBOL_GPL(kvm_disable_largepages);
888
Izik Eiduscea7bb22007-10-17 19:17:48 +0200889int is_error_page(struct page *page)
890{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300891 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200892}
893EXPORT_SYMBOL_GPL(is_error_page);
894
Anthony Liguori35149e22008-04-02 14:46:56 -0500895int is_error_pfn(pfn_t pfn)
896{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300897 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500898}
899EXPORT_SYMBOL_GPL(is_error_pfn);
900
Huang Yingbf998152010-05-31 14:28:19 +0800901int is_hwpoison_pfn(pfn_t pfn)
902{
903 return pfn == hwpoison_pfn;
904}
905EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
906
Gleb Natapovedba23e2010-07-07 20:16:45 +0300907int is_fault_pfn(pfn_t pfn)
908{
909 return pfn == fault_pfn;
910}
911EXPORT_SYMBOL_GPL(is_fault_pfn);
912
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200913static inline unsigned long bad_hva(void)
914{
915 return PAGE_OFFSET;
916}
917
918int kvm_is_error_hva(unsigned long addr)
919{
920 return addr == bad_hva();
921}
922EXPORT_SYMBOL_GPL(kvm_is_error_hva);
923
Gleb Natapov49c77542010-10-18 15:22:23 +0200924static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
925 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926{
927 int i;
928
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200929 for (i = 0; i < slots->nmemslots; ++i) {
930 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931
932 if (gfn >= memslot->base_gfn
933 && gfn < memslot->base_gfn + memslot->npages)
934 return memslot;
935 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000936 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937}
Gleb Natapov49c77542010-10-18 15:22:23 +0200938
939struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
940{
941 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
942}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300943EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Izik Eiduse0d62c72007-10-24 23:57:46 +0200945int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
946{
947 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800948 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200949
Izik Eiduse0d62c72007-10-24 23:57:46 +0200950 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200951 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200952
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200953 if (memslot->flags & KVM_MEMSLOT_INVALID)
954 continue;
955
Izik Eiduse0d62c72007-10-24 23:57:46 +0200956 if (gfn >= memslot->base_gfn
957 && gfn < memslot->base_gfn + memslot->npages)
958 return 1;
959 }
960 return 0;
961}
962EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
963
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100964unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
965{
966 struct vm_area_struct *vma;
967 unsigned long addr, size;
968
969 size = PAGE_SIZE;
970
971 addr = gfn_to_hva(kvm, gfn);
972 if (kvm_is_error_hva(addr))
973 return PAGE_SIZE;
974
975 down_read(&current->mm->mmap_sem);
976 vma = find_vma(current->mm, addr);
977 if (!vma)
978 goto out;
979
980 size = vma_kernel_pagesize(vma);
981
982out:
983 up_read(&current->mm->mmap_sem);
984
985 return size;
986}
987
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200988int memslot_id(struct kvm *kvm, gfn_t gfn)
989{
990 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800991 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200992 struct kvm_memory_slot *memslot = NULL;
993
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200994 for (i = 0; i < slots->nmemslots; ++i) {
995 memslot = &slots->memslots[i];
996
997 if (gfn >= memslot->base_gfn
998 && gfn < memslot->base_gfn + memslot->npages)
999 break;
1000 }
1001
1002 return memslot - slots->memslots;
1003}
1004
Gleb Natapov49c77542010-10-18 15:22:23 +02001005static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001006 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001007{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001008 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001009 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001010
1011 if (nr_pages)
1012 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1013
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001014 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001015}
Xiao Guangrong48987782010-08-22 19:11:43 +08001016
1017unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1018{
Gleb Natapov49c77542010-10-18 15:22:23 +02001019 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001020}
Sheng Yang0d150292008-04-25 21:44:50 +08001021EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001022
Gleb Natapov80300892010-10-19 18:13:41 +02001023static pfn_t get_fault_pfn(void)
1024{
1025 get_page(fault_page);
1026 return fault_pfn;
1027}
1028
Gleb Natapovaf585b92010-10-14 11:22:46 +02001029static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001030 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001031{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001032 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001033 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001034 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001035
Gleb Natapovaf585b92010-10-14 11:22:46 +02001036 /* we can do it either atomically or asynchronously, not both */
1037 BUG_ON(atomic && async);
1038
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001039 BUG_ON(!write_fault && !writable);
1040
1041 if (writable)
1042 *writable = true;
1043
Gleb Natapovaf585b92010-10-14 11:22:46 +02001044 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001045 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001046
1047 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001048 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001049
1050 if (writable)
1051 *writable = write_fault;
1052
1053 npages = get_user_pages_fast(addr, 1, write_fault, page);
1054
1055 /* map read fault as writable if possible */
1056 if (unlikely(!write_fault) && npages == 1) {
1057 struct page *wpage[1];
1058
1059 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1060 if (npages == 1) {
1061 *writable = true;
1062 put_page(page[0]);
1063 page[0] = wpage[0];
1064 }
1065 npages = 1;
1066 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001067 }
Izik Eidus539cb662007-11-11 22:05:04 +02001068
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001069 if (unlikely(npages != 1)) {
1070 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001071
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001072 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001073 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001074
Huang Yingbbeb3402010-06-22 14:23:11 +08001075 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001076 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001077 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001078 get_page(hwpoison_page);
1079 return page_to_pfn(hwpoison_page);
1080 }
1081
Gleb Natapov80300892010-10-19 18:13:41 +02001082 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001083
Gleb Natapov80300892010-10-19 18:13:41 +02001084 if (vma == NULL)
1085 pfn = get_fault_pfn();
1086 else if ((vma->vm_flags & VM_PFNMAP)) {
1087 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1088 vma->vm_pgoff;
1089 BUG_ON(!kvm_is_mmio_pfn(pfn));
1090 } else {
1091 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001092 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001093 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001094 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001095 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001096 } else
1097 pfn = page_to_pfn(page[0]);
1098
1099 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001100}
1101
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001102pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1103{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001104 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001105}
1106EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1107
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001108static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1109 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001110{
1111 unsigned long addr;
1112
Gleb Natapovaf585b92010-10-14 11:22:46 +02001113 if (async)
1114 *async = false;
1115
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001116 addr = gfn_to_hva(kvm, gfn);
1117 if (kvm_is_error_hva(addr)) {
1118 get_page(bad_page);
1119 return page_to_pfn(bad_page);
1120 }
1121
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001122 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001123}
1124
1125pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1126{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001127 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001128}
1129EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1130
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001131pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1132 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001133{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001134 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001135}
1136EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1137
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001138pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1139{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001140 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001141}
Anthony Liguori35149e22008-04-02 14:46:56 -05001142EXPORT_SYMBOL_GPL(gfn_to_pfn);
1143
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001144pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1145 bool *writable)
1146{
1147 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1148}
1149EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1150
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001151pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1152 struct kvm_memory_slot *slot, gfn_t gfn)
1153{
1154 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001155 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001156}
1157
Xiao Guangrong48987782010-08-22 19:11:43 +08001158int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1159 int nr_pages)
1160{
1161 unsigned long addr;
1162 gfn_t entry;
1163
Gleb Natapov49c77542010-10-18 15:22:23 +02001164 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001165 if (kvm_is_error_hva(addr))
1166 return -1;
1167
1168 if (entry < nr_pages)
1169 return 0;
1170
1171 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1172}
1173EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1174
Anthony Liguori35149e22008-04-02 14:46:56 -05001175struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1176{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001177 pfn_t pfn;
1178
1179 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001180 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001181 return pfn_to_page(pfn);
1182
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001183 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001184
1185 get_page(bad_page);
1186 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001187}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001188
Avi Kivity954bbbc22007-03-30 14:02:32 +03001189EXPORT_SYMBOL_GPL(gfn_to_page);
1190
Izik Eidusb4231d62007-11-20 11:49:33 +02001191void kvm_release_page_clean(struct page *page)
1192{
Anthony Liguori35149e22008-04-02 14:46:56 -05001193 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001194}
1195EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1196
Anthony Liguori35149e22008-04-02 14:46:56 -05001197void kvm_release_pfn_clean(pfn_t pfn)
1198{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001199 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001200 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001201}
1202EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1203
Izik Eidusb4231d62007-11-20 11:49:33 +02001204void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001205{
Anthony Liguori35149e22008-04-02 14:46:56 -05001206 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001207}
Izik Eidusb4231d62007-11-20 11:49:33 +02001208EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001209
Anthony Liguori35149e22008-04-02 14:46:56 -05001210void kvm_release_pfn_dirty(pfn_t pfn)
1211{
1212 kvm_set_pfn_dirty(pfn);
1213 kvm_release_pfn_clean(pfn);
1214}
1215EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1216
1217void kvm_set_page_dirty(struct page *page)
1218{
1219 kvm_set_pfn_dirty(page_to_pfn(page));
1220}
1221EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1222
1223void kvm_set_pfn_dirty(pfn_t pfn)
1224{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001225 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001226 struct page *page = pfn_to_page(pfn);
1227 if (!PageReserved(page))
1228 SetPageDirty(page);
1229 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001230}
1231EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1232
1233void kvm_set_pfn_accessed(pfn_t pfn)
1234{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001235 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001236 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001237}
1238EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1239
1240void kvm_get_pfn(pfn_t pfn)
1241{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001242 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001243 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001244}
1245EXPORT_SYMBOL_GPL(kvm_get_pfn);
1246
Izik Eidus195aefd2007-10-01 22:14:18 +02001247static int next_segment(unsigned long len, int offset)
1248{
1249 if (len > PAGE_SIZE - offset)
1250 return PAGE_SIZE - offset;
1251 else
1252 return len;
1253}
1254
1255int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1256 int len)
1257{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001258 int r;
1259 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001260
Izik Eiduse0506bc2007-11-11 22:10:22 +02001261 addr = gfn_to_hva(kvm, gfn);
1262 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001263 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001264 r = copy_from_user(data, (void __user *)addr + offset, len);
1265 if (r)
1266 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001267 return 0;
1268}
1269EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1270
1271int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1272{
1273 gfn_t gfn = gpa >> PAGE_SHIFT;
1274 int seg;
1275 int offset = offset_in_page(gpa);
1276 int ret;
1277
1278 while ((seg = next_segment(len, offset)) != 0) {
1279 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1280 if (ret < 0)
1281 return ret;
1282 offset = 0;
1283 len -= seg;
1284 data += seg;
1285 ++gfn;
1286 }
1287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(kvm_read_guest);
1290
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001291int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1292 unsigned long len)
1293{
1294 int r;
1295 unsigned long addr;
1296 gfn_t gfn = gpa >> PAGE_SHIFT;
1297 int offset = offset_in_page(gpa);
1298
1299 addr = gfn_to_hva(kvm, gfn);
1300 if (kvm_is_error_hva(addr))
1301 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001302 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001303 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001304 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001305 if (r)
1306 return -EFAULT;
1307 return 0;
1308}
1309EXPORT_SYMBOL(kvm_read_guest_atomic);
1310
Izik Eidus195aefd2007-10-01 22:14:18 +02001311int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1312 int offset, int len)
1313{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001314 int r;
1315 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001316
Izik Eiduse0506bc2007-11-11 22:10:22 +02001317 addr = gfn_to_hva(kvm, gfn);
1318 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001319 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001320 r = copy_to_user((void __user *)addr + offset, data, len);
1321 if (r)
1322 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001323 mark_page_dirty(kvm, gfn);
1324 return 0;
1325}
1326EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1327
1328int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1329 unsigned long len)
1330{
1331 gfn_t gfn = gpa >> PAGE_SHIFT;
1332 int seg;
1333 int offset = offset_in_page(gpa);
1334 int ret;
1335
1336 while ((seg = next_segment(len, offset)) != 0) {
1337 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1338 if (ret < 0)
1339 return ret;
1340 offset = 0;
1341 len -= seg;
1342 data += seg;
1343 ++gfn;
1344 }
1345 return 0;
1346}
1347
Gleb Natapov49c77542010-10-18 15:22:23 +02001348int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1349 gpa_t gpa)
1350{
1351 struct kvm_memslots *slots = kvm_memslots(kvm);
1352 int offset = offset_in_page(gpa);
1353 gfn_t gfn = gpa >> PAGE_SHIFT;
1354
1355 ghc->gpa = gpa;
1356 ghc->generation = slots->generation;
1357 ghc->memslot = __gfn_to_memslot(slots, gfn);
1358 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1359 if (!kvm_is_error_hva(ghc->hva))
1360 ghc->hva += offset;
1361 else
1362 return -EFAULT;
1363
1364 return 0;
1365}
1366EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1367
1368int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1369 void *data, unsigned long len)
1370{
1371 struct kvm_memslots *slots = kvm_memslots(kvm);
1372 int r;
1373
1374 if (slots->generation != ghc->generation)
1375 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1376
1377 if (kvm_is_error_hva(ghc->hva))
1378 return -EFAULT;
1379
1380 r = copy_to_user((void __user *)ghc->hva, data, len);
1381 if (r)
1382 return -EFAULT;
1383 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1384
1385 return 0;
1386}
1387EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1388
Izik Eidus195aefd2007-10-01 22:14:18 +02001389int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1390{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001391 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1392 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001393}
1394EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1395
1396int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1397{
1398 gfn_t gfn = gpa >> PAGE_SHIFT;
1399 int seg;
1400 int offset = offset_in_page(gpa);
1401 int ret;
1402
1403 while ((seg = next_segment(len, offset)) != 0) {
1404 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1405 if (ret < 0)
1406 return ret;
1407 offset = 0;
1408 len -= seg;
1409 ++gfn;
1410 }
1411 return 0;
1412}
1413EXPORT_SYMBOL_GPL(kvm_clear_guest);
1414
Gleb Natapov49c77542010-10-18 15:22:23 +02001415void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1416 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001418 if (memslot && memslot->dirty_bitmap) {
1419 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001421 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 }
1423}
1424
Gleb Natapov49c77542010-10-18 15:22:23 +02001425void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1426{
1427 struct kvm_memory_slot *memslot;
1428
1429 memslot = gfn_to_memslot(kvm, gfn);
1430 mark_page_dirty_in_slot(kvm, memslot, gfn);
1431}
1432
Eddie Dongb6958ce2007-07-18 12:15:21 +03001433/*
1434 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1435 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001436void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001437{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001438 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001439
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001440 for (;;) {
1441 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001442
Gleb Natapova1b37102009-07-09 15:33:52 +03001443 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001444 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001445 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001446 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001447 if (kvm_cpu_has_pending_timer(vcpu))
1448 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001449 if (signal_pending(current))
1450 break;
1451
Eddie Dongb6958ce2007-07-18 12:15:21 +03001452 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001453 }
1454
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001455 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001456}
1457
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458void kvm_resched(struct kvm_vcpu *vcpu)
1459{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001460 if (!need_resched())
1461 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463}
1464EXPORT_SYMBOL_GPL(kvm_resched);
1465
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001466void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1467{
1468 ktime_t expires;
1469 DEFINE_WAIT(wait);
1470
1471 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1472
1473 /* Sleep for 100 us, and hope lock-holder got scheduled */
1474 expires = ktime_add_ns(ktime_get(), 100000UL);
1475 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1476
1477 finish_wait(&vcpu->wq, &wait);
1478}
1479EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1480
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001481static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001482{
1483 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001484 struct page *page;
1485
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001486 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001487 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001488#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001489 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001490 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001491#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001492#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1493 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1494 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1495#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001496 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001497 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001498 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001499 vmf->page = page;
1500 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001501}
1502
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001503static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001504 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001505};
1506
1507static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1508{
1509 vma->vm_ops = &kvm_vcpu_vm_ops;
1510 return 0;
1511}
1512
Avi Kivitybccf2152007-02-21 18:04:26 +02001513static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1514{
1515 struct kvm_vcpu *vcpu = filp->private_data;
1516
Al Viro66c0b392008-04-19 20:33:56 +01001517 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001518 return 0;
1519}
1520
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001521static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001522 .release = kvm_vcpu_release,
1523 .unlocked_ioctl = kvm_vcpu_ioctl,
1524 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001525 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001526 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001527};
1528
1529/*
1530 * Allocates an inode for the vcpu.
1531 */
1532static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1533{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001534 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001535}
1536
Avi Kivityc5ea7662007-02-20 18:41:05 +02001537/*
1538 * Creates some virtual cpus. Good luck creating more than one.
1539 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001540static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001541{
1542 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001543 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001544
Gleb Natapov73880c82009-06-09 15:56:28 +03001545 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001546 if (IS_ERR(vcpu))
1547 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001548
Avi Kivity15ad7142007-07-11 18:17:21 +03001549 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1550
Avi Kivity26e52152007-11-20 15:30:24 +02001551 r = kvm_arch_vcpu_setup(vcpu);
1552 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001553 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001554
Shaohua Li11ec2802007-07-23 14:51:37 +08001555 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001556 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1557 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001558 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001559 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001560
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001561 kvm_for_each_vcpu(r, v, kvm)
1562 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001563 r = -EEXIST;
1564 goto vcpu_destroy;
1565 }
1566
1567 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001568
1569 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001570 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001571 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001572 if (r < 0) {
1573 kvm_put_kvm(kvm);
1574 goto vcpu_destroy;
1575 }
1576
1577 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1578 smp_wmb();
1579 atomic_inc(&kvm->online_vcpus);
1580
1581#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1582 if (kvm->bsp_vcpu_id == id)
1583 kvm->bsp_vcpu = vcpu;
1584#endif
1585 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001586 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001587
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001588vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001589 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001590 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001591 return r;
1592}
1593
Avi Kivity1961d272007-03-05 19:46:05 +02001594static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1595{
1596 if (sigset) {
1597 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1598 vcpu->sigset_active = 1;
1599 vcpu->sigset = *sigset;
1600 } else
1601 vcpu->sigset_active = 0;
1602 return 0;
1603}
1604
Avi Kivitybccf2152007-02-21 18:04:26 +02001605static long kvm_vcpu_ioctl(struct file *filp,
1606 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607{
Avi Kivitybccf2152007-02-21 18:04:26 +02001608 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001609 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001610 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001611 struct kvm_fpu *fpu = NULL;
1612 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001614 if (vcpu->kvm->mm != current->mm)
1615 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001616
1617#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1618 /*
1619 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1620 * so vcpu_load() would break it.
1621 */
1622 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1623 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1624#endif
1625
1626
1627 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001629 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001630 r = -EINVAL;
1631 if (arg)
1632 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001633 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001634 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001637 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001639 r = -ENOMEM;
1640 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1641 if (!kvm_regs)
1642 goto out;
1643 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001645 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001647 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1648 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001650out_free1:
1651 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652 break;
1653 }
1654 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001655 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001657 r = -ENOMEM;
1658 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1659 if (!kvm_regs)
1660 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001662 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1663 goto out_free2;
1664 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001666 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001668out_free2:
1669 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 break;
1671 }
1672 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001673 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1674 r = -ENOMEM;
1675 if (!kvm_sregs)
1676 goto out;
1677 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 if (r)
1679 goto out;
1680 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001681 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682 goto out;
1683 r = 0;
1684 break;
1685 }
1686 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001687 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1688 r = -ENOMEM;
1689 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001691 r = -EFAULT;
1692 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1693 goto out;
1694 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695 if (r)
1696 goto out;
1697 r = 0;
1698 break;
1699 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001700 case KVM_GET_MP_STATE: {
1701 struct kvm_mp_state mp_state;
1702
1703 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1704 if (r)
1705 goto out;
1706 r = -EFAULT;
1707 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1708 goto out;
1709 r = 0;
1710 break;
1711 }
1712 case KVM_SET_MP_STATE: {
1713 struct kvm_mp_state mp_state;
1714
1715 r = -EFAULT;
1716 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1717 goto out;
1718 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1719 if (r)
1720 goto out;
1721 r = 0;
1722 break;
1723 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 case KVM_TRANSLATE: {
1725 struct kvm_translation tr;
1726
1727 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001728 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001730 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 if (r)
1732 goto out;
1733 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001734 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 goto out;
1736 r = 0;
1737 break;
1738 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001739 case KVM_SET_GUEST_DEBUG: {
1740 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741
1742 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001743 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001745 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746 if (r)
1747 goto out;
1748 r = 0;
1749 break;
1750 }
Avi Kivity1961d272007-03-05 19:46:05 +02001751 case KVM_SET_SIGNAL_MASK: {
1752 struct kvm_signal_mask __user *sigmask_arg = argp;
1753 struct kvm_signal_mask kvm_sigmask;
1754 sigset_t sigset, *p;
1755
1756 p = NULL;
1757 if (argp) {
1758 r = -EFAULT;
1759 if (copy_from_user(&kvm_sigmask, argp,
1760 sizeof kvm_sigmask))
1761 goto out;
1762 r = -EINVAL;
1763 if (kvm_sigmask.len != sizeof sigset)
1764 goto out;
1765 r = -EFAULT;
1766 if (copy_from_user(&sigset, sigmask_arg->sigset,
1767 sizeof sigset))
1768 goto out;
1769 p = &sigset;
1770 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001771 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001772 break;
1773 }
Avi Kivityb8836732007-04-01 16:34:31 +03001774 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001775 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1776 r = -ENOMEM;
1777 if (!fpu)
1778 goto out;
1779 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001780 if (r)
1781 goto out;
1782 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001783 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001784 goto out;
1785 r = 0;
1786 break;
1787 }
1788 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001789 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1790 r = -ENOMEM;
1791 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001792 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001793 r = -EFAULT;
1794 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1795 goto out;
1796 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001797 if (r)
1798 goto out;
1799 r = 0;
1800 break;
1801 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001802 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001803 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001804 }
1805out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001806 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001807 kfree(fpu);
1808 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001809 return r;
1810}
1811
1812static long kvm_vm_ioctl(struct file *filp,
1813 unsigned int ioctl, unsigned long arg)
1814{
1815 struct kvm *kvm = filp->private_data;
1816 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001817 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001818
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001819 if (kvm->mm != current->mm)
1820 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001821 switch (ioctl) {
1822 case KVM_CREATE_VCPU:
1823 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1824 if (r < 0)
1825 goto out;
1826 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001827 case KVM_SET_USER_MEMORY_REGION: {
1828 struct kvm_userspace_memory_region kvm_userspace_mem;
1829
1830 r = -EFAULT;
1831 if (copy_from_user(&kvm_userspace_mem, argp,
1832 sizeof kvm_userspace_mem))
1833 goto out;
1834
1835 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 if (r)
1837 goto out;
1838 break;
1839 }
1840 case KVM_GET_DIRTY_LOG: {
1841 struct kvm_dirty_log log;
1842
1843 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001844 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001846 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 if (r)
1848 goto out;
1849 break;
1850 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001851#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1852 case KVM_REGISTER_COALESCED_MMIO: {
1853 struct kvm_coalesced_mmio_zone zone;
1854 r = -EFAULT;
1855 if (copy_from_user(&zone, argp, sizeof zone))
1856 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001857 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1858 if (r)
1859 goto out;
1860 r = 0;
1861 break;
1862 }
1863 case KVM_UNREGISTER_COALESCED_MMIO: {
1864 struct kvm_coalesced_mmio_zone zone;
1865 r = -EFAULT;
1866 if (copy_from_user(&zone, argp, sizeof zone))
1867 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001868 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1869 if (r)
1870 goto out;
1871 r = 0;
1872 break;
1873 }
1874#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001875 case KVM_IRQFD: {
1876 struct kvm_irqfd data;
1877
1878 r = -EFAULT;
1879 if (copy_from_user(&data, argp, sizeof data))
1880 goto out;
1881 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1882 break;
1883 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001884 case KVM_IOEVENTFD: {
1885 struct kvm_ioeventfd data;
1886
1887 r = -EFAULT;
1888 if (copy_from_user(&data, argp, sizeof data))
1889 goto out;
1890 r = kvm_ioeventfd(kvm, &data);
1891 break;
1892 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001893#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1894 case KVM_SET_BOOT_CPU_ID:
1895 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001896 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001897 if (atomic_read(&kvm->online_vcpus) != 0)
1898 r = -EBUSY;
1899 else
1900 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001901 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001902 break;
1903#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001904 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001905 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001906 if (r == -ENOTTY)
1907 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001908 }
1909out:
1910 return r;
1911}
1912
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001913#ifdef CONFIG_COMPAT
1914struct compat_kvm_dirty_log {
1915 __u32 slot;
1916 __u32 padding1;
1917 union {
1918 compat_uptr_t dirty_bitmap; /* one bit per page */
1919 __u64 padding2;
1920 };
1921};
1922
1923static long kvm_vm_compat_ioctl(struct file *filp,
1924 unsigned int ioctl, unsigned long arg)
1925{
1926 struct kvm *kvm = filp->private_data;
1927 int r;
1928
1929 if (kvm->mm != current->mm)
1930 return -EIO;
1931 switch (ioctl) {
1932 case KVM_GET_DIRTY_LOG: {
1933 struct compat_kvm_dirty_log compat_log;
1934 struct kvm_dirty_log log;
1935
1936 r = -EFAULT;
1937 if (copy_from_user(&compat_log, (void __user *)arg,
1938 sizeof(compat_log)))
1939 goto out;
1940 log.slot = compat_log.slot;
1941 log.padding1 = compat_log.padding1;
1942 log.padding2 = compat_log.padding2;
1943 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1944
1945 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1946 if (r)
1947 goto out;
1948 break;
1949 }
1950 default:
1951 r = kvm_vm_ioctl(filp, ioctl, arg);
1952 }
1953
1954out:
1955 return r;
1956}
1957#endif
1958
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001959static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001960{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001961 struct page *page[1];
1962 unsigned long addr;
1963 int npages;
1964 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001965 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001966
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001967 addr = gfn_to_hva(kvm, gfn);
1968 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001969 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001970
1971 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1972 NULL);
1973 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001974 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001975
1976 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001977 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001978}
1979
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001980static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001981 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001982};
1983
1984static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1985{
1986 vma->vm_ops = &kvm_vm_vm_ops;
1987 return 0;
1988}
1989
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001990static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001991 .release = kvm_vm_release,
1992 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001993#ifdef CONFIG_COMPAT
1994 .compat_ioctl = kvm_vm_compat_ioctl,
1995#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001996 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001997 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001998};
1999
2000static int kvm_dev_ioctl_create_vm(void)
2001{
Heiko Carstensaac87632010-10-27 17:22:10 +02002002 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002003 struct kvm *kvm;
2004
Avi Kivityf17abe92007-02-21 19:28:04 +02002005 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002006 if (IS_ERR(kvm))
2007 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002008#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2009 r = kvm_coalesced_mmio_init(kvm);
2010 if (r < 0) {
2011 kvm_put_kvm(kvm);
2012 return r;
2013 }
2014#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002015 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2016 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002017 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002018
Heiko Carstensaac87632010-10-27 17:22:10 +02002019 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002020}
2021
Avi Kivity1a811b62008-12-08 18:25:27 +02002022static long kvm_dev_ioctl_check_extension_generic(long arg)
2023{
2024 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002025 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002026 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002027 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002028#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2029 case KVM_CAP_SET_BOOT_CPU_ID:
2030#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002031 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002032 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002033#ifdef CONFIG_HAVE_KVM_IRQCHIP
2034 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002035 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002036#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002037 default:
2038 break;
2039 }
2040 return kvm_dev_ioctl_check_extension(arg);
2041}
2042
Avi Kivityf17abe92007-02-21 19:28:04 +02002043static long kvm_dev_ioctl(struct file *filp,
2044 unsigned int ioctl, unsigned long arg)
2045{
Avi Kivity07c45a32007-03-07 13:05:38 +02002046 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002047
2048 switch (ioctl) {
2049 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002050 r = -EINVAL;
2051 if (arg)
2052 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002053 r = KVM_API_VERSION;
2054 break;
2055 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002056 r = -EINVAL;
2057 if (arg)
2058 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002059 r = kvm_dev_ioctl_create_vm();
2060 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002061 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002062 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002063 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002064 case KVM_GET_VCPU_MMAP_SIZE:
2065 r = -EINVAL;
2066 if (arg)
2067 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002068 r = PAGE_SIZE; /* struct kvm_run */
2069#ifdef CONFIG_X86
2070 r += PAGE_SIZE; /* pio data page */
2071#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002072#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2073 r += PAGE_SIZE; /* coalesced mmio ring page */
2074#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002075 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002076 case KVM_TRACE_ENABLE:
2077 case KVM_TRACE_PAUSE:
2078 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002079 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002080 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002082 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083 }
2084out:
2085 return r;
2086}
2087
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089 .unlocked_ioctl = kvm_dev_ioctl,
2090 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002091 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092};
2093
2094static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002095 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 "kvm",
2097 &kvm_chardev_ops,
2098};
2099
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002100static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002101{
2102 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002103 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002104
Rusty Russell7f59f492008-12-07 21:25:45 +10302105 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002106 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002107
Rusty Russell7f59f492008-12-07 21:25:45 +10302108 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002109
2110 r = kvm_arch_hardware_enable(NULL);
2111
2112 if (r) {
2113 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2114 atomic_inc(&hardware_enable_failed);
2115 printk(KERN_INFO "kvm: enabling virtualization on "
2116 "CPU%d failed\n", cpu);
2117 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002118}
2119
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002120static void hardware_enable(void *junk)
2121{
2122 spin_lock(&kvm_lock);
2123 hardware_enable_nolock(junk);
2124 spin_unlock(&kvm_lock);
2125}
2126
2127static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002128{
2129 int cpu = raw_smp_processor_id();
2130
Rusty Russell7f59f492008-12-07 21:25:45 +10302131 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002132 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302133 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002134 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002135}
2136
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002137static void hardware_disable(void *junk)
2138{
2139 spin_lock(&kvm_lock);
2140 hardware_disable_nolock(junk);
2141 spin_unlock(&kvm_lock);
2142}
2143
Alexander Graf10474ae2009-09-15 11:37:46 +02002144static void hardware_disable_all_nolock(void)
2145{
2146 BUG_ON(!kvm_usage_count);
2147
2148 kvm_usage_count--;
2149 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002150 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002151}
2152
2153static void hardware_disable_all(void)
2154{
2155 spin_lock(&kvm_lock);
2156 hardware_disable_all_nolock();
2157 spin_unlock(&kvm_lock);
2158}
2159
2160static int hardware_enable_all(void)
2161{
2162 int r = 0;
2163
2164 spin_lock(&kvm_lock);
2165
2166 kvm_usage_count++;
2167 if (kvm_usage_count == 1) {
2168 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002169 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002170
2171 if (atomic_read(&hardware_enable_failed)) {
2172 hardware_disable_all_nolock();
2173 r = -EBUSY;
2174 }
2175 }
2176
2177 spin_unlock(&kvm_lock);
2178
2179 return r;
2180}
2181
Avi Kivity774c47f2007-02-12 00:54:47 -08002182static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2183 void *v)
2184{
2185 int cpu = (long)v;
2186
Alexander Graf10474ae2009-09-15 11:37:46 +02002187 if (!kvm_usage_count)
2188 return NOTIFY_OK;
2189
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002190 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002191 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002192 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002193 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2194 cpu);
2195 hardware_disable(NULL);
2196 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002197 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002198 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2199 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002200 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002201 break;
2202 }
2203 return NOTIFY_OK;
2204}
2205
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002206
Avi Kivityb7c41452010-12-02 17:52:50 +02002207asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002208{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002209 /* Fault while not rebooting. We want the trace. */
2210 BUG();
2211}
Avi Kivityb7c41452010-12-02 17:52:50 +02002212EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002213
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002214static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002215 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002216{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002217 /*
2218 * Some (well, at least mine) BIOSes hang on reboot if
2219 * in vmx root mode.
2220 *
2221 * And Intel TXT required VMX off for all cpu when system shutdown.
2222 */
2223 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2224 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002225 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002226 return NOTIFY_OK;
2227}
2228
2229static struct notifier_block kvm_reboot_notifier = {
2230 .notifier_call = kvm_reboot,
2231 .priority = 0,
2232};
2233
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002234static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002235{
2236 int i;
2237
2238 for (i = 0; i < bus->dev_count; i++) {
2239 struct kvm_io_device *pos = bus->devs[i];
2240
2241 kvm_iodevice_destructor(pos);
2242 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002243 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002244}
2245
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002246/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002247int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002248 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002249{
2250 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002251 struct kvm_io_bus *bus;
2252
2253 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002254 for (i = 0; i < bus->dev_count; i++)
2255 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2256 return 0;
2257 return -EOPNOTSUPP;
2258}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002259
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002260/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002261int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2262 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002263{
2264 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002265 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002266
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002267 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002268 for (i = 0; i < bus->dev_count; i++)
2269 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2270 return 0;
2271 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002272}
2273
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002274/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002275int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2276 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002277{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002278 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002279
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002280 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002281 if (bus->dev_count > NR_IOBUS_DEVS-1)
2282 return -ENOSPC;
2283
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002284 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2285 if (!new_bus)
2286 return -ENOMEM;
2287 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2288 new_bus->devs[new_bus->dev_count++] = dev;
2289 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2290 synchronize_srcu_expedited(&kvm->srcu);
2291 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002292
2293 return 0;
2294}
2295
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002296/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002297int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2298 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002299{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002300 int i, r;
2301 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002302
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002303 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2304 if (!new_bus)
2305 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002306
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002307 bus = kvm->buses[bus_idx];
2308 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2309
2310 r = -ENOENT;
2311 for (i = 0; i < new_bus->dev_count; i++)
2312 if (new_bus->devs[i] == dev) {
2313 r = 0;
2314 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002315 break;
2316 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002317
2318 if (r) {
2319 kfree(new_bus);
2320 return r;
2321 }
2322
2323 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2324 synchronize_srcu_expedited(&kvm->srcu);
2325 kfree(bus);
2326 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002327}
2328
Avi Kivity774c47f2007-02-12 00:54:47 -08002329static struct notifier_block kvm_cpu_notifier = {
2330 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002331};
2332
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002333static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002334{
2335 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002336 struct kvm *kvm;
2337
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002338 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002339 spin_lock(&kvm_lock);
2340 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002341 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002342 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002343 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002344}
2345
2346DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2347
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002348static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002349{
2350 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002351 struct kvm *kvm;
2352 struct kvm_vcpu *vcpu;
2353 int i;
2354
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002355 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002356 spin_lock(&kvm_lock);
2357 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002358 kvm_for_each_vcpu(i, vcpu, kvm)
2359 *val += *(u32 *)((void *)vcpu + offset);
2360
Avi Kivity1165f5f2007-04-19 17:27:43 +03002361 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002362 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002363}
2364
Avi Kivityba1389b2007-11-18 16:24:12 +02002365DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2366
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002367static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002368 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2369 [KVM_STAT_VM] = &vm_stat_fops,
2370};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002371
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002372static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373{
2374 struct kvm_stats_debugfs_item *p;
2375
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002376 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002378 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002379 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002380 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381}
2382
2383static void kvm_exit_debug(void)
2384{
2385 struct kvm_stats_debugfs_item *p;
2386
2387 for (p = debugfs_entries; p->name; ++p)
2388 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002389 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390}
2391
Avi Kivity59ae6c62007-02-12 00:54:48 -08002392static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2393{
Alexander Graf10474ae2009-09-15 11:37:46 +02002394 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002395 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002396 return 0;
2397}
2398
2399static int kvm_resume(struct sys_device *dev)
2400{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002401 if (kvm_usage_count) {
2402 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002403 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002404 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002405 return 0;
2406}
2407
2408static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002409 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002410 .suspend = kvm_suspend,
2411 .resume = kvm_resume,
2412};
2413
2414static struct sys_device kvm_sysdev = {
2415 .id = 0,
2416 .cls = &kvm_sysdev_class,
2417};
2418
Izik Eiduscea7bb22007-10-17 19:17:48 +02002419struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002420pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421
Avi Kivity15ad7142007-07-11 18:17:21 +03002422static inline
2423struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2424{
2425 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2426}
2427
2428static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2429{
2430 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2431
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002432 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002433}
2434
2435static void kvm_sched_out(struct preempt_notifier *pn,
2436 struct task_struct *next)
2437{
2438 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2439
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002440 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002441}
2442
Avi Kivity0ee75be2010-04-28 15:39:01 +03002443int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002444 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445{
2446 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002447 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002449 r = kvm_arch_init(opaque);
2450 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002451 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002452
2453 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2454
2455 if (bad_page == NULL) {
2456 r = -ENOMEM;
2457 goto out;
2458 }
2459
Anthony Liguori35149e22008-04-02 14:46:56 -05002460 bad_pfn = page_to_pfn(bad_page);
2461
Huang Yingbf998152010-05-31 14:28:19 +08002462 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2463
2464 if (hwpoison_page == NULL) {
2465 r = -ENOMEM;
2466 goto out_free_0;
2467 }
2468
2469 hwpoison_pfn = page_to_pfn(hwpoison_page);
2470
Gleb Natapovedba23e2010-07-07 20:16:45 +03002471 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2472
2473 if (fault_page == NULL) {
2474 r = -ENOMEM;
2475 goto out_free_0;
2476 }
2477
2478 fault_pfn = page_to_pfn(fault_page);
2479
Avi Kivity8437a612009-06-06 14:52:35 -07002480 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302481 r = -ENOMEM;
2482 goto out_free_0;
2483 }
2484
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002485 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302487 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488
Yang, Sheng002c7f72007-07-31 14:23:01 +03002489 for_each_online_cpu(cpu) {
2490 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002491 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002492 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002493 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002494 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002495 }
2496
Avi Kivity774c47f2007-02-12 00:54:47 -08002497 r = register_cpu_notifier(&kvm_cpu_notifier);
2498 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002499 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 register_reboot_notifier(&kvm_reboot_notifier);
2501
Avi Kivity59ae6c62007-02-12 00:54:48 -08002502 r = sysdev_class_register(&kvm_sysdev_class);
2503 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002504 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002505
2506 r = sysdev_register(&kvm_sysdev);
2507 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002508 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002509
Rusty Russellc16f8622007-07-30 21:12:19 +10002510 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002511 if (!vcpu_align)
2512 vcpu_align = __alignof__(struct kvm_vcpu);
2513 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002514 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002515 if (!kvm_vcpu_cache) {
2516 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002517 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002518 }
2519
Gleb Natapovaf585b92010-10-14 11:22:46 +02002520 r = kvm_async_pf_init();
2521 if (r)
2522 goto out_free;
2523
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002525 kvm_vm_fops.owner = module;
2526 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527
2528 r = misc_register(&kvm_dev);
2529 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002530 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002531 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 }
2533
Avi Kivity15ad7142007-07-11 18:17:21 +03002534 kvm_preempt_ops.sched_in = kvm_sched_in;
2535 kvm_preempt_ops.sched_out = kvm_sched_out;
2536
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002537 kvm_init_debug();
2538
Avi Kivityc7addb92007-09-16 18:58:32 +02002539 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540
Gleb Natapovaf585b92010-10-14 11:22:46 +02002541out_unreg:
2542 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002544 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002545out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002546 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002547out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002548 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002549out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002551 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002552out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002553out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002554 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302555out_free_0a:
2556 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002557out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002558 if (fault_page)
2559 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002560 if (hwpoison_page)
2561 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002562 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002563out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002564 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002565out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566 return r;
2567}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002568EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002570void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002572 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002574 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002575 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002576 sysdev_unregister(&kvm_sysdev);
2577 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002579 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002580 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002581 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002582 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302583 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002584 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002585 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002587EXPORT_SYMBOL_GPL(kvm_exit);