blob: 96ebc0679415baeff88d7764b1c0e956820adad2 [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 }
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700470
OGAWA Hirofumi85722cd2011-05-11 09:28:28 +0900471 spin_lock_init(&kvm->mmu_lock);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200472 kvm->mm = current->mm;
473 atomic_inc(&kvm->mm->mm_count);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400474 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800475 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300476 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200477 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300478 atomic_set(&kvm->users_count, 1);
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700479
480 r = kvm_init_mmu_notifier(kvm);
481 if (r)
482 goto out_err;
483
Jan Kiszkae935b832011-02-08 12:55:33 +0100484 raw_spin_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000485 list_add(&kvm->vm_list, &vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100486 raw_spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100487
Avi Kivityf17abe92007-02-21 19:28:04 +0200488 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200489
490out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100491 cleanup_srcu_struct(&kvm->srcu);
492out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200493 hardware_disable_all();
494out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200495 for (i = 0; i < KVM_NR_BUSES; i++)
496 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200497 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100498 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200499 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200500}
501
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900502static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
503{
504 if (!memslot->dirty_bitmap)
505 return;
506
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900507 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
508 vfree(memslot->dirty_bitmap_head);
509 else
510 kfree(memslot->dirty_bitmap_head);
511
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900512 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900513 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900514}
515
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516/*
517 * Free any memory in @free but not in @dont.
518 */
519static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
520 struct kvm_memory_slot *dont)
521{
Joerg Roedelec04b262009-06-19 15:16:23 +0200522 int i;
523
Izik Eidus290fc382007-09-27 14:11:22 +0200524 if (!dont || free->rmap != dont->rmap)
525 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526
527 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900528 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529
Joerg Roedelec04b262009-06-19 15:16:23 +0200530
531 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
532 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
533 vfree(free->lpage_info[i]);
534 free->lpage_info[i] = NULL;
535 }
536 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300537
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500539 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540}
541
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800542void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543{
544 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200545 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200547 for (i = 0; i < slots->nmemslots; ++i)
548 kvm_free_physmem_slot(&slots->memslots[i], NULL);
549
550 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551}
552
Avi Kivityf17abe92007-02-21 19:28:04 +0200553static void kvm_destroy_vm(struct kvm *kvm)
554{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200555 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200556 struct mm_struct *mm = kvm->mm;
557
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800558 kvm_arch_sync_events(kvm);
Jan Kiszkae935b832011-02-08 12:55:33 +0100559 raw_spin_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800560 list_del(&kvm->vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100561 raw_spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200562 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200563 for (i = 0; i < KVM_NR_BUSES; i++)
564 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200565 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200566#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
567 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200568#else
569 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200570#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800571 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100572 kvm_free_physmem(kvm);
573 cleanup_srcu_struct(&kvm->srcu);
574 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200575 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200576 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200577}
578
Izik Eidusd39f13b2008-03-30 16:01:25 +0300579void kvm_get_kvm(struct kvm *kvm)
580{
581 atomic_inc(&kvm->users_count);
582}
583EXPORT_SYMBOL_GPL(kvm_get_kvm);
584
585void kvm_put_kvm(struct kvm *kvm)
586{
587 if (atomic_dec_and_test(&kvm->users_count))
588 kvm_destroy_vm(kvm);
589}
590EXPORT_SYMBOL_GPL(kvm_put_kvm);
591
592
Avi Kivityf17abe92007-02-21 19:28:04 +0200593static int kvm_vm_release(struct inode *inode, struct file *filp)
594{
595 struct kvm *kvm = filp->private_data;
596
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400597 kvm_irqfd_release(kvm);
598
Izik Eidusd39f13b2008-03-30 16:01:25 +0300599 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 return 0;
601}
602
Heiko Carstensd48ead82011-01-17 21:21:08 +0100603#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900604/*
605 * Allocation size is twice as large as the actual dirty bitmap size.
606 * This makes it possible to do double buffering: see x86's
607 * kvm_vm_ioctl_get_dirty_log().
608 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900609static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
610{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900611 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900612
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900613 if (dirty_bytes > PAGE_SIZE)
614 memslot->dirty_bitmap = vzalloc(dirty_bytes);
615 else
616 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
617
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900618 if (!memslot->dirty_bitmap)
619 return -ENOMEM;
620
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900621 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900622 return 0;
623}
Heiko Carstensd48ead82011-01-17 21:21:08 +0100624#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900625
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 * Allocate some memory and give it an address in the guest physical address
628 * space.
629 *
630 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800631 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500632 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800634int __kvm_set_memory_region(struct kvm *kvm,
635 struct kvm_userspace_memory_region *mem,
636 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637{
Avi Kivity8234b222010-12-27 12:08:45 +0200638 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200640 unsigned long npages;
641 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642 struct kvm_memory_slot *memslot;
643 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200644 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
646 r = -EINVAL;
647 /* General sanity checks */
648 if (mem->memory_size & (PAGE_SIZE - 1))
649 goto out;
650 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
651 goto out;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +0900652 /* We can read the guest memory with __xxx_user() later on. */
653 if (user_alloc &&
654 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Heiko Carstens9e3bb6b2011-05-24 07:51:27 +0200655 !access_ok(VERIFY_WRITE,
656 (void __user *)(unsigned long)mem->userspace_addr,
657 mem->memory_size)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600658 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200659 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 goto out;
661 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
662 goto out;
663
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200664 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
666 npages = mem->memory_size >> PAGE_SHIFT;
667
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900668 r = -EINVAL;
669 if (npages > KVM_MEM_MAX_NR_PAGES)
670 goto out;
671
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 if (!npages)
673 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
674
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 new = old = *memslot;
676
Avi Kivitye36d96f2010-06-21 10:56:36 +0300677 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 new.base_gfn = base_gfn;
679 new.npages = npages;
680 new.flags = mem->flags;
681
682 /* Disallow changing a memory slot's size. */
683 r = -EINVAL;
684 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800685 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686
687 /* Check for overlaps */
688 r = -EEXIST;
689 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200690 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200692 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 continue;
694 if (!((base_gfn + npages <= s->base_gfn) ||
695 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800696 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699 /* Free page dirty bitmap if unneeded */
700 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000701 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702
703 r = -ENOMEM;
704
705 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200706#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500707 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900708 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200709
710 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800711 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200712
Izik Eidus80b14b52007-10-25 11:54:04 +0200713 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200714 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200716 if (!npages)
717 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300718
Joerg Roedelec04b262009-06-19 15:16:23 +0200719 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200720 unsigned long ugfn;
721 unsigned long j;
722 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200723 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300724
Joerg Roedelec04b262009-06-19 15:16:23 +0200725 /* Avoid unused variable warning if no large pages */
726 (void)level;
727
728 if (new.lpage_info[i])
729 continue;
730
Joerg Roedel82855412010-07-01 16:00:11 +0200731 lpages = 1 + ((base_gfn + npages - 1)
732 >> KVM_HPAGE_GFN_SHIFT(level));
733 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200734
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900735 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200736
737 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300738 goto out_free;
739
Joerg Roedel82855412010-07-01 16:00:11 +0200740 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200741 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200742 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200743 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300744 ugfn = new.userspace_addr >> PAGE_SHIFT;
745 /*
746 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300747 * other, or if explicitly asked to, disable large page
748 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300749 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200750 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300751 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200752 for (j = 0; j < lpages; ++j)
753 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300754 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755
Joerg Roedelec04b262009-06-19 15:16:23 +0200756skip_lpage:
757
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 /* Allocate page dirty bitmap if needed */
759 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900760 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800761 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200762 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200764#else /* not defined CONFIG_S390 */
765 new.user_alloc = user_alloc;
766 if (user_alloc)
767 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200768#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200770 if (!npages) {
771 r = -ENOMEM;
772 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
773 if (!slots)
774 goto out_free;
775 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
776 if (mem->slot >= slots->nmemslots)
777 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200778 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200779 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
780
781 old_memslots = kvm->memslots;
782 rcu_assign_pointer(kvm->memslots, slots);
783 synchronize_srcu_expedited(&kvm->srcu);
784 /* From this point no new shadow pages pointing to a deleted
785 * memslot will be created.
786 *
787 * validation of sp->gfn happens in:
788 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
789 * - kvm_is_visible_gfn (mmu_check_roots)
790 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300791 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200792 kfree(old_memslots);
793 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300794
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200795 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
796 if (r)
797 goto out_free;
798
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200799 /* map the pages in iommu page table */
800 if (npages) {
801 r = kvm_iommu_map_pages(kvm, &new);
802 if (r)
803 goto out_free;
804 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200805
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200806 r = -ENOMEM;
807 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
808 if (!slots)
809 goto out_free;
810 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
811 if (mem->slot >= slots->nmemslots)
812 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200813 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200814
815 /* actual memory is freed via old in kvm_free_physmem_slot below */
816 if (!npages) {
817 new.rmap = NULL;
818 new.dirty_bitmap = NULL;
819 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
820 new.lpage_info[i] = NULL;
821 }
822
823 slots->memslots[mem->slot] = new;
824 old_memslots = kvm->memslots;
825 rcu_assign_pointer(kvm->memslots, slots);
826 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200828 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800829
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200830 kvm_free_physmem_slot(&old, &new);
831 kfree(old_memslots);
832
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 return 0;
834
Sheng Yangf78e0e22007-10-29 09:40:42 +0800835out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 kvm_free_physmem_slot(&new, &old);
837out:
838 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200839
840}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800841EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
842
843int kvm_set_memory_region(struct kvm *kvm,
844 struct kvm_userspace_memory_region *mem,
845 int user_alloc)
846{
847 int r;
848
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200849 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800850 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200851 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800852 return r;
853}
Izik Eidus210c7c42007-10-24 23:52:57 +0200854EXPORT_SYMBOL_GPL(kvm_set_memory_region);
855
Carsten Otte1fe779f2007-10-29 16:08:35 +0100856int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
857 struct
858 kvm_userspace_memory_region *mem,
859 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200860{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200861 if (mem->slot >= KVM_MEMORY_SLOTS)
862 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200863 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864}
865
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800866int kvm_get_dirty_log(struct kvm *kvm,
867 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868{
869 struct kvm_memory_slot *memslot;
870 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900871 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 unsigned long any = 0;
873
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 r = -EINVAL;
875 if (log->slot >= KVM_MEMORY_SLOTS)
876 goto out;
877
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200878 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 r = -ENOENT;
880 if (!memslot->dirty_bitmap)
881 goto out;
882
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900883 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884
Uri Lublincd1a4a92007-02-22 16:43:09 +0200885 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 any = memslot->dirty_bitmap[i];
887
888 r = -EFAULT;
889 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
890 goto out;
891
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800892 if (any)
893 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
895 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 return r;
898}
899
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300900void kvm_disable_largepages(void)
901{
902 largepages_enabled = false;
903}
904EXPORT_SYMBOL_GPL(kvm_disable_largepages);
905
Izik Eiduscea7bb22007-10-17 19:17:48 +0200906int is_error_page(struct page *page)
907{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300908 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200909}
910EXPORT_SYMBOL_GPL(is_error_page);
911
Anthony Liguori35149e22008-04-02 14:46:56 -0500912int is_error_pfn(pfn_t pfn)
913{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300914 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500915}
916EXPORT_SYMBOL_GPL(is_error_pfn);
917
Huang Yingbf998152010-05-31 14:28:19 +0800918int is_hwpoison_pfn(pfn_t pfn)
919{
920 return pfn == hwpoison_pfn;
921}
922EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
923
Gleb Natapovedba23e2010-07-07 20:16:45 +0300924int is_fault_pfn(pfn_t pfn)
925{
926 return pfn == fault_pfn;
927}
928EXPORT_SYMBOL_GPL(is_fault_pfn);
929
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200930static inline unsigned long bad_hva(void)
931{
932 return PAGE_OFFSET;
933}
934
935int kvm_is_error_hva(unsigned long addr)
936{
937 return addr == bad_hva();
938}
939EXPORT_SYMBOL_GPL(kvm_is_error_hva);
940
Gleb Natapov49c77542010-10-18 15:22:23 +0200941static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
942 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943{
944 int i;
945
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200946 for (i = 0; i < slots->nmemslots; ++i) {
947 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948
949 if (gfn >= memslot->base_gfn
950 && gfn < memslot->base_gfn + memslot->npages)
951 return memslot;
952 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000953 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954}
Gleb Natapov49c77542010-10-18 15:22:23 +0200955
956struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
957{
958 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
959}
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300960EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961
Izik Eiduse0d62c72007-10-24 23:57:46 +0200962int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
963{
964 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800965 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200966
Izik Eiduse0d62c72007-10-24 23:57:46 +0200967 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200968 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200969
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200970 if (memslot->flags & KVM_MEMSLOT_INVALID)
971 continue;
972
Izik Eiduse0d62c72007-10-24 23:57:46 +0200973 if (gfn >= memslot->base_gfn
974 && gfn < memslot->base_gfn + memslot->npages)
975 return 1;
976 }
977 return 0;
978}
979EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
980
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100981unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
982{
983 struct vm_area_struct *vma;
984 unsigned long addr, size;
985
986 size = PAGE_SIZE;
987
988 addr = gfn_to_hva(kvm, gfn);
989 if (kvm_is_error_hva(addr))
990 return PAGE_SIZE;
991
992 down_read(&current->mm->mmap_sem);
993 vma = find_vma(current->mm, addr);
994 if (!vma)
995 goto out;
996
997 size = vma_kernel_pagesize(vma);
998
999out:
1000 up_read(&current->mm->mmap_sem);
1001
1002 return size;
1003}
1004
Gleb Natapov49c77542010-10-18 15:22:23 +02001005static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001006 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001007{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001008 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001009 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001010
1011 if (nr_pages)
1012 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1013
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001014 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001015}
Xiao Guangrong48987782010-08-22 19:11:43 +08001016
1017unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1018{
Gleb Natapov49c77542010-10-18 15:22:23 +02001019 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001020}
Sheng Yang0d150292008-04-25 21:44:50 +08001021EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001022
Gleb Natapov80300892010-10-19 18:13:41 +02001023static pfn_t get_fault_pfn(void)
1024{
1025 get_page(fault_page);
1026 return fault_pfn;
1027}
1028
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001029int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1030 unsigned long start, int write, struct page **page)
1031{
1032 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1033
1034 if (write)
1035 flags |= FOLL_WRITE;
1036
1037 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1038}
1039
Huang Yingfafc3db2011-01-30 11:15:49 +08001040static inline int check_user_page_hwpoison(unsigned long addr)
1041{
1042 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1043
1044 rc = __get_user_pages(current, current->mm, addr, 1,
1045 flags, NULL, NULL, NULL);
1046 return rc == -EHWPOISON;
1047}
1048
Gleb Natapovaf585b92010-10-14 11:22:46 +02001049static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001050 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001051{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001052 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001053 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001054 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001055
Gleb Natapovaf585b92010-10-14 11:22:46 +02001056 /* we can do it either atomically or asynchronously, not both */
1057 BUG_ON(atomic && async);
1058
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001059 BUG_ON(!write_fault && !writable);
1060
1061 if (writable)
1062 *writable = true;
1063
Gleb Natapovaf585b92010-10-14 11:22:46 +02001064 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001065 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001066
1067 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001068 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001069
1070 if (writable)
1071 *writable = write_fault;
1072
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001073 if (async) {
1074 down_read(&current->mm->mmap_sem);
1075 npages = get_user_page_nowait(current, current->mm,
1076 addr, write_fault, page);
1077 up_read(&current->mm->mmap_sem);
1078 } else
1079 npages = get_user_pages_fast(addr, 1, write_fault,
1080 page);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001081
1082 /* map read fault as writable if possible */
1083 if (unlikely(!write_fault) && npages == 1) {
1084 struct page *wpage[1];
1085
1086 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1087 if (npages == 1) {
1088 *writable = true;
1089 put_page(page[0]);
1090 page[0] = wpage[0];
1091 }
1092 npages = 1;
1093 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001094 }
Izik Eidus539cb662007-11-11 22:05:04 +02001095
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001096 if (unlikely(npages != 1)) {
1097 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001098
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001099 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001100 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001101
Huang Yingbbeb3402010-06-22 14:23:11 +08001102 down_read(&current->mm->mmap_sem);
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001103 if (npages == -EHWPOISON ||
1104 (!async && check_user_page_hwpoison(addr))) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001105 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001106 get_page(hwpoison_page);
1107 return page_to_pfn(hwpoison_page);
1108 }
1109
Gleb Natapov80300892010-10-19 18:13:41 +02001110 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001111
Gleb Natapov80300892010-10-19 18:13:41 +02001112 if (vma == NULL)
1113 pfn = get_fault_pfn();
1114 else if ((vma->vm_flags & VM_PFNMAP)) {
1115 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1116 vma->vm_pgoff;
1117 BUG_ON(!kvm_is_mmio_pfn(pfn));
1118 } else {
1119 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001120 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001121 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001122 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001123 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001124 } else
1125 pfn = page_to_pfn(page[0]);
1126
1127 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001128}
1129
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001130pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1131{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001132 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001133}
1134EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1135
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001136static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1137 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001138{
1139 unsigned long addr;
1140
Gleb Natapovaf585b92010-10-14 11:22:46 +02001141 if (async)
1142 *async = false;
1143
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001144 addr = gfn_to_hva(kvm, gfn);
1145 if (kvm_is_error_hva(addr)) {
1146 get_page(bad_page);
1147 return page_to_pfn(bad_page);
1148 }
1149
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001150 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001151}
1152
1153pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1154{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001155 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001156}
1157EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1158
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001159pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1160 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001161{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001162 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001163}
1164EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1165
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001166pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1167{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001168 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001169}
Anthony Liguori35149e22008-04-02 14:46:56 -05001170EXPORT_SYMBOL_GPL(gfn_to_pfn);
1171
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001172pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1173 bool *writable)
1174{
1175 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1176}
1177EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1178
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001179pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1180 struct kvm_memory_slot *slot, gfn_t gfn)
1181{
1182 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001183 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001184}
1185
Xiao Guangrong48987782010-08-22 19:11:43 +08001186int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1187 int nr_pages)
1188{
1189 unsigned long addr;
1190 gfn_t entry;
1191
Gleb Natapov49c77542010-10-18 15:22:23 +02001192 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001193 if (kvm_is_error_hva(addr))
1194 return -1;
1195
1196 if (entry < nr_pages)
1197 return 0;
1198
1199 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1200}
1201EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1202
Anthony Liguori35149e22008-04-02 14:46:56 -05001203struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1204{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001205 pfn_t pfn;
1206
1207 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001208 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001209 return pfn_to_page(pfn);
1210
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001211 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001212
1213 get_page(bad_page);
1214 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001215}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001216
Avi Kivity954bbbc2007-03-30 14:02:32 +03001217EXPORT_SYMBOL_GPL(gfn_to_page);
1218
Izik Eidusb4231d62007-11-20 11:49:33 +02001219void kvm_release_page_clean(struct page *page)
1220{
Anthony Liguori35149e22008-04-02 14:46:56 -05001221 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001222}
1223EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1224
Anthony Liguori35149e22008-04-02 14:46:56 -05001225void kvm_release_pfn_clean(pfn_t pfn)
1226{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001227 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001228 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001229}
1230EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1231
Izik Eidusb4231d62007-11-20 11:49:33 +02001232void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001233{
Anthony Liguori35149e22008-04-02 14:46:56 -05001234 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001235}
Izik Eidusb4231d62007-11-20 11:49:33 +02001236EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001237
Anthony Liguori35149e22008-04-02 14:46:56 -05001238void kvm_release_pfn_dirty(pfn_t pfn)
1239{
1240 kvm_set_pfn_dirty(pfn);
1241 kvm_release_pfn_clean(pfn);
1242}
1243EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1244
1245void kvm_set_page_dirty(struct page *page)
1246{
1247 kvm_set_pfn_dirty(page_to_pfn(page));
1248}
1249EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1250
1251void kvm_set_pfn_dirty(pfn_t pfn)
1252{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001253 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001254 struct page *page = pfn_to_page(pfn);
1255 if (!PageReserved(page))
1256 SetPageDirty(page);
1257 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001258}
1259EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1260
1261void kvm_set_pfn_accessed(pfn_t pfn)
1262{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001263 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001264 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001265}
1266EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1267
1268void kvm_get_pfn(pfn_t pfn)
1269{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001270 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001271 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001272}
1273EXPORT_SYMBOL_GPL(kvm_get_pfn);
1274
Izik Eidus195aefd2007-10-01 22:14:18 +02001275static int next_segment(unsigned long len, int offset)
1276{
1277 if (len > PAGE_SIZE - offset)
1278 return PAGE_SIZE - offset;
1279 else
1280 return len;
1281}
1282
1283int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1284 int len)
1285{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001286 int r;
1287 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001288
Izik Eiduse0506bc2007-11-11 22:10:22 +02001289 addr = gfn_to_hva(kvm, gfn);
1290 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001291 return -EFAULT;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001292 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001293 if (r)
1294 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001295 return 0;
1296}
1297EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1298
1299int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1300{
1301 gfn_t gfn = gpa >> PAGE_SHIFT;
1302 int seg;
1303 int offset = offset_in_page(gpa);
1304 int ret;
1305
1306 while ((seg = next_segment(len, offset)) != 0) {
1307 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1308 if (ret < 0)
1309 return ret;
1310 offset = 0;
1311 len -= seg;
1312 data += seg;
1313 ++gfn;
1314 }
1315 return 0;
1316}
1317EXPORT_SYMBOL_GPL(kvm_read_guest);
1318
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001319int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1320 unsigned long len)
1321{
1322 int r;
1323 unsigned long addr;
1324 gfn_t gfn = gpa >> PAGE_SHIFT;
1325 int offset = offset_in_page(gpa);
1326
1327 addr = gfn_to_hva(kvm, gfn);
1328 if (kvm_is_error_hva(addr))
1329 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001330 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001331 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001332 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001333 if (r)
1334 return -EFAULT;
1335 return 0;
1336}
1337EXPORT_SYMBOL(kvm_read_guest_atomic);
1338
Izik Eidus195aefd2007-10-01 22:14:18 +02001339int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1340 int offset, int len)
1341{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001342 int r;
1343 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001344
Izik Eiduse0506bc2007-11-11 22:10:22 +02001345 addr = gfn_to_hva(kvm, gfn);
1346 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001347 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001348 r = copy_to_user((void __user *)addr + offset, data, len);
1349 if (r)
1350 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001351 mark_page_dirty(kvm, gfn);
1352 return 0;
1353}
1354EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1355
1356int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1357 unsigned long len)
1358{
1359 gfn_t gfn = gpa >> PAGE_SHIFT;
1360 int seg;
1361 int offset = offset_in_page(gpa);
1362 int ret;
1363
1364 while ((seg = next_segment(len, offset)) != 0) {
1365 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1366 if (ret < 0)
1367 return ret;
1368 offset = 0;
1369 len -= seg;
1370 data += seg;
1371 ++gfn;
1372 }
1373 return 0;
1374}
1375
Gleb Natapov49c77542010-10-18 15:22:23 +02001376int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1377 gpa_t gpa)
1378{
1379 struct kvm_memslots *slots = kvm_memslots(kvm);
1380 int offset = offset_in_page(gpa);
1381 gfn_t gfn = gpa >> PAGE_SHIFT;
1382
1383 ghc->gpa = gpa;
1384 ghc->generation = slots->generation;
1385 ghc->memslot = __gfn_to_memslot(slots, gfn);
1386 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1387 if (!kvm_is_error_hva(ghc->hva))
1388 ghc->hva += offset;
1389 else
1390 return -EFAULT;
1391
1392 return 0;
1393}
1394EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1395
1396int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1397 void *data, unsigned long len)
1398{
1399 struct kvm_memslots *slots = kvm_memslots(kvm);
1400 int r;
1401
1402 if (slots->generation != ghc->generation)
1403 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1404
1405 if (kvm_is_error_hva(ghc->hva))
1406 return -EFAULT;
1407
1408 r = copy_to_user((void __user *)ghc->hva, data, len);
1409 if (r)
1410 return -EFAULT;
1411 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1412
1413 return 0;
1414}
1415EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1416
Izik Eidus195aefd2007-10-01 22:14:18 +02001417int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1418{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001419 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1420 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001421}
1422EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1423
1424int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1425{
1426 gfn_t gfn = gpa >> PAGE_SHIFT;
1427 int seg;
1428 int offset = offset_in_page(gpa);
1429 int ret;
1430
1431 while ((seg = next_segment(len, offset)) != 0) {
1432 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1433 if (ret < 0)
1434 return ret;
1435 offset = 0;
1436 len -= seg;
1437 ++gfn;
1438 }
1439 return 0;
1440}
1441EXPORT_SYMBOL_GPL(kvm_clear_guest);
1442
Gleb Natapov49c77542010-10-18 15:22:23 +02001443void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1444 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001446 if (memslot && memslot->dirty_bitmap) {
1447 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
Akinobu Mitacd7e48c2011-03-23 16:42:04 -07001449 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 }
1451}
1452
Gleb Natapov49c77542010-10-18 15:22:23 +02001453void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1454{
1455 struct kvm_memory_slot *memslot;
1456
1457 memslot = gfn_to_memslot(kvm, gfn);
1458 mark_page_dirty_in_slot(kvm, memslot, gfn);
1459}
1460
Eddie Dongb6958ce2007-07-18 12:15:21 +03001461/*
1462 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1463 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001464void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001465{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001466 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001467
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001468 for (;;) {
1469 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001470
Gleb Natapova1b37102009-07-09 15:33:52 +03001471 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001472 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001473 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001474 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001475 if (kvm_cpu_has_pending_timer(vcpu))
1476 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001477 if (signal_pending(current))
1478 break;
1479
Eddie Dongb6958ce2007-07-18 12:15:21 +03001480 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001481 }
1482
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001483 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001484}
1485
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486void kvm_resched(struct kvm_vcpu *vcpu)
1487{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001488 if (!need_resched())
1489 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491}
1492EXPORT_SYMBOL_GPL(kvm_resched);
1493
Rik van Riel217ece62011-02-01 09:53:28 -05001494void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001495{
Rik van Riel217ece62011-02-01 09:53:28 -05001496 struct kvm *kvm = me->kvm;
1497 struct kvm_vcpu *vcpu;
1498 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1499 int yielded = 0;
1500 int pass;
1501 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001502
Rik van Riel217ece62011-02-01 09:53:28 -05001503 /*
1504 * We boost the priority of a VCPU that is runnable but not
1505 * currently running, because it got preempted by something
1506 * else and called schedule in __vcpu_run. Hopefully that
1507 * VCPU is holding the lock that we need and will release it.
1508 * We approximate round-robin by starting at the last boosted VCPU.
1509 */
1510 for (pass = 0; pass < 2 && !yielded; pass++) {
1511 kvm_for_each_vcpu(i, vcpu, kvm) {
1512 struct task_struct *task = NULL;
1513 struct pid *pid;
1514 if (!pass && i < last_boosted_vcpu) {
1515 i = last_boosted_vcpu;
1516 continue;
1517 } else if (pass && i > last_boosted_vcpu)
1518 break;
1519 if (vcpu == me)
1520 continue;
1521 if (waitqueue_active(&vcpu->wq))
1522 continue;
1523 rcu_read_lock();
1524 pid = rcu_dereference(vcpu->pid);
1525 if (pid)
1526 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1527 rcu_read_unlock();
1528 if (!task)
1529 continue;
1530 if (task->flags & PF_VCPU) {
1531 put_task_struct(task);
1532 continue;
1533 }
1534 if (yield_to(task, 1)) {
1535 put_task_struct(task);
1536 kvm->last_boosted_vcpu = i;
1537 yielded = 1;
1538 break;
1539 }
1540 put_task_struct(task);
1541 }
1542 }
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001543}
1544EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1545
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001546static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001547{
1548 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001549 struct page *page;
1550
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001551 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001552 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001553#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001554 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001555 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001556#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001557#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1558 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1559 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1560#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001561 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001562 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001563 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001564 vmf->page = page;
1565 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001566}
1567
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001568static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001569 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001570};
1571
1572static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1573{
1574 vma->vm_ops = &kvm_vcpu_vm_ops;
1575 return 0;
1576}
1577
Avi Kivitybccf2152007-02-21 18:04:26 +02001578static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1579{
1580 struct kvm_vcpu *vcpu = filp->private_data;
1581
Al Viro66c0b392008-04-19 20:33:56 +01001582 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001583 return 0;
1584}
1585
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001586static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001587 .release = kvm_vcpu_release,
1588 .unlocked_ioctl = kvm_vcpu_ioctl,
1589 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001590 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001591 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001592};
1593
1594/*
1595 * Allocates an inode for the vcpu.
1596 */
1597static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1598{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001599 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001600}
1601
Avi Kivityc5ea7662007-02-20 18:41:05 +02001602/*
1603 * Creates some virtual cpus. Good luck creating more than one.
1604 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001605static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001606{
1607 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001608 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001609
Gleb Natapov73880c82009-06-09 15:56:28 +03001610 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001611 if (IS_ERR(vcpu))
1612 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001613
Avi Kivity15ad7142007-07-11 18:17:21 +03001614 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1615
Avi Kivity26e52152007-11-20 15:30:24 +02001616 r = kvm_arch_vcpu_setup(vcpu);
1617 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001618 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001619
Shaohua Li11ec2802007-07-23 14:51:37 +08001620 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001621 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1622 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001623 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001624 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001625
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001626 kvm_for_each_vcpu(r, v, kvm)
1627 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001628 r = -EEXIST;
1629 goto vcpu_destroy;
1630 }
1631
1632 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001633
1634 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001635 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001636 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001637 if (r < 0) {
1638 kvm_put_kvm(kvm);
1639 goto vcpu_destroy;
1640 }
1641
1642 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1643 smp_wmb();
1644 atomic_inc(&kvm->online_vcpus);
1645
1646#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1647 if (kvm->bsp_vcpu_id == id)
1648 kvm->bsp_vcpu = vcpu;
1649#endif
1650 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001651 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001652
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001653vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001654 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001655 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001656 return r;
1657}
1658
Avi Kivity1961d272007-03-05 19:46:05 +02001659static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1660{
1661 if (sigset) {
1662 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1663 vcpu->sigset_active = 1;
1664 vcpu->sigset = *sigset;
1665 } else
1666 vcpu->sigset_active = 0;
1667 return 0;
1668}
1669
Avi Kivitybccf2152007-02-21 18:04:26 +02001670static long kvm_vcpu_ioctl(struct file *filp,
1671 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672{
Avi Kivitybccf2152007-02-21 18:04:26 +02001673 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001674 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001675 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001676 struct kvm_fpu *fpu = NULL;
1677 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001679 if (vcpu->kvm->mm != current->mm)
1680 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001681
1682#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1683 /*
1684 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1685 * so vcpu_load() would break it.
1686 */
1687 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1688 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1689#endif
1690
1691
1692 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001694 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001695 r = -EINVAL;
1696 if (arg)
1697 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001698 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001699 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001702 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001704 r = -ENOMEM;
1705 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1706 if (!kvm_regs)
1707 goto out;
1708 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001710 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001712 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1713 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001715out_free1:
1716 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717 break;
1718 }
1719 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001720 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001722 r = -ENOMEM;
1723 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1724 if (!kvm_regs)
1725 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001727 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1728 goto out_free2;
1729 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001731 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001733out_free2:
1734 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 break;
1736 }
1737 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001738 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1739 r = -ENOMEM;
1740 if (!kvm_sregs)
1741 goto out;
1742 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 if (r)
1744 goto out;
1745 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001746 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 goto out;
1748 r = 0;
1749 break;
1750 }
1751 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001752 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1753 r = -ENOMEM;
1754 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001756 r = -EFAULT;
1757 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1758 goto out;
1759 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760 if (r)
1761 goto out;
1762 r = 0;
1763 break;
1764 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001765 case KVM_GET_MP_STATE: {
1766 struct kvm_mp_state mp_state;
1767
1768 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1769 if (r)
1770 goto out;
1771 r = -EFAULT;
1772 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1773 goto out;
1774 r = 0;
1775 break;
1776 }
1777 case KVM_SET_MP_STATE: {
1778 struct kvm_mp_state mp_state;
1779
1780 r = -EFAULT;
1781 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1782 goto out;
1783 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1784 if (r)
1785 goto out;
1786 r = 0;
1787 break;
1788 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 case KVM_TRANSLATE: {
1790 struct kvm_translation tr;
1791
1792 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001793 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001795 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796 if (r)
1797 goto out;
1798 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001799 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 goto out;
1801 r = 0;
1802 break;
1803 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001804 case KVM_SET_GUEST_DEBUG: {
1805 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806
1807 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001808 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001810 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811 if (r)
1812 goto out;
1813 r = 0;
1814 break;
1815 }
Avi Kivity1961d272007-03-05 19:46:05 +02001816 case KVM_SET_SIGNAL_MASK: {
1817 struct kvm_signal_mask __user *sigmask_arg = argp;
1818 struct kvm_signal_mask kvm_sigmask;
1819 sigset_t sigset, *p;
1820
1821 p = NULL;
1822 if (argp) {
1823 r = -EFAULT;
1824 if (copy_from_user(&kvm_sigmask, argp,
1825 sizeof kvm_sigmask))
1826 goto out;
1827 r = -EINVAL;
1828 if (kvm_sigmask.len != sizeof sigset)
1829 goto out;
1830 r = -EFAULT;
1831 if (copy_from_user(&sigset, sigmask_arg->sigset,
1832 sizeof sigset))
1833 goto out;
1834 p = &sigset;
1835 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001836 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001837 break;
1838 }
Avi Kivityb8836732007-04-01 16:34:31 +03001839 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001840 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1841 r = -ENOMEM;
1842 if (!fpu)
1843 goto out;
1844 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001845 if (r)
1846 goto out;
1847 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001848 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001849 goto out;
1850 r = 0;
1851 break;
1852 }
1853 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001854 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1855 r = -ENOMEM;
1856 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001857 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001858 r = -EFAULT;
1859 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1860 goto out;
1861 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001862 if (r)
1863 goto out;
1864 r = 0;
1865 break;
1866 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001867 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001868 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001869 }
1870out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001871 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001872 kfree(fpu);
1873 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001874 return r;
1875}
1876
1877static long kvm_vm_ioctl(struct file *filp,
1878 unsigned int ioctl, unsigned long arg)
1879{
1880 struct kvm *kvm = filp->private_data;
1881 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001882 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001883
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001884 if (kvm->mm != current->mm)
1885 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001886 switch (ioctl) {
1887 case KVM_CREATE_VCPU:
1888 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1889 if (r < 0)
1890 goto out;
1891 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001892 case KVM_SET_USER_MEMORY_REGION: {
1893 struct kvm_userspace_memory_region kvm_userspace_mem;
1894
1895 r = -EFAULT;
1896 if (copy_from_user(&kvm_userspace_mem, argp,
1897 sizeof kvm_userspace_mem))
1898 goto out;
1899
1900 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901 if (r)
1902 goto out;
1903 break;
1904 }
1905 case KVM_GET_DIRTY_LOG: {
1906 struct kvm_dirty_log log;
1907
1908 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001909 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001911 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 if (r)
1913 goto out;
1914 break;
1915 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001916#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1917 case KVM_REGISTER_COALESCED_MMIO: {
1918 struct kvm_coalesced_mmio_zone zone;
1919 r = -EFAULT;
1920 if (copy_from_user(&zone, argp, sizeof zone))
1921 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001922 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1923 if (r)
1924 goto out;
1925 r = 0;
1926 break;
1927 }
1928 case KVM_UNREGISTER_COALESCED_MMIO: {
1929 struct kvm_coalesced_mmio_zone zone;
1930 r = -EFAULT;
1931 if (copy_from_user(&zone, argp, sizeof zone))
1932 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001933 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1934 if (r)
1935 goto out;
1936 r = 0;
1937 break;
1938 }
1939#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001940 case KVM_IRQFD: {
1941 struct kvm_irqfd data;
1942
1943 r = -EFAULT;
1944 if (copy_from_user(&data, argp, sizeof data))
1945 goto out;
1946 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1947 break;
1948 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001949 case KVM_IOEVENTFD: {
1950 struct kvm_ioeventfd data;
1951
1952 r = -EFAULT;
1953 if (copy_from_user(&data, argp, sizeof data))
1954 goto out;
1955 r = kvm_ioeventfd(kvm, &data);
1956 break;
1957 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001958#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1959 case KVM_SET_BOOT_CPU_ID:
1960 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001961 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001962 if (atomic_read(&kvm->online_vcpus) != 0)
1963 r = -EBUSY;
1964 else
1965 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001966 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001967 break;
1968#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001969 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001970 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001971 if (r == -ENOTTY)
1972 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001973 }
1974out:
1975 return r;
1976}
1977
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001978#ifdef CONFIG_COMPAT
1979struct compat_kvm_dirty_log {
1980 __u32 slot;
1981 __u32 padding1;
1982 union {
1983 compat_uptr_t dirty_bitmap; /* one bit per page */
1984 __u64 padding2;
1985 };
1986};
1987
1988static long kvm_vm_compat_ioctl(struct file *filp,
1989 unsigned int ioctl, unsigned long arg)
1990{
1991 struct kvm *kvm = filp->private_data;
1992 int r;
1993
1994 if (kvm->mm != current->mm)
1995 return -EIO;
1996 switch (ioctl) {
1997 case KVM_GET_DIRTY_LOG: {
1998 struct compat_kvm_dirty_log compat_log;
1999 struct kvm_dirty_log log;
2000
2001 r = -EFAULT;
2002 if (copy_from_user(&compat_log, (void __user *)arg,
2003 sizeof(compat_log)))
2004 goto out;
2005 log.slot = compat_log.slot;
2006 log.padding1 = compat_log.padding1;
2007 log.padding2 = compat_log.padding2;
2008 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2009
2010 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2011 if (r)
2012 goto out;
2013 break;
2014 }
2015 default:
2016 r = kvm_vm_ioctl(filp, ioctl, arg);
2017 }
2018
2019out:
2020 return r;
2021}
2022#endif
2023
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002024static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002025{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002026 struct page *page[1];
2027 unsigned long addr;
2028 int npages;
2029 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002030 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002031
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002032 addr = gfn_to_hva(kvm, gfn);
2033 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002034 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002035
2036 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2037 NULL);
2038 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002039 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002040
2041 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002042 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002043}
2044
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002045static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002046 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002047};
2048
2049static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2050{
2051 vma->vm_ops = &kvm_vm_vm_ops;
2052 return 0;
2053}
2054
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002055static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002056 .release = kvm_vm_release,
2057 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002058#ifdef CONFIG_COMPAT
2059 .compat_ioctl = kvm_vm_compat_ioctl,
2060#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002061 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002062 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002063};
2064
2065static int kvm_dev_ioctl_create_vm(void)
2066{
Heiko Carstensaac87632010-10-27 17:22:10 +02002067 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002068 struct kvm *kvm;
2069
Avi Kivityf17abe92007-02-21 19:28:04 +02002070 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002071 if (IS_ERR(kvm))
2072 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002073#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2074 r = kvm_coalesced_mmio_init(kvm);
2075 if (r < 0) {
2076 kvm_put_kvm(kvm);
2077 return r;
2078 }
2079#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002080 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2081 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002082 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002083
Heiko Carstensaac87632010-10-27 17:22:10 +02002084 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002085}
2086
Avi Kivity1a811b62008-12-08 18:25:27 +02002087static long kvm_dev_ioctl_check_extension_generic(long arg)
2088{
2089 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002090 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002091 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002092 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002093#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2094 case KVM_CAP_SET_BOOT_CPU_ID:
2095#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002096 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002097 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002098#ifdef CONFIG_HAVE_KVM_IRQCHIP
2099 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002100 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002101#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002102 default:
2103 break;
2104 }
2105 return kvm_dev_ioctl_check_extension(arg);
2106}
2107
Avi Kivityf17abe92007-02-21 19:28:04 +02002108static long kvm_dev_ioctl(struct file *filp,
2109 unsigned int ioctl, unsigned long arg)
2110{
Avi Kivity07c45a32007-03-07 13:05:38 +02002111 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002112
2113 switch (ioctl) {
2114 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002115 r = -EINVAL;
2116 if (arg)
2117 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002118 r = KVM_API_VERSION;
2119 break;
2120 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002121 r = -EINVAL;
2122 if (arg)
2123 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002124 r = kvm_dev_ioctl_create_vm();
2125 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002126 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002127 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002128 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002129 case KVM_GET_VCPU_MMAP_SIZE:
2130 r = -EINVAL;
2131 if (arg)
2132 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002133 r = PAGE_SIZE; /* struct kvm_run */
2134#ifdef CONFIG_X86
2135 r += PAGE_SIZE; /* pio data page */
2136#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002137#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2138 r += PAGE_SIZE; /* coalesced mmio ring page */
2139#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002140 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002141 case KVM_TRACE_ENABLE:
2142 case KVM_TRACE_PAUSE:
2143 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002144 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002147 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 }
2149out:
2150 return r;
2151}
2152
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154 .unlocked_ioctl = kvm_dev_ioctl,
2155 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002156 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157};
2158
2159static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002160 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161 "kvm",
2162 &kvm_chardev_ops,
2163};
2164
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002165static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002166{
2167 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002168 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002169
Rusty Russell7f59f492008-12-07 21:25:45 +10302170 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002171 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002172
Rusty Russell7f59f492008-12-07 21:25:45 +10302173 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002174
2175 r = kvm_arch_hardware_enable(NULL);
2176
2177 if (r) {
2178 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2179 atomic_inc(&hardware_enable_failed);
2180 printk(KERN_INFO "kvm: enabling virtualization on "
2181 "CPU%d failed\n", cpu);
2182 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002183}
2184
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002185static void hardware_enable(void *junk)
2186{
Jan Kiszkae935b832011-02-08 12:55:33 +01002187 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002188 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002189 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002190}
2191
2192static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002193{
2194 int cpu = raw_smp_processor_id();
2195
Rusty Russell7f59f492008-12-07 21:25:45 +10302196 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002197 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302198 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002199 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002200}
2201
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002202static void hardware_disable(void *junk)
2203{
Jan Kiszkae935b832011-02-08 12:55:33 +01002204 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002205 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002206 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002207}
2208
Alexander Graf10474ae2009-09-15 11:37:46 +02002209static void hardware_disable_all_nolock(void)
2210{
2211 BUG_ON(!kvm_usage_count);
2212
2213 kvm_usage_count--;
2214 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002215 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002216}
2217
2218static void hardware_disable_all(void)
2219{
Jan Kiszkae935b832011-02-08 12:55:33 +01002220 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002221 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002222 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002223}
2224
2225static int hardware_enable_all(void)
2226{
2227 int r = 0;
2228
Jan Kiszkae935b832011-02-08 12:55:33 +01002229 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002230
2231 kvm_usage_count++;
2232 if (kvm_usage_count == 1) {
2233 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002234 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002235
2236 if (atomic_read(&hardware_enable_failed)) {
2237 hardware_disable_all_nolock();
2238 r = -EBUSY;
2239 }
2240 }
2241
Jan Kiszkae935b832011-02-08 12:55:33 +01002242 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002243
2244 return r;
2245}
2246
Avi Kivity774c47f2007-02-12 00:54:47 -08002247static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2248 void *v)
2249{
2250 int cpu = (long)v;
2251
Alexander Graf10474ae2009-09-15 11:37:46 +02002252 if (!kvm_usage_count)
2253 return NOTIFY_OK;
2254
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002255 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002256 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002257 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002258 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2259 cpu);
2260 hardware_disable(NULL);
2261 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002262 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002263 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2264 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002265 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002266 break;
2267 }
2268 return NOTIFY_OK;
2269}
2270
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002271
Avi Kivityb7c41452010-12-02 17:52:50 +02002272asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002273{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002274 /* Fault while not rebooting. We want the trace. */
2275 BUG();
2276}
Avi Kivityb7c41452010-12-02 17:52:50 +02002277EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002278
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002279static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002280 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002281{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002282 /*
2283 * Some (well, at least mine) BIOSes hang on reboot if
2284 * in vmx root mode.
2285 *
2286 * And Intel TXT required VMX off for all cpu when system shutdown.
2287 */
2288 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2289 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002290 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002291 return NOTIFY_OK;
2292}
2293
2294static struct notifier_block kvm_reboot_notifier = {
2295 .notifier_call = kvm_reboot,
2296 .priority = 0,
2297};
2298
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002299static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002300{
2301 int i;
2302
2303 for (i = 0; i < bus->dev_count; i++) {
2304 struct kvm_io_device *pos = bus->devs[i];
2305
2306 kvm_iodevice_destructor(pos);
2307 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002308 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002309}
2310
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002311/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002312int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002313 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002314{
2315 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002316 struct kvm_io_bus *bus;
2317
2318 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002319 for (i = 0; i < bus->dev_count; i++)
2320 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2321 return 0;
2322 return -EOPNOTSUPP;
2323}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002324
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002325/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002326int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2327 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002328{
2329 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002330 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002331
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002332 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002333 for (i = 0; i < bus->dev_count; i++)
2334 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2335 return 0;
2336 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002337}
2338
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002339/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002340int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2341 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002342{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002343 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002344
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002345 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002346 if (bus->dev_count > NR_IOBUS_DEVS-1)
2347 return -ENOSPC;
2348
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002349 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2350 if (!new_bus)
2351 return -ENOMEM;
2352 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2353 new_bus->devs[new_bus->dev_count++] = dev;
2354 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2355 synchronize_srcu_expedited(&kvm->srcu);
2356 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002357
2358 return 0;
2359}
2360
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002361/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002362int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2363 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002364{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002365 int i, r;
2366 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002367
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002368 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2369 if (!new_bus)
2370 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002371
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002372 bus = kvm->buses[bus_idx];
2373 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2374
2375 r = -ENOENT;
2376 for (i = 0; i < new_bus->dev_count; i++)
2377 if (new_bus->devs[i] == dev) {
2378 r = 0;
2379 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002380 break;
2381 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002382
2383 if (r) {
2384 kfree(new_bus);
2385 return r;
2386 }
2387
2388 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2389 synchronize_srcu_expedited(&kvm->srcu);
2390 kfree(bus);
2391 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002392}
2393
Avi Kivity774c47f2007-02-12 00:54:47 -08002394static struct notifier_block kvm_cpu_notifier = {
2395 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002396};
2397
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002398static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002399{
2400 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002401 struct kvm *kvm;
2402
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002403 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002404 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002405 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002406 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002407 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002408 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002409}
2410
2411DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2412
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002413static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002414{
2415 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002416 struct kvm *kvm;
2417 struct kvm_vcpu *vcpu;
2418 int i;
2419
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002420 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002421 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002422 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002423 kvm_for_each_vcpu(i, vcpu, kvm)
2424 *val += *(u32 *)((void *)vcpu + offset);
2425
Jan Kiszkae935b832011-02-08 12:55:33 +01002426 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002427 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002428}
2429
Avi Kivityba1389b2007-11-18 16:24:12 +02002430DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2431
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002432static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002433 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2434 [KVM_STAT_VM] = &vm_stat_fops,
2435};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002436
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002437static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438{
2439 struct kvm_stats_debugfs_item *p;
2440
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002441 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002443 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002444 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002445 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446}
2447
2448static void kvm_exit_debug(void)
2449{
2450 struct kvm_stats_debugfs_item *p;
2451
2452 for (p = debugfs_entries; p->name; ++p)
2453 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002454 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455}
2456
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002457static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002458{
Alexander Graf10474ae2009-09-15 11:37:46 +02002459 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002460 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002461 return 0;
2462}
2463
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002464static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002465{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002466 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002467 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002468 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002469 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002470}
2471
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002472static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08002473 .suspend = kvm_suspend,
2474 .resume = kvm_resume,
2475};
2476
Izik Eiduscea7bb22007-10-17 19:17:48 +02002477struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002478pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479
Avi Kivity15ad7142007-07-11 18:17:21 +03002480static inline
2481struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2482{
2483 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2484}
2485
2486static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2487{
2488 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2489
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002490 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002491}
2492
2493static void kvm_sched_out(struct preempt_notifier *pn,
2494 struct task_struct *next)
2495{
2496 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2497
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002498 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002499}
2500
Avi Kivity0ee75be2010-04-28 15:39:01 +03002501int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002502 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503{
2504 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002505 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002507 r = kvm_arch_init(opaque);
2508 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002509 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002510
2511 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2512
2513 if (bad_page == NULL) {
2514 r = -ENOMEM;
2515 goto out;
2516 }
2517
Anthony Liguori35149e22008-04-02 14:46:56 -05002518 bad_pfn = page_to_pfn(bad_page);
2519
Huang Yingbf998152010-05-31 14:28:19 +08002520 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2521
2522 if (hwpoison_page == NULL) {
2523 r = -ENOMEM;
2524 goto out_free_0;
2525 }
2526
2527 hwpoison_pfn = page_to_pfn(hwpoison_page);
2528
Gleb Natapovedba23e2010-07-07 20:16:45 +03002529 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2530
2531 if (fault_page == NULL) {
2532 r = -ENOMEM;
2533 goto out_free_0;
2534 }
2535
2536 fault_pfn = page_to_pfn(fault_page);
2537
Avi Kivity8437a612009-06-06 14:52:35 -07002538 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302539 r = -ENOMEM;
2540 goto out_free_0;
2541 }
2542
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002543 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302545 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546
Yang, Sheng002c7f72007-07-31 14:23:01 +03002547 for_each_online_cpu(cpu) {
2548 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002549 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002550 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002551 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002552 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002553 }
2554
Avi Kivity774c47f2007-02-12 00:54:47 -08002555 r = register_cpu_notifier(&kvm_cpu_notifier);
2556 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002557 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 register_reboot_notifier(&kvm_reboot_notifier);
2559
Rusty Russellc16f8622007-07-30 21:12:19 +10002560 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002561 if (!vcpu_align)
2562 vcpu_align = __alignof__(struct kvm_vcpu);
2563 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002564 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002565 if (!kvm_vcpu_cache) {
2566 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002567 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10002568 }
2569
Gleb Natapovaf585b92010-10-14 11:22:46 +02002570 r = kvm_async_pf_init();
2571 if (r)
2572 goto out_free;
2573
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002575 kvm_vm_fops.owner = module;
2576 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577
2578 r = misc_register(&kvm_dev);
2579 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002580 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002581 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 }
2583
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002584 register_syscore_ops(&kvm_syscore_ops);
2585
Avi Kivity15ad7142007-07-11 18:17:21 +03002586 kvm_preempt_ops.sched_in = kvm_sched_in;
2587 kvm_preempt_ops.sched_out = kvm_sched_out;
2588
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002589 kvm_init_debug();
2590
Avi Kivityc7addb92007-09-16 18:58:32 +02002591 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592
Gleb Natapovaf585b92010-10-14 11:22:46 +02002593out_unreg:
2594 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002596 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002597out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002599 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002600out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002601out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002602 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302603out_free_0a:
2604 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002605out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002606 if (fault_page)
2607 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002608 if (hwpoison_page)
2609 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002610 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002611out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002612 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002613out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 return r;
2615}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002616EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002618void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002620 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002622 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002623 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002624 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002626 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002627 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002628 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002629 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302630 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002631 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002632 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002634EXPORT_SYMBOL_GPL(kvm_exit);