blob: a3a8f5fb369e58d4db3881219149bad5631f0e07 [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
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900591/*
592 * Allocation size is twice as large as the actual dirty bitmap size.
593 * This makes it possible to do double buffering: see x86's
594 * kvm_vm_ioctl_get_dirty_log().
595 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900596static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
597{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900598 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900599
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900600 if (dirty_bytes > PAGE_SIZE)
601 memslot->dirty_bitmap = vzalloc(dirty_bytes);
602 else
603 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
604
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900605 if (!memslot->dirty_bitmap)
606 return -ENOMEM;
607
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900608 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900609 return 0;
610}
611
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613 * Allocate some memory and give it an address in the guest physical address
614 * space.
615 *
616 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800617 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500618 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800620int __kvm_set_memory_region(struct kvm *kvm,
621 struct kvm_userspace_memory_region *mem,
622 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623{
Avi Kivity8234b222010-12-27 12:08:45 +0200624 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200626 unsigned long npages;
627 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628 struct kvm_memory_slot *memslot;
629 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200630 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
632 r = -EINVAL;
633 /* General sanity checks */
634 if (mem->memory_size & (PAGE_SIZE - 1))
635 goto out;
636 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
637 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800638 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600639 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200640 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 goto out;
642 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
643 goto out;
644
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200645 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
647 npages = mem->memory_size >> PAGE_SHIFT;
648
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900649 r = -EINVAL;
650 if (npages > KVM_MEM_MAX_NR_PAGES)
651 goto out;
652
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 if (!npages)
654 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
655
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 new = old = *memslot;
657
Avi Kivitye36d96f2010-06-21 10:56:36 +0300658 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 new.base_gfn = base_gfn;
660 new.npages = npages;
661 new.flags = mem->flags;
662
663 /* Disallow changing a memory slot's size. */
664 r = -EINVAL;
665 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800666 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667
668 /* Check for overlaps */
669 r = -EEXIST;
670 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200671 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200673 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674 continue;
675 if (!((base_gfn + npages <= s->base_gfn) ||
676 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800677 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 /* Free page dirty bitmap if unneeded */
681 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000682 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
684 r = -ENOMEM;
685
686 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200687#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500688 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900689 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200690
691 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800692 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200693
Izik Eidus80b14b52007-10-25 11:54:04 +0200694 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200695 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200697 if (!npages)
698 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300699
Joerg Roedelec04b262009-06-19 15:16:23 +0200700 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200701 unsigned long ugfn;
702 unsigned long j;
703 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200704 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300705
Joerg Roedelec04b262009-06-19 15:16:23 +0200706 /* Avoid unused variable warning if no large pages */
707 (void)level;
708
709 if (new.lpage_info[i])
710 continue;
711
Joerg Roedel82855412010-07-01 16:00:11 +0200712 lpages = 1 + ((base_gfn + npages - 1)
713 >> KVM_HPAGE_GFN_SHIFT(level));
714 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200715
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900716 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200717
718 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300719 goto out_free;
720
Joerg Roedel82855412010-07-01 16:00:11 +0200721 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200722 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200723 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200724 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300725 ugfn = new.userspace_addr >> PAGE_SHIFT;
726 /*
727 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300728 * other, or if explicitly asked to, disable large page
729 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300730 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200731 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300732 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200733 for (j = 0; j < lpages; ++j)
734 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300735 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736
Joerg Roedelec04b262009-06-19 15:16:23 +0200737skip_lpage:
738
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 /* Allocate page dirty bitmap if needed */
740 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900741 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800742 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200743 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200745#else /* not defined CONFIG_S390 */
746 new.user_alloc = user_alloc;
747 if (user_alloc)
748 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200749#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200751 if (!npages) {
752 r = -ENOMEM;
753 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
754 if (!slots)
755 goto out_free;
756 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
757 if (mem->slot >= slots->nmemslots)
758 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200759 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200760 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
761
762 old_memslots = kvm->memslots;
763 rcu_assign_pointer(kvm->memslots, slots);
764 synchronize_srcu_expedited(&kvm->srcu);
765 /* From this point no new shadow pages pointing to a deleted
766 * memslot will be created.
767 *
768 * validation of sp->gfn happens in:
769 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
770 * - kvm_is_visible_gfn (mmu_check_roots)
771 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300772 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200773 kfree(old_memslots);
774 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300775
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200776 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
777 if (r)
778 goto out_free;
779
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200780 /* map the pages in iommu page table */
781 if (npages) {
782 r = kvm_iommu_map_pages(kvm, &new);
783 if (r)
784 goto out_free;
785 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200786
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200787 r = -ENOMEM;
788 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
789 if (!slots)
790 goto out_free;
791 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
792 if (mem->slot >= slots->nmemslots)
793 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200794 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200795
796 /* actual memory is freed via old in kvm_free_physmem_slot below */
797 if (!npages) {
798 new.rmap = NULL;
799 new.dirty_bitmap = NULL;
800 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
801 new.lpage_info[i] = NULL;
802 }
803
804 slots->memslots[mem->slot] = new;
805 old_memslots = kvm->memslots;
806 rcu_assign_pointer(kvm->memslots, slots);
807 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200809 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800810
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200811 kvm_free_physmem_slot(&old, &new);
812 kfree(old_memslots);
813
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 return 0;
815
Sheng Yangf78e0e22007-10-29 09:40:42 +0800816out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 kvm_free_physmem_slot(&new, &old);
818out:
819 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200820
821}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800822EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
823
824int kvm_set_memory_region(struct kvm *kvm,
825 struct kvm_userspace_memory_region *mem,
826 int user_alloc)
827{
828 int r;
829
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200830 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800831 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200832 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800833 return r;
834}
Izik Eidus210c7c42007-10-24 23:52:57 +0200835EXPORT_SYMBOL_GPL(kvm_set_memory_region);
836
Carsten Otte1fe779f2007-10-29 16:08:35 +0100837int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
838 struct
839 kvm_userspace_memory_region *mem,
840 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200841{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200842 if (mem->slot >= KVM_MEMORY_SLOTS)
843 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200844 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845}
846
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800847int kvm_get_dirty_log(struct kvm *kvm,
848 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849{
850 struct kvm_memory_slot *memslot;
851 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900852 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 unsigned long any = 0;
854
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 r = -EINVAL;
856 if (log->slot >= KVM_MEMORY_SLOTS)
857 goto out;
858
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200859 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 r = -ENOENT;
861 if (!memslot->dirty_bitmap)
862 goto out;
863
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900864 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865
Uri Lublincd1a4a92007-02-22 16:43:09 +0200866 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 any = memslot->dirty_bitmap[i];
868
869 r = -EFAULT;
870 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
871 goto out;
872
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800873 if (any)
874 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
876 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 return r;
879}
880
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300881void kvm_disable_largepages(void)
882{
883 largepages_enabled = false;
884}
885EXPORT_SYMBOL_GPL(kvm_disable_largepages);
886
Izik Eiduscea7bb22007-10-17 19:17:48 +0200887int is_error_page(struct page *page)
888{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300889 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200890}
891EXPORT_SYMBOL_GPL(is_error_page);
892
Anthony Liguori35149e22008-04-02 14:46:56 -0500893int is_error_pfn(pfn_t pfn)
894{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300895 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500896}
897EXPORT_SYMBOL_GPL(is_error_pfn);
898
Huang Yingbf998152010-05-31 14:28:19 +0800899int is_hwpoison_pfn(pfn_t pfn)
900{
901 return pfn == hwpoison_pfn;
902}
903EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
904
Gleb Natapovedba23e2010-07-07 20:16:45 +0300905int is_fault_pfn(pfn_t pfn)
906{
907 return pfn == fault_pfn;
908}
909EXPORT_SYMBOL_GPL(is_fault_pfn);
910
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200911static inline unsigned long bad_hva(void)
912{
913 return PAGE_OFFSET;
914}
915
916int kvm_is_error_hva(unsigned long addr)
917{
918 return addr == bad_hva();
919}
920EXPORT_SYMBOL_GPL(kvm_is_error_hva);
921
Gleb Natapov49c77542010-10-18 15:22:23 +0200922static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
923 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924{
925 int i;
926
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200927 for (i = 0; i < slots->nmemslots; ++i) {
928 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
930 if (gfn >= memslot->base_gfn
931 && gfn < memslot->base_gfn + memslot->npages)
932 return memslot;
933 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000934 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935}
Gleb Natapov49c77542010-10-18 15:22:23 +0200936
937struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
938{
939 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
940}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300941EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942
Izik Eiduse0d62c72007-10-24 23:57:46 +0200943int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
944{
945 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800946 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200947
Izik Eiduse0d62c72007-10-24 23:57:46 +0200948 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200949 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200950
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200951 if (memslot->flags & KVM_MEMSLOT_INVALID)
952 continue;
953
Izik Eiduse0d62c72007-10-24 23:57:46 +0200954 if (gfn >= memslot->base_gfn
955 && gfn < memslot->base_gfn + memslot->npages)
956 return 1;
957 }
958 return 0;
959}
960EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
961
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100962unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
963{
964 struct vm_area_struct *vma;
965 unsigned long addr, size;
966
967 size = PAGE_SIZE;
968
969 addr = gfn_to_hva(kvm, gfn);
970 if (kvm_is_error_hva(addr))
971 return PAGE_SIZE;
972
973 down_read(&current->mm->mmap_sem);
974 vma = find_vma(current->mm, addr);
975 if (!vma)
976 goto out;
977
978 size = vma_kernel_pagesize(vma);
979
980out:
981 up_read(&current->mm->mmap_sem);
982
983 return size;
984}
985
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200986int memslot_id(struct kvm *kvm, gfn_t gfn)
987{
988 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800989 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200990 struct kvm_memory_slot *memslot = NULL;
991
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200992 for (i = 0; i < slots->nmemslots; ++i) {
993 memslot = &slots->memslots[i];
994
995 if (gfn >= memslot->base_gfn
996 && gfn < memslot->base_gfn + memslot->npages)
997 break;
998 }
999
1000 return memslot - slots->memslots;
1001}
1002
Gleb Natapov49c77542010-10-18 15:22:23 +02001003static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001004 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001005{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001006 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001007 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001008
1009 if (nr_pages)
1010 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1011
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001012 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001013}
Xiao Guangrong48987782010-08-22 19:11:43 +08001014
1015unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1016{
Gleb Natapov49c77542010-10-18 15:22:23 +02001017 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001018}
Sheng Yang0d150292008-04-25 21:44:50 +08001019EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001020
Gleb Natapov80300892010-10-19 18:13:41 +02001021static pfn_t get_fault_pfn(void)
1022{
1023 get_page(fault_page);
1024 return fault_pfn;
1025}
1026
Gleb Natapovaf585b92010-10-14 11:22:46 +02001027static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001028 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001029{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001030 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001031 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001032 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001033
Gleb Natapovaf585b92010-10-14 11:22:46 +02001034 /* we can do it either atomically or asynchronously, not both */
1035 BUG_ON(atomic && async);
1036
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001037 BUG_ON(!write_fault && !writable);
1038
1039 if (writable)
1040 *writable = true;
1041
Gleb Natapovaf585b92010-10-14 11:22:46 +02001042 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001043 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001044
1045 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001046 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001047
1048 if (writable)
1049 *writable = write_fault;
1050
1051 npages = get_user_pages_fast(addr, 1, write_fault, page);
1052
1053 /* map read fault as writable if possible */
1054 if (unlikely(!write_fault) && npages == 1) {
1055 struct page *wpage[1];
1056
1057 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1058 if (npages == 1) {
1059 *writable = true;
1060 put_page(page[0]);
1061 page[0] = wpage[0];
1062 }
1063 npages = 1;
1064 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001065 }
Izik Eidus539cb662007-11-11 22:05:04 +02001066
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001067 if (unlikely(npages != 1)) {
1068 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001069
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001070 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001071 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001072
Huang Yingbbeb3402010-06-22 14:23:11 +08001073 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001074 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001075 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001076 get_page(hwpoison_page);
1077 return page_to_pfn(hwpoison_page);
1078 }
1079
Gleb Natapov80300892010-10-19 18:13:41 +02001080 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001081
Gleb Natapov80300892010-10-19 18:13:41 +02001082 if (vma == NULL)
1083 pfn = get_fault_pfn();
1084 else if ((vma->vm_flags & VM_PFNMAP)) {
1085 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1086 vma->vm_pgoff;
1087 BUG_ON(!kvm_is_mmio_pfn(pfn));
1088 } else {
1089 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001090 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001091 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001092 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001093 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001094 } else
1095 pfn = page_to_pfn(page[0]);
1096
1097 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001098}
1099
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001100pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1101{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001102 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001103}
1104EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1105
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001106static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1107 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001108{
1109 unsigned long addr;
1110
Gleb Natapovaf585b92010-10-14 11:22:46 +02001111 if (async)
1112 *async = false;
1113
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001114 addr = gfn_to_hva(kvm, gfn);
1115 if (kvm_is_error_hva(addr)) {
1116 get_page(bad_page);
1117 return page_to_pfn(bad_page);
1118 }
1119
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001120 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001121}
1122
1123pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1124{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001125 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001126}
1127EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1128
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001129pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1130 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001131{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001132 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001133}
1134EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1135
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001136pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1137{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001138 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001139}
Anthony Liguori35149e22008-04-02 14:46:56 -05001140EXPORT_SYMBOL_GPL(gfn_to_pfn);
1141
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001142pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1143 bool *writable)
1144{
1145 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1146}
1147EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1148
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001149pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1150 struct kvm_memory_slot *slot, gfn_t gfn)
1151{
1152 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001153 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001154}
1155
Xiao Guangrong48987782010-08-22 19:11:43 +08001156int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1157 int nr_pages)
1158{
1159 unsigned long addr;
1160 gfn_t entry;
1161
Gleb Natapov49c77542010-10-18 15:22:23 +02001162 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001163 if (kvm_is_error_hva(addr))
1164 return -1;
1165
1166 if (entry < nr_pages)
1167 return 0;
1168
1169 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1170}
1171EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1172
Anthony Liguori35149e22008-04-02 14:46:56 -05001173struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1174{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001175 pfn_t pfn;
1176
1177 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001178 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001179 return pfn_to_page(pfn);
1180
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001181 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001182
1183 get_page(bad_page);
1184 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001185}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001186
Avi Kivity954bbbc22007-03-30 14:02:32 +03001187EXPORT_SYMBOL_GPL(gfn_to_page);
1188
Izik Eidusb4231d62007-11-20 11:49:33 +02001189void kvm_release_page_clean(struct page *page)
1190{
Anthony Liguori35149e22008-04-02 14:46:56 -05001191 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001192}
1193EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1194
Anthony Liguori35149e22008-04-02 14:46:56 -05001195void kvm_release_pfn_clean(pfn_t pfn)
1196{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001197 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001198 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001199}
1200EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1201
Izik Eidusb4231d62007-11-20 11:49:33 +02001202void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001203{
Anthony Liguori35149e22008-04-02 14:46:56 -05001204 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001205}
Izik Eidusb4231d62007-11-20 11:49:33 +02001206EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001207
Anthony Liguori35149e22008-04-02 14:46:56 -05001208void kvm_release_pfn_dirty(pfn_t pfn)
1209{
1210 kvm_set_pfn_dirty(pfn);
1211 kvm_release_pfn_clean(pfn);
1212}
1213EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1214
1215void kvm_set_page_dirty(struct page *page)
1216{
1217 kvm_set_pfn_dirty(page_to_pfn(page));
1218}
1219EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1220
1221void kvm_set_pfn_dirty(pfn_t pfn)
1222{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001223 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001224 struct page *page = pfn_to_page(pfn);
1225 if (!PageReserved(page))
1226 SetPageDirty(page);
1227 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001228}
1229EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1230
1231void kvm_set_pfn_accessed(pfn_t pfn)
1232{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001233 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001234 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001235}
1236EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1237
1238void kvm_get_pfn(pfn_t pfn)
1239{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001240 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001241 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001242}
1243EXPORT_SYMBOL_GPL(kvm_get_pfn);
1244
Izik Eidus195aefd2007-10-01 22:14:18 +02001245static int next_segment(unsigned long len, int offset)
1246{
1247 if (len > PAGE_SIZE - offset)
1248 return PAGE_SIZE - offset;
1249 else
1250 return len;
1251}
1252
1253int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1254 int len)
1255{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001256 int r;
1257 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001258
Izik Eiduse0506bc2007-11-11 22:10:22 +02001259 addr = gfn_to_hva(kvm, gfn);
1260 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001261 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001262 r = copy_from_user(data, (void __user *)addr + offset, len);
1263 if (r)
1264 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001265 return 0;
1266}
1267EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1268
1269int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1270{
1271 gfn_t gfn = gpa >> PAGE_SHIFT;
1272 int seg;
1273 int offset = offset_in_page(gpa);
1274 int ret;
1275
1276 while ((seg = next_segment(len, offset)) != 0) {
1277 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1278 if (ret < 0)
1279 return ret;
1280 offset = 0;
1281 len -= seg;
1282 data += seg;
1283 ++gfn;
1284 }
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(kvm_read_guest);
1288
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001289int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1290 unsigned long len)
1291{
1292 int r;
1293 unsigned long addr;
1294 gfn_t gfn = gpa >> PAGE_SHIFT;
1295 int offset = offset_in_page(gpa);
1296
1297 addr = gfn_to_hva(kvm, gfn);
1298 if (kvm_is_error_hva(addr))
1299 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001300 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001301 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001302 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001303 if (r)
1304 return -EFAULT;
1305 return 0;
1306}
1307EXPORT_SYMBOL(kvm_read_guest_atomic);
1308
Izik Eidus195aefd2007-10-01 22:14:18 +02001309int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1310 int offset, int len)
1311{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001312 int r;
1313 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001314
Izik Eiduse0506bc2007-11-11 22:10:22 +02001315 addr = gfn_to_hva(kvm, gfn);
1316 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001317 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001318 r = copy_to_user((void __user *)addr + offset, data, len);
1319 if (r)
1320 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001321 mark_page_dirty(kvm, gfn);
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1325
1326int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1327 unsigned long len)
1328{
1329 gfn_t gfn = gpa >> PAGE_SHIFT;
1330 int seg;
1331 int offset = offset_in_page(gpa);
1332 int ret;
1333
1334 while ((seg = next_segment(len, offset)) != 0) {
1335 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1336 if (ret < 0)
1337 return ret;
1338 offset = 0;
1339 len -= seg;
1340 data += seg;
1341 ++gfn;
1342 }
1343 return 0;
1344}
1345
Gleb Natapov49c77542010-10-18 15:22:23 +02001346int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1347 gpa_t gpa)
1348{
1349 struct kvm_memslots *slots = kvm_memslots(kvm);
1350 int offset = offset_in_page(gpa);
1351 gfn_t gfn = gpa >> PAGE_SHIFT;
1352
1353 ghc->gpa = gpa;
1354 ghc->generation = slots->generation;
1355 ghc->memslot = __gfn_to_memslot(slots, gfn);
1356 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1357 if (!kvm_is_error_hva(ghc->hva))
1358 ghc->hva += offset;
1359 else
1360 return -EFAULT;
1361
1362 return 0;
1363}
1364EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1365
1366int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1367 void *data, unsigned long len)
1368{
1369 struct kvm_memslots *slots = kvm_memslots(kvm);
1370 int r;
1371
1372 if (slots->generation != ghc->generation)
1373 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1374
1375 if (kvm_is_error_hva(ghc->hva))
1376 return -EFAULT;
1377
1378 r = copy_to_user((void __user *)ghc->hva, data, len);
1379 if (r)
1380 return -EFAULT;
1381 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1382
1383 return 0;
1384}
1385EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1386
Izik Eidus195aefd2007-10-01 22:14:18 +02001387int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1388{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001389 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1390 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001391}
1392EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1393
1394int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1395{
1396 gfn_t gfn = gpa >> PAGE_SHIFT;
1397 int seg;
1398 int offset = offset_in_page(gpa);
1399 int ret;
1400
1401 while ((seg = next_segment(len, offset)) != 0) {
1402 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1403 if (ret < 0)
1404 return ret;
1405 offset = 0;
1406 len -= seg;
1407 ++gfn;
1408 }
1409 return 0;
1410}
1411EXPORT_SYMBOL_GPL(kvm_clear_guest);
1412
Gleb Natapov49c77542010-10-18 15:22:23 +02001413void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1414 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001416 if (memslot && memslot->dirty_bitmap) {
1417 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001419 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 }
1421}
1422
Gleb Natapov49c77542010-10-18 15:22:23 +02001423void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1424{
1425 struct kvm_memory_slot *memslot;
1426
1427 memslot = gfn_to_memslot(kvm, gfn);
1428 mark_page_dirty_in_slot(kvm, memslot, gfn);
1429}
1430
Eddie Dongb6958ce2007-07-18 12:15:21 +03001431/*
1432 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1433 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001434void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001435{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001436 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001437
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001438 for (;;) {
1439 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001440
Gleb Natapova1b37102009-07-09 15:33:52 +03001441 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001442 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001443 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001444 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001445 if (kvm_cpu_has_pending_timer(vcpu))
1446 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001447 if (signal_pending(current))
1448 break;
1449
Eddie Dongb6958ce2007-07-18 12:15:21 +03001450 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001451 }
1452
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001453 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001454}
1455
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456void kvm_resched(struct kvm_vcpu *vcpu)
1457{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001458 if (!need_resched())
1459 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461}
1462EXPORT_SYMBOL_GPL(kvm_resched);
1463
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001464void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1465{
1466 ktime_t expires;
1467 DEFINE_WAIT(wait);
1468
1469 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1470
1471 /* Sleep for 100 us, and hope lock-holder got scheduled */
1472 expires = ktime_add_ns(ktime_get(), 100000UL);
1473 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1474
1475 finish_wait(&vcpu->wq, &wait);
1476}
1477EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1478
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001479static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001480{
1481 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001482 struct page *page;
1483
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001484 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001485 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001486#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001487 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001488 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001489#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001490#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1491 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1492 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1493#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001494 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001495 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001496 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001497 vmf->page = page;
1498 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001499}
1500
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001501static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001502 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001503};
1504
1505static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1506{
1507 vma->vm_ops = &kvm_vcpu_vm_ops;
1508 return 0;
1509}
1510
Avi Kivitybccf2152007-02-21 18:04:26 +02001511static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1512{
1513 struct kvm_vcpu *vcpu = filp->private_data;
1514
Al Viro66c0b392008-04-19 20:33:56 +01001515 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001516 return 0;
1517}
1518
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001519static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001520 .release = kvm_vcpu_release,
1521 .unlocked_ioctl = kvm_vcpu_ioctl,
1522 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001523 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001524 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001525};
1526
1527/*
1528 * Allocates an inode for the vcpu.
1529 */
1530static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1531{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001532 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001533}
1534
Avi Kivityc5ea7662007-02-20 18:41:05 +02001535/*
1536 * Creates some virtual cpus. Good luck creating more than one.
1537 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001538static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001539{
1540 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001541 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001542
Gleb Natapov73880c82009-06-09 15:56:28 +03001543 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001544 if (IS_ERR(vcpu))
1545 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001546
Avi Kivity15ad7142007-07-11 18:17:21 +03001547 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1548
Avi Kivity26e52152007-11-20 15:30:24 +02001549 r = kvm_arch_vcpu_setup(vcpu);
1550 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001551 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001552
Shaohua Li11ec2802007-07-23 14:51:37 +08001553 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001554 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1555 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001556 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001557 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001558
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001559 kvm_for_each_vcpu(r, v, kvm)
1560 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001561 r = -EEXIST;
1562 goto vcpu_destroy;
1563 }
1564
1565 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001566
1567 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001568 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001569 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001570 if (r < 0) {
1571 kvm_put_kvm(kvm);
1572 goto vcpu_destroy;
1573 }
1574
1575 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1576 smp_wmb();
1577 atomic_inc(&kvm->online_vcpus);
1578
1579#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1580 if (kvm->bsp_vcpu_id == id)
1581 kvm->bsp_vcpu = vcpu;
1582#endif
1583 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001584 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001585
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001586vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001587 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001588 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001589 return r;
1590}
1591
Avi Kivity1961d272007-03-05 19:46:05 +02001592static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1593{
1594 if (sigset) {
1595 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1596 vcpu->sigset_active = 1;
1597 vcpu->sigset = *sigset;
1598 } else
1599 vcpu->sigset_active = 0;
1600 return 0;
1601}
1602
Avi Kivitybccf2152007-02-21 18:04:26 +02001603static long kvm_vcpu_ioctl(struct file *filp,
1604 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605{
Avi Kivitybccf2152007-02-21 18:04:26 +02001606 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001607 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001608 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001609 struct kvm_fpu *fpu = NULL;
1610 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001612 if (vcpu->kvm->mm != current->mm)
1613 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001614
1615#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1616 /*
1617 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1618 * so vcpu_load() would break it.
1619 */
1620 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1621 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1622#endif
1623
1624
1625 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001627 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001628 r = -EINVAL;
1629 if (arg)
1630 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001631 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001632 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001635 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001637 r = -ENOMEM;
1638 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1639 if (!kvm_regs)
1640 goto out;
1641 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001643 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001645 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1646 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001648out_free1:
1649 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650 break;
1651 }
1652 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001653 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001655 r = -ENOMEM;
1656 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1657 if (!kvm_regs)
1658 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001660 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1661 goto out_free2;
1662 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001664 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001666out_free2:
1667 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 break;
1669 }
1670 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001671 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1672 r = -ENOMEM;
1673 if (!kvm_sregs)
1674 goto out;
1675 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676 if (r)
1677 goto out;
1678 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001679 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 goto out;
1681 r = 0;
1682 break;
1683 }
1684 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001685 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1686 r = -ENOMEM;
1687 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001689 r = -EFAULT;
1690 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1691 goto out;
1692 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 if (r)
1694 goto out;
1695 r = 0;
1696 break;
1697 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001698 case KVM_GET_MP_STATE: {
1699 struct kvm_mp_state mp_state;
1700
1701 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1702 if (r)
1703 goto out;
1704 r = -EFAULT;
1705 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1706 goto out;
1707 r = 0;
1708 break;
1709 }
1710 case KVM_SET_MP_STATE: {
1711 struct kvm_mp_state mp_state;
1712
1713 r = -EFAULT;
1714 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1715 goto out;
1716 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1717 if (r)
1718 goto out;
1719 r = 0;
1720 break;
1721 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 case KVM_TRANSLATE: {
1723 struct kvm_translation tr;
1724
1725 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001726 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001728 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 if (r)
1730 goto out;
1731 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001732 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 goto out;
1734 r = 0;
1735 break;
1736 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001737 case KVM_SET_GUEST_DEBUG: {
1738 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739
1740 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001741 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001743 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 if (r)
1745 goto out;
1746 r = 0;
1747 break;
1748 }
Avi Kivity1961d272007-03-05 19:46:05 +02001749 case KVM_SET_SIGNAL_MASK: {
1750 struct kvm_signal_mask __user *sigmask_arg = argp;
1751 struct kvm_signal_mask kvm_sigmask;
1752 sigset_t sigset, *p;
1753
1754 p = NULL;
1755 if (argp) {
1756 r = -EFAULT;
1757 if (copy_from_user(&kvm_sigmask, argp,
1758 sizeof kvm_sigmask))
1759 goto out;
1760 r = -EINVAL;
1761 if (kvm_sigmask.len != sizeof sigset)
1762 goto out;
1763 r = -EFAULT;
1764 if (copy_from_user(&sigset, sigmask_arg->sigset,
1765 sizeof sigset))
1766 goto out;
1767 p = &sigset;
1768 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001769 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001770 break;
1771 }
Avi Kivityb8836732007-04-01 16:34:31 +03001772 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001773 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1774 r = -ENOMEM;
1775 if (!fpu)
1776 goto out;
1777 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001778 if (r)
1779 goto out;
1780 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001781 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001782 goto out;
1783 r = 0;
1784 break;
1785 }
1786 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001787 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1788 r = -ENOMEM;
1789 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001790 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001791 r = -EFAULT;
1792 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1793 goto out;
1794 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001795 if (r)
1796 goto out;
1797 r = 0;
1798 break;
1799 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001800 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001801 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001802 }
1803out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001804 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001805 kfree(fpu);
1806 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001807 return r;
1808}
1809
1810static long kvm_vm_ioctl(struct file *filp,
1811 unsigned int ioctl, unsigned long arg)
1812{
1813 struct kvm *kvm = filp->private_data;
1814 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001815 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001816
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001817 if (kvm->mm != current->mm)
1818 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001819 switch (ioctl) {
1820 case KVM_CREATE_VCPU:
1821 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1822 if (r < 0)
1823 goto out;
1824 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001825 case KVM_SET_USER_MEMORY_REGION: {
1826 struct kvm_userspace_memory_region kvm_userspace_mem;
1827
1828 r = -EFAULT;
1829 if (copy_from_user(&kvm_userspace_mem, argp,
1830 sizeof kvm_userspace_mem))
1831 goto out;
1832
1833 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 if (r)
1835 goto out;
1836 break;
1837 }
1838 case KVM_GET_DIRTY_LOG: {
1839 struct kvm_dirty_log log;
1840
1841 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001842 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001844 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 if (r)
1846 goto out;
1847 break;
1848 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001849#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1850 case KVM_REGISTER_COALESCED_MMIO: {
1851 struct kvm_coalesced_mmio_zone zone;
1852 r = -EFAULT;
1853 if (copy_from_user(&zone, argp, sizeof zone))
1854 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001855 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1856 if (r)
1857 goto out;
1858 r = 0;
1859 break;
1860 }
1861 case KVM_UNREGISTER_COALESCED_MMIO: {
1862 struct kvm_coalesced_mmio_zone zone;
1863 r = -EFAULT;
1864 if (copy_from_user(&zone, argp, sizeof zone))
1865 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001866 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1867 if (r)
1868 goto out;
1869 r = 0;
1870 break;
1871 }
1872#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001873 case KVM_IRQFD: {
1874 struct kvm_irqfd data;
1875
1876 r = -EFAULT;
1877 if (copy_from_user(&data, argp, sizeof data))
1878 goto out;
1879 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1880 break;
1881 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001882 case KVM_IOEVENTFD: {
1883 struct kvm_ioeventfd data;
1884
1885 r = -EFAULT;
1886 if (copy_from_user(&data, argp, sizeof data))
1887 goto out;
1888 r = kvm_ioeventfd(kvm, &data);
1889 break;
1890 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001891#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1892 case KVM_SET_BOOT_CPU_ID:
1893 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001894 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001895 if (atomic_read(&kvm->online_vcpus) != 0)
1896 r = -EBUSY;
1897 else
1898 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001899 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001900 break;
1901#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001902 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001903 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001904 if (r == -ENOTTY)
1905 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001906 }
1907out:
1908 return r;
1909}
1910
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001911#ifdef CONFIG_COMPAT
1912struct compat_kvm_dirty_log {
1913 __u32 slot;
1914 __u32 padding1;
1915 union {
1916 compat_uptr_t dirty_bitmap; /* one bit per page */
1917 __u64 padding2;
1918 };
1919};
1920
1921static long kvm_vm_compat_ioctl(struct file *filp,
1922 unsigned int ioctl, unsigned long arg)
1923{
1924 struct kvm *kvm = filp->private_data;
1925 int r;
1926
1927 if (kvm->mm != current->mm)
1928 return -EIO;
1929 switch (ioctl) {
1930 case KVM_GET_DIRTY_LOG: {
1931 struct compat_kvm_dirty_log compat_log;
1932 struct kvm_dirty_log log;
1933
1934 r = -EFAULT;
1935 if (copy_from_user(&compat_log, (void __user *)arg,
1936 sizeof(compat_log)))
1937 goto out;
1938 log.slot = compat_log.slot;
1939 log.padding1 = compat_log.padding1;
1940 log.padding2 = compat_log.padding2;
1941 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1942
1943 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1944 if (r)
1945 goto out;
1946 break;
1947 }
1948 default:
1949 r = kvm_vm_ioctl(filp, ioctl, arg);
1950 }
1951
1952out:
1953 return r;
1954}
1955#endif
1956
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001957static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001958{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001959 struct page *page[1];
1960 unsigned long addr;
1961 int npages;
1962 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001963 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001964
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001965 addr = gfn_to_hva(kvm, gfn);
1966 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001967 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001968
1969 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1970 NULL);
1971 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001972 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001973
1974 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001975 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001976}
1977
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001978static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001979 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001980};
1981
1982static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1983{
1984 vma->vm_ops = &kvm_vm_vm_ops;
1985 return 0;
1986}
1987
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001988static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001989 .release = kvm_vm_release,
1990 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001991#ifdef CONFIG_COMPAT
1992 .compat_ioctl = kvm_vm_compat_ioctl,
1993#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001994 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001995 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02001996};
1997
1998static int kvm_dev_ioctl_create_vm(void)
1999{
Heiko Carstensaac87632010-10-27 17:22:10 +02002000 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002001 struct kvm *kvm;
2002
Avi Kivityf17abe92007-02-21 19:28:04 +02002003 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002004 if (IS_ERR(kvm))
2005 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002006#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2007 r = kvm_coalesced_mmio_init(kvm);
2008 if (r < 0) {
2009 kvm_put_kvm(kvm);
2010 return r;
2011 }
2012#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002013 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2014 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002015 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002016
Heiko Carstensaac87632010-10-27 17:22:10 +02002017 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002018}
2019
Avi Kivity1a811b62008-12-08 18:25:27 +02002020static long kvm_dev_ioctl_check_extension_generic(long arg)
2021{
2022 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002023 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002024 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002025 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002026#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2027 case KVM_CAP_SET_BOOT_CPU_ID:
2028#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002029 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002030 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002031#ifdef CONFIG_HAVE_KVM_IRQCHIP
2032 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002033 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002034#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002035 default:
2036 break;
2037 }
2038 return kvm_dev_ioctl_check_extension(arg);
2039}
2040
Avi Kivityf17abe92007-02-21 19:28:04 +02002041static long kvm_dev_ioctl(struct file *filp,
2042 unsigned int ioctl, unsigned long arg)
2043{
Avi Kivity07c45a32007-03-07 13:05:38 +02002044 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002045
2046 switch (ioctl) {
2047 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002048 r = -EINVAL;
2049 if (arg)
2050 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002051 r = KVM_API_VERSION;
2052 break;
2053 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002054 r = -EINVAL;
2055 if (arg)
2056 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002057 r = kvm_dev_ioctl_create_vm();
2058 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002059 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002060 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002061 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002062 case KVM_GET_VCPU_MMAP_SIZE:
2063 r = -EINVAL;
2064 if (arg)
2065 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002066 r = PAGE_SIZE; /* struct kvm_run */
2067#ifdef CONFIG_X86
2068 r += PAGE_SIZE; /* pio data page */
2069#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002070#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2071 r += PAGE_SIZE; /* coalesced mmio ring page */
2072#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002073 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002074 case KVM_TRACE_ENABLE:
2075 case KVM_TRACE_PAUSE:
2076 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002077 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002080 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 }
2082out:
2083 return r;
2084}
2085
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 .unlocked_ioctl = kvm_dev_ioctl,
2088 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002089 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090};
2091
2092static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002093 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 "kvm",
2095 &kvm_chardev_ops,
2096};
2097
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002098static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002099{
2100 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002101 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002102
Rusty Russell7f59f492008-12-07 21:25:45 +10302103 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002104 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002105
Rusty Russell7f59f492008-12-07 21:25:45 +10302106 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002107
2108 r = kvm_arch_hardware_enable(NULL);
2109
2110 if (r) {
2111 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2112 atomic_inc(&hardware_enable_failed);
2113 printk(KERN_INFO "kvm: enabling virtualization on "
2114 "CPU%d failed\n", cpu);
2115 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002116}
2117
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002118static void hardware_enable(void *junk)
2119{
2120 spin_lock(&kvm_lock);
2121 hardware_enable_nolock(junk);
2122 spin_unlock(&kvm_lock);
2123}
2124
2125static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002126{
2127 int cpu = raw_smp_processor_id();
2128
Rusty Russell7f59f492008-12-07 21:25:45 +10302129 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002130 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302131 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002132 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002133}
2134
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002135static void hardware_disable(void *junk)
2136{
2137 spin_lock(&kvm_lock);
2138 hardware_disable_nolock(junk);
2139 spin_unlock(&kvm_lock);
2140}
2141
Alexander Graf10474ae2009-09-15 11:37:46 +02002142static void hardware_disable_all_nolock(void)
2143{
2144 BUG_ON(!kvm_usage_count);
2145
2146 kvm_usage_count--;
2147 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002148 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002149}
2150
2151static void hardware_disable_all(void)
2152{
2153 spin_lock(&kvm_lock);
2154 hardware_disable_all_nolock();
2155 spin_unlock(&kvm_lock);
2156}
2157
2158static int hardware_enable_all(void)
2159{
2160 int r = 0;
2161
2162 spin_lock(&kvm_lock);
2163
2164 kvm_usage_count++;
2165 if (kvm_usage_count == 1) {
2166 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002167 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002168
2169 if (atomic_read(&hardware_enable_failed)) {
2170 hardware_disable_all_nolock();
2171 r = -EBUSY;
2172 }
2173 }
2174
2175 spin_unlock(&kvm_lock);
2176
2177 return r;
2178}
2179
Avi Kivity774c47f2007-02-12 00:54:47 -08002180static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2181 void *v)
2182{
2183 int cpu = (long)v;
2184
Alexander Graf10474ae2009-09-15 11:37:46 +02002185 if (!kvm_usage_count)
2186 return NOTIFY_OK;
2187
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002188 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002189 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002190 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002191 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2192 cpu);
2193 hardware_disable(NULL);
2194 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002195 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002196 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2197 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002198 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002199 break;
2200 }
2201 return NOTIFY_OK;
2202}
2203
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002204
Avi Kivityb7c41452010-12-02 17:52:50 +02002205asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002206{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002207 /* Fault while not rebooting. We want the trace. */
2208 BUG();
2209}
Avi Kivityb7c41452010-12-02 17:52:50 +02002210EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002211
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002212static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002213 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002214{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002215 /*
2216 * Some (well, at least mine) BIOSes hang on reboot if
2217 * in vmx root mode.
2218 *
2219 * And Intel TXT required VMX off for all cpu when system shutdown.
2220 */
2221 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2222 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002223 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002224 return NOTIFY_OK;
2225}
2226
2227static struct notifier_block kvm_reboot_notifier = {
2228 .notifier_call = kvm_reboot,
2229 .priority = 0,
2230};
2231
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002232static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002233{
2234 int i;
2235
2236 for (i = 0; i < bus->dev_count; i++) {
2237 struct kvm_io_device *pos = bus->devs[i];
2238
2239 kvm_iodevice_destructor(pos);
2240 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002241 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002242}
2243
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002244/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002245int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002246 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002247{
2248 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002249 struct kvm_io_bus *bus;
2250
2251 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002252 for (i = 0; i < bus->dev_count; i++)
2253 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2254 return 0;
2255 return -EOPNOTSUPP;
2256}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002257
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002258/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002259int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2260 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002261{
2262 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002263 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002264
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002265 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002266 for (i = 0; i < bus->dev_count; i++)
2267 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2268 return 0;
2269 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002270}
2271
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002272/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002273int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2274 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002275{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002276 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002277
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002278 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002279 if (bus->dev_count > NR_IOBUS_DEVS-1)
2280 return -ENOSPC;
2281
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002282 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2283 if (!new_bus)
2284 return -ENOMEM;
2285 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2286 new_bus->devs[new_bus->dev_count++] = dev;
2287 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2288 synchronize_srcu_expedited(&kvm->srcu);
2289 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002290
2291 return 0;
2292}
2293
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002294/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002295int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2296 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002297{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002298 int i, r;
2299 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002300
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002301 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2302 if (!new_bus)
2303 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002304
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002305 bus = kvm->buses[bus_idx];
2306 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2307
2308 r = -ENOENT;
2309 for (i = 0; i < new_bus->dev_count; i++)
2310 if (new_bus->devs[i] == dev) {
2311 r = 0;
2312 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002313 break;
2314 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002315
2316 if (r) {
2317 kfree(new_bus);
2318 return r;
2319 }
2320
2321 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2322 synchronize_srcu_expedited(&kvm->srcu);
2323 kfree(bus);
2324 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002325}
2326
Avi Kivity774c47f2007-02-12 00:54:47 -08002327static struct notifier_block kvm_cpu_notifier = {
2328 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002329};
2330
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002331static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002332{
2333 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002334 struct kvm *kvm;
2335
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002336 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002337 spin_lock(&kvm_lock);
2338 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002339 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002340 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002341 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002342}
2343
2344DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2345
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002346static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002347{
2348 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002349 struct kvm *kvm;
2350 struct kvm_vcpu *vcpu;
2351 int i;
2352
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002353 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002354 spin_lock(&kvm_lock);
2355 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002356 kvm_for_each_vcpu(i, vcpu, kvm)
2357 *val += *(u32 *)((void *)vcpu + offset);
2358
Avi Kivity1165f5f2007-04-19 17:27:43 +03002359 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002360 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002361}
2362
Avi Kivityba1389b2007-11-18 16:24:12 +02002363DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2364
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002365static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002366 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2367 [KVM_STAT_VM] = &vm_stat_fops,
2368};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002369
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002370static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
2372 struct kvm_stats_debugfs_item *p;
2373
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002374 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002376 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002377 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002378 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379}
2380
2381static void kvm_exit_debug(void)
2382{
2383 struct kvm_stats_debugfs_item *p;
2384
2385 for (p = debugfs_entries; p->name; ++p)
2386 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002387 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388}
2389
Avi Kivity59ae6c62007-02-12 00:54:48 -08002390static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2391{
Alexander Graf10474ae2009-09-15 11:37:46 +02002392 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002393 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002394 return 0;
2395}
2396
2397static int kvm_resume(struct sys_device *dev)
2398{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002399 if (kvm_usage_count) {
2400 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002401 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002402 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002403 return 0;
2404}
2405
2406static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002407 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002408 .suspend = kvm_suspend,
2409 .resume = kvm_resume,
2410};
2411
2412static struct sys_device kvm_sysdev = {
2413 .id = 0,
2414 .cls = &kvm_sysdev_class,
2415};
2416
Izik Eiduscea7bb22007-10-17 19:17:48 +02002417struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002418pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419
Avi Kivity15ad7142007-07-11 18:17:21 +03002420static inline
2421struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2422{
2423 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2424}
2425
2426static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2427{
2428 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2429
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002430 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002431}
2432
2433static void kvm_sched_out(struct preempt_notifier *pn,
2434 struct task_struct *next)
2435{
2436 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2437
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002438 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002439}
2440
Avi Kivity0ee75be2010-04-28 15:39:01 +03002441int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002442 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443{
2444 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002445 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002447 r = kvm_arch_init(opaque);
2448 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002449 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002450
2451 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2452
2453 if (bad_page == NULL) {
2454 r = -ENOMEM;
2455 goto out;
2456 }
2457
Anthony Liguori35149e22008-04-02 14:46:56 -05002458 bad_pfn = page_to_pfn(bad_page);
2459
Huang Yingbf998152010-05-31 14:28:19 +08002460 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2461
2462 if (hwpoison_page == NULL) {
2463 r = -ENOMEM;
2464 goto out_free_0;
2465 }
2466
2467 hwpoison_pfn = page_to_pfn(hwpoison_page);
2468
Gleb Natapovedba23e2010-07-07 20:16:45 +03002469 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2470
2471 if (fault_page == NULL) {
2472 r = -ENOMEM;
2473 goto out_free_0;
2474 }
2475
2476 fault_pfn = page_to_pfn(fault_page);
2477
Avi Kivity8437a612009-06-06 14:52:35 -07002478 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302479 r = -ENOMEM;
2480 goto out_free_0;
2481 }
2482
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002483 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302485 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486
Yang, Sheng002c7f72007-07-31 14:23:01 +03002487 for_each_online_cpu(cpu) {
2488 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002489 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002490 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002491 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002492 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002493 }
2494
Avi Kivity774c47f2007-02-12 00:54:47 -08002495 r = register_cpu_notifier(&kvm_cpu_notifier);
2496 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002497 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 register_reboot_notifier(&kvm_reboot_notifier);
2499
Avi Kivity59ae6c62007-02-12 00:54:48 -08002500 r = sysdev_class_register(&kvm_sysdev_class);
2501 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002502 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002503
2504 r = sysdev_register(&kvm_sysdev);
2505 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002506 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002507
Rusty Russellc16f8622007-07-30 21:12:19 +10002508 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002509 if (!vcpu_align)
2510 vcpu_align = __alignof__(struct kvm_vcpu);
2511 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002512 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002513 if (!kvm_vcpu_cache) {
2514 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002515 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002516 }
2517
Gleb Natapovaf585b92010-10-14 11:22:46 +02002518 r = kvm_async_pf_init();
2519 if (r)
2520 goto out_free;
2521
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002523 kvm_vm_fops.owner = module;
2524 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525
2526 r = misc_register(&kvm_dev);
2527 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002528 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002529 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 }
2531
Avi Kivity15ad7142007-07-11 18:17:21 +03002532 kvm_preempt_ops.sched_in = kvm_sched_in;
2533 kvm_preempt_ops.sched_out = kvm_sched_out;
2534
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002535 kvm_init_debug();
2536
Avi Kivityc7addb92007-09-16 18:58:32 +02002537 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538
Gleb Natapovaf585b92010-10-14 11:22:46 +02002539out_unreg:
2540 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002542 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002543out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002544 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002545out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002546 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002547out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002549 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002550out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002551out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002552 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302553out_free_0a:
2554 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002555out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002556 if (fault_page)
2557 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002558 if (hwpoison_page)
2559 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002560 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002561out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002562 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002563out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 return r;
2565}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002566EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002568void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002570 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002572 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002573 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002574 sysdev_unregister(&kvm_sysdev);
2575 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002577 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002578 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002579 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002580 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302581 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002582 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002583 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002585EXPORT_SYMBOL_GPL(kvm_exit);