blob: 2089f8b68a1699f68c3768820c81f211700f2f6f [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{
Sheng Yang4f906c12008-11-24 14:32:51 +0800143 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800144 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
Sheng Yang00e3ed32008-11-24 14:32:50 +0800179static int assigned_device_update_intx(struct kvm *kvm,
180 struct kvm_assigned_dev_kernel *adev,
181 struct kvm_assigned_irq *airq)
182{
Sheng Yangfbac7812008-11-24 14:32:52 +0800183 adev->guest_irq = airq->guest_irq;
184 adev->ack_notifier.gsi = airq->guest_irq;
185
186 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800187 return 0;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800188
189 if (irqchip_in_kernel(kvm)) {
190 if (!capable(CAP_SYS_RAWIO))
191 return -EPERM;
192
193 if (airq->host_irq)
194 adev->host_irq = airq->host_irq;
195 else
196 adev->host_irq = adev->dev->irq;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800197
198 /* Even though this is PCI, we don't want to use shared
199 * interrupts. Sharing host devices with guest-assigned devices
200 * on the same interrupt line is not a happy situation: there
201 * are going to be long delays in accepting, acking, etc.
202 */
203 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
204 0, "kvm_assigned_intx_device", (void *)adev))
205 return -EIO;
206 }
207
Sheng Yang4f906c12008-11-24 14:32:51 +0800208 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
209 KVM_ASSIGNED_DEV_HOST_INTX;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800210 return 0;
211}
212
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800213static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
214 struct kvm_assigned_irq
215 *assigned_irq)
216{
217 int r = 0;
218 struct kvm_assigned_dev_kernel *match;
219
220 mutex_lock(&kvm->lock);
221
222 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
223 assigned_irq->assigned_dev_id);
224 if (!match) {
225 mutex_unlock(&kvm->lock);
226 return -EINVAL;
227 }
228
Sheng Yang4f906c12008-11-24 14:32:51 +0800229 if (!match->irq_requested_type) {
Sheng Yang342ffb92008-11-24 14:32:49 +0800230 INIT_WORK(&match->interrupt_work,
231 kvm_assigned_dev_interrupt_work_handler);
232 if (irqchip_in_kernel(kvm)) {
233 /* Register ack nofitier */
234 match->ack_notifier.gsi = -1;
235 match->ack_notifier.irq_acked =
236 kvm_assigned_dev_ack_irq;
237 kvm_register_irq_ack_notifier(kvm,
238 &match->ack_notifier);
239
240 /* Request IRQ source ID */
241 r = kvm_request_irq_source_id(kvm);
242 if (r < 0)
243 goto out_release;
244 else
245 match->irq_source_id = r;
246 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800247 }
248
Sheng Yang00e3ed32008-11-24 14:32:50 +0800249 r = assigned_device_update_intx(kvm, match, assigned_irq);
250 if (r)
251 goto out_release;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800252
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800253 mutex_unlock(&kvm->lock);
254 return r;
255out_release:
256 mutex_unlock(&kvm->lock);
257 kvm_free_assigned_device(kvm, match);
258 return r;
259}
260
261static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
262 struct kvm_assigned_pci_dev *assigned_dev)
263{
264 int r = 0;
265 struct kvm_assigned_dev_kernel *match;
266 struct pci_dev *dev;
267
268 mutex_lock(&kvm->lock);
269
270 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
271 assigned_dev->assigned_dev_id);
272 if (match) {
273 /* device already assigned */
274 r = -EINVAL;
275 goto out;
276 }
277
278 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
279 if (match == NULL) {
280 printk(KERN_INFO "%s: Couldn't allocate memory\n",
281 __func__);
282 r = -ENOMEM;
283 goto out;
284 }
285 dev = pci_get_bus_and_slot(assigned_dev->busnr,
286 assigned_dev->devfn);
287 if (!dev) {
288 printk(KERN_INFO "%s: host device not found\n", __func__);
289 r = -EINVAL;
290 goto out_free;
291 }
292 if (pci_enable_device(dev)) {
293 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
294 r = -EBUSY;
295 goto out_put;
296 }
297 r = pci_request_regions(dev, "kvm_assigned_device");
298 if (r) {
299 printk(KERN_INFO "%s: Could not get access to device regions\n",
300 __func__);
301 goto out_disable;
302 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800303
304 pci_reset_function(dev);
305
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800306 match->assigned_dev_id = assigned_dev->assigned_dev_id;
307 match->host_busnr = assigned_dev->busnr;
308 match->host_devfn = assigned_dev->devfn;
309 match->dev = dev;
310
311 match->kvm = kvm;
312
313 list_add(&match->list, &kvm->arch.assigned_dev_head);
314
315 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
316 r = kvm_iommu_map_guest(kvm, match);
317 if (r)
318 goto out_list_del;
319 }
320
321out:
322 mutex_unlock(&kvm->lock);
323 return r;
324out_list_del:
325 list_del(&match->list);
326 pci_release_regions(dev);
327out_disable:
328 pci_disable_device(dev);
329out_put:
330 pci_dev_put(dev);
331out_free:
332 kfree(match);
333 mutex_unlock(&kvm->lock);
334 return r;
335}
336#endif
337
James Morris5aacf0c2006-12-22 01:04:55 -0800338static inline int valid_vcpu(int n)
339{
340 return likely(n >= 0 && n < KVM_MAX_VCPUS);
341}
342
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800343inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300344{
345 if (pfn_valid(pfn))
346 return PageReserved(pfn_to_page(pfn));
347
348 return true;
349}
350
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351/*
352 * Switches to specified vcpu, until a matching vcpu_put()
353 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200354void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355{
Avi Kivity15ad7142007-07-11 18:17:21 +0300356 int cpu;
357
Avi Kivitybccf2152007-02-21 18:04:26 +0200358 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300359 cpu = get_cpu();
360 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200361 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300362 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200363}
364
Carsten Otte313a3dc2007-10-11 19:16:52 +0200365void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366{
Avi Kivity15ad7142007-07-11 18:17:21 +0300367 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200368 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300369 preempt_notifier_unregister(&vcpu->preempt_notifier);
370 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 mutex_unlock(&vcpu->mutex);
372}
373
Avi Kivityd9e368d2007-06-07 19:18:30 +0300374static void ack_flush(void *_completed)
375{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300376}
377
378void kvm_flush_remote_tlbs(struct kvm *kvm)
379{
Avi Kivity597a5f52008-07-20 14:24:22 +0300380 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300381 cpumask_t cpus;
382 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300383
Avi Kivity597a5f52008-07-20 14:24:22 +0300384 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300385 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000386 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
387 vcpu = kvm->vcpus[i];
388 if (!vcpu)
389 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200390 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300391 continue;
392 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300393 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200394 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300395 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200396 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300397 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200398 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200399 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300400out:
401 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300402}
403
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500404void kvm_reload_remote_mmus(struct kvm *kvm)
405{
Avi Kivity597a5f52008-07-20 14:24:22 +0300406 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500407 cpumask_t cpus;
408 struct kvm_vcpu *vcpu;
409
Avi Kivity597a5f52008-07-20 14:24:22 +0300410 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500411 cpus_clear(cpus);
412 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
413 vcpu = kvm->vcpus[i];
414 if (!vcpu)
415 continue;
416 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
417 continue;
418 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300419 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500420 cpu_set(cpu, cpus);
421 }
422 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300423 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500424 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300425out:
426 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500427}
428
429
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000430int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
431{
432 struct page *page;
433 int r;
434
435 mutex_init(&vcpu->mutex);
436 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000437 vcpu->kvm = kvm;
438 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300439 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000440
441 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
442 if (!page) {
443 r = -ENOMEM;
444 goto fail;
445 }
446 vcpu->run = page_address(page);
447
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800448 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000449 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800450 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000451 return 0;
452
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000453fail_free_run:
454 free_page((unsigned long)vcpu->run);
455fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000456 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000457}
458EXPORT_SYMBOL_GPL(kvm_vcpu_init);
459
460void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
461{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800462 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000463 free_page((unsigned long)vcpu->run);
464}
465EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
466
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200467#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
468static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
469{
470 return container_of(mn, struct kvm, mmu_notifier);
471}
472
473static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
474 struct mm_struct *mm,
475 unsigned long address)
476{
477 struct kvm *kvm = mmu_notifier_to_kvm(mn);
478 int need_tlb_flush;
479
480 /*
481 * When ->invalidate_page runs, the linux pte has been zapped
482 * already but the page is still allocated until
483 * ->invalidate_page returns. So if we increase the sequence
484 * here the kvm page fault will notice if the spte can't be
485 * established because the page is going to be freed. If
486 * instead the kvm page fault establishes the spte before
487 * ->invalidate_page runs, kvm_unmap_hva will release it
488 * before returning.
489 *
490 * The sequence increase only need to be seen at spin_unlock
491 * time, and not at spin_lock time.
492 *
493 * Increasing the sequence after the spin_unlock would be
494 * unsafe because the kvm page fault could then establish the
495 * pte after kvm_unmap_hva returned, without noticing the page
496 * is going to be freed.
497 */
498 spin_lock(&kvm->mmu_lock);
499 kvm->mmu_notifier_seq++;
500 need_tlb_flush = kvm_unmap_hva(kvm, address);
501 spin_unlock(&kvm->mmu_lock);
502
503 /* we've to flush the tlb before the pages can be freed */
504 if (need_tlb_flush)
505 kvm_flush_remote_tlbs(kvm);
506
507}
508
509static void kvm_mmu_notifier_invalidate_range_start(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 int need_tlb_flush = 0;
516
517 spin_lock(&kvm->mmu_lock);
518 /*
519 * The count increase must become visible at unlock time as no
520 * spte can be established without taking the mmu_lock and
521 * count is also read inside the mmu_lock critical section.
522 */
523 kvm->mmu_notifier_count++;
524 for (; start < end; start += PAGE_SIZE)
525 need_tlb_flush |= kvm_unmap_hva(kvm, start);
526 spin_unlock(&kvm->mmu_lock);
527
528 /* we've to flush the tlb before the pages can be freed */
529 if (need_tlb_flush)
530 kvm_flush_remote_tlbs(kvm);
531}
532
533static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
534 struct mm_struct *mm,
535 unsigned long start,
536 unsigned long end)
537{
538 struct kvm *kvm = mmu_notifier_to_kvm(mn);
539
540 spin_lock(&kvm->mmu_lock);
541 /*
542 * This sequence increase will notify the kvm page fault that
543 * the page that is going to be mapped in the spte could have
544 * been freed.
545 */
546 kvm->mmu_notifier_seq++;
547 /*
548 * The above sequence increase must be visible before the
549 * below count decrease but both values are read by the kvm
550 * page fault under mmu_lock spinlock so we don't need to add
551 * a smb_wmb() here in between the two.
552 */
553 kvm->mmu_notifier_count--;
554 spin_unlock(&kvm->mmu_lock);
555
556 BUG_ON(kvm->mmu_notifier_count < 0);
557}
558
559static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
560 struct mm_struct *mm,
561 unsigned long address)
562{
563 struct kvm *kvm = mmu_notifier_to_kvm(mn);
564 int young;
565
566 spin_lock(&kvm->mmu_lock);
567 young = kvm_age_hva(kvm, address);
568 spin_unlock(&kvm->mmu_lock);
569
570 if (young)
571 kvm_flush_remote_tlbs(kvm);
572
573 return young;
574}
575
576static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
577 .invalidate_page = kvm_mmu_notifier_invalidate_page,
578 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
579 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
580 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
581};
582#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
583
Avi Kivityf17abe92007-02-21 19:28:04 +0200584static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800586 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200587#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
588 struct page *page;
589#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800591 if (IS_ERR(kvm))
592 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593
Laurent Vivier5f94c172008-05-30 16:05:54 +0200594#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
595 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
596 if (!page) {
597 kfree(kvm);
598 return ERR_PTR(-ENOMEM);
599 }
600 kvm->coalesced_mmio_ring =
601 (struct kvm_coalesced_mmio_ring *)page_address(page);
602#endif
603
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200604#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
605 {
606 int err;
607 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
608 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
609 if (err) {
610#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
611 put_page(page);
612#endif
613 kfree(kvm);
614 return ERR_PTR(err);
615 }
616 }
617#endif
618
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200619 kvm->mm = current->mm;
620 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500621 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300622 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800623 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400624 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200625 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300626 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000627 spin_lock(&kvm_lock);
628 list_add(&kvm->vm_list, &vm_list);
629 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200630#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
631 kvm_coalesced_mmio_init(kvm);
632#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800633out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200634 return kvm;
635}
636
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637/*
638 * Free any memory in @free but not in @dont.
639 */
640static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
641 struct kvm_memory_slot *dont)
642{
Izik Eidus290fc382007-09-27 14:11:22 +0200643 if (!dont || free->rmap != dont->rmap)
644 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
646 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
647 vfree(free->dirty_bitmap);
648
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300649 if (!dont || free->lpage_info != dont->lpage_info)
650 vfree(free->lpage_info);
651
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000653 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500654 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300655 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656}
657
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800658void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659{
660 int i;
661
662 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000663 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664}
665
Avi Kivityf17abe92007-02-21 19:28:04 +0200666static void kvm_destroy_vm(struct kvm *kvm)
667{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200668 struct mm_struct *mm = kvm->mm;
669
Avi Kivity133de902007-02-12 00:54:44 -0800670 spin_lock(&kvm_lock);
671 list_del(&kvm->vm_list);
672 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300673 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400674 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200675#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
676 if (kvm->coalesced_mmio_ring != NULL)
677 free_page((unsigned long)kvm->coalesced_mmio_ring);
678#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200679#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
680 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
681#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800682 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200683 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200684}
685
Izik Eidusd39f13b2008-03-30 16:01:25 +0300686void kvm_get_kvm(struct kvm *kvm)
687{
688 atomic_inc(&kvm->users_count);
689}
690EXPORT_SYMBOL_GPL(kvm_get_kvm);
691
692void kvm_put_kvm(struct kvm *kvm)
693{
694 if (atomic_dec_and_test(&kvm->users_count))
695 kvm_destroy_vm(kvm);
696}
697EXPORT_SYMBOL_GPL(kvm_put_kvm);
698
699
Avi Kivityf17abe92007-02-21 19:28:04 +0200700static int kvm_vm_release(struct inode *inode, struct file *filp)
701{
702 struct kvm *kvm = filp->private_data;
703
Izik Eidusd39f13b2008-03-30 16:01:25 +0300704 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705 return 0;
706}
707
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 * Allocate some memory and give it an address in the guest physical address
710 * space.
711 *
712 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800713 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500714 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800716int __kvm_set_memory_region(struct kvm *kvm,
717 struct kvm_userspace_memory_region *mem,
718 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719{
720 int r;
721 gfn_t base_gfn;
722 unsigned long npages;
723 unsigned long i;
724 struct kvm_memory_slot *memslot;
725 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726
727 r = -EINVAL;
728 /* General sanity checks */
729 if (mem->memory_size & (PAGE_SIZE - 1))
730 goto out;
731 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
732 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800733 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600734 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200735 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 goto out;
737 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
738 goto out;
739
740 memslot = &kvm->memslots[mem->slot];
741 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
742 npages = mem->memory_size >> PAGE_SHIFT;
743
744 if (!npages)
745 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
746
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 new = old = *memslot;
748
749 new.base_gfn = base_gfn;
750 new.npages = npages;
751 new.flags = mem->flags;
752
753 /* Disallow changing a memory slot's size. */
754 r = -EINVAL;
755 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800756 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757
758 /* Check for overlaps */
759 r = -EEXIST;
760 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
761 struct kvm_memory_slot *s = &kvm->memslots[i];
762
763 if (s == memslot)
764 continue;
765 if (!((base_gfn + npages <= s->base_gfn) ||
766 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800767 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 /* Free page dirty bitmap if unneeded */
771 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000772 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
774 r = -ENOMEM;
775
776 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200777#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500778 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400779 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200780
781 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800782 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200783
Izik Eidus290fc382007-09-27 14:11:22 +0200784 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500785
Izik Eidus80b14b52007-10-25 11:54:04 +0200786 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200787 /*
788 * hva_to_rmmap() serialzies with the mmu_lock and to be
789 * safe it has to ignore memslots with !user_alloc &&
790 * !userspace_addr.
791 */
792 if (user_alloc)
793 new.userspace_addr = mem->userspace_addr;
794 else
795 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300797 if (npages && !new.lpage_info) {
798 int largepages = npages / KVM_PAGES_PER_HPAGE;
799 if (npages % KVM_PAGES_PER_HPAGE)
800 largepages++;
801 if (base_gfn % KVM_PAGES_PER_HPAGE)
802 largepages++;
803
804 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
805
806 if (!new.lpage_info)
807 goto out_free;
808
809 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
810
811 if (base_gfn % KVM_PAGES_PER_HPAGE)
812 new.lpage_info[0].write_count = 1;
813 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
814 new.lpage_info[largepages-1].write_count = 1;
815 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816
817 /* Allocate page dirty bitmap if needed */
818 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
819 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
820
821 new.dirty_bitmap = vmalloc(dirty_bytes);
822 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800823 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 memset(new.dirty_bitmap, 0, dirty_bytes);
825 }
Carsten Otteeff01142008-06-27 15:05:31 +0200826#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300828 if (!npages)
829 kvm_arch_flush_shadow(kvm);
830
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200831 spin_lock(&kvm->mmu_lock);
832 if (mem->slot >= kvm->nmemslots)
833 kvm->nmemslots = mem->slot + 1;
834
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200836 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Zhang Xiantao0de10342007-11-20 16:25:04 +0800838 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
839 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200840 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800841 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200842 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800843 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800844 }
845
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 kvm_free_physmem_slot(&old, &new);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800847#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300848 /* map the pages in iommu page table */
849 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
850 if (r)
851 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800852#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 return 0;
854
Sheng Yangf78e0e22007-10-29 09:40:42 +0800855out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 kvm_free_physmem_slot(&new, &old);
857out:
858 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200859
860}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800861EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
862
863int kvm_set_memory_region(struct kvm *kvm,
864 struct kvm_userspace_memory_region *mem,
865 int user_alloc)
866{
867 int r;
868
Izik Eidus72dc67a2008-02-10 18:04:15 +0200869 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800870 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200871 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800872 return r;
873}
Izik Eidus210c7c42007-10-24 23:52:57 +0200874EXPORT_SYMBOL_GPL(kvm_set_memory_region);
875
Carsten Otte1fe779f2007-10-29 16:08:35 +0100876int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
877 struct
878 kvm_userspace_memory_region *mem,
879 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200880{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200881 if (mem->slot >= KVM_MEMORY_SLOTS)
882 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200883 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884}
885
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800886int kvm_get_dirty_log(struct kvm *kvm,
887 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888{
889 struct kvm_memory_slot *memslot;
890 int r, i;
891 int n;
892 unsigned long any = 0;
893
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 r = -EINVAL;
895 if (log->slot >= KVM_MEMORY_SLOTS)
896 goto out;
897
898 memslot = &kvm->memslots[log->slot];
899 r = -ENOENT;
900 if (!memslot->dirty_bitmap)
901 goto out;
902
Uri Lublincd1a4a92007-02-22 16:43:09 +0200903 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904
Uri Lublincd1a4a92007-02-22 16:43:09 +0200905 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 any = memslot->dirty_bitmap[i];
907
908 r = -EFAULT;
909 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
910 goto out;
911
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800912 if (any)
913 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914
915 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 return r;
918}
919
Izik Eiduscea7bb22007-10-17 19:17:48 +0200920int is_error_page(struct page *page)
921{
922 return page == bad_page;
923}
924EXPORT_SYMBOL_GPL(is_error_page);
925
Anthony Liguori35149e22008-04-02 14:46:56 -0500926int is_error_pfn(pfn_t pfn)
927{
928 return pfn == bad_pfn;
929}
930EXPORT_SYMBOL_GPL(is_error_pfn);
931
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200932static inline unsigned long bad_hva(void)
933{
934 return PAGE_OFFSET;
935}
936
937int kvm_is_error_hva(unsigned long addr)
938{
939 return addr == bad_hva();
940}
941EXPORT_SYMBOL_GPL(kvm_is_error_hva);
942
Izik Eidus28430992008-10-03 17:40:32 +0300943struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944{
945 int i;
946
947 for (i = 0; i < kvm->nmemslots; ++i) {
948 struct kvm_memory_slot *memslot = &kvm->memslots[i];
949
950 if (gfn >= memslot->base_gfn
951 && gfn < memslot->base_gfn + memslot->npages)
952 return memslot;
953 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000954 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955}
Izik Eidus28430992008-10-03 17:40:32 +0300956EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +0300957
958struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
959{
960 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300961 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +0300962}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963
Izik Eiduse0d62c72007-10-24 23:57:46 +0200964int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
965{
966 int i;
967
968 gfn = unalias_gfn(kvm, gfn);
969 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
970 struct kvm_memory_slot *memslot = &kvm->memslots[i];
971
972 if (gfn >= memslot->base_gfn
973 && gfn < memslot->base_gfn + memslot->npages)
974 return 1;
975 }
976 return 0;
977}
978EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
979
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300980unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +0200981{
982 struct kvm_memory_slot *slot;
983
984 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +0300985 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +0200986 if (!slot)
987 return bad_hva();
988 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
989}
Sheng Yang0d150292008-04-25 21:44:50 +0800990EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +0200991
Anthony Liguori35149e22008-04-02 14:46:56 -0500992pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300993{
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500994 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +0200995 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500996 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -0500997 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300998
Avi Kivity60395222007-10-21 11:03:36 +0200999 might_sleep();
1000
Izik Eidus539cb662007-11-11 22:05:04 +02001001 addr = gfn_to_hva(kvm, gfn);
1002 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001003 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001004 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001005 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001006
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001007 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001008
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001009 if (unlikely(npages != 1)) {
1010 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001011
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001012 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001013 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001014
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001015 if (vma == NULL || addr < vma->vm_start ||
1016 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001017 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001018 get_page(bad_page);
1019 return page_to_pfn(bad_page);
1020 }
1021
1022 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001023 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001024 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001025 } else
1026 pfn = page_to_pfn(page[0]);
1027
1028 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001029}
1030
1031EXPORT_SYMBOL_GPL(gfn_to_pfn);
1032
1033struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1034{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001035 pfn_t pfn;
1036
1037 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001038 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001039 return pfn_to_page(pfn);
1040
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001041 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001042
1043 get_page(bad_page);
1044 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001045}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001046
Avi Kivity954bbbc2007-03-30 14:02:32 +03001047EXPORT_SYMBOL_GPL(gfn_to_page);
1048
Izik Eidusb4231d62007-11-20 11:49:33 +02001049void kvm_release_page_clean(struct page *page)
1050{
Anthony Liguori35149e22008-04-02 14:46:56 -05001051 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001052}
1053EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1054
Anthony Liguori35149e22008-04-02 14:46:56 -05001055void kvm_release_pfn_clean(pfn_t pfn)
1056{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001057 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001058 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001059}
1060EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1061
Izik Eidusb4231d62007-11-20 11:49:33 +02001062void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001063{
Anthony Liguori35149e22008-04-02 14:46:56 -05001064 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001065}
Izik Eidusb4231d62007-11-20 11:49:33 +02001066EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001067
Anthony Liguori35149e22008-04-02 14:46:56 -05001068void kvm_release_pfn_dirty(pfn_t pfn)
1069{
1070 kvm_set_pfn_dirty(pfn);
1071 kvm_release_pfn_clean(pfn);
1072}
1073EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1074
1075void kvm_set_page_dirty(struct page *page)
1076{
1077 kvm_set_pfn_dirty(page_to_pfn(page));
1078}
1079EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1080
1081void kvm_set_pfn_dirty(pfn_t pfn)
1082{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001083 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001084 struct page *page = pfn_to_page(pfn);
1085 if (!PageReserved(page))
1086 SetPageDirty(page);
1087 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001088}
1089EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1090
1091void kvm_set_pfn_accessed(pfn_t pfn)
1092{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001093 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001094 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001095}
1096EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1097
1098void kvm_get_pfn(pfn_t pfn)
1099{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001100 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001101 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001102}
1103EXPORT_SYMBOL_GPL(kvm_get_pfn);
1104
Izik Eidus195aefd2007-10-01 22:14:18 +02001105static int next_segment(unsigned long len, int offset)
1106{
1107 if (len > PAGE_SIZE - offset)
1108 return PAGE_SIZE - offset;
1109 else
1110 return len;
1111}
1112
1113int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1114 int len)
1115{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001116 int r;
1117 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001118
Izik Eiduse0506bc2007-11-11 22:10:22 +02001119 addr = gfn_to_hva(kvm, gfn);
1120 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001121 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001122 r = copy_from_user(data, (void __user *)addr + offset, len);
1123 if (r)
1124 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001125 return 0;
1126}
1127EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1128
1129int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1130{
1131 gfn_t gfn = gpa >> PAGE_SHIFT;
1132 int seg;
1133 int offset = offset_in_page(gpa);
1134 int ret;
1135
1136 while ((seg = next_segment(len, offset)) != 0) {
1137 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1138 if (ret < 0)
1139 return ret;
1140 offset = 0;
1141 len -= seg;
1142 data += seg;
1143 ++gfn;
1144 }
1145 return 0;
1146}
1147EXPORT_SYMBOL_GPL(kvm_read_guest);
1148
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001149int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1150 unsigned long len)
1151{
1152 int r;
1153 unsigned long addr;
1154 gfn_t gfn = gpa >> PAGE_SHIFT;
1155 int offset = offset_in_page(gpa);
1156
1157 addr = gfn_to_hva(kvm, gfn);
1158 if (kvm_is_error_hva(addr))
1159 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001160 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001161 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001162 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001163 if (r)
1164 return -EFAULT;
1165 return 0;
1166}
1167EXPORT_SYMBOL(kvm_read_guest_atomic);
1168
Izik Eidus195aefd2007-10-01 22:14:18 +02001169int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1170 int offset, int len)
1171{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001172 int r;
1173 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001174
Izik Eiduse0506bc2007-11-11 22:10:22 +02001175 addr = gfn_to_hva(kvm, gfn);
1176 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001177 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001178 r = copy_to_user((void __user *)addr + offset, data, len);
1179 if (r)
1180 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001181 mark_page_dirty(kvm, gfn);
1182 return 0;
1183}
1184EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1185
1186int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1187 unsigned long len)
1188{
1189 gfn_t gfn = gpa >> PAGE_SHIFT;
1190 int seg;
1191 int offset = offset_in_page(gpa);
1192 int ret;
1193
1194 while ((seg = next_segment(len, offset)) != 0) {
1195 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1196 if (ret < 0)
1197 return ret;
1198 offset = 0;
1199 len -= seg;
1200 data += seg;
1201 ++gfn;
1202 }
1203 return 0;
1204}
1205
1206int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1207{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001208 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001209}
1210EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1211
1212int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1213{
1214 gfn_t gfn = gpa >> PAGE_SHIFT;
1215 int seg;
1216 int offset = offset_in_page(gpa);
1217 int ret;
1218
1219 while ((seg = next_segment(len, offset)) != 0) {
1220 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1221 if (ret < 0)
1222 return ret;
1223 offset = 0;
1224 len -= seg;
1225 ++gfn;
1226 }
1227 return 0;
1228}
1229EXPORT_SYMBOL_GPL(kvm_clear_guest);
1230
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1232{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001233 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234
Uri Lublin3b6fff12007-10-30 10:42:09 +02001235 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001236 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001237 if (memslot && memslot->dirty_bitmap) {
1238 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239
Rusty Russell7e9d6192007-07-31 20:41:14 +10001240 /* avoid RMW */
1241 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1242 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 }
1244}
1245
Eddie Dongb6958ce2007-07-18 12:15:21 +03001246/*
1247 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1248 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001249void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001250{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001251 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001252
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001253 for (;;) {
1254 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001255
Marcelo Tosattid7690172008-09-08 15:23:48 -03001256 if (kvm_cpu_has_interrupt(vcpu) ||
1257 kvm_cpu_has_pending_timer(vcpu) ||
1258 kvm_arch_vcpu_runnable(vcpu)) {
1259 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001260 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001261 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001262 if (signal_pending(current))
1263 break;
1264
Eddie Dongb6958ce2007-07-18 12:15:21 +03001265 vcpu_put(vcpu);
1266 schedule();
1267 vcpu_load(vcpu);
1268 }
1269
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001270 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001271}
1272
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273void kvm_resched(struct kvm_vcpu *vcpu)
1274{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001275 if (!need_resched())
1276 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279EXPORT_SYMBOL_GPL(kvm_resched);
1280
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001281static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001282{
1283 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001284 struct page *page;
1285
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001286 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001287 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001288#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001289 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001290 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001291#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001292#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1293 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1294 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1295#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001296 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001297 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001298 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001299 vmf->page = page;
1300 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001301}
1302
1303static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001304 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001305};
1306
1307static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1308{
1309 vma->vm_ops = &kvm_vcpu_vm_ops;
1310 return 0;
1311}
1312
Avi Kivitybccf2152007-02-21 18:04:26 +02001313static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1314{
1315 struct kvm_vcpu *vcpu = filp->private_data;
1316
Al Viro66c0b392008-04-19 20:33:56 +01001317 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001318 return 0;
1319}
1320
Jan Engelhardt5c502742008-01-22 20:46:14 +01001321static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001322 .release = kvm_vcpu_release,
1323 .unlocked_ioctl = kvm_vcpu_ioctl,
1324 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001325 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001326};
1327
1328/*
1329 * Allocates an inode for the vcpu.
1330 */
1331static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1332{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001333 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001334 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001335 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001336 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001337}
1338
Avi Kivityc5ea7662007-02-20 18:41:05 +02001339/*
1340 * Creates some virtual cpus. Good luck creating more than one.
1341 */
1342static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1343{
1344 int r;
1345 struct kvm_vcpu *vcpu;
1346
Avi Kivityc5ea7662007-02-20 18:41:05 +02001347 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001348 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001349
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001350 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001351 if (IS_ERR(vcpu))
1352 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001353
Avi Kivity15ad7142007-07-11 18:17:21 +03001354 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1355
Avi Kivity26e52152007-11-20 15:30:24 +02001356 r = kvm_arch_vcpu_setup(vcpu);
1357 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001358 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001359
Shaohua Li11ec2802007-07-23 14:51:37 +08001360 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001361 if (kvm->vcpus[n]) {
1362 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001363 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001364 }
1365 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001366 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001367
1368 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001369 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001370 r = create_vcpu_fd(vcpu);
1371 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001372 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001373 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001374
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001375unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001376 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001377 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001378vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001379 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001380 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001381 return r;
1382}
1383
Avi Kivity1961d272007-03-05 19:46:05 +02001384static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1385{
1386 if (sigset) {
1387 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1388 vcpu->sigset_active = 1;
1389 vcpu->sigset = *sigset;
1390 } else
1391 vcpu->sigset_active = 0;
1392 return 0;
1393}
1394
Avi Kivitybccf2152007-02-21 18:04:26 +02001395static long kvm_vcpu_ioctl(struct file *filp,
1396 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397{
Avi Kivitybccf2152007-02-21 18:04:26 +02001398 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001399 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001400 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001401 struct kvm_fpu *fpu = NULL;
1402 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001404 if (vcpu->kvm->mm != current->mm)
1405 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001407 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001408 r = -EINVAL;
1409 if (arg)
1410 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001411 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 case KVM_GET_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;
1420 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001422 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001424 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1425 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001427out_free1:
1428 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 break;
1430 }
1431 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001432 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001434 r = -ENOMEM;
1435 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1436 if (!kvm_regs)
1437 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001439 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1440 goto out_free2;
1441 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001443 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001445out_free2:
1446 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 break;
1448 }
1449 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001450 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1451 r = -ENOMEM;
1452 if (!kvm_sregs)
1453 goto out;
1454 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 if (r)
1456 goto out;
1457 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001458 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 goto out;
1460 r = 0;
1461 break;
1462 }
1463 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001464 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1465 r = -ENOMEM;
1466 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001468 r = -EFAULT;
1469 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1470 goto out;
1471 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 if (r)
1473 goto out;
1474 r = 0;
1475 break;
1476 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001477 case KVM_GET_MP_STATE: {
1478 struct kvm_mp_state mp_state;
1479
1480 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1481 if (r)
1482 goto out;
1483 r = -EFAULT;
1484 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1485 goto out;
1486 r = 0;
1487 break;
1488 }
1489 case KVM_SET_MP_STATE: {
1490 struct kvm_mp_state mp_state;
1491
1492 r = -EFAULT;
1493 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1494 goto out;
1495 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1496 if (r)
1497 goto out;
1498 r = 0;
1499 break;
1500 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 case KVM_TRANSLATE: {
1502 struct kvm_translation tr;
1503
1504 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001505 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001507 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 if (r)
1509 goto out;
1510 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001511 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 goto out;
1513 r = 0;
1514 break;
1515 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 case KVM_DEBUG_GUEST: {
1517 struct kvm_debug_guest dbg;
1518
1519 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001520 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001522 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 if (r)
1524 goto out;
1525 r = 0;
1526 break;
1527 }
Avi Kivity1961d272007-03-05 19:46:05 +02001528 case KVM_SET_SIGNAL_MASK: {
1529 struct kvm_signal_mask __user *sigmask_arg = argp;
1530 struct kvm_signal_mask kvm_sigmask;
1531 sigset_t sigset, *p;
1532
1533 p = NULL;
1534 if (argp) {
1535 r = -EFAULT;
1536 if (copy_from_user(&kvm_sigmask, argp,
1537 sizeof kvm_sigmask))
1538 goto out;
1539 r = -EINVAL;
1540 if (kvm_sigmask.len != sizeof sigset)
1541 goto out;
1542 r = -EFAULT;
1543 if (copy_from_user(&sigset, sigmask_arg->sigset,
1544 sizeof sigset))
1545 goto out;
1546 p = &sigset;
1547 }
1548 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1549 break;
1550 }
Avi Kivityb8836732007-04-01 16:34:31 +03001551 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001552 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1553 r = -ENOMEM;
1554 if (!fpu)
1555 goto out;
1556 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001557 if (r)
1558 goto out;
1559 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001560 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001561 goto out;
1562 r = 0;
1563 break;
1564 }
1565 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001566 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1567 r = -ENOMEM;
1568 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001569 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001570 r = -EFAULT;
1571 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1572 goto out;
1573 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001574 if (r)
1575 goto out;
1576 r = 0;
1577 break;
1578 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001579 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001580 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001581 }
1582out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001583 kfree(fpu);
1584 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001585 return r;
1586}
1587
1588static long kvm_vm_ioctl(struct file *filp,
1589 unsigned int ioctl, unsigned long arg)
1590{
1591 struct kvm *kvm = filp->private_data;
1592 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001593 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001594
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001595 if (kvm->mm != current->mm)
1596 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001597 switch (ioctl) {
1598 case KVM_CREATE_VCPU:
1599 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1600 if (r < 0)
1601 goto out;
1602 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001603 case KVM_SET_USER_MEMORY_REGION: {
1604 struct kvm_userspace_memory_region kvm_userspace_mem;
1605
1606 r = -EFAULT;
1607 if (copy_from_user(&kvm_userspace_mem, argp,
1608 sizeof kvm_userspace_mem))
1609 goto out;
1610
1611 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612 if (r)
1613 goto out;
1614 break;
1615 }
1616 case KVM_GET_DIRTY_LOG: {
1617 struct kvm_dirty_log log;
1618
1619 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001620 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001622 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 if (r)
1624 goto out;
1625 break;
1626 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001627#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1628 case KVM_REGISTER_COALESCED_MMIO: {
1629 struct kvm_coalesced_mmio_zone zone;
1630 r = -EFAULT;
1631 if (copy_from_user(&zone, argp, sizeof zone))
1632 goto out;
1633 r = -ENXIO;
1634 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1635 if (r)
1636 goto out;
1637 r = 0;
1638 break;
1639 }
1640 case KVM_UNREGISTER_COALESCED_MMIO: {
1641 struct kvm_coalesced_mmio_zone zone;
1642 r = -EFAULT;
1643 if (copy_from_user(&zone, argp, sizeof zone))
1644 goto out;
1645 r = -ENXIO;
1646 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1647 if (r)
1648 goto out;
1649 r = 0;
1650 break;
1651 }
1652#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001653#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1654 case KVM_ASSIGN_PCI_DEVICE: {
1655 struct kvm_assigned_pci_dev assigned_dev;
1656
1657 r = -EFAULT;
1658 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1659 goto out;
1660 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1661 if (r)
1662 goto out;
1663 break;
1664 }
1665 case KVM_ASSIGN_IRQ: {
1666 struct kvm_assigned_irq assigned_irq;
1667
1668 r = -EFAULT;
1669 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1670 goto out;
1671 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1672 if (r)
1673 goto out;
1674 break;
1675 }
1676#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001677 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001678 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001679 }
1680out:
1681 return r;
1682}
1683
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001684static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001685{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001686 struct page *page[1];
1687 unsigned long addr;
1688 int npages;
1689 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001690 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001691
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001692 addr = gfn_to_hva(kvm, gfn);
1693 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001694 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001695
1696 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1697 NULL);
1698 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001699 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001700
1701 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001702 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001703}
1704
1705static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001706 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001707};
1708
1709static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1710{
1711 vma->vm_ops = &kvm_vm_vm_ops;
1712 return 0;
1713}
1714
Jan Engelhardt5c502742008-01-22 20:46:14 +01001715static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001716 .release = kvm_vm_release,
1717 .unlocked_ioctl = kvm_vm_ioctl,
1718 .compat_ioctl = kvm_vm_ioctl,
1719 .mmap = kvm_vm_mmap,
1720};
1721
1722static int kvm_dev_ioctl_create_vm(void)
1723{
Al Viro2030a422008-02-23 06:46:49 -05001724 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001725 struct kvm *kvm;
1726
Avi Kivityf17abe92007-02-21 19:28:04 +02001727 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001728 if (IS_ERR(kvm))
1729 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001730 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001731 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001732 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001733
Avi Kivityf17abe92007-02-21 19:28:04 +02001734 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001735}
1736
1737static long kvm_dev_ioctl(struct file *filp,
1738 unsigned int ioctl, unsigned long arg)
1739{
Avi Kivity07c45a32007-03-07 13:05:38 +02001740 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001741
1742 switch (ioctl) {
1743 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001744 r = -EINVAL;
1745 if (arg)
1746 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001747 r = KVM_API_VERSION;
1748 break;
1749 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001750 r = -EINVAL;
1751 if (arg)
1752 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001753 r = kvm_dev_ioctl_create_vm();
1754 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001755 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001756 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001757 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001758 case KVM_GET_VCPU_MMAP_SIZE:
1759 r = -EINVAL;
1760 if (arg)
1761 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001762 r = PAGE_SIZE; /* struct kvm_run */
1763#ifdef CONFIG_X86
1764 r += PAGE_SIZE; /* pio data page */
1765#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001766#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1767 r += PAGE_SIZE; /* coalesced mmio ring page */
1768#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001769 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001770 case KVM_TRACE_ENABLE:
1771 case KVM_TRACE_PAUSE:
1772 case KVM_TRACE_DISABLE:
1773 r = kvm_trace_ioctl(ioctl, arg);
1774 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001776 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777 }
1778out:
1779 return r;
1780}
1781
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 .unlocked_ioctl = kvm_dev_ioctl,
1784 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785};
1786
1787static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001788 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 "kvm",
1790 &kvm_chardev_ops,
1791};
1792
Avi Kivity1b6c0162007-05-24 13:03:52 +03001793static void hardware_enable(void *junk)
1794{
1795 int cpu = raw_smp_processor_id();
1796
1797 if (cpu_isset(cpu, cpus_hardware_enabled))
1798 return;
1799 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001800 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001801}
1802
1803static void hardware_disable(void *junk)
1804{
1805 int cpu = raw_smp_processor_id();
1806
1807 if (!cpu_isset(cpu, cpus_hardware_enabled))
1808 return;
1809 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001810 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001811}
1812
Avi Kivity774c47f2007-02-12 00:54:47 -08001813static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1814 void *v)
1815{
1816 int cpu = (long)v;
1817
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001818 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001819 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001820 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001821 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1822 cpu);
1823 hardware_disable(NULL);
1824 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001825 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001826 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1827 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001828 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001829 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001830 case CPU_ONLINE:
1831 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1832 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001833 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001834 break;
1835 }
1836 return NOTIFY_OK;
1837}
1838
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001839
1840asmlinkage void kvm_handle_fault_on_reboot(void)
1841{
1842 if (kvm_rebooting)
1843 /* spin while reset goes on */
1844 while (true)
1845 ;
1846 /* Fault while not rebooting. We want the trace. */
1847 BUG();
1848}
1849EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1850
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001851static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001852 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001853{
1854 if (val == SYS_RESTART) {
1855 /*
1856 * Some (well, at least mine) BIOSes hang on reboot if
1857 * in vmx root mode.
1858 */
1859 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001860 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001861 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001862 }
1863 return NOTIFY_OK;
1864}
1865
1866static struct notifier_block kvm_reboot_notifier = {
1867 .notifier_call = kvm_reboot,
1868 .priority = 0,
1869};
1870
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001871void kvm_io_bus_init(struct kvm_io_bus *bus)
1872{
1873 memset(bus, 0, sizeof(*bus));
1874}
1875
1876void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1877{
1878 int i;
1879
1880 for (i = 0; i < bus->dev_count; i++) {
1881 struct kvm_io_device *pos = bus->devs[i];
1882
1883 kvm_iodevice_destructor(pos);
1884 }
1885}
1886
Laurent Vivier92760492008-05-30 16:05:53 +02001887struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1888 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001889{
1890 int i;
1891
1892 for (i = 0; i < bus->dev_count; i++) {
1893 struct kvm_io_device *pos = bus->devs[i];
1894
Laurent Vivier92760492008-05-30 16:05:53 +02001895 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001896 return pos;
1897 }
1898
1899 return NULL;
1900}
1901
1902void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1903{
1904 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1905
1906 bus->devs[bus->dev_count++] = dev;
1907}
1908
Avi Kivity774c47f2007-02-12 00:54:47 -08001909static struct notifier_block kvm_cpu_notifier = {
1910 .notifier_call = kvm_cpu_hotplug,
1911 .priority = 20, /* must be > scheduler priority */
1912};
1913
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001914static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001915{
1916 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001917 struct kvm *kvm;
1918
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001919 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001920 spin_lock(&kvm_lock);
1921 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001922 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001923 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001924 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001925}
1926
1927DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1928
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001929static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001930{
1931 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001932 struct kvm *kvm;
1933 struct kvm_vcpu *vcpu;
1934 int i;
1935
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001936 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001937 spin_lock(&kvm_lock);
1938 list_for_each_entry(kvm, &vm_list, vm_list)
1939 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001940 vcpu = kvm->vcpus[i];
1941 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001942 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001943 }
1944 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001945 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001946}
1947
Avi Kivityba1389b2007-11-18 16:24:12 +02001948DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
1949
1950static struct file_operations *stat_fops[] = {
1951 [KVM_STAT_VCPU] = &vcpu_stat_fops,
1952 [KVM_STAT_VM] = &vm_stat_fops,
1953};
Avi Kivity1165f5f2007-04-19 17:27:43 +03001954
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001955static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956{
1957 struct kvm_stats_debugfs_item *p;
1958
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001959 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001961 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03001962 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02001963 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964}
1965
1966static void kvm_exit_debug(void)
1967{
1968 struct kvm_stats_debugfs_item *p;
1969
1970 for (p = debugfs_entries; p->name; ++p)
1971 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05001972 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973}
1974
Avi Kivity59ae6c62007-02-12 00:54:48 -08001975static int kvm_suspend(struct sys_device *dev, pm_message_t state)
1976{
Avi Kivity4267c412007-05-24 13:09:41 +03001977 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001978 return 0;
1979}
1980
1981static int kvm_resume(struct sys_device *dev)
1982{
Avi Kivity4267c412007-05-24 13:09:41 +03001983 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08001984 return 0;
1985}
1986
1987static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001988 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08001989 .suspend = kvm_suspend,
1990 .resume = kvm_resume,
1991};
1992
1993static struct sys_device kvm_sysdev = {
1994 .id = 0,
1995 .cls = &kvm_sysdev_class,
1996};
1997
Izik Eiduscea7bb22007-10-17 19:17:48 +02001998struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05001999pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000
Avi Kivity15ad7142007-07-11 18:17:21 +03002001static inline
2002struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2003{
2004 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2005}
2006
2007static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2008{
2009 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2010
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002011 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002012}
2013
2014static void kvm_sched_out(struct preempt_notifier *pn,
2015 struct task_struct *next)
2016{
2017 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2018
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002019 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002020}
2021
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002022int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002023 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024{
2025 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002026 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002028 kvm_init_debug();
2029
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002030 r = kvm_arch_init(opaque);
2031 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002032 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002033
2034 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2035
2036 if (bad_page == NULL) {
2037 r = -ENOMEM;
2038 goto out;
2039 }
2040
Anthony Liguori35149e22008-04-02 14:46:56 -05002041 bad_pfn = page_to_pfn(bad_page);
2042
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002043 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002045 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046
Yang, Sheng002c7f72007-07-31 14:23:01 +03002047 for_each_online_cpu(cpu) {
2048 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002049 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002050 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002051 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002052 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002053 }
2054
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002055 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002056 r = register_cpu_notifier(&kvm_cpu_notifier);
2057 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002058 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059 register_reboot_notifier(&kvm_reboot_notifier);
2060
Avi Kivity59ae6c62007-02-12 00:54:48 -08002061 r = sysdev_class_register(&kvm_sysdev_class);
2062 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002063 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002064
2065 r = sysdev_register(&kvm_sysdev);
2066 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002067 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002068
Rusty Russellc16f8622007-07-30 21:12:19 +10002069 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2070 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002071 __alignof__(struct kvm_vcpu),
2072 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002073 if (!kvm_vcpu_cache) {
2074 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002075 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002076 }
2077
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 kvm_chardev_ops.owner = module;
2079
2080 r = misc_register(&kvm_dev);
2081 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002082 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083 goto out_free;
2084 }
2085
Avi Kivity15ad7142007-07-11 18:17:21 +03002086 kvm_preempt_ops.sched_in = kvm_sched_in;
2087 kvm_preempt_ops.sched_out = kvm_sched_out;
2088
Avi Kivityc7addb92007-09-16 18:58:32 +02002089 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090
2091out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002092 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002093out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002094 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002095out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002096 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002097out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002099 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002100out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002101 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002102out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002103 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002104out_free_0:
2105 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002106out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002107 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002108 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002109out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 return r;
2111}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002112EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002114void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002116 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002118 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002119 sysdev_unregister(&kvm_sysdev);
2120 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002122 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002123 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002124 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002125 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02002127 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002129EXPORT_SYMBOL_GPL(kvm_exit);