blob: cf0ab8ed3845c898e927d62608221512148bee4a [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,
108 assigned_dev->guest_irq, 1);
109 mutex_unlock(&assigned_dev->kvm->lock);
110 kvm_put_kvm(assigned_dev->kvm);
111}
112
113/* FIXME: Implement the OR logic needed to make shared interrupts on
114 * this line behave properly
115 */
116static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
117{
118 struct kvm_assigned_dev_kernel *assigned_dev =
119 (struct kvm_assigned_dev_kernel *) dev_id;
120
121 kvm_get_kvm(assigned_dev->kvm);
122 schedule_work(&assigned_dev->interrupt_work);
123 disable_irq_nosync(irq);
124 return IRQ_HANDLED;
125}
126
127/* Ack the irq line for an assigned device */
128static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
129{
130 struct kvm_assigned_dev_kernel *dev;
131
132 if (kian->gsi == -1)
133 return;
134
135 dev = container_of(kian, struct kvm_assigned_dev_kernel,
136 ack_notifier);
137 kvm_set_irq(dev->kvm, dev->guest_irq, 0);
138 enable_irq(dev->host_irq);
139}
140
141static void kvm_free_assigned_device(struct kvm *kvm,
142 struct kvm_assigned_dev_kernel
143 *assigned_dev)
144{
145 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
146 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
147
148 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
149
150 if (cancel_work_sync(&assigned_dev->interrupt_work))
151 /* We had pending work. That means we will have to take
152 * care of kvm_put_kvm.
153 */
154 kvm_put_kvm(kvm);
155
156 pci_release_regions(assigned_dev->dev);
157 pci_disable_device(assigned_dev->dev);
158 pci_dev_put(assigned_dev->dev);
159
160 list_del(&assigned_dev->list);
161 kfree(assigned_dev);
162}
163
164void kvm_free_all_assigned_devices(struct kvm *kvm)
165{
166 struct list_head *ptr, *ptr2;
167 struct kvm_assigned_dev_kernel *assigned_dev;
168
169 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
170 assigned_dev = list_entry(ptr,
171 struct kvm_assigned_dev_kernel,
172 list);
173
174 kvm_free_assigned_device(kvm, assigned_dev);
175 }
176}
177
178static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
179 struct kvm_assigned_irq
180 *assigned_irq)
181{
182 int r = 0;
183 struct kvm_assigned_dev_kernel *match;
184
185 mutex_lock(&kvm->lock);
186
187 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
188 assigned_irq->assigned_dev_id);
189 if (!match) {
190 mutex_unlock(&kvm->lock);
191 return -EINVAL;
192 }
193
194 if (match->irq_requested) {
195 match->guest_irq = assigned_irq->guest_irq;
196 match->ack_notifier.gsi = assigned_irq->guest_irq;
197 mutex_unlock(&kvm->lock);
198 return 0;
199 }
200
201 INIT_WORK(&match->interrupt_work,
202 kvm_assigned_dev_interrupt_work_handler);
203
204 if (irqchip_in_kernel(kvm)) {
205 if (!capable(CAP_SYS_RAWIO)) {
206 r = -EPERM;
207 goto out_release;
208 }
209
210 if (assigned_irq->host_irq)
211 match->host_irq = assigned_irq->host_irq;
212 else
213 match->host_irq = match->dev->irq;
214 match->guest_irq = assigned_irq->guest_irq;
215 match->ack_notifier.gsi = assigned_irq->guest_irq;
216 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
217 kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
218
219 /* Even though this is PCI, we don't want to use shared
220 * interrupts. Sharing host devices with guest-assigned devices
221 * on the same interrupt line is not a happy situation: there
222 * are going to be long delays in accepting, acking, etc.
223 */
224 if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
225 "kvm_assigned_device", (void *)match)) {
226 r = -EIO;
227 goto out_release;
228 }
229 }
230
231 match->irq_requested = true;
232 mutex_unlock(&kvm->lock);
233 return r;
234out_release:
235 mutex_unlock(&kvm->lock);
236 kvm_free_assigned_device(kvm, match);
237 return r;
238}
239
240static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
241 struct kvm_assigned_pci_dev *assigned_dev)
242{
243 int r = 0;
244 struct kvm_assigned_dev_kernel *match;
245 struct pci_dev *dev;
246
247 mutex_lock(&kvm->lock);
248
249 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
250 assigned_dev->assigned_dev_id);
251 if (match) {
252 /* device already assigned */
253 r = -EINVAL;
254 goto out;
255 }
256
257 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
258 if (match == NULL) {
259 printk(KERN_INFO "%s: Couldn't allocate memory\n",
260 __func__);
261 r = -ENOMEM;
262 goto out;
263 }
264 dev = pci_get_bus_and_slot(assigned_dev->busnr,
265 assigned_dev->devfn);
266 if (!dev) {
267 printk(KERN_INFO "%s: host device not found\n", __func__);
268 r = -EINVAL;
269 goto out_free;
270 }
271 if (pci_enable_device(dev)) {
272 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
273 r = -EBUSY;
274 goto out_put;
275 }
276 r = pci_request_regions(dev, "kvm_assigned_device");
277 if (r) {
278 printk(KERN_INFO "%s: Could not get access to device regions\n",
279 __func__);
280 goto out_disable;
281 }
282 match->assigned_dev_id = assigned_dev->assigned_dev_id;
283 match->host_busnr = assigned_dev->busnr;
284 match->host_devfn = assigned_dev->devfn;
285 match->dev = dev;
286
287 match->kvm = kvm;
288
289 list_add(&match->list, &kvm->arch.assigned_dev_head);
290
291 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
292 r = kvm_iommu_map_guest(kvm, match);
293 if (r)
294 goto out_list_del;
295 }
296
297out:
298 mutex_unlock(&kvm->lock);
299 return r;
300out_list_del:
301 list_del(&match->list);
302 pci_release_regions(dev);
303out_disable:
304 pci_disable_device(dev);
305out_put:
306 pci_dev_put(dev);
307out_free:
308 kfree(match);
309 mutex_unlock(&kvm->lock);
310 return r;
311}
312#endif
313
James Morris5aacf0c2006-12-22 01:04:55 -0800314static inline int valid_vcpu(int n)
315{
316 return likely(n >= 0 && n < KVM_MAX_VCPUS);
317}
318
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800319inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300320{
321 if (pfn_valid(pfn))
322 return PageReserved(pfn_to_page(pfn));
323
324 return true;
325}
326
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327/*
328 * Switches to specified vcpu, until a matching vcpu_put()
329 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200330void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331{
Avi Kivity15ad7142007-07-11 18:17:21 +0300332 int cpu;
333
Avi Kivitybccf2152007-02-21 18:04:26 +0200334 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300335 cpu = get_cpu();
336 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200337 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300338 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200339}
340
Carsten Otte313a3dc2007-10-11 19:16:52 +0200341void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342{
Avi Kivity15ad7142007-07-11 18:17:21 +0300343 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200344 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300345 preempt_notifier_unregister(&vcpu->preempt_notifier);
346 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 mutex_unlock(&vcpu->mutex);
348}
349
Avi Kivityd9e368d2007-06-07 19:18:30 +0300350static void ack_flush(void *_completed)
351{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300352}
353
354void kvm_flush_remote_tlbs(struct kvm *kvm)
355{
Avi Kivity597a5f52008-07-20 14:24:22 +0300356 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300357 cpumask_t cpus;
358 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300359
Avi Kivity597a5f52008-07-20 14:24:22 +0300360 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300361 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000362 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
363 vcpu = kvm->vcpus[i];
364 if (!vcpu)
365 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200366 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300367 continue;
368 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300369 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200370 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300371 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200372 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300373 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200374 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200375 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300376out:
377 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300378}
379
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500380void kvm_reload_remote_mmus(struct kvm *kvm)
381{
Avi Kivity597a5f52008-07-20 14:24:22 +0300382 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500383 cpumask_t cpus;
384 struct kvm_vcpu *vcpu;
385
Avi Kivity597a5f52008-07-20 14:24:22 +0300386 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500387 cpus_clear(cpus);
388 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
389 vcpu = kvm->vcpus[i];
390 if (!vcpu)
391 continue;
392 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
393 continue;
394 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300395 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500396 cpu_set(cpu, cpus);
397 }
398 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300399 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500400 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300401out:
402 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500403}
404
405
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000406int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
407{
408 struct page *page;
409 int r;
410
411 mutex_init(&vcpu->mutex);
412 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000413 vcpu->kvm = kvm;
414 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300415 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000416
417 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
418 if (!page) {
419 r = -ENOMEM;
420 goto fail;
421 }
422 vcpu->run = page_address(page);
423
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800424 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000425 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800426 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000427 return 0;
428
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000429fail_free_run:
430 free_page((unsigned long)vcpu->run);
431fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000432 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000433}
434EXPORT_SYMBOL_GPL(kvm_vcpu_init);
435
436void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
437{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800438 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000439 free_page((unsigned long)vcpu->run);
440}
441EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
442
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200443#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
444static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
445{
446 return container_of(mn, struct kvm, mmu_notifier);
447}
448
449static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
450 struct mm_struct *mm,
451 unsigned long address)
452{
453 struct kvm *kvm = mmu_notifier_to_kvm(mn);
454 int need_tlb_flush;
455
456 /*
457 * When ->invalidate_page runs, the linux pte has been zapped
458 * already but the page is still allocated until
459 * ->invalidate_page returns. So if we increase the sequence
460 * here the kvm page fault will notice if the spte can't be
461 * established because the page is going to be freed. If
462 * instead the kvm page fault establishes the spte before
463 * ->invalidate_page runs, kvm_unmap_hva will release it
464 * before returning.
465 *
466 * The sequence increase only need to be seen at spin_unlock
467 * time, and not at spin_lock time.
468 *
469 * Increasing the sequence after the spin_unlock would be
470 * unsafe because the kvm page fault could then establish the
471 * pte after kvm_unmap_hva returned, without noticing the page
472 * is going to be freed.
473 */
474 spin_lock(&kvm->mmu_lock);
475 kvm->mmu_notifier_seq++;
476 need_tlb_flush = kvm_unmap_hva(kvm, address);
477 spin_unlock(&kvm->mmu_lock);
478
479 /* we've to flush the tlb before the pages can be freed */
480 if (need_tlb_flush)
481 kvm_flush_remote_tlbs(kvm);
482
483}
484
485static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
486 struct mm_struct *mm,
487 unsigned long start,
488 unsigned long end)
489{
490 struct kvm *kvm = mmu_notifier_to_kvm(mn);
491 int need_tlb_flush = 0;
492
493 spin_lock(&kvm->mmu_lock);
494 /*
495 * The count increase must become visible at unlock time as no
496 * spte can be established without taking the mmu_lock and
497 * count is also read inside the mmu_lock critical section.
498 */
499 kvm->mmu_notifier_count++;
500 for (; start < end; start += PAGE_SIZE)
501 need_tlb_flush |= kvm_unmap_hva(kvm, start);
502 spin_unlock(&kvm->mmu_lock);
503
504 /* we've to flush the tlb before the pages can be freed */
505 if (need_tlb_flush)
506 kvm_flush_remote_tlbs(kvm);
507}
508
509static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
510 struct mm_struct *mm,
511 unsigned long start,
512 unsigned long end)
513{
514 struct kvm *kvm = mmu_notifier_to_kvm(mn);
515
516 spin_lock(&kvm->mmu_lock);
517 /*
518 * This sequence increase will notify the kvm page fault that
519 * the page that is going to be mapped in the spte could have
520 * been freed.
521 */
522 kvm->mmu_notifier_seq++;
523 /*
524 * The above sequence increase must be visible before the
525 * below count decrease but both values are read by the kvm
526 * page fault under mmu_lock spinlock so we don't need to add
527 * a smb_wmb() here in between the two.
528 */
529 kvm->mmu_notifier_count--;
530 spin_unlock(&kvm->mmu_lock);
531
532 BUG_ON(kvm->mmu_notifier_count < 0);
533}
534
535static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
536 struct mm_struct *mm,
537 unsigned long address)
538{
539 struct kvm *kvm = mmu_notifier_to_kvm(mn);
540 int young;
541
542 spin_lock(&kvm->mmu_lock);
543 young = kvm_age_hva(kvm, address);
544 spin_unlock(&kvm->mmu_lock);
545
546 if (young)
547 kvm_flush_remote_tlbs(kvm);
548
549 return young;
550}
551
552static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
553 .invalidate_page = kvm_mmu_notifier_invalidate_page,
554 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
555 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
556 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
557};
558#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
559
Avi Kivityf17abe92007-02-21 19:28:04 +0200560static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800562 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200563#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
564 struct page *page;
565#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800567 if (IS_ERR(kvm))
568 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569
Laurent Vivier5f94c172008-05-30 16:05:54 +0200570#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
571 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
572 if (!page) {
573 kfree(kvm);
574 return ERR_PTR(-ENOMEM);
575 }
576 kvm->coalesced_mmio_ring =
577 (struct kvm_coalesced_mmio_ring *)page_address(page);
578#endif
579
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200580#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
581 {
582 int err;
583 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
584 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
585 if (err) {
586#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
587 put_page(page);
588#endif
589 kfree(kvm);
590 return ERR_PTR(err);
591 }
592 }
593#endif
594
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200595 kvm->mm = current->mm;
596 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500597 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300598 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800599 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400600 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200601 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300602 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000603 spin_lock(&kvm_lock);
604 list_add(&kvm->vm_list, &vm_list);
605 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200606#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
607 kvm_coalesced_mmio_init(kvm);
608#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800609out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200610 return kvm;
611}
612
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613/*
614 * Free any memory in @free but not in @dont.
615 */
616static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
617 struct kvm_memory_slot *dont)
618{
Izik Eidus290fc382007-09-27 14:11:22 +0200619 if (!dont || free->rmap != dont->rmap)
620 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621
622 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
623 vfree(free->dirty_bitmap);
624
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300625 if (!dont || free->lpage_info != dont->lpage_info)
626 vfree(free->lpage_info);
627
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000629 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500630 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300631 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632}
633
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800634void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635{
636 int i;
637
638 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000639 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640}
641
Avi Kivityf17abe92007-02-21 19:28:04 +0200642static void kvm_destroy_vm(struct kvm *kvm)
643{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200644 struct mm_struct *mm = kvm->mm;
645
Avi Kivity133de902007-02-12 00:54:44 -0800646 spin_lock(&kvm_lock);
647 list_del(&kvm->vm_list);
648 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300649 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400650 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200651#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
652 if (kvm->coalesced_mmio_ring != NULL)
653 free_page((unsigned long)kvm->coalesced_mmio_ring);
654#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200655#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
656 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
657#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800658 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200659 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200660}
661
Izik Eidusd39f13b2008-03-30 16:01:25 +0300662void kvm_get_kvm(struct kvm *kvm)
663{
664 atomic_inc(&kvm->users_count);
665}
666EXPORT_SYMBOL_GPL(kvm_get_kvm);
667
668void kvm_put_kvm(struct kvm *kvm)
669{
670 if (atomic_dec_and_test(&kvm->users_count))
671 kvm_destroy_vm(kvm);
672}
673EXPORT_SYMBOL_GPL(kvm_put_kvm);
674
675
Avi Kivityf17abe92007-02-21 19:28:04 +0200676static int kvm_vm_release(struct inode *inode, struct file *filp)
677{
678 struct kvm *kvm = filp->private_data;
679
Izik Eidusd39f13b2008-03-30 16:01:25 +0300680 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 return 0;
682}
683
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 * Allocate some memory and give it an address in the guest physical address
686 * space.
687 *
688 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800689 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500690 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800692int __kvm_set_memory_region(struct kvm *kvm,
693 struct kvm_userspace_memory_region *mem,
694 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695{
696 int r;
697 gfn_t base_gfn;
698 unsigned long npages;
699 unsigned long i;
700 struct kvm_memory_slot *memslot;
701 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702
703 r = -EINVAL;
704 /* General sanity checks */
705 if (mem->memory_size & (PAGE_SIZE - 1))
706 goto out;
707 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
708 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200709 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 goto out;
711 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
712 goto out;
713
714 memslot = &kvm->memslots[mem->slot];
715 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
716 npages = mem->memory_size >> PAGE_SHIFT;
717
718 if (!npages)
719 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
720
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 new = old = *memslot;
722
723 new.base_gfn = base_gfn;
724 new.npages = npages;
725 new.flags = mem->flags;
726
727 /* Disallow changing a memory slot's size. */
728 r = -EINVAL;
729 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800730 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
732 /* Check for overlaps */
733 r = -EEXIST;
734 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
735 struct kvm_memory_slot *s = &kvm->memslots[i];
736
737 if (s == memslot)
738 continue;
739 if (!((base_gfn + npages <= s->base_gfn) ||
740 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800741 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 /* Free page dirty bitmap if unneeded */
745 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000746 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
748 r = -ENOMEM;
749
750 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200751#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500752 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400753 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200754
755 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800756 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200757
Izik Eidus290fc382007-09-27 14:11:22 +0200758 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500759
Izik Eidus80b14b52007-10-25 11:54:04 +0200760 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200761 /*
762 * hva_to_rmmap() serialzies with the mmu_lock and to be
763 * safe it has to ignore memslots with !user_alloc &&
764 * !userspace_addr.
765 */
766 if (user_alloc)
767 new.userspace_addr = mem->userspace_addr;
768 else
769 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300771 if (npages && !new.lpage_info) {
772 int largepages = npages / KVM_PAGES_PER_HPAGE;
773 if (npages % KVM_PAGES_PER_HPAGE)
774 largepages++;
775 if (base_gfn % KVM_PAGES_PER_HPAGE)
776 largepages++;
777
778 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
779
780 if (!new.lpage_info)
781 goto out_free;
782
783 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
784
785 if (base_gfn % KVM_PAGES_PER_HPAGE)
786 new.lpage_info[0].write_count = 1;
787 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
788 new.lpage_info[largepages-1].write_count = 1;
789 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790
791 /* Allocate page dirty bitmap if needed */
792 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
793 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
794
795 new.dirty_bitmap = vmalloc(dirty_bytes);
796 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800797 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 memset(new.dirty_bitmap, 0, dirty_bytes);
799 }
Carsten Otteeff01142008-06-27 15:05:31 +0200800#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300802 if (!npages)
803 kvm_arch_flush_shadow(kvm);
804
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200805 spin_lock(&kvm->mmu_lock);
806 if (mem->slot >= kvm->nmemslots)
807 kvm->nmemslots = mem->slot + 1;
808
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200810 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
Zhang Xiantao0de10342007-11-20 16:25:04 +0800812 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
813 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200814 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800815 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200816 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800817 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800818 }
819
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 kvm_free_physmem_slot(&old, &new);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800821#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300822 /* map the pages in iommu page table */
823 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
824 if (r)
825 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800826#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 return 0;
828
Sheng Yangf78e0e22007-10-29 09:40:42 +0800829out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 kvm_free_physmem_slot(&new, &old);
831out:
832 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200833
834}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800835EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
836
837int kvm_set_memory_region(struct kvm *kvm,
838 struct kvm_userspace_memory_region *mem,
839 int user_alloc)
840{
841 int r;
842
Izik Eidus72dc67a2008-02-10 18:04:15 +0200843 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800844 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200845 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800846 return r;
847}
Izik Eidus210c7c42007-10-24 23:52:57 +0200848EXPORT_SYMBOL_GPL(kvm_set_memory_region);
849
Carsten Otte1fe779f2007-10-29 16:08:35 +0100850int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
851 struct
852 kvm_userspace_memory_region *mem,
853 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200854{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200855 if (mem->slot >= KVM_MEMORY_SLOTS)
856 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200857 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858}
859
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800860int kvm_get_dirty_log(struct kvm *kvm,
861 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862{
863 struct kvm_memory_slot *memslot;
864 int r, i;
865 int n;
866 unsigned long any = 0;
867
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 r = -EINVAL;
869 if (log->slot >= KVM_MEMORY_SLOTS)
870 goto out;
871
872 memslot = &kvm->memslots[log->slot];
873 r = -ENOENT;
874 if (!memslot->dirty_bitmap)
875 goto out;
876
Uri Lublincd1a4a92007-02-22 16:43:09 +0200877 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
Uri Lublincd1a4a92007-02-22 16:43:09 +0200879 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 any = memslot->dirty_bitmap[i];
881
882 r = -EFAULT;
883 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
884 goto out;
885
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800886 if (any)
887 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888
889 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return r;
892}
893
Izik Eiduscea7bb22007-10-17 19:17:48 +0200894int is_error_page(struct page *page)
895{
896 return page == bad_page;
897}
898EXPORT_SYMBOL_GPL(is_error_page);
899
Anthony Liguori35149e22008-04-02 14:46:56 -0500900int is_error_pfn(pfn_t pfn)
901{
902 return pfn == bad_pfn;
903}
904EXPORT_SYMBOL_GPL(is_error_pfn);
905
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200906static inline unsigned long bad_hva(void)
907{
908 return PAGE_OFFSET;
909}
910
911int kvm_is_error_hva(unsigned long addr)
912{
913 return addr == bad_hva();
914}
915EXPORT_SYMBOL_GPL(kvm_is_error_hva);
916
Avi Kivitye8207542007-03-30 16:54:30 +0300917static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918{
919 int i;
920
921 for (i = 0; i < kvm->nmemslots; ++i) {
922 struct kvm_memory_slot *memslot = &kvm->memslots[i];
923
924 if (gfn >= memslot->base_gfn
925 && gfn < memslot->base_gfn + memslot->npages)
926 return memslot;
927 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000928 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929}
Avi Kivitye8207542007-03-30 16:54:30 +0300930
931struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
932{
933 gfn = unalias_gfn(kvm, gfn);
934 return __gfn_to_memslot(kvm, gfn);
935}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Izik Eiduse0d62c72007-10-24 23:57:46 +0200937int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
938{
939 int i;
940
941 gfn = unalias_gfn(kvm, gfn);
942 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
943 struct kvm_memory_slot *memslot = &kvm->memslots[i];
944
945 if (gfn >= memslot->base_gfn
946 && gfn < memslot->base_gfn + memslot->npages)
947 return 1;
948 }
949 return 0;
950}
951EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
952
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300953unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200954{
955 struct kvm_memory_slot *slot;
956
957 gfn = unalias_gfn(kvm, gfn);
958 slot = __gfn_to_memslot(kvm, gfn);
959 if (!slot)
960 return bad_hva();
961 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
962}
Sheng Yang0d150292008-04-25 21:44:50 +0800963EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200964
Anthony Liguori35149e22008-04-02 14:46:56 -0500965pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300966{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500967 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200968 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500969 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500970 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300971
Avi Kivity60395222007-10-21 11:03:36 +0200972 might_sleep();
973
Izik Eidus539cb662007-11-11 22:05:04 +0200974 addr = gfn_to_hva(kvm, gfn);
975 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200976 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -0500977 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200978 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200979
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300980 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +0200981
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500982 if (unlikely(npages != 1)) {
983 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500984
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300985 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500986 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300987
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500988 if (vma == NULL || addr < vma->vm_start ||
989 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300990 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500991 get_page(bad_page);
992 return page_to_pfn(bad_page);
993 }
994
995 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300996 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800997 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500998 } else
999 pfn = page_to_pfn(page[0]);
1000
1001 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001002}
1003
1004EXPORT_SYMBOL_GPL(gfn_to_pfn);
1005
1006struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1007{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001008 pfn_t pfn;
1009
1010 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001011 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001012 return pfn_to_page(pfn);
1013
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001014 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001015
1016 get_page(bad_page);
1017 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001018}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001019
Avi Kivity954bbbc2007-03-30 14:02:32 +03001020EXPORT_SYMBOL_GPL(gfn_to_page);
1021
Izik Eidusb4231d62007-11-20 11:49:33 +02001022void kvm_release_page_clean(struct page *page)
1023{
Anthony Liguori35149e22008-04-02 14:46:56 -05001024 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001025}
1026EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1027
Anthony Liguori35149e22008-04-02 14:46:56 -05001028void kvm_release_pfn_clean(pfn_t pfn)
1029{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001030 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001031 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001032}
1033EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1034
Izik Eidusb4231d62007-11-20 11:49:33 +02001035void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001036{
Anthony Liguori35149e22008-04-02 14:46:56 -05001037 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001038}
Izik Eidusb4231d62007-11-20 11:49:33 +02001039EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001040
Anthony Liguori35149e22008-04-02 14:46:56 -05001041void kvm_release_pfn_dirty(pfn_t pfn)
1042{
1043 kvm_set_pfn_dirty(pfn);
1044 kvm_release_pfn_clean(pfn);
1045}
1046EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1047
1048void kvm_set_page_dirty(struct page *page)
1049{
1050 kvm_set_pfn_dirty(page_to_pfn(page));
1051}
1052EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1053
1054void kvm_set_pfn_dirty(pfn_t pfn)
1055{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001056 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001057 struct page *page = pfn_to_page(pfn);
1058 if (!PageReserved(page))
1059 SetPageDirty(page);
1060 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001061}
1062EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1063
1064void kvm_set_pfn_accessed(pfn_t pfn)
1065{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001066 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001067 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001068}
1069EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1070
1071void kvm_get_pfn(pfn_t pfn)
1072{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001073 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001074 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001075}
1076EXPORT_SYMBOL_GPL(kvm_get_pfn);
1077
Izik Eidus195aefd2007-10-01 22:14:18 +02001078static int next_segment(unsigned long len, int offset)
1079{
1080 if (len > PAGE_SIZE - offset)
1081 return PAGE_SIZE - offset;
1082 else
1083 return len;
1084}
1085
1086int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1087 int len)
1088{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001089 int r;
1090 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001091
Izik Eiduse0506bc2007-11-11 22:10:22 +02001092 addr = gfn_to_hva(kvm, gfn);
1093 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001094 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001095 r = copy_from_user(data, (void __user *)addr + offset, len);
1096 if (r)
1097 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001098 return 0;
1099}
1100EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1101
1102int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1103{
1104 gfn_t gfn = gpa >> PAGE_SHIFT;
1105 int seg;
1106 int offset = offset_in_page(gpa);
1107 int ret;
1108
1109 while ((seg = next_segment(len, offset)) != 0) {
1110 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1111 if (ret < 0)
1112 return ret;
1113 offset = 0;
1114 len -= seg;
1115 data += seg;
1116 ++gfn;
1117 }
1118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(kvm_read_guest);
1121
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001122int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1123 unsigned long len)
1124{
1125 int r;
1126 unsigned long addr;
1127 gfn_t gfn = gpa >> PAGE_SHIFT;
1128 int offset = offset_in_page(gpa);
1129
1130 addr = gfn_to_hva(kvm, gfn);
1131 if (kvm_is_error_hva(addr))
1132 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001133 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001134 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001135 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001136 if (r)
1137 return -EFAULT;
1138 return 0;
1139}
1140EXPORT_SYMBOL(kvm_read_guest_atomic);
1141
Izik Eidus195aefd2007-10-01 22:14:18 +02001142int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1143 int offset, int len)
1144{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001145 int r;
1146 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001147
Izik Eiduse0506bc2007-11-11 22:10:22 +02001148 addr = gfn_to_hva(kvm, gfn);
1149 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001150 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001151 r = copy_to_user((void __user *)addr + offset, data, len);
1152 if (r)
1153 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001154 mark_page_dirty(kvm, gfn);
1155 return 0;
1156}
1157EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1158
1159int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1160 unsigned long len)
1161{
1162 gfn_t gfn = gpa >> PAGE_SHIFT;
1163 int seg;
1164 int offset = offset_in_page(gpa);
1165 int ret;
1166
1167 while ((seg = next_segment(len, offset)) != 0) {
1168 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1169 if (ret < 0)
1170 return ret;
1171 offset = 0;
1172 len -= seg;
1173 data += seg;
1174 ++gfn;
1175 }
1176 return 0;
1177}
1178
1179int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1180{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001181 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001182}
1183EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1184
1185int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1186{
1187 gfn_t gfn = gpa >> PAGE_SHIFT;
1188 int seg;
1189 int offset = offset_in_page(gpa);
1190 int ret;
1191
1192 while ((seg = next_segment(len, offset)) != 0) {
1193 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1194 if (ret < 0)
1195 return ret;
1196 offset = 0;
1197 len -= seg;
1198 ++gfn;
1199 }
1200 return 0;
1201}
1202EXPORT_SYMBOL_GPL(kvm_clear_guest);
1203
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1205{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001206 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
Uri Lublin3b6fff12007-10-30 10:42:09 +02001208 gfn = unalias_gfn(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001209 memslot = __gfn_to_memslot(kvm, gfn);
1210 if (memslot && memslot->dirty_bitmap) {
1211 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212
Rusty Russell7e9d6192007-07-31 20:41:14 +10001213 /* avoid RMW */
1214 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1215 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 }
1217}
1218
Eddie Dongb6958ce2007-07-18 12:15:21 +03001219/*
1220 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1221 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001222void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001223{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001224 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001225
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001226 for (;;) {
1227 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001228
Marcelo Tosattid7690172008-09-08 15:23:48 -03001229 if (kvm_cpu_has_interrupt(vcpu) ||
1230 kvm_cpu_has_pending_timer(vcpu) ||
1231 kvm_arch_vcpu_runnable(vcpu)) {
1232 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001233 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001234 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001235 if (signal_pending(current))
1236 break;
1237
Eddie Dongb6958ce2007-07-18 12:15:21 +03001238 vcpu_put(vcpu);
1239 schedule();
1240 vcpu_load(vcpu);
1241 }
1242
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001243 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001244}
1245
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246void kvm_resched(struct kvm_vcpu *vcpu)
1247{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001248 if (!need_resched())
1249 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251}
1252EXPORT_SYMBOL_GPL(kvm_resched);
1253
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001254static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001255{
1256 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001257 struct page *page;
1258
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001259 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001260 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001261#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001262 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001263 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001264#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001265#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1266 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1267 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1268#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001269 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001270 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001271 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001272 vmf->page = page;
1273 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001274}
1275
1276static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001277 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001278};
1279
1280static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1281{
1282 vma->vm_ops = &kvm_vcpu_vm_ops;
1283 return 0;
1284}
1285
Avi Kivitybccf2152007-02-21 18:04:26 +02001286static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1287{
1288 struct kvm_vcpu *vcpu = filp->private_data;
1289
Al Viro66c0b392008-04-19 20:33:56 +01001290 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001291 return 0;
1292}
1293
Jan Engelhardt5c502742008-01-22 20:46:14 +01001294static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001295 .release = kvm_vcpu_release,
1296 .unlocked_ioctl = kvm_vcpu_ioctl,
1297 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001298 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001299};
1300
1301/*
1302 * Allocates an inode for the vcpu.
1303 */
1304static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1305{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001306 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001307 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001308 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001309 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001310}
1311
Avi Kivityc5ea7662007-02-20 18:41:05 +02001312/*
1313 * Creates some virtual cpus. Good luck creating more than one.
1314 */
1315static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1316{
1317 int r;
1318 struct kvm_vcpu *vcpu;
1319
Avi Kivityc5ea7662007-02-20 18:41:05 +02001320 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001321 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001322
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001323 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001324 if (IS_ERR(vcpu))
1325 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001326
Avi Kivity15ad7142007-07-11 18:17:21 +03001327 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1328
Avi Kivity26e52152007-11-20 15:30:24 +02001329 r = kvm_arch_vcpu_setup(vcpu);
1330 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001331 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001332
Shaohua Li11ec2802007-07-23 14:51:37 +08001333 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001334 if (kvm->vcpus[n]) {
1335 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001336 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001337 }
1338 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001339 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001340
1341 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001342 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001343 r = create_vcpu_fd(vcpu);
1344 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001345 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001346 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001347
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001348unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001349 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001350 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001351vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001352 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001353 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001354 return r;
1355}
1356
Avi Kivity1961d272007-03-05 19:46:05 +02001357static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1358{
1359 if (sigset) {
1360 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1361 vcpu->sigset_active = 1;
1362 vcpu->sigset = *sigset;
1363 } else
1364 vcpu->sigset_active = 0;
1365 return 0;
1366}
1367
Avi Kivitybccf2152007-02-21 18:04:26 +02001368static long kvm_vcpu_ioctl(struct file *filp,
1369 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370{
Avi Kivitybccf2152007-02-21 18:04:26 +02001371 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001372 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001373 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001374 struct kvm_fpu *fpu = NULL;
1375 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001377 if (vcpu->kvm->mm != current->mm)
1378 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001380 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001381 r = -EINVAL;
1382 if (arg)
1383 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001384 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001387 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001389 r = -ENOMEM;
1390 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1391 if (!kvm_regs)
1392 goto out;
1393 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001395 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001397 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1398 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001400out_free1:
1401 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402 break;
1403 }
1404 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001405 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001407 r = -ENOMEM;
1408 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1409 if (!kvm_regs)
1410 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001412 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1413 goto out_free2;
1414 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001416 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001418out_free2:
1419 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 break;
1421 }
1422 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001423 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1424 r = -ENOMEM;
1425 if (!kvm_sregs)
1426 goto out;
1427 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 if (r)
1429 goto out;
1430 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001431 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432 goto out;
1433 r = 0;
1434 break;
1435 }
1436 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001437 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1438 r = -ENOMEM;
1439 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001441 r = -EFAULT;
1442 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1443 goto out;
1444 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 if (r)
1446 goto out;
1447 r = 0;
1448 break;
1449 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001450 case KVM_GET_MP_STATE: {
1451 struct kvm_mp_state mp_state;
1452
1453 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1454 if (r)
1455 goto out;
1456 r = -EFAULT;
1457 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1458 goto out;
1459 r = 0;
1460 break;
1461 }
1462 case KVM_SET_MP_STATE: {
1463 struct kvm_mp_state mp_state;
1464
1465 r = -EFAULT;
1466 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1467 goto out;
1468 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1469 if (r)
1470 goto out;
1471 r = 0;
1472 break;
1473 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474 case KVM_TRANSLATE: {
1475 struct kvm_translation tr;
1476
1477 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001478 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001480 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 if (r)
1482 goto out;
1483 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001484 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 goto out;
1486 r = 0;
1487 break;
1488 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 case KVM_DEBUG_GUEST: {
1490 struct kvm_debug_guest dbg;
1491
1492 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001493 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001495 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 if (r)
1497 goto out;
1498 r = 0;
1499 break;
1500 }
Avi Kivity1961d272007-03-05 19:46:05 +02001501 case KVM_SET_SIGNAL_MASK: {
1502 struct kvm_signal_mask __user *sigmask_arg = argp;
1503 struct kvm_signal_mask kvm_sigmask;
1504 sigset_t sigset, *p;
1505
1506 p = NULL;
1507 if (argp) {
1508 r = -EFAULT;
1509 if (copy_from_user(&kvm_sigmask, argp,
1510 sizeof kvm_sigmask))
1511 goto out;
1512 r = -EINVAL;
1513 if (kvm_sigmask.len != sizeof sigset)
1514 goto out;
1515 r = -EFAULT;
1516 if (copy_from_user(&sigset, sigmask_arg->sigset,
1517 sizeof sigset))
1518 goto out;
1519 p = &sigset;
1520 }
1521 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1522 break;
1523 }
Avi Kivityb8836732007-04-01 16:34:31 +03001524 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001525 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1526 r = -ENOMEM;
1527 if (!fpu)
1528 goto out;
1529 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001530 if (r)
1531 goto out;
1532 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001533 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001534 goto out;
1535 r = 0;
1536 break;
1537 }
1538 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001539 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1540 r = -ENOMEM;
1541 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001542 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001543 r = -EFAULT;
1544 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1545 goto out;
1546 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001547 if (r)
1548 goto out;
1549 r = 0;
1550 break;
1551 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001552 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001553 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001554 }
1555out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001556 kfree(fpu);
1557 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001558 return r;
1559}
1560
1561static long kvm_vm_ioctl(struct file *filp,
1562 unsigned int ioctl, unsigned long arg)
1563{
1564 struct kvm *kvm = filp->private_data;
1565 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001566 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001567
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001568 if (kvm->mm != current->mm)
1569 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001570 switch (ioctl) {
1571 case KVM_CREATE_VCPU:
1572 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1573 if (r < 0)
1574 goto out;
1575 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001576 case KVM_SET_USER_MEMORY_REGION: {
1577 struct kvm_userspace_memory_region kvm_userspace_mem;
1578
1579 r = -EFAULT;
1580 if (copy_from_user(&kvm_userspace_mem, argp,
1581 sizeof kvm_userspace_mem))
1582 goto out;
1583
1584 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 if (r)
1586 goto out;
1587 break;
1588 }
1589 case KVM_GET_DIRTY_LOG: {
1590 struct kvm_dirty_log log;
1591
1592 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001593 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001595 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 if (r)
1597 goto out;
1598 break;
1599 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001600#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1601 case KVM_REGISTER_COALESCED_MMIO: {
1602 struct kvm_coalesced_mmio_zone zone;
1603 r = -EFAULT;
1604 if (copy_from_user(&zone, argp, sizeof zone))
1605 goto out;
1606 r = -ENXIO;
1607 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1608 if (r)
1609 goto out;
1610 r = 0;
1611 break;
1612 }
1613 case KVM_UNREGISTER_COALESCED_MMIO: {
1614 struct kvm_coalesced_mmio_zone zone;
1615 r = -EFAULT;
1616 if (copy_from_user(&zone, argp, sizeof zone))
1617 goto out;
1618 r = -ENXIO;
1619 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1620 if (r)
1621 goto out;
1622 r = 0;
1623 break;
1624 }
1625#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001626#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1627 case KVM_ASSIGN_PCI_DEVICE: {
1628 struct kvm_assigned_pci_dev assigned_dev;
1629
1630 r = -EFAULT;
1631 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1632 goto out;
1633 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1634 if (r)
1635 goto out;
1636 break;
1637 }
1638 case KVM_ASSIGN_IRQ: {
1639 struct kvm_assigned_irq assigned_irq;
1640
1641 r = -EFAULT;
1642 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1643 goto out;
1644 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1645 if (r)
1646 goto out;
1647 break;
1648 }
1649#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001650 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001651 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001652 }
1653out:
1654 return r;
1655}
1656
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001657static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001658{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001659 struct page *page[1];
1660 unsigned long addr;
1661 int npages;
1662 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001663 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001664
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001665 addr = gfn_to_hva(kvm, gfn);
1666 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001667 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001668
1669 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1670 NULL);
1671 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001672 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001673
1674 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001675 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001676}
1677
1678static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001679 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001680};
1681
1682static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1683{
1684 vma->vm_ops = &kvm_vm_vm_ops;
1685 return 0;
1686}
1687
Jan Engelhardt5c502742008-01-22 20:46:14 +01001688static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001689 .release = kvm_vm_release,
1690 .unlocked_ioctl = kvm_vm_ioctl,
1691 .compat_ioctl = kvm_vm_ioctl,
1692 .mmap = kvm_vm_mmap,
1693};
1694
1695static int kvm_dev_ioctl_create_vm(void)
1696{
Al Viro2030a422008-02-23 06:46:49 -05001697 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001698 struct kvm *kvm;
1699
Avi Kivityf17abe92007-02-21 19:28:04 +02001700 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001701 if (IS_ERR(kvm))
1702 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001703 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001704 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001705 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001706
Avi Kivityf17abe92007-02-21 19:28:04 +02001707 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001708}
1709
1710static long kvm_dev_ioctl(struct file *filp,
1711 unsigned int ioctl, unsigned long arg)
1712{
Avi Kivity07c45a32007-03-07 13:05:38 +02001713 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001714
1715 switch (ioctl) {
1716 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001717 r = -EINVAL;
1718 if (arg)
1719 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001720 r = KVM_API_VERSION;
1721 break;
1722 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001723 r = -EINVAL;
1724 if (arg)
1725 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001726 r = kvm_dev_ioctl_create_vm();
1727 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001728 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001729 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001730 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001731 case KVM_GET_VCPU_MMAP_SIZE:
1732 r = -EINVAL;
1733 if (arg)
1734 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001735 r = PAGE_SIZE; /* struct kvm_run */
1736#ifdef CONFIG_X86
1737 r += PAGE_SIZE; /* pio data page */
1738#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001739#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1740 r += PAGE_SIZE; /* coalesced mmio ring page */
1741#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001742 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001743 case KVM_TRACE_ENABLE:
1744 case KVM_TRACE_PAUSE:
1745 case KVM_TRACE_DISABLE:
1746 r = kvm_trace_ioctl(ioctl, arg);
1747 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001749 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 }
1751out:
1752 return r;
1753}
1754
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756 .unlocked_ioctl = kvm_dev_ioctl,
1757 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758};
1759
1760static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001761 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762 "kvm",
1763 &kvm_chardev_ops,
1764};
1765
Avi Kivity1b6c0162007-05-24 13:03:52 +03001766static void hardware_enable(void *junk)
1767{
1768 int cpu = raw_smp_processor_id();
1769
1770 if (cpu_isset(cpu, cpus_hardware_enabled))
1771 return;
1772 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001773 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001774}
1775
1776static void hardware_disable(void *junk)
1777{
1778 int cpu = raw_smp_processor_id();
1779
1780 if (!cpu_isset(cpu, cpus_hardware_enabled))
1781 return;
1782 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001783 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001784}
1785
Avi Kivity774c47f2007-02-12 00:54:47 -08001786static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1787 void *v)
1788{
1789 int cpu = (long)v;
1790
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001791 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001792 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001793 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001794 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1795 cpu);
1796 hardware_disable(NULL);
1797 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001798 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001799 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1800 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001801 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001802 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001803 case CPU_ONLINE:
1804 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1805 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001806 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001807 break;
1808 }
1809 return NOTIFY_OK;
1810}
1811
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001812
1813asmlinkage void kvm_handle_fault_on_reboot(void)
1814{
1815 if (kvm_rebooting)
1816 /* spin while reset goes on */
1817 while (true)
1818 ;
1819 /* Fault while not rebooting. We want the trace. */
1820 BUG();
1821}
1822EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1823
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001824static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001825 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001826{
1827 if (val == SYS_RESTART) {
1828 /*
1829 * Some (well, at least mine) BIOSes hang on reboot if
1830 * in vmx root mode.
1831 */
1832 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001833 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001834 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001835 }
1836 return NOTIFY_OK;
1837}
1838
1839static struct notifier_block kvm_reboot_notifier = {
1840 .notifier_call = kvm_reboot,
1841 .priority = 0,
1842};
1843
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001844void kvm_io_bus_init(struct kvm_io_bus *bus)
1845{
1846 memset(bus, 0, sizeof(*bus));
1847}
1848
1849void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1850{
1851 int i;
1852
1853 for (i = 0; i < bus->dev_count; i++) {
1854 struct kvm_io_device *pos = bus->devs[i];
1855
1856 kvm_iodevice_destructor(pos);
1857 }
1858}
1859
Laurent Vivier92760492008-05-30 16:05:53 +02001860struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1861 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001862{
1863 int i;
1864
1865 for (i = 0; i < bus->dev_count; i++) {
1866 struct kvm_io_device *pos = bus->devs[i];
1867
Laurent Vivier92760492008-05-30 16:05:53 +02001868 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001869 return pos;
1870 }
1871
1872 return NULL;
1873}
1874
1875void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1876{
1877 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1878
1879 bus->devs[bus->dev_count++] = dev;
1880}
1881
Avi Kivity774c47f2007-02-12 00:54:47 -08001882static struct notifier_block kvm_cpu_notifier = {
1883 .notifier_call = kvm_cpu_hotplug,
1884 .priority = 20, /* must be > scheduler priority */
1885};
1886
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001887static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001888{
1889 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001890 struct kvm *kvm;
1891
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001892 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001893 spin_lock(&kvm_lock);
1894 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001895 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001896 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001897 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001898}
1899
1900DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1901
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001902static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001903{
1904 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001905 struct kvm *kvm;
1906 struct kvm_vcpu *vcpu;
1907 int i;
1908
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001909 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001910 spin_lock(&kvm_lock);
1911 list_for_each_entry(kvm, &vm_list, vm_list)
1912 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001913 vcpu = kvm->vcpus[i];
1914 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001915 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001916 }
1917 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001918 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001919}
1920
Avi Kivityba1389b2007-11-18 16:24:12 +02001921DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1922
1923static struct file_operations *stat_fops[] = {
1924 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1925 [KVM_STAT_VM] = &vm_stat_fops,
1926};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001927
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001928static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929{
1930 struct kvm_stats_debugfs_item *p;
1931
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001932 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001934 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001935 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001936 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937}
1938
1939static void kvm_exit_debug(void)
1940{
1941 struct kvm_stats_debugfs_item *p;
1942
1943 for (p = debugfs_entries; p->name; ++p)
1944 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001945 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946}
1947
Avi Kivity59ae6c62007-02-12 00:54:48 -08001948static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1949{
Avi Kivity4267c412007-05-24 13:09:41 +03001950 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001951 return 0;
1952}
1953
1954static int kvm_resume(struct sys_device *dev)
1955{
Avi Kivity4267c412007-05-24 13:09:41 +03001956 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001957 return 0;
1958}
1959
1960static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01001961 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001962 .suspend = kvm_suspend,
1963 .resume = kvm_resume,
1964};
1965
1966static struct sys_device kvm_sysdev = {
1967 .id = 0,
1968 .cls = &kvm_sysdev_class,
1969};
1970
Izik Eiduscea7bb22007-10-17 19:17:48 +02001971struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001972pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973
Avi Kivity15ad7142007-07-11 18:17:21 +03001974static inline
1975struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
1976{
1977 return container_of(pn, struct kvm_vcpu, preempt_notifier);
1978}
1979
1980static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
1981{
1982 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1983
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001984 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001985}
1986
1987static void kvm_sched_out(struct preempt_notifier *pn,
1988 struct task_struct *next)
1989{
1990 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
1991
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001992 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001993}
1994
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08001995int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10001996 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997{
1998 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001999 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002001 kvm_init_debug();
2002
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002003 r = kvm_arch_init(opaque);
2004 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002005 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002006
2007 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2008
2009 if (bad_page == NULL) {
2010 r = -ENOMEM;
2011 goto out;
2012 }
2013
Anthony Liguori35149e22008-04-02 14:46:56 -05002014 bad_pfn = page_to_pfn(bad_page);
2015
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002016 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002018 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019
Yang, Sheng002c7f72007-07-31 14:23:01 +03002020 for_each_online_cpu(cpu) {
2021 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002022 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002023 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002024 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002025 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002026 }
2027
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002028 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002029 r = register_cpu_notifier(&kvm_cpu_notifier);
2030 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002031 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032 register_reboot_notifier(&kvm_reboot_notifier);
2033
Avi Kivity59ae6c62007-02-12 00:54:48 -08002034 r = sysdev_class_register(&kvm_sysdev_class);
2035 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002036 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002037
2038 r = sysdev_register(&kvm_sysdev);
2039 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002040 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002041
Rusty Russellc16f8622007-07-30 21:12:19 +10002042 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2043 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002044 __alignof__(struct kvm_vcpu),
2045 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002046 if (!kvm_vcpu_cache) {
2047 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002048 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002049 }
2050
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 kvm_chardev_ops.owner = module;
2052
2053 r = misc_register(&kvm_dev);
2054 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002055 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 goto out_free;
2057 }
2058
Avi Kivity15ad7142007-07-11 18:17:21 +03002059 kvm_preempt_ops.sched_in = kvm_sched_in;
2060 kvm_preempt_ops.sched_out = kvm_sched_out;
2061
Avi Kivityc7addb92007-09-16 18:58:32 +02002062 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063
2064out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002065 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002066out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002067 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002068out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002069 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002070out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002072 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002073out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002074 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002075out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002076 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002077out_free_0:
2078 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002079out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002080 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002081 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002082out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083 return r;
2084}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002085EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002087void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002089 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002091 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002092 sysdev_unregister(&kvm_sysdev);
2093 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002095 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002096 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002097 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002098 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02002100 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002102EXPORT_SYMBOL_GPL(kvm_exit);