blob: 1838052f3c9e15292dc51db567b2a5a98419f41f [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.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Hollis Blancharde2174022007-12-03 15:30:24 -060018#include "iodev.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/kvm.h>
22#include <linux/module.h>
23#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/percpu.h>
25#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/debugfs.h>
31#include <linux/highmem.h>
32#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080033#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080034#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030036#include <linux/cpumask.h>
37#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040038#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030039#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050040#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020041#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050042#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050043#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/processor.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/io.h>
47#include <asm/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020048#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Laurent Vivier5f94c172008-05-30 16:05:54 +020050#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
51#include "coalesced_mmio.h"
52#endif
53
Xiantao Zhang8a98f662008-10-06 13:47:38 +080054#ifdef KVM_CAP_DEVICE_ASSIGNMENT
55#include <linux/pci.h>
56#include <linux/interrupt.h>
57#include "irq.h"
58#endif
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080063DEFINE_SPINLOCK(kvm_lock);
64LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080065
Avi Kivity1b6c0162007-05-24 13:03:52 +030066static cpumask_t cpus_hardware_enabled;
67
Rusty Russellc16f8622007-07-30 21:12:19 +100068struct kmem_cache *kvm_vcpu_cache;
69EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030070
Avi Kivity15ad7142007-07-11 18:17:21 +030071static __read_mostly struct preempt_ops kvm_preempt_ops;
72
Hollis Blanchard76f7c872008-04-15 16:05:42 -050073struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080074
Avi Kivitybccf2152007-02-21 18:04:26 +020075static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
76 unsigned long arg);
77
Avi Kivity4ecac3f2008-05-13 13:23:38 +030078bool kvm_rebooting;
79
Xiantao Zhang8a98f662008-10-06 13:47:38 +080080#ifdef KVM_CAP_DEVICE_ASSIGNMENT
81static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
82 int assigned_dev_id)
83{
84 struct list_head *ptr;
85 struct kvm_assigned_dev_kernel *match;
86
87 list_for_each(ptr, head) {
88 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
89 if (match->assigned_dev_id == assigned_dev_id)
90 return match;
91 }
92 return NULL;
93}
94
95static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
96{
97 struct kvm_assigned_dev_kernel *assigned_dev;
98
99 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
100 interrupt_work);
101
102 /* This is taken to safely inject irq inside the guest. When
103 * the interrupt injection (or the ioapic code) uses a
104 * finer-grained lock, update this
105 */
106 mutex_lock(&assigned_dev->kvm->lock);
107 kvm_set_irq(assigned_dev->kvm,
Sheng Yang5550af42008-10-15 20:15:06 +0800108 assigned_dev->irq_source_id,
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800109 assigned_dev->guest_irq, 1);
110 mutex_unlock(&assigned_dev->kvm->lock);
111 kvm_put_kvm(assigned_dev->kvm);
112}
113
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800114static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
115{
116 struct kvm_assigned_dev_kernel *assigned_dev =
117 (struct kvm_assigned_dev_kernel *) dev_id;
118
119 kvm_get_kvm(assigned_dev->kvm);
120 schedule_work(&assigned_dev->interrupt_work);
121 disable_irq_nosync(irq);
122 return IRQ_HANDLED;
123}
124
125/* Ack the irq line for an assigned device */
126static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
127{
128 struct kvm_assigned_dev_kernel *dev;
129
130 if (kian->gsi == -1)
131 return;
132
133 dev = container_of(kian, struct kvm_assigned_dev_kernel,
134 ack_notifier);
Sheng Yang5550af42008-10-15 20:15:06 +0800135 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800136 enable_irq(dev->host_irq);
137}
138
139static void kvm_free_assigned_device(struct kvm *kvm,
140 struct kvm_assigned_dev_kernel
141 *assigned_dev)
142{
143 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
144 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
145
Sheng Yange19e30e2008-10-20 16:07:10 +0800146 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Sheng Yang5550af42008-10-15 20:15:06 +0800147 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800148
149 if (cancel_work_sync(&assigned_dev->interrupt_work))
150 /* We had pending work. That means we will have to take
151 * care of kvm_put_kvm.
152 */
153 kvm_put_kvm(kvm);
154
Sheng Yang6eb55812008-10-31 12:37:41 +0800155 pci_reset_function(assigned_dev->dev);
156
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800157 pci_release_regions(assigned_dev->dev);
158 pci_disable_device(assigned_dev->dev);
159 pci_dev_put(assigned_dev->dev);
160
161 list_del(&assigned_dev->list);
162 kfree(assigned_dev);
163}
164
165void kvm_free_all_assigned_devices(struct kvm *kvm)
166{
167 struct list_head *ptr, *ptr2;
168 struct kvm_assigned_dev_kernel *assigned_dev;
169
170 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
171 assigned_dev = list_entry(ptr,
172 struct kvm_assigned_dev_kernel,
173 list);
174
175 kvm_free_assigned_device(kvm, assigned_dev);
176 }
177}
178
179static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
180 struct kvm_assigned_irq
181 *assigned_irq)
182{
183 int r = 0;
184 struct kvm_assigned_dev_kernel *match;
185
186 mutex_lock(&kvm->lock);
187
188 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
189 assigned_irq->assigned_dev_id);
190 if (!match) {
191 mutex_unlock(&kvm->lock);
192 return -EINVAL;
193 }
194
195 if (match->irq_requested) {
196 match->guest_irq = assigned_irq->guest_irq;
197 match->ack_notifier.gsi = assigned_irq->guest_irq;
198 mutex_unlock(&kvm->lock);
199 return 0;
200 }
201
202 INIT_WORK(&match->interrupt_work,
203 kvm_assigned_dev_interrupt_work_handler);
204
205 if (irqchip_in_kernel(kvm)) {
206 if (!capable(CAP_SYS_RAWIO)) {
207 r = -EPERM;
208 goto out_release;
209 }
210
211 if (assigned_irq->host_irq)
212 match->host_irq = assigned_irq->host_irq;
213 else
214 match->host_irq = match->dev->irq;
215 match->guest_irq = assigned_irq->guest_irq;
216 match->ack_notifier.gsi = assigned_irq->guest_irq;
217 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
218 kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
Sheng Yang5550af42008-10-15 20:15:06 +0800219 r = kvm_request_irq_source_id(kvm);
220 if (r < 0)
221 goto out_release;
222 else
223 match->irq_source_id = r;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800224
225 /* Even though this is PCI, we don't want to use shared
226 * interrupts. Sharing host devices with guest-assigned devices
227 * on the same interrupt line is not a happy situation: there
228 * are going to be long delays in accepting, acking, etc.
229 */
230 if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
231 "kvm_assigned_device", (void *)match)) {
232 r = -EIO;
233 goto out_release;
234 }
235 }
236
237 match->irq_requested = true;
238 mutex_unlock(&kvm->lock);
239 return r;
240out_release:
241 mutex_unlock(&kvm->lock);
242 kvm_free_assigned_device(kvm, match);
243 return r;
244}
245
246static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
247 struct kvm_assigned_pci_dev *assigned_dev)
248{
249 int r = 0;
250 struct kvm_assigned_dev_kernel *match;
251 struct pci_dev *dev;
252
253 mutex_lock(&kvm->lock);
254
255 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
256 assigned_dev->assigned_dev_id);
257 if (match) {
258 /* device already assigned */
259 r = -EINVAL;
260 goto out;
261 }
262
263 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
264 if (match == NULL) {
265 printk(KERN_INFO "%s: Couldn't allocate memory\n",
266 __func__);
267 r = -ENOMEM;
268 goto out;
269 }
270 dev = pci_get_bus_and_slot(assigned_dev->busnr,
271 assigned_dev->devfn);
272 if (!dev) {
273 printk(KERN_INFO "%s: host device not found\n", __func__);
274 r = -EINVAL;
275 goto out_free;
276 }
277 if (pci_enable_device(dev)) {
278 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
279 r = -EBUSY;
280 goto out_put;
281 }
282 r = pci_request_regions(dev, "kvm_assigned_device");
283 if (r) {
284 printk(KERN_INFO "%s: Could not get access to device regions\n",
285 __func__);
286 goto out_disable;
287 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800288
289 pci_reset_function(dev);
290
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800291 match->assigned_dev_id = assigned_dev->assigned_dev_id;
292 match->host_busnr = assigned_dev->busnr;
293 match->host_devfn = assigned_dev->devfn;
294 match->dev = dev;
295
296 match->kvm = kvm;
297
298 list_add(&match->list, &kvm->arch.assigned_dev_head);
299
300 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
301 r = kvm_iommu_map_guest(kvm, match);
302 if (r)
303 goto out_list_del;
304 }
305
306out:
307 mutex_unlock(&kvm->lock);
308 return r;
309out_list_del:
310 list_del(&match->list);
311 pci_release_regions(dev);
312out_disable:
313 pci_disable_device(dev);
314out_put:
315 pci_dev_put(dev);
316out_free:
317 kfree(match);
318 mutex_unlock(&kvm->lock);
319 return r;
320}
321#endif
322
James Morris5aacf0c2006-12-22 01:04:55 -0800323static inline int valid_vcpu(int n)
324{
325 return likely(n >= 0 && n < KVM_MAX_VCPUS);
326}
327
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800328inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300329{
330 if (pfn_valid(pfn))
331 return PageReserved(pfn_to_page(pfn));
332
333 return true;
334}
335
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336/*
337 * Switches to specified vcpu, until a matching vcpu_put()
338 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200339void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340{
Avi Kivity15ad7142007-07-11 18:17:21 +0300341 int cpu;
342
Avi Kivitybccf2152007-02-21 18:04:26 +0200343 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300344 cpu = get_cpu();
345 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200346 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300347 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200348}
349
Carsten Otte313a3dc2007-10-11 19:16:52 +0200350void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351{
Avi Kivity15ad7142007-07-11 18:17:21 +0300352 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200353 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300354 preempt_notifier_unregister(&vcpu->preempt_notifier);
355 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 mutex_unlock(&vcpu->mutex);
357}
358
Avi Kivityd9e368d2007-06-07 19:18:30 +0300359static void ack_flush(void *_completed)
360{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300361}
362
363void kvm_flush_remote_tlbs(struct kvm *kvm)
364{
Avi Kivity597a5f52008-07-20 14:24:22 +0300365 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300366 cpumask_t cpus;
367 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300368
Avi Kivity597a5f52008-07-20 14:24:22 +0300369 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300370 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000371 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
372 vcpu = kvm->vcpus[i];
373 if (!vcpu)
374 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200375 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300376 continue;
377 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300378 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200379 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300380 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200381 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300382 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200383 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200384 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300385out:
386 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300387}
388
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500389void kvm_reload_remote_mmus(struct kvm *kvm)
390{
Avi Kivity597a5f52008-07-20 14:24:22 +0300391 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500392 cpumask_t cpus;
393 struct kvm_vcpu *vcpu;
394
Avi Kivity597a5f52008-07-20 14:24:22 +0300395 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500396 cpus_clear(cpus);
397 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
398 vcpu = kvm->vcpus[i];
399 if (!vcpu)
400 continue;
401 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
402 continue;
403 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300404 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500405 cpu_set(cpu, cpus);
406 }
407 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300408 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500409 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300410out:
411 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500412}
413
414
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000415int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
416{
417 struct page *page;
418 int r;
419
420 mutex_init(&vcpu->mutex);
421 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000422 vcpu->kvm = kvm;
423 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300424 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000425
426 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
427 if (!page) {
428 r = -ENOMEM;
429 goto fail;
430 }
431 vcpu->run = page_address(page);
432
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800433 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000434 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800435 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000436 return 0;
437
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000438fail_free_run:
439 free_page((unsigned long)vcpu->run);
440fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000441 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000442}
443EXPORT_SYMBOL_GPL(kvm_vcpu_init);
444
445void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
446{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800447 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000448 free_page((unsigned long)vcpu->run);
449}
450EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
451
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200452#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
453static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
454{
455 return container_of(mn, struct kvm, mmu_notifier);
456}
457
458static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
459 struct mm_struct *mm,
460 unsigned long address)
461{
462 struct kvm *kvm = mmu_notifier_to_kvm(mn);
463 int need_tlb_flush;
464
465 /*
466 * When ->invalidate_page runs, the linux pte has been zapped
467 * already but the page is still allocated until
468 * ->invalidate_page returns. So if we increase the sequence
469 * here the kvm page fault will notice if the spte can't be
470 * established because the page is going to be freed. If
471 * instead the kvm page fault establishes the spte before
472 * ->invalidate_page runs, kvm_unmap_hva will release it
473 * before returning.
474 *
475 * The sequence increase only need to be seen at spin_unlock
476 * time, and not at spin_lock time.
477 *
478 * Increasing the sequence after the spin_unlock would be
479 * unsafe because the kvm page fault could then establish the
480 * pte after kvm_unmap_hva returned, without noticing the page
481 * is going to be freed.
482 */
483 spin_lock(&kvm->mmu_lock);
484 kvm->mmu_notifier_seq++;
485 need_tlb_flush = kvm_unmap_hva(kvm, address);
486 spin_unlock(&kvm->mmu_lock);
487
488 /* we've to flush the tlb before the pages can be freed */
489 if (need_tlb_flush)
490 kvm_flush_remote_tlbs(kvm);
491
492}
493
494static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
495 struct mm_struct *mm,
496 unsigned long start,
497 unsigned long end)
498{
499 struct kvm *kvm = mmu_notifier_to_kvm(mn);
500 int need_tlb_flush = 0;
501
502 spin_lock(&kvm->mmu_lock);
503 /*
504 * The count increase must become visible at unlock time as no
505 * spte can be established without taking the mmu_lock and
506 * count is also read inside the mmu_lock critical section.
507 */
508 kvm->mmu_notifier_count++;
509 for (; start < end; start += PAGE_SIZE)
510 need_tlb_flush |= kvm_unmap_hva(kvm, start);
511 spin_unlock(&kvm->mmu_lock);
512
513 /* we've to flush the tlb before the pages can be freed */
514 if (need_tlb_flush)
515 kvm_flush_remote_tlbs(kvm);
516}
517
518static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
519 struct mm_struct *mm,
520 unsigned long start,
521 unsigned long end)
522{
523 struct kvm *kvm = mmu_notifier_to_kvm(mn);
524
525 spin_lock(&kvm->mmu_lock);
526 /*
527 * This sequence increase will notify the kvm page fault that
528 * the page that is going to be mapped in the spte could have
529 * been freed.
530 */
531 kvm->mmu_notifier_seq++;
532 /*
533 * The above sequence increase must be visible before the
534 * below count decrease but both values are read by the kvm
535 * page fault under mmu_lock spinlock so we don't need to add
536 * a smb_wmb() here in between the two.
537 */
538 kvm->mmu_notifier_count--;
539 spin_unlock(&kvm->mmu_lock);
540
541 BUG_ON(kvm->mmu_notifier_count < 0);
542}
543
544static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
545 struct mm_struct *mm,
546 unsigned long address)
547{
548 struct kvm *kvm = mmu_notifier_to_kvm(mn);
549 int young;
550
551 spin_lock(&kvm->mmu_lock);
552 young = kvm_age_hva(kvm, address);
553 spin_unlock(&kvm->mmu_lock);
554
555 if (young)
556 kvm_flush_remote_tlbs(kvm);
557
558 return young;
559}
560
561static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
562 .invalidate_page = kvm_mmu_notifier_invalidate_page,
563 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
564 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
565 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
566};
567#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
568
Avi Kivityf17abe92007-02-21 19:28:04 +0200569static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800571 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200572#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
573 struct page *page;
574#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800576 if (IS_ERR(kvm))
577 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578
Laurent Vivier5f94c172008-05-30 16:05:54 +0200579#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
580 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
581 if (!page) {
582 kfree(kvm);
583 return ERR_PTR(-ENOMEM);
584 }
585 kvm->coalesced_mmio_ring =
586 (struct kvm_coalesced_mmio_ring *)page_address(page);
587#endif
588
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200589#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
590 {
591 int err;
592 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
593 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
594 if (err) {
595#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
596 put_page(page);
597#endif
598 kfree(kvm);
599 return ERR_PTR(err);
600 }
601 }
602#endif
603
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200604 kvm->mm = current->mm;
605 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500606 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300607 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800608 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400609 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200610 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300611 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000612 spin_lock(&kvm_lock);
613 list_add(&kvm->vm_list, &vm_list);
614 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200615#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
616 kvm_coalesced_mmio_init(kvm);
617#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800618out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200619 return kvm;
620}
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622/*
623 * Free any memory in @free but not in @dont.
624 */
625static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
626 struct kvm_memory_slot *dont)
627{
Izik Eidus290fc382007-09-27 14:11:22 +0200628 if (!dont || free->rmap != dont->rmap)
629 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630
631 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
632 vfree(free->dirty_bitmap);
633
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300634 if (!dont || free->lpage_info != dont->lpage_info)
635 vfree(free->lpage_info);
636
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000638 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500639 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300640 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641}
642
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800643void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644{
645 int i;
646
647 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000648 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649}
650
Avi Kivityf17abe92007-02-21 19:28:04 +0200651static void kvm_destroy_vm(struct kvm *kvm)
652{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200653 struct mm_struct *mm = kvm->mm;
654
Avi Kivity133de902007-02-12 00:54:44 -0800655 spin_lock(&kvm_lock);
656 list_del(&kvm->vm_list);
657 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300658 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400659 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200660#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
661 if (kvm->coalesced_mmio_ring != NULL)
662 free_page((unsigned long)kvm->coalesced_mmio_ring);
663#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200664#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
665 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
666#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800667 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200668 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200669}
670
Izik Eidusd39f13b2008-03-30 16:01:25 +0300671void kvm_get_kvm(struct kvm *kvm)
672{
673 atomic_inc(&kvm->users_count);
674}
675EXPORT_SYMBOL_GPL(kvm_get_kvm);
676
677void kvm_put_kvm(struct kvm *kvm)
678{
679 if (atomic_dec_and_test(&kvm->users_count))
680 kvm_destroy_vm(kvm);
681}
682EXPORT_SYMBOL_GPL(kvm_put_kvm);
683
684
Avi Kivityf17abe92007-02-21 19:28:04 +0200685static int kvm_vm_release(struct inode *inode, struct file *filp)
686{
687 struct kvm *kvm = filp->private_data;
688
Izik Eidusd39f13b2008-03-30 16:01:25 +0300689 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 return 0;
691}
692
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694 * Allocate some memory and give it an address in the guest physical address
695 * space.
696 *
697 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800698 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500699 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800701int __kvm_set_memory_region(struct kvm *kvm,
702 struct kvm_userspace_memory_region *mem,
703 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704{
705 int r;
706 gfn_t base_gfn;
707 unsigned long npages;
708 unsigned long i;
709 struct kvm_memory_slot *memslot;
710 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711
712 r = -EINVAL;
713 /* General sanity checks */
714 if (mem->memory_size & (PAGE_SIZE - 1))
715 goto out;
716 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
717 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200718 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 goto out;
720 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
721 goto out;
722
723 memslot = &kvm->memslots[mem->slot];
724 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
725 npages = mem->memory_size >> PAGE_SHIFT;
726
727 if (!npages)
728 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
729
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 new = old = *memslot;
731
732 new.base_gfn = base_gfn;
733 new.npages = npages;
734 new.flags = mem->flags;
735
736 /* Disallow changing a memory slot's size. */
737 r = -EINVAL;
738 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800739 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740
741 /* Check for overlaps */
742 r = -EEXIST;
743 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
744 struct kvm_memory_slot *s = &kvm->memslots[i];
745
746 if (s == memslot)
747 continue;
748 if (!((base_gfn + npages <= s->base_gfn) ||
749 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800750 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 /* Free page dirty bitmap if unneeded */
754 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000755 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756
757 r = -ENOMEM;
758
759 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200760#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500761 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400762 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200763
764 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800765 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200766
Izik Eidus290fc382007-09-27 14:11:22 +0200767 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500768
Izik Eidus80b14b52007-10-25 11:54:04 +0200769 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200770 /*
771 * hva_to_rmmap() serialzies with the mmu_lock and to be
772 * safe it has to ignore memslots with !user_alloc &&
773 * !userspace_addr.
774 */
775 if (user_alloc)
776 new.userspace_addr = mem->userspace_addr;
777 else
778 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300780 if (npages && !new.lpage_info) {
781 int largepages = npages / KVM_PAGES_PER_HPAGE;
782 if (npages % KVM_PAGES_PER_HPAGE)
783 largepages++;
784 if (base_gfn % KVM_PAGES_PER_HPAGE)
785 largepages++;
786
787 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
788
789 if (!new.lpage_info)
790 goto out_free;
791
792 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
793
794 if (base_gfn % KVM_PAGES_PER_HPAGE)
795 new.lpage_info[0].write_count = 1;
796 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
797 new.lpage_info[largepages-1].write_count = 1;
798 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799
800 /* Allocate page dirty bitmap if needed */
801 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
802 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
803
804 new.dirty_bitmap = vmalloc(dirty_bytes);
805 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800806 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 memset(new.dirty_bitmap, 0, dirty_bytes);
808 }
Carsten Otteeff01142008-06-27 15:05:31 +0200809#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300811 if (!npages)
812 kvm_arch_flush_shadow(kvm);
813
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200814 spin_lock(&kvm->mmu_lock);
815 if (mem->slot >= kvm->nmemslots)
816 kvm->nmemslots = mem->slot + 1;
817
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200819 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820
Zhang Xiantao0de10342007-11-20 16:25:04 +0800821 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
822 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200823 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800824 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200825 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800826 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800827 }
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 kvm_free_physmem_slot(&old, &new);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800830#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300831 /* map the pages in iommu page table */
832 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
833 if (r)
834 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800835#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 return 0;
837
Sheng Yangf78e0e22007-10-29 09:40:42 +0800838out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 kvm_free_physmem_slot(&new, &old);
840out:
841 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200842
843}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800844EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
845
846int kvm_set_memory_region(struct kvm *kvm,
847 struct kvm_userspace_memory_region *mem,
848 int user_alloc)
849{
850 int r;
851
Izik Eidus72dc67a2008-02-10 18:04:15 +0200852 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800853 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200854 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800855 return r;
856}
Izik Eidus210c7c42007-10-24 23:52:57 +0200857EXPORT_SYMBOL_GPL(kvm_set_memory_region);
858
Carsten Otte1fe779f2007-10-29 16:08:35 +0100859int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
860 struct
861 kvm_userspace_memory_region *mem,
862 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200863{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200864 if (mem->slot >= KVM_MEMORY_SLOTS)
865 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200866 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867}
868
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800869int kvm_get_dirty_log(struct kvm *kvm,
870 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871{
872 struct kvm_memory_slot *memslot;
873 int r, i;
874 int n;
875 unsigned long any = 0;
876
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 r = -EINVAL;
878 if (log->slot >= KVM_MEMORY_SLOTS)
879 goto out;
880
881 memslot = &kvm->memslots[log->slot];
882 r = -ENOENT;
883 if (!memslot->dirty_bitmap)
884 goto out;
885
Uri Lublincd1a4a92007-02-22 16:43:09 +0200886 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887
Uri Lublincd1a4a92007-02-22 16:43:09 +0200888 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 any = memslot->dirty_bitmap[i];
890
891 r = -EFAULT;
892 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
893 goto out;
894
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800895 if (any)
896 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897
898 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 return r;
901}
902
Izik Eiduscea7bb22007-10-17 19:17:48 +0200903int is_error_page(struct page *page)
904{
905 return page == bad_page;
906}
907EXPORT_SYMBOL_GPL(is_error_page);
908
Anthony Liguori35149e22008-04-02 14:46:56 -0500909int is_error_pfn(pfn_t pfn)
910{
911 return pfn == bad_pfn;
912}
913EXPORT_SYMBOL_GPL(is_error_pfn);
914
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200915static inline unsigned long bad_hva(void)
916{
917 return PAGE_OFFSET;
918}
919
920int kvm_is_error_hva(unsigned long addr)
921{
922 return addr == bad_hva();
923}
924EXPORT_SYMBOL_GPL(kvm_is_error_hva);
925
Avi Kivitye8207542007-03-30 16:54:30 +0300926static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927{
928 int i;
929
930 for (i = 0; i < kvm->nmemslots; ++i) {
931 struct kvm_memory_slot *memslot = &kvm->memslots[i];
932
933 if (gfn >= memslot->base_gfn
934 && gfn < memslot->base_gfn + memslot->npages)
935 return memslot;
936 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000937 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938}
Avi Kivitye8207542007-03-30 16:54:30 +0300939
940struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
941{
942 gfn = unalias_gfn(kvm, gfn);
943 return __gfn_to_memslot(kvm, gfn);
944}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Izik Eiduse0d62c72007-10-24 23:57:46 +0200946int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
947{
948 int i;
949
950 gfn = unalias_gfn(kvm, gfn);
951 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
952 struct kvm_memory_slot *memslot = &kvm->memslots[i];
953
954 if (gfn >= memslot->base_gfn
955 && gfn < memslot->base_gfn + memslot->npages)
956 return 1;
957 }
958 return 0;
959}
960EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
961
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300962unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200963{
964 struct kvm_memory_slot *slot;
965
966 gfn = unalias_gfn(kvm, gfn);
967 slot = __gfn_to_memslot(kvm, gfn);
968 if (!slot)
969 return bad_hva();
970 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
971}
Sheng Yang0d150292008-04-25 21:44:50 +0800972EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200973
Anthony Liguori35149e22008-04-02 14:46:56 -0500974pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300975{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500976 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200977 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500978 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500979 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300980
Avi Kivity60395222007-10-21 11:03:36 +0200981 might_sleep();
982
Izik Eidus539cb662007-11-11 22:05:04 +0200983 addr = gfn_to_hva(kvm, gfn);
984 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200985 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500986 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200987 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200988
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300989 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200990
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500991 if (unlikely(npages != 1)) {
992 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500993
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300994 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500995 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300996
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500997 if (vma == NULL || addr < vma->vm_start ||
998 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300999 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001000 get_page(bad_page);
1001 return page_to_pfn(bad_page);
1002 }
1003
1004 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001005 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001006 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001007 } else
1008 pfn = page_to_pfn(page[0]);
1009
1010 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001011}
1012
1013EXPORT_SYMBOL_GPL(gfn_to_pfn);
1014
1015struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1016{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001017 pfn_t pfn;
1018
1019 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001020 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001021 return pfn_to_page(pfn);
1022
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001023 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001024
1025 get_page(bad_page);
1026 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001027}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001028
Avi Kivity954bbbc2007-03-30 14:02:32 +03001029EXPORT_SYMBOL_GPL(gfn_to_page);
1030
Izik Eidusb4231d62007-11-20 11:49:33 +02001031void kvm_release_page_clean(struct page *page)
1032{
Anthony Liguori35149e22008-04-02 14:46:56 -05001033 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001034}
1035EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1036
Anthony Liguori35149e22008-04-02 14:46:56 -05001037void kvm_release_pfn_clean(pfn_t pfn)
1038{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001039 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001040 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001041}
1042EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1043
Izik Eidusb4231d62007-11-20 11:49:33 +02001044void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001045{
Anthony Liguori35149e22008-04-02 14:46:56 -05001046 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001047}
Izik Eidusb4231d62007-11-20 11:49:33 +02001048EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001049
Anthony Liguori35149e22008-04-02 14:46:56 -05001050void kvm_release_pfn_dirty(pfn_t pfn)
1051{
1052 kvm_set_pfn_dirty(pfn);
1053 kvm_release_pfn_clean(pfn);
1054}
1055EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1056
1057void kvm_set_page_dirty(struct page *page)
1058{
1059 kvm_set_pfn_dirty(page_to_pfn(page));
1060}
1061EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1062
1063void kvm_set_pfn_dirty(pfn_t pfn)
1064{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001065 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001066 struct page *page = pfn_to_page(pfn);
1067 if (!PageReserved(page))
1068 SetPageDirty(page);
1069 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001070}
1071EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1072
1073void kvm_set_pfn_accessed(pfn_t pfn)
1074{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001075 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001076 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001077}
1078EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1079
1080void kvm_get_pfn(pfn_t pfn)
1081{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001082 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001083 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001084}
1085EXPORT_SYMBOL_GPL(kvm_get_pfn);
1086
Izik Eidus195aefd2007-10-01 22:14:18 +02001087static int next_segment(unsigned long len, int offset)
1088{
1089 if (len > PAGE_SIZE - offset)
1090 return PAGE_SIZE - offset;
1091 else
1092 return len;
1093}
1094
1095int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1096 int len)
1097{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001098 int r;
1099 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001100
Izik Eiduse0506bc2007-11-11 22:10:22 +02001101 addr = gfn_to_hva(kvm, gfn);
1102 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001103 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001104 r = copy_from_user(data, (void __user *)addr + offset, len);
1105 if (r)
1106 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001107 return 0;
1108}
1109EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1110
1111int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1112{
1113 gfn_t gfn = gpa >> PAGE_SHIFT;
1114 int seg;
1115 int offset = offset_in_page(gpa);
1116 int ret;
1117
1118 while ((seg = next_segment(len, offset)) != 0) {
1119 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1120 if (ret < 0)
1121 return ret;
1122 offset = 0;
1123 len -= seg;
1124 data += seg;
1125 ++gfn;
1126 }
1127 return 0;
1128}
1129EXPORT_SYMBOL_GPL(kvm_read_guest);
1130
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001131int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1132 unsigned long len)
1133{
1134 int r;
1135 unsigned long addr;
1136 gfn_t gfn = gpa >> PAGE_SHIFT;
1137 int offset = offset_in_page(gpa);
1138
1139 addr = gfn_to_hva(kvm, gfn);
1140 if (kvm_is_error_hva(addr))
1141 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001142 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001143 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001144 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001145 if (r)
1146 return -EFAULT;
1147 return 0;
1148}
1149EXPORT_SYMBOL(kvm_read_guest_atomic);
1150
Izik Eidus195aefd2007-10-01 22:14:18 +02001151int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1152 int offset, int len)
1153{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001154 int r;
1155 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001156
Izik Eiduse0506bc2007-11-11 22:10:22 +02001157 addr = gfn_to_hva(kvm, gfn);
1158 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001159 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001160 r = copy_to_user((void __user *)addr + offset, data, len);
1161 if (r)
1162 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001163 mark_page_dirty(kvm, gfn);
1164 return 0;
1165}
1166EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1167
1168int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1169 unsigned long len)
1170{
1171 gfn_t gfn = gpa >> PAGE_SHIFT;
1172 int seg;
1173 int offset = offset_in_page(gpa);
1174 int ret;
1175
1176 while ((seg = next_segment(len, offset)) != 0) {
1177 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1178 if (ret < 0)
1179 return ret;
1180 offset = 0;
1181 len -= seg;
1182 data += seg;
1183 ++gfn;
1184 }
1185 return 0;
1186}
1187
1188int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1189{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001190 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001191}
1192EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1193
1194int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1195{
1196 gfn_t gfn = gpa >> PAGE_SHIFT;
1197 int seg;
1198 int offset = offset_in_page(gpa);
1199 int ret;
1200
1201 while ((seg = next_segment(len, offset)) != 0) {
1202 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1203 if (ret < 0)
1204 return ret;
1205 offset = 0;
1206 len -= seg;
1207 ++gfn;
1208 }
1209 return 0;
1210}
1211EXPORT_SYMBOL_GPL(kvm_clear_guest);
1212
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1214{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001215 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216
Uri Lublin3b6fff12007-10-30 10:42:09 +02001217 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001218 memslot = __gfn_to_memslot(kvm, gfn);
1219 if (memslot && memslot->dirty_bitmap) {
1220 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221
Rusty Russell7e9d6192007-07-31 20:41:14 +10001222 /* avoid RMW */
1223 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1224 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 }
1226}
1227
Eddie Dongb6958ce2007-07-18 12:15:21 +03001228/*
1229 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1230 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001231void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001232{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001233 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001234
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001235 for (;;) {
1236 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001237
Marcelo Tosattid7690172008-09-08 15:23:48 -03001238 if (kvm_cpu_has_interrupt(vcpu) ||
1239 kvm_cpu_has_pending_timer(vcpu) ||
1240 kvm_arch_vcpu_runnable(vcpu)) {
1241 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001242 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001243 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001244 if (signal_pending(current))
1245 break;
1246
Eddie Dongb6958ce2007-07-18 12:15:21 +03001247 vcpu_put(vcpu);
1248 schedule();
1249 vcpu_load(vcpu);
1250 }
1251
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001252 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001253}
1254
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255void kvm_resched(struct kvm_vcpu *vcpu)
1256{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001257 if (!need_resched())
1258 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260}
1261EXPORT_SYMBOL_GPL(kvm_resched);
1262
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001263static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001264{
1265 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001266 struct page *page;
1267
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001268 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001269 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001270#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001271 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001272 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001273#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001274#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1275 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1276 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1277#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001278 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001279 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001280 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001281 vmf->page = page;
1282 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001283}
1284
1285static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001286 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001287};
1288
1289static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1290{
1291 vma->vm_ops = &kvm_vcpu_vm_ops;
1292 return 0;
1293}
1294
Avi Kivitybccf2152007-02-21 18:04:26 +02001295static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1296{
1297 struct kvm_vcpu *vcpu = filp->private_data;
1298
Al Viro66c0b392008-04-19 20:33:56 +01001299 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001300 return 0;
1301}
1302
Jan Engelhardt5c502742008-01-22 20:46:14 +01001303static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001304 .release = kvm_vcpu_release,
1305 .unlocked_ioctl = kvm_vcpu_ioctl,
1306 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001307 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001308};
1309
1310/*
1311 * Allocates an inode for the vcpu.
1312 */
1313static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1314{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001315 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001316 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001317 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001318 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001319}
1320
Avi Kivityc5ea7662007-02-20 18:41:05 +02001321/*
1322 * Creates some virtual cpus. Good luck creating more than one.
1323 */
1324static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1325{
1326 int r;
1327 struct kvm_vcpu *vcpu;
1328
Avi Kivityc5ea7662007-02-20 18:41:05 +02001329 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001330 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001331
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001332 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001333 if (IS_ERR(vcpu))
1334 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001335
Avi Kivity15ad7142007-07-11 18:17:21 +03001336 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1337
Avi Kivity26e52152007-11-20 15:30:24 +02001338 r = kvm_arch_vcpu_setup(vcpu);
1339 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001340 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001341
Shaohua Li11ec2802007-07-23 14:51:37 +08001342 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001343 if (kvm->vcpus[n]) {
1344 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001345 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001346 }
1347 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001348 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001349
1350 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001351 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001352 r = create_vcpu_fd(vcpu);
1353 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001354 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001355 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001356
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001357unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001358 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001359 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001360vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001361 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001362 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001363 return r;
1364}
1365
Avi Kivity1961d272007-03-05 19:46:05 +02001366static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1367{
1368 if (sigset) {
1369 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1370 vcpu->sigset_active = 1;
1371 vcpu->sigset = *sigset;
1372 } else
1373 vcpu->sigset_active = 0;
1374 return 0;
1375}
1376
Avi Kivitybccf2152007-02-21 18:04:26 +02001377static long kvm_vcpu_ioctl(struct file *filp,
1378 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379{
Avi Kivitybccf2152007-02-21 18:04:26 +02001380 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001381 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001382 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001383 struct kvm_fpu *fpu = NULL;
1384 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001386 if (vcpu->kvm->mm != current->mm)
1387 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001389 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001390 r = -EINVAL;
1391 if (arg)
1392 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001393 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001396 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001398 r = -ENOMEM;
1399 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1400 if (!kvm_regs)
1401 goto out;
1402 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001404 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001406 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1407 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001409out_free1:
1410 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 break;
1412 }
1413 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001414 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001416 r = -ENOMEM;
1417 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1418 if (!kvm_regs)
1419 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001421 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1422 goto out_free2;
1423 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001425 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001427out_free2:
1428 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 break;
1430 }
1431 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001432 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1433 r = -ENOMEM;
1434 if (!kvm_sregs)
1435 goto out;
1436 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 if (r)
1438 goto out;
1439 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001440 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 goto out;
1442 r = 0;
1443 break;
1444 }
1445 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001446 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1447 r = -ENOMEM;
1448 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001450 r = -EFAULT;
1451 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1452 goto out;
1453 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 if (r)
1455 goto out;
1456 r = 0;
1457 break;
1458 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001459 case KVM_GET_MP_STATE: {
1460 struct kvm_mp_state mp_state;
1461
1462 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1463 if (r)
1464 goto out;
1465 r = -EFAULT;
1466 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1467 goto out;
1468 r = 0;
1469 break;
1470 }
1471 case KVM_SET_MP_STATE: {
1472 struct kvm_mp_state mp_state;
1473
1474 r = -EFAULT;
1475 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1476 goto out;
1477 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1478 if (r)
1479 goto out;
1480 r = 0;
1481 break;
1482 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 case KVM_TRANSLATE: {
1484 struct kvm_translation tr;
1485
1486 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001487 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001489 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490 if (r)
1491 goto out;
1492 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001493 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 goto out;
1495 r = 0;
1496 break;
1497 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 case KVM_DEBUG_GUEST: {
1499 struct kvm_debug_guest dbg;
1500
1501 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001502 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001504 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 if (r)
1506 goto out;
1507 r = 0;
1508 break;
1509 }
Avi Kivity1961d272007-03-05 19:46:05 +02001510 case KVM_SET_SIGNAL_MASK: {
1511 struct kvm_signal_mask __user *sigmask_arg = argp;
1512 struct kvm_signal_mask kvm_sigmask;
1513 sigset_t sigset, *p;
1514
1515 p = NULL;
1516 if (argp) {
1517 r = -EFAULT;
1518 if (copy_from_user(&kvm_sigmask, argp,
1519 sizeof kvm_sigmask))
1520 goto out;
1521 r = -EINVAL;
1522 if (kvm_sigmask.len != sizeof sigset)
1523 goto out;
1524 r = -EFAULT;
1525 if (copy_from_user(&sigset, sigmask_arg->sigset,
1526 sizeof sigset))
1527 goto out;
1528 p = &sigset;
1529 }
1530 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1531 break;
1532 }
Avi Kivityb8836732007-04-01 16:34:31 +03001533 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001534 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1535 r = -ENOMEM;
1536 if (!fpu)
1537 goto out;
1538 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001539 if (r)
1540 goto out;
1541 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001542 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001543 goto out;
1544 r = 0;
1545 break;
1546 }
1547 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001548 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1549 r = -ENOMEM;
1550 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001551 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001552 r = -EFAULT;
1553 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1554 goto out;
1555 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001556 if (r)
1557 goto out;
1558 r = 0;
1559 break;
1560 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001561 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001562 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001563 }
1564out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001565 kfree(fpu);
1566 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001567 return r;
1568}
1569
1570static long kvm_vm_ioctl(struct file *filp,
1571 unsigned int ioctl, unsigned long arg)
1572{
1573 struct kvm *kvm = filp->private_data;
1574 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001575 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001576
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001577 if (kvm->mm != current->mm)
1578 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001579 switch (ioctl) {
1580 case KVM_CREATE_VCPU:
1581 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1582 if (r < 0)
1583 goto out;
1584 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001585 case KVM_SET_USER_MEMORY_REGION: {
1586 struct kvm_userspace_memory_region kvm_userspace_mem;
1587
1588 r = -EFAULT;
1589 if (copy_from_user(&kvm_userspace_mem, argp,
1590 sizeof kvm_userspace_mem))
1591 goto out;
1592
1593 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 if (r)
1595 goto out;
1596 break;
1597 }
1598 case KVM_GET_DIRTY_LOG: {
1599 struct kvm_dirty_log log;
1600
1601 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001602 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001604 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605 if (r)
1606 goto out;
1607 break;
1608 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001609#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1610 case KVM_REGISTER_COALESCED_MMIO: {
1611 struct kvm_coalesced_mmio_zone zone;
1612 r = -EFAULT;
1613 if (copy_from_user(&zone, argp, sizeof zone))
1614 goto out;
1615 r = -ENXIO;
1616 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1617 if (r)
1618 goto out;
1619 r = 0;
1620 break;
1621 }
1622 case KVM_UNREGISTER_COALESCED_MMIO: {
1623 struct kvm_coalesced_mmio_zone zone;
1624 r = -EFAULT;
1625 if (copy_from_user(&zone, argp, sizeof zone))
1626 goto out;
1627 r = -ENXIO;
1628 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1629 if (r)
1630 goto out;
1631 r = 0;
1632 break;
1633 }
1634#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001635#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1636 case KVM_ASSIGN_PCI_DEVICE: {
1637 struct kvm_assigned_pci_dev assigned_dev;
1638
1639 r = -EFAULT;
1640 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1641 goto out;
1642 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1643 if (r)
1644 goto out;
1645 break;
1646 }
1647 case KVM_ASSIGN_IRQ: {
1648 struct kvm_assigned_irq assigned_irq;
1649
1650 r = -EFAULT;
1651 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1652 goto out;
1653 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1654 if (r)
1655 goto out;
1656 break;
1657 }
1658#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001659 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001660 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001661 }
1662out:
1663 return r;
1664}
1665
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001666static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001667{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001668 struct page *page[1];
1669 unsigned long addr;
1670 int npages;
1671 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001672 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001673
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001674 addr = gfn_to_hva(kvm, gfn);
1675 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001676 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001677
1678 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1679 NULL);
1680 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001681 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001682
1683 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001684 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001685}
1686
1687static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001688 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001689};
1690
1691static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1692{
1693 vma->vm_ops = &kvm_vm_vm_ops;
1694 return 0;
1695}
1696
Jan Engelhardt5c502742008-01-22 20:46:14 +01001697static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001698 .release = kvm_vm_release,
1699 .unlocked_ioctl = kvm_vm_ioctl,
1700 .compat_ioctl = kvm_vm_ioctl,
1701 .mmap = kvm_vm_mmap,
1702};
1703
1704static int kvm_dev_ioctl_create_vm(void)
1705{
Al Viro2030a422008-02-23 06:46:49 -05001706 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001707 struct kvm *kvm;
1708
Avi Kivityf17abe92007-02-21 19:28:04 +02001709 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001710 if (IS_ERR(kvm))
1711 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001712 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001713 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001714 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001715
Avi Kivityf17abe92007-02-21 19:28:04 +02001716 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001717}
1718
1719static long kvm_dev_ioctl(struct file *filp,
1720 unsigned int ioctl, unsigned long arg)
1721{
Avi Kivity07c45a32007-03-07 13:05:38 +02001722 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001723
1724 switch (ioctl) {
1725 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001726 r = -EINVAL;
1727 if (arg)
1728 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001729 r = KVM_API_VERSION;
1730 break;
1731 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001732 r = -EINVAL;
1733 if (arg)
1734 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001735 r = kvm_dev_ioctl_create_vm();
1736 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001737 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001738 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001739 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001740 case KVM_GET_VCPU_MMAP_SIZE:
1741 r = -EINVAL;
1742 if (arg)
1743 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001744 r = PAGE_SIZE; /* struct kvm_run */
1745#ifdef CONFIG_X86
1746 r += PAGE_SIZE; /* pio data page */
1747#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001748#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1749 r += PAGE_SIZE; /* coalesced mmio ring page */
1750#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001751 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001752 case KVM_TRACE_ENABLE:
1753 case KVM_TRACE_PAUSE:
1754 case KVM_TRACE_DISABLE:
1755 r = kvm_trace_ioctl(ioctl, arg);
1756 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001758 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 }
1760out:
1761 return r;
1762}
1763
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765 .unlocked_ioctl = kvm_dev_ioctl,
1766 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767};
1768
1769static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001770 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771 "kvm",
1772 &kvm_chardev_ops,
1773};
1774
Avi Kivity1b6c0162007-05-24 13:03:52 +03001775static void hardware_enable(void *junk)
1776{
1777 int cpu = raw_smp_processor_id();
1778
1779 if (cpu_isset(cpu, cpus_hardware_enabled))
1780 return;
1781 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001782 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001783}
1784
1785static void hardware_disable(void *junk)
1786{
1787 int cpu = raw_smp_processor_id();
1788
1789 if (!cpu_isset(cpu, cpus_hardware_enabled))
1790 return;
1791 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001792 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001793}
1794
Avi Kivity774c47f2007-02-12 00:54:47 -08001795static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1796 void *v)
1797{
1798 int cpu = (long)v;
1799
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001800 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001801 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001802 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001803 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1804 cpu);
1805 hardware_disable(NULL);
1806 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001807 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001808 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1809 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001810 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001811 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001812 case CPU_ONLINE:
1813 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1814 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001815 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001816 break;
1817 }
1818 return NOTIFY_OK;
1819}
1820
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001821
1822asmlinkage void kvm_handle_fault_on_reboot(void)
1823{
1824 if (kvm_rebooting)
1825 /* spin while reset goes on */
1826 while (true)
1827 ;
1828 /* Fault while not rebooting. We want the trace. */
1829 BUG();
1830}
1831EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1832
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001833static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001834 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001835{
1836 if (val == SYS_RESTART) {
1837 /*
1838 * Some (well, at least mine) BIOSes hang on reboot if
1839 * in vmx root mode.
1840 */
1841 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001842 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001843 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001844 }
1845 return NOTIFY_OK;
1846}
1847
1848static struct notifier_block kvm_reboot_notifier = {
1849 .notifier_call = kvm_reboot,
1850 .priority = 0,
1851};
1852
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001853void kvm_io_bus_init(struct kvm_io_bus *bus)
1854{
1855 memset(bus, 0, sizeof(*bus));
1856}
1857
1858void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1859{
1860 int i;
1861
1862 for (i = 0; i < bus->dev_count; i++) {
1863 struct kvm_io_device *pos = bus->devs[i];
1864
1865 kvm_iodevice_destructor(pos);
1866 }
1867}
1868
Laurent Vivier92760492008-05-30 16:05:53 +02001869struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1870 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001871{
1872 int i;
1873
1874 for (i = 0; i < bus->dev_count; i++) {
1875 struct kvm_io_device *pos = bus->devs[i];
1876
Laurent Vivier92760492008-05-30 16:05:53 +02001877 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001878 return pos;
1879 }
1880
1881 return NULL;
1882}
1883
1884void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1885{
1886 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1887
1888 bus->devs[bus->dev_count++] = dev;
1889}
1890
Avi Kivity774c47f2007-02-12 00:54:47 -08001891static struct notifier_block kvm_cpu_notifier = {
1892 .notifier_call = kvm_cpu_hotplug,
1893 .priority = 20, /* must be > scheduler priority */
1894};
1895
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001896static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001897{
1898 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001899 struct kvm *kvm;
1900
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001901 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001902 spin_lock(&kvm_lock);
1903 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001904 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001905 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001906 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001907}
1908
1909DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1910
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001911static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001912{
1913 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001914 struct kvm *kvm;
1915 struct kvm_vcpu *vcpu;
1916 int i;
1917
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001918 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001919 spin_lock(&kvm_lock);
1920 list_for_each_entry(kvm, &vm_list, vm_list)
1921 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001922 vcpu = kvm->vcpus[i];
1923 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001924 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001925 }
1926 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001927 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001928}
1929
Avi Kivityba1389b2007-11-18 16:24:12 +02001930DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1931
1932static struct file_operations *stat_fops[] = {
1933 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1934 [KVM_STAT_VM] = &vm_stat_fops,
1935};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001936
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001937static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938{
1939 struct kvm_stats_debugfs_item *p;
1940
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001941 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001943 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001944 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001945 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946}
1947
1948static void kvm_exit_debug(void)
1949{
1950 struct kvm_stats_debugfs_item *p;
1951
1952 for (p = debugfs_entries; p->name; ++p)
1953 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001954 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955}
1956
Avi Kivity59ae6c62007-02-12 00:54:48 -08001957static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1958{
Avi Kivity4267c412007-05-24 13:09:41 +03001959 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001960 return 0;
1961}
1962
1963static int kvm_resume(struct sys_device *dev)
1964{
Avi Kivity4267c412007-05-24 13:09:41 +03001965 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001966 return 0;
1967}
1968
1969static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001970 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001971 .suspend = kvm_suspend,
1972 .resume = kvm_resume,
1973};
1974
1975static struct sys_device kvm_sysdev = {
1976 .id = 0,
1977 .cls = &kvm_sysdev_class,
1978};
1979
Izik Eiduscea7bb22007-10-17 19:17:48 +02001980struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001981pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982
Avi Kivity15ad7142007-07-11 18:17:21 +03001983static inline
1984struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1985{
1986 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1987}
1988
1989static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1990{
1991 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1992
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001993 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001994}
1995
1996static void kvm_sched_out(struct preempt_notifier *pn,
1997 struct task_struct *next)
1998{
1999 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2000
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002001 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002002}
2003
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002004int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002005 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006{
2007 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002008 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002009
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002010 kvm_init_debug();
2011
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002012 r = kvm_arch_init(opaque);
2013 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002014 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002015
2016 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2017
2018 if (bad_page == NULL) {
2019 r = -ENOMEM;
2020 goto out;
2021 }
2022
Anthony Liguori35149e22008-04-02 14:46:56 -05002023 bad_pfn = page_to_pfn(bad_page);
2024
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002025 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002027 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028
Yang, Sheng002c7f72007-07-31 14:23:01 +03002029 for_each_online_cpu(cpu) {
2030 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002031 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002032 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002033 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002034 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002035 }
2036
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002037 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002038 r = register_cpu_notifier(&kvm_cpu_notifier);
2039 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002040 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 register_reboot_notifier(&kvm_reboot_notifier);
2042
Avi Kivity59ae6c62007-02-12 00:54:48 -08002043 r = sysdev_class_register(&kvm_sysdev_class);
2044 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002045 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002046
2047 r = sysdev_register(&kvm_sysdev);
2048 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002049 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002050
Rusty Russellc16f8622007-07-30 21:12:19 +10002051 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2052 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002053 __alignof__(struct kvm_vcpu),
2054 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002055 if (!kvm_vcpu_cache) {
2056 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002057 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002058 }
2059
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 kvm_chardev_ops.owner = module;
2061
2062 r = misc_register(&kvm_dev);
2063 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002064 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065 goto out_free;
2066 }
2067
Avi Kivity15ad7142007-07-11 18:17:21 +03002068 kvm_preempt_ops.sched_in = kvm_sched_in;
2069 kvm_preempt_ops.sched_out = kvm_sched_out;
2070
Avi Kivityc7addb92007-09-16 18:58:32 +02002071 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072
2073out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002074 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002075out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002076 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002077out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002078 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002079out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002080 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002081 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002082out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002083 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002084out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002085 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002086out_free_0:
2087 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002088out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002089 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002090 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002091out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 return r;
2093}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002094EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002096void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002098 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002100 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002101 sysdev_unregister(&kvm_sysdev);
2102 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002104 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002105 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002106 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002107 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02002109 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002111EXPORT_SYMBOL_GPL(kvm_exit);