blob: bc8bfd15ab716413de25e5d5dac18f673dc98441 [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>
Alexander Grafc8240bd2009-10-30 05:47:26 +000055#include <asm-generic/bitops/le.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Laurent Vivier5f94c172008-05-30 16:05:54 +020057#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020058#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020059
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060#define CREATE_TRACE_POINTS
61#include <trace/events/kvm.h>
62
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Marcelo Tosattifa40a822009-06-04 15:08:24 -030066/*
67 * Ordering of locks:
68 *
Sheng Yangfae3a352009-12-15 10:28:07 +080069 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030070 */
71
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072DEFINE_SPINLOCK(kvm_lock);
73LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080074
Rusty Russell7f59f492008-12-07 21:25:45 +103075static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020076static int kvm_usage_count = 0;
77static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030078
Rusty Russellc16f8622007-07-30 21:12:19 +100079struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030081
Avi Kivity15ad7142007-07-11 18:17:21 +030082static __read_mostly struct preempt_ops kvm_preempt_ops;
83
Hollis Blanchard76f7c872008-04-15 16:05:42 -050084struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivitybccf2152007-02-21 18:04:26 +020086static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020088static int hardware_enable_all(void);
89static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020090
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020091static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
92
Avi Kivityb7c41452010-12-02 17:52:50 +020093bool kvm_rebooting;
94EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +030095
Marcelo Tosatti54dee992009-06-11 12:07:44 -030096static bool largepages_enabled = true;
97
Gleb Natapovfa7bff82010-07-07 20:16:44 +030098static struct page *hwpoison_page;
99static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +0800100
Gleb Natapovedba23e2010-07-07 20:16:45 +0300101static struct page *fault_page;
102static pfn_t fault_pfn;
103
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800104inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300105{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100106 if (pfn_valid(pfn)) {
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800107 int reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800108 struct page *tail = pfn_to_page(pfn);
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800109 struct page *head = compound_trans_head(tail);
110 reserved = PageReserved(head);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800111 if (head != tail) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800112 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800113 * "head" is not a dangling pointer
114 * (compound_trans_head takes care of that)
115 * but the hugepage may have been splitted
116 * from under us (and we may not hold a
117 * reference count on the head page so it can
118 * be reused before we run PageReferenced), so
119 * we've to check PageTail before returning
120 * what we just read.
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800121 */
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800122 smp_rmb();
123 if (PageTail(tail))
124 return reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800125 }
126 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100127 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300128
129 return true;
130}
131
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132/*
133 * Switches to specified vcpu, until a matching vcpu_put()
134 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200135void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136{
Avi Kivity15ad7142007-07-11 18:17:21 +0300137 int cpu;
138
Avi Kivitybccf2152007-02-21 18:04:26 +0200139 mutex_lock(&vcpu->mutex);
Rik van Riel34bb10b2011-02-01 09:52:41 -0500140 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
141 /* The thread running this VCPU changed. */
142 struct pid *oldpid = vcpu->pid;
143 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
144 rcu_assign_pointer(vcpu->pid, newpid);
145 synchronize_rcu();
146 put_pid(oldpid);
147 }
Avi Kivity15ad7142007-07-11 18:17:21 +0300148 cpu = get_cpu();
149 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200150 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300151 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200152}
153
Carsten Otte313a3dc2007-10-11 19:16:52 +0200154void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155{
Avi Kivity15ad7142007-07-11 18:17:21 +0300156 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200157 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300158 preempt_notifier_unregister(&vcpu->preempt_notifier);
159 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160 mutex_unlock(&vcpu->mutex);
161}
162
Avi Kivityd9e368d2007-06-07 19:18:30 +0300163static void ack_flush(void *_completed)
164{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300165}
166
Rusty Russell49846892008-12-08 20:26:24 +1030167static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300168{
Avi Kivity597a5f52008-07-20 14:24:22 +0300169 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030170 cpumask_var_t cpus;
171 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300172 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300173
Li Zefan79f55992009-06-15 14:58:26 +0800174 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030175
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800176 me = get_cpu();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300177 kvm_for_each_vcpu(i, vcpu, kvm) {
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800178 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300179 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800180
181 /* Set ->requests bit before we read ->mode */
182 smp_mb();
183
184 if (cpus != NULL && cpu != -1 && cpu != me &&
185 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030186 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300187 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030188 if (unlikely(cpus == NULL))
189 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
190 else if (!cpumask_empty(cpus))
191 smp_call_function_many(cpus, ack_flush, NULL, 1);
192 else
193 called = false;
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800194 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030195 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030196 return called;
197}
198
199void kvm_flush_remote_tlbs(struct kvm *kvm)
200{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800201 int dirty_count = kvm->tlbs_dirty;
202
203 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030204 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
205 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800206 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300207}
208
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500209void kvm_reload_remote_mmus(struct kvm *kvm)
210{
Rusty Russell49846892008-12-08 20:26:24 +1030211 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500212}
213
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000214int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
215{
216 struct page *page;
217 int r;
218
219 mutex_init(&vcpu->mutex);
220 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000221 vcpu->kvm = kvm;
222 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500223 vcpu->pid = NULL;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300224 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200225 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000226
227 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
228 if (!page) {
229 r = -ENOMEM;
230 goto fail;
231 }
232 vcpu->run = page_address(page);
233
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800234 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000235 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800236 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000237 return 0;
238
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000239fail_free_run:
240 free_page((unsigned long)vcpu->run);
241fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000242 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000243}
244EXPORT_SYMBOL_GPL(kvm_vcpu_init);
245
246void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
247{
Rik van Riel34bb10b2011-02-01 09:52:41 -0500248 put_pid(vcpu->pid);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800249 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000250 free_page((unsigned long)vcpu->run);
251}
252EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
253
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200254#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
255static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
256{
257 return container_of(mn, struct kvm, mmu_notifier);
258}
259
260static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
261 struct mm_struct *mm,
262 unsigned long address)
263{
264 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200265 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200266
267 /*
268 * When ->invalidate_page runs, the linux pte has been zapped
269 * already but the page is still allocated until
270 * ->invalidate_page returns. So if we increase the sequence
271 * here the kvm page fault will notice if the spte can't be
272 * established because the page is going to be freed. If
273 * instead the kvm page fault establishes the spte before
274 * ->invalidate_page runs, kvm_unmap_hva will release it
275 * before returning.
276 *
277 * The sequence increase only need to be seen at spin_unlock
278 * time, and not at spin_lock time.
279 *
280 * Increasing the sequence after the spin_unlock would be
281 * unsafe because the kvm page fault could then establish the
282 * pte after kvm_unmap_hva returned, without noticing the page
283 * is going to be freed.
284 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200285 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200286 spin_lock(&kvm->mmu_lock);
287 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800288 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200289 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200290 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200291
292 /* we've to flush the tlb before the pages can be freed */
293 if (need_tlb_flush)
294 kvm_flush_remote_tlbs(kvm);
295
296}
297
Izik Eidus3da0dd42009-09-23 21:47:18 +0300298static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
299 struct mm_struct *mm,
300 unsigned long address,
301 pte_t pte)
302{
303 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200304 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300305
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200306 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300307 spin_lock(&kvm->mmu_lock);
308 kvm->mmu_notifier_seq++;
309 kvm_set_spte_hva(kvm, address, pte);
310 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200311 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300312}
313
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200314static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
315 struct mm_struct *mm,
316 unsigned long start,
317 unsigned long end)
318{
319 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200320 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200321
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200322 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200323 spin_lock(&kvm->mmu_lock);
324 /*
325 * The count increase must become visible at unlock time as no
326 * spte can be established without taking the mmu_lock and
327 * count is also read inside the mmu_lock critical section.
328 */
329 kvm->mmu_notifier_count++;
330 for (; start < end; start += PAGE_SIZE)
331 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800332 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200333 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200334 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200335
336 /* we've to flush the tlb before the pages can be freed */
337 if (need_tlb_flush)
338 kvm_flush_remote_tlbs(kvm);
339}
340
341static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
342 struct mm_struct *mm,
343 unsigned long start,
344 unsigned long end)
345{
346 struct kvm *kvm = mmu_notifier_to_kvm(mn);
347
348 spin_lock(&kvm->mmu_lock);
349 /*
350 * This sequence increase will notify the kvm page fault that
351 * the page that is going to be mapped in the spte could have
352 * been freed.
353 */
354 kvm->mmu_notifier_seq++;
355 /*
356 * The above sequence increase must be visible before the
357 * below count decrease but both values are read by the kvm
358 * page fault under mmu_lock spinlock so we don't need to add
359 * a smb_wmb() here in between the two.
360 */
361 kvm->mmu_notifier_count--;
362 spin_unlock(&kvm->mmu_lock);
363
364 BUG_ON(kvm->mmu_notifier_count < 0);
365}
366
367static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
368 struct mm_struct *mm,
369 unsigned long address)
370{
371 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200372 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200373
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200374 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200375 spin_lock(&kvm->mmu_lock);
376 young = kvm_age_hva(kvm, address);
377 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200378 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200379
380 if (young)
381 kvm_flush_remote_tlbs(kvm);
382
383 return young;
384}
385
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800386static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
387 struct mm_struct *mm,
388 unsigned long address)
389{
390 struct kvm *kvm = mmu_notifier_to_kvm(mn);
391 int young, idx;
392
393 idx = srcu_read_lock(&kvm->srcu);
394 spin_lock(&kvm->mmu_lock);
395 young = kvm_test_age_hva(kvm, address);
396 spin_unlock(&kvm->mmu_lock);
397 srcu_read_unlock(&kvm->srcu, idx);
398
399 return young;
400}
401
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100402static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
403 struct mm_struct *mm)
404{
405 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800406 int idx;
407
408 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100409 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800410 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100411}
412
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200413static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
414 .invalidate_page = kvm_mmu_notifier_invalidate_page,
415 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
416 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
417 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800418 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300419 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100420 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200421};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200422
423static int kvm_init_mmu_notifier(struct kvm *kvm)
424{
425 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
426 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
427}
428
429#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
430
431static int kvm_init_mmu_notifier(struct kvm *kvm)
432{
433 return 0;
434}
435
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200436#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
437
Avi Kivityf17abe92007-02-21 19:28:04 +0200438static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100440 int r, i;
441 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100443 if (!kvm)
444 return ERR_PTR(-ENOMEM);
445
446 r = kvm_arch_init_vm(kvm);
447 if (r)
448 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200449
450 r = hardware_enable_all();
451 if (r)
452 goto out_err_nodisable;
453
Avi Kivity75858a82009-01-04 17:10:50 +0200454#ifdef CONFIG_HAVE_KVM_IRQCHIP
455 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300456 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200457#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200459 r = -ENOMEM;
460 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
461 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100462 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200463 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100464 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200465 for (i = 0; i < KVM_NR_BUSES; i++) {
466 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
467 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100468 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200469 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200470 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200471
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200472 r = kvm_init_mmu_notifier(kvm);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100473 if (r)
Avi Kivity283d0c62009-12-20 14:25:19 +0200474 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200475
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200476 kvm->mm = current->mm;
477 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500478 spin_lock_init(&kvm->mmu_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400479 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800480 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300481 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200482 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300483 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000484 spin_lock(&kvm_lock);
485 list_add(&kvm->vm_list, &vm_list);
486 spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100487
Avi Kivityf17abe92007-02-21 19:28:04 +0200488 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200489
490out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100491 cleanup_srcu_struct(&kvm->srcu);
492out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200493 hardware_disable_all();
494out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200495 for (i = 0; i < KVM_NR_BUSES; i++)
496 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200497 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100498 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200499 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200500}
501
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900502static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
503{
504 if (!memslot->dirty_bitmap)
505 return;
506
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900507 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
508 vfree(memslot->dirty_bitmap_head);
509 else
510 kfree(memslot->dirty_bitmap_head);
511
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900512 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900513 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900514}
515
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516/*
517 * Free any memory in @free but not in @dont.
518 */
519static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
520 struct kvm_memory_slot *dont)
521{
Joerg Roedelec04b262009-06-19 15:16:23 +0200522 int i;
523
Izik Eidus290fc382007-09-27 14:11:22 +0200524 if (!dont || free->rmap != dont->rmap)
525 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526
527 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900528 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529
Joerg Roedelec04b262009-06-19 15:16:23 +0200530
531 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
532 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
533 vfree(free->lpage_info[i]);
534 free->lpage_info[i] = NULL;
535 }
536 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300537
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500539 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540}
541
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800542void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543{
544 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200545 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200547 for (i = 0; i < slots->nmemslots; ++i)
548 kvm_free_physmem_slot(&slots->memslots[i], NULL);
549
550 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551}
552
Avi Kivityf17abe92007-02-21 19:28:04 +0200553static void kvm_destroy_vm(struct kvm *kvm)
554{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200555 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200556 struct mm_struct *mm = kvm->mm;
557
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800558 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800559 spin_lock(&kvm_lock);
560 list_del(&kvm->vm_list);
561 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200562 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200563 for (i = 0; i < KVM_NR_BUSES; i++)
564 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200565 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200566#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
567 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200568#else
569 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200570#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800571 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100572 kvm_free_physmem(kvm);
573 cleanup_srcu_struct(&kvm->srcu);
574 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200575 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200576 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200577}
578
Izik Eidusd39f13b2008-03-30 16:01:25 +0300579void kvm_get_kvm(struct kvm *kvm)
580{
581 atomic_inc(&kvm->users_count);
582}
583EXPORT_SYMBOL_GPL(kvm_get_kvm);
584
585void kvm_put_kvm(struct kvm *kvm)
586{
587 if (atomic_dec_and_test(&kvm->users_count))
588 kvm_destroy_vm(kvm);
589}
590EXPORT_SYMBOL_GPL(kvm_put_kvm);
591
592
Avi Kivityf17abe92007-02-21 19:28:04 +0200593static int kvm_vm_release(struct inode *inode, struct file *filp)
594{
595 struct kvm *kvm = filp->private_data;
596
Gregory Haskins721eecb2009-05-20 10:30:49 -0400597 kvm_irqfd_release(kvm);
598
Izik Eidusd39f13b2008-03-30 16:01:25 +0300599 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 return 0;
601}
602
Heiko Carstensd48ead82011-01-17 21:21:08 +0100603#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900604/*
605 * Allocation size is twice as large as the actual dirty bitmap size.
606 * This makes it possible to do double buffering: see x86's
607 * kvm_vm_ioctl_get_dirty_log().
608 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900609static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
610{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900611 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900612
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900613 if (dirty_bytes > PAGE_SIZE)
614 memslot->dirty_bitmap = vzalloc(dirty_bytes);
615 else
616 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
617
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900618 if (!memslot->dirty_bitmap)
619 return -ENOMEM;
620
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900621 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900622 return 0;
623}
Heiko Carstensd48ead82011-01-17 21:21:08 +0100624#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900625
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 * Allocate some memory and give it an address in the guest physical address
628 * space.
629 *
630 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800631 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500632 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800634int __kvm_set_memory_region(struct kvm *kvm,
635 struct kvm_userspace_memory_region *mem,
636 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637{
Avi Kivity8234b222010-12-27 12:08:45 +0200638 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200640 unsigned long npages;
641 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642 struct kvm_memory_slot *memslot;
643 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200644 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
646 r = -EINVAL;
647 /* General sanity checks */
648 if (mem->memory_size & (PAGE_SIZE - 1))
649 goto out;
650 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
651 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800652 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600653 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200654 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 goto out;
656 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
657 goto out;
658
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200659 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
661 npages = mem->memory_size >> PAGE_SHIFT;
662
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900663 r = -EINVAL;
664 if (npages > KVM_MEM_MAX_NR_PAGES)
665 goto out;
666
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 if (!npages)
668 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
669
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670 new = old = *memslot;
671
Avi Kivitye36d96f2010-06-21 10:56:36 +0300672 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 new.base_gfn = base_gfn;
674 new.npages = npages;
675 new.flags = mem->flags;
676
677 /* Disallow changing a memory slot's size. */
678 r = -EINVAL;
679 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800680 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681
682 /* Check for overlaps */
683 r = -EEXIST;
684 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200685 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200687 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 continue;
689 if (!((base_gfn + npages <= s->base_gfn) ||
690 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800691 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 /* Free page dirty bitmap if unneeded */
695 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000696 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697
698 r = -ENOMEM;
699
700 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200701#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500702 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900703 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200704
705 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800706 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200707
Izik Eidus80b14b52007-10-25 11:54:04 +0200708 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200709 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200711 if (!npages)
712 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300713
Joerg Roedelec04b262009-06-19 15:16:23 +0200714 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200715 unsigned long ugfn;
716 unsigned long j;
717 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200718 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300719
Joerg Roedelec04b262009-06-19 15:16:23 +0200720 /* Avoid unused variable warning if no large pages */
721 (void)level;
722
723 if (new.lpage_info[i])
724 continue;
725
Joerg Roedel82855412010-07-01 16:00:11 +0200726 lpages = 1 + ((base_gfn + npages - 1)
727 >> KVM_HPAGE_GFN_SHIFT(level));
728 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200729
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900730 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200731
732 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300733 goto out_free;
734
Joerg Roedel82855412010-07-01 16:00:11 +0200735 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200736 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200737 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200738 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300739 ugfn = new.userspace_addr >> PAGE_SHIFT;
740 /*
741 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300742 * other, or if explicitly asked to, disable large page
743 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300744 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200745 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300746 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200747 for (j = 0; j < lpages; ++j)
748 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750
Joerg Roedelec04b262009-06-19 15:16:23 +0200751skip_lpage:
752
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 /* Allocate page dirty bitmap if needed */
754 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900755 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800756 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200757 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200759#else /* not defined CONFIG_S390 */
760 new.user_alloc = user_alloc;
761 if (user_alloc)
762 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200763#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200765 if (!npages) {
766 r = -ENOMEM;
767 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
768 if (!slots)
769 goto out_free;
770 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
771 if (mem->slot >= slots->nmemslots)
772 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200773 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200774 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
775
776 old_memslots = kvm->memslots;
777 rcu_assign_pointer(kvm->memslots, slots);
778 synchronize_srcu_expedited(&kvm->srcu);
779 /* From this point no new shadow pages pointing to a deleted
780 * memslot will be created.
781 *
782 * validation of sp->gfn happens in:
783 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
784 * - kvm_is_visible_gfn (mmu_check_roots)
785 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300786 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200787 kfree(old_memslots);
788 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300789
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200790 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
791 if (r)
792 goto out_free;
793
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200794 /* map the pages in iommu page table */
795 if (npages) {
796 r = kvm_iommu_map_pages(kvm, &new);
797 if (r)
798 goto out_free;
799 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200800
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200801 r = -ENOMEM;
802 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
803 if (!slots)
804 goto out_free;
805 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
806 if (mem->slot >= slots->nmemslots)
807 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200808 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200809
810 /* actual memory is freed via old in kvm_free_physmem_slot below */
811 if (!npages) {
812 new.rmap = NULL;
813 new.dirty_bitmap = NULL;
814 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
815 new.lpage_info[i] = NULL;
816 }
817
818 slots->memslots[mem->slot] = new;
819 old_memslots = kvm->memslots;
820 rcu_assign_pointer(kvm->memslots, slots);
821 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200823 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800824
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200825 kvm_free_physmem_slot(&old, &new);
826 kfree(old_memslots);
827
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 return 0;
829
Sheng Yangf78e0e22007-10-29 09:40:42 +0800830out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831 kvm_free_physmem_slot(&new, &old);
832out:
833 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200834
835}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800836EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
837
838int kvm_set_memory_region(struct kvm *kvm,
839 struct kvm_userspace_memory_region *mem,
840 int user_alloc)
841{
842 int r;
843
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200844 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800845 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200846 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800847 return r;
848}
Izik Eidus210c7c42007-10-24 23:52:57 +0200849EXPORT_SYMBOL_GPL(kvm_set_memory_region);
850
Carsten Otte1fe779f2007-10-29 16:08:35 +0100851int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
852 struct
853 kvm_userspace_memory_region *mem,
854 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200855{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200856 if (mem->slot >= KVM_MEMORY_SLOTS)
857 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200858 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859}
860
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800861int kvm_get_dirty_log(struct kvm *kvm,
862 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863{
864 struct kvm_memory_slot *memslot;
865 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900866 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 unsigned long any = 0;
868
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 r = -EINVAL;
870 if (log->slot >= KVM_MEMORY_SLOTS)
871 goto out;
872
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200873 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 r = -ENOENT;
875 if (!memslot->dirty_bitmap)
876 goto out;
877
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900878 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879
Uri Lublincd1a4a92007-02-22 16:43:09 +0200880 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 any = memslot->dirty_bitmap[i];
882
883 r = -EFAULT;
884 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
885 goto out;
886
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800887 if (any)
888 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889
890 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 return r;
893}
894
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300895void kvm_disable_largepages(void)
896{
897 largepages_enabled = false;
898}
899EXPORT_SYMBOL_GPL(kvm_disable_largepages);
900
Izik Eiduscea7bb22007-10-17 19:17:48 +0200901int is_error_page(struct page *page)
902{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300903 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200904}
905EXPORT_SYMBOL_GPL(is_error_page);
906
Anthony Liguori35149e22008-04-02 14:46:56 -0500907int is_error_pfn(pfn_t pfn)
908{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300909 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500910}
911EXPORT_SYMBOL_GPL(is_error_pfn);
912
Huang Yingbf998152010-05-31 14:28:19 +0800913int is_hwpoison_pfn(pfn_t pfn)
914{
915 return pfn == hwpoison_pfn;
916}
917EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
918
Gleb Natapovedba23e2010-07-07 20:16:45 +0300919int is_fault_pfn(pfn_t pfn)
920{
921 return pfn == fault_pfn;
922}
923EXPORT_SYMBOL_GPL(is_fault_pfn);
924
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200925static inline unsigned long bad_hva(void)
926{
927 return PAGE_OFFSET;
928}
929
930int kvm_is_error_hva(unsigned long addr)
931{
932 return addr == bad_hva();
933}
934EXPORT_SYMBOL_GPL(kvm_is_error_hva);
935
Gleb Natapov49c77542010-10-18 15:22:23 +0200936static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
937 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938{
939 int i;
940
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200941 for (i = 0; i < slots->nmemslots; ++i) {
942 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
944 if (gfn >= memslot->base_gfn
945 && gfn < memslot->base_gfn + memslot->npages)
946 return memslot;
947 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000948 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949}
Gleb Natapov49c77542010-10-18 15:22:23 +0200950
951struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
952{
953 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
954}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300955EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956
Izik Eiduse0d62c72007-10-24 23:57:46 +0200957int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
958{
959 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800960 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200961
Izik Eiduse0d62c72007-10-24 23:57:46 +0200962 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200963 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200964
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200965 if (memslot->flags & KVM_MEMSLOT_INVALID)
966 continue;
967
Izik Eiduse0d62c72007-10-24 23:57:46 +0200968 if (gfn >= memslot->base_gfn
969 && gfn < memslot->base_gfn + memslot->npages)
970 return 1;
971 }
972 return 0;
973}
974EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
975
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100976unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
977{
978 struct vm_area_struct *vma;
979 unsigned long addr, size;
980
981 size = PAGE_SIZE;
982
983 addr = gfn_to_hva(kvm, gfn);
984 if (kvm_is_error_hva(addr))
985 return PAGE_SIZE;
986
987 down_read(&current->mm->mmap_sem);
988 vma = find_vma(current->mm, addr);
989 if (!vma)
990 goto out;
991
992 size = vma_kernel_pagesize(vma);
993
994out:
995 up_read(&current->mm->mmap_sem);
996
997 return size;
998}
999
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001000int memslot_id(struct kvm *kvm, gfn_t gfn)
1001{
1002 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001003 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001004 struct kvm_memory_slot *memslot = NULL;
1005
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001006 for (i = 0; i < slots->nmemslots; ++i) {
1007 memslot = &slots->memslots[i];
1008
1009 if (gfn >= memslot->base_gfn
1010 && gfn < memslot->base_gfn + memslot->npages)
1011 break;
1012 }
1013
1014 return memslot - slots->memslots;
1015}
1016
Gleb Natapov49c77542010-10-18 15:22:23 +02001017static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001018 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001019{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001020 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001021 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001022
1023 if (nr_pages)
1024 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1025
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001026 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001027}
Xiao Guangrong48987782010-08-22 19:11:43 +08001028
1029unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1030{
Gleb Natapov49c77542010-10-18 15:22:23 +02001031 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001032}
Sheng Yang0d150292008-04-25 21:44:50 +08001033EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001034
Gleb Natapov80300892010-10-19 18:13:41 +02001035static pfn_t get_fault_pfn(void)
1036{
1037 get_page(fault_page);
1038 return fault_pfn;
1039}
1040
Huang Yingfafc3db2011-01-30 11:15:49 +08001041static inline int check_user_page_hwpoison(unsigned long addr)
1042{
1043 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1044
1045 rc = __get_user_pages(current, current->mm, addr, 1,
1046 flags, NULL, NULL, NULL);
1047 return rc == -EHWPOISON;
1048}
1049
Gleb Natapovaf585b92010-10-14 11:22:46 +02001050static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001051 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001052{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001053 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001054 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001055 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001056
Gleb Natapovaf585b92010-10-14 11:22:46 +02001057 /* we can do it either atomically or asynchronously, not both */
1058 BUG_ON(atomic && async);
1059
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001060 BUG_ON(!write_fault && !writable);
1061
1062 if (writable)
1063 *writable = true;
1064
Gleb Natapovaf585b92010-10-14 11:22:46 +02001065 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001066 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001067
1068 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001069 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001070
1071 if (writable)
1072 *writable = write_fault;
1073
1074 npages = get_user_pages_fast(addr, 1, write_fault, page);
1075
1076 /* map read fault as writable if possible */
1077 if (unlikely(!write_fault) && npages == 1) {
1078 struct page *wpage[1];
1079
1080 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1081 if (npages == 1) {
1082 *writable = true;
1083 put_page(page[0]);
1084 page[0] = wpage[0];
1085 }
1086 npages = 1;
1087 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001088 }
Izik Eidus539cb662007-11-11 22:05:04 +02001089
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001090 if (unlikely(npages != 1)) {
1091 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001092
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001093 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001094 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001095
Huang Yingbbeb3402010-06-22 14:23:11 +08001096 down_read(&current->mm->mmap_sem);
Huang Yingfafc3db2011-01-30 11:15:49 +08001097 if (check_user_page_hwpoison(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001098 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001099 get_page(hwpoison_page);
1100 return page_to_pfn(hwpoison_page);
1101 }
1102
Gleb Natapov80300892010-10-19 18:13:41 +02001103 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001104
Gleb Natapov80300892010-10-19 18:13:41 +02001105 if (vma == NULL)
1106 pfn = get_fault_pfn();
1107 else if ((vma->vm_flags & VM_PFNMAP)) {
1108 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1109 vma->vm_pgoff;
1110 BUG_ON(!kvm_is_mmio_pfn(pfn));
1111 } else {
1112 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001113 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001114 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001115 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001116 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001117 } else
1118 pfn = page_to_pfn(page[0]);
1119
1120 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001121}
1122
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001123pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1124{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001125 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001126}
1127EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1128
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001129static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1130 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001131{
1132 unsigned long addr;
1133
Gleb Natapovaf585b92010-10-14 11:22:46 +02001134 if (async)
1135 *async = false;
1136
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001137 addr = gfn_to_hva(kvm, gfn);
1138 if (kvm_is_error_hva(addr)) {
1139 get_page(bad_page);
1140 return page_to_pfn(bad_page);
1141 }
1142
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001143 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001144}
1145
1146pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1147{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001148 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001149}
1150EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1151
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001152pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1153 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001154{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001155 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001156}
1157EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1158
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001159pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1160{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001161 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001162}
Anthony Liguori35149e22008-04-02 14:46:56 -05001163EXPORT_SYMBOL_GPL(gfn_to_pfn);
1164
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001165pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1166 bool *writable)
1167{
1168 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1169}
1170EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1171
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001172pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1173 struct kvm_memory_slot *slot, gfn_t gfn)
1174{
1175 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001176 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001177}
1178
Xiao Guangrong48987782010-08-22 19:11:43 +08001179int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1180 int nr_pages)
1181{
1182 unsigned long addr;
1183 gfn_t entry;
1184
Gleb Natapov49c77542010-10-18 15:22:23 +02001185 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001186 if (kvm_is_error_hva(addr))
1187 return -1;
1188
1189 if (entry < nr_pages)
1190 return 0;
1191
1192 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1193}
1194EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1195
Anthony Liguori35149e22008-04-02 14:46:56 -05001196struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1197{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001198 pfn_t pfn;
1199
1200 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001201 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001202 return pfn_to_page(pfn);
1203
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001204 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001205
1206 get_page(bad_page);
1207 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001208}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001209
Avi Kivity954bbbc22007-03-30 14:02:32 +03001210EXPORT_SYMBOL_GPL(gfn_to_page);
1211
Izik Eidusb4231d62007-11-20 11:49:33 +02001212void kvm_release_page_clean(struct page *page)
1213{
Anthony Liguori35149e22008-04-02 14:46:56 -05001214 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001215}
1216EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1217
Anthony Liguori35149e22008-04-02 14:46:56 -05001218void kvm_release_pfn_clean(pfn_t pfn)
1219{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001220 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001221 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001222}
1223EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1224
Izik Eidusb4231d62007-11-20 11:49:33 +02001225void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001226{
Anthony Liguori35149e22008-04-02 14:46:56 -05001227 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001228}
Izik Eidusb4231d62007-11-20 11:49:33 +02001229EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001230
Anthony Liguori35149e22008-04-02 14:46:56 -05001231void kvm_release_pfn_dirty(pfn_t pfn)
1232{
1233 kvm_set_pfn_dirty(pfn);
1234 kvm_release_pfn_clean(pfn);
1235}
1236EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1237
1238void kvm_set_page_dirty(struct page *page)
1239{
1240 kvm_set_pfn_dirty(page_to_pfn(page));
1241}
1242EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1243
1244void kvm_set_pfn_dirty(pfn_t pfn)
1245{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001246 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001247 struct page *page = pfn_to_page(pfn);
1248 if (!PageReserved(page))
1249 SetPageDirty(page);
1250 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001251}
1252EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1253
1254void kvm_set_pfn_accessed(pfn_t pfn)
1255{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001256 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001257 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001258}
1259EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1260
1261void kvm_get_pfn(pfn_t pfn)
1262{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001263 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001264 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001265}
1266EXPORT_SYMBOL_GPL(kvm_get_pfn);
1267
Izik Eidus195aefd2007-10-01 22:14:18 +02001268static int next_segment(unsigned long len, int offset)
1269{
1270 if (len > PAGE_SIZE - offset)
1271 return PAGE_SIZE - offset;
1272 else
1273 return len;
1274}
1275
1276int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1277 int len)
1278{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001279 int r;
1280 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001281
Izik Eiduse0506bc2007-11-11 22:10:22 +02001282 addr = gfn_to_hva(kvm, gfn);
1283 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001284 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001285 r = copy_from_user(data, (void __user *)addr + offset, len);
1286 if (r)
1287 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001288 return 0;
1289}
1290EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1291
1292int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1293{
1294 gfn_t gfn = gpa >> PAGE_SHIFT;
1295 int seg;
1296 int offset = offset_in_page(gpa);
1297 int ret;
1298
1299 while ((seg = next_segment(len, offset)) != 0) {
1300 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1301 if (ret < 0)
1302 return ret;
1303 offset = 0;
1304 len -= seg;
1305 data += seg;
1306 ++gfn;
1307 }
1308 return 0;
1309}
1310EXPORT_SYMBOL_GPL(kvm_read_guest);
1311
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001312int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1313 unsigned long len)
1314{
1315 int r;
1316 unsigned long addr;
1317 gfn_t gfn = gpa >> PAGE_SHIFT;
1318 int offset = offset_in_page(gpa);
1319
1320 addr = gfn_to_hva(kvm, gfn);
1321 if (kvm_is_error_hva(addr))
1322 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001323 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001324 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001325 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001326 if (r)
1327 return -EFAULT;
1328 return 0;
1329}
1330EXPORT_SYMBOL(kvm_read_guest_atomic);
1331
Izik Eidus195aefd2007-10-01 22:14:18 +02001332int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1333 int offset, int len)
1334{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001335 int r;
1336 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001337
Izik Eiduse0506bc2007-11-11 22:10:22 +02001338 addr = gfn_to_hva(kvm, gfn);
1339 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001340 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001341 r = copy_to_user((void __user *)addr + offset, data, len);
1342 if (r)
1343 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001344 mark_page_dirty(kvm, gfn);
1345 return 0;
1346}
1347EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1348
1349int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1350 unsigned long len)
1351{
1352 gfn_t gfn = gpa >> PAGE_SHIFT;
1353 int seg;
1354 int offset = offset_in_page(gpa);
1355 int ret;
1356
1357 while ((seg = next_segment(len, offset)) != 0) {
1358 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1359 if (ret < 0)
1360 return ret;
1361 offset = 0;
1362 len -= seg;
1363 data += seg;
1364 ++gfn;
1365 }
1366 return 0;
1367}
1368
Gleb Natapov49c77542010-10-18 15:22:23 +02001369int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1370 gpa_t gpa)
1371{
1372 struct kvm_memslots *slots = kvm_memslots(kvm);
1373 int offset = offset_in_page(gpa);
1374 gfn_t gfn = gpa >> PAGE_SHIFT;
1375
1376 ghc->gpa = gpa;
1377 ghc->generation = slots->generation;
1378 ghc->memslot = __gfn_to_memslot(slots, gfn);
1379 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1380 if (!kvm_is_error_hva(ghc->hva))
1381 ghc->hva += offset;
1382 else
1383 return -EFAULT;
1384
1385 return 0;
1386}
1387EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1388
1389int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1390 void *data, unsigned long len)
1391{
1392 struct kvm_memslots *slots = kvm_memslots(kvm);
1393 int r;
1394
1395 if (slots->generation != ghc->generation)
1396 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1397
1398 if (kvm_is_error_hva(ghc->hva))
1399 return -EFAULT;
1400
1401 r = copy_to_user((void __user *)ghc->hva, data, len);
1402 if (r)
1403 return -EFAULT;
1404 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1405
1406 return 0;
1407}
1408EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1409
Izik Eidus195aefd2007-10-01 22:14:18 +02001410int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1411{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001412 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1413 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001414}
1415EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1416
1417int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1418{
1419 gfn_t gfn = gpa >> PAGE_SHIFT;
1420 int seg;
1421 int offset = offset_in_page(gpa);
1422 int ret;
1423
1424 while ((seg = next_segment(len, offset)) != 0) {
1425 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1426 if (ret < 0)
1427 return ret;
1428 offset = 0;
1429 len -= seg;
1430 ++gfn;
1431 }
1432 return 0;
1433}
1434EXPORT_SYMBOL_GPL(kvm_clear_guest);
1435
Gleb Natapov49c77542010-10-18 15:22:23 +02001436void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1437 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001439 if (memslot && memslot->dirty_bitmap) {
1440 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001442 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443 }
1444}
1445
Gleb Natapov49c77542010-10-18 15:22:23 +02001446void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1447{
1448 struct kvm_memory_slot *memslot;
1449
1450 memslot = gfn_to_memslot(kvm, gfn);
1451 mark_page_dirty_in_slot(kvm, memslot, gfn);
1452}
1453
Eddie Dongb6958ce2007-07-18 12:15:21 +03001454/*
1455 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1456 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001457void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001458{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001459 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001460
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001461 for (;;) {
1462 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001463
Gleb Natapova1b37102009-07-09 15:33:52 +03001464 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001465 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001466 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001467 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001468 if (kvm_cpu_has_pending_timer(vcpu))
1469 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001470 if (signal_pending(current))
1471 break;
1472
Eddie Dongb6958ce2007-07-18 12:15:21 +03001473 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001474 }
1475
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001476 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001477}
1478
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479void kvm_resched(struct kvm_vcpu *vcpu)
1480{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001481 if (!need_resched())
1482 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484}
1485EXPORT_SYMBOL_GPL(kvm_resched);
1486
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001487void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1488{
1489 ktime_t expires;
1490 DEFINE_WAIT(wait);
1491
1492 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1493
1494 /* Sleep for 100 us, and hope lock-holder got scheduled */
1495 expires = ktime_add_ns(ktime_get(), 100000UL);
1496 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1497
1498 finish_wait(&vcpu->wq, &wait);
1499}
1500EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1501
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001502static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001503{
1504 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001505 struct page *page;
1506
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001507 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001508 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001509#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001510 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001511 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001512#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001513#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1514 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1515 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1516#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001517 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001518 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001519 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001520 vmf->page = page;
1521 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001522}
1523
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001524static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001525 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001526};
1527
1528static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1529{
1530 vma->vm_ops = &kvm_vcpu_vm_ops;
1531 return 0;
1532}
1533
Avi Kivitybccf2152007-02-21 18:04:26 +02001534static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1535{
1536 struct kvm_vcpu *vcpu = filp->private_data;
1537
Al Viro66c0b392008-04-19 20:33:56 +01001538 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001539 return 0;
1540}
1541
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001542static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001543 .release = kvm_vcpu_release,
1544 .unlocked_ioctl = kvm_vcpu_ioctl,
1545 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001546 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001547 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001548};
1549
1550/*
1551 * Allocates an inode for the vcpu.
1552 */
1553static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1554{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001555 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001556}
1557
Avi Kivityc5ea7662007-02-20 18:41:05 +02001558/*
1559 * Creates some virtual cpus. Good luck creating more than one.
1560 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001561static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001562{
1563 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001564 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001565
Gleb Natapov73880c82009-06-09 15:56:28 +03001566 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001567 if (IS_ERR(vcpu))
1568 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001569
Avi Kivity15ad7142007-07-11 18:17:21 +03001570 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1571
Avi Kivity26e52152007-11-20 15:30:24 +02001572 r = kvm_arch_vcpu_setup(vcpu);
1573 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001574 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001575
Shaohua Li11ec2802007-07-23 14:51:37 +08001576 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001577 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1578 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001579 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001580 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001581
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001582 kvm_for_each_vcpu(r, v, kvm)
1583 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001584 r = -EEXIST;
1585 goto vcpu_destroy;
1586 }
1587
1588 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001589
1590 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001591 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001592 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001593 if (r < 0) {
1594 kvm_put_kvm(kvm);
1595 goto vcpu_destroy;
1596 }
1597
1598 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1599 smp_wmb();
1600 atomic_inc(&kvm->online_vcpus);
1601
1602#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1603 if (kvm->bsp_vcpu_id == id)
1604 kvm->bsp_vcpu = vcpu;
1605#endif
1606 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001607 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001608
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001609vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001610 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001611 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001612 return r;
1613}
1614
Avi Kivity1961d272007-03-05 19:46:05 +02001615static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1616{
1617 if (sigset) {
1618 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1619 vcpu->sigset_active = 1;
1620 vcpu->sigset = *sigset;
1621 } else
1622 vcpu->sigset_active = 0;
1623 return 0;
1624}
1625
Avi Kivitybccf2152007-02-21 18:04:26 +02001626static long kvm_vcpu_ioctl(struct file *filp,
1627 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628{
Avi Kivitybccf2152007-02-21 18:04:26 +02001629 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001630 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001631 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001632 struct kvm_fpu *fpu = NULL;
1633 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001635 if (vcpu->kvm->mm != current->mm)
1636 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001637
1638#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1639 /*
1640 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1641 * so vcpu_load() would break it.
1642 */
1643 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1644 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1645#endif
1646
1647
1648 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001650 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001651 r = -EINVAL;
1652 if (arg)
1653 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001654 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001655 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001658 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001660 r = -ENOMEM;
1661 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1662 if (!kvm_regs)
1663 goto out;
1664 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001666 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001668 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1669 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001671out_free1:
1672 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 break;
1674 }
1675 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001676 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001678 r = -ENOMEM;
1679 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1680 if (!kvm_regs)
1681 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001683 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1684 goto out_free2;
1685 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001687 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001689out_free2:
1690 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 break;
1692 }
1693 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001694 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1695 r = -ENOMEM;
1696 if (!kvm_sregs)
1697 goto out;
1698 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 if (r)
1700 goto out;
1701 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001702 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 goto out;
1704 r = 0;
1705 break;
1706 }
1707 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001708 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1709 r = -ENOMEM;
1710 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001712 r = -EFAULT;
1713 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1714 goto out;
1715 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716 if (r)
1717 goto out;
1718 r = 0;
1719 break;
1720 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001721 case KVM_GET_MP_STATE: {
1722 struct kvm_mp_state mp_state;
1723
1724 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1725 if (r)
1726 goto out;
1727 r = -EFAULT;
1728 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1729 goto out;
1730 r = 0;
1731 break;
1732 }
1733 case KVM_SET_MP_STATE: {
1734 struct kvm_mp_state mp_state;
1735
1736 r = -EFAULT;
1737 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1738 goto out;
1739 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1740 if (r)
1741 goto out;
1742 r = 0;
1743 break;
1744 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745 case KVM_TRANSLATE: {
1746 struct kvm_translation tr;
1747
1748 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001749 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001751 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752 if (r)
1753 goto out;
1754 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001755 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756 goto out;
1757 r = 0;
1758 break;
1759 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001760 case KVM_SET_GUEST_DEBUG: {
1761 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762
1763 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001764 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001766 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 if (r)
1768 goto out;
1769 r = 0;
1770 break;
1771 }
Avi Kivity1961d272007-03-05 19:46:05 +02001772 case KVM_SET_SIGNAL_MASK: {
1773 struct kvm_signal_mask __user *sigmask_arg = argp;
1774 struct kvm_signal_mask kvm_sigmask;
1775 sigset_t sigset, *p;
1776
1777 p = NULL;
1778 if (argp) {
1779 r = -EFAULT;
1780 if (copy_from_user(&kvm_sigmask, argp,
1781 sizeof kvm_sigmask))
1782 goto out;
1783 r = -EINVAL;
1784 if (kvm_sigmask.len != sizeof sigset)
1785 goto out;
1786 r = -EFAULT;
1787 if (copy_from_user(&sigset, sigmask_arg->sigset,
1788 sizeof sigset))
1789 goto out;
1790 p = &sigset;
1791 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001792 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001793 break;
1794 }
Avi Kivityb8836732007-04-01 16:34:31 +03001795 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001796 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1797 r = -ENOMEM;
1798 if (!fpu)
1799 goto out;
1800 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001801 if (r)
1802 goto out;
1803 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001804 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001805 goto out;
1806 r = 0;
1807 break;
1808 }
1809 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001810 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1811 r = -ENOMEM;
1812 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001813 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001814 r = -EFAULT;
1815 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1816 goto out;
1817 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001818 if (r)
1819 goto out;
1820 r = 0;
1821 break;
1822 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001823 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001824 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001825 }
1826out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001827 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001828 kfree(fpu);
1829 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001830 return r;
1831}
1832
1833static long kvm_vm_ioctl(struct file *filp,
1834 unsigned int ioctl, unsigned long arg)
1835{
1836 struct kvm *kvm = filp->private_data;
1837 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001838 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001839
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001840 if (kvm->mm != current->mm)
1841 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001842 switch (ioctl) {
1843 case KVM_CREATE_VCPU:
1844 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1845 if (r < 0)
1846 goto out;
1847 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001848 case KVM_SET_USER_MEMORY_REGION: {
1849 struct kvm_userspace_memory_region kvm_userspace_mem;
1850
1851 r = -EFAULT;
1852 if (copy_from_user(&kvm_userspace_mem, argp,
1853 sizeof kvm_userspace_mem))
1854 goto out;
1855
1856 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 if (r)
1858 goto out;
1859 break;
1860 }
1861 case KVM_GET_DIRTY_LOG: {
1862 struct kvm_dirty_log log;
1863
1864 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001865 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001867 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 if (r)
1869 goto out;
1870 break;
1871 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001872#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1873 case KVM_REGISTER_COALESCED_MMIO: {
1874 struct kvm_coalesced_mmio_zone zone;
1875 r = -EFAULT;
1876 if (copy_from_user(&zone, argp, sizeof zone))
1877 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001878 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1879 if (r)
1880 goto out;
1881 r = 0;
1882 break;
1883 }
1884 case KVM_UNREGISTER_COALESCED_MMIO: {
1885 struct kvm_coalesced_mmio_zone zone;
1886 r = -EFAULT;
1887 if (copy_from_user(&zone, argp, sizeof zone))
1888 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001889 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1890 if (r)
1891 goto out;
1892 r = 0;
1893 break;
1894 }
1895#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04001896 case KVM_IRQFD: {
1897 struct kvm_irqfd data;
1898
1899 r = -EFAULT;
1900 if (copy_from_user(&data, argp, sizeof data))
1901 goto out;
1902 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1903 break;
1904 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001905 case KVM_IOEVENTFD: {
1906 struct kvm_ioeventfd data;
1907
1908 r = -EFAULT;
1909 if (copy_from_user(&data, argp, sizeof data))
1910 goto out;
1911 r = kvm_ioeventfd(kvm, &data);
1912 break;
1913 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001914#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1915 case KVM_SET_BOOT_CPU_ID:
1916 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001917 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001918 if (atomic_read(&kvm->online_vcpus) != 0)
1919 r = -EBUSY;
1920 else
1921 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001922 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001923 break;
1924#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001925 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001926 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001927 if (r == -ENOTTY)
1928 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001929 }
1930out:
1931 return r;
1932}
1933
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001934#ifdef CONFIG_COMPAT
1935struct compat_kvm_dirty_log {
1936 __u32 slot;
1937 __u32 padding1;
1938 union {
1939 compat_uptr_t dirty_bitmap; /* one bit per page */
1940 __u64 padding2;
1941 };
1942};
1943
1944static long kvm_vm_compat_ioctl(struct file *filp,
1945 unsigned int ioctl, unsigned long arg)
1946{
1947 struct kvm *kvm = filp->private_data;
1948 int r;
1949
1950 if (kvm->mm != current->mm)
1951 return -EIO;
1952 switch (ioctl) {
1953 case KVM_GET_DIRTY_LOG: {
1954 struct compat_kvm_dirty_log compat_log;
1955 struct kvm_dirty_log log;
1956
1957 r = -EFAULT;
1958 if (copy_from_user(&compat_log, (void __user *)arg,
1959 sizeof(compat_log)))
1960 goto out;
1961 log.slot = compat_log.slot;
1962 log.padding1 = compat_log.padding1;
1963 log.padding2 = compat_log.padding2;
1964 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1965
1966 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1967 if (r)
1968 goto out;
1969 break;
1970 }
1971 default:
1972 r = kvm_vm_ioctl(filp, ioctl, arg);
1973 }
1974
1975out:
1976 return r;
1977}
1978#endif
1979
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001980static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001981{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001982 struct page *page[1];
1983 unsigned long addr;
1984 int npages;
1985 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001986 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001987
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001988 addr = gfn_to_hva(kvm, gfn);
1989 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001990 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001991
1992 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1993 NULL);
1994 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001995 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001996
1997 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001998 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001999}
2000
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002001static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002002 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002003};
2004
2005static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2006{
2007 vma->vm_ops = &kvm_vm_vm_ops;
2008 return 0;
2009}
2010
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002011static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002012 .release = kvm_vm_release,
2013 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002014#ifdef CONFIG_COMPAT
2015 .compat_ioctl = kvm_vm_compat_ioctl,
2016#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002017 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002018 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002019};
2020
2021static int kvm_dev_ioctl_create_vm(void)
2022{
Heiko Carstensaac87632010-10-27 17:22:10 +02002023 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002024 struct kvm *kvm;
2025
Avi Kivityf17abe92007-02-21 19:28:04 +02002026 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002027 if (IS_ERR(kvm))
2028 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002029#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2030 r = kvm_coalesced_mmio_init(kvm);
2031 if (r < 0) {
2032 kvm_put_kvm(kvm);
2033 return r;
2034 }
2035#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002036 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2037 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002038 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002039
Heiko Carstensaac87632010-10-27 17:22:10 +02002040 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002041}
2042
Avi Kivity1a811b62008-12-08 18:25:27 +02002043static long kvm_dev_ioctl_check_extension_generic(long arg)
2044{
2045 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002046 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002047 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002048 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002049#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2050 case KVM_CAP_SET_BOOT_CPU_ID:
2051#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002052 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002053 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002054#ifdef CONFIG_HAVE_KVM_IRQCHIP
2055 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002056 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002057#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002058 default:
2059 break;
2060 }
2061 return kvm_dev_ioctl_check_extension(arg);
2062}
2063
Avi Kivityf17abe92007-02-21 19:28:04 +02002064static long kvm_dev_ioctl(struct file *filp,
2065 unsigned int ioctl, unsigned long arg)
2066{
Avi Kivity07c45a32007-03-07 13:05:38 +02002067 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002068
2069 switch (ioctl) {
2070 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002071 r = -EINVAL;
2072 if (arg)
2073 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002074 r = KVM_API_VERSION;
2075 break;
2076 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002077 r = -EINVAL;
2078 if (arg)
2079 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002080 r = kvm_dev_ioctl_create_vm();
2081 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002082 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002083 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002084 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002085 case KVM_GET_VCPU_MMAP_SIZE:
2086 r = -EINVAL;
2087 if (arg)
2088 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002089 r = PAGE_SIZE; /* struct kvm_run */
2090#ifdef CONFIG_X86
2091 r += PAGE_SIZE; /* pio data page */
2092#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002093#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2094 r += PAGE_SIZE; /* coalesced mmio ring page */
2095#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002096 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002097 case KVM_TRACE_ENABLE:
2098 case KVM_TRACE_PAUSE:
2099 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002100 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002101 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002103 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 }
2105out:
2106 return r;
2107}
2108
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 .unlocked_ioctl = kvm_dev_ioctl,
2111 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002112 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113};
2114
2115static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002116 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 "kvm",
2118 &kvm_chardev_ops,
2119};
2120
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002121static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002122{
2123 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002124 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002125
Rusty Russell7f59f492008-12-07 21:25:45 +10302126 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002127 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002128
Rusty Russell7f59f492008-12-07 21:25:45 +10302129 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002130
2131 r = kvm_arch_hardware_enable(NULL);
2132
2133 if (r) {
2134 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2135 atomic_inc(&hardware_enable_failed);
2136 printk(KERN_INFO "kvm: enabling virtualization on "
2137 "CPU%d failed\n", cpu);
2138 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002139}
2140
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002141static void hardware_enable(void *junk)
2142{
2143 spin_lock(&kvm_lock);
2144 hardware_enable_nolock(junk);
2145 spin_unlock(&kvm_lock);
2146}
2147
2148static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002149{
2150 int cpu = raw_smp_processor_id();
2151
Rusty Russell7f59f492008-12-07 21:25:45 +10302152 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002153 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302154 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002155 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002156}
2157
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002158static void hardware_disable(void *junk)
2159{
2160 spin_lock(&kvm_lock);
2161 hardware_disable_nolock(junk);
2162 spin_unlock(&kvm_lock);
2163}
2164
Alexander Graf10474ae2009-09-15 11:37:46 +02002165static void hardware_disable_all_nolock(void)
2166{
2167 BUG_ON(!kvm_usage_count);
2168
2169 kvm_usage_count--;
2170 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002171 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002172}
2173
2174static void hardware_disable_all(void)
2175{
2176 spin_lock(&kvm_lock);
2177 hardware_disable_all_nolock();
2178 spin_unlock(&kvm_lock);
2179}
2180
2181static int hardware_enable_all(void)
2182{
2183 int r = 0;
2184
2185 spin_lock(&kvm_lock);
2186
2187 kvm_usage_count++;
2188 if (kvm_usage_count == 1) {
2189 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002190 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002191
2192 if (atomic_read(&hardware_enable_failed)) {
2193 hardware_disable_all_nolock();
2194 r = -EBUSY;
2195 }
2196 }
2197
2198 spin_unlock(&kvm_lock);
2199
2200 return r;
2201}
2202
Avi Kivity774c47f2007-02-12 00:54:47 -08002203static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2204 void *v)
2205{
2206 int cpu = (long)v;
2207
Alexander Graf10474ae2009-09-15 11:37:46 +02002208 if (!kvm_usage_count)
2209 return NOTIFY_OK;
2210
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002211 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002212 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002213 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002214 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2215 cpu);
2216 hardware_disable(NULL);
2217 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002218 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002219 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2220 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002221 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002222 break;
2223 }
2224 return NOTIFY_OK;
2225}
2226
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002227
Avi Kivityb7c41452010-12-02 17:52:50 +02002228asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002229{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002230 /* Fault while not rebooting. We want the trace. */
2231 BUG();
2232}
Avi Kivityb7c41452010-12-02 17:52:50 +02002233EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002234
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002235static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002236 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002237{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002238 /*
2239 * Some (well, at least mine) BIOSes hang on reboot if
2240 * in vmx root mode.
2241 *
2242 * And Intel TXT required VMX off for all cpu when system shutdown.
2243 */
2244 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2245 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002246 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002247 return NOTIFY_OK;
2248}
2249
2250static struct notifier_block kvm_reboot_notifier = {
2251 .notifier_call = kvm_reboot,
2252 .priority = 0,
2253};
2254
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002255static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002256{
2257 int i;
2258
2259 for (i = 0; i < bus->dev_count; i++) {
2260 struct kvm_io_device *pos = bus->devs[i];
2261
2262 kvm_iodevice_destructor(pos);
2263 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002264 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002265}
2266
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002267/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002268int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002269 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002270{
2271 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002272 struct kvm_io_bus *bus;
2273
2274 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002275 for (i = 0; i < bus->dev_count; i++)
2276 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2277 return 0;
2278 return -EOPNOTSUPP;
2279}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002280
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002281/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002282int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2283 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002284{
2285 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002286 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002287
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002288 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002289 for (i = 0; i < bus->dev_count; i++)
2290 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2291 return 0;
2292 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002293}
2294
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002295/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002296int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2297 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002298{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002299 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002300
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002301 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002302 if (bus->dev_count > NR_IOBUS_DEVS-1)
2303 return -ENOSPC;
2304
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002305 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2306 if (!new_bus)
2307 return -ENOMEM;
2308 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2309 new_bus->devs[new_bus->dev_count++] = dev;
2310 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2311 synchronize_srcu_expedited(&kvm->srcu);
2312 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002313
2314 return 0;
2315}
2316
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002317/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002318int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2319 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002320{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002321 int i, r;
2322 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002323
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002324 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2325 if (!new_bus)
2326 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002327
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002328 bus = kvm->buses[bus_idx];
2329 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2330
2331 r = -ENOENT;
2332 for (i = 0; i < new_bus->dev_count; i++)
2333 if (new_bus->devs[i] == dev) {
2334 r = 0;
2335 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002336 break;
2337 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002338
2339 if (r) {
2340 kfree(new_bus);
2341 return r;
2342 }
2343
2344 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2345 synchronize_srcu_expedited(&kvm->srcu);
2346 kfree(bus);
2347 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002348}
2349
Avi Kivity774c47f2007-02-12 00:54:47 -08002350static struct notifier_block kvm_cpu_notifier = {
2351 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002352};
2353
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002354static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002355{
2356 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002357 struct kvm *kvm;
2358
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002359 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002360 spin_lock(&kvm_lock);
2361 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002362 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002363 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002364 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002365}
2366
2367DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2368
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002369static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002370{
2371 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002372 struct kvm *kvm;
2373 struct kvm_vcpu *vcpu;
2374 int i;
2375
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002376 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002377 spin_lock(&kvm_lock);
2378 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002379 kvm_for_each_vcpu(i, vcpu, kvm)
2380 *val += *(u32 *)((void *)vcpu + offset);
2381
Avi Kivity1165f5f2007-04-19 17:27:43 +03002382 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002383 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002384}
2385
Avi Kivityba1389b2007-11-18 16:24:12 +02002386DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2387
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002388static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002389 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2390 [KVM_STAT_VM] = &vm_stat_fops,
2391};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002392
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002393static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394{
2395 struct kvm_stats_debugfs_item *p;
2396
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002397 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002399 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002400 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002401 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402}
2403
2404static void kvm_exit_debug(void)
2405{
2406 struct kvm_stats_debugfs_item *p;
2407
2408 for (p = debugfs_entries; p->name; ++p)
2409 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002410 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411}
2412
Avi Kivity59ae6c62007-02-12 00:54:48 -08002413static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2414{
Alexander Graf10474ae2009-09-15 11:37:46 +02002415 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002416 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002417 return 0;
2418}
2419
2420static int kvm_resume(struct sys_device *dev)
2421{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002422 if (kvm_usage_count) {
2423 WARN_ON(spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002424 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002425 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002426 return 0;
2427}
2428
2429static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002430 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002431 .suspend = kvm_suspend,
2432 .resume = kvm_resume,
2433};
2434
2435static struct sys_device kvm_sysdev = {
2436 .id = 0,
2437 .cls = &kvm_sysdev_class,
2438};
2439
Izik Eiduscea7bb22007-10-17 19:17:48 +02002440struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002441pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442
Avi Kivity15ad7142007-07-11 18:17:21 +03002443static inline
2444struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2445{
2446 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2447}
2448
2449static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2450{
2451 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2452
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002453 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002454}
2455
2456static void kvm_sched_out(struct preempt_notifier *pn,
2457 struct task_struct *next)
2458{
2459 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2460
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002461 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002462}
2463
Avi Kivity0ee75be2010-04-28 15:39:01 +03002464int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002465 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466{
2467 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002468 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002470 r = kvm_arch_init(opaque);
2471 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002472 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002473
2474 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2475
2476 if (bad_page == NULL) {
2477 r = -ENOMEM;
2478 goto out;
2479 }
2480
Anthony Liguori35149e22008-04-02 14:46:56 -05002481 bad_pfn = page_to_pfn(bad_page);
2482
Huang Yingbf998152010-05-31 14:28:19 +08002483 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2484
2485 if (hwpoison_page == NULL) {
2486 r = -ENOMEM;
2487 goto out_free_0;
2488 }
2489
2490 hwpoison_pfn = page_to_pfn(hwpoison_page);
2491
Gleb Natapovedba23e2010-07-07 20:16:45 +03002492 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2493
2494 if (fault_page == NULL) {
2495 r = -ENOMEM;
2496 goto out_free_0;
2497 }
2498
2499 fault_pfn = page_to_pfn(fault_page);
2500
Avi Kivity8437a612009-06-06 14:52:35 -07002501 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302502 r = -ENOMEM;
2503 goto out_free_0;
2504 }
2505
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002506 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302508 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509
Yang, Sheng002c7f72007-07-31 14:23:01 +03002510 for_each_online_cpu(cpu) {
2511 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002512 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002513 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002514 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002515 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002516 }
2517
Avi Kivity774c47f2007-02-12 00:54:47 -08002518 r = register_cpu_notifier(&kvm_cpu_notifier);
2519 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002520 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 register_reboot_notifier(&kvm_reboot_notifier);
2522
Avi Kivity59ae6c62007-02-12 00:54:48 -08002523 r = sysdev_class_register(&kvm_sysdev_class);
2524 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002525 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002526
2527 r = sysdev_register(&kvm_sysdev);
2528 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002529 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002530
Rusty Russellc16f8622007-07-30 21:12:19 +10002531 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002532 if (!vcpu_align)
2533 vcpu_align = __alignof__(struct kvm_vcpu);
2534 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002535 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002536 if (!kvm_vcpu_cache) {
2537 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002538 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002539 }
2540
Gleb Natapovaf585b92010-10-14 11:22:46 +02002541 r = kvm_async_pf_init();
2542 if (r)
2543 goto out_free;
2544
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002546 kvm_vm_fops.owner = module;
2547 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548
2549 r = misc_register(&kvm_dev);
2550 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002551 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002552 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 }
2554
Avi Kivity15ad7142007-07-11 18:17:21 +03002555 kvm_preempt_ops.sched_in = kvm_sched_in;
2556 kvm_preempt_ops.sched_out = kvm_sched_out;
2557
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002558 kvm_init_debug();
2559
Avi Kivityc7addb92007-09-16 18:58:32 +02002560 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561
Gleb Natapovaf585b92010-10-14 11:22:46 +02002562out_unreg:
2563 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002565 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002566out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002567 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002568out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002569 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002570out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002572 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002573out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002574out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002575 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302576out_free_0a:
2577 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002578out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002579 if (fault_page)
2580 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002581 if (hwpoison_page)
2582 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002583 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002584out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002585 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002586out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 return r;
2588}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002589EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002591void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002593 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002595 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002596 kvm_async_pf_deinit();
Avi Kivity59ae6c62007-02-12 00:54:48 -08002597 sysdev_unregister(&kvm_sysdev);
2598 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002600 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002601 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002602 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002603 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302604 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002605 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002606 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002608EXPORT_SYMBOL_GPL(kvm_exit);