blob: 266bdaf0ce44d13bd80a895c44001d290ef37213 [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
Sheng Yang5319c662008-11-24 14:32:57 +080063static int msi2intx = 1;
64module_param(msi2intx, bool, 0);
65
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080066DEFINE_SPINLOCK(kvm_lock);
67LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080068
Rusty Russell7f59f492008-12-07 21:25:45 +103069static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030070
Rusty Russellc16f8622007-07-30 21:12:19 +100071struct kmem_cache *kvm_vcpu_cache;
72EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030073
Avi Kivity15ad7142007-07-11 18:17:21 +030074static __read_mostly struct preempt_ops kvm_preempt_ops;
75
Hollis Blanchard76f7c872008-04-15 16:05:42 -050076struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080077
Avi Kivitybccf2152007-02-21 18:04:26 +020078static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
79 unsigned long arg);
80
Hannes Edere8ba5d32008-11-28 17:02:42 +010081static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030082
Xiantao Zhang8a98f662008-10-06 13:47:38 +080083#ifdef KVM_CAP_DEVICE_ASSIGNMENT
84static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
85 int assigned_dev_id)
86{
87 struct list_head *ptr;
88 struct kvm_assigned_dev_kernel *match;
89
90 list_for_each(ptr, head) {
91 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
92 if (match->assigned_dev_id == assigned_dev_id)
93 return match;
94 }
95 return NULL;
96}
97
98static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
99{
100 struct kvm_assigned_dev_kernel *assigned_dev;
101
102 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
103 interrupt_work);
104
105 /* This is taken to safely inject irq inside the guest. When
106 * the interrupt injection (or the ioapic code) uses a
107 * finer-grained lock, update this
108 */
109 mutex_lock(&assigned_dev->kvm->lock);
Sheng Yang79950e12009-02-10 13:57:06 +0800110 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
111 assigned_dev->guest_irq, 1);
112
113 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI) {
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800114 enable_irq(assigned_dev->host_irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000115 assigned_dev->host_irq_disabled = false;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800116 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800117 mutex_unlock(&assigned_dev->kvm->lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800118}
119
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800120static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
121{
122 struct kvm_assigned_dev_kernel *assigned_dev =
123 (struct kvm_assigned_dev_kernel *) dev_id;
124
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800125 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000126
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800127 disable_irq_nosync(irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000128 assigned_dev->host_irq_disabled = true;
129
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800130 return IRQ_HANDLED;
131}
132
133/* Ack the irq line for an assigned device */
134static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
135{
136 struct kvm_assigned_dev_kernel *dev;
137
138 if (kian->gsi == -1)
139 return;
140
141 dev = container_of(kian, struct kvm_assigned_dev_kernel,
142 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000143
Sheng Yang5550af42008-10-15 20:15:06 +0800144 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000145
146 /* The guest irq may be shared so this ack may be
147 * from another device.
148 */
149 if (dev->host_irq_disabled) {
150 enable_irq(dev->host_irq);
151 dev->host_irq_disabled = false;
152 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800153}
154
Sheng Yangba4cef32009-01-06 10:03:03 +0800155/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000156static void kvm_free_assigned_irq(struct kvm *kvm,
157 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800158{
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000159 if (!irqchip_in_kernel(kvm))
160 return;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800161
Sheng Yange19e30e2008-10-20 16:07:10 +0800162 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000163
164 if (assigned_dev->irq_source_id != -1)
165 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
166 assigned_dev->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800167
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000168 if (!assigned_dev->irq_requested_type)
169 return;
170
Sheng Yangba4cef32009-01-06 10:03:03 +0800171 /*
172 * In kvm_free_device_irq, cancel_work_sync return true if:
173 * 1. work is scheduled, and then cancelled.
174 * 2. work callback is executed.
175 *
176 * The first one ensured that the irq is disabled and no more events
177 * would happen. But for the second one, the irq may be enabled (e.g.
178 * for MSI). So we disable irq here to prevent further events.
179 *
180 * Notice this maybe result in nested disable if the interrupt type is
181 * INTx, but it's OK for we are going to free it.
182 *
183 * If this function is a part of VM destroy, please ensure that till
184 * now, the kvm state is still legal for probably we also have to wait
185 * interrupt_work done.
186 */
187 disable_irq_nosync(assigned_dev->host_irq);
188 cancel_work_sync(&assigned_dev->interrupt_work);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800189
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000190 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
191
192 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
193 pci_disable_msi(assigned_dev->dev);
194
195 assigned_dev->irq_requested_type = 0;
196}
197
198
199static void kvm_free_assigned_device(struct kvm *kvm,
200 struct kvm_assigned_dev_kernel
201 *assigned_dev)
202{
203 kvm_free_assigned_irq(kvm, assigned_dev);
204
Sheng Yang6eb55812008-10-31 12:37:41 +0800205 pci_reset_function(assigned_dev->dev);
206
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800207 pci_release_regions(assigned_dev->dev);
208 pci_disable_device(assigned_dev->dev);
209 pci_dev_put(assigned_dev->dev);
210
211 list_del(&assigned_dev->list);
212 kfree(assigned_dev);
213}
214
215void kvm_free_all_assigned_devices(struct kvm *kvm)
216{
217 struct list_head *ptr, *ptr2;
218 struct kvm_assigned_dev_kernel *assigned_dev;
219
220 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
221 assigned_dev = list_entry(ptr,
222 struct kvm_assigned_dev_kernel,
223 list);
224
225 kvm_free_assigned_device(kvm, assigned_dev);
226 }
227}
228
Sheng Yang00e3ed32008-11-24 14:32:50 +0800229static int assigned_device_update_intx(struct kvm *kvm,
230 struct kvm_assigned_dev_kernel *adev,
231 struct kvm_assigned_irq *airq)
232{
Sheng Yangfbac7812008-11-24 14:32:52 +0800233 adev->guest_irq = airq->guest_irq;
234 adev->ack_notifier.gsi = airq->guest_irq;
235
236 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800237 return 0;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800238
239 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800240 if (!msi2intx &&
Sheng Yangd7cff1c2009-01-06 16:25:10 +0800241 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
242 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800243 pci_disable_msi(adev->dev);
244 }
245
Sheng Yang00e3ed32008-11-24 14:32:50 +0800246 if (!capable(CAP_SYS_RAWIO))
247 return -EPERM;
248
249 if (airq->host_irq)
250 adev->host_irq = airq->host_irq;
251 else
252 adev->host_irq = adev->dev->irq;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800253
254 /* Even though this is PCI, we don't want to use shared
255 * interrupts. Sharing host devices with guest-assigned devices
256 * on the same interrupt line is not a happy situation: there
257 * are going to be long delays in accepting, acking, etc.
258 */
259 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
260 0, "kvm_assigned_intx_device", (void *)adev))
261 return -EIO;
262 }
263
Sheng Yang4f906c12008-11-24 14:32:51 +0800264 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
265 KVM_ASSIGNED_DEV_HOST_INTX;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800266 return 0;
267}
268
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800269#ifdef CONFIG_X86
270static int assigned_device_update_msi(struct kvm *kvm,
271 struct kvm_assigned_dev_kernel *adev,
272 struct kvm_assigned_irq *airq)
273{
274 int r;
275
Sheng Yang79950e12009-02-10 13:57:06 +0800276 adev->guest_irq = airq->guest_irq;
Sheng Yang5319c662008-11-24 14:32:57 +0800277 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
278 /* x86 don't care upper address of guest msi message addr */
279 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
280 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
Sheng Yang5319c662008-11-24 14:32:57 +0800281 adev->ack_notifier.gsi = -1;
282 } else if (msi2intx) {
283 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
284 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
Sheng Yang5319c662008-11-24 14:32:57 +0800285 adev->ack_notifier.gsi = airq->guest_irq;
Sheng Yang17071fe2009-01-06 16:25:11 +0800286 } else {
287 /*
288 * Guest require to disable device MSI, we disable MSI and
289 * re-enable INTx by default again. Notice it's only for
290 * non-msi2intx.
291 */
292 assigned_device_update_intx(kvm, adev, airq);
293 return 0;
Sheng Yang5319c662008-11-24 14:32:57 +0800294 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800295
296 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
297 return 0;
298
299 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800300 if (!msi2intx) {
301 if (adev->irq_requested_type &
302 KVM_ASSIGNED_DEV_HOST_INTX)
303 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800304
Sheng Yang5319c662008-11-24 14:32:57 +0800305 r = pci_enable_msi(adev->dev);
306 if (r)
307 return r;
308 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800309
310 adev->host_irq = adev->dev->irq;
311 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
312 "kvm_assigned_msi_device", (void *)adev))
313 return -EIO;
314 }
315
Sheng Yang5319c662008-11-24 14:32:57 +0800316 if (!msi2intx)
317 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
318
319 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800320 return 0;
321}
322#endif
323
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800324static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
325 struct kvm_assigned_irq
326 *assigned_irq)
327{
328 int r = 0;
329 struct kvm_assigned_dev_kernel *match;
Sheng Yang17071fe2009-01-06 16:25:11 +0800330 u32 current_flags = 0, changed_flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800331
332 mutex_lock(&kvm->lock);
333
334 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
335 assigned_irq->assigned_dev_id);
336 if (!match) {
337 mutex_unlock(&kvm->lock);
338 return -EINVAL;
339 }
340
Sheng Yang4f906c12008-11-24 14:32:51 +0800341 if (!match->irq_requested_type) {
Sheng Yang342ffb92008-11-24 14:32:49 +0800342 INIT_WORK(&match->interrupt_work,
343 kvm_assigned_dev_interrupt_work_handler);
344 if (irqchip_in_kernel(kvm)) {
345 /* Register ack nofitier */
346 match->ack_notifier.gsi = -1;
347 match->ack_notifier.irq_acked =
348 kvm_assigned_dev_ack_irq;
349 kvm_register_irq_ack_notifier(kvm,
350 &match->ack_notifier);
351
352 /* Request IRQ source ID */
353 r = kvm_request_irq_source_id(kvm);
354 if (r < 0)
355 goto out_release;
356 else
357 match->irq_source_id = r;
Sheng Yang5319c662008-11-24 14:32:57 +0800358
359#ifdef CONFIG_X86
360 /* Determine host device irq type, we can know the
361 * result from dev->msi_enabled */
362 if (msi2intx)
363 pci_enable_msi(match->dev);
364#endif
Sheng Yang342ffb92008-11-24 14:32:49 +0800365 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800366 }
367
Sheng Yang17071fe2009-01-06 16:25:11 +0800368 if ((match->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) &&
369 (match->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI))
370 current_flags |= KVM_DEV_IRQ_ASSIGN_ENABLE_MSI;
371
372 changed_flags = assigned_irq->flags ^ current_flags;
373
374 if ((changed_flags & KVM_DEV_IRQ_ASSIGN_MSI_ACTION) ||
Sheng Yang5319c662008-11-24 14:32:57 +0800375 (msi2intx && match->dev->msi_enabled)) {
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800376#ifdef CONFIG_X86
377 r = assigned_device_update_msi(kvm, match, assigned_irq);
378 if (r) {
379 printk(KERN_WARNING "kvm: failed to enable "
380 "MSI device!\n");
381 goto out_release;
382 }
383#else
384 r = -ENOTTY;
385#endif
386 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
387 /* Host device IRQ 0 means don't support INTx */
Sheng Yang5319c662008-11-24 14:32:57 +0800388 if (!msi2intx) {
389 printk(KERN_WARNING
390 "kvm: wait device to enable MSI!\n");
391 r = 0;
392 } else {
393 printk(KERN_WARNING
394 "kvm: failed to enable MSI device!\n");
395 r = -ENOTTY;
396 goto out_release;
397 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800398 } else {
399 /* Non-sharing INTx mode */
400 r = assigned_device_update_intx(kvm, match, assigned_irq);
401 if (r) {
402 printk(KERN_WARNING "kvm: failed to enable "
403 "INTx device!\n");
404 goto out_release;
405 }
406 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800407
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800408 mutex_unlock(&kvm->lock);
409 return r;
410out_release:
411 mutex_unlock(&kvm->lock);
412 kvm_free_assigned_device(kvm, match);
413 return r;
414}
415
416static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
417 struct kvm_assigned_pci_dev *assigned_dev)
418{
419 int r = 0;
420 struct kvm_assigned_dev_kernel *match;
421 struct pci_dev *dev;
422
Mark McLoughlin682edb42009-02-05 18:23:46 +0000423 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800424 mutex_lock(&kvm->lock);
425
426 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
427 assigned_dev->assigned_dev_id);
428 if (match) {
429 /* device already assigned */
430 r = -EINVAL;
431 goto out;
432 }
433
434 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
435 if (match == NULL) {
436 printk(KERN_INFO "%s: Couldn't allocate memory\n",
437 __func__);
438 r = -ENOMEM;
439 goto out;
440 }
441 dev = pci_get_bus_and_slot(assigned_dev->busnr,
442 assigned_dev->devfn);
443 if (!dev) {
444 printk(KERN_INFO "%s: host device not found\n", __func__);
445 r = -EINVAL;
446 goto out_free;
447 }
448 if (pci_enable_device(dev)) {
449 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
450 r = -EBUSY;
451 goto out_put;
452 }
453 r = pci_request_regions(dev, "kvm_assigned_device");
454 if (r) {
455 printk(KERN_INFO "%s: Could not get access to device regions\n",
456 __func__);
457 goto out_disable;
458 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800459
460 pci_reset_function(dev);
461
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800462 match->assigned_dev_id = assigned_dev->assigned_dev_id;
463 match->host_busnr = assigned_dev->busnr;
464 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800465 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800466 match->dev = dev;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000467 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800468 match->kvm = kvm;
469
470 list_add(&match->list, &kvm->arch.assigned_dev_head);
471
472 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100473 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800474 r = kvm_iommu_map_guest(kvm);
475 if (r)
476 goto out_list_del;
477 }
478 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800479 if (r)
480 goto out_list_del;
481 }
482
483out:
484 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000485 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800486 return r;
487out_list_del:
488 list_del(&match->list);
489 pci_release_regions(dev);
490out_disable:
491 pci_disable_device(dev);
492out_put:
493 pci_dev_put(dev);
494out_free:
495 kfree(match);
496 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000497 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800498 return r;
499}
500#endif
501
Weidong Han0a920352008-12-02 21:24:23 +0800502#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
503static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
504 struct kvm_assigned_pci_dev *assigned_dev)
505{
506 int r = 0;
507 struct kvm_assigned_dev_kernel *match;
508
509 mutex_lock(&kvm->lock);
510
511 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
512 assigned_dev->assigned_dev_id);
513 if (!match) {
514 printk(KERN_INFO "%s: device hasn't been assigned before, "
515 "so cannot be deassigned\n", __func__);
516 r = -EINVAL;
517 goto out;
518 }
519
520 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
521 kvm_deassign_device(kvm, match);
522
523 kvm_free_assigned_device(kvm, match);
524
525out:
526 mutex_unlock(&kvm->lock);
527 return r;
528}
529#endif
530
James Morris5aacf0c2006-12-22 01:04:55 -0800531static inline int valid_vcpu(int n)
532{
533 return likely(n >= 0 && n < KVM_MAX_VCPUS);
534}
535
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800536inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300537{
538 if (pfn_valid(pfn))
539 return PageReserved(pfn_to_page(pfn));
540
541 return true;
542}
543
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544/*
545 * Switches to specified vcpu, until a matching vcpu_put()
546 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200547void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548{
Avi Kivity15ad7142007-07-11 18:17:21 +0300549 int cpu;
550
Avi Kivitybccf2152007-02-21 18:04:26 +0200551 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300552 cpu = get_cpu();
553 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200554 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300555 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200556}
557
Carsten Otte313a3dc2007-10-11 19:16:52 +0200558void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559{
Avi Kivity15ad7142007-07-11 18:17:21 +0300560 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200561 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300562 preempt_notifier_unregister(&vcpu->preempt_notifier);
563 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 mutex_unlock(&vcpu->mutex);
565}
566
Avi Kivityd9e368d2007-06-07 19:18:30 +0300567static void ack_flush(void *_completed)
568{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300569}
570
Rusty Russell49846892008-12-08 20:26:24 +1030571static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300572{
Avi Kivity597a5f52008-07-20 14:24:22 +0300573 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030574 cpumask_var_t cpus;
575 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300576 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300577
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030578 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
579 cpumask_clear(cpus);
580
Avi Kivity597a5f52008-07-20 14:24:22 +0300581 me = get_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000582 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
583 vcpu = kvm->vcpus[i];
584 if (!vcpu)
585 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030586 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300587 continue;
588 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030589 if (cpus != NULL && cpu != -1 && cpu != me)
590 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300591 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030592 if (unlikely(cpus == NULL))
593 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
594 else if (!cpumask_empty(cpus))
595 smp_call_function_many(cpus, ack_flush, NULL, 1);
596 else
597 called = false;
Avi Kivity597a5f52008-07-20 14:24:22 +0300598 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030599 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030600 return called;
601}
602
603void kvm_flush_remote_tlbs(struct kvm *kvm)
604{
605 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
606 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300607}
608
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500609void kvm_reload_remote_mmus(struct kvm *kvm)
610{
Rusty Russell49846892008-12-08 20:26:24 +1030611 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500612}
613
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000614int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
615{
616 struct page *page;
617 int r;
618
619 mutex_init(&vcpu->mutex);
620 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000621 vcpu->kvm = kvm;
622 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300623 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000624
625 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
626 if (!page) {
627 r = -ENOMEM;
628 goto fail;
629 }
630 vcpu->run = page_address(page);
631
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800632 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000633 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800634 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000635 return 0;
636
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000637fail_free_run:
638 free_page((unsigned long)vcpu->run);
639fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000640 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000641}
642EXPORT_SYMBOL_GPL(kvm_vcpu_init);
643
644void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
645{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800646 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000647 free_page((unsigned long)vcpu->run);
648}
649EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
650
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200651#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
652static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
653{
654 return container_of(mn, struct kvm, mmu_notifier);
655}
656
657static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
658 struct mm_struct *mm,
659 unsigned long address)
660{
661 struct kvm *kvm = mmu_notifier_to_kvm(mn);
662 int need_tlb_flush;
663
664 /*
665 * When ->invalidate_page runs, the linux pte has been zapped
666 * already but the page is still allocated until
667 * ->invalidate_page returns. So if we increase the sequence
668 * here the kvm page fault will notice if the spte can't be
669 * established because the page is going to be freed. If
670 * instead the kvm page fault establishes the spte before
671 * ->invalidate_page runs, kvm_unmap_hva will release it
672 * before returning.
673 *
674 * The sequence increase only need to be seen at spin_unlock
675 * time, and not at spin_lock time.
676 *
677 * Increasing the sequence after the spin_unlock would be
678 * unsafe because the kvm page fault could then establish the
679 * pte after kvm_unmap_hva returned, without noticing the page
680 * is going to be freed.
681 */
682 spin_lock(&kvm->mmu_lock);
683 kvm->mmu_notifier_seq++;
684 need_tlb_flush = kvm_unmap_hva(kvm, address);
685 spin_unlock(&kvm->mmu_lock);
686
687 /* we've to flush the tlb before the pages can be freed */
688 if (need_tlb_flush)
689 kvm_flush_remote_tlbs(kvm);
690
691}
692
693static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
694 struct mm_struct *mm,
695 unsigned long start,
696 unsigned long end)
697{
698 struct kvm *kvm = mmu_notifier_to_kvm(mn);
699 int need_tlb_flush = 0;
700
701 spin_lock(&kvm->mmu_lock);
702 /*
703 * The count increase must become visible at unlock time as no
704 * spte can be established without taking the mmu_lock and
705 * count is also read inside the mmu_lock critical section.
706 */
707 kvm->mmu_notifier_count++;
708 for (; start < end; start += PAGE_SIZE)
709 need_tlb_flush |= kvm_unmap_hva(kvm, start);
710 spin_unlock(&kvm->mmu_lock);
711
712 /* we've to flush the tlb before the pages can be freed */
713 if (need_tlb_flush)
714 kvm_flush_remote_tlbs(kvm);
715}
716
717static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
718 struct mm_struct *mm,
719 unsigned long start,
720 unsigned long end)
721{
722 struct kvm *kvm = mmu_notifier_to_kvm(mn);
723
724 spin_lock(&kvm->mmu_lock);
725 /*
726 * This sequence increase will notify the kvm page fault that
727 * the page that is going to be mapped in the spte could have
728 * been freed.
729 */
730 kvm->mmu_notifier_seq++;
731 /*
732 * The above sequence increase must be visible before the
733 * below count decrease but both values are read by the kvm
734 * page fault under mmu_lock spinlock so we don't need to add
735 * a smb_wmb() here in between the two.
736 */
737 kvm->mmu_notifier_count--;
738 spin_unlock(&kvm->mmu_lock);
739
740 BUG_ON(kvm->mmu_notifier_count < 0);
741}
742
743static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
744 struct mm_struct *mm,
745 unsigned long address)
746{
747 struct kvm *kvm = mmu_notifier_to_kvm(mn);
748 int young;
749
750 spin_lock(&kvm->mmu_lock);
751 young = kvm_age_hva(kvm, address);
752 spin_unlock(&kvm->mmu_lock);
753
754 if (young)
755 kvm_flush_remote_tlbs(kvm);
756
757 return young;
758}
759
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100760static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
761 struct mm_struct *mm)
762{
763 struct kvm *kvm = mmu_notifier_to_kvm(mn);
764 kvm_arch_flush_shadow(kvm);
765}
766
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200767static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
768 .invalidate_page = kvm_mmu_notifier_invalidate_page,
769 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
770 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
771 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100772 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200773};
774#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
775
Avi Kivityf17abe92007-02-21 19:28:04 +0200776static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800778 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200779#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
780 struct page *page;
781#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800783 if (IS_ERR(kvm))
784 goto out;
Avi Kivity75858a82009-01-04 17:10:50 +0200785#ifdef CONFIG_HAVE_KVM_IRQCHIP
Avi Kivity399ec802008-11-19 13:58:46 +0200786 INIT_LIST_HEAD(&kvm->irq_routing);
Avi Kivity75858a82009-01-04 17:10:50 +0200787 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
788#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789
Laurent Vivier5f94c172008-05-30 16:05:54 +0200790#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
791 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
792 if (!page) {
793 kfree(kvm);
794 return ERR_PTR(-ENOMEM);
795 }
796 kvm->coalesced_mmio_ring =
797 (struct kvm_coalesced_mmio_ring *)page_address(page);
798#endif
799
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200800#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
801 {
802 int err;
803 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
804 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
805 if (err) {
806#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
807 put_page(page);
808#endif
809 kfree(kvm);
810 return ERR_PTR(err);
811 }
812 }
813#endif
814
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200815 kvm->mm = current->mm;
816 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500817 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300818 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800819 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400820 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200821 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300822 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000823 spin_lock(&kvm_lock);
824 list_add(&kvm->vm_list, &vm_list);
825 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200826#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
827 kvm_coalesced_mmio_init(kvm);
828#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800829out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200830 return kvm;
831}
832
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833/*
834 * Free any memory in @free but not in @dont.
835 */
836static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
837 struct kvm_memory_slot *dont)
838{
Izik Eidus290fc382007-09-27 14:11:22 +0200839 if (!dont || free->rmap != dont->rmap)
840 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
842 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
843 vfree(free->dirty_bitmap);
844
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300845 if (!dont || free->lpage_info != dont->lpage_info)
846 vfree(free->lpage_info);
847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000849 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500850 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300851 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852}
853
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800854void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855{
856 int i;
857
858 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000859 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860}
861
Avi Kivityf17abe92007-02-21 19:28:04 +0200862static void kvm_destroy_vm(struct kvm *kvm)
863{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200864 struct mm_struct *mm = kvm->mm;
865
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800866 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800867 spin_lock(&kvm_lock);
868 list_del(&kvm->vm_list);
869 spin_unlock(&kvm_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200870 kvm_free_irq_routing(kvm);
Eddie Dong74906342007-06-19 18:05:03 +0300871 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400872 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200873#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
874 if (kvm->coalesced_mmio_ring != NULL)
875 free_page((unsigned long)kvm->coalesced_mmio_ring);
876#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200877#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
878 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
879#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800880 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200881 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200882}
883
Izik Eidusd39f13b2008-03-30 16:01:25 +0300884void kvm_get_kvm(struct kvm *kvm)
885{
886 atomic_inc(&kvm->users_count);
887}
888EXPORT_SYMBOL_GPL(kvm_get_kvm);
889
890void kvm_put_kvm(struct kvm *kvm)
891{
892 if (atomic_dec_and_test(&kvm->users_count))
893 kvm_destroy_vm(kvm);
894}
895EXPORT_SYMBOL_GPL(kvm_put_kvm);
896
897
Avi Kivityf17abe92007-02-21 19:28:04 +0200898static int kvm_vm_release(struct inode *inode, struct file *filp)
899{
900 struct kvm *kvm = filp->private_data;
901
Izik Eidusd39f13b2008-03-30 16:01:25 +0300902 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 return 0;
904}
905
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 * Allocate some memory and give it an address in the guest physical address
908 * space.
909 *
910 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800911 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500912 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800914int __kvm_set_memory_region(struct kvm *kvm,
915 struct kvm_userspace_memory_region *mem,
916 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917{
918 int r;
919 gfn_t base_gfn;
920 unsigned long npages;
921 unsigned long i;
922 struct kvm_memory_slot *memslot;
923 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924
925 r = -EINVAL;
926 /* General sanity checks */
927 if (mem->memory_size & (PAGE_SIZE - 1))
928 goto out;
929 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
930 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800931 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600932 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200933 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 goto out;
935 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
936 goto out;
937
938 memslot = &kvm->memslots[mem->slot];
939 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
940 npages = mem->memory_size >> PAGE_SHIFT;
941
942 if (!npages)
943 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
944
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 new = old = *memslot;
946
947 new.base_gfn = base_gfn;
948 new.npages = npages;
949 new.flags = mem->flags;
950
951 /* Disallow changing a memory slot's size. */
952 r = -EINVAL;
953 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800954 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
956 /* Check for overlaps */
957 r = -EEXIST;
958 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
959 struct kvm_memory_slot *s = &kvm->memslots[i];
960
961 if (s == memslot)
962 continue;
963 if (!((base_gfn + npages <= s->base_gfn) ||
964 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800965 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 /* Free page dirty bitmap if unneeded */
969 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000970 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
972 r = -ENOMEM;
973
974 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200975#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500976 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400977 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200978
979 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800980 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200981
Izik Eidus290fc382007-09-27 14:11:22 +0200982 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500983
Izik Eidus80b14b52007-10-25 11:54:04 +0200984 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200985 /*
986 * hva_to_rmmap() serialzies with the mmu_lock and to be
987 * safe it has to ignore memslots with !user_alloc &&
988 * !userspace_addr.
989 */
990 if (user_alloc)
991 new.userspace_addr = mem->userspace_addr;
992 else
993 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300995 if (npages && !new.lpage_info) {
996 int largepages = npages / KVM_PAGES_PER_HPAGE;
997 if (npages % KVM_PAGES_PER_HPAGE)
998 largepages++;
999 if (base_gfn % KVM_PAGES_PER_HPAGE)
1000 largepages++;
1001
1002 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1003
1004 if (!new.lpage_info)
1005 goto out_free;
1006
1007 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1008
1009 if (base_gfn % KVM_PAGES_PER_HPAGE)
1010 new.lpage_info[0].write_count = 1;
1011 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1012 new.lpage_info[largepages-1].write_count = 1;
1013 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
1015 /* Allocate page dirty bitmap if needed */
1016 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1017 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1018
1019 new.dirty_bitmap = vmalloc(dirty_bytes);
1020 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001021 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 memset(new.dirty_bitmap, 0, dirty_bytes);
1023 }
Carsten Otteeff01142008-06-27 15:05:31 +02001024#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001026 if (!npages)
1027 kvm_arch_flush_shadow(kvm);
1028
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001029 spin_lock(&kvm->mmu_lock);
1030 if (mem->slot >= kvm->nmemslots)
1031 kvm->nmemslots = mem->slot + 1;
1032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001034 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
Zhang Xiantao0de10342007-11-20 16:25:04 +08001036 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1037 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001038 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001039 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001040 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001041 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001042 }
1043
Glauber Costa6f897242008-12-03 13:40:51 -02001044 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1045 /* Slot deletion case: we have to update the current slot */
1046 if (!npages)
1047 *memslot = old;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001048#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001049 /* map the pages in iommu page table */
1050 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1051 if (r)
1052 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001053#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 return 0;
1055
Sheng Yangf78e0e22007-10-29 09:40:42 +08001056out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 kvm_free_physmem_slot(&new, &old);
1058out:
1059 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001060
1061}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001062EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1063
1064int kvm_set_memory_region(struct kvm *kvm,
1065 struct kvm_userspace_memory_region *mem,
1066 int user_alloc)
1067{
1068 int r;
1069
Izik Eidus72dc67a2008-02-10 18:04:15 +02001070 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001071 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001072 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001073 return r;
1074}
Izik Eidus210c7c42007-10-24 23:52:57 +02001075EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1076
Carsten Otte1fe779f2007-10-29 16:08:35 +01001077int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1078 struct
1079 kvm_userspace_memory_region *mem,
1080 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001081{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001082 if (mem->slot >= KVM_MEMORY_SLOTS)
1083 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001084 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085}
1086
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001087int kvm_get_dirty_log(struct kvm *kvm,
1088 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089{
1090 struct kvm_memory_slot *memslot;
1091 int r, i;
1092 int n;
1093 unsigned long any = 0;
1094
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 r = -EINVAL;
1096 if (log->slot >= KVM_MEMORY_SLOTS)
1097 goto out;
1098
1099 memslot = &kvm->memslots[log->slot];
1100 r = -ENOENT;
1101 if (!memslot->dirty_bitmap)
1102 goto out;
1103
Uri Lublincd1a4a92007-02-22 16:43:09 +02001104 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105
Uri Lublincd1a4a92007-02-22 16:43:09 +02001106 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 any = memslot->dirty_bitmap[i];
1108
1109 r = -EFAULT;
1110 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1111 goto out;
1112
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001113 if (any)
1114 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115
1116 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 return r;
1119}
1120
Izik Eiduscea7bb22007-10-17 19:17:48 +02001121int is_error_page(struct page *page)
1122{
1123 return page == bad_page;
1124}
1125EXPORT_SYMBOL_GPL(is_error_page);
1126
Anthony Liguori35149e22008-04-02 14:46:56 -05001127int is_error_pfn(pfn_t pfn)
1128{
1129 return pfn == bad_pfn;
1130}
1131EXPORT_SYMBOL_GPL(is_error_pfn);
1132
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001133static inline unsigned long bad_hva(void)
1134{
1135 return PAGE_OFFSET;
1136}
1137
1138int kvm_is_error_hva(unsigned long addr)
1139{
1140 return addr == bad_hva();
1141}
1142EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1143
Izik Eidus28430992008-10-03 17:40:32 +03001144struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145{
1146 int i;
1147
1148 for (i = 0; i < kvm->nmemslots; ++i) {
1149 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1150
1151 if (gfn >= memslot->base_gfn
1152 && gfn < memslot->base_gfn + memslot->npages)
1153 return memslot;
1154 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001155 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156}
Izik Eidus28430992008-10-03 17:40:32 +03001157EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001158
1159struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1160{
1161 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001162 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001163}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
Izik Eiduse0d62c72007-10-24 23:57:46 +02001165int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1166{
1167 int i;
1168
1169 gfn = unalias_gfn(kvm, gfn);
1170 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1171 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1172
1173 if (gfn >= memslot->base_gfn
1174 && gfn < memslot->base_gfn + memslot->npages)
1175 return 1;
1176 }
1177 return 0;
1178}
1179EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1180
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001181unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001182{
1183 struct kvm_memory_slot *slot;
1184
1185 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001186 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001187 if (!slot)
1188 return bad_hva();
1189 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1190}
Sheng Yang0d150292008-04-25 21:44:50 +08001191EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001192
Anthony Liguori35149e22008-04-02 14:46:56 -05001193pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001194{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001195 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001196 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001197 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001198 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001199
Avi Kivity60395222007-10-21 11:03:36 +02001200 might_sleep();
1201
Izik Eidus539cb662007-11-11 22:05:04 +02001202 addr = gfn_to_hva(kvm, gfn);
1203 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001204 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001205 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001206 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001207
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001208 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001209
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001210 if (unlikely(npages != 1)) {
1211 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001212
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001213 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001214 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001215
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001216 if (vma == NULL || addr < vma->vm_start ||
1217 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001218 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001219 get_page(bad_page);
1220 return page_to_pfn(bad_page);
1221 }
1222
1223 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001224 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001225 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001226 } else
1227 pfn = page_to_pfn(page[0]);
1228
1229 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001230}
1231
1232EXPORT_SYMBOL_GPL(gfn_to_pfn);
1233
1234struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1235{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001236 pfn_t pfn;
1237
1238 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001239 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001240 return pfn_to_page(pfn);
1241
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001242 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001243
1244 get_page(bad_page);
1245 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001246}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001247
Avi Kivity954bbbc22007-03-30 14:02:32 +03001248EXPORT_SYMBOL_GPL(gfn_to_page);
1249
Izik Eidusb4231d62007-11-20 11:49:33 +02001250void kvm_release_page_clean(struct page *page)
1251{
Anthony Liguori35149e22008-04-02 14:46:56 -05001252 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001253}
1254EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1255
Anthony Liguori35149e22008-04-02 14:46:56 -05001256void kvm_release_pfn_clean(pfn_t pfn)
1257{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001258 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001259 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001260}
1261EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1262
Izik Eidusb4231d62007-11-20 11:49:33 +02001263void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001264{
Anthony Liguori35149e22008-04-02 14:46:56 -05001265 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001266}
Izik Eidusb4231d62007-11-20 11:49:33 +02001267EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001268
Anthony Liguori35149e22008-04-02 14:46:56 -05001269void kvm_release_pfn_dirty(pfn_t pfn)
1270{
1271 kvm_set_pfn_dirty(pfn);
1272 kvm_release_pfn_clean(pfn);
1273}
1274EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1275
1276void kvm_set_page_dirty(struct page *page)
1277{
1278 kvm_set_pfn_dirty(page_to_pfn(page));
1279}
1280EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1281
1282void kvm_set_pfn_dirty(pfn_t pfn)
1283{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001284 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001285 struct page *page = pfn_to_page(pfn);
1286 if (!PageReserved(page))
1287 SetPageDirty(page);
1288 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001289}
1290EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1291
1292void kvm_set_pfn_accessed(pfn_t pfn)
1293{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001294 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001295 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001296}
1297EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1298
1299void kvm_get_pfn(pfn_t pfn)
1300{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001301 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001302 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001303}
1304EXPORT_SYMBOL_GPL(kvm_get_pfn);
1305
Izik Eidus195aefd2007-10-01 22:14:18 +02001306static int next_segment(unsigned long len, int offset)
1307{
1308 if (len > PAGE_SIZE - offset)
1309 return PAGE_SIZE - offset;
1310 else
1311 return len;
1312}
1313
1314int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1315 int len)
1316{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001317 int r;
1318 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001319
Izik Eiduse0506bc2007-11-11 22:10:22 +02001320 addr = gfn_to_hva(kvm, gfn);
1321 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001322 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001323 r = copy_from_user(data, (void __user *)addr + offset, len);
1324 if (r)
1325 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001326 return 0;
1327}
1328EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1329
1330int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1331{
1332 gfn_t gfn = gpa >> PAGE_SHIFT;
1333 int seg;
1334 int offset = offset_in_page(gpa);
1335 int ret;
1336
1337 while ((seg = next_segment(len, offset)) != 0) {
1338 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1339 if (ret < 0)
1340 return ret;
1341 offset = 0;
1342 len -= seg;
1343 data += seg;
1344 ++gfn;
1345 }
1346 return 0;
1347}
1348EXPORT_SYMBOL_GPL(kvm_read_guest);
1349
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001350int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1351 unsigned long len)
1352{
1353 int r;
1354 unsigned long addr;
1355 gfn_t gfn = gpa >> PAGE_SHIFT;
1356 int offset = offset_in_page(gpa);
1357
1358 addr = gfn_to_hva(kvm, gfn);
1359 if (kvm_is_error_hva(addr))
1360 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001361 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001362 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001363 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001364 if (r)
1365 return -EFAULT;
1366 return 0;
1367}
1368EXPORT_SYMBOL(kvm_read_guest_atomic);
1369
Izik Eidus195aefd2007-10-01 22:14:18 +02001370int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1371 int offset, int len)
1372{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001373 int r;
1374 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001375
Izik Eiduse0506bc2007-11-11 22:10:22 +02001376 addr = gfn_to_hva(kvm, gfn);
1377 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001378 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001379 r = copy_to_user((void __user *)addr + offset, data, len);
1380 if (r)
1381 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001382 mark_page_dirty(kvm, gfn);
1383 return 0;
1384}
1385EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1386
1387int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1388 unsigned long len)
1389{
1390 gfn_t gfn = gpa >> PAGE_SHIFT;
1391 int seg;
1392 int offset = offset_in_page(gpa);
1393 int ret;
1394
1395 while ((seg = next_segment(len, offset)) != 0) {
1396 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1397 if (ret < 0)
1398 return ret;
1399 offset = 0;
1400 len -= seg;
1401 data += seg;
1402 ++gfn;
1403 }
1404 return 0;
1405}
1406
1407int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1408{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001409 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001410}
1411EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1412
1413int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1414{
1415 gfn_t gfn = gpa >> PAGE_SHIFT;
1416 int seg;
1417 int offset = offset_in_page(gpa);
1418 int ret;
1419
1420 while ((seg = next_segment(len, offset)) != 0) {
1421 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1422 if (ret < 0)
1423 return ret;
1424 offset = 0;
1425 len -= seg;
1426 ++gfn;
1427 }
1428 return 0;
1429}
1430EXPORT_SYMBOL_GPL(kvm_clear_guest);
1431
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1433{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001434 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435
Uri Lublin3b6fff12007-10-30 10:42:09 +02001436 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001437 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001438 if (memslot && memslot->dirty_bitmap) {
1439 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Rusty Russell7e9d6192007-07-31 20:41:14 +10001441 /* avoid RMW */
1442 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1443 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 }
1445}
1446
Eddie Dongb6958ce2007-07-18 12:15:21 +03001447/*
1448 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1449 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001450void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001451{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001452 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001453
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001454 for (;;) {
1455 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001456
Marcelo Tosattid7690172008-09-08 15:23:48 -03001457 if (kvm_cpu_has_interrupt(vcpu) ||
1458 kvm_cpu_has_pending_timer(vcpu) ||
1459 kvm_arch_vcpu_runnable(vcpu)) {
1460 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001461 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001462 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001463 if (signal_pending(current))
1464 break;
1465
Eddie Dongb6958ce2007-07-18 12:15:21 +03001466 vcpu_put(vcpu);
1467 schedule();
1468 vcpu_load(vcpu);
1469 }
1470
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001471 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001472}
1473
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474void kvm_resched(struct kvm_vcpu *vcpu)
1475{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001476 if (!need_resched())
1477 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479}
1480EXPORT_SYMBOL_GPL(kvm_resched);
1481
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001482static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001483{
1484 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001485 struct page *page;
1486
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001487 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001488 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001489#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001490 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001491 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001492#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001493#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1494 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1495 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1496#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001497 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001498 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001499 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001500 vmf->page = page;
1501 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001502}
1503
1504static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001505 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001506};
1507
1508static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1509{
1510 vma->vm_ops = &kvm_vcpu_vm_ops;
1511 return 0;
1512}
1513
Avi Kivitybccf2152007-02-21 18:04:26 +02001514static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1515{
1516 struct kvm_vcpu *vcpu = filp->private_data;
1517
Al Viro66c0b392008-04-19 20:33:56 +01001518 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001519 return 0;
1520}
1521
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001522static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001523 .release = kvm_vcpu_release,
1524 .unlocked_ioctl = kvm_vcpu_ioctl,
1525 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001526 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001527};
1528
1529/*
1530 * Allocates an inode for the vcpu.
1531 */
1532static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1533{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001534 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001535 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001536 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001537 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001538}
1539
Avi Kivityc5ea7662007-02-20 18:41:05 +02001540/*
1541 * Creates some virtual cpus. Good luck creating more than one.
1542 */
1543static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1544{
1545 int r;
1546 struct kvm_vcpu *vcpu;
1547
Avi Kivityc5ea7662007-02-20 18:41:05 +02001548 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001549 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001550
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001551 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001552 if (IS_ERR(vcpu))
1553 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001554
Avi Kivity15ad7142007-07-11 18:17:21 +03001555 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1556
Avi Kivity26e52152007-11-20 15:30:24 +02001557 r = kvm_arch_vcpu_setup(vcpu);
1558 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001559 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001560
Shaohua Li11ec2802007-07-23 14:51:37 +08001561 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001562 if (kvm->vcpus[n]) {
1563 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001564 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001565 }
1566 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001567 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001568
1569 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001570 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001571 r = create_vcpu_fd(vcpu);
1572 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001573 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001574 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001575
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001576unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001577 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001578 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001579vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001580 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001581 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001582 return r;
1583}
1584
Avi Kivity1961d272007-03-05 19:46:05 +02001585static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1586{
1587 if (sigset) {
1588 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1589 vcpu->sigset_active = 1;
1590 vcpu->sigset = *sigset;
1591 } else
1592 vcpu->sigset_active = 0;
1593 return 0;
1594}
1595
Avi Kivitybccf2152007-02-21 18:04:26 +02001596static long kvm_vcpu_ioctl(struct file *filp,
1597 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598{
Avi Kivitybccf2152007-02-21 18:04:26 +02001599 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001600 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001601 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001602 struct kvm_fpu *fpu = NULL;
1603 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001605 if (vcpu->kvm->mm != current->mm)
1606 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001608 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001609 r = -EINVAL;
1610 if (arg)
1611 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001612 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001615 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001617 r = -ENOMEM;
1618 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1619 if (!kvm_regs)
1620 goto out;
1621 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001623 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001625 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1626 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001628out_free1:
1629 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 break;
1631 }
1632 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001633 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001635 r = -ENOMEM;
1636 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1637 if (!kvm_regs)
1638 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001640 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1641 goto out_free2;
1642 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001644 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001646out_free2:
1647 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 break;
1649 }
1650 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001651 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1652 r = -ENOMEM;
1653 if (!kvm_sregs)
1654 goto out;
1655 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 if (r)
1657 goto out;
1658 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001659 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 goto out;
1661 r = 0;
1662 break;
1663 }
1664 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001665 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1666 r = -ENOMEM;
1667 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001669 r = -EFAULT;
1670 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1671 goto out;
1672 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 if (r)
1674 goto out;
1675 r = 0;
1676 break;
1677 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001678 case KVM_GET_MP_STATE: {
1679 struct kvm_mp_state mp_state;
1680
1681 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1682 if (r)
1683 goto out;
1684 r = -EFAULT;
1685 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1686 goto out;
1687 r = 0;
1688 break;
1689 }
1690 case KVM_SET_MP_STATE: {
1691 struct kvm_mp_state mp_state;
1692
1693 r = -EFAULT;
1694 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1695 goto out;
1696 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1697 if (r)
1698 goto out;
1699 r = 0;
1700 break;
1701 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702 case KVM_TRANSLATE: {
1703 struct kvm_translation tr;
1704
1705 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001706 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001708 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 if (r)
1710 goto out;
1711 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001712 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 goto out;
1714 r = 0;
1715 break;
1716 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001717 case KVM_SET_GUEST_DEBUG: {
1718 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719
1720 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001721 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001723 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 if (r)
1725 goto out;
1726 r = 0;
1727 break;
1728 }
Avi Kivity1961d272007-03-05 19:46:05 +02001729 case KVM_SET_SIGNAL_MASK: {
1730 struct kvm_signal_mask __user *sigmask_arg = argp;
1731 struct kvm_signal_mask kvm_sigmask;
1732 sigset_t sigset, *p;
1733
1734 p = NULL;
1735 if (argp) {
1736 r = -EFAULT;
1737 if (copy_from_user(&kvm_sigmask, argp,
1738 sizeof kvm_sigmask))
1739 goto out;
1740 r = -EINVAL;
1741 if (kvm_sigmask.len != sizeof sigset)
1742 goto out;
1743 r = -EFAULT;
1744 if (copy_from_user(&sigset, sigmask_arg->sigset,
1745 sizeof sigset))
1746 goto out;
1747 p = &sigset;
1748 }
1749 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1750 break;
1751 }
Avi Kivityb8836732007-04-01 16:34:31 +03001752 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001753 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1754 r = -ENOMEM;
1755 if (!fpu)
1756 goto out;
1757 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001758 if (r)
1759 goto out;
1760 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001761 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001762 goto out;
1763 r = 0;
1764 break;
1765 }
1766 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001767 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1768 r = -ENOMEM;
1769 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001770 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001771 r = -EFAULT;
1772 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1773 goto out;
1774 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001775 if (r)
1776 goto out;
1777 r = 0;
1778 break;
1779 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001780 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001781 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001782 }
1783out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001784 kfree(fpu);
1785 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001786 return r;
1787}
1788
1789static long kvm_vm_ioctl(struct file *filp,
1790 unsigned int ioctl, unsigned long arg)
1791{
1792 struct kvm *kvm = filp->private_data;
1793 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001794 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001795
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001796 if (kvm->mm != current->mm)
1797 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001798 switch (ioctl) {
1799 case KVM_CREATE_VCPU:
1800 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1801 if (r < 0)
1802 goto out;
1803 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001804 case KVM_SET_USER_MEMORY_REGION: {
1805 struct kvm_userspace_memory_region kvm_userspace_mem;
1806
1807 r = -EFAULT;
1808 if (copy_from_user(&kvm_userspace_mem, argp,
1809 sizeof kvm_userspace_mem))
1810 goto out;
1811
1812 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 if (r)
1814 goto out;
1815 break;
1816 }
1817 case KVM_GET_DIRTY_LOG: {
1818 struct kvm_dirty_log log;
1819
1820 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001821 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001823 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 if (r)
1825 goto out;
1826 break;
1827 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001828#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1829 case KVM_REGISTER_COALESCED_MMIO: {
1830 struct kvm_coalesced_mmio_zone zone;
1831 r = -EFAULT;
1832 if (copy_from_user(&zone, argp, sizeof zone))
1833 goto out;
1834 r = -ENXIO;
1835 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1836 if (r)
1837 goto out;
1838 r = 0;
1839 break;
1840 }
1841 case KVM_UNREGISTER_COALESCED_MMIO: {
1842 struct kvm_coalesced_mmio_zone zone;
1843 r = -EFAULT;
1844 if (copy_from_user(&zone, argp, sizeof zone))
1845 goto out;
1846 r = -ENXIO;
1847 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1848 if (r)
1849 goto out;
1850 r = 0;
1851 break;
1852 }
1853#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001854#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1855 case KVM_ASSIGN_PCI_DEVICE: {
1856 struct kvm_assigned_pci_dev assigned_dev;
1857
1858 r = -EFAULT;
1859 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1860 goto out;
1861 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1862 if (r)
1863 goto out;
1864 break;
1865 }
1866 case KVM_ASSIGN_IRQ: {
1867 struct kvm_assigned_irq assigned_irq;
1868
1869 r = -EFAULT;
1870 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1871 goto out;
1872 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1873 if (r)
1874 goto out;
1875 break;
1876 }
1877#endif
Weidong Han0a920352008-12-02 21:24:23 +08001878#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1879 case KVM_DEASSIGN_PCI_DEVICE: {
1880 struct kvm_assigned_pci_dev assigned_dev;
1881
1882 r = -EFAULT;
1883 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1884 goto out;
1885 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1886 if (r)
1887 goto out;
1888 break;
1889 }
1890#endif
Avi Kivity399ec802008-11-19 13:58:46 +02001891#ifdef KVM_CAP_IRQ_ROUTING
1892 case KVM_SET_GSI_ROUTING: {
1893 struct kvm_irq_routing routing;
1894 struct kvm_irq_routing __user *urouting;
1895 struct kvm_irq_routing_entry *entries;
1896
1897 r = -EFAULT;
1898 if (copy_from_user(&routing, argp, sizeof(routing)))
1899 goto out;
1900 r = -EINVAL;
1901 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
1902 goto out;
1903 if (routing.flags)
1904 goto out;
1905 r = -ENOMEM;
1906 entries = vmalloc(routing.nr * sizeof(*entries));
1907 if (!entries)
1908 goto out;
1909 r = -EFAULT;
1910 urouting = argp;
1911 if (copy_from_user(entries, urouting->entries,
1912 routing.nr * sizeof(*entries)))
1913 goto out_free_irq_routing;
1914 r = kvm_set_irq_routing(kvm, entries, routing.nr,
1915 routing.flags);
1916 out_free_irq_routing:
1917 vfree(entries);
1918 break;
1919 }
1920#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001921 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001922 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001923 }
1924out:
1925 return r;
1926}
1927
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001928static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001929{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001930 struct page *page[1];
1931 unsigned long addr;
1932 int npages;
1933 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001934 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001935
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001936 addr = gfn_to_hva(kvm, gfn);
1937 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001938 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001939
1940 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1941 NULL);
1942 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001943 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001944
1945 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001946 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001947}
1948
1949static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001950 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001951};
1952
1953static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1954{
1955 vma->vm_ops = &kvm_vm_vm_ops;
1956 return 0;
1957}
1958
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001959static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001960 .release = kvm_vm_release,
1961 .unlocked_ioctl = kvm_vm_ioctl,
1962 .compat_ioctl = kvm_vm_ioctl,
1963 .mmap = kvm_vm_mmap,
1964};
1965
1966static int kvm_dev_ioctl_create_vm(void)
1967{
Al Viro2030a422008-02-23 06:46:49 -05001968 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001969 struct kvm *kvm;
1970
Avi Kivityf17abe92007-02-21 19:28:04 +02001971 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001972 if (IS_ERR(kvm))
1973 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001974 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001975 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001976 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001977
Avi Kivityf17abe92007-02-21 19:28:04 +02001978 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001979}
1980
Avi Kivity1a811b62008-12-08 18:25:27 +02001981static long kvm_dev_ioctl_check_extension_generic(long arg)
1982{
1983 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001984 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001985 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1986 return 1;
Avi Kivity399ec802008-11-19 13:58:46 +02001987#ifdef CONFIG_HAVE_KVM_IRQCHIP
1988 case KVM_CAP_IRQ_ROUTING:
1989 return 1;
1990#endif
Avi Kivity1a811b62008-12-08 18:25:27 +02001991 default:
1992 break;
1993 }
1994 return kvm_dev_ioctl_check_extension(arg);
1995}
1996
Avi Kivityf17abe92007-02-21 19:28:04 +02001997static long kvm_dev_ioctl(struct file *filp,
1998 unsigned int ioctl, unsigned long arg)
1999{
Avi Kivity07c45a32007-03-07 13:05:38 +02002000 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002001
2002 switch (ioctl) {
2003 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002004 r = -EINVAL;
2005 if (arg)
2006 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002007 r = KVM_API_VERSION;
2008 break;
2009 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002010 r = -EINVAL;
2011 if (arg)
2012 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002013 r = kvm_dev_ioctl_create_vm();
2014 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002015 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002016 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002017 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002018 case KVM_GET_VCPU_MMAP_SIZE:
2019 r = -EINVAL;
2020 if (arg)
2021 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002022 r = PAGE_SIZE; /* struct kvm_run */
2023#ifdef CONFIG_X86
2024 r += PAGE_SIZE; /* pio data page */
2025#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002026#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2027 r += PAGE_SIZE; /* coalesced mmio ring page */
2028#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002029 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002030 case KVM_TRACE_ENABLE:
2031 case KVM_TRACE_PAUSE:
2032 case KVM_TRACE_DISABLE:
2033 r = kvm_trace_ioctl(ioctl, arg);
2034 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002036 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 }
2038out:
2039 return r;
2040}
2041
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043 .unlocked_ioctl = kvm_dev_ioctl,
2044 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045};
2046
2047static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002048 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049 "kvm",
2050 &kvm_chardev_ops,
2051};
2052
Avi Kivity1b6c0162007-05-24 13:03:52 +03002053static void hardware_enable(void *junk)
2054{
2055 int cpu = raw_smp_processor_id();
2056
Rusty Russell7f59f492008-12-07 21:25:45 +10302057 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002058 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302059 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002060 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002061}
2062
2063static void hardware_disable(void *junk)
2064{
2065 int cpu = raw_smp_processor_id();
2066
Rusty Russell7f59f492008-12-07 21:25:45 +10302067 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002068 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302069 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002070 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002071}
2072
Avi Kivity774c47f2007-02-12 00:54:47 -08002073static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2074 void *v)
2075{
2076 int cpu = (long)v;
2077
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002078 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002079 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002080 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002081 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2082 cpu);
2083 hardware_disable(NULL);
2084 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002085 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002086 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2087 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002088 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002089 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002090 case CPU_ONLINE:
2091 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2092 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002093 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002094 break;
2095 }
2096 return NOTIFY_OK;
2097}
2098
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002099
2100asmlinkage void kvm_handle_fault_on_reboot(void)
2101{
2102 if (kvm_rebooting)
2103 /* spin while reset goes on */
2104 while (true)
2105 ;
2106 /* Fault while not rebooting. We want the trace. */
2107 BUG();
2108}
2109EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2110
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002111static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002112 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002113{
2114 if (val == SYS_RESTART) {
2115 /*
2116 * Some (well, at least mine) BIOSes hang on reboot if
2117 * in vmx root mode.
2118 */
2119 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002120 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002121 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002122 }
2123 return NOTIFY_OK;
2124}
2125
2126static struct notifier_block kvm_reboot_notifier = {
2127 .notifier_call = kvm_reboot,
2128 .priority = 0,
2129};
2130
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002131void kvm_io_bus_init(struct kvm_io_bus *bus)
2132{
2133 memset(bus, 0, sizeof(*bus));
2134}
2135
2136void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2137{
2138 int i;
2139
2140 for (i = 0; i < bus->dev_count; i++) {
2141 struct kvm_io_device *pos = bus->devs[i];
2142
2143 kvm_iodevice_destructor(pos);
2144 }
2145}
2146
Laurent Vivier92760492008-05-30 16:05:53 +02002147struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2148 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002149{
2150 int i;
2151
2152 for (i = 0; i < bus->dev_count; i++) {
2153 struct kvm_io_device *pos = bus->devs[i];
2154
Laurent Vivier92760492008-05-30 16:05:53 +02002155 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002156 return pos;
2157 }
2158
2159 return NULL;
2160}
2161
2162void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2163{
2164 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2165
2166 bus->devs[bus->dev_count++] = dev;
2167}
2168
Avi Kivity774c47f2007-02-12 00:54:47 -08002169static struct notifier_block kvm_cpu_notifier = {
2170 .notifier_call = kvm_cpu_hotplug,
2171 .priority = 20, /* must be > scheduler priority */
2172};
2173
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002174static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002175{
2176 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002177 struct kvm *kvm;
2178
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002179 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002180 spin_lock(&kvm_lock);
2181 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002182 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002183 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002184 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002185}
2186
2187DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2188
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002189static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002190{
2191 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002192 struct kvm *kvm;
2193 struct kvm_vcpu *vcpu;
2194 int i;
2195
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002196 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002197 spin_lock(&kvm_lock);
2198 list_for_each_entry(kvm, &vm_list, vm_list)
2199 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002200 vcpu = kvm->vcpus[i];
2201 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002202 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002203 }
2204 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002205 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002206}
2207
Avi Kivityba1389b2007-11-18 16:24:12 +02002208DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2209
2210static struct file_operations *stat_fops[] = {
2211 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2212 [KVM_STAT_VM] = &vm_stat_fops,
2213};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002214
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002215static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216{
2217 struct kvm_stats_debugfs_item *p;
2218
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002219 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002221 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002222 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002223 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224}
2225
2226static void kvm_exit_debug(void)
2227{
2228 struct kvm_stats_debugfs_item *p;
2229
2230 for (p = debugfs_entries; p->name; ++p)
2231 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002232 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233}
2234
Avi Kivity59ae6c62007-02-12 00:54:48 -08002235static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2236{
Avi Kivity4267c412007-05-24 13:09:41 +03002237 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002238 return 0;
2239}
2240
2241static int kvm_resume(struct sys_device *dev)
2242{
Avi Kivity4267c412007-05-24 13:09:41 +03002243 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002244 return 0;
2245}
2246
2247static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002248 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002249 .suspend = kvm_suspend,
2250 .resume = kvm_resume,
2251};
2252
2253static struct sys_device kvm_sysdev = {
2254 .id = 0,
2255 .cls = &kvm_sysdev_class,
2256};
2257
Izik Eiduscea7bb22007-10-17 19:17:48 +02002258struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002259pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260
Avi Kivity15ad7142007-07-11 18:17:21 +03002261static inline
2262struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2263{
2264 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2265}
2266
2267static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2268{
2269 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2270
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002271 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002272}
2273
2274static void kvm_sched_out(struct preempt_notifier *pn,
2275 struct task_struct *next)
2276{
2277 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2278
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002279 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002280}
2281
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002282int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002283 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284{
2285 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002286 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002288 kvm_init_debug();
2289
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002290 r = kvm_arch_init(opaque);
2291 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002292 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002293
2294 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2295
2296 if (bad_page == NULL) {
2297 r = -ENOMEM;
2298 goto out;
2299 }
2300
Anthony Liguori35149e22008-04-02 14:46:56 -05002301 bad_pfn = page_to_pfn(bad_page);
2302
Rusty Russell7f59f492008-12-07 21:25:45 +10302303 if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2304 r = -ENOMEM;
2305 goto out_free_0;
2306 }
2307
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002308 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302310 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311
Yang, Sheng002c7f72007-07-31 14:23:01 +03002312 for_each_online_cpu(cpu) {
2313 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002314 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002315 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002316 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002317 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002318 }
2319
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002320 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002321 r = register_cpu_notifier(&kvm_cpu_notifier);
2322 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002323 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 register_reboot_notifier(&kvm_reboot_notifier);
2325
Avi Kivity59ae6c62007-02-12 00:54:48 -08002326 r = sysdev_class_register(&kvm_sysdev_class);
2327 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002328 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002329
2330 r = sysdev_register(&kvm_sysdev);
2331 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002332 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002333
Rusty Russellc16f8622007-07-30 21:12:19 +10002334 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2335 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002336 __alignof__(struct kvm_vcpu),
2337 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002338 if (!kvm_vcpu_cache) {
2339 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002340 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002341 }
2342
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002344 kvm_vm_fops.owner = module;
2345 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
2347 r = misc_register(&kvm_dev);
2348 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002349 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 goto out_free;
2351 }
2352
Avi Kivity15ad7142007-07-11 18:17:21 +03002353 kvm_preempt_ops.sched_in = kvm_sched_in;
2354 kvm_preempt_ops.sched_out = kvm_sched_out;
Sheng Yang5319c662008-11-24 14:32:57 +08002355#ifndef CONFIG_X86
2356 msi2intx = 0;
2357#endif
Avi Kivity15ad7142007-07-11 18:17:21 +03002358
Avi Kivityc7addb92007-09-16 18:58:32 +02002359 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360
2361out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002362 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002363out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002364 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002365out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002366 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002367out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002369 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002370out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002371 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002372out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002373 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302374out_free_0a:
2375 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002376out_free_0:
2377 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002378out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002379 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002380 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002381out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 return r;
2383}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002384EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002386void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002388 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002390 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002391 sysdev_unregister(&kvm_sysdev);
2392 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002394 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002395 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002396 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002397 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302399 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002400 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002402EXPORT_SYMBOL_GPL(kvm_exit);