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