blob: 68dda513cd728e0a241e9e4f96f00ea5d3552a0f [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>
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +010033#include <linux/syscore_ops.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>
Sasha Levin743eeb02011-07-27 16:00:48 +030050#include <linux/sort.h>
51#include <linux/bsearch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
Avi Kivitye4956062007-06-28 14:15:57 -040053#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040054#include <asm/io.h>
55#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020056#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Laurent Vivier5f94c172008-05-30 16:05:54 +020058#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020059#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020060
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061#define CREATE_TRACE_POINTS
62#include <trace/events/kvm.h>
63
Avi Kivity6aa8b732006-12-10 02:21:36 -080064MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
Marcelo Tosattifa40a822009-06-04 15:08:24 -030067/*
68 * Ordering of locks:
69 *
Sheng Yangfae3a352009-12-15 10:28:07 +080070 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030071 */
72
Jan Kiszkae935b832011-02-08 12:55:33 +010073DEFINE_RAW_SPINLOCK(kvm_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080074LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080075
Rusty Russell7f59f492008-12-07 21:25:45 +103076static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020077static int kvm_usage_count = 0;
78static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030079
Rusty Russellc16f8622007-07-30 21:12:19 +100080struct kmem_cache *kvm_vcpu_cache;
81EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030082
Avi Kivity15ad7142007-07-11 18:17:21 +030083static __read_mostly struct preempt_ops kvm_preempt_ops;
84
Hollis Blanchard76f7c872008-04-15 16:05:42 -050085struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
Avi Kivitybccf2152007-02-21 18:04:26 +020087static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
88 unsigned long arg);
Alexander Graf1dda6062011-06-08 02:45:37 +020089#ifdef CONFIG_COMPAT
90static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
91 unsigned long arg);
92#endif
Alexander Graf10474ae2009-09-15 11:37:46 +020093static int hardware_enable_all(void);
94static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020095
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020096static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
97
Avi Kivityb7c41452010-12-02 17:52:50 +020098bool kvm_rebooting;
99EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300100
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300101static bool largepages_enabled = true;
102
Xiao Guangrongca0565f52012-07-17 21:54:52 +0800103struct page *bad_page;
104static pfn_t bad_pfn;
105
Gleb Natapovfa7bff82010-07-07 20:16:44 +0300106static struct page *hwpoison_page;
107static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +0800108
Xiao Guangrong903816f2012-07-17 21:54:11 +0800109static struct page *fault_page;
110static pfn_t fault_pfn;
Gleb Natapovedba23e2010-07-07 20:16:45 +0300111
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800112inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300113{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100114 if (pfn_valid(pfn)) {
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800115 int reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800116 struct page *tail = pfn_to_page(pfn);
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800117 struct page *head = compound_trans_head(tail);
118 reserved = PageReserved(head);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800119 if (head != tail) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800120 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800121 * "head" is not a dangling pointer
122 * (compound_trans_head takes care of that)
123 * but the hugepage may have been splitted
124 * from under us (and we may not hold a
125 * reference count on the head page so it can
126 * be reused before we run PageReferenced), so
127 * we've to check PageTail before returning
128 * what we just read.
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800129 */
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800130 smp_rmb();
131 if (PageTail(tail))
132 return reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800133 }
134 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100135 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300136
137 return true;
138}
139
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140/*
141 * Switches to specified vcpu, until a matching vcpu_put()
142 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200143void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144{
Avi Kivity15ad7142007-07-11 18:17:21 +0300145 int cpu;
146
Avi Kivitybccf2152007-02-21 18:04:26 +0200147 mutex_lock(&vcpu->mutex);
Rik van Riel34bb10b2011-02-01 09:52:41 -0500148 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
149 /* The thread running this VCPU changed. */
150 struct pid *oldpid = vcpu->pid;
151 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
152 rcu_assign_pointer(vcpu->pid, newpid);
153 synchronize_rcu();
154 put_pid(oldpid);
155 }
Avi Kivity15ad7142007-07-11 18:17:21 +0300156 cpu = get_cpu();
157 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200158 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300159 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200160}
161
Carsten Otte313a3dc2007-10-11 19:16:52 +0200162void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163{
Avi Kivity15ad7142007-07-11 18:17:21 +0300164 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200165 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300166 preempt_notifier_unregister(&vcpu->preempt_notifier);
167 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 mutex_unlock(&vcpu->mutex);
169}
170
Avi Kivityd9e368d2007-06-07 19:18:30 +0300171static void ack_flush(void *_completed)
172{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300173}
174
Rusty Russell49846892008-12-08 20:26:24 +1030175static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300176{
Avi Kivity597a5f52008-07-20 14:24:22 +0300177 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030178 cpumask_var_t cpus;
179 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300180 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300181
Li Zefan79f55992009-06-15 14:58:26 +0800182 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030183
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800184 me = get_cpu();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300185 kvm_for_each_vcpu(i, vcpu, kvm) {
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800186 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300187 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800188
189 /* Set ->requests bit before we read ->mode */
190 smp_mb();
191
192 if (cpus != NULL && cpu != -1 && cpu != me &&
193 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030194 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300195 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030196 if (unlikely(cpus == NULL))
197 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
198 else if (!cpumask_empty(cpus))
199 smp_call_function_many(cpus, ack_flush, NULL, 1);
200 else
201 called = false;
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800202 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030203 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030204 return called;
205}
206
207void kvm_flush_remote_tlbs(struct kvm *kvm)
208{
Alex Shibec87d62012-03-04 13:13:38 +0800209 long dirty_count = kvm->tlbs_dirty;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800210
211 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030212 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
213 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800214 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300215}
216
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500217void kvm_reload_remote_mmus(struct kvm *kvm)
218{
Rusty Russell49846892008-12-08 20:26:24 +1030219 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500220}
221
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000222int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
223{
224 struct page *page;
225 int r;
226
227 mutex_init(&vcpu->mutex);
228 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000229 vcpu->kvm = kvm;
230 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500231 vcpu->pid = NULL;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300232 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200233 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000234
235 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
236 if (!page) {
237 r = -ENOMEM;
238 goto fail;
239 }
240 vcpu->run = page_address(page);
241
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800242 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000243 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800244 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000245 return 0;
246
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000247fail_free_run:
248 free_page((unsigned long)vcpu->run);
249fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000250 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000251}
252EXPORT_SYMBOL_GPL(kvm_vcpu_init);
253
254void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
255{
Rik van Riel34bb10b2011-02-01 09:52:41 -0500256 put_pid(vcpu->pid);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800257 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000258 free_page((unsigned long)vcpu->run);
259}
260EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
261
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200262#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
263static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
264{
265 return container_of(mn, struct kvm, mmu_notifier);
266}
267
268static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
269 struct mm_struct *mm,
270 unsigned long address)
271{
272 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200273 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200274
275 /*
276 * When ->invalidate_page runs, the linux pte has been zapped
277 * already but the page is still allocated until
278 * ->invalidate_page returns. So if we increase the sequence
279 * here the kvm page fault will notice if the spte can't be
280 * established because the page is going to be freed. If
281 * instead the kvm page fault establishes the spte before
282 * ->invalidate_page runs, kvm_unmap_hva will release it
283 * before returning.
284 *
285 * The sequence increase only need to be seen at spin_unlock
286 * time, and not at spin_lock time.
287 *
288 * Increasing the sequence after the spin_unlock would be
289 * unsafe because the kvm page fault could then establish the
290 * pte after kvm_unmap_hva returned, without noticing the page
291 * is going to be freed.
292 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200293 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200294 spin_lock(&kvm->mmu_lock);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900295
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200296 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800297 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200298 /* we've to flush the tlb before the pages can be freed */
299 if (need_tlb_flush)
300 kvm_flush_remote_tlbs(kvm);
301
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900302 spin_unlock(&kvm->mmu_lock);
303 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200304}
305
Izik Eidus3da0dd42009-09-23 21:47:18 +0300306static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
307 struct mm_struct *mm,
308 unsigned long address,
309 pte_t pte)
310{
311 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200312 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300313
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200314 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300315 spin_lock(&kvm->mmu_lock);
316 kvm->mmu_notifier_seq++;
317 kvm_set_spte_hva(kvm, address, pte);
318 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200319 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300320}
321
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200322static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
323 struct mm_struct *mm,
324 unsigned long start,
325 unsigned long end)
326{
327 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200328 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200329
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200330 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200331 spin_lock(&kvm->mmu_lock);
332 /*
333 * The count increase must become visible at unlock time as no
334 * spte can be established without taking the mmu_lock and
335 * count is also read inside the mmu_lock critical section.
336 */
337 kvm->mmu_notifier_count++;
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +0900338 need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800339 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200340 /* we've to flush the tlb before the pages can be freed */
341 if (need_tlb_flush)
342 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900343
344 spin_unlock(&kvm->mmu_lock);
345 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200346}
347
348static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
349 struct mm_struct *mm,
350 unsigned long start,
351 unsigned long end)
352{
353 struct kvm *kvm = mmu_notifier_to_kvm(mn);
354
355 spin_lock(&kvm->mmu_lock);
356 /*
357 * This sequence increase will notify the kvm page fault that
358 * the page that is going to be mapped in the spte could have
359 * been freed.
360 */
361 kvm->mmu_notifier_seq++;
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000362 smp_wmb();
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200363 /*
364 * The above sequence increase must be visible before the
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000365 * below count decrease, which is ensured by the smp_wmb above
366 * in conjunction with the smp_rmb in mmu_notifier_retry().
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200367 */
368 kvm->mmu_notifier_count--;
369 spin_unlock(&kvm->mmu_lock);
370
371 BUG_ON(kvm->mmu_notifier_count < 0);
372}
373
374static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
375 struct mm_struct *mm,
376 unsigned long address)
377{
378 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200379 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200380
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200381 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200382 spin_lock(&kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200383
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900384 young = kvm_age_hva(kvm, address);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200385 if (young)
386 kvm_flush_remote_tlbs(kvm);
387
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900388 spin_unlock(&kvm->mmu_lock);
389 srcu_read_unlock(&kvm->srcu, idx);
390
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200391 return young;
392}
393
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800394static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
395 struct mm_struct *mm,
396 unsigned long address)
397{
398 struct kvm *kvm = mmu_notifier_to_kvm(mn);
399 int young, idx;
400
401 idx = srcu_read_lock(&kvm->srcu);
402 spin_lock(&kvm->mmu_lock);
403 young = kvm_test_age_hva(kvm, address);
404 spin_unlock(&kvm->mmu_lock);
405 srcu_read_unlock(&kvm->srcu, idx);
406
407 return young;
408}
409
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100410static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
411 struct mm_struct *mm)
412{
413 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800414 int idx;
415
416 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100417 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800418 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100419}
420
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200421static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
422 .invalidate_page = kvm_mmu_notifier_invalidate_page,
423 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
424 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
425 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800426 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300427 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100428 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200429};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200430
431static int kvm_init_mmu_notifier(struct kvm *kvm)
432{
433 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
434 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
435}
436
437#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
438
439static int kvm_init_mmu_notifier(struct kvm *kvm)
440{
441 return 0;
442}
443
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200444#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
445
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800446static void kvm_init_memslots_id(struct kvm *kvm)
447{
448 int i;
449 struct kvm_memslots *slots = kvm->memslots;
450
451 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800452 slots->id_to_index[i] = slots->memslots[i].id = i;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800453}
454
Carsten Ottee08b9632012-01-04 10:25:20 +0100455static struct kvm *kvm_create_vm(unsigned long type)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100457 int r, i;
458 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100460 if (!kvm)
461 return ERR_PTR(-ENOMEM);
462
Carsten Ottee08b9632012-01-04 10:25:20 +0100463 r = kvm_arch_init_vm(kvm, type);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100464 if (r)
465 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200466
467 r = hardware_enable_all();
468 if (r)
469 goto out_err_nodisable;
470
Avi Kivity75858a82009-01-04 17:10:50 +0200471#ifdef CONFIG_HAVE_KVM_IRQCHIP
472 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300473 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200474#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200476 r = -ENOMEM;
477 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
478 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100479 goto out_err_nosrcu;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800480 kvm_init_memslots_id(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200481 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100482 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200483 for (i = 0; i < KVM_NR_BUSES; i++) {
484 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
485 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100486 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200487 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200488 }
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700489
OGAWA Hirofumi85722cd2011-05-11 09:28:28 +0900490 spin_lock_init(&kvm->mmu_lock);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200491 kvm->mm = current->mm;
492 atomic_inc(&kvm->mm->mm_count);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400493 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800494 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300495 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200496 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300497 atomic_set(&kvm->users_count, 1);
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700498
499 r = kvm_init_mmu_notifier(kvm);
500 if (r)
501 goto out_err;
502
Jan Kiszkae935b832011-02-08 12:55:33 +0100503 raw_spin_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000504 list_add(&kvm->vm_list, &vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100505 raw_spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100506
Avi Kivityf17abe92007-02-21 19:28:04 +0200507 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200508
509out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100510 cleanup_srcu_struct(&kvm->srcu);
511out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200512 hardware_disable_all();
513out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200514 for (i = 0; i < KVM_NR_BUSES; i++)
515 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200516 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100517 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200518 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200519}
520
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900521/*
522 * Avoid using vmalloc for a small buffer.
523 * Should not be used when the size is statically known.
524 */
Takuya Yoshikawac1a7b322012-05-20 13:15:07 +0900525void *kvm_kvzalloc(unsigned long size)
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900526{
527 if (size > PAGE_SIZE)
528 return vzalloc(size);
529 else
530 return kzalloc(size, GFP_KERNEL);
531}
532
Takuya Yoshikawac1a7b322012-05-20 13:15:07 +0900533void kvm_kvfree(const void *addr)
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900534{
535 if (is_vmalloc_addr(addr))
536 vfree(addr);
537 else
538 kfree(addr);
539}
540
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900541static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
542{
543 if (!memslot->dirty_bitmap)
544 return;
545
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900546 kvm_kvfree(memslot->dirty_bitmap);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900547 memslot->dirty_bitmap = NULL;
548}
549
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550/*
551 * Free any memory in @free but not in @dont.
552 */
553static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
554 struct kvm_memory_slot *dont)
555{
Izik Eidus290fc382007-09-27 14:11:22 +0200556 if (!dont || free->rmap != dont->rmap)
557 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
559 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900560 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900562 kvm_arch_free_memslot(free, dont);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500565 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566}
567
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800568void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569{
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200570 struct kvm_memslots *slots = kvm->memslots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +0800571 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +0800573 kvm_for_each_memslot(memslot, slots)
574 kvm_free_physmem_slot(memslot, NULL);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200575
576 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577}
578
Avi Kivityf17abe92007-02-21 19:28:04 +0200579static void kvm_destroy_vm(struct kvm *kvm)
580{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200581 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200582 struct mm_struct *mm = kvm->mm;
583
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800584 kvm_arch_sync_events(kvm);
Jan Kiszkae935b832011-02-08 12:55:33 +0100585 raw_spin_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800586 list_del(&kvm->vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100587 raw_spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200588 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200589 for (i = 0; i < KVM_NR_BUSES; i++)
590 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200591 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200592#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
593 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200594#else
595 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200596#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800597 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100598 kvm_free_physmem(kvm);
599 cleanup_srcu_struct(&kvm->srcu);
600 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200601 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200602 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200603}
604
Izik Eidusd39f13b2008-03-30 16:01:25 +0300605void kvm_get_kvm(struct kvm *kvm)
606{
607 atomic_inc(&kvm->users_count);
608}
609EXPORT_SYMBOL_GPL(kvm_get_kvm);
610
611void kvm_put_kvm(struct kvm *kvm)
612{
613 if (atomic_dec_and_test(&kvm->users_count))
614 kvm_destroy_vm(kvm);
615}
616EXPORT_SYMBOL_GPL(kvm_put_kvm);
617
618
Avi Kivityf17abe92007-02-21 19:28:04 +0200619static int kvm_vm_release(struct inode *inode, struct file *filp)
620{
621 struct kvm *kvm = filp->private_data;
622
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400623 kvm_irqfd_release(kvm);
624
Izik Eidusd39f13b2008-03-30 16:01:25 +0300625 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 return 0;
627}
628
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900629/*
630 * Allocation size is twice as large as the actual dirty bitmap size.
Takuya Yoshikawa93474b22012-03-01 19:34:45 +0900631 * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900632 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900633static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
634{
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900635#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900636 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900637
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900638 memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900639 if (!memslot->dirty_bitmap)
640 return -ENOMEM;
641
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900642#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900643 return 0;
644}
645
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800646static int cmp_memslot(const void *slot1, const void *slot2)
647{
648 struct kvm_memory_slot *s1, *s2;
649
650 s1 = (struct kvm_memory_slot *)slot1;
651 s2 = (struct kvm_memory_slot *)slot2;
652
653 if (s1->npages < s2->npages)
654 return 1;
655 if (s1->npages > s2->npages)
656 return -1;
657
658 return 0;
659}
660
661/*
662 * Sort the memslots base on its size, so the larger slots
663 * will get better fit.
664 */
665static void sort_memslots(struct kvm_memslots *slots)
666{
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800667 int i;
668
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800669 sort(slots->memslots, KVM_MEM_SLOTS_NUM,
670 sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800671
672 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
673 slots->id_to_index[slots->memslots[i].id] = i;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800674}
675
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800676void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
677{
678 if (new) {
679 int id = new->id;
Xiao Guangrong28a37542011-11-24 19:04:35 +0800680 struct kvm_memory_slot *old = id_to_memslot(slots, id);
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800681 unsigned long npages = old->npages;
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800682
Xiao Guangrong28a37542011-11-24 19:04:35 +0800683 *old = *new;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800684 if (new->npages != npages)
685 sort_memslots(slots);
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800686 }
687
688 slots->generation++;
689}
690
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 * Allocate some memory and give it an address in the guest physical address
693 * space.
694 *
695 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800696 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500697 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800699int __kvm_set_memory_region(struct kvm *kvm,
700 struct kvm_userspace_memory_region *mem,
701 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702{
Avi Kivity8234b222010-12-27 12:08:45 +0200703 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200705 unsigned long npages;
706 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707 struct kvm_memory_slot *memslot;
708 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200709 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710
711 r = -EINVAL;
712 /* General sanity checks */
713 if (mem->memory_size & (PAGE_SIZE - 1))
714 goto out;
715 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
716 goto out;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +0900717 /* We can read the guest memory with __xxx_user() later on. */
718 if (user_alloc &&
719 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Heiko Carstens9e3bb6b2011-05-24 07:51:27 +0200720 !access_ok(VERIFY_WRITE,
721 (void __user *)(unsigned long)mem->userspace_addr,
722 mem->memory_size)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600723 goto out;
Xiao Guangrong93a5cef2011-11-24 17:37:48 +0800724 if (mem->slot >= KVM_MEM_SLOTS_NUM)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 goto out;
726 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
727 goto out;
728
Xiao Guangrong28a37542011-11-24 19:04:35 +0800729 memslot = id_to_memslot(kvm->memslots, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
731 npages = mem->memory_size >> PAGE_SHIFT;
732
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900733 r = -EINVAL;
734 if (npages > KVM_MEM_MAX_NR_PAGES)
735 goto out;
736
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 if (!npages)
738 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
739
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 new = old = *memslot;
741
Avi Kivitye36d96f2010-06-21 10:56:36 +0300742 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 new.base_gfn = base_gfn;
744 new.npages = npages;
745 new.flags = mem->flags;
746
747 /* Disallow changing a memory slot's size. */
748 r = -EINVAL;
749 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800750 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751
752 /* Check for overlaps */
753 r = -EEXIST;
754 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200755 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200757 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 continue;
759 if (!((base_gfn + npages <= s->base_gfn) ||
760 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800761 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 /* Free page dirty bitmap if unneeded */
765 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000766 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767
768 r = -ENOMEM;
769
770 /* Allocate if a slot is being created */
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900771 if (npages && !old.npages) {
772 new.user_alloc = user_alloc;
773 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200774#ifndef CONFIG_S390
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900775 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200776 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800777 goto out_free;
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900778#endif /* not defined CONFIG_S390 */
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900779 if (kvm_arch_create_memslot(&new, npages))
780 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 /* Allocate page dirty bitmap if needed */
784 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900785 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800786 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200787 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 }
789
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200790 if (!npages) {
Xiao Guangrong28a37542011-11-24 19:04:35 +0800791 struct kvm_memory_slot *slot;
792
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200793 r = -ENOMEM;
Thomas Meyer6da64fd2011-11-08 20:32:19 +0100794 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
795 GFP_KERNEL);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200796 if (!slots)
797 goto out_free;
Xiao Guangrong28a37542011-11-24 19:04:35 +0800798 slot = id_to_memslot(slots, mem->slot);
799 slot->flags |= KVM_MEMSLOT_INVALID;
800
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800801 update_memslots(slots, NULL);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200802
803 old_memslots = kvm->memslots;
804 rcu_assign_pointer(kvm->memslots, slots);
805 synchronize_srcu_expedited(&kvm->srcu);
806 /* From this point no new shadow pages pointing to a deleted
807 * memslot will be created.
808 *
809 * validation of sp->gfn happens in:
810 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
811 * - kvm_is_visible_gfn (mmu_check_roots)
812 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300813 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200814 kfree(old_memslots);
815 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300816
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200817 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
818 if (r)
819 goto out_free;
820
Alex Williamson32f6daa2012-04-11 09:51:49 -0600821 /* map/unmap the pages in iommu page table */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200822 if (npages) {
823 r = kvm_iommu_map_pages(kvm, &new);
824 if (r)
825 goto out_free;
Alex Williamson32f6daa2012-04-11 09:51:49 -0600826 } else
827 kvm_iommu_unmap_pages(kvm, &old);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200828
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200829 r = -ENOMEM;
Thomas Meyer6da64fd2011-11-08 20:32:19 +0100830 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
831 GFP_KERNEL);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200832 if (!slots)
833 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200834
835 /* actual memory is freed via old in kvm_free_physmem_slot below */
836 if (!npages) {
837 new.rmap = NULL;
838 new.dirty_bitmap = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900839 memset(&new.arch, 0, sizeof(new.arch));
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200840 }
841
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800842 update_memslots(slots, &new);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200843 old_memslots = kvm->memslots;
844 rcu_assign_pointer(kvm->memslots, slots);
845 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200847 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800848
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800849 /*
850 * If the new memory slot is created, we need to clear all
851 * mmio sptes.
852 */
853 if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
854 kvm_arch_flush_shadow(kvm);
855
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200856 kvm_free_physmem_slot(&old, &new);
857 kfree(old_memslots);
858
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 return 0;
860
Sheng Yangf78e0e22007-10-29 09:40:42 +0800861out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 kvm_free_physmem_slot(&new, &old);
863out:
864 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200865
866}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800867EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
868
869int kvm_set_memory_region(struct kvm *kvm,
870 struct kvm_userspace_memory_region *mem,
871 int user_alloc)
872{
873 int r;
874
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200875 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800876 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200877 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800878 return r;
879}
Izik Eidus210c7c42007-10-24 23:52:57 +0200880EXPORT_SYMBOL_GPL(kvm_set_memory_region);
881
Carsten Otte1fe779f2007-10-29 16:08:35 +0100882int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
883 struct
884 kvm_userspace_memory_region *mem,
885 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200886{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200887 if (mem->slot >= KVM_MEMORY_SLOTS)
888 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200889 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890}
891
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800892int kvm_get_dirty_log(struct kvm *kvm,
893 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894{
895 struct kvm_memory_slot *memslot;
896 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900897 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 unsigned long any = 0;
899
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 r = -EINVAL;
901 if (log->slot >= KVM_MEMORY_SLOTS)
902 goto out;
903
Xiao Guangrong28a37542011-11-24 19:04:35 +0800904 memslot = id_to_memslot(kvm->memslots, log->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 r = -ENOENT;
906 if (!memslot->dirty_bitmap)
907 goto out;
908
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900909 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910
Uri Lublincd1a4a92007-02-22 16:43:09 +0200911 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 any = memslot->dirty_bitmap[i];
913
914 r = -EFAULT;
915 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
916 goto out;
917
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800918 if (any)
919 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920
921 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 return r;
924}
925
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900926bool kvm_largepages_enabled(void)
927{
928 return largepages_enabled;
929}
930
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300931void kvm_disable_largepages(void)
932{
933 largepages_enabled = false;
934}
935EXPORT_SYMBOL_GPL(kvm_disable_largepages);
936
Izik Eiduscea7bb22007-10-17 19:17:48 +0200937int is_error_page(struct page *page)
938{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300939 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200940}
941EXPORT_SYMBOL_GPL(is_error_page);
942
Anthony Liguori35149e22008-04-02 14:46:56 -0500943int is_error_pfn(pfn_t pfn)
944{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300945 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500946}
947EXPORT_SYMBOL_GPL(is_error_pfn);
948
Huang Yingbf998152010-05-31 14:28:19 +0800949int is_hwpoison_pfn(pfn_t pfn)
950{
951 return pfn == hwpoison_pfn;
952}
953EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
954
Xiao Guangrongfce92dc2011-07-12 03:28:54 +0800955int is_noslot_pfn(pfn_t pfn)
956{
957 return pfn == bad_pfn;
958}
959EXPORT_SYMBOL_GPL(is_noslot_pfn);
960
961int is_invalid_pfn(pfn_t pfn)
962{
963 return pfn == hwpoison_pfn || pfn == fault_pfn;
964}
965EXPORT_SYMBOL_GPL(is_invalid_pfn);
966
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200967static inline unsigned long bad_hva(void)
968{
969 return PAGE_OFFSET;
970}
971
972int kvm_is_error_hva(unsigned long addr)
973{
974 return addr == bad_hva();
975}
976EXPORT_SYMBOL_GPL(kvm_is_error_hva);
977
Gleb Natapov49c77542010-10-18 15:22:23 +0200978struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
979{
980 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
981}
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300982EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983
Izik Eiduse0d62c72007-10-24 23:57:46 +0200984int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
985{
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800986 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200987
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800988 if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
989 memslot->flags & KVM_MEMSLOT_INVALID)
990 return 0;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200991
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800992 return 1;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200993}
994EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
995
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100996unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
997{
998 struct vm_area_struct *vma;
999 unsigned long addr, size;
1000
1001 size = PAGE_SIZE;
1002
1003 addr = gfn_to_hva(kvm, gfn);
1004 if (kvm_is_error_hva(addr))
1005 return PAGE_SIZE;
1006
1007 down_read(&current->mm->mmap_sem);
1008 vma = find_vma(current->mm, addr);
1009 if (!vma)
1010 goto out;
1011
1012 size = vma_kernel_pagesize(vma);
1013
1014out:
1015 up_read(&current->mm->mmap_sem);
1016
1017 return size;
1018}
1019
Gleb Natapov49c77542010-10-18 15:22:23 +02001020static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001021 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001022{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001023 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001024 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001025
1026 if (nr_pages)
1027 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1028
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001029 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001030}
Xiao Guangrong48987782010-08-22 19:11:43 +08001031
1032unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1033{
Gleb Natapov49c77542010-10-18 15:22:23 +02001034 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001035}
Sheng Yang0d150292008-04-25 21:44:50 +08001036EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001037
Xiao Guangrong903816f2012-07-17 21:54:11 +08001038pfn_t get_fault_pfn(void)
Gleb Natapov80300892010-10-19 18:13:41 +02001039{
1040 get_page(fault_page);
1041 return fault_pfn;
1042}
Xiao Guangrong903816f2012-07-17 21:54:11 +08001043EXPORT_SYMBOL_GPL(get_fault_pfn);
Gleb Natapov80300892010-10-19 18:13:41 +02001044
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001045int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1046 unsigned long start, int write, struct page **page)
1047{
1048 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1049
1050 if (write)
1051 flags |= FOLL_WRITE;
1052
1053 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1054}
1055
Huang Yingfafc3db2011-01-30 11:15:49 +08001056static inline int check_user_page_hwpoison(unsigned long addr)
1057{
1058 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1059
1060 rc = __get_user_pages(current, current->mm, addr, 1,
1061 flags, NULL, NULL, NULL);
1062 return rc == -EHWPOISON;
1063}
1064
Xiao Guangrongd5661042012-07-17 21:56:16 +08001065static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
1066 bool write_fault, bool *writable)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001067{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001068 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001069 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001070 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001071
Gleb Natapovaf585b92010-10-14 11:22:46 +02001072 /* we can do it either atomically or asynchronously, not both */
1073 BUG_ON(atomic && async);
1074
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001075 BUG_ON(!write_fault && !writable);
1076
1077 if (writable)
1078 *writable = true;
1079
Gleb Natapovaf585b92010-10-14 11:22:46 +02001080 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001081 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001082
1083 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001084 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001085
1086 if (writable)
1087 *writable = write_fault;
1088
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001089 if (async) {
1090 down_read(&current->mm->mmap_sem);
1091 npages = get_user_page_nowait(current, current->mm,
1092 addr, write_fault, page);
1093 up_read(&current->mm->mmap_sem);
1094 } else
1095 npages = get_user_pages_fast(addr, 1, write_fault,
1096 page);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001097
1098 /* map read fault as writable if possible */
1099 if (unlikely(!write_fault) && npages == 1) {
1100 struct page *wpage[1];
1101
1102 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1103 if (npages == 1) {
1104 *writable = true;
1105 put_page(page[0]);
1106 page[0] = wpage[0];
1107 }
1108 npages = 1;
1109 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001110 }
Izik Eidus539cb662007-11-11 22:05:04 +02001111
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001112 if (unlikely(npages != 1)) {
1113 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001114
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001115 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001116 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001117
Huang Yingbbeb3402010-06-22 14:23:11 +08001118 down_read(&current->mm->mmap_sem);
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001119 if (npages == -EHWPOISON ||
1120 (!async && check_user_page_hwpoison(addr))) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001121 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001122 get_page(hwpoison_page);
1123 return page_to_pfn(hwpoison_page);
1124 }
1125
Gleb Natapov80300892010-10-19 18:13:41 +02001126 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001127
Gleb Natapov80300892010-10-19 18:13:41 +02001128 if (vma == NULL)
1129 pfn = get_fault_pfn();
1130 else if ((vma->vm_flags & VM_PFNMAP)) {
1131 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1132 vma->vm_pgoff;
1133 BUG_ON(!kvm_is_mmio_pfn(pfn));
1134 } else {
1135 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001136 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001137 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001138 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001139 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001140 } else
1141 pfn = page_to_pfn(page[0]);
1142
1143 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001144}
1145
Xiao Guangrongd5661042012-07-17 21:56:16 +08001146pfn_t hva_to_pfn_atomic(unsigned long addr)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001147{
Xiao Guangrongd5661042012-07-17 21:56:16 +08001148 return hva_to_pfn(addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001149}
1150EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1151
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001152static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1153 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001154{
1155 unsigned long addr;
1156
Gleb Natapovaf585b92010-10-14 11:22:46 +02001157 if (async)
1158 *async = false;
1159
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001160 addr = gfn_to_hva(kvm, gfn);
1161 if (kvm_is_error_hva(addr)) {
1162 get_page(bad_page);
1163 return page_to_pfn(bad_page);
1164 }
1165
Xiao Guangrongd5661042012-07-17 21:56:16 +08001166 return hva_to_pfn(addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001167}
1168
1169pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1170{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001171 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001172}
1173EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1174
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001175pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1176 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001177{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001178 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001179}
1180EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1181
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001182pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1183{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001184 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001185}
Anthony Liguori35149e22008-04-02 14:46:56 -05001186EXPORT_SYMBOL_GPL(gfn_to_pfn);
1187
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001188pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1189 bool *writable)
1190{
1191 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1192}
1193EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1194
Xiao Guangrongd5661042012-07-17 21:56:16 +08001195pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001196{
1197 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Xiao Guangrongd5661042012-07-17 21:56:16 +08001198 return hva_to_pfn(addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001199}
1200
Xiao Guangrong48987782010-08-22 19:11:43 +08001201int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1202 int nr_pages)
1203{
1204 unsigned long addr;
1205 gfn_t entry;
1206
Gleb Natapov49c77542010-10-18 15:22:23 +02001207 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001208 if (kvm_is_error_hva(addr))
1209 return -1;
1210
1211 if (entry < nr_pages)
1212 return 0;
1213
1214 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1215}
1216EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1217
Anthony Liguori35149e22008-04-02 14:46:56 -05001218struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1219{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001220 pfn_t pfn;
1221
1222 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001223 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001224 return pfn_to_page(pfn);
1225
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001226 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001227
1228 get_page(bad_page);
1229 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001230}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001231
Avi Kivity954bbbc2007-03-30 14:02:32 +03001232EXPORT_SYMBOL_GPL(gfn_to_page);
1233
Izik Eidusb4231d62007-11-20 11:49:33 +02001234void kvm_release_page_clean(struct page *page)
1235{
Anthony Liguori35149e22008-04-02 14:46:56 -05001236 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001237}
1238EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1239
Anthony Liguori35149e22008-04-02 14:46:56 -05001240void kvm_release_pfn_clean(pfn_t pfn)
1241{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001242 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001243 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001244}
1245EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1246
Izik Eidusb4231d62007-11-20 11:49:33 +02001247void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001248{
Anthony Liguori35149e22008-04-02 14:46:56 -05001249 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001250}
Izik Eidusb4231d62007-11-20 11:49:33 +02001251EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001252
Anthony Liguori35149e22008-04-02 14:46:56 -05001253void kvm_release_pfn_dirty(pfn_t pfn)
1254{
1255 kvm_set_pfn_dirty(pfn);
1256 kvm_release_pfn_clean(pfn);
1257}
1258EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1259
1260void kvm_set_page_dirty(struct page *page)
1261{
1262 kvm_set_pfn_dirty(page_to_pfn(page));
1263}
1264EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1265
1266void kvm_set_pfn_dirty(pfn_t pfn)
1267{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001268 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001269 struct page *page = pfn_to_page(pfn);
1270 if (!PageReserved(page))
1271 SetPageDirty(page);
1272 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001273}
1274EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1275
1276void kvm_set_pfn_accessed(pfn_t pfn)
1277{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001278 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001279 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001280}
1281EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1282
1283void kvm_get_pfn(pfn_t pfn)
1284{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001285 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001286 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001287}
1288EXPORT_SYMBOL_GPL(kvm_get_pfn);
1289
Izik Eidus195aefd2007-10-01 22:14:18 +02001290static int next_segment(unsigned long len, int offset)
1291{
1292 if (len > PAGE_SIZE - offset)
1293 return PAGE_SIZE - offset;
1294 else
1295 return len;
1296}
1297
1298int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1299 int len)
1300{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001301 int r;
1302 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001303
Izik Eiduse0506bc2007-11-11 22:10:22 +02001304 addr = gfn_to_hva(kvm, gfn);
1305 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001306 return -EFAULT;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001307 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001308 if (r)
1309 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001310 return 0;
1311}
1312EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1313
1314int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1315{
1316 gfn_t gfn = gpa >> PAGE_SHIFT;
1317 int seg;
1318 int offset = offset_in_page(gpa);
1319 int ret;
1320
1321 while ((seg = next_segment(len, offset)) != 0) {
1322 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1323 if (ret < 0)
1324 return ret;
1325 offset = 0;
1326 len -= seg;
1327 data += seg;
1328 ++gfn;
1329 }
1330 return 0;
1331}
1332EXPORT_SYMBOL_GPL(kvm_read_guest);
1333
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001334int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1335 unsigned long len)
1336{
1337 int r;
1338 unsigned long addr;
1339 gfn_t gfn = gpa >> PAGE_SHIFT;
1340 int offset = offset_in_page(gpa);
1341
1342 addr = gfn_to_hva(kvm, gfn);
1343 if (kvm_is_error_hva(addr))
1344 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001345 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001346 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001347 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001348 if (r)
1349 return -EFAULT;
1350 return 0;
1351}
1352EXPORT_SYMBOL(kvm_read_guest_atomic);
1353
Izik Eidus195aefd2007-10-01 22:14:18 +02001354int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1355 int offset, int len)
1356{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001357 int r;
1358 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001359
Izik Eiduse0506bc2007-11-11 22:10:22 +02001360 addr = gfn_to_hva(kvm, gfn);
1361 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001362 return -EFAULT;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001363 r = __copy_to_user((void __user *)addr + offset, data, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001364 if (r)
1365 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001366 mark_page_dirty(kvm, gfn);
1367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1370
1371int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1372 unsigned long len)
1373{
1374 gfn_t gfn = gpa >> PAGE_SHIFT;
1375 int seg;
1376 int offset = offset_in_page(gpa);
1377 int ret;
1378
1379 while ((seg = next_segment(len, offset)) != 0) {
1380 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1381 if (ret < 0)
1382 return ret;
1383 offset = 0;
1384 len -= seg;
1385 data += seg;
1386 ++gfn;
1387 }
1388 return 0;
1389}
1390
Gleb Natapov49c77542010-10-18 15:22:23 +02001391int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1392 gpa_t gpa)
1393{
1394 struct kvm_memslots *slots = kvm_memslots(kvm);
1395 int offset = offset_in_page(gpa);
1396 gfn_t gfn = gpa >> PAGE_SHIFT;
1397
1398 ghc->gpa = gpa;
1399 ghc->generation = slots->generation;
Paul Mackerras9d4cba72012-01-12 20:09:51 +00001400 ghc->memslot = gfn_to_memslot(kvm, gfn);
Gleb Natapov49c77542010-10-18 15:22:23 +02001401 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1402 if (!kvm_is_error_hva(ghc->hva))
1403 ghc->hva += offset;
1404 else
1405 return -EFAULT;
1406
1407 return 0;
1408}
1409EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1410
1411int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1412 void *data, unsigned long len)
1413{
1414 struct kvm_memslots *slots = kvm_memslots(kvm);
1415 int r;
1416
1417 if (slots->generation != ghc->generation)
1418 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1419
1420 if (kvm_is_error_hva(ghc->hva))
1421 return -EFAULT;
1422
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001423 r = __copy_to_user((void __user *)ghc->hva, data, len);
Gleb Natapov49c77542010-10-18 15:22:23 +02001424 if (r)
1425 return -EFAULT;
1426 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1427
1428 return 0;
1429}
1430EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1431
Gleb Natapove03b6442011-07-11 15:28:11 -04001432int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1433 void *data, unsigned long len)
1434{
1435 struct kvm_memslots *slots = kvm_memslots(kvm);
1436 int r;
1437
1438 if (slots->generation != ghc->generation)
1439 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1440
1441 if (kvm_is_error_hva(ghc->hva))
1442 return -EFAULT;
1443
1444 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1445 if (r)
1446 return -EFAULT;
1447
1448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1451
Izik Eidus195aefd2007-10-01 22:14:18 +02001452int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1453{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001454 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1455 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001456}
1457EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1458
1459int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1460{
1461 gfn_t gfn = gpa >> PAGE_SHIFT;
1462 int seg;
1463 int offset = offset_in_page(gpa);
1464 int ret;
1465
1466 while ((seg = next_segment(len, offset)) != 0) {
1467 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1468 if (ret < 0)
1469 return ret;
1470 offset = 0;
1471 len -= seg;
1472 ++gfn;
1473 }
1474 return 0;
1475}
1476EXPORT_SYMBOL_GPL(kvm_clear_guest);
1477
Gleb Natapov49c77542010-10-18 15:22:23 +02001478void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1479 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001481 if (memslot && memslot->dirty_bitmap) {
1482 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483
Takuya Yoshikawa93474b22012-03-01 19:34:45 +09001484 /* TODO: introduce set_bit_le() and use it */
1485 test_and_set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 }
1487}
1488
Gleb Natapov49c77542010-10-18 15:22:23 +02001489void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1490{
1491 struct kvm_memory_slot *memslot;
1492
1493 memslot = gfn_to_memslot(kvm, gfn);
1494 mark_page_dirty_in_slot(kvm, memslot, gfn);
1495}
1496
Eddie Dongb6958ce2007-07-18 12:15:21 +03001497/*
1498 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1499 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001500void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001501{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001502 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001503
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001504 for (;;) {
1505 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001506
Gleb Natapova1b37102009-07-09 15:33:52 +03001507 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001508 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001509 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001510 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001511 if (kvm_cpu_has_pending_timer(vcpu))
1512 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001513 if (signal_pending(current))
1514 break;
1515
Eddie Dongb6958ce2007-07-18 12:15:21 +03001516 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001517 }
1518
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001519 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001520}
1521
Marcelo Tosatti8c847802012-03-14 17:58:48 -03001522#ifndef CONFIG_S390
Christoffer Dallb6d33832012-03-08 16:44:24 -05001523/*
1524 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
1525 */
1526void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1527{
1528 int me;
1529 int cpu = vcpu->cpu;
1530 wait_queue_head_t *wqp;
1531
1532 wqp = kvm_arch_vcpu_wq(vcpu);
1533 if (waitqueue_active(wqp)) {
1534 wake_up_interruptible(wqp);
1535 ++vcpu->stat.halt_wakeup;
1536 }
1537
1538 me = get_cpu();
1539 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
1540 if (kvm_arch_vcpu_should_kick(vcpu))
1541 smp_send_reschedule(cpu);
1542 put_cpu();
1543}
Marcelo Tosatti8c847802012-03-14 17:58:48 -03001544#endif /* !CONFIG_S390 */
Christoffer Dallb6d33832012-03-08 16:44:24 -05001545
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546void kvm_resched(struct kvm_vcpu *vcpu)
1547{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001548 if (!need_resched())
1549 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551}
1552EXPORT_SYMBOL_GPL(kvm_resched);
1553
Konstantin Weitz41628d32012-04-25 15:30:38 +02001554bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
1555{
1556 struct pid *pid;
1557 struct task_struct *task = NULL;
1558
1559 rcu_read_lock();
1560 pid = rcu_dereference(target->pid);
1561 if (pid)
1562 task = get_pid_task(target->pid, PIDTYPE_PID);
1563 rcu_read_unlock();
1564 if (!task)
1565 return false;
1566 if (task->flags & PF_VCPU) {
1567 put_task_struct(task);
1568 return false;
1569 }
1570 if (yield_to(task, 1)) {
1571 put_task_struct(task);
1572 return true;
1573 }
1574 put_task_struct(task);
1575 return false;
1576}
1577EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
1578
Rik van Riel217ece62011-02-01 09:53:28 -05001579void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001580{
Rik van Riel217ece62011-02-01 09:53:28 -05001581 struct kvm *kvm = me->kvm;
1582 struct kvm_vcpu *vcpu;
1583 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1584 int yielded = 0;
1585 int pass;
1586 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001587
Rik van Riel217ece62011-02-01 09:53:28 -05001588 /*
1589 * We boost the priority of a VCPU that is runnable but not
1590 * currently running, because it got preempted by something
1591 * else and called schedule in __vcpu_run. Hopefully that
1592 * VCPU is holding the lock that we need and will release it.
1593 * We approximate round-robin by starting at the last boosted VCPU.
1594 */
1595 for (pass = 0; pass < 2 && !yielded; pass++) {
1596 kvm_for_each_vcpu(i, vcpu, kvm) {
Rik van Riel5cfc2aa2012-06-19 16:51:04 -04001597 if (!pass && i <= last_boosted_vcpu) {
Rik van Riel217ece62011-02-01 09:53:28 -05001598 i = last_boosted_vcpu;
1599 continue;
1600 } else if (pass && i > last_boosted_vcpu)
1601 break;
1602 if (vcpu == me)
1603 continue;
1604 if (waitqueue_active(&vcpu->wq))
1605 continue;
Konstantin Weitz41628d32012-04-25 15:30:38 +02001606 if (kvm_vcpu_yield_to(vcpu)) {
Rik van Riel217ece62011-02-01 09:53:28 -05001607 kvm->last_boosted_vcpu = i;
1608 yielded = 1;
1609 break;
1610 }
Rik van Riel217ece62011-02-01 09:53:28 -05001611 }
1612 }
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001613}
1614EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1615
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001616static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001617{
1618 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001619 struct page *page;
1620
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001621 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001622 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001623#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001624 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001625 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001626#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001627#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1628 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1629 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1630#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001631 else
Carsten Otte5b1c1492012-01-04 10:25:23 +01001632 return kvm_arch_vcpu_fault(vcpu, vmf);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001633 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001634 vmf->page = page;
1635 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001636}
1637
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001638static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001639 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001640};
1641
1642static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1643{
1644 vma->vm_ops = &kvm_vcpu_vm_ops;
1645 return 0;
1646}
1647
Avi Kivitybccf2152007-02-21 18:04:26 +02001648static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1649{
1650 struct kvm_vcpu *vcpu = filp->private_data;
1651
Al Viro66c0b392008-04-19 20:33:56 +01001652 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001653 return 0;
1654}
1655
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001656static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001657 .release = kvm_vcpu_release,
1658 .unlocked_ioctl = kvm_vcpu_ioctl,
Alexander Graf1dda6062011-06-08 02:45:37 +02001659#ifdef CONFIG_COMPAT
1660 .compat_ioctl = kvm_vcpu_compat_ioctl,
1661#endif
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001662 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001663 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001664};
1665
1666/*
1667 * Allocates an inode for the vcpu.
1668 */
1669static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1670{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001671 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001672}
1673
Avi Kivityc5ea7662007-02-20 18:41:05 +02001674/*
1675 * Creates some virtual cpus. Good luck creating more than one.
1676 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001677static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001678{
1679 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001680 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001681
Gleb Natapov73880c82009-06-09 15:56:28 +03001682 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001683 if (IS_ERR(vcpu))
1684 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001685
Avi Kivity15ad7142007-07-11 18:17:21 +03001686 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1687
Avi Kivity26e52152007-11-20 15:30:24 +02001688 r = kvm_arch_vcpu_setup(vcpu);
1689 if (r)
Jan Kiszkad7805922011-05-23 10:33:05 +02001690 goto vcpu_destroy;
Avi Kivity26e52152007-11-20 15:30:24 +02001691
Shaohua Li11ec2802007-07-23 14:51:37 +08001692 mutex_lock(&kvm->lock);
Avi Kivity3e515702012-03-05 14:23:29 +02001693 if (!kvm_vcpu_compatible(vcpu)) {
1694 r = -EINVAL;
1695 goto unlock_vcpu_destroy;
1696 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001697 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1698 r = -EINVAL;
Jan Kiszkad7805922011-05-23 10:33:05 +02001699 goto unlock_vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001700 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001701
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001702 kvm_for_each_vcpu(r, v, kvm)
1703 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001704 r = -EEXIST;
Jan Kiszkad7805922011-05-23 10:33:05 +02001705 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03001706 }
1707
1708 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001709
1710 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001711 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001712 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001713 if (r < 0) {
1714 kvm_put_kvm(kvm);
Jan Kiszkad7805922011-05-23 10:33:05 +02001715 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03001716 }
1717
1718 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1719 smp_wmb();
1720 atomic_inc(&kvm->online_vcpus);
1721
Gleb Natapov73880c82009-06-09 15:56:28 +03001722 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001723 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001724
Jan Kiszkad7805922011-05-23 10:33:05 +02001725unlock_vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001726 mutex_unlock(&kvm->lock);
Jan Kiszkad7805922011-05-23 10:33:05 +02001727vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001728 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001729 return r;
1730}
1731
Avi Kivity1961d272007-03-05 19:46:05 +02001732static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1733{
1734 if (sigset) {
1735 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1736 vcpu->sigset_active = 1;
1737 vcpu->sigset = *sigset;
1738 } else
1739 vcpu->sigset_active = 0;
1740 return 0;
1741}
1742
Avi Kivitybccf2152007-02-21 18:04:26 +02001743static long kvm_vcpu_ioctl(struct file *filp,
1744 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745{
Avi Kivitybccf2152007-02-21 18:04:26 +02001746 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001747 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001748 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001749 struct kvm_fpu *fpu = NULL;
1750 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001752 if (vcpu->kvm->mm != current->mm)
1753 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001754
1755#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1756 /*
1757 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1758 * so vcpu_load() would break it.
1759 */
1760 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1761 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1762#endif
1763
1764
1765 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001767 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001768 r = -EINVAL;
1769 if (arg)
1770 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001771 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001772 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001775 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001777 r = -ENOMEM;
1778 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1779 if (!kvm_regs)
1780 goto out;
1781 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001783 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001785 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1786 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001788out_free1:
1789 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 break;
1791 }
1792 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001793 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001795 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001796 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
1797 if (IS_ERR(kvm_regs)) {
1798 r = PTR_ERR(kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001799 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001800 }
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001801 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001803 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001805out_free2:
1806 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 break;
1808 }
1809 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001810 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1811 r = -ENOMEM;
1812 if (!kvm_sregs)
1813 goto out;
1814 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 if (r)
1816 goto out;
1817 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001818 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819 goto out;
1820 r = 0;
1821 break;
1822 }
1823 case KVM_SET_SREGS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02001824 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
1825 if (IS_ERR(kvm_sregs)) {
1826 r = PTR_ERR(kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001828 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07001829 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 if (r)
1831 goto out;
1832 r = 0;
1833 break;
1834 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001835 case KVM_GET_MP_STATE: {
1836 struct kvm_mp_state mp_state;
1837
1838 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1839 if (r)
1840 goto out;
1841 r = -EFAULT;
1842 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1843 goto out;
1844 r = 0;
1845 break;
1846 }
1847 case KVM_SET_MP_STATE: {
1848 struct kvm_mp_state mp_state;
1849
1850 r = -EFAULT;
1851 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1852 goto out;
1853 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1854 if (r)
1855 goto out;
1856 r = 0;
1857 break;
1858 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 case KVM_TRANSLATE: {
1860 struct kvm_translation tr;
1861
1862 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001863 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001865 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 if (r)
1867 goto out;
1868 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001869 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 goto out;
1871 r = 0;
1872 break;
1873 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001874 case KVM_SET_GUEST_DEBUG: {
1875 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876
1877 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001878 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001880 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 if (r)
1882 goto out;
1883 r = 0;
1884 break;
1885 }
Avi Kivity1961d272007-03-05 19:46:05 +02001886 case KVM_SET_SIGNAL_MASK: {
1887 struct kvm_signal_mask __user *sigmask_arg = argp;
1888 struct kvm_signal_mask kvm_sigmask;
1889 sigset_t sigset, *p;
1890
1891 p = NULL;
1892 if (argp) {
1893 r = -EFAULT;
1894 if (copy_from_user(&kvm_sigmask, argp,
1895 sizeof kvm_sigmask))
1896 goto out;
1897 r = -EINVAL;
1898 if (kvm_sigmask.len != sizeof sigset)
1899 goto out;
1900 r = -EFAULT;
1901 if (copy_from_user(&sigset, sigmask_arg->sigset,
1902 sizeof sigset))
1903 goto out;
1904 p = &sigset;
1905 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001906 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001907 break;
1908 }
Avi Kivityb8836732007-04-01 16:34:31 +03001909 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001910 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1911 r = -ENOMEM;
1912 if (!fpu)
1913 goto out;
1914 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001915 if (r)
1916 goto out;
1917 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001918 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001919 goto out;
1920 r = 0;
1921 break;
1922 }
1923 case KVM_SET_FPU: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02001924 fpu = memdup_user(argp, sizeof(*fpu));
1925 if (IS_ERR(fpu)) {
1926 r = PTR_ERR(fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001927 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001928 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07001929 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001930 if (r)
1931 goto out;
1932 r = 0;
1933 break;
1934 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001935 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001936 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001937 }
1938out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001939 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001940 kfree(fpu);
1941 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001942 return r;
1943}
1944
Alexander Graf1dda6062011-06-08 02:45:37 +02001945#ifdef CONFIG_COMPAT
1946static long kvm_vcpu_compat_ioctl(struct file *filp,
1947 unsigned int ioctl, unsigned long arg)
1948{
1949 struct kvm_vcpu *vcpu = filp->private_data;
1950 void __user *argp = compat_ptr(arg);
1951 int r;
1952
1953 if (vcpu->kvm->mm != current->mm)
1954 return -EIO;
1955
1956 switch (ioctl) {
1957 case KVM_SET_SIGNAL_MASK: {
1958 struct kvm_signal_mask __user *sigmask_arg = argp;
1959 struct kvm_signal_mask kvm_sigmask;
1960 compat_sigset_t csigset;
1961 sigset_t sigset;
1962
1963 if (argp) {
1964 r = -EFAULT;
1965 if (copy_from_user(&kvm_sigmask, argp,
1966 sizeof kvm_sigmask))
1967 goto out;
1968 r = -EINVAL;
1969 if (kvm_sigmask.len != sizeof csigset)
1970 goto out;
1971 r = -EFAULT;
1972 if (copy_from_user(&csigset, sigmask_arg->sigset,
1973 sizeof csigset))
1974 goto out;
1975 }
1976 sigset_from_compat(&sigset, &csigset);
1977 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1978 break;
1979 }
1980 default:
1981 r = kvm_vcpu_ioctl(filp, ioctl, arg);
1982 }
1983
1984out:
1985 return r;
1986}
1987#endif
1988
Avi Kivitybccf2152007-02-21 18:04:26 +02001989static long kvm_vm_ioctl(struct file *filp,
1990 unsigned int ioctl, unsigned long arg)
1991{
1992 struct kvm *kvm = filp->private_data;
1993 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001994 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001995
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001996 if (kvm->mm != current->mm)
1997 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001998 switch (ioctl) {
1999 case KVM_CREATE_VCPU:
2000 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2001 if (r < 0)
2002 goto out;
2003 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002004 case KVM_SET_USER_MEMORY_REGION: {
2005 struct kvm_userspace_memory_region kvm_userspace_mem;
2006
2007 r = -EFAULT;
2008 if (copy_from_user(&kvm_userspace_mem, argp,
2009 sizeof kvm_userspace_mem))
2010 goto out;
2011
2012 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013 if (r)
2014 goto out;
2015 break;
2016 }
2017 case KVM_GET_DIRTY_LOG: {
2018 struct kvm_dirty_log log;
2019
2020 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002021 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002023 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 if (r)
2025 goto out;
2026 break;
2027 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002028#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2029 case KVM_REGISTER_COALESCED_MMIO: {
2030 struct kvm_coalesced_mmio_zone zone;
2031 r = -EFAULT;
2032 if (copy_from_user(&zone, argp, sizeof zone))
2033 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02002034 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2035 if (r)
2036 goto out;
2037 r = 0;
2038 break;
2039 }
2040 case KVM_UNREGISTER_COALESCED_MMIO: {
2041 struct kvm_coalesced_mmio_zone zone;
2042 r = -EFAULT;
2043 if (copy_from_user(&zone, argp, sizeof zone))
2044 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02002045 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2046 if (r)
2047 goto out;
2048 r = 0;
2049 break;
2050 }
2051#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04002052 case KVM_IRQFD: {
2053 struct kvm_irqfd data;
2054
2055 r = -EFAULT;
2056 if (copy_from_user(&data, argp, sizeof data))
2057 goto out;
2058 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2059 break;
2060 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002061 case KVM_IOEVENTFD: {
2062 struct kvm_ioeventfd data;
2063
2064 r = -EFAULT;
2065 if (copy_from_user(&data, argp, sizeof data))
2066 goto out;
2067 r = kvm_ioeventfd(kvm, &data);
2068 break;
2069 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002070#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2071 case KVM_SET_BOOT_CPU_ID:
2072 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002073 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002074 if (atomic_read(&kvm->online_vcpus) != 0)
2075 r = -EBUSY;
2076 else
2077 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002078 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002079 break;
2080#endif
Jan Kiszka07975ad2012-03-29 21:14:12 +02002081#ifdef CONFIG_HAVE_KVM_MSI
2082 case KVM_SIGNAL_MSI: {
2083 struct kvm_msi msi;
2084
2085 r = -EFAULT;
2086 if (copy_from_user(&msi, argp, sizeof msi))
2087 goto out;
2088 r = kvm_send_userspace_msi(kvm, &msi);
2089 break;
2090 }
2091#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002092 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002093 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03002094 if (r == -ENOTTY)
2095 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002096 }
2097out:
2098 return r;
2099}
2100
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002101#ifdef CONFIG_COMPAT
2102struct compat_kvm_dirty_log {
2103 __u32 slot;
2104 __u32 padding1;
2105 union {
2106 compat_uptr_t dirty_bitmap; /* one bit per page */
2107 __u64 padding2;
2108 };
2109};
2110
2111static long kvm_vm_compat_ioctl(struct file *filp,
2112 unsigned int ioctl, unsigned long arg)
2113{
2114 struct kvm *kvm = filp->private_data;
2115 int r;
2116
2117 if (kvm->mm != current->mm)
2118 return -EIO;
2119 switch (ioctl) {
2120 case KVM_GET_DIRTY_LOG: {
2121 struct compat_kvm_dirty_log compat_log;
2122 struct kvm_dirty_log log;
2123
2124 r = -EFAULT;
2125 if (copy_from_user(&compat_log, (void __user *)arg,
2126 sizeof(compat_log)))
2127 goto out;
2128 log.slot = compat_log.slot;
2129 log.padding1 = compat_log.padding1;
2130 log.padding2 = compat_log.padding2;
2131 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2132
2133 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2134 if (r)
2135 goto out;
2136 break;
2137 }
2138 default:
2139 r = kvm_vm_ioctl(filp, ioctl, arg);
2140 }
2141
2142out:
2143 return r;
2144}
2145#endif
2146
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002147static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002148{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002149 struct page *page[1];
2150 unsigned long addr;
2151 int npages;
2152 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002153 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002154
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002155 addr = gfn_to_hva(kvm, gfn);
2156 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002157 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002158
2159 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2160 NULL);
2161 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002162 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002163
2164 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002165 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002166}
2167
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002168static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002169 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002170};
2171
2172static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2173{
2174 vma->vm_ops = &kvm_vm_vm_ops;
2175 return 0;
2176}
2177
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002178static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002179 .release = kvm_vm_release,
2180 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002181#ifdef CONFIG_COMPAT
2182 .compat_ioctl = kvm_vm_compat_ioctl,
2183#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002184 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002185 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002186};
2187
Carsten Ottee08b9632012-01-04 10:25:20 +01002188static int kvm_dev_ioctl_create_vm(unsigned long type)
Avi Kivityf17abe92007-02-21 19:28:04 +02002189{
Heiko Carstensaac87632010-10-27 17:22:10 +02002190 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002191 struct kvm *kvm;
2192
Carsten Ottee08b9632012-01-04 10:25:20 +01002193 kvm = kvm_create_vm(type);
Avi Kivityd6d28162007-06-28 08:38:16 -04002194 if (IS_ERR(kvm))
2195 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002196#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2197 r = kvm_coalesced_mmio_init(kvm);
2198 if (r < 0) {
2199 kvm_put_kvm(kvm);
2200 return r;
2201 }
2202#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002203 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2204 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002205 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002206
Heiko Carstensaac87632010-10-27 17:22:10 +02002207 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002208}
2209
Avi Kivity1a811b62008-12-08 18:25:27 +02002210static long kvm_dev_ioctl_check_extension_generic(long arg)
2211{
2212 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002213 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002214 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002215 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002216#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2217 case KVM_CAP_SET_BOOT_CPU_ID:
2218#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002219 case KVM_CAP_INTERNAL_ERROR_DATA:
Jan Kiszka07975ad2012-03-29 21:14:12 +02002220#ifdef CONFIG_HAVE_KVM_MSI
2221 case KVM_CAP_SIGNAL_MSI:
2222#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002223 return 1;
Marc Zyngier9900b4b2012-06-15 15:07:02 -04002224#ifdef KVM_CAP_IRQ_ROUTING
Avi Kivity399ec802008-11-19 13:58:46 +02002225 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002226 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002227#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002228 default:
2229 break;
2230 }
2231 return kvm_dev_ioctl_check_extension(arg);
2232}
2233
Avi Kivityf17abe92007-02-21 19:28:04 +02002234static long kvm_dev_ioctl(struct file *filp,
2235 unsigned int ioctl, unsigned long arg)
2236{
Avi Kivity07c45a32007-03-07 13:05:38 +02002237 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002238
2239 switch (ioctl) {
2240 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002241 r = -EINVAL;
2242 if (arg)
2243 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002244 r = KVM_API_VERSION;
2245 break;
2246 case KVM_CREATE_VM:
Carsten Ottee08b9632012-01-04 10:25:20 +01002247 r = kvm_dev_ioctl_create_vm(arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002248 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002249 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002250 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002251 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002252 case KVM_GET_VCPU_MMAP_SIZE:
2253 r = -EINVAL;
2254 if (arg)
2255 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002256 r = PAGE_SIZE; /* struct kvm_run */
2257#ifdef CONFIG_X86
2258 r += PAGE_SIZE; /* pio data page */
2259#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002260#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2261 r += PAGE_SIZE; /* coalesced mmio ring page */
2262#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002263 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002264 case KVM_TRACE_ENABLE:
2265 case KVM_TRACE_PAUSE:
2266 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002267 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002268 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002270 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 }
2272out:
2273 return r;
2274}
2275
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277 .unlocked_ioctl = kvm_dev_ioctl,
2278 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002279 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280};
2281
2282static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002283 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 "kvm",
2285 &kvm_chardev_ops,
2286};
2287
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002288static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002289{
2290 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002291 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002292
Rusty Russell7f59f492008-12-07 21:25:45 +10302293 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002294 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002295
Rusty Russell7f59f492008-12-07 21:25:45 +10302296 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002297
2298 r = kvm_arch_hardware_enable(NULL);
2299
2300 if (r) {
2301 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2302 atomic_inc(&hardware_enable_failed);
2303 printk(KERN_INFO "kvm: enabling virtualization on "
2304 "CPU%d failed\n", cpu);
2305 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002306}
2307
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002308static void hardware_enable(void *junk)
2309{
Jan Kiszkae935b832011-02-08 12:55:33 +01002310 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002311 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002312 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002313}
2314
2315static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002316{
2317 int cpu = raw_smp_processor_id();
2318
Rusty Russell7f59f492008-12-07 21:25:45 +10302319 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002320 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302321 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002322 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002323}
2324
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002325static void hardware_disable(void *junk)
2326{
Jan Kiszkae935b832011-02-08 12:55:33 +01002327 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002328 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002329 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002330}
2331
Alexander Graf10474ae2009-09-15 11:37:46 +02002332static void hardware_disable_all_nolock(void)
2333{
2334 BUG_ON(!kvm_usage_count);
2335
2336 kvm_usage_count--;
2337 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002338 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002339}
2340
2341static void hardware_disable_all(void)
2342{
Jan Kiszkae935b832011-02-08 12:55:33 +01002343 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002344 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002345 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002346}
2347
2348static int hardware_enable_all(void)
2349{
2350 int r = 0;
2351
Jan Kiszkae935b832011-02-08 12:55:33 +01002352 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002353
2354 kvm_usage_count++;
2355 if (kvm_usage_count == 1) {
2356 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002357 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002358
2359 if (atomic_read(&hardware_enable_failed)) {
2360 hardware_disable_all_nolock();
2361 r = -EBUSY;
2362 }
2363 }
2364
Jan Kiszkae935b832011-02-08 12:55:33 +01002365 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002366
2367 return r;
2368}
2369
Avi Kivity774c47f2007-02-12 00:54:47 -08002370static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2371 void *v)
2372{
2373 int cpu = (long)v;
2374
Alexander Graf10474ae2009-09-15 11:37:46 +02002375 if (!kvm_usage_count)
2376 return NOTIFY_OK;
2377
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002378 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002379 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002380 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002381 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2382 cpu);
2383 hardware_disable(NULL);
2384 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002385 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002386 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2387 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002388 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002389 break;
2390 }
2391 return NOTIFY_OK;
2392}
2393
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002394
Avi Kivityb7c41452010-12-02 17:52:50 +02002395asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002396{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002397 /* Fault while not rebooting. We want the trace. */
2398 BUG();
2399}
Avi Kivityb7c41452010-12-02 17:52:50 +02002400EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002401
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002402static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002403 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002404{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002405 /*
2406 * Some (well, at least mine) BIOSes hang on reboot if
2407 * in vmx root mode.
2408 *
2409 * And Intel TXT required VMX off for all cpu when system shutdown.
2410 */
2411 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2412 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002413 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002414 return NOTIFY_OK;
2415}
2416
2417static struct notifier_block kvm_reboot_notifier = {
2418 .notifier_call = kvm_reboot,
2419 .priority = 0,
2420};
2421
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002422static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002423{
2424 int i;
2425
2426 for (i = 0; i < bus->dev_count; i++) {
Sasha Levin743eeb02011-07-27 16:00:48 +03002427 struct kvm_io_device *pos = bus->range[i].dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002428
2429 kvm_iodevice_destructor(pos);
2430 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002431 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002432}
2433
Sasha Levin743eeb02011-07-27 16:00:48 +03002434int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2435{
2436 const struct kvm_io_range *r1 = p1;
2437 const struct kvm_io_range *r2 = p2;
2438
2439 if (r1->addr < r2->addr)
2440 return -1;
2441 if (r1->addr + r1->len > r2->addr + r2->len)
2442 return 1;
2443 return 0;
2444}
2445
2446int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
2447 gpa_t addr, int len)
2448{
Sasha Levin743eeb02011-07-27 16:00:48 +03002449 bus->range[bus->dev_count++] = (struct kvm_io_range) {
2450 .addr = addr,
2451 .len = len,
2452 .dev = dev,
2453 };
2454
2455 sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
2456 kvm_io_bus_sort_cmp, NULL);
2457
2458 return 0;
2459}
2460
2461int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
2462 gpa_t addr, int len)
2463{
2464 struct kvm_io_range *range, key;
2465 int off;
2466
2467 key = (struct kvm_io_range) {
2468 .addr = addr,
2469 .len = len,
2470 };
2471
2472 range = bsearch(&key, bus->range, bus->dev_count,
2473 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
2474 if (range == NULL)
2475 return -ENOENT;
2476
2477 off = range - bus->range;
2478
2479 while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
2480 off--;
2481
2482 return off;
2483}
2484
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002485/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002486int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002487 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002488{
Sasha Levin743eeb02011-07-27 16:00:48 +03002489 int idx;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002490 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03002491 struct kvm_io_range range;
2492
2493 range = (struct kvm_io_range) {
2494 .addr = addr,
2495 .len = len,
2496 };
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002497
2498 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Sasha Levin743eeb02011-07-27 16:00:48 +03002499 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2500 if (idx < 0)
2501 return -EOPNOTSUPP;
2502
2503 while (idx < bus->dev_count &&
2504 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2505 if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002506 return 0;
Sasha Levin743eeb02011-07-27 16:00:48 +03002507 idx++;
2508 }
2509
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002510 return -EOPNOTSUPP;
2511}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002512
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002513/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002514int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2515 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002516{
Sasha Levin743eeb02011-07-27 16:00:48 +03002517 int idx;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002518 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03002519 struct kvm_io_range range;
2520
2521 range = (struct kvm_io_range) {
2522 .addr = addr,
2523 .len = len,
2524 };
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002525
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002526 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Sasha Levin743eeb02011-07-27 16:00:48 +03002527 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2528 if (idx < 0)
2529 return -EOPNOTSUPP;
2530
2531 while (idx < bus->dev_count &&
2532 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2533 if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002534 return 0;
Sasha Levin743eeb02011-07-27 16:00:48 +03002535 idx++;
2536 }
2537
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002538 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002539}
2540
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002541/* Caller must hold slots_lock. */
Sasha Levin743eeb02011-07-27 16:00:48 +03002542int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2543 int len, struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002544{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002545 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002546
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002547 bus = kvm->buses[bus_idx];
Amos Konga13007162012-03-09 12:17:32 +08002548 if (bus->dev_count > NR_IOBUS_DEVS - 1)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002549 return -ENOSPC;
2550
Amos Konga13007162012-03-09 12:17:32 +08002551 new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
2552 sizeof(struct kvm_io_range)), GFP_KERNEL);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002553 if (!new_bus)
2554 return -ENOMEM;
Amos Konga13007162012-03-09 12:17:32 +08002555 memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
2556 sizeof(struct kvm_io_range)));
Sasha Levin743eeb02011-07-27 16:00:48 +03002557 kvm_io_bus_insert_dev(new_bus, dev, addr, len);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002558 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2559 synchronize_srcu_expedited(&kvm->srcu);
2560 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002561
2562 return 0;
2563}
2564
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002565/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002566int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2567 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002568{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002569 int i, r;
2570 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002571
Sasha Levincdfca7b2011-12-04 19:36:28 +02002572 bus = kvm->buses[bus_idx];
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002573 r = -ENOENT;
Amos Konga13007162012-03-09 12:17:32 +08002574 for (i = 0; i < bus->dev_count; i++)
2575 if (bus->range[i].dev == dev) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002576 r = 0;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002577 break;
2578 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002579
Amos Konga13007162012-03-09 12:17:32 +08002580 if (r)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002581 return r;
Amos Konga13007162012-03-09 12:17:32 +08002582
2583 new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
2584 sizeof(struct kvm_io_range)), GFP_KERNEL);
2585 if (!new_bus)
2586 return -ENOMEM;
2587
2588 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
2589 new_bus->dev_count--;
2590 memcpy(new_bus->range + i, bus->range + i + 1,
2591 (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002592
2593 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2594 synchronize_srcu_expedited(&kvm->srcu);
2595 kfree(bus);
2596 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002597}
2598
Avi Kivity774c47f2007-02-12 00:54:47 -08002599static struct notifier_block kvm_cpu_notifier = {
2600 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002601};
2602
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002603static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002604{
2605 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002606 struct kvm *kvm;
2607
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002608 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002609 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002610 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002611 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002612 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002613 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002614}
2615
2616DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2617
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002618static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002619{
2620 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002621 struct kvm *kvm;
2622 struct kvm_vcpu *vcpu;
2623 int i;
2624
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002625 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002626 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002627 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002628 kvm_for_each_vcpu(i, vcpu, kvm)
2629 *val += *(u32 *)((void *)vcpu + offset);
2630
Jan Kiszkae935b832011-02-08 12:55:33 +01002631 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002632 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002633}
2634
Avi Kivityba1389b2007-11-18 16:24:12 +02002635DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2636
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002637static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002638 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2639 [KVM_STAT_VM] = &vm_stat_fops,
2640};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002641
Hamo4f69b682011-12-15 14:23:16 +08002642static int kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643{
Hamo4f69b682011-12-15 14:23:16 +08002644 int r = -EFAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645 struct kvm_stats_debugfs_item *p;
2646
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002647 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Hamo4f69b682011-12-15 14:23:16 +08002648 if (kvm_debugfs_dir == NULL)
2649 goto out;
2650
2651 for (p = debugfs_entries; p->name; ++p) {
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002652 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002653 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002654 stat_fops[p->kind]);
Hamo4f69b682011-12-15 14:23:16 +08002655 if (p->dentry == NULL)
2656 goto out_dir;
2657 }
2658
2659 return 0;
2660
2661out_dir:
2662 debugfs_remove_recursive(kvm_debugfs_dir);
2663out:
2664 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665}
2666
2667static void kvm_exit_debug(void)
2668{
2669 struct kvm_stats_debugfs_item *p;
2670
2671 for (p = debugfs_entries; p->name; ++p)
2672 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002673 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674}
2675
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002676static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002677{
Alexander Graf10474ae2009-09-15 11:37:46 +02002678 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002679 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002680 return 0;
2681}
2682
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002683static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002684{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002685 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002686 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002687 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002688 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002689}
2690
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002691static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08002692 .suspend = kvm_suspend,
2693 .resume = kvm_resume,
2694};
2695
Avi Kivity15ad7142007-07-11 18:17:21 +03002696static inline
2697struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2698{
2699 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2700}
2701
2702static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2703{
2704 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2705
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002706 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002707}
2708
2709static void kvm_sched_out(struct preempt_notifier *pn,
2710 struct task_struct *next)
2711{
2712 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2713
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002714 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002715}
2716
Avi Kivity0ee75be2010-04-28 15:39:01 +03002717int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002718 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719{
2720 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002721 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002723 r = kvm_arch_init(opaque);
2724 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002725 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002726
2727 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2728
2729 if (bad_page == NULL) {
2730 r = -ENOMEM;
2731 goto out;
2732 }
2733
Anthony Liguori35149e22008-04-02 14:46:56 -05002734 bad_pfn = page_to_pfn(bad_page);
2735
Huang Yingbf998152010-05-31 14:28:19 +08002736 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2737
2738 if (hwpoison_page == NULL) {
2739 r = -ENOMEM;
2740 goto out_free_0;
2741 }
2742
2743 hwpoison_pfn = page_to_pfn(hwpoison_page);
2744
Gleb Natapovedba23e2010-07-07 20:16:45 +03002745 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2746
2747 if (fault_page == NULL) {
2748 r = -ENOMEM;
2749 goto out_free_0;
2750 }
2751
2752 fault_pfn = page_to_pfn(fault_page);
2753
Avi Kivity8437a6172009-06-06 14:52:35 -07002754 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302755 r = -ENOMEM;
2756 goto out_free_0;
2757 }
2758
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002759 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302761 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762
Yang, Sheng002c7f72007-07-31 14:23:01 +03002763 for_each_online_cpu(cpu) {
2764 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002765 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002766 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002767 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002768 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002769 }
2770
Avi Kivity774c47f2007-02-12 00:54:47 -08002771 r = register_cpu_notifier(&kvm_cpu_notifier);
2772 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002773 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 register_reboot_notifier(&kvm_reboot_notifier);
2775
Rusty Russellc16f8622007-07-30 21:12:19 +10002776 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002777 if (!vcpu_align)
2778 vcpu_align = __alignof__(struct kvm_vcpu);
2779 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002780 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002781 if (!kvm_vcpu_cache) {
2782 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002783 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10002784 }
2785
Gleb Natapovaf585b92010-10-14 11:22:46 +02002786 r = kvm_async_pf_init();
2787 if (r)
2788 goto out_free;
2789
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002791 kvm_vm_fops.owner = module;
2792 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793
2794 r = misc_register(&kvm_dev);
2795 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002796 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002797 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 }
2799
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002800 register_syscore_ops(&kvm_syscore_ops);
2801
Avi Kivity15ad7142007-07-11 18:17:21 +03002802 kvm_preempt_ops.sched_in = kvm_sched_in;
2803 kvm_preempt_ops.sched_out = kvm_sched_out;
2804
Hamo4f69b682011-12-15 14:23:16 +08002805 r = kvm_init_debug();
2806 if (r) {
2807 printk(KERN_ERR "kvm: create debugfs files failed\n");
2808 goto out_undebugfs;
2809 }
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002810
Avi Kivityc7addb92007-09-16 18:58:32 +02002811 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812
Hamo4f69b682011-12-15 14:23:16 +08002813out_undebugfs:
2814 unregister_syscore_ops(&kvm_syscore_ops);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002815out_unreg:
2816 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002818 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002819out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002821 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002822out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002823out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002824 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302825out_free_0a:
2826 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002827out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002828 if (fault_page)
2829 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002830 if (hwpoison_page)
2831 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002832 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002833out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002834 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002835out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 return r;
2837}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002838EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002840void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002842 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002844 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002845 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002846 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002848 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002849 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002850 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002851 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302852 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002853 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002854 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002856EXPORT_SYMBOL_GPL(kvm_exit);