]>
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 | ||
6aa8b732 AK |
93 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
94 | ||
bccf2150 AK |
95 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
96 | unsigned long arg); | |
97 | ||
5aacf0ca JM |
98 | static inline int valid_vcpu(int n) |
99 | { | |
100 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
101 | } | |
102 | ||
7702fd1f AK |
103 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
104 | { | |
105 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) | |
106 | return; | |
107 | ||
108 | vcpu->guest_fpu_loaded = 1; | |
b114b080 RR |
109 | fx_save(&vcpu->host_fx_image); |
110 | fx_restore(&vcpu->guest_fx_image); | |
7702fd1f AK |
111 | } |
112 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); | |
113 | ||
114 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
115 | { | |
116 | if (!vcpu->guest_fpu_loaded) | |
117 | return; | |
118 | ||
119 | vcpu->guest_fpu_loaded = 0; | |
b114b080 RR |
120 | fx_save(&vcpu->guest_fx_image); |
121 | fx_restore(&vcpu->host_fx_image); | |
7702fd1f AK |
122 | } |
123 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); | |
124 | ||
bccf2150 AK |
125 | /* |
126 | * Switches to specified vcpu, until a matching vcpu_put() | |
127 | */ | |
313a3dc7 | 128 | void vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 129 | { |
15ad7146 AK |
130 | int cpu; |
131 | ||
bccf2150 | 132 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
133 | cpu = get_cpu(); |
134 | preempt_notifier_register(&vcpu->preempt_notifier); | |
313a3dc7 | 135 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 136 | put_cpu(); |
6aa8b732 AK |
137 | } |
138 | ||
313a3dc7 | 139 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 140 | { |
15ad7146 | 141 | preempt_disable(); |
313a3dc7 | 142 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
143 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
144 | preempt_enable(); | |
6aa8b732 AK |
145 | mutex_unlock(&vcpu->mutex); |
146 | } | |
147 | ||
d9e368d6 AK |
148 | static void ack_flush(void *_completed) |
149 | { | |
d9e368d6 AK |
150 | } |
151 | ||
152 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
153 | { | |
49d3bd7e | 154 | int i, cpu; |
d9e368d6 AK |
155 | cpumask_t cpus; |
156 | struct kvm_vcpu *vcpu; | |
d9e368d6 | 157 | |
d9e368d6 | 158 | cpus_clear(cpus); |
fb3f0f51 RR |
159 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
160 | vcpu = kvm->vcpus[i]; | |
161 | if (!vcpu) | |
162 | continue; | |
3176bc3e | 163 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
d9e368d6 AK |
164 | continue; |
165 | cpu = vcpu->cpu; | |
166 | if (cpu != -1 && cpu != raw_smp_processor_id()) | |
49d3bd7e | 167 | cpu_set(cpu, cpus); |
d9e368d6 | 168 | } |
49d3bd7e | 169 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
d9e368d6 AK |
170 | } |
171 | ||
fb3f0f51 RR |
172 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
173 | { | |
174 | struct page *page; | |
175 | int r; | |
176 | ||
177 | mutex_init(&vcpu->mutex); | |
178 | vcpu->cpu = -1; | |
179 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
180 | vcpu->kvm = kvm; | |
181 | vcpu->vcpu_id = id; | |
c5ec1534 HQ |
182 | if (!irqchip_in_kernel(kvm) || id == 0) |
183 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
184 | else | |
185 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; | |
b6958ce4 | 186 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
187 | |
188 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
189 | if (!page) { | |
190 | r = -ENOMEM; | |
191 | goto fail; | |
192 | } | |
193 | vcpu->run = page_address(page); | |
194 | ||
195 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
196 | if (!page) { | |
197 | r = -ENOMEM; | |
198 | goto fail_free_run; | |
199 | } | |
200 | vcpu->pio_data = page_address(page); | |
201 | ||
fb3f0f51 RR |
202 | r = kvm_mmu_create(vcpu); |
203 | if (r < 0) | |
204 | goto fail_free_pio_data; | |
205 | ||
76fafa5e RR |
206 | if (irqchip_in_kernel(kvm)) { |
207 | r = kvm_create_lapic(vcpu); | |
208 | if (r < 0) | |
209 | goto fail_mmu_destroy; | |
210 | } | |
211 | ||
fb3f0f51 RR |
212 | return 0; |
213 | ||
76fafa5e RR |
214 | fail_mmu_destroy: |
215 | kvm_mmu_destroy(vcpu); | |
fb3f0f51 RR |
216 | fail_free_pio_data: |
217 | free_page((unsigned long)vcpu->pio_data); | |
218 | fail_free_run: | |
219 | free_page((unsigned long)vcpu->run); | |
220 | fail: | |
76fafa5e | 221 | return r; |
fb3f0f51 RR |
222 | } |
223 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
224 | ||
225 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
226 | { | |
d589444e | 227 | kvm_free_lapic(vcpu); |
fb3f0f51 RR |
228 | kvm_mmu_destroy(vcpu); |
229 | free_page((unsigned long)vcpu->pio_data); | |
230 | free_page((unsigned long)vcpu->run); | |
231 | } | |
232 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
233 | ||
f17abe9a | 234 | static struct kvm *kvm_create_vm(void) |
6aa8b732 AK |
235 | { |
236 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
6aa8b732 AK |
237 | |
238 | if (!kvm) | |
f17abe9a | 239 | return ERR_PTR(-ENOMEM); |
6aa8b732 | 240 | |
74906345 | 241 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 242 | mutex_init(&kvm->lock); |
6aa8b732 | 243 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
2eeb2e94 | 244 | kvm_io_bus_init(&kvm->mmio_bus); |
5e58cfe4 RR |
245 | spin_lock(&kvm_lock); |
246 | list_add(&kvm->vm_list, &vm_list); | |
247 | spin_unlock(&kvm_lock); | |
f17abe9a AK |
248 | return kvm; |
249 | } | |
250 | ||
6aa8b732 AK |
251 | /* |
252 | * Free any memory in @free but not in @dont. | |
253 | */ | |
254 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
255 | struct kvm_memory_slot *dont) | |
256 | { | |
290fc38d IE |
257 | if (!dont || free->rmap != dont->rmap) |
258 | vfree(free->rmap); | |
6aa8b732 AK |
259 | |
260 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
261 | vfree(free->dirty_bitmap); | |
262 | ||
6aa8b732 | 263 | free->npages = 0; |
8b6d44c7 | 264 | free->dirty_bitmap = NULL; |
8d4e1288 | 265 | free->rmap = NULL; |
6aa8b732 AK |
266 | } |
267 | ||
268 | static void kvm_free_physmem(struct kvm *kvm) | |
269 | { | |
270 | int i; | |
271 | ||
272 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 273 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
274 | } |
275 | ||
039576c0 AK |
276 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
277 | { | |
278 | int i; | |
279 | ||
3077c451 | 280 | for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i) |
039576c0 | 281 | if (vcpu->pio.guest_pages[i]) { |
8a7ae055 | 282 | kvm_release_page(vcpu->pio.guest_pages[i]); |
039576c0 AK |
283 | vcpu->pio.guest_pages[i] = NULL; |
284 | } | |
285 | } | |
286 | ||
7b53aa56 AK |
287 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
288 | { | |
7b53aa56 AK |
289 | vcpu_load(vcpu); |
290 | kvm_mmu_unload(vcpu); | |
291 | vcpu_put(vcpu); | |
292 | } | |
293 | ||
6aa8b732 AK |
294 | static void kvm_free_vcpus(struct kvm *kvm) |
295 | { | |
296 | unsigned int i; | |
297 | ||
7b53aa56 AK |
298 | /* |
299 | * Unpin any mmu pages first. | |
300 | */ | |
301 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
fb3f0f51 RR |
302 | if (kvm->vcpus[i]) |
303 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); | |
304 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
305 | if (kvm->vcpus[i]) { | |
cbdd1bea | 306 | kvm_x86_ops->vcpu_free(kvm->vcpus[i]); |
fb3f0f51 RR |
307 | kvm->vcpus[i] = NULL; |
308 | } | |
309 | } | |
310 | ||
6aa8b732 AK |
311 | } |
312 | ||
f17abe9a AK |
313 | static void kvm_destroy_vm(struct kvm *kvm) |
314 | { | |
133de902 AK |
315 | spin_lock(&kvm_lock); |
316 | list_del(&kvm->vm_list); | |
317 | spin_unlock(&kvm_lock); | |
74906345 | 318 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 319 | kvm_io_bus_destroy(&kvm->mmio_bus); |
85f455f7 | 320 | kfree(kvm->vpic); |
1fd4f2a5 | 321 | kfree(kvm->vioapic); |
6aa8b732 AK |
322 | kvm_free_vcpus(kvm); |
323 | kvm_free_physmem(kvm); | |
324 | kfree(kvm); | |
f17abe9a AK |
325 | } |
326 | ||
327 | static int kvm_vm_release(struct inode *inode, struct file *filp) | |
328 | { | |
329 | struct kvm *kvm = filp->private_data; | |
330 | ||
331 | kvm_destroy_vm(kvm); | |
6aa8b732 AK |
332 | return 0; |
333 | } | |
334 | ||
335 | static void inject_gp(struct kvm_vcpu *vcpu) | |
336 | { | |
cbdd1bea | 337 | kvm_x86_ops->inject_gp(vcpu, 0); |
6aa8b732 AK |
338 | } |
339 | ||
6aa8b732 AK |
340 | void fx_init(struct kvm_vcpu *vcpu) |
341 | { | |
b114b080 | 342 | unsigned after_mxcsr_mask; |
6aa8b732 | 343 | |
9bd01506 RR |
344 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
345 | preempt_disable(); | |
b114b080 | 346 | fx_save(&vcpu->host_fx_image); |
6aa8b732 | 347 | fpu_init(); |
b114b080 RR |
348 | fx_save(&vcpu->guest_fx_image); |
349 | fx_restore(&vcpu->host_fx_image); | |
9bd01506 | 350 | preempt_enable(); |
6aa8b732 | 351 | |
380102c8 | 352 | vcpu->cr0 |= X86_CR0_ET; |
b114b080 RR |
353 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
354 | vcpu->guest_fx_image.mxcsr = 0x1f80; | |
355 | memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask, | |
356 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); | |
6aa8b732 AK |
357 | } |
358 | EXPORT_SYMBOL_GPL(fx_init); | |
359 | ||
6aa8b732 AK |
360 | /* |
361 | * Allocate some memory and give it an address in the guest physical address | |
362 | * space. | |
363 | * | |
364 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e SY |
365 | * |
366 | * Must be called holding kvm->lock. | |
6aa8b732 | 367 | */ |
f78e0e2e SY |
368 | int __kvm_set_memory_region(struct kvm *kvm, |
369 | struct kvm_userspace_memory_region *mem, | |
370 | int user_alloc) | |
6aa8b732 AK |
371 | { |
372 | int r; | |
373 | gfn_t base_gfn; | |
374 | unsigned long npages; | |
375 | unsigned long i; | |
376 | struct kvm_memory_slot *memslot; | |
377 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
378 | |
379 | r = -EINVAL; | |
380 | /* General sanity checks */ | |
381 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
382 | goto out; | |
383 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
384 | goto out; | |
e0d62c7f | 385 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
6aa8b732 AK |
386 | goto out; |
387 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
388 | goto out; | |
389 | ||
390 | memslot = &kvm->memslots[mem->slot]; | |
391 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
392 | npages = mem->memory_size >> PAGE_SHIFT; | |
393 | ||
394 | if (!npages) | |
395 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
396 | ||
6aa8b732 AK |
397 | new = old = *memslot; |
398 | ||
399 | new.base_gfn = base_gfn; | |
400 | new.npages = npages; | |
401 | new.flags = mem->flags; | |
402 | ||
403 | /* Disallow changing a memory slot's size. */ | |
404 | r = -EINVAL; | |
405 | if (npages && old.npages && npages != old.npages) | |
f78e0e2e | 406 | goto out_free; |
6aa8b732 AK |
407 | |
408 | /* Check for overlaps */ | |
409 | r = -EEXIST; | |
410 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
411 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
412 | ||
413 | if (s == memslot) | |
414 | continue; | |
415 | if (!((base_gfn + npages <= s->base_gfn) || | |
416 | (base_gfn >= s->base_gfn + s->npages))) | |
f78e0e2e | 417 | goto out_free; |
6aa8b732 | 418 | } |
6aa8b732 | 419 | |
6aa8b732 AK |
420 | /* Free page dirty bitmap if unneeded */ |
421 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 422 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
423 | |
424 | r = -ENOMEM; | |
425 | ||
426 | /* Allocate if a slot is being created */ | |
8d4e1288 | 427 | if (npages && !new.rmap) { |
d77c26fc | 428 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
290fc38d IE |
429 | |
430 | if (!new.rmap) | |
f78e0e2e | 431 | goto out_free; |
290fc38d | 432 | |
290fc38d | 433 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
8d4e1288 | 434 | |
80b14b5b | 435 | new.user_alloc = user_alloc; |
8d4e1288 | 436 | if (user_alloc) |
8a7ae055 | 437 | new.userspace_addr = mem->userspace_addr; |
8d4e1288 AL |
438 | else { |
439 | down_write(¤t->mm->mmap_sem); | |
440 | new.userspace_addr = do_mmap(NULL, 0, | |
441 | npages * PAGE_SIZE, | |
442 | PROT_READ | PROT_WRITE, | |
443 | MAP_SHARED | MAP_ANONYMOUS, | |
444 | 0); | |
445 | up_write(¤t->mm->mmap_sem); | |
446 | ||
447 | if (IS_ERR((void *)new.userspace_addr)) | |
f78e0e2e | 448 | goto out_free; |
6aa8b732 | 449 | } |
80b14b5b IE |
450 | } else { |
451 | if (!old.user_alloc && old.rmap) { | |
452 | int ret; | |
453 | ||
454 | down_write(¤t->mm->mmap_sem); | |
455 | ret = do_munmap(current->mm, old.userspace_addr, | |
456 | old.npages * PAGE_SIZE); | |
457 | up_write(¤t->mm->mmap_sem); | |
458 | if (ret < 0) | |
459 | printk(KERN_WARNING | |
460 | "kvm_vm_ioctl_set_memory_region: " | |
461 | "failed to munmap memory\n"); | |
462 | } | |
6aa8b732 AK |
463 | } |
464 | ||
465 | /* Allocate page dirty bitmap if needed */ | |
466 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
467 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
468 | ||
469 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
470 | if (!new.dirty_bitmap) | |
f78e0e2e | 471 | goto out_free; |
6aa8b732 AK |
472 | memset(new.dirty_bitmap, 0, dirty_bytes); |
473 | } | |
474 | ||
6aa8b732 AK |
475 | if (mem->slot >= kvm->nmemslots) |
476 | kvm->nmemslots = mem->slot + 1; | |
477 | ||
82ce2c96 IE |
478 | if (!kvm->n_requested_mmu_pages) { |
479 | unsigned int n_pages; | |
480 | ||
481 | if (npages) { | |
482 | n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000; | |
483 | kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages + | |
484 | n_pages); | |
485 | } else { | |
486 | unsigned int nr_mmu_pages; | |
487 | ||
488 | n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000; | |
489 | nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages; | |
490 | nr_mmu_pages = max(nr_mmu_pages, | |
491 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); | |
492 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
493 | } | |
494 | } | |
495 | ||
6aa8b732 | 496 | *memslot = new; |
6aa8b732 | 497 | |
90cb0529 AK |
498 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
499 | kvm_flush_remote_tlbs(kvm); | |
6aa8b732 | 500 | |
6aa8b732 AK |
501 | kvm_free_physmem_slot(&old, &new); |
502 | return 0; | |
503 | ||
f78e0e2e | 504 | out_free: |
6aa8b732 AK |
505 | kvm_free_physmem_slot(&new, &old); |
506 | out: | |
507 | return r; | |
210c7c4d IE |
508 | |
509 | } | |
f78e0e2e SY |
510 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
511 | ||
512 | int kvm_set_memory_region(struct kvm *kvm, | |
513 | struct kvm_userspace_memory_region *mem, | |
514 | int user_alloc) | |
515 | { | |
516 | int r; | |
517 | ||
518 | mutex_lock(&kvm->lock); | |
519 | r = __kvm_set_memory_region(kvm, mem, user_alloc); | |
520 | mutex_unlock(&kvm->lock); | |
521 | return r; | |
522 | } | |
210c7c4d IE |
523 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
524 | ||
1fe779f8 CO |
525 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
526 | struct | |
527 | kvm_userspace_memory_region *mem, | |
528 | int user_alloc) | |
210c7c4d | 529 | { |
e0d62c7f IE |
530 | if (mem->slot >= KVM_MEMORY_SLOTS) |
531 | return -EINVAL; | |
210c7c4d | 532 | return kvm_set_memory_region(kvm, mem, user_alloc); |
6aa8b732 AK |
533 | } |
534 | ||
535 | /* | |
536 | * Get (and clear) the dirty memory log for a memory slot. | |
537 | */ | |
2c6f5df9 AK |
538 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
539 | struct kvm_dirty_log *log) | |
6aa8b732 AK |
540 | { |
541 | struct kvm_memory_slot *memslot; | |
542 | int r, i; | |
543 | int n; | |
544 | unsigned long any = 0; | |
545 | ||
11ec2804 | 546 | mutex_lock(&kvm->lock); |
6aa8b732 | 547 | |
6aa8b732 AK |
548 | r = -EINVAL; |
549 | if (log->slot >= KVM_MEMORY_SLOTS) | |
550 | goto out; | |
551 | ||
552 | memslot = &kvm->memslots[log->slot]; | |
553 | r = -ENOENT; | |
554 | if (!memslot->dirty_bitmap) | |
555 | goto out; | |
556 | ||
cd1a4a98 | 557 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 558 | |
cd1a4a98 | 559 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
560 | any = memslot->dirty_bitmap[i]; |
561 | ||
562 | r = -EFAULT; | |
563 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
564 | goto out; | |
565 | ||
39214915 RR |
566 | /* If nothing is dirty, don't bother messing with page tables. */ |
567 | if (any) { | |
39214915 RR |
568 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
569 | kvm_flush_remote_tlbs(kvm); | |
570 | memset(memslot->dirty_bitmap, 0, n); | |
39214915 | 571 | } |
6aa8b732 AK |
572 | |
573 | r = 0; | |
574 | ||
575 | out: | |
11ec2804 | 576 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
577 | return r; |
578 | } | |
579 | ||
cea7bb21 IE |
580 | int is_error_page(struct page *page) |
581 | { | |
582 | return page == bad_page; | |
583 | } | |
584 | EXPORT_SYMBOL_GPL(is_error_page); | |
585 | ||
290fc38d | 586 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
e8207547 AK |
587 | { |
588 | int i; | |
589 | struct kvm_mem_alias *alias; | |
590 | ||
591 | for (i = 0; i < kvm->naliases; ++i) { | |
592 | alias = &kvm->aliases[i]; | |
593 | if (gfn >= alias->base_gfn | |
594 | && gfn < alias->base_gfn + alias->npages) | |
595 | return alias->target_gfn + gfn - alias->base_gfn; | |
596 | } | |
597 | return gfn; | |
598 | } | |
599 | ||
600 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
6aa8b732 AK |
601 | { |
602 | int i; | |
603 | ||
604 | for (i = 0; i < kvm->nmemslots; ++i) { | |
605 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
606 | ||
607 | if (gfn >= memslot->base_gfn | |
608 | && gfn < memslot->base_gfn + memslot->npages) | |
609 | return memslot; | |
610 | } | |
8b6d44c7 | 611 | return NULL; |
6aa8b732 | 612 | } |
e8207547 AK |
613 | |
614 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
615 | { | |
616 | gfn = unalias_gfn(kvm, gfn); | |
617 | return __gfn_to_memslot(kvm, gfn); | |
618 | } | |
6aa8b732 | 619 | |
e0d62c7f IE |
620 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
621 | { | |
622 | int i; | |
623 | ||
624 | gfn = unalias_gfn(kvm, gfn); | |
625 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
626 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
627 | ||
628 | if (gfn >= memslot->base_gfn | |
629 | && gfn < memslot->base_gfn + memslot->npages) | |
630 | return 1; | |
631 | } | |
632 | return 0; | |
633 | } | |
634 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
635 | ||
954bbbc2 AK |
636 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
637 | { | |
638 | struct kvm_memory_slot *slot; | |
8d4e1288 AL |
639 | struct page *page[1]; |
640 | int npages; | |
954bbbc2 | 641 | |
60395224 AK |
642 | might_sleep(); |
643 | ||
e8207547 AK |
644 | gfn = unalias_gfn(kvm, gfn); |
645 | slot = __gfn_to_memslot(kvm, gfn); | |
8a7ae055 IE |
646 | if (!slot) { |
647 | get_page(bad_page); | |
cea7bb21 | 648 | return bad_page; |
8a7ae055 | 649 | } |
8d4e1288 AL |
650 | |
651 | down_read(¤t->mm->mmap_sem); | |
652 | npages = get_user_pages(current, current->mm, | |
653 | slot->userspace_addr | |
654 | + (gfn - slot->base_gfn) * PAGE_SIZE, 1, | |
655 | 1, 1, page, NULL); | |
656 | up_read(¤t->mm->mmap_sem); | |
657 | if (npages != 1) { | |
658 | get_page(bad_page); | |
659 | return bad_page; | |
8a7ae055 | 660 | } |
8d4e1288 AL |
661 | |
662 | return page[0]; | |
954bbbc2 AK |
663 | } |
664 | EXPORT_SYMBOL_GPL(gfn_to_page); | |
665 | ||
8a7ae055 IE |
666 | void kvm_release_page(struct page *page) |
667 | { | |
668 | if (!PageReserved(page)) | |
669 | SetPageDirty(page); | |
670 | put_page(page); | |
671 | } | |
672 | EXPORT_SYMBOL_GPL(kvm_release_page); | |
673 | ||
195aefde IE |
674 | static int next_segment(unsigned long len, int offset) |
675 | { | |
676 | if (len > PAGE_SIZE - offset) | |
677 | return PAGE_SIZE - offset; | |
678 | else | |
679 | return len; | |
680 | } | |
681 | ||
682 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
683 | int len) | |
684 | { | |
685 | void *page_virt; | |
686 | struct page *page; | |
687 | ||
688 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
689 | if (is_error_page(page)) { |
690 | kvm_release_page(page); | |
195aefde | 691 | return -EFAULT; |
8a7ae055 | 692 | } |
195aefde IE |
693 | page_virt = kmap_atomic(page, KM_USER0); |
694 | ||
695 | memcpy(data, page_virt + offset, len); | |
696 | ||
697 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 698 | kvm_release_page(page); |
195aefde IE |
699 | return 0; |
700 | } | |
701 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); | |
702 | ||
703 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) | |
704 | { | |
705 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
706 | int seg; | |
707 | int offset = offset_in_page(gpa); | |
708 | int ret; | |
709 | ||
710 | while ((seg = next_segment(len, offset)) != 0) { | |
711 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
712 | if (ret < 0) | |
713 | return ret; | |
714 | offset = 0; | |
715 | len -= seg; | |
716 | data += seg; | |
717 | ++gfn; | |
718 | } | |
719 | return 0; | |
720 | } | |
721 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
722 | ||
723 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, | |
724 | int offset, int len) | |
725 | { | |
726 | void *page_virt; | |
727 | struct page *page; | |
728 | ||
729 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
730 | if (is_error_page(page)) { |
731 | kvm_release_page(page); | |
195aefde | 732 | return -EFAULT; |
8a7ae055 | 733 | } |
195aefde IE |
734 | page_virt = kmap_atomic(page, KM_USER0); |
735 | ||
736 | memcpy(page_virt + offset, data, len); | |
737 | ||
738 | kunmap_atomic(page_virt, KM_USER0); | |
739 | mark_page_dirty(kvm, gfn); | |
8a7ae055 | 740 | kvm_release_page(page); |
195aefde IE |
741 | return 0; |
742 | } | |
743 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); | |
744 | ||
745 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
746 | unsigned long len) | |
747 | { | |
748 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
749 | int seg; | |
750 | int offset = offset_in_page(gpa); | |
751 | int ret; | |
752 | ||
753 | while ((seg = next_segment(len, offset)) != 0) { | |
754 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
755 | if (ret < 0) | |
756 | return ret; | |
757 | offset = 0; | |
758 | len -= seg; | |
759 | data += seg; | |
760 | ++gfn; | |
761 | } | |
762 | return 0; | |
763 | } | |
764 | ||
765 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) | |
766 | { | |
767 | void *page_virt; | |
768 | struct page *page; | |
769 | ||
770 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
771 | if (is_error_page(page)) { |
772 | kvm_release_page(page); | |
195aefde | 773 | return -EFAULT; |
8a7ae055 | 774 | } |
195aefde IE |
775 | page_virt = kmap_atomic(page, KM_USER0); |
776 | ||
777 | memset(page_virt + offset, 0, len); | |
778 | ||
779 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 780 | kvm_release_page(page); |
195aefde IE |
781 | return 0; |
782 | } | |
783 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
784 | ||
785 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
786 | { | |
787 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
788 | int seg; | |
789 | int offset = offset_in_page(gpa); | |
790 | int ret; | |
791 | ||
792 | while ((seg = next_segment(len, offset)) != 0) { | |
793 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); | |
794 | if (ret < 0) | |
795 | return ret; | |
796 | offset = 0; | |
797 | len -= seg; | |
798 | ++gfn; | |
799 | } | |
800 | return 0; | |
801 | } | |
802 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
803 | ||
7e9d619d | 804 | /* WARNING: Does not work on aliased pages. */ |
6aa8b732 AK |
805 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
806 | { | |
31389947 | 807 | struct kvm_memory_slot *memslot; |
6aa8b732 | 808 | |
7e9d619d RR |
809 | memslot = __gfn_to_memslot(kvm, gfn); |
810 | if (memslot && memslot->dirty_bitmap) { | |
811 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 812 | |
7e9d619d RR |
813 | /* avoid RMW */ |
814 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
815 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
816 | } |
817 | } | |
818 | ||
e7d5d76c | 819 | int emulator_read_std(unsigned long addr, |
4c690a1e | 820 | void *val, |
6aa8b732 | 821 | unsigned int bytes, |
cebff02b | 822 | struct kvm_vcpu *vcpu) |
6aa8b732 | 823 | { |
6aa8b732 AK |
824 | void *data = val; |
825 | ||
826 | while (bytes) { | |
827 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
828 | unsigned offset = addr & (PAGE_SIZE-1); | |
829 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
195aefde | 830 | int ret; |
6aa8b732 AK |
831 | |
832 | if (gpa == UNMAPPED_GVA) | |
833 | return X86EMUL_PROPAGATE_FAULT; | |
195aefde IE |
834 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); |
835 | if (ret < 0) | |
6aa8b732 | 836 | return X86EMUL_UNHANDLEABLE; |
6aa8b732 AK |
837 | |
838 | bytes -= tocopy; | |
839 | data += tocopy; | |
840 | addr += tocopy; | |
841 | } | |
842 | ||
843 | return X86EMUL_CONTINUE; | |
844 | } | |
e7d5d76c | 845 | EXPORT_SYMBOL_GPL(emulator_read_std); |
6aa8b732 AK |
846 | |
847 | static int emulator_write_std(unsigned long addr, | |
4c690a1e | 848 | const void *val, |
6aa8b732 | 849 | unsigned int bytes, |
cebff02b | 850 | struct kvm_vcpu *vcpu) |
6aa8b732 | 851 | { |
f0242478 | 852 | pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes); |
6aa8b732 AK |
853 | return X86EMUL_UNHANDLEABLE; |
854 | } | |
855 | ||
97222cc8 ED |
856 | /* |
857 | * Only apic need an MMIO device hook, so shortcut now.. | |
858 | */ | |
859 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, | |
860 | gpa_t addr) | |
861 | { | |
862 | struct kvm_io_device *dev; | |
863 | ||
864 | if (vcpu->apic) { | |
865 | dev = &vcpu->apic->dev; | |
866 | if (dev->in_range(dev, addr)) | |
867 | return dev; | |
868 | } | |
869 | return NULL; | |
870 | } | |
871 | ||
2eeb2e94 GH |
872 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
873 | gpa_t addr) | |
874 | { | |
97222cc8 ED |
875 | struct kvm_io_device *dev; |
876 | ||
877 | dev = vcpu_find_pervcpu_dev(vcpu, addr); | |
878 | if (dev == NULL) | |
879 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); | |
880 | return dev; | |
2eeb2e94 GH |
881 | } |
882 | ||
74906345 ED |
883 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
884 | gpa_t addr) | |
885 | { | |
886 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); | |
887 | } | |
888 | ||
6aa8b732 | 889 | static int emulator_read_emulated(unsigned long addr, |
4c690a1e | 890 | void *val, |
6aa8b732 | 891 | unsigned int bytes, |
cebff02b | 892 | struct kvm_vcpu *vcpu) |
6aa8b732 | 893 | { |
2eeb2e94 GH |
894 | struct kvm_io_device *mmio_dev; |
895 | gpa_t gpa; | |
6aa8b732 AK |
896 | |
897 | if (vcpu->mmio_read_completed) { | |
898 | memcpy(val, vcpu->mmio_data, bytes); | |
899 | vcpu->mmio_read_completed = 0; | |
900 | return X86EMUL_CONTINUE; | |
f78e0e2e | 901 | } |
d27d4aca | 902 | |
2eeb2e94 | 903 | gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
f78e0e2e SY |
904 | |
905 | /* For APIC access vmexit */ | |
906 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
907 | goto mmio; | |
908 | ||
909 | if (emulator_read_std(addr, val, bytes, vcpu) | |
910 | == X86EMUL_CONTINUE) | |
911 | return X86EMUL_CONTINUE; | |
2eeb2e94 GH |
912 | if (gpa == UNMAPPED_GVA) |
913 | return X86EMUL_PROPAGATE_FAULT; | |
6aa8b732 | 914 | |
f78e0e2e | 915 | mmio: |
2eeb2e94 GH |
916 | /* |
917 | * Is this MMIO handled locally? | |
918 | */ | |
919 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
920 | if (mmio_dev) { | |
921 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); | |
922 | return X86EMUL_CONTINUE; | |
6aa8b732 | 923 | } |
2eeb2e94 GH |
924 | |
925 | vcpu->mmio_needed = 1; | |
926 | vcpu->mmio_phys_addr = gpa; | |
927 | vcpu->mmio_size = bytes; | |
928 | vcpu->mmio_is_write = 0; | |
929 | ||
930 | return X86EMUL_UNHANDLEABLE; | |
6aa8b732 AK |
931 | } |
932 | ||
da4a00f0 | 933 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
4c690a1e | 934 | const void *val, int bytes) |
da4a00f0 | 935 | { |
195aefde | 936 | int ret; |
da4a00f0 | 937 | |
195aefde IE |
938 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
939 | if (ret < 0) | |
da4a00f0 | 940 | return 0; |
fe551881 | 941 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); |
da4a00f0 AK |
942 | return 1; |
943 | } | |
944 | ||
b0fcd903 AK |
945 | static int emulator_write_emulated_onepage(unsigned long addr, |
946 | const void *val, | |
947 | unsigned int bytes, | |
cebff02b | 948 | struct kvm_vcpu *vcpu) |
6aa8b732 | 949 | { |
2eeb2e94 GH |
950 | struct kvm_io_device *mmio_dev; |
951 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
6aa8b732 | 952 | |
c9047f53 | 953 | if (gpa == UNMAPPED_GVA) { |
cbdd1bea | 954 | kvm_x86_ops->inject_page_fault(vcpu, addr, 2); |
6aa8b732 | 955 | return X86EMUL_PROPAGATE_FAULT; |
c9047f53 | 956 | } |
6aa8b732 | 957 | |
f78e0e2e SY |
958 | /* For APIC access vmexit */ |
959 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
960 | goto mmio; | |
961 | ||
da4a00f0 AK |
962 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
963 | return X86EMUL_CONTINUE; | |
964 | ||
f78e0e2e | 965 | mmio: |
2eeb2e94 GH |
966 | /* |
967 | * Is this MMIO handled locally? | |
968 | */ | |
969 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
970 | if (mmio_dev) { | |
971 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); | |
972 | return X86EMUL_CONTINUE; | |
973 | } | |
974 | ||
6aa8b732 AK |
975 | vcpu->mmio_needed = 1; |
976 | vcpu->mmio_phys_addr = gpa; | |
977 | vcpu->mmio_size = bytes; | |
978 | vcpu->mmio_is_write = 1; | |
4c690a1e | 979 | memcpy(vcpu->mmio_data, val, bytes); |
6aa8b732 AK |
980 | |
981 | return X86EMUL_CONTINUE; | |
982 | } | |
983 | ||
e7d5d76c | 984 | int emulator_write_emulated(unsigned long addr, |
b0fcd903 AK |
985 | const void *val, |
986 | unsigned int bytes, | |
cebff02b | 987 | struct kvm_vcpu *vcpu) |
b0fcd903 AK |
988 | { |
989 | /* Crossing a page boundary? */ | |
990 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
991 | int rc, now; | |
992 | ||
993 | now = -addr & ~PAGE_MASK; | |
cebff02b | 994 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
b0fcd903 AK |
995 | if (rc != X86EMUL_CONTINUE) |
996 | return rc; | |
997 | addr += now; | |
998 | val += now; | |
999 | bytes -= now; | |
1000 | } | |
cebff02b | 1001 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
b0fcd903 | 1002 | } |
e7d5d76c | 1003 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
b0fcd903 | 1004 | |
6aa8b732 | 1005 | static int emulator_cmpxchg_emulated(unsigned long addr, |
4c690a1e AK |
1006 | const void *old, |
1007 | const void *new, | |
6aa8b732 | 1008 | unsigned int bytes, |
cebff02b | 1009 | struct kvm_vcpu *vcpu) |
6aa8b732 AK |
1010 | { |
1011 | static int reported; | |
1012 | ||
1013 | if (!reported) { | |
1014 | reported = 1; | |
1015 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
1016 | } | |
cebff02b | 1017 | return emulator_write_emulated(addr, new, bytes, vcpu); |
6aa8b732 AK |
1018 | } |
1019 | ||
1020 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
1021 | { | |
cbdd1bea | 1022 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
6aa8b732 AK |
1023 | } |
1024 | ||
1025 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
1026 | { | |
6aa8b732 AK |
1027 | return X86EMUL_CONTINUE; |
1028 | } | |
1029 | ||
1030 | int emulate_clts(struct kvm_vcpu *vcpu) | |
1031 | { | |
404fb881 | 1032 | kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS); |
6aa8b732 AK |
1033 | return X86EMUL_CONTINUE; |
1034 | } | |
1035 | ||
d77c26fc | 1036 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
6aa8b732 AK |
1037 | { |
1038 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
1039 | ||
1040 | switch (dr) { | |
1041 | case 0 ... 3: | |
cbdd1bea | 1042 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
6aa8b732 AK |
1043 | return X86EMUL_CONTINUE; |
1044 | default: | |
f0242478 | 1045 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr); |
6aa8b732 AK |
1046 | return X86EMUL_UNHANDLEABLE; |
1047 | } | |
1048 | } | |
1049 | ||
1050 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
1051 | { | |
1052 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
1053 | int exception; | |
1054 | ||
cbdd1bea | 1055 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
6aa8b732 AK |
1056 | if (exception) { |
1057 | /* FIXME: better handling */ | |
1058 | return X86EMUL_UNHANDLEABLE; | |
1059 | } | |
1060 | return X86EMUL_CONTINUE; | |
1061 | } | |
1062 | ||
054b1369 | 1063 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
6aa8b732 AK |
1064 | { |
1065 | static int reported; | |
1066 | u8 opcodes[4]; | |
054b1369 | 1067 | unsigned long rip = vcpu->rip; |
6aa8b732 AK |
1068 | unsigned long rip_linear; |
1069 | ||
054b1369 | 1070 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
6aa8b732 AK |
1071 | |
1072 | if (reported) | |
1073 | return; | |
1074 | ||
054b1369 | 1075 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
6aa8b732 | 1076 | |
054b1369 AK |
1077 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
1078 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
6aa8b732 AK |
1079 | reported = 1; |
1080 | } | |
054b1369 | 1081 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
6aa8b732 AK |
1082 | |
1083 | struct x86_emulate_ops emulate_ops = { | |
1084 | .read_std = emulator_read_std, | |
1085 | .write_std = emulator_write_std, | |
1086 | .read_emulated = emulator_read_emulated, | |
1087 | .write_emulated = emulator_write_emulated, | |
1088 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
1089 | }; | |
1090 | ||
1091 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
1092 | struct kvm_run *run, | |
1093 | unsigned long cr2, | |
3427318f LV |
1094 | u16 error_code, |
1095 | int no_decode) | |
6aa8b732 | 1096 | { |
a22436b7 | 1097 | int r; |
6aa8b732 | 1098 | |
e7df56e4 | 1099 | vcpu->mmio_fault_cr2 = cr2; |
cbdd1bea | 1100 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 | 1101 | |
6aa8b732 | 1102 | vcpu->mmio_is_write = 0; |
e70669ab | 1103 | vcpu->pio.string = 0; |
3427318f LV |
1104 | |
1105 | if (!no_decode) { | |
1106 | int cs_db, cs_l; | |
1107 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
1108 | ||
1109 | vcpu->emulate_ctxt.vcpu = vcpu; | |
1110 | vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); | |
1111 | vcpu->emulate_ctxt.cr2 = cr2; | |
1112 | vcpu->emulate_ctxt.mode = | |
1113 | (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM) | |
1114 | ? X86EMUL_MODE_REAL : cs_l | |
1115 | ? X86EMUL_MODE_PROT64 : cs_db | |
1116 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
1117 | ||
1118 | if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) { | |
1119 | vcpu->emulate_ctxt.cs_base = 0; | |
1120 | vcpu->emulate_ctxt.ds_base = 0; | |
1121 | vcpu->emulate_ctxt.es_base = 0; | |
1122 | vcpu->emulate_ctxt.ss_base = 0; | |
1123 | } else { | |
1124 | vcpu->emulate_ctxt.cs_base = | |
1125 | get_segment_base(vcpu, VCPU_SREG_CS); | |
1126 | vcpu->emulate_ctxt.ds_base = | |
1127 | get_segment_base(vcpu, VCPU_SREG_DS); | |
1128 | vcpu->emulate_ctxt.es_base = | |
1129 | get_segment_base(vcpu, VCPU_SREG_ES); | |
1130 | vcpu->emulate_ctxt.ss_base = | |
1131 | get_segment_base(vcpu, VCPU_SREG_SS); | |
1132 | } | |
1133 | ||
1134 | vcpu->emulate_ctxt.gs_base = | |
1135 | get_segment_base(vcpu, VCPU_SREG_GS); | |
1136 | vcpu->emulate_ctxt.fs_base = | |
1137 | get_segment_base(vcpu, VCPU_SREG_FS); | |
1138 | ||
1139 | r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops); | |
a22436b7 LV |
1140 | if (r) { |
1141 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) | |
1142 | return EMULATE_DONE; | |
1143 | return EMULATE_FAIL; | |
1144 | } | |
3427318f LV |
1145 | } |
1146 | ||
a22436b7 | 1147 | r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops); |
1be3aa47 | 1148 | |
e70669ab LV |
1149 | if (vcpu->pio.string) |
1150 | return EMULATE_DO_MMIO; | |
6aa8b732 AK |
1151 | |
1152 | if ((r || vcpu->mmio_is_write) && run) { | |
8fc0d085 | 1153 | run->exit_reason = KVM_EXIT_MMIO; |
6aa8b732 AK |
1154 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
1155 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
1156 | run->mmio.len = vcpu->mmio_size; | |
1157 | run->mmio.is_write = vcpu->mmio_is_write; | |
1158 | } | |
1159 | ||
1160 | if (r) { | |
a436036b AK |
1161 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
1162 | return EMULATE_DONE; | |
6aa8b732 | 1163 | if (!vcpu->mmio_needed) { |
054b1369 | 1164 | kvm_report_emulation_failure(vcpu, "mmio"); |
6aa8b732 AK |
1165 | return EMULATE_FAIL; |
1166 | } | |
1167 | return EMULATE_DO_MMIO; | |
1168 | } | |
1169 | ||
cbdd1bea | 1170 | kvm_x86_ops->decache_regs(vcpu); |
3427318f | 1171 | kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags); |
6aa8b732 | 1172 | |
02c83209 AK |
1173 | if (vcpu->mmio_is_write) { |
1174 | vcpu->mmio_needed = 0; | |
6aa8b732 | 1175 | return EMULATE_DO_MMIO; |
02c83209 | 1176 | } |
6aa8b732 AK |
1177 | |
1178 | return EMULATE_DONE; | |
1179 | } | |
1180 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
1181 | ||
b6958ce4 ED |
1182 | /* |
1183 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
1184 | */ | |
c5ec1534 | 1185 | static void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 1186 | { |
b6958ce4 ED |
1187 | DECLARE_WAITQUEUE(wait, current); |
1188 | ||
1189 | add_wait_queue(&vcpu->wq, &wait); | |
1190 | ||
1191 | /* | |
1192 | * We will block until either an interrupt or a signal wakes us up | |
1193 | */ | |
c5ec1534 HQ |
1194 | while (!kvm_cpu_has_interrupt(vcpu) |
1195 | && !signal_pending(current) | |
1196 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE | |
1197 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { | |
b6958ce4 ED |
1198 | set_current_state(TASK_INTERRUPTIBLE); |
1199 | vcpu_put(vcpu); | |
1200 | schedule(); | |
1201 | vcpu_load(vcpu); | |
1202 | } | |
d3bef15f | 1203 | |
c5ec1534 | 1204 | __set_current_state(TASK_RUNNING); |
b6958ce4 | 1205 | remove_wait_queue(&vcpu->wq, &wait); |
b6958ce4 ED |
1206 | } |
1207 | ||
1208 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
1209 | { | |
d3bef15f | 1210 | ++vcpu->stat.halt_exits; |
b6958ce4 | 1211 | if (irqchip_in_kernel(vcpu->kvm)) { |
c5ec1534 HQ |
1212 | vcpu->mp_state = VCPU_MP_STATE_HALTED; |
1213 | kvm_vcpu_block(vcpu); | |
1214 | if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE) | |
1215 | return -EINTR; | |
b6958ce4 ED |
1216 | return 1; |
1217 | } else { | |
1218 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
1219 | return 0; | |
1220 | } | |
d3bef15f AK |
1221 | } |
1222 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); | |
1223 | ||
7aa81cc0 | 1224 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
270fd9b9 | 1225 | { |
7aa81cc0 | 1226 | unsigned long nr, a0, a1, a2, a3, ret; |
270fd9b9 | 1227 | |
cbdd1bea | 1228 | kvm_x86_ops->cache_regs(vcpu); |
7aa81cc0 AL |
1229 | |
1230 | nr = vcpu->regs[VCPU_REGS_RAX]; | |
1231 | a0 = vcpu->regs[VCPU_REGS_RBX]; | |
1232 | a1 = vcpu->regs[VCPU_REGS_RCX]; | |
1233 | a2 = vcpu->regs[VCPU_REGS_RDX]; | |
1234 | a3 = vcpu->regs[VCPU_REGS_RSI]; | |
1235 | ||
1236 | if (!is_long_mode(vcpu)) { | |
1237 | nr &= 0xFFFFFFFF; | |
1238 | a0 &= 0xFFFFFFFF; | |
1239 | a1 &= 0xFFFFFFFF; | |
1240 | a2 &= 0xFFFFFFFF; | |
1241 | a3 &= 0xFFFFFFFF; | |
270fd9b9 | 1242 | } |
7aa81cc0 | 1243 | |
270fd9b9 AK |
1244 | switch (nr) { |
1245 | default: | |
7aa81cc0 AL |
1246 | ret = -KVM_ENOSYS; |
1247 | break; | |
270fd9b9 AK |
1248 | } |
1249 | vcpu->regs[VCPU_REGS_RAX] = ret; | |
cbdd1bea | 1250 | kvm_x86_ops->decache_regs(vcpu); |
7aa81cc0 AL |
1251 | return 0; |
1252 | } | |
1253 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
1254 | ||
1255 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) | |
1256 | { | |
1257 | char instruction[3]; | |
1258 | int ret = 0; | |
1259 | ||
1260 | mutex_lock(&vcpu->kvm->lock); | |
1261 | ||
1262 | /* | |
1263 | * Blow out the MMU to ensure that no other VCPU has an active mapping | |
1264 | * to ensure that the updated hypercall appears atomically across all | |
1265 | * VCPUs. | |
1266 | */ | |
1267 | kvm_mmu_zap_all(vcpu->kvm); | |
1268 | ||
1269 | kvm_x86_ops->cache_regs(vcpu); | |
1270 | kvm_x86_ops->patch_hypercall(vcpu, instruction); | |
1271 | if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu) | |
1272 | != X86EMUL_CONTINUE) | |
1273 | ret = -EFAULT; | |
1274 | ||
1275 | mutex_unlock(&vcpu->kvm->lock); | |
1276 | ||
1277 | return ret; | |
270fd9b9 | 1278 | } |
270fd9b9 | 1279 | |
6aa8b732 AK |
1280 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
1281 | { | |
1282 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
1283 | } | |
1284 | ||
1285 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1286 | { | |
1287 | struct descriptor_table dt = { limit, base }; | |
1288 | ||
cbdd1bea | 1289 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
1290 | } |
1291 | ||
1292 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1293 | { | |
1294 | struct descriptor_table dt = { limit, base }; | |
1295 | ||
cbdd1bea | 1296 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
1297 | } |
1298 | ||
1299 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
1300 | unsigned long *rflags) | |
1301 | { | |
1302 | lmsw(vcpu, msw); | |
cbdd1bea | 1303 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1304 | } |
1305 | ||
1306 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
1307 | { | |
cbdd1bea | 1308 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
1309 | switch (cr) { |
1310 | case 0: | |
1311 | return vcpu->cr0; | |
1312 | case 2: | |
1313 | return vcpu->cr2; | |
1314 | case 3: | |
1315 | return vcpu->cr3; | |
1316 | case 4: | |
1317 | return vcpu->cr4; | |
1318 | default: | |
1319 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1320 | return 0; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
1325 | unsigned long *rflags) | |
1326 | { | |
1327 | switch (cr) { | |
1328 | case 0: | |
1329 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); | |
cbdd1bea | 1330 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1331 | break; |
1332 | case 2: | |
1333 | vcpu->cr2 = val; | |
1334 | break; | |
1335 | case 3: | |
1336 | set_cr3(vcpu, val); | |
1337 | break; | |
1338 | case 4: | |
1339 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); | |
1340 | break; | |
1341 | default: | |
1342 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1343 | } | |
1344 | } | |
1345 | ||
3bab1f5d AK |
1346 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
1347 | { | |
1348 | u64 data; | |
1349 | ||
1350 | switch (msr) { | |
1351 | case 0xc0010010: /* SYSCFG */ | |
1352 | case 0xc0010015: /* HWCR */ | |
1353 | case MSR_IA32_PLATFORM_ID: | |
1354 | case MSR_IA32_P5_MC_ADDR: | |
1355 | case MSR_IA32_P5_MC_TYPE: | |
1356 | case MSR_IA32_MC0_CTL: | |
1357 | case MSR_IA32_MCG_STATUS: | |
1358 | case MSR_IA32_MCG_CAP: | |
1359 | case MSR_IA32_MC0_MISC: | |
1360 | case MSR_IA32_MC0_MISC+4: | |
1361 | case MSR_IA32_MC0_MISC+8: | |
1362 | case MSR_IA32_MC0_MISC+12: | |
1363 | case MSR_IA32_MC0_MISC+16: | |
1364 | case MSR_IA32_UCODE_REV: | |
a8d13ea2 | 1365 | case MSR_IA32_PERF_STATUS: |
2dc7094b | 1366 | case MSR_IA32_EBL_CR_POWERON: |
3bab1f5d AK |
1367 | /* MTRR registers */ |
1368 | case 0xfe: | |
1369 | case 0x200 ... 0x2ff: | |
1370 | data = 0; | |
1371 | break; | |
a8d13ea2 AK |
1372 | case 0xcd: /* fsb frequency */ |
1373 | data = 3; | |
1374 | break; | |
3bab1f5d | 1375 | case MSR_IA32_APICBASE: |
7017fc3d | 1376 | data = kvm_get_apic_base(vcpu); |
3bab1f5d | 1377 | break; |
6f00e68f AK |
1378 | case MSR_IA32_MISC_ENABLE: |
1379 | data = vcpu->ia32_misc_enable_msr; | |
1380 | break; | |
3bab1f5d AK |
1381 | #ifdef CONFIG_X86_64 |
1382 | case MSR_EFER: | |
1383 | data = vcpu->shadow_efer; | |
1384 | break; | |
1385 | #endif | |
1386 | default: | |
f0242478 | 1387 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
3bab1f5d AK |
1388 | return 1; |
1389 | } | |
1390 | *pdata = data; | |
1391 | return 0; | |
1392 | } | |
1393 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
1394 | ||
6aa8b732 AK |
1395 | /* |
1396 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
1397 | * Returns 0 on success, non-0 otherwise. | |
1398 | * Assumes vcpu_load() was already called. | |
1399 | */ | |
35f3f286 | 1400 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
6aa8b732 | 1401 | { |
cbdd1bea | 1402 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
6aa8b732 AK |
1403 | } |
1404 | ||
05b3e0c2 | 1405 | #ifdef CONFIG_X86_64 |
6aa8b732 | 1406 | |
3bab1f5d | 1407 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
6aa8b732 | 1408 | { |
6aa8b732 AK |
1409 | if (efer & EFER_RESERVED_BITS) { |
1410 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", | |
1411 | efer); | |
1412 | inject_gp(vcpu); | |
1413 | return; | |
1414 | } | |
1415 | ||
1416 | if (is_paging(vcpu) | |
1417 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { | |
1418 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); | |
1419 | inject_gp(vcpu); | |
1420 | return; | |
1421 | } | |
1422 | ||
cbdd1bea | 1423 | kvm_x86_ops->set_efer(vcpu, efer); |
7725f0ba | 1424 | |
6aa8b732 AK |
1425 | efer &= ~EFER_LMA; |
1426 | efer |= vcpu->shadow_efer & EFER_LMA; | |
1427 | ||
1428 | vcpu->shadow_efer = efer; | |
6aa8b732 | 1429 | } |
6aa8b732 AK |
1430 | |
1431 | #endif | |
1432 | ||
3bab1f5d AK |
1433 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
1434 | { | |
1435 | switch (msr) { | |
1436 | #ifdef CONFIG_X86_64 | |
1437 | case MSR_EFER: | |
1438 | set_efer(vcpu, data); | |
1439 | break; | |
1440 | #endif | |
1441 | case MSR_IA32_MC0_STATUS: | |
f0242478 | 1442 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
3bab1f5d AK |
1443 | __FUNCTION__, data); |
1444 | break; | |
0e5bf0d0 | 1445 | case MSR_IA32_MCG_STATUS: |
f0242478 | 1446 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
0e5bf0d0 SK |
1447 | __FUNCTION__, data); |
1448 | break; | |
3bab1f5d AK |
1449 | case MSR_IA32_UCODE_REV: |
1450 | case MSR_IA32_UCODE_WRITE: | |
1451 | case 0x200 ... 0x2ff: /* MTRRs */ | |
1452 | break; | |
1453 | case MSR_IA32_APICBASE: | |
7017fc3d | 1454 | kvm_set_apic_base(vcpu, data); |
3bab1f5d | 1455 | break; |
6f00e68f AK |
1456 | case MSR_IA32_MISC_ENABLE: |
1457 | vcpu->ia32_misc_enable_msr = data; | |
1458 | break; | |
3bab1f5d | 1459 | default: |
f0242478 | 1460 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr); |
3bab1f5d AK |
1461 | return 1; |
1462 | } | |
1463 | return 0; | |
1464 | } | |
1465 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
1466 | ||
6aa8b732 AK |
1467 | /* |
1468 | * Writes msr value into into the appropriate "register". | |
1469 | * Returns 0 on success, non-0 otherwise. | |
1470 | * Assumes vcpu_load() was already called. | |
1471 | */ | |
35f3f286 | 1472 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
6aa8b732 | 1473 | { |
cbdd1bea | 1474 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
6aa8b732 AK |
1475 | } |
1476 | ||
1477 | void kvm_resched(struct kvm_vcpu *vcpu) | |
1478 | { | |
3fca0365 YD |
1479 | if (!need_resched()) |
1480 | return; | |
6aa8b732 | 1481 | cond_resched(); |
6aa8b732 AK |
1482 | } |
1483 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1484 | ||
06465c5a AK |
1485 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
1486 | { | |
1487 | int i; | |
1488 | u32 function; | |
1489 | struct kvm_cpuid_entry *e, *best; | |
1490 | ||
cbdd1bea | 1491 | kvm_x86_ops->cache_regs(vcpu); |
06465c5a AK |
1492 | function = vcpu->regs[VCPU_REGS_RAX]; |
1493 | vcpu->regs[VCPU_REGS_RAX] = 0; | |
1494 | vcpu->regs[VCPU_REGS_RBX] = 0; | |
1495 | vcpu->regs[VCPU_REGS_RCX] = 0; | |
1496 | vcpu->regs[VCPU_REGS_RDX] = 0; | |
1497 | best = NULL; | |
1498 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
1499 | e = &vcpu->cpuid_entries[i]; | |
1500 | if (e->function == function) { | |
1501 | best = e; | |
1502 | break; | |
1503 | } | |
1504 | /* | |
1505 | * Both basic or both extended? | |
1506 | */ | |
1507 | if (((e->function ^ function) & 0x80000000) == 0) | |
1508 | if (!best || e->function > best->function) | |
1509 | best = e; | |
1510 | } | |
1511 | if (best) { | |
1512 | vcpu->regs[VCPU_REGS_RAX] = best->eax; | |
1513 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; | |
1514 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; | |
1515 | vcpu->regs[VCPU_REGS_RDX] = best->edx; | |
1516 | } | |
cbdd1bea CE |
1517 | kvm_x86_ops->decache_regs(vcpu); |
1518 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
06465c5a AK |
1519 | } |
1520 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); | |
1521 | ||
039576c0 | 1522 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
46fc1477 | 1523 | { |
039576c0 AK |
1524 | void *p = vcpu->pio_data; |
1525 | void *q; | |
1526 | unsigned bytes; | |
1527 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; | |
1528 | ||
039576c0 AK |
1529 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
1530 | PAGE_KERNEL); | |
1531 | if (!q) { | |
039576c0 AK |
1532 | free_pio_guest_pages(vcpu); |
1533 | return -ENOMEM; | |
1534 | } | |
1535 | q += vcpu->pio.guest_page_offset; | |
1536 | bytes = vcpu->pio.size * vcpu->pio.cur_count; | |
1537 | if (vcpu->pio.in) | |
1538 | memcpy(q, p, bytes); | |
1539 | else | |
1540 | memcpy(p, q, bytes); | |
1541 | q -= vcpu->pio.guest_page_offset; | |
1542 | vunmap(q); | |
039576c0 AK |
1543 | free_pio_guest_pages(vcpu); |
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | static int complete_pio(struct kvm_vcpu *vcpu) | |
1548 | { | |
1549 | struct kvm_pio_request *io = &vcpu->pio; | |
46fc1477 | 1550 | long delta; |
039576c0 | 1551 | int r; |
46fc1477 | 1552 | |
cbdd1bea | 1553 | kvm_x86_ops->cache_regs(vcpu); |
46fc1477 AK |
1554 | |
1555 | if (!io->string) { | |
039576c0 AK |
1556 | if (io->in) |
1557 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, | |
46fc1477 AK |
1558 | io->size); |
1559 | } else { | |
039576c0 AK |
1560 | if (io->in) { |
1561 | r = pio_copy_data(vcpu); | |
1562 | if (r) { | |
cbdd1bea | 1563 | kvm_x86_ops->cache_regs(vcpu); |
039576c0 AK |
1564 | return r; |
1565 | } | |
1566 | } | |
1567 | ||
46fc1477 AK |
1568 | delta = 1; |
1569 | if (io->rep) { | |
039576c0 | 1570 | delta *= io->cur_count; |
46fc1477 AK |
1571 | /* |
1572 | * The size of the register should really depend on | |
1573 | * current address size. | |
1574 | */ | |
1575 | vcpu->regs[VCPU_REGS_RCX] -= delta; | |
1576 | } | |
039576c0 | 1577 | if (io->down) |
46fc1477 AK |
1578 | delta = -delta; |
1579 | delta *= io->size; | |
039576c0 | 1580 | if (io->in) |
46fc1477 AK |
1581 | vcpu->regs[VCPU_REGS_RDI] += delta; |
1582 | else | |
1583 | vcpu->regs[VCPU_REGS_RSI] += delta; | |
1584 | } | |
1585 | ||
cbdd1bea | 1586 | kvm_x86_ops->decache_regs(vcpu); |
46fc1477 | 1587 | |
039576c0 AK |
1588 | io->count -= io->cur_count; |
1589 | io->cur_count = 0; | |
1590 | ||
039576c0 | 1591 | return 0; |
46fc1477 AK |
1592 | } |
1593 | ||
65619eb5 ED |
1594 | static void kernel_pio(struct kvm_io_device *pio_dev, |
1595 | struct kvm_vcpu *vcpu, | |
1596 | void *pd) | |
74906345 ED |
1597 | { |
1598 | /* TODO: String I/O for in kernel device */ | |
1599 | ||
9cf98828 | 1600 | mutex_lock(&vcpu->kvm->lock); |
74906345 ED |
1601 | if (vcpu->pio.in) |
1602 | kvm_iodevice_read(pio_dev, vcpu->pio.port, | |
1603 | vcpu->pio.size, | |
65619eb5 | 1604 | pd); |
74906345 ED |
1605 | else |
1606 | kvm_iodevice_write(pio_dev, vcpu->pio.port, | |
1607 | vcpu->pio.size, | |
65619eb5 | 1608 | pd); |
9cf98828 | 1609 | mutex_unlock(&vcpu->kvm->lock); |
65619eb5 ED |
1610 | } |
1611 | ||
1612 | static void pio_string_write(struct kvm_io_device *pio_dev, | |
1613 | struct kvm_vcpu *vcpu) | |
1614 | { | |
1615 | struct kvm_pio_request *io = &vcpu->pio; | |
1616 | void *pd = vcpu->pio_data; | |
1617 | int i; | |
1618 | ||
9cf98828 | 1619 | mutex_lock(&vcpu->kvm->lock); |
65619eb5 ED |
1620 | for (i = 0; i < io->cur_count; i++) { |
1621 | kvm_iodevice_write(pio_dev, io->port, | |
1622 | io->size, | |
1623 | pd); | |
1624 | pd += io->size; | |
1625 | } | |
9cf98828 | 1626 | mutex_unlock(&vcpu->kvm->lock); |
74906345 ED |
1627 | } |
1628 | ||
d77c26fc | 1629 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
3090dd73 LV |
1630 | int size, unsigned port) |
1631 | { | |
1632 | struct kvm_io_device *pio_dev; | |
1633 | ||
1634 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1635 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
1636 | vcpu->run->io.size = vcpu->pio.size = size; | |
1637 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
1638 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1; | |
1639 | vcpu->run->io.port = vcpu->pio.port = port; | |
1640 | vcpu->pio.in = in; | |
1641 | vcpu->pio.string = 0; | |
1642 | vcpu->pio.down = 0; | |
1643 | vcpu->pio.guest_page_offset = 0; | |
1644 | vcpu->pio.rep = 0; | |
1645 | ||
cbdd1bea | 1646 | kvm_x86_ops->cache_regs(vcpu); |
3090dd73 | 1647 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); |
cbdd1bea | 1648 | kvm_x86_ops->decache_regs(vcpu); |
3090dd73 | 1649 | |
0967b7bf AK |
1650 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
1651 | ||
3090dd73 LV |
1652 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
1653 | if (pio_dev) { | |
1654 | kernel_pio(pio_dev, vcpu, vcpu->pio_data); | |
1655 | complete_pio(vcpu); | |
1656 | return 1; | |
1657 | } | |
1658 | return 0; | |
1659 | } | |
1660 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); | |
1661 | ||
1662 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
1663 | int size, unsigned long count, int down, | |
039576c0 AK |
1664 | gva_t address, int rep, unsigned port) |
1665 | { | |
1666 | unsigned now, in_page; | |
65619eb5 | 1667 | int i, ret = 0; |
039576c0 AK |
1668 | int nr_pages = 1; |
1669 | struct page *page; | |
74906345 | 1670 | struct kvm_io_device *pio_dev; |
039576c0 AK |
1671 | |
1672 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1673 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
3090dd73 | 1674 | vcpu->run->io.size = vcpu->pio.size = size; |
039576c0 | 1675 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
3090dd73 LV |
1676 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count; |
1677 | vcpu->run->io.port = vcpu->pio.port = port; | |
039576c0 | 1678 | vcpu->pio.in = in; |
3090dd73 | 1679 | vcpu->pio.string = 1; |
039576c0 AK |
1680 | vcpu->pio.down = down; |
1681 | vcpu->pio.guest_page_offset = offset_in_page(address); | |
1682 | vcpu->pio.rep = rep; | |
1683 | ||
039576c0 | 1684 | if (!count) { |
cbdd1bea | 1685 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
039576c0 AK |
1686 | return 1; |
1687 | } | |
1688 | ||
039576c0 AK |
1689 | if (!down) |
1690 | in_page = PAGE_SIZE - offset_in_page(address); | |
1691 | else | |
1692 | in_page = offset_in_page(address) + size; | |
1693 | now = min(count, (unsigned long)in_page / size); | |
1694 | if (!now) { | |
1695 | /* | |
1696 | * String I/O straddles page boundary. Pin two guest pages | |
1697 | * so that we satisfy atomicity constraints. Do just one | |
1698 | * transaction to avoid complexity. | |
1699 | */ | |
1700 | nr_pages = 2; | |
1701 | now = 1; | |
1702 | } | |
1703 | if (down) { | |
1704 | /* | |
1705 | * String I/O in reverse. Yuck. Kill the guest, fix later. | |
1706 | */ | |
f0242478 | 1707 | pr_unimpl(vcpu, "guest string pio down\n"); |
039576c0 AK |
1708 | inject_gp(vcpu); |
1709 | return 1; | |
1710 | } | |
1711 | vcpu->run->io.count = now; | |
1712 | vcpu->pio.cur_count = now; | |
1713 | ||
0967b7bf AK |
1714 | if (vcpu->pio.cur_count == vcpu->pio.count) |
1715 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
1716 | ||
039576c0 | 1717 | for (i = 0; i < nr_pages; ++i) { |
11ec2804 | 1718 | mutex_lock(&vcpu->kvm->lock); |
039576c0 | 1719 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
039576c0 | 1720 | vcpu->pio.guest_pages[i] = page; |
11ec2804 | 1721 | mutex_unlock(&vcpu->kvm->lock); |
039576c0 AK |
1722 | if (!page) { |
1723 | inject_gp(vcpu); | |
1724 | free_pio_guest_pages(vcpu); | |
1725 | return 1; | |
1726 | } | |
1727 | } | |
1728 | ||
3090dd73 | 1729 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
65619eb5 ED |
1730 | if (!vcpu->pio.in) { |
1731 | /* string PIO write */ | |
1732 | ret = pio_copy_data(vcpu); | |
1733 | if (ret >= 0 && pio_dev) { | |
1734 | pio_string_write(pio_dev, vcpu); | |
1735 | complete_pio(vcpu); | |
1736 | if (vcpu->pio.count == 0) | |
1737 | ret = 1; | |
1738 | } | |
1739 | } else if (pio_dev) | |
f0242478 | 1740 | pr_unimpl(vcpu, "no string pio read support yet, " |
65619eb5 ED |
1741 | "port %x size %d count %ld\n", |
1742 | port, size, count); | |
1743 | ||
1744 | return ret; | |
039576c0 | 1745 | } |
3090dd73 | 1746 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
039576c0 | 1747 | |
04d2cc77 AK |
1748 | /* |
1749 | * Check if userspace requested an interrupt window, and that the | |
1750 | * interrupt window is open. | |
1751 | * | |
1752 | * No need to exit to userspace if we already have an interrupt queued. | |
1753 | */ | |
1754 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
1755 | struct kvm_run *kvm_run) | |
1756 | { | |
1757 | return (!vcpu->irq_summary && | |
1758 | kvm_run->request_interrupt_window && | |
1759 | vcpu->interrupt_window_open && | |
1760 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); | |
1761 | } | |
1762 | ||
1763 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
1764 | struct kvm_run *kvm_run) | |
1765 | { | |
1766 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
1767 | kvm_run->cr8 = get_cr8(vcpu); | |
1768 | kvm_run->apic_base = kvm_get_apic_base(vcpu); | |
1769 | if (irqchip_in_kernel(vcpu->kvm)) | |
1770 | kvm_run->ready_for_interrupt_injection = 1; | |
1771 | else | |
1772 | kvm_run->ready_for_interrupt_injection = | |
1773 | (vcpu->interrupt_window_open && | |
1774 | vcpu->irq_summary == 0); | |
1775 | } | |
1776 | ||
1777 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1778 | { | |
1779 | int r; | |
1780 | ||
1781 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { | |
d77c26fc | 1782 | pr_debug("vcpu %d received sipi with vector # %x\n", |
04d2cc77 AK |
1783 | vcpu->vcpu_id, vcpu->sipi_vector); |
1784 | kvm_lapic_reset(vcpu); | |
e00c8cf2 AK |
1785 | r = kvm_x86_ops->vcpu_reset(vcpu); |
1786 | if (r) | |
1787 | return r; | |
04d2cc77 AK |
1788 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
1789 | } | |
1790 | ||
1791 | preempted: | |
1792 | if (vcpu->guest_debug.enabled) | |
1793 | kvm_x86_ops->guest_debug_pre(vcpu); | |
1794 | ||
1795 | again: | |
1796 | r = kvm_mmu_reload(vcpu); | |
1797 | if (unlikely(r)) | |
1798 | goto out; | |
1799 | ||
ab6ef34b AK |
1800 | kvm_inject_pending_timer_irqs(vcpu); |
1801 | ||
04d2cc77 AK |
1802 | preempt_disable(); |
1803 | ||
1804 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
1805 | kvm_load_guest_fpu(vcpu); | |
1806 | ||
1807 | local_irq_disable(); | |
1808 | ||
1809 | if (signal_pending(current)) { | |
1810 | local_irq_enable(); | |
1811 | preempt_enable(); | |
1812 | r = -EINTR; | |
1813 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
1814 | ++vcpu->stat.signal_exits; | |
1815 | goto out; | |
1816 | } | |
1817 | ||
1818 | if (irqchip_in_kernel(vcpu->kvm)) | |
1819 | kvm_x86_ops->inject_pending_irq(vcpu); | |
1820 | else if (!vcpu->mmio_read_completed) | |
1821 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); | |
1822 | ||
1823 | vcpu->guest_mode = 1; | |
d172fcd3 | 1824 | kvm_guest_enter(); |
04d2cc77 AK |
1825 | |
1826 | if (vcpu->requests) | |
3176bc3e | 1827 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
04d2cc77 AK |
1828 | kvm_x86_ops->tlb_flush(vcpu); |
1829 | ||
1830 | kvm_x86_ops->run(vcpu, kvm_run); | |
1831 | ||
1832 | vcpu->guest_mode = 0; | |
1833 | local_irq_enable(); | |
1834 | ||
1835 | ++vcpu->stat.exits; | |
1836 | ||
0552f73b LV |
1837 | /* |
1838 | * We must have an instruction between local_irq_enable() and | |
1839 | * kvm_guest_exit(), so the timer interrupt isn't delayed by | |
1840 | * the interrupt shadow. The stat.exits increment will do nicely. | |
1841 | * But we need to prevent reordering, hence this barrier(): | |
1842 | */ | |
1843 | barrier(); | |
1844 | ||
1845 | kvm_guest_exit(); | |
1846 | ||
04d2cc77 AK |
1847 | preempt_enable(); |
1848 | ||
1849 | /* | |
1850 | * Profile KVM exit RIPs: | |
1851 | */ | |
1852 | if (unlikely(prof_on == KVM_PROFILING)) { | |
1853 | kvm_x86_ops->cache_regs(vcpu); | |
1854 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); | |
1855 | } | |
1856 | ||
1857 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); | |
1858 | ||
1859 | if (r > 0) { | |
1860 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
1861 | r = -EINTR; | |
1862 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
1863 | ++vcpu->stat.request_irq_exits; | |
1864 | goto out; | |
1865 | } | |
1866 | if (!need_resched()) { | |
1867 | ++vcpu->stat.light_exits; | |
1868 | goto again; | |
1869 | } | |
1870 | } | |
1871 | ||
1872 | out: | |
1873 | if (r > 0) { | |
1874 | kvm_resched(vcpu); | |
1875 | goto preempted; | |
1876 | } | |
1877 | ||
1878 | post_kvm_run_save(vcpu, kvm_run); | |
1879 | ||
1880 | return r; | |
1881 | } | |
1882 | ||
1883 | ||
bccf2150 | 1884 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
6aa8b732 | 1885 | { |
6aa8b732 | 1886 | int r; |
1961d276 | 1887 | sigset_t sigsaved; |
6aa8b732 | 1888 | |
bccf2150 | 1889 | vcpu_load(vcpu); |
6aa8b732 | 1890 | |
c5ec1534 HQ |
1891 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { |
1892 | kvm_vcpu_block(vcpu); | |
1893 | vcpu_put(vcpu); | |
1894 | return -EAGAIN; | |
1895 | } | |
1896 | ||
1961d276 AK |
1897 | if (vcpu->sigset_active) |
1898 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
1899 | ||
54810342 | 1900 | /* re-sync apic's tpr */ |
5cd4f6fd HQ |
1901 | if (!irqchip_in_kernel(vcpu->kvm)) |
1902 | set_cr8(vcpu, kvm_run->cr8); | |
54810342 | 1903 | |
02c83209 AK |
1904 | if (vcpu->pio.cur_count) { |
1905 | r = complete_pio(vcpu); | |
1906 | if (r) | |
1907 | goto out; | |
1908 | } | |
34c16eec | 1909 | #if CONFIG_HAS_IOMEM |
02c83209 AK |
1910 | if (vcpu->mmio_needed) { |
1911 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
1912 | vcpu->mmio_read_completed = 1; | |
1913 | vcpu->mmio_needed = 0; | |
1914 | r = emulate_instruction(vcpu, kvm_run, | |
3427318f | 1915 | vcpu->mmio_fault_cr2, 0, 1); |
02c83209 AK |
1916 | if (r == EMULATE_DO_MMIO) { |
1917 | /* | |
1918 | * Read-modify-write. Back to userspace. | |
1919 | */ | |
02c83209 AK |
1920 | r = 0; |
1921 | goto out; | |
46fc1477 | 1922 | } |
6aa8b732 | 1923 | } |
34c16eec | 1924 | #endif |
8eb7d334 | 1925 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
cbdd1bea | 1926 | kvm_x86_ops->cache_regs(vcpu); |
b4e63f56 | 1927 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
cbdd1bea | 1928 | kvm_x86_ops->decache_regs(vcpu); |
b4e63f56 AK |
1929 | } |
1930 | ||
04d2cc77 | 1931 | r = __vcpu_run(vcpu, kvm_run); |
6aa8b732 | 1932 | |
039576c0 | 1933 | out: |
1961d276 AK |
1934 | if (vcpu->sigset_active) |
1935 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
1936 | ||
6aa8b732 AK |
1937 | vcpu_put(vcpu); |
1938 | return r; | |
1939 | } | |
1940 | ||
bccf2150 AK |
1941 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
1942 | struct kvm_regs *regs) | |
6aa8b732 | 1943 | { |
bccf2150 | 1944 | vcpu_load(vcpu); |
6aa8b732 | 1945 | |
cbdd1bea | 1946 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 AK |
1947 | |
1948 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
1949 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
1950 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
1951 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
1952 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
1953 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
1954 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
1955 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
05b3e0c2 | 1956 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1957 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
1958 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
1959 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
1960 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
1961 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
1962 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
1963 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
1964 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
1965 | #endif | |
1966 | ||
1967 | regs->rip = vcpu->rip; | |
cbdd1bea | 1968 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1969 | |
1970 | /* | |
1971 | * Don't leak debug flags in case they were set for guest debugging | |
1972 | */ | |
1973 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
1974 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
1975 | ||
1976 | vcpu_put(vcpu); | |
1977 | ||
1978 | return 0; | |
1979 | } | |
1980 | ||
bccf2150 AK |
1981 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
1982 | struct kvm_regs *regs) | |
6aa8b732 | 1983 | { |
bccf2150 | 1984 | vcpu_load(vcpu); |
6aa8b732 AK |
1985 | |
1986 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
1987 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
1988 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
1989 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
1990 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
1991 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
1992 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
1993 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
05b3e0c2 | 1994 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1995 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
1996 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
1997 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
1998 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
1999 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
2000 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
2001 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
2002 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
2003 | #endif | |
2004 | ||
2005 | vcpu->rip = regs->rip; | |
cbdd1bea | 2006 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
6aa8b732 | 2007 | |
cbdd1bea | 2008 | kvm_x86_ops->decache_regs(vcpu); |
6aa8b732 AK |
2009 | |
2010 | vcpu_put(vcpu); | |
2011 | ||
2012 | return 0; | |
2013 | } | |
2014 | ||
2015 | static void get_segment(struct kvm_vcpu *vcpu, | |
2016 | struct kvm_segment *var, int seg) | |
2017 | { | |
cbdd1bea | 2018 | return kvm_x86_ops->get_segment(vcpu, var, seg); |
6aa8b732 AK |
2019 | } |
2020 | ||
bccf2150 AK |
2021 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
2022 | struct kvm_sregs *sregs) | |
6aa8b732 | 2023 | { |
6aa8b732 | 2024 | struct descriptor_table dt; |
2a8067f1 | 2025 | int pending_vec; |
6aa8b732 | 2026 | |
bccf2150 | 2027 | vcpu_load(vcpu); |
6aa8b732 AK |
2028 | |
2029 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
2030 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2031 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2032 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2033 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2034 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2035 | ||
2036 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2037 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2038 | ||
cbdd1bea | 2039 | kvm_x86_ops->get_idt(vcpu, &dt); |
6aa8b732 AK |
2040 | sregs->idt.limit = dt.limit; |
2041 | sregs->idt.base = dt.base; | |
cbdd1bea | 2042 | kvm_x86_ops->get_gdt(vcpu, &dt); |
6aa8b732 AK |
2043 | sregs->gdt.limit = dt.limit; |
2044 | sregs->gdt.base = dt.base; | |
2045 | ||
cbdd1bea | 2046 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
2047 | sregs->cr0 = vcpu->cr0; |
2048 | sregs->cr2 = vcpu->cr2; | |
2049 | sregs->cr3 = vcpu->cr3; | |
2050 | sregs->cr4 = vcpu->cr4; | |
7017fc3d | 2051 | sregs->cr8 = get_cr8(vcpu); |
6aa8b732 | 2052 | sregs->efer = vcpu->shadow_efer; |
7017fc3d | 2053 | sregs->apic_base = kvm_get_apic_base(vcpu); |
6aa8b732 | 2054 | |
2a8067f1 | 2055 | if (irqchip_in_kernel(vcpu->kvm)) { |
c52fb35a HQ |
2056 | memset(sregs->interrupt_bitmap, 0, |
2057 | sizeof sregs->interrupt_bitmap); | |
cbdd1bea | 2058 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
2a8067f1 | 2059 | if (pending_vec >= 0) |
d77c26fc MD |
2060 | set_bit(pending_vec, |
2061 | (unsigned long *)sregs->interrupt_bitmap); | |
2a8067f1 | 2062 | } else |
c52fb35a HQ |
2063 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
2064 | sizeof sregs->interrupt_bitmap); | |
6aa8b732 AK |
2065 | |
2066 | vcpu_put(vcpu); | |
2067 | ||
2068 | return 0; | |
2069 | } | |
2070 | ||
2071 | static void set_segment(struct kvm_vcpu *vcpu, | |
2072 | struct kvm_segment *var, int seg) | |
2073 | { | |
cbdd1bea | 2074 | return kvm_x86_ops->set_segment(vcpu, var, seg); |
6aa8b732 AK |
2075 | } |
2076 | ||
bccf2150 AK |
2077 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
2078 | struct kvm_sregs *sregs) | |
6aa8b732 | 2079 | { |
6aa8b732 | 2080 | int mmu_reset_needed = 0; |
2a8067f1 | 2081 | int i, pending_vec, max_bits; |
6aa8b732 AK |
2082 | struct descriptor_table dt; |
2083 | ||
bccf2150 | 2084 | vcpu_load(vcpu); |
6aa8b732 | 2085 | |
6aa8b732 AK |
2086 | dt.limit = sregs->idt.limit; |
2087 | dt.base = sregs->idt.base; | |
cbdd1bea | 2088 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
2089 | dt.limit = sregs->gdt.limit; |
2090 | dt.base = sregs->gdt.base; | |
cbdd1bea | 2091 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
2092 | |
2093 | vcpu->cr2 = sregs->cr2; | |
2094 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
2095 | vcpu->cr3 = sregs->cr3; | |
2096 | ||
7017fc3d | 2097 | set_cr8(vcpu, sregs->cr8); |
6aa8b732 AK |
2098 | |
2099 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
05b3e0c2 | 2100 | #ifdef CONFIG_X86_64 |
cbdd1bea | 2101 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
6aa8b732 | 2102 | #endif |
7017fc3d | 2103 | kvm_set_apic_base(vcpu, sregs->apic_base); |
6aa8b732 | 2104 | |
cbdd1bea | 2105 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
399badf3 | 2106 | |
6aa8b732 | 2107 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
81f50e3b | 2108 | vcpu->cr0 = sregs->cr0; |
cbdd1bea | 2109 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
6aa8b732 AK |
2110 | |
2111 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
cbdd1bea | 2112 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
1b0973bd AK |
2113 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
2114 | load_pdptrs(vcpu, vcpu->cr3); | |
6aa8b732 AK |
2115 | |
2116 | if (mmu_reset_needed) | |
2117 | kvm_mmu_reset_context(vcpu); | |
2118 | ||
c52fb35a HQ |
2119 | if (!irqchip_in_kernel(vcpu->kvm)) { |
2120 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
2121 | sizeof vcpu->irq_pending); | |
2122 | vcpu->irq_summary = 0; | |
2123 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) | |
2124 | if (vcpu->irq_pending[i]) | |
2125 | __set_bit(i, &vcpu->irq_summary); | |
2a8067f1 ED |
2126 | } else { |
2127 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; | |
2128 | pending_vec = find_first_bit( | |
2129 | (const unsigned long *)sregs->interrupt_bitmap, | |
2130 | max_bits); | |
2131 | /* Only pending external irq is handled here */ | |
2132 | if (pending_vec < max_bits) { | |
cbdd1bea | 2133 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
d77c26fc MD |
2134 | pr_debug("Set back pending irq %d\n", |
2135 | pending_vec); | |
2a8067f1 | 2136 | } |
c52fb35a | 2137 | } |
6aa8b732 | 2138 | |
024aa1c0 AK |
2139 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
2140 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2141 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2142 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2143 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2144 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2145 | ||
2146 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2147 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2148 | ||
6aa8b732 AK |
2149 | vcpu_put(vcpu); |
2150 | ||
2151 | return 0; | |
2152 | } | |
2153 | ||
1747fb71 RR |
2154 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
2155 | { | |
2156 | struct kvm_segment cs; | |
2157 | ||
2158 | get_segment(vcpu, &cs, VCPU_SREG_CS); | |
2159 | *db = cs.db; | |
2160 | *l = cs.l; | |
2161 | } | |
2162 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
2163 | ||
6aa8b732 AK |
2164 | /* |
2165 | * Translate a guest virtual address to a guest physical address. | |
2166 | */ | |
bccf2150 AK |
2167 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
2168 | struct kvm_translation *tr) | |
6aa8b732 AK |
2169 | { |
2170 | unsigned long vaddr = tr->linear_address; | |
6aa8b732 AK |
2171 | gpa_t gpa; |
2172 | ||
bccf2150 | 2173 | vcpu_load(vcpu); |
11ec2804 | 2174 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 AK |
2175 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
2176 | tr->physical_address = gpa; | |
2177 | tr->valid = gpa != UNMAPPED_GVA; | |
2178 | tr->writeable = 1; | |
2179 | tr->usermode = 0; | |
11ec2804 | 2180 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
2181 | vcpu_put(vcpu); |
2182 | ||
2183 | return 0; | |
2184 | } | |
2185 | ||
bccf2150 AK |
2186 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
2187 | struct kvm_interrupt *irq) | |
6aa8b732 | 2188 | { |
6aa8b732 AK |
2189 | if (irq->irq < 0 || irq->irq >= 256) |
2190 | return -EINVAL; | |
97222cc8 ED |
2191 | if (irqchip_in_kernel(vcpu->kvm)) |
2192 | return -ENXIO; | |
bccf2150 | 2193 | vcpu_load(vcpu); |
6aa8b732 AK |
2194 | |
2195 | set_bit(irq->irq, vcpu->irq_pending); | |
2196 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
2197 | ||
2198 | vcpu_put(vcpu); | |
2199 | ||
2200 | return 0; | |
2201 | } | |
2202 | ||
bccf2150 AK |
2203 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
2204 | struct kvm_debug_guest *dbg) | |
6aa8b732 | 2205 | { |
6aa8b732 AK |
2206 | int r; |
2207 | ||
bccf2150 | 2208 | vcpu_load(vcpu); |
6aa8b732 | 2209 | |
cbdd1bea | 2210 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
6aa8b732 AK |
2211 | |
2212 | vcpu_put(vcpu); | |
2213 | ||
2214 | return r; | |
2215 | } | |
2216 | ||
9a2bb7f4 AK |
2217 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
2218 | unsigned long address, | |
2219 | int *type) | |
2220 | { | |
2221 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
2222 | unsigned long pgoff; | |
2223 | struct page *page; | |
2224 | ||
9a2bb7f4 | 2225 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
039576c0 AK |
2226 | if (pgoff == 0) |
2227 | page = virt_to_page(vcpu->run); | |
2228 | else if (pgoff == KVM_PIO_PAGE_OFFSET) | |
2229 | page = virt_to_page(vcpu->pio_data); | |
2230 | else | |
9a2bb7f4 | 2231 | return NOPAGE_SIGBUS; |
9a2bb7f4 | 2232 | get_page(page); |
cd0d9137 NAQ |
2233 | if (type != NULL) |
2234 | *type = VM_FAULT_MINOR; | |
2235 | ||
9a2bb7f4 AK |
2236 | return page; |
2237 | } | |
2238 | ||
2239 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
2240 | .nopage = kvm_vcpu_nopage, | |
2241 | }; | |
2242 | ||
2243 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
2244 | { | |
2245 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
2246 | return 0; | |
2247 | } | |
2248 | ||
bccf2150 AK |
2249 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
2250 | { | |
2251 | struct kvm_vcpu *vcpu = filp->private_data; | |
2252 | ||
2253 | fput(vcpu->kvm->filp); | |
2254 | return 0; | |
2255 | } | |
2256 | ||
2257 | static struct file_operations kvm_vcpu_fops = { | |
2258 | .release = kvm_vcpu_release, | |
2259 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
2260 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 2261 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
2262 | }; |
2263 | ||
2264 | /* | |
2265 | * Allocates an inode for the vcpu. | |
2266 | */ | |
2267 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
2268 | { | |
2269 | int fd, r; | |
2270 | struct inode *inode; | |
2271 | struct file *file; | |
2272 | ||
d6d28168 AK |
2273 | r = anon_inode_getfd(&fd, &inode, &file, |
2274 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); | |
2275 | if (r) | |
2276 | return r; | |
bccf2150 | 2277 | atomic_inc(&vcpu->kvm->filp->f_count); |
bccf2150 | 2278 | return fd; |
bccf2150 AK |
2279 | } |
2280 | ||
c5ea7660 AK |
2281 | /* |
2282 | * Creates some virtual cpus. Good luck creating more than one. | |
2283 | */ | |
2284 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
2285 | { | |
2286 | int r; | |
2287 | struct kvm_vcpu *vcpu; | |
2288 | ||
c5ea7660 | 2289 | if (!valid_vcpu(n)) |
fb3f0f51 | 2290 | return -EINVAL; |
c5ea7660 | 2291 | |
cbdd1bea | 2292 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
fb3f0f51 RR |
2293 | if (IS_ERR(vcpu)) |
2294 | return PTR_ERR(vcpu); | |
c5ea7660 | 2295 | |
15ad7146 AK |
2296 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
2297 | ||
b114b080 RR |
2298 | /* We do fxsave: this must be aligned. */ |
2299 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); | |
2300 | ||
fb3f0f51 | 2301 | vcpu_load(vcpu); |
e00c8cf2 AK |
2302 | r = kvm_x86_ops->vcpu_reset(vcpu); |
2303 | if (r == 0) | |
2304 | r = kvm_mmu_setup(vcpu); | |
c5ea7660 | 2305 | vcpu_put(vcpu); |
c5ea7660 | 2306 | if (r < 0) |
fb3f0f51 RR |
2307 | goto free_vcpu; |
2308 | ||
11ec2804 | 2309 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
2310 | if (kvm->vcpus[n]) { |
2311 | r = -EEXIST; | |
11ec2804 | 2312 | mutex_unlock(&kvm->lock); |
fb3f0f51 RR |
2313 | goto mmu_unload; |
2314 | } | |
2315 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 2316 | mutex_unlock(&kvm->lock); |
c5ea7660 | 2317 | |
fb3f0f51 | 2318 | /* Now it's all set up, let userspace reach it */ |
bccf2150 AK |
2319 | r = create_vcpu_fd(vcpu); |
2320 | if (r < 0) | |
fb3f0f51 RR |
2321 | goto unlink; |
2322 | return r; | |
39c3b86e | 2323 | |
fb3f0f51 | 2324 | unlink: |
11ec2804 | 2325 | mutex_lock(&kvm->lock); |
fb3f0f51 | 2326 | kvm->vcpus[n] = NULL; |
11ec2804 | 2327 | mutex_unlock(&kvm->lock); |
a2fa3e9f | 2328 | |
fb3f0f51 RR |
2329 | mmu_unload: |
2330 | vcpu_load(vcpu); | |
2331 | kvm_mmu_unload(vcpu); | |
2332 | vcpu_put(vcpu); | |
c5ea7660 | 2333 | |
fb3f0f51 | 2334 | free_vcpu: |
cbdd1bea | 2335 | kvm_x86_ops->vcpu_free(vcpu); |
c5ea7660 AK |
2336 | return r; |
2337 | } | |
2338 | ||
1961d276 AK |
2339 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
2340 | { | |
2341 | if (sigset) { | |
2342 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
2343 | vcpu->sigset_active = 1; | |
2344 | vcpu->sigset = *sigset; | |
2345 | } else | |
2346 | vcpu->sigset_active = 0; | |
2347 | return 0; | |
2348 | } | |
2349 | ||
b8836737 AK |
2350 | /* |
2351 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when | |
2352 | * we have asm/x86/processor.h | |
2353 | */ | |
2354 | struct fxsave { | |
2355 | u16 cwd; | |
2356 | u16 swd; | |
2357 | u16 twd; | |
2358 | u16 fop; | |
2359 | u64 rip; | |
2360 | u64 rdp; | |
2361 | u32 mxcsr; | |
2362 | u32 mxcsr_mask; | |
2363 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ | |
2364 | #ifdef CONFIG_X86_64 | |
2365 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ | |
2366 | #else | |
2367 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ | |
2368 | #endif | |
2369 | }; | |
2370 | ||
2371 | static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2372 | { | |
b114b080 | 2373 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
b8836737 AK |
2374 | |
2375 | vcpu_load(vcpu); | |
2376 | ||
2377 | memcpy(fpu->fpr, fxsave->st_space, 128); | |
2378 | fpu->fcw = fxsave->cwd; | |
2379 | fpu->fsw = fxsave->swd; | |
2380 | fpu->ftwx = fxsave->twd; | |
2381 | fpu->last_opcode = fxsave->fop; | |
2382 | fpu->last_ip = fxsave->rip; | |
2383 | fpu->last_dp = fxsave->rdp; | |
2384 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); | |
2385 | ||
2386 | vcpu_put(vcpu); | |
2387 | ||
2388 | return 0; | |
2389 | } | |
2390 | ||
2391 | static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2392 | { | |
b114b080 | 2393 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
b8836737 AK |
2394 | |
2395 | vcpu_load(vcpu); | |
2396 | ||
2397 | memcpy(fxsave->st_space, fpu->fpr, 128); | |
2398 | fxsave->cwd = fpu->fcw; | |
2399 | fxsave->swd = fpu->fsw; | |
2400 | fxsave->twd = fpu->ftwx; | |
2401 | fxsave->fop = fpu->last_opcode; | |
2402 | fxsave->rip = fpu->last_ip; | |
2403 | fxsave->rdp = fpu->last_dp; | |
2404 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); | |
2405 | ||
2406 | vcpu_put(vcpu); | |
2407 | ||
2408 | return 0; | |
2409 | } | |
2410 | ||
bccf2150 AK |
2411 | static long kvm_vcpu_ioctl(struct file *filp, |
2412 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 2413 | { |
bccf2150 | 2414 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 2415 | void __user *argp = (void __user *)arg; |
313a3dc7 | 2416 | int r; |
6aa8b732 AK |
2417 | |
2418 | switch (ioctl) { | |
9a2bb7f4 | 2419 | case KVM_RUN: |
f0fe5108 AK |
2420 | r = -EINVAL; |
2421 | if (arg) | |
2422 | goto out; | |
9a2bb7f4 | 2423 | r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 2424 | break; |
6aa8b732 AK |
2425 | case KVM_GET_REGS: { |
2426 | struct kvm_regs kvm_regs; | |
2427 | ||
bccf2150 AK |
2428 | memset(&kvm_regs, 0, sizeof kvm_regs); |
2429 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); | |
6aa8b732 AK |
2430 | if (r) |
2431 | goto out; | |
2432 | r = -EFAULT; | |
2f366987 | 2433 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
6aa8b732 AK |
2434 | goto out; |
2435 | r = 0; | |
2436 | break; | |
2437 | } | |
2438 | case KVM_SET_REGS: { | |
2439 | struct kvm_regs kvm_regs; | |
2440 | ||
2441 | r = -EFAULT; | |
2f366987 | 2442 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
6aa8b732 | 2443 | goto out; |
bccf2150 | 2444 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
2445 | if (r) |
2446 | goto out; | |
2447 | r = 0; | |
2448 | break; | |
2449 | } | |
2450 | case KVM_GET_SREGS: { | |
2451 | struct kvm_sregs kvm_sregs; | |
2452 | ||
bccf2150 AK |
2453 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
2454 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); | |
6aa8b732 AK |
2455 | if (r) |
2456 | goto out; | |
2457 | r = -EFAULT; | |
2f366987 | 2458 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
6aa8b732 AK |
2459 | goto out; |
2460 | r = 0; | |
2461 | break; | |
2462 | } | |
2463 | case KVM_SET_SREGS: { | |
2464 | struct kvm_sregs kvm_sregs; | |
2465 | ||
2466 | r = -EFAULT; | |
2f366987 | 2467 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
6aa8b732 | 2468 | goto out; |
bccf2150 | 2469 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
2470 | if (r) |
2471 | goto out; | |
2472 | r = 0; | |
2473 | break; | |
2474 | } | |
2475 | case KVM_TRANSLATE: { | |
2476 | struct kvm_translation tr; | |
2477 | ||
2478 | r = -EFAULT; | |
2f366987 | 2479 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 2480 | goto out; |
bccf2150 | 2481 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
2482 | if (r) |
2483 | goto out; | |
2484 | r = -EFAULT; | |
2f366987 | 2485 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
2486 | goto out; |
2487 | r = 0; | |
2488 | break; | |
2489 | } | |
2490 | case KVM_INTERRUPT: { | |
2491 | struct kvm_interrupt irq; | |
2492 | ||
2493 | r = -EFAULT; | |
2f366987 | 2494 | if (copy_from_user(&irq, argp, sizeof irq)) |
6aa8b732 | 2495 | goto out; |
bccf2150 | 2496 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
6aa8b732 AK |
2497 | if (r) |
2498 | goto out; | |
2499 | r = 0; | |
2500 | break; | |
2501 | } | |
2502 | case KVM_DEBUG_GUEST: { | |
2503 | struct kvm_debug_guest dbg; | |
2504 | ||
2505 | r = -EFAULT; | |
2f366987 | 2506 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 2507 | goto out; |
bccf2150 | 2508 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
2509 | if (r) |
2510 | goto out; | |
2511 | r = 0; | |
2512 | break; | |
2513 | } | |
1961d276 AK |
2514 | case KVM_SET_SIGNAL_MASK: { |
2515 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2516 | struct kvm_signal_mask kvm_sigmask; | |
2517 | sigset_t sigset, *p; | |
2518 | ||
2519 | p = NULL; | |
2520 | if (argp) { | |
2521 | r = -EFAULT; | |
2522 | if (copy_from_user(&kvm_sigmask, argp, | |
2523 | sizeof kvm_sigmask)) | |
2524 | goto out; | |
2525 | r = -EINVAL; | |
2526 | if (kvm_sigmask.len != sizeof sigset) | |
2527 | goto out; | |
2528 | r = -EFAULT; | |
2529 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
2530 | sizeof sigset)) | |
2531 | goto out; | |
2532 | p = &sigset; | |
2533 | } | |
2534 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
2535 | break; | |
2536 | } | |
b8836737 AK |
2537 | case KVM_GET_FPU: { |
2538 | struct kvm_fpu fpu; | |
2539 | ||
2540 | memset(&fpu, 0, sizeof fpu); | |
2541 | r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu); | |
2542 | if (r) | |
2543 | goto out; | |
2544 | r = -EFAULT; | |
2545 | if (copy_to_user(argp, &fpu, sizeof fpu)) | |
2546 | goto out; | |
2547 | r = 0; | |
2548 | break; | |
2549 | } | |
2550 | case KVM_SET_FPU: { | |
2551 | struct kvm_fpu fpu; | |
2552 | ||
2553 | r = -EFAULT; | |
2554 | if (copy_from_user(&fpu, argp, sizeof fpu)) | |
2555 | goto out; | |
2556 | r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu); | |
2557 | if (r) | |
2558 | goto out; | |
2559 | r = 0; | |
2560 | break; | |
2561 | } | |
bccf2150 | 2562 | default: |
313a3dc7 | 2563 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
2564 | } |
2565 | out: | |
2566 | return r; | |
2567 | } | |
2568 | ||
2569 | static long kvm_vm_ioctl(struct file *filp, | |
2570 | unsigned int ioctl, unsigned long arg) | |
2571 | { | |
2572 | struct kvm *kvm = filp->private_data; | |
2573 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 2574 | int r; |
bccf2150 AK |
2575 | |
2576 | switch (ioctl) { | |
2577 | case KVM_CREATE_VCPU: | |
2578 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
2579 | if (r < 0) | |
2580 | goto out; | |
2581 | break; | |
6fc138d2 IE |
2582 | case KVM_SET_USER_MEMORY_REGION: { |
2583 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
2584 | ||
2585 | r = -EFAULT; | |
2586 | if (copy_from_user(&kvm_userspace_mem, argp, | |
2587 | sizeof kvm_userspace_mem)) | |
2588 | goto out; | |
2589 | ||
2590 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); | |
6aa8b732 AK |
2591 | if (r) |
2592 | goto out; | |
2593 | break; | |
2594 | } | |
2595 | case KVM_GET_DIRTY_LOG: { | |
2596 | struct kvm_dirty_log log; | |
2597 | ||
2598 | r = -EFAULT; | |
2f366987 | 2599 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 2600 | goto out; |
2c6f5df9 | 2601 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
2602 | if (r) |
2603 | goto out; | |
2604 | break; | |
2605 | } | |
f17abe9a | 2606 | default: |
1fe779f8 | 2607 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
2608 | } |
2609 | out: | |
2610 | return r; | |
2611 | } | |
2612 | ||
2613 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | |
2614 | unsigned long address, | |
2615 | int *type) | |
2616 | { | |
2617 | struct kvm *kvm = vma->vm_file->private_data; | |
2618 | unsigned long pgoff; | |
f17abe9a AK |
2619 | struct page *page; |
2620 | ||
f17abe9a | 2621 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
e0d62c7f IE |
2622 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
2623 | return NOPAGE_SIGBUS; | |
954bbbc2 | 2624 | page = gfn_to_page(kvm, pgoff); |
8a7ae055 IE |
2625 | if (is_error_page(page)) { |
2626 | kvm_release_page(page); | |
f17abe9a | 2627 | return NOPAGE_SIGBUS; |
8a7ae055 | 2628 | } |
cd0d9137 NAQ |
2629 | if (type != NULL) |
2630 | *type = VM_FAULT_MINOR; | |
2631 | ||
f17abe9a AK |
2632 | return page; |
2633 | } | |
2634 | ||
2635 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
2636 | .nopage = kvm_vm_nopage, | |
2637 | }; | |
2638 | ||
2639 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
2640 | { | |
2641 | vma->vm_ops = &kvm_vm_vm_ops; | |
2642 | return 0; | |
2643 | } | |
2644 | ||
2645 | static struct file_operations kvm_vm_fops = { | |
2646 | .release = kvm_vm_release, | |
2647 | .unlocked_ioctl = kvm_vm_ioctl, | |
2648 | .compat_ioctl = kvm_vm_ioctl, | |
2649 | .mmap = kvm_vm_mmap, | |
2650 | }; | |
2651 | ||
2652 | static int kvm_dev_ioctl_create_vm(void) | |
2653 | { | |
2654 | int fd, r; | |
2655 | struct inode *inode; | |
2656 | struct file *file; | |
2657 | struct kvm *kvm; | |
2658 | ||
f17abe9a | 2659 | kvm = kvm_create_vm(); |
d6d28168 AK |
2660 | if (IS_ERR(kvm)) |
2661 | return PTR_ERR(kvm); | |
2662 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); | |
2663 | if (r) { | |
2664 | kvm_destroy_vm(kvm); | |
2665 | return r; | |
f17abe9a AK |
2666 | } |
2667 | ||
bccf2150 | 2668 | kvm->filp = file; |
f17abe9a | 2669 | |
f17abe9a | 2670 | return fd; |
f17abe9a AK |
2671 | } |
2672 | ||
2673 | static long kvm_dev_ioctl(struct file *filp, | |
2674 | unsigned int ioctl, unsigned long arg) | |
2675 | { | |
2676 | void __user *argp = (void __user *)arg; | |
07c45a36 | 2677 | long r = -EINVAL; |
f17abe9a AK |
2678 | |
2679 | switch (ioctl) { | |
2680 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
2681 | r = -EINVAL; |
2682 | if (arg) | |
2683 | goto out; | |
f17abe9a AK |
2684 | r = KVM_API_VERSION; |
2685 | break; | |
2686 | case KVM_CREATE_VM: | |
f0fe5108 AK |
2687 | r = -EINVAL; |
2688 | if (arg) | |
2689 | goto out; | |
f17abe9a AK |
2690 | r = kvm_dev_ioctl_create_vm(); |
2691 | break; | |
85f455f7 ED |
2692 | case KVM_CHECK_EXTENSION: { |
2693 | int ext = (long)argp; | |
2694 | ||
2695 | switch (ext) { | |
2696 | case KVM_CAP_IRQCHIP: | |
b6958ce4 | 2697 | case KVM_CAP_HLT: |
82ce2c96 | 2698 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
6fc138d2 | 2699 | case KVM_CAP_USER_MEMORY: |
cbc94022 | 2700 | case KVM_CAP_SET_TSS_ADDR: |
85f455f7 ED |
2701 | r = 1; |
2702 | break; | |
2703 | default: | |
2704 | r = 0; | |
2705 | break; | |
2706 | } | |
5d308f45 | 2707 | break; |
85f455f7 | 2708 | } |
07c45a36 AK |
2709 | case KVM_GET_VCPU_MMAP_SIZE: |
2710 | r = -EINVAL; | |
2711 | if (arg) | |
2712 | goto out; | |
039576c0 | 2713 | r = 2 * PAGE_SIZE; |
07c45a36 | 2714 | break; |
6aa8b732 | 2715 | default: |
043405e1 | 2716 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
2717 | } |
2718 | out: | |
2719 | return r; | |
2720 | } | |
2721 | ||
6aa8b732 | 2722 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
2723 | .unlocked_ioctl = kvm_dev_ioctl, |
2724 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
2725 | }; |
2726 | ||
2727 | static struct miscdevice kvm_dev = { | |
bbe4432e | 2728 | KVM_MINOR, |
6aa8b732 AK |
2729 | "kvm", |
2730 | &kvm_chardev_ops, | |
2731 | }; | |
2732 | ||
774c47f1 AK |
2733 | /* |
2734 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus | |
2735 | * cached on it. | |
2736 | */ | |
2737 | static void decache_vcpus_on_cpu(int cpu) | |
2738 | { | |
2739 | struct kvm *vm; | |
2740 | struct kvm_vcpu *vcpu; | |
2741 | int i; | |
2742 | ||
2743 | spin_lock(&kvm_lock); | |
11ec2804 | 2744 | list_for_each_entry(vm, &vm_list, vm_list) |
774c47f1 | 2745 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
fb3f0f51 RR |
2746 | vcpu = vm->vcpus[i]; |
2747 | if (!vcpu) | |
2748 | continue; | |
774c47f1 AK |
2749 | /* |
2750 | * If the vcpu is locked, then it is running on some | |
2751 | * other cpu and therefore it is not cached on the | |
2752 | * cpu in question. | |
2753 | * | |
2754 | * If it's not locked, check the last cpu it executed | |
2755 | * on. | |
2756 | */ | |
2757 | if (mutex_trylock(&vcpu->mutex)) { | |
2758 | if (vcpu->cpu == cpu) { | |
cbdd1bea | 2759 | kvm_x86_ops->vcpu_decache(vcpu); |
774c47f1 AK |
2760 | vcpu->cpu = -1; |
2761 | } | |
2762 | mutex_unlock(&vcpu->mutex); | |
2763 | } | |
2764 | } | |
2765 | spin_unlock(&kvm_lock); | |
2766 | } | |
2767 | ||
1b6c0168 AK |
2768 | static void hardware_enable(void *junk) |
2769 | { | |
2770 | int cpu = raw_smp_processor_id(); | |
2771 | ||
2772 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
2773 | return; | |
2774 | cpu_set(cpu, cpus_hardware_enabled); | |
cbdd1bea | 2775 | kvm_x86_ops->hardware_enable(NULL); |
1b6c0168 AK |
2776 | } |
2777 | ||
2778 | static void hardware_disable(void *junk) | |
2779 | { | |
2780 | int cpu = raw_smp_processor_id(); | |
2781 | ||
2782 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
2783 | return; | |
2784 | cpu_clear(cpu, cpus_hardware_enabled); | |
2785 | decache_vcpus_on_cpu(cpu); | |
cbdd1bea | 2786 | kvm_x86_ops->hardware_disable(NULL); |
1b6c0168 AK |
2787 | } |
2788 | ||
774c47f1 AK |
2789 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
2790 | void *v) | |
2791 | { | |
2792 | int cpu = (long)v; | |
2793 | ||
2794 | switch (val) { | |
cec9ad27 AK |
2795 | case CPU_DYING: |
2796 | case CPU_DYING_FROZEN: | |
6ec8a856 AK |
2797 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
2798 | cpu); | |
2799 | hardware_disable(NULL); | |
2800 | break; | |
774c47f1 | 2801 | case CPU_UP_CANCELED: |
8bb78442 | 2802 | case CPU_UP_CANCELED_FROZEN: |
43934a38 JK |
2803 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
2804 | cpu); | |
1b6c0168 | 2805 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
774c47f1 | 2806 | break; |
43934a38 | 2807 | case CPU_ONLINE: |
8bb78442 | 2808 | case CPU_ONLINE_FROZEN: |
43934a38 JK |
2809 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
2810 | cpu); | |
1b6c0168 | 2811 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
774c47f1 AK |
2812 | break; |
2813 | } | |
2814 | return NOTIFY_OK; | |
2815 | } | |
2816 | ||
9a2b85c6 | 2817 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 2818 | void *v) |
9a2b85c6 RR |
2819 | { |
2820 | if (val == SYS_RESTART) { | |
2821 | /* | |
2822 | * Some (well, at least mine) BIOSes hang on reboot if | |
2823 | * in vmx root mode. | |
2824 | */ | |
2825 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
2826 | on_each_cpu(hardware_disable, NULL, 0, 1); | |
2827 | } | |
2828 | return NOTIFY_OK; | |
2829 | } | |
2830 | ||
2831 | static struct notifier_block kvm_reboot_notifier = { | |
2832 | .notifier_call = kvm_reboot, | |
2833 | .priority = 0, | |
2834 | }; | |
2835 | ||
2eeb2e94 GH |
2836 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
2837 | { | |
2838 | memset(bus, 0, sizeof(*bus)); | |
2839 | } | |
2840 | ||
2841 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
2842 | { | |
2843 | int i; | |
2844 | ||
2845 | for (i = 0; i < bus->dev_count; i++) { | |
2846 | struct kvm_io_device *pos = bus->devs[i]; | |
2847 | ||
2848 | kvm_iodevice_destructor(pos); | |
2849 | } | |
2850 | } | |
2851 | ||
2852 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) | |
2853 | { | |
2854 | int i; | |
2855 | ||
2856 | for (i = 0; i < bus->dev_count; i++) { | |
2857 | struct kvm_io_device *pos = bus->devs[i]; | |
2858 | ||
2859 | if (pos->in_range(pos, addr)) | |
2860 | return pos; | |
2861 | } | |
2862 | ||
2863 | return NULL; | |
2864 | } | |
2865 | ||
2866 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
2867 | { | |
2868 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
2869 | ||
2870 | bus->devs[bus->dev_count++] = dev; | |
2871 | } | |
2872 | ||
774c47f1 AK |
2873 | static struct notifier_block kvm_cpu_notifier = { |
2874 | .notifier_call = kvm_cpu_hotplug, | |
2875 | .priority = 20, /* must be > scheduler priority */ | |
2876 | }; | |
2877 | ||
1165f5fe AK |
2878 | static u64 stat_get(void *_offset) |
2879 | { | |
2880 | unsigned offset = (long)_offset; | |
2881 | u64 total = 0; | |
2882 | struct kvm *kvm; | |
2883 | struct kvm_vcpu *vcpu; | |
2884 | int i; | |
2885 | ||
2886 | spin_lock(&kvm_lock); | |
2887 | list_for_each_entry(kvm, &vm_list, vm_list) | |
2888 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
2889 | vcpu = kvm->vcpus[i]; |
2890 | if (vcpu) | |
2891 | total += *(u32 *)((void *)vcpu + offset); | |
1165f5fe AK |
2892 | } |
2893 | spin_unlock(&kvm_lock); | |
2894 | return total; | |
2895 | } | |
2896 | ||
3dea7ca7 | 2897 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
1165f5fe | 2898 | |
6aa8b732 AK |
2899 | static __init void kvm_init_debug(void) |
2900 | { | |
2901 | struct kvm_stats_debugfs_item *p; | |
2902 | ||
8b6d44c7 | 2903 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 2904 | for (p = debugfs_entries; p->name; ++p) |
1165f5fe AK |
2905 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
2906 | (void *)(long)p->offset, | |
2907 | &stat_fops); | |
6aa8b732 AK |
2908 | } |
2909 | ||
2910 | static void kvm_exit_debug(void) | |
2911 | { | |
2912 | struct kvm_stats_debugfs_item *p; | |
2913 | ||
2914 | for (p = debugfs_entries; p->name; ++p) | |
2915 | debugfs_remove(p->dentry); | |
2916 | debugfs_remove(debugfs_dir); | |
2917 | } | |
2918 | ||
59ae6c6b AK |
2919 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
2920 | { | |
4267c41a | 2921 | hardware_disable(NULL); |
59ae6c6b AK |
2922 | return 0; |
2923 | } | |
2924 | ||
2925 | static int kvm_resume(struct sys_device *dev) | |
2926 | { | |
4267c41a | 2927 | hardware_enable(NULL); |
59ae6c6b AK |
2928 | return 0; |
2929 | } | |
2930 | ||
2931 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 2932 | .name = "kvm", |
59ae6c6b AK |
2933 | .suspend = kvm_suspend, |
2934 | .resume = kvm_resume, | |
2935 | }; | |
2936 | ||
2937 | static struct sys_device kvm_sysdev = { | |
2938 | .id = 0, | |
2939 | .cls = &kvm_sysdev_class, | |
2940 | }; | |
2941 | ||
cea7bb21 | 2942 | struct page *bad_page; |
6aa8b732 | 2943 | |
15ad7146 AK |
2944 | static inline |
2945 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
2946 | { | |
2947 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
2948 | } | |
2949 | ||
2950 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
2951 | { | |
2952 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
2953 | ||
cbdd1bea | 2954 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 AK |
2955 | } |
2956 | ||
2957 | static void kvm_sched_out(struct preempt_notifier *pn, | |
2958 | struct task_struct *next) | |
2959 | { | |
2960 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
2961 | ||
cbdd1bea | 2962 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
2963 | } |
2964 | ||
cbdd1bea | 2965 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
c16f862d | 2966 | struct module *module) |
6aa8b732 AK |
2967 | { |
2968 | int r; | |
002c7f7c | 2969 | int cpu; |
6aa8b732 | 2970 | |
cbdd1bea | 2971 | if (kvm_x86_ops) { |
09db28b8 YI |
2972 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
2973 | return -EEXIST; | |
2974 | } | |
2975 | ||
e097f35c | 2976 | if (!ops->cpu_has_kvm_support()) { |
6aa8b732 AK |
2977 | printk(KERN_ERR "kvm: no hardware support\n"); |
2978 | return -EOPNOTSUPP; | |
2979 | } | |
e097f35c | 2980 | if (ops->disabled_by_bios()) { |
6aa8b732 AK |
2981 | printk(KERN_ERR "kvm: disabled by bios\n"); |
2982 | return -EOPNOTSUPP; | |
2983 | } | |
2984 | ||
cbdd1bea | 2985 | kvm_x86_ops = ops; |
e097f35c | 2986 | |
cbdd1bea | 2987 | r = kvm_x86_ops->hardware_setup(); |
6aa8b732 | 2988 | if (r < 0) |
ca45aaae | 2989 | goto out; |
6aa8b732 | 2990 | |
002c7f7c YS |
2991 | for_each_online_cpu(cpu) { |
2992 | smp_call_function_single(cpu, | |
cbdd1bea | 2993 | kvm_x86_ops->check_processor_compatibility, |
002c7f7c YS |
2994 | &r, 0, 1); |
2995 | if (r < 0) | |
2996 | goto out_free_0; | |
2997 | } | |
2998 | ||
1b6c0168 | 2999 | on_each_cpu(hardware_enable, NULL, 0, 1); |
774c47f1 AK |
3000 | r = register_cpu_notifier(&kvm_cpu_notifier); |
3001 | if (r) | |
3002 | goto out_free_1; | |
6aa8b732 AK |
3003 | register_reboot_notifier(&kvm_reboot_notifier); |
3004 | ||
59ae6c6b AK |
3005 | r = sysdev_class_register(&kvm_sysdev_class); |
3006 | if (r) | |
3007 | goto out_free_2; | |
3008 | ||
3009 | r = sysdev_register(&kvm_sysdev); | |
3010 | if (r) | |
3011 | goto out_free_3; | |
3012 | ||
c16f862d RR |
3013 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
3014 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
3015 | __alignof__(struct kvm_vcpu), 0, 0); | |
3016 | if (!kvm_vcpu_cache) { | |
3017 | r = -ENOMEM; | |
3018 | goto out_free_4; | |
3019 | } | |
3020 | ||
6aa8b732 AK |
3021 | kvm_chardev_ops.owner = module; |
3022 | ||
3023 | r = misc_register(&kvm_dev); | |
3024 | if (r) { | |
d77c26fc | 3025 | printk(KERN_ERR "kvm: misc device register failed\n"); |
6aa8b732 AK |
3026 | goto out_free; |
3027 | } | |
3028 | ||
15ad7146 AK |
3029 | kvm_preempt_ops.sched_in = kvm_sched_in; |
3030 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
3031 | ||
c7addb90 AK |
3032 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
3033 | ||
3034 | return 0; | |
6aa8b732 AK |
3035 | |
3036 | out_free: | |
c16f862d RR |
3037 | kmem_cache_destroy(kvm_vcpu_cache); |
3038 | out_free_4: | |
59ae6c6b AK |
3039 | sysdev_unregister(&kvm_sysdev); |
3040 | out_free_3: | |
3041 | sysdev_class_unregister(&kvm_sysdev_class); | |
3042 | out_free_2: | |
6aa8b732 | 3043 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 AK |
3044 | unregister_cpu_notifier(&kvm_cpu_notifier); |
3045 | out_free_1: | |
1b6c0168 | 3046 | on_each_cpu(hardware_disable, NULL, 0, 1); |
002c7f7c | 3047 | out_free_0: |
cbdd1bea | 3048 | kvm_x86_ops->hardware_unsetup(); |
ca45aaae | 3049 | out: |
cbdd1bea | 3050 | kvm_x86_ops = NULL; |
6aa8b732 AK |
3051 | return r; |
3052 | } | |
d77c26fc | 3053 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
6aa8b732 | 3054 | |
cbdd1bea | 3055 | void kvm_exit_x86(void) |
6aa8b732 AK |
3056 | { |
3057 | misc_deregister(&kvm_dev); | |
c16f862d | 3058 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
3059 | sysdev_unregister(&kvm_sysdev); |
3060 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 3061 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 3062 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1b6c0168 | 3063 | on_each_cpu(hardware_disable, NULL, 0, 1); |
cbdd1bea CE |
3064 | kvm_x86_ops->hardware_unsetup(); |
3065 | kvm_x86_ops = NULL; | |
6aa8b732 | 3066 | } |
d77c26fc | 3067 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |
6aa8b732 AK |
3068 | |
3069 | static __init int kvm_init(void) | |
3070 | { | |
37e29d90 AK |
3071 | int r; |
3072 | ||
b5a33a75 AK |
3073 | r = kvm_mmu_module_init(); |
3074 | if (r) | |
3075 | goto out4; | |
3076 | ||
6aa8b732 AK |
3077 | kvm_init_debug(); |
3078 | ||
043405e1 | 3079 | kvm_arch_init(); |
bf591b24 | 3080 | |
cea7bb21 | 3081 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
d77c26fc MD |
3082 | |
3083 | if (bad_page == NULL) { | |
6aa8b732 AK |
3084 | r = -ENOMEM; |
3085 | goto out; | |
3086 | } | |
3087 | ||
58e690e6 | 3088 | return 0; |
6aa8b732 AK |
3089 | |
3090 | out: | |
3091 | kvm_exit_debug(); | |
b5a33a75 AK |
3092 | kvm_mmu_module_exit(); |
3093 | out4: | |
6aa8b732 AK |
3094 | return r; |
3095 | } | |
3096 | ||
3097 | static __exit void kvm_exit(void) | |
3098 | { | |
3099 | kvm_exit_debug(); | |
cea7bb21 | 3100 | __free_page(bad_page); |
b5a33a75 | 3101 | kvm_mmu_module_exit(); |
6aa8b732 AK |
3102 | } |
3103 | ||
3104 | module_init(kvm_init) | |
3105 | module_exit(kvm_exit) |