blob: 5186e728c53ed7e27ba6d32df31d37f7aa140ebc [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.
Avi Kivity221d0592010-05-23 18:37:00 +03008 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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"
Laurent Vivier5f94c172008-05-30 16:05:54 +020058
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059#define CREATE_TRACE_POINTS
60#include <trace/events/kvm.h>
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Marcelo Tosattifa40a822009-06-04 15:08:24 -030065/*
66 * Ordering of locks:
67 *
Sheng Yangfae3a352009-12-15 10:28:07 +080068 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030069 */
70
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080071DEFINE_SPINLOCK(kvm_lock);
72LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080073
Rusty Russell7f59f492008-12-07 21:25:45 +103074static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020075static int kvm_usage_count = 0;
76static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030077
Rusty Russellc16f8622007-07-30 21:12:19 +100078struct kmem_cache *kvm_vcpu_cache;
79EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030080
Avi Kivity15ad7142007-07-11 18:17:21 +030081static __read_mostly struct preempt_ops kvm_preempt_ops;
82
Hollis Blanchard76f7c872008-04-15 16:05:42 -050083struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Avi Kivitybccf2152007-02-21 18:04:26 +020085static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
86 unsigned long arg);
Alexander Graf10474ae2009-09-15 11:37:46 +020087static int hardware_enable_all(void);
88static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020089
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020090static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
91
Hannes Edere8ba5d32008-11-28 17:02:42 +010092static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030093
Marcelo Tosatti54dee992009-06-11 12:07:44 -030094static bool largepages_enabled = true;
95
Gleb Natapovfa7bff82010-07-07 20:16:44 +030096static struct page *hwpoison_page;
97static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +080098
Gleb Natapovedba23e2010-07-07 20:16:45 +030099static struct page *fault_page;
100static pfn_t fault_pfn;
101
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800102inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300103{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100104 if (pfn_valid(pfn)) {
105 struct page *page = compound_head(pfn_to_page(pfn));
106 return PageReserved(page);
107 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300108
109 return true;
110}
111
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112/*
113 * Switches to specified vcpu, until a matching vcpu_put()
114 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200115void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116{
Avi Kivity15ad7142007-07-11 18:17:21 +0300117 int cpu;
118
Avi Kivitybccf2152007-02-21 18:04:26 +0200119 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300120 cpu = get_cpu();
121 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200122 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300123 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200124}
125
Carsten Otte313a3dc2007-10-11 19:16:52 +0200126void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127{
Avi Kivity15ad7142007-07-11 18:17:21 +0300128 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200129 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300130 preempt_notifier_unregister(&vcpu->preempt_notifier);
131 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132 mutex_unlock(&vcpu->mutex);
133}
134
Avi Kivityd9e368d2007-06-07 19:18:30 +0300135static void ack_flush(void *_completed)
136{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300137}
138
Rusty Russell49846892008-12-08 20:26:24 +1030139static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300140{
Avi Kivity597a5f52008-07-20 14:24:22 +0300141 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030142 cpumask_var_t cpus;
143 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300144 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300145
Li Zefan79f55992009-06-15 14:58:26 +0800146 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030147
Avi Kivity70e335e2010-02-18 11:25:22 +0200148 raw_spin_lock(&kvm->requests_lock);
Jan Kiszkae601e3b2009-07-20 11:30:12 +0200149 me = smp_processor_id();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300150 kvm_for_each_vcpu(i, vcpu, kvm) {
Avi Kivitya8eeb042010-05-10 12:34:53 +0300151 if (kvm_make_check_request(req, vcpu))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300152 continue;
153 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030154 if (cpus != NULL && cpu != -1 && cpu != me)
155 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300156 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030157 if (unlikely(cpus == NULL))
158 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
159 else if (!cpumask_empty(cpus))
160 smp_call_function_many(cpus, ack_flush, NULL, 1);
161 else
162 called = false;
Avi Kivity70e335e2010-02-18 11:25:22 +0200163 raw_spin_unlock(&kvm->requests_lock);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030164 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030165 return called;
166}
167
168void kvm_flush_remote_tlbs(struct kvm *kvm)
169{
170 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
171 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300172}
173
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500174void kvm_reload_remote_mmus(struct kvm *kvm)
175{
Rusty Russell49846892008-12-08 20:26:24 +1030176 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500177}
178
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000179int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
180{
181 struct page *page;
182 int r;
183
184 mutex_init(&vcpu->mutex);
185 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000186 vcpu->kvm = kvm;
187 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300188 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000189
190 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
191 if (!page) {
192 r = -ENOMEM;
193 goto fail;
194 }
195 vcpu->run = page_address(page);
196
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800197 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000198 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800199 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000200 return 0;
201
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000202fail_free_run:
203 free_page((unsigned long)vcpu->run);
204fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000205 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000206}
207EXPORT_SYMBOL_GPL(kvm_vcpu_init);
208
209void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
210{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800211 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 free_page((unsigned long)vcpu->run);
213}
214EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
215
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200216#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
217static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
218{
219 return container_of(mn, struct kvm, mmu_notifier);
220}
221
222static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
223 struct mm_struct *mm,
224 unsigned long address)
225{
226 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200227 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200228
229 /*
230 * When ->invalidate_page runs, the linux pte has been zapped
231 * already but the page is still allocated until
232 * ->invalidate_page returns. So if we increase the sequence
233 * here the kvm page fault will notice if the spte can't be
234 * established because the page is going to be freed. If
235 * instead the kvm page fault establishes the spte before
236 * ->invalidate_page runs, kvm_unmap_hva will release it
237 * before returning.
238 *
239 * The sequence increase only need to be seen at spin_unlock
240 * time, and not at spin_lock time.
241 *
242 * Increasing the sequence after the spin_unlock would be
243 * unsafe because the kvm page fault could then establish the
244 * pte after kvm_unmap_hva returned, without noticing the page
245 * is going to be freed.
246 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200247 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200248 spin_lock(&kvm->mmu_lock);
249 kvm->mmu_notifier_seq++;
250 need_tlb_flush = kvm_unmap_hva(kvm, address);
251 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200252 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200253
254 /* we've to flush the tlb before the pages can be freed */
255 if (need_tlb_flush)
256 kvm_flush_remote_tlbs(kvm);
257
258}
259
Izik Eidus3da0dd42009-09-23 21:47:18 +0300260static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
261 struct mm_struct *mm,
262 unsigned long address,
263 pte_t pte)
264{
265 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200266 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300267
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200268 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300269 spin_lock(&kvm->mmu_lock);
270 kvm->mmu_notifier_seq++;
271 kvm_set_spte_hva(kvm, address, pte);
272 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200273 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300274}
275
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200276static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
277 struct mm_struct *mm,
278 unsigned long start,
279 unsigned long end)
280{
281 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200282 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200283
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200284 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200285 spin_lock(&kvm->mmu_lock);
286 /*
287 * The count increase must become visible at unlock time as no
288 * spte can be established without taking the mmu_lock and
289 * count is also read inside the mmu_lock critical section.
290 */
291 kvm->mmu_notifier_count++;
292 for (; start < end; start += PAGE_SIZE)
293 need_tlb_flush |= kvm_unmap_hva(kvm, start);
294 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200295 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200296
297 /* we've to flush the tlb before the pages can be freed */
298 if (need_tlb_flush)
299 kvm_flush_remote_tlbs(kvm);
300}
301
302static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
303 struct mm_struct *mm,
304 unsigned long start,
305 unsigned long end)
306{
307 struct kvm *kvm = mmu_notifier_to_kvm(mn);
308
309 spin_lock(&kvm->mmu_lock);
310 /*
311 * This sequence increase will notify the kvm page fault that
312 * the page that is going to be mapped in the spte could have
313 * been freed.
314 */
315 kvm->mmu_notifier_seq++;
316 /*
317 * The above sequence increase must be visible before the
318 * below count decrease but both values are read by the kvm
319 * page fault under mmu_lock spinlock so we don't need to add
320 * a smb_wmb() here in between the two.
321 */
322 kvm->mmu_notifier_count--;
323 spin_unlock(&kvm->mmu_lock);
324
325 BUG_ON(kvm->mmu_notifier_count < 0);
326}
327
328static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
329 struct mm_struct *mm,
330 unsigned long address)
331{
332 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200333 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200334
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200335 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200336 spin_lock(&kvm->mmu_lock);
337 young = kvm_age_hva(kvm, address);
338 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200339 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200340
341 if (young)
342 kvm_flush_remote_tlbs(kvm);
343
344 return young;
345}
346
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100347static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
348 struct mm_struct *mm)
349{
350 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800351 int idx;
352
353 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100354 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800355 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100356}
357
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200358static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
359 .invalidate_page = kvm_mmu_notifier_invalidate_page,
360 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
361 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
362 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300363 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100364 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200365};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200366
367static int kvm_init_mmu_notifier(struct kvm *kvm)
368{
369 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
370 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
371}
372
373#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
374
375static int kvm_init_mmu_notifier(struct kvm *kvm)
376{
377 return 0;
378}
379
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200380#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
381
Avi Kivityf17abe92007-02-21 19:28:04 +0200382static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200384 int r = 0, i;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800385 struct kvm *kvm = kvm_arch_create_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800387 if (IS_ERR(kvm))
388 goto out;
Alexander Graf10474ae2009-09-15 11:37:46 +0200389
390 r = hardware_enable_all();
391 if (r)
392 goto out_err_nodisable;
393
Avi Kivity75858a82009-01-04 17:10:50 +0200394#ifdef CONFIG_HAVE_KVM_IRQCHIP
395 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300396 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200397#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200399 r = -ENOMEM;
400 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
401 if (!kvm->memslots)
402 goto out_err;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200403 if (init_srcu_struct(&kvm->srcu))
404 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200405 for (i = 0; i < KVM_NR_BUSES; i++) {
406 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
407 GFP_KERNEL);
408 if (!kvm->buses[i]) {
409 cleanup_srcu_struct(&kvm->srcu);
410 goto out_err;
411 }
412 }
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200413
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200414 r = kvm_init_mmu_notifier(kvm);
Avi Kivity283d0c62009-12-20 14:25:19 +0200415 if (r) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200416 cleanup_srcu_struct(&kvm->srcu);
Avi Kivity283d0c62009-12-20 14:25:19 +0200417 goto out_err;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200418 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200419
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200420 kvm->mm = current->mm;
421 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500422 spin_lock_init(&kvm->mmu_lock);
Avi Kivity70e335e2010-02-18 11:25:22 +0200423 raw_spin_lock_init(&kvm->requests_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400424 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800425 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300426 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200427 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300428 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000429 spin_lock(&kvm_lock);
430 list_add(&kvm->vm_list, &vm_list);
431 spin_unlock(&kvm_lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800432out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200433 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200434
435out_err:
436 hardware_disable_all();
437out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200438 for (i = 0; i < KVM_NR_BUSES; i++)
439 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200440 kfree(kvm->memslots);
Alexander Graf10474ae2009-09-15 11:37:46 +0200441 kfree(kvm);
442 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200443}
444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445/*
446 * Free any memory in @free but not in @dont.
447 */
448static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
449 struct kvm_memory_slot *dont)
450{
Joerg Roedelec04b262009-06-19 15:16:23 +0200451 int i;
452
Izik Eidus290fc382007-09-27 14:11:22 +0200453 if (!dont || free->rmap != dont->rmap)
454 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455
456 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
457 vfree(free->dirty_bitmap);
458
Joerg Roedelec04b262009-06-19 15:16:23 +0200459
460 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
461 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
462 vfree(free->lpage_info[i]);
463 free->lpage_info[i] = NULL;
464 }
465 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000468 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500469 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470}
471
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800472void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473{
474 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200475 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200477 for (i = 0; i < slots->nmemslots; ++i)
478 kvm_free_physmem_slot(&slots->memslots[i], NULL);
479
480 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481}
482
Avi Kivityf17abe92007-02-21 19:28:04 +0200483static void kvm_destroy_vm(struct kvm *kvm)
484{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200485 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200486 struct mm_struct *mm = kvm->mm;
487
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800488 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800489 spin_lock(&kvm_lock);
490 list_del(&kvm->vm_list);
491 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200492 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200493 for (i = 0; i < KVM_NR_BUSES; i++)
494 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200495 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200496#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
497 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200498#else
499 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200500#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800501 kvm_arch_destroy_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200502 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200503 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200504}
505
Izik Eidusd39f13b2008-03-30 16:01:25 +0300506void kvm_get_kvm(struct kvm *kvm)
507{
508 atomic_inc(&kvm->users_count);
509}
510EXPORT_SYMBOL_GPL(kvm_get_kvm);
511
512void kvm_put_kvm(struct kvm *kvm)
513{
514 if (atomic_dec_and_test(&kvm->users_count))
515 kvm_destroy_vm(kvm);
516}
517EXPORT_SYMBOL_GPL(kvm_put_kvm);
518
519
Avi Kivityf17abe92007-02-21 19:28:04 +0200520static int kvm_vm_release(struct inode *inode, struct file *filp)
521{
522 struct kvm *kvm = filp->private_data;
523
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400524 kvm_irqfd_release(kvm);
525
Izik Eidusd39f13b2008-03-30 16:01:25 +0300526 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 return 0;
528}
529
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531 * Allocate some memory and give it an address in the guest physical address
532 * space.
533 *
534 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800535 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500536 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800538int __kvm_set_memory_region(struct kvm *kvm,
539 struct kvm_userspace_memory_region *mem,
540 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200542 int r, flush_shadow = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200544 unsigned long npages;
545 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 struct kvm_memory_slot *memslot;
547 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200548 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549
550 r = -EINVAL;
551 /* General sanity checks */
552 if (mem->memory_size & (PAGE_SIZE - 1))
553 goto out;
554 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
555 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800556 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600557 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200558 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 goto out;
560 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
561 goto out;
562
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200563 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
565 npages = mem->memory_size >> PAGE_SHIFT;
566
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900567 r = -EINVAL;
568 if (npages > KVM_MEM_MAX_NR_PAGES)
569 goto out;
570
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 if (!npages)
572 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
573
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574 new = old = *memslot;
575
Avi Kivitye36d96f2010-06-21 10:56:36 +0300576 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 new.base_gfn = base_gfn;
578 new.npages = npages;
579 new.flags = mem->flags;
580
581 /* Disallow changing a memory slot's size. */
582 r = -EINVAL;
583 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800584 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585
586 /* Check for overlaps */
587 r = -EEXIST;
588 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200589 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200591 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 continue;
593 if (!((base_gfn + npages <= s->base_gfn) ||
594 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800595 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598 /* Free page dirty bitmap if unneeded */
599 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000600 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601
602 r = -ENOMEM;
603
604 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200605#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500606 if (npages && !new.rmap) {
Lai Jiangshan3bd89002010-06-02 17:01:23 +0800607 new.rmap = vmalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200608
609 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800610 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200611
Izik Eidus290fc382007-09-27 14:11:22 +0200612 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500613
Izik Eidus80b14b52007-10-25 11:54:04 +0200614 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200615 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200617 if (!npages)
618 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300619
Joerg Roedelec04b262009-06-19 15:16:23 +0200620 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200621 unsigned long ugfn;
622 unsigned long j;
623 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200624 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300625
Joerg Roedelec04b262009-06-19 15:16:23 +0200626 /* Avoid unused variable warning if no large pages */
627 (void)level;
628
629 if (new.lpage_info[i])
630 continue;
631
Joerg Roedel82855412010-07-01 16:00:11 +0200632 lpages = 1 + ((base_gfn + npages - 1)
633 >> KVM_HPAGE_GFN_SHIFT(level));
634 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200635
636 new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
637
638 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300639 goto out_free;
640
Joerg Roedelec04b262009-06-19 15:16:23 +0200641 memset(new.lpage_info[i], 0,
642 lpages * sizeof(*new.lpage_info[i]));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300643
Joerg Roedel82855412010-07-01 16:00:11 +0200644 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200645 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200646 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200647 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300648 ugfn = new.userspace_addr >> PAGE_SHIFT;
649 /*
650 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300651 * other, or if explicitly asked to, disable large page
652 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300653 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200654 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300655 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200656 for (j = 0; j < lpages; ++j)
657 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300658 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Joerg Roedelec04b262009-06-19 15:16:23 +0200660skip_lpage:
661
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 /* Allocate page dirty bitmap if needed */
663 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900664 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665
666 new.dirty_bitmap = vmalloc(dirty_bytes);
667 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800668 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 memset(new.dirty_bitmap, 0, dirty_bytes);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200670 /* destroy any largepage mappings for dirty tracking */
Izik Eiduse2445842009-06-10 19:23:24 +0300671 if (old.npages)
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200672 flush_shadow = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200674#else /* not defined CONFIG_S390 */
675 new.user_alloc = user_alloc;
676 if (user_alloc)
677 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200678#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200680 if (!npages) {
681 r = -ENOMEM;
682 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
683 if (!slots)
684 goto out_free;
685 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
686 if (mem->slot >= slots->nmemslots)
687 slots->nmemslots = mem->slot + 1;
688 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
689
690 old_memslots = kvm->memslots;
691 rcu_assign_pointer(kvm->memslots, slots);
692 synchronize_srcu_expedited(&kvm->srcu);
693 /* From this point no new shadow pages pointing to a deleted
694 * memslot will be created.
695 *
696 * validation of sp->gfn happens in:
697 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
698 * - kvm_is_visible_gfn (mmu_check_roots)
699 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300700 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200701 kfree(old_memslots);
702 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300703
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200704 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
705 if (r)
706 goto out_free;
707
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200708#ifdef CONFIG_DMAR
709 /* map the pages in iommu page table */
710 if (npages) {
711 r = kvm_iommu_map_pages(kvm, &new);
712 if (r)
713 goto out_free;
714 }
715#endif
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200716
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200717 r = -ENOMEM;
718 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
719 if (!slots)
720 goto out_free;
721 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
722 if (mem->slot >= slots->nmemslots)
723 slots->nmemslots = mem->slot + 1;
724
725 /* actual memory is freed via old in kvm_free_physmem_slot below */
726 if (!npages) {
727 new.rmap = NULL;
728 new.dirty_bitmap = NULL;
729 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
730 new.lpage_info[i] = NULL;
731 }
732
733 slots->memslots[mem->slot] = new;
734 old_memslots = kvm->memslots;
735 rcu_assign_pointer(kvm->memslots, slots);
736 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200738 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800739
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200740 kvm_free_physmem_slot(&old, &new);
741 kfree(old_memslots);
742
743 if (flush_shadow)
744 kvm_arch_flush_shadow(kvm);
745
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 return 0;
747
Sheng Yangf78e0e22007-10-29 09:40:42 +0800748out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 kvm_free_physmem_slot(&new, &old);
750out:
751 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200752
753}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800754EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
755
756int kvm_set_memory_region(struct kvm *kvm,
757 struct kvm_userspace_memory_region *mem,
758 int user_alloc)
759{
760 int r;
761
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200762 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800763 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200764 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800765 return r;
766}
Izik Eidus210c7c42007-10-24 23:52:57 +0200767EXPORT_SYMBOL_GPL(kvm_set_memory_region);
768
Carsten Otte1fe779f2007-10-29 16:08:35 +0100769int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
770 struct
771 kvm_userspace_memory_region *mem,
772 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200773{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200774 if (mem->slot >= KVM_MEMORY_SLOTS)
775 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200776 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777}
778
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800779int kvm_get_dirty_log(struct kvm *kvm,
780 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781{
782 struct kvm_memory_slot *memslot;
783 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900784 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 unsigned long any = 0;
786
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 r = -EINVAL;
788 if (log->slot >= KVM_MEMORY_SLOTS)
789 goto out;
790
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200791 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 r = -ENOENT;
793 if (!memslot->dirty_bitmap)
794 goto out;
795
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900796 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797
Uri Lublincd1a4a92007-02-22 16:43:09 +0200798 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 any = memslot->dirty_bitmap[i];
800
801 r = -EFAULT;
802 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
803 goto out;
804
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800805 if (any)
806 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807
808 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 return r;
811}
812
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300813void kvm_disable_largepages(void)
814{
815 largepages_enabled = false;
816}
817EXPORT_SYMBOL_GPL(kvm_disable_largepages);
818
Izik Eiduscea7bb22007-10-17 19:17:48 +0200819int is_error_page(struct page *page)
820{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300821 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200822}
823EXPORT_SYMBOL_GPL(is_error_page);
824
Anthony Liguori35149e22008-04-02 14:46:56 -0500825int is_error_pfn(pfn_t pfn)
826{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300827 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500828}
829EXPORT_SYMBOL_GPL(is_error_pfn);
830
Huang Yingbf998152010-05-31 14:28:19 +0800831int is_hwpoison_pfn(pfn_t pfn)
832{
833 return pfn == hwpoison_pfn;
834}
835EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
836
Gleb Natapovedba23e2010-07-07 20:16:45 +0300837int is_fault_pfn(pfn_t pfn)
838{
839 return pfn == fault_pfn;
840}
841EXPORT_SYMBOL_GPL(is_fault_pfn);
842
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200843static inline unsigned long bad_hva(void)
844{
845 return PAGE_OFFSET;
846}
847
848int kvm_is_error_hva(unsigned long addr)
849{
850 return addr == bad_hva();
851}
852EXPORT_SYMBOL_GPL(kvm_is_error_hva);
853
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300854struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855{
856 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800857 struct kvm_memslots *slots = kvm_memslots(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200859 for (i = 0; i < slots->nmemslots; ++i) {
860 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
862 if (gfn >= memslot->base_gfn
863 && gfn < memslot->base_gfn + memslot->npages)
864 return memslot;
865 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000866 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867}
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300868EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869
Izik Eiduse0d62c72007-10-24 23:57:46 +0200870int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
871{
872 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800873 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200874
Izik Eiduse0d62c72007-10-24 23:57:46 +0200875 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200876 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200877
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200878 if (memslot->flags & KVM_MEMSLOT_INVALID)
879 continue;
880
Izik Eiduse0d62c72007-10-24 23:57:46 +0200881 if (gfn >= memslot->base_gfn
882 && gfn < memslot->base_gfn + memslot->npages)
883 return 1;
884 }
885 return 0;
886}
887EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
888
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100889unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
890{
891 struct vm_area_struct *vma;
892 unsigned long addr, size;
893
894 size = PAGE_SIZE;
895
896 addr = gfn_to_hva(kvm, gfn);
897 if (kvm_is_error_hva(addr))
898 return PAGE_SIZE;
899
900 down_read(&current->mm->mmap_sem);
901 vma = find_vma(current->mm, addr);
902 if (!vma)
903 goto out;
904
905 size = vma_kernel_pagesize(vma);
906
907out:
908 up_read(&current->mm->mmap_sem);
909
910 return size;
911}
912
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200913int memslot_id(struct kvm *kvm, gfn_t gfn)
914{
915 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800916 struct kvm_memslots *slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200917 struct kvm_memory_slot *memslot = NULL;
918
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200919 for (i = 0; i < slots->nmemslots; ++i) {
920 memslot = &slots->memslots[i];
921
922 if (gfn >= memslot->base_gfn
923 && gfn < memslot->base_gfn + memslot->npages)
924 break;
925 }
926
927 return memslot - slots->memslots;
928}
929
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900930static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
931{
932 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
933}
934
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300935unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200936{
937 struct kvm_memory_slot *slot;
938
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300939 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200940 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +0200941 return bad_hva();
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +0900942 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200943}
Sheng Yang0d150292008-04-25 21:44:50 +0800944EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200945
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200946static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300947{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500948 struct page *page[1];
949 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500950 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300951
Avi Kivity60395222007-10-21 11:03:36 +0200952 might_sleep();
953
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300954 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200955
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500956 if (unlikely(npages != 1)) {
957 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500958
Huang Yingbbeb3402010-06-22 14:23:11 +0800959 down_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +0800960 if (is_hwpoison_address(addr)) {
Huang Yingbbeb3402010-06-22 14:23:11 +0800961 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +0800962 get_page(hwpoison_page);
963 return page_to_pfn(hwpoison_page);
964 }
965
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500966 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300967
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500968 if (vma == NULL || addr < vma->vm_start ||
969 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300970 up_read(&current->mm->mmap_sem);
Gleb Natapovedba23e2010-07-07 20:16:45 +0300971 get_page(fault_page);
972 return page_to_pfn(fault_page);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500973 }
974
975 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300976 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800977 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500978 } else
979 pfn = page_to_pfn(page[0]);
980
981 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500982}
983
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200984pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
985{
986 unsigned long addr;
987
988 addr = gfn_to_hva(kvm, gfn);
989 if (kvm_is_error_hva(addr)) {
990 get_page(bad_page);
991 return page_to_pfn(bad_page);
992 }
993
994 return hva_to_pfn(kvm, addr);
995}
Anthony Liguori35149e22008-04-02 14:46:56 -0500996EXPORT_SYMBOL_GPL(gfn_to_pfn);
997
Marcelo Tosatti506f0d62009-12-23 14:35:19 -0200998pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
999 struct kvm_memory_slot *slot, gfn_t gfn)
1000{
1001 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
1002 return hva_to_pfn(kvm, addr);
1003}
1004
Anthony Liguori35149e22008-04-02 14:46:56 -05001005struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1006{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001007 pfn_t pfn;
1008
1009 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001010 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001011 return pfn_to_page(pfn);
1012
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001013 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001014
1015 get_page(bad_page);
1016 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001017}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001018
Avi Kivity954bbbc2007-03-30 14:02:32 +03001019EXPORT_SYMBOL_GPL(gfn_to_page);
1020
Izik Eidusb4231d62007-11-20 11:49:33 +02001021void kvm_release_page_clean(struct page *page)
1022{
Anthony Liguori35149e22008-04-02 14:46:56 -05001023 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001024}
1025EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1026
Anthony Liguori35149e22008-04-02 14:46:56 -05001027void kvm_release_pfn_clean(pfn_t pfn)
1028{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001029 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001030 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001031}
1032EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1033
Izik Eidusb4231d62007-11-20 11:49:33 +02001034void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001035{
Anthony Liguori35149e22008-04-02 14:46:56 -05001036 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001037}
Izik Eidusb4231d62007-11-20 11:49:33 +02001038EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001039
Anthony Liguori35149e22008-04-02 14:46:56 -05001040void kvm_release_pfn_dirty(pfn_t pfn)
1041{
1042 kvm_set_pfn_dirty(pfn);
1043 kvm_release_pfn_clean(pfn);
1044}
1045EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1046
1047void kvm_set_page_dirty(struct page *page)
1048{
1049 kvm_set_pfn_dirty(page_to_pfn(page));
1050}
1051EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1052
1053void kvm_set_pfn_dirty(pfn_t pfn)
1054{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001055 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001056 struct page *page = pfn_to_page(pfn);
1057 if (!PageReserved(page))
1058 SetPageDirty(page);
1059 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001060}
1061EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1062
1063void kvm_set_pfn_accessed(pfn_t pfn)
1064{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001065 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001066 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001067}
1068EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1069
1070void kvm_get_pfn(pfn_t pfn)
1071{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001072 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001073 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001074}
1075EXPORT_SYMBOL_GPL(kvm_get_pfn);
1076
Izik Eidus195aefd2007-10-01 22:14:18 +02001077static int next_segment(unsigned long len, int offset)
1078{
1079 if (len > PAGE_SIZE - offset)
1080 return PAGE_SIZE - offset;
1081 else
1082 return len;
1083}
1084
1085int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1086 int len)
1087{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001088 int r;
1089 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001090
Izik Eiduse0506bc2007-11-11 22:10:22 +02001091 addr = gfn_to_hva(kvm, gfn);
1092 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001093 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001094 r = copy_from_user(data, (void __user *)addr + offset, len);
1095 if (r)
1096 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001097 return 0;
1098}
1099EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1100
1101int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1102{
1103 gfn_t gfn = gpa >> PAGE_SHIFT;
1104 int seg;
1105 int offset = offset_in_page(gpa);
1106 int ret;
1107
1108 while ((seg = next_segment(len, offset)) != 0) {
1109 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1110 if (ret < 0)
1111 return ret;
1112 offset = 0;
1113 len -= seg;
1114 data += seg;
1115 ++gfn;
1116 }
1117 return 0;
1118}
1119EXPORT_SYMBOL_GPL(kvm_read_guest);
1120
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001121int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1122 unsigned long len)
1123{
1124 int r;
1125 unsigned long addr;
1126 gfn_t gfn = gpa >> PAGE_SHIFT;
1127 int offset = offset_in_page(gpa);
1128
1129 addr = gfn_to_hva(kvm, gfn);
1130 if (kvm_is_error_hva(addr))
1131 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001132 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001133 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001134 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001135 if (r)
1136 return -EFAULT;
1137 return 0;
1138}
1139EXPORT_SYMBOL(kvm_read_guest_atomic);
1140
Izik Eidus195aefd2007-10-01 22:14:18 +02001141int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1142 int offset, int len)
1143{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001144 int r;
1145 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001146
Izik Eiduse0506bc2007-11-11 22:10:22 +02001147 addr = gfn_to_hva(kvm, gfn);
1148 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001149 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001150 r = copy_to_user((void __user *)addr + offset, data, len);
1151 if (r)
1152 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001153 mark_page_dirty(kvm, gfn);
1154 return 0;
1155}
1156EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1157
1158int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1159 unsigned long len)
1160{
1161 gfn_t gfn = gpa >> PAGE_SHIFT;
1162 int seg;
1163 int offset = offset_in_page(gpa);
1164 int ret;
1165
1166 while ((seg = next_segment(len, offset)) != 0) {
1167 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1168 if (ret < 0)
1169 return ret;
1170 offset = 0;
1171 len -= seg;
1172 data += seg;
1173 ++gfn;
1174 }
1175 return 0;
1176}
1177
1178int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1179{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001180 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001181}
1182EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1183
1184int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1185{
1186 gfn_t gfn = gpa >> PAGE_SHIFT;
1187 int seg;
1188 int offset = offset_in_page(gpa);
1189 int ret;
1190
1191 while ((seg = next_segment(len, offset)) != 0) {
1192 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1193 if (ret < 0)
1194 return ret;
1195 offset = 0;
1196 len -= seg;
1197 ++gfn;
1198 }
1199 return 0;
1200}
1201EXPORT_SYMBOL_GPL(kvm_clear_guest);
1202
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1204{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001205 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206
Avi Kivitya1f4d3952010-06-21 11:44:20 +03001207 memslot = gfn_to_memslot(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001208 if (memslot && memslot->dirty_bitmap) {
1209 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210
Takuya Yoshikawad1476932010-04-23 17:48:35 +09001211 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 }
1213}
1214
Eddie Dongb6958ce2007-07-18 12:15:21 +03001215/*
1216 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1217 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001218void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001219{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001220 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001221
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001222 for (;;) {
1223 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001224
Gleb Natapova1b37102009-07-09 15:33:52 +03001225 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001226 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001227 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001228 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001229 if (kvm_cpu_has_pending_timer(vcpu))
1230 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001231 if (signal_pending(current))
1232 break;
1233
Eddie Dongb6958ce2007-07-18 12:15:21 +03001234 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001235 }
1236
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001237 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001238}
1239
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240void kvm_resched(struct kvm_vcpu *vcpu)
1241{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001242 if (!need_resched())
1243 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245}
1246EXPORT_SYMBOL_GPL(kvm_resched);
1247
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001248void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1249{
1250 ktime_t expires;
1251 DEFINE_WAIT(wait);
1252
1253 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1254
1255 /* Sleep for 100 us, and hope lock-holder got scheduled */
1256 expires = ktime_add_ns(ktime_get(), 100000UL);
1257 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1258
1259 finish_wait(&vcpu->wq, &wait);
1260}
1261EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1262
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001263static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001264{
1265 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001266 struct page *page;
1267
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001268 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001269 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001270#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001271 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001272 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001273#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001274#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1275 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1276 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1277#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001278 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001279 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001280 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001281 vmf->page = page;
1282 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001283}
1284
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001285static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001286 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001287};
1288
1289static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1290{
1291 vma->vm_ops = &kvm_vcpu_vm_ops;
1292 return 0;
1293}
1294
Avi Kivitybccf2152007-02-21 18:04:26 +02001295static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1296{
1297 struct kvm_vcpu *vcpu = filp->private_data;
1298
Al Viro66c0b392008-04-19 20:33:56 +01001299 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001300 return 0;
1301}
1302
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001303static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001304 .release = kvm_vcpu_release,
1305 .unlocked_ioctl = kvm_vcpu_ioctl,
1306 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001307 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001308};
1309
1310/*
1311 * Allocates an inode for the vcpu.
1312 */
1313static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1314{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001315 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001316}
1317
Avi Kivityc5ea7662007-02-20 18:41:05 +02001318/*
1319 * Creates some virtual cpus. Good luck creating more than one.
1320 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001321static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001322{
1323 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001324 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001325
Gleb Natapov73880c82009-06-09 15:56:28 +03001326 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001327 if (IS_ERR(vcpu))
1328 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001329
Avi Kivity15ad7142007-07-11 18:17:21 +03001330 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1331
Avi Kivity26e52152007-11-20 15:30:24 +02001332 r = kvm_arch_vcpu_setup(vcpu);
1333 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001334 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001335
Shaohua Li11ec2802007-07-23 14:51:37 +08001336 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001337 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1338 r = -EINVAL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001339 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001340 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001341
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001342 kvm_for_each_vcpu(r, v, kvm)
1343 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001344 r = -EEXIST;
1345 goto vcpu_destroy;
1346 }
1347
1348 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001349
1350 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001351 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001352 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001353 if (r < 0) {
1354 kvm_put_kvm(kvm);
1355 goto vcpu_destroy;
1356 }
1357
1358 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1359 smp_wmb();
1360 atomic_inc(&kvm->online_vcpus);
1361
1362#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1363 if (kvm->bsp_vcpu_id == id)
1364 kvm->bsp_vcpu = vcpu;
1365#endif
1366 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001367 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001368
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001369vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001370 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001371 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001372 return r;
1373}
1374
Avi Kivity1961d272007-03-05 19:46:05 +02001375static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1376{
1377 if (sigset) {
1378 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1379 vcpu->sigset_active = 1;
1380 vcpu->sigset = *sigset;
1381 } else
1382 vcpu->sigset_active = 0;
1383 return 0;
1384}
1385
Avi Kivitybccf2152007-02-21 18:04:26 +02001386static long kvm_vcpu_ioctl(struct file *filp,
1387 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388{
Avi Kivitybccf2152007-02-21 18:04:26 +02001389 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001390 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001391 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001392 struct kvm_fpu *fpu = NULL;
1393 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001395 if (vcpu->kvm->mm != current->mm)
1396 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001397
1398#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1399 /*
1400 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1401 * so vcpu_load() would break it.
1402 */
1403 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1404 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1405#endif
1406
1407
1408 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001410 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001411 r = -EINVAL;
1412 if (arg)
1413 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001414 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001417 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001419 r = -ENOMEM;
1420 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1421 if (!kvm_regs)
1422 goto out;
1423 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001425 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001427 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1428 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001430out_free1:
1431 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432 break;
1433 }
1434 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001435 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001437 r = -ENOMEM;
1438 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1439 if (!kvm_regs)
1440 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001442 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1443 goto out_free2;
1444 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001446 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001448out_free2:
1449 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 break;
1451 }
1452 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001453 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1454 r = -ENOMEM;
1455 if (!kvm_sregs)
1456 goto out;
1457 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 if (r)
1459 goto out;
1460 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001461 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 goto out;
1463 r = 0;
1464 break;
1465 }
1466 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001467 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1468 r = -ENOMEM;
1469 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001471 r = -EFAULT;
1472 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1473 goto out;
1474 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 if (r)
1476 goto out;
1477 r = 0;
1478 break;
1479 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001480 case KVM_GET_MP_STATE: {
1481 struct kvm_mp_state mp_state;
1482
1483 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1484 if (r)
1485 goto out;
1486 r = -EFAULT;
1487 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1488 goto out;
1489 r = 0;
1490 break;
1491 }
1492 case KVM_SET_MP_STATE: {
1493 struct kvm_mp_state mp_state;
1494
1495 r = -EFAULT;
1496 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1497 goto out;
1498 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1499 if (r)
1500 goto out;
1501 r = 0;
1502 break;
1503 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 case KVM_TRANSLATE: {
1505 struct kvm_translation tr;
1506
1507 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001508 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001510 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 if (r)
1512 goto out;
1513 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001514 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 goto out;
1516 r = 0;
1517 break;
1518 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001519 case KVM_SET_GUEST_DEBUG: {
1520 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521
1522 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001523 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001525 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 if (r)
1527 goto out;
1528 r = 0;
1529 break;
1530 }
Avi Kivity1961d272007-03-05 19:46:05 +02001531 case KVM_SET_SIGNAL_MASK: {
1532 struct kvm_signal_mask __user *sigmask_arg = argp;
1533 struct kvm_signal_mask kvm_sigmask;
1534 sigset_t sigset, *p;
1535
1536 p = NULL;
1537 if (argp) {
1538 r = -EFAULT;
1539 if (copy_from_user(&kvm_sigmask, argp,
1540 sizeof kvm_sigmask))
1541 goto out;
1542 r = -EINVAL;
1543 if (kvm_sigmask.len != sizeof sigset)
1544 goto out;
1545 r = -EFAULT;
1546 if (copy_from_user(&sigset, sigmask_arg->sigset,
1547 sizeof sigset))
1548 goto out;
1549 p = &sigset;
1550 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001551 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001552 break;
1553 }
Avi Kivityb8836732007-04-01 16:34:31 +03001554 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001555 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1556 r = -ENOMEM;
1557 if (!fpu)
1558 goto out;
1559 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001560 if (r)
1561 goto out;
1562 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001563 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001564 goto out;
1565 r = 0;
1566 break;
1567 }
1568 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001569 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1570 r = -ENOMEM;
1571 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001572 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001573 r = -EFAULT;
1574 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1575 goto out;
1576 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001577 if (r)
1578 goto out;
1579 r = 0;
1580 break;
1581 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001582 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001583 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001584 }
1585out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001586 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001587 kfree(fpu);
1588 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001589 return r;
1590}
1591
1592static long kvm_vm_ioctl(struct file *filp,
1593 unsigned int ioctl, unsigned long arg)
1594{
1595 struct kvm *kvm = filp->private_data;
1596 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001597 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001598
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001599 if (kvm->mm != current->mm)
1600 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001601 switch (ioctl) {
1602 case KVM_CREATE_VCPU:
1603 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1604 if (r < 0)
1605 goto out;
1606 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001607 case KVM_SET_USER_MEMORY_REGION: {
1608 struct kvm_userspace_memory_region kvm_userspace_mem;
1609
1610 r = -EFAULT;
1611 if (copy_from_user(&kvm_userspace_mem, argp,
1612 sizeof kvm_userspace_mem))
1613 goto out;
1614
1615 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 if (r)
1617 goto out;
1618 break;
1619 }
1620 case KVM_GET_DIRTY_LOG: {
1621 struct kvm_dirty_log log;
1622
1623 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001624 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001626 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 if (r)
1628 goto out;
1629 break;
1630 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001631#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1632 case KVM_REGISTER_COALESCED_MMIO: {
1633 struct kvm_coalesced_mmio_zone zone;
1634 r = -EFAULT;
1635 if (copy_from_user(&zone, argp, sizeof zone))
1636 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001637 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1638 if (r)
1639 goto out;
1640 r = 0;
1641 break;
1642 }
1643 case KVM_UNREGISTER_COALESCED_MMIO: {
1644 struct kvm_coalesced_mmio_zone zone;
1645 r = -EFAULT;
1646 if (copy_from_user(&zone, argp, sizeof zone))
1647 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02001648 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1649 if (r)
1650 goto out;
1651 r = 0;
1652 break;
1653 }
1654#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001655 case KVM_IRQFD: {
1656 struct kvm_irqfd data;
1657
1658 r = -EFAULT;
1659 if (copy_from_user(&data, argp, sizeof data))
1660 goto out;
1661 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
1662 break;
1663 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001664 case KVM_IOEVENTFD: {
1665 struct kvm_ioeventfd data;
1666
1667 r = -EFAULT;
1668 if (copy_from_user(&data, argp, sizeof data))
1669 goto out;
1670 r = kvm_ioeventfd(kvm, &data);
1671 break;
1672 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001673#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1674 case KVM_SET_BOOT_CPU_ID:
1675 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001676 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001677 if (atomic_read(&kvm->online_vcpus) != 0)
1678 r = -EBUSY;
1679 else
1680 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03001681 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001682 break;
1683#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001684 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001685 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001686 if (r == -ENOTTY)
1687 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001688 }
1689out:
1690 return r;
1691}
1692
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001693#ifdef CONFIG_COMPAT
1694struct compat_kvm_dirty_log {
1695 __u32 slot;
1696 __u32 padding1;
1697 union {
1698 compat_uptr_t dirty_bitmap; /* one bit per page */
1699 __u64 padding2;
1700 };
1701};
1702
1703static long kvm_vm_compat_ioctl(struct file *filp,
1704 unsigned int ioctl, unsigned long arg)
1705{
1706 struct kvm *kvm = filp->private_data;
1707 int r;
1708
1709 if (kvm->mm != current->mm)
1710 return -EIO;
1711 switch (ioctl) {
1712 case KVM_GET_DIRTY_LOG: {
1713 struct compat_kvm_dirty_log compat_log;
1714 struct kvm_dirty_log log;
1715
1716 r = -EFAULT;
1717 if (copy_from_user(&compat_log, (void __user *)arg,
1718 sizeof(compat_log)))
1719 goto out;
1720 log.slot = compat_log.slot;
1721 log.padding1 = compat_log.padding1;
1722 log.padding2 = compat_log.padding2;
1723 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1724
1725 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1726 if (r)
1727 goto out;
1728 break;
1729 }
1730 default:
1731 r = kvm_vm_ioctl(filp, ioctl, arg);
1732 }
1733
1734out:
1735 return r;
1736}
1737#endif
1738
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001739static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001740{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001741 struct page *page[1];
1742 unsigned long addr;
1743 int npages;
1744 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001745 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001746
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001747 addr = gfn_to_hva(kvm, gfn);
1748 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001749 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001750
1751 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1752 NULL);
1753 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001754 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001755
1756 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001757 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001758}
1759
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001760static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001761 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001762};
1763
1764static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1765{
1766 vma->vm_ops = &kvm_vm_vm_ops;
1767 return 0;
1768}
1769
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001770static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001771 .release = kvm_vm_release,
1772 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02001773#ifdef CONFIG_COMPAT
1774 .compat_ioctl = kvm_vm_compat_ioctl,
1775#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001776 .mmap = kvm_vm_mmap,
1777};
1778
1779static int kvm_dev_ioctl_create_vm(void)
1780{
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001781 int fd, r;
Avi Kivityf17abe92007-02-21 19:28:04 +02001782 struct kvm *kvm;
1783
Avi Kivityf17abe92007-02-21 19:28:04 +02001784 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001785 if (IS_ERR(kvm))
1786 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09001787#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1788 r = kvm_coalesced_mmio_init(kvm);
1789 if (r < 0) {
1790 kvm_put_kvm(kvm);
1791 return r;
1792 }
1793#endif
Roland Dreier628ff7c2009-12-18 09:41:24 -08001794 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
Al Viro2030a422008-02-23 06:46:49 -05001795 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001796 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001797
Avi Kivityf17abe92007-02-21 19:28:04 +02001798 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001799}
1800
Avi Kivity1a811b62008-12-08 18:25:27 +02001801static long kvm_dev_ioctl_check_extension_generic(long arg)
1802{
1803 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001804 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001805 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02001806 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03001807#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1808 case KVM_CAP_SET_BOOT_CPU_ID:
1809#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02001810 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02001811 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001812#ifdef CONFIG_HAVE_KVM_IRQCHIP
1813 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08001814 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02001815#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001816 default:
1817 break;
1818 }
1819 return kvm_dev_ioctl_check_extension(arg);
1820}
1821
Avi Kivityf17abe92007-02-21 19:28:04 +02001822static long kvm_dev_ioctl(struct file *filp,
1823 unsigned int ioctl, unsigned long arg)
1824{
Avi Kivity07c45a32007-03-07 13:05:38 +02001825 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001826
1827 switch (ioctl) {
1828 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001829 r = -EINVAL;
1830 if (arg)
1831 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001832 r = KVM_API_VERSION;
1833 break;
1834 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001835 r = -EINVAL;
1836 if (arg)
1837 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001838 r = kvm_dev_ioctl_create_vm();
1839 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001840 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02001841 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001842 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001843 case KVM_GET_VCPU_MMAP_SIZE:
1844 r = -EINVAL;
1845 if (arg)
1846 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001847 r = PAGE_SIZE; /* struct kvm_run */
1848#ifdef CONFIG_X86
1849 r += PAGE_SIZE; /* pio data page */
1850#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001851#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1852 r += PAGE_SIZE; /* coalesced mmio ring page */
1853#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001854 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001855 case KVM_TRACE_ENABLE:
1856 case KVM_TRACE_PAUSE:
1857 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03001858 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001861 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 }
1863out:
1864 return r;
1865}
1866
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 .unlocked_ioctl = kvm_dev_ioctl,
1869 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870};
1871
1872static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001873 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874 "kvm",
1875 &kvm_chardev_ops,
1876};
1877
Avi Kivity1b6c0162007-05-24 13:03:52 +03001878static void hardware_enable(void *junk)
1879{
1880 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02001881 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03001882
Rusty Russell7f59f492008-12-07 21:25:45 +10301883 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001884 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02001885
Rusty Russell7f59f492008-12-07 21:25:45 +10301886 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02001887
1888 r = kvm_arch_hardware_enable(NULL);
1889
1890 if (r) {
1891 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1892 atomic_inc(&hardware_enable_failed);
1893 printk(KERN_INFO "kvm: enabling virtualization on "
1894 "CPU%d failed\n", cpu);
1895 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03001896}
1897
1898static void hardware_disable(void *junk)
1899{
1900 int cpu = raw_smp_processor_id();
1901
Rusty Russell7f59f492008-12-07 21:25:45 +10301902 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03001903 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10301904 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001905 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001906}
1907
Alexander Graf10474ae2009-09-15 11:37:46 +02001908static void hardware_disable_all_nolock(void)
1909{
1910 BUG_ON(!kvm_usage_count);
1911
1912 kvm_usage_count--;
1913 if (!kvm_usage_count)
1914 on_each_cpu(hardware_disable, NULL, 1);
1915}
1916
1917static void hardware_disable_all(void)
1918{
1919 spin_lock(&kvm_lock);
1920 hardware_disable_all_nolock();
1921 spin_unlock(&kvm_lock);
1922}
1923
1924static int hardware_enable_all(void)
1925{
1926 int r = 0;
1927
1928 spin_lock(&kvm_lock);
1929
1930 kvm_usage_count++;
1931 if (kvm_usage_count == 1) {
1932 atomic_set(&hardware_enable_failed, 0);
1933 on_each_cpu(hardware_enable, NULL, 1);
1934
1935 if (atomic_read(&hardware_enable_failed)) {
1936 hardware_disable_all_nolock();
1937 r = -EBUSY;
1938 }
1939 }
1940
1941 spin_unlock(&kvm_lock);
1942
1943 return r;
1944}
1945
Avi Kivity774c47f2007-02-12 00:54:47 -08001946static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1947 void *v)
1948{
1949 int cpu = (long)v;
1950
Alexander Graf10474ae2009-09-15 11:37:46 +02001951 if (!kvm_usage_count)
1952 return NOTIFY_OK;
1953
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001954 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001955 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001956 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001957 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1958 cpu);
1959 hardware_disable(NULL);
1960 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10001961 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02001962 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1963 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10001964 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08001965 break;
1966 }
1967 return NOTIFY_OK;
1968}
1969
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001970
1971asmlinkage void kvm_handle_fault_on_reboot(void)
1972{
Avi Kivityca242ac2010-09-21 19:59:43 +02001973 if (kvm_rebooting) {
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001974 /* spin while reset goes on */
Avi Kivityca242ac2010-09-21 19:59:43 +02001975 local_irq_enable();
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001976 while (true)
1977 ;
Avi Kivityca242ac2010-09-21 19:59:43 +02001978 }
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001979 /* Fault while not rebooting. We want the trace. */
1980 BUG();
1981}
1982EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1983
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001984static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001985 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001986{
Sheng Yang8e1c1812009-04-29 11:09:04 +08001987 /*
1988 * Some (well, at least mine) BIOSes hang on reboot if
1989 * in vmx root mode.
1990 *
1991 * And Intel TXT required VMX off for all cpu when system shutdown.
1992 */
1993 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
1994 kvm_rebooting = true;
1995 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001996 return NOTIFY_OK;
1997}
1998
1999static struct notifier_block kvm_reboot_notifier = {
2000 .notifier_call = kvm_reboot,
2001 .priority = 0,
2002};
2003
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002004static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002005{
2006 int i;
2007
2008 for (i = 0; i < bus->dev_count; i++) {
2009 struct kvm_io_device *pos = bus->devs[i];
2010
2011 kvm_iodevice_destructor(pos);
2012 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002013 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002014}
2015
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002016/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002017int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002018 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002019{
2020 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002021 struct kvm_io_bus *bus;
2022
2023 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002024 for (i = 0; i < bus->dev_count; i++)
2025 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2026 return 0;
2027 return -EOPNOTSUPP;
2028}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002029
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002030/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002031int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2032 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002033{
2034 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002035 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002036
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002037 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002038 for (i = 0; i < bus->dev_count; i++)
2039 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2040 return 0;
2041 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002042}
2043
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002044/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002045int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2046 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002047{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002048 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002049
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002050 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002051 if (bus->dev_count > NR_IOBUS_DEVS-1)
2052 return -ENOSPC;
2053
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002054 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2055 if (!new_bus)
2056 return -ENOMEM;
2057 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2058 new_bus->devs[new_bus->dev_count++] = dev;
2059 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2060 synchronize_srcu_expedited(&kvm->srcu);
2061 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002062
2063 return 0;
2064}
2065
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002066/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002067int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2068 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002069{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002070 int i, r;
2071 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002072
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002073 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2074 if (!new_bus)
2075 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002076
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002077 bus = kvm->buses[bus_idx];
2078 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2079
2080 r = -ENOENT;
2081 for (i = 0; i < new_bus->dev_count; i++)
2082 if (new_bus->devs[i] == dev) {
2083 r = 0;
2084 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002085 break;
2086 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002087
2088 if (r) {
2089 kfree(new_bus);
2090 return r;
2091 }
2092
2093 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2094 synchronize_srcu_expedited(&kvm->srcu);
2095 kfree(bus);
2096 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002097}
2098
Avi Kivity774c47f2007-02-12 00:54:47 -08002099static struct notifier_block kvm_cpu_notifier = {
2100 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002101};
2102
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002103static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002104{
2105 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002106 struct kvm *kvm;
2107
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002108 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002109 spin_lock(&kvm_lock);
2110 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002111 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002112 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002113 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002114}
2115
2116DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2117
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002118static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002119{
2120 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002121 struct kvm *kvm;
2122 struct kvm_vcpu *vcpu;
2123 int i;
2124
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002125 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002126 spin_lock(&kvm_lock);
2127 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002128 kvm_for_each_vcpu(i, vcpu, kvm)
2129 *val += *(u32 *)((void *)vcpu + offset);
2130
Avi Kivity1165f5f2007-04-19 17:27:43 +03002131 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002132 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002133}
2134
Avi Kivityba1389b2007-11-18 16:24:12 +02002135DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2136
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002137static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002138 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2139 [KVM_STAT_VM] = &vm_stat_fops,
2140};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002141
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002142static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143{
2144 struct kvm_stats_debugfs_item *p;
2145
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002146 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002148 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002149 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002150 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151}
2152
2153static void kvm_exit_debug(void)
2154{
2155 struct kvm_stats_debugfs_item *p;
2156
2157 for (p = debugfs_entries; p->name; ++p)
2158 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002159 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160}
2161
Avi Kivity59ae6c62007-02-12 00:54:48 -08002162static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2163{
Alexander Graf10474ae2009-09-15 11:37:46 +02002164 if (kvm_usage_count)
2165 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002166 return 0;
2167}
2168
2169static int kvm_resume(struct sys_device *dev)
2170{
Alexander Graf10474ae2009-09-15 11:37:46 +02002171 if (kvm_usage_count)
2172 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002173 return 0;
2174}
2175
2176static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002177 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002178 .suspend = kvm_suspend,
2179 .resume = kvm_resume,
2180};
2181
2182static struct sys_device kvm_sysdev = {
2183 .id = 0,
2184 .cls = &kvm_sysdev_class,
2185};
2186
Izik Eiduscea7bb22007-10-17 19:17:48 +02002187struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002188pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189
Avi Kivity15ad7142007-07-11 18:17:21 +03002190static inline
2191struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2192{
2193 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2194}
2195
2196static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2197{
2198 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2199
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002200 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002201}
2202
2203static void kvm_sched_out(struct preempt_notifier *pn,
2204 struct task_struct *next)
2205{
2206 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2207
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002208 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002209}
2210
Avi Kivity0ee75be2010-04-28 15:39:01 +03002211int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002212 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213{
2214 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002215 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002217 r = kvm_arch_init(opaque);
2218 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002219 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002220
2221 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2222
2223 if (bad_page == NULL) {
2224 r = -ENOMEM;
2225 goto out;
2226 }
2227
Anthony Liguori35149e22008-04-02 14:46:56 -05002228 bad_pfn = page_to_pfn(bad_page);
2229
Huang Yingbf998152010-05-31 14:28:19 +08002230 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2231
2232 if (hwpoison_page == NULL) {
2233 r = -ENOMEM;
2234 goto out_free_0;
2235 }
2236
2237 hwpoison_pfn = page_to_pfn(hwpoison_page);
2238
Gleb Natapovedba23e2010-07-07 20:16:45 +03002239 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2240
2241 if (fault_page == NULL) {
2242 r = -ENOMEM;
2243 goto out_free_0;
2244 }
2245
2246 fault_pfn = page_to_pfn(fault_page);
2247
Avi Kivity8437a6172009-06-06 14:52:35 -07002248 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302249 r = -ENOMEM;
2250 goto out_free_0;
2251 }
2252
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002253 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302255 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256
Yang, Sheng002c7f72007-07-31 14:23:01 +03002257 for_each_online_cpu(cpu) {
2258 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002259 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002260 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002261 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002262 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002263 }
2264
Avi Kivity774c47f2007-02-12 00:54:47 -08002265 r = register_cpu_notifier(&kvm_cpu_notifier);
2266 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002267 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 register_reboot_notifier(&kvm_reboot_notifier);
2269
Avi Kivity59ae6c62007-02-12 00:54:48 -08002270 r = sysdev_class_register(&kvm_sysdev_class);
2271 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002272 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002273
2274 r = sysdev_register(&kvm_sysdev);
2275 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002276 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002277
Rusty Russellc16f8622007-07-30 21:12:19 +10002278 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002279 if (!vcpu_align)
2280 vcpu_align = __alignof__(struct kvm_vcpu);
2281 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002282 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002283 if (!kvm_vcpu_cache) {
2284 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002285 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002286 }
2287
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002289 kvm_vm_fops.owner = module;
2290 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291
2292 r = misc_register(&kvm_dev);
2293 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002294 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 goto out_free;
2296 }
2297
Avi Kivity15ad7142007-07-11 18:17:21 +03002298 kvm_preempt_ops.sched_in = kvm_sched_in;
2299 kvm_preempt_ops.sched_out = kvm_sched_out;
2300
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002301 kvm_init_debug();
2302
Avi Kivityc7addb92007-09-16 18:58:32 +02002303 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304
2305out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002306 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002307out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002308 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002309out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002310 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002311out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002313 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002314out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002315out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002316 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302317out_free_0a:
2318 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002319out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002320 if (fault_page)
2321 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002322 if (hwpoison_page)
2323 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002324 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002325out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002326 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002327out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328 return r;
2329}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002330EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002332void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002334 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002336 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002337 sysdev_unregister(&kvm_sysdev);
2338 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002340 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002341 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002342 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002343 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302344 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002345 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002346 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002348EXPORT_SYMBOL_GPL(kvm_exit);