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