blob: 6330653480e467d0f79776d0e0a2c90ebe56f7e1 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040052#include <asm/io.h>
53#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020054#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Laurent Vivier5f94c172008-05-30 16:05:54 +020056#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020057#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020058
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059#define CREATE_TRACE_POINTS
60#include <trace/events/kvm.h>
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Marcelo Tosattifa40a822009-06-04 15:08:24 -030065/*
66 * Ordering of locks:
67 *
Sheng Yangfae3a352009-12-15 10:28:07 +080068 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030069 */
70
Jan Kiszkae935b832011-02-08 12:55:33 +010071DEFINE_RAW_SPINLOCK(kvm_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080073
Rusty Russell7f59f492008-12-07 21:25:45 +103074static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020075static int kvm_usage_count = 0;
76static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030077
Rusty Russellc16f8622007-07-30 21:12:19 +100078struct kmem_cache *kvm_vcpu_cache;
79EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030080
Avi Kivity15ad7142007-07-11 18:17:21 +030081static __read_mostly struct preempt_ops kvm_preempt_ops;
82
Hollis Blanchard76f7c872008-04-15 16:05:42 -050083struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Avi Kivitybccf2152007-02-21 18:04:26 +020085static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
86 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020087static int hardware_enable_all(void);
88static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020089
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020090static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
91
Avi Kivityb7c41452010-12-02 17:52:50 +020092bool kvm_rebooting;
93EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +030094
Marcelo Tosatti54dee992009-06-11 12:07:44 -030095static bool largepages_enabled = true;
96
Gleb Natapovfa7bff82010-07-07 20:16:44 +030097static struct page *hwpoison_page;
98static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +080099
Gleb Natapovedba23e2010-07-07 20:16:45 +0300100static struct page *fault_page;
101static pfn_t fault_pfn;
102
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800103inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300104{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100105 if (pfn_valid(pfn)) {
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800106 int reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800107 struct page *tail = pfn_to_page(pfn);
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800108 struct page *head = compound_trans_head(tail);
109 reserved = PageReserved(head);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800110 if (head != tail) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800111 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800112 * "head" is not a dangling pointer
113 * (compound_trans_head takes care of that)
114 * but the hugepage may have been splitted
115 * from under us (and we may not hold a
116 * reference count on the head page so it can
117 * be reused before we run PageReferenced), so
118 * we've to check PageTail before returning
119 * what we just read.
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800120 */
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800121 smp_rmb();
122 if (PageTail(tail))
123 return reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800124 }
125 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100126 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300127
128 return true;
129}
130
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131/*
132 * Switches to specified vcpu, until a matching vcpu_put()
133 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200134void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135{
Avi Kivity15ad7142007-07-11 18:17:21 +0300136 int cpu;
137
Avi Kivitybccf2152007-02-21 18:04:26 +0200138 mutex_lock(&vcpu->mutex);
Rik van Riel34bb10b2011-02-01 09:52:41 -0500139 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
140 /* The thread running this VCPU changed. */
141 struct pid *oldpid = vcpu->pid;
142 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
143 rcu_assign_pointer(vcpu->pid, newpid);
144 synchronize_rcu();
145 put_pid(oldpid);
146 }
Avi Kivity15ad7142007-07-11 18:17:21 +0300147 cpu = get_cpu();
148 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200149 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300150 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200151}
152
Carsten Otte313a3dc2007-10-11 19:16:52 +0200153void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154{
Avi Kivity15ad7142007-07-11 18:17:21 +0300155 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200156 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300157 preempt_notifier_unregister(&vcpu->preempt_notifier);
158 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159 mutex_unlock(&vcpu->mutex);
160}
161
Avi Kivityd9e368d2007-06-07 19:18:30 +0300162static void ack_flush(void *_completed)
163{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300164}
165
Rusty Russell49846892008-12-08 20:26:24 +1030166static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300167{
Avi Kivity597a5f52008-07-20 14:24:22 +0300168 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030169 cpumask_var_t cpus;
170 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300171 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300172
Li Zefan79f55992009-06-15 14:58:26 +0800173 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030174
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800175 me = get_cpu();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300176 kvm_for_each_vcpu(i, vcpu, kvm) {
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800177 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300178 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800179
180 /* Set ->requests bit before we read ->mode */
181 smp_mb();
182
183 if (cpus != NULL && cpu != -1 && cpu != me &&
184 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030185 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300186 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030187 if (unlikely(cpus == NULL))
188 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
189 else if (!cpumask_empty(cpus))
190 smp_call_function_many(cpus, ack_flush, NULL, 1);
191 else
192 called = false;
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800193 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030194 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030195 return called;
196}
197
198void kvm_flush_remote_tlbs(struct kvm *kvm)
199{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800200 int dirty_count = kvm->tlbs_dirty;
201
202 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030203 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
204 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800205 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300206}
207
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500208void kvm_reload_remote_mmus(struct kvm *kvm)
209{
Rusty Russell49846892008-12-08 20:26:24 +1030210 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500211}
212
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000213int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
214{
215 struct page *page;
216 int r;
217
218 mutex_init(&vcpu->mutex);
219 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000220 vcpu->kvm = kvm;
221 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500222 vcpu->pid = NULL;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300223 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200224 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225
226 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
227 if (!page) {
228 r = -ENOMEM;
229 goto fail;
230 }
231 vcpu->run = page_address(page);
232
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800233 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000234 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800235 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000236 return 0;
237
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000238fail_free_run:
239 free_page((unsigned long)vcpu->run);
240fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000241 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000242}
243EXPORT_SYMBOL_GPL(kvm_vcpu_init);
244
245void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
246{
Rik van Riel34bb10b2011-02-01 09:52:41 -0500247 put_pid(vcpu->pid);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800248 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000249 free_page((unsigned long)vcpu->run);
250}
251EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
252
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200253#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
254static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
255{
256 return container_of(mn, struct kvm, mmu_notifier);
257}
258
259static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
260 struct mm_struct *mm,
261 unsigned long address)
262{
263 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200264 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200265
266 /*
267 * When ->invalidate_page runs, the linux pte has been zapped
268 * already but the page is still allocated until
269 * ->invalidate_page returns. So if we increase the sequence
270 * here the kvm page fault will notice if the spte can't be
271 * established because the page is going to be freed. If
272 * instead the kvm page fault establishes the spte before
273 * ->invalidate_page runs, kvm_unmap_hva will release it
274 * before returning.
275 *
276 * The sequence increase only need to be seen at spin_unlock
277 * time, and not at spin_lock time.
278 *
279 * Increasing the sequence after the spin_unlock would be
280 * unsafe because the kvm page fault could then establish the
281 * pte after kvm_unmap_hva returned, without noticing the page
282 * is going to be freed.
283 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200284 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200285 spin_lock(&kvm->mmu_lock);
286 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800287 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200288 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200289 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200290
291 /* we've to flush the tlb before the pages can be freed */
292 if (need_tlb_flush)
293 kvm_flush_remote_tlbs(kvm);
294
295}
296
Izik Eidus3da0dd42009-09-23 21:47:18 +0300297static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
298 struct mm_struct *mm,
299 unsigned long address,
300 pte_t pte)
301{
302 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200303 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300304
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200305 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300306 spin_lock(&kvm->mmu_lock);
307 kvm->mmu_notifier_seq++;
308 kvm_set_spte_hva(kvm, address, pte);
309 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200310 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300311}
312
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200313static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
314 struct mm_struct *mm,
315 unsigned long start,
316 unsigned long end)
317{
318 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200319 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200320
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200321 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200322 spin_lock(&kvm->mmu_lock);
323 /*
324 * The count increase must become visible at unlock time as no
325 * spte can be established without taking the mmu_lock and
326 * count is also read inside the mmu_lock critical section.
327 */
328 kvm->mmu_notifier_count++;
329 for (; start < end; start += PAGE_SIZE)
330 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800331 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200332 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200333 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200334
335 /* we've to flush the tlb before the pages can be freed */
336 if (need_tlb_flush)
337 kvm_flush_remote_tlbs(kvm);
338}
339
340static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
341 struct mm_struct *mm,
342 unsigned long start,
343 unsigned long end)
344{
345 struct kvm *kvm = mmu_notifier_to_kvm(mn);
346
347 spin_lock(&kvm->mmu_lock);
348 /*
349 * This sequence increase will notify the kvm page fault that
350 * the page that is going to be mapped in the spte could have
351 * been freed.
352 */
353 kvm->mmu_notifier_seq++;
354 /*
355 * The above sequence increase must be visible before the
356 * below count decrease but both values are read by the kvm
357 * page fault under mmu_lock spinlock so we don't need to add
358 * a smb_wmb() here in between the two.
359 */
360 kvm->mmu_notifier_count--;
361 spin_unlock(&kvm->mmu_lock);
362
363 BUG_ON(kvm->mmu_notifier_count < 0);
364}
365
366static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
367 struct mm_struct *mm,
368 unsigned long address)
369{
370 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200371 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200372
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200373 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200374 spin_lock(&kvm->mmu_lock);
375 young = kvm_age_hva(kvm, address);
376 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200377 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200378
379 if (young)
380 kvm_flush_remote_tlbs(kvm);
381
382 return young;
383}
384
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800385static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
386 struct mm_struct *mm,
387 unsigned long address)
388{
389 struct kvm *kvm = mmu_notifier_to_kvm(mn);
390 int young, idx;
391
392 idx = srcu_read_lock(&kvm->srcu);
393 spin_lock(&kvm->mmu_lock);
394 young = kvm_test_age_hva(kvm, address);
395 spin_unlock(&kvm->mmu_lock);
396 srcu_read_unlock(&kvm->srcu, idx);
397
398 return young;
399}
400
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100401static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
402 struct mm_struct *mm)
403{
404 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800405 int idx;
406
407 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100408 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800409 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100410}
411
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200412static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
413 .invalidate_page = kvm_mmu_notifier_invalidate_page,
414 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
415 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
416 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800417 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300418 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100419 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200420};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200421
422static int kvm_init_mmu_notifier(struct kvm *kvm)
423{
424 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
425 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
426}
427
428#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
429
430static int kvm_init_mmu_notifier(struct kvm *kvm)
431{
432 return 0;
433}
434
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200435#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
436
Avi Kivityf17abe92007-02-21 19:28:04 +0200437static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100439 int r, i;
440 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100442 if (!kvm)
443 return ERR_PTR(-ENOMEM);
444
445 r = kvm_arch_init_vm(kvm);
446 if (r)
447 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200448
449 r = hardware_enable_all();
450 if (r)
451 goto out_err_nodisable;
452
Avi Kivity75858a82009-01-04 17:10:50 +0200453#ifdef CONFIG_HAVE_KVM_IRQCHIP
454 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300455 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200456#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200458 r = -ENOMEM;
459 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
460 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100461 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200462 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100463 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200464 for (i = 0; i < KVM_NR_BUSES; i++) {
465 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
466 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100467 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200468 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200469 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200470
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200471 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100472 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200473 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200474
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200475 kvm->mm = current->mm;
476 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500477 spin_lock_init(&kvm->mmu_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400478 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800479 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300480 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200481 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300482 atomic_set(&kvm->users_count, 1);
Jan Kiszkae935b832011-02-08 12:55:33 +0100483 raw_spin_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000484 list_add(&kvm->vm_list, &vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100485 raw_spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100486
Avi Kivityf17abe92007-02-21 19:28:04 +0200487 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200488
489out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100490 cleanup_srcu_struct(&kvm->srcu);
491out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200492 hardware_disable_all();
493out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200494 for (i = 0; i < KVM_NR_BUSES; i++)
495 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200496 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100497 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200498 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200499}
500
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900501static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
502{
503 if (!memslot->dirty_bitmap)
504 return;
505
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900506 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
507 vfree(memslot->dirty_bitmap_head);
508 else
509 kfree(memslot->dirty_bitmap_head);
510
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900511 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900512 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900513}
514
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515/*
516 * Free any memory in @free but not in @dont.
517 */
518static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
519 struct kvm_memory_slot *dont)
520{
Joerg Roedelec04b262009-06-19 15:16:23 +0200521 int i;
522
Izik Eidus290fc382007-09-27 14:11:22 +0200523 if (!dont || free->rmap != dont->rmap)
524 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525
526 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900527 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528
Joerg Roedelec04b262009-06-19 15:16:23 +0200529
530 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
531 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
532 vfree(free->lpage_info[i]);
533 free->lpage_info[i] = NULL;
534 }
535 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300536
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500538 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539}
540
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800541void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542{
543 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200544 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200546 for (i = 0; i < slots->nmemslots; ++i)
547 kvm_free_physmem_slot(&slots->memslots[i], NULL);
548
549 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550}
551
Avi Kivityf17abe92007-02-21 19:28:04 +0200552static void kvm_destroy_vm(struct kvm *kvm)
553{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200554 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200555 struct mm_struct *mm = kvm->mm;
556
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800557 kvm_arch_sync_events(kvm);
Jan Kiszkae935b832011-02-08 12:55:33 +0100558 raw_spin_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800559 list_del(&kvm->vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100560 raw_spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200561 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200562 for (i = 0; i < KVM_NR_BUSES; i++)
563 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200564 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200565#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
566 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200567#else
568 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200569#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800570 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100571 kvm_free_physmem(kvm);
572 cleanup_srcu_struct(&kvm->srcu);
573 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200574 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200575 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200576}
577
Izik Eidusd39f13b2008-03-30 16:01:25 +0300578void kvm_get_kvm(struct kvm *kvm)
579{
580 atomic_inc(&kvm->users_count);
581}
582EXPORT_SYMBOL_GPL(kvm_get_kvm);
583
584void kvm_put_kvm(struct kvm *kvm)
585{
586 if (atomic_dec_and_test(&kvm->users_count))
587 kvm_destroy_vm(kvm);
588}
589EXPORT_SYMBOL_GPL(kvm_put_kvm);
590
591
Avi Kivityf17abe92007-02-21 19:28:04 +0200592static int kvm_vm_release(struct inode *inode, struct file *filp)
593{
594 struct kvm *kvm = filp->private_data;
595
Gregory Haskins721eecb2009-05-20 10:30:49 -0400596 kvm_irqfd_release(kvm);
597
Izik Eidusd39f13b2008-03-30 16:01:25 +0300598 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 return 0;
600}
601
Heiko Carstensd48ead82011-01-17 21:21:08 +0100602#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900603/*
604 * Allocation size is twice as large as the actual dirty bitmap size.
605 * This makes it possible to do double buffering: see x86's
606 * kvm_vm_ioctl_get_dirty_log().
607 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900608static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
609{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900610 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900611
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900612 if (dirty_bytes > PAGE_SIZE)
613 memslot->dirty_bitmap = vzalloc(dirty_bytes);
614 else
615 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
616
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900617 if (!memslot->dirty_bitmap)
618 return -ENOMEM;
619
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900620 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900621 return 0;
622}
Heiko Carstensd48ead82011-01-17 21:21:08 +0100623#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 * Allocate some memory and give it an address in the guest physical address
627 * space.
628 *
629 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800630 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500631 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800633int __kvm_set_memory_region(struct kvm *kvm,
634 struct kvm_userspace_memory_region *mem,
635 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636{
Avi Kivity8234b222010-12-27 12:08:45 +0200637 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200639 unsigned long npages;
640 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 struct kvm_memory_slot *memslot;
642 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200643 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
645 r = -EINVAL;
646 /* General sanity checks */
647 if (mem->memory_size & (PAGE_SIZE - 1))
648 goto out;
649 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
650 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800651 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600652 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200653 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 goto out;
655 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
656 goto out;
657
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200658 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
660 npages = mem->memory_size >> PAGE_SHIFT;
661
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900662 r = -EINVAL;
663 if (npages > KVM_MEM_MAX_NR_PAGES)
664 goto out;
665
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 if (!npages)
667 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
668
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 new = old = *memslot;
670
Avi Kivitye36d96f2010-06-21 10:56:36 +0300671 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 new.base_gfn = base_gfn;
673 new.npages = npages;
674 new.flags = mem->flags;
675
676 /* Disallow changing a memory slot's size. */
677 r = -EINVAL;
678 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800679 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680
681 /* Check for overlaps */
682 r = -EEXIST;
683 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200684 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200686 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 continue;
688 if (!((base_gfn + npages <= s->base_gfn) ||
689 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800690 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 /* Free page dirty bitmap if unneeded */
694 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000695 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696
697 r = -ENOMEM;
698
699 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200700#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500701 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900702 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200703
704 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800705 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200706
Izik Eidus80b14b52007-10-25 11:54:04 +0200707 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200708 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200710 if (!npages)
711 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300712
Joerg Roedelec04b262009-06-19 15:16:23 +0200713 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200714 unsigned long ugfn;
715 unsigned long j;
716 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200717 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300718
Joerg Roedelec04b262009-06-19 15:16:23 +0200719 /* Avoid unused variable warning if no large pages */
720 (void)level;
721
722 if (new.lpage_info[i])
723 continue;
724
Joerg Roedel82855412010-07-01 16:00:11 +0200725 lpages = 1 + ((base_gfn + npages - 1)
726 >> KVM_HPAGE_GFN_SHIFT(level));
727 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200728
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900729 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200730
731 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300732 goto out_free;
733
Joerg Roedel82855412010-07-01 16:00:11 +0200734 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200735 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200736 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200737 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300738 ugfn = new.userspace_addr >> PAGE_SHIFT;
739 /*
740 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300741 * other, or if explicitly asked to, disable large page
742 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300743 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200744 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300745 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200746 for (j = 0; j < lpages; ++j)
747 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749
Joerg Roedelec04b262009-06-19 15:16:23 +0200750skip_lpage:
751
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 /* Allocate page dirty bitmap if needed */
753 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900754 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800755 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200756 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200758#else /* not defined CONFIG_S390 */
759 new.user_alloc = user_alloc;
760 if (user_alloc)
761 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200762#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200764 if (!npages) {
765 r = -ENOMEM;
766 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
767 if (!slots)
768 goto out_free;
769 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
770 if (mem->slot >= slots->nmemslots)
771 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200772 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200773 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
774
775 old_memslots = kvm->memslots;
776 rcu_assign_pointer(kvm->memslots, slots);
777 synchronize_srcu_expedited(&kvm->srcu);
778 /* From this point no new shadow pages pointing to a deleted
779 * memslot will be created.
780 *
781 * validation of sp->gfn happens in:
782 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
783 * - kvm_is_visible_gfn (mmu_check_roots)
784 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300785 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200786 kfree(old_memslots);
787 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300788
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200789 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
790 if (r)
791 goto out_free;
792
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200793 /* map the pages in iommu page table */
794 if (npages) {
795 r = kvm_iommu_map_pages(kvm, &new);
796 if (r)
797 goto out_free;
798 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200799
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200800 r = -ENOMEM;
801 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
802 if (!slots)
803 goto out_free;
804 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
805 if (mem->slot >= slots->nmemslots)
806 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200807 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200808
809 /* actual memory is freed via old in kvm_free_physmem_slot below */
810 if (!npages) {
811 new.rmap = NULL;
812 new.dirty_bitmap = NULL;
813 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
814 new.lpage_info[i] = NULL;
815 }
816
817 slots->memslots[mem->slot] = new;
818 old_memslots = kvm->memslots;
819 rcu_assign_pointer(kvm->memslots, slots);
820 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200822 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800823
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200824 kvm_free_physmem_slot(&old, &new);
825 kfree(old_memslots);
826
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 return 0;
828
Sheng Yangf78e0e22007-10-29 09:40:42 +0800829out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 kvm_free_physmem_slot(&new, &old);
831out:
832 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200833
834}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800835EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
836
837int kvm_set_memory_region(struct kvm *kvm,
838 struct kvm_userspace_memory_region *mem,
839 int user_alloc)
840{
841 int r;
842
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200843 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800844 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200845 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800846 return r;
847}
Izik Eidus210c7c42007-10-24 23:52:57 +0200848EXPORT_SYMBOL_GPL(kvm_set_memory_region);
849
Carsten Otte1fe779f2007-10-29 16:08:35 +0100850int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
851 struct
852 kvm_userspace_memory_region *mem,
853 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200854{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200855 if (mem->slot >= KVM_MEMORY_SLOTS)
856 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200857 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858}
859
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800860int kvm_get_dirty_log(struct kvm *kvm,
861 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862{
863 struct kvm_memory_slot *memslot;
864 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900865 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 unsigned long any = 0;
867
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 r = -EINVAL;
869 if (log->slot >= KVM_MEMORY_SLOTS)
870 goto out;
871
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200872 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 r = -ENOENT;
874 if (!memslot->dirty_bitmap)
875 goto out;
876
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900877 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
Uri Lublincd1a4a92007-02-22 16:43:09 +0200879 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 any = memslot->dirty_bitmap[i];
881
882 r = -EFAULT;
883 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
884 goto out;
885
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800886 if (any)
887 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888
889 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return r;
892}
893
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300894void kvm_disable_largepages(void)
895{
896 largepages_enabled = false;
897}
898EXPORT_SYMBOL_GPL(kvm_disable_largepages);
899
Izik Eiduscea7bb22007-10-17 19:17:48 +0200900int is_error_page(struct page *page)
901{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300902 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200903}
904EXPORT_SYMBOL_GPL(is_error_page);
905
Anthony Liguori35149e22008-04-02 14:46:56 -0500906int is_error_pfn(pfn_t pfn)
907{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300908 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500909}
910EXPORT_SYMBOL_GPL(is_error_pfn);
911
Huang Yingbf998152010-05-31 14:28:19 +0800912int is_hwpoison_pfn(pfn_t pfn)
913{
914 return pfn == hwpoison_pfn;
915}
916EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
917
Gleb Natapovedba23e2010-07-07 20:16:45 +0300918int is_fault_pfn(pfn_t pfn)
919{
920 return pfn == fault_pfn;
921}
922EXPORT_SYMBOL_GPL(is_fault_pfn);
923
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200924static inline unsigned long bad_hva(void)
925{
926 return PAGE_OFFSET;
927}
928
929int kvm_is_error_hva(unsigned long addr)
930{
931 return addr == bad_hva();
932}
933EXPORT_SYMBOL_GPL(kvm_is_error_hva);
934
Gleb Natapov49c77542010-10-18 15:22:23 +0200935static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
936 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937{
938 int i;
939
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200940 for (i = 0; i < slots->nmemslots; ++i) {
941 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942
943 if (gfn >= memslot->base_gfn
944 && gfn < memslot->base_gfn + memslot->npages)
945 return memslot;
946 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000947 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948}
Gleb Natapov49c77542010-10-18 15:22:23 +0200949
950struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
951{
952 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
953}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300954EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Izik Eiduse0d62c72007-10-24 23:57:46 +0200956int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
957{
958 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800959 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200960
Izik Eiduse0d62c72007-10-24 23:57:46 +0200961 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200962 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200963
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200964 if (memslot->flags & KVM_MEMSLOT_INVALID)
965 continue;
966
Izik Eiduse0d62c72007-10-24 23:57:46 +0200967 if (gfn >= memslot->base_gfn
968 && gfn < memslot->base_gfn + memslot->npages)
969 return 1;
970 }
971 return 0;
972}
973EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
974
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100975unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
976{
977 struct vm_area_struct *vma;
978 unsigned long addr, size;
979
980 size = PAGE_SIZE;
981
982 addr = gfn_to_hva(kvm, gfn);
983 if (kvm_is_error_hva(addr))
984 return PAGE_SIZE;
985
986 down_read(&current->mm->mmap_sem);
987 vma = find_vma(current->mm, addr);
988 if (!vma)
989 goto out;
990
991 size = vma_kernel_pagesize(vma);
992
993out:
994 up_read(&current->mm->mmap_sem);
995
996 return size;
997}
998
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200999int memslot_id(struct kvm *kvm, gfn_t gfn)
1000{
1001 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001002 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001003 struct kvm_memory_slot *memslot = NULL;
1004
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001005 for (i = 0; i < slots->nmemslots; ++i) {
1006 memslot = &slots->memslots[i];
1007
1008 if (gfn >= memslot->base_gfn
1009 && gfn < memslot->base_gfn + memslot->npages)
1010 break;
1011 }
1012
1013 return memslot - slots->memslots;
1014}
1015
Gleb Natapov49c77542010-10-18 15:22:23 +02001016static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001017 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001018{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001019 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001020 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001021
1022 if (nr_pages)
1023 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1024
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001025 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001026}
Xiao Guangrong48987782010-08-22 19:11:43 +08001027
1028unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1029{
Gleb Natapov49c77542010-10-18 15:22:23 +02001030 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001031}
Sheng Yang0d150292008-04-25 21:44:50 +08001032EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001033
Gleb Natapov80300892010-10-19 18:13:41 +02001034static pfn_t get_fault_pfn(void)
1035{
1036 get_page(fault_page);
1037 return fault_pfn;
1038}
1039
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001040int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1041 unsigned long start, int write, struct page **page)
1042{
1043 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1044
1045 if (write)
1046 flags |= FOLL_WRITE;
1047
1048 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1049}
1050
Huang Yingfafc3db2011-01-30 11:15:49 +08001051static inline int check_user_page_hwpoison(unsigned long addr)
1052{
1053 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1054
1055 rc = __get_user_pages(current, current->mm, addr, 1,
1056 flags, NULL, NULL, NULL);
1057 return rc == -EHWPOISON;
1058}
1059
Gleb Natapovaf585b92010-10-14 11:22:46 +02001060static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001061 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001062{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001063 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001064 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001065 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001066
Gleb Natapovaf585b92010-10-14 11:22:46 +02001067 /* we can do it either atomically or asynchronously, not both */
1068 BUG_ON(atomic && async);
1069
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001070 BUG_ON(!write_fault && !writable);
1071
1072 if (writable)
1073 *writable = true;
1074
Gleb Natapovaf585b92010-10-14 11:22:46 +02001075 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001076 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001077
1078 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001079 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001080
1081 if (writable)
1082 *writable = write_fault;
1083
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001084 if (async) {
1085 down_read(&current->mm->mmap_sem);
1086 npages = get_user_page_nowait(current, current->mm,
1087 addr, write_fault, page);
1088 up_read(&current->mm->mmap_sem);
1089 } else
1090 npages = get_user_pages_fast(addr, 1, write_fault,
1091 page);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001092
1093 /* map read fault as writable if possible */
1094 if (unlikely(!write_fault) && npages == 1) {
1095 struct page *wpage[1];
1096
1097 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1098 if (npages == 1) {
1099 *writable = true;
1100 put_page(page[0]);
1101 page[0] = wpage[0];
1102 }
1103 npages = 1;
1104 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001105 }
Izik Eidus539cb662007-11-11 22:05:04 +02001106
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001107 if (unlikely(npages != 1)) {
1108 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001109
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001110 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001111 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001112
Huang Yingbbeb3402010-06-22 14:23:11 +08001113 down_read(&current->mm->mmap_sem);
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001114 if (npages == -EHWPOISON ||
1115 (!async && check_user_page_hwpoison(addr))) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001116 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001117 get_page(hwpoison_page);
1118 return page_to_pfn(hwpoison_page);
1119 }
1120
Gleb Natapov80300892010-10-19 18:13:41 +02001121 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001122
Gleb Natapov80300892010-10-19 18:13:41 +02001123 if (vma == NULL)
1124 pfn = get_fault_pfn();
1125 else if ((vma->vm_flags & VM_PFNMAP)) {
1126 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1127 vma->vm_pgoff;
1128 BUG_ON(!kvm_is_mmio_pfn(pfn));
1129 } else {
1130 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001131 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001132 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001133 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001134 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001135 } else
1136 pfn = page_to_pfn(page[0]);
1137
1138 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001139}
1140
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001141pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1142{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001143 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001144}
1145EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1146
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001147static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1148 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001149{
1150 unsigned long addr;
1151
Gleb Natapovaf585b92010-10-14 11:22:46 +02001152 if (async)
1153 *async = false;
1154
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001155 addr = gfn_to_hva(kvm, gfn);
1156 if (kvm_is_error_hva(addr)) {
1157 get_page(bad_page);
1158 return page_to_pfn(bad_page);
1159 }
1160
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001161 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001162}
1163
1164pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1165{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001166 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001167}
1168EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1169
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001170pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1171 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001172{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001173 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001174}
1175EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1176
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001177pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1178{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001179 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001180}
Anthony Liguori35149e22008-04-02 14:46:56 -05001181EXPORT_SYMBOL_GPL(gfn_to_pfn);
1182
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001183pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1184 bool *writable)
1185{
1186 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1187}
1188EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1189
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001190pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1191 struct kvm_memory_slot *slot, gfn_t gfn)
1192{
1193 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001194 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001195}
1196
Xiao Guangrong48987782010-08-22 19:11:43 +08001197int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1198 int nr_pages)
1199{
1200 unsigned long addr;
1201 gfn_t entry;
1202
Gleb Natapov49c77542010-10-18 15:22:23 +02001203 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001204 if (kvm_is_error_hva(addr))
1205 return -1;
1206
1207 if (entry < nr_pages)
1208 return 0;
1209
1210 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1211}
1212EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1213
Anthony Liguori35149e22008-04-02 14:46:56 -05001214struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1215{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001216 pfn_t pfn;
1217
1218 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001219 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001220 return pfn_to_page(pfn);
1221
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001222 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001223
1224 get_page(bad_page);
1225 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001226}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001227
Avi Kivity954bbbc22007-03-30 14:02:32 +03001228EXPORT_SYMBOL_GPL(gfn_to_page);
1229
Izik Eidusb4231d62007-11-20 11:49:33 +02001230void kvm_release_page_clean(struct page *page)
1231{
Anthony Liguori35149e22008-04-02 14:46:56 -05001232 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001233}
1234EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1235
Anthony Liguori35149e22008-04-02 14:46:56 -05001236void kvm_release_pfn_clean(pfn_t pfn)
1237{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001238 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001239 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001240}
1241EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1242
Izik Eidusb4231d62007-11-20 11:49:33 +02001243void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001244{
Anthony Liguori35149e22008-04-02 14:46:56 -05001245 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001246}
Izik Eidusb4231d62007-11-20 11:49:33 +02001247EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001248
Anthony Liguori35149e22008-04-02 14:46:56 -05001249void kvm_release_pfn_dirty(pfn_t pfn)
1250{
1251 kvm_set_pfn_dirty(pfn);
1252 kvm_release_pfn_clean(pfn);
1253}
1254EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1255
1256void kvm_set_page_dirty(struct page *page)
1257{
1258 kvm_set_pfn_dirty(page_to_pfn(page));
1259}
1260EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1261
1262void kvm_set_pfn_dirty(pfn_t pfn)
1263{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001264 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001265 struct page *page = pfn_to_page(pfn);
1266 if (!PageReserved(page))
1267 SetPageDirty(page);
1268 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001269}
1270EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1271
1272void kvm_set_pfn_accessed(pfn_t pfn)
1273{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001274 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001275 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001276}
1277EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1278
1279void kvm_get_pfn(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 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001283}
1284EXPORT_SYMBOL_GPL(kvm_get_pfn);
1285
Izik Eidus195aefd2007-10-01 22:14:18 +02001286static int next_segment(unsigned long len, int offset)
1287{
1288 if (len > PAGE_SIZE - offset)
1289 return PAGE_SIZE - offset;
1290 else
1291 return len;
1292}
1293
1294int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1295 int len)
1296{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001297 int r;
1298 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001299
Izik Eiduse0506bc2007-11-11 22:10:22 +02001300 addr = gfn_to_hva(kvm, gfn);
1301 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001302 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001303 r = copy_from_user(data, (void __user *)addr + offset, len);
1304 if (r)
1305 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001306 return 0;
1307}
1308EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1309
1310int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1311{
1312 gfn_t gfn = gpa >> PAGE_SHIFT;
1313 int seg;
1314 int offset = offset_in_page(gpa);
1315 int ret;
1316
1317 while ((seg = next_segment(len, offset)) != 0) {
1318 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1319 if (ret < 0)
1320 return ret;
1321 offset = 0;
1322 len -= seg;
1323 data += seg;
1324 ++gfn;
1325 }
1326 return 0;
1327}
1328EXPORT_SYMBOL_GPL(kvm_read_guest);
1329
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001330int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1331 unsigned long len)
1332{
1333 int r;
1334 unsigned long addr;
1335 gfn_t gfn = gpa >> PAGE_SHIFT;
1336 int offset = offset_in_page(gpa);
1337
1338 addr = gfn_to_hva(kvm, gfn);
1339 if (kvm_is_error_hva(addr))
1340 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001341 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001342 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001343 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001344 if (r)
1345 return -EFAULT;
1346 return 0;
1347}
1348EXPORT_SYMBOL(kvm_read_guest_atomic);
1349
Izik Eidus195aefd2007-10-01 22:14:18 +02001350int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1351 int offset, int len)
1352{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001353 int r;
1354 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001355
Izik Eiduse0506bc2007-11-11 22:10:22 +02001356 addr = gfn_to_hva(kvm, gfn);
1357 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001358 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001359 r = copy_to_user((void __user *)addr + offset, data, len);
1360 if (r)
1361 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001362 mark_page_dirty(kvm, gfn);
1363 return 0;
1364}
1365EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1366
1367int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1368 unsigned long len)
1369{
1370 gfn_t gfn = gpa >> PAGE_SHIFT;
1371 int seg;
1372 int offset = offset_in_page(gpa);
1373 int ret;
1374
1375 while ((seg = next_segment(len, offset)) != 0) {
1376 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1377 if (ret < 0)
1378 return ret;
1379 offset = 0;
1380 len -= seg;
1381 data += seg;
1382 ++gfn;
1383 }
1384 return 0;
1385}
1386
Gleb Natapov49c77542010-10-18 15:22:23 +02001387int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1388 gpa_t gpa)
1389{
1390 struct kvm_memslots *slots = kvm_memslots(kvm);
1391 int offset = offset_in_page(gpa);
1392 gfn_t gfn = gpa >> PAGE_SHIFT;
1393
1394 ghc->gpa = gpa;
1395 ghc->generation = slots->generation;
1396 ghc->memslot = __gfn_to_memslot(slots, gfn);
1397 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1398 if (!kvm_is_error_hva(ghc->hva))
1399 ghc->hva += offset;
1400 else
1401 return -EFAULT;
1402
1403 return 0;
1404}
1405EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1406
1407int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1408 void *data, unsigned long len)
1409{
1410 struct kvm_memslots *slots = kvm_memslots(kvm);
1411 int r;
1412
1413 if (slots->generation != ghc->generation)
1414 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1415
1416 if (kvm_is_error_hva(ghc->hva))
1417 return -EFAULT;
1418
1419 r = copy_to_user((void __user *)ghc->hva, data, len);
1420 if (r)
1421 return -EFAULT;
1422 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1423
1424 return 0;
1425}
1426EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1427
Izik Eidus195aefd2007-10-01 22:14:18 +02001428int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1429{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001430 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1431 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001432}
1433EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1434
1435int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1436{
1437 gfn_t gfn = gpa >> PAGE_SHIFT;
1438 int seg;
1439 int offset = offset_in_page(gpa);
1440 int ret;
1441
1442 while ((seg = next_segment(len, offset)) != 0) {
1443 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1444 if (ret < 0)
1445 return ret;
1446 offset = 0;
1447 len -= seg;
1448 ++gfn;
1449 }
1450 return 0;
1451}
1452EXPORT_SYMBOL_GPL(kvm_clear_guest);
1453
Gleb Natapov49c77542010-10-18 15:22:23 +02001454void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1455 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001457 if (memslot && memslot->dirty_bitmap) {
1458 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459
Akinobu Mitacd7e48c2011-03-23 16:42:04 -07001460 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461 }
1462}
1463
Gleb Natapov49c77542010-10-18 15:22:23 +02001464void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1465{
1466 struct kvm_memory_slot *memslot;
1467
1468 memslot = gfn_to_memslot(kvm, gfn);
1469 mark_page_dirty_in_slot(kvm, memslot, gfn);
1470}
1471
Eddie Dongb6958ce2007-07-18 12:15:21 +03001472/*
1473 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1474 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001475void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001476{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001477 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001478
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001479 for (;;) {
1480 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001481
Gleb Natapova1b37102009-07-09 15:33:52 +03001482 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001483 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001484 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001485 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001486 if (kvm_cpu_has_pending_timer(vcpu))
1487 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001488 if (signal_pending(current))
1489 break;
1490
Eddie Dongb6958ce2007-07-18 12:15:21 +03001491 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001492 }
1493
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001494 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001495}
1496
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497void kvm_resched(struct kvm_vcpu *vcpu)
1498{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001499 if (!need_resched())
1500 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502}
1503EXPORT_SYMBOL_GPL(kvm_resched);
1504
Rik van Riel217ece62011-02-01 09:53:28 -05001505void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001506{
Rik van Riel217ece62011-02-01 09:53:28 -05001507 struct kvm *kvm = me->kvm;
1508 struct kvm_vcpu *vcpu;
1509 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1510 int yielded = 0;
1511 int pass;
1512 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001513
Rik van Riel217ece62011-02-01 09:53:28 -05001514 /*
1515 * We boost the priority of a VCPU that is runnable but not
1516 * currently running, because it got preempted by something
1517 * else and called schedule in __vcpu_run. Hopefully that
1518 * VCPU is holding the lock that we need and will release it.
1519 * We approximate round-robin by starting at the last boosted VCPU.
1520 */
1521 for (pass = 0; pass < 2 && !yielded; pass++) {
1522 kvm_for_each_vcpu(i, vcpu, kvm) {
1523 struct task_struct *task = NULL;
1524 struct pid *pid;
1525 if (!pass && i < last_boosted_vcpu) {
1526 i = last_boosted_vcpu;
1527 continue;
1528 } else if (pass && i > last_boosted_vcpu)
1529 break;
1530 if (vcpu == me)
1531 continue;
1532 if (waitqueue_active(&vcpu->wq))
1533 continue;
1534 rcu_read_lock();
1535 pid = rcu_dereference(vcpu->pid);
1536 if (pid)
1537 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1538 rcu_read_unlock();
1539 if (!task)
1540 continue;
1541 if (task->flags & PF_VCPU) {
1542 put_task_struct(task);
1543 continue;
1544 }
1545 if (yield_to(task, 1)) {
1546 put_task_struct(task);
1547 kvm->last_boosted_vcpu = i;
1548 yielded = 1;
1549 break;
1550 }
1551 put_task_struct(task);
1552 }
1553 }
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001554}
1555EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1556
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001557static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001558{
1559 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001560 struct page *page;
1561
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001562 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001563 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001564#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001565 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001566 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001567#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001568#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1569 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1570 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1571#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001572 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001573 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001574 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001575 vmf->page = page;
1576 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001577}
1578
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001579static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001580 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001581};
1582
1583static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1584{
1585 vma->vm_ops = &kvm_vcpu_vm_ops;
1586 return 0;
1587}
1588
Avi Kivitybccf2152007-02-21 18:04:26 +02001589static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1590{
1591 struct kvm_vcpu *vcpu = filp->private_data;
1592
Al Viro66c0b392008-04-19 20:33:56 +01001593 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001594 return 0;
1595}
1596
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001597static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001598 .release = kvm_vcpu_release,
1599 .unlocked_ioctl = kvm_vcpu_ioctl,
1600 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001601 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001602 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001603};
1604
1605/*
1606 * Allocates an inode for the vcpu.
1607 */
1608static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1609{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001610 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001611}
1612
Avi Kivityc5ea7662007-02-20 18:41:05 +02001613/*
1614 * Creates some virtual cpus. Good luck creating more than one.
1615 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001616static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001617{
1618 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001619 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001620
Gleb Natapov73880c82009-06-09 15:56:28 +03001621 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001622 if (IS_ERR(vcpu))
1623 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001624
Avi Kivity15ad7142007-07-11 18:17:21 +03001625 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1626
Avi Kivity26e52152007-11-20 15:30:24 +02001627 r = kvm_arch_vcpu_setup(vcpu);
1628 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001629 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001630
Shaohua Li11ec2802007-07-23 14:51:37 +08001631 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001632 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1633 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001634 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001635 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001636
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001637 kvm_for_each_vcpu(r, v, kvm)
1638 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001639 r = -EEXIST;
1640 goto vcpu_destroy;
1641 }
1642
1643 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001644
1645 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001646 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001647 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001648 if (r < 0) {
1649 kvm_put_kvm(kvm);
1650 goto vcpu_destroy;
1651 }
1652
1653 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1654 smp_wmb();
1655 atomic_inc(&kvm->online_vcpus);
1656
1657#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1658 if (kvm->bsp_vcpu_id == id)
1659 kvm->bsp_vcpu = vcpu;
1660#endif
1661 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001662 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001663
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001664vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001665 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001666 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001667 return r;
1668}
1669
Avi Kivity1961d272007-03-05 19:46:05 +02001670static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1671{
1672 if (sigset) {
1673 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1674 vcpu->sigset_active = 1;
1675 vcpu->sigset = *sigset;
1676 } else
1677 vcpu->sigset_active = 0;
1678 return 0;
1679}
1680
Avi Kivitybccf2152007-02-21 18:04:26 +02001681static long kvm_vcpu_ioctl(struct file *filp,
1682 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683{
Avi Kivitybccf2152007-02-21 18:04:26 +02001684 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001685 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001686 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001687 struct kvm_fpu *fpu = NULL;
1688 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001690 if (vcpu->kvm->mm != current->mm)
1691 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001692
1693#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1694 /*
1695 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1696 * so vcpu_load() would break it.
1697 */
1698 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1699 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1700#endif
1701
1702
1703 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001705 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001706 r = -EINVAL;
1707 if (arg)
1708 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001709 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001710 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001713 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001715 r = -ENOMEM;
1716 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1717 if (!kvm_regs)
1718 goto out;
1719 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001721 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001723 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1724 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001726out_free1:
1727 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 break;
1729 }
1730 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001731 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001733 r = -ENOMEM;
1734 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1735 if (!kvm_regs)
1736 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001738 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1739 goto out_free2;
1740 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001742 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001744out_free2:
1745 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746 break;
1747 }
1748 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001749 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1750 r = -ENOMEM;
1751 if (!kvm_sregs)
1752 goto out;
1753 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754 if (r)
1755 goto out;
1756 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001757 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 goto out;
1759 r = 0;
1760 break;
1761 }
1762 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001763 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1764 r = -ENOMEM;
1765 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001767 r = -EFAULT;
1768 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1769 goto out;
1770 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771 if (r)
1772 goto out;
1773 r = 0;
1774 break;
1775 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001776 case KVM_GET_MP_STATE: {
1777 struct kvm_mp_state mp_state;
1778
1779 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1780 if (r)
1781 goto out;
1782 r = -EFAULT;
1783 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1784 goto out;
1785 r = 0;
1786 break;
1787 }
1788 case KVM_SET_MP_STATE: {
1789 struct kvm_mp_state mp_state;
1790
1791 r = -EFAULT;
1792 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1793 goto out;
1794 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1795 if (r)
1796 goto out;
1797 r = 0;
1798 break;
1799 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 case KVM_TRANSLATE: {
1801 struct kvm_translation tr;
1802
1803 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001804 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001806 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 if (r)
1808 goto out;
1809 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001810 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811 goto out;
1812 r = 0;
1813 break;
1814 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001815 case KVM_SET_GUEST_DEBUG: {
1816 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817
1818 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001819 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001821 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822 if (r)
1823 goto out;
1824 r = 0;
1825 break;
1826 }
Avi Kivity1961d272007-03-05 19:46:05 +02001827 case KVM_SET_SIGNAL_MASK: {
1828 struct kvm_signal_mask __user *sigmask_arg = argp;
1829 struct kvm_signal_mask kvm_sigmask;
1830 sigset_t sigset, *p;
1831
1832 p = NULL;
1833 if (argp) {
1834 r = -EFAULT;
1835 if (copy_from_user(&kvm_sigmask, argp,
1836 sizeof kvm_sigmask))
1837 goto out;
1838 r = -EINVAL;
1839 if (kvm_sigmask.len != sizeof sigset)
1840 goto out;
1841 r = -EFAULT;
1842 if (copy_from_user(&sigset, sigmask_arg->sigset,
1843 sizeof sigset))
1844 goto out;
1845 p = &sigset;
1846 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001847 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001848 break;
1849 }
Avi Kivityb8836732007-04-01 16:34:31 +03001850 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001851 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1852 r = -ENOMEM;
1853 if (!fpu)
1854 goto out;
1855 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001856 if (r)
1857 goto out;
1858 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001859 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001860 goto out;
1861 r = 0;
1862 break;
1863 }
1864 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001865 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1866 r = -ENOMEM;
1867 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001868 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001869 r = -EFAULT;
1870 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1871 goto out;
1872 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001873 if (r)
1874 goto out;
1875 r = 0;
1876 break;
1877 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001878 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001879 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001880 }
1881out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001882 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001883 kfree(fpu);
1884 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001885 return r;
1886}
1887
1888static long kvm_vm_ioctl(struct file *filp,
1889 unsigned int ioctl, unsigned long arg)
1890{
1891 struct kvm *kvm = filp->private_data;
1892 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001893 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001894
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001895 if (kvm->mm != current->mm)
1896 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001897 switch (ioctl) {
1898 case KVM_CREATE_VCPU:
1899 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1900 if (r < 0)
1901 goto out;
1902 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001903 case KVM_SET_USER_MEMORY_REGION: {
1904 struct kvm_userspace_memory_region kvm_userspace_mem;
1905
1906 r = -EFAULT;
1907 if (copy_from_user(&kvm_userspace_mem, argp,
1908 sizeof kvm_userspace_mem))
1909 goto out;
1910
1911 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 if (r)
1913 goto out;
1914 break;
1915 }
1916 case KVM_GET_DIRTY_LOG: {
1917 struct kvm_dirty_log log;
1918
1919 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001920 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001922 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923 if (r)
1924 goto out;
1925 break;
1926 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001927#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1928 case KVM_REGISTER_COALESCED_MMIO: {
1929 struct kvm_coalesced_mmio_zone zone;
1930 r = -EFAULT;
1931 if (copy_from_user(&zone, argp, sizeof zone))
1932 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001933 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1934 if (r)
1935 goto out;
1936 r = 0;
1937 break;
1938 }
1939 case KVM_UNREGISTER_COALESCED_MMIO: {
1940 struct kvm_coalesced_mmio_zone zone;
1941 r = -EFAULT;
1942 if (copy_from_user(&zone, argp, sizeof zone))
1943 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001944 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1945 if (r)
1946 goto out;
1947 r = 0;
1948 break;
1949 }
1950#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001951 case KVM_IRQFD: {
1952 struct kvm_irqfd data;
1953
1954 r = -EFAULT;
1955 if (copy_from_user(&data, argp, sizeof data))
1956 goto out;
1957 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1958 break;
1959 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001960 case KVM_IOEVENTFD: {
1961 struct kvm_ioeventfd data;
1962
1963 r = -EFAULT;
1964 if (copy_from_user(&data, argp, sizeof data))
1965 goto out;
1966 r = kvm_ioeventfd(kvm, &data);
1967 break;
1968 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001969#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1970 case KVM_SET_BOOT_CPU_ID:
1971 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001972 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001973 if (atomic_read(&kvm->online_vcpus) != 0)
1974 r = -EBUSY;
1975 else
1976 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001977 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001978 break;
1979#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001980 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001981 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001982 if (r == -ENOTTY)
1983 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001984 }
1985out:
1986 return r;
1987}
1988
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001989#ifdef CONFIG_COMPAT
1990struct compat_kvm_dirty_log {
1991 __u32 slot;
1992 __u32 padding1;
1993 union {
1994 compat_uptr_t dirty_bitmap; /* one bit per page */
1995 __u64 padding2;
1996 };
1997};
1998
1999static long kvm_vm_compat_ioctl(struct file *filp,
2000 unsigned int ioctl, unsigned long arg)
2001{
2002 struct kvm *kvm = filp->private_data;
2003 int r;
2004
2005 if (kvm->mm != current->mm)
2006 return -EIO;
2007 switch (ioctl) {
2008 case KVM_GET_DIRTY_LOG: {
2009 struct compat_kvm_dirty_log compat_log;
2010 struct kvm_dirty_log log;
2011
2012 r = -EFAULT;
2013 if (copy_from_user(&compat_log, (void __user *)arg,
2014 sizeof(compat_log)))
2015 goto out;
2016 log.slot = compat_log.slot;
2017 log.padding1 = compat_log.padding1;
2018 log.padding2 = compat_log.padding2;
2019 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2020
2021 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2022 if (r)
2023 goto out;
2024 break;
2025 }
2026 default:
2027 r = kvm_vm_ioctl(filp, ioctl, arg);
2028 }
2029
2030out:
2031 return r;
2032}
2033#endif
2034
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002035static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002036{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002037 struct page *page[1];
2038 unsigned long addr;
2039 int npages;
2040 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002041 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002042
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002043 addr = gfn_to_hva(kvm, gfn);
2044 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002045 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002046
2047 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2048 NULL);
2049 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002050 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002051
2052 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002053 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002054}
2055
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002056static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002057 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002058};
2059
2060static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2061{
2062 vma->vm_ops = &kvm_vm_vm_ops;
2063 return 0;
2064}
2065
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002066static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002067 .release = kvm_vm_release,
2068 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002069#ifdef CONFIG_COMPAT
2070 .compat_ioctl = kvm_vm_compat_ioctl,
2071#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002072 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002073 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002074};
2075
2076static int kvm_dev_ioctl_create_vm(void)
2077{
Heiko Carstensaac87632010-10-27 17:22:10 +02002078 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002079 struct kvm *kvm;
2080
Avi Kivityf17abe92007-02-21 19:28:04 +02002081 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002082 if (IS_ERR(kvm))
2083 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002084#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2085 r = kvm_coalesced_mmio_init(kvm);
2086 if (r < 0) {
2087 kvm_put_kvm(kvm);
2088 return r;
2089 }
2090#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002091 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2092 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002093 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002094
Heiko Carstensaac87632010-10-27 17:22:10 +02002095 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002096}
2097
Avi Kivity1a811b62008-12-08 18:25:27 +02002098static long kvm_dev_ioctl_check_extension_generic(long arg)
2099{
2100 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002101 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002102 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002103 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002104#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2105 case KVM_CAP_SET_BOOT_CPU_ID:
2106#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002107 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002108 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002109#ifdef CONFIG_HAVE_KVM_IRQCHIP
2110 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002111 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002112#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002113 default:
2114 break;
2115 }
2116 return kvm_dev_ioctl_check_extension(arg);
2117}
2118
Avi Kivityf17abe92007-02-21 19:28:04 +02002119static long kvm_dev_ioctl(struct file *filp,
2120 unsigned int ioctl, unsigned long arg)
2121{
Avi Kivity07c45a32007-03-07 13:05:38 +02002122 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002123
2124 switch (ioctl) {
2125 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002126 r = -EINVAL;
2127 if (arg)
2128 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002129 r = KVM_API_VERSION;
2130 break;
2131 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002132 r = -EINVAL;
2133 if (arg)
2134 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002135 r = kvm_dev_ioctl_create_vm();
2136 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002137 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002138 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002139 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002140 case KVM_GET_VCPU_MMAP_SIZE:
2141 r = -EINVAL;
2142 if (arg)
2143 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002144 r = PAGE_SIZE; /* struct kvm_run */
2145#ifdef CONFIG_X86
2146 r += PAGE_SIZE; /* pio data page */
2147#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002148#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2149 r += PAGE_SIZE; /* coalesced mmio ring page */
2150#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002151 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002152 case KVM_TRACE_ENABLE:
2153 case KVM_TRACE_PAUSE:
2154 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002155 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002156 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002158 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 }
2160out:
2161 return r;
2162}
2163
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 .unlocked_ioctl = kvm_dev_ioctl,
2166 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002167 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168};
2169
2170static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002171 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 "kvm",
2173 &kvm_chardev_ops,
2174};
2175
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002176static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002177{
2178 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002179 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002180
Rusty Russell7f59f492008-12-07 21:25:45 +10302181 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002182 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002183
Rusty Russell7f59f492008-12-07 21:25:45 +10302184 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002185
2186 r = kvm_arch_hardware_enable(NULL);
2187
2188 if (r) {
2189 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2190 atomic_inc(&hardware_enable_failed);
2191 printk(KERN_INFO "kvm: enabling virtualization on "
2192 "CPU%d failed\n", cpu);
2193 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002194}
2195
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002196static void hardware_enable(void *junk)
2197{
Jan Kiszkae935b832011-02-08 12:55:33 +01002198 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002199 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002200 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002201}
2202
2203static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002204{
2205 int cpu = raw_smp_processor_id();
2206
Rusty Russell7f59f492008-12-07 21:25:45 +10302207 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002208 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302209 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002210 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002211}
2212
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002213static void hardware_disable(void *junk)
2214{
Jan Kiszkae935b832011-02-08 12:55:33 +01002215 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002216 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002217 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002218}
2219
Alexander Graf10474ae2009-09-15 11:37:46 +02002220static void hardware_disable_all_nolock(void)
2221{
2222 BUG_ON(!kvm_usage_count);
2223
2224 kvm_usage_count--;
2225 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002226 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002227}
2228
2229static void hardware_disable_all(void)
2230{
Jan Kiszkae935b832011-02-08 12:55:33 +01002231 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002232 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002233 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002234}
2235
2236static int hardware_enable_all(void)
2237{
2238 int r = 0;
2239
Jan Kiszkae935b832011-02-08 12:55:33 +01002240 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002241
2242 kvm_usage_count++;
2243 if (kvm_usage_count == 1) {
2244 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002245 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002246
2247 if (atomic_read(&hardware_enable_failed)) {
2248 hardware_disable_all_nolock();
2249 r = -EBUSY;
2250 }
2251 }
2252
Jan Kiszkae935b832011-02-08 12:55:33 +01002253 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002254
2255 return r;
2256}
2257
Avi Kivity774c47f2007-02-12 00:54:47 -08002258static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2259 void *v)
2260{
2261 int cpu = (long)v;
2262
Alexander Graf10474ae2009-09-15 11:37:46 +02002263 if (!kvm_usage_count)
2264 return NOTIFY_OK;
2265
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002266 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002267 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002268 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002269 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2270 cpu);
2271 hardware_disable(NULL);
2272 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002273 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002274 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2275 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002276 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002277 break;
2278 }
2279 return NOTIFY_OK;
2280}
2281
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002282
Avi Kivityb7c41452010-12-02 17:52:50 +02002283asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002284{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002285 /* Fault while not rebooting. We want the trace. */
2286 BUG();
2287}
Avi Kivityb7c41452010-12-02 17:52:50 +02002288EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002289
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002290static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002291 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002292{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002293 /*
2294 * Some (well, at least mine) BIOSes hang on reboot if
2295 * in vmx root mode.
2296 *
2297 * And Intel TXT required VMX off for all cpu when system shutdown.
2298 */
2299 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2300 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002301 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002302 return NOTIFY_OK;
2303}
2304
2305static struct notifier_block kvm_reboot_notifier = {
2306 .notifier_call = kvm_reboot,
2307 .priority = 0,
2308};
2309
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002310static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002311{
2312 int i;
2313
2314 for (i = 0; i < bus->dev_count; i++) {
2315 struct kvm_io_device *pos = bus->devs[i];
2316
2317 kvm_iodevice_destructor(pos);
2318 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002319 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002320}
2321
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002322/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002323int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002324 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002325{
2326 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002327 struct kvm_io_bus *bus;
2328
2329 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002330 for (i = 0; i < bus->dev_count; i++)
2331 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2332 return 0;
2333 return -EOPNOTSUPP;
2334}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002335
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002336/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002337int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2338 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002339{
2340 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002341 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002342
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002343 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002344 for (i = 0; i < bus->dev_count; i++)
2345 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2346 return 0;
2347 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002348}
2349
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002350/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002351int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2352 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002353{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002354 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002355
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002356 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002357 if (bus->dev_count > NR_IOBUS_DEVS-1)
2358 return -ENOSPC;
2359
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002360 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2361 if (!new_bus)
2362 return -ENOMEM;
2363 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2364 new_bus->devs[new_bus->dev_count++] = dev;
2365 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2366 synchronize_srcu_expedited(&kvm->srcu);
2367 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002368
2369 return 0;
2370}
2371
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002372/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002373int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2374 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002375{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002376 int i, r;
2377 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002378
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002379 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2380 if (!new_bus)
2381 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002382
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002383 bus = kvm->buses[bus_idx];
2384 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2385
2386 r = -ENOENT;
2387 for (i = 0; i < new_bus->dev_count; i++)
2388 if (new_bus->devs[i] == dev) {
2389 r = 0;
2390 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002391 break;
2392 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002393
2394 if (r) {
2395 kfree(new_bus);
2396 return r;
2397 }
2398
2399 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2400 synchronize_srcu_expedited(&kvm->srcu);
2401 kfree(bus);
2402 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002403}
2404
Avi Kivity774c47f2007-02-12 00:54:47 -08002405static struct notifier_block kvm_cpu_notifier = {
2406 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002407};
2408
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002409static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002410{
2411 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002412 struct kvm *kvm;
2413
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002414 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002415 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002416 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002417 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002418 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002419 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002420}
2421
2422DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2423
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002424static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002425{
2426 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002427 struct kvm *kvm;
2428 struct kvm_vcpu *vcpu;
2429 int i;
2430
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002431 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002432 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002433 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002434 kvm_for_each_vcpu(i, vcpu, kvm)
2435 *val += *(u32 *)((void *)vcpu + offset);
2436
Jan Kiszkae935b832011-02-08 12:55:33 +01002437 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002438 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002439}
2440
Avi Kivityba1389b2007-11-18 16:24:12 +02002441DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2442
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002443static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002444 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2445 [KVM_STAT_VM] = &vm_stat_fops,
2446};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002447
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002448static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449{
2450 struct kvm_stats_debugfs_item *p;
2451
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002452 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002454 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002455 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002456 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457}
2458
2459static void kvm_exit_debug(void)
2460{
2461 struct kvm_stats_debugfs_item *p;
2462
2463 for (p = debugfs_entries; p->name; ++p)
2464 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002465 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466}
2467
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002468static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002469{
Alexander Graf10474ae2009-09-15 11:37:46 +02002470 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002471 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002472 return 0;
2473}
2474
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002475static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002476{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002477 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002478 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002479 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002480 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002481}
2482
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002483static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08002484 .suspend = kvm_suspend,
2485 .resume = kvm_resume,
2486};
2487
Izik Eiduscea7bb22007-10-17 19:17:48 +02002488struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002489pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490
Avi Kivity15ad7142007-07-11 18:17:21 +03002491static inline
2492struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2493{
2494 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2495}
2496
2497static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2498{
2499 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2500
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002501 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002502}
2503
2504static void kvm_sched_out(struct preempt_notifier *pn,
2505 struct task_struct *next)
2506{
2507 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2508
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002509 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002510}
2511
Avi Kivity0ee75be2010-04-28 15:39:01 +03002512int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002513 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514{
2515 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002516 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002518 r = kvm_arch_init(opaque);
2519 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002520 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002521
2522 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2523
2524 if (bad_page == NULL) {
2525 r = -ENOMEM;
2526 goto out;
2527 }
2528
Anthony Liguori35149e22008-04-02 14:46:56 -05002529 bad_pfn = page_to_pfn(bad_page);
2530
Huang Yingbf998152010-05-31 14:28:19 +08002531 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2532
2533 if (hwpoison_page == NULL) {
2534 r = -ENOMEM;
2535 goto out_free_0;
2536 }
2537
2538 hwpoison_pfn = page_to_pfn(hwpoison_page);
2539
Gleb Natapovedba23e2010-07-07 20:16:45 +03002540 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2541
2542 if (fault_page == NULL) {
2543 r = -ENOMEM;
2544 goto out_free_0;
2545 }
2546
2547 fault_pfn = page_to_pfn(fault_page);
2548
Avi Kivity8437a612009-06-06 14:52:35 -07002549 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302550 r = -ENOMEM;
2551 goto out_free_0;
2552 }
2553
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002554 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302556 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557
Yang, Sheng002c7f72007-07-31 14:23:01 +03002558 for_each_online_cpu(cpu) {
2559 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002560 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002561 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002562 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002563 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002564 }
2565
Avi Kivity774c47f2007-02-12 00:54:47 -08002566 r = register_cpu_notifier(&kvm_cpu_notifier);
2567 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002568 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 register_reboot_notifier(&kvm_reboot_notifier);
2570
Rusty Russellc16f8622007-07-30 21:12:19 +10002571 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002572 if (!vcpu_align)
2573 vcpu_align = __alignof__(struct kvm_vcpu);
2574 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002575 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002576 if (!kvm_vcpu_cache) {
2577 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002578 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10002579 }
2580
Gleb Natapovaf585b92010-10-14 11:22:46 +02002581 r = kvm_async_pf_init();
2582 if (r)
2583 goto out_free;
2584
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002586 kvm_vm_fops.owner = module;
2587 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588
2589 r = misc_register(&kvm_dev);
2590 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002591 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002592 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 }
2594
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002595 register_syscore_ops(&kvm_syscore_ops);
2596
Avi Kivity15ad7142007-07-11 18:17:21 +03002597 kvm_preempt_ops.sched_in = kvm_sched_in;
2598 kvm_preempt_ops.sched_out = kvm_sched_out;
2599
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002600 kvm_init_debug();
2601
Avi Kivityc7addb92007-09-16 18:58:32 +02002602 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603
Gleb Natapovaf585b92010-10-14 11:22:46 +02002604out_unreg:
2605 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002607 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002608out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002610 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002611out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002612out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002613 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302614out_free_0a:
2615 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002616out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002617 if (fault_page)
2618 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002619 if (hwpoison_page)
2620 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002621 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002622out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002623 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002624out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 return r;
2626}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002627EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002629void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002631 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002633 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002634 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002635 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002637 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002638 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002639 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002640 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302641 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002642 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002643 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002645EXPORT_SYMBOL_GPL(kvm_exit);