blob: 228c1d18a614e68c71dcae495ba0b0ef73549c74 [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
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +080067DEFINE_SPINLOCK(kvm_lock);
68LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -080069
Avi Kivity1b6c0162007-05-24 13:03:52 +030070static cpumask_t cpus_hardware_enabled;
71
Rusty Russellc16f8622007-07-30 21:12:19 +100072struct kmem_cache *kvm_vcpu_cache;
73EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030074
Avi Kivity15ad7142007-07-11 18:17:21 +030075static __read_mostly struct preempt_ops kvm_preempt_ops;
76
Hollis Blanchard76f7c872008-04-15 16:05:42 -050077struct dentry *kvm_debugfs_dir;
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Avi Kivitybccf2152007-02-21 18:04:26 +020079static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
80 unsigned long arg);
81
Avi Kivity4ecac3f2008-05-13 13:23:38 +030082bool kvm_rebooting;
83
Xiantao Zhang8a98f662008-10-06 13:47:38 +080084#ifdef KVM_CAP_DEVICE_ASSIGNMENT
Sheng Yangf64769e2008-11-24 14:32:55 +080085
86#ifdef CONFIG_X86
87static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
88{
89 int vcpu_id;
90 struct kvm_vcpu *vcpu;
91 struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
92 int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
93 >> MSI_ADDR_DEST_ID_SHIFT;
94 int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
95 >> MSI_DATA_VECTOR_SHIFT;
96 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
97 (unsigned long *)&dev->guest_msi.address_lo);
98 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
99 (unsigned long *)&dev->guest_msi.data);
100 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
101 (unsigned long *)&dev->guest_msi.data);
102 u32 deliver_bitmask;
103
104 BUG_ON(!ioapic);
105
106 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
107 dest_id, dest_mode);
108 /* IOAPIC delivery mode value is the same as MSI here */
109 switch (delivery_mode) {
110 case IOAPIC_LOWEST_PRIORITY:
111 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
112 deliver_bitmask);
113 if (vcpu != NULL)
114 kvm_apic_set_irq(vcpu, vector, trig_mode);
115 else
116 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
117 break;
118 case IOAPIC_FIXED:
119 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
120 if (!(deliver_bitmask & (1 << vcpu_id)))
121 continue;
122 deliver_bitmask &= ~(1 << vcpu_id);
123 vcpu = ioapic->kvm->vcpus[vcpu_id];
124 if (vcpu)
125 kvm_apic_set_irq(vcpu, vector, trig_mode);
126 }
127 break;
128 default:
129 printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
130 }
131}
132#else
133static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
134#endif
135
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800136static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
137 int assigned_dev_id)
138{
139 struct list_head *ptr;
140 struct kvm_assigned_dev_kernel *match;
141
142 list_for_each(ptr, head) {
143 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
144 if (match->assigned_dev_id == assigned_dev_id)
145 return match;
146 }
147 return NULL;
148}
149
150static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
151{
152 struct kvm_assigned_dev_kernel *assigned_dev;
153
154 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
155 interrupt_work);
156
157 /* This is taken to safely inject irq inside the guest. When
158 * the interrupt injection (or the ioapic code) uses a
159 * finer-grained lock, update this
160 */
161 mutex_lock(&assigned_dev->kvm->lock);
162 kvm_set_irq(assigned_dev->kvm,
Sheng Yang5550af42008-10-15 20:15:06 +0800163 assigned_dev->irq_source_id,
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800164 assigned_dev->guest_irq, 1);
165 mutex_unlock(&assigned_dev->kvm->lock);
166 kvm_put_kvm(assigned_dev->kvm);
167}
168
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800169static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
170{
171 struct kvm_assigned_dev_kernel *assigned_dev =
172 (struct kvm_assigned_dev_kernel *) dev_id;
173
174 kvm_get_kvm(assigned_dev->kvm);
175 schedule_work(&assigned_dev->interrupt_work);
176 disable_irq_nosync(irq);
177 return IRQ_HANDLED;
178}
179
180/* Ack the irq line for an assigned device */
181static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
182{
183 struct kvm_assigned_dev_kernel *dev;
184
185 if (kian->gsi == -1)
186 return;
187
188 dev = container_of(kian, struct kvm_assigned_dev_kernel,
189 ack_notifier);
Sheng Yang5550af42008-10-15 20:15:06 +0800190 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800191 enable_irq(dev->host_irq);
192}
193
194static void kvm_free_assigned_device(struct kvm *kvm,
195 struct kvm_assigned_dev_kernel
196 *assigned_dev)
197{
Sheng Yang4f906c12008-11-24 14:32:51 +0800198 if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested_type)
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800199 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
200
Sheng Yange19e30e2008-10-20 16:07:10 +0800201 kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
Sheng Yang5550af42008-10-15 20:15:06 +0800202 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800203
204 if (cancel_work_sync(&assigned_dev->interrupt_work))
205 /* We had pending work. That means we will have to take
206 * care of kvm_put_kvm.
207 */
208 kvm_put_kvm(kvm);
209
Sheng Yang6eb55812008-10-31 12:37:41 +0800210 pci_reset_function(assigned_dev->dev);
211
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800212 pci_release_regions(assigned_dev->dev);
213 pci_disable_device(assigned_dev->dev);
214 pci_dev_put(assigned_dev->dev);
215
216 list_del(&assigned_dev->list);
217 kfree(assigned_dev);
218}
219
220void kvm_free_all_assigned_devices(struct kvm *kvm)
221{
222 struct list_head *ptr, *ptr2;
223 struct kvm_assigned_dev_kernel *assigned_dev;
224
225 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
226 assigned_dev = list_entry(ptr,
227 struct kvm_assigned_dev_kernel,
228 list);
229
230 kvm_free_assigned_device(kvm, assigned_dev);
231 }
232}
233
Sheng Yang00e3ed32008-11-24 14:32:50 +0800234static int assigned_device_update_intx(struct kvm *kvm,
235 struct kvm_assigned_dev_kernel *adev,
236 struct kvm_assigned_irq *airq)
237{
Sheng Yangfbac7812008-11-24 14:32:52 +0800238 adev->guest_irq = airq->guest_irq;
239 adev->ack_notifier.gsi = airq->guest_irq;
240
241 if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
Sheng Yang00e3ed32008-11-24 14:32:50 +0800242 return 0;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800243
244 if (irqchip_in_kernel(kvm)) {
245 if (!capable(CAP_SYS_RAWIO))
246 return -EPERM;
247
248 if (airq->host_irq)
249 adev->host_irq = airq->host_irq;
250 else
251 adev->host_irq = adev->dev->irq;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800252
253 /* Even though this is PCI, we don't want to use shared
254 * interrupts. Sharing host devices with guest-assigned devices
255 * on the same interrupt line is not a happy situation: there
256 * are going to be long delays in accepting, acking, etc.
257 */
258 if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
259 0, "kvm_assigned_intx_device", (void *)adev))
260 return -EIO;
261 }
262
Sheng Yang4f906c12008-11-24 14:32:51 +0800263 adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
264 KVM_ASSIGNED_DEV_HOST_INTX;
Sheng Yang00e3ed32008-11-24 14:32:50 +0800265 return 0;
266}
267
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800268static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
269 struct kvm_assigned_irq
270 *assigned_irq)
271{
272 int r = 0;
273 struct kvm_assigned_dev_kernel *match;
274
275 mutex_lock(&kvm->lock);
276
277 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
278 assigned_irq->assigned_dev_id);
279 if (!match) {
280 mutex_unlock(&kvm->lock);
281 return -EINVAL;
282 }
283
Sheng Yang4f906c12008-11-24 14:32:51 +0800284 if (!match->irq_requested_type) {
Sheng Yang342ffb92008-11-24 14:32:49 +0800285 INIT_WORK(&match->interrupt_work,
286 kvm_assigned_dev_interrupt_work_handler);
287 if (irqchip_in_kernel(kvm)) {
288 /* Register ack nofitier */
289 match->ack_notifier.gsi = -1;
290 match->ack_notifier.irq_acked =
291 kvm_assigned_dev_ack_irq;
292 kvm_register_irq_ack_notifier(kvm,
293 &match->ack_notifier);
294
295 /* Request IRQ source ID */
296 r = kvm_request_irq_source_id(kvm);
297 if (r < 0)
298 goto out_release;
299 else
300 match->irq_source_id = r;
301 }
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800302 }
303
Sheng Yang00e3ed32008-11-24 14:32:50 +0800304 r = assigned_device_update_intx(kvm, match, assigned_irq);
305 if (r)
306 goto out_release;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800307
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800308 mutex_unlock(&kvm->lock);
309 return r;
310out_release:
311 mutex_unlock(&kvm->lock);
312 kvm_free_assigned_device(kvm, match);
313 return r;
314}
315
316static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
317 struct kvm_assigned_pci_dev *assigned_dev)
318{
319 int r = 0;
320 struct kvm_assigned_dev_kernel *match;
321 struct pci_dev *dev;
322
323 mutex_lock(&kvm->lock);
324
325 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
326 assigned_dev->assigned_dev_id);
327 if (match) {
328 /* device already assigned */
329 r = -EINVAL;
330 goto out;
331 }
332
333 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
334 if (match == NULL) {
335 printk(KERN_INFO "%s: Couldn't allocate memory\n",
336 __func__);
337 r = -ENOMEM;
338 goto out;
339 }
340 dev = pci_get_bus_and_slot(assigned_dev->busnr,
341 assigned_dev->devfn);
342 if (!dev) {
343 printk(KERN_INFO "%s: host device not found\n", __func__);
344 r = -EINVAL;
345 goto out_free;
346 }
347 if (pci_enable_device(dev)) {
348 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
349 r = -EBUSY;
350 goto out_put;
351 }
352 r = pci_request_regions(dev, "kvm_assigned_device");
353 if (r) {
354 printk(KERN_INFO "%s: Could not get access to device regions\n",
355 __func__);
356 goto out_disable;
357 }
Sheng Yang6eb55812008-10-31 12:37:41 +0800358
359 pci_reset_function(dev);
360
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800361 match->assigned_dev_id = assigned_dev->assigned_dev_id;
362 match->host_busnr = assigned_dev->busnr;
363 match->host_devfn = assigned_dev->devfn;
364 match->dev = dev;
365
366 match->kvm = kvm;
367
368 list_add(&match->list, &kvm->arch.assigned_dev_head);
369
370 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
371 r = kvm_iommu_map_guest(kvm, match);
372 if (r)
373 goto out_list_del;
374 }
375
376out:
377 mutex_unlock(&kvm->lock);
378 return r;
379out_list_del:
380 list_del(&match->list);
381 pci_release_regions(dev);
382out_disable:
383 pci_disable_device(dev);
384out_put:
385 pci_dev_put(dev);
386out_free:
387 kfree(match);
388 mutex_unlock(&kvm->lock);
389 return r;
390}
391#endif
392
James Morris5aacf0c2006-12-22 01:04:55 -0800393static inline int valid_vcpu(int n)
394{
395 return likely(n >= 0 && n < KVM_MAX_VCPUS);
396}
397
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +0800398inline int kvm_is_mmio_pfn(pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300399{
400 if (pfn_valid(pfn))
401 return PageReserved(pfn_to_page(pfn));
402
403 return true;
404}
405
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406/*
407 * Switches to specified vcpu, until a matching vcpu_put()
408 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200409void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410{
Avi Kivity15ad7142007-07-11 18:17:21 +0300411 int cpu;
412
Avi Kivitybccf2152007-02-21 18:04:26 +0200413 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300414 cpu = get_cpu();
415 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200416 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300417 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200418}
419
Carsten Otte313a3dc2007-10-11 19:16:52 +0200420void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421{
Avi Kivity15ad7142007-07-11 18:17:21 +0300422 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200423 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300424 preempt_notifier_unregister(&vcpu->preempt_notifier);
425 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 mutex_unlock(&vcpu->mutex);
427}
428
Avi Kivityd9e368d2007-06-07 19:18:30 +0300429static void ack_flush(void *_completed)
430{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300431}
432
433void kvm_flush_remote_tlbs(struct kvm *kvm)
434{
Avi Kivity597a5f52008-07-20 14:24:22 +0300435 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300436 cpumask_t cpus;
437 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300438
Avi Kivity597a5f52008-07-20 14:24:22 +0300439 me = get_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300440 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000441 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
442 vcpu = kvm->vcpus[i];
443 if (!vcpu)
444 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200445 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300446 continue;
447 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300448 if (cpu != -1 && cpu != me)
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200449 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300450 }
Avi Kivity0f74a242007-11-20 23:01:14 +0200451 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300452 goto out;
Avi Kivity0f74a242007-11-20 23:01:14 +0200453 ++kvm->stat.remote_tlb_flush;
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200454 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300455out:
456 put_cpu();
Avi Kivityd9e368d2007-06-07 19:18:30 +0300457}
458
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500459void kvm_reload_remote_mmus(struct kvm *kvm)
460{
Avi Kivity597a5f52008-07-20 14:24:22 +0300461 int i, cpu, me;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500462 cpumask_t cpus;
463 struct kvm_vcpu *vcpu;
464
Avi Kivity597a5f52008-07-20 14:24:22 +0300465 me = get_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500466 cpus_clear(cpus);
467 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
468 vcpu = kvm->vcpus[i];
469 if (!vcpu)
470 continue;
471 if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
472 continue;
473 cpu = vcpu->cpu;
Avi Kivity597a5f52008-07-20 14:24:22 +0300474 if (cpu != -1 && cpu != me)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500475 cpu_set(cpu, cpus);
476 }
477 if (cpus_empty(cpus))
Avi Kivity597a5f52008-07-20 14:24:22 +0300478 goto out;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500479 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivity597a5f52008-07-20 14:24:22 +0300480out:
481 put_cpu();
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500482}
483
484
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000485int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
486{
487 struct page *page;
488 int r;
489
490 mutex_init(&vcpu->mutex);
491 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000492 vcpu->kvm = kvm;
493 vcpu->vcpu_id = id;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300494 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000495
496 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
497 if (!page) {
498 r = -ENOMEM;
499 goto fail;
500 }
501 vcpu->run = page_address(page);
502
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800503 r = kvm_arch_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000504 if (r < 0)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800505 goto fail_free_run;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000506 return 0;
507
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000508fail_free_run:
509 free_page((unsigned long)vcpu->run);
510fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000511 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000512}
513EXPORT_SYMBOL_GPL(kvm_vcpu_init);
514
515void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
516{
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800517 kvm_arch_vcpu_uninit(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000518 free_page((unsigned long)vcpu->run);
519}
520EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
521
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200522#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
523static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
524{
525 return container_of(mn, struct kvm, mmu_notifier);
526}
527
528static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
529 struct mm_struct *mm,
530 unsigned long address)
531{
532 struct kvm *kvm = mmu_notifier_to_kvm(mn);
533 int need_tlb_flush;
534
535 /*
536 * When ->invalidate_page runs, the linux pte has been zapped
537 * already but the page is still allocated until
538 * ->invalidate_page returns. So if we increase the sequence
539 * here the kvm page fault will notice if the spte can't be
540 * established because the page is going to be freed. If
541 * instead the kvm page fault establishes the spte before
542 * ->invalidate_page runs, kvm_unmap_hva will release it
543 * before returning.
544 *
545 * The sequence increase only need to be seen at spin_unlock
546 * time, and not at spin_lock time.
547 *
548 * Increasing the sequence after the spin_unlock would be
549 * unsafe because the kvm page fault could then establish the
550 * pte after kvm_unmap_hva returned, without noticing the page
551 * is going to be freed.
552 */
553 spin_lock(&kvm->mmu_lock);
554 kvm->mmu_notifier_seq++;
555 need_tlb_flush = kvm_unmap_hva(kvm, address);
556 spin_unlock(&kvm->mmu_lock);
557
558 /* we've to flush the tlb before the pages can be freed */
559 if (need_tlb_flush)
560 kvm_flush_remote_tlbs(kvm);
561
562}
563
564static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
565 struct mm_struct *mm,
566 unsigned long start,
567 unsigned long end)
568{
569 struct kvm *kvm = mmu_notifier_to_kvm(mn);
570 int need_tlb_flush = 0;
571
572 spin_lock(&kvm->mmu_lock);
573 /*
574 * The count increase must become visible at unlock time as no
575 * spte can be established without taking the mmu_lock and
576 * count is also read inside the mmu_lock critical section.
577 */
578 kvm->mmu_notifier_count++;
579 for (; start < end; start += PAGE_SIZE)
580 need_tlb_flush |= kvm_unmap_hva(kvm, start);
581 spin_unlock(&kvm->mmu_lock);
582
583 /* we've to flush the tlb before the pages can be freed */
584 if (need_tlb_flush)
585 kvm_flush_remote_tlbs(kvm);
586}
587
588static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
589 struct mm_struct *mm,
590 unsigned long start,
591 unsigned long end)
592{
593 struct kvm *kvm = mmu_notifier_to_kvm(mn);
594
595 spin_lock(&kvm->mmu_lock);
596 /*
597 * This sequence increase will notify the kvm page fault that
598 * the page that is going to be mapped in the spte could have
599 * been freed.
600 */
601 kvm->mmu_notifier_seq++;
602 /*
603 * The above sequence increase must be visible before the
604 * below count decrease but both values are read by the kvm
605 * page fault under mmu_lock spinlock so we don't need to add
606 * a smb_wmb() here in between the two.
607 */
608 kvm->mmu_notifier_count--;
609 spin_unlock(&kvm->mmu_lock);
610
611 BUG_ON(kvm->mmu_notifier_count < 0);
612}
613
614static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
615 struct mm_struct *mm,
616 unsigned long address)
617{
618 struct kvm *kvm = mmu_notifier_to_kvm(mn);
619 int young;
620
621 spin_lock(&kvm->mmu_lock);
622 young = kvm_age_hva(kvm, address);
623 spin_unlock(&kvm->mmu_lock);
624
625 if (young)
626 kvm_flush_remote_tlbs(kvm);
627
628 return young;
629}
630
631static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
632 .invalidate_page = kvm_mmu_notifier_invalidate_page,
633 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
634 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
635 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
636};
637#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
638
Avi Kivityf17abe92007-02-21 19:28:04 +0200639static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640{
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800641 struct kvm *kvm = kvm_arch_create_vm();
Laurent Vivier5f94c172008-05-30 16:05:54 +0200642#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
643 struct page *page;
644#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800646 if (IS_ERR(kvm))
647 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648
Laurent Vivier5f94c172008-05-30 16:05:54 +0200649#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
650 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
651 if (!page) {
652 kfree(kvm);
653 return ERR_PTR(-ENOMEM);
654 }
655 kvm->coalesced_mmio_ring =
656 (struct kvm_coalesced_mmio_ring *)page_address(page);
657#endif
658
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200659#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
660 {
661 int err;
662 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
663 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
664 if (err) {
665#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
666 put_page(page);
667#endif
668 kfree(kvm);
669 return ERR_PTR(err);
670 }
671 }
672#endif
673
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200674 kvm->mm = current->mm;
675 atomic_inc(&kvm->mm->mm_count);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500676 spin_lock_init(&kvm->mmu_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300677 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800678 mutex_init(&kvm->lock);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400679 kvm_io_bus_init(&kvm->mmio_bus);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200680 init_rwsem(&kvm->slots_lock);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300681 atomic_set(&kvm->users_count, 1);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000682 spin_lock(&kvm_lock);
683 list_add(&kvm->vm_list, &vm_list);
684 spin_unlock(&kvm_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200685#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
686 kvm_coalesced_mmio_init(kvm);
687#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800688out:
Avi Kivityf17abe92007-02-21 19:28:04 +0200689 return kvm;
690}
691
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692/*
693 * Free any memory in @free but not in @dont.
694 */
695static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
696 struct kvm_memory_slot *dont)
697{
Izik Eidus290fc382007-09-27 14:11:22 +0200698 if (!dont || free->rmap != dont->rmap)
699 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700
701 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
702 vfree(free->dirty_bitmap);
703
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300704 if (!dont || free->lpage_info != dont->lpage_info)
705 vfree(free->lpage_info);
706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000708 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500709 free->rmap = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300710 free->lpage_info = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711}
712
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800713void kvm_free_physmem(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714{
715 int i;
716
717 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000718 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719}
720
Avi Kivityf17abe92007-02-21 19:28:04 +0200721static void kvm_destroy_vm(struct kvm *kvm)
722{
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200723 struct mm_struct *mm = kvm->mm;
724
Avi Kivity133de902007-02-12 00:54:44 -0800725 spin_lock(&kvm_lock);
726 list_del(&kvm->vm_list);
727 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300728 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400729 kvm_io_bus_destroy(&kvm->mmio_bus);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200730#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
731 if (kvm->coalesced_mmio_ring != NULL)
732 free_page((unsigned long)kvm->coalesced_mmio_ring);
733#endif
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200734#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
735 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
736#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800737 kvm_arch_destroy_vm(kvm);
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200738 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200739}
740
Izik Eidusd39f13b2008-03-30 16:01:25 +0300741void kvm_get_kvm(struct kvm *kvm)
742{
743 atomic_inc(&kvm->users_count);
744}
745EXPORT_SYMBOL_GPL(kvm_get_kvm);
746
747void kvm_put_kvm(struct kvm *kvm)
748{
749 if (atomic_dec_and_test(&kvm->users_count))
750 kvm_destroy_vm(kvm);
751}
752EXPORT_SYMBOL_GPL(kvm_put_kvm);
753
754
Avi Kivityf17abe92007-02-21 19:28:04 +0200755static int kvm_vm_release(struct inode *inode, struct file *filp)
756{
757 struct kvm *kvm = filp->private_data;
758
Izik Eidusd39f13b2008-03-30 16:01:25 +0300759 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 return 0;
761}
762
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 * Allocate some memory and give it an address in the guest physical address
765 * space.
766 *
767 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800768 *
Marcelo Tosatti10589a42007-12-20 19:18:22 -0500769 * Must be called holding mmap_sem for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800771int __kvm_set_memory_region(struct kvm *kvm,
772 struct kvm_userspace_memory_region *mem,
773 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774{
775 int r;
776 gfn_t base_gfn;
777 unsigned long npages;
778 unsigned long i;
779 struct kvm_memory_slot *memslot;
780 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781
782 r = -EINVAL;
783 /* General sanity checks */
784 if (mem->memory_size & (PAGE_SIZE - 1))
785 goto out;
786 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
787 goto out;
Sheng Yange7cacd42008-11-11 15:30:40 +0800788 if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
Hollis Blanchard78749802008-11-07 13:32:12 -0600789 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200790 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 goto out;
792 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
793 goto out;
794
795 memslot = &kvm->memslots[mem->slot];
796 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
797 npages = mem->memory_size >> PAGE_SHIFT;
798
799 if (!npages)
800 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
801
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 new = old = *memslot;
803
804 new.base_gfn = base_gfn;
805 new.npages = npages;
806 new.flags = mem->flags;
807
808 /* Disallow changing a memory slot's size. */
809 r = -EINVAL;
810 if (npages && old.npages && npages != old.npages)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800811 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
813 /* Check for overlaps */
814 r = -EEXIST;
815 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
816 struct kvm_memory_slot *s = &kvm->memslots[i];
817
818 if (s == memslot)
819 continue;
820 if (!((base_gfn + npages <= s->base_gfn) ||
821 (base_gfn >= s->base_gfn + s->npages)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800822 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 /* Free page dirty bitmap if unneeded */
826 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000827 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828
829 r = -ENOMEM;
830
831 /* Allocate if a slot is being created */
Carsten Otteeff01142008-06-27 15:05:31 +0200832#ifndef CONFIG_S390
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500833 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400834 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200835
836 if (!new.rmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800837 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200838
Izik Eidus290fc382007-09-27 14:11:22 +0200839 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500840
Izik Eidus80b14b52007-10-25 11:54:04 +0200841 new.user_alloc = user_alloc;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200842 /*
843 * hva_to_rmmap() serialzies with the mmu_lock and to be
844 * safe it has to ignore memslots with !user_alloc &&
845 * !userspace_addr.
846 */
847 if (user_alloc)
848 new.userspace_addr = mem->userspace_addr;
849 else
850 new.userspace_addr = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300852 if (npages && !new.lpage_info) {
853 int largepages = npages / KVM_PAGES_PER_HPAGE;
854 if (npages % KVM_PAGES_PER_HPAGE)
855 largepages++;
856 if (base_gfn % KVM_PAGES_PER_HPAGE)
857 largepages++;
858
859 new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
860
861 if (!new.lpage_info)
862 goto out_free;
863
864 memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
865
866 if (base_gfn % KVM_PAGES_PER_HPAGE)
867 new.lpage_info[0].write_count = 1;
868 if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
869 new.lpage_info[largepages-1].write_count = 1;
870 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871
872 /* Allocate page dirty bitmap if needed */
873 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
874 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
875
876 new.dirty_bitmap = vmalloc(dirty_bytes);
877 if (!new.dirty_bitmap)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800878 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 memset(new.dirty_bitmap, 0, dirty_bytes);
880 }
Carsten Otteeff01142008-06-27 15:05:31 +0200881#endif /* not defined CONFIG_S390 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300883 if (!npages)
884 kvm_arch_flush_shadow(kvm);
885
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200886 spin_lock(&kvm->mmu_lock);
887 if (mem->slot >= kvm->nmemslots)
888 kvm->nmemslots = mem->slot + 1;
889
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 *memslot = new;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200891 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892
Zhang Xiantao0de10342007-11-20 16:25:04 +0800893 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
894 if (r) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200895 spin_lock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800896 *memslot = old;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +0200897 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +0800898 goto out_free;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +0800899 }
900
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 kvm_free_physmem_slot(&old, &new);
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800902#ifdef CONFIG_DMAR
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300903 /* map the pages in iommu page table */
904 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
905 if (r)
906 goto out;
Xiantao Zhang8a98f662008-10-06 13:47:38 +0800907#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 return 0;
909
Sheng Yangf78e0e22007-10-29 09:40:42 +0800910out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 kvm_free_physmem_slot(&new, &old);
912out:
913 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200914
915}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800916EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
917
918int kvm_set_memory_region(struct kvm *kvm,
919 struct kvm_userspace_memory_region *mem,
920 int user_alloc)
921{
922 int r;
923
Izik Eidus72dc67a2008-02-10 18:04:15 +0200924 down_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800925 r = __kvm_set_memory_region(kvm, mem, user_alloc);
Izik Eidus72dc67a2008-02-10 18:04:15 +0200926 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800927 return r;
928}
Izik Eidus210c7c42007-10-24 23:52:57 +0200929EXPORT_SYMBOL_GPL(kvm_set_memory_region);
930
Carsten Otte1fe779f2007-10-29 16:08:35 +0100931int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
932 struct
933 kvm_userspace_memory_region *mem,
934 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200935{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200936 if (mem->slot >= KVM_MEMORY_SLOTS)
937 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200938 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939}
940
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800941int kvm_get_dirty_log(struct kvm *kvm,
942 struct kvm_dirty_log *log, int *is_dirty)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943{
944 struct kvm_memory_slot *memslot;
945 int r, i;
946 int n;
947 unsigned long any = 0;
948
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 r = -EINVAL;
950 if (log->slot >= KVM_MEMORY_SLOTS)
951 goto out;
952
953 memslot = &kvm->memslots[log->slot];
954 r = -ENOENT;
955 if (!memslot->dirty_bitmap)
956 goto out;
957
Uri Lublincd1a4a92007-02-22 16:43:09 +0200958 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959
Uri Lublincd1a4a92007-02-22 16:43:09 +0200960 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 any = memslot->dirty_bitmap[i];
962
963 r = -EFAULT;
964 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
965 goto out;
966
Zhang Xiantao5bb064d2007-11-18 20:29:43 +0800967 if (any)
968 *is_dirty = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
970 r = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 return r;
973}
974
Izik Eiduscea7bb22007-10-17 19:17:48 +0200975int is_error_page(struct page *page)
976{
977 return page == bad_page;
978}
979EXPORT_SYMBOL_GPL(is_error_page);
980
Anthony Liguori35149e22008-04-02 14:46:56 -0500981int is_error_pfn(pfn_t pfn)
982{
983 return pfn == bad_pfn;
984}
985EXPORT_SYMBOL_GPL(is_error_pfn);
986
Izik Eidusf9d46eb2007-11-11 22:02:22 +0200987static inline unsigned long bad_hva(void)
988{
989 return PAGE_OFFSET;
990}
991
992int kvm_is_error_hva(unsigned long addr)
993{
994 return addr == bad_hva();
995}
996EXPORT_SYMBOL_GPL(kvm_is_error_hva);
997
Izik Eidus28430992008-10-03 17:40:32 +0300998struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999{
1000 int i;
1001
1002 for (i = 0; i < kvm->nmemslots; ++i) {
1003 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1004
1005 if (gfn >= memslot->base_gfn
1006 && gfn < memslot->base_gfn + memslot->npages)
1007 return memslot;
1008 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001009 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010}
Izik Eidus28430992008-10-03 17:40:32 +03001011EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
Avi Kivitye8207542007-03-30 16:54:30 +03001012
1013struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1014{
1015 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001016 return gfn_to_memslot_unaliased(kvm, gfn);
Avi Kivitye8207542007-03-30 16:54:30 +03001017}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018
Izik Eiduse0d62c72007-10-24 23:57:46 +02001019int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1020{
1021 int i;
1022
1023 gfn = unalias_gfn(kvm, gfn);
1024 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1025 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1026
1027 if (gfn >= memslot->base_gfn
1028 && gfn < memslot->base_gfn + memslot->npages)
1029 return 1;
1030 }
1031 return 0;
1032}
1033EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1034
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001035unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
Izik Eidus539cb662007-11-11 22:05:04 +02001036{
1037 struct kvm_memory_slot *slot;
1038
1039 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001040 slot = gfn_to_memslot_unaliased(kvm, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001041 if (!slot)
1042 return bad_hva();
1043 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1044}
Sheng Yang0d150292008-04-25 21:44:50 +08001045EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001046
Anthony Liguori35149e22008-04-02 14:46:56 -05001047pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001048{
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001049 struct page *page[1];
Izik Eidus539cb662007-11-11 22:05:04 +02001050 unsigned long addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001051 int npages;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001052 pfn_t pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001053
Avi Kivity60395222007-10-21 11:03:36 +02001054 might_sleep();
1055
Izik Eidus539cb662007-11-11 22:05:04 +02001056 addr = gfn_to_hva(kvm, gfn);
1057 if (kvm_is_error_hva(addr)) {
Izik Eidus8a7ae052007-10-18 11:09:33 +02001058 get_page(bad_page);
Anthony Liguori35149e22008-04-02 14:46:56 -05001059 return page_to_pfn(bad_page);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001060 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001061
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001062 npages = get_user_pages_fast(addr, 1, 1, page);
Izik Eidus539cb662007-11-11 22:05:04 +02001063
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001064 if (unlikely(npages != 1)) {
1065 struct vm_area_struct *vma;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001066
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001067 down_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001068 vma = find_vma(current->mm, addr);
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001069
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001070 if (vma == NULL || addr < vma->vm_start ||
1071 !(vma->vm_flags & VM_PFNMAP)) {
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001072 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001073 get_page(bad_page);
1074 return page_to_pfn(bad_page);
1075 }
1076
1077 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001078 up_read(&current->mm->mmap_sem);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001079 BUG_ON(!kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001080 } else
1081 pfn = page_to_pfn(page[0]);
1082
1083 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001084}
1085
1086EXPORT_SYMBOL_GPL(gfn_to_pfn);
1087
1088struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1089{
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001090 pfn_t pfn;
1091
1092 pfn = gfn_to_pfn(kvm, gfn);
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001093 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001094 return pfn_to_page(pfn);
1095
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001096 WARN_ON(kvm_is_mmio_pfn(pfn));
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001097
1098 get_page(bad_page);
1099 return bad_page;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001100}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05001101
Avi Kivity954bbbc2007-03-30 14:02:32 +03001102EXPORT_SYMBOL_GPL(gfn_to_page);
1103
Izik Eidusb4231d62007-11-20 11:49:33 +02001104void kvm_release_page_clean(struct page *page)
1105{
Anthony Liguori35149e22008-04-02 14:46:56 -05001106 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02001107}
1108EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1109
Anthony Liguori35149e22008-04-02 14:46:56 -05001110void kvm_release_pfn_clean(pfn_t pfn)
1111{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001112 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001113 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001114}
1115EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1116
Izik Eidusb4231d62007-11-20 11:49:33 +02001117void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02001118{
Anthony Liguori35149e22008-04-02 14:46:56 -05001119 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02001120}
Izik Eidusb4231d62007-11-20 11:49:33 +02001121EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001122
Anthony Liguori35149e22008-04-02 14:46:56 -05001123void kvm_release_pfn_dirty(pfn_t pfn)
1124{
1125 kvm_set_pfn_dirty(pfn);
1126 kvm_release_pfn_clean(pfn);
1127}
1128EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1129
1130void kvm_set_page_dirty(struct page *page)
1131{
1132 kvm_set_pfn_dirty(page_to_pfn(page));
1133}
1134EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1135
1136void kvm_set_pfn_dirty(pfn_t pfn)
1137{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001138 if (!kvm_is_mmio_pfn(pfn)) {
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001139 struct page *page = pfn_to_page(pfn);
1140 if (!PageReserved(page))
1141 SetPageDirty(page);
1142 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001143}
1144EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1145
1146void kvm_set_pfn_accessed(pfn_t pfn)
1147{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001148 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001149 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001150}
1151EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1152
1153void kvm_get_pfn(pfn_t pfn)
1154{
Xiantao Zhangc77fb9d2008-09-27 10:55:40 +08001155 if (!kvm_is_mmio_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001156 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05001157}
1158EXPORT_SYMBOL_GPL(kvm_get_pfn);
1159
Izik Eidus195aefd2007-10-01 22:14:18 +02001160static int next_segment(unsigned long len, int offset)
1161{
1162 if (len > PAGE_SIZE - offset)
1163 return PAGE_SIZE - offset;
1164 else
1165 return len;
1166}
1167
1168int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1169 int len)
1170{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001171 int r;
1172 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001173
Izik Eiduse0506bc2007-11-11 22:10:22 +02001174 addr = gfn_to_hva(kvm, gfn);
1175 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001176 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001177 r = copy_from_user(data, (void __user *)addr + offset, len);
1178 if (r)
1179 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001180 return 0;
1181}
1182EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1183
1184int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1185{
1186 gfn_t gfn = gpa >> PAGE_SHIFT;
1187 int seg;
1188 int offset = offset_in_page(gpa);
1189 int ret;
1190
1191 while ((seg = next_segment(len, offset)) != 0) {
1192 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1193 if (ret < 0)
1194 return ret;
1195 offset = 0;
1196 len -= seg;
1197 data += seg;
1198 ++gfn;
1199 }
1200 return 0;
1201}
1202EXPORT_SYMBOL_GPL(kvm_read_guest);
1203
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001204int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1205 unsigned long len)
1206{
1207 int r;
1208 unsigned long addr;
1209 gfn_t gfn = gpa >> PAGE_SHIFT;
1210 int offset = offset_in_page(gpa);
1211
1212 addr = gfn_to_hva(kvm, gfn);
1213 if (kvm_is_error_hva(addr))
1214 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001215 pagefault_disable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001216 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01001217 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05001218 if (r)
1219 return -EFAULT;
1220 return 0;
1221}
1222EXPORT_SYMBOL(kvm_read_guest_atomic);
1223
Izik Eidus195aefd2007-10-01 22:14:18 +02001224int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1225 int offset, int len)
1226{
Izik Eiduse0506bc2007-11-11 22:10:22 +02001227 int r;
1228 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02001229
Izik Eiduse0506bc2007-11-11 22:10:22 +02001230 addr = gfn_to_hva(kvm, gfn);
1231 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02001232 return -EFAULT;
Izik Eiduse0506bc2007-11-11 22:10:22 +02001233 r = copy_to_user((void __user *)addr + offset, data, len);
1234 if (r)
1235 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001236 mark_page_dirty(kvm, gfn);
1237 return 0;
1238}
1239EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1240
1241int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1242 unsigned long len)
1243{
1244 gfn_t gfn = gpa >> PAGE_SHIFT;
1245 int seg;
1246 int offset = offset_in_page(gpa);
1247 int ret;
1248
1249 while ((seg = next_segment(len, offset)) != 0) {
1250 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1251 if (ret < 0)
1252 return ret;
1253 offset = 0;
1254 len -= seg;
1255 data += seg;
1256 ++gfn;
1257 }
1258 return 0;
1259}
1260
1261int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1262{
Izik Eidus3e021bf2007-11-19 11:16:57 +02001263 return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02001264}
1265EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1266
1267int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1268{
1269 gfn_t gfn = gpa >> PAGE_SHIFT;
1270 int seg;
1271 int offset = offset_in_page(gpa);
1272 int ret;
1273
1274 while ((seg = next_segment(len, offset)) != 0) {
1275 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1276 if (ret < 0)
1277 return ret;
1278 offset = 0;
1279 len -= seg;
1280 ++gfn;
1281 }
1282 return 0;
1283}
1284EXPORT_SYMBOL_GPL(kvm_clear_guest);
1285
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1287{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001288 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289
Uri Lublin3b6fff12007-10-30 10:42:09 +02001290 gfn = unalias_gfn(kvm, gfn);
Izik Eidus28430992008-10-03 17:40:32 +03001291 memslot = gfn_to_memslot_unaliased(kvm, gfn);
Rusty Russell7e9d6192007-07-31 20:41:14 +10001292 if (memslot && memslot->dirty_bitmap) {
1293 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294
Rusty Russell7e9d6192007-07-31 20:41:14 +10001295 /* avoid RMW */
1296 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1297 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298 }
1299}
1300
Eddie Dongb6958ce2007-07-18 12:15:21 +03001301/*
1302 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1303 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05001304void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001305{
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001306 DEFINE_WAIT(wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001307
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001308 for (;;) {
1309 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001310
Marcelo Tosattid7690172008-09-08 15:23:48 -03001311 if (kvm_cpu_has_interrupt(vcpu) ||
1312 kvm_cpu_has_pending_timer(vcpu) ||
1313 kvm_arch_vcpu_runnable(vcpu)) {
1314 set_bit(KVM_REQ_UNHALT, &vcpu->requests);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001315 break;
Marcelo Tosattid7690172008-09-08 15:23:48 -03001316 }
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001317 if (signal_pending(current))
1318 break;
1319
Eddie Dongb6958ce2007-07-18 12:15:21 +03001320 vcpu_put(vcpu);
1321 schedule();
1322 vcpu_load(vcpu);
1323 }
1324
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03001325 finish_wait(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001326}
1327
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328void kvm_resched(struct kvm_vcpu *vcpu)
1329{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001330 if (!need_resched())
1331 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334EXPORT_SYMBOL_GPL(kvm_resched);
1335
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001336static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001337{
1338 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001339 struct page *page;
1340
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001341 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02001342 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02001343#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001344 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001345 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02001346#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001347#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1348 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1349 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1350#endif
Avi Kivity039576c2007-03-20 12:46:50 +02001351 else
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001352 return VM_FAULT_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001353 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001354 vmf->page = page;
1355 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001356}
1357
1358static struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001359 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001360};
1361
1362static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1363{
1364 vma->vm_ops = &kvm_vcpu_vm_ops;
1365 return 0;
1366}
1367
Avi Kivitybccf2152007-02-21 18:04:26 +02001368static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1369{
1370 struct kvm_vcpu *vcpu = filp->private_data;
1371
Al Viro66c0b392008-04-19 20:33:56 +01001372 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001373 return 0;
1374}
1375
Jan Engelhardt5c502742008-01-22 20:46:14 +01001376static const struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02001377 .release = kvm_vcpu_release,
1378 .unlocked_ioctl = kvm_vcpu_ioctl,
1379 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001380 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02001381};
1382
1383/*
1384 * Allocates an inode for the vcpu.
1385 */
1386static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1387{
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001388 int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
Al Viro2030a422008-02-23 06:46:49 -05001389 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001390 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001391 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02001392}
1393
Avi Kivityc5ea7662007-02-20 18:41:05 +02001394/*
1395 * Creates some virtual cpus. Good luck creating more than one.
1396 */
1397static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1398{
1399 int r;
1400 struct kvm_vcpu *vcpu;
1401
Avi Kivityc5ea7662007-02-20 18:41:05 +02001402 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001403 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001404
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001405 vcpu = kvm_arch_vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001406 if (IS_ERR(vcpu))
1407 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001408
Avi Kivity15ad7142007-07-11 18:17:21 +03001409 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1410
Avi Kivity26e52152007-11-20 15:30:24 +02001411 r = kvm_arch_vcpu_setup(vcpu);
1412 if (r)
Glauber Costa7d8fece2008-09-17 23:16:59 -03001413 return r;
Avi Kivity26e52152007-11-20 15:30:24 +02001414
Shaohua Li11ec2802007-07-23 14:51:37 +08001415 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001416 if (kvm->vcpus[n]) {
1417 r = -EEXIST;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001418 goto vcpu_destroy;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001419 }
1420 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08001421 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001422
1423 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01001424 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02001425 r = create_vcpu_fd(vcpu);
1426 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001427 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02001428 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001429
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001430unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08001431 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001432 kvm->vcpus[n] = NULL;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001433vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03001434 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06001435 kvm_arch_vcpu_destroy(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02001436 return r;
1437}
1438
Avi Kivity1961d272007-03-05 19:46:05 +02001439static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1440{
1441 if (sigset) {
1442 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1443 vcpu->sigset_active = 1;
1444 vcpu->sigset = *sigset;
1445 } else
1446 vcpu->sigset_active = 0;
1447 return 0;
1448}
1449
Avi Kivitybccf2152007-02-21 18:04:26 +02001450static long kvm_vcpu_ioctl(struct file *filp,
1451 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452{
Avi Kivitybccf2152007-02-21 18:04:26 +02001453 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00001454 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001455 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001456 struct kvm_fpu *fpu = NULL;
1457 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001459 if (vcpu->kvm->mm != current->mm)
1460 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02001462 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001463 r = -EINVAL;
1464 if (arg)
1465 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001466 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001469 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001471 r = -ENOMEM;
1472 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1473 if (!kvm_regs)
1474 goto out;
1475 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001477 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001479 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1480 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001482out_free1:
1483 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 break;
1485 }
1486 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001487 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001489 r = -ENOMEM;
1490 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1491 if (!kvm_regs)
1492 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001494 if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1495 goto out_free2;
1496 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001498 goto out_free2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08001500out_free2:
1501 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 break;
1503 }
1504 case KVM_GET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001505 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1506 r = -ENOMEM;
1507 if (!kvm_sregs)
1508 goto out;
1509 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 if (r)
1511 goto out;
1512 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001513 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 goto out;
1515 r = 0;
1516 break;
1517 }
1518 case KVM_SET_SREGS: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001519 kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1520 r = -ENOMEM;
1521 if (!kvm_sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001523 r = -EFAULT;
1524 if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1525 goto out;
1526 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 if (r)
1528 goto out;
1529 r = 0;
1530 break;
1531 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001532 case KVM_GET_MP_STATE: {
1533 struct kvm_mp_state mp_state;
1534
1535 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1536 if (r)
1537 goto out;
1538 r = -EFAULT;
1539 if (copy_to_user(argp, &mp_state, sizeof mp_state))
1540 goto out;
1541 r = 0;
1542 break;
1543 }
1544 case KVM_SET_MP_STATE: {
1545 struct kvm_mp_state mp_state;
1546
1547 r = -EFAULT;
1548 if (copy_from_user(&mp_state, argp, sizeof mp_state))
1549 goto out;
1550 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1551 if (r)
1552 goto out;
1553 r = 0;
1554 break;
1555 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556 case KVM_TRANSLATE: {
1557 struct kvm_translation tr;
1558
1559 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001560 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08001562 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563 if (r)
1564 goto out;
1565 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001566 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 goto out;
1568 r = 0;
1569 break;
1570 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 case KVM_DEBUG_GUEST: {
1572 struct kvm_debug_guest dbg;
1573
1574 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001575 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576 goto out;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05001577 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 if (r)
1579 goto out;
1580 r = 0;
1581 break;
1582 }
Avi Kivity1961d272007-03-05 19:46:05 +02001583 case KVM_SET_SIGNAL_MASK: {
1584 struct kvm_signal_mask __user *sigmask_arg = argp;
1585 struct kvm_signal_mask kvm_sigmask;
1586 sigset_t sigset, *p;
1587
1588 p = NULL;
1589 if (argp) {
1590 r = -EFAULT;
1591 if (copy_from_user(&kvm_sigmask, argp,
1592 sizeof kvm_sigmask))
1593 goto out;
1594 r = -EINVAL;
1595 if (kvm_sigmask.len != sizeof sigset)
1596 goto out;
1597 r = -EFAULT;
1598 if (copy_from_user(&sigset, sigmask_arg->sigset,
1599 sizeof sigset))
1600 goto out;
1601 p = &sigset;
1602 }
1603 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1604 break;
1605 }
Avi Kivityb8836732007-04-01 16:34:31 +03001606 case KVM_GET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001607 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1608 r = -ENOMEM;
1609 if (!fpu)
1610 goto out;
1611 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001612 if (r)
1613 goto out;
1614 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001615 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03001616 goto out;
1617 r = 0;
1618 break;
1619 }
1620 case KVM_SET_FPU: {
Dave Hansenfa3795a2008-08-11 10:01:46 -07001621 fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1622 r = -ENOMEM;
1623 if (!fpu)
Avi Kivityb8836732007-04-01 16:34:31 +03001624 goto out;
Dave Hansenfa3795a2008-08-11 10:01:46 -07001625 r = -EFAULT;
1626 if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1627 goto out;
1628 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03001629 if (r)
1630 goto out;
1631 r = 0;
1632 break;
1633 }
Avi Kivitybccf2152007-02-21 18:04:26 +02001634 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02001635 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02001636 }
1637out:
Dave Hansenfa3795a2008-08-11 10:01:46 -07001638 kfree(fpu);
1639 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02001640 return r;
1641}
1642
1643static long kvm_vm_ioctl(struct file *filp,
1644 unsigned int ioctl, unsigned long arg)
1645{
1646 struct kvm *kvm = filp->private_data;
1647 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001648 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02001649
Avi Kivity6d4e4c42007-11-21 16:41:05 +02001650 if (kvm->mm != current->mm)
1651 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02001652 switch (ioctl) {
1653 case KVM_CREATE_VCPU:
1654 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1655 if (r < 0)
1656 goto out;
1657 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02001658 case KVM_SET_USER_MEMORY_REGION: {
1659 struct kvm_userspace_memory_region kvm_userspace_mem;
1660
1661 r = -EFAULT;
1662 if (copy_from_user(&kvm_userspace_mem, argp,
1663 sizeof kvm_userspace_mem))
1664 goto out;
1665
1666 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 if (r)
1668 goto out;
1669 break;
1670 }
1671 case KVM_GET_DIRTY_LOG: {
1672 struct kvm_dirty_log log;
1673
1674 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00001675 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02001677 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678 if (r)
1679 goto out;
1680 break;
1681 }
Laurent Vivier5f94c172008-05-30 16:05:54 +02001682#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1683 case KVM_REGISTER_COALESCED_MMIO: {
1684 struct kvm_coalesced_mmio_zone zone;
1685 r = -EFAULT;
1686 if (copy_from_user(&zone, argp, sizeof zone))
1687 goto out;
1688 r = -ENXIO;
1689 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1690 if (r)
1691 goto out;
1692 r = 0;
1693 break;
1694 }
1695 case KVM_UNREGISTER_COALESCED_MMIO: {
1696 struct kvm_coalesced_mmio_zone zone;
1697 r = -EFAULT;
1698 if (copy_from_user(&zone, argp, sizeof zone))
1699 goto out;
1700 r = -ENXIO;
1701 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1702 if (r)
1703 goto out;
1704 r = 0;
1705 break;
1706 }
1707#endif
Xiantao Zhang8a98f662008-10-06 13:47:38 +08001708#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1709 case KVM_ASSIGN_PCI_DEVICE: {
1710 struct kvm_assigned_pci_dev assigned_dev;
1711
1712 r = -EFAULT;
1713 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1714 goto out;
1715 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1716 if (r)
1717 goto out;
1718 break;
1719 }
1720 case KVM_ASSIGN_IRQ: {
1721 struct kvm_assigned_irq assigned_irq;
1722
1723 r = -EFAULT;
1724 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1725 goto out;
1726 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1727 if (r)
1728 goto out;
1729 break;
1730 }
1731#endif
Avi Kivityf17abe92007-02-21 19:28:04 +02001732 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001733 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02001734 }
1735out:
1736 return r;
1737}
1738
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001739static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Avi Kivityf17abe92007-02-21 19:28:04 +02001740{
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001741 struct page *page[1];
1742 unsigned long addr;
1743 int npages;
1744 gfn_t gfn = vmf->pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02001745 struct kvm *kvm = vma->vm_file->private_data;
Avi Kivityf17abe92007-02-21 19:28:04 +02001746
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001747 addr = gfn_to_hva(kvm, gfn);
1748 if (kvm_is_error_hva(addr))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001749 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001750
1751 npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1752 NULL);
1753 if (unlikely(npages != 1))
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001754 return VM_FAULT_SIGBUS;
Marcelo Tosatti777b3f42008-09-16 20:54:46 -03001755
1756 vmf->page = page[0];
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001757 return 0;
Avi Kivityf17abe92007-02-21 19:28:04 +02001758}
1759
1760static struct vm_operations_struct kvm_vm_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11001761 .fault = kvm_vm_fault,
Avi Kivityf17abe92007-02-21 19:28:04 +02001762};
1763
1764static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1765{
1766 vma->vm_ops = &kvm_vm_vm_ops;
1767 return 0;
1768}
1769
Jan Engelhardt5c502742008-01-22 20:46:14 +01001770static const struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02001771 .release = kvm_vm_release,
1772 .unlocked_ioctl = kvm_vm_ioctl,
1773 .compat_ioctl = kvm_vm_ioctl,
1774 .mmap = kvm_vm_mmap,
1775};
1776
1777static int kvm_dev_ioctl_create_vm(void)
1778{
Al Viro2030a422008-02-23 06:46:49 -05001779 int fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001780 struct kvm *kvm;
1781
Avi Kivityf17abe92007-02-21 19:28:04 +02001782 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04001783 if (IS_ERR(kvm))
1784 return PTR_ERR(kvm);
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -07001785 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
Al Viro2030a422008-02-23 06:46:49 -05001786 if (fd < 0)
Al Viro66c0b392008-04-19 20:33:56 +01001787 kvm_put_kvm(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02001788
Avi Kivityf17abe92007-02-21 19:28:04 +02001789 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02001790}
1791
1792static long kvm_dev_ioctl(struct file *filp,
1793 unsigned int ioctl, unsigned long arg)
1794{
Avi Kivity07c45a32007-03-07 13:05:38 +02001795 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02001796
1797 switch (ioctl) {
1798 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001799 r = -EINVAL;
1800 if (arg)
1801 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001802 r = KVM_API_VERSION;
1803 break;
1804 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02001805 r = -EINVAL;
1806 if (arg)
1807 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02001808 r = kvm_dev_ioctl_create_vm();
1809 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001810 case KVM_CHECK_EXTENSION:
Christian Borntraeger1e1c65e2008-04-21 13:48:24 +02001811 r = kvm_dev_ioctl_check_extension(arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02001812 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02001813 case KVM_GET_VCPU_MMAP_SIZE:
1814 r = -EINVAL;
1815 if (arg)
1816 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02001817 r = PAGE_SIZE; /* struct kvm_run */
1818#ifdef CONFIG_X86
1819 r += PAGE_SIZE; /* pio data page */
1820#endif
Laurent Vivier5f94c172008-05-30 16:05:54 +02001821#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1822 r += PAGE_SIZE; /* coalesced mmio ring page */
1823#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02001824 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04001825 case KVM_TRACE_ENABLE:
1826 case KVM_TRACE_PAUSE:
1827 case KVM_TRACE_DISABLE:
1828 r = kvm_trace_ioctl(ioctl, arg);
1829 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 default:
Carsten Otte043405e2007-10-10 17:16:19 +02001831 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832 }
1833out:
1834 return r;
1835}
1836
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838 .unlocked_ioctl = kvm_dev_ioctl,
1839 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840};
1841
1842static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02001843 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844 "kvm",
1845 &kvm_chardev_ops,
1846};
1847
Avi Kivity1b6c0162007-05-24 13:03:52 +03001848static void hardware_enable(void *junk)
1849{
1850 int cpu = raw_smp_processor_id();
1851
1852 if (cpu_isset(cpu, cpus_hardware_enabled))
1853 return;
1854 cpu_set(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001855 kvm_arch_hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001856}
1857
1858static void hardware_disable(void *junk)
1859{
1860 int cpu = raw_smp_processor_id();
1861
1862 if (!cpu_isset(cpu, cpus_hardware_enabled))
1863 return;
1864 cpu_clear(cpu, cpus_hardware_enabled);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08001865 kvm_arch_hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03001866}
1867
Avi Kivity774c47f2007-02-12 00:54:47 -08001868static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1869 void *v)
1870{
1871 int cpu = (long)v;
1872
Avi Kivity1a6f4d72007-11-11 18:37:32 +02001873 val &= ~CPU_TASKS_FROZEN;
Avi Kivity774c47f2007-02-12 00:54:47 -08001874 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03001875 case CPU_DYING:
Avi Kivity6ec8a852007-08-19 15:57:26 +03001876 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1877 cpu);
1878 hardware_disable(NULL);
1879 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08001880 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02001881 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1882 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001883 smp_call_function_single(cpu, hardware_disable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001884 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02001885 case CPU_ONLINE:
1886 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1887 cpu);
Jens Axboe8691e5a2008-06-06 11:18:06 +02001888 smp_call_function_single(cpu, hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08001889 break;
1890 }
1891 return NOTIFY_OK;
1892}
1893
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001894
1895asmlinkage void kvm_handle_fault_on_reboot(void)
1896{
1897 if (kvm_rebooting)
1898 /* spin while reset goes on */
1899 while (true)
1900 ;
1901 /* Fault while not rebooting. We want the trace. */
1902 BUG();
1903}
1904EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
1905
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001906static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04001907 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001908{
1909 if (val == SYS_RESTART) {
1910 /*
1911 * Some (well, at least mine) BIOSes hang on reboot if
1912 * in vmx root mode.
1913 */
1914 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001915 kvm_rebooting = true;
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001916 on_each_cpu(hardware_disable, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10001917 }
1918 return NOTIFY_OK;
1919}
1920
1921static struct notifier_block kvm_reboot_notifier = {
1922 .notifier_call = kvm_reboot,
1923 .priority = 0,
1924};
1925
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001926void kvm_io_bus_init(struct kvm_io_bus *bus)
1927{
1928 memset(bus, 0, sizeof(*bus));
1929}
1930
1931void kvm_io_bus_destroy(struct kvm_io_bus *bus)
1932{
1933 int i;
1934
1935 for (i = 0; i < bus->dev_count; i++) {
1936 struct kvm_io_device *pos = bus->devs[i];
1937
1938 kvm_iodevice_destructor(pos);
1939 }
1940}
1941
Laurent Vivier92760492008-05-30 16:05:53 +02001942struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
1943 gpa_t addr, int len, int is_write)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001944{
1945 int i;
1946
1947 for (i = 0; i < bus->dev_count; i++) {
1948 struct kvm_io_device *pos = bus->devs[i];
1949
Laurent Vivier92760492008-05-30 16:05:53 +02001950 if (pos->in_range(pos, addr, len, is_write))
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001951 return pos;
1952 }
1953
1954 return NULL;
1955}
1956
1957void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
1958{
1959 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
1960
1961 bus->devs[bus->dev_count++] = dev;
1962}
1963
Avi Kivity774c47f2007-02-12 00:54:47 -08001964static struct notifier_block kvm_cpu_notifier = {
1965 .notifier_call = kvm_cpu_hotplug,
1966 .priority = 20, /* must be > scheduler priority */
1967};
1968
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001969static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02001970{
1971 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02001972 struct kvm *kvm;
1973
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001974 *val = 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001975 spin_lock(&kvm_lock);
1976 list_for_each_entry(kvm, &vm_list, vm_list)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001977 *val += *(u32 *)((void *)kvm + offset);
Avi Kivityba1389b2007-11-18 16:24:12 +02001978 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001979 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02001980}
1981
1982DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
1983
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001984static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03001985{
1986 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001987 struct kvm *kvm;
1988 struct kvm_vcpu *vcpu;
1989 int i;
1990
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001991 *val = 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001992 spin_lock(&kvm_lock);
1993 list_for_each_entry(kvm, &vm_list, vm_list)
1994 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001995 vcpu = kvm->vcpus[i];
1996 if (vcpu)
Christoph Hellwig8b88b092008-02-08 04:20:26 -08001997 *val += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03001998 }
1999 spin_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08002000 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002001}
2002
Avi Kivityba1389b2007-11-18 16:24:12 +02002003DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2004
2005static struct file_operations *stat_fops[] = {
2006 [KVM_STAT_VCPU] = &vcpu_stat_fops,
2007 [KVM_STAT_VM] = &vm_stat_fops,
2008};
Avi Kivity1165f5f2007-04-19 17:27:43 +03002009
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002010static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011{
2012 struct kvm_stats_debugfs_item *p;
2013
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002014 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015 for (p = debugfs_entries; p->name; ++p)
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002016 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
Avi Kivity1165f5f2007-04-19 17:27:43 +03002017 (void *)(long)p->offset,
Avi Kivityba1389b2007-11-18 16:24:12 +02002018 stat_fops[p->kind]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019}
2020
2021static void kvm_exit_debug(void)
2022{
2023 struct kvm_stats_debugfs_item *p;
2024
2025 for (p = debugfs_entries; p->name; ++p)
2026 debugfs_remove(p->dentry);
Hollis Blanchard76f7c872008-04-15 16:05:42 -05002027 debugfs_remove(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028}
2029
Avi Kivity59ae6c62007-02-12 00:54:48 -08002030static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2031{
Avi Kivity4267c412007-05-24 13:09:41 +03002032 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002033 return 0;
2034}
2035
2036static int kvm_resume(struct sys_device *dev)
2037{
Avi Kivity4267c412007-05-24 13:09:41 +03002038 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002039 return 0;
2040}
2041
2042static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002043 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002044 .suspend = kvm_suspend,
2045 .resume = kvm_resume,
2046};
2047
2048static struct sys_device kvm_sysdev = {
2049 .id = 0,
2050 .cls = &kvm_sysdev_class,
2051};
2052
Izik Eiduscea7bb22007-10-17 19:17:48 +02002053struct page *bad_page;
Anthony Liguori35149e22008-04-02 14:46:56 -05002054pfn_t bad_pfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055
Avi Kivity15ad7142007-07-11 18:17:21 +03002056static inline
2057struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2058{
2059 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2060}
2061
2062static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2063{
2064 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2065
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002066 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002067}
2068
2069static void kvm_sched_out(struct preempt_notifier *pn,
2070 struct task_struct *next)
2071{
2072 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2073
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002074 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002075}
2076
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002077int kvm_init(void *opaque, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002078 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079{
2080 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002081 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002083 kvm_init_debug();
2084
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002085 r = kvm_arch_init(opaque);
2086 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002087 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002088
2089 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2090
2091 if (bad_page == NULL) {
2092 r = -ENOMEM;
2093 goto out;
2094 }
2095
Anthony Liguori35149e22008-04-02 14:46:56 -05002096 bad_pfn = page_to_pfn(bad_page);
2097
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002098 r = kvm_arch_hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002100 goto out_free_0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101
Yang, Sheng002c7f72007-07-31 14:23:01 +03002102 for_each_online_cpu(cpu) {
2103 smp_call_function_single(cpu,
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002104 kvm_arch_check_processor_compat,
Jens Axboe8691e5a2008-06-06 11:18:06 +02002105 &r, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03002106 if (r < 0)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002107 goto out_free_1;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002108 }
2109
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002110 on_each_cpu(hardware_enable, NULL, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002111 r = register_cpu_notifier(&kvm_cpu_notifier);
2112 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002113 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 register_reboot_notifier(&kvm_reboot_notifier);
2115
Avi Kivity59ae6c62007-02-12 00:54:48 -08002116 r = sysdev_class_register(&kvm_sysdev_class);
2117 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002118 goto out_free_3;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002119
2120 r = sysdev_register(&kvm_sysdev);
2121 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08002122 goto out_free_4;
Avi Kivity59ae6c62007-02-12 00:54:48 -08002123
Rusty Russellc16f8622007-07-30 21:12:19 +10002124 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2125 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
Joe Perches56919c52007-11-12 20:06:51 -08002126 __alignof__(struct kvm_vcpu),
2127 0, NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10002128 if (!kvm_vcpu_cache) {
2129 r = -ENOMEM;
Zhang Xiantaod23087842007-11-29 15:35:39 +08002130 goto out_free_5;
Rusty Russellc16f8622007-07-30 21:12:19 +10002131 }
2132
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 kvm_chardev_ops.owner = module;
2134
2135 r = misc_register(&kvm_dev);
2136 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002137 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 goto out_free;
2139 }
2140
Avi Kivity15ad7142007-07-11 18:17:21 +03002141 kvm_preempt_ops.sched_in = kvm_sched_in;
2142 kvm_preempt_ops.sched_out = kvm_sched_out;
2143
Avi Kivityc7addb92007-09-16 18:58:32 +02002144 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145
2146out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10002147 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002148out_free_5:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002149 sysdev_unregister(&kvm_sysdev);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002150out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002151 sysdev_class_unregister(&kvm_sysdev_class);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002152out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002154 unregister_cpu_notifier(&kvm_cpu_notifier);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002155out_free_2:
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002156 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaod23087842007-11-29 15:35:39 +08002157out_free_1:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002158 kvm_arch_hardware_unsetup();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002159out_free_0:
2160 __free_page(bad_page);
Avi Kivityca45aaa2007-03-01 19:21:03 +02002161out:
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002162 kvm_arch_exit();
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002163 kvm_exit_debug();
Zhang Xiantaod23087842007-11-29 15:35:39 +08002164out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 return r;
2166}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002167EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002169void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04002171 kvm_trace_cleanup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10002173 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002174 sysdev_unregister(&kvm_sysdev);
2175 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002177 unregister_cpu_notifier(&kvm_cpu_notifier);
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002178 on_each_cpu(hardware_disable, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08002179 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002180 kvm_arch_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02002182 __free_page(bad_page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002184EXPORT_SYMBOL_GPL(kvm_exit);