blob: 7bee6dc8cdb2b5cc65dcc68b0ecf67aa4774d446 [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>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020046#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020047#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010048#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040052#include <asm/io.h>
53#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020054#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Laurent Vivier5f94c172008-05-30 16:05:54 +020056#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020057#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020058
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059#define CREATE_TRACE_POINTS
60#include <trace/events/kvm.h>
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Marcelo Tosattifa40a822009-06-04 15:08:24 -030065/*
66 * Ordering of locks:
67 *
Sheng Yangfae3a352009-12-15 10:28:07 +080068 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030069 */
70
Jan Kiszkae935b832011-02-08 12:55:33 +010071DEFINE_RAW_SPINLOCK(kvm_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080073
Rusty Russell7f59f492008-12-07 21:25:45 +103074static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020075static int kvm_usage_count = 0;
76static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030077
Rusty Russellc16f8622007-07-30 21:12:19 +100078struct kmem_cache *kvm_vcpu_cache;
79EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030080
Avi Kivity15ad7142007-07-11 18:17:21 +030081static __read_mostly struct preempt_ops kvm_preempt_ops;
82
Hollis Blanchard76f7c872008-04-15 16:05:42 -050083struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Avi Kivitybccf2152007-02-21 18:04:26 +020085static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
86 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020087static int hardware_enable_all(void);
88static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020089
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020090static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
91
Avi Kivityb7c41452010-12-02 17:52:50 +020092bool kvm_rebooting;
93EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +030094
Marcelo Tosatti54dee992009-06-11 12:07:44 -030095static bool largepages_enabled = true;
96
Gleb Natapovfa7bff82010-07-07 20:16:44 +030097static struct page *hwpoison_page;
98static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +080099
Gleb Natapovedba23e2010-07-07 20:16:45 +0300100static struct page *fault_page;
101static pfn_t fault_pfn;
102
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800103inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300104{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100105 if (pfn_valid(pfn)) {
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800106 int reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800107 struct page *tail = pfn_to_page(pfn);
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800108 struct page *head = compound_trans_head(tail);
109 reserved = PageReserved(head);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800110 if (head != tail) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800111 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800112 * "head" is not a dangling pointer
113 * (compound_trans_head takes care of that)
114 * but the hugepage may have been splitted
115 * from under us (and we may not hold a
116 * reference count on the head page so it can
117 * be reused before we run PageReferenced), so
118 * we've to check PageTail before returning
119 * what we just read.
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800120 */
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800121 smp_rmb();
122 if (PageTail(tail))
123 return reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800124 }
125 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100126 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300127
128 return true;
129}
130
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131/*
132 * Switches to specified vcpu, until a matching vcpu_put()
133 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200134void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135{
Avi Kivity15ad7142007-07-11 18:17:21 +0300136 int cpu;
137
Avi Kivitybccf2152007-02-21 18:04:26 +0200138 mutex_lock(&vcpu->mutex);
Rik van Riel34bb10b2011-02-01 09:52:41 -0500139 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
140 /* The thread running this VCPU changed. */
141 struct pid *oldpid = vcpu->pid;
142 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
143 rcu_assign_pointer(vcpu->pid, newpid);
144 synchronize_rcu();
145 put_pid(oldpid);
146 }
Avi Kivity15ad7142007-07-11 18:17:21 +0300147 cpu = get_cpu();
148 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200149 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300150 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200151}
152
Carsten Otte313a3dc2007-10-11 19:16:52 +0200153void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154{
Avi Kivity15ad7142007-07-11 18:17:21 +0300155 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200156 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300157 preempt_notifier_unregister(&vcpu->preempt_notifier);
158 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159 mutex_unlock(&vcpu->mutex);
160}
161
Avi Kivityd9e368d2007-06-07 19:18:30 +0300162static void ack_flush(void *_completed)
163{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300164}
165
Rusty Russell49846892008-12-08 20:26:24 +1030166static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300167{
Avi Kivity597a5f52008-07-20 14:24:22 +0300168 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030169 cpumask_var_t cpus;
170 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300171 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300172
Li Zefan79f55992009-06-15 14:58:26 +0800173 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030174
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800175 me = get_cpu();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300176 kvm_for_each_vcpu(i, vcpu, kvm) {
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800177 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300178 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800179
180 /* Set ->requests bit before we read ->mode */
181 smp_mb();
182
183 if (cpus != NULL && cpu != -1 && cpu != me &&
184 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030185 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300186 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030187 if (unlikely(cpus == NULL))
188 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
189 else if (!cpumask_empty(cpus))
190 smp_call_function_many(cpus, ack_flush, NULL, 1);
191 else
192 called = false;
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800193 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030194 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030195 return called;
196}
197
198void kvm_flush_remote_tlbs(struct kvm *kvm)
199{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800200 int dirty_count = kvm->tlbs_dirty;
201
202 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030203 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
204 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800205 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300206}
207
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500208void kvm_reload_remote_mmus(struct kvm *kvm)
209{
Rusty Russell49846892008-12-08 20:26:24 +1030210 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500211}
212
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000213int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
214{
215 struct page *page;
216 int r;
217
218 mutex_init(&vcpu->mutex);
219 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000220 vcpu->kvm = kvm;
221 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500222 vcpu->pid = NULL;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300223 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200224 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225
226 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
227 if (!page) {
228 r = -ENOMEM;
229 goto fail;
230 }
231 vcpu->run = page_address(page);
232
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800233 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000234 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800235 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000236 return 0;
237
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000238fail_free_run:
239 free_page((unsigned long)vcpu->run);
240fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000241 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000242}
243EXPORT_SYMBOL_GPL(kvm_vcpu_init);
244
245void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
246{
Rik van Riel34bb10b2011-02-01 09:52:41 -0500247 put_pid(vcpu->pid);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800248 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000249 free_page((unsigned long)vcpu->run);
250}
251EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
252
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200253#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
254static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
255{
256 return container_of(mn, struct kvm, mmu_notifier);
257}
258
259static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
260 struct mm_struct *mm,
261 unsigned long address)
262{
263 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200264 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200265
266 /*
267 * When ->invalidate_page runs, the linux pte has been zapped
268 * already but the page is still allocated until
269 * ->invalidate_page returns. So if we increase the sequence
270 * here the kvm page fault will notice if the spte can't be
271 * established because the page is going to be freed. If
272 * instead the kvm page fault establishes the spte before
273 * ->invalidate_page runs, kvm_unmap_hva will release it
274 * before returning.
275 *
276 * The sequence increase only need to be seen at spin_unlock
277 * time, and not at spin_lock time.
278 *
279 * Increasing the sequence after the spin_unlock would be
280 * unsafe because the kvm page fault could then establish the
281 * pte after kvm_unmap_hva returned, without noticing the page
282 * is going to be freed.
283 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200284 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200285 spin_lock(&kvm->mmu_lock);
286 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800287 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200288 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200289 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200290
291 /* we've to flush the tlb before the pages can be freed */
292 if (need_tlb_flush)
293 kvm_flush_remote_tlbs(kvm);
294
295}
296
Izik Eidus3da0dd42009-09-23 21:47:18 +0300297static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
298 struct mm_struct *mm,
299 unsigned long address,
300 pte_t pte)
301{
302 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200303 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300304
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200305 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300306 spin_lock(&kvm->mmu_lock);
307 kvm->mmu_notifier_seq++;
308 kvm_set_spte_hva(kvm, address, pte);
309 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200310 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300311}
312
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200313static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
314 struct mm_struct *mm,
315 unsigned long start,
316 unsigned long end)
317{
318 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200319 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200320
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200321 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200322 spin_lock(&kvm->mmu_lock);
323 /*
324 * The count increase must become visible at unlock time as no
325 * spte can be established without taking the mmu_lock and
326 * count is also read inside the mmu_lock critical section.
327 */
328 kvm->mmu_notifier_count++;
329 for (; start < end; start += PAGE_SIZE)
330 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800331 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200332 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200333 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200334
335 /* we've to flush the tlb before the pages can be freed */
336 if (need_tlb_flush)
337 kvm_flush_remote_tlbs(kvm);
338}
339
340static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
341 struct mm_struct *mm,
342 unsigned long start,
343 unsigned long end)
344{
345 struct kvm *kvm = mmu_notifier_to_kvm(mn);
346
347 spin_lock(&kvm->mmu_lock);
348 /*
349 * This sequence increase will notify the kvm page fault that
350 * the page that is going to be mapped in the spte could have
351 * been freed.
352 */
353 kvm->mmu_notifier_seq++;
354 /*
355 * The above sequence increase must be visible before the
356 * below count decrease but both values are read by the kvm
357 * page fault under mmu_lock spinlock so we don't need to add
358 * a smb_wmb() here in between the two.
359 */
360 kvm->mmu_notifier_count--;
361 spin_unlock(&kvm->mmu_lock);
362
363 BUG_ON(kvm->mmu_notifier_count < 0);
364}
365
366static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
367 struct mm_struct *mm,
368 unsigned long address)
369{
370 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200371 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200372
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200373 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200374 spin_lock(&kvm->mmu_lock);
375 young = kvm_age_hva(kvm, address);
376 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200377 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200378
379 if (young)
380 kvm_flush_remote_tlbs(kvm);
381
382 return young;
383}
384
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800385static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
386 struct mm_struct *mm,
387 unsigned long address)
388{
389 struct kvm *kvm = mmu_notifier_to_kvm(mn);
390 int young, idx;
391
392 idx = srcu_read_lock(&kvm->srcu);
393 spin_lock(&kvm->mmu_lock);
394 young = kvm_test_age_hva(kvm, address);
395 spin_unlock(&kvm->mmu_lock);
396 srcu_read_unlock(&kvm->srcu, idx);
397
398 return young;
399}
400
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100401static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
402 struct mm_struct *mm)
403{
404 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800405 int idx;
406
407 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100408 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800409 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100410}
411
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200412static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
413 .invalidate_page = kvm_mmu_notifier_invalidate_page,
414 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
415 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
416 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800417 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300418 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100419 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200420};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200421
422static int kvm_init_mmu_notifier(struct kvm *kvm)
423{
424 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
425 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
426}
427
428#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
429
430static int kvm_init_mmu_notifier(struct kvm *kvm)
431{
432 return 0;
433}
434
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200435#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
436
Avi Kivityf17abe92007-02-21 19:28:04 +0200437static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100439 int r, i;
440 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100442 if (!kvm)
443 return ERR_PTR(-ENOMEM);
444
445 r = kvm_arch_init_vm(kvm);
446 if (r)
447 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200448
449 r = hardware_enable_all();
450 if (r)
451 goto out_err_nodisable;
452
Avi Kivity75858a82009-01-04 17:10:50 +0200453#ifdef CONFIG_HAVE_KVM_IRQCHIP
454 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300455 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200456#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200458 r = -ENOMEM;
459 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
460 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100461 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200462 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100463 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200464 for (i = 0; i < KVM_NR_BUSES; i++) {
465 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
466 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100467 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200468 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200469 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200470
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200471 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100472 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200473 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200474
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200475 kvm->mm = current->mm;
476 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500477 spin_lock_init(&kvm->mmu_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400478 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800479 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300480 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200481 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300482 atomic_set(&kvm->users_count, 1);
Jan Kiszkae935b832011-02-08 12:55:33 +0100483 raw_spin_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000484 list_add(&kvm->vm_list, &vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100485 raw_spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100486
Avi Kivityf17abe92007-02-21 19:28:04 +0200487 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200488
489out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100490 cleanup_srcu_struct(&kvm->srcu);
491out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200492 hardware_disable_all();
493out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200494 for (i = 0; i < KVM_NR_BUSES; i++)
495 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200496 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100497 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200498 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200499}
500
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900501static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
502{
503 if (!memslot->dirty_bitmap)
504 return;
505
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900506 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
507 vfree(memslot->dirty_bitmap_head);
508 else
509 kfree(memslot->dirty_bitmap_head);
510
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900511 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900512 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900513}
514
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515/*
516 * Free any memory in @free but not in @dont.
517 */
518static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
519 struct kvm_memory_slot *dont)
520{
Joerg Roedelec04b262009-06-19 15:16:23 +0200521 int i;
522
Izik Eidus290fc382007-09-27 14:11:22 +0200523 if (!dont || free->rmap != dont->rmap)
524 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525
526 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900527 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528
Joerg Roedelec04b262009-06-19 15:16:23 +0200529
530 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
531 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
532 vfree(free->lpage_info[i]);
533 free->lpage_info[i] = NULL;
534 }
535 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300536
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500538 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539}
540
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800541void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542{
543 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200544 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200546 for (i = 0; i < slots->nmemslots; ++i)
547 kvm_free_physmem_slot(&slots->memslots[i], NULL);
548
549 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550}
551
Avi Kivityf17abe92007-02-21 19:28:04 +0200552static void kvm_destroy_vm(struct kvm *kvm)
553{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200554 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200555 struct mm_struct *mm = kvm->mm;
556
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800557 kvm_arch_sync_events(kvm);
Jan Kiszkae935b832011-02-08 12:55:33 +0100558 raw_spin_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800559 list_del(&kvm->vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100560 raw_spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200561 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200562 for (i = 0; i < KVM_NR_BUSES; i++)
563 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200564 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200565#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
566 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200567#else
568 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200569#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800570 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100571 kvm_free_physmem(kvm);
572 cleanup_srcu_struct(&kvm->srcu);
573 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200574 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200575 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200576}
577
Izik Eidusd39f13b2008-03-30 16:01:25 +0300578void kvm_get_kvm(struct kvm *kvm)
579{
580 atomic_inc(&kvm->users_count);
581}
582EXPORT_SYMBOL_GPL(kvm_get_kvm);
583
584void kvm_put_kvm(struct kvm *kvm)
585{
586 if (atomic_dec_and_test(&kvm->users_count))
587 kvm_destroy_vm(kvm);
588}
589EXPORT_SYMBOL_GPL(kvm_put_kvm);
590
591
Avi Kivityf17abe92007-02-21 19:28:04 +0200592static int kvm_vm_release(struct inode *inode, struct file *filp)
593{
594 struct kvm *kvm = filp->private_data;
595
Gregory Haskins721eecb2009-05-20 10:30:49 -0400596 kvm_irqfd_release(kvm);
597
Izik Eidusd39f13b2008-03-30 16:01:25 +0300598 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 return 0;
600}
601
Heiko Carstensd48ead82011-01-17 21:21:08 +0100602#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900603/*
604 * Allocation size is twice as large as the actual dirty bitmap size.
605 * This makes it possible to do double buffering: see x86's
606 * kvm_vm_ioctl_get_dirty_log().
607 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900608static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
609{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900610 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900611
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900612 if (dirty_bytes > PAGE_SIZE)
613 memslot->dirty_bitmap = vzalloc(dirty_bytes);
614 else
615 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
616
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900617 if (!memslot->dirty_bitmap)
618 return -ENOMEM;
619
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900620 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900621 return 0;
622}
Heiko Carstensd48ead82011-01-17 21:21:08 +0100623#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 * Allocate some memory and give it an address in the guest physical address
627 * space.
628 *
629 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800630 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500631 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800633int __kvm_set_memory_region(struct kvm *kvm,
634 struct kvm_userspace_memory_region *mem,
635 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636{
Avi Kivity8234b222010-12-27 12:08:45 +0200637 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200639 unsigned long npages;
640 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 struct kvm_memory_slot *memslot;
642 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200643 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
645 r = -EINVAL;
646 /* General sanity checks */
647 if (mem->memory_size & (PAGE_SIZE - 1))
648 goto out;
649 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
650 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800651 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600652 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200653 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 goto out;
655 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
656 goto out;
657
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200658 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
660 npages = mem->memory_size >> PAGE_SHIFT;
661
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900662 r = -EINVAL;
663 if (npages > KVM_MEM_MAX_NR_PAGES)
664 goto out;
665
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 if (!npages)
667 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
668
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 new = old = *memslot;
670
Avi Kivitye36d96f2010-06-21 10:56:36 +0300671 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 new.base_gfn = base_gfn;
673 new.npages = npages;
674 new.flags = mem->flags;
675
676 /* Disallow changing a memory slot's size. */
677 r = -EINVAL;
678 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800679 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680
681 /* Check for overlaps */
682 r = -EEXIST;
683 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200684 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200686 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 continue;
688 if (!((base_gfn + npages <= s->base_gfn) ||
689 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800690 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 /* Free page dirty bitmap if unneeded */
694 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000695 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696
697 r = -ENOMEM;
698
699 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200700#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500701 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900702 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200703
704 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800705 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200706
Izik Eidus80b14b52007-10-25 11:54:04 +0200707 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200708 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200710 if (!npages)
711 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300712
Joerg Roedelec04b262009-06-19 15:16:23 +0200713 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200714 unsigned long ugfn;
715 unsigned long j;
716 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200717 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300718
Joerg Roedelec04b262009-06-19 15:16:23 +0200719 /* Avoid unused variable warning if no large pages */
720 (void)level;
721
722 if (new.lpage_info[i])
723 continue;
724
Joerg Roedel82855412010-07-01 16:00:11 +0200725 lpages = 1 + ((base_gfn + npages - 1)
726 >> KVM_HPAGE_GFN_SHIFT(level));
727 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200728
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900729 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200730
731 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300732 goto out_free;
733
Joerg Roedel82855412010-07-01 16:00:11 +0200734 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200735 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200736 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200737 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300738 ugfn = new.userspace_addr >> PAGE_SHIFT;
739 /*
740 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300741 * other, or if explicitly asked to, disable large page
742 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300743 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200744 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300745 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200746 for (j = 0; j < lpages; ++j)
747 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749
Joerg Roedelec04b262009-06-19 15:16:23 +0200750skip_lpage:
751
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 /* Allocate page dirty bitmap if needed */
753 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900754 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800755 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200756 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200758#else /* not defined CONFIG_S390 */
759 new.user_alloc = user_alloc;
760 if (user_alloc)
761 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200762#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200764 if (!npages) {
765 r = -ENOMEM;
766 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
767 if (!slots)
768 goto out_free;
769 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
770 if (mem->slot >= slots->nmemslots)
771 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200772 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200773 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
774
775 old_memslots = kvm->memslots;
776 rcu_assign_pointer(kvm->memslots, slots);
777 synchronize_srcu_expedited(&kvm->srcu);
778 /* From this point no new shadow pages pointing to a deleted
779 * memslot will be created.
780 *
781 * validation of sp->gfn happens in:
782 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
783 * - kvm_is_visible_gfn (mmu_check_roots)
784 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300785 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200786 kfree(old_memslots);
787 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300788
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200789 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
790 if (r)
791 goto out_free;
792
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200793 /* map the pages in iommu page table */
794 if (npages) {
795 r = kvm_iommu_map_pages(kvm, &new);
796 if (r)
797 goto out_free;
798 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200799
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200800 r = -ENOMEM;
801 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
802 if (!slots)
803 goto out_free;
804 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
805 if (mem->slot >= slots->nmemslots)
806 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200807 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200808
809 /* actual memory is freed via old in kvm_free_physmem_slot below */
810 if (!npages) {
811 new.rmap = NULL;
812 new.dirty_bitmap = NULL;
813 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
814 new.lpage_info[i] = NULL;
815 }
816
817 slots->memslots[mem->slot] = new;
818 old_memslots = kvm->memslots;
819 rcu_assign_pointer(kvm->memslots, slots);
820 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200822 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800823
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200824 kvm_free_physmem_slot(&old, &new);
825 kfree(old_memslots);
826
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 return 0;
828
Sheng Yangf78e0e22007-10-29 09:40:42 +0800829out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 kvm_free_physmem_slot(&new, &old);
831out:
832 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200833
834}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800835EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
836
837int kvm_set_memory_region(struct kvm *kvm,
838 struct kvm_userspace_memory_region *mem,
839 int user_alloc)
840{
841 int r;
842
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200843 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800844 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200845 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800846 return r;
847}
Izik Eidus210c7c42007-10-24 23:52:57 +0200848EXPORT_SYMBOL_GPL(kvm_set_memory_region);
849
Carsten Otte1fe779f2007-10-29 16:08:35 +0100850int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
851 struct
852 kvm_userspace_memory_region *mem,
853 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200854{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200855 if (mem->slot >= KVM_MEMORY_SLOTS)
856 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200857 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858}
859
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800860int kvm_get_dirty_log(struct kvm *kvm,
861 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862{
863 struct kvm_memory_slot *memslot;
864 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900865 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 unsigned long any = 0;
867
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 r = -EINVAL;
869 if (log->slot >= KVM_MEMORY_SLOTS)
870 goto out;
871
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200872 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 r = -ENOENT;
874 if (!memslot->dirty_bitmap)
875 goto out;
876
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900877 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
Uri Lublincd1a4a92007-02-22 16:43:09 +0200879 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 any = memslot->dirty_bitmap[i];
881
882 r = -EFAULT;
883 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
884 goto out;
885
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800886 if (any)
887 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888
889 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return r;
892}
893
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300894void kvm_disable_largepages(void)
895{
896 largepages_enabled = false;
897}
898EXPORT_SYMBOL_GPL(kvm_disable_largepages);
899
Izik Eiduscea7bb22007-10-17 19:17:48 +0200900int is_error_page(struct page *page)
901{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300902 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200903}
904EXPORT_SYMBOL_GPL(is_error_page);
905
Anthony Liguori35149e22008-04-02 14:46:56 -0500906int is_error_pfn(pfn_t pfn)
907{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300908 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500909}
910EXPORT_SYMBOL_GPL(is_error_pfn);
911
Huang Yingbf998152010-05-31 14:28:19 +0800912int is_hwpoison_pfn(pfn_t pfn)
913{
914 return pfn == hwpoison_pfn;
915}
916EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
917
Gleb Natapovedba23e2010-07-07 20:16:45 +0300918int is_fault_pfn(pfn_t pfn)
919{
920 return pfn == fault_pfn;
921}
922EXPORT_SYMBOL_GPL(is_fault_pfn);
923
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200924static inline unsigned long bad_hva(void)
925{
926 return PAGE_OFFSET;
927}
928
929int kvm_is_error_hva(unsigned long addr)
930{
931 return addr == bad_hva();
932}
933EXPORT_SYMBOL_GPL(kvm_is_error_hva);
934
Gleb Natapov49c77542010-10-18 15:22:23 +0200935static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
936 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937{
938 int i;
939
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200940 for (i = 0; i < slots->nmemslots; ++i) {
941 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942
943 if (gfn >= memslot->base_gfn
944 && gfn < memslot->base_gfn + memslot->npages)
945 return memslot;
946 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000947 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948}
Gleb Natapov49c77542010-10-18 15:22:23 +0200949
950struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
951{
952 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
953}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300954EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Izik Eiduse0d62c72007-10-24 23:57:46 +0200956int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
957{
958 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800959 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200960
Izik Eiduse0d62c72007-10-24 23:57:46 +0200961 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200962 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200963
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200964 if (memslot->flags & KVM_MEMSLOT_INVALID)
965 continue;
966
Izik Eiduse0d62c72007-10-24 23:57:46 +0200967 if (gfn >= memslot->base_gfn
968 && gfn < memslot->base_gfn + memslot->npages)
969 return 1;
970 }
971 return 0;
972}
973EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
974
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100975unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
976{
977 struct vm_area_struct *vma;
978 unsigned long addr, size;
979
980 size = PAGE_SIZE;
981
982 addr = gfn_to_hva(kvm, gfn);
983 if (kvm_is_error_hva(addr))
984 return PAGE_SIZE;
985
986 down_read(&current->mm->mmap_sem);
987 vma = find_vma(current->mm, addr);
988 if (!vma)
989 goto out;
990
991 size = vma_kernel_pagesize(vma);
992
993out:
994 up_read(&current->mm->mmap_sem);
995
996 return size;
997}
998
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200999int memslot_id(struct kvm *kvm, gfn_t gfn)
1000{
1001 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001002 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001003 struct kvm_memory_slot *memslot = NULL;
1004
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001005 for (i = 0; i < slots->nmemslots; ++i) {
1006 memslot = &slots->memslots[i];
1007
1008 if (gfn >= memslot->base_gfn
1009 && gfn < memslot->base_gfn + memslot->npages)
1010 break;
1011 }
1012
1013 return memslot - slots->memslots;
1014}
1015
Gleb Natapov49c77542010-10-18 15:22:23 +02001016static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001017 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001018{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001019 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001020 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001021
1022 if (nr_pages)
1023 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1024
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001025 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001026}
Xiao Guangrong48987782010-08-22 19:11:43 +08001027
1028unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1029{
Gleb Natapov49c77542010-10-18 15:22:23 +02001030 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001031}
Sheng Yang0d150292008-04-25 21:44:50 +08001032EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001033
Gleb Natapov80300892010-10-19 18:13:41 +02001034static pfn_t get_fault_pfn(void)
1035{
1036 get_page(fault_page);
1037 return fault_pfn;
1038}
1039
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 Kivity954bbbc22007-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 Kivity954bbbc22007-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
1073 npages = get_user_pages_fast(addr, 1, write_fault, page);
1074
1075 /* map read fault as writable if possible */
1076 if (unlikely(!write_fault) && npages == 1) {
1077 struct page *wpage[1];
1078
1079 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1080 if (npages == 1) {
1081 *writable = true;
1082 put_page(page[0]);
1083 page[0] = wpage[0];
1084 }
1085 npages = 1;
1086 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001087 }
Izik Eidus539cb662007-11-11 22:05:04 +02001088
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001089 if (unlikely(npages != 1)) {
1090 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001091
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001092 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001093 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001094
Huang Yingbbeb3402010-06-22 14:23:11 +08001095 down_read(&current->mm->mmap_sem);
Huang Yingfafc3db2011-01-30 11:15:49 +08001096 if (check_user_page_hwpoison(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001097 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001098 get_page(hwpoison_page);
1099 return page_to_pfn(hwpoison_page);
1100 }
1101
Gleb Natapov80300892010-10-19 18:13:41 +02001102 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001103
Gleb Natapov80300892010-10-19 18:13:41 +02001104 if (vma == NULL)
1105 pfn = get_fault_pfn();
1106 else if ((vma->vm_flags & VM_PFNMAP)) {
1107 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1108 vma->vm_pgoff;
1109 BUG_ON(!kvm_is_mmio_pfn(pfn));
1110 } else {
1111 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001112 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001113 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001114 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001115 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001116 } else
1117 pfn = page_to_pfn(page[0]);
1118
1119 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001120}
1121
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001122pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1123{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001124 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001125}
1126EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1127
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001128static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1129 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001130{
1131 unsigned long addr;
1132
Gleb Natapovaf585b92010-10-14 11:22:46 +02001133 if (async)
1134 *async = false;
1135
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001136 addr = gfn_to_hva(kvm, gfn);
1137 if (kvm_is_error_hva(addr)) {
1138 get_page(bad_page);
1139 return page_to_pfn(bad_page);
1140 }
1141
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001142 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001143}
1144
1145pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1146{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001147 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001148}
1149EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1150
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001151pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1152 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001153{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001154 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001155}
1156EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1157
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001158pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1159{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001160 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001161}
Anthony Liguori35149e22008-04-02 14:46:56 -05001162EXPORT_SYMBOL_GPL(gfn_to_pfn);
1163
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001164pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1165 bool *writable)
1166{
1167 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1168}
1169EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1170
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001171pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1172 struct kvm_memory_slot *slot, gfn_t gfn)
1173{
1174 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001175 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001176}
1177
Xiao Guangrong48987782010-08-22 19:11:43 +08001178int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1179 int nr_pages)
1180{
1181 unsigned long addr;
1182 gfn_t entry;
1183
Gleb Natapov49c77542010-10-18 15:22:23 +02001184 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001185 if (kvm_is_error_hva(addr))
1186 return -1;
1187
1188 if (entry < nr_pages)
1189 return 0;
1190
1191 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1192}
1193EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1194
Anthony Liguori35149e22008-04-02 14:46:56 -05001195struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1196{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001197 pfn_t pfn;
1198
1199 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001200 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001201 return pfn_to_page(pfn);
1202
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001203 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001204
1205 get_page(bad_page);
1206 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001207}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001208
Avi Kivity954bbbc22007-03-30 14:02:32 +03001209EXPORT_SYMBOL_GPL(gfn_to_page);
1210
Izik Eidusb4231d62007-11-20 11:49:33 +02001211void kvm_release_page_clean(struct page *page)
1212{
Anthony Liguori35149e22008-04-02 14:46:56 -05001213 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001214}
1215EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1216
Anthony Liguori35149e22008-04-02 14:46:56 -05001217void kvm_release_pfn_clean(pfn_t pfn)
1218{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001219 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001220 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001221}
1222EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1223
Izik Eidusb4231d62007-11-20 11:49:33 +02001224void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001225{
Anthony Liguori35149e22008-04-02 14:46:56 -05001226 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001227}
Izik Eidusb4231d62007-11-20 11:49:33 +02001228EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001229
Anthony Liguori35149e22008-04-02 14:46:56 -05001230void kvm_release_pfn_dirty(pfn_t pfn)
1231{
1232 kvm_set_pfn_dirty(pfn);
1233 kvm_release_pfn_clean(pfn);
1234}
1235EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1236
1237void kvm_set_page_dirty(struct page *page)
1238{
1239 kvm_set_pfn_dirty(page_to_pfn(page));
1240}
1241EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1242
1243void kvm_set_pfn_dirty(pfn_t pfn)
1244{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001245 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001246 struct page *page = pfn_to_page(pfn);
1247 if (!PageReserved(page))
1248 SetPageDirty(page);
1249 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001250}
1251EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1252
1253void kvm_set_pfn_accessed(pfn_t pfn)
1254{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001255 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001256 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001257}
1258EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1259
1260void kvm_get_pfn(pfn_t pfn)
1261{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001262 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001263 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001264}
1265EXPORT_SYMBOL_GPL(kvm_get_pfn);
1266
Izik Eidus195aefd2007-10-01 22:14:18 +02001267static int next_segment(unsigned long len, int offset)
1268{
1269 if (len > PAGE_SIZE - offset)
1270 return PAGE_SIZE - offset;
1271 else
1272 return len;
1273}
1274
1275int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1276 int len)
1277{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001278 int r;
1279 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001280
Izik Eiduse0506bc2007-11-11 22:10:22 +02001281 addr = gfn_to_hva(kvm, gfn);
1282 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001283 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001284 r = copy_from_user(data, (void __user *)addr + offset, len);
1285 if (r)
1286 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1290
1291int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1292{
1293 gfn_t gfn = gpa >> PAGE_SHIFT;
1294 int seg;
1295 int offset = offset_in_page(gpa);
1296 int ret;
1297
1298 while ((seg = next_segment(len, offset)) != 0) {
1299 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1300 if (ret < 0)
1301 return ret;
1302 offset = 0;
1303 len -= seg;
1304 data += seg;
1305 ++gfn;
1306 }
1307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(kvm_read_guest);
1310
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001311int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1312 unsigned long len)
1313{
1314 int r;
1315 unsigned long addr;
1316 gfn_t gfn = gpa >> PAGE_SHIFT;
1317 int offset = offset_in_page(gpa);
1318
1319 addr = gfn_to_hva(kvm, gfn);
1320 if (kvm_is_error_hva(addr))
1321 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001322 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001323 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001324 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001325 if (r)
1326 return -EFAULT;
1327 return 0;
1328}
1329EXPORT_SYMBOL(kvm_read_guest_atomic);
1330
Izik Eidus195aefd2007-10-01 22:14:18 +02001331int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1332 int offset, int len)
1333{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001334 int r;
1335 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001336
Izik Eiduse0506bc2007-11-11 22:10:22 +02001337 addr = gfn_to_hva(kvm, gfn);
1338 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001339 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001340 r = copy_to_user((void __user *)addr + offset, data, len);
1341 if (r)
1342 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001343 mark_page_dirty(kvm, gfn);
1344 return 0;
1345}
1346EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1347
1348int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1349 unsigned long len)
1350{
1351 gfn_t gfn = gpa >> PAGE_SHIFT;
1352 int seg;
1353 int offset = offset_in_page(gpa);
1354 int ret;
1355
1356 while ((seg = next_segment(len, offset)) != 0) {
1357 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1358 if (ret < 0)
1359 return ret;
1360 offset = 0;
1361 len -= seg;
1362 data += seg;
1363 ++gfn;
1364 }
1365 return 0;
1366}
1367
Gleb Natapov49c77542010-10-18 15:22:23 +02001368int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1369 gpa_t gpa)
1370{
1371 struct kvm_memslots *slots = kvm_memslots(kvm);
1372 int offset = offset_in_page(gpa);
1373 gfn_t gfn = gpa >> PAGE_SHIFT;
1374
1375 ghc->gpa = gpa;
1376 ghc->generation = slots->generation;
1377 ghc->memslot = __gfn_to_memslot(slots, gfn);
1378 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1379 if (!kvm_is_error_hva(ghc->hva))
1380 ghc->hva += offset;
1381 else
1382 return -EFAULT;
1383
1384 return 0;
1385}
1386EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1387
1388int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1389 void *data, unsigned long len)
1390{
1391 struct kvm_memslots *slots = kvm_memslots(kvm);
1392 int r;
1393
1394 if (slots->generation != ghc->generation)
1395 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1396
1397 if (kvm_is_error_hva(ghc->hva))
1398 return -EFAULT;
1399
1400 r = copy_to_user((void __user *)ghc->hva, data, len);
1401 if (r)
1402 return -EFAULT;
1403 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1404
1405 return 0;
1406}
1407EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1408
Izik Eidus195aefd2007-10-01 22:14:18 +02001409int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1410{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001411 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1412 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001413}
1414EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1415
1416int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1417{
1418 gfn_t gfn = gpa >> PAGE_SHIFT;
1419 int seg;
1420 int offset = offset_in_page(gpa);
1421 int ret;
1422
1423 while ((seg = next_segment(len, offset)) != 0) {
1424 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1425 if (ret < 0)
1426 return ret;
1427 offset = 0;
1428 len -= seg;
1429 ++gfn;
1430 }
1431 return 0;
1432}
1433EXPORT_SYMBOL_GPL(kvm_clear_guest);
1434
Gleb Natapov49c77542010-10-18 15:22:23 +02001435void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1436 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001438 if (memslot && memslot->dirty_bitmap) {
1439 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Akinobu Mitacd7e48c2011-03-23 16:42:04 -07001441 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 }
1443}
1444
Gleb Natapov49c77542010-10-18 15:22:23 +02001445void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1446{
1447 struct kvm_memory_slot *memslot;
1448
1449 memslot = gfn_to_memslot(kvm, gfn);
1450 mark_page_dirty_in_slot(kvm, memslot, gfn);
1451}
1452
Eddie Dongb6958ce2007-07-18 12:15:21 +03001453/*
1454 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1455 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001456void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001457{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001458 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001459
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001460 for (;;) {
1461 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001462
Gleb Natapova1b37102009-07-09 15:33:52 +03001463 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001464 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001465 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001466 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001467 if (kvm_cpu_has_pending_timer(vcpu))
1468 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001469 if (signal_pending(current))
1470 break;
1471
Eddie Dongb6958ce2007-07-18 12:15:21 +03001472 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001473 }
1474
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001475 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001476}
1477
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478void kvm_resched(struct kvm_vcpu *vcpu)
1479{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001480 if (!need_resched())
1481 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483}
1484EXPORT_SYMBOL_GPL(kvm_resched);
1485
Rik van Riel217ece62011-02-01 09:53:28 -05001486void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001487{
Rik van Riel217ece62011-02-01 09:53:28 -05001488 struct kvm *kvm = me->kvm;
1489 struct kvm_vcpu *vcpu;
1490 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1491 int yielded = 0;
1492 int pass;
1493 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001494
Rik van Riel217ece62011-02-01 09:53:28 -05001495 /*
1496 * We boost the priority of a VCPU that is runnable but not
1497 * currently running, because it got preempted by something
1498 * else and called schedule in __vcpu_run. Hopefully that
1499 * VCPU is holding the lock that we need and will release it.
1500 * We approximate round-robin by starting at the last boosted VCPU.
1501 */
1502 for (pass = 0; pass < 2 && !yielded; pass++) {
1503 kvm_for_each_vcpu(i, vcpu, kvm) {
1504 struct task_struct *task = NULL;
1505 struct pid *pid;
1506 if (!pass && i < last_boosted_vcpu) {
1507 i = last_boosted_vcpu;
1508 continue;
1509 } else if (pass && i > last_boosted_vcpu)
1510 break;
1511 if (vcpu == me)
1512 continue;
1513 if (waitqueue_active(&vcpu->wq))
1514 continue;
1515 rcu_read_lock();
1516 pid = rcu_dereference(vcpu->pid);
1517 if (pid)
1518 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1519 rcu_read_unlock();
1520 if (!task)
1521 continue;
1522 if (task->flags & PF_VCPU) {
1523 put_task_struct(task);
1524 continue;
1525 }
1526 if (yield_to(task, 1)) {
1527 put_task_struct(task);
1528 kvm->last_boosted_vcpu = i;
1529 yielded = 1;
1530 break;
1531 }
1532 put_task_struct(task);
1533 }
1534 }
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001535}
1536EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1537
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001538static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001539{
1540 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001541 struct page *page;
1542
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001543 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001544 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001545#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001546 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001547 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001548#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001549#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1550 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1551 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1552#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001553 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001554 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001555 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001556 vmf->page = page;
1557 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001558}
1559
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001560static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001561 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001562};
1563
1564static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1565{
1566 vma->vm_ops = &kvm_vcpu_vm_ops;
1567 return 0;
1568}
1569
Avi Kivitybccf2152007-02-21 18:04:26 +02001570static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1571{
1572 struct kvm_vcpu *vcpu = filp->private_data;
1573
Al Viro66c0b392008-04-19 20:33:56 +01001574 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001575 return 0;
1576}
1577
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001578static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001579 .release = kvm_vcpu_release,
1580 .unlocked_ioctl = kvm_vcpu_ioctl,
1581 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001582 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001583 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001584};
1585
1586/*
1587 * Allocates an inode for the vcpu.
1588 */
1589static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1590{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001591 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001592}
1593
Avi Kivityc5ea7662007-02-20 18:41:05 +02001594/*
1595 * Creates some virtual cpus. Good luck creating more than one.
1596 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001597static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001598{
1599 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001600 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001601
Gleb Natapov73880c82009-06-09 15:56:28 +03001602 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001603 if (IS_ERR(vcpu))
1604 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001605
Avi Kivity15ad7142007-07-11 18:17:21 +03001606 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1607
Avi Kivity26e52152007-11-20 15:30:24 +02001608 r = kvm_arch_vcpu_setup(vcpu);
1609 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001610 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001611
Shaohua Li11ec2802007-07-23 14:51:37 +08001612 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001613 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1614 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001615 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001616 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001617
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001618 kvm_for_each_vcpu(r, v, kvm)
1619 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001620 r = -EEXIST;
1621 goto vcpu_destroy;
1622 }
1623
1624 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001625
1626 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001627 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001628 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001629 if (r < 0) {
1630 kvm_put_kvm(kvm);
1631 goto vcpu_destroy;
1632 }
1633
1634 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1635 smp_wmb();
1636 atomic_inc(&kvm->online_vcpus);
1637
1638#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1639 if (kvm->bsp_vcpu_id == id)
1640 kvm->bsp_vcpu = vcpu;
1641#endif
1642 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001643 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001644
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001645vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001646 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001647 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001648 return r;
1649}
1650
Avi Kivity1961d272007-03-05 19:46:05 +02001651static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1652{
1653 if (sigset) {
1654 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1655 vcpu->sigset_active = 1;
1656 vcpu->sigset = *sigset;
1657 } else
1658 vcpu->sigset_active = 0;
1659 return 0;
1660}
1661
Avi Kivitybccf2152007-02-21 18:04:26 +02001662static long kvm_vcpu_ioctl(struct file *filp,
1663 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664{
Avi Kivitybccf2152007-02-21 18:04:26 +02001665 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001666 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001667 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001668 struct kvm_fpu *fpu = NULL;
1669 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001671 if (vcpu->kvm->mm != current->mm)
1672 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001673
1674#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1675 /*
1676 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1677 * so vcpu_load() would break it.
1678 */
1679 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1680 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1681#endif
1682
1683
1684 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001686 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001687 r = -EINVAL;
1688 if (arg)
1689 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001690 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001691 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001694 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001696 r = -ENOMEM;
1697 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1698 if (!kvm_regs)
1699 goto out;
1700 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001702 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001704 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1705 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001707out_free1:
1708 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 break;
1710 }
1711 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001712 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001714 r = -ENOMEM;
1715 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1716 if (!kvm_regs)
1717 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001719 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1720 goto out_free2;
1721 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001723 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001725out_free2:
1726 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727 break;
1728 }
1729 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001730 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1731 r = -ENOMEM;
1732 if (!kvm_sregs)
1733 goto out;
1734 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 if (r)
1736 goto out;
1737 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001738 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 goto out;
1740 r = 0;
1741 break;
1742 }
1743 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001744 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1745 r = -ENOMEM;
1746 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001748 r = -EFAULT;
1749 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1750 goto out;
1751 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752 if (r)
1753 goto out;
1754 r = 0;
1755 break;
1756 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001757 case KVM_GET_MP_STATE: {
1758 struct kvm_mp_state mp_state;
1759
1760 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1761 if (r)
1762 goto out;
1763 r = -EFAULT;
1764 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1765 goto out;
1766 r = 0;
1767 break;
1768 }
1769 case KVM_SET_MP_STATE: {
1770 struct kvm_mp_state mp_state;
1771
1772 r = -EFAULT;
1773 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1774 goto out;
1775 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1776 if (r)
1777 goto out;
1778 r = 0;
1779 break;
1780 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781 case KVM_TRANSLATE: {
1782 struct kvm_translation tr;
1783
1784 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001785 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001787 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 if (r)
1789 goto out;
1790 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001791 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792 goto out;
1793 r = 0;
1794 break;
1795 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001796 case KVM_SET_GUEST_DEBUG: {
1797 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798
1799 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001800 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001802 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 if (r)
1804 goto out;
1805 r = 0;
1806 break;
1807 }
Avi Kivity1961d272007-03-05 19:46:05 +02001808 case KVM_SET_SIGNAL_MASK: {
1809 struct kvm_signal_mask __user *sigmask_arg = argp;
1810 struct kvm_signal_mask kvm_sigmask;
1811 sigset_t sigset, *p;
1812
1813 p = NULL;
1814 if (argp) {
1815 r = -EFAULT;
1816 if (copy_from_user(&kvm_sigmask, argp,
1817 sizeof kvm_sigmask))
1818 goto out;
1819 r = -EINVAL;
1820 if (kvm_sigmask.len != sizeof sigset)
1821 goto out;
1822 r = -EFAULT;
1823 if (copy_from_user(&sigset, sigmask_arg->sigset,
1824 sizeof sigset))
1825 goto out;
1826 p = &sigset;
1827 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001828 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001829 break;
1830 }
Avi Kivityb8836732007-04-01 16:34:31 +03001831 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001832 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1833 r = -ENOMEM;
1834 if (!fpu)
1835 goto out;
1836 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001837 if (r)
1838 goto out;
1839 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001840 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001841 goto out;
1842 r = 0;
1843 break;
1844 }
1845 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001846 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1847 r = -ENOMEM;
1848 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001849 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001850 r = -EFAULT;
1851 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1852 goto out;
1853 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001854 if (r)
1855 goto out;
1856 r = 0;
1857 break;
1858 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001859 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001860 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001861 }
1862out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001863 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001864 kfree(fpu);
1865 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001866 return r;
1867}
1868
1869static long kvm_vm_ioctl(struct file *filp,
1870 unsigned int ioctl, unsigned long arg)
1871{
1872 struct kvm *kvm = filp->private_data;
1873 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001874 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001875
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001876 if (kvm->mm != current->mm)
1877 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001878 switch (ioctl) {
1879 case KVM_CREATE_VCPU:
1880 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1881 if (r < 0)
1882 goto out;
1883 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001884 case KVM_SET_USER_MEMORY_REGION: {
1885 struct kvm_userspace_memory_region kvm_userspace_mem;
1886
1887 r = -EFAULT;
1888 if (copy_from_user(&kvm_userspace_mem, argp,
1889 sizeof kvm_userspace_mem))
1890 goto out;
1891
1892 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 if (r)
1894 goto out;
1895 break;
1896 }
1897 case KVM_GET_DIRTY_LOG: {
1898 struct kvm_dirty_log log;
1899
1900 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001901 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001903 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904 if (r)
1905 goto out;
1906 break;
1907 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001908#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1909 case KVM_REGISTER_COALESCED_MMIO: {
1910 struct kvm_coalesced_mmio_zone zone;
1911 r = -EFAULT;
1912 if (copy_from_user(&zone, argp, sizeof zone))
1913 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001914 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1915 if (r)
1916 goto out;
1917 r = 0;
1918 break;
1919 }
1920 case KVM_UNREGISTER_COALESCED_MMIO: {
1921 struct kvm_coalesced_mmio_zone zone;
1922 r = -EFAULT;
1923 if (copy_from_user(&zone, argp, sizeof zone))
1924 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001925 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1926 if (r)
1927 goto out;
1928 r = 0;
1929 break;
1930 }
1931#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001932 case KVM_IRQFD: {
1933 struct kvm_irqfd data;
1934
1935 r = -EFAULT;
1936 if (copy_from_user(&data, argp, sizeof data))
1937 goto out;
1938 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1939 break;
1940 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001941 case KVM_IOEVENTFD: {
1942 struct kvm_ioeventfd data;
1943
1944 r = -EFAULT;
1945 if (copy_from_user(&data, argp, sizeof data))
1946 goto out;
1947 r = kvm_ioeventfd(kvm, &data);
1948 break;
1949 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001950#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1951 case KVM_SET_BOOT_CPU_ID:
1952 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001953 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001954 if (atomic_read(&kvm->online_vcpus) != 0)
1955 r = -EBUSY;
1956 else
1957 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001958 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001959 break;
1960#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001961 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001962 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001963 if (r == -ENOTTY)
1964 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001965 }
1966out:
1967 return r;
1968}
1969
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001970#ifdef CONFIG_COMPAT
1971struct compat_kvm_dirty_log {
1972 __u32 slot;
1973 __u32 padding1;
1974 union {
1975 compat_uptr_t dirty_bitmap; /* one bit per page */
1976 __u64 padding2;
1977 };
1978};
1979
1980static long kvm_vm_compat_ioctl(struct file *filp,
1981 unsigned int ioctl, unsigned long arg)
1982{
1983 struct kvm *kvm = filp->private_data;
1984 int r;
1985
1986 if (kvm->mm != current->mm)
1987 return -EIO;
1988 switch (ioctl) {
1989 case KVM_GET_DIRTY_LOG: {
1990 struct compat_kvm_dirty_log compat_log;
1991 struct kvm_dirty_log log;
1992
1993 r = -EFAULT;
1994 if (copy_from_user(&compat_log, (void __user *)arg,
1995 sizeof(compat_log)))
1996 goto out;
1997 log.slot = compat_log.slot;
1998 log.padding1 = compat_log.padding1;
1999 log.padding2 = compat_log.padding2;
2000 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2001
2002 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2003 if (r)
2004 goto out;
2005 break;
2006 }
2007 default:
2008 r = kvm_vm_ioctl(filp, ioctl, arg);
2009 }
2010
2011out:
2012 return r;
2013}
2014#endif
2015
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002016static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002017{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002018 struct page *page[1];
2019 unsigned long addr;
2020 int npages;
2021 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002022 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002023
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002024 addr = gfn_to_hva(kvm, gfn);
2025 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002026 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002027
2028 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2029 NULL);
2030 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002031 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002032
2033 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002034 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002035}
2036
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002037static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002038 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002039};
2040
2041static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2042{
2043 vma->vm_ops = &kvm_vm_vm_ops;
2044 return 0;
2045}
2046
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002047static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002048 .release = kvm_vm_release,
2049 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002050#ifdef CONFIG_COMPAT
2051 .compat_ioctl = kvm_vm_compat_ioctl,
2052#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002053 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002054 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002055};
2056
2057static int kvm_dev_ioctl_create_vm(void)
2058{
Heiko Carstensaac87632010-10-27 17:22:10 +02002059 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002060 struct kvm *kvm;
2061
Avi Kivityf17abe92007-02-21 19:28:04 +02002062 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002063 if (IS_ERR(kvm))
2064 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002065#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2066 r = kvm_coalesced_mmio_init(kvm);
2067 if (r < 0) {
2068 kvm_put_kvm(kvm);
2069 return r;
2070 }
2071#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002072 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2073 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002074 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002075
Heiko Carstensaac87632010-10-27 17:22:10 +02002076 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002077}
2078
Avi Kivity1a811b62008-12-08 18:25:27 +02002079static long kvm_dev_ioctl_check_extension_generic(long arg)
2080{
2081 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002082 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002083 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002084 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002085#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2086 case KVM_CAP_SET_BOOT_CPU_ID:
2087#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002088 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002089 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002090#ifdef CONFIG_HAVE_KVM_IRQCHIP
2091 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002092 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002093#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002094 default:
2095 break;
2096 }
2097 return kvm_dev_ioctl_check_extension(arg);
2098}
2099
Avi Kivityf17abe92007-02-21 19:28:04 +02002100static long kvm_dev_ioctl(struct file *filp,
2101 unsigned int ioctl, unsigned long arg)
2102{
Avi Kivity07c45a32007-03-07 13:05:38 +02002103 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002104
2105 switch (ioctl) {
2106 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002107 r = -EINVAL;
2108 if (arg)
2109 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002110 r = KVM_API_VERSION;
2111 break;
2112 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002113 r = -EINVAL;
2114 if (arg)
2115 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002116 r = kvm_dev_ioctl_create_vm();
2117 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002118 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002119 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002120 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002121 case KVM_GET_VCPU_MMAP_SIZE:
2122 r = -EINVAL;
2123 if (arg)
2124 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002125 r = PAGE_SIZE; /* struct kvm_run */
2126#ifdef CONFIG_X86
2127 r += PAGE_SIZE; /* pio data page */
2128#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002129#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2130 r += PAGE_SIZE; /* coalesced mmio ring page */
2131#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002132 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002133 case KVM_TRACE_ENABLE:
2134 case KVM_TRACE_PAUSE:
2135 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002136 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002137 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002139 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 }
2141out:
2142 return r;
2143}
2144
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 .unlocked_ioctl = kvm_dev_ioctl,
2147 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002148 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149};
2150
2151static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002152 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 "kvm",
2154 &kvm_chardev_ops,
2155};
2156
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002157static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002158{
2159 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002160 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002161
Rusty Russell7f59f492008-12-07 21:25:45 +10302162 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002163 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002164
Rusty Russell7f59f492008-12-07 21:25:45 +10302165 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002166
2167 r = kvm_arch_hardware_enable(NULL);
2168
2169 if (r) {
2170 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2171 atomic_inc(&hardware_enable_failed);
2172 printk(KERN_INFO "kvm: enabling virtualization on "
2173 "CPU%d failed\n", cpu);
2174 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002175}
2176
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002177static void hardware_enable(void *junk)
2178{
Jan Kiszkae935b832011-02-08 12:55:33 +01002179 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002180 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002181 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002182}
2183
2184static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002185{
2186 int cpu = raw_smp_processor_id();
2187
Rusty Russell7f59f492008-12-07 21:25:45 +10302188 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002189 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302190 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002191 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002192}
2193
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002194static void hardware_disable(void *junk)
2195{
Jan Kiszkae935b832011-02-08 12:55:33 +01002196 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002197 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002198 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002199}
2200
Alexander Graf10474ae2009-09-15 11:37:46 +02002201static void hardware_disable_all_nolock(void)
2202{
2203 BUG_ON(!kvm_usage_count);
2204
2205 kvm_usage_count--;
2206 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002207 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002208}
2209
2210static void hardware_disable_all(void)
2211{
Jan Kiszkae935b832011-02-08 12:55:33 +01002212 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002213 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002214 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002215}
2216
2217static int hardware_enable_all(void)
2218{
2219 int r = 0;
2220
Jan Kiszkae935b832011-02-08 12:55:33 +01002221 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002222
2223 kvm_usage_count++;
2224 if (kvm_usage_count == 1) {
2225 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002226 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002227
2228 if (atomic_read(&hardware_enable_failed)) {
2229 hardware_disable_all_nolock();
2230 r = -EBUSY;
2231 }
2232 }
2233
Jan Kiszkae935b832011-02-08 12:55:33 +01002234 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002235
2236 return r;
2237}
2238
Avi Kivity774c47f2007-02-12 00:54:47 -08002239static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2240 void *v)
2241{
2242 int cpu = (long)v;
2243
Alexander Graf10474ae2009-09-15 11:37:46 +02002244 if (!kvm_usage_count)
2245 return NOTIFY_OK;
2246
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002247 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002248 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002249 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002250 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2251 cpu);
2252 hardware_disable(NULL);
2253 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002254 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002255 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2256 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002257 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002258 break;
2259 }
2260 return NOTIFY_OK;
2261}
2262
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002263
Avi Kivityb7c41452010-12-02 17:52:50 +02002264asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002265{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002266 /* Fault while not rebooting. We want the trace. */
2267 BUG();
2268}
Avi Kivityb7c41452010-12-02 17:52:50 +02002269EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002270
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002271static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002272 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002273{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002274 /*
2275 * Some (well, at least mine) BIOSes hang on reboot if
2276 * in vmx root mode.
2277 *
2278 * And Intel TXT required VMX off for all cpu when system shutdown.
2279 */
2280 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2281 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002282 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002283 return NOTIFY_OK;
2284}
2285
2286static struct notifier_block kvm_reboot_notifier = {
2287 .notifier_call = kvm_reboot,
2288 .priority = 0,
2289};
2290
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002291static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002292{
2293 int i;
2294
2295 for (i = 0; i < bus->dev_count; i++) {
2296 struct kvm_io_device *pos = bus->devs[i];
2297
2298 kvm_iodevice_destructor(pos);
2299 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002300 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002301}
2302
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002303/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002304int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002305 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002306{
2307 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002308 struct kvm_io_bus *bus;
2309
2310 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002311 for (i = 0; i < bus->dev_count; i++)
2312 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2313 return 0;
2314 return -EOPNOTSUPP;
2315}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002316
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002317/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002318int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2319 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002320{
2321 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002322 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002323
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002324 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002325 for (i = 0; i < bus->dev_count; i++)
2326 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2327 return 0;
2328 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002329}
2330
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002331/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002332int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2333 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002334{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002335 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002336
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002337 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002338 if (bus->dev_count > NR_IOBUS_DEVS-1)
2339 return -ENOSPC;
2340
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002341 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2342 if (!new_bus)
2343 return -ENOMEM;
2344 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2345 new_bus->devs[new_bus->dev_count++] = dev;
2346 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2347 synchronize_srcu_expedited(&kvm->srcu);
2348 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002349
2350 return 0;
2351}
2352
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002353/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002354int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2355 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002356{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002357 int i, r;
2358 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002359
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002360 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2361 if (!new_bus)
2362 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002363
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002364 bus = kvm->buses[bus_idx];
2365 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2366
2367 r = -ENOENT;
2368 for (i = 0; i < new_bus->dev_count; i++)
2369 if (new_bus->devs[i] == dev) {
2370 r = 0;
2371 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002372 break;
2373 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002374
2375 if (r) {
2376 kfree(new_bus);
2377 return r;
2378 }
2379
2380 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2381 synchronize_srcu_expedited(&kvm->srcu);
2382 kfree(bus);
2383 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002384}
2385
Avi Kivity774c47f2007-02-12 00:54:47 -08002386static struct notifier_block kvm_cpu_notifier = {
2387 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002388};
2389
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002390static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002391{
2392 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002393 struct kvm *kvm;
2394
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002395 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002396 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002397 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002398 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002399 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002400 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002401}
2402
2403DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2404
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002405static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002406{
2407 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002408 struct kvm *kvm;
2409 struct kvm_vcpu *vcpu;
2410 int i;
2411
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002412 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002413 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002414 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002415 kvm_for_each_vcpu(i, vcpu, kvm)
2416 *val += *(u32 *)((void *)vcpu + offset);
2417
Jan Kiszkae935b832011-02-08 12:55:33 +01002418 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002419 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002420}
2421
Avi Kivityba1389b2007-11-18 16:24:12 +02002422DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2423
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002424static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002425 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2426 [KVM_STAT_VM] = &vm_stat_fops,
2427};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002428
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002429static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430{
2431 struct kvm_stats_debugfs_item *p;
2432
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002433 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002435 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002436 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002437 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438}
2439
2440static void kvm_exit_debug(void)
2441{
2442 struct kvm_stats_debugfs_item *p;
2443
2444 for (p = debugfs_entries; p->name; ++p)
2445 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002446 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447}
2448
Avi Kivity59ae6c62007-02-12 00:54:48 -08002449static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2450{
Alexander Graf10474ae2009-09-15 11:37:46 +02002451 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002452 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002453 return 0;
2454}
2455
2456static int kvm_resume(struct sys_device *dev)
2457{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002458 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002459 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002460 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002461 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002462 return 0;
2463}
2464
2465static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002466 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002467 .suspend = kvm_suspend,
2468 .resume = kvm_resume,
2469};
2470
2471static struct sys_device kvm_sysdev = {
2472 .id = 0,
2473 .cls = &kvm_sysdev_class,
2474};
2475
Izik Eiduscea7bb22007-10-17 19:17:48 +02002476struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002477pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478
Avi Kivity15ad7142007-07-11 18:17:21 +03002479static inline
2480struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2481{
2482 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2483}
2484
2485static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2486{
2487 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2488
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002489 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002490}
2491
2492static void kvm_sched_out(struct preempt_notifier *pn,
2493 struct task_struct *next)
2494{
2495 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2496
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002497 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002498}
2499
Avi Kivity0ee75be2010-04-28 15:39:01 +03002500int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002501 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502{
2503 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002504 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002506 r = kvm_arch_init(opaque);
2507 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002508 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002509
2510 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2511
2512 if (bad_page == NULL) {
2513 r = -ENOMEM;
2514 goto out;
2515 }
2516
Anthony Liguori35149e22008-04-02 14:46:56 -05002517 bad_pfn = page_to_pfn(bad_page);
2518
Huang Yingbf998152010-05-31 14:28:19 +08002519 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2520
2521 if (hwpoison_page == NULL) {
2522 r = -ENOMEM;
2523 goto out_free_0;
2524 }
2525
2526 hwpoison_pfn = page_to_pfn(hwpoison_page);
2527
Gleb Natapovedba23e2010-07-07 20:16:45 +03002528 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2529
2530 if (fault_page == NULL) {
2531 r = -ENOMEM;
2532 goto out_free_0;
2533 }
2534
2535 fault_pfn = page_to_pfn(fault_page);
2536
Avi Kivity8437a612009-06-06 14:52:35 -07002537 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302538 r = -ENOMEM;
2539 goto out_free_0;
2540 }
2541
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002542 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302544 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545
Yang, Sheng002c7f72007-07-31 14:23:01 +03002546 for_each_online_cpu(cpu) {
2547 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002548 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002549 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002550 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002551 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002552 }
2553
Avi Kivity774c47f2007-02-12 00:54:47 -08002554 r = register_cpu_notifier(&kvm_cpu_notifier);
2555 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002556 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 register_reboot_notifier(&kvm_reboot_notifier);
2558
Avi Kivity59ae6c62007-02-12 00:54:48 -08002559 r = sysdev_class_register(&kvm_sysdev_class);
2560 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002561 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002562
2563 r = sysdev_register(&kvm_sysdev);
2564 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002565 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002566
Rusty Russellc16f8622007-07-30 21:12:19 +10002567 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002568 if (!vcpu_align)
2569 vcpu_align = __alignof__(struct kvm_vcpu);
2570 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002571 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002572 if (!kvm_vcpu_cache) {
2573 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002574 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002575 }
2576
Gleb Natapovaf585b92010-10-14 11:22:46 +02002577 r = kvm_async_pf_init();
2578 if (r)
2579 goto out_free;
2580
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002582 kvm_vm_fops.owner = module;
2583 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584
2585 r = misc_register(&kvm_dev);
2586 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002587 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002588 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 }
2590
Avi Kivity15ad7142007-07-11 18:17:21 +03002591 kvm_preempt_ops.sched_in = kvm_sched_in;
2592 kvm_preempt_ops.sched_out = kvm_sched_out;
2593
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002594 kvm_init_debug();
2595
Avi Kivityc7addb92007-09-16 18:58:32 +02002596 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597
Gleb Natapovaf585b92010-10-14 11:22:46 +02002598out_unreg:
2599 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002601 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002602out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002603 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002604out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002605 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002606out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002608 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002609out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002610out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002611 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302612out_free_0a:
2613 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002614out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002615 if (fault_page)
2616 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002617 if (hwpoison_page)
2618 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002619 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002620out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002621 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002622out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 return r;
2624}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002625EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002627void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002629 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002631 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002632 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002633 sysdev_unregister(&kvm_sysdev);
2634 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002636 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002637 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002638 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002639 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302640 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002641 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002642 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002644EXPORT_SYMBOL_GPL(kvm_exit);