blob: 22cdb960660a61cd6c142278b08ed175b3d9110b [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 }
OGAWA Hirofumi85722cd2011-05-11 09:28:28 +0900470 spin_lock_init(&kvm->mmu_lock);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200471
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200472 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100473 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200474 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200475
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200476 kvm->mm = current->mm;
477 atomic_inc(&kvm->mm->mm_count);
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 Haskins721eecbf2009-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;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +0900651 /* We can read the guest memory with __xxx_user() later on. */
652 if (user_alloc &&
653 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
654 !access_ok(VERIFY_WRITE, mem->userspace_addr, mem->memory_size)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600655 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200656 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 goto out;
658 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
659 goto out;
660
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200661 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
663 npages = mem->memory_size >> PAGE_SHIFT;
664
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900665 r = -EINVAL;
666 if (npages > KVM_MEM_MAX_NR_PAGES)
667 goto out;
668
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 if (!npages)
670 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
671
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 new = old = *memslot;
673
Avi Kivitye36d96f2010-06-21 10:56:36 +0300674 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 new.base_gfn = base_gfn;
676 new.npages = npages;
677 new.flags = mem->flags;
678
679 /* Disallow changing a memory slot's size. */
680 r = -EINVAL;
681 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800682 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
684 /* Check for overlaps */
685 r = -EEXIST;
686 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200687 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200689 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 continue;
691 if (!((base_gfn + npages <= s->base_gfn) ||
692 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800693 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696 /* Free page dirty bitmap if unneeded */
697 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000698 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699
700 r = -ENOMEM;
701
702 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200703#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500704 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900705 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200706
707 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800708 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200709
Izik Eidus80b14b52007-10-25 11:54:04 +0200710 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200711 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200713 if (!npages)
714 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300715
Joerg Roedelec04b262009-06-19 15:16:23 +0200716 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200717 unsigned long ugfn;
718 unsigned long j;
719 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200720 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300721
Joerg Roedelec04b262009-06-19 15:16:23 +0200722 /* Avoid unused variable warning if no large pages */
723 (void)level;
724
725 if (new.lpage_info[i])
726 continue;
727
Joerg Roedel82855412010-07-01 16:00:11 +0200728 lpages = 1 + ((base_gfn + npages - 1)
729 >> KVM_HPAGE_GFN_SHIFT(level));
730 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200731
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900732 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200733
734 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300735 goto out_free;
736
Joerg Roedel82855412010-07-01 16:00:11 +0200737 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200738 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200739 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200740 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300741 ugfn = new.userspace_addr >> PAGE_SHIFT;
742 /*
743 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300744 * other, or if explicitly asked to, disable large page
745 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300746 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200747 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300748 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200749 for (j = 0; j < lpages; ++j)
750 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
Joerg Roedelec04b262009-06-19 15:16:23 +0200753skip_lpage:
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 /* Allocate page dirty bitmap if needed */
756 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900757 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800758 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200759 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200761#else /* not defined CONFIG_S390 */
762 new.user_alloc = user_alloc;
763 if (user_alloc)
764 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200765#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200767 if (!npages) {
768 r = -ENOMEM;
769 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
770 if (!slots)
771 goto out_free;
772 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
773 if (mem->slot >= slots->nmemslots)
774 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200775 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200776 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
777
778 old_memslots = kvm->memslots;
779 rcu_assign_pointer(kvm->memslots, slots);
780 synchronize_srcu_expedited(&kvm->srcu);
781 /* From this point no new shadow pages pointing to a deleted
782 * memslot will be created.
783 *
784 * validation of sp->gfn happens in:
785 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
786 * - kvm_is_visible_gfn (mmu_check_roots)
787 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300788 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200789 kfree(old_memslots);
790 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300791
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200792 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
793 if (r)
794 goto out_free;
795
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200796 /* map the pages in iommu page table */
797 if (npages) {
798 r = kvm_iommu_map_pages(kvm, &new);
799 if (r)
800 goto out_free;
801 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200802
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200803 r = -ENOMEM;
804 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
805 if (!slots)
806 goto out_free;
807 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
808 if (mem->slot >= slots->nmemslots)
809 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200810 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200811
812 /* actual memory is freed via old in kvm_free_physmem_slot below */
813 if (!npages) {
814 new.rmap = NULL;
815 new.dirty_bitmap = NULL;
816 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
817 new.lpage_info[i] = NULL;
818 }
819
820 slots->memslots[mem->slot] = new;
821 old_memslots = kvm->memslots;
822 rcu_assign_pointer(kvm->memslots, slots);
823 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200825 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800826
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200827 kvm_free_physmem_slot(&old, &new);
828 kfree(old_memslots);
829
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 return 0;
831
Sheng Yangf78e0e22007-10-29 09:40:42 +0800832out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 kvm_free_physmem_slot(&new, &old);
834out:
835 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200836
837}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800838EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
839
840int kvm_set_memory_region(struct kvm *kvm,
841 struct kvm_userspace_memory_region *mem,
842 int user_alloc)
843{
844 int r;
845
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200846 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800847 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200848 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800849 return r;
850}
Izik Eidus210c7c42007-10-24 23:52:57 +0200851EXPORT_SYMBOL_GPL(kvm_set_memory_region);
852
Carsten Otte1fe779f2007-10-29 16:08:35 +0100853int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
854 struct
855 kvm_userspace_memory_region *mem,
856 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200857{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200858 if (mem->slot >= KVM_MEMORY_SLOTS)
859 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200860 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861}
862
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800863int kvm_get_dirty_log(struct kvm *kvm,
864 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865{
866 struct kvm_memory_slot *memslot;
867 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900868 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 unsigned long any = 0;
870
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 r = -EINVAL;
872 if (log->slot >= KVM_MEMORY_SLOTS)
873 goto out;
874
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200875 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 r = -ENOENT;
877 if (!memslot->dirty_bitmap)
878 goto out;
879
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900880 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881
Uri Lublincd1a4a92007-02-22 16:43:09 +0200882 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 any = memslot->dirty_bitmap[i];
884
885 r = -EFAULT;
886 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
887 goto out;
888
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800889 if (any)
890 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891
892 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 return r;
895}
896
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300897void kvm_disable_largepages(void)
898{
899 largepages_enabled = false;
900}
901EXPORT_SYMBOL_GPL(kvm_disable_largepages);
902
Izik Eiduscea7bb22007-10-17 19:17:48 +0200903int is_error_page(struct page *page)
904{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300905 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200906}
907EXPORT_SYMBOL_GPL(is_error_page);
908
Anthony Liguori35149e22008-04-02 14:46:56 -0500909int is_error_pfn(pfn_t pfn)
910{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300911 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500912}
913EXPORT_SYMBOL_GPL(is_error_pfn);
914
Huang Yingbf998152010-05-31 14:28:19 +0800915int is_hwpoison_pfn(pfn_t pfn)
916{
917 return pfn == hwpoison_pfn;
918}
919EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
920
Gleb Natapovedba23e2010-07-07 20:16:45 +0300921int is_fault_pfn(pfn_t pfn)
922{
923 return pfn == fault_pfn;
924}
925EXPORT_SYMBOL_GPL(is_fault_pfn);
926
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200927static inline unsigned long bad_hva(void)
928{
929 return PAGE_OFFSET;
930}
931
932int kvm_is_error_hva(unsigned long addr)
933{
934 return addr == bad_hva();
935}
936EXPORT_SYMBOL_GPL(kvm_is_error_hva);
937
Gleb Natapov49c77542010-10-18 15:22:23 +0200938static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
939 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940{
941 int i;
942
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200943 for (i = 0; i < slots->nmemslots; ++i) {
944 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
946 if (gfn >= memslot->base_gfn
947 && gfn < memslot->base_gfn + memslot->npages)
948 return memslot;
949 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000950 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951}
Gleb Natapov49c77542010-10-18 15:22:23 +0200952
953struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
954{
955 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
956}
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300957EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958
Izik Eiduse0d62c72007-10-24 23:57:46 +0200959int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
960{
961 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800962 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200963
Izik Eiduse0d62c72007-10-24 23:57:46 +0200964 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200965 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200966
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200967 if (memslot->flags & KVM_MEMSLOT_INVALID)
968 continue;
969
Izik Eiduse0d62c72007-10-24 23:57:46 +0200970 if (gfn >= memslot->base_gfn
971 && gfn < memslot->base_gfn + memslot->npages)
972 return 1;
973 }
974 return 0;
975}
976EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
977
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100978unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
979{
980 struct vm_area_struct *vma;
981 unsigned long addr, size;
982
983 size = PAGE_SIZE;
984
985 addr = gfn_to_hva(kvm, gfn);
986 if (kvm_is_error_hva(addr))
987 return PAGE_SIZE;
988
989 down_read(&current->mm->mmap_sem);
990 vma = find_vma(current->mm, addr);
991 if (!vma)
992 goto out;
993
994 size = vma_kernel_pagesize(vma);
995
996out:
997 up_read(&current->mm->mmap_sem);
998
999 return size;
1000}
1001
Gleb Natapov49c77542010-10-18 15:22:23 +02001002static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001003 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001004{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001005 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001006 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001007
1008 if (nr_pages)
1009 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1010
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001011 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001012}
Xiao Guangrong48987782010-08-22 19:11:43 +08001013
1014unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1015{
Gleb Natapov49c77542010-10-18 15:22:23 +02001016 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001017}
Sheng Yang0d150292008-04-25 21:44:50 +08001018EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001019
Gleb Natapov80300892010-10-19 18:13:41 +02001020static pfn_t get_fault_pfn(void)
1021{
1022 get_page(fault_page);
1023 return fault_pfn;
1024}
1025
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001026int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1027 unsigned long start, int write, struct page **page)
1028{
1029 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1030
1031 if (write)
1032 flags |= FOLL_WRITE;
1033
1034 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1035}
1036
Huang Yingfafc3db2011-01-30 11:15:49 +08001037static inline int check_user_page_hwpoison(unsigned long addr)
1038{
1039 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1040
1041 rc = __get_user_pages(current, current->mm, addr, 1,
1042 flags, NULL, NULL, NULL);
1043 return rc == -EHWPOISON;
1044}
1045
Gleb Natapovaf585b92010-10-14 11:22:46 +02001046static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001047 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001048{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001049 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001050 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001051 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001052
Gleb Natapovaf585b92010-10-14 11:22:46 +02001053 /* we can do it either atomically or asynchronously, not both */
1054 BUG_ON(atomic && async);
1055
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001056 BUG_ON(!write_fault && !writable);
1057
1058 if (writable)
1059 *writable = true;
1060
Gleb Natapovaf585b92010-10-14 11:22:46 +02001061 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001062 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001063
1064 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001065 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001066
1067 if (writable)
1068 *writable = write_fault;
1069
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001070 if (async) {
1071 down_read(&current->mm->mmap_sem);
1072 npages = get_user_page_nowait(current, current->mm,
1073 addr, write_fault, page);
1074 up_read(&current->mm->mmap_sem);
1075 } else
1076 npages = get_user_pages_fast(addr, 1, write_fault,
1077 page);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001078
1079 /* map read fault as writable if possible */
1080 if (unlikely(!write_fault) && npages == 1) {
1081 struct page *wpage[1];
1082
1083 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1084 if (npages == 1) {
1085 *writable = true;
1086 put_page(page[0]);
1087 page[0] = wpage[0];
1088 }
1089 npages = 1;
1090 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001091 }
Izik Eidus539cb662007-11-11 22:05:04 +02001092
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001093 if (unlikely(npages != 1)) {
1094 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001095
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001096 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001097 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001098
Huang Yingbbeb3402010-06-22 14:23:11 +08001099 down_read(&current->mm->mmap_sem);
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001100 if (npages == -EHWPOISON ||
1101 (!async && check_user_page_hwpoison(addr))) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001102 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001103 get_page(hwpoison_page);
1104 return page_to_pfn(hwpoison_page);
1105 }
1106
Gleb Natapov80300892010-10-19 18:13:41 +02001107 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001108
Gleb Natapov80300892010-10-19 18:13:41 +02001109 if (vma == NULL)
1110 pfn = get_fault_pfn();
1111 else if ((vma->vm_flags & VM_PFNMAP)) {
1112 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1113 vma->vm_pgoff;
1114 BUG_ON(!kvm_is_mmio_pfn(pfn));
1115 } else {
1116 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001117 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001118 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001119 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001120 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001121 } else
1122 pfn = page_to_pfn(page[0]);
1123
1124 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001125}
1126
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001127pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1128{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001129 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001130}
1131EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1132
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001133static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1134 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001135{
1136 unsigned long addr;
1137
Gleb Natapovaf585b92010-10-14 11:22:46 +02001138 if (async)
1139 *async = false;
1140
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001141 addr = gfn_to_hva(kvm, gfn);
1142 if (kvm_is_error_hva(addr)) {
1143 get_page(bad_page);
1144 return page_to_pfn(bad_page);
1145 }
1146
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001147 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001148}
1149
1150pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1151{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001152 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001153}
1154EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1155
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001156pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1157 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001158{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001159 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001160}
1161EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1162
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001163pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1164{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001165 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001166}
Anthony Liguori35149e22008-04-02 14:46:56 -05001167EXPORT_SYMBOL_GPL(gfn_to_pfn);
1168
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001169pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1170 bool *writable)
1171{
1172 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1173}
1174EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1175
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001176pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1177 struct kvm_memory_slot *slot, gfn_t gfn)
1178{
1179 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001180 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001181}
1182
Xiao Guangrong48987782010-08-22 19:11:43 +08001183int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1184 int nr_pages)
1185{
1186 unsigned long addr;
1187 gfn_t entry;
1188
Gleb Natapov49c77542010-10-18 15:22:23 +02001189 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001190 if (kvm_is_error_hva(addr))
1191 return -1;
1192
1193 if (entry < nr_pages)
1194 return 0;
1195
1196 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1197}
1198EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1199
Anthony Liguori35149e22008-04-02 14:46:56 -05001200struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1201{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001202 pfn_t pfn;
1203
1204 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001205 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001206 return pfn_to_page(pfn);
1207
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001208 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001209
1210 get_page(bad_page);
1211 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001212}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001213
Avi Kivity954bbbc2007-03-30 14:02:32 +03001214EXPORT_SYMBOL_GPL(gfn_to_page);
1215
Izik Eidusb4231d62007-11-20 11:49:33 +02001216void kvm_release_page_clean(struct page *page)
1217{
Anthony Liguori35149e22008-04-02 14:46:56 -05001218 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001219}
1220EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1221
Anthony Liguori35149e22008-04-02 14:46:56 -05001222void kvm_release_pfn_clean(pfn_t pfn)
1223{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001224 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001225 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001226}
1227EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1228
Izik Eidusb4231d62007-11-20 11:49:33 +02001229void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001230{
Anthony Liguori35149e22008-04-02 14:46:56 -05001231 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001232}
Izik Eidusb4231d62007-11-20 11:49:33 +02001233EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001234
Anthony Liguori35149e22008-04-02 14:46:56 -05001235void kvm_release_pfn_dirty(pfn_t pfn)
1236{
1237 kvm_set_pfn_dirty(pfn);
1238 kvm_release_pfn_clean(pfn);
1239}
1240EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1241
1242void kvm_set_page_dirty(struct page *page)
1243{
1244 kvm_set_pfn_dirty(page_to_pfn(page));
1245}
1246EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1247
1248void kvm_set_pfn_dirty(pfn_t pfn)
1249{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001250 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001251 struct page *page = pfn_to_page(pfn);
1252 if (!PageReserved(page))
1253 SetPageDirty(page);
1254 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001255}
1256EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1257
1258void kvm_set_pfn_accessed(pfn_t pfn)
1259{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001260 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001261 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001262}
1263EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1264
1265void kvm_get_pfn(pfn_t pfn)
1266{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001267 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001268 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001269}
1270EXPORT_SYMBOL_GPL(kvm_get_pfn);
1271
Izik Eidus195aefd2007-10-01 22:14:18 +02001272static int next_segment(unsigned long len, int offset)
1273{
1274 if (len > PAGE_SIZE - offset)
1275 return PAGE_SIZE - offset;
1276 else
1277 return len;
1278}
1279
1280int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1281 int len)
1282{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001283 int r;
1284 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001285
Izik Eiduse0506bc2007-11-11 22:10:22 +02001286 addr = gfn_to_hva(kvm, gfn);
1287 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001288 return -EFAULT;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001289 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001290 if (r)
1291 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001292 return 0;
1293}
1294EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1295
1296int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1297{
1298 gfn_t gfn = gpa >> PAGE_SHIFT;
1299 int seg;
1300 int offset = offset_in_page(gpa);
1301 int ret;
1302
1303 while ((seg = next_segment(len, offset)) != 0) {
1304 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1305 if (ret < 0)
1306 return ret;
1307 offset = 0;
1308 len -= seg;
1309 data += seg;
1310 ++gfn;
1311 }
1312 return 0;
1313}
1314EXPORT_SYMBOL_GPL(kvm_read_guest);
1315
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001316int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1317 unsigned long len)
1318{
1319 int r;
1320 unsigned long addr;
1321 gfn_t gfn = gpa >> PAGE_SHIFT;
1322 int offset = offset_in_page(gpa);
1323
1324 addr = gfn_to_hva(kvm, gfn);
1325 if (kvm_is_error_hva(addr))
1326 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001327 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001328 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001329 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001330 if (r)
1331 return -EFAULT;
1332 return 0;
1333}
1334EXPORT_SYMBOL(kvm_read_guest_atomic);
1335
Izik Eidus195aefd2007-10-01 22:14:18 +02001336int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1337 int offset, int len)
1338{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001339 int r;
1340 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001341
Izik Eiduse0506bc2007-11-11 22:10:22 +02001342 addr = gfn_to_hva(kvm, gfn);
1343 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001344 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001345 r = copy_to_user((void __user *)addr + offset, data, len);
1346 if (r)
1347 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001348 mark_page_dirty(kvm, gfn);
1349 return 0;
1350}
1351EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1352
1353int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1354 unsigned long len)
1355{
1356 gfn_t gfn = gpa >> PAGE_SHIFT;
1357 int seg;
1358 int offset = offset_in_page(gpa);
1359 int ret;
1360
1361 while ((seg = next_segment(len, offset)) != 0) {
1362 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1363 if (ret < 0)
1364 return ret;
1365 offset = 0;
1366 len -= seg;
1367 data += seg;
1368 ++gfn;
1369 }
1370 return 0;
1371}
1372
Gleb Natapov49c77542010-10-18 15:22:23 +02001373int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1374 gpa_t gpa)
1375{
1376 struct kvm_memslots *slots = kvm_memslots(kvm);
1377 int offset = offset_in_page(gpa);
1378 gfn_t gfn = gpa >> PAGE_SHIFT;
1379
1380 ghc->gpa = gpa;
1381 ghc->generation = slots->generation;
1382 ghc->memslot = __gfn_to_memslot(slots, gfn);
1383 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1384 if (!kvm_is_error_hva(ghc->hva))
1385 ghc->hva += offset;
1386 else
1387 return -EFAULT;
1388
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1392
1393int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1394 void *data, unsigned long len)
1395{
1396 struct kvm_memslots *slots = kvm_memslots(kvm);
1397 int r;
1398
1399 if (slots->generation != ghc->generation)
1400 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1401
1402 if (kvm_is_error_hva(ghc->hva))
1403 return -EFAULT;
1404
1405 r = copy_to_user((void __user *)ghc->hva, data, len);
1406 if (r)
1407 return -EFAULT;
1408 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1409
1410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1413
Izik Eidus195aefd2007-10-01 22:14:18 +02001414int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1415{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001416 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1417 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001418}
1419EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1420
1421int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1422{
1423 gfn_t gfn = gpa >> PAGE_SHIFT;
1424 int seg;
1425 int offset = offset_in_page(gpa);
1426 int ret;
1427
1428 while ((seg = next_segment(len, offset)) != 0) {
1429 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1430 if (ret < 0)
1431 return ret;
1432 offset = 0;
1433 len -= seg;
1434 ++gfn;
1435 }
1436 return 0;
1437}
1438EXPORT_SYMBOL_GPL(kvm_clear_guest);
1439
Gleb Natapov49c77542010-10-18 15:22:23 +02001440void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1441 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001443 if (memslot && memslot->dirty_bitmap) {
1444 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445
Akinobu Mitacd7e48c2011-03-23 16:42:04 -07001446 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 }
1448}
1449
Gleb Natapov49c77542010-10-18 15:22:23 +02001450void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1451{
1452 struct kvm_memory_slot *memslot;
1453
1454 memslot = gfn_to_memslot(kvm, gfn);
1455 mark_page_dirty_in_slot(kvm, memslot, gfn);
1456}
1457
Eddie Dongb6958ce2007-07-18 12:15:21 +03001458/*
1459 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1460 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001461void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001462{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001463 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001464
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001465 for (;;) {
1466 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001467
Gleb Natapova1b37102009-07-09 15:33:52 +03001468 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001469 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001470 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001471 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001472 if (kvm_cpu_has_pending_timer(vcpu))
1473 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001474 if (signal_pending(current))
1475 break;
1476
Eddie Dongb6958ce2007-07-18 12:15:21 +03001477 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001478 }
1479
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001480 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001481}
1482
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483void kvm_resched(struct kvm_vcpu *vcpu)
1484{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001485 if (!need_resched())
1486 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489EXPORT_SYMBOL_GPL(kvm_resched);
1490
Rik van Riel217ece62011-02-01 09:53:28 -05001491void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001492{
Rik van Riel217ece62011-02-01 09:53:28 -05001493 struct kvm *kvm = me->kvm;
1494 struct kvm_vcpu *vcpu;
1495 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1496 int yielded = 0;
1497 int pass;
1498 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001499
Rik van Riel217ece62011-02-01 09:53:28 -05001500 /*
1501 * We boost the priority of a VCPU that is runnable but not
1502 * currently running, because it got preempted by something
1503 * else and called schedule in __vcpu_run. Hopefully that
1504 * VCPU is holding the lock that we need and will release it.
1505 * We approximate round-robin by starting at the last boosted VCPU.
1506 */
1507 for (pass = 0; pass < 2 && !yielded; pass++) {
1508 kvm_for_each_vcpu(i, vcpu, kvm) {
1509 struct task_struct *task = NULL;
1510 struct pid *pid;
1511 if (!pass && i < last_boosted_vcpu) {
1512 i = last_boosted_vcpu;
1513 continue;
1514 } else if (pass && i > last_boosted_vcpu)
1515 break;
1516 if (vcpu == me)
1517 continue;
1518 if (waitqueue_active(&vcpu->wq))
1519 continue;
1520 rcu_read_lock();
1521 pid = rcu_dereference(vcpu->pid);
1522 if (pid)
1523 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1524 rcu_read_unlock();
1525 if (!task)
1526 continue;
1527 if (task->flags & PF_VCPU) {
1528 put_task_struct(task);
1529 continue;
1530 }
1531 if (yield_to(task, 1)) {
1532 put_task_struct(task);
1533 kvm->last_boosted_vcpu = i;
1534 yielded = 1;
1535 break;
1536 }
1537 put_task_struct(task);
1538 }
1539 }
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001540}
1541EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1542
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001543static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001544{
1545 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001546 struct page *page;
1547
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001548 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001549 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001550#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001551 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001552 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001553#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001554#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1555 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1556 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1557#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001558 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001559 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001560 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001561 vmf->page = page;
1562 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001563}
1564
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001565static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001566 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001567};
1568
1569static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1570{
1571 vma->vm_ops = &kvm_vcpu_vm_ops;
1572 return 0;
1573}
1574
Avi Kivitybccf2152007-02-21 18:04:26 +02001575static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1576{
1577 struct kvm_vcpu *vcpu = filp->private_data;
1578
Al Viro66c0b392008-04-19 20:33:56 +01001579 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001580 return 0;
1581}
1582
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001583static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001584 .release = kvm_vcpu_release,
1585 .unlocked_ioctl = kvm_vcpu_ioctl,
1586 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001587 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001588 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001589};
1590
1591/*
1592 * Allocates an inode for the vcpu.
1593 */
1594static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1595{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001596 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001597}
1598
Avi Kivityc5ea7662007-02-20 18:41:05 +02001599/*
1600 * Creates some virtual cpus. Good luck creating more than one.
1601 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001602static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001603{
1604 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001605 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001606
Gleb Natapov73880c82009-06-09 15:56:28 +03001607 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001608 if (IS_ERR(vcpu))
1609 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001610
Avi Kivity15ad7142007-07-11 18:17:21 +03001611 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1612
Avi Kivity26e52152007-11-20 15:30:24 +02001613 r = kvm_arch_vcpu_setup(vcpu);
1614 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001615 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001616
Shaohua Li11ec2802007-07-23 14:51:37 +08001617 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001618 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1619 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001620 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001621 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001622
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001623 kvm_for_each_vcpu(r, v, kvm)
1624 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001625 r = -EEXIST;
1626 goto vcpu_destroy;
1627 }
1628
1629 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001630
1631 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001632 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001633 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001634 if (r < 0) {
1635 kvm_put_kvm(kvm);
1636 goto vcpu_destroy;
1637 }
1638
1639 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1640 smp_wmb();
1641 atomic_inc(&kvm->online_vcpus);
1642
1643#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1644 if (kvm->bsp_vcpu_id == id)
1645 kvm->bsp_vcpu = vcpu;
1646#endif
1647 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001648 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001649
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001650vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001651 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001652 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001653 return r;
1654}
1655
Avi Kivity1961d272007-03-05 19:46:05 +02001656static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1657{
1658 if (sigset) {
1659 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1660 vcpu->sigset_active = 1;
1661 vcpu->sigset = *sigset;
1662 } else
1663 vcpu->sigset_active = 0;
1664 return 0;
1665}
1666
Avi Kivitybccf2152007-02-21 18:04:26 +02001667static long kvm_vcpu_ioctl(struct file *filp,
1668 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Avi Kivitybccf2152007-02-21 18:04:26 +02001670 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001671 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001672 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001673 struct kvm_fpu *fpu = NULL;
1674 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001676 if (vcpu->kvm->mm != current->mm)
1677 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001678
1679#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1680 /*
1681 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1682 * so vcpu_load() would break it.
1683 */
1684 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1685 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1686#endif
1687
1688
1689 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001691 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001692 r = -EINVAL;
1693 if (arg)
1694 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001695 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001696 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001699 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001701 r = -ENOMEM;
1702 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1703 if (!kvm_regs)
1704 goto out;
1705 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001707 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001709 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1710 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001712out_free1:
1713 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 break;
1715 }
1716 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001717 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001719 r = -ENOMEM;
1720 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1721 if (!kvm_regs)
1722 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001724 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1725 goto out_free2;
1726 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001728 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001730out_free2:
1731 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 break;
1733 }
1734 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001735 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1736 r = -ENOMEM;
1737 if (!kvm_sregs)
1738 goto out;
1739 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 if (r)
1741 goto out;
1742 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001743 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 goto out;
1745 r = 0;
1746 break;
1747 }
1748 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001749 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1750 r = -ENOMEM;
1751 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001753 r = -EFAULT;
1754 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1755 goto out;
1756 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 if (r)
1758 goto out;
1759 r = 0;
1760 break;
1761 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001762 case KVM_GET_MP_STATE: {
1763 struct kvm_mp_state mp_state;
1764
1765 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1766 if (r)
1767 goto out;
1768 r = -EFAULT;
1769 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1770 goto out;
1771 r = 0;
1772 break;
1773 }
1774 case KVM_SET_MP_STATE: {
1775 struct kvm_mp_state mp_state;
1776
1777 r = -EFAULT;
1778 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1779 goto out;
1780 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1781 if (r)
1782 goto out;
1783 r = 0;
1784 break;
1785 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786 case KVM_TRANSLATE: {
1787 struct kvm_translation tr;
1788
1789 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001790 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001792 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 if (r)
1794 goto out;
1795 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001796 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797 goto out;
1798 r = 0;
1799 break;
1800 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001801 case KVM_SET_GUEST_DEBUG: {
1802 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803
1804 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001805 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001807 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808 if (r)
1809 goto out;
1810 r = 0;
1811 break;
1812 }
Avi Kivity1961d272007-03-05 19:46:05 +02001813 case KVM_SET_SIGNAL_MASK: {
1814 struct kvm_signal_mask __user *sigmask_arg = argp;
1815 struct kvm_signal_mask kvm_sigmask;
1816 sigset_t sigset, *p;
1817
1818 p = NULL;
1819 if (argp) {
1820 r = -EFAULT;
1821 if (copy_from_user(&kvm_sigmask, argp,
1822 sizeof kvm_sigmask))
1823 goto out;
1824 r = -EINVAL;
1825 if (kvm_sigmask.len != sizeof sigset)
1826 goto out;
1827 r = -EFAULT;
1828 if (copy_from_user(&sigset, sigmask_arg->sigset,
1829 sizeof sigset))
1830 goto out;
1831 p = &sigset;
1832 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001833 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001834 break;
1835 }
Avi Kivityb8836732007-04-01 16:34:31 +03001836 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001837 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1838 r = -ENOMEM;
1839 if (!fpu)
1840 goto out;
1841 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001842 if (r)
1843 goto out;
1844 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001845 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001846 goto out;
1847 r = 0;
1848 break;
1849 }
1850 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001851 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1852 r = -ENOMEM;
1853 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001854 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001855 r = -EFAULT;
1856 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1857 goto out;
1858 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001859 if (r)
1860 goto out;
1861 r = 0;
1862 break;
1863 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001864 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001865 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001866 }
1867out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001868 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001869 kfree(fpu);
1870 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001871 return r;
1872}
1873
1874static long kvm_vm_ioctl(struct file *filp,
1875 unsigned int ioctl, unsigned long arg)
1876{
1877 struct kvm *kvm = filp->private_data;
1878 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001879 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001880
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001881 if (kvm->mm != current->mm)
1882 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001883 switch (ioctl) {
1884 case KVM_CREATE_VCPU:
1885 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1886 if (r < 0)
1887 goto out;
1888 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001889 case KVM_SET_USER_MEMORY_REGION: {
1890 struct kvm_userspace_memory_region kvm_userspace_mem;
1891
1892 r = -EFAULT;
1893 if (copy_from_user(&kvm_userspace_mem, argp,
1894 sizeof kvm_userspace_mem))
1895 goto out;
1896
1897 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 if (r)
1899 goto out;
1900 break;
1901 }
1902 case KVM_GET_DIRTY_LOG: {
1903 struct kvm_dirty_log log;
1904
1905 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001906 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001908 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909 if (r)
1910 goto out;
1911 break;
1912 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001913#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1914 case KVM_REGISTER_COALESCED_MMIO: {
1915 struct kvm_coalesced_mmio_zone zone;
1916 r = -EFAULT;
1917 if (copy_from_user(&zone, argp, sizeof zone))
1918 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001919 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1920 if (r)
1921 goto out;
1922 r = 0;
1923 break;
1924 }
1925 case KVM_UNREGISTER_COALESCED_MMIO: {
1926 struct kvm_coalesced_mmio_zone zone;
1927 r = -EFAULT;
1928 if (copy_from_user(&zone, argp, sizeof zone))
1929 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001930 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1931 if (r)
1932 goto out;
1933 r = 0;
1934 break;
1935 }
1936#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001937 case KVM_IRQFD: {
1938 struct kvm_irqfd data;
1939
1940 r = -EFAULT;
1941 if (copy_from_user(&data, argp, sizeof data))
1942 goto out;
1943 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1944 break;
1945 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001946 case KVM_IOEVENTFD: {
1947 struct kvm_ioeventfd data;
1948
1949 r = -EFAULT;
1950 if (copy_from_user(&data, argp, sizeof data))
1951 goto out;
1952 r = kvm_ioeventfd(kvm, &data);
1953 break;
1954 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001955#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1956 case KVM_SET_BOOT_CPU_ID:
1957 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001958 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001959 if (atomic_read(&kvm->online_vcpus) != 0)
1960 r = -EBUSY;
1961 else
1962 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001963 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001964 break;
1965#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001966 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001967 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001968 if (r == -ENOTTY)
1969 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001970 }
1971out:
1972 return r;
1973}
1974
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001975#ifdef CONFIG_COMPAT
1976struct compat_kvm_dirty_log {
1977 __u32 slot;
1978 __u32 padding1;
1979 union {
1980 compat_uptr_t dirty_bitmap; /* one bit per page */
1981 __u64 padding2;
1982 };
1983};
1984
1985static long kvm_vm_compat_ioctl(struct file *filp,
1986 unsigned int ioctl, unsigned long arg)
1987{
1988 struct kvm *kvm = filp->private_data;
1989 int r;
1990
1991 if (kvm->mm != current->mm)
1992 return -EIO;
1993 switch (ioctl) {
1994 case KVM_GET_DIRTY_LOG: {
1995 struct compat_kvm_dirty_log compat_log;
1996 struct kvm_dirty_log log;
1997
1998 r = -EFAULT;
1999 if (copy_from_user(&compat_log, (void __user *)arg,
2000 sizeof(compat_log)))
2001 goto out;
2002 log.slot = compat_log.slot;
2003 log.padding1 = compat_log.padding1;
2004 log.padding2 = compat_log.padding2;
2005 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2006
2007 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2008 if (r)
2009 goto out;
2010 break;
2011 }
2012 default:
2013 r = kvm_vm_ioctl(filp, ioctl, arg);
2014 }
2015
2016out:
2017 return r;
2018}
2019#endif
2020
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002021static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002022{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002023 struct page *page[1];
2024 unsigned long addr;
2025 int npages;
2026 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002027 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002028
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002029 addr = gfn_to_hva(kvm, gfn);
2030 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002031 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002032
2033 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2034 NULL);
2035 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002036 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002037
2038 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002039 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002040}
2041
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002042static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002043 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002044};
2045
2046static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2047{
2048 vma->vm_ops = &kvm_vm_vm_ops;
2049 return 0;
2050}
2051
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002052static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002053 .release = kvm_vm_release,
2054 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002055#ifdef CONFIG_COMPAT
2056 .compat_ioctl = kvm_vm_compat_ioctl,
2057#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002058 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002059 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002060};
2061
2062static int kvm_dev_ioctl_create_vm(void)
2063{
Heiko Carstensaac87632010-10-27 17:22:10 +02002064 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002065 struct kvm *kvm;
2066
Avi Kivityf17abe92007-02-21 19:28:04 +02002067 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002068 if (IS_ERR(kvm))
2069 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002070#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2071 r = kvm_coalesced_mmio_init(kvm);
2072 if (r < 0) {
2073 kvm_put_kvm(kvm);
2074 return r;
2075 }
2076#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002077 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2078 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002079 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002080
Heiko Carstensaac87632010-10-27 17:22:10 +02002081 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002082}
2083
Avi Kivity1a811b62008-12-08 18:25:27 +02002084static long kvm_dev_ioctl_check_extension_generic(long arg)
2085{
2086 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002087 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002088 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002089 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002090#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2091 case KVM_CAP_SET_BOOT_CPU_ID:
2092#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002093 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002094 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002095#ifdef CONFIG_HAVE_KVM_IRQCHIP
2096 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002097 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002098#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002099 default:
2100 break;
2101 }
2102 return kvm_dev_ioctl_check_extension(arg);
2103}
2104
Avi Kivityf17abe92007-02-21 19:28:04 +02002105static long kvm_dev_ioctl(struct file *filp,
2106 unsigned int ioctl, unsigned long arg)
2107{
Avi Kivity07c45a32007-03-07 13:05:38 +02002108 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002109
2110 switch (ioctl) {
2111 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002112 r = -EINVAL;
2113 if (arg)
2114 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002115 r = KVM_API_VERSION;
2116 break;
2117 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002118 r = -EINVAL;
2119 if (arg)
2120 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002121 r = kvm_dev_ioctl_create_vm();
2122 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002123 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002124 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002125 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002126 case KVM_GET_VCPU_MMAP_SIZE:
2127 r = -EINVAL;
2128 if (arg)
2129 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002130 r = PAGE_SIZE; /* struct kvm_run */
2131#ifdef CONFIG_X86
2132 r += PAGE_SIZE; /* pio data page */
2133#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002134#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2135 r += PAGE_SIZE; /* coalesced mmio ring page */
2136#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002137 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002138 case KVM_TRACE_ENABLE:
2139 case KVM_TRACE_PAUSE:
2140 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002141 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002142 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002144 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145 }
2146out:
2147 return r;
2148}
2149
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 .unlocked_ioctl = kvm_dev_ioctl,
2152 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002153 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154};
2155
2156static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002157 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158 "kvm",
2159 &kvm_chardev_ops,
2160};
2161
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002162static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002163{
2164 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002165 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002166
Rusty Russell7f59f492008-12-07 21:25:45 +10302167 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002168 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002169
Rusty Russell7f59f492008-12-07 21:25:45 +10302170 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002171
2172 r = kvm_arch_hardware_enable(NULL);
2173
2174 if (r) {
2175 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2176 atomic_inc(&hardware_enable_failed);
2177 printk(KERN_INFO "kvm: enabling virtualization on "
2178 "CPU%d failed\n", cpu);
2179 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002180}
2181
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002182static void hardware_enable(void *junk)
2183{
Jan Kiszkae935b832011-02-08 12:55:33 +01002184 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002185 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002186 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002187}
2188
2189static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002190{
2191 int cpu = raw_smp_processor_id();
2192
Rusty Russell7f59f492008-12-07 21:25:45 +10302193 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002194 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302195 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002196 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002197}
2198
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002199static void hardware_disable(void *junk)
2200{
Jan Kiszkae935b832011-02-08 12:55:33 +01002201 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002202 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002203 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002204}
2205
Alexander Graf10474ae2009-09-15 11:37:46 +02002206static void hardware_disable_all_nolock(void)
2207{
2208 BUG_ON(!kvm_usage_count);
2209
2210 kvm_usage_count--;
2211 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002212 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002213}
2214
2215static void hardware_disable_all(void)
2216{
Jan Kiszkae935b832011-02-08 12:55:33 +01002217 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002218 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002219 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002220}
2221
2222static int hardware_enable_all(void)
2223{
2224 int r = 0;
2225
Jan Kiszkae935b832011-02-08 12:55:33 +01002226 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002227
2228 kvm_usage_count++;
2229 if (kvm_usage_count == 1) {
2230 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002231 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002232
2233 if (atomic_read(&hardware_enable_failed)) {
2234 hardware_disable_all_nolock();
2235 r = -EBUSY;
2236 }
2237 }
2238
Jan Kiszkae935b832011-02-08 12:55:33 +01002239 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002240
2241 return r;
2242}
2243
Avi Kivity774c47f2007-02-12 00:54:47 -08002244static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2245 void *v)
2246{
2247 int cpu = (long)v;
2248
Alexander Graf10474ae2009-09-15 11:37:46 +02002249 if (!kvm_usage_count)
2250 return NOTIFY_OK;
2251
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002252 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002253 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002254 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002255 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2256 cpu);
2257 hardware_disable(NULL);
2258 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002259 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002260 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2261 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002262 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002263 break;
2264 }
2265 return NOTIFY_OK;
2266}
2267
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002268
Avi Kivityb7c41452010-12-02 17:52:50 +02002269asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002270{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002271 /* Fault while not rebooting. We want the trace. */
2272 BUG();
2273}
Avi Kivityb7c41452010-12-02 17:52:50 +02002274EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002275
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002276static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002277 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002278{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002279 /*
2280 * Some (well, at least mine) BIOSes hang on reboot if
2281 * in vmx root mode.
2282 *
2283 * And Intel TXT required VMX off for all cpu when system shutdown.
2284 */
2285 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2286 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002287 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002288 return NOTIFY_OK;
2289}
2290
2291static struct notifier_block kvm_reboot_notifier = {
2292 .notifier_call = kvm_reboot,
2293 .priority = 0,
2294};
2295
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002296static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002297{
2298 int i;
2299
2300 for (i = 0; i < bus->dev_count; i++) {
2301 struct kvm_io_device *pos = bus->devs[i];
2302
2303 kvm_iodevice_destructor(pos);
2304 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002305 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002306}
2307
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002308/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002309int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002310 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002311{
2312 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002313 struct kvm_io_bus *bus;
2314
2315 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002316 for (i = 0; i < bus->dev_count; i++)
2317 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2318 return 0;
2319 return -EOPNOTSUPP;
2320}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002321
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002322/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002323int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2324 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002325{
2326 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002327 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002328
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002329 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_read(bus->devs[i], addr, len, val))
2332 return 0;
2333 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002334}
2335
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002336/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002337int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2338 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002339{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002340 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002341
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002342 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002343 if (bus->dev_count > NR_IOBUS_DEVS-1)
2344 return -ENOSPC;
2345
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002346 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2347 if (!new_bus)
2348 return -ENOMEM;
2349 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2350 new_bus->devs[new_bus->dev_count++] = dev;
2351 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2352 synchronize_srcu_expedited(&kvm->srcu);
2353 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002354
2355 return 0;
2356}
2357
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002358/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002359int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2360 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002361{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002362 int i, r;
2363 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002364
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002365 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2366 if (!new_bus)
2367 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002368
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002369 bus = kvm->buses[bus_idx];
2370 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2371
2372 r = -ENOENT;
2373 for (i = 0; i < new_bus->dev_count; i++)
2374 if (new_bus->devs[i] == dev) {
2375 r = 0;
2376 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002377 break;
2378 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002379
2380 if (r) {
2381 kfree(new_bus);
2382 return r;
2383 }
2384
2385 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2386 synchronize_srcu_expedited(&kvm->srcu);
2387 kfree(bus);
2388 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002389}
2390
Avi Kivity774c47f2007-02-12 00:54:47 -08002391static struct notifier_block kvm_cpu_notifier = {
2392 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002393};
2394
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002395static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002396{
2397 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002398 struct kvm *kvm;
2399
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002400 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002401 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002402 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002403 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002404 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002405 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002406}
2407
2408DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2409
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002410static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002411{
2412 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002413 struct kvm *kvm;
2414 struct kvm_vcpu *vcpu;
2415 int i;
2416
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002417 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002418 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002419 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002420 kvm_for_each_vcpu(i, vcpu, kvm)
2421 *val += *(u32 *)((void *)vcpu + offset);
2422
Jan Kiszkae935b832011-02-08 12:55:33 +01002423 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002424 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002425}
2426
Avi Kivityba1389b2007-11-18 16:24:12 +02002427DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2428
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002429static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002430 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2431 [KVM_STAT_VM] = &vm_stat_fops,
2432};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002433
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002434static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435{
2436 struct kvm_stats_debugfs_item *p;
2437
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002438 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002440 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002441 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002442 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
2445static void kvm_exit_debug(void)
2446{
2447 struct kvm_stats_debugfs_item *p;
2448
2449 for (p = debugfs_entries; p->name; ++p)
2450 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002451 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452}
2453
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002454static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002455{
Alexander Graf10474ae2009-09-15 11:37:46 +02002456 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002457 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002458 return 0;
2459}
2460
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002461static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002462{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002463 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002464 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002465 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002466 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002467}
2468
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002469static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08002470 .suspend = kvm_suspend,
2471 .resume = kvm_resume,
2472};
2473
Izik Eiduscea7bb22007-10-17 19:17:48 +02002474struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002475pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476
Avi Kivity15ad7142007-07-11 18:17:21 +03002477static inline
2478struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2479{
2480 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2481}
2482
2483static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2484{
2485 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2486
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002487 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002488}
2489
2490static void kvm_sched_out(struct preempt_notifier *pn,
2491 struct task_struct *next)
2492{
2493 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2494
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002495 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002496}
2497
Avi Kivity0ee75be2010-04-28 15:39:01 +03002498int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002499 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500{
2501 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002502 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002504 r = kvm_arch_init(opaque);
2505 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002506 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002507
2508 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2509
2510 if (bad_page == NULL) {
2511 r = -ENOMEM;
2512 goto out;
2513 }
2514
Anthony Liguori35149e22008-04-02 14:46:56 -05002515 bad_pfn = page_to_pfn(bad_page);
2516
Huang Yingbf998152010-05-31 14:28:19 +08002517 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2518
2519 if (hwpoison_page == NULL) {
2520 r = -ENOMEM;
2521 goto out_free_0;
2522 }
2523
2524 hwpoison_pfn = page_to_pfn(hwpoison_page);
2525
Gleb Natapovedba23e2010-07-07 20:16:45 +03002526 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2527
2528 if (fault_page == NULL) {
2529 r = -ENOMEM;
2530 goto out_free_0;
2531 }
2532
2533 fault_pfn = page_to_pfn(fault_page);
2534
Avi Kivity8437a612009-06-06 14:52:35 -07002535 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302536 r = -ENOMEM;
2537 goto out_free_0;
2538 }
2539
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002540 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302542 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543
Yang, Sheng002c7f72007-07-31 14:23:01 +03002544 for_each_online_cpu(cpu) {
2545 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002546 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002547 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002548 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002549 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002550 }
2551
Avi Kivity774c47f2007-02-12 00:54:47 -08002552 r = register_cpu_notifier(&kvm_cpu_notifier);
2553 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002554 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 register_reboot_notifier(&kvm_reboot_notifier);
2556
Rusty Russellc16f8622007-07-30 21:12:19 +10002557 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002558 if (!vcpu_align)
2559 vcpu_align = __alignof__(struct kvm_vcpu);
2560 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002561 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002562 if (!kvm_vcpu_cache) {
2563 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002564 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10002565 }
2566
Gleb Natapovaf585b92010-10-14 11:22:46 +02002567 r = kvm_async_pf_init();
2568 if (r)
2569 goto out_free;
2570
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002572 kvm_vm_fops.owner = module;
2573 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574
2575 r = misc_register(&kvm_dev);
2576 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002577 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002578 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579 }
2580
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002581 register_syscore_ops(&kvm_syscore_ops);
2582
Avi Kivity15ad7142007-07-11 18:17:21 +03002583 kvm_preempt_ops.sched_in = kvm_sched_in;
2584 kvm_preempt_ops.sched_out = kvm_sched_out;
2585
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002586 kvm_init_debug();
2587
Avi Kivityc7addb92007-09-16 18:58:32 +02002588 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589
Gleb Natapovaf585b92010-10-14 11:22:46 +02002590out_unreg:
2591 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002593 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002594out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002596 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002597out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002598out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002599 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302600out_free_0a:
2601 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002602out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002603 if (fault_page)
2604 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002605 if (hwpoison_page)
2606 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002607 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002608out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002609 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002610out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002611 return r;
2612}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002613EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002615void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002617 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002619 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002620 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002621 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002623 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002624 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002625 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002626 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302627 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002628 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002629 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002631EXPORT_SYMBOL_GPL(kvm_exit);