blob: aefdda390f5e73f297897db863da1ee9e129a6a8 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Hollis Blancharde2174022007-12-03 15:30:24 -060019#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/percpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +010033#include <linux/syscore_ops.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080044#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030045#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020046#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020047#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010048#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Avi Kivitye4956062007-06-28 14:15:57 -040051#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040052#include <asm/io.h>
53#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020054#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Laurent Vivier5f94c172008-05-30 16:05:54 +020056#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020057#include "async_pf.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020058
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059#define CREATE_TRACE_POINTS
60#include <trace/events/kvm.h>
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Marcelo Tosattifa40a822009-06-04 15:08:24 -030065/*
66 * Ordering of locks:
67 *
Sheng Yangfae3a352009-12-15 10:28:07 +080068 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030069 */
70
Jan Kiszkae935b832011-02-08 12:55:33 +010071DEFINE_RAW_SPINLOCK(kvm_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080072LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080073
Rusty Russell7f59f492008-12-07 21:25:45 +103074static cpumask_var_t cpus_hardware_enabled;
Alexander Graf10474ae2009-09-15 11:37:46 +020075static int kvm_usage_count = 0;
76static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +030077
Rusty Russellc16f8622007-07-30 21:12:19 +100078struct kmem_cache *kvm_vcpu_cache;
79EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030080
Avi Kivity15ad7142007-07-11 18:17:21 +030081static __read_mostly struct preempt_ops kvm_preempt_ops;
82
Hollis Blanchard76f7c872008-04-15 16:05:42 -050083struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Avi Kivitybccf2152007-02-21 18:04:26 +020085static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
86 unsigned long arg);
Alexander Graf1dda6062011-06-08 02:45:37 +020087#ifdef CONFIG_COMPAT
88static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
89 unsigned long arg);
90#endif
Alexander Graf10474ae2009-09-15 11:37:46 +020091static int hardware_enable_all(void);
92static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +020093
Marcelo Tosattie93f8a02009-12-23 14:35:24 -020094static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
95
Avi Kivityb7c41452010-12-02 17:52:50 +020096bool kvm_rebooting;
97EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +030098
Marcelo Tosatti54dee992009-06-11 12:07:44 -030099static bool largepages_enabled = true;
100
Gleb Natapovfa7bff82010-07-07 20:16:44 +0300101static struct page *hwpoison_page;
102static pfn_t hwpoison_pfn;
Huang Yingbf998152010-05-31 14:28:19 +0800103
Xiao Guangrongfce92dc2011-07-12 03:28:54 +0800104struct page *fault_page;
105pfn_t fault_pfn;
Gleb Natapovedba23e2010-07-07 20:16:45 +0300106
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800107inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300108{
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100109 if (pfn_valid(pfn)) {
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800110 int reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800111 struct page *tail = pfn_to_page(pfn);
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800112 struct page *head = compound_trans_head(tail);
113 reserved = PageReserved(head);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800114 if (head != tail) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800115 /*
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800116 * "head" is not a dangling pointer
117 * (compound_trans_head takes care of that)
118 * but the hugepage may have been splitted
119 * from under us (and we may not hold a
120 * reference count on the head page so it can
121 * be reused before we run PageReferenced), so
122 * we've to check PageTail before returning
123 * what we just read.
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800124 */
Andrea Arcangeli22e5c472011-01-13 15:47:20 -0800125 smp_rmb();
126 if (PageTail(tail))
127 return reserved;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800128 }
129 return PageReserved(tail);
Joerg Roedelfc5659c2009-02-18 14:08:58 +0100130 }
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300131
132 return true;
133}
134
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135/*
136 * Switches to specified vcpu, until a matching vcpu_put()
137 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200138void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139{
Avi Kivity15ad7142007-07-11 18:17:21 +0300140 int cpu;
141
Avi Kivitybccf2152007-02-21 18:04:26 +0200142 mutex_lock(&vcpu->mutex);
Rik van Riel34bb10b2011-02-01 09:52:41 -0500143 if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
144 /* The thread running this VCPU changed. */
145 struct pid *oldpid = vcpu->pid;
146 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
147 rcu_assign_pointer(vcpu->pid, newpid);
148 synchronize_rcu();
149 put_pid(oldpid);
150 }
Avi Kivity15ad7142007-07-11 18:17:21 +0300151 cpu = get_cpu();
152 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200153 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300154 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200155}
156
Carsten Otte313a3dc2007-10-11 19:16:52 +0200157void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158{
Avi Kivity15ad7142007-07-11 18:17:21 +0300159 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200160 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300161 preempt_notifier_unregister(&vcpu->preempt_notifier);
162 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163 mutex_unlock(&vcpu->mutex);
164}
165
Avi Kivityd9e368d2007-06-07 19:18:30 +0300166static void ack_flush(void *_completed)
167{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300168}
169
Rusty Russell49846892008-12-08 20:26:24 +1030170static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300171{
Avi Kivity597a5f52008-07-20 14:24:22 +0300172 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030173 cpumask_var_t cpus;
174 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300175 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300176
Li Zefan79f55992009-06-15 14:58:26 +0800177 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030178
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800179 me = get_cpu();
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300180 kvm_for_each_vcpu(i, vcpu, kvm) {
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800181 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300182 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800183
184 /* Set ->requests bit before we read ->mode */
185 smp_mb();
186
187 if (cpus != NULL && cpu != -1 && cpu != me &&
188 kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030189 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300190 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030191 if (unlikely(cpus == NULL))
192 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
193 else if (!cpumask_empty(cpus))
194 smp_call_function_many(cpus, ack_flush, NULL, 1);
195 else
196 called = false;
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800197 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030198 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030199 return called;
200}
201
202void kvm_flush_remote_tlbs(struct kvm *kvm)
203{
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800204 int dirty_count = kvm->tlbs_dirty;
205
206 smp_mb();
Rusty Russell49846892008-12-08 20:26:24 +1030207 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
208 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800209 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210}
211
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500212void kvm_reload_remote_mmus(struct kvm *kvm)
213{
Rusty Russell49846892008-12-08 20:26:24 +1030214 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500215}
216
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000217int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
218{
219 struct page *page;
220 int r;
221
222 mutex_init(&vcpu->mutex);
223 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000224 vcpu->kvm = kvm;
225 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500226 vcpu->pid = NULL;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300227 init_waitqueue_head(&vcpu->wq);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200228 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000229
230 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
231 if (!page) {
232 r = -ENOMEM;
233 goto fail;
234 }
235 vcpu->run = page_address(page);
236
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800237 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000238 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800239 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000240 return 0;
241
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000242fail_free_run:
243 free_page((unsigned long)vcpu->run);
244fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000245 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000246}
247EXPORT_SYMBOL_GPL(kvm_vcpu_init);
248
249void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
250{
Rik van Riel34bb10b2011-02-01 09:52:41 -0500251 put_pid(vcpu->pid);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800252 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000253 free_page((unsigned long)vcpu->run);
254}
255EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
256
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200257#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
258static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
259{
260 return container_of(mn, struct kvm, mmu_notifier);
261}
262
263static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
264 struct mm_struct *mm,
265 unsigned long address)
266{
267 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200268 int need_tlb_flush, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200269
270 /*
271 * When ->invalidate_page runs, the linux pte has been zapped
272 * already but the page is still allocated until
273 * ->invalidate_page returns. So if we increase the sequence
274 * here the kvm page fault will notice if the spte can't be
275 * established because the page is going to be freed. If
276 * instead the kvm page fault establishes the spte before
277 * ->invalidate_page runs, kvm_unmap_hva will release it
278 * before returning.
279 *
280 * The sequence increase only need to be seen at spin_unlock
281 * time, and not at spin_lock time.
282 *
283 * Increasing the sequence after the spin_unlock would be
284 * unsafe because the kvm page fault could then establish the
285 * pte after kvm_unmap_hva returned, without noticing the page
286 * is going to be freed.
287 */
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200288 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200289 spin_lock(&kvm->mmu_lock);
290 kvm->mmu_notifier_seq++;
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800291 need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200292 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200293 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200294
295 /* we've to flush the tlb before the pages can be freed */
296 if (need_tlb_flush)
297 kvm_flush_remote_tlbs(kvm);
298
299}
300
Izik Eidus3da0dd42009-09-23 21:47:18 +0300301static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
302 struct mm_struct *mm,
303 unsigned long address,
304 pte_t pte)
305{
306 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200307 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300308
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200309 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300310 spin_lock(&kvm->mmu_lock);
311 kvm->mmu_notifier_seq++;
312 kvm_set_spte_hva(kvm, address, pte);
313 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200314 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300315}
316
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200317static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
318 struct mm_struct *mm,
319 unsigned long start,
320 unsigned long end)
321{
322 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200323 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200324
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200325 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200326 spin_lock(&kvm->mmu_lock);
327 /*
328 * The count increase must become visible at unlock time as no
329 * spte can be established without taking the mmu_lock and
330 * count is also read inside the mmu_lock critical section.
331 */
332 kvm->mmu_notifier_count++;
333 for (; start < end; start += PAGE_SIZE)
334 need_tlb_flush |= kvm_unmap_hva(kvm, start);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800335 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200336 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200337 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200338
339 /* we've to flush the tlb before the pages can be freed */
340 if (need_tlb_flush)
341 kvm_flush_remote_tlbs(kvm);
342}
343
344static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
345 struct mm_struct *mm,
346 unsigned long start,
347 unsigned long end)
348{
349 struct kvm *kvm = mmu_notifier_to_kvm(mn);
350
351 spin_lock(&kvm->mmu_lock);
352 /*
353 * This sequence increase will notify the kvm page fault that
354 * the page that is going to be mapped in the spte could have
355 * been freed.
356 */
357 kvm->mmu_notifier_seq++;
358 /*
359 * The above sequence increase must be visible before the
360 * below count decrease but both values are read by the kvm
361 * page fault under mmu_lock spinlock so we don't need to add
362 * a smb_wmb() here in between the two.
363 */
364 kvm->mmu_notifier_count--;
365 spin_unlock(&kvm->mmu_lock);
366
367 BUG_ON(kvm->mmu_notifier_count < 0);
368}
369
370static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
371 struct mm_struct *mm,
372 unsigned long address)
373{
374 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200375 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200376
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200377 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200378 spin_lock(&kvm->mmu_lock);
379 young = kvm_age_hva(kvm, address);
380 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200381 srcu_read_unlock(&kvm->srcu, idx);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200382
383 if (young)
384 kvm_flush_remote_tlbs(kvm);
385
386 return young;
387}
388
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800389static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
390 struct mm_struct *mm,
391 unsigned long address)
392{
393 struct kvm *kvm = mmu_notifier_to_kvm(mn);
394 int young, idx;
395
396 idx = srcu_read_lock(&kvm->srcu);
397 spin_lock(&kvm->mmu_lock);
398 young = kvm_test_age_hva(kvm, address);
399 spin_unlock(&kvm->mmu_lock);
400 srcu_read_unlock(&kvm->srcu, idx);
401
402 return young;
403}
404
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100405static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
406 struct mm_struct *mm)
407{
408 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800409 int idx;
410
411 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100412 kvm_arch_flush_shadow(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800413 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100414}
415
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200416static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
417 .invalidate_page = kvm_mmu_notifier_invalidate_page,
418 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
419 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
420 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800421 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300422 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100423 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200424};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200425
426static int kvm_init_mmu_notifier(struct kvm *kvm)
427{
428 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
429 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
430}
431
432#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
433
434static int kvm_init_mmu_notifier(struct kvm *kvm)
435{
436 return 0;
437}
438
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200439#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
440
Avi Kivityf17abe92007-02-21 19:28:04 +0200441static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100443 int r, i;
444 struct kvm *kvm = kvm_arch_alloc_vm();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100446 if (!kvm)
447 return ERR_PTR(-ENOMEM);
448
449 r = kvm_arch_init_vm(kvm);
450 if (r)
451 goto out_err_nodisable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200452
453 r = hardware_enable_all();
454 if (r)
455 goto out_err_nodisable;
456
Avi Kivity75858a82009-01-04 17:10:50 +0200457#ifdef CONFIG_HAVE_KVM_IRQCHIP
458 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300459 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200460#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200462 r = -ENOMEM;
463 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
464 if (!kvm->memslots)
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100465 goto out_err_nosrcu;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200466 if (init_srcu_struct(&kvm->srcu))
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100467 goto out_err_nosrcu;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200468 for (i = 0; i < KVM_NR_BUSES; i++) {
469 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
470 GFP_KERNEL);
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100471 if (!kvm->buses[i])
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200472 goto out_err;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200473 }
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700474
OGAWA Hirofumi85722cd2011-05-11 09:28:28 +0900475 spin_lock_init(&kvm->mmu_lock);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200476 kvm->mm = current->mm;
477 atomic_inc(&kvm->mm->mm_count);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400478 kvm_eventfd_init(kvm);
Shaohua Li11ec2802007-07-23 14:51:37 +0800479 mutex_init(&kvm->lock);
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300480 mutex_init(&kvm->irq_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200481 mutex_init(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300482 atomic_set(&kvm->users_count, 1);
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700483
484 r = kvm_init_mmu_notifier(kvm);
485 if (r)
486 goto out_err;
487
Jan Kiszkae935b832011-02-08 12:55:33 +0100488 raw_spin_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000489 list_add(&kvm->vm_list, &vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100490 raw_spin_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100491
Avi Kivityf17abe92007-02-21 19:28:04 +0200492 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200493
494out_err:
Jan Kiszka57e7fbe2010-11-09 12:42:12 +0100495 cleanup_srcu_struct(&kvm->srcu);
496out_err_nosrcu:
Alexander Graf10474ae2009-09-15 11:37:46 +0200497 hardware_disable_all();
498out_err_nodisable:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200499 for (i = 0; i < KVM_NR_BUSES; i++)
500 kfree(kvm->buses[i]);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200501 kfree(kvm->memslots);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100502 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200503 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200504}
505
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900506static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
507{
508 if (!memslot->dirty_bitmap)
509 return;
510
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900511 if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
512 vfree(memslot->dirty_bitmap_head);
513 else
514 kfree(memslot->dirty_bitmap_head);
515
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900516 memslot->dirty_bitmap = NULL;
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900517 memslot->dirty_bitmap_head = NULL;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900518}
519
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520/*
521 * Free any memory in @free but not in @dont.
522 */
523static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
524 struct kvm_memory_slot *dont)
525{
Joerg Roedelec04b262009-06-19 15:16:23 +0200526 int i;
527
Izik Eidus290fc382007-09-27 14:11:22 +0200528 if (!dont || free->rmap != dont->rmap)
529 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530
531 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900532 kvm_destroy_dirty_bitmap(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533
Joerg Roedelec04b262009-06-19 15:16:23 +0200534
535 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
536 if (!dont || free->lpage_info[i] != dont->lpage_info[i]) {
537 vfree(free->lpage_info[i]);
538 free->lpage_info[i] = NULL;
539 }
540 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300541
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 free->npages = 0;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500543 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544}
545
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800546void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547{
548 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200549 struct kvm_memslots *slots = kvm->memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200551 for (i = 0; i < slots->nmemslots; ++i)
552 kvm_free_physmem_slot(&slots->memslots[i], NULL);
553
554 kfree(kvm->memslots);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555}
556
Avi Kivityf17abe92007-02-21 19:28:04 +0200557static void kvm_destroy_vm(struct kvm *kvm)
558{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200559 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200560 struct mm_struct *mm = kvm->mm;
561
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800562 kvm_arch_sync_events(kvm);
Jan Kiszkae935b832011-02-08 12:55:33 +0100563 raw_spin_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800564 list_del(&kvm->vm_list);
Jan Kiszkae935b832011-02-08 12:55:33 +0100565 raw_spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200566 kvm_free_irq_routing(kvm);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200567 for (i = 0; i < KVM_NR_BUSES; i++)
568 kvm_io_bus_destroy(kvm->buses[i]);
Avi Kivity980da6c2009-12-20 15:13:43 +0200569 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200570#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
571 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200572#else
573 kvm_arch_flush_shadow(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200574#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800575 kvm_arch_destroy_vm(kvm);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100576 kvm_free_physmem(kvm);
577 cleanup_srcu_struct(&kvm->srcu);
578 kvm_arch_free_vm(kvm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200579 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200580 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200581}
582
Izik Eidusd39f13b2008-03-30 16:01:25 +0300583void kvm_get_kvm(struct kvm *kvm)
584{
585 atomic_inc(&kvm->users_count);
586}
587EXPORT_SYMBOL_GPL(kvm_get_kvm);
588
589void kvm_put_kvm(struct kvm *kvm)
590{
591 if (atomic_dec_and_test(&kvm->users_count))
592 kvm_destroy_vm(kvm);
593}
594EXPORT_SYMBOL_GPL(kvm_put_kvm);
595
596
Avi Kivityf17abe92007-02-21 19:28:04 +0200597static int kvm_vm_release(struct inode *inode, struct file *filp)
598{
599 struct kvm *kvm = filp->private_data;
600
Gregory Haskins721eecb2009-05-20 10:30:49 -0400601 kvm_irqfd_release(kvm);
602
Izik Eidusd39f13b2008-03-30 16:01:25 +0300603 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604 return 0;
605}
606
Heiko Carstensd48ead82011-01-17 21:21:08 +0100607#ifndef CONFIG_S390
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900608/*
609 * Allocation size is twice as large as the actual dirty bitmap size.
610 * This makes it possible to do double buffering: see x86's
611 * kvm_vm_ioctl_get_dirty_log().
612 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900613static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
614{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900615 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900616
Takuya Yoshikawa6f9e5c172010-11-01 14:36:09 +0900617 if (dirty_bytes > PAGE_SIZE)
618 memslot->dirty_bitmap = vzalloc(dirty_bytes);
619 else
620 memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
621
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900622 if (!memslot->dirty_bitmap)
623 return -ENOMEM;
624
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900625 memslot->dirty_bitmap_head = memslot->dirty_bitmap;
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900626 return 0;
627}
Heiko Carstensd48ead82011-01-17 21:21:08 +0100628#endif /* !CONFIG_S390 */
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900629
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 * Allocate some memory and give it an address in the guest physical address
632 * space.
633 *
634 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800635 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500636 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800638int __kvm_set_memory_region(struct kvm *kvm,
639 struct kvm_userspace_memory_region *mem,
640 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641{
Avi Kivity8234b222010-12-27 12:08:45 +0200642 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 gfn_t base_gfn;
Heiko Carstens28bcb112009-09-03 17:35:35 +0200644 unsigned long npages;
645 unsigned long i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 struct kvm_memory_slot *memslot;
647 struct kvm_memory_slot old, new;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200648 struct kvm_memslots *slots, *old_memslots;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649
650 r = -EINVAL;
651 /* General sanity checks */
652 if (mem->memory_size & (PAGE_SIZE - 1))
653 goto out;
654 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
655 goto out;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +0900656 /* We can read the guest memory with __xxx_user() later on. */
657 if (user_alloc &&
658 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Heiko Carstens9e3bb6b2011-05-24 07:51:27 +0200659 !access_ok(VERIFY_WRITE,
660 (void __user *)(unsigned long)mem->userspace_addr,
661 mem->memory_size)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600662 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200663 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 goto out;
665 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
666 goto out;
667
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200668 memslot = &kvm->memslots->memslots[mem->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
670 npages = mem->memory_size >> PAGE_SHIFT;
671
Takuya Yoshikawa660c22c2010-04-13 22:47:24 +0900672 r = -EINVAL;
673 if (npages > KVM_MEM_MAX_NR_PAGES)
674 goto out;
675
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 if (!npages)
677 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
678
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679 new = old = *memslot;
680
Avi Kivitye36d96f2010-06-21 10:56:36 +0300681 new.id = mem->slot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682 new.base_gfn = base_gfn;
683 new.npages = npages;
684 new.flags = mem->flags;
685
686 /* Disallow changing a memory slot's size. */
687 r = -EINVAL;
688 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800689 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690
691 /* Check for overlaps */
692 r = -EEXIST;
693 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200694 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695
Jan Kiszka4cd481f2009-04-13 11:59:32 +0200696 if (s == memslot || !s->npages)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697 continue;
698 if (!((base_gfn + npages <= s->base_gfn) ||
699 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800700 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703 /* Free page dirty bitmap if unneeded */
704 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000705 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
707 r = -ENOMEM;
708
709 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200710#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500711 if (npages && !new.rmap) {
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900712 new.rmap = vzalloc(npages * sizeof(*new.rmap));
Izik Eidus290fc382007-09-27 14:11:22 +0200713
714 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800715 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200716
Izik Eidus80b14b52007-10-25 11:54:04 +0200717 new.user_alloc = user_alloc;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200718 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 }
Joerg Roedelec04b262009-06-19 15:16:23 +0200720 if (!npages)
721 goto skip_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300722
Joerg Roedelec04b262009-06-19 15:16:23 +0200723 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
Heiko Carstens28bcb112009-09-03 17:35:35 +0200724 unsigned long ugfn;
725 unsigned long j;
726 int lpages;
Joerg Roedelec04b262009-06-19 15:16:23 +0200727 int level = i + 2;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300728
Joerg Roedelec04b262009-06-19 15:16:23 +0200729 /* Avoid unused variable warning if no large pages */
730 (void)level;
731
732 if (new.lpage_info[i])
733 continue;
734
Joerg Roedel82855412010-07-01 16:00:11 +0200735 lpages = 1 + ((base_gfn + npages - 1)
736 >> KVM_HPAGE_GFN_SHIFT(level));
737 lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
Joerg Roedelec04b262009-06-19 15:16:23 +0200738
Takuya Yoshikawa26535032010-11-02 10:49:34 +0900739 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
Joerg Roedelec04b262009-06-19 15:16:23 +0200740
741 if (!new.lpage_info[i])
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300742 goto out_free;
743
Joerg Roedel82855412010-07-01 16:00:11 +0200744 if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200745 new.lpage_info[i][0].write_count = 1;
Joerg Roedel82855412010-07-01 16:00:11 +0200746 if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Joerg Roedelec04b262009-06-19 15:16:23 +0200747 new.lpage_info[i][lpages - 1].write_count = 1;
Avi Kivityac045272009-06-08 15:52:39 +0300748 ugfn = new.userspace_addr >> PAGE_SHIFT;
749 /*
750 * If the gfn and userspace address are not aligned wrt each
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300751 * other, or if explicitly asked to, disable large page
752 * support for this slot
Avi Kivityac045272009-06-08 15:52:39 +0300753 */
Joerg Roedelec04b262009-06-19 15:16:23 +0200754 if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300755 !largepages_enabled)
Joerg Roedelec04b262009-06-19 15:16:23 +0200756 for (j = 0; j < lpages; ++j)
757 new.lpage_info[i][j].write_count = 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300758 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759
Joerg Roedelec04b262009-06-19 15:16:23 +0200760skip_lpage:
761
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 /* Allocate page dirty bitmap if needed */
763 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900764 if (kvm_create_dirty_bitmap(&new) < 0)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800765 goto out_free;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200766 /* destroy any largepage mappings for dirty tracking */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 }
Christian Borntraeger3eea8432009-06-23 17:24:06 +0200768#else /* not defined CONFIG_S390 */
769 new.user_alloc = user_alloc;
770 if (user_alloc)
771 new.userspace_addr = mem->userspace_addr;
Carsten Otteeff01142008-06-27 15:05:31 +0200772#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200774 if (!npages) {
775 r = -ENOMEM;
776 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
777 if (!slots)
778 goto out_free;
779 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
780 if (mem->slot >= slots->nmemslots)
781 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200782 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200783 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
784
785 old_memslots = kvm->memslots;
786 rcu_assign_pointer(kvm->memslots, slots);
787 synchronize_srcu_expedited(&kvm->srcu);
788 /* From this point no new shadow pages pointing to a deleted
789 * memslot will be created.
790 *
791 * validation of sp->gfn happens in:
792 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
793 * - kvm_is_visible_gfn (mmu_check_roots)
794 */
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300795 kvm_arch_flush_shadow(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200796 kfree(old_memslots);
797 }
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300798
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200799 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
800 if (r)
801 goto out_free;
802
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200803 /* map the pages in iommu page table */
804 if (npages) {
805 r = kvm_iommu_map_pages(kvm, &new);
806 if (r)
807 goto out_free;
808 }
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200809
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200810 r = -ENOMEM;
811 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
812 if (!slots)
813 goto out_free;
814 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
815 if (mem->slot >= slots->nmemslots)
816 slots->nmemslots = mem->slot + 1;
Gleb Natapov49c77542010-10-18 15:22:23 +0200817 slots->generation++;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200818
819 /* actual memory is freed via old in kvm_free_physmem_slot below */
820 if (!npages) {
821 new.rmap = NULL;
822 new.dirty_bitmap = NULL;
823 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
824 new.lpage_info[i] = NULL;
825 }
826
827 slots->memslots[mem->slot] = new;
828 old_memslots = kvm->memslots;
829 rcu_assign_pointer(kvm->memslots, slots);
830 synchronize_srcu_expedited(&kvm->srcu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200832 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800833
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800834 /*
835 * If the new memory slot is created, we need to clear all
836 * mmio sptes.
837 */
838 if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
839 kvm_arch_flush_shadow(kvm);
840
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200841 kvm_free_physmem_slot(&old, &new);
842 kfree(old_memslots);
843
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 return 0;
845
Sheng Yangf78e0e22007-10-29 09:40:42 +0800846out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 kvm_free_physmem_slot(&new, &old);
848out:
849 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200850
851}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800852EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
853
854int kvm_set_memory_region(struct kvm *kvm,
855 struct kvm_userspace_memory_region *mem,
856 int user_alloc)
857{
858 int r;
859
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200860 mutex_lock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800861 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200862 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800863 return r;
864}
Izik Eidus210c7c42007-10-24 23:52:57 +0200865EXPORT_SYMBOL_GPL(kvm_set_memory_region);
866
Carsten Otte1fe779f2007-10-29 16:08:35 +0100867int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
868 struct
869 kvm_userspace_memory_region *mem,
870 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200871{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200872 if (mem->slot >= KVM_MEMORY_SLOTS)
873 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200874 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875}
876
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800877int kvm_get_dirty_log(struct kvm *kvm,
878 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879{
880 struct kvm_memory_slot *memslot;
881 int r, i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900882 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 unsigned long any = 0;
884
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 r = -EINVAL;
886 if (log->slot >= KVM_MEMORY_SLOTS)
887 goto out;
888
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200889 memslot = &kvm->memslots->memslots[log->slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 r = -ENOENT;
891 if (!memslot->dirty_bitmap)
892 goto out;
893
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +0900894 n = kvm_dirty_bitmap_bytes(memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895
Uri Lublincd1a4a92007-02-22 16:43:09 +0200896 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 any = memslot->dirty_bitmap[i];
898
899 r = -EFAULT;
900 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
901 goto out;
902
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800903 if (any)
904 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905
906 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 return r;
909}
910
Marcelo Tosatti54dee992009-06-11 12:07:44 -0300911void kvm_disable_largepages(void)
912{
913 largepages_enabled = false;
914}
915EXPORT_SYMBOL_GPL(kvm_disable_largepages);
916
Izik Eiduscea7bb22007-10-17 19:17:48 +0200917int is_error_page(struct page *page)
918{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300919 return page == bad_page || page == hwpoison_page || page == fault_page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200920}
921EXPORT_SYMBOL_GPL(is_error_page);
922
Anthony Liguori35149e22008-04-02 14:46:56 -0500923int is_error_pfn(pfn_t pfn)
924{
Gleb Natapovedba23e2010-07-07 20:16:45 +0300925 return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -0500926}
927EXPORT_SYMBOL_GPL(is_error_pfn);
928
Huang Yingbf998152010-05-31 14:28:19 +0800929int is_hwpoison_pfn(pfn_t pfn)
930{
931 return pfn == hwpoison_pfn;
932}
933EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
934
Gleb Natapovedba23e2010-07-07 20:16:45 +0300935int is_fault_pfn(pfn_t pfn)
936{
937 return pfn == fault_pfn;
938}
939EXPORT_SYMBOL_GPL(is_fault_pfn);
940
Xiao Guangrongfce92dc2011-07-12 03:28:54 +0800941int is_noslot_pfn(pfn_t pfn)
942{
943 return pfn == bad_pfn;
944}
945EXPORT_SYMBOL_GPL(is_noslot_pfn);
946
947int is_invalid_pfn(pfn_t pfn)
948{
949 return pfn == hwpoison_pfn || pfn == fault_pfn;
950}
951EXPORT_SYMBOL_GPL(is_invalid_pfn);
952
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200953static inline unsigned long bad_hva(void)
954{
955 return PAGE_OFFSET;
956}
957
958int kvm_is_error_hva(unsigned long addr)
959{
960 return addr == bad_hva();
961}
962EXPORT_SYMBOL_GPL(kvm_is_error_hva);
963
Gleb Natapov49c77542010-10-18 15:22:23 +0200964static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
965 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966{
967 int i;
968
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200969 for (i = 0; i < slots->nmemslots; ++i) {
970 struct kvm_memory_slot *memslot = &slots->memslots[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
972 if (gfn >= memslot->base_gfn
973 && gfn < memslot->base_gfn + memslot->npages)
974 return memslot;
975 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000976 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977}
Gleb Natapov49c77542010-10-18 15:22:23 +0200978
979struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
980{
981 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
982}
Avi Kivitya1f4d392010-06-21 11:44:20 +0300983EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Izik Eiduse0d62c72007-10-24 23:57:46 +0200985int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
986{
987 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800988 struct kvm_memslots *slots = kvm_memslots(kvm);
Izik Eiduse0d62c72007-10-24 23:57:46 +0200989
Izik Eiduse0d62c72007-10-24 23:57:46 +0200990 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200991 struct kvm_memory_slot *memslot = &slots->memslots[i];
Izik Eiduse0d62c72007-10-24 23:57:46 +0200992
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200993 if (memslot->flags & KVM_MEMSLOT_INVALID)
994 continue;
995
Izik Eiduse0d62c72007-10-24 23:57:46 +0200996 if (gfn >= memslot->base_gfn
997 && gfn < memslot->base_gfn + memslot->npages)
998 return 1;
999 }
1000 return 0;
1001}
1002EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1003
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001004unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
1005{
1006 struct vm_area_struct *vma;
1007 unsigned long addr, size;
1008
1009 size = PAGE_SIZE;
1010
1011 addr = gfn_to_hva(kvm, gfn);
1012 if (kvm_is_error_hva(addr))
1013 return PAGE_SIZE;
1014
1015 down_read(&current->mm->mmap_sem);
1016 vma = find_vma(current->mm, addr);
1017 if (!vma)
1018 goto out;
1019
1020 size = vma_kernel_pagesize(vma);
1021
1022out:
1023 up_read(&current->mm->mmap_sem);
1024
1025 return size;
1026}
1027
Gleb Natapov49c77542010-10-18 15:22:23 +02001028static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
Xiao Guangrong48987782010-08-22 19:11:43 +08001029 gfn_t *nr_pages)
Izik Eidus539cb662007-11-11 22:05:04 +02001030{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001031 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Izik Eidus539cb662007-11-11 22:05:04 +02001032 return bad_hva();
Xiao Guangrong48987782010-08-22 19:11:43 +08001033
1034 if (nr_pages)
1035 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1036
Takuya Yoshikawaf5c98032010-02-25 11:33:19 +09001037 return gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001038}
Xiao Guangrong48987782010-08-22 19:11:43 +08001039
1040unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1041{
Gleb Natapov49c77542010-10-18 15:22:23 +02001042 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001043}
Sheng Yang0d150292008-04-25 21:44:50 +08001044EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001045
Gleb Natapov80300892010-10-19 18:13:41 +02001046static pfn_t get_fault_pfn(void)
1047{
1048 get_page(fault_page);
1049 return fault_pfn;
1050}
1051
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001052int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1053 unsigned long start, int write, struct page **page)
1054{
1055 int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1056
1057 if (write)
1058 flags |= FOLL_WRITE;
1059
1060 return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1061}
1062
Huang Yingfafc3db2011-01-30 11:15:49 +08001063static inline int check_user_page_hwpoison(unsigned long addr)
1064{
1065 int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1066
1067 rc = __get_user_pages(current, current->mm, addr, 1,
1068 flags, NULL, NULL, NULL);
1069 return rc == -EHWPOISON;
1070}
1071
Gleb Natapovaf585b92010-10-14 11:22:46 +02001072static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001073 bool *async, bool write_fault, bool *writable)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001074{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001075 struct page *page[1];
Gleb Natapovaf585b92010-10-14 11:22:46 +02001076 int npages = 0;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001077 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001078
Gleb Natapovaf585b92010-10-14 11:22:46 +02001079 /* we can do it either atomically or asynchronously, not both */
1080 BUG_ON(atomic && async);
1081
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001082 BUG_ON(!write_fault && !writable);
1083
1084 if (writable)
1085 *writable = true;
1086
Gleb Natapovaf585b92010-10-14 11:22:46 +02001087 if (atomic || async)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001088 npages = __get_user_pages_fast(addr, 1, 1, page);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001089
1090 if (unlikely(npages != 1) && !atomic) {
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001091 might_sleep();
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001092
1093 if (writable)
1094 *writable = write_fault;
1095
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001096 if (async) {
1097 down_read(&current->mm->mmap_sem);
1098 npages = get_user_page_nowait(current, current->mm,
1099 addr, write_fault, page);
1100 up_read(&current->mm->mmap_sem);
1101 } else
1102 npages = get_user_pages_fast(addr, 1, write_fault,
1103 page);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001104
1105 /* map read fault as writable if possible */
1106 if (unlikely(!write_fault) && npages == 1) {
1107 struct page *wpage[1];
1108
1109 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1110 if (npages == 1) {
1111 *writable = true;
1112 put_page(page[0]);
1113 page[0] = wpage[0];
1114 }
1115 npages = 1;
1116 }
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001117 }
Izik Eidus539cb662007-11-11 22:05:04 +02001118
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001119 if (unlikely(npages != 1)) {
1120 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001121
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001122 if (atomic)
Gleb Natapov80300892010-10-19 18:13:41 +02001123 return get_fault_pfn();
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001124
Huang Yingbbeb3402010-06-22 14:23:11 +08001125 down_read(&current->mm->mmap_sem);
Gleb Natapov0857b9e2011-02-01 13:21:47 +02001126 if (npages == -EHWPOISON ||
1127 (!async && check_user_page_hwpoison(addr))) {
Huang Yingbbeb3402010-06-22 14:23:11 +08001128 up_read(&current->mm->mmap_sem);
Huang Yingbf998152010-05-31 14:28:19 +08001129 get_page(hwpoison_page);
1130 return page_to_pfn(hwpoison_page);
1131 }
1132
Gleb Natapov80300892010-10-19 18:13:41 +02001133 vma = find_vma_intersection(current->mm, addr, addr+1);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001134
Gleb Natapov80300892010-10-19 18:13:41 +02001135 if (vma == NULL)
1136 pfn = get_fault_pfn();
1137 else if ((vma->vm_flags & VM_PFNMAP)) {
1138 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1139 vma->vm_pgoff;
1140 BUG_ON(!kvm_is_mmio_pfn(pfn));
1141 } else {
1142 if (async && (vma->vm_flags & VM_WRITE))
Gleb Natapovaf585b92010-10-14 11:22:46 +02001143 *async = true;
Gleb Natapov80300892010-10-19 18:13:41 +02001144 pfn = get_fault_pfn();
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001145 }
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001146 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001147 } else
1148 pfn = page_to_pfn(page[0]);
1149
1150 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001151}
1152
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001153pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
1154{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001155 return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001156}
1157EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
1158
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001159static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1160 bool write_fault, bool *writable)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001161{
1162 unsigned long addr;
1163
Gleb Natapovaf585b92010-10-14 11:22:46 +02001164 if (async)
1165 *async = false;
1166
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001167 addr = gfn_to_hva(kvm, gfn);
1168 if (kvm_is_error_hva(addr)) {
1169 get_page(bad_page);
1170 return page_to_pfn(bad_page);
1171 }
1172
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001173 return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001174}
1175
1176pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1177{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001178 return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001179}
1180EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1181
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001182pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1183 bool write_fault, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02001184{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001185 return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02001186}
1187EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1188
Xiao Guangrong365fb3f2010-08-28 19:24:13 +08001189pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1190{
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001191 return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001192}
Anthony Liguori35149e22008-04-02 14:46:56 -05001193EXPORT_SYMBOL_GPL(gfn_to_pfn);
1194
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001195pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1196 bool *writable)
1197{
1198 return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1199}
1200EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1201
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001202pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
1203 struct kvm_memory_slot *slot, gfn_t gfn)
1204{
1205 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001206 return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001207}
1208
Xiao Guangrong48987782010-08-22 19:11:43 +08001209int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1210 int nr_pages)
1211{
1212 unsigned long addr;
1213 gfn_t entry;
1214
Gleb Natapov49c77542010-10-18 15:22:23 +02001215 addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001216 if (kvm_is_error_hva(addr))
1217 return -1;
1218
1219 if (entry < nr_pages)
1220 return 0;
1221
1222 return __get_user_pages_fast(addr, nr_pages, 1, pages);
1223}
1224EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1225
Anthony Liguori35149e22008-04-02 14:46:56 -05001226struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1227{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001228 pfn_t pfn;
1229
1230 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001231 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001232 return pfn_to_page(pfn);
1233
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001234 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001235
1236 get_page(bad_page);
1237 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001238}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001239
Avi Kivity954bbbc22007-03-30 14:02:32 +03001240EXPORT_SYMBOL_GPL(gfn_to_page);
1241
Izik Eidusb4231d62007-11-20 11:49:33 +02001242void kvm_release_page_clean(struct page *page)
1243{
Anthony Liguori35149e22008-04-02 14:46:56 -05001244 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001245}
1246EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1247
Anthony Liguori35149e22008-04-02 14:46:56 -05001248void kvm_release_pfn_clean(pfn_t pfn)
1249{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001250 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001251 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001252}
1253EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1254
Izik Eidusb4231d62007-11-20 11:49:33 +02001255void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001256{
Anthony Liguori35149e22008-04-02 14:46:56 -05001257 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001258}
Izik Eidusb4231d62007-11-20 11:49:33 +02001259EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001260
Anthony Liguori35149e22008-04-02 14:46:56 -05001261void kvm_release_pfn_dirty(pfn_t pfn)
1262{
1263 kvm_set_pfn_dirty(pfn);
1264 kvm_release_pfn_clean(pfn);
1265}
1266EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1267
1268void kvm_set_page_dirty(struct page *page)
1269{
1270 kvm_set_pfn_dirty(page_to_pfn(page));
1271}
1272EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1273
1274void kvm_set_pfn_dirty(pfn_t pfn)
1275{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001276 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001277 struct page *page = pfn_to_page(pfn);
1278 if (!PageReserved(page))
1279 SetPageDirty(page);
1280 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001281}
1282EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1283
1284void kvm_set_pfn_accessed(pfn_t pfn)
1285{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001286 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001287 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001288}
1289EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1290
1291void kvm_get_pfn(pfn_t pfn)
1292{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001293 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001294 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001295}
1296EXPORT_SYMBOL_GPL(kvm_get_pfn);
1297
Izik Eidus195aefd2007-10-01 22:14:18 +02001298static int next_segment(unsigned long len, int offset)
1299{
1300 if (len > PAGE_SIZE - offset)
1301 return PAGE_SIZE - offset;
1302 else
1303 return len;
1304}
1305
1306int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1307 int len)
1308{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001309 int r;
1310 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001311
Izik Eiduse0506bc2007-11-11 22:10:22 +02001312 addr = gfn_to_hva(kvm, gfn);
1313 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001314 return -EFAULT;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001315 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001316 if (r)
1317 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001318 return 0;
1319}
1320EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1321
1322int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1323{
1324 gfn_t gfn = gpa >> PAGE_SHIFT;
1325 int seg;
1326 int offset = offset_in_page(gpa);
1327 int ret;
1328
1329 while ((seg = next_segment(len, offset)) != 0) {
1330 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1331 if (ret < 0)
1332 return ret;
1333 offset = 0;
1334 len -= seg;
1335 data += seg;
1336 ++gfn;
1337 }
1338 return 0;
1339}
1340EXPORT_SYMBOL_GPL(kvm_read_guest);
1341
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001342int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1343 unsigned long len)
1344{
1345 int r;
1346 unsigned long addr;
1347 gfn_t gfn = gpa >> PAGE_SHIFT;
1348 int offset = offset_in_page(gpa);
1349
1350 addr = gfn_to_hva(kvm, gfn);
1351 if (kvm_is_error_hva(addr))
1352 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001353 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001354 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001355 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001356 if (r)
1357 return -EFAULT;
1358 return 0;
1359}
1360EXPORT_SYMBOL(kvm_read_guest_atomic);
1361
Izik Eidus195aefd2007-10-01 22:14:18 +02001362int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1363 int offset, int len)
1364{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001365 int r;
1366 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001367
Izik Eiduse0506bc2007-11-11 22:10:22 +02001368 addr = gfn_to_hva(kvm, gfn);
1369 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001370 return -EFAULT;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001371 r = __copy_to_user((void __user *)addr + offset, data, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02001372 if (r)
1373 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001374 mark_page_dirty(kvm, gfn);
1375 return 0;
1376}
1377EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1378
1379int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1380 unsigned long len)
1381{
1382 gfn_t gfn = gpa >> PAGE_SHIFT;
1383 int seg;
1384 int offset = offset_in_page(gpa);
1385 int ret;
1386
1387 while ((seg = next_segment(len, offset)) != 0) {
1388 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1389 if (ret < 0)
1390 return ret;
1391 offset = 0;
1392 len -= seg;
1393 data += seg;
1394 ++gfn;
1395 }
1396 return 0;
1397}
1398
Gleb Natapov49c77542010-10-18 15:22:23 +02001399int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1400 gpa_t gpa)
1401{
1402 struct kvm_memslots *slots = kvm_memslots(kvm);
1403 int offset = offset_in_page(gpa);
1404 gfn_t gfn = gpa >> PAGE_SHIFT;
1405
1406 ghc->gpa = gpa;
1407 ghc->generation = slots->generation;
1408 ghc->memslot = __gfn_to_memslot(slots, gfn);
1409 ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
1410 if (!kvm_is_error_hva(ghc->hva))
1411 ghc->hva += offset;
1412 else
1413 return -EFAULT;
1414
1415 return 0;
1416}
1417EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1418
1419int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1420 void *data, unsigned long len)
1421{
1422 struct kvm_memslots *slots = kvm_memslots(kvm);
1423 int r;
1424
1425 if (slots->generation != ghc->generation)
1426 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1427
1428 if (kvm_is_error_hva(ghc->hva))
1429 return -EFAULT;
1430
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001431 r = __copy_to_user((void __user *)ghc->hva, data, len);
Gleb Natapov49c77542010-10-18 15:22:23 +02001432 if (r)
1433 return -EFAULT;
1434 mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1435
1436 return 0;
1437}
1438EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1439
Gleb Natapove03b6442011-07-11 15:28:11 -04001440int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1441 void *data, unsigned long len)
1442{
1443 struct kvm_memslots *slots = kvm_memslots(kvm);
1444 int r;
1445
1446 if (slots->generation != ghc->generation)
1447 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
1448
1449 if (kvm_is_error_hva(ghc->hva))
1450 return -EFAULT;
1451
1452 r = __copy_from_user(data, (void __user *)ghc->hva, len);
1453 if (r)
1454 return -EFAULT;
1455
1456 return 0;
1457}
1458EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1459
Izik Eidus195aefd2007-10-01 22:14:18 +02001460int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1461{
Heiko Carstens3bcc8a82010-10-27 17:21:21 +02001462 return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
1463 offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001464}
1465EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1466
1467int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1468{
1469 gfn_t gfn = gpa >> PAGE_SHIFT;
1470 int seg;
1471 int offset = offset_in_page(gpa);
1472 int ret;
1473
1474 while ((seg = next_segment(len, offset)) != 0) {
1475 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1476 if (ret < 0)
1477 return ret;
1478 offset = 0;
1479 len -= seg;
1480 ++gfn;
1481 }
1482 return 0;
1483}
1484EXPORT_SYMBOL_GPL(kvm_clear_guest);
1485
Gleb Natapov49c77542010-10-18 15:22:23 +02001486void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
1487 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488{
Rusty Russell7e9d6192007-07-31 20:41:14 +10001489 if (memslot && memslot->dirty_bitmap) {
1490 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491
Akinobu Mitacd7e48c2011-03-23 16:42:04 -07001492 __set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493 }
1494}
1495
Gleb Natapov49c77542010-10-18 15:22:23 +02001496void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1497{
1498 struct kvm_memory_slot *memslot;
1499
1500 memslot = gfn_to_memslot(kvm, gfn);
1501 mark_page_dirty_in_slot(kvm, memslot, gfn);
1502}
1503
Eddie Dongb6958ce2007-07-18 12:15:21 +03001504/*
1505 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1506 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001507void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001508{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001509 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001510
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001511 for (;;) {
1512 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001513
Gleb Natapova1b37102009-07-09 15:33:52 +03001514 if (kvm_arch_vcpu_runnable(vcpu)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001515 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001516 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001517 }
Gleb Natapov09cec752009-03-23 15:11:44 +02001518 if (kvm_cpu_has_pending_timer(vcpu))
1519 break;
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001520 if (signal_pending(current))
1521 break;
1522
Eddie Dongb6958ce2007-07-18 12:15:21 +03001523 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03001524 }
1525
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001526 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001527}
1528
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529void kvm_resched(struct kvm_vcpu *vcpu)
1530{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001531 if (!need_resched())
1532 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534}
1535EXPORT_SYMBOL_GPL(kvm_resched);
1536
Rik van Riel217ece62011-02-01 09:53:28 -05001537void kvm_vcpu_on_spin(struct kvm_vcpu *me)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001538{
Rik van Riel217ece62011-02-01 09:53:28 -05001539 struct kvm *kvm = me->kvm;
1540 struct kvm_vcpu *vcpu;
1541 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1542 int yielded = 0;
1543 int pass;
1544 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001545
Rik van Riel217ece62011-02-01 09:53:28 -05001546 /*
1547 * We boost the priority of a VCPU that is runnable but not
1548 * currently running, because it got preempted by something
1549 * else and called schedule in __vcpu_run. Hopefully that
1550 * VCPU is holding the lock that we need and will release it.
1551 * We approximate round-robin by starting at the last boosted VCPU.
1552 */
1553 for (pass = 0; pass < 2 && !yielded; pass++) {
1554 kvm_for_each_vcpu(i, vcpu, kvm) {
1555 struct task_struct *task = NULL;
1556 struct pid *pid;
1557 if (!pass && i < last_boosted_vcpu) {
1558 i = last_boosted_vcpu;
1559 continue;
1560 } else if (pass && i > last_boosted_vcpu)
1561 break;
1562 if (vcpu == me)
1563 continue;
1564 if (waitqueue_active(&vcpu->wq))
1565 continue;
1566 rcu_read_lock();
1567 pid = rcu_dereference(vcpu->pid);
1568 if (pid)
1569 task = get_pid_task(vcpu->pid, PIDTYPE_PID);
1570 rcu_read_unlock();
1571 if (!task)
1572 continue;
1573 if (task->flags & PF_VCPU) {
1574 put_task_struct(task);
1575 continue;
1576 }
1577 if (yield_to(task, 1)) {
1578 put_task_struct(task);
1579 kvm->last_boosted_vcpu = i;
1580 yielded = 1;
1581 break;
1582 }
1583 put_task_struct(task);
1584 }
1585 }
Zhai, Edwind255f4f2009-10-09 18:03:20 +08001586}
1587EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1588
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001589static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001590{
1591 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001592 struct page *page;
1593
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001594 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001595 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001596#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001597 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001598 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001599#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001600#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1601 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1602 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1603#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001604 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001605 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001606 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001607 vmf->page = page;
1608 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001609}
1610
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001611static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001612 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001613};
1614
1615static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1616{
1617 vma->vm_ops = &kvm_vcpu_vm_ops;
1618 return 0;
1619}
1620
Avi Kivitybccf2152007-02-21 18:04:26 +02001621static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1622{
1623 struct kvm_vcpu *vcpu = filp->private_data;
1624
Al Viro66c0b392008-04-19 20:33:56 +01001625 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001626 return 0;
1627}
1628
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001629static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001630 .release = kvm_vcpu_release,
1631 .unlocked_ioctl = kvm_vcpu_ioctl,
Alexander Graf1dda6062011-06-08 02:45:37 +02001632#ifdef CONFIG_COMPAT
1633 .compat_ioctl = kvm_vcpu_compat_ioctl,
1634#endif
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001635 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001636 .llseek = noop_llseek,
Avi Kivitybccf2152007-02-21 18:04:26 +02001637};
1638
1639/*
1640 * Allocates an inode for the vcpu.
1641 */
1642static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1643{
Roland Dreier628ff7c2009-12-18 09:41:24 -08001644 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
Avi Kivitybccf2152007-02-21 18:04:26 +02001645}
1646
Avi Kivityc5ea7662007-02-20 18:41:05 +02001647/*
1648 * Creates some virtual cpus. Good luck creating more than one.
1649 */
Gleb Natapov73880c82009-06-09 15:56:28 +03001650static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02001651{
1652 int r;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001653 struct kvm_vcpu *vcpu, *v;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001654
Gleb Natapov73880c82009-06-09 15:56:28 +03001655 vcpu = kvm_arch_vcpu_create(kvm, id);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001656 if (IS_ERR(vcpu))
1657 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001658
Avi Kivity15ad7142007-07-11 18:17:21 +03001659 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1660
Avi Kivity26e52152007-11-20 15:30:24 +02001661 r = kvm_arch_vcpu_setup(vcpu);
1662 if (r)
Jan Kiszkad7805922011-05-23 10:33:05 +02001663 goto vcpu_destroy;
Avi Kivity26e52152007-11-20 15:30:24 +02001664
Shaohua Li11ec2802007-07-23 14:51:37 +08001665 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03001666 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1667 r = -EINVAL;
Jan Kiszkad7805922011-05-23 10:33:05 +02001668 goto unlock_vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001669 }
Gleb Natapov73880c82009-06-09 15:56:28 +03001670
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001671 kvm_for_each_vcpu(r, v, kvm)
1672 if (v->vcpu_id == id) {
Gleb Natapov73880c82009-06-09 15:56:28 +03001673 r = -EEXIST;
Jan Kiszkad7805922011-05-23 10:33:05 +02001674 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03001675 }
1676
1677 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001678
1679 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001680 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001681 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03001682 if (r < 0) {
1683 kvm_put_kvm(kvm);
Jan Kiszkad7805922011-05-23 10:33:05 +02001684 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03001685 }
1686
1687 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1688 smp_wmb();
1689 atomic_inc(&kvm->online_vcpus);
1690
1691#ifdef CONFIG_KVM_APIC_ARCHITECTURE
1692 if (kvm->bsp_vcpu_id == id)
1693 kvm->bsp_vcpu = vcpu;
1694#endif
1695 mutex_unlock(&kvm->lock);
Avi Kivitybccf2152007-02-21 18:04:26 +02001696 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001697
Jan Kiszkad7805922011-05-23 10:33:05 +02001698unlock_vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001699 mutex_unlock(&kvm->lock);
Jan Kiszkad7805922011-05-23 10:33:05 +02001700vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001701 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001702 return r;
1703}
1704
Avi Kivity1961d272007-03-05 19:46:05 +02001705static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1706{
1707 if (sigset) {
1708 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1709 vcpu->sigset_active = 1;
1710 vcpu->sigset = *sigset;
1711 } else
1712 vcpu->sigset_active = 0;
1713 return 0;
1714}
1715
Avi Kivitybccf2152007-02-21 18:04:26 +02001716static long kvm_vcpu_ioctl(struct file *filp,
1717 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718{
Avi Kivitybccf2152007-02-21 18:04:26 +02001719 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001720 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001721 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001722 struct kvm_fpu *fpu = NULL;
1723 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001725 if (vcpu->kvm->mm != current->mm)
1726 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03001727
1728#if defined(CONFIG_S390) || defined(CONFIG_PPC)
1729 /*
1730 * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1731 * so vcpu_load() would break it.
1732 */
1733 if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1734 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1735#endif
1736
1737
1738 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001740 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001741 r = -EINVAL;
1742 if (arg)
1743 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001744 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Gleb Natapov64be5002010-10-24 16:49:08 +02001745 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001748 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001750 r = -ENOMEM;
1751 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1752 if (!kvm_regs)
1753 goto out;
1754 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001756 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001758 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1759 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001761out_free1:
1762 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 break;
1764 }
1765 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001766 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001768 r = -ENOMEM;
1769 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1770 if (!kvm_regs)
1771 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001773 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1774 goto out_free2;
1775 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001777 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001779out_free2:
1780 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781 break;
1782 }
1783 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001784 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1785 r = -ENOMEM;
1786 if (!kvm_sregs)
1787 goto out;
1788 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 if (r)
1790 goto out;
1791 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001792 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 goto out;
1794 r = 0;
1795 break;
1796 }
1797 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001798 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1799 r = -ENOMEM;
1800 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001802 r = -EFAULT;
1803 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1804 goto out;
1805 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 if (r)
1807 goto out;
1808 r = 0;
1809 break;
1810 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001811 case KVM_GET_MP_STATE: {
1812 struct kvm_mp_state mp_state;
1813
1814 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1815 if (r)
1816 goto out;
1817 r = -EFAULT;
1818 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1819 goto out;
1820 r = 0;
1821 break;
1822 }
1823 case KVM_SET_MP_STATE: {
1824 struct kvm_mp_state mp_state;
1825
1826 r = -EFAULT;
1827 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1828 goto out;
1829 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1830 if (r)
1831 goto out;
1832 r = 0;
1833 break;
1834 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 case KVM_TRANSLATE: {
1836 struct kvm_translation tr;
1837
1838 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001839 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001841 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001842 if (r)
1843 goto out;
1844 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001845 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846 goto out;
1847 r = 0;
1848 break;
1849 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001850 case KVM_SET_GUEST_DEBUG: {
1851 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852
1853 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001854 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001856 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 if (r)
1858 goto out;
1859 r = 0;
1860 break;
1861 }
Avi Kivity1961d272007-03-05 19:46:05 +02001862 case KVM_SET_SIGNAL_MASK: {
1863 struct kvm_signal_mask __user *sigmask_arg = argp;
1864 struct kvm_signal_mask kvm_sigmask;
1865 sigset_t sigset, *p;
1866
1867 p = NULL;
1868 if (argp) {
1869 r = -EFAULT;
1870 if (copy_from_user(&kvm_sigmask, argp,
1871 sizeof kvm_sigmask))
1872 goto out;
1873 r = -EINVAL;
1874 if (kvm_sigmask.len != sizeof sigset)
1875 goto out;
1876 r = -EFAULT;
1877 if (copy_from_user(&sigset, sigmask_arg->sigset,
1878 sizeof sigset))
1879 goto out;
1880 p = &sigset;
1881 }
Andi Kleen376d41f2010-06-10 13:10:47 +02001882 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02001883 break;
1884 }
Avi Kivityb8836732007-04-01 16:34:31 +03001885 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001886 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1887 r = -ENOMEM;
1888 if (!fpu)
1889 goto out;
1890 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001891 if (r)
1892 goto out;
1893 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001894 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001895 goto out;
1896 r = 0;
1897 break;
1898 }
1899 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001900 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1901 r = -ENOMEM;
1902 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001903 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001904 r = -EFAULT;
1905 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1906 goto out;
1907 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001908 if (r)
1909 goto out;
1910 r = 0;
1911 break;
1912 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001913 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001914 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001915 }
1916out:
Avi Kivity2122ff52010-05-13 11:25:04 +03001917 vcpu_put(vcpu);
Dave Hansenfa3795a2008-08-11 10:01:46 -07001918 kfree(fpu);
1919 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001920 return r;
1921}
1922
Alexander Graf1dda6062011-06-08 02:45:37 +02001923#ifdef CONFIG_COMPAT
1924static long kvm_vcpu_compat_ioctl(struct file *filp,
1925 unsigned int ioctl, unsigned long arg)
1926{
1927 struct kvm_vcpu *vcpu = filp->private_data;
1928 void __user *argp = compat_ptr(arg);
1929 int r;
1930
1931 if (vcpu->kvm->mm != current->mm)
1932 return -EIO;
1933
1934 switch (ioctl) {
1935 case KVM_SET_SIGNAL_MASK: {
1936 struct kvm_signal_mask __user *sigmask_arg = argp;
1937 struct kvm_signal_mask kvm_sigmask;
1938 compat_sigset_t csigset;
1939 sigset_t sigset;
1940
1941 if (argp) {
1942 r = -EFAULT;
1943 if (copy_from_user(&kvm_sigmask, argp,
1944 sizeof kvm_sigmask))
1945 goto out;
1946 r = -EINVAL;
1947 if (kvm_sigmask.len != sizeof csigset)
1948 goto out;
1949 r = -EFAULT;
1950 if (copy_from_user(&csigset, sigmask_arg->sigset,
1951 sizeof csigset))
1952 goto out;
1953 }
1954 sigset_from_compat(&sigset, &csigset);
1955 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1956 break;
1957 }
1958 default:
1959 r = kvm_vcpu_ioctl(filp, ioctl, arg);
1960 }
1961
1962out:
1963 return r;
1964}
1965#endif
1966
Avi Kivitybccf2152007-02-21 18:04:26 +02001967static long kvm_vm_ioctl(struct file *filp,
1968 unsigned int ioctl, unsigned long arg)
1969{
1970 struct kvm *kvm = filp->private_data;
1971 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001972 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001973
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001974 if (kvm->mm != current->mm)
1975 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001976 switch (ioctl) {
1977 case KVM_CREATE_VCPU:
1978 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1979 if (r < 0)
1980 goto out;
1981 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001982 case KVM_SET_USER_MEMORY_REGION: {
1983 struct kvm_userspace_memory_region kvm_userspace_mem;
1984
1985 r = -EFAULT;
1986 if (copy_from_user(&kvm_userspace_mem, argp,
1987 sizeof kvm_userspace_mem))
1988 goto out;
1989
1990 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991 if (r)
1992 goto out;
1993 break;
1994 }
1995 case KVM_GET_DIRTY_LOG: {
1996 struct kvm_dirty_log log;
1997
1998 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001999 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002001 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 if (r)
2003 goto out;
2004 break;
2005 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02002006#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2007 case KVM_REGISTER_COALESCED_MMIO: {
2008 struct kvm_coalesced_mmio_zone zone;
2009 r = -EFAULT;
2010 if (copy_from_user(&zone, argp, sizeof zone))
2011 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02002012 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2013 if (r)
2014 goto out;
2015 r = 0;
2016 break;
2017 }
2018 case KVM_UNREGISTER_COALESCED_MMIO: {
2019 struct kvm_coalesced_mmio_zone zone;
2020 r = -EFAULT;
2021 if (copy_from_user(&zone, argp, sizeof zone))
2022 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02002023 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2024 if (r)
2025 goto out;
2026 r = 0;
2027 break;
2028 }
2029#endif
Gregory Haskins721eecb2009-05-20 10:30:49 -04002030 case KVM_IRQFD: {
2031 struct kvm_irqfd data;
2032
2033 r = -EFAULT;
2034 if (copy_from_user(&data, argp, sizeof data))
2035 goto out;
2036 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2037 break;
2038 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002039 case KVM_IOEVENTFD: {
2040 struct kvm_ioeventfd data;
2041
2042 r = -EFAULT;
2043 if (copy_from_user(&data, argp, sizeof data))
2044 goto out;
2045 r = kvm_ioeventfd(kvm, &data);
2046 break;
2047 }
Gleb Natapov73880c82009-06-09 15:56:28 +03002048#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2049 case KVM_SET_BOOT_CPU_ID:
2050 r = 0;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002051 mutex_lock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002052 if (atomic_read(&kvm->online_vcpus) != 0)
2053 r = -EBUSY;
2054 else
2055 kvm->bsp_vcpu_id = arg;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002056 mutex_unlock(&kvm->lock);
Gleb Natapov73880c82009-06-09 15:56:28 +03002057 break;
2058#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002059 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002060 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivitybfd99ff2009-08-26 14:57:50 +03002061 if (r == -ENOTTY)
2062 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002063 }
2064out:
2065 return r;
2066}
2067
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002068#ifdef CONFIG_COMPAT
2069struct compat_kvm_dirty_log {
2070 __u32 slot;
2071 __u32 padding1;
2072 union {
2073 compat_uptr_t dirty_bitmap; /* one bit per page */
2074 __u64 padding2;
2075 };
2076};
2077
2078static long kvm_vm_compat_ioctl(struct file *filp,
2079 unsigned int ioctl, unsigned long arg)
2080{
2081 struct kvm *kvm = filp->private_data;
2082 int r;
2083
2084 if (kvm->mm != current->mm)
2085 return -EIO;
2086 switch (ioctl) {
2087 case KVM_GET_DIRTY_LOG: {
2088 struct compat_kvm_dirty_log compat_log;
2089 struct kvm_dirty_log log;
2090
2091 r = -EFAULT;
2092 if (copy_from_user(&compat_log, (void __user *)arg,
2093 sizeof(compat_log)))
2094 goto out;
2095 log.slot = compat_log.slot;
2096 log.padding1 = compat_log.padding1;
2097 log.padding2 = compat_log.padding2;
2098 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2099
2100 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2101 if (r)
2102 goto out;
2103 break;
2104 }
2105 default:
2106 r = kvm_vm_ioctl(filp, ioctl, arg);
2107 }
2108
2109out:
2110 return r;
2111}
2112#endif
2113
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002114static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02002115{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002116 struct page *page[1];
2117 unsigned long addr;
2118 int npages;
2119 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002120 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02002121
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002122 addr = gfn_to_hva(kvm, gfn);
2123 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002124 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002125
2126 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
2127 NULL);
2128 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002129 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03002130
2131 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002132 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02002133}
2134
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002135static const struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002136 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02002137};
2138
2139static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2140{
2141 vma->vm_ops = &kvm_vm_vm_ops;
2142 return 0;
2143}
2144
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002145static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02002146 .release = kvm_vm_release,
2147 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6ff58942009-10-22 14:19:27 +02002148#ifdef CONFIG_COMPAT
2149 .compat_ioctl = kvm_vm_compat_ioctl,
2150#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02002151 .mmap = kvm_vm_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002152 .llseek = noop_llseek,
Avi Kivityf17abe92007-02-21 19:28:04 +02002153};
2154
2155static int kvm_dev_ioctl_create_vm(void)
2156{
Heiko Carstensaac87632010-10-27 17:22:10 +02002157 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002158 struct kvm *kvm;
2159
Avi Kivityf17abe92007-02-21 19:28:04 +02002160 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002161 if (IS_ERR(kvm))
2162 return PTR_ERR(kvm);
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09002163#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2164 r = kvm_coalesced_mmio_init(kvm);
2165 if (r < 0) {
2166 kvm_put_kvm(kvm);
2167 return r;
2168 }
2169#endif
Heiko Carstensaac87632010-10-27 17:22:10 +02002170 r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2171 if (r < 0)
Al Viro66c0b392008-04-19 20:33:56 +01002172 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02002173
Heiko Carstensaac87632010-10-27 17:22:10 +02002174 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002175}
2176
Avi Kivity1a811b62008-12-08 18:25:27 +02002177static long kvm_dev_ioctl_check_extension_generic(long arg)
2178{
2179 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02002180 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02002181 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
Jan Kiszka4cd481f2009-04-13 11:59:32 +02002182 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Gleb Natapov73880c82009-06-09 15:56:28 +03002183#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2184 case KVM_CAP_SET_BOOT_CPU_ID:
2185#endif
Avi Kivitya9c73992009-11-04 11:54:59 +02002186 case KVM_CAP_INTERNAL_ERROR_DATA:
Avi Kivity1a811b62008-12-08 18:25:27 +02002187 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02002188#ifdef CONFIG_HAVE_KVM_IRQCHIP
2189 case KVM_CAP_IRQ_ROUTING:
Sheng Yang36463142009-03-16 16:33:43 +08002190 return KVM_MAX_IRQ_ROUTES;
Avi Kivity399ec802008-11-19 13:58:46 +02002191#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02002192 default:
2193 break;
2194 }
2195 return kvm_dev_ioctl_check_extension(arg);
2196}
2197
Avi Kivityf17abe92007-02-21 19:28:04 +02002198static long kvm_dev_ioctl(struct file *filp,
2199 unsigned int ioctl, unsigned long arg)
2200{
Avi Kivity07c45a32007-03-07 13:05:38 +02002201 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002202
2203 switch (ioctl) {
2204 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002205 r = -EINVAL;
2206 if (arg)
2207 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002208 r = KVM_API_VERSION;
2209 break;
2210 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002211 r = -EINVAL;
2212 if (arg)
2213 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002214 r = kvm_dev_ioctl_create_vm();
2215 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002216 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002217 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002218 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002219 case KVM_GET_VCPU_MMAP_SIZE:
2220 r = -EINVAL;
2221 if (arg)
2222 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002223 r = PAGE_SIZE; /* struct kvm_run */
2224#ifdef CONFIG_X86
2225 r += PAGE_SIZE; /* pio data page */
2226#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002227#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2228 r += PAGE_SIZE; /* coalesced mmio ring page */
2229#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002230 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002231 case KVM_TRACE_ENABLE:
2232 case KVM_TRACE_PAUSE:
2233 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03002234 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002235 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002237 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 }
2239out:
2240 return r;
2241}
2242
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 .unlocked_ioctl = kvm_dev_ioctl,
2245 .compat_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002246 .llseek = noop_llseek,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247};
2248
2249static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002250 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 "kvm",
2252 &kvm_chardev_ops,
2253};
2254
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002255static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002256{
2257 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02002258 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03002259
Rusty Russell7f59f492008-12-07 21:25:45 +10302260 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002261 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02002262
Rusty Russell7f59f492008-12-07 21:25:45 +10302263 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02002264
2265 r = kvm_arch_hardware_enable(NULL);
2266
2267 if (r) {
2268 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2269 atomic_inc(&hardware_enable_failed);
2270 printk(KERN_INFO "kvm: enabling virtualization on "
2271 "CPU%d failed\n", cpu);
2272 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03002273}
2274
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002275static void hardware_enable(void *junk)
2276{
Jan Kiszkae935b832011-02-08 12:55:33 +01002277 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002278 hardware_enable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002279 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002280}
2281
2282static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03002283{
2284 int cpu = raw_smp_processor_id();
2285
Rusty Russell7f59f492008-12-07 21:25:45 +10302286 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002287 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302288 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002289 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002290}
2291
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002292static void hardware_disable(void *junk)
2293{
Jan Kiszkae935b832011-02-08 12:55:33 +01002294 raw_spin_lock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002295 hardware_disable_nolock(junk);
Jan Kiszkae935b832011-02-08 12:55:33 +01002296 raw_spin_unlock(&kvm_lock);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002297}
2298
Alexander Graf10474ae2009-09-15 11:37:46 +02002299static void hardware_disable_all_nolock(void)
2300{
2301 BUG_ON(!kvm_usage_count);
2302
2303 kvm_usage_count--;
2304 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002305 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002306}
2307
2308static void hardware_disable_all(void)
2309{
Jan Kiszkae935b832011-02-08 12:55:33 +01002310 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002311 hardware_disable_all_nolock();
Jan Kiszkae935b832011-02-08 12:55:33 +01002312 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002313}
2314
2315static int hardware_enable_all(void)
2316{
2317 int r = 0;
2318
Jan Kiszkae935b832011-02-08 12:55:33 +01002319 raw_spin_lock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002320
2321 kvm_usage_count++;
2322 if (kvm_usage_count == 1) {
2323 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002324 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02002325
2326 if (atomic_read(&hardware_enable_failed)) {
2327 hardware_disable_all_nolock();
2328 r = -EBUSY;
2329 }
2330 }
2331
Jan Kiszkae935b832011-02-08 12:55:33 +01002332 raw_spin_unlock(&kvm_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02002333
2334 return r;
2335}
2336
Avi Kivity774c47f2007-02-12 00:54:47 -08002337static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2338 void *v)
2339{
2340 int cpu = (long)v;
2341
Alexander Graf10474ae2009-09-15 11:37:46 +02002342 if (!kvm_usage_count)
2343 return NOTIFY_OK;
2344
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002345 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002346 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002347 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002348 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2349 cpu);
2350 hardware_disable(NULL);
2351 break;
Zachary Amsdenda908f22010-08-19 22:07:27 -10002352 case CPU_STARTING:
Jeremy Katz43934a32007-02-19 14:37:46 +02002353 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2354 cpu);
Zachary Amsdenda908f22010-08-19 22:07:27 -10002355 hardware_enable(NULL);
Avi Kivity774c47f2007-02-12 00:54:47 -08002356 break;
2357 }
2358 return NOTIFY_OK;
2359}
2360
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002361
Avi Kivityb7c41452010-12-02 17:52:50 +02002362asmlinkage void kvm_spurious_fault(void)
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002363{
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002364 /* Fault while not rebooting. We want the trace. */
2365 BUG();
2366}
Avi Kivityb7c41452010-12-02 17:52:50 +02002367EXPORT_SYMBOL_GPL(kvm_spurious_fault);
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002368
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002369static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002370 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002371{
Sheng Yang8e1c1812009-04-29 11:09:04 +08002372 /*
2373 * Some (well, at least mine) BIOSes hang on reboot if
2374 * in vmx root mode.
2375 *
2376 * And Intel TXT required VMX off for all cpu when system shutdown.
2377 */
2378 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2379 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002380 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002381 return NOTIFY_OK;
2382}
2383
2384static struct notifier_block kvm_reboot_notifier = {
2385 .notifier_call = kvm_reboot,
2386 .priority = 0,
2387};
2388
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002389static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002390{
2391 int i;
2392
2393 for (i = 0; i < bus->dev_count; i++) {
2394 struct kvm_io_device *pos = bus->devs[i];
2395
2396 kvm_iodevice_destructor(pos);
2397 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002398 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002399}
2400
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002401/* kvm_io_bus_write - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002402int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002403 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002404{
2405 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002406 struct kvm_io_bus *bus;
2407
2408 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002409 for (i = 0; i < bus->dev_count; i++)
2410 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2411 return 0;
2412 return -EOPNOTSUPP;
2413}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002414
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002415/* kvm_io_bus_read - called under kvm->slots_lock */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002416int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2417 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002418{
2419 int i;
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002420 struct kvm_io_bus *bus;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002421
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002422 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002423 for (i = 0; i < bus->dev_count; i++)
2424 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2425 return 0;
2426 return -EOPNOTSUPP;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002427}
2428
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002429/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002430int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2431 struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002432{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002433 struct kvm_io_bus *new_bus, *bus;
Gregory Haskins090b7af2009-07-07 17:08:44 -04002434
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002435 bus = kvm->buses[bus_idx];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002436 if (bus->dev_count > NR_IOBUS_DEVS-1)
2437 return -ENOSPC;
2438
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002439 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2440 if (!new_bus)
2441 return -ENOMEM;
2442 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2443 new_bus->devs[new_bus->dev_count++] = dev;
2444 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2445 synchronize_srcu_expedited(&kvm->srcu);
2446 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04002447
2448 return 0;
2449}
2450
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002451/* Caller must hold slots_lock. */
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002452int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2453 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04002454{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002455 int i, r;
2456 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03002457
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002458 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2459 if (!new_bus)
2460 return -ENOMEM;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002461
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002462 bus = kvm->buses[bus_idx];
2463 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2464
2465 r = -ENOENT;
2466 for (i = 0; i < new_bus->dev_count; i++)
2467 if (new_bus->devs[i] == dev) {
2468 r = 0;
2469 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
Gregory Haskins090b7af2009-07-07 17:08:44 -04002470 break;
2471 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02002472
2473 if (r) {
2474 kfree(new_bus);
2475 return r;
2476 }
2477
2478 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2479 synchronize_srcu_expedited(&kvm->srcu);
2480 kfree(bus);
2481 return r;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002482}
2483
Avi Kivity774c47f2007-02-12 00:54:47 -08002484static struct notifier_block kvm_cpu_notifier = {
2485 .notifier_call = kvm_cpu_hotplug,
Avi Kivity774c47f2007-02-12 00:54:47 -08002486};
2487
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002488static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002489{
2490 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002491 struct kvm *kvm;
2492
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002493 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002494 raw_spin_lock(&kvm_lock);
Avi Kivityba1389b2007-11-18 16:24:12 +02002495 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002496 *val += *(u32 *)((void *)kvm + offset);
Jan Kiszkae935b832011-02-08 12:55:33 +01002497 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002498 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002499}
2500
2501DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2502
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002503static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002504{
2505 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002506 struct kvm *kvm;
2507 struct kvm_vcpu *vcpu;
2508 int i;
2509
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002510 *val = 0;
Jan Kiszkae935b832011-02-08 12:55:33 +01002511 raw_spin_lock(&kvm_lock);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002512 list_for_each_entry(kvm, &vm_list, vm_list)
Gleb Natapov988a2ca2009-06-09 15:56:29 +03002513 kvm_for_each_vcpu(i, vcpu, kvm)
2514 *val += *(u32 *)((void *)vcpu + offset);
2515
Jan Kiszkae935b832011-02-08 12:55:33 +01002516 raw_spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002517 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002518}
2519
Avi Kivityba1389b2007-11-18 16:24:12 +02002520DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2521
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002522static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02002523 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2524 [KVM_STAT_VM] = &vm_stat_fops,
2525};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002526
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002527static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528{
2529 struct kvm_stats_debugfs_item *p;
2530
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002531 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002533 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002534 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002535 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536}
2537
2538static void kvm_exit_debug(void)
2539{
2540 struct kvm_stats_debugfs_item *p;
2541
2542 for (p = debugfs_entries; p->name; ++p)
2543 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002544 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545}
2546
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002547static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002548{
Alexander Graf10474ae2009-09-15 11:37:46 +02002549 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002550 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002551 return 0;
2552}
2553
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002554static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08002555{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002556 if (kvm_usage_count) {
Jan Kiszkae935b832011-02-08 12:55:33 +01002557 WARN_ON(raw_spin_is_locked(&kvm_lock));
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002558 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10002559 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08002560}
2561
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002562static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08002563 .suspend = kvm_suspend,
2564 .resume = kvm_resume,
2565};
2566
Izik Eiduscea7bb22007-10-17 19:17:48 +02002567struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002568pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569
Avi Kivity15ad7142007-07-11 18:17:21 +03002570static inline
2571struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2572{
2573 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2574}
2575
2576static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2577{
2578 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2579
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002580 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002581}
2582
2583static void kvm_sched_out(struct preempt_notifier *pn,
2584 struct task_struct *next)
2585{
2586 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2587
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002588 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002589}
2590
Avi Kivity0ee75be2010-04-28 15:39:01 +03002591int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10002592 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593{
2594 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002595 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002597 r = kvm_arch_init(opaque);
2598 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002599 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002600
2601 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2602
2603 if (bad_page == NULL) {
2604 r = -ENOMEM;
2605 goto out;
2606 }
2607
Anthony Liguori35149e22008-04-02 14:46:56 -05002608 bad_pfn = page_to_pfn(bad_page);
2609
Huang Yingbf998152010-05-31 14:28:19 +08002610 hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2611
2612 if (hwpoison_page == NULL) {
2613 r = -ENOMEM;
2614 goto out_free_0;
2615 }
2616
2617 hwpoison_pfn = page_to_pfn(hwpoison_page);
2618
Gleb Natapovedba23e2010-07-07 20:16:45 +03002619 fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2620
2621 if (fault_page == NULL) {
2622 r = -ENOMEM;
2623 goto out_free_0;
2624 }
2625
2626 fault_pfn = page_to_pfn(fault_page);
2627
Avi Kivity8437a612009-06-06 14:52:35 -07002628 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10302629 r = -ENOMEM;
2630 goto out_free_0;
2631 }
2632
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002633 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302635 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636
Yang, Sheng002c7f72007-07-31 14:23:01 +03002637 for_each_online_cpu(cpu) {
2638 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002639 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002640 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002641 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002642 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002643 }
2644
Avi Kivity774c47f2007-02-12 00:54:47 -08002645 r = register_cpu_notifier(&kvm_cpu_notifier);
2646 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002647 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 register_reboot_notifier(&kvm_reboot_notifier);
2649
Rusty Russellc16f8622007-07-30 21:12:19 +10002650 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03002651 if (!vcpu_align)
2652 vcpu_align = __alignof__(struct kvm_vcpu);
2653 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
Joe Perches56919c52007-11-12 20:06:51 -08002654 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002655 if (!kvm_vcpu_cache) {
2656 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002657 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10002658 }
2659
Gleb Natapovaf585b92010-10-14 11:22:46 +02002660 r = kvm_async_pf_init();
2661 if (r)
2662 goto out_free;
2663
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002665 kvm_vm_fops.owner = module;
2666 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667
2668 r = misc_register(&kvm_dev);
2669 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002670 printk(KERN_ERR "kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02002671 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 }
2673
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002674 register_syscore_ops(&kvm_syscore_ops);
2675
Avi Kivity15ad7142007-07-11 18:17:21 +03002676 kvm_preempt_ops.sched_in = kvm_sched_in;
2677 kvm_preempt_ops.sched_out = kvm_sched_out;
2678
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002679 kvm_init_debug();
2680
Avi Kivityc7addb92007-09-16 18:58:32 +02002681 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682
Gleb Natapovaf585b92010-10-14 11:22:46 +02002683out_unreg:
2684 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002686 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002687out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002689 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002690out_free_2:
Zhang Xiantaod23087842007-11-29 15:35:39 +08002691out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002692 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302693out_free_0a:
2694 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002695out_free_0:
Gleb Natapovedba23e2010-07-07 20:16:45 +03002696 if (fault_page)
2697 __free_page(fault_page);
Huang Yingbf998152010-05-31 14:28:19 +08002698 if (hwpoison_page)
2699 __free_page(hwpoison_page);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002700 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002701out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002702 kvm_arch_exit();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002703out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 return r;
2705}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002706EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002708void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709{
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07002710 kvm_exit_debug();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002712 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002713 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01002714 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002716 unregister_cpu_notifier(&kvm_cpu_notifier);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09002717 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002718 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002719 kvm_arch_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10302720 free_cpumask_var(cpus_hardware_enabled);
Huang Yingbf998152010-05-31 14:28:19 +08002721 __free_page(hwpoison_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002722 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002724EXPORT_SYMBOL_GPL(kvm_exit);