blob: 1e10ebe1a3706378766de8fae728d783b4ebefc9 [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
Raghavendra K T4c088492012-07-18 19:07:46 +0530242 kvm_vcpu_set_in_spin_loop(vcpu, false);
243 kvm_vcpu_set_dy_eligible(vcpu, false);
244
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800245 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000246 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800247 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000248 return 0;
249
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000250fail_free_run:
251 free_page((unsigned long)vcpu->run);
252fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000253 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000254}
255EXPORT_SYMBOL_GPL(kvm_vcpu_init);
256
257void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
258{
Rik van Riel34bb10b2011-02-01 09:52:41 -0500259 put_pid(vcpu->pid);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800260 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000261 free_page((unsigned long)vcpu->run);
262}
263EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
264
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200265#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
266static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
267{
268 return container_of(mn, struct kvm, mmu_notifier);
269}
270
271static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
272 struct mm_struct *mm,
273 unsigned long address)
274{
275 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200276 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200277
278 /*
279 * When ->invalidate_page runs, the linux pte has been zapped
280 * already but the page is still allocated until
281 * ->invalidate_page returns. So if we increase the sequence
282 * here the kvm page fault will notice if the spte can't be
283 * established because the page is going to be freed. If
284 * instead the kvm page fault establishes the spte before
285 * ->invalidate_page runs, kvm_unmap_hva will release it
286 * before returning.
287 *
288 * The sequence increase only need to be seen at spin_unlock
289 * time, and not at spin_lock time.
290 *
291 * Increasing the sequence after the spin_unlock would be
292 * unsafe because the kvm page fault could then establish the
293 * pte after kvm_unmap_hva returned, without noticing the page
294 * is going to be freed.
295 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200296 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200297 spin_lock(&kvm->mmu_lock);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900298
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200299 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800300 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200301 /* we've to flush the tlb before the pages can be freed */
302 if (need_tlb_flush)
303 kvm_flush_remote_tlbs(kvm);
304
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900305 spin_unlock(&kvm->mmu_lock);
306 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200307}
308
Izik Eidus3da0dd42009-09-23 21:47:18 +0300309static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
310 struct mm_struct *mm,
311 unsigned long address,
312 pte_t pte)
313{
314 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200315 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300316
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200317 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300318 spin_lock(&kvm->mmu_lock);
319 kvm->mmu_notifier_seq++;
320 kvm_set_spte_hva(kvm, address, pte);
321 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200322 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300323}
324
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200325static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
326 struct mm_struct *mm,
327 unsigned long start,
328 unsigned long end)
329{
330 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200331 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200332
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200333 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200334 spin_lock(&kvm->mmu_lock);
335 /*
336 * The count increase must become visible at unlock time as no
337 * spte can be established without taking the mmu_lock and
338 * count is also read inside the mmu_lock critical section.
339 */
340 kvm->mmu_notifier_count++;
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +0900341 need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800342 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200343 /* we've to flush the tlb before the pages can be freed */
344 if (need_tlb_flush)
345 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900346
347 spin_unlock(&kvm->mmu_lock);
348 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200349}
350
351static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
352 struct mm_struct *mm,
353 unsigned long start,
354 unsigned long end)
355{
356 struct kvm *kvm = mmu_notifier_to_kvm(mn);
357
358 spin_lock(&kvm->mmu_lock);
359 /*
360 * This sequence increase will notify the kvm page fault that
361 * the page that is going to be mapped in the spte could have
362 * been freed.
363 */
364 kvm->mmu_notifier_seq++;
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000365 smp_wmb();
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200366 /*
367 * The above sequence increase must be visible before the
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000368 * below count decrease, which is ensured by the smp_wmb above
369 * in conjunction with the smp_rmb in mmu_notifier_retry().
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200370 */
371 kvm->mmu_notifier_count--;
372 spin_unlock(&kvm->mmu_lock);
373
374 BUG_ON(kvm->mmu_notifier_count < 0);
375}
376
377static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
378 struct mm_struct *mm,
379 unsigned long address)
380{
381 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200382 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200383
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200384 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200385 spin_lock(&kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200386
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900387 young = kvm_age_hva(kvm, address);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200388 if (young)
389 kvm_flush_remote_tlbs(kvm);
390
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900391 spin_unlock(&kvm->mmu_lock);
392 srcu_read_unlock(&kvm->srcu, idx);
393
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200394 return young;
395}
396
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800397static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
398 struct mm_struct *mm,
399 unsigned long address)
400{
401 struct kvm *kvm = mmu_notifier_to_kvm(mn);
402 int young, idx;
403
404 idx = srcu_read_lock(&kvm->srcu);
405 spin_lock(&kvm->mmu_lock);
406 young = kvm_test_age_hva(kvm, address);
407 spin_unlock(&kvm->mmu_lock);
408 srcu_read_unlock(&kvm->srcu, idx);
409
410 return young;
411}
412
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100413static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
414 struct mm_struct *mm)
415{
416 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800417 int idx;
418
419 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100420 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800421 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100422}
423
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200424static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
425 .invalidate_page = kvm_mmu_notifier_invalidate_page,
426 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
427 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
428 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800429 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300430 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100431 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200432};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200433
434static int kvm_init_mmu_notifier(struct kvm *kvm)
435{
436 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
437 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
438}
439
440#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
441
442static int kvm_init_mmu_notifier(struct kvm *kvm)
443{
444 return 0;
445}
446
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200447#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
448
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800449static void kvm_init_memslots_id(struct kvm *kvm)
450{
451 int i;
452 struct kvm_memslots *slots = kvm->memslots;
453
454 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800455 slots->id_to_index[i] = slots->memslots[i].id = i;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800456}
457
Carsten Ottee08b9632012-01-04 10:25:20 +0100458static struct kvm *kvm_create_vm(unsigned long type)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100460 int r, i;
461 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100463 if (!kvm)
464 return ERR_PTR(-ENOMEM);
465
Carsten Ottee08b9632012-01-04 10:25:20 +0100466 r = kvm_arch_init_vm(kvm, type);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100467 if (r)
468 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200469
470 r = hardware_enable_all();
471 if (r)
472 goto out_err_nodisable;
473
Avi Kivity75858a82009-01-04 17:10:50 +0200474#ifdef CONFIG_HAVE_KVM_IRQCHIP
475 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300476 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200477#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200479 r = -ENOMEM;
480 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
481 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100482 goto out_err_nosrcu;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800483 kvm_init_memslots_id(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200484 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100485 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200486 for (i = 0; i < KVM_NR_BUSES; i++) {
487 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
488 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100489 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200490 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200491 }
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700492
OGAWA Hirofumi85722cd2011-05-11 09:28:28 +0900493 spin_lock_init(&kvm->mmu_lock);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200494 kvm->mm = current->mm;
495 atomic_inc(&kvm->mm->mm_count);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400496 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800497 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300498 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200499 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300500 atomic_set(&kvm->users_count, 1);
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700501
502 r = kvm_init_mmu_notifier(kvm);
503 if (r)
504 goto out_err;
505
Jan Kiszkae935b832011-02-08 12:55:33 +0100506 raw_spin_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000507 list_add(&kvm->vm_list, &vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100508 raw_spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100509
Avi Kivityf17abe92007-02-21 19:28:04 +0200510 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200511
512out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100513 cleanup_srcu_struct(&kvm->srcu);
514out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200515 hardware_disable_all();
516out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200517 for (i = 0; i < KVM_NR_BUSES; i++)
518 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200519 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100520 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200521 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200522}
523
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900524/*
525 * Avoid using vmalloc for a small buffer.
526 * Should not be used when the size is statically known.
527 */
Takuya Yoshikawac1a7b322012-05-20 13:15:07 +0900528void *kvm_kvzalloc(unsigned long size)
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900529{
530 if (size > PAGE_SIZE)
531 return vzalloc(size);
532 else
533 return kzalloc(size, GFP_KERNEL);
534}
535
Takuya Yoshikawac1a7b322012-05-20 13:15:07 +0900536void kvm_kvfree(const void *addr)
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900537{
538 if (is_vmalloc_addr(addr))
539 vfree(addr);
540 else
541 kfree(addr);
542}
543
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900544static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
545{
546 if (!memslot->dirty_bitmap)
547 return;
548
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900549 kvm_kvfree(memslot->dirty_bitmap);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900550 memslot->dirty_bitmap = NULL;
551}
552
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553/*
554 * Free any memory in @free but not in @dont.
555 */
556static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
557 struct kvm_memory_slot *dont)
558{
Izik Eidus290fc382007-09-27 14:11:22 +0200559 if (!dont || free->rmap != dont->rmap)
560 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
562 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900563 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900565 kvm_arch_free_memslot(free, dont);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300566
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500568 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569}
570
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800571void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572{
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200573 struct kvm_memslots *slots = kvm->memslots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +0800574 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +0800576 kvm_for_each_memslot(memslot, slots)
577 kvm_free_physmem_slot(memslot, NULL);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200578
579 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580}
581
Avi Kivityf17abe92007-02-21 19:28:04 +0200582static void kvm_destroy_vm(struct kvm *kvm)
583{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200584 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200585 struct mm_struct *mm = kvm->mm;
586
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800587 kvm_arch_sync_events(kvm);
Jan Kiszkae935b832011-02-08 12:55:33 +0100588 raw_spin_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800589 list_del(&kvm->vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100590 raw_spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200591 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200592 for (i = 0; i < KVM_NR_BUSES; i++)
593 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200594 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200595#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
596 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200597#else
598 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200599#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800600 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100601 kvm_free_physmem(kvm);
602 cleanup_srcu_struct(&kvm->srcu);
603 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200604 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200605 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200606}
607
Izik Eidusd39f13b2008-03-30 16:01:25 +0300608void kvm_get_kvm(struct kvm *kvm)
609{
610 atomic_inc(&kvm->users_count);
611}
612EXPORT_SYMBOL_GPL(kvm_get_kvm);
613
614void kvm_put_kvm(struct kvm *kvm)
615{
616 if (atomic_dec_and_test(&kvm->users_count))
617 kvm_destroy_vm(kvm);
618}
619EXPORT_SYMBOL_GPL(kvm_put_kvm);
620
621
Avi Kivityf17abe92007-02-21 19:28:04 +0200622static int kvm_vm_release(struct inode *inode, struct file *filp)
623{
624 struct kvm *kvm = filp->private_data;
625
Gregory Haskins721eecb2009-05-20 10:30:49 -0400626 kvm_irqfd_release(kvm);
627
Izik Eidusd39f13b2008-03-30 16:01:25 +0300628 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 return 0;
630}
631
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900632/*
633 * Allocation size is twice as large as the actual dirty bitmap size.
Takuya Yoshikawa93474b22012-03-01 19:34:45 +0900634 * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900635 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900636static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
637{
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900638#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900639 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900640
Takuya Yoshikawa92eca8f2012-05-20 13:13:28 +0900641 memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900642 if (!memslot->dirty_bitmap)
643 return -ENOMEM;
644
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900645#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900646 return 0;
647}
648
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800649static int cmp_memslot(const void *slot1, const void *slot2)
650{
651 struct kvm_memory_slot *s1, *s2;
652
653 s1 = (struct kvm_memory_slot *)slot1;
654 s2 = (struct kvm_memory_slot *)slot2;
655
656 if (s1->npages < s2->npages)
657 return 1;
658 if (s1->npages > s2->npages)
659 return -1;
660
661 return 0;
662}
663
664/*
665 * Sort the memslots base on its size, so the larger slots
666 * will get better fit.
667 */
668static void sort_memslots(struct kvm_memslots *slots)
669{
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800670 int i;
671
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800672 sort(slots->memslots, KVM_MEM_SLOTS_NUM,
673 sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800674
675 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
676 slots->id_to_index[slots->memslots[i].id] = i;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800677}
678
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800679void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
680{
681 if (new) {
682 int id = new->id;
Xiao Guangrong28a37542011-11-24 19:04:35 +0800683 struct kvm_memory_slot *old = id_to_memslot(slots, id);
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800684 unsigned long npages = old->npages;
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800685
Xiao Guangrong28a37542011-11-24 19:04:35 +0800686 *old = *new;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800687 if (new->npages != npages)
688 sort_memslots(slots);
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800689 }
690
691 slots->generation++;
692}
693
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 * Allocate some memory and give it an address in the guest physical address
696 * space.
697 *
698 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800699 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500700 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800702int __kvm_set_memory_region(struct kvm *kvm,
703 struct kvm_userspace_memory_region *mem,
704 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705{
Avi Kivity8234b222010-12-27 12:08:45 +0200706 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200708 unsigned long npages;
709 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 struct kvm_memory_slot *memslot;
711 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200712 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713
714 r = -EINVAL;
715 /* General sanity checks */
716 if (mem->memory_size & (PAGE_SIZE - 1))
717 goto out;
718 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
719 goto out;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +0900720 /* We can read the guest memory with __xxx_user() later on. */
721 if (user_alloc &&
722 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Heiko Carstens9e3bb6b2011-05-24 07:51:27 +0200723 !access_ok(VERIFY_WRITE,
724 (void __user *)(unsigned long)mem->userspace_addr,
725 mem->memory_size)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600726 goto out;
Xiao Guangrong93a5cef2011-11-24 17:37:48 +0800727 if (mem->slot >= KVM_MEM_SLOTS_NUM)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 goto out;
729 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
730 goto out;
731
Xiao Guangrong28a37542011-11-24 19:04:35 +0800732 memslot = id_to_memslot(kvm->memslots, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
734 npages = mem->memory_size >> PAGE_SHIFT;
735
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900736 r = -EINVAL;
737 if (npages > KVM_MEM_MAX_NR_PAGES)
738 goto out;
739
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 if (!npages)
741 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
742
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 new = old = *memslot;
744
Avi Kivitye36d96f2010-06-21 10:56:36 +0300745 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 new.base_gfn = base_gfn;
747 new.npages = npages;
748 new.flags = mem->flags;
749
750 /* Disallow changing a memory slot's size. */
751 r = -EINVAL;
752 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800753 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754
755 /* Check for overlaps */
756 r = -EEXIST;
757 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200758 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200760 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761 continue;
762 if (!((base_gfn + npages <= s->base_gfn) ||
763 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800764 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 /* Free page dirty bitmap if unneeded */
768 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000769 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770
771 r = -ENOMEM;
772
773 /* Allocate if a slot is being created */
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900774 if (npages && !old.npages) {
775 new.user_alloc = user_alloc;
776 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200777#ifndef CONFIG_S390
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900778 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200779 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800780 goto out_free;
Takuya Yoshikawa189a2f72012-02-08 13:01:09 +0900781#endif /* not defined CONFIG_S390 */
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900782 if (kvm_arch_create_memslot(&new, npages))
783 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200785
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 /* Allocate page dirty bitmap if needed */
787 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900788 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800789 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200790 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 }
792
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200793 if (!npages) {
Xiao Guangrong28a37542011-11-24 19:04:35 +0800794 struct kvm_memory_slot *slot;
795
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200796 r = -ENOMEM;
Thomas Meyer6da64fd2011-11-08 20:32:19 +0100797 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
798 GFP_KERNEL);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200799 if (!slots)
800 goto out_free;
Xiao Guangrong28a37542011-11-24 19:04:35 +0800801 slot = id_to_memslot(slots, mem->slot);
802 slot->flags |= KVM_MEMSLOT_INVALID;
803
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800804 update_memslots(slots, NULL);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200805
806 old_memslots = kvm->memslots;
807 rcu_assign_pointer(kvm->memslots, slots);
808 synchronize_srcu_expedited(&kvm->srcu);
809 /* From this point no new shadow pages pointing to a deleted
810 * memslot will be created.
811 *
812 * validation of sp->gfn happens in:
813 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
814 * - kvm_is_visible_gfn (mmu_check_roots)
815 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300816 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200817 kfree(old_memslots);
818 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300819
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200820 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
821 if (r)
822 goto out_free;
823
Alex Williamson32f6daa2012-04-11 09:51:49 -0600824 /* map/unmap the pages in iommu page table */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200825 if (npages) {
826 r = kvm_iommu_map_pages(kvm, &new);
827 if (r)
828 goto out_free;
Alex Williamson32f6daa2012-04-11 09:51:49 -0600829 } else
830 kvm_iommu_unmap_pages(kvm, &old);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200831
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200832 r = -ENOMEM;
Thomas Meyer6da64fd2011-11-08 20:32:19 +0100833 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
834 GFP_KERNEL);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200835 if (!slots)
836 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200837
838 /* actual memory is freed via old in kvm_free_physmem_slot below */
839 if (!npages) {
840 new.rmap = NULL;
841 new.dirty_bitmap = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900842 memset(&new.arch, 0, sizeof(new.arch));
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200843 }
844
Xiao Guangrongbe593d62011-11-24 17:38:24 +0800845 update_memslots(slots, &new);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200846 old_memslots = kvm->memslots;
847 rcu_assign_pointer(kvm->memslots, slots);
848 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200850 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800851
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800852 /*
853 * If the new memory slot is created, we need to clear all
854 * mmio sptes.
855 */
856 if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
857 kvm_arch_flush_shadow(kvm);
858
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200859 kvm_free_physmem_slot(&old, &new);
860 kfree(old_memslots);
861
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 return 0;
863
Sheng Yangf78e0e22007-10-29 09:40:42 +0800864out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 kvm_free_physmem_slot(&new, &old);
866out:
867 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200868
869}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800870EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
871
872int kvm_set_memory_region(struct kvm *kvm,
873 struct kvm_userspace_memory_region *mem,
874 int user_alloc)
875{
876 int r;
877
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200878 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800879 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200880 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800881 return r;
882}
Izik Eidus210c7c42007-10-24 23:52:57 +0200883EXPORT_SYMBOL_GPL(kvm_set_memory_region);
884
Carsten Otte1fe779f2007-10-29 16:08:35 +0100885int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
886 struct
887 kvm_userspace_memory_region *mem,
888 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200889{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200890 if (mem->slot >= KVM_MEMORY_SLOTS)
891 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200892 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893}
894
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800895int kvm_get_dirty_log(struct kvm *kvm,
896 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897{
898 struct kvm_memory_slot *memslot;
899 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900900 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 unsigned long any = 0;
902
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 r = -EINVAL;
904 if (log->slot >= KVM_MEMORY_SLOTS)
905 goto out;
906
Xiao Guangrong28a37542011-11-24 19:04:35 +0800907 memslot = id_to_memslot(kvm->memslots, log->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 r = -ENOENT;
909 if (!memslot->dirty_bitmap)
910 goto out;
911
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900912 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Uri Lublincd1a4a92007-02-22 16:43:09 +0200914 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 any = memslot->dirty_bitmap[i];
916
917 r = -EFAULT;
918 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
919 goto out;
920
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800921 if (any)
922 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
924 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 return r;
927}
928
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900929bool kvm_largepages_enabled(void)
930{
931 return largepages_enabled;
932}
933
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300934void kvm_disable_largepages(void)
935{
936 largepages_enabled = false;
937}
938EXPORT_SYMBOL_GPL(kvm_disable_largepages);
939
Izik Eiduscea7bb22007-10-17 19:17:48 +0200940int is_error_page(struct page *page)
941{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300942 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200943}
944EXPORT_SYMBOL_GPL(is_error_page);
945
Anthony Liguori35149e22008-04-02 14:46:56 -0500946int is_error_pfn(pfn_t pfn)
947{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300948 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500949}
950EXPORT_SYMBOL_GPL(is_error_pfn);
951
Huang Yingbf998152010-05-31 14:28:19 +0800952int is_hwpoison_pfn(pfn_t pfn)
953{
954 return pfn == hwpoison_pfn;
955}
956EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
957
Xiao Guangrongfce92dc2011-07-12 03:28:54 +0800958int is_noslot_pfn(pfn_t pfn)
959{
960 return pfn == bad_pfn;
961}
962EXPORT_SYMBOL_GPL(is_noslot_pfn);
963
964int is_invalid_pfn(pfn_t pfn)
965{
966 return pfn == hwpoison_pfn || pfn == fault_pfn;
967}
968EXPORT_SYMBOL_GPL(is_invalid_pfn);
969
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200970static inline unsigned long bad_hva(void)
971{
972 return PAGE_OFFSET;
973}
974
975int kvm_is_error_hva(unsigned long addr)
976{
977 return addr == bad_hva();
978}
979EXPORT_SYMBOL_GPL(kvm_is_error_hva);
980
Gleb Natapov49c77542010-10-18 15:22:23 +0200981struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
982{
983 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
984}
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300985EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986
Izik Eiduse0d62c72007-10-24 23:57:46 +0200987int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
988{
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800989 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200990
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800991 if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
992 memslot->flags & KVM_MEMSLOT_INVALID)
993 return 0;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200994
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800995 return 1;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200996}
997EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
998
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100999unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
1000{
1001 struct vm_area_struct *vma;
1002 unsigned long addr, size;
1003
1004 size = PAGE_SIZE;
1005
1006 addr = gfn_to_hva(kvm, gfn);
1007 if (kvm_is_error_hva(addr))
1008 return PAGE_SIZE;
1009
1010 down_read(&current->mm->mmap_sem);
1011 vma = find_vma(current->mm, addr);
1012 if (!vma)
1013 goto out;
1014
1015 size = vma_kernel_pagesize(vma);
1016
1017out:
1018 up_read(&current->mm->mmap_sem);
1019
1020 return size;
1021}
1022
Gleb Natapov49c77542010-10-18 15:22:23 +02001023static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001024 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001025{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001026 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001027 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001028
1029 if (nr_pages)
1030 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1031
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001032 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001033}
Xiao Guangrong48987782010-08-22 19:11:43 +08001034
1035unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1036{
Gleb Natapov49c77542010-10-18 15:22:23 +02001037 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001038}
Sheng Yang0d150292008-04-25 21:44:50 +08001039EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001040
Xiao Guangrong903816f2012-07-17 21:54:11 +08001041pfn_t get_fault_pfn(void)
Gleb Natapov80300892010-10-19 18:13:41 +02001042{
1043 get_page(fault_page);
1044 return fault_pfn;
1045}
Xiao Guangrong903816f2012-07-17 21:54:11 +08001046EXPORT_SYMBOL_GPL(get_fault_pfn);
Gleb Natapov80300892010-10-19 18:13:41 +02001047
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001048int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1049 unsigned long start, int write, struct page **page)
1050{
1051 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1052
1053 if (write)
1054 flags |= FOLL_WRITE;
1055
1056 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1057}
1058
Huang Yingfafc3db2011-01-30 11:15:49 +08001059static inline int check_user_page_hwpoison(unsigned long addr)
1060{
1061 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1062
1063 rc = __get_user_pages(current, current->mm, addr, 1,
1064 flags, NULL, NULL, NULL);
1065 return rc == -EHWPOISON;
1066}
1067
Xiao Guangrongd5661042012-07-17 21:56:16 +08001068static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
1069 bool write_fault, bool *writable)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001070{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001071 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001072 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001073 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001074
Gleb Natapovaf585b92010-10-14 11:22:46 +02001075 /* we can do it either atomically or asynchronously, not both */
1076 BUG_ON(atomic && async);
1077
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001078 BUG_ON(!write_fault && !writable);
1079
1080 if (writable)
1081 *writable = true;
1082
Gleb Natapovaf585b92010-10-14 11:22:46 +02001083 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001084 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001085
1086 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001087 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001088
1089 if (writable)
1090 *writable = write_fault;
1091
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001092 if (async) {
1093 down_read(&current->mm->mmap_sem);
1094 npages = get_user_page_nowait(current, current->mm,
1095 addr, write_fault, page);
1096 up_read(&current->mm->mmap_sem);
1097 } else
1098 npages = get_user_pages_fast(addr, 1, write_fault,
1099 page);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001100
1101 /* map read fault as writable if possible */
1102 if (unlikely(!write_fault) && npages == 1) {
1103 struct page *wpage[1];
1104
1105 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1106 if (npages == 1) {
1107 *writable = true;
1108 put_page(page[0]);
1109 page[0] = wpage[0];
1110 }
1111 npages = 1;
1112 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001113 }
Izik Eidus539cb662007-11-11 22:05:04 +02001114
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001115 if (unlikely(npages != 1)) {
1116 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001117
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001118 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001119 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001120
Huang Yingbbeb3402010-06-22 14:23:11 +08001121 down_read(&current->mm->mmap_sem);
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001122 if (npages == -EHWPOISON ||
1123 (!async && check_user_page_hwpoison(addr))) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001124 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001125 get_page(hwpoison_page);
1126 return page_to_pfn(hwpoison_page);
1127 }
1128
Gleb Natapov80300892010-10-19 18:13:41 +02001129 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001130
Gleb Natapov80300892010-10-19 18:13:41 +02001131 if (vma == NULL)
1132 pfn = get_fault_pfn();
1133 else if ((vma->vm_flags & VM_PFNMAP)) {
1134 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1135 vma->vm_pgoff;
1136 BUG_ON(!kvm_is_mmio_pfn(pfn));
1137 } else {
1138 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001139 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001140 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001141 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001142 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001143 } else
1144 pfn = page_to_pfn(page[0]);
1145
1146 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001147}
1148
Xiao Guangrongd5661042012-07-17 21:56:16 +08001149pfn_t hva_to_pfn_atomic(unsigned long addr)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001150{
Xiao Guangrongd5661042012-07-17 21:56:16 +08001151 return hva_to_pfn(addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001152}
1153EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1154
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001155static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1156 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001157{
1158 unsigned long addr;
1159
Gleb Natapovaf585b92010-10-14 11:22:46 +02001160 if (async)
1161 *async = false;
1162
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001163 addr = gfn_to_hva(kvm, gfn);
1164 if (kvm_is_error_hva(addr)) {
1165 get_page(bad_page);
1166 return page_to_pfn(bad_page);
1167 }
1168
Xiao Guangrongd5661042012-07-17 21:56:16 +08001169 return hva_to_pfn(addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001170}
1171
1172pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1173{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001174 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001175}
1176EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1177
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001178pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1179 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001180{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001181 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001182}
1183EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1184
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001185pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1186{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001187 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001188}
Anthony Liguori35149e22008-04-02 14:46:56 -05001189EXPORT_SYMBOL_GPL(gfn_to_pfn);
1190
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001191pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1192 bool *writable)
1193{
1194 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1195}
1196EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1197
Xiao Guangrongd5661042012-07-17 21:56:16 +08001198pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001199{
1200 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Xiao Guangrongd5661042012-07-17 21:56:16 +08001201 return hva_to_pfn(addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001202}
1203
Xiao Guangrong48987782010-08-22 19:11:43 +08001204int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1205 int nr_pages)
1206{
1207 unsigned long addr;
1208 gfn_t entry;
1209
Gleb Natapov49c77542010-10-18 15:22:23 +02001210 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001211 if (kvm_is_error_hva(addr))
1212 return -1;
1213
1214 if (entry < nr_pages)
1215 return 0;
1216
1217 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1218}
1219EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1220
Anthony Liguori35149e22008-04-02 14:46:56 -05001221struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1222{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001223 pfn_t pfn;
1224
1225 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001226 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001227 return pfn_to_page(pfn);
1228
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001229 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001230
1231 get_page(bad_page);
1232 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001233}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001234
Avi Kivity954bbbc2007-03-30 14:02:32 +03001235EXPORT_SYMBOL_GPL(gfn_to_page);
1236
Izik Eidusb4231d62007-11-20 11:49:33 +02001237void kvm_release_page_clean(struct page *page)
1238{
Anthony Liguori35149e22008-04-02 14:46:56 -05001239 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001240}
1241EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1242
Anthony Liguori35149e22008-04-02 14:46:56 -05001243void kvm_release_pfn_clean(pfn_t pfn)
1244{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001245 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001246 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001247}
1248EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1249
Izik Eidusb4231d62007-11-20 11:49:33 +02001250void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001251{
Anthony Liguori35149e22008-04-02 14:46:56 -05001252 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001253}
Izik Eidusb4231d62007-11-20 11:49:33 +02001254EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001255
Anthony Liguori35149e22008-04-02 14:46:56 -05001256void kvm_release_pfn_dirty(pfn_t pfn)
1257{
1258 kvm_set_pfn_dirty(pfn);
1259 kvm_release_pfn_clean(pfn);
1260}
1261EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1262
1263void kvm_set_page_dirty(struct page *page)
1264{
1265 kvm_set_pfn_dirty(page_to_pfn(page));
1266}
1267EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1268
1269void kvm_set_pfn_dirty(pfn_t pfn)
1270{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001271 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001272 struct page *page = pfn_to_page(pfn);
1273 if (!PageReserved(page))
1274 SetPageDirty(page);
1275 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001276}
1277EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1278
1279void kvm_set_pfn_accessed(pfn_t pfn)
1280{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001281 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001282 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001283}
1284EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1285
1286void kvm_get_pfn(pfn_t pfn)
1287{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001288 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001289 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001290}
1291EXPORT_SYMBOL_GPL(kvm_get_pfn);
1292
Izik Eidus195aefd2007-10-01 22:14:18 +02001293static int next_segment(unsigned long len, int offset)
1294{
1295 if (len > PAGE_SIZE - offset)
1296 return PAGE_SIZE - offset;
1297 else
1298 return len;
1299}
1300
1301int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1302 int len)
1303{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001304 int r;
1305 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001306
Izik Eiduse0506bc2007-11-11 22:10:22 +02001307 addr = gfn_to_hva(kvm, gfn);
1308 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001309 return -EFAULT;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001310 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001311 if (r)
1312 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001313 return 0;
1314}
1315EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1316
1317int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1318{
1319 gfn_t gfn = gpa >> PAGE_SHIFT;
1320 int seg;
1321 int offset = offset_in_page(gpa);
1322 int ret;
1323
1324 while ((seg = next_segment(len, offset)) != 0) {
1325 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1326 if (ret < 0)
1327 return ret;
1328 offset = 0;
1329 len -= seg;
1330 data += seg;
1331 ++gfn;
1332 }
1333 return 0;
1334}
1335EXPORT_SYMBOL_GPL(kvm_read_guest);
1336
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001337int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1338 unsigned long len)
1339{
1340 int r;
1341 unsigned long addr;
1342 gfn_t gfn = gpa >> PAGE_SHIFT;
1343 int offset = offset_in_page(gpa);
1344
1345 addr = gfn_to_hva(kvm, gfn);
1346 if (kvm_is_error_hva(addr))
1347 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001348 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001349 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001350 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001351 if (r)
1352 return -EFAULT;
1353 return 0;
1354}
1355EXPORT_SYMBOL(kvm_read_guest_atomic);
1356
Izik Eidus195aefd2007-10-01 22:14:18 +02001357int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1358 int offset, int len)
1359{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001360 int r;
1361 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001362
Izik Eiduse0506bc2007-11-11 22:10:22 +02001363 addr = gfn_to_hva(kvm, gfn);
1364 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001365 return -EFAULT;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001366 r = __copy_to_user((void __user *)addr + offset, data, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001367 if (r)
1368 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001369 mark_page_dirty(kvm, gfn);
1370 return 0;
1371}
1372EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1373
1374int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1375 unsigned long len)
1376{
1377 gfn_t gfn = gpa >> PAGE_SHIFT;
1378 int seg;
1379 int offset = offset_in_page(gpa);
1380 int ret;
1381
1382 while ((seg = next_segment(len, offset)) != 0) {
1383 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1384 if (ret < 0)
1385 return ret;
1386 offset = 0;
1387 len -= seg;
1388 data += seg;
1389 ++gfn;
1390 }
1391 return 0;
1392}
1393
Gleb Natapov49c77542010-10-18 15:22:23 +02001394int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1395 gpa_t gpa)
1396{
1397 struct kvm_memslots *slots = kvm_memslots(kvm);
1398 int offset = offset_in_page(gpa);
1399 gfn_t gfn = gpa >> PAGE_SHIFT;
1400
1401 ghc->gpa = gpa;
1402 ghc->generation = slots->generation;
Paul Mackerras9d4cba72012-01-12 20:09:51 +00001403 ghc->memslot = gfn_to_memslot(kvm, gfn);
Gleb Natapov49c77542010-10-18 15:22:23 +02001404 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1405 if (!kvm_is_error_hva(ghc->hva))
1406 ghc->hva += offset;
1407 else
1408 return -EFAULT;
1409
1410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1413
1414int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1415 void *data, unsigned long len)
1416{
1417 struct kvm_memslots *slots = kvm_memslots(kvm);
1418 int r;
1419
1420 if (slots->generation != ghc->generation)
1421 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1422
1423 if (kvm_is_error_hva(ghc->hva))
1424 return -EFAULT;
1425
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001426 r = __copy_to_user((void __user *)ghc->hva, data, len);
Gleb Natapov49c77542010-10-18 15:22:23 +02001427 if (r)
1428 return -EFAULT;
1429 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1434
Gleb Natapove03b6442011-07-11 15:28:11 -04001435int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1436 void *data, unsigned long len)
1437{
1438 struct kvm_memslots *slots = kvm_memslots(kvm);
1439 int r;
1440
1441 if (slots->generation != ghc->generation)
1442 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1443
1444 if (kvm_is_error_hva(ghc->hva))
1445 return -EFAULT;
1446
1447 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1448 if (r)
1449 return -EFAULT;
1450
1451 return 0;
1452}
1453EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1454
Izik Eidus195aefd2007-10-01 22:14:18 +02001455int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1456{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001457 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1458 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001459}
1460EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1461
1462int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1463{
1464 gfn_t gfn = gpa >> PAGE_SHIFT;
1465 int seg;
1466 int offset = offset_in_page(gpa);
1467 int ret;
1468
1469 while ((seg = next_segment(len, offset)) != 0) {
1470 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1471 if (ret < 0)
1472 return ret;
1473 offset = 0;
1474 len -= seg;
1475 ++gfn;
1476 }
1477 return 0;
1478}
1479EXPORT_SYMBOL_GPL(kvm_clear_guest);
1480
Gleb Natapov49c77542010-10-18 15:22:23 +02001481void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1482 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001484 if (memslot && memslot->dirty_bitmap) {
1485 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
Takuya Yoshikawa93474b22012-03-01 19:34:45 +09001487 /* TODO: introduce set_bit_le() and use it */
1488 test_and_set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 }
1490}
1491
Gleb Natapov49c77542010-10-18 15:22:23 +02001492void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1493{
1494 struct kvm_memory_slot *memslot;
1495
1496 memslot = gfn_to_memslot(kvm, gfn);
1497 mark_page_dirty_in_slot(kvm, memslot, gfn);
1498}
1499
Eddie Dongb6958ce2007-07-18 12:15:21 +03001500/*
1501 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1502 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001503void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001504{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001505 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001506
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001507 for (;;) {
1508 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001509
Gleb Natapova1b37102009-07-09 15:33:52 +03001510 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001511 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001512 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001513 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001514 if (kvm_cpu_has_pending_timer(vcpu))
1515 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001516 if (signal_pending(current))
1517 break;
1518
Eddie Dongb6958ce2007-07-18 12:15:21 +03001519 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001520 }
1521
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001522 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001523}
1524
Marcelo Tosatti8c847802012-03-14 17:58:48 -03001525#ifndef CONFIG_S390
Christoffer Dallb6d33832012-03-08 16:44:24 -05001526/*
1527 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
1528 */
1529void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1530{
1531 int me;
1532 int cpu = vcpu->cpu;
1533 wait_queue_head_t *wqp;
1534
1535 wqp = kvm_arch_vcpu_wq(vcpu);
1536 if (waitqueue_active(wqp)) {
1537 wake_up_interruptible(wqp);
1538 ++vcpu->stat.halt_wakeup;
1539 }
1540
1541 me = get_cpu();
1542 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
1543 if (kvm_arch_vcpu_should_kick(vcpu))
1544 smp_send_reschedule(cpu);
1545 put_cpu();
1546}
Marcelo Tosatti8c847802012-03-14 17:58:48 -03001547#endif /* !CONFIG_S390 */
Christoffer Dallb6d33832012-03-08 16:44:24 -05001548
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549void kvm_resched(struct kvm_vcpu *vcpu)
1550{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001551 if (!need_resched())
1552 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554}
1555EXPORT_SYMBOL_GPL(kvm_resched);
1556
Konstantin Weitz41628d32012-04-25 15:30:38 +02001557bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
1558{
1559 struct pid *pid;
1560 struct task_struct *task = NULL;
1561
1562 rcu_read_lock();
1563 pid = rcu_dereference(target->pid);
1564 if (pid)
1565 task = get_pid_task(target->pid, PIDTYPE_PID);
1566 rcu_read_unlock();
1567 if (!task)
1568 return false;
1569 if (task->flags & PF_VCPU) {
1570 put_task_struct(task);
1571 return false;
1572 }
1573 if (yield_to(task, 1)) {
1574 put_task_struct(task);
1575 return true;
1576 }
1577 put_task_struct(task);
1578 return false;
1579}
1580EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
1581
Raghavendra K T06e48c52012-07-19 15:17:52 +05301582#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
1583/*
1584 * Helper that checks whether a VCPU is eligible for directed yield.
1585 * Most eligible candidate to yield is decided by following heuristics:
1586 *
1587 * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
1588 * (preempted lock holder), indicated by @in_spin_loop.
1589 * Set at the beiginning and cleared at the end of interception/PLE handler.
1590 *
1591 * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
1592 * chance last time (mostly it has become eligible now since we have probably
1593 * yielded to lockholder in last iteration. This is done by toggling
1594 * @dy_eligible each time a VCPU checked for eligibility.)
1595 *
1596 * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
1597 * to preempted lock-holder could result in wrong VCPU selection and CPU
1598 * burning. Giving priority for a potential lock-holder increases lock
1599 * progress.
1600 *
1601 * Since algorithm is based on heuristics, accessing another VCPU data without
1602 * locking does not harm. It may result in trying to yield to same VCPU, fail
1603 * and continue with next VCPU and so on.
1604 */
1605bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
1606{
1607 bool eligible;
1608
1609 eligible = !vcpu->spin_loop.in_spin_loop ||
1610 (vcpu->spin_loop.in_spin_loop &&
1611 vcpu->spin_loop.dy_eligible);
1612
1613 if (vcpu->spin_loop.in_spin_loop)
1614 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
1615
1616 return eligible;
1617}
1618#endif
Rik van Riel217ece62011-02-01 09:53:28 -05001619void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001620{
Rik van Riel217ece62011-02-01 09:53:28 -05001621 struct kvm *kvm = me->kvm;
1622 struct kvm_vcpu *vcpu;
1623 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1624 int yielded = 0;
1625 int pass;
1626 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001627
Raghavendra K T4c088492012-07-18 19:07:46 +05301628 kvm_vcpu_set_in_spin_loop(me, true);
Rik van Riel217ece62011-02-01 09:53:28 -05001629 /*
1630 * We boost the priority of a VCPU that is runnable but not
1631 * currently running, because it got preempted by something
1632 * else and called schedule in __vcpu_run. Hopefully that
1633 * VCPU is holding the lock that we need and will release it.
1634 * We approximate round-robin by starting at the last boosted VCPU.
1635 */
1636 for (pass = 0; pass < 2 && !yielded; pass++) {
1637 kvm_for_each_vcpu(i, vcpu, kvm) {
Rik van Riel5cfc2aa2012-06-19 16:51:04 -04001638 if (!pass && i <= last_boosted_vcpu) {
Rik van Riel217ece62011-02-01 09:53:28 -05001639 i = last_boosted_vcpu;
1640 continue;
1641 } else if (pass && i > last_boosted_vcpu)
1642 break;
1643 if (vcpu == me)
1644 continue;
1645 if (waitqueue_active(&vcpu->wq))
1646 continue;
Raghavendra K T06e48c52012-07-19 15:17:52 +05301647 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
1648 continue;
Konstantin Weitz41628d32012-04-25 15:30:38 +02001649 if (kvm_vcpu_yield_to(vcpu)) {
Rik van Riel217ece62011-02-01 09:53:28 -05001650 kvm->last_boosted_vcpu = i;
1651 yielded = 1;
1652 break;
1653 }
Rik van Riel217ece62011-02-01 09:53:28 -05001654 }
1655 }
Raghavendra K T4c088492012-07-18 19:07:46 +05301656 kvm_vcpu_set_in_spin_loop(me, false);
Raghavendra K T06e48c52012-07-19 15:17:52 +05301657
1658 /* Ensure vcpu is not eligible during next spinloop */
1659 kvm_vcpu_set_dy_eligible(me, false);
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001660}
1661EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1662
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001663static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001664{
1665 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001666 struct page *page;
1667
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001668 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001669 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001670#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001671 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001672 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001673#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001674#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1675 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1676 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1677#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001678 else
Carsten Otte5b1c1492012-01-04 10:25:23 +01001679 return kvm_arch_vcpu_fault(vcpu, vmf);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001680 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001681 vmf->page = page;
1682 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001683}
1684
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001685static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001686 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001687};
1688
1689static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1690{
1691 vma->vm_ops = &kvm_vcpu_vm_ops;
1692 return 0;
1693}
1694
Avi Kivitybccf2152007-02-21 18:04:26 +02001695static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1696{
1697 struct kvm_vcpu *vcpu = filp->private_data;
1698
Al Viro66c0b392008-04-19 20:33:56 +01001699 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001700 return 0;
1701}
1702
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001703static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001704 .release = kvm_vcpu_release,
1705 .unlocked_ioctl = kvm_vcpu_ioctl,
Alexander Graf1dda6062011-06-08 02:45:37 +02001706#ifdef CONFIG_COMPAT
1707 .compat_ioctl = kvm_vcpu_compat_ioctl,
1708#endif
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001709 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001710 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001711};
1712
1713/*
1714 * Allocates an inode for the vcpu.
1715 */
1716static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1717{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001718 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001719}
1720
Avi Kivityc5ea7662007-02-20 18:41:05 +02001721/*
1722 * Creates some virtual cpus. Good luck creating more than one.
1723 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001724static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001725{
1726 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001727 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001728
Gleb Natapov73880c82009-06-09 15:56:28 +03001729 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001730 if (IS_ERR(vcpu))
1731 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001732
Avi Kivity15ad7142007-07-11 18:17:21 +03001733 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1734
Avi Kivity26e52152007-11-20 15:30:24 +02001735 r = kvm_arch_vcpu_setup(vcpu);
1736 if (r)
Jan Kiszkad7805922011-05-23 10:33:05 +02001737 goto vcpu_destroy;
Avi Kivity26e52152007-11-20 15:30:24 +02001738
Shaohua Li11ec2802007-07-23 14:51:37 +08001739 mutex_lock(&kvm->lock);
Avi Kivity3e515702012-03-05 14:23:29 +02001740 if (!kvm_vcpu_compatible(vcpu)) {
1741 r = -EINVAL;
1742 goto unlock_vcpu_destroy;
1743 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001744 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1745 r = -EINVAL;
Jan Kiszkad7805922011-05-23 10:33:05 +02001746 goto unlock_vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001747 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001748
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001749 kvm_for_each_vcpu(r, v, kvm)
1750 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001751 r = -EEXIST;
Jan Kiszkad7805922011-05-23 10:33:05 +02001752 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03001753 }
1754
1755 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001756
1757 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001758 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001759 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001760 if (r < 0) {
1761 kvm_put_kvm(kvm);
Jan Kiszkad7805922011-05-23 10:33:05 +02001762 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03001763 }
1764
1765 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1766 smp_wmb();
1767 atomic_inc(&kvm->online_vcpus);
1768
Gleb Natapov73880c82009-06-09 15:56:28 +03001769 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001770 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001771
Jan Kiszkad7805922011-05-23 10:33:05 +02001772unlock_vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001773 mutex_unlock(&kvm->lock);
Jan Kiszkad7805922011-05-23 10:33:05 +02001774vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001775 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001776 return r;
1777}
1778
Avi Kivity1961d272007-03-05 19:46:05 +02001779static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1780{
1781 if (sigset) {
1782 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1783 vcpu->sigset_active = 1;
1784 vcpu->sigset = *sigset;
1785 } else
1786 vcpu->sigset_active = 0;
1787 return 0;
1788}
1789
Avi Kivitybccf2152007-02-21 18:04:26 +02001790static long kvm_vcpu_ioctl(struct file *filp,
1791 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792{
Avi Kivitybccf2152007-02-21 18:04:26 +02001793 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001794 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001795 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001796 struct kvm_fpu *fpu = NULL;
1797 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001799 if (vcpu->kvm->mm != current->mm)
1800 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001801
1802#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1803 /*
1804 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1805 * so vcpu_load() would break it.
1806 */
1807 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1808 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1809#endif
1810
1811
1812 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001814 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001815 r = -EINVAL;
1816 if (arg)
1817 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001818 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001819 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001822 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001824 r = -ENOMEM;
1825 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1826 if (!kvm_regs)
1827 goto out;
1828 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001830 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001832 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1833 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001835out_free1:
1836 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 break;
1838 }
1839 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001840 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001842 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001843 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
1844 if (IS_ERR(kvm_regs)) {
1845 r = PTR_ERR(kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001846 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001847 }
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001848 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001850 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001852out_free2:
1853 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854 break;
1855 }
1856 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001857 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1858 r = -ENOMEM;
1859 if (!kvm_sregs)
1860 goto out;
1861 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 if (r)
1863 goto out;
1864 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001865 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 goto out;
1867 r = 0;
1868 break;
1869 }
1870 case KVM_SET_SREGS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02001871 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
1872 if (IS_ERR(kvm_sregs)) {
1873 r = PTR_ERR(kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001875 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07001876 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 if (r)
1878 goto out;
1879 r = 0;
1880 break;
1881 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001882 case KVM_GET_MP_STATE: {
1883 struct kvm_mp_state mp_state;
1884
1885 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1886 if (r)
1887 goto out;
1888 r = -EFAULT;
1889 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1890 goto out;
1891 r = 0;
1892 break;
1893 }
1894 case KVM_SET_MP_STATE: {
1895 struct kvm_mp_state mp_state;
1896
1897 r = -EFAULT;
1898 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1899 goto out;
1900 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1901 if (r)
1902 goto out;
1903 r = 0;
1904 break;
1905 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 case KVM_TRANSLATE: {
1907 struct kvm_translation tr;
1908
1909 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001910 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001912 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 if (r)
1914 goto out;
1915 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001916 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917 goto out;
1918 r = 0;
1919 break;
1920 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001921 case KVM_SET_GUEST_DEBUG: {
1922 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923
1924 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001925 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001927 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 if (r)
1929 goto out;
1930 r = 0;
1931 break;
1932 }
Avi Kivity1961d272007-03-05 19:46:05 +02001933 case KVM_SET_SIGNAL_MASK: {
1934 struct kvm_signal_mask __user *sigmask_arg = argp;
1935 struct kvm_signal_mask kvm_sigmask;
1936 sigset_t sigset, *p;
1937
1938 p = NULL;
1939 if (argp) {
1940 r = -EFAULT;
1941 if (copy_from_user(&kvm_sigmask, argp,
1942 sizeof kvm_sigmask))
1943 goto out;
1944 r = -EINVAL;
1945 if (kvm_sigmask.len != sizeof sigset)
1946 goto out;
1947 r = -EFAULT;
1948 if (copy_from_user(&sigset, sigmask_arg->sigset,
1949 sizeof sigset))
1950 goto out;
1951 p = &sigset;
1952 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001953 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001954 break;
1955 }
Avi Kivityb8836732007-04-01 16:34:31 +03001956 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001957 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1958 r = -ENOMEM;
1959 if (!fpu)
1960 goto out;
1961 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001962 if (r)
1963 goto out;
1964 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001965 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001966 goto out;
1967 r = 0;
1968 break;
1969 }
1970 case KVM_SET_FPU: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02001971 fpu = memdup_user(argp, sizeof(*fpu));
1972 if (IS_ERR(fpu)) {
1973 r = PTR_ERR(fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001974 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001975 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07001976 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001977 if (r)
1978 goto out;
1979 r = 0;
1980 break;
1981 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001982 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001983 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001984 }
1985out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001986 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001987 kfree(fpu);
1988 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001989 return r;
1990}
1991
Alexander Graf1dda6062011-06-08 02:45:37 +02001992#ifdef CONFIG_COMPAT
1993static long kvm_vcpu_compat_ioctl(struct file *filp,
1994 unsigned int ioctl, unsigned long arg)
1995{
1996 struct kvm_vcpu *vcpu = filp->private_data;
1997 void __user *argp = compat_ptr(arg);
1998 int r;
1999
2000 if (vcpu->kvm->mm != current->mm)
2001 return -EIO;
2002
2003 switch (ioctl) {
2004 case KVM_SET_SIGNAL_MASK: {
2005 struct kvm_signal_mask __user *sigmask_arg = argp;
2006 struct kvm_signal_mask kvm_sigmask;
2007 compat_sigset_t csigset;
2008 sigset_t sigset;
2009
2010 if (argp) {
2011 r = -EFAULT;
2012 if (copy_from_user(&kvm_sigmask, argp,
2013 sizeof kvm_sigmask))
2014 goto out;
2015 r = -EINVAL;
2016 if (kvm_sigmask.len != sizeof csigset)
2017 goto out;
2018 r = -EFAULT;
2019 if (copy_from_user(&csigset, sigmask_arg->sigset,
2020 sizeof csigset))
2021 goto out;
2022 }
2023 sigset_from_compat(&sigset, &csigset);
2024 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2025 break;
2026 }
2027 default:
2028 r = kvm_vcpu_ioctl(filp, ioctl, arg);
2029 }
2030
2031out:
2032 return r;
2033}
2034#endif
2035
Avi Kivitybccf2152007-02-21 18:04:26 +02002036static long kvm_vm_ioctl(struct file *filp,
2037 unsigned int ioctl, unsigned long arg)
2038{
2039 struct kvm *kvm = filp->private_data;
2040 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002041 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002042
Avi Kivity6d4e4c42007-11-21 16:41:05 +02002043 if (kvm->mm != current->mm)
2044 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002045 switch (ioctl) {
2046 case KVM_CREATE_VCPU:
2047 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2048 if (r < 0)
2049 goto out;
2050 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002051 case KVM_SET_USER_MEMORY_REGION: {
2052 struct kvm_userspace_memory_region kvm_userspace_mem;
2053
2054 r = -EFAULT;
2055 if (copy_from_user(&kvm_userspace_mem, argp,
2056 sizeof kvm_userspace_mem))
2057 goto out;
2058
2059 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 if (r)
2061 goto out;
2062 break;
2063 }
2064 case KVM_GET_DIRTY_LOG: {
2065 struct kvm_dirty_log log;
2066
2067 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002068 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002069 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002070 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071 if (r)
2072 goto out;
2073 break;
2074 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002075#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2076 case KVM_REGISTER_COALESCED_MMIO: {
2077 struct kvm_coalesced_mmio_zone zone;
2078 r = -EFAULT;
2079 if (copy_from_user(&zone, argp, sizeof zone))
2080 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02002081 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2082 if (r)
2083 goto out;
2084 r = 0;
2085 break;
2086 }
2087 case KVM_UNREGISTER_COALESCED_MMIO: {
2088 struct kvm_coalesced_mmio_zone zone;
2089 r = -EFAULT;
2090 if (copy_from_user(&zone, argp, sizeof zone))
2091 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02002092 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2093 if (r)
2094 goto out;
2095 r = 0;
2096 break;
2097 }
2098#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04002099 case KVM_IRQFD: {
2100 struct kvm_irqfd data;
2101
2102 r = -EFAULT;
2103 if (copy_from_user(&data, argp, sizeof data))
2104 goto out;
2105 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2106 break;
2107 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002108 case KVM_IOEVENTFD: {
2109 struct kvm_ioeventfd data;
2110
2111 r = -EFAULT;
2112 if (copy_from_user(&data, argp, sizeof data))
2113 goto out;
2114 r = kvm_ioeventfd(kvm, &data);
2115 break;
2116 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002117#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2118 case KVM_SET_BOOT_CPU_ID:
2119 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002120 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002121 if (atomic_read(&kvm->online_vcpus) != 0)
2122 r = -EBUSY;
2123 else
2124 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002125 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002126 break;
2127#endif
Jan Kiszka07975ad2012-03-29 21:14:12 +02002128#ifdef CONFIG_HAVE_KVM_MSI
2129 case KVM_SIGNAL_MSI: {
2130 struct kvm_msi msi;
2131
2132 r = -EFAULT;
2133 if (copy_from_user(&msi, argp, sizeof msi))
2134 goto out;
2135 r = kvm_send_userspace_msi(kvm, &msi);
2136 break;
2137 }
2138#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002139 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002140 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03002141 if (r == -ENOTTY)
2142 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002143 }
2144out:
2145 return r;
2146}
2147
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002148#ifdef CONFIG_COMPAT
2149struct compat_kvm_dirty_log {
2150 __u32 slot;
2151 __u32 padding1;
2152 union {
2153 compat_uptr_t dirty_bitmap; /* one bit per page */
2154 __u64 padding2;
2155 };
2156};
2157
2158static long kvm_vm_compat_ioctl(struct file *filp,
2159 unsigned int ioctl, unsigned long arg)
2160{
2161 struct kvm *kvm = filp->private_data;
2162 int r;
2163
2164 if (kvm->mm != current->mm)
2165 return -EIO;
2166 switch (ioctl) {
2167 case KVM_GET_DIRTY_LOG: {
2168 struct compat_kvm_dirty_log compat_log;
2169 struct kvm_dirty_log log;
2170
2171 r = -EFAULT;
2172 if (copy_from_user(&compat_log, (void __user *)arg,
2173 sizeof(compat_log)))
2174 goto out;
2175 log.slot = compat_log.slot;
2176 log.padding1 = compat_log.padding1;
2177 log.padding2 = compat_log.padding2;
2178 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2179
2180 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2181 if (r)
2182 goto out;
2183 break;
2184 }
2185 default:
2186 r = kvm_vm_ioctl(filp, ioctl, arg);
2187 }
2188
2189out:
2190 return r;
2191}
2192#endif
2193
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002194static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002195{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002196 struct page *page[1];
2197 unsigned long addr;
2198 int npages;
2199 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002200 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002201
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002202 addr = gfn_to_hva(kvm, gfn);
2203 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002204 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002205
2206 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2207 NULL);
2208 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002209 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002210
2211 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002212 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002213}
2214
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002215static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002216 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002217};
2218
2219static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2220{
2221 vma->vm_ops = &kvm_vm_vm_ops;
2222 return 0;
2223}
2224
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002225static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002226 .release = kvm_vm_release,
2227 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002228#ifdef CONFIG_COMPAT
2229 .compat_ioctl = kvm_vm_compat_ioctl,
2230#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002231 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002232 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002233};
2234
Carsten Ottee08b9632012-01-04 10:25:20 +01002235static int kvm_dev_ioctl_create_vm(unsigned long type)
Avi Kivityf17abe92007-02-21 19:28:04 +02002236{
Heiko Carstensaac87632010-10-27 17:22:10 +02002237 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002238 struct kvm *kvm;
2239
Carsten Ottee08b9632012-01-04 10:25:20 +01002240 kvm = kvm_create_vm(type);
Avi Kivityd6d28162007-06-28 08:38:16 -04002241 if (IS_ERR(kvm))
2242 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002243#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2244 r = kvm_coalesced_mmio_init(kvm);
2245 if (r < 0) {
2246 kvm_put_kvm(kvm);
2247 return r;
2248 }
2249#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002250 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2251 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002252 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002253
Heiko Carstensaac87632010-10-27 17:22:10 +02002254 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002255}
2256
Avi Kivity1a811b62008-12-08 18:25:27 +02002257static long kvm_dev_ioctl_check_extension_generic(long arg)
2258{
2259 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002260 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002261 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002262 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002263#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2264 case KVM_CAP_SET_BOOT_CPU_ID:
2265#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002266 case KVM_CAP_INTERNAL_ERROR_DATA:
Jan Kiszka07975ad2012-03-29 21:14:12 +02002267#ifdef CONFIG_HAVE_KVM_MSI
2268 case KVM_CAP_SIGNAL_MSI:
2269#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002270 return 1;
Marc Zyngier9900b4b2012-06-15 15:07:02 -04002271#ifdef KVM_CAP_IRQ_ROUTING
Avi Kivity399ec802008-11-19 13:58:46 +02002272 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002273 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002274#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002275 default:
2276 break;
2277 }
2278 return kvm_dev_ioctl_check_extension(arg);
2279}
2280
Avi Kivityf17abe92007-02-21 19:28:04 +02002281static long kvm_dev_ioctl(struct file *filp,
2282 unsigned int ioctl, unsigned long arg)
2283{
Avi Kivity07c45a32007-03-07 13:05:38 +02002284 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002285
2286 switch (ioctl) {
2287 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002288 r = -EINVAL;
2289 if (arg)
2290 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002291 r = KVM_API_VERSION;
2292 break;
2293 case KVM_CREATE_VM:
Carsten Ottee08b9632012-01-04 10:25:20 +01002294 r = kvm_dev_ioctl_create_vm(arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002295 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002296 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002297 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002298 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002299 case KVM_GET_VCPU_MMAP_SIZE:
2300 r = -EINVAL;
2301 if (arg)
2302 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002303 r = PAGE_SIZE; /* struct kvm_run */
2304#ifdef CONFIG_X86
2305 r += PAGE_SIZE; /* pio data page */
2306#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002307#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2308 r += PAGE_SIZE; /* coalesced mmio ring page */
2309#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002310 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002311 case KVM_TRACE_ENABLE:
2312 case KVM_TRACE_PAUSE:
2313 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002314 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002315 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002317 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 }
2319out:
2320 return r;
2321}
2322
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 .unlocked_ioctl = kvm_dev_ioctl,
2325 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002326 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327};
2328
2329static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002330 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 "kvm",
2332 &kvm_chardev_ops,
2333};
2334
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002335static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002336{
2337 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002338 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002339
Rusty Russell7f59f492008-12-07 21:25:45 +10302340 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002341 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002342
Rusty Russell7f59f492008-12-07 21:25:45 +10302343 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002344
2345 r = kvm_arch_hardware_enable(NULL);
2346
2347 if (r) {
2348 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2349 atomic_inc(&hardware_enable_failed);
2350 printk(KERN_INFO "kvm: enabling virtualization on "
2351 "CPU%d failed\n", cpu);
2352 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002353}
2354
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002355static void hardware_enable(void *junk)
2356{
Jan Kiszkae935b832011-02-08 12:55:33 +01002357 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002358 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002359 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002360}
2361
2362static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002363{
2364 int cpu = raw_smp_processor_id();
2365
Rusty Russell7f59f492008-12-07 21:25:45 +10302366 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002367 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302368 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002369 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002370}
2371
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002372static void hardware_disable(void *junk)
2373{
Jan Kiszkae935b832011-02-08 12:55:33 +01002374 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002375 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002376 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002377}
2378
Alexander Graf10474ae2009-09-15 11:37:46 +02002379static void hardware_disable_all_nolock(void)
2380{
2381 BUG_ON(!kvm_usage_count);
2382
2383 kvm_usage_count--;
2384 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002385 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002386}
2387
2388static void hardware_disable_all(void)
2389{
Jan Kiszkae935b832011-02-08 12:55:33 +01002390 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002391 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002392 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002393}
2394
2395static int hardware_enable_all(void)
2396{
2397 int r = 0;
2398
Jan Kiszkae935b832011-02-08 12:55:33 +01002399 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002400
2401 kvm_usage_count++;
2402 if (kvm_usage_count == 1) {
2403 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002404 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002405
2406 if (atomic_read(&hardware_enable_failed)) {
2407 hardware_disable_all_nolock();
2408 r = -EBUSY;
2409 }
2410 }
2411
Jan Kiszkae935b832011-02-08 12:55:33 +01002412 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002413
2414 return r;
2415}
2416
Avi Kivity774c47f2007-02-12 00:54:47 -08002417static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2418 void *v)
2419{
2420 int cpu = (long)v;
2421
Alexander Graf10474ae2009-09-15 11:37:46 +02002422 if (!kvm_usage_count)
2423 return NOTIFY_OK;
2424
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002425 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002426 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002427 case CPU_DYING:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03002428 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2429 cpu);
2430 hardware_disable(NULL);
2431 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002432 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002433 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2434 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002435 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002436 break;
2437 }
2438 return NOTIFY_OK;
2439}
2440
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002441
Avi Kivityb7c41452010-12-02 17:52:50 +02002442asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002443{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002444 /* Fault while not rebooting. We want the trace. */
2445 BUG();
2446}
Avi Kivityb7c41452010-12-02 17:52:50 +02002447EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002448
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002449static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002450 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002451{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002452 /*
2453 * Some (well, at least mine) BIOSes hang on reboot if
2454 * in vmx root mode.
2455 *
2456 * And Intel TXT required VMX off for all cpu when system shutdown.
2457 */
2458 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2459 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002460 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002461 return NOTIFY_OK;
2462}
2463
2464static struct notifier_block kvm_reboot_notifier = {
2465 .notifier_call = kvm_reboot,
2466 .priority = 0,
2467};
2468
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002469static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002470{
2471 int i;
2472
2473 for (i = 0; i < bus->dev_count; i++) {
Sasha Levin743eeb02011-07-27 16:00:48 +03002474 struct kvm_io_device *pos = bus->range[i].dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002475
2476 kvm_iodevice_destructor(pos);
2477 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002478 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002479}
2480
Sasha Levin743eeb02011-07-27 16:00:48 +03002481int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2482{
2483 const struct kvm_io_range *r1 = p1;
2484 const struct kvm_io_range *r2 = p2;
2485
2486 if (r1->addr < r2->addr)
2487 return -1;
2488 if (r1->addr + r1->len > r2->addr + r2->len)
2489 return 1;
2490 return 0;
2491}
2492
2493int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
2494 gpa_t addr, int len)
2495{
Sasha Levin743eeb02011-07-27 16:00:48 +03002496 bus->range[bus->dev_count++] = (struct kvm_io_range) {
2497 .addr = addr,
2498 .len = len,
2499 .dev = dev,
2500 };
2501
2502 sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
2503 kvm_io_bus_sort_cmp, NULL);
2504
2505 return 0;
2506}
2507
2508int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
2509 gpa_t addr, int len)
2510{
2511 struct kvm_io_range *range, key;
2512 int off;
2513
2514 key = (struct kvm_io_range) {
2515 .addr = addr,
2516 .len = len,
2517 };
2518
2519 range = bsearch(&key, bus->range, bus->dev_count,
2520 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
2521 if (range == NULL)
2522 return -ENOENT;
2523
2524 off = range - bus->range;
2525
2526 while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
2527 off--;
2528
2529 return off;
2530}
2531
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002532/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002533int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002534 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002535{
Sasha Levin743eeb02011-07-27 16:00:48 +03002536 int idx;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002537 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03002538 struct kvm_io_range range;
2539
2540 range = (struct kvm_io_range) {
2541 .addr = addr,
2542 .len = len,
2543 };
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002544
2545 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Sasha Levin743eeb02011-07-27 16:00:48 +03002546 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2547 if (idx < 0)
2548 return -EOPNOTSUPP;
2549
2550 while (idx < bus->dev_count &&
2551 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2552 if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002553 return 0;
Sasha Levin743eeb02011-07-27 16:00:48 +03002554 idx++;
2555 }
2556
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002557 return -EOPNOTSUPP;
2558}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002559
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002560/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002561int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2562 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002563{
Sasha Levin743eeb02011-07-27 16:00:48 +03002564 int idx;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002565 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03002566 struct kvm_io_range range;
2567
2568 range = (struct kvm_io_range) {
2569 .addr = addr,
2570 .len = len,
2571 };
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002572
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002573 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Sasha Levin743eeb02011-07-27 16:00:48 +03002574 idx = kvm_io_bus_get_first_dev(bus, addr, len);
2575 if (idx < 0)
2576 return -EOPNOTSUPP;
2577
2578 while (idx < bus->dev_count &&
2579 kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
2580 if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002581 return 0;
Sasha Levin743eeb02011-07-27 16:00:48 +03002582 idx++;
2583 }
2584
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002585 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002586}
2587
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002588/* Caller must hold slots_lock. */
Sasha Levin743eeb02011-07-27 16:00:48 +03002589int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2590 int len, struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002591{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002592 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002593
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002594 bus = kvm->buses[bus_idx];
Amos Konga13007162012-03-09 12:17:32 +08002595 if (bus->dev_count > NR_IOBUS_DEVS - 1)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002596 return -ENOSPC;
2597
Amos Konga13007162012-03-09 12:17:32 +08002598 new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
2599 sizeof(struct kvm_io_range)), GFP_KERNEL);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002600 if (!new_bus)
2601 return -ENOMEM;
Amos Konga13007162012-03-09 12:17:32 +08002602 memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
2603 sizeof(struct kvm_io_range)));
Sasha Levin743eeb02011-07-27 16:00:48 +03002604 kvm_io_bus_insert_dev(new_bus, dev, addr, len);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002605 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2606 synchronize_srcu_expedited(&kvm->srcu);
2607 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002608
2609 return 0;
2610}
2611
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002612/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002613int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2614 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002615{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002616 int i, r;
2617 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002618
Sasha Levincdfca7b2011-12-04 19:36:28 +02002619 bus = kvm->buses[bus_idx];
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002620 r = -ENOENT;
Amos Konga13007162012-03-09 12:17:32 +08002621 for (i = 0; i < bus->dev_count; i++)
2622 if (bus->range[i].dev == dev) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002623 r = 0;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002624 break;
2625 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002626
Amos Konga13007162012-03-09 12:17:32 +08002627 if (r)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002628 return r;
Amos Konga13007162012-03-09 12:17:32 +08002629
2630 new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
2631 sizeof(struct kvm_io_range)), GFP_KERNEL);
2632 if (!new_bus)
2633 return -ENOMEM;
2634
2635 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
2636 new_bus->dev_count--;
2637 memcpy(new_bus->range + i, bus->range + i + 1,
2638 (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002639
2640 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2641 synchronize_srcu_expedited(&kvm->srcu);
2642 kfree(bus);
2643 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002644}
2645
Avi Kivity774c47f2007-02-12 00:54:47 -08002646static struct notifier_block kvm_cpu_notifier = {
2647 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002648};
2649
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002650static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002651{
2652 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002653 struct kvm *kvm;
2654
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002655 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002656 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002657 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002658 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002659 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002660 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002661}
2662
2663DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2664
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002665static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002666{
2667 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002668 struct kvm *kvm;
2669 struct kvm_vcpu *vcpu;
2670 int i;
2671
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002672 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002673 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002674 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002675 kvm_for_each_vcpu(i, vcpu, kvm)
2676 *val += *(u32 *)((void *)vcpu + offset);
2677
Jan Kiszkae935b832011-02-08 12:55:33 +01002678 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002679 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002680}
2681
Avi Kivityba1389b2007-11-18 16:24:12 +02002682DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2683
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002684static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002685 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2686 [KVM_STAT_VM] = &vm_stat_fops,
2687};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002688
Hamo4f69b682011-12-15 14:23:16 +08002689static int kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690{
Hamo4f69b682011-12-15 14:23:16 +08002691 int r = -EFAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 struct kvm_stats_debugfs_item *p;
2693
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002694 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Hamo4f69b682011-12-15 14:23:16 +08002695 if (kvm_debugfs_dir == NULL)
2696 goto out;
2697
2698 for (p = debugfs_entries; p->name; ++p) {
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002699 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002700 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002701 stat_fops[p->kind]);
Hamo4f69b682011-12-15 14:23:16 +08002702 if (p->dentry == NULL)
2703 goto out_dir;
2704 }
2705
2706 return 0;
2707
2708out_dir:
2709 debugfs_remove_recursive(kvm_debugfs_dir);
2710out:
2711 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
2714static void kvm_exit_debug(void)
2715{
2716 struct kvm_stats_debugfs_item *p;
2717
2718 for (p = debugfs_entries; p->name; ++p)
2719 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002720 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721}
2722
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002723static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002724{
Alexander Graf10474ae2009-09-15 11:37:46 +02002725 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002726 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002727 return 0;
2728}
2729
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002730static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002731{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002732 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002733 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002734 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002735 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002736}
2737
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002738static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08002739 .suspend = kvm_suspend,
2740 .resume = kvm_resume,
2741};
2742
Avi Kivity15ad7142007-07-11 18:17:21 +03002743static inline
2744struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2745{
2746 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2747}
2748
2749static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2750{
2751 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2752
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002753 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002754}
2755
2756static void kvm_sched_out(struct preempt_notifier *pn,
2757 struct task_struct *next)
2758{
2759 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2760
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002761 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002762}
2763
Avi Kivity0ee75be2010-04-28 15:39:01 +03002764int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002765 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766{
2767 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002768 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002770 r = kvm_arch_init(opaque);
2771 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002772 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002773
2774 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2775
2776 if (bad_page == NULL) {
2777 r = -ENOMEM;
2778 goto out;
2779 }
2780
Anthony Liguori35149e22008-04-02 14:46:56 -05002781 bad_pfn = page_to_pfn(bad_page);
2782
Huang Yingbf998152010-05-31 14:28:19 +08002783 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2784
2785 if (hwpoison_page == NULL) {
2786 r = -ENOMEM;
2787 goto out_free_0;
2788 }
2789
2790 hwpoison_pfn = page_to_pfn(hwpoison_page);
2791
Gleb Natapovedba23e2010-07-07 20:16:45 +03002792 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2793
2794 if (fault_page == NULL) {
2795 r = -ENOMEM;
2796 goto out_free_0;
2797 }
2798
2799 fault_pfn = page_to_pfn(fault_page);
2800
Avi Kivity8437a612009-06-06 14:52:35 -07002801 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302802 r = -ENOMEM;
2803 goto out_free_0;
2804 }
2805
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002806 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302808 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809
Yang, Sheng002c7f72007-07-31 14:23:01 +03002810 for_each_online_cpu(cpu) {
2811 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002812 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002813 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002814 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002815 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002816 }
2817
Avi Kivity774c47f2007-02-12 00:54:47 -08002818 r = register_cpu_notifier(&kvm_cpu_notifier);
2819 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002820 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 register_reboot_notifier(&kvm_reboot_notifier);
2822
Rusty Russellc16f8622007-07-30 21:12:19 +10002823 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002824 if (!vcpu_align)
2825 vcpu_align = __alignof__(struct kvm_vcpu);
2826 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002827 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002828 if (!kvm_vcpu_cache) {
2829 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002830 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10002831 }
2832
Gleb Natapovaf585b92010-10-14 11:22:46 +02002833 r = kvm_async_pf_init();
2834 if (r)
2835 goto out_free;
2836
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002838 kvm_vm_fops.owner = module;
2839 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840
2841 r = misc_register(&kvm_dev);
2842 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002843 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002844 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 }
2846
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002847 register_syscore_ops(&kvm_syscore_ops);
2848
Avi Kivity15ad7142007-07-11 18:17:21 +03002849 kvm_preempt_ops.sched_in = kvm_sched_in;
2850 kvm_preempt_ops.sched_out = kvm_sched_out;
2851
Hamo4f69b682011-12-15 14:23:16 +08002852 r = kvm_init_debug();
2853 if (r) {
2854 printk(KERN_ERR "kvm: create debugfs files failed\n");
2855 goto out_undebugfs;
2856 }
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002857
Avi Kivityc7addb92007-09-16 18:58:32 +02002858 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859
Hamo4f69b682011-12-15 14:23:16 +08002860out_undebugfs:
2861 unregister_syscore_ops(&kvm_syscore_ops);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002862out_unreg:
2863 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002865 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002866out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002868 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002869out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002870out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002871 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302872out_free_0a:
2873 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002874out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002875 if (fault_page)
2876 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002877 if (hwpoison_page)
2878 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002879 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002880out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002881 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002882out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 return r;
2884}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002885EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002887void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002889 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002891 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002892 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002893 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002895 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002896 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002897 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002898 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302899 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002900 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002901 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002903EXPORT_SYMBOL_GPL(kvm_exit);