blob: 29a667ce35b0ebc1840f1fcbb6ae3cf3313fa3b8 [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
Sheng Yangf64769e2008-11-24 14:32:55 +080050#ifdef CONFIG_X86
51#include <asm/msidef.h>
52#endif
53
Laurent Vivier5f94c172008-05-30 16:05:54 +020054#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
55#include "coalesced_mmio.h"
56#endif
57
Xiantao Zhang8a98f662008-10-06 13:47:38 +080058#ifdef KVM_CAP_DEVICE_ASSIGNMENT
59#include <linux/pci.h>
60#include <linux/interrupt.h>
61#include "irq.h"
62#endif
63
Avi Kivity6aa8b732006-12-10 02:21:36 -080064MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
Sheng Yang5319c662008-11-24 14:32:57 +080067static int msi2intx = 1;
68module_param(msi2intx, bool, 0);
69
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080070DEFINE_SPINLOCK(kvm_lock);
71LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080072
Rusty Russell7f59f492008-12-07 21:25:45 +103073static cpumask_var_t cpus_hardware_enabled;
Avi Kivity1b6c0162007-05-24 13:03:52 +030074
Rusty Russellc16f8622007-07-30 21:12:19 +100075struct kmem_cache *kvm_vcpu_cache;
76EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030077
Avi Kivity15ad7142007-07-11 18:17:21 +030078static __read_mostly struct preempt_ops kvm_preempt_ops;
79
Hollis Blanchard76f7c872008-04-15 16:05:42 -050080struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
Avi Kivitybccf2152007-02-21 18:04:26 +020082static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
83 unsigned long arg);
84
Hannes Edere8ba5d32008-11-28 17:02:42 +010085static bool kvm_rebooting;
Avi Kivity4ecac3f2008-05-13 13:23:38 +030086
Xiantao Zhang8a98f662008-10-06 13:47:38 +080087#ifdef KVM_CAP_DEVICE_ASSIGNMENT
Sheng Yangf64769e2008-11-24 14:32:55 +080088
89#ifdef CONFIG_X86
90static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
91{
92 int vcpu_id;
93 struct kvm_vcpu *vcpu;
94 struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
95 int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
96 >> MSI_ADDR_DEST_ID_SHIFT;
97 int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
98 >> MSI_DATA_VECTOR_SHIFT;
99 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
100 (unsigned long *)&dev->guest_msi.address_lo);
101 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
102 (unsigned long *)&dev->guest_msi.data);
103 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
104 (unsigned long *)&dev->guest_msi.data);
105 u32 deliver_bitmask;
106
107 BUG_ON(!ioapic);
108
109 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
110 dest_id, dest_mode);
111 /* IOAPIC delivery mode value is the same as MSI here */
112 switch (delivery_mode) {
113 case IOAPIC_LOWEST_PRIORITY:
114 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
115 deliver_bitmask);
116 if (vcpu != NULL)
117 kvm_apic_set_irq(vcpu, vector, trig_mode);
118 else
119 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
120 break;
121 case IOAPIC_FIXED:
122 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
123 if (!(deliver_bitmask & (1 << vcpu_id)))
124 continue;
125 deliver_bitmask &= ~(1 << vcpu_id);
126 vcpu = ioapic->kvm->vcpus[vcpu_id];
127 if (vcpu)
128 kvm_apic_set_irq(vcpu, vector, trig_mode);
129 }
130 break;
131 default:
132 printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
133 }
134}
135#else
136static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
137#endif
138
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800139static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
140 int assigned_dev_id)
141{
142 struct list_head *ptr;
143 struct kvm_assigned_dev_kernel *match;
144
145 list_for_each(ptr, head) {
146 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
147 if (match->assigned_dev_id == assigned_dev_id)
148 return match;
149 }
150 return NULL;
151}
152
153static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
154{
155 struct kvm_assigned_dev_kernel *assigned_dev;
156
157 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
158 interrupt_work);
159
160 /* This is taken to safely inject irq inside the guest. When
161 * the interrupt injection (or the ioapic code) uses a
162 * finer-grained lock, update this
163 */
164 mutex_lock(&assigned_dev->kvm->lock);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800165 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX)
166 kvm_set_irq(assigned_dev->kvm,
167 assigned_dev->irq_source_id,
168 assigned_dev->guest_irq, 1);
169 else if (assigned_dev->irq_requested_type &
170 KVM_ASSIGNED_DEV_GUEST_MSI) {
171 assigned_device_msi_dispatch(assigned_dev);
172 enable_irq(assigned_dev->host_irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000173 assigned_dev->host_irq_disabled = false;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800174 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800175 mutex_unlock(&assigned_dev->kvm->lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800176}
177
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800178static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
179{
180 struct kvm_assigned_dev_kernel *assigned_dev =
181 (struct kvm_assigned_dev_kernel *) dev_id;
182
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800183 schedule_work(&assigned_dev->interrupt_work);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000184
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800185 disable_irq_nosync(irq);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000186 assigned_dev->host_irq_disabled = true;
187
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800188 return IRQ_HANDLED;
189}
190
191/* Ack the irq line for an assigned device */
192static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
193{
194 struct kvm_assigned_dev_kernel *dev;
195
196 if (kian->gsi == -1)
197 return;
198
199 dev = container_of(kian, struct kvm_assigned_dev_kernel,
200 ack_notifier);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000201
Sheng Yang5550af42008-10-15 20:15:06 +0800202 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Mark McLoughlindefaf152008-12-02 12:16:33 +0000203
204 /* The guest irq may be shared so this ack may be
205 * from another device.
206 */
207 if (dev->host_irq_disabled) {
208 enable_irq(dev->host_irq);
209 dev->host_irq_disabled = false;
210 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800211}
212
Sheng Yangba4cef32009-01-06 10:03:03 +0800213/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000214static void kvm_free_assigned_irq(struct kvm *kvm,
215 struct kvm_assigned_dev_kernel *assigned_dev)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800216{
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000217 if (!irqchip_in_kernel(kvm))
218 return;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800219
Sheng Yange19e30e2008-10-20 16:07:10 +0800220 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000221
222 if (assigned_dev->irq_source_id != -1)
223 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
224 assigned_dev->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800225
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000226 if (!assigned_dev->irq_requested_type)
227 return;
228
Sheng Yangba4cef32009-01-06 10:03:03 +0800229 /*
230 * In kvm_free_device_irq, cancel_work_sync return true if:
231 * 1. work is scheduled, and then cancelled.
232 * 2. work callback is executed.
233 *
234 * The first one ensured that the irq is disabled and no more events
235 * would happen. But for the second one, the irq may be enabled (e.g.
236 * for MSI). So we disable irq here to prevent further events.
237 *
238 * Notice this maybe result in nested disable if the interrupt type is
239 * INTx, but it's OK for we are going to free it.
240 *
241 * If this function is a part of VM destroy, please ensure that till
242 * now, the kvm state is still legal for probably we also have to wait
243 * interrupt_work done.
244 */
245 disable_irq_nosync(assigned_dev->host_irq);
246 cancel_work_sync(&assigned_dev->interrupt_work);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800247
Mark McLoughlin4a643be2008-12-01 13:57:49 +0000248 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
249
250 if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
251 pci_disable_msi(assigned_dev->dev);
252
253 assigned_dev->irq_requested_type = 0;
254}
255
256
257static void kvm_free_assigned_device(struct kvm *kvm,
258 struct kvm_assigned_dev_kernel
259 *assigned_dev)
260{
261 kvm_free_assigned_irq(kvm, assigned_dev);
262
Sheng Yang6eb55812008-10-31 12:37:41 +0800263 pci_reset_function(assigned_dev->dev);
264
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800265 pci_release_regions(assigned_dev->dev);
266 pci_disable_device(assigned_dev->dev);
267 pci_dev_put(assigned_dev->dev);
268
269 list_del(&assigned_dev->list);
270 kfree(assigned_dev);
271}
272
273void kvm_free_all_assigned_devices(struct kvm *kvm)
274{
275 struct list_head *ptr, *ptr2;
276 struct kvm_assigned_dev_kernel *assigned_dev;
277
278 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
279 assigned_dev = list_entry(ptr,
280 struct kvm_assigned_dev_kernel,
281 list);
282
283 kvm_free_assigned_device(kvm, assigned_dev);
284 }
285}
286
Sheng Yang00e3ed32008-11-24 14:32:50 +0800287static int assigned_device_update_intx(struct kvm *kvm,
288 struct kvm_assigned_dev_kernel *adev,
289 struct kvm_assigned_irq *airq)
290{
Sheng Yangfbac7812008-11-24 14:32:52 +0800291 adev->guest_irq = airq->guest_irq;
292 adev->ack_notifier.gsi = airq->guest_irq;
293
294 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800295 return 0;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800296
297 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800298 if (!msi2intx &&
Sheng Yangd7cff1c2009-01-06 16:25:10 +0800299 (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
300 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800301 pci_disable_msi(adev->dev);
302 }
303
Sheng Yang00e3ed32008-11-24 14:32:50 +0800304 if (!capable(CAP_SYS_RAWIO))
305 return -EPERM;
306
307 if (airq->host_irq)
308 adev->host_irq = airq->host_irq;
309 else
310 adev->host_irq = adev->dev->irq;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800311
312 /* Even though this is PCI, we don't want to use shared
313 * interrupts. Sharing host devices with guest-assigned devices
314 * on the same interrupt line is not a happy situation: there
315 * are going to be long delays in accepting, acking, etc.
316 */
317 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
318 0, "kvm_assigned_intx_device", (void *)adev))
319 return -EIO;
320 }
321
Sheng Yang4f906c12008-11-24 14:32:51 +0800322 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
323 KVM_ASSIGNED_DEV_HOST_INTX;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800324 return 0;
325}
326
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800327#ifdef CONFIG_X86
328static int assigned_device_update_msi(struct kvm *kvm,
329 struct kvm_assigned_dev_kernel *adev,
330 struct kvm_assigned_irq *airq)
331{
332 int r;
333
Sheng Yang5319c662008-11-24 14:32:57 +0800334 if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
335 /* x86 don't care upper address of guest msi message addr */
336 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
337 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
338 adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
339 adev->guest_msi.data = airq->guest_msi.data;
340 adev->ack_notifier.gsi = -1;
341 } else if (msi2intx) {
342 adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
343 adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
344 adev->guest_irq = airq->guest_irq;
345 adev->ack_notifier.gsi = airq->guest_irq;
346 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800347
348 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
349 return 0;
350
351 if (irqchip_in_kernel(kvm)) {
Sheng Yang5319c662008-11-24 14:32:57 +0800352 if (!msi2intx) {
353 if (adev->irq_requested_type &
354 KVM_ASSIGNED_DEV_HOST_INTX)
355 free_irq(adev->host_irq, (void *)adev);
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800356
Sheng Yang5319c662008-11-24 14:32:57 +0800357 r = pci_enable_msi(adev->dev);
358 if (r)
359 return r;
360 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800361
362 adev->host_irq = adev->dev->irq;
363 if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
364 "kvm_assigned_msi_device", (void *)adev))
365 return -EIO;
366 }
367
Sheng Yang5319c662008-11-24 14:32:57 +0800368 if (!msi2intx)
369 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
370
371 adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800372 return 0;
373}
374#endif
375
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800376static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
377 struct kvm_assigned_irq
378 *assigned_irq)
379{
380 int r = 0;
381 struct kvm_assigned_dev_kernel *match;
382
383 mutex_lock(&kvm->lock);
384
385 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
386 assigned_irq->assigned_dev_id);
387 if (!match) {
388 mutex_unlock(&kvm->lock);
389 return -EINVAL;
390 }
391
Sheng Yang4f906c12008-11-24 14:32:51 +0800392 if (!match->irq_requested_type) {
Sheng Yang342ffb92008-11-24 14:32:49 +0800393 INIT_WORK(&match->interrupt_work,
394 kvm_assigned_dev_interrupt_work_handler);
395 if (irqchip_in_kernel(kvm)) {
396 /* Register ack nofitier */
397 match->ack_notifier.gsi = -1;
398 match->ack_notifier.irq_acked =
399 kvm_assigned_dev_ack_irq;
400 kvm_register_irq_ack_notifier(kvm,
401 &match->ack_notifier);
402
403 /* Request IRQ source ID */
404 r = kvm_request_irq_source_id(kvm);
405 if (r < 0)
406 goto out_release;
407 else
408 match->irq_source_id = r;
Sheng Yang5319c662008-11-24 14:32:57 +0800409
410#ifdef CONFIG_X86
411 /* Determine host device irq type, we can know the
412 * result from dev->msi_enabled */
413 if (msi2intx)
414 pci_enable_msi(match->dev);
415#endif
Sheng Yang342ffb92008-11-24 14:32:49 +0800416 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800417 }
418
Sheng Yang5319c662008-11-24 14:32:57 +0800419 if ((!msi2intx &&
420 (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
421 (msi2intx && match->dev->msi_enabled)) {
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800422#ifdef CONFIG_X86
423 r = assigned_device_update_msi(kvm, match, assigned_irq);
424 if (r) {
425 printk(KERN_WARNING "kvm: failed to enable "
426 "MSI device!\n");
427 goto out_release;
428 }
429#else
430 r = -ENOTTY;
431#endif
432 } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
433 /* Host device IRQ 0 means don't support INTx */
Sheng Yang5319c662008-11-24 14:32:57 +0800434 if (!msi2intx) {
435 printk(KERN_WARNING
436 "kvm: wait device to enable MSI!\n");
437 r = 0;
438 } else {
439 printk(KERN_WARNING
440 "kvm: failed to enable MSI device!\n");
441 r = -ENOTTY;
442 goto out_release;
443 }
Sheng Yang6b9cc7f2008-11-24 14:32:56 +0800444 } else {
445 /* Non-sharing INTx mode */
446 r = assigned_device_update_intx(kvm, match, assigned_irq);
447 if (r) {
448 printk(KERN_WARNING "kvm: failed to enable "
449 "INTx device!\n");
450 goto out_release;
451 }
452 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800453
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800454 mutex_unlock(&kvm->lock);
455 return r;
456out_release:
457 mutex_unlock(&kvm->lock);
458 kvm_free_assigned_device(kvm, match);
459 return r;
460}
461
462static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
463 struct kvm_assigned_pci_dev *assigned_dev)
464{
465 int r = 0;
466 struct kvm_assigned_dev_kernel *match;
467 struct pci_dev *dev;
468
Mark McLoughlin682edb42009-02-05 18:23:46 +0000469 down_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800470 mutex_lock(&kvm->lock);
471
472 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
473 assigned_dev->assigned_dev_id);
474 if (match) {
475 /* device already assigned */
476 r = -EINVAL;
477 goto out;
478 }
479
480 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
481 if (match == NULL) {
482 printk(KERN_INFO "%s: Couldn't allocate memory\n",
483 __func__);
484 r = -ENOMEM;
485 goto out;
486 }
487 dev = pci_get_bus_and_slot(assigned_dev->busnr,
488 assigned_dev->devfn);
489 if (!dev) {
490 printk(KERN_INFO "%s: host device not found\n", __func__);
491 r = -EINVAL;
492 goto out_free;
493 }
494 if (pci_enable_device(dev)) {
495 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
496 r = -EBUSY;
497 goto out_put;
498 }
499 r = pci_request_regions(dev, "kvm_assigned_device");
500 if (r) {
501 printk(KERN_INFO "%s: Could not get access to device regions\n",
502 __func__);
503 goto out_disable;
504 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800505
506 pci_reset_function(dev);
507
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800508 match->assigned_dev_id = assigned_dev->assigned_dev_id;
509 match->host_busnr = assigned_dev->busnr;
510 match->host_devfn = assigned_dev->devfn;
Weidong Hanb6535742008-12-08 23:29:53 +0800511 match->flags = assigned_dev->flags;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800512 match->dev = dev;
Mark McLoughlinf29b2672008-12-01 13:57:47 +0000513 match->irq_source_id = -1;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800514 match->kvm = kvm;
515
516 list_add(&match->list, &kvm->arch.assigned_dev_head);
517
518 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100519 if (!kvm->arch.iommu_domain) {
Weidong Han260782b2008-12-02 21:03:39 +0800520 r = kvm_iommu_map_guest(kvm);
521 if (r)
522 goto out_list_del;
523 }
524 r = kvm_assign_device(kvm, match);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800525 if (r)
526 goto out_list_del;
527 }
528
529out:
530 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000531 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800532 return r;
533out_list_del:
534 list_del(&match->list);
535 pci_release_regions(dev);
536out_disable:
537 pci_disable_device(dev);
538out_put:
539 pci_dev_put(dev);
540out_free:
541 kfree(match);
542 mutex_unlock(&kvm->lock);
Mark McLoughlin682edb42009-02-05 18:23:46 +0000543 up_read(&kvm->slots_lock);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800544 return r;
545}
546#endif
547
Weidong Han0a920352008-12-02 21:24:23 +0800548#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
549static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
550 struct kvm_assigned_pci_dev *assigned_dev)
551{
552 int r = 0;
553 struct kvm_assigned_dev_kernel *match;
554
555 mutex_lock(&kvm->lock);
556
557 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
558 assigned_dev->assigned_dev_id);
559 if (!match) {
560 printk(KERN_INFO "%s: device hasn't been assigned before, "
561 "so cannot be deassigned\n", __func__);
562 r = -EINVAL;
563 goto out;
564 }
565
566 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
567 kvm_deassign_device(kvm, match);
568
569 kvm_free_assigned_device(kvm, match);
570
571out:
572 mutex_unlock(&kvm->lock);
573 return r;
574}
575#endif
576
James Morris5aacf0c2006-12-22 01:04:55 -0800577static inline int valid_vcpu(int n)
578{
579 return likely(n >= 0 && n < KVM_MAX_VCPUS);
580}
581
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800582inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300583{
584 if (pfn_valid(pfn))
585 return PageReserved(pfn_to_page(pfn));
586
587 return true;
588}
589
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590/*
591 * Switches to specified vcpu, until a matching vcpu_put()
592 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200593void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594{
Avi Kivity15ad7142007-07-11 18:17:21 +0300595 int cpu;
596
Avi Kivitybccf2152007-02-21 18:04:26 +0200597 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300598 cpu = get_cpu();
599 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200600 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300601 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200602}
603
Carsten Otte313a3dc2007-10-11 19:16:52 +0200604void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605{
Avi Kivity15ad7142007-07-11 18:17:21 +0300606 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200607 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300608 preempt_notifier_unregister(&vcpu->preempt_notifier);
609 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 mutex_unlock(&vcpu->mutex);
611}
612
Avi Kivityd9e368d2007-06-07 19:18:30 +0300613static void ack_flush(void *_completed)
614{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300615}
616
Rusty Russell49846892008-12-08 20:26:24 +1030617static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300618{
Avi Kivity597a5f52008-07-20 14:24:22 +0300619 int i, cpu, me;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030620 cpumask_var_t cpus;
621 bool called = true;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300622 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300623
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030624 if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
625 cpumask_clear(cpus);
626
Avi Kivity597a5f52008-07-20 14:24:22 +0300627 me = get_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000628 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
629 vcpu = kvm->vcpus[i];
630 if (!vcpu)
631 continue;
Rusty Russell49846892008-12-08 20:26:24 +1030632 if (test_and_set_bit(req, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300633 continue;
634 cpu = vcpu->cpu;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030635 if (cpus != NULL && cpu != -1 && cpu != me)
636 cpumask_set_cpu(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300637 }
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030638 if (unlikely(cpus == NULL))
639 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
640 else if (!cpumask_empty(cpus))
641 smp_call_function_many(cpus, ack_flush, NULL, 1);
642 else
643 called = false;
Avi Kivity597a5f52008-07-20 14:24:22 +0300644 put_cpu();
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030645 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030646 return called;
647}
648
649void kvm_flush_remote_tlbs(struct kvm *kvm)
650{
651 if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
652 ++kvm->stat.remote_tlb_flush;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300653}
654
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500655void kvm_reload_remote_mmus(struct kvm *kvm)
656{
Rusty Russell49846892008-12-08 20:26:24 +1030657 make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500658}
659
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000660int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
661{
662 struct page *page;
663 int r;
664
665 mutex_init(&vcpu->mutex);
666 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000667 vcpu->kvm = kvm;
668 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300669 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000670
671 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
672 if (!page) {
673 r = -ENOMEM;
674 goto fail;
675 }
676 vcpu->run = page_address(page);
677
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800678 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000679 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800680 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000681 return 0;
682
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000683fail_free_run:
684 free_page((unsigned long)vcpu->run);
685fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000686 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000687}
688EXPORT_SYMBOL_GPL(kvm_vcpu_init);
689
690void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
691{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800692 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000693 free_page((unsigned long)vcpu->run);
694}
695EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
696
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200697#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
698static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
699{
700 return container_of(mn, struct kvm, mmu_notifier);
701}
702
703static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
704 struct mm_struct *mm,
705 unsigned long address)
706{
707 struct kvm *kvm = mmu_notifier_to_kvm(mn);
708 int need_tlb_flush;
709
710 /*
711 * When ->invalidate_page runs, the linux pte has been zapped
712 * already but the page is still allocated until
713 * ->invalidate_page returns. So if we increase the sequence
714 * here the kvm page fault will notice if the spte can't be
715 * established because the page is going to be freed. If
716 * instead the kvm page fault establishes the spte before
717 * ->invalidate_page runs, kvm_unmap_hva will release it
718 * before returning.
719 *
720 * The sequence increase only need to be seen at spin_unlock
721 * time, and not at spin_lock time.
722 *
723 * Increasing the sequence after the spin_unlock would be
724 * unsafe because the kvm page fault could then establish the
725 * pte after kvm_unmap_hva returned, without noticing the page
726 * is going to be freed.
727 */
728 spin_lock(&kvm->mmu_lock);
729 kvm->mmu_notifier_seq++;
730 need_tlb_flush = kvm_unmap_hva(kvm, address);
731 spin_unlock(&kvm->mmu_lock);
732
733 /* we've to flush the tlb before the pages can be freed */
734 if (need_tlb_flush)
735 kvm_flush_remote_tlbs(kvm);
736
737}
738
739static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
740 struct mm_struct *mm,
741 unsigned long start,
742 unsigned long end)
743{
744 struct kvm *kvm = mmu_notifier_to_kvm(mn);
745 int need_tlb_flush = 0;
746
747 spin_lock(&kvm->mmu_lock);
748 /*
749 * The count increase must become visible at unlock time as no
750 * spte can be established without taking the mmu_lock and
751 * count is also read inside the mmu_lock critical section.
752 */
753 kvm->mmu_notifier_count++;
754 for (; start < end; start += PAGE_SIZE)
755 need_tlb_flush |= kvm_unmap_hva(kvm, start);
756 spin_unlock(&kvm->mmu_lock);
757
758 /* we've to flush the tlb before the pages can be freed */
759 if (need_tlb_flush)
760 kvm_flush_remote_tlbs(kvm);
761}
762
763static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
764 struct mm_struct *mm,
765 unsigned long start,
766 unsigned long end)
767{
768 struct kvm *kvm = mmu_notifier_to_kvm(mn);
769
770 spin_lock(&kvm->mmu_lock);
771 /*
772 * This sequence increase will notify the kvm page fault that
773 * the page that is going to be mapped in the spte could have
774 * been freed.
775 */
776 kvm->mmu_notifier_seq++;
777 /*
778 * The above sequence increase must be visible before the
779 * below count decrease but both values are read by the kvm
780 * page fault under mmu_lock spinlock so we don't need to add
781 * a smb_wmb() here in between the two.
782 */
783 kvm->mmu_notifier_count--;
784 spin_unlock(&kvm->mmu_lock);
785
786 BUG_ON(kvm->mmu_notifier_count < 0);
787}
788
789static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
790 struct mm_struct *mm,
791 unsigned long address)
792{
793 struct kvm *kvm = mmu_notifier_to_kvm(mn);
794 int young;
795
796 spin_lock(&kvm->mmu_lock);
797 young = kvm_age_hva(kvm, address);
798 spin_unlock(&kvm->mmu_lock);
799
800 if (young)
801 kvm_flush_remote_tlbs(kvm);
802
803 return young;
804}
805
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100806static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
807 struct mm_struct *mm)
808{
809 struct kvm *kvm = mmu_notifier_to_kvm(mn);
810 kvm_arch_flush_shadow(kvm);
811}
812
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200813static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
814 .invalidate_page = kvm_mmu_notifier_invalidate_page,
815 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
816 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
817 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100818 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200819};
820#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
821
Avi Kivityf17abe92007-02-21 19:28:04 +0200822static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800824 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200825#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
826 struct page *page;
827#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800829 if (IS_ERR(kvm))
830 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831
Laurent Vivier5f94c172008-05-30 16:05:54 +0200832#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
833 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
834 if (!page) {
835 kfree(kvm);
836 return ERR_PTR(-ENOMEM);
837 }
838 kvm->coalesced_mmio_ring =
839 (struct kvm_coalesced_mmio_ring *)page_address(page);
840#endif
841
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200842#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
843 {
844 int err;
845 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
846 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
847 if (err) {
848#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
849 put_page(page);
850#endif
851 kfree(kvm);
852 return ERR_PTR(err);
853 }
854 }
855#endif
856
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200857 kvm->mm = current->mm;
858 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500859 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300860 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800861 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400862 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200863 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300864 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000865 spin_lock(&kvm_lock);
866 list_add(&kvm->vm_list, &vm_list);
867 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200868#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
869 kvm_coalesced_mmio_init(kvm);
870#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800871out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200872 return kvm;
873}
874
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875/*
876 * Free any memory in @free but not in @dont.
877 */
878static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
879 struct kvm_memory_slot *dont)
880{
Izik Eidus290fc382007-09-27 14:11:22 +0200881 if (!dont || free->rmap != dont->rmap)
882 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883
884 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
885 vfree(free->dirty_bitmap);
886
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300887 if (!dont || free->lpage_info != dont->lpage_info)
888 vfree(free->lpage_info);
889
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000891 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500892 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300893 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894}
895
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800896void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897{
898 int i;
899
900 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000901 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902}
903
Avi Kivityf17abe92007-02-21 19:28:04 +0200904static void kvm_destroy_vm(struct kvm *kvm)
905{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200906 struct mm_struct *mm = kvm->mm;
907
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800908 kvm_arch_sync_events(kvm);
Avi Kivity133de902007-02-12 00:54:44 -0800909 spin_lock(&kvm_lock);
910 list_del(&kvm->vm_list);
911 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300912 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400913 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200914#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
915 if (kvm->coalesced_mmio_ring != NULL)
916 free_page((unsigned long)kvm->coalesced_mmio_ring);
917#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200918#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
919 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
920#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800921 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200922 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200923}
924
Izik Eidusd39f13b2008-03-30 16:01:25 +0300925void kvm_get_kvm(struct kvm *kvm)
926{
927 atomic_inc(&kvm->users_count);
928}
929EXPORT_SYMBOL_GPL(kvm_get_kvm);
930
931void kvm_put_kvm(struct kvm *kvm)
932{
933 if (atomic_dec_and_test(&kvm->users_count))
934 kvm_destroy_vm(kvm);
935}
936EXPORT_SYMBOL_GPL(kvm_put_kvm);
937
938
Avi Kivityf17abe92007-02-21 19:28:04 +0200939static int kvm_vm_release(struct inode *inode, struct file *filp)
940{
941 struct kvm *kvm = filp->private_data;
942
Izik Eidusd39f13b2008-03-30 16:01:25 +0300943 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 return 0;
945}
946
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 * Allocate some memory and give it an address in the guest physical address
949 * space.
950 *
951 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800952 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500953 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800955int __kvm_set_memory_region(struct kvm *kvm,
956 struct kvm_userspace_memory_region *mem,
957 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958{
959 int r;
960 gfn_t base_gfn;
961 unsigned long npages;
962 unsigned long i;
963 struct kvm_memory_slot *memslot;
964 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
966 r = -EINVAL;
967 /* General sanity checks */
968 if (mem->memory_size & (PAGE_SIZE - 1))
969 goto out;
970 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
971 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800972 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600973 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200974 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 goto out;
976 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
977 goto out;
978
979 memslot = &kvm->memslots[mem->slot];
980 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
981 npages = mem->memory_size >> PAGE_SHIFT;
982
983 if (!npages)
984 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
985
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 new = old = *memslot;
987
988 new.base_gfn = base_gfn;
989 new.npages = npages;
990 new.flags = mem->flags;
991
992 /* Disallow changing a memory slot's size. */
993 r = -EINVAL;
994 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800995 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996
997 /* Check for overlaps */
998 r = -EEXIST;
999 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1000 struct kvm_memory_slot *s = &kvm->memslots[i];
1001
1002 if (s == memslot)
1003 continue;
1004 if (!((base_gfn + npages <= s->base_gfn) ||
1005 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +08001006 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 /* Free page dirty bitmap if unneeded */
1010 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001011 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012
1013 r = -ENOMEM;
1014
1015 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +02001016#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001017 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001018 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +02001019
1020 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001021 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +02001022
Izik Eidus290fc382007-09-27 14:11:22 +02001023 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001024
Izik Eidus80b14b52007-10-25 11:54:04 +02001025 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001026 /*
1027 * hva_to_rmmap() serialzies with the mmu_lock and to be
1028 * safe it has to ignore memslots with !user_alloc &&
1029 * !userspace_addr.
1030 */
1031 if (user_alloc)
1032 new.userspace_addr = mem->userspace_addr;
1033 else
1034 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001036 if (npages && !new.lpage_info) {
1037 int largepages = npages / KVM_PAGES_PER_HPAGE;
1038 if (npages % KVM_PAGES_PER_HPAGE)
1039 largepages++;
1040 if (base_gfn % KVM_PAGES_PER_HPAGE)
1041 largepages++;
1042
1043 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1044
1045 if (!new.lpage_info)
1046 goto out_free;
1047
1048 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1049
1050 if (base_gfn % KVM_PAGES_PER_HPAGE)
1051 new.lpage_info[0].write_count = 1;
1052 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1053 new.lpage_info[largepages-1].write_count = 1;
1054 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055
1056 /* Allocate page dirty bitmap if needed */
1057 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1058 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1059
1060 new.dirty_bitmap = vmalloc(dirty_bytes);
1061 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001062 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 memset(new.dirty_bitmap, 0, dirty_bytes);
1064 }
Carsten Otteeff01142008-06-27 15:05:31 +02001065#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001067 if (!npages)
1068 kvm_arch_flush_shadow(kvm);
1069
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001070 spin_lock(&kvm->mmu_lock);
1071 if (mem->slot >= kvm->nmemslots)
1072 kvm->nmemslots = mem->slot + 1;
1073
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001075 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076
Zhang Xiantao0de10342007-11-20 16:25:04 +08001077 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1078 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001079 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001080 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02001081 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08001082 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001083 }
1084
Glauber Costa6f897242008-12-03 13:40:51 -02001085 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1086 /* Slot deletion case: we have to update the current slot */
1087 if (!npages)
1088 *memslot = old;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001089#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001090 /* map the pages in iommu page table */
1091 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1092 if (r)
1093 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001094#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 return 0;
1096
Sheng Yangf78e0e22007-10-29 09:40:42 +08001097out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 kvm_free_physmem_slot(&new, &old);
1099out:
1100 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001101
1102}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001103EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1104
1105int kvm_set_memory_region(struct kvm *kvm,
1106 struct kvm_userspace_memory_region *mem,
1107 int user_alloc)
1108{
1109 int r;
1110
Izik Eidus72dc67a2008-02-10 18:04:15 +02001111 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001112 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001113 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001114 return r;
1115}
Izik Eidus210c7c42007-10-24 23:52:57 +02001116EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1117
Carsten Otte1fe779f2007-10-29 16:08:35 +01001118int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1119 struct
1120 kvm_userspace_memory_region *mem,
1121 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +02001122{
Izik Eiduse0d62c72007-10-24 23:57:46 +02001123 if (mem->slot >= KVM_MEMORY_SLOTS)
1124 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +02001125 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
1127
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001128int kvm_get_dirty_log(struct kvm *kvm,
1129 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130{
1131 struct kvm_memory_slot *memslot;
1132 int r, i;
1133 int n;
1134 unsigned long any = 0;
1135
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 r = -EINVAL;
1137 if (log->slot >= KVM_MEMORY_SLOTS)
1138 goto out;
1139
1140 memslot = &kvm->memslots[log->slot];
1141 r = -ENOENT;
1142 if (!memslot->dirty_bitmap)
1143 goto out;
1144
Uri Lublincd1a4a92007-02-22 16:43:09 +02001145 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146
Uri Lublincd1a4a92007-02-22 16:43:09 +02001147 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 any = memslot->dirty_bitmap[i];
1149
1150 r = -EFAULT;
1151 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1152 goto out;
1153
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001154 if (any)
1155 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
1157 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 return r;
1160}
1161
Izik Eiduscea7bb22007-10-17 19:17:48 +02001162int is_error_page(struct page *page)
1163{
1164 return page == bad_page;
1165}
1166EXPORT_SYMBOL_GPL(is_error_page);
1167
Anthony Liguori35149e22008-04-02 14:46:56 -05001168int is_error_pfn(pfn_t pfn)
1169{
1170 return pfn == bad_pfn;
1171}
1172EXPORT_SYMBOL_GPL(is_error_pfn);
1173
Izik Eidusf9d46eb2007-11-11 22:02:22 +02001174static inline unsigned long bad_hva(void)
1175{
1176 return PAGE_OFFSET;
1177}
1178
1179int kvm_is_error_hva(unsigned long addr)
1180{
1181 return addr == bad_hva();
1182}
1183EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1184
Izik Eidus28430992008-10-03 17:40:32 +03001185struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186{
1187 int i;
1188
1189 for (i = 0; i < kvm->nmemslots; ++i) {
1190 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1191
1192 if (gfn >= memslot->base_gfn
1193 && gfn < memslot->base_gfn + memslot->npages)
1194 return memslot;
1195 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001196 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197}
Izik Eidus28430992008-10-03 17:40:32 +03001198EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001199
1200struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1201{
1202 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001203 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001204}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205
Izik Eiduse0d62c72007-10-24 23:57:46 +02001206int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1207{
1208 int i;
1209
1210 gfn = unalias_gfn(kvm, gfn);
1211 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1212 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1213
1214 if (gfn >= memslot->base_gfn
1215 && gfn < memslot->base_gfn + memslot->npages)
1216 return 1;
1217 }
1218 return 0;
1219}
1220EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1221
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001222unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001223{
1224 struct kvm_memory_slot *slot;
1225
1226 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001227 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001228 if (!slot)
1229 return bad_hva();
1230 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1231}
Sheng Yang0d150292008-04-25 21:44:50 +08001232EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001233
Anthony Liguori35149e22008-04-02 14:46:56 -05001234pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc22007-03-30 14:02:32 +03001235{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001236 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001237 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001238 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001239 pfn_t pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001240
Avi Kivity60395222007-10-21 11:03:36 +02001241 might_sleep();
1242
Izik Eidus539cb662007-11-11 22:05:04 +02001243 addr = gfn_to_hva(kvm, gfn);
1244 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001245 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001246 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001247 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001248
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001249 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001250
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001251 if (unlikely(npages != 1)) {
1252 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001253
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001254 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001255 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001256
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001257 if (vma == NULL || addr < vma->vm_start ||
1258 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001259 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001260 get_page(bad_page);
1261 return page_to_pfn(bad_page);
1262 }
1263
1264 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001265 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001266 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001267 } else
1268 pfn = page_to_pfn(page[0]);
1269
1270 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001271}
1272
1273EXPORT_SYMBOL_GPL(gfn_to_pfn);
1274
1275struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1276{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001277 pfn_t pfn;
1278
1279 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001280 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001281 return pfn_to_page(pfn);
1282
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001283 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001284
1285 get_page(bad_page);
1286 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001287}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001288
Avi Kivity954bbbc22007-03-30 14:02:32 +03001289EXPORT_SYMBOL_GPL(gfn_to_page);
1290
Izik Eidusb4231d62007-11-20 11:49:33 +02001291void kvm_release_page_clean(struct page *page)
1292{
Anthony Liguori35149e22008-04-02 14:46:56 -05001293 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001294}
1295EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1296
Anthony Liguori35149e22008-04-02 14:46:56 -05001297void kvm_release_pfn_clean(pfn_t pfn)
1298{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001299 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001300 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001301}
1302EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1303
Izik Eidusb4231d62007-11-20 11:49:33 +02001304void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001305{
Anthony Liguori35149e22008-04-02 14:46:56 -05001306 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001307}
Izik Eidusb4231d62007-11-20 11:49:33 +02001308EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001309
Anthony Liguori35149e22008-04-02 14:46:56 -05001310void kvm_release_pfn_dirty(pfn_t pfn)
1311{
1312 kvm_set_pfn_dirty(pfn);
1313 kvm_release_pfn_clean(pfn);
1314}
1315EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1316
1317void kvm_set_page_dirty(struct page *page)
1318{
1319 kvm_set_pfn_dirty(page_to_pfn(page));
1320}
1321EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1322
1323void kvm_set_pfn_dirty(pfn_t pfn)
1324{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001325 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001326 struct page *page = pfn_to_page(pfn);
1327 if (!PageReserved(page))
1328 SetPageDirty(page);
1329 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001330}
1331EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1332
1333void kvm_set_pfn_accessed(pfn_t pfn)
1334{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001335 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001336 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001337}
1338EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1339
1340void kvm_get_pfn(pfn_t pfn)
1341{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001342 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001343 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001344}
1345EXPORT_SYMBOL_GPL(kvm_get_pfn);
1346
Izik Eidus195aefd2007-10-01 22:14:18 +02001347static int next_segment(unsigned long len, int offset)
1348{
1349 if (len > PAGE_SIZE - offset)
1350 return PAGE_SIZE - offset;
1351 else
1352 return len;
1353}
1354
1355int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1356 int len)
1357{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001358 int r;
1359 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001360
Izik Eiduse0506bc2007-11-11 22:10:22 +02001361 addr = gfn_to_hva(kvm, gfn);
1362 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001363 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001364 r = copy_from_user(data, (void __user *)addr + offset, len);
1365 if (r)
1366 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1370
1371int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1372{
1373 gfn_t gfn = gpa >> PAGE_SHIFT;
1374 int seg;
1375 int offset = offset_in_page(gpa);
1376 int ret;
1377
1378 while ((seg = next_segment(len, offset)) != 0) {
1379 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1380 if (ret < 0)
1381 return ret;
1382 offset = 0;
1383 len -= seg;
1384 data += seg;
1385 ++gfn;
1386 }
1387 return 0;
1388}
1389EXPORT_SYMBOL_GPL(kvm_read_guest);
1390
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001391int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1392 unsigned long len)
1393{
1394 int r;
1395 unsigned long addr;
1396 gfn_t gfn = gpa >> PAGE_SHIFT;
1397 int offset = offset_in_page(gpa);
1398
1399 addr = gfn_to_hva(kvm, gfn);
1400 if (kvm_is_error_hva(addr))
1401 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001402 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001403 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001404 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001405 if (r)
1406 return -EFAULT;
1407 return 0;
1408}
1409EXPORT_SYMBOL(kvm_read_guest_atomic);
1410
Izik Eidus195aefd2007-10-01 22:14:18 +02001411int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1412 int offset, int len)
1413{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001414 int r;
1415 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001416
Izik Eiduse0506bc2007-11-11 22:10:22 +02001417 addr = gfn_to_hva(kvm, gfn);
1418 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001419 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001420 r = copy_to_user((void __user *)addr + offset, data, len);
1421 if (r)
1422 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001423 mark_page_dirty(kvm, gfn);
1424 return 0;
1425}
1426EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1427
1428int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1429 unsigned long len)
1430{
1431 gfn_t gfn = gpa >> PAGE_SHIFT;
1432 int seg;
1433 int offset = offset_in_page(gpa);
1434 int ret;
1435
1436 while ((seg = next_segment(len, offset)) != 0) {
1437 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1438 if (ret < 0)
1439 return ret;
1440 offset = 0;
1441 len -= seg;
1442 data += seg;
1443 ++gfn;
1444 }
1445 return 0;
1446}
1447
1448int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1449{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001450 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001451}
1452EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1453
1454int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1455{
1456 gfn_t gfn = gpa >> PAGE_SHIFT;
1457 int seg;
1458 int offset = offset_in_page(gpa);
1459 int ret;
1460
1461 while ((seg = next_segment(len, offset)) != 0) {
1462 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1463 if (ret < 0)
1464 return ret;
1465 offset = 0;
1466 len -= seg;
1467 ++gfn;
1468 }
1469 return 0;
1470}
1471EXPORT_SYMBOL_GPL(kvm_clear_guest);
1472
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1474{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001475 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476
Uri Lublin3b6fff12007-10-30 10:42:09 +02001477 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001478 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001479 if (memslot && memslot->dirty_bitmap) {
1480 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481
Rusty Russell7e9d6192007-07-31 20:41:14 +10001482 /* avoid RMW */
1483 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1484 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 }
1486}
1487
Eddie Dongb6958ce2007-07-18 12:15:21 +03001488/*
1489 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1490 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001491void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001492{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001493 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001494
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001495 for (;;) {
1496 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001497
Marcelo Tosattid7690172008-09-08 15:23:48 -03001498 if (kvm_cpu_has_interrupt(vcpu) ||
1499 kvm_cpu_has_pending_timer(vcpu) ||
1500 kvm_arch_vcpu_runnable(vcpu)) {
1501 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001502 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001503 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001504 if (signal_pending(current))
1505 break;
1506
Eddie Dongb6958ce2007-07-18 12:15:21 +03001507 vcpu_put(vcpu);
1508 schedule();
1509 vcpu_load(vcpu);
1510 }
1511
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001512 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001513}
1514
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515void kvm_resched(struct kvm_vcpu *vcpu)
1516{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001517 if (!need_resched())
1518 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520}
1521EXPORT_SYMBOL_GPL(kvm_resched);
1522
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001523static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001524{
1525 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001526 struct page *page;
1527
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001528 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001529 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001530#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001531 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001532 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001533#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001534#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1535 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1536 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1537#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001538 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001539 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001540 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001541 vmf->page = page;
1542 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001543}
1544
1545static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001546 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001547};
1548
1549static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1550{
1551 vma->vm_ops = &kvm_vcpu_vm_ops;
1552 return 0;
1553}
1554
Avi Kivitybccf2152007-02-21 18:04:26 +02001555static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1556{
1557 struct kvm_vcpu *vcpu = filp->private_data;
1558
Al Viro66c0b392008-04-19 20:33:56 +01001559 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001560 return 0;
1561}
1562
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001563static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001564 .release = kvm_vcpu_release,
1565 .unlocked_ioctl = kvm_vcpu_ioctl,
1566 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001567 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001568};
1569
1570/*
1571 * Allocates an inode for the vcpu.
1572 */
1573static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1574{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001575 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001576 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001577 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001578 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001579}
1580
Avi Kivityc5ea7662007-02-20 18:41:05 +02001581/*
1582 * Creates some virtual cpus. Good luck creating more than one.
1583 */
1584static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1585{
1586 int r;
1587 struct kvm_vcpu *vcpu;
1588
Avi Kivityc5ea7662007-02-20 18:41:05 +02001589 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001590 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001591
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001592 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001593 if (IS_ERR(vcpu))
1594 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001595
Avi Kivity15ad7142007-07-11 18:17:21 +03001596 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1597
Avi Kivity26e52152007-11-20 15:30:24 +02001598 r = kvm_arch_vcpu_setup(vcpu);
1599 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001600 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001601
Shaohua Li11ec2802007-07-23 14:51:37 +08001602 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001603 if (kvm->vcpus[n]) {
1604 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001605 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001606 }
1607 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001608 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001609
1610 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001611 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001612 r = create_vcpu_fd(vcpu);
1613 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001614 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001615 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001616
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001617unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001618 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001619 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001620vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001621 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001622 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001623 return r;
1624}
1625
Avi Kivity1961d272007-03-05 19:46:05 +02001626static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1627{
1628 if (sigset) {
1629 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1630 vcpu->sigset_active = 1;
1631 vcpu->sigset = *sigset;
1632 } else
1633 vcpu->sigset_active = 0;
1634 return 0;
1635}
1636
Avi Kivitybccf2152007-02-21 18:04:26 +02001637static long kvm_vcpu_ioctl(struct file *filp,
1638 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639{
Avi Kivitybccf2152007-02-21 18:04:26 +02001640 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00001641 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001642 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001643 struct kvm_fpu *fpu = NULL;
1644 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001646 if (vcpu->kvm->mm != current->mm)
1647 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001649 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001650 r = -EINVAL;
1651 if (arg)
1652 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001653 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001656 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001658 r = -ENOMEM;
1659 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1660 if (!kvm_regs)
1661 goto out;
1662 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001664 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001666 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1667 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001669out_free1:
1670 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 break;
1672 }
1673 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001674 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001676 r = -ENOMEM;
1677 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1678 if (!kvm_regs)
1679 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001681 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1682 goto out_free2;
1683 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001685 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001687out_free2:
1688 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 break;
1690 }
1691 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001692 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1693 r = -ENOMEM;
1694 if (!kvm_sregs)
1695 goto out;
1696 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 if (r)
1698 goto out;
1699 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001700 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 goto out;
1702 r = 0;
1703 break;
1704 }
1705 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001706 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1707 r = -ENOMEM;
1708 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001710 r = -EFAULT;
1711 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1712 goto out;
1713 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 if (r)
1715 goto out;
1716 r = 0;
1717 break;
1718 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001719 case KVM_GET_MP_STATE: {
1720 struct kvm_mp_state mp_state;
1721
1722 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1723 if (r)
1724 goto out;
1725 r = -EFAULT;
1726 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1727 goto out;
1728 r = 0;
1729 break;
1730 }
1731 case KVM_SET_MP_STATE: {
1732 struct kvm_mp_state mp_state;
1733
1734 r = -EFAULT;
1735 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1736 goto out;
1737 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1738 if (r)
1739 goto out;
1740 r = 0;
1741 break;
1742 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 case KVM_TRANSLATE: {
1744 struct kvm_translation tr;
1745
1746 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001747 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001749 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 if (r)
1751 goto out;
1752 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001753 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754 goto out;
1755 r = 0;
1756 break;
1757 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 case KVM_DEBUG_GUEST: {
1759 struct kvm_debug_guest dbg;
1760
1761 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001762 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001764 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765 if (r)
1766 goto out;
1767 r = 0;
1768 break;
1769 }
Avi Kivity1961d272007-03-05 19:46:05 +02001770 case KVM_SET_SIGNAL_MASK: {
1771 struct kvm_signal_mask __user *sigmask_arg = argp;
1772 struct kvm_signal_mask kvm_sigmask;
1773 sigset_t sigset, *p;
1774
1775 p = NULL;
1776 if (argp) {
1777 r = -EFAULT;
1778 if (copy_from_user(&kvm_sigmask, argp,
1779 sizeof kvm_sigmask))
1780 goto out;
1781 r = -EINVAL;
1782 if (kvm_sigmask.len != sizeof sigset)
1783 goto out;
1784 r = -EFAULT;
1785 if (copy_from_user(&sigset, sigmask_arg->sigset,
1786 sizeof sigset))
1787 goto out;
1788 p = &sigset;
1789 }
1790 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1791 break;
1792 }
Avi Kivityb8836732007-04-01 16:34:31 +03001793 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001794 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1795 r = -ENOMEM;
1796 if (!fpu)
1797 goto out;
1798 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001799 if (r)
1800 goto out;
1801 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001802 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001803 goto out;
1804 r = 0;
1805 break;
1806 }
1807 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001808 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1809 r = -ENOMEM;
1810 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001811 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001812 r = -EFAULT;
1813 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1814 goto out;
1815 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001816 if (r)
1817 goto out;
1818 r = 0;
1819 break;
1820 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001821 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001822 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001823 }
1824out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001825 kfree(fpu);
1826 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001827 return r;
1828}
1829
1830static long kvm_vm_ioctl(struct file *filp,
1831 unsigned int ioctl, unsigned long arg)
1832{
1833 struct kvm *kvm = filp->private_data;
1834 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001835 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001836
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001837 if (kvm->mm != current->mm)
1838 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001839 switch (ioctl) {
1840 case KVM_CREATE_VCPU:
1841 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1842 if (r < 0)
1843 goto out;
1844 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001845 case KVM_SET_USER_MEMORY_REGION: {
1846 struct kvm_userspace_memory_region kvm_userspace_mem;
1847
1848 r = -EFAULT;
1849 if (copy_from_user(&kvm_userspace_mem, argp,
1850 sizeof kvm_userspace_mem))
1851 goto out;
1852
1853 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854 if (r)
1855 goto out;
1856 break;
1857 }
1858 case KVM_GET_DIRTY_LOG: {
1859 struct kvm_dirty_log log;
1860
1861 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00001862 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001864 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 if (r)
1866 goto out;
1867 break;
1868 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001869#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1870 case KVM_REGISTER_COALESCED_MMIO: {
1871 struct kvm_coalesced_mmio_zone zone;
1872 r = -EFAULT;
1873 if (copy_from_user(&zone, argp, sizeof zone))
1874 goto out;
1875 r = -ENXIO;
1876 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1877 if (r)
1878 goto out;
1879 r = 0;
1880 break;
1881 }
1882 case KVM_UNREGISTER_COALESCED_MMIO: {
1883 struct kvm_coalesced_mmio_zone zone;
1884 r = -EFAULT;
1885 if (copy_from_user(&zone, argp, sizeof zone))
1886 goto out;
1887 r = -ENXIO;
1888 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1889 if (r)
1890 goto out;
1891 r = 0;
1892 break;
1893 }
1894#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001895#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1896 case KVM_ASSIGN_PCI_DEVICE: {
1897 struct kvm_assigned_pci_dev assigned_dev;
1898
1899 r = -EFAULT;
1900 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1901 goto out;
1902 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1903 if (r)
1904 goto out;
1905 break;
1906 }
1907 case KVM_ASSIGN_IRQ: {
1908 struct kvm_assigned_irq assigned_irq;
1909
1910 r = -EFAULT;
1911 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1912 goto out;
1913 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1914 if (r)
1915 goto out;
1916 break;
1917 }
1918#endif
Weidong Han0a920352008-12-02 21:24:23 +08001919#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1920 case KVM_DEASSIGN_PCI_DEVICE: {
1921 struct kvm_assigned_pci_dev assigned_dev;
1922
1923 r = -EFAULT;
1924 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1925 goto out;
1926 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1927 if (r)
1928 goto out;
1929 break;
1930 }
1931#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001932 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001933 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001934 }
1935out:
1936 return r;
1937}
1938
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001939static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001940{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001941 struct page *page[1];
1942 unsigned long addr;
1943 int npages;
1944 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001945 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001946
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001947 addr = gfn_to_hva(kvm, gfn);
1948 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001949 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001950
1951 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1952 NULL);
1953 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001954 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001955
1956 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001957 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001958}
1959
1960static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001961 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001962};
1963
1964static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1965{
1966 vma->vm_ops = &kvm_vm_vm_ops;
1967 return 0;
1968}
1969
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01001970static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001971 .release = kvm_vm_release,
1972 .unlocked_ioctl = kvm_vm_ioctl,
1973 .compat_ioctl = kvm_vm_ioctl,
1974 .mmap = kvm_vm_mmap,
1975};
1976
1977static int kvm_dev_ioctl_create_vm(void)
1978{
Al Viro2030a422008-02-23 06:46:49 -05001979 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001980 struct kvm *kvm;
1981
Avi Kivityf17abe92007-02-21 19:28:04 +02001982 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001983 if (IS_ERR(kvm))
1984 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001985 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001986 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001987 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001988
Avi Kivityf17abe92007-02-21 19:28:04 +02001989 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001990}
1991
Avi Kivity1a811b62008-12-08 18:25:27 +02001992static long kvm_dev_ioctl_check_extension_generic(long arg)
1993{
1994 switch (arg) {
Avi Kivityca9edae2008-12-08 18:29:29 +02001995 case KVM_CAP_USER_MEMORY:
Avi Kivity1a811b62008-12-08 18:25:27 +02001996 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1997 return 1;
1998 default:
1999 break;
2000 }
2001 return kvm_dev_ioctl_check_extension(arg);
2002}
2003
Avi Kivityf17abe92007-02-21 19:28:04 +02002004static long kvm_dev_ioctl(struct file *filp,
2005 unsigned int ioctl, unsigned long arg)
2006{
Avi Kivity07c45a32007-03-07 13:05:38 +02002007 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002008
2009 switch (ioctl) {
2010 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002011 r = -EINVAL;
2012 if (arg)
2013 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002014 r = KVM_API_VERSION;
2015 break;
2016 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002017 r = -EINVAL;
2018 if (arg)
2019 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002020 r = kvm_dev_ioctl_create_vm();
2021 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002022 case KVM_CHECK_EXTENSION:
Avi Kivity1a811b62008-12-08 18:25:27 +02002023 r = kvm_dev_ioctl_check_extension_generic(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02002024 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002025 case KVM_GET_VCPU_MMAP_SIZE:
2026 r = -EINVAL;
2027 if (arg)
2028 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02002029 r = PAGE_SIZE; /* struct kvm_run */
2030#ifdef CONFIG_X86
2031 r += PAGE_SIZE; /* pio data page */
2032#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02002033#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2034 r += PAGE_SIZE; /* coalesced mmio ring page */
2035#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02002036 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002037 case KVM_TRACE_ENABLE:
2038 case KVM_TRACE_PAUSE:
2039 case KVM_TRACE_DISABLE:
2040 r = kvm_trace_ioctl(ioctl, arg);
2041 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002043 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 }
2045out:
2046 return r;
2047}
2048
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050 .unlocked_ioctl = kvm_dev_ioctl,
2051 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052};
2053
2054static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002055 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 "kvm",
2057 &kvm_chardev_ops,
2058};
2059
Avi Kivity1b6c0162007-05-24 13:03:52 +03002060static void hardware_enable(void *junk)
2061{
2062 int cpu = raw_smp_processor_id();
2063
Rusty Russell7f59f492008-12-07 21:25:45 +10302064 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002065 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302066 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002067 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002068}
2069
2070static void hardware_disable(void *junk)
2071{
2072 int cpu = raw_smp_processor_id();
2073
Rusty Russell7f59f492008-12-07 21:25:45 +10302074 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03002075 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10302076 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002077 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002078}
2079
Avi Kivity774c47f2007-02-12 00:54:47 -08002080static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2081 void *v)
2082{
2083 int cpu = (long)v;
2084
Avi Kivity1a6f4d72007-11-11 18:37:32 +02002085 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08002086 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002087 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002088 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2089 cpu);
2090 hardware_disable(NULL);
2091 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002092 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002093 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2094 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002095 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002096 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002097 case CPU_ONLINE:
2098 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2099 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02002100 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002101 break;
2102 }
2103 return NOTIFY_OK;
2104}
2105
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002106
2107asmlinkage void kvm_handle_fault_on_reboot(void)
2108{
2109 if (kvm_rebooting)
2110 /* spin while reset goes on */
2111 while (true)
2112 ;
2113 /* Fault while not rebooting. We want the trace. */
2114 BUG();
2115}
2116EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2117
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002118static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002119 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002120{
2121 if (val == SYS_RESTART) {
2122 /*
2123 * Some (well, at least mine) BIOSes hang on reboot if
2124 * in vmx root mode.
2125 */
2126 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002127 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002128 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002129 }
2130 return NOTIFY_OK;
2131}
2132
2133static struct notifier_block kvm_reboot_notifier = {
2134 .notifier_call = kvm_reboot,
2135 .priority = 0,
2136};
2137
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002138void kvm_io_bus_init(struct kvm_io_bus *bus)
2139{
2140 memset(bus, 0, sizeof(*bus));
2141}
2142
2143void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2144{
2145 int i;
2146
2147 for (i = 0; i < bus->dev_count; i++) {
2148 struct kvm_io_device *pos = bus->devs[i];
2149
2150 kvm_iodevice_destructor(pos);
2151 }
2152}
2153
Laurent Vivier92760492008-05-30 16:05:53 +02002154struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2155 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002156{
2157 int i;
2158
2159 for (i = 0; i < bus->dev_count; i++) {
2160 struct kvm_io_device *pos = bus->devs[i];
2161
Laurent Vivier92760492008-05-30 16:05:53 +02002162 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002163 return pos;
2164 }
2165
2166 return NULL;
2167}
2168
2169void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2170{
2171 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2172
2173 bus->devs[bus->dev_count++] = dev;
2174}
2175
Avi Kivity774c47f2007-02-12 00:54:47 -08002176static struct notifier_block kvm_cpu_notifier = {
2177 .notifier_call = kvm_cpu_hotplug,
2178 .priority = 20, /* must be > scheduler priority */
2179};
2180
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002181static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02002182{
2183 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02002184 struct kvm *kvm;
2185
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002186 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002187 spin_lock(&kvm_lock);
2188 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002189 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02002190 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002191 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02002192}
2193
2194DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2195
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002196static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002197{
2198 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002199 struct kvm *kvm;
2200 struct kvm_vcpu *vcpu;
2201 int i;
2202
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002203 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002204 spin_lock(&kvm_lock);
2205 list_for_each_entry(kvm, &vm_list, vm_list)
2206 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002207 vcpu = kvm->vcpus[i];
2208 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002209 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002210 }
2211 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002212 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002213}
2214
Avi Kivityba1389b2007-11-18 16:24:12 +02002215DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2216
2217static struct file_operations *stat_fops[] = {
2218 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2219 [KVM_STAT_VM] = &vm_stat_fops,
2220};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002221
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002222static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223{
2224 struct kvm_stats_debugfs_item *p;
2225
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002226 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002228 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002229 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002230 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231}
2232
2233static void kvm_exit_debug(void)
2234{
2235 struct kvm_stats_debugfs_item *p;
2236
2237 for (p = debugfs_entries; p->name; ++p)
2238 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002239 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240}
2241
Avi Kivity59ae6c62007-02-12 00:54:48 -08002242static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2243{
Avi Kivity4267c412007-05-24 13:09:41 +03002244 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002245 return 0;
2246}
2247
2248static int kvm_resume(struct sys_device *dev)
2249{
Avi Kivity4267c412007-05-24 13:09:41 +03002250 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002251 return 0;
2252}
2253
2254static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002255 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002256 .suspend = kvm_suspend,
2257 .resume = kvm_resume,
2258};
2259
2260static struct sys_device kvm_sysdev = {
2261 .id = 0,
2262 .cls = &kvm_sysdev_class,
2263};
2264
Izik Eiduscea7bb22007-10-17 19:17:48 +02002265struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002266pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267
Avi Kivity15ad7142007-07-11 18:17:21 +03002268static inline
2269struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2270{
2271 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2272}
2273
2274static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2275{
2276 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2277
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002278 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002279}
2280
2281static void kvm_sched_out(struct preempt_notifier *pn,
2282 struct task_struct *next)
2283{
2284 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2285
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002286 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002287}
2288
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002289int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002290 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291{
2292 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002293 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002295 kvm_init_debug();
2296
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002297 r = kvm_arch_init(opaque);
2298 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002299 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002300
2301 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2302
2303 if (bad_page == NULL) {
2304 r = -ENOMEM;
2305 goto out;
2306 }
2307
Anthony Liguori35149e22008-04-02 14:46:56 -05002308 bad_pfn = page_to_pfn(bad_page);
2309
Rusty Russell7f59f492008-12-07 21:25:45 +10302310 if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2311 r = -ENOMEM;
2312 goto out_free_0;
2313 }
2314
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002315 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 if (r < 0)
Rusty Russell7f59f492008-12-07 21:25:45 +10302317 goto out_free_0a;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318
Yang, Sheng002c7f72007-07-31 14:23:01 +03002319 for_each_online_cpu(cpu) {
2320 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002321 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002322 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002323 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002324 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002325 }
2326
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002327 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002328 r = register_cpu_notifier(&kvm_cpu_notifier);
2329 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002330 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 register_reboot_notifier(&kvm_reboot_notifier);
2332
Avi Kivity59ae6c62007-02-12 00:54:48 -08002333 r = sysdev_class_register(&kvm_sysdev_class);
2334 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002335 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002336
2337 r = sysdev_register(&kvm_sysdev);
2338 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002339 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002340
Rusty Russellc16f8622007-07-30 21:12:19 +10002341 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2342 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002343 __alignof__(struct kvm_vcpu),
2344 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002345 if (!kvm_vcpu_cache) {
2346 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002347 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002348 }
2349
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002351 kvm_vm_fops.owner = module;
2352 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
2354 r = misc_register(&kvm_dev);
2355 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002356 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 goto out_free;
2358 }
2359
Avi Kivity15ad7142007-07-11 18:17:21 +03002360 kvm_preempt_ops.sched_in = kvm_sched_in;
2361 kvm_preempt_ops.sched_out = kvm_sched_out;
Sheng Yang5319c662008-11-24 14:32:57 +08002362#ifndef CONFIG_X86
2363 msi2intx = 0;
2364#endif
Avi Kivity15ad7142007-07-11 18:17:21 +03002365
Avi Kivityc7addb92007-09-16 18:58:32 +02002366 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367
2368out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002369 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002370out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002371 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002372out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002373 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002374out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002376 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002377out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002378 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002379out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002380 kvm_arch_hardware_unsetup();
Rusty Russell7f59f492008-12-07 21:25:45 +10302381out_free_0a:
2382 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002383out_free_0:
2384 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002385out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002386 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002387 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002388out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 return r;
2390}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002391EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002393void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002395 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002397 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002398 sysdev_unregister(&kvm_sysdev);
2399 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002401 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002402 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002403 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002404 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 kvm_exit_debug();
Rusty Russell7f59f492008-12-07 21:25:45 +10302406 free_cpumask_var(cpus_hardware_enabled);
Izik Eiduscea7bb22007-10-17 19:17:48 +02002407 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002409EXPORT_SYMBOL_GPL(kvm_exit);