Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
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 | ||
e2174021 | 18 | #include "iodev.h" |
6aa8b732 | 19 | |
edf88417 | 20 | #include <linux/kvm_host.h> |
6aa8b732 AK |
21 | #include <linux/kvm.h> |
22 | #include <linux/module.h> | |
23 | #include <linux/errno.h> | |
6aa8b732 AK |
24 | #include <linux/percpu.h> |
25 | #include <linux/gfp.h> | |
6aa8b732 AK |
26 | #include <linux/mm.h> |
27 | #include <linux/miscdevice.h> | |
28 | #include <linux/vmalloc.h> | |
6aa8b732 | 29 | #include <linux/reboot.h> |
6aa8b732 AK |
30 | #include <linux/debugfs.h> |
31 | #include <linux/highmem.h> | |
32 | #include <linux/file.h> | |
59ae6c6b | 33 | #include <linux/sysdev.h> |
774c47f1 | 34 | #include <linux/cpu.h> |
e8edc6e0 | 35 | #include <linux/sched.h> |
d9e368d6 AK |
36 | #include <linux/cpumask.h> |
37 | #include <linux/smp.h> | |
d6d28168 | 38 | #include <linux/anon_inodes.h> |
04d2cc77 | 39 | #include <linux/profile.h> |
7aa81cc0 | 40 | #include <linux/kvm_para.h> |
6fc138d2 | 41 | #include <linux/pagemap.h> |
8d4e1288 | 42 | #include <linux/mman.h> |
35149e21 | 43 | #include <linux/swap.h> |
6aa8b732 | 44 | |
e495606d | 45 | #include <asm/processor.h> |
e495606d AK |
46 | #include <asm/io.h> |
47 | #include <asm/uaccess.h> | |
3e021bf5 | 48 | #include <asm/pgtable.h> |
6aa8b732 | 49 | |
f64769eb SY |
50 | #ifdef CONFIG_X86 |
51 | #include <asm/msidef.h> | |
52 | #endif | |
53 | ||
5f94c174 LV |
54 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
55 | #include "coalesced_mmio.h" | |
56 | #endif | |
57 | ||
8a98f664 XZ |
58 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
59 | #include <linux/pci.h> | |
60 | #include <linux/interrupt.h> | |
61 | #include "irq.h" | |
62 | #endif | |
63 | ||
6aa8b732 AK |
64 | MODULE_AUTHOR("Qumranet"); |
65 | MODULE_LICENSE("GPL"); | |
66 | ||
e9b11c17 ZX |
67 | DEFINE_SPINLOCK(kvm_lock); |
68 | LIST_HEAD(vm_list); | |
133de902 | 69 | |
1b6c0168 AK |
70 | static cpumask_t cpus_hardware_enabled; |
71 | ||
c16f862d RR |
72 | struct kmem_cache *kvm_vcpu_cache; |
73 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 74 | |
15ad7146 AK |
75 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
76 | ||
76f7c879 | 77 | struct dentry *kvm_debugfs_dir; |
6aa8b732 | 78 | |
bccf2150 AK |
79 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
80 | unsigned long arg); | |
81 | ||
4ecac3fd AK |
82 | bool kvm_rebooting; |
83 | ||
8a98f664 | 84 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
f64769eb SY |
85 | |
86 | #ifdef CONFIG_X86 | |
87 | static 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 | |
133 | static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {} | |
134 | #endif | |
135 | ||
8a98f664 XZ |
136 | static 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 | ||
150 | static 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); | |
6b9cc7fd SY |
162 | if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX) |
163 | kvm_set_irq(assigned_dev->kvm, | |
164 | assigned_dev->irq_source_id, | |
165 | assigned_dev->guest_irq, 1); | |
166 | else if (assigned_dev->irq_requested_type & | |
167 | KVM_ASSIGNED_DEV_GUEST_MSI) { | |
168 | assigned_device_msi_dispatch(assigned_dev); | |
169 | enable_irq(assigned_dev->host_irq); | |
170 | } | |
8a98f664 XZ |
171 | mutex_unlock(&assigned_dev->kvm->lock); |
172 | kvm_put_kvm(assigned_dev->kvm); | |
173 | } | |
174 | ||
8a98f664 XZ |
175 | static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) |
176 | { | |
177 | struct kvm_assigned_dev_kernel *assigned_dev = | |
178 | (struct kvm_assigned_dev_kernel *) dev_id; | |
179 | ||
180 | kvm_get_kvm(assigned_dev->kvm); | |
181 | schedule_work(&assigned_dev->interrupt_work); | |
182 | disable_irq_nosync(irq); | |
183 | return IRQ_HANDLED; | |
184 | } | |
185 | ||
186 | /* Ack the irq line for an assigned device */ | |
187 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) | |
188 | { | |
189 | struct kvm_assigned_dev_kernel *dev; | |
190 | ||
191 | if (kian->gsi == -1) | |
192 | return; | |
193 | ||
194 | dev = container_of(kian, struct kvm_assigned_dev_kernel, | |
195 | ack_notifier); | |
5550af4d | 196 | kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0); |
8a98f664 XZ |
197 | enable_irq(dev->host_irq); |
198 | } | |
199 | ||
200 | static void kvm_free_assigned_device(struct kvm *kvm, | |
201 | struct kvm_assigned_dev_kernel | |
202 | *assigned_dev) | |
203 | { | |
4f906c19 | 204 | if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested_type) |
8a98f664 | 205 | free_irq(assigned_dev->host_irq, (void *)assigned_dev); |
6b9cc7fd SY |
206 | if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) |
207 | pci_disable_msi(assigned_dev->dev); | |
8a98f664 | 208 | |
e19e30ef | 209 | kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier); |
5550af4d | 210 | kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id); |
8a98f664 XZ |
211 | |
212 | if (cancel_work_sync(&assigned_dev->interrupt_work)) | |
213 | /* We had pending work. That means we will have to take | |
214 | * care of kvm_put_kvm. | |
215 | */ | |
216 | kvm_put_kvm(kvm); | |
217 | ||
6eb55818 SY |
218 | pci_reset_function(assigned_dev->dev); |
219 | ||
8a98f664 XZ |
220 | pci_release_regions(assigned_dev->dev); |
221 | pci_disable_device(assigned_dev->dev); | |
222 | pci_dev_put(assigned_dev->dev); | |
223 | ||
224 | list_del(&assigned_dev->list); | |
225 | kfree(assigned_dev); | |
226 | } | |
227 | ||
228 | void kvm_free_all_assigned_devices(struct kvm *kvm) | |
229 | { | |
230 | struct list_head *ptr, *ptr2; | |
231 | struct kvm_assigned_dev_kernel *assigned_dev; | |
232 | ||
233 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { | |
234 | assigned_dev = list_entry(ptr, | |
235 | struct kvm_assigned_dev_kernel, | |
236 | list); | |
237 | ||
238 | kvm_free_assigned_device(kvm, assigned_dev); | |
239 | } | |
240 | } | |
241 | ||
00e3ed39 SY |
242 | static int assigned_device_update_intx(struct kvm *kvm, |
243 | struct kvm_assigned_dev_kernel *adev, | |
244 | struct kvm_assigned_irq *airq) | |
245 | { | |
fbac7818 SY |
246 | adev->guest_irq = airq->guest_irq; |
247 | adev->ack_notifier.gsi = airq->guest_irq; | |
248 | ||
249 | if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX) | |
00e3ed39 | 250 | return 0; |
00e3ed39 SY |
251 | |
252 | if (irqchip_in_kernel(kvm)) { | |
6b9cc7fd SY |
253 | if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) { |
254 | free_irq(adev->host_irq, (void *)kvm); | |
255 | pci_disable_msi(adev->dev); | |
256 | } | |
257 | ||
00e3ed39 SY |
258 | if (!capable(CAP_SYS_RAWIO)) |
259 | return -EPERM; | |
260 | ||
261 | if (airq->host_irq) | |
262 | adev->host_irq = airq->host_irq; | |
263 | else | |
264 | adev->host_irq = adev->dev->irq; | |
00e3ed39 SY |
265 | |
266 | /* Even though this is PCI, we don't want to use shared | |
267 | * interrupts. Sharing host devices with guest-assigned devices | |
268 | * on the same interrupt line is not a happy situation: there | |
269 | * are going to be long delays in accepting, acking, etc. | |
270 | */ | |
271 | if (request_irq(adev->host_irq, kvm_assigned_dev_intr, | |
272 | 0, "kvm_assigned_intx_device", (void *)adev)) | |
273 | return -EIO; | |
274 | } | |
275 | ||
4f906c19 SY |
276 | adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX | |
277 | KVM_ASSIGNED_DEV_HOST_INTX; | |
00e3ed39 SY |
278 | return 0; |
279 | } | |
280 | ||
6b9cc7fd SY |
281 | #ifdef CONFIG_X86 |
282 | static int assigned_device_update_msi(struct kvm *kvm, | |
283 | struct kvm_assigned_dev_kernel *adev, | |
284 | struct kvm_assigned_irq *airq) | |
285 | { | |
286 | int r; | |
287 | ||
288 | /* x86 don't care upper address of guest msi message addr */ | |
289 | adev->guest_msi.address_lo = airq->guest_msi.addr_lo; | |
290 | adev->guest_msi.data = airq->guest_msi.data; | |
291 | adev->ack_notifier.gsi = -1; | |
292 | ||
293 | if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI) | |
294 | return 0; | |
295 | ||
296 | if (irqchip_in_kernel(kvm)) { | |
297 | if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX) | |
298 | free_irq(adev->host_irq, (void *)adev); | |
299 | ||
300 | r = pci_enable_msi(adev->dev); | |
301 | if (r) | |
302 | return r; | |
303 | ||
304 | adev->host_irq = adev->dev->irq; | |
305 | if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0, | |
306 | "kvm_assigned_msi_device", (void *)adev)) | |
307 | return -EIO; | |
308 | } | |
309 | ||
310 | adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI | | |
311 | KVM_ASSIGNED_DEV_HOST_MSI; | |
312 | return 0; | |
313 | } | |
314 | #endif | |
315 | ||
8a98f664 XZ |
316 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, |
317 | struct kvm_assigned_irq | |
318 | *assigned_irq) | |
319 | { | |
320 | int r = 0; | |
321 | struct kvm_assigned_dev_kernel *match; | |
322 | ||
323 | mutex_lock(&kvm->lock); | |
324 | ||
325 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | |
326 | assigned_irq->assigned_dev_id); | |
327 | if (!match) { | |
328 | mutex_unlock(&kvm->lock); | |
329 | return -EINVAL; | |
330 | } | |
331 | ||
4f906c19 | 332 | if (!match->irq_requested_type) { |
342ffb93 SY |
333 | INIT_WORK(&match->interrupt_work, |
334 | kvm_assigned_dev_interrupt_work_handler); | |
335 | if (irqchip_in_kernel(kvm)) { | |
336 | /* Register ack nofitier */ | |
337 | match->ack_notifier.gsi = -1; | |
338 | match->ack_notifier.irq_acked = | |
339 | kvm_assigned_dev_ack_irq; | |
340 | kvm_register_irq_ack_notifier(kvm, | |
341 | &match->ack_notifier); | |
342 | ||
343 | /* Request IRQ source ID */ | |
344 | r = kvm_request_irq_source_id(kvm); | |
345 | if (r < 0) | |
346 | goto out_release; | |
347 | else | |
348 | match->irq_source_id = r; | |
349 | } | |
8a98f664 XZ |
350 | } |
351 | ||
6b9cc7fd SY |
352 | if (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) { |
353 | #ifdef CONFIG_X86 | |
354 | r = assigned_device_update_msi(kvm, match, assigned_irq); | |
355 | if (r) { | |
356 | printk(KERN_WARNING "kvm: failed to enable " | |
357 | "MSI device!\n"); | |
358 | goto out_release; | |
359 | } | |
360 | #else | |
361 | r = -ENOTTY; | |
362 | #endif | |
363 | } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) { | |
364 | /* Host device IRQ 0 means don't support INTx */ | |
365 | printk(KERN_WARNING "kvm: wait device to enable MSI!\n"); | |
366 | r = 0; | |
367 | } else { | |
368 | /* Non-sharing INTx mode */ | |
369 | r = assigned_device_update_intx(kvm, match, assigned_irq); | |
370 | if (r) { | |
371 | printk(KERN_WARNING "kvm: failed to enable " | |
372 | "INTx device!\n"); | |
373 | goto out_release; | |
374 | } | |
375 | } | |
8a98f664 | 376 | |
8a98f664 XZ |
377 | mutex_unlock(&kvm->lock); |
378 | return r; | |
379 | out_release: | |
380 | mutex_unlock(&kvm->lock); | |
381 | kvm_free_assigned_device(kvm, match); | |
382 | return r; | |
383 | } | |
384 | ||
385 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, | |
386 | struct kvm_assigned_pci_dev *assigned_dev) | |
387 | { | |
388 | int r = 0; | |
389 | struct kvm_assigned_dev_kernel *match; | |
390 | struct pci_dev *dev; | |
391 | ||
392 | mutex_lock(&kvm->lock); | |
393 | ||
394 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | |
395 | assigned_dev->assigned_dev_id); | |
396 | if (match) { | |
397 | /* device already assigned */ | |
398 | r = -EINVAL; | |
399 | goto out; | |
400 | } | |
401 | ||
402 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); | |
403 | if (match == NULL) { | |
404 | printk(KERN_INFO "%s: Couldn't allocate memory\n", | |
405 | __func__); | |
406 | r = -ENOMEM; | |
407 | goto out; | |
408 | } | |
409 | dev = pci_get_bus_and_slot(assigned_dev->busnr, | |
410 | assigned_dev->devfn); | |
411 | if (!dev) { | |
412 | printk(KERN_INFO "%s: host device not found\n", __func__); | |
413 | r = -EINVAL; | |
414 | goto out_free; | |
415 | } | |
416 | if (pci_enable_device(dev)) { | |
417 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); | |
418 | r = -EBUSY; | |
419 | goto out_put; | |
420 | } | |
421 | r = pci_request_regions(dev, "kvm_assigned_device"); | |
422 | if (r) { | |
423 | printk(KERN_INFO "%s: Could not get access to device regions\n", | |
424 | __func__); | |
425 | goto out_disable; | |
426 | } | |
6eb55818 SY |
427 | |
428 | pci_reset_function(dev); | |
429 | ||
8a98f664 XZ |
430 | match->assigned_dev_id = assigned_dev->assigned_dev_id; |
431 | match->host_busnr = assigned_dev->busnr; | |
432 | match->host_devfn = assigned_dev->devfn; | |
433 | match->dev = dev; | |
434 | ||
435 | match->kvm = kvm; | |
436 | ||
437 | list_add(&match->list, &kvm->arch.assigned_dev_head); | |
438 | ||
439 | if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) { | |
440 | r = kvm_iommu_map_guest(kvm, match); | |
441 | if (r) | |
442 | goto out_list_del; | |
443 | } | |
444 | ||
445 | out: | |
446 | mutex_unlock(&kvm->lock); | |
447 | return r; | |
448 | out_list_del: | |
449 | list_del(&match->list); | |
450 | pci_release_regions(dev); | |
451 | out_disable: | |
452 | pci_disable_device(dev); | |
453 | out_put: | |
454 | pci_dev_put(dev); | |
455 | out_free: | |
456 | kfree(match); | |
457 | mutex_unlock(&kvm->lock); | |
458 | return r; | |
459 | } | |
460 | #endif | |
461 | ||
5aacf0ca JM |
462 | static inline int valid_vcpu(int n) |
463 | { | |
464 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
465 | } | |
466 | ||
c77fb9dc | 467 | inline int kvm_is_mmio_pfn(pfn_t pfn) |
cbff90a7 BAY |
468 | { |
469 | if (pfn_valid(pfn)) | |
470 | return PageReserved(pfn_to_page(pfn)); | |
471 | ||
472 | return true; | |
473 | } | |
474 | ||
bccf2150 AK |
475 | /* |
476 | * Switches to specified vcpu, until a matching vcpu_put() | |
477 | */ | |
313a3dc7 | 478 | void vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 479 | { |
15ad7146 AK |
480 | int cpu; |
481 | ||
bccf2150 | 482 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
483 | cpu = get_cpu(); |
484 | preempt_notifier_register(&vcpu->preempt_notifier); | |
313a3dc7 | 485 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 486 | put_cpu(); |
6aa8b732 AK |
487 | } |
488 | ||
313a3dc7 | 489 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 490 | { |
15ad7146 | 491 | preempt_disable(); |
313a3dc7 | 492 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
493 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
494 | preempt_enable(); | |
6aa8b732 AK |
495 | mutex_unlock(&vcpu->mutex); |
496 | } | |
497 | ||
d9e368d6 AK |
498 | static void ack_flush(void *_completed) |
499 | { | |
d9e368d6 AK |
500 | } |
501 | ||
502 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
503 | { | |
597a5f55 | 504 | int i, cpu, me; |
d9e368d6 AK |
505 | cpumask_t cpus; |
506 | struct kvm_vcpu *vcpu; | |
d9e368d6 | 507 | |
597a5f55 | 508 | me = get_cpu(); |
d9e368d6 | 509 | cpus_clear(cpus); |
fb3f0f51 RR |
510 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
511 | vcpu = kvm->vcpus[i]; | |
512 | if (!vcpu) | |
513 | continue; | |
3176bc3e | 514 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
d9e368d6 AK |
515 | continue; |
516 | cpu = vcpu->cpu; | |
597a5f55 | 517 | if (cpu != -1 && cpu != me) |
49d3bd7e | 518 | cpu_set(cpu, cpus); |
d9e368d6 | 519 | } |
0f74a24c | 520 | if (cpus_empty(cpus)) |
597a5f55 | 521 | goto out; |
0f74a24c | 522 | ++kvm->stat.remote_tlb_flush; |
49d3bd7e | 523 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
597a5f55 AK |
524 | out: |
525 | put_cpu(); | |
d9e368d6 AK |
526 | } |
527 | ||
2e53d63a MT |
528 | void kvm_reload_remote_mmus(struct kvm *kvm) |
529 | { | |
597a5f55 | 530 | int i, cpu, me; |
2e53d63a MT |
531 | cpumask_t cpus; |
532 | struct kvm_vcpu *vcpu; | |
533 | ||
597a5f55 | 534 | me = get_cpu(); |
2e53d63a MT |
535 | cpus_clear(cpus); |
536 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
537 | vcpu = kvm->vcpus[i]; | |
538 | if (!vcpu) | |
539 | continue; | |
540 | if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) | |
541 | continue; | |
542 | cpu = vcpu->cpu; | |
597a5f55 | 543 | if (cpu != -1 && cpu != me) |
2e53d63a MT |
544 | cpu_set(cpu, cpus); |
545 | } | |
546 | if (cpus_empty(cpus)) | |
597a5f55 | 547 | goto out; |
2e53d63a | 548 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
597a5f55 AK |
549 | out: |
550 | put_cpu(); | |
2e53d63a MT |
551 | } |
552 | ||
553 | ||
fb3f0f51 RR |
554 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
555 | { | |
556 | struct page *page; | |
557 | int r; | |
558 | ||
559 | mutex_init(&vcpu->mutex); | |
560 | vcpu->cpu = -1; | |
fb3f0f51 RR |
561 | vcpu->kvm = kvm; |
562 | vcpu->vcpu_id = id; | |
b6958ce4 | 563 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
564 | |
565 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
566 | if (!page) { | |
567 | r = -ENOMEM; | |
568 | goto fail; | |
569 | } | |
570 | vcpu->run = page_address(page); | |
571 | ||
e9b11c17 | 572 | r = kvm_arch_vcpu_init(vcpu); |
fb3f0f51 | 573 | if (r < 0) |
e9b11c17 | 574 | goto fail_free_run; |
fb3f0f51 RR |
575 | return 0; |
576 | ||
fb3f0f51 RR |
577 | fail_free_run: |
578 | free_page((unsigned long)vcpu->run); | |
579 | fail: | |
76fafa5e | 580 | return r; |
fb3f0f51 RR |
581 | } |
582 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
583 | ||
584 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
585 | { | |
e9b11c17 | 586 | kvm_arch_vcpu_uninit(vcpu); |
fb3f0f51 RR |
587 | free_page((unsigned long)vcpu->run); |
588 | } | |
589 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
590 | ||
e930bffe AA |
591 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
592 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) | |
593 | { | |
594 | return container_of(mn, struct kvm, mmu_notifier); | |
595 | } | |
596 | ||
597 | static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, | |
598 | struct mm_struct *mm, | |
599 | unsigned long address) | |
600 | { | |
601 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
602 | int need_tlb_flush; | |
603 | ||
604 | /* | |
605 | * When ->invalidate_page runs, the linux pte has been zapped | |
606 | * already but the page is still allocated until | |
607 | * ->invalidate_page returns. So if we increase the sequence | |
608 | * here the kvm page fault will notice if the spte can't be | |
609 | * established because the page is going to be freed. If | |
610 | * instead the kvm page fault establishes the spte before | |
611 | * ->invalidate_page runs, kvm_unmap_hva will release it | |
612 | * before returning. | |
613 | * | |
614 | * The sequence increase only need to be seen at spin_unlock | |
615 | * time, and not at spin_lock time. | |
616 | * | |
617 | * Increasing the sequence after the spin_unlock would be | |
618 | * unsafe because the kvm page fault could then establish the | |
619 | * pte after kvm_unmap_hva returned, without noticing the page | |
620 | * is going to be freed. | |
621 | */ | |
622 | spin_lock(&kvm->mmu_lock); | |
623 | kvm->mmu_notifier_seq++; | |
624 | need_tlb_flush = kvm_unmap_hva(kvm, address); | |
625 | spin_unlock(&kvm->mmu_lock); | |
626 | ||
627 | /* we've to flush the tlb before the pages can be freed */ | |
628 | if (need_tlb_flush) | |
629 | kvm_flush_remote_tlbs(kvm); | |
630 | ||
631 | } | |
632 | ||
633 | static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, | |
634 | struct mm_struct *mm, | |
635 | unsigned long start, | |
636 | unsigned long end) | |
637 | { | |
638 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
639 | int need_tlb_flush = 0; | |
640 | ||
641 | spin_lock(&kvm->mmu_lock); | |
642 | /* | |
643 | * The count increase must become visible at unlock time as no | |
644 | * spte can be established without taking the mmu_lock and | |
645 | * count is also read inside the mmu_lock critical section. | |
646 | */ | |
647 | kvm->mmu_notifier_count++; | |
648 | for (; start < end; start += PAGE_SIZE) | |
649 | need_tlb_flush |= kvm_unmap_hva(kvm, start); | |
650 | spin_unlock(&kvm->mmu_lock); | |
651 | ||
652 | /* we've to flush the tlb before the pages can be freed */ | |
653 | if (need_tlb_flush) | |
654 | kvm_flush_remote_tlbs(kvm); | |
655 | } | |
656 | ||
657 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, | |
658 | struct mm_struct *mm, | |
659 | unsigned long start, | |
660 | unsigned long end) | |
661 | { | |
662 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
663 | ||
664 | spin_lock(&kvm->mmu_lock); | |
665 | /* | |
666 | * This sequence increase will notify the kvm page fault that | |
667 | * the page that is going to be mapped in the spte could have | |
668 | * been freed. | |
669 | */ | |
670 | kvm->mmu_notifier_seq++; | |
671 | /* | |
672 | * The above sequence increase must be visible before the | |
673 | * below count decrease but both values are read by the kvm | |
674 | * page fault under mmu_lock spinlock so we don't need to add | |
675 | * a smb_wmb() here in between the two. | |
676 | */ | |
677 | kvm->mmu_notifier_count--; | |
678 | spin_unlock(&kvm->mmu_lock); | |
679 | ||
680 | BUG_ON(kvm->mmu_notifier_count < 0); | |
681 | } | |
682 | ||
683 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, | |
684 | struct mm_struct *mm, | |
685 | unsigned long address) | |
686 | { | |
687 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
688 | int young; | |
689 | ||
690 | spin_lock(&kvm->mmu_lock); | |
691 | young = kvm_age_hva(kvm, address); | |
692 | spin_unlock(&kvm->mmu_lock); | |
693 | ||
694 | if (young) | |
695 | kvm_flush_remote_tlbs(kvm); | |
696 | ||
697 | return young; | |
698 | } | |
699 | ||
700 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { | |
701 | .invalidate_page = kvm_mmu_notifier_invalidate_page, | |
702 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, | |
703 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, | |
704 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, | |
705 | }; | |
706 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ | |
707 | ||
f17abe9a | 708 | static struct kvm *kvm_create_vm(void) |
6aa8b732 | 709 | { |
d19a9cd2 | 710 | struct kvm *kvm = kvm_arch_create_vm(); |
5f94c174 LV |
711 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
712 | struct page *page; | |
713 | #endif | |
6aa8b732 | 714 | |
d19a9cd2 ZX |
715 | if (IS_ERR(kvm)) |
716 | goto out; | |
6aa8b732 | 717 | |
5f94c174 LV |
718 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
719 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
720 | if (!page) { | |
721 | kfree(kvm); | |
722 | return ERR_PTR(-ENOMEM); | |
723 | } | |
724 | kvm->coalesced_mmio_ring = | |
725 | (struct kvm_coalesced_mmio_ring *)page_address(page); | |
726 | #endif | |
727 | ||
e930bffe AA |
728 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
729 | { | |
730 | int err; | |
731 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; | |
732 | err = mmu_notifier_register(&kvm->mmu_notifier, current->mm); | |
733 | if (err) { | |
734 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
735 | put_page(page); | |
736 | #endif | |
737 | kfree(kvm); | |
738 | return ERR_PTR(err); | |
739 | } | |
740 | } | |
741 | #endif | |
742 | ||
6d4e4c4f AK |
743 | kvm->mm = current->mm; |
744 | atomic_inc(&kvm->mm->mm_count); | |
aaee2c94 | 745 | spin_lock_init(&kvm->mmu_lock); |
74906345 | 746 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 747 | mutex_init(&kvm->lock); |
2eeb2e94 | 748 | kvm_io_bus_init(&kvm->mmio_bus); |
72dc67a6 | 749 | init_rwsem(&kvm->slots_lock); |
d39f13b0 | 750 | atomic_set(&kvm->users_count, 1); |
5e58cfe4 RR |
751 | spin_lock(&kvm_lock); |
752 | list_add(&kvm->vm_list, &vm_list); | |
753 | spin_unlock(&kvm_lock); | |
5f94c174 LV |
754 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
755 | kvm_coalesced_mmio_init(kvm); | |
756 | #endif | |
d19a9cd2 | 757 | out: |
f17abe9a AK |
758 | return kvm; |
759 | } | |
760 | ||
6aa8b732 AK |
761 | /* |
762 | * Free any memory in @free but not in @dont. | |
763 | */ | |
764 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
765 | struct kvm_memory_slot *dont) | |
766 | { | |
290fc38d IE |
767 | if (!dont || free->rmap != dont->rmap) |
768 | vfree(free->rmap); | |
6aa8b732 AK |
769 | |
770 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
771 | vfree(free->dirty_bitmap); | |
772 | ||
05da4558 MT |
773 | if (!dont || free->lpage_info != dont->lpage_info) |
774 | vfree(free->lpage_info); | |
775 | ||
6aa8b732 | 776 | free->npages = 0; |
8b6d44c7 | 777 | free->dirty_bitmap = NULL; |
8d4e1288 | 778 | free->rmap = NULL; |
05da4558 | 779 | free->lpage_info = NULL; |
6aa8b732 AK |
780 | } |
781 | ||
d19a9cd2 | 782 | void kvm_free_physmem(struct kvm *kvm) |
6aa8b732 AK |
783 | { |
784 | int i; | |
785 | ||
786 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 787 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
788 | } |
789 | ||
f17abe9a AK |
790 | static void kvm_destroy_vm(struct kvm *kvm) |
791 | { | |
6d4e4c4f AK |
792 | struct mm_struct *mm = kvm->mm; |
793 | ||
133de902 AK |
794 | spin_lock(&kvm_lock); |
795 | list_del(&kvm->vm_list); | |
796 | spin_unlock(&kvm_lock); | |
74906345 | 797 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 798 | kvm_io_bus_destroy(&kvm->mmio_bus); |
5f94c174 LV |
799 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
800 | if (kvm->coalesced_mmio_ring != NULL) | |
801 | free_page((unsigned long)kvm->coalesced_mmio_ring); | |
e930bffe AA |
802 | #endif |
803 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) | |
804 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); | |
5f94c174 | 805 | #endif |
d19a9cd2 | 806 | kvm_arch_destroy_vm(kvm); |
6d4e4c4f | 807 | mmdrop(mm); |
f17abe9a AK |
808 | } |
809 | ||
d39f13b0 IE |
810 | void kvm_get_kvm(struct kvm *kvm) |
811 | { | |
812 | atomic_inc(&kvm->users_count); | |
813 | } | |
814 | EXPORT_SYMBOL_GPL(kvm_get_kvm); | |
815 | ||
816 | void kvm_put_kvm(struct kvm *kvm) | |
817 | { | |
818 | if (atomic_dec_and_test(&kvm->users_count)) | |
819 | kvm_destroy_vm(kvm); | |
820 | } | |
821 | EXPORT_SYMBOL_GPL(kvm_put_kvm); | |
822 | ||
823 | ||
f17abe9a AK |
824 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
825 | { | |
826 | struct kvm *kvm = filp->private_data; | |
827 | ||
d39f13b0 | 828 | kvm_put_kvm(kvm); |
6aa8b732 AK |
829 | return 0; |
830 | } | |
831 | ||
6aa8b732 AK |
832 | /* |
833 | * Allocate some memory and give it an address in the guest physical address | |
834 | * space. | |
835 | * | |
836 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e | 837 | * |
10589a46 | 838 | * Must be called holding mmap_sem for write. |
6aa8b732 | 839 | */ |
f78e0e2e SY |
840 | int __kvm_set_memory_region(struct kvm *kvm, |
841 | struct kvm_userspace_memory_region *mem, | |
842 | int user_alloc) | |
6aa8b732 AK |
843 | { |
844 | int r; | |
845 | gfn_t base_gfn; | |
846 | unsigned long npages; | |
847 | unsigned long i; | |
848 | struct kvm_memory_slot *memslot; | |
849 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
850 | |
851 | r = -EINVAL; | |
852 | /* General sanity checks */ | |
853 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
854 | goto out; | |
855 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
856 | goto out; | |
e7cacd40 | 857 | if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1))) |
78749809 | 858 | goto out; |
e0d62c7f | 859 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
6aa8b732 AK |
860 | goto out; |
861 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
862 | goto out; | |
863 | ||
864 | memslot = &kvm->memslots[mem->slot]; | |
865 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
866 | npages = mem->memory_size >> PAGE_SHIFT; | |
867 | ||
868 | if (!npages) | |
869 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
870 | ||
6aa8b732 AK |
871 | new = old = *memslot; |
872 | ||
873 | new.base_gfn = base_gfn; | |
874 | new.npages = npages; | |
875 | new.flags = mem->flags; | |
876 | ||
877 | /* Disallow changing a memory slot's size. */ | |
878 | r = -EINVAL; | |
879 | if (npages && old.npages && npages != old.npages) | |
f78e0e2e | 880 | goto out_free; |
6aa8b732 AK |
881 | |
882 | /* Check for overlaps */ | |
883 | r = -EEXIST; | |
884 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
885 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
886 | ||
887 | if (s == memslot) | |
888 | continue; | |
889 | if (!((base_gfn + npages <= s->base_gfn) || | |
890 | (base_gfn >= s->base_gfn + s->npages))) | |
f78e0e2e | 891 | goto out_free; |
6aa8b732 | 892 | } |
6aa8b732 | 893 | |
6aa8b732 AK |
894 | /* Free page dirty bitmap if unneeded */ |
895 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 896 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
897 | |
898 | r = -ENOMEM; | |
899 | ||
900 | /* Allocate if a slot is being created */ | |
eff0114a | 901 | #ifndef CONFIG_S390 |
8d4e1288 | 902 | if (npages && !new.rmap) { |
d77c26fc | 903 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
290fc38d IE |
904 | |
905 | if (!new.rmap) | |
f78e0e2e | 906 | goto out_free; |
290fc38d | 907 | |
290fc38d | 908 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
8d4e1288 | 909 | |
80b14b5b | 910 | new.user_alloc = user_alloc; |
604b38ac AA |
911 | /* |
912 | * hva_to_rmmap() serialzies with the mmu_lock and to be | |
913 | * safe it has to ignore memslots with !user_alloc && | |
914 | * !userspace_addr. | |
915 | */ | |
916 | if (user_alloc) | |
917 | new.userspace_addr = mem->userspace_addr; | |
918 | else | |
919 | new.userspace_addr = 0; | |
6aa8b732 | 920 | } |
05da4558 MT |
921 | if (npages && !new.lpage_info) { |
922 | int largepages = npages / KVM_PAGES_PER_HPAGE; | |
923 | if (npages % KVM_PAGES_PER_HPAGE) | |
924 | largepages++; | |
925 | if (base_gfn % KVM_PAGES_PER_HPAGE) | |
926 | largepages++; | |
927 | ||
928 | new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info)); | |
929 | ||
930 | if (!new.lpage_info) | |
931 | goto out_free; | |
932 | ||
933 | memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info)); | |
934 | ||
935 | if (base_gfn % KVM_PAGES_PER_HPAGE) | |
936 | new.lpage_info[0].write_count = 1; | |
937 | if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE) | |
938 | new.lpage_info[largepages-1].write_count = 1; | |
939 | } | |
6aa8b732 AK |
940 | |
941 | /* Allocate page dirty bitmap if needed */ | |
942 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
943 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
944 | ||
945 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
946 | if (!new.dirty_bitmap) | |
f78e0e2e | 947 | goto out_free; |
6aa8b732 AK |
948 | memset(new.dirty_bitmap, 0, dirty_bytes); |
949 | } | |
eff0114a | 950 | #endif /* not defined CONFIG_S390 */ |
6aa8b732 | 951 | |
34d4cb8f MT |
952 | if (!npages) |
953 | kvm_arch_flush_shadow(kvm); | |
954 | ||
604b38ac AA |
955 | spin_lock(&kvm->mmu_lock); |
956 | if (mem->slot >= kvm->nmemslots) | |
957 | kvm->nmemslots = mem->slot + 1; | |
958 | ||
3ad82a7e | 959 | *memslot = new; |
604b38ac | 960 | spin_unlock(&kvm->mmu_lock); |
3ad82a7e | 961 | |
0de10343 ZX |
962 | r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc); |
963 | if (r) { | |
604b38ac | 964 | spin_lock(&kvm->mmu_lock); |
0de10343 | 965 | *memslot = old; |
604b38ac | 966 | spin_unlock(&kvm->mmu_lock); |
0de10343 | 967 | goto out_free; |
82ce2c96 IE |
968 | } |
969 | ||
6aa8b732 | 970 | kvm_free_physmem_slot(&old, &new); |
8a98f664 | 971 | #ifdef CONFIG_DMAR |
62c476c7 BAY |
972 | /* map the pages in iommu page table */ |
973 | r = kvm_iommu_map_pages(kvm, base_gfn, npages); | |
974 | if (r) | |
975 | goto out; | |
8a98f664 | 976 | #endif |
6aa8b732 AK |
977 | return 0; |
978 | ||
f78e0e2e | 979 | out_free: |
6aa8b732 AK |
980 | kvm_free_physmem_slot(&new, &old); |
981 | out: | |
982 | return r; | |
210c7c4d IE |
983 | |
984 | } | |
f78e0e2e SY |
985 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
986 | ||
987 | int kvm_set_memory_region(struct kvm *kvm, | |
988 | struct kvm_userspace_memory_region *mem, | |
989 | int user_alloc) | |
990 | { | |
991 | int r; | |
992 | ||
72dc67a6 | 993 | down_write(&kvm->slots_lock); |
f78e0e2e | 994 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
72dc67a6 | 995 | up_write(&kvm->slots_lock); |
f78e0e2e SY |
996 | return r; |
997 | } | |
210c7c4d IE |
998 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
999 | ||
1fe779f8 CO |
1000 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
1001 | struct | |
1002 | kvm_userspace_memory_region *mem, | |
1003 | int user_alloc) | |
210c7c4d | 1004 | { |
e0d62c7f IE |
1005 | if (mem->slot >= KVM_MEMORY_SLOTS) |
1006 | return -EINVAL; | |
210c7c4d | 1007 | return kvm_set_memory_region(kvm, mem, user_alloc); |
6aa8b732 AK |
1008 | } |
1009 | ||
5bb064dc ZX |
1010 | int kvm_get_dirty_log(struct kvm *kvm, |
1011 | struct kvm_dirty_log *log, int *is_dirty) | |
6aa8b732 AK |
1012 | { |
1013 | struct kvm_memory_slot *memslot; | |
1014 | int r, i; | |
1015 | int n; | |
1016 | unsigned long any = 0; | |
1017 | ||
6aa8b732 AK |
1018 | r = -EINVAL; |
1019 | if (log->slot >= KVM_MEMORY_SLOTS) | |
1020 | goto out; | |
1021 | ||
1022 | memslot = &kvm->memslots[log->slot]; | |
1023 | r = -ENOENT; | |
1024 | if (!memslot->dirty_bitmap) | |
1025 | goto out; | |
1026 | ||
cd1a4a98 | 1027 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 1028 | |
cd1a4a98 | 1029 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
1030 | any = memslot->dirty_bitmap[i]; |
1031 | ||
1032 | r = -EFAULT; | |
1033 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
1034 | goto out; | |
1035 | ||
5bb064dc ZX |
1036 | if (any) |
1037 | *is_dirty = 1; | |
6aa8b732 AK |
1038 | |
1039 | r = 0; | |
6aa8b732 | 1040 | out: |
6aa8b732 AK |
1041 | return r; |
1042 | } | |
1043 | ||
cea7bb21 IE |
1044 | int is_error_page(struct page *page) |
1045 | { | |
1046 | return page == bad_page; | |
1047 | } | |
1048 | EXPORT_SYMBOL_GPL(is_error_page); | |
1049 | ||
35149e21 AL |
1050 | int is_error_pfn(pfn_t pfn) |
1051 | { | |
1052 | return pfn == bad_pfn; | |
1053 | } | |
1054 | EXPORT_SYMBOL_GPL(is_error_pfn); | |
1055 | ||
f9d46eb0 IE |
1056 | static inline unsigned long bad_hva(void) |
1057 | { | |
1058 | return PAGE_OFFSET; | |
1059 | } | |
1060 | ||
1061 | int kvm_is_error_hva(unsigned long addr) | |
1062 | { | |
1063 | return addr == bad_hva(); | |
1064 | } | |
1065 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); | |
1066 | ||
2843099f | 1067 | struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn) |
6aa8b732 AK |
1068 | { |
1069 | int i; | |
1070 | ||
1071 | for (i = 0; i < kvm->nmemslots; ++i) { | |
1072 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
1073 | ||
1074 | if (gfn >= memslot->base_gfn | |
1075 | && gfn < memslot->base_gfn + memslot->npages) | |
1076 | return memslot; | |
1077 | } | |
8b6d44c7 | 1078 | return NULL; |
6aa8b732 | 1079 | } |
2843099f | 1080 | EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased); |
e8207547 AK |
1081 | |
1082 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
1083 | { | |
1084 | gfn = unalias_gfn(kvm, gfn); | |
2843099f | 1085 | return gfn_to_memslot_unaliased(kvm, gfn); |
e8207547 | 1086 | } |
6aa8b732 | 1087 | |
e0d62c7f IE |
1088 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
1089 | { | |
1090 | int i; | |
1091 | ||
1092 | gfn = unalias_gfn(kvm, gfn); | |
1093 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
1094 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
1095 | ||
1096 | if (gfn >= memslot->base_gfn | |
1097 | && gfn < memslot->base_gfn + memslot->npages) | |
1098 | return 1; | |
1099 | } | |
1100 | return 0; | |
1101 | } | |
1102 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
1103 | ||
05da4558 | 1104 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
539cb660 IE |
1105 | { |
1106 | struct kvm_memory_slot *slot; | |
1107 | ||
1108 | gfn = unalias_gfn(kvm, gfn); | |
2843099f | 1109 | slot = gfn_to_memslot_unaliased(kvm, gfn); |
539cb660 IE |
1110 | if (!slot) |
1111 | return bad_hva(); | |
1112 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); | |
1113 | } | |
0d150298 | 1114 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
539cb660 | 1115 | |
35149e21 | 1116 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
954bbbc2 | 1117 | { |
8d4e1288 | 1118 | struct page *page[1]; |
539cb660 | 1119 | unsigned long addr; |
8d4e1288 | 1120 | int npages; |
2e2e3738 | 1121 | pfn_t pfn; |
954bbbc2 | 1122 | |
60395224 AK |
1123 | might_sleep(); |
1124 | ||
539cb660 IE |
1125 | addr = gfn_to_hva(kvm, gfn); |
1126 | if (kvm_is_error_hva(addr)) { | |
8a7ae055 | 1127 | get_page(bad_page); |
35149e21 | 1128 | return page_to_pfn(bad_page); |
8a7ae055 | 1129 | } |
8d4e1288 | 1130 | |
4c2155ce | 1131 | npages = get_user_pages_fast(addr, 1, 1, page); |
539cb660 | 1132 | |
2e2e3738 AL |
1133 | if (unlikely(npages != 1)) { |
1134 | struct vm_area_struct *vma; | |
1135 | ||
4c2155ce | 1136 | down_read(¤t->mm->mmap_sem); |
2e2e3738 | 1137 | vma = find_vma(current->mm, addr); |
4c2155ce | 1138 | |
2e2e3738 AL |
1139 | if (vma == NULL || addr < vma->vm_start || |
1140 | !(vma->vm_flags & VM_PFNMAP)) { | |
4c2155ce | 1141 | up_read(¤t->mm->mmap_sem); |
2e2e3738 AL |
1142 | get_page(bad_page); |
1143 | return page_to_pfn(bad_page); | |
1144 | } | |
1145 | ||
1146 | pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | |
4c2155ce | 1147 | up_read(¤t->mm->mmap_sem); |
c77fb9dc | 1148 | BUG_ON(!kvm_is_mmio_pfn(pfn)); |
2e2e3738 AL |
1149 | } else |
1150 | pfn = page_to_pfn(page[0]); | |
8d4e1288 | 1151 | |
2e2e3738 | 1152 | return pfn; |
35149e21 AL |
1153 | } |
1154 | ||
1155 | EXPORT_SYMBOL_GPL(gfn_to_pfn); | |
1156 | ||
1157 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
1158 | { | |
2e2e3738 AL |
1159 | pfn_t pfn; |
1160 | ||
1161 | pfn = gfn_to_pfn(kvm, gfn); | |
c77fb9dc | 1162 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 AL |
1163 | return pfn_to_page(pfn); |
1164 | ||
c77fb9dc | 1165 | WARN_ON(kvm_is_mmio_pfn(pfn)); |
2e2e3738 AL |
1166 | |
1167 | get_page(bad_page); | |
1168 | return bad_page; | |
954bbbc2 | 1169 | } |
aab61cc0 | 1170 | |
954bbbc2 AK |
1171 | EXPORT_SYMBOL_GPL(gfn_to_page); |
1172 | ||
b4231d61 IE |
1173 | void kvm_release_page_clean(struct page *page) |
1174 | { | |
35149e21 | 1175 | kvm_release_pfn_clean(page_to_pfn(page)); |
b4231d61 IE |
1176 | } |
1177 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); | |
1178 | ||
35149e21 AL |
1179 | void kvm_release_pfn_clean(pfn_t pfn) |
1180 | { | |
c77fb9dc | 1181 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 | 1182 | put_page(pfn_to_page(pfn)); |
35149e21 AL |
1183 | } |
1184 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); | |
1185 | ||
b4231d61 | 1186 | void kvm_release_page_dirty(struct page *page) |
8a7ae055 | 1187 | { |
35149e21 AL |
1188 | kvm_release_pfn_dirty(page_to_pfn(page)); |
1189 | } | |
1190 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); | |
1191 | ||
1192 | void kvm_release_pfn_dirty(pfn_t pfn) | |
1193 | { | |
1194 | kvm_set_pfn_dirty(pfn); | |
1195 | kvm_release_pfn_clean(pfn); | |
1196 | } | |
1197 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); | |
1198 | ||
1199 | void kvm_set_page_dirty(struct page *page) | |
1200 | { | |
1201 | kvm_set_pfn_dirty(page_to_pfn(page)); | |
1202 | } | |
1203 | EXPORT_SYMBOL_GPL(kvm_set_page_dirty); | |
1204 | ||
1205 | void kvm_set_pfn_dirty(pfn_t pfn) | |
1206 | { | |
c77fb9dc | 1207 | if (!kvm_is_mmio_pfn(pfn)) { |
2e2e3738 AL |
1208 | struct page *page = pfn_to_page(pfn); |
1209 | if (!PageReserved(page)) | |
1210 | SetPageDirty(page); | |
1211 | } | |
8a7ae055 | 1212 | } |
35149e21 AL |
1213 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
1214 | ||
1215 | void kvm_set_pfn_accessed(pfn_t pfn) | |
1216 | { | |
c77fb9dc | 1217 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 | 1218 | mark_page_accessed(pfn_to_page(pfn)); |
35149e21 AL |
1219 | } |
1220 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); | |
1221 | ||
1222 | void kvm_get_pfn(pfn_t pfn) | |
1223 | { | |
c77fb9dc | 1224 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 | 1225 | get_page(pfn_to_page(pfn)); |
35149e21 AL |
1226 | } |
1227 | EXPORT_SYMBOL_GPL(kvm_get_pfn); | |
8a7ae055 | 1228 | |
195aefde IE |
1229 | static int next_segment(unsigned long len, int offset) |
1230 | { | |
1231 | if (len > PAGE_SIZE - offset) | |
1232 | return PAGE_SIZE - offset; | |
1233 | else | |
1234 | return len; | |
1235 | } | |
1236 | ||
1237 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
1238 | int len) | |
1239 | { | |
e0506bcb IE |
1240 | int r; |
1241 | unsigned long addr; | |
195aefde | 1242 | |
e0506bcb IE |
1243 | addr = gfn_to_hva(kvm, gfn); |
1244 | if (kvm_is_error_hva(addr)) | |
1245 | return -EFAULT; | |
1246 | r = copy_from_user(data, (void __user *)addr + offset, len); | |
1247 | if (r) | |
195aefde | 1248 | return -EFAULT; |
195aefde IE |
1249 | return 0; |
1250 | } | |
1251 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); | |
1252 | ||
1253 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) | |
1254 | { | |
1255 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1256 | int seg; | |
1257 | int offset = offset_in_page(gpa); | |
1258 | int ret; | |
1259 | ||
1260 | while ((seg = next_segment(len, offset)) != 0) { | |
1261 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
1262 | if (ret < 0) | |
1263 | return ret; | |
1264 | offset = 0; | |
1265 | len -= seg; | |
1266 | data += seg; | |
1267 | ++gfn; | |
1268 | } | |
1269 | return 0; | |
1270 | } | |
1271 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
1272 | ||
7ec54588 MT |
1273 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
1274 | unsigned long len) | |
1275 | { | |
1276 | int r; | |
1277 | unsigned long addr; | |
1278 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1279 | int offset = offset_in_page(gpa); | |
1280 | ||
1281 | addr = gfn_to_hva(kvm, gfn); | |
1282 | if (kvm_is_error_hva(addr)) | |
1283 | return -EFAULT; | |
0aac03f0 | 1284 | pagefault_disable(); |
7ec54588 | 1285 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
0aac03f0 | 1286 | pagefault_enable(); |
7ec54588 MT |
1287 | if (r) |
1288 | return -EFAULT; | |
1289 | return 0; | |
1290 | } | |
1291 | EXPORT_SYMBOL(kvm_read_guest_atomic); | |
1292 | ||
195aefde IE |
1293 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
1294 | int offset, int len) | |
1295 | { | |
e0506bcb IE |
1296 | int r; |
1297 | unsigned long addr; | |
195aefde | 1298 | |
e0506bcb IE |
1299 | addr = gfn_to_hva(kvm, gfn); |
1300 | if (kvm_is_error_hva(addr)) | |
1301 | return -EFAULT; | |
1302 | r = copy_to_user((void __user *)addr + offset, data, len); | |
1303 | if (r) | |
195aefde | 1304 | return -EFAULT; |
195aefde IE |
1305 | mark_page_dirty(kvm, gfn); |
1306 | return 0; | |
1307 | } | |
1308 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); | |
1309 | ||
1310 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
1311 | unsigned long len) | |
1312 | { | |
1313 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1314 | int seg; | |
1315 | int offset = offset_in_page(gpa); | |
1316 | int ret; | |
1317 | ||
1318 | while ((seg = next_segment(len, offset)) != 0) { | |
1319 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
1320 | if (ret < 0) | |
1321 | return ret; | |
1322 | offset = 0; | |
1323 | len -= seg; | |
1324 | data += seg; | |
1325 | ++gfn; | |
1326 | } | |
1327 | return 0; | |
1328 | } | |
1329 | ||
1330 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) | |
1331 | { | |
3e021bf5 | 1332 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
195aefde IE |
1333 | } |
1334 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
1335 | ||
1336 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
1337 | { | |
1338 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1339 | int seg; | |
1340 | int offset = offset_in_page(gpa); | |
1341 | int ret; | |
1342 | ||
1343 | while ((seg = next_segment(len, offset)) != 0) { | |
1344 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); | |
1345 | if (ret < 0) | |
1346 | return ret; | |
1347 | offset = 0; | |
1348 | len -= seg; | |
1349 | ++gfn; | |
1350 | } | |
1351 | return 0; | |
1352 | } | |
1353 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
1354 | ||
6aa8b732 AK |
1355 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
1356 | { | |
31389947 | 1357 | struct kvm_memory_slot *memslot; |
6aa8b732 | 1358 | |
3b6fff19 | 1359 | gfn = unalias_gfn(kvm, gfn); |
2843099f | 1360 | memslot = gfn_to_memslot_unaliased(kvm, gfn); |
7e9d619d RR |
1361 | if (memslot && memslot->dirty_bitmap) { |
1362 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 1363 | |
7e9d619d RR |
1364 | /* avoid RMW */ |
1365 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
1366 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
1367 | } |
1368 | } | |
1369 | ||
b6958ce4 ED |
1370 | /* |
1371 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
1372 | */ | |
8776e519 | 1373 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 1374 | { |
e5c239cf MT |
1375 | DEFINE_WAIT(wait); |
1376 | ||
1377 | for (;;) { | |
1378 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); | |
1379 | ||
d7690175 MT |
1380 | if (kvm_cpu_has_interrupt(vcpu) || |
1381 | kvm_cpu_has_pending_timer(vcpu) || | |
1382 | kvm_arch_vcpu_runnable(vcpu)) { | |
1383 | set_bit(KVM_REQ_UNHALT, &vcpu->requests); | |
e5c239cf | 1384 | break; |
d7690175 | 1385 | } |
e5c239cf MT |
1386 | if (signal_pending(current)) |
1387 | break; | |
1388 | ||
b6958ce4 ED |
1389 | vcpu_put(vcpu); |
1390 | schedule(); | |
1391 | vcpu_load(vcpu); | |
1392 | } | |
d3bef15f | 1393 | |
e5c239cf | 1394 | finish_wait(&vcpu->wq, &wait); |
b6958ce4 ED |
1395 | } |
1396 | ||
6aa8b732 AK |
1397 | void kvm_resched(struct kvm_vcpu *vcpu) |
1398 | { | |
3fca0365 YD |
1399 | if (!need_resched()) |
1400 | return; | |
6aa8b732 | 1401 | cond_resched(); |
6aa8b732 AK |
1402 | } |
1403 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1404 | ||
e4a533a4 | 1405 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
9a2bb7f4 AK |
1406 | { |
1407 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
9a2bb7f4 AK |
1408 | struct page *page; |
1409 | ||
e4a533a4 | 1410 | if (vmf->pgoff == 0) |
039576c0 | 1411 | page = virt_to_page(vcpu->run); |
09566765 | 1412 | #ifdef CONFIG_X86 |
e4a533a4 | 1413 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
ad312c7c | 1414 | page = virt_to_page(vcpu->arch.pio_data); |
5f94c174 LV |
1415 | #endif |
1416 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
1417 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) | |
1418 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); | |
09566765 | 1419 | #endif |
039576c0 | 1420 | else |
e4a533a4 | 1421 | return VM_FAULT_SIGBUS; |
9a2bb7f4 | 1422 | get_page(page); |
e4a533a4 | 1423 | vmf->page = page; |
1424 | return 0; | |
9a2bb7f4 AK |
1425 | } |
1426 | ||
1427 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
e4a533a4 | 1428 | .fault = kvm_vcpu_fault, |
9a2bb7f4 AK |
1429 | }; |
1430 | ||
1431 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
1432 | { | |
1433 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
1434 | return 0; | |
1435 | } | |
1436 | ||
bccf2150 AK |
1437 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
1438 | { | |
1439 | struct kvm_vcpu *vcpu = filp->private_data; | |
1440 | ||
66c0b394 | 1441 | kvm_put_kvm(vcpu->kvm); |
bccf2150 AK |
1442 | return 0; |
1443 | } | |
1444 | ||
5c502742 | 1445 | static const struct file_operations kvm_vcpu_fops = { |
bccf2150 AK |
1446 | .release = kvm_vcpu_release, |
1447 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
1448 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 1449 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
1450 | }; |
1451 | ||
1452 | /* | |
1453 | * Allocates an inode for the vcpu. | |
1454 | */ | |
1455 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
1456 | { | |
7d9dbca3 | 1457 | int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); |
2030a42c | 1458 | if (fd < 0) |
66c0b394 | 1459 | kvm_put_kvm(vcpu->kvm); |
bccf2150 | 1460 | return fd; |
bccf2150 AK |
1461 | } |
1462 | ||
c5ea7660 AK |
1463 | /* |
1464 | * Creates some virtual cpus. Good luck creating more than one. | |
1465 | */ | |
1466 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
1467 | { | |
1468 | int r; | |
1469 | struct kvm_vcpu *vcpu; | |
1470 | ||
c5ea7660 | 1471 | if (!valid_vcpu(n)) |
fb3f0f51 | 1472 | return -EINVAL; |
c5ea7660 | 1473 | |
e9b11c17 | 1474 | vcpu = kvm_arch_vcpu_create(kvm, n); |
fb3f0f51 RR |
1475 | if (IS_ERR(vcpu)) |
1476 | return PTR_ERR(vcpu); | |
c5ea7660 | 1477 | |
15ad7146 AK |
1478 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
1479 | ||
26e5215f AK |
1480 | r = kvm_arch_vcpu_setup(vcpu); |
1481 | if (r) | |
7d8fece6 | 1482 | return r; |
26e5215f | 1483 | |
11ec2804 | 1484 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
1485 | if (kvm->vcpus[n]) { |
1486 | r = -EEXIST; | |
e9b11c17 | 1487 | goto vcpu_destroy; |
fb3f0f51 RR |
1488 | } |
1489 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 1490 | mutex_unlock(&kvm->lock); |
c5ea7660 | 1491 | |
fb3f0f51 | 1492 | /* Now it's all set up, let userspace reach it */ |
66c0b394 | 1493 | kvm_get_kvm(kvm); |
bccf2150 AK |
1494 | r = create_vcpu_fd(vcpu); |
1495 | if (r < 0) | |
fb3f0f51 RR |
1496 | goto unlink; |
1497 | return r; | |
39c3b86e | 1498 | |
fb3f0f51 | 1499 | unlink: |
11ec2804 | 1500 | mutex_lock(&kvm->lock); |
fb3f0f51 | 1501 | kvm->vcpus[n] = NULL; |
e9b11c17 | 1502 | vcpu_destroy: |
7d8fece6 | 1503 | mutex_unlock(&kvm->lock); |
d40ccc62 | 1504 | kvm_arch_vcpu_destroy(vcpu); |
c5ea7660 AK |
1505 | return r; |
1506 | } | |
1507 | ||
1961d276 AK |
1508 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
1509 | { | |
1510 | if (sigset) { | |
1511 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1512 | vcpu->sigset_active = 1; | |
1513 | vcpu->sigset = *sigset; | |
1514 | } else | |
1515 | vcpu->sigset_active = 0; | |
1516 | return 0; | |
1517 | } | |
1518 | ||
bccf2150 AK |
1519 | static long kvm_vcpu_ioctl(struct file *filp, |
1520 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 1521 | { |
bccf2150 | 1522 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 1523 | void __user *argp = (void __user *)arg; |
313a3dc7 | 1524 | int r; |
fa3795a7 DH |
1525 | struct kvm_fpu *fpu = NULL; |
1526 | struct kvm_sregs *kvm_sregs = NULL; | |
6aa8b732 | 1527 | |
6d4e4c4f AK |
1528 | if (vcpu->kvm->mm != current->mm) |
1529 | return -EIO; | |
6aa8b732 | 1530 | switch (ioctl) { |
9a2bb7f4 | 1531 | case KVM_RUN: |
f0fe5108 AK |
1532 | r = -EINVAL; |
1533 | if (arg) | |
1534 | goto out; | |
b6c7a5dc | 1535 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 1536 | break; |
6aa8b732 | 1537 | case KVM_GET_REGS: { |
3e4bb3ac | 1538 | struct kvm_regs *kvm_regs; |
6aa8b732 | 1539 | |
3e4bb3ac XZ |
1540 | r = -ENOMEM; |
1541 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); | |
1542 | if (!kvm_regs) | |
6aa8b732 | 1543 | goto out; |
3e4bb3ac XZ |
1544 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
1545 | if (r) | |
1546 | goto out_free1; | |
6aa8b732 | 1547 | r = -EFAULT; |
3e4bb3ac XZ |
1548 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
1549 | goto out_free1; | |
6aa8b732 | 1550 | r = 0; |
3e4bb3ac XZ |
1551 | out_free1: |
1552 | kfree(kvm_regs); | |
6aa8b732 AK |
1553 | break; |
1554 | } | |
1555 | case KVM_SET_REGS: { | |
3e4bb3ac | 1556 | struct kvm_regs *kvm_regs; |
6aa8b732 | 1557 | |
3e4bb3ac XZ |
1558 | r = -ENOMEM; |
1559 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); | |
1560 | if (!kvm_regs) | |
6aa8b732 | 1561 | goto out; |
3e4bb3ac XZ |
1562 | r = -EFAULT; |
1563 | if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs))) | |
1564 | goto out_free2; | |
1565 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); | |
6aa8b732 | 1566 | if (r) |
3e4bb3ac | 1567 | goto out_free2; |
6aa8b732 | 1568 | r = 0; |
3e4bb3ac XZ |
1569 | out_free2: |
1570 | kfree(kvm_regs); | |
6aa8b732 AK |
1571 | break; |
1572 | } | |
1573 | case KVM_GET_SREGS: { | |
fa3795a7 DH |
1574 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
1575 | r = -ENOMEM; | |
1576 | if (!kvm_sregs) | |
1577 | goto out; | |
1578 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); | |
6aa8b732 AK |
1579 | if (r) |
1580 | goto out; | |
1581 | r = -EFAULT; | |
fa3795a7 | 1582 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
6aa8b732 AK |
1583 | goto out; |
1584 | r = 0; | |
1585 | break; | |
1586 | } | |
1587 | case KVM_SET_SREGS: { | |
fa3795a7 DH |
1588 | kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
1589 | r = -ENOMEM; | |
1590 | if (!kvm_sregs) | |
1591 | goto out; | |
6aa8b732 | 1592 | r = -EFAULT; |
fa3795a7 | 1593 | if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs))) |
6aa8b732 | 1594 | goto out; |
fa3795a7 | 1595 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
6aa8b732 AK |
1596 | if (r) |
1597 | goto out; | |
1598 | r = 0; | |
1599 | break; | |
1600 | } | |
62d9f0db MT |
1601 | case KVM_GET_MP_STATE: { |
1602 | struct kvm_mp_state mp_state; | |
1603 | ||
1604 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); | |
1605 | if (r) | |
1606 | goto out; | |
1607 | r = -EFAULT; | |
1608 | if (copy_to_user(argp, &mp_state, sizeof mp_state)) | |
1609 | goto out; | |
1610 | r = 0; | |
1611 | break; | |
1612 | } | |
1613 | case KVM_SET_MP_STATE: { | |
1614 | struct kvm_mp_state mp_state; | |
1615 | ||
1616 | r = -EFAULT; | |
1617 | if (copy_from_user(&mp_state, argp, sizeof mp_state)) | |
1618 | goto out; | |
1619 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); | |
1620 | if (r) | |
1621 | goto out; | |
1622 | r = 0; | |
1623 | break; | |
1624 | } | |
6aa8b732 AK |
1625 | case KVM_TRANSLATE: { |
1626 | struct kvm_translation tr; | |
1627 | ||
1628 | r = -EFAULT; | |
2f366987 | 1629 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 1630 | goto out; |
8b006791 | 1631 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
1632 | if (r) |
1633 | goto out; | |
1634 | r = -EFAULT; | |
2f366987 | 1635 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
1636 | goto out; |
1637 | r = 0; | |
1638 | break; | |
1639 | } | |
6aa8b732 AK |
1640 | case KVM_DEBUG_GUEST: { |
1641 | struct kvm_debug_guest dbg; | |
1642 | ||
1643 | r = -EFAULT; | |
2f366987 | 1644 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 1645 | goto out; |
b6c7a5dc | 1646 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
1647 | if (r) |
1648 | goto out; | |
1649 | r = 0; | |
1650 | break; | |
1651 | } | |
1961d276 AK |
1652 | case KVM_SET_SIGNAL_MASK: { |
1653 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
1654 | struct kvm_signal_mask kvm_sigmask; | |
1655 | sigset_t sigset, *p; | |
1656 | ||
1657 | p = NULL; | |
1658 | if (argp) { | |
1659 | r = -EFAULT; | |
1660 | if (copy_from_user(&kvm_sigmask, argp, | |
1661 | sizeof kvm_sigmask)) | |
1662 | goto out; | |
1663 | r = -EINVAL; | |
1664 | if (kvm_sigmask.len != sizeof sigset) | |
1665 | goto out; | |
1666 | r = -EFAULT; | |
1667 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
1668 | sizeof sigset)) | |
1669 | goto out; | |
1670 | p = &sigset; | |
1671 | } | |
1672 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
1673 | break; | |
1674 | } | |
b8836737 | 1675 | case KVM_GET_FPU: { |
fa3795a7 DH |
1676 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
1677 | r = -ENOMEM; | |
1678 | if (!fpu) | |
1679 | goto out; | |
1680 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); | |
b8836737 AK |
1681 | if (r) |
1682 | goto out; | |
1683 | r = -EFAULT; | |
fa3795a7 | 1684 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
b8836737 AK |
1685 | goto out; |
1686 | r = 0; | |
1687 | break; | |
1688 | } | |
1689 | case KVM_SET_FPU: { | |
fa3795a7 DH |
1690 | fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
1691 | r = -ENOMEM; | |
1692 | if (!fpu) | |
1693 | goto out; | |
b8836737 | 1694 | r = -EFAULT; |
fa3795a7 | 1695 | if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu))) |
b8836737 | 1696 | goto out; |
fa3795a7 | 1697 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
b8836737 AK |
1698 | if (r) |
1699 | goto out; | |
1700 | r = 0; | |
1701 | break; | |
1702 | } | |
bccf2150 | 1703 | default: |
313a3dc7 | 1704 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
1705 | } |
1706 | out: | |
fa3795a7 DH |
1707 | kfree(fpu); |
1708 | kfree(kvm_sregs); | |
bccf2150 AK |
1709 | return r; |
1710 | } | |
1711 | ||
1712 | static long kvm_vm_ioctl(struct file *filp, | |
1713 | unsigned int ioctl, unsigned long arg) | |
1714 | { | |
1715 | struct kvm *kvm = filp->private_data; | |
1716 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 1717 | int r; |
bccf2150 | 1718 | |
6d4e4c4f AK |
1719 | if (kvm->mm != current->mm) |
1720 | return -EIO; | |
bccf2150 AK |
1721 | switch (ioctl) { |
1722 | case KVM_CREATE_VCPU: | |
1723 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
1724 | if (r < 0) | |
1725 | goto out; | |
1726 | break; | |
6fc138d2 IE |
1727 | case KVM_SET_USER_MEMORY_REGION: { |
1728 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1729 | ||
1730 | r = -EFAULT; | |
1731 | if (copy_from_user(&kvm_userspace_mem, argp, | |
1732 | sizeof kvm_userspace_mem)) | |
1733 | goto out; | |
1734 | ||
1735 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); | |
6aa8b732 AK |
1736 | if (r) |
1737 | goto out; | |
1738 | break; | |
1739 | } | |
1740 | case KVM_GET_DIRTY_LOG: { | |
1741 | struct kvm_dirty_log log; | |
1742 | ||
1743 | r = -EFAULT; | |
2f366987 | 1744 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 1745 | goto out; |
2c6f5df9 | 1746 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
1747 | if (r) |
1748 | goto out; | |
1749 | break; | |
1750 | } | |
5f94c174 LV |
1751 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
1752 | case KVM_REGISTER_COALESCED_MMIO: { | |
1753 | struct kvm_coalesced_mmio_zone zone; | |
1754 | r = -EFAULT; | |
1755 | if (copy_from_user(&zone, argp, sizeof zone)) | |
1756 | goto out; | |
1757 | r = -ENXIO; | |
1758 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); | |
1759 | if (r) | |
1760 | goto out; | |
1761 | r = 0; | |
1762 | break; | |
1763 | } | |
1764 | case KVM_UNREGISTER_COALESCED_MMIO: { | |
1765 | struct kvm_coalesced_mmio_zone zone; | |
1766 | r = -EFAULT; | |
1767 | if (copy_from_user(&zone, argp, sizeof zone)) | |
1768 | goto out; | |
1769 | r = -ENXIO; | |
1770 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); | |
1771 | if (r) | |
1772 | goto out; | |
1773 | r = 0; | |
1774 | break; | |
1775 | } | |
8a98f664 XZ |
1776 | #endif |
1777 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT | |
1778 | case KVM_ASSIGN_PCI_DEVICE: { | |
1779 | struct kvm_assigned_pci_dev assigned_dev; | |
1780 | ||
1781 | r = -EFAULT; | |
1782 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) | |
1783 | goto out; | |
1784 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); | |
1785 | if (r) | |
1786 | goto out; | |
1787 | break; | |
1788 | } | |
1789 | case KVM_ASSIGN_IRQ: { | |
1790 | struct kvm_assigned_irq assigned_irq; | |
1791 | ||
1792 | r = -EFAULT; | |
1793 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) | |
1794 | goto out; | |
1795 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); | |
1796 | if (r) | |
1797 | goto out; | |
1798 | break; | |
1799 | } | |
5f94c174 | 1800 | #endif |
f17abe9a | 1801 | default: |
1fe779f8 | 1802 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
1803 | } |
1804 | out: | |
1805 | return r; | |
1806 | } | |
1807 | ||
e4a533a4 | 1808 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
f17abe9a | 1809 | { |
777b3f49 MT |
1810 | struct page *page[1]; |
1811 | unsigned long addr; | |
1812 | int npages; | |
1813 | gfn_t gfn = vmf->pgoff; | |
f17abe9a | 1814 | struct kvm *kvm = vma->vm_file->private_data; |
f17abe9a | 1815 | |
777b3f49 MT |
1816 | addr = gfn_to_hva(kvm, gfn); |
1817 | if (kvm_is_error_hva(addr)) | |
e4a533a4 | 1818 | return VM_FAULT_SIGBUS; |
777b3f49 MT |
1819 | |
1820 | npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page, | |
1821 | NULL); | |
1822 | if (unlikely(npages != 1)) | |
e4a533a4 | 1823 | return VM_FAULT_SIGBUS; |
777b3f49 MT |
1824 | |
1825 | vmf->page = page[0]; | |
e4a533a4 | 1826 | return 0; |
f17abe9a AK |
1827 | } |
1828 | ||
1829 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
e4a533a4 | 1830 | .fault = kvm_vm_fault, |
f17abe9a AK |
1831 | }; |
1832 | ||
1833 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
1834 | { | |
1835 | vma->vm_ops = &kvm_vm_vm_ops; | |
1836 | return 0; | |
1837 | } | |
1838 | ||
5c502742 | 1839 | static const struct file_operations kvm_vm_fops = { |
f17abe9a AK |
1840 | .release = kvm_vm_release, |
1841 | .unlocked_ioctl = kvm_vm_ioctl, | |
1842 | .compat_ioctl = kvm_vm_ioctl, | |
1843 | .mmap = kvm_vm_mmap, | |
1844 | }; | |
1845 | ||
1846 | static int kvm_dev_ioctl_create_vm(void) | |
1847 | { | |
2030a42c | 1848 | int fd; |
f17abe9a AK |
1849 | struct kvm *kvm; |
1850 | ||
f17abe9a | 1851 | kvm = kvm_create_vm(); |
d6d28168 AK |
1852 | if (IS_ERR(kvm)) |
1853 | return PTR_ERR(kvm); | |
7d9dbca3 | 1854 | fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0); |
2030a42c | 1855 | if (fd < 0) |
66c0b394 | 1856 | kvm_put_kvm(kvm); |
f17abe9a | 1857 | |
f17abe9a | 1858 | return fd; |
f17abe9a AK |
1859 | } |
1860 | ||
1861 | static long kvm_dev_ioctl(struct file *filp, | |
1862 | unsigned int ioctl, unsigned long arg) | |
1863 | { | |
07c45a36 | 1864 | long r = -EINVAL; |
f17abe9a AK |
1865 | |
1866 | switch (ioctl) { | |
1867 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
1868 | r = -EINVAL; |
1869 | if (arg) | |
1870 | goto out; | |
f17abe9a AK |
1871 | r = KVM_API_VERSION; |
1872 | break; | |
1873 | case KVM_CREATE_VM: | |
f0fe5108 AK |
1874 | r = -EINVAL; |
1875 | if (arg) | |
1876 | goto out; | |
f17abe9a AK |
1877 | r = kvm_dev_ioctl_create_vm(); |
1878 | break; | |
018d00d2 | 1879 | case KVM_CHECK_EXTENSION: |
1e1c65e0 | 1880 | r = kvm_dev_ioctl_check_extension(arg); |
5d308f45 | 1881 | break; |
07c45a36 AK |
1882 | case KVM_GET_VCPU_MMAP_SIZE: |
1883 | r = -EINVAL; | |
1884 | if (arg) | |
1885 | goto out; | |
adb1ff46 AK |
1886 | r = PAGE_SIZE; /* struct kvm_run */ |
1887 | #ifdef CONFIG_X86 | |
1888 | r += PAGE_SIZE; /* pio data page */ | |
5f94c174 LV |
1889 | #endif |
1890 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
1891 | r += PAGE_SIZE; /* coalesced mmio ring page */ | |
adb1ff46 | 1892 | #endif |
07c45a36 | 1893 | break; |
d4c9ff2d FEL |
1894 | case KVM_TRACE_ENABLE: |
1895 | case KVM_TRACE_PAUSE: | |
1896 | case KVM_TRACE_DISABLE: | |
1897 | r = kvm_trace_ioctl(ioctl, arg); | |
1898 | break; | |
6aa8b732 | 1899 | default: |
043405e1 | 1900 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
1901 | } |
1902 | out: | |
1903 | return r; | |
1904 | } | |
1905 | ||
6aa8b732 | 1906 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
1907 | .unlocked_ioctl = kvm_dev_ioctl, |
1908 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
1909 | }; |
1910 | ||
1911 | static struct miscdevice kvm_dev = { | |
bbe4432e | 1912 | KVM_MINOR, |
6aa8b732 AK |
1913 | "kvm", |
1914 | &kvm_chardev_ops, | |
1915 | }; | |
1916 | ||
1b6c0168 AK |
1917 | static void hardware_enable(void *junk) |
1918 | { | |
1919 | int cpu = raw_smp_processor_id(); | |
1920 | ||
1921 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
1922 | return; | |
1923 | cpu_set(cpu, cpus_hardware_enabled); | |
e9b11c17 | 1924 | kvm_arch_hardware_enable(NULL); |
1b6c0168 AK |
1925 | } |
1926 | ||
1927 | static void hardware_disable(void *junk) | |
1928 | { | |
1929 | int cpu = raw_smp_processor_id(); | |
1930 | ||
1931 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
1932 | return; | |
1933 | cpu_clear(cpu, cpus_hardware_enabled); | |
e9b11c17 | 1934 | kvm_arch_hardware_disable(NULL); |
1b6c0168 AK |
1935 | } |
1936 | ||
774c47f1 AK |
1937 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
1938 | void *v) | |
1939 | { | |
1940 | int cpu = (long)v; | |
1941 | ||
1a6f4d7f | 1942 | val &= ~CPU_TASKS_FROZEN; |
774c47f1 | 1943 | switch (val) { |
cec9ad27 | 1944 | case CPU_DYING: |
6ec8a856 AK |
1945 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1946 | cpu); | |
1947 | hardware_disable(NULL); | |
1948 | break; | |
774c47f1 | 1949 | case CPU_UP_CANCELED: |
43934a38 JK |
1950 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1951 | cpu); | |
8691e5a8 | 1952 | smp_call_function_single(cpu, hardware_disable, NULL, 1); |
774c47f1 | 1953 | break; |
43934a38 JK |
1954 | case CPU_ONLINE: |
1955 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", | |
1956 | cpu); | |
8691e5a8 | 1957 | smp_call_function_single(cpu, hardware_enable, NULL, 1); |
774c47f1 AK |
1958 | break; |
1959 | } | |
1960 | return NOTIFY_OK; | |
1961 | } | |
1962 | ||
4ecac3fd AK |
1963 | |
1964 | asmlinkage void kvm_handle_fault_on_reboot(void) | |
1965 | { | |
1966 | if (kvm_rebooting) | |
1967 | /* spin while reset goes on */ | |
1968 | while (true) | |
1969 | ; | |
1970 | /* Fault while not rebooting. We want the trace. */ | |
1971 | BUG(); | |
1972 | } | |
1973 | EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot); | |
1974 | ||
9a2b85c6 | 1975 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 1976 | void *v) |
9a2b85c6 RR |
1977 | { |
1978 | if (val == SYS_RESTART) { | |
1979 | /* | |
1980 | * Some (well, at least mine) BIOSes hang on reboot if | |
1981 | * in vmx root mode. | |
1982 | */ | |
1983 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
4ecac3fd | 1984 | kvm_rebooting = true; |
15c8b6c1 | 1985 | on_each_cpu(hardware_disable, NULL, 1); |
9a2b85c6 RR |
1986 | } |
1987 | return NOTIFY_OK; | |
1988 | } | |
1989 | ||
1990 | static struct notifier_block kvm_reboot_notifier = { | |
1991 | .notifier_call = kvm_reboot, | |
1992 | .priority = 0, | |
1993 | }; | |
1994 | ||
2eeb2e94 GH |
1995 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
1996 | { | |
1997 | memset(bus, 0, sizeof(*bus)); | |
1998 | } | |
1999 | ||
2000 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
2001 | { | |
2002 | int i; | |
2003 | ||
2004 | for (i = 0; i < bus->dev_count; i++) { | |
2005 | struct kvm_io_device *pos = bus->devs[i]; | |
2006 | ||
2007 | kvm_iodevice_destructor(pos); | |
2008 | } | |
2009 | } | |
2010 | ||
92760499 LV |
2011 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, |
2012 | gpa_t addr, int len, int is_write) | |
2eeb2e94 GH |
2013 | { |
2014 | int i; | |
2015 | ||
2016 | for (i = 0; i < bus->dev_count; i++) { | |
2017 | struct kvm_io_device *pos = bus->devs[i]; | |
2018 | ||
92760499 | 2019 | if (pos->in_range(pos, addr, len, is_write)) |
2eeb2e94 GH |
2020 | return pos; |
2021 | } | |
2022 | ||
2023 | return NULL; | |
2024 | } | |
2025 | ||
2026 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
2027 | { | |
2028 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
2029 | ||
2030 | bus->devs[bus->dev_count++] = dev; | |
2031 | } | |
2032 | ||
774c47f1 AK |
2033 | static struct notifier_block kvm_cpu_notifier = { |
2034 | .notifier_call = kvm_cpu_hotplug, | |
2035 | .priority = 20, /* must be > scheduler priority */ | |
2036 | }; | |
2037 | ||
8b88b099 | 2038 | static int vm_stat_get(void *_offset, u64 *val) |
ba1389b7 AK |
2039 | { |
2040 | unsigned offset = (long)_offset; | |
ba1389b7 AK |
2041 | struct kvm *kvm; |
2042 | ||
8b88b099 | 2043 | *val = 0; |
ba1389b7 AK |
2044 | spin_lock(&kvm_lock); |
2045 | list_for_each_entry(kvm, &vm_list, vm_list) | |
8b88b099 | 2046 | *val += *(u32 *)((void *)kvm + offset); |
ba1389b7 | 2047 | spin_unlock(&kvm_lock); |
8b88b099 | 2048 | return 0; |
ba1389b7 AK |
2049 | } |
2050 | ||
2051 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); | |
2052 | ||
8b88b099 | 2053 | static int vcpu_stat_get(void *_offset, u64 *val) |
1165f5fe AK |
2054 | { |
2055 | unsigned offset = (long)_offset; | |
1165f5fe AK |
2056 | struct kvm *kvm; |
2057 | struct kvm_vcpu *vcpu; | |
2058 | int i; | |
2059 | ||
8b88b099 | 2060 | *val = 0; |
1165f5fe AK |
2061 | spin_lock(&kvm_lock); |
2062 | list_for_each_entry(kvm, &vm_list, vm_list) | |
2063 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
2064 | vcpu = kvm->vcpus[i]; |
2065 | if (vcpu) | |
8b88b099 | 2066 | *val += *(u32 *)((void *)vcpu + offset); |
1165f5fe AK |
2067 | } |
2068 | spin_unlock(&kvm_lock); | |
8b88b099 | 2069 | return 0; |
1165f5fe AK |
2070 | } |
2071 | ||
ba1389b7 AK |
2072 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
2073 | ||
2074 | static struct file_operations *stat_fops[] = { | |
2075 | [KVM_STAT_VCPU] = &vcpu_stat_fops, | |
2076 | [KVM_STAT_VM] = &vm_stat_fops, | |
2077 | }; | |
1165f5fe | 2078 | |
a16b043c | 2079 | static void kvm_init_debug(void) |
6aa8b732 AK |
2080 | { |
2081 | struct kvm_stats_debugfs_item *p; | |
2082 | ||
76f7c879 | 2083 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 2084 | for (p = debugfs_entries; p->name; ++p) |
76f7c879 | 2085 | p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir, |
1165f5fe | 2086 | (void *)(long)p->offset, |
ba1389b7 | 2087 | stat_fops[p->kind]); |
6aa8b732 AK |
2088 | } |
2089 | ||
2090 | static void kvm_exit_debug(void) | |
2091 | { | |
2092 | struct kvm_stats_debugfs_item *p; | |
2093 | ||
2094 | for (p = debugfs_entries; p->name; ++p) | |
2095 | debugfs_remove(p->dentry); | |
76f7c879 | 2096 | debugfs_remove(kvm_debugfs_dir); |
6aa8b732 AK |
2097 | } |
2098 | ||
59ae6c6b AK |
2099 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
2100 | { | |
4267c41a | 2101 | hardware_disable(NULL); |
59ae6c6b AK |
2102 | return 0; |
2103 | } | |
2104 | ||
2105 | static int kvm_resume(struct sys_device *dev) | |
2106 | { | |
4267c41a | 2107 | hardware_enable(NULL); |
59ae6c6b AK |
2108 | return 0; |
2109 | } | |
2110 | ||
2111 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 2112 | .name = "kvm", |
59ae6c6b AK |
2113 | .suspend = kvm_suspend, |
2114 | .resume = kvm_resume, | |
2115 | }; | |
2116 | ||
2117 | static struct sys_device kvm_sysdev = { | |
2118 | .id = 0, | |
2119 | .cls = &kvm_sysdev_class, | |
2120 | }; | |
2121 | ||
cea7bb21 | 2122 | struct page *bad_page; |
35149e21 | 2123 | pfn_t bad_pfn; |
6aa8b732 | 2124 | |
15ad7146 AK |
2125 | static inline |
2126 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
2127 | { | |
2128 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
2129 | } | |
2130 | ||
2131 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
2132 | { | |
2133 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
2134 | ||
e9b11c17 | 2135 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 AK |
2136 | } |
2137 | ||
2138 | static void kvm_sched_out(struct preempt_notifier *pn, | |
2139 | struct task_struct *next) | |
2140 | { | |
2141 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
2142 | ||
e9b11c17 | 2143 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
2144 | } |
2145 | ||
f8c16bba | 2146 | int kvm_init(void *opaque, unsigned int vcpu_size, |
c16f862d | 2147 | struct module *module) |
6aa8b732 AK |
2148 | { |
2149 | int r; | |
002c7f7c | 2150 | int cpu; |
6aa8b732 | 2151 | |
cb498ea2 ZX |
2152 | kvm_init_debug(); |
2153 | ||
f8c16bba ZX |
2154 | r = kvm_arch_init(opaque); |
2155 | if (r) | |
d2308784 | 2156 | goto out_fail; |
cb498ea2 ZX |
2157 | |
2158 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
2159 | ||
2160 | if (bad_page == NULL) { | |
2161 | r = -ENOMEM; | |
2162 | goto out; | |
2163 | } | |
2164 | ||
35149e21 AL |
2165 | bad_pfn = page_to_pfn(bad_page); |
2166 | ||
e9b11c17 | 2167 | r = kvm_arch_hardware_setup(); |
6aa8b732 | 2168 | if (r < 0) |
d2308784 | 2169 | goto out_free_0; |
6aa8b732 | 2170 | |
002c7f7c YS |
2171 | for_each_online_cpu(cpu) { |
2172 | smp_call_function_single(cpu, | |
e9b11c17 | 2173 | kvm_arch_check_processor_compat, |
8691e5a8 | 2174 | &r, 1); |
002c7f7c | 2175 | if (r < 0) |
d2308784 | 2176 | goto out_free_1; |
002c7f7c YS |
2177 | } |
2178 | ||
15c8b6c1 | 2179 | on_each_cpu(hardware_enable, NULL, 1); |
774c47f1 AK |
2180 | r = register_cpu_notifier(&kvm_cpu_notifier); |
2181 | if (r) | |
d2308784 | 2182 | goto out_free_2; |
6aa8b732 AK |
2183 | register_reboot_notifier(&kvm_reboot_notifier); |
2184 | ||
59ae6c6b AK |
2185 | r = sysdev_class_register(&kvm_sysdev_class); |
2186 | if (r) | |
d2308784 | 2187 | goto out_free_3; |
59ae6c6b AK |
2188 | |
2189 | r = sysdev_register(&kvm_sysdev); | |
2190 | if (r) | |
d2308784 | 2191 | goto out_free_4; |
59ae6c6b | 2192 | |
c16f862d RR |
2193 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
2194 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
56919c5c JP |
2195 | __alignof__(struct kvm_vcpu), |
2196 | 0, NULL); | |
c16f862d RR |
2197 | if (!kvm_vcpu_cache) { |
2198 | r = -ENOMEM; | |
d2308784 | 2199 | goto out_free_5; |
c16f862d RR |
2200 | } |
2201 | ||
6aa8b732 AK |
2202 | kvm_chardev_ops.owner = module; |
2203 | ||
2204 | r = misc_register(&kvm_dev); | |
2205 | if (r) { | |
d77c26fc | 2206 | printk(KERN_ERR "kvm: misc device register failed\n"); |
6aa8b732 AK |
2207 | goto out_free; |
2208 | } | |
2209 | ||
15ad7146 AK |
2210 | kvm_preempt_ops.sched_in = kvm_sched_in; |
2211 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
2212 | ||
c7addb90 | 2213 | return 0; |
6aa8b732 AK |
2214 | |
2215 | out_free: | |
c16f862d | 2216 | kmem_cache_destroy(kvm_vcpu_cache); |
d2308784 | 2217 | out_free_5: |
59ae6c6b | 2218 | sysdev_unregister(&kvm_sysdev); |
d2308784 | 2219 | out_free_4: |
59ae6c6b | 2220 | sysdev_class_unregister(&kvm_sysdev_class); |
d2308784 | 2221 | out_free_3: |
6aa8b732 | 2222 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 | 2223 | unregister_cpu_notifier(&kvm_cpu_notifier); |
d2308784 | 2224 | out_free_2: |
15c8b6c1 | 2225 | on_each_cpu(hardware_disable, NULL, 1); |
d2308784 | 2226 | out_free_1: |
e9b11c17 | 2227 | kvm_arch_hardware_unsetup(); |
d2308784 ZX |
2228 | out_free_0: |
2229 | __free_page(bad_page); | |
ca45aaae | 2230 | out: |
f8c16bba | 2231 | kvm_arch_exit(); |
cb498ea2 | 2232 | kvm_exit_debug(); |
d2308784 | 2233 | out_fail: |
6aa8b732 AK |
2234 | return r; |
2235 | } | |
cb498ea2 | 2236 | EXPORT_SYMBOL_GPL(kvm_init); |
6aa8b732 | 2237 | |
cb498ea2 | 2238 | void kvm_exit(void) |
6aa8b732 | 2239 | { |
d4c9ff2d | 2240 | kvm_trace_cleanup(); |
6aa8b732 | 2241 | misc_deregister(&kvm_dev); |
c16f862d | 2242 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
2243 | sysdev_unregister(&kvm_sysdev); |
2244 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 2245 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 2246 | unregister_cpu_notifier(&kvm_cpu_notifier); |
15c8b6c1 | 2247 | on_each_cpu(hardware_disable, NULL, 1); |
e9b11c17 | 2248 | kvm_arch_hardware_unsetup(); |
f8c16bba | 2249 | kvm_arch_exit(); |
6aa8b732 | 2250 | kvm_exit_debug(); |
cea7bb21 | 2251 | __free_page(bad_page); |
6aa8b732 | 2252 | } |
cb498ea2 | 2253 | EXPORT_SYMBOL_GPL(kvm_exit); |