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 | ||
18 | #include "kvm.h" | |
043405e1 | 19 | #include "x86.h" |
e495606d | 20 | #include "x86_emulate.h" |
85f455f7 | 21 | #include "irq.h" |
6aa8b732 AK |
22 | |
23 | #include <linux/kvm.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/errno.h> | |
6aa8b732 AK |
26 | #include <linux/percpu.h> |
27 | #include <linux/gfp.h> | |
6aa8b732 AK |
28 | #include <linux/mm.h> |
29 | #include <linux/miscdevice.h> | |
30 | #include <linux/vmalloc.h> | |
6aa8b732 | 31 | #include <linux/reboot.h> |
6aa8b732 AK |
32 | #include <linux/debugfs.h> |
33 | #include <linux/highmem.h> | |
34 | #include <linux/file.h> | |
59ae6c6b | 35 | #include <linux/sysdev.h> |
774c47f1 | 36 | #include <linux/cpu.h> |
e8edc6e0 | 37 | #include <linux/sched.h> |
d9e368d6 AK |
38 | #include <linux/cpumask.h> |
39 | #include <linux/smp.h> | |
d6d28168 | 40 | #include <linux/anon_inodes.h> |
04d2cc77 | 41 | #include <linux/profile.h> |
7aa81cc0 | 42 | #include <linux/kvm_para.h> |
6fc138d2 | 43 | #include <linux/pagemap.h> |
8d4e1288 | 44 | #include <linux/mman.h> |
6aa8b732 | 45 | |
e495606d AK |
46 | #include <asm/processor.h> |
47 | #include <asm/msr.h> | |
48 | #include <asm/io.h> | |
49 | #include <asm/uaccess.h> | |
50 | #include <asm/desc.h> | |
6aa8b732 AK |
51 | |
52 | MODULE_AUTHOR("Qumranet"); | |
53 | MODULE_LICENSE("GPL"); | |
54 | ||
133de902 AK |
55 | static DEFINE_SPINLOCK(kvm_lock); |
56 | static LIST_HEAD(vm_list); | |
57 | ||
1b6c0168 AK |
58 | static cpumask_t cpus_hardware_enabled; |
59 | ||
cbdd1bea | 60 | struct kvm_x86_ops *kvm_x86_ops; |
c16f862d RR |
61 | struct kmem_cache *kvm_vcpu_cache; |
62 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 63 | |
15ad7146 AK |
64 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
65 | ||
6aa8b732 AK |
66 | static struct dentry *debugfs_dir; |
67 | ||
bccf2150 AK |
68 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
69 | unsigned long arg); | |
70 | ||
5aacf0ca JM |
71 | static inline int valid_vcpu(int n) |
72 | { | |
73 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
74 | } | |
75 | ||
bccf2150 AK |
76 | /* |
77 | * Switches to specified vcpu, until a matching vcpu_put() | |
78 | */ | |
313a3dc7 | 79 | void vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 80 | { |
15ad7146 AK |
81 | int cpu; |
82 | ||
bccf2150 | 83 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
84 | cpu = get_cpu(); |
85 | preempt_notifier_register(&vcpu->preempt_notifier); | |
313a3dc7 | 86 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 87 | put_cpu(); |
6aa8b732 AK |
88 | } |
89 | ||
313a3dc7 | 90 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 91 | { |
15ad7146 | 92 | preempt_disable(); |
313a3dc7 | 93 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
94 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
95 | preempt_enable(); | |
6aa8b732 AK |
96 | mutex_unlock(&vcpu->mutex); |
97 | } | |
98 | ||
d9e368d6 AK |
99 | static void ack_flush(void *_completed) |
100 | { | |
d9e368d6 AK |
101 | } |
102 | ||
103 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
104 | { | |
49d3bd7e | 105 | int i, cpu; |
d9e368d6 AK |
106 | cpumask_t cpus; |
107 | struct kvm_vcpu *vcpu; | |
d9e368d6 | 108 | |
d9e368d6 | 109 | cpus_clear(cpus); |
fb3f0f51 RR |
110 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
111 | vcpu = kvm->vcpus[i]; | |
112 | if (!vcpu) | |
113 | continue; | |
3176bc3e | 114 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
d9e368d6 AK |
115 | continue; |
116 | cpu = vcpu->cpu; | |
117 | if (cpu != -1 && cpu != raw_smp_processor_id()) | |
49d3bd7e | 118 | cpu_set(cpu, cpus); |
d9e368d6 | 119 | } |
49d3bd7e | 120 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
d9e368d6 AK |
121 | } |
122 | ||
fb3f0f51 RR |
123 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
124 | { | |
125 | struct page *page; | |
126 | int r; | |
127 | ||
128 | mutex_init(&vcpu->mutex); | |
129 | vcpu->cpu = -1; | |
130 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
131 | vcpu->kvm = kvm; | |
132 | vcpu->vcpu_id = id; | |
c5ec1534 HQ |
133 | if (!irqchip_in_kernel(kvm) || id == 0) |
134 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
135 | else | |
136 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; | |
b6958ce4 | 137 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
138 | |
139 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
140 | if (!page) { | |
141 | r = -ENOMEM; | |
142 | goto fail; | |
143 | } | |
144 | vcpu->run = page_address(page); | |
145 | ||
146 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
147 | if (!page) { | |
148 | r = -ENOMEM; | |
149 | goto fail_free_run; | |
150 | } | |
151 | vcpu->pio_data = page_address(page); | |
152 | ||
fb3f0f51 RR |
153 | r = kvm_mmu_create(vcpu); |
154 | if (r < 0) | |
155 | goto fail_free_pio_data; | |
156 | ||
76fafa5e RR |
157 | if (irqchip_in_kernel(kvm)) { |
158 | r = kvm_create_lapic(vcpu); | |
159 | if (r < 0) | |
160 | goto fail_mmu_destroy; | |
161 | } | |
162 | ||
fb3f0f51 RR |
163 | return 0; |
164 | ||
76fafa5e RR |
165 | fail_mmu_destroy: |
166 | kvm_mmu_destroy(vcpu); | |
fb3f0f51 RR |
167 | fail_free_pio_data: |
168 | free_page((unsigned long)vcpu->pio_data); | |
169 | fail_free_run: | |
170 | free_page((unsigned long)vcpu->run); | |
171 | fail: | |
76fafa5e | 172 | return r; |
fb3f0f51 RR |
173 | } |
174 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
175 | ||
176 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
177 | { | |
d589444e | 178 | kvm_free_lapic(vcpu); |
fb3f0f51 RR |
179 | kvm_mmu_destroy(vcpu); |
180 | free_page((unsigned long)vcpu->pio_data); | |
181 | free_page((unsigned long)vcpu->run); | |
182 | } | |
183 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
184 | ||
f17abe9a | 185 | static struct kvm *kvm_create_vm(void) |
6aa8b732 AK |
186 | { |
187 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
6aa8b732 AK |
188 | |
189 | if (!kvm) | |
f17abe9a | 190 | return ERR_PTR(-ENOMEM); |
6aa8b732 | 191 | |
74906345 | 192 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 193 | mutex_init(&kvm->lock); |
6aa8b732 | 194 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
2eeb2e94 | 195 | kvm_io_bus_init(&kvm->mmio_bus); |
5e58cfe4 RR |
196 | spin_lock(&kvm_lock); |
197 | list_add(&kvm->vm_list, &vm_list); | |
198 | spin_unlock(&kvm_lock); | |
f17abe9a AK |
199 | return kvm; |
200 | } | |
201 | ||
6aa8b732 AK |
202 | /* |
203 | * Free any memory in @free but not in @dont. | |
204 | */ | |
205 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
206 | struct kvm_memory_slot *dont) | |
207 | { | |
290fc38d IE |
208 | if (!dont || free->rmap != dont->rmap) |
209 | vfree(free->rmap); | |
6aa8b732 AK |
210 | |
211 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
212 | vfree(free->dirty_bitmap); | |
213 | ||
6aa8b732 | 214 | free->npages = 0; |
8b6d44c7 | 215 | free->dirty_bitmap = NULL; |
8d4e1288 | 216 | free->rmap = NULL; |
6aa8b732 AK |
217 | } |
218 | ||
219 | static void kvm_free_physmem(struct kvm *kvm) | |
220 | { | |
221 | int i; | |
222 | ||
223 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 224 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
225 | } |
226 | ||
7b53aa56 AK |
227 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
228 | { | |
7b53aa56 AK |
229 | vcpu_load(vcpu); |
230 | kvm_mmu_unload(vcpu); | |
231 | vcpu_put(vcpu); | |
232 | } | |
233 | ||
6aa8b732 AK |
234 | static void kvm_free_vcpus(struct kvm *kvm) |
235 | { | |
236 | unsigned int i; | |
237 | ||
7b53aa56 AK |
238 | /* |
239 | * Unpin any mmu pages first. | |
240 | */ | |
241 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
fb3f0f51 RR |
242 | if (kvm->vcpus[i]) |
243 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); | |
244 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
245 | if (kvm->vcpus[i]) { | |
cbdd1bea | 246 | kvm_x86_ops->vcpu_free(kvm->vcpus[i]); |
fb3f0f51 RR |
247 | kvm->vcpus[i] = NULL; |
248 | } | |
249 | } | |
250 | ||
6aa8b732 AK |
251 | } |
252 | ||
f17abe9a AK |
253 | static void kvm_destroy_vm(struct kvm *kvm) |
254 | { | |
133de902 AK |
255 | spin_lock(&kvm_lock); |
256 | list_del(&kvm->vm_list); | |
257 | spin_unlock(&kvm_lock); | |
74906345 | 258 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 259 | kvm_io_bus_destroy(&kvm->mmio_bus); |
85f455f7 | 260 | kfree(kvm->vpic); |
1fd4f2a5 | 261 | kfree(kvm->vioapic); |
6aa8b732 AK |
262 | kvm_free_vcpus(kvm); |
263 | kvm_free_physmem(kvm); | |
264 | kfree(kvm); | |
f17abe9a AK |
265 | } |
266 | ||
267 | static int kvm_vm_release(struct inode *inode, struct file *filp) | |
268 | { | |
269 | struct kvm *kvm = filp->private_data; | |
270 | ||
271 | kvm_destroy_vm(kvm); | |
6aa8b732 AK |
272 | return 0; |
273 | } | |
274 | ||
6aa8b732 AK |
275 | /* |
276 | * Allocate some memory and give it an address in the guest physical address | |
277 | * space. | |
278 | * | |
279 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e SY |
280 | * |
281 | * Must be called holding kvm->lock. | |
6aa8b732 | 282 | */ |
f78e0e2e SY |
283 | int __kvm_set_memory_region(struct kvm *kvm, |
284 | struct kvm_userspace_memory_region *mem, | |
285 | int user_alloc) | |
6aa8b732 AK |
286 | { |
287 | int r; | |
288 | gfn_t base_gfn; | |
289 | unsigned long npages; | |
290 | unsigned long i; | |
291 | struct kvm_memory_slot *memslot; | |
292 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
293 | |
294 | r = -EINVAL; | |
295 | /* General sanity checks */ | |
296 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
297 | goto out; | |
298 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
299 | goto out; | |
e0d62c7f | 300 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
6aa8b732 AK |
301 | goto out; |
302 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
303 | goto out; | |
304 | ||
305 | memslot = &kvm->memslots[mem->slot]; | |
306 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
307 | npages = mem->memory_size >> PAGE_SHIFT; | |
308 | ||
309 | if (!npages) | |
310 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
311 | ||
6aa8b732 AK |
312 | new = old = *memslot; |
313 | ||
314 | new.base_gfn = base_gfn; | |
315 | new.npages = npages; | |
316 | new.flags = mem->flags; | |
317 | ||
318 | /* Disallow changing a memory slot's size. */ | |
319 | r = -EINVAL; | |
320 | if (npages && old.npages && npages != old.npages) | |
f78e0e2e | 321 | goto out_free; |
6aa8b732 AK |
322 | |
323 | /* Check for overlaps */ | |
324 | r = -EEXIST; | |
325 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
326 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
327 | ||
328 | if (s == memslot) | |
329 | continue; | |
330 | if (!((base_gfn + npages <= s->base_gfn) || | |
331 | (base_gfn >= s->base_gfn + s->npages))) | |
f78e0e2e | 332 | goto out_free; |
6aa8b732 | 333 | } |
6aa8b732 | 334 | |
6aa8b732 AK |
335 | /* Free page dirty bitmap if unneeded */ |
336 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 337 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
338 | |
339 | r = -ENOMEM; | |
340 | ||
341 | /* Allocate if a slot is being created */ | |
8d4e1288 | 342 | if (npages && !new.rmap) { |
d77c26fc | 343 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
290fc38d IE |
344 | |
345 | if (!new.rmap) | |
f78e0e2e | 346 | goto out_free; |
290fc38d | 347 | |
290fc38d | 348 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
8d4e1288 | 349 | |
80b14b5b | 350 | new.user_alloc = user_alloc; |
8d4e1288 | 351 | if (user_alloc) |
8a7ae055 | 352 | new.userspace_addr = mem->userspace_addr; |
8d4e1288 AL |
353 | else { |
354 | down_write(¤t->mm->mmap_sem); | |
355 | new.userspace_addr = do_mmap(NULL, 0, | |
356 | npages * PAGE_SIZE, | |
357 | PROT_READ | PROT_WRITE, | |
358 | MAP_SHARED | MAP_ANONYMOUS, | |
359 | 0); | |
360 | up_write(¤t->mm->mmap_sem); | |
361 | ||
362 | if (IS_ERR((void *)new.userspace_addr)) | |
f78e0e2e | 363 | goto out_free; |
6aa8b732 | 364 | } |
80b14b5b IE |
365 | } else { |
366 | if (!old.user_alloc && old.rmap) { | |
367 | int ret; | |
368 | ||
369 | down_write(¤t->mm->mmap_sem); | |
370 | ret = do_munmap(current->mm, old.userspace_addr, | |
371 | old.npages * PAGE_SIZE); | |
372 | up_write(¤t->mm->mmap_sem); | |
373 | if (ret < 0) | |
374 | printk(KERN_WARNING | |
375 | "kvm_vm_ioctl_set_memory_region: " | |
376 | "failed to munmap memory\n"); | |
377 | } | |
6aa8b732 AK |
378 | } |
379 | ||
380 | /* Allocate page dirty bitmap if needed */ | |
381 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
382 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
383 | ||
384 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
385 | if (!new.dirty_bitmap) | |
f78e0e2e | 386 | goto out_free; |
6aa8b732 AK |
387 | memset(new.dirty_bitmap, 0, dirty_bytes); |
388 | } | |
389 | ||
6aa8b732 AK |
390 | if (mem->slot >= kvm->nmemslots) |
391 | kvm->nmemslots = mem->slot + 1; | |
392 | ||
82ce2c96 IE |
393 | if (!kvm->n_requested_mmu_pages) { |
394 | unsigned int n_pages; | |
395 | ||
396 | if (npages) { | |
397 | n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000; | |
398 | kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages + | |
399 | n_pages); | |
400 | } else { | |
401 | unsigned int nr_mmu_pages; | |
402 | ||
403 | n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000; | |
404 | nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages; | |
405 | nr_mmu_pages = max(nr_mmu_pages, | |
406 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); | |
407 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
408 | } | |
409 | } | |
410 | ||
6aa8b732 | 411 | *memslot = new; |
6aa8b732 | 412 | |
90cb0529 AK |
413 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
414 | kvm_flush_remote_tlbs(kvm); | |
6aa8b732 | 415 | |
6aa8b732 AK |
416 | kvm_free_physmem_slot(&old, &new); |
417 | return 0; | |
418 | ||
f78e0e2e | 419 | out_free: |
6aa8b732 AK |
420 | kvm_free_physmem_slot(&new, &old); |
421 | out: | |
422 | return r; | |
210c7c4d IE |
423 | |
424 | } | |
f78e0e2e SY |
425 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
426 | ||
427 | int kvm_set_memory_region(struct kvm *kvm, | |
428 | struct kvm_userspace_memory_region *mem, | |
429 | int user_alloc) | |
430 | { | |
431 | int r; | |
432 | ||
433 | mutex_lock(&kvm->lock); | |
434 | r = __kvm_set_memory_region(kvm, mem, user_alloc); | |
435 | mutex_unlock(&kvm->lock); | |
436 | return r; | |
437 | } | |
210c7c4d IE |
438 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
439 | ||
1fe779f8 CO |
440 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
441 | struct | |
442 | kvm_userspace_memory_region *mem, | |
443 | int user_alloc) | |
210c7c4d | 444 | { |
e0d62c7f IE |
445 | if (mem->slot >= KVM_MEMORY_SLOTS) |
446 | return -EINVAL; | |
210c7c4d | 447 | return kvm_set_memory_region(kvm, mem, user_alloc); |
6aa8b732 AK |
448 | } |
449 | ||
450 | /* | |
451 | * Get (and clear) the dirty memory log for a memory slot. | |
452 | */ | |
2c6f5df9 AK |
453 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
454 | struct kvm_dirty_log *log) | |
6aa8b732 AK |
455 | { |
456 | struct kvm_memory_slot *memslot; | |
457 | int r, i; | |
458 | int n; | |
459 | unsigned long any = 0; | |
460 | ||
11ec2804 | 461 | mutex_lock(&kvm->lock); |
6aa8b732 | 462 | |
6aa8b732 AK |
463 | r = -EINVAL; |
464 | if (log->slot >= KVM_MEMORY_SLOTS) | |
465 | goto out; | |
466 | ||
467 | memslot = &kvm->memslots[log->slot]; | |
468 | r = -ENOENT; | |
469 | if (!memslot->dirty_bitmap) | |
470 | goto out; | |
471 | ||
cd1a4a98 | 472 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 473 | |
cd1a4a98 | 474 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
475 | any = memslot->dirty_bitmap[i]; |
476 | ||
477 | r = -EFAULT; | |
478 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
479 | goto out; | |
480 | ||
39214915 RR |
481 | /* If nothing is dirty, don't bother messing with page tables. */ |
482 | if (any) { | |
39214915 RR |
483 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
484 | kvm_flush_remote_tlbs(kvm); | |
485 | memset(memslot->dirty_bitmap, 0, n); | |
39214915 | 486 | } |
6aa8b732 AK |
487 | |
488 | r = 0; | |
489 | ||
490 | out: | |
11ec2804 | 491 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
492 | return r; |
493 | } | |
494 | ||
cea7bb21 IE |
495 | int is_error_page(struct page *page) |
496 | { | |
497 | return page == bad_page; | |
498 | } | |
499 | EXPORT_SYMBOL_GPL(is_error_page); | |
500 | ||
290fc38d | 501 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
e8207547 AK |
502 | { |
503 | int i; | |
504 | struct kvm_mem_alias *alias; | |
505 | ||
506 | for (i = 0; i < kvm->naliases; ++i) { | |
507 | alias = &kvm->aliases[i]; | |
508 | if (gfn >= alias->base_gfn | |
509 | && gfn < alias->base_gfn + alias->npages) | |
510 | return alias->target_gfn + gfn - alias->base_gfn; | |
511 | } | |
512 | return gfn; | |
513 | } | |
514 | ||
515 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
6aa8b732 AK |
516 | { |
517 | int i; | |
518 | ||
519 | for (i = 0; i < kvm->nmemslots; ++i) { | |
520 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
521 | ||
522 | if (gfn >= memslot->base_gfn | |
523 | && gfn < memslot->base_gfn + memslot->npages) | |
524 | return memslot; | |
525 | } | |
8b6d44c7 | 526 | return NULL; |
6aa8b732 | 527 | } |
e8207547 AK |
528 | |
529 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
530 | { | |
531 | gfn = unalias_gfn(kvm, gfn); | |
532 | return __gfn_to_memslot(kvm, gfn); | |
533 | } | |
6aa8b732 | 534 | |
e0d62c7f IE |
535 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
536 | { | |
537 | int i; | |
538 | ||
539 | gfn = unalias_gfn(kvm, gfn); | |
540 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
541 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
542 | ||
543 | if (gfn >= memslot->base_gfn | |
544 | && gfn < memslot->base_gfn + memslot->npages) | |
545 | return 1; | |
546 | } | |
547 | return 0; | |
548 | } | |
549 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
550 | ||
aab61cc0 AL |
551 | /* |
552 | * Requires current->mm->mmap_sem to be held | |
553 | */ | |
554 | static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
954bbbc2 AK |
555 | { |
556 | struct kvm_memory_slot *slot; | |
8d4e1288 AL |
557 | struct page *page[1]; |
558 | int npages; | |
954bbbc2 | 559 | |
60395224 AK |
560 | might_sleep(); |
561 | ||
e8207547 AK |
562 | gfn = unalias_gfn(kvm, gfn); |
563 | slot = __gfn_to_memslot(kvm, gfn); | |
8a7ae055 IE |
564 | if (!slot) { |
565 | get_page(bad_page); | |
cea7bb21 | 566 | return bad_page; |
8a7ae055 | 567 | } |
8d4e1288 | 568 | |
8d4e1288 AL |
569 | npages = get_user_pages(current, current->mm, |
570 | slot->userspace_addr | |
571 | + (gfn - slot->base_gfn) * PAGE_SIZE, 1, | |
572 | 1, 1, page, NULL); | |
8d4e1288 AL |
573 | if (npages != 1) { |
574 | get_page(bad_page); | |
575 | return bad_page; | |
8a7ae055 | 576 | } |
8d4e1288 AL |
577 | |
578 | return page[0]; | |
954bbbc2 | 579 | } |
aab61cc0 AL |
580 | |
581 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
582 | { | |
583 | struct page *page; | |
584 | ||
585 | down_read(¤t->mm->mmap_sem); | |
586 | page = __gfn_to_page(kvm, gfn); | |
587 | up_read(¤t->mm->mmap_sem); | |
588 | ||
589 | return page; | |
590 | } | |
591 | ||
954bbbc2 AK |
592 | EXPORT_SYMBOL_GPL(gfn_to_page); |
593 | ||
8a7ae055 IE |
594 | void kvm_release_page(struct page *page) |
595 | { | |
596 | if (!PageReserved(page)) | |
597 | SetPageDirty(page); | |
598 | put_page(page); | |
599 | } | |
600 | EXPORT_SYMBOL_GPL(kvm_release_page); | |
601 | ||
195aefde IE |
602 | static int next_segment(unsigned long len, int offset) |
603 | { | |
604 | if (len > PAGE_SIZE - offset) | |
605 | return PAGE_SIZE - offset; | |
606 | else | |
607 | return len; | |
608 | } | |
609 | ||
610 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
611 | int len) | |
612 | { | |
613 | void *page_virt; | |
614 | struct page *page; | |
615 | ||
616 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
617 | if (is_error_page(page)) { |
618 | kvm_release_page(page); | |
195aefde | 619 | return -EFAULT; |
8a7ae055 | 620 | } |
195aefde IE |
621 | page_virt = kmap_atomic(page, KM_USER0); |
622 | ||
623 | memcpy(data, page_virt + offset, len); | |
624 | ||
625 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 626 | kvm_release_page(page); |
195aefde IE |
627 | return 0; |
628 | } | |
629 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); | |
630 | ||
631 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) | |
632 | { | |
633 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
634 | int seg; | |
635 | int offset = offset_in_page(gpa); | |
636 | int ret; | |
637 | ||
638 | while ((seg = next_segment(len, offset)) != 0) { | |
639 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
640 | if (ret < 0) | |
641 | return ret; | |
642 | offset = 0; | |
643 | len -= seg; | |
644 | data += seg; | |
645 | ++gfn; | |
646 | } | |
647 | return 0; | |
648 | } | |
649 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
650 | ||
651 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, | |
652 | int offset, int len) | |
653 | { | |
654 | void *page_virt; | |
655 | struct page *page; | |
656 | ||
657 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
658 | if (is_error_page(page)) { |
659 | kvm_release_page(page); | |
195aefde | 660 | return -EFAULT; |
8a7ae055 | 661 | } |
195aefde IE |
662 | page_virt = kmap_atomic(page, KM_USER0); |
663 | ||
664 | memcpy(page_virt + offset, data, len); | |
665 | ||
666 | kunmap_atomic(page_virt, KM_USER0); | |
667 | mark_page_dirty(kvm, gfn); | |
8a7ae055 | 668 | kvm_release_page(page); |
195aefde IE |
669 | return 0; |
670 | } | |
671 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); | |
672 | ||
673 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
674 | unsigned long len) | |
675 | { | |
676 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
677 | int seg; | |
678 | int offset = offset_in_page(gpa); | |
679 | int ret; | |
680 | ||
681 | while ((seg = next_segment(len, offset)) != 0) { | |
682 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
683 | if (ret < 0) | |
684 | return ret; | |
685 | offset = 0; | |
686 | len -= seg; | |
687 | data += seg; | |
688 | ++gfn; | |
689 | } | |
690 | return 0; | |
691 | } | |
692 | ||
693 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) | |
694 | { | |
695 | void *page_virt; | |
696 | struct page *page; | |
697 | ||
698 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
699 | if (is_error_page(page)) { |
700 | kvm_release_page(page); | |
195aefde | 701 | return -EFAULT; |
8a7ae055 | 702 | } |
195aefde IE |
703 | page_virt = kmap_atomic(page, KM_USER0); |
704 | ||
705 | memset(page_virt + offset, 0, len); | |
706 | ||
707 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 708 | kvm_release_page(page); |
195aefde IE |
709 | return 0; |
710 | } | |
711 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
712 | ||
713 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
714 | { | |
715 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
716 | int seg; | |
717 | int offset = offset_in_page(gpa); | |
718 | int ret; | |
719 | ||
720 | while ((seg = next_segment(len, offset)) != 0) { | |
721 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); | |
722 | if (ret < 0) | |
723 | return ret; | |
724 | offset = 0; | |
725 | len -= seg; | |
726 | ++gfn; | |
727 | } | |
728 | return 0; | |
729 | } | |
730 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
731 | ||
6aa8b732 AK |
732 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
733 | { | |
31389947 | 734 | struct kvm_memory_slot *memslot; |
6aa8b732 | 735 | |
3b6fff19 | 736 | gfn = unalias_gfn(kvm, gfn); |
7e9d619d RR |
737 | memslot = __gfn_to_memslot(kvm, gfn); |
738 | if (memslot && memslot->dirty_bitmap) { | |
739 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 740 | |
7e9d619d RR |
741 | /* avoid RMW */ |
742 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
743 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
744 | } |
745 | } | |
746 | ||
b6958ce4 ED |
747 | /* |
748 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
749 | */ | |
8776e519 | 750 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 751 | { |
b6958ce4 ED |
752 | DECLARE_WAITQUEUE(wait, current); |
753 | ||
754 | add_wait_queue(&vcpu->wq, &wait); | |
755 | ||
756 | /* | |
757 | * We will block until either an interrupt or a signal wakes us up | |
758 | */ | |
c5ec1534 HQ |
759 | while (!kvm_cpu_has_interrupt(vcpu) |
760 | && !signal_pending(current) | |
761 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE | |
762 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { | |
b6958ce4 ED |
763 | set_current_state(TASK_INTERRUPTIBLE); |
764 | vcpu_put(vcpu); | |
765 | schedule(); | |
766 | vcpu_load(vcpu); | |
767 | } | |
d3bef15f | 768 | |
c5ec1534 | 769 | __set_current_state(TASK_RUNNING); |
b6958ce4 | 770 | remove_wait_queue(&vcpu->wq, &wait); |
b6958ce4 ED |
771 | } |
772 | ||
6aa8b732 AK |
773 | void kvm_resched(struct kvm_vcpu *vcpu) |
774 | { | |
3fca0365 YD |
775 | if (!need_resched()) |
776 | return; | |
6aa8b732 | 777 | cond_resched(); |
6aa8b732 AK |
778 | } |
779 | EXPORT_SYMBOL_GPL(kvm_resched); | |
780 | ||
04d2cc77 AK |
781 | /* |
782 | * Check if userspace requested an interrupt window, and that the | |
783 | * interrupt window is open. | |
784 | * | |
785 | * No need to exit to userspace if we already have an interrupt queued. | |
786 | */ | |
787 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
788 | struct kvm_run *kvm_run) | |
789 | { | |
790 | return (!vcpu->irq_summary && | |
791 | kvm_run->request_interrupt_window && | |
792 | vcpu->interrupt_window_open && | |
793 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); | |
794 | } | |
795 | ||
796 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
797 | struct kvm_run *kvm_run) | |
798 | { | |
799 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
800 | kvm_run->cr8 = get_cr8(vcpu); | |
801 | kvm_run->apic_base = kvm_get_apic_base(vcpu); | |
802 | if (irqchip_in_kernel(vcpu->kvm)) | |
803 | kvm_run->ready_for_interrupt_injection = 1; | |
804 | else | |
805 | kvm_run->ready_for_interrupt_injection = | |
806 | (vcpu->interrupt_window_open && | |
807 | vcpu->irq_summary == 0); | |
808 | } | |
809 | ||
810 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
811 | { | |
812 | int r; | |
813 | ||
814 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { | |
d77c26fc | 815 | pr_debug("vcpu %d received sipi with vector # %x\n", |
04d2cc77 AK |
816 | vcpu->vcpu_id, vcpu->sipi_vector); |
817 | kvm_lapic_reset(vcpu); | |
e00c8cf2 AK |
818 | r = kvm_x86_ops->vcpu_reset(vcpu); |
819 | if (r) | |
820 | return r; | |
04d2cc77 AK |
821 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
822 | } | |
823 | ||
824 | preempted: | |
825 | if (vcpu->guest_debug.enabled) | |
826 | kvm_x86_ops->guest_debug_pre(vcpu); | |
827 | ||
828 | again: | |
829 | r = kvm_mmu_reload(vcpu); | |
830 | if (unlikely(r)) | |
831 | goto out; | |
832 | ||
ab6ef34b AK |
833 | kvm_inject_pending_timer_irqs(vcpu); |
834 | ||
04d2cc77 AK |
835 | preempt_disable(); |
836 | ||
837 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
838 | kvm_load_guest_fpu(vcpu); | |
839 | ||
840 | local_irq_disable(); | |
841 | ||
842 | if (signal_pending(current)) { | |
843 | local_irq_enable(); | |
844 | preempt_enable(); | |
845 | r = -EINTR; | |
846 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
847 | ++vcpu->stat.signal_exits; | |
848 | goto out; | |
849 | } | |
850 | ||
851 | if (irqchip_in_kernel(vcpu->kvm)) | |
852 | kvm_x86_ops->inject_pending_irq(vcpu); | |
853 | else if (!vcpu->mmio_read_completed) | |
854 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); | |
855 | ||
856 | vcpu->guest_mode = 1; | |
d172fcd3 | 857 | kvm_guest_enter(); |
04d2cc77 AK |
858 | |
859 | if (vcpu->requests) | |
3176bc3e | 860 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
04d2cc77 AK |
861 | kvm_x86_ops->tlb_flush(vcpu); |
862 | ||
863 | kvm_x86_ops->run(vcpu, kvm_run); | |
864 | ||
865 | vcpu->guest_mode = 0; | |
866 | local_irq_enable(); | |
867 | ||
868 | ++vcpu->stat.exits; | |
869 | ||
0552f73b LV |
870 | /* |
871 | * We must have an instruction between local_irq_enable() and | |
872 | * kvm_guest_exit(), so the timer interrupt isn't delayed by | |
873 | * the interrupt shadow. The stat.exits increment will do nicely. | |
874 | * But we need to prevent reordering, hence this barrier(): | |
875 | */ | |
876 | barrier(); | |
877 | ||
878 | kvm_guest_exit(); | |
879 | ||
04d2cc77 AK |
880 | preempt_enable(); |
881 | ||
882 | /* | |
883 | * Profile KVM exit RIPs: | |
884 | */ | |
885 | if (unlikely(prof_on == KVM_PROFILING)) { | |
886 | kvm_x86_ops->cache_regs(vcpu); | |
887 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); | |
888 | } | |
889 | ||
890 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); | |
891 | ||
892 | if (r > 0) { | |
893 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
894 | r = -EINTR; | |
895 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
896 | ++vcpu->stat.request_irq_exits; | |
897 | goto out; | |
898 | } | |
899 | if (!need_resched()) { | |
900 | ++vcpu->stat.light_exits; | |
901 | goto again; | |
902 | } | |
903 | } | |
904 | ||
905 | out: | |
906 | if (r > 0) { | |
907 | kvm_resched(vcpu); | |
908 | goto preempted; | |
909 | } | |
910 | ||
911 | post_kvm_run_save(vcpu, kvm_run); | |
912 | ||
913 | return r; | |
914 | } | |
915 | ||
916 | ||
bccf2150 | 917 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
6aa8b732 | 918 | { |
6aa8b732 | 919 | int r; |
1961d276 | 920 | sigset_t sigsaved; |
6aa8b732 | 921 | |
bccf2150 | 922 | vcpu_load(vcpu); |
6aa8b732 | 923 | |
c5ec1534 HQ |
924 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { |
925 | kvm_vcpu_block(vcpu); | |
926 | vcpu_put(vcpu); | |
927 | return -EAGAIN; | |
928 | } | |
929 | ||
1961d276 AK |
930 | if (vcpu->sigset_active) |
931 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
932 | ||
54810342 | 933 | /* re-sync apic's tpr */ |
5cd4f6fd HQ |
934 | if (!irqchip_in_kernel(vcpu->kvm)) |
935 | set_cr8(vcpu, kvm_run->cr8); | |
54810342 | 936 | |
02c83209 AK |
937 | if (vcpu->pio.cur_count) { |
938 | r = complete_pio(vcpu); | |
939 | if (r) | |
940 | goto out; | |
941 | } | |
34c16eec | 942 | #if CONFIG_HAS_IOMEM |
02c83209 AK |
943 | if (vcpu->mmio_needed) { |
944 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
945 | vcpu->mmio_read_completed = 1; | |
946 | vcpu->mmio_needed = 0; | |
947 | r = emulate_instruction(vcpu, kvm_run, | |
3427318f | 948 | vcpu->mmio_fault_cr2, 0, 1); |
02c83209 AK |
949 | if (r == EMULATE_DO_MMIO) { |
950 | /* | |
951 | * Read-modify-write. Back to userspace. | |
952 | */ | |
02c83209 AK |
953 | r = 0; |
954 | goto out; | |
46fc1477 | 955 | } |
6aa8b732 | 956 | } |
34c16eec | 957 | #endif |
8eb7d334 | 958 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
cbdd1bea | 959 | kvm_x86_ops->cache_regs(vcpu); |
b4e63f56 | 960 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
cbdd1bea | 961 | kvm_x86_ops->decache_regs(vcpu); |
b4e63f56 AK |
962 | } |
963 | ||
04d2cc77 | 964 | r = __vcpu_run(vcpu, kvm_run); |
6aa8b732 | 965 | |
039576c0 | 966 | out: |
1961d276 AK |
967 | if (vcpu->sigset_active) |
968 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
969 | ||
6aa8b732 AK |
970 | vcpu_put(vcpu); |
971 | return r; | |
972 | } | |
973 | ||
bccf2150 AK |
974 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
975 | struct kvm_regs *regs) | |
6aa8b732 | 976 | { |
bccf2150 | 977 | vcpu_load(vcpu); |
6aa8b732 | 978 | |
cbdd1bea | 979 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 AK |
980 | |
981 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
982 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
983 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
984 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
985 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
986 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
987 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
988 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
05b3e0c2 | 989 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
990 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
991 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
992 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
993 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
994 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
995 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
996 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
997 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
998 | #endif | |
999 | ||
1000 | regs->rip = vcpu->rip; | |
cbdd1bea | 1001 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1002 | |
1003 | /* | |
1004 | * Don't leak debug flags in case they were set for guest debugging | |
1005 | */ | |
1006 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
1007 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
1008 | ||
1009 | vcpu_put(vcpu); | |
1010 | ||
1011 | return 0; | |
1012 | } | |
1013 | ||
bccf2150 AK |
1014 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
1015 | struct kvm_regs *regs) | |
6aa8b732 | 1016 | { |
bccf2150 | 1017 | vcpu_load(vcpu); |
6aa8b732 AK |
1018 | |
1019 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
1020 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
1021 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
1022 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
1023 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
1024 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
1025 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
1026 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
05b3e0c2 | 1027 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1028 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
1029 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
1030 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
1031 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
1032 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
1033 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
1034 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
1035 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
1036 | #endif | |
1037 | ||
1038 | vcpu->rip = regs->rip; | |
cbdd1bea | 1039 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
6aa8b732 | 1040 | |
cbdd1bea | 1041 | kvm_x86_ops->decache_regs(vcpu); |
6aa8b732 AK |
1042 | |
1043 | vcpu_put(vcpu); | |
1044 | ||
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | static void get_segment(struct kvm_vcpu *vcpu, | |
1049 | struct kvm_segment *var, int seg) | |
1050 | { | |
cbdd1bea | 1051 | return kvm_x86_ops->get_segment(vcpu, var, seg); |
6aa8b732 AK |
1052 | } |
1053 | ||
bccf2150 AK |
1054 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
1055 | struct kvm_sregs *sregs) | |
6aa8b732 | 1056 | { |
6aa8b732 | 1057 | struct descriptor_table dt; |
2a8067f1 | 1058 | int pending_vec; |
6aa8b732 | 1059 | |
bccf2150 | 1060 | vcpu_load(vcpu); |
6aa8b732 AK |
1061 | |
1062 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
1063 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
1064 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
1065 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
1066 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
1067 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
1068 | ||
1069 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
1070 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
1071 | ||
cbdd1bea | 1072 | kvm_x86_ops->get_idt(vcpu, &dt); |
6aa8b732 AK |
1073 | sregs->idt.limit = dt.limit; |
1074 | sregs->idt.base = dt.base; | |
cbdd1bea | 1075 | kvm_x86_ops->get_gdt(vcpu, &dt); |
6aa8b732 AK |
1076 | sregs->gdt.limit = dt.limit; |
1077 | sregs->gdt.base = dt.base; | |
1078 | ||
cbdd1bea | 1079 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
1080 | sregs->cr0 = vcpu->cr0; |
1081 | sregs->cr2 = vcpu->cr2; | |
1082 | sregs->cr3 = vcpu->cr3; | |
1083 | sregs->cr4 = vcpu->cr4; | |
7017fc3d | 1084 | sregs->cr8 = get_cr8(vcpu); |
6aa8b732 | 1085 | sregs->efer = vcpu->shadow_efer; |
7017fc3d | 1086 | sregs->apic_base = kvm_get_apic_base(vcpu); |
6aa8b732 | 1087 | |
2a8067f1 | 1088 | if (irqchip_in_kernel(vcpu->kvm)) { |
c52fb35a HQ |
1089 | memset(sregs->interrupt_bitmap, 0, |
1090 | sizeof sregs->interrupt_bitmap); | |
cbdd1bea | 1091 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
2a8067f1 | 1092 | if (pending_vec >= 0) |
d77c26fc MD |
1093 | set_bit(pending_vec, |
1094 | (unsigned long *)sregs->interrupt_bitmap); | |
2a8067f1 | 1095 | } else |
c52fb35a HQ |
1096 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
1097 | sizeof sregs->interrupt_bitmap); | |
6aa8b732 AK |
1098 | |
1099 | vcpu_put(vcpu); | |
1100 | ||
1101 | return 0; | |
1102 | } | |
1103 | ||
1104 | static void set_segment(struct kvm_vcpu *vcpu, | |
1105 | struct kvm_segment *var, int seg) | |
1106 | { | |
cbdd1bea | 1107 | return kvm_x86_ops->set_segment(vcpu, var, seg); |
6aa8b732 AK |
1108 | } |
1109 | ||
bccf2150 AK |
1110 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
1111 | struct kvm_sregs *sregs) | |
6aa8b732 | 1112 | { |
6aa8b732 | 1113 | int mmu_reset_needed = 0; |
2a8067f1 | 1114 | int i, pending_vec, max_bits; |
6aa8b732 AK |
1115 | struct descriptor_table dt; |
1116 | ||
bccf2150 | 1117 | vcpu_load(vcpu); |
6aa8b732 | 1118 | |
6aa8b732 AK |
1119 | dt.limit = sregs->idt.limit; |
1120 | dt.base = sregs->idt.base; | |
cbdd1bea | 1121 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
1122 | dt.limit = sregs->gdt.limit; |
1123 | dt.base = sregs->gdt.base; | |
cbdd1bea | 1124 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
1125 | |
1126 | vcpu->cr2 = sregs->cr2; | |
1127 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
1128 | vcpu->cr3 = sregs->cr3; | |
1129 | ||
7017fc3d | 1130 | set_cr8(vcpu, sregs->cr8); |
6aa8b732 AK |
1131 | |
1132 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
05b3e0c2 | 1133 | #ifdef CONFIG_X86_64 |
cbdd1bea | 1134 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
6aa8b732 | 1135 | #endif |
7017fc3d | 1136 | kvm_set_apic_base(vcpu, sregs->apic_base); |
6aa8b732 | 1137 | |
cbdd1bea | 1138 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
399badf3 | 1139 | |
6aa8b732 | 1140 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
81f50e3b | 1141 | vcpu->cr0 = sregs->cr0; |
cbdd1bea | 1142 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
6aa8b732 AK |
1143 | |
1144 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
cbdd1bea | 1145 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
1b0973bd AK |
1146 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
1147 | load_pdptrs(vcpu, vcpu->cr3); | |
6aa8b732 AK |
1148 | |
1149 | if (mmu_reset_needed) | |
1150 | kvm_mmu_reset_context(vcpu); | |
1151 | ||
c52fb35a HQ |
1152 | if (!irqchip_in_kernel(vcpu->kvm)) { |
1153 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
1154 | sizeof vcpu->irq_pending); | |
1155 | vcpu->irq_summary = 0; | |
1156 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) | |
1157 | if (vcpu->irq_pending[i]) | |
1158 | __set_bit(i, &vcpu->irq_summary); | |
2a8067f1 ED |
1159 | } else { |
1160 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; | |
1161 | pending_vec = find_first_bit( | |
1162 | (const unsigned long *)sregs->interrupt_bitmap, | |
1163 | max_bits); | |
1164 | /* Only pending external irq is handled here */ | |
1165 | if (pending_vec < max_bits) { | |
cbdd1bea | 1166 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
d77c26fc MD |
1167 | pr_debug("Set back pending irq %d\n", |
1168 | pending_vec); | |
2a8067f1 | 1169 | } |
c52fb35a | 1170 | } |
6aa8b732 | 1171 | |
024aa1c0 AK |
1172 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
1173 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
1174 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
1175 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
1176 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
1177 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
1178 | ||
1179 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
1180 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
1181 | ||
6aa8b732 AK |
1182 | vcpu_put(vcpu); |
1183 | ||
1184 | return 0; | |
1185 | } | |
1186 | ||
1747fb71 RR |
1187 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
1188 | { | |
1189 | struct kvm_segment cs; | |
1190 | ||
1191 | get_segment(vcpu, &cs, VCPU_SREG_CS); | |
1192 | *db = cs.db; | |
1193 | *l = cs.l; | |
1194 | } | |
1195 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
1196 | ||
6aa8b732 AK |
1197 | /* |
1198 | * Translate a guest virtual address to a guest physical address. | |
1199 | */ | |
bccf2150 AK |
1200 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
1201 | struct kvm_translation *tr) | |
6aa8b732 AK |
1202 | { |
1203 | unsigned long vaddr = tr->linear_address; | |
6aa8b732 AK |
1204 | gpa_t gpa; |
1205 | ||
bccf2150 | 1206 | vcpu_load(vcpu); |
11ec2804 | 1207 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 AK |
1208 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
1209 | tr->physical_address = gpa; | |
1210 | tr->valid = gpa != UNMAPPED_GVA; | |
1211 | tr->writeable = 1; | |
1212 | tr->usermode = 0; | |
11ec2804 | 1213 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
1214 | vcpu_put(vcpu); |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
bccf2150 AK |
1219 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
1220 | struct kvm_interrupt *irq) | |
6aa8b732 | 1221 | { |
6aa8b732 AK |
1222 | if (irq->irq < 0 || irq->irq >= 256) |
1223 | return -EINVAL; | |
97222cc8 ED |
1224 | if (irqchip_in_kernel(vcpu->kvm)) |
1225 | return -ENXIO; | |
bccf2150 | 1226 | vcpu_load(vcpu); |
6aa8b732 AK |
1227 | |
1228 | set_bit(irq->irq, vcpu->irq_pending); | |
1229 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
1230 | ||
1231 | vcpu_put(vcpu); | |
1232 | ||
1233 | return 0; | |
1234 | } | |
1235 | ||
bccf2150 AK |
1236 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
1237 | struct kvm_debug_guest *dbg) | |
6aa8b732 | 1238 | { |
6aa8b732 AK |
1239 | int r; |
1240 | ||
bccf2150 | 1241 | vcpu_load(vcpu); |
6aa8b732 | 1242 | |
cbdd1bea | 1243 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
6aa8b732 AK |
1244 | |
1245 | vcpu_put(vcpu); | |
1246 | ||
1247 | return r; | |
1248 | } | |
1249 | ||
9a2bb7f4 AK |
1250 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
1251 | unsigned long address, | |
1252 | int *type) | |
1253 | { | |
1254 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
1255 | unsigned long pgoff; | |
1256 | struct page *page; | |
1257 | ||
9a2bb7f4 | 1258 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
039576c0 AK |
1259 | if (pgoff == 0) |
1260 | page = virt_to_page(vcpu->run); | |
1261 | else if (pgoff == KVM_PIO_PAGE_OFFSET) | |
1262 | page = virt_to_page(vcpu->pio_data); | |
1263 | else | |
9a2bb7f4 | 1264 | return NOPAGE_SIGBUS; |
9a2bb7f4 | 1265 | get_page(page); |
cd0d9137 NAQ |
1266 | if (type != NULL) |
1267 | *type = VM_FAULT_MINOR; | |
1268 | ||
9a2bb7f4 AK |
1269 | return page; |
1270 | } | |
1271 | ||
1272 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
1273 | .nopage = kvm_vcpu_nopage, | |
1274 | }; | |
1275 | ||
1276 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
1277 | { | |
1278 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
1279 | return 0; | |
1280 | } | |
1281 | ||
bccf2150 AK |
1282 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
1283 | { | |
1284 | struct kvm_vcpu *vcpu = filp->private_data; | |
1285 | ||
1286 | fput(vcpu->kvm->filp); | |
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | static struct file_operations kvm_vcpu_fops = { | |
1291 | .release = kvm_vcpu_release, | |
1292 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
1293 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 1294 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
1295 | }; |
1296 | ||
1297 | /* | |
1298 | * Allocates an inode for the vcpu. | |
1299 | */ | |
1300 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
1301 | { | |
1302 | int fd, r; | |
1303 | struct inode *inode; | |
1304 | struct file *file; | |
1305 | ||
d6d28168 AK |
1306 | r = anon_inode_getfd(&fd, &inode, &file, |
1307 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); | |
1308 | if (r) | |
1309 | return r; | |
bccf2150 | 1310 | atomic_inc(&vcpu->kvm->filp->f_count); |
bccf2150 | 1311 | return fd; |
bccf2150 AK |
1312 | } |
1313 | ||
c5ea7660 AK |
1314 | /* |
1315 | * Creates some virtual cpus. Good luck creating more than one. | |
1316 | */ | |
1317 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
1318 | { | |
1319 | int r; | |
1320 | struct kvm_vcpu *vcpu; | |
1321 | ||
c5ea7660 | 1322 | if (!valid_vcpu(n)) |
fb3f0f51 | 1323 | return -EINVAL; |
c5ea7660 | 1324 | |
cbdd1bea | 1325 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
fb3f0f51 RR |
1326 | if (IS_ERR(vcpu)) |
1327 | return PTR_ERR(vcpu); | |
c5ea7660 | 1328 | |
15ad7146 AK |
1329 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
1330 | ||
b114b080 RR |
1331 | /* We do fxsave: this must be aligned. */ |
1332 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); | |
1333 | ||
fb3f0f51 | 1334 | vcpu_load(vcpu); |
e00c8cf2 AK |
1335 | r = kvm_x86_ops->vcpu_reset(vcpu); |
1336 | if (r == 0) | |
1337 | r = kvm_mmu_setup(vcpu); | |
c5ea7660 | 1338 | vcpu_put(vcpu); |
c5ea7660 | 1339 | if (r < 0) |
fb3f0f51 RR |
1340 | goto free_vcpu; |
1341 | ||
11ec2804 | 1342 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
1343 | if (kvm->vcpus[n]) { |
1344 | r = -EEXIST; | |
11ec2804 | 1345 | mutex_unlock(&kvm->lock); |
fb3f0f51 RR |
1346 | goto mmu_unload; |
1347 | } | |
1348 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 1349 | mutex_unlock(&kvm->lock); |
c5ea7660 | 1350 | |
fb3f0f51 | 1351 | /* Now it's all set up, let userspace reach it */ |
bccf2150 AK |
1352 | r = create_vcpu_fd(vcpu); |
1353 | if (r < 0) | |
fb3f0f51 RR |
1354 | goto unlink; |
1355 | return r; | |
39c3b86e | 1356 | |
fb3f0f51 | 1357 | unlink: |
11ec2804 | 1358 | mutex_lock(&kvm->lock); |
fb3f0f51 | 1359 | kvm->vcpus[n] = NULL; |
11ec2804 | 1360 | mutex_unlock(&kvm->lock); |
a2fa3e9f | 1361 | |
fb3f0f51 RR |
1362 | mmu_unload: |
1363 | vcpu_load(vcpu); | |
1364 | kvm_mmu_unload(vcpu); | |
1365 | vcpu_put(vcpu); | |
c5ea7660 | 1366 | |
fb3f0f51 | 1367 | free_vcpu: |
cbdd1bea | 1368 | kvm_x86_ops->vcpu_free(vcpu); |
c5ea7660 AK |
1369 | return r; |
1370 | } | |
1371 | ||
1961d276 AK |
1372 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
1373 | { | |
1374 | if (sigset) { | |
1375 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1376 | vcpu->sigset_active = 1; | |
1377 | vcpu->sigset = *sigset; | |
1378 | } else | |
1379 | vcpu->sigset_active = 0; | |
1380 | return 0; | |
1381 | } | |
1382 | ||
bccf2150 AK |
1383 | static long kvm_vcpu_ioctl(struct file *filp, |
1384 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 1385 | { |
bccf2150 | 1386 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 1387 | void __user *argp = (void __user *)arg; |
313a3dc7 | 1388 | int r; |
6aa8b732 AK |
1389 | |
1390 | switch (ioctl) { | |
9a2bb7f4 | 1391 | case KVM_RUN: |
f0fe5108 AK |
1392 | r = -EINVAL; |
1393 | if (arg) | |
1394 | goto out; | |
9a2bb7f4 | 1395 | r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 1396 | break; |
6aa8b732 AK |
1397 | case KVM_GET_REGS: { |
1398 | struct kvm_regs kvm_regs; | |
1399 | ||
bccf2150 AK |
1400 | memset(&kvm_regs, 0, sizeof kvm_regs); |
1401 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); | |
6aa8b732 AK |
1402 | if (r) |
1403 | goto out; | |
1404 | r = -EFAULT; | |
2f366987 | 1405 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
6aa8b732 AK |
1406 | goto out; |
1407 | r = 0; | |
1408 | break; | |
1409 | } | |
1410 | case KVM_SET_REGS: { | |
1411 | struct kvm_regs kvm_regs; | |
1412 | ||
1413 | r = -EFAULT; | |
2f366987 | 1414 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
6aa8b732 | 1415 | goto out; |
bccf2150 | 1416 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
1417 | if (r) |
1418 | goto out; | |
1419 | r = 0; | |
1420 | break; | |
1421 | } | |
1422 | case KVM_GET_SREGS: { | |
1423 | struct kvm_sregs kvm_sregs; | |
1424 | ||
bccf2150 AK |
1425 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
1426 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); | |
6aa8b732 AK |
1427 | if (r) |
1428 | goto out; | |
1429 | r = -EFAULT; | |
2f366987 | 1430 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
6aa8b732 AK |
1431 | goto out; |
1432 | r = 0; | |
1433 | break; | |
1434 | } | |
1435 | case KVM_SET_SREGS: { | |
1436 | struct kvm_sregs kvm_sregs; | |
1437 | ||
1438 | r = -EFAULT; | |
2f366987 | 1439 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
6aa8b732 | 1440 | goto out; |
bccf2150 | 1441 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
1442 | if (r) |
1443 | goto out; | |
1444 | r = 0; | |
1445 | break; | |
1446 | } | |
1447 | case KVM_TRANSLATE: { | |
1448 | struct kvm_translation tr; | |
1449 | ||
1450 | r = -EFAULT; | |
2f366987 | 1451 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 1452 | goto out; |
bccf2150 | 1453 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
1454 | if (r) |
1455 | goto out; | |
1456 | r = -EFAULT; | |
2f366987 | 1457 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
1458 | goto out; |
1459 | r = 0; | |
1460 | break; | |
1461 | } | |
1462 | case KVM_INTERRUPT: { | |
1463 | struct kvm_interrupt irq; | |
1464 | ||
1465 | r = -EFAULT; | |
2f366987 | 1466 | if (copy_from_user(&irq, argp, sizeof irq)) |
6aa8b732 | 1467 | goto out; |
bccf2150 | 1468 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
6aa8b732 AK |
1469 | if (r) |
1470 | goto out; | |
1471 | r = 0; | |
1472 | break; | |
1473 | } | |
1474 | case KVM_DEBUG_GUEST: { | |
1475 | struct kvm_debug_guest dbg; | |
1476 | ||
1477 | r = -EFAULT; | |
2f366987 | 1478 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 1479 | goto out; |
bccf2150 | 1480 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
1481 | if (r) |
1482 | goto out; | |
1483 | r = 0; | |
1484 | break; | |
1485 | } | |
1961d276 AK |
1486 | case KVM_SET_SIGNAL_MASK: { |
1487 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
1488 | struct kvm_signal_mask kvm_sigmask; | |
1489 | sigset_t sigset, *p; | |
1490 | ||
1491 | p = NULL; | |
1492 | if (argp) { | |
1493 | r = -EFAULT; | |
1494 | if (copy_from_user(&kvm_sigmask, argp, | |
1495 | sizeof kvm_sigmask)) | |
1496 | goto out; | |
1497 | r = -EINVAL; | |
1498 | if (kvm_sigmask.len != sizeof sigset) | |
1499 | goto out; | |
1500 | r = -EFAULT; | |
1501 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
1502 | sizeof sigset)) | |
1503 | goto out; | |
1504 | p = &sigset; | |
1505 | } | |
1506 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
1507 | break; | |
1508 | } | |
b8836737 AK |
1509 | case KVM_GET_FPU: { |
1510 | struct kvm_fpu fpu; | |
1511 | ||
1512 | memset(&fpu, 0, sizeof fpu); | |
d0752060 | 1513 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
b8836737 AK |
1514 | if (r) |
1515 | goto out; | |
1516 | r = -EFAULT; | |
1517 | if (copy_to_user(argp, &fpu, sizeof fpu)) | |
1518 | goto out; | |
1519 | r = 0; | |
1520 | break; | |
1521 | } | |
1522 | case KVM_SET_FPU: { | |
1523 | struct kvm_fpu fpu; | |
1524 | ||
1525 | r = -EFAULT; | |
1526 | if (copy_from_user(&fpu, argp, sizeof fpu)) | |
1527 | goto out; | |
d0752060 | 1528 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
b8836737 AK |
1529 | if (r) |
1530 | goto out; | |
1531 | r = 0; | |
1532 | break; | |
1533 | } | |
bccf2150 | 1534 | default: |
313a3dc7 | 1535 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
1536 | } |
1537 | out: | |
1538 | return r; | |
1539 | } | |
1540 | ||
1541 | static long kvm_vm_ioctl(struct file *filp, | |
1542 | unsigned int ioctl, unsigned long arg) | |
1543 | { | |
1544 | struct kvm *kvm = filp->private_data; | |
1545 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 1546 | int r; |
bccf2150 AK |
1547 | |
1548 | switch (ioctl) { | |
1549 | case KVM_CREATE_VCPU: | |
1550 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
1551 | if (r < 0) | |
1552 | goto out; | |
1553 | break; | |
6fc138d2 IE |
1554 | case KVM_SET_USER_MEMORY_REGION: { |
1555 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1556 | ||
1557 | r = -EFAULT; | |
1558 | if (copy_from_user(&kvm_userspace_mem, argp, | |
1559 | sizeof kvm_userspace_mem)) | |
1560 | goto out; | |
1561 | ||
1562 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); | |
6aa8b732 AK |
1563 | if (r) |
1564 | goto out; | |
1565 | break; | |
1566 | } | |
1567 | case KVM_GET_DIRTY_LOG: { | |
1568 | struct kvm_dirty_log log; | |
1569 | ||
1570 | r = -EFAULT; | |
2f366987 | 1571 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 1572 | goto out; |
2c6f5df9 | 1573 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
1574 | if (r) |
1575 | goto out; | |
1576 | break; | |
1577 | } | |
f17abe9a | 1578 | default: |
1fe779f8 | 1579 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
1580 | } |
1581 | out: | |
1582 | return r; | |
1583 | } | |
1584 | ||
1585 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | |
1586 | unsigned long address, | |
1587 | int *type) | |
1588 | { | |
1589 | struct kvm *kvm = vma->vm_file->private_data; | |
1590 | unsigned long pgoff; | |
f17abe9a AK |
1591 | struct page *page; |
1592 | ||
f17abe9a | 1593 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
e0d62c7f IE |
1594 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
1595 | return NOPAGE_SIGBUS; | |
aab61cc0 AL |
1596 | /* current->mm->mmap_sem is already held so call lockless version */ |
1597 | page = __gfn_to_page(kvm, pgoff); | |
8a7ae055 IE |
1598 | if (is_error_page(page)) { |
1599 | kvm_release_page(page); | |
f17abe9a | 1600 | return NOPAGE_SIGBUS; |
8a7ae055 | 1601 | } |
cd0d9137 NAQ |
1602 | if (type != NULL) |
1603 | *type = VM_FAULT_MINOR; | |
1604 | ||
f17abe9a AK |
1605 | return page; |
1606 | } | |
1607 | ||
1608 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
1609 | .nopage = kvm_vm_nopage, | |
1610 | }; | |
1611 | ||
1612 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
1613 | { | |
1614 | vma->vm_ops = &kvm_vm_vm_ops; | |
1615 | return 0; | |
1616 | } | |
1617 | ||
1618 | static struct file_operations kvm_vm_fops = { | |
1619 | .release = kvm_vm_release, | |
1620 | .unlocked_ioctl = kvm_vm_ioctl, | |
1621 | .compat_ioctl = kvm_vm_ioctl, | |
1622 | .mmap = kvm_vm_mmap, | |
1623 | }; | |
1624 | ||
1625 | static int kvm_dev_ioctl_create_vm(void) | |
1626 | { | |
1627 | int fd, r; | |
1628 | struct inode *inode; | |
1629 | struct file *file; | |
1630 | struct kvm *kvm; | |
1631 | ||
f17abe9a | 1632 | kvm = kvm_create_vm(); |
d6d28168 AK |
1633 | if (IS_ERR(kvm)) |
1634 | return PTR_ERR(kvm); | |
1635 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); | |
1636 | if (r) { | |
1637 | kvm_destroy_vm(kvm); | |
1638 | return r; | |
f17abe9a AK |
1639 | } |
1640 | ||
bccf2150 | 1641 | kvm->filp = file; |
f17abe9a | 1642 | |
f17abe9a | 1643 | return fd; |
f17abe9a AK |
1644 | } |
1645 | ||
1646 | static long kvm_dev_ioctl(struct file *filp, | |
1647 | unsigned int ioctl, unsigned long arg) | |
1648 | { | |
1649 | void __user *argp = (void __user *)arg; | |
07c45a36 | 1650 | long r = -EINVAL; |
f17abe9a AK |
1651 | |
1652 | switch (ioctl) { | |
1653 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
1654 | r = -EINVAL; |
1655 | if (arg) | |
1656 | goto out; | |
f17abe9a AK |
1657 | r = KVM_API_VERSION; |
1658 | break; | |
1659 | case KVM_CREATE_VM: | |
f0fe5108 AK |
1660 | r = -EINVAL; |
1661 | if (arg) | |
1662 | goto out; | |
f17abe9a AK |
1663 | r = kvm_dev_ioctl_create_vm(); |
1664 | break; | |
85f455f7 ED |
1665 | case KVM_CHECK_EXTENSION: { |
1666 | int ext = (long)argp; | |
1667 | ||
1668 | switch (ext) { | |
1669 | case KVM_CAP_IRQCHIP: | |
b6958ce4 | 1670 | case KVM_CAP_HLT: |
82ce2c96 | 1671 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
6fc138d2 | 1672 | case KVM_CAP_USER_MEMORY: |
cbc94022 | 1673 | case KVM_CAP_SET_TSS_ADDR: |
85f455f7 ED |
1674 | r = 1; |
1675 | break; | |
1676 | default: | |
1677 | r = 0; | |
1678 | break; | |
1679 | } | |
5d308f45 | 1680 | break; |
85f455f7 | 1681 | } |
07c45a36 AK |
1682 | case KVM_GET_VCPU_MMAP_SIZE: |
1683 | r = -EINVAL; | |
1684 | if (arg) | |
1685 | goto out; | |
039576c0 | 1686 | r = 2 * PAGE_SIZE; |
07c45a36 | 1687 | break; |
6aa8b732 | 1688 | default: |
043405e1 | 1689 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
1690 | } |
1691 | out: | |
1692 | return r; | |
1693 | } | |
1694 | ||
6aa8b732 | 1695 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
1696 | .unlocked_ioctl = kvm_dev_ioctl, |
1697 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
1698 | }; |
1699 | ||
1700 | static struct miscdevice kvm_dev = { | |
bbe4432e | 1701 | KVM_MINOR, |
6aa8b732 AK |
1702 | "kvm", |
1703 | &kvm_chardev_ops, | |
1704 | }; | |
1705 | ||
774c47f1 AK |
1706 | /* |
1707 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus | |
1708 | * cached on it. | |
1709 | */ | |
1710 | static void decache_vcpus_on_cpu(int cpu) | |
1711 | { | |
1712 | struct kvm *vm; | |
1713 | struct kvm_vcpu *vcpu; | |
1714 | int i; | |
1715 | ||
1716 | spin_lock(&kvm_lock); | |
11ec2804 | 1717 | list_for_each_entry(vm, &vm_list, vm_list) |
774c47f1 | 1718 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
fb3f0f51 RR |
1719 | vcpu = vm->vcpus[i]; |
1720 | if (!vcpu) | |
1721 | continue; | |
774c47f1 AK |
1722 | /* |
1723 | * If the vcpu is locked, then it is running on some | |
1724 | * other cpu and therefore it is not cached on the | |
1725 | * cpu in question. | |
1726 | * | |
1727 | * If it's not locked, check the last cpu it executed | |
1728 | * on. | |
1729 | */ | |
1730 | if (mutex_trylock(&vcpu->mutex)) { | |
1731 | if (vcpu->cpu == cpu) { | |
cbdd1bea | 1732 | kvm_x86_ops->vcpu_decache(vcpu); |
774c47f1 AK |
1733 | vcpu->cpu = -1; |
1734 | } | |
1735 | mutex_unlock(&vcpu->mutex); | |
1736 | } | |
1737 | } | |
1738 | spin_unlock(&kvm_lock); | |
1739 | } | |
1740 | ||
1b6c0168 AK |
1741 | static void hardware_enable(void *junk) |
1742 | { | |
1743 | int cpu = raw_smp_processor_id(); | |
1744 | ||
1745 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
1746 | return; | |
1747 | cpu_set(cpu, cpus_hardware_enabled); | |
cbdd1bea | 1748 | kvm_x86_ops->hardware_enable(NULL); |
1b6c0168 AK |
1749 | } |
1750 | ||
1751 | static void hardware_disable(void *junk) | |
1752 | { | |
1753 | int cpu = raw_smp_processor_id(); | |
1754 | ||
1755 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
1756 | return; | |
1757 | cpu_clear(cpu, cpus_hardware_enabled); | |
1758 | decache_vcpus_on_cpu(cpu); | |
cbdd1bea | 1759 | kvm_x86_ops->hardware_disable(NULL); |
1b6c0168 AK |
1760 | } |
1761 | ||
774c47f1 AK |
1762 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
1763 | void *v) | |
1764 | { | |
1765 | int cpu = (long)v; | |
1766 | ||
1767 | switch (val) { | |
cec9ad27 AK |
1768 | case CPU_DYING: |
1769 | case CPU_DYING_FROZEN: | |
6ec8a856 AK |
1770 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1771 | cpu); | |
1772 | hardware_disable(NULL); | |
1773 | break; | |
774c47f1 | 1774 | case CPU_UP_CANCELED: |
8bb78442 | 1775 | case CPU_UP_CANCELED_FROZEN: |
43934a38 JK |
1776 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1777 | cpu); | |
1b6c0168 | 1778 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
774c47f1 | 1779 | break; |
43934a38 | 1780 | case CPU_ONLINE: |
8bb78442 | 1781 | case CPU_ONLINE_FROZEN: |
43934a38 JK |
1782 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
1783 | cpu); | |
1b6c0168 | 1784 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
774c47f1 AK |
1785 | break; |
1786 | } | |
1787 | return NOTIFY_OK; | |
1788 | } | |
1789 | ||
9a2b85c6 | 1790 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 1791 | void *v) |
9a2b85c6 RR |
1792 | { |
1793 | if (val == SYS_RESTART) { | |
1794 | /* | |
1795 | * Some (well, at least mine) BIOSes hang on reboot if | |
1796 | * in vmx root mode. | |
1797 | */ | |
1798 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
1799 | on_each_cpu(hardware_disable, NULL, 0, 1); | |
1800 | } | |
1801 | return NOTIFY_OK; | |
1802 | } | |
1803 | ||
1804 | static struct notifier_block kvm_reboot_notifier = { | |
1805 | .notifier_call = kvm_reboot, | |
1806 | .priority = 0, | |
1807 | }; | |
1808 | ||
2eeb2e94 GH |
1809 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
1810 | { | |
1811 | memset(bus, 0, sizeof(*bus)); | |
1812 | } | |
1813 | ||
1814 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
1815 | { | |
1816 | int i; | |
1817 | ||
1818 | for (i = 0; i < bus->dev_count; i++) { | |
1819 | struct kvm_io_device *pos = bus->devs[i]; | |
1820 | ||
1821 | kvm_iodevice_destructor(pos); | |
1822 | } | |
1823 | } | |
1824 | ||
1825 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) | |
1826 | { | |
1827 | int i; | |
1828 | ||
1829 | for (i = 0; i < bus->dev_count; i++) { | |
1830 | struct kvm_io_device *pos = bus->devs[i]; | |
1831 | ||
1832 | if (pos->in_range(pos, addr)) | |
1833 | return pos; | |
1834 | } | |
1835 | ||
1836 | return NULL; | |
1837 | } | |
1838 | ||
1839 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
1840 | { | |
1841 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
1842 | ||
1843 | bus->devs[bus->dev_count++] = dev; | |
1844 | } | |
1845 | ||
774c47f1 AK |
1846 | static struct notifier_block kvm_cpu_notifier = { |
1847 | .notifier_call = kvm_cpu_hotplug, | |
1848 | .priority = 20, /* must be > scheduler priority */ | |
1849 | }; | |
1850 | ||
1165f5fe AK |
1851 | static u64 stat_get(void *_offset) |
1852 | { | |
1853 | unsigned offset = (long)_offset; | |
1854 | u64 total = 0; | |
1855 | struct kvm *kvm; | |
1856 | struct kvm_vcpu *vcpu; | |
1857 | int i; | |
1858 | ||
1859 | spin_lock(&kvm_lock); | |
1860 | list_for_each_entry(kvm, &vm_list, vm_list) | |
1861 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
1862 | vcpu = kvm->vcpus[i]; |
1863 | if (vcpu) | |
1864 | total += *(u32 *)((void *)vcpu + offset); | |
1165f5fe AK |
1865 | } |
1866 | spin_unlock(&kvm_lock); | |
1867 | return total; | |
1868 | } | |
1869 | ||
3dea7ca7 | 1870 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
1165f5fe | 1871 | |
6aa8b732 AK |
1872 | static __init void kvm_init_debug(void) |
1873 | { | |
1874 | struct kvm_stats_debugfs_item *p; | |
1875 | ||
8b6d44c7 | 1876 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 1877 | for (p = debugfs_entries; p->name; ++p) |
1165f5fe AK |
1878 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
1879 | (void *)(long)p->offset, | |
1880 | &stat_fops); | |
6aa8b732 AK |
1881 | } |
1882 | ||
1883 | static void kvm_exit_debug(void) | |
1884 | { | |
1885 | struct kvm_stats_debugfs_item *p; | |
1886 | ||
1887 | for (p = debugfs_entries; p->name; ++p) | |
1888 | debugfs_remove(p->dentry); | |
1889 | debugfs_remove(debugfs_dir); | |
1890 | } | |
1891 | ||
59ae6c6b AK |
1892 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
1893 | { | |
4267c41a | 1894 | hardware_disable(NULL); |
59ae6c6b AK |
1895 | return 0; |
1896 | } | |
1897 | ||
1898 | static int kvm_resume(struct sys_device *dev) | |
1899 | { | |
4267c41a | 1900 | hardware_enable(NULL); |
59ae6c6b AK |
1901 | return 0; |
1902 | } | |
1903 | ||
1904 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 1905 | .name = "kvm", |
59ae6c6b AK |
1906 | .suspend = kvm_suspend, |
1907 | .resume = kvm_resume, | |
1908 | }; | |
1909 | ||
1910 | static struct sys_device kvm_sysdev = { | |
1911 | .id = 0, | |
1912 | .cls = &kvm_sysdev_class, | |
1913 | }; | |
1914 | ||
cea7bb21 | 1915 | struct page *bad_page; |
6aa8b732 | 1916 | |
15ad7146 AK |
1917 | static inline |
1918 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
1919 | { | |
1920 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
1921 | } | |
1922 | ||
1923 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
1924 | { | |
1925 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1926 | ||
cbdd1bea | 1927 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 AK |
1928 | } |
1929 | ||
1930 | static void kvm_sched_out(struct preempt_notifier *pn, | |
1931 | struct task_struct *next) | |
1932 | { | |
1933 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1934 | ||
cbdd1bea | 1935 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
1936 | } |
1937 | ||
cbdd1bea | 1938 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
c16f862d | 1939 | struct module *module) |
6aa8b732 AK |
1940 | { |
1941 | int r; | |
002c7f7c | 1942 | int cpu; |
6aa8b732 | 1943 | |
cbdd1bea | 1944 | if (kvm_x86_ops) { |
09db28b8 YI |
1945 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
1946 | return -EEXIST; | |
1947 | } | |
1948 | ||
e097f35c | 1949 | if (!ops->cpu_has_kvm_support()) { |
6aa8b732 AK |
1950 | printk(KERN_ERR "kvm: no hardware support\n"); |
1951 | return -EOPNOTSUPP; | |
1952 | } | |
e097f35c | 1953 | if (ops->disabled_by_bios()) { |
6aa8b732 AK |
1954 | printk(KERN_ERR "kvm: disabled by bios\n"); |
1955 | return -EOPNOTSUPP; | |
1956 | } | |
1957 | ||
cbdd1bea | 1958 | kvm_x86_ops = ops; |
e097f35c | 1959 | |
cbdd1bea | 1960 | r = kvm_x86_ops->hardware_setup(); |
6aa8b732 | 1961 | if (r < 0) |
ca45aaae | 1962 | goto out; |
6aa8b732 | 1963 | |
002c7f7c YS |
1964 | for_each_online_cpu(cpu) { |
1965 | smp_call_function_single(cpu, | |
cbdd1bea | 1966 | kvm_x86_ops->check_processor_compatibility, |
002c7f7c YS |
1967 | &r, 0, 1); |
1968 | if (r < 0) | |
1969 | goto out_free_0; | |
1970 | } | |
1971 | ||
1b6c0168 | 1972 | on_each_cpu(hardware_enable, NULL, 0, 1); |
774c47f1 AK |
1973 | r = register_cpu_notifier(&kvm_cpu_notifier); |
1974 | if (r) | |
1975 | goto out_free_1; | |
6aa8b732 AK |
1976 | register_reboot_notifier(&kvm_reboot_notifier); |
1977 | ||
59ae6c6b AK |
1978 | r = sysdev_class_register(&kvm_sysdev_class); |
1979 | if (r) | |
1980 | goto out_free_2; | |
1981 | ||
1982 | r = sysdev_register(&kvm_sysdev); | |
1983 | if (r) | |
1984 | goto out_free_3; | |
1985 | ||
c16f862d RR |
1986 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
1987 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
1988 | __alignof__(struct kvm_vcpu), 0, 0); | |
1989 | if (!kvm_vcpu_cache) { | |
1990 | r = -ENOMEM; | |
1991 | goto out_free_4; | |
1992 | } | |
1993 | ||
6aa8b732 AK |
1994 | kvm_chardev_ops.owner = module; |
1995 | ||
1996 | r = misc_register(&kvm_dev); | |
1997 | if (r) { | |
d77c26fc | 1998 | printk(KERN_ERR "kvm: misc device register failed\n"); |
6aa8b732 AK |
1999 | goto out_free; |
2000 | } | |
2001 | ||
15ad7146 AK |
2002 | kvm_preempt_ops.sched_in = kvm_sched_in; |
2003 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
2004 | ||
c7addb90 AK |
2005 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
2006 | ||
2007 | return 0; | |
6aa8b732 AK |
2008 | |
2009 | out_free: | |
c16f862d RR |
2010 | kmem_cache_destroy(kvm_vcpu_cache); |
2011 | out_free_4: | |
59ae6c6b AK |
2012 | sysdev_unregister(&kvm_sysdev); |
2013 | out_free_3: | |
2014 | sysdev_class_unregister(&kvm_sysdev_class); | |
2015 | out_free_2: | |
6aa8b732 | 2016 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 AK |
2017 | unregister_cpu_notifier(&kvm_cpu_notifier); |
2018 | out_free_1: | |
1b6c0168 | 2019 | on_each_cpu(hardware_disable, NULL, 0, 1); |
002c7f7c | 2020 | out_free_0: |
cbdd1bea | 2021 | kvm_x86_ops->hardware_unsetup(); |
ca45aaae | 2022 | out: |
cbdd1bea | 2023 | kvm_x86_ops = NULL; |
6aa8b732 AK |
2024 | return r; |
2025 | } | |
d77c26fc | 2026 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
6aa8b732 | 2027 | |
cbdd1bea | 2028 | void kvm_exit_x86(void) |
6aa8b732 AK |
2029 | { |
2030 | misc_deregister(&kvm_dev); | |
c16f862d | 2031 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
2032 | sysdev_unregister(&kvm_sysdev); |
2033 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 2034 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 2035 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1b6c0168 | 2036 | on_each_cpu(hardware_disable, NULL, 0, 1); |
cbdd1bea CE |
2037 | kvm_x86_ops->hardware_unsetup(); |
2038 | kvm_x86_ops = NULL; | |
6aa8b732 | 2039 | } |
d77c26fc | 2040 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |
6aa8b732 AK |
2041 | |
2042 | static __init int kvm_init(void) | |
2043 | { | |
37e29d90 AK |
2044 | int r; |
2045 | ||
b5a33a75 AK |
2046 | r = kvm_mmu_module_init(); |
2047 | if (r) | |
2048 | goto out4; | |
2049 | ||
6aa8b732 AK |
2050 | kvm_init_debug(); |
2051 | ||
043405e1 | 2052 | kvm_arch_init(); |
bf591b24 | 2053 | |
cea7bb21 | 2054 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
d77c26fc MD |
2055 | |
2056 | if (bad_page == NULL) { | |
6aa8b732 AK |
2057 | r = -ENOMEM; |
2058 | goto out; | |
2059 | } | |
2060 | ||
58e690e6 | 2061 | return 0; |
6aa8b732 AK |
2062 | |
2063 | out: | |
2064 | kvm_exit_debug(); | |
b5a33a75 AK |
2065 | kvm_mmu_module_exit(); |
2066 | out4: | |
6aa8b732 AK |
2067 | return r; |
2068 | } | |
2069 | ||
2070 | static __exit void kvm_exit(void) | |
2071 | { | |
2072 | kvm_exit_debug(); | |
cea7bb21 | 2073 | __free_page(bad_page); |
b5a33a75 | 2074 | kvm_mmu_module_exit(); |
6aa8b732 AK |
2075 | } |
2076 | ||
2077 | module_init(kvm_init) | |
2078 | module_exit(kvm_exit) |