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" | |
19 | ||
20 | #include <linux/kvm.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/errno.h> | |
23 | #include <asm/processor.h> | |
24 | #include <linux/percpu.h> | |
25 | #include <linux/gfp.h> | |
26 | #include <asm/msr.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/miscdevice.h> | |
29 | #include <linux/vmalloc.h> | |
30 | #include <asm/uaccess.h> | |
31 | #include <linux/reboot.h> | |
32 | #include <asm/io.h> | |
33 | #include <linux/debugfs.h> | |
34 | #include <linux/highmem.h> | |
35 | #include <linux/file.h> | |
36 | #include <asm/desc.h> | |
37 | ||
38 | #include "x86_emulate.h" | |
39 | #include "segment_descriptor.h" | |
40 | ||
41 | MODULE_AUTHOR("Qumranet"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
44 | struct kvm_arch_ops *kvm_arch_ops; | |
45 | struct kvm_stat kvm_stat; | |
46 | EXPORT_SYMBOL_GPL(kvm_stat); | |
47 | ||
48 | static struct kvm_stats_debugfs_item { | |
49 | const char *name; | |
50 | u32 *data; | |
51 | struct dentry *dentry; | |
52 | } debugfs_entries[] = { | |
53 | { "pf_fixed", &kvm_stat.pf_fixed }, | |
54 | { "pf_guest", &kvm_stat.pf_guest }, | |
55 | { "tlb_flush", &kvm_stat.tlb_flush }, | |
56 | { "invlpg", &kvm_stat.invlpg }, | |
57 | { "exits", &kvm_stat.exits }, | |
58 | { "io_exits", &kvm_stat.io_exits }, | |
59 | { "mmio_exits", &kvm_stat.mmio_exits }, | |
60 | { "signal_exits", &kvm_stat.signal_exits }, | |
61 | { "irq_exits", &kvm_stat.irq_exits }, | |
62 | { 0, 0 } | |
63 | }; | |
64 | ||
65 | static struct dentry *debugfs_dir; | |
66 | ||
67 | #define MAX_IO_MSRS 256 | |
68 | ||
69 | #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL | |
70 | #define LMSW_GUEST_MASK 0x0eULL | |
71 | #define CR4_RESEVED_BITS (~((1ULL << 11) - 1)) | |
72 | #define CR8_RESEVED_BITS (~0x0fULL) | |
73 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe | |
74 | ||
05b3e0c2 | 75 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
76 | // LDT or TSS descriptor in the GDT. 16 bytes. |
77 | struct segment_descriptor_64 { | |
78 | struct segment_descriptor s; | |
79 | u32 base_higher; | |
80 | u32 pad_zero; | |
81 | }; | |
82 | ||
83 | #endif | |
84 | ||
85 | unsigned long segment_base(u16 selector) | |
86 | { | |
87 | struct descriptor_table gdt; | |
88 | struct segment_descriptor *d; | |
89 | unsigned long table_base; | |
90 | typedef unsigned long ul; | |
91 | unsigned long v; | |
92 | ||
93 | if (selector == 0) | |
94 | return 0; | |
95 | ||
96 | asm ("sgdt %0" : "=m"(gdt)); | |
97 | table_base = gdt.base; | |
98 | ||
99 | if (selector & 4) { /* from ldt */ | |
100 | u16 ldt_selector; | |
101 | ||
102 | asm ("sldt %0" : "=g"(ldt_selector)); | |
103 | table_base = segment_base(ldt_selector); | |
104 | } | |
105 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); | |
106 | v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24); | |
05b3e0c2 | 107 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
108 | if (d->system == 0 |
109 | && (d->type == 2 || d->type == 9 || d->type == 11)) | |
110 | v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32; | |
111 | #endif | |
112 | return v; | |
113 | } | |
114 | EXPORT_SYMBOL_GPL(segment_base); | |
115 | ||
116 | int kvm_read_guest(struct kvm_vcpu *vcpu, | |
117 | gva_t addr, | |
118 | unsigned long size, | |
119 | void *dest) | |
120 | { | |
121 | unsigned char *host_buf = dest; | |
122 | unsigned long req_size = size; | |
123 | ||
124 | while (size) { | |
125 | hpa_t paddr; | |
126 | unsigned now; | |
127 | unsigned offset; | |
128 | hva_t guest_buf; | |
129 | ||
130 | paddr = gva_to_hpa(vcpu, addr); | |
131 | ||
132 | if (is_error_hpa(paddr)) | |
133 | break; | |
134 | ||
135 | guest_buf = (hva_t)kmap_atomic( | |
136 | pfn_to_page(paddr >> PAGE_SHIFT), | |
137 | KM_USER0); | |
138 | offset = addr & ~PAGE_MASK; | |
139 | guest_buf |= offset; | |
140 | now = min(size, PAGE_SIZE - offset); | |
141 | memcpy(host_buf, (void*)guest_buf, now); | |
142 | host_buf += now; | |
143 | addr += now; | |
144 | size -= now; | |
145 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); | |
146 | } | |
147 | return req_size - size; | |
148 | } | |
149 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
150 | ||
151 | int kvm_write_guest(struct kvm_vcpu *vcpu, | |
152 | gva_t addr, | |
153 | unsigned long size, | |
154 | void *data) | |
155 | { | |
156 | unsigned char *host_buf = data; | |
157 | unsigned long req_size = size; | |
158 | ||
159 | while (size) { | |
160 | hpa_t paddr; | |
161 | unsigned now; | |
162 | unsigned offset; | |
163 | hva_t guest_buf; | |
164 | ||
165 | paddr = gva_to_hpa(vcpu, addr); | |
166 | ||
167 | if (is_error_hpa(paddr)) | |
168 | break; | |
169 | ||
170 | guest_buf = (hva_t)kmap_atomic( | |
171 | pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0); | |
172 | offset = addr & ~PAGE_MASK; | |
173 | guest_buf |= offset; | |
174 | now = min(size, PAGE_SIZE - offset); | |
175 | memcpy((void*)guest_buf, host_buf, now); | |
176 | host_buf += now; | |
177 | addr += now; | |
178 | size -= now; | |
179 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); | |
180 | } | |
181 | return req_size - size; | |
182 | } | |
183 | EXPORT_SYMBOL_GPL(kvm_write_guest); | |
184 | ||
185 | static int vcpu_slot(struct kvm_vcpu *vcpu) | |
186 | { | |
187 | return vcpu - vcpu->kvm->vcpus; | |
188 | } | |
189 | ||
190 | /* | |
191 | * Switches to specified vcpu, until a matching vcpu_put() | |
192 | */ | |
193 | static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot) | |
194 | { | |
195 | struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot]; | |
196 | ||
197 | mutex_lock(&vcpu->mutex); | |
198 | if (unlikely(!vcpu->vmcs)) { | |
199 | mutex_unlock(&vcpu->mutex); | |
200 | return 0; | |
201 | } | |
202 | return kvm_arch_ops->vcpu_load(vcpu); | |
203 | } | |
204 | ||
205 | static void vcpu_put(struct kvm_vcpu *vcpu) | |
206 | { | |
207 | kvm_arch_ops->vcpu_put(vcpu); | |
6aa8b732 AK |
208 | mutex_unlock(&vcpu->mutex); |
209 | } | |
210 | ||
211 | static int kvm_dev_open(struct inode *inode, struct file *filp) | |
212 | { | |
213 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
214 | int i; | |
215 | ||
216 | if (!kvm) | |
217 | return -ENOMEM; | |
218 | ||
219 | spin_lock_init(&kvm->lock); | |
220 | INIT_LIST_HEAD(&kvm->active_mmu_pages); | |
221 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
222 | struct kvm_vcpu *vcpu = &kvm->vcpus[i]; | |
223 | ||
224 | mutex_init(&vcpu->mutex); | |
225 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
226 | INIT_LIST_HEAD(&vcpu->free_pages); | |
227 | } | |
228 | filp->private_data = kvm; | |
229 | return 0; | |
230 | } | |
231 | ||
232 | /* | |
233 | * Free any memory in @free but not in @dont. | |
234 | */ | |
235 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
236 | struct kvm_memory_slot *dont) | |
237 | { | |
238 | int i; | |
239 | ||
240 | if (!dont || free->phys_mem != dont->phys_mem) | |
241 | if (free->phys_mem) { | |
242 | for (i = 0; i < free->npages; ++i) | |
243 | __free_page(free->phys_mem[i]); | |
244 | vfree(free->phys_mem); | |
245 | } | |
246 | ||
247 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
248 | vfree(free->dirty_bitmap); | |
249 | ||
250 | free->phys_mem = 0; | |
251 | free->npages = 0; | |
252 | free->dirty_bitmap = 0; | |
253 | } | |
254 | ||
255 | static void kvm_free_physmem(struct kvm *kvm) | |
256 | { | |
257 | int i; | |
258 | ||
259 | for (i = 0; i < kvm->nmemslots; ++i) | |
260 | kvm_free_physmem_slot(&kvm->memslots[i], 0); | |
261 | } | |
262 | ||
263 | static void kvm_free_vcpu(struct kvm_vcpu *vcpu) | |
264 | { | |
265 | kvm_arch_ops->vcpu_free(vcpu); | |
266 | kvm_mmu_destroy(vcpu); | |
267 | } | |
268 | ||
269 | static void kvm_free_vcpus(struct kvm *kvm) | |
270 | { | |
271 | unsigned int i; | |
272 | ||
273 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
274 | kvm_free_vcpu(&kvm->vcpus[i]); | |
275 | } | |
276 | ||
277 | static int kvm_dev_release(struct inode *inode, struct file *filp) | |
278 | { | |
279 | struct kvm *kvm = filp->private_data; | |
280 | ||
281 | kvm_free_vcpus(kvm); | |
282 | kvm_free_physmem(kvm); | |
283 | kfree(kvm); | |
284 | return 0; | |
285 | } | |
286 | ||
287 | static void inject_gp(struct kvm_vcpu *vcpu) | |
288 | { | |
289 | kvm_arch_ops->inject_gp(vcpu, 0); | |
290 | } | |
291 | ||
292 | static int pdptrs_have_reserved_bits_set(struct kvm_vcpu *vcpu, | |
293 | unsigned long cr3) | |
294 | { | |
295 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
296 | unsigned offset = (cr3 & (PAGE_SIZE-1)) >> 5; | |
297 | int i; | |
298 | u64 pdpte; | |
299 | u64 *pdpt; | |
300 | struct kvm_memory_slot *memslot; | |
301 | ||
302 | spin_lock(&vcpu->kvm->lock); | |
303 | memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn); | |
304 | /* FIXME: !memslot - emulate? 0xff? */ | |
305 | pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0); | |
306 | ||
307 | for (i = 0; i < 4; ++i) { | |
308 | pdpte = pdpt[offset + i]; | |
309 | if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) | |
310 | break; | |
311 | } | |
312 | ||
313 | kunmap_atomic(pdpt, KM_USER0); | |
314 | spin_unlock(&vcpu->kvm->lock); | |
315 | ||
316 | return i != 4; | |
317 | } | |
318 | ||
319 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) | |
320 | { | |
321 | if (cr0 & CR0_RESEVED_BITS) { | |
322 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", | |
323 | cr0, vcpu->cr0); | |
324 | inject_gp(vcpu); | |
325 | return; | |
326 | } | |
327 | ||
328 | if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) { | |
329 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); | |
330 | inject_gp(vcpu); | |
331 | return; | |
332 | } | |
333 | ||
334 | if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) { | |
335 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " | |
336 | "and a clear PE flag\n"); | |
337 | inject_gp(vcpu); | |
338 | return; | |
339 | } | |
340 | ||
341 | if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) { | |
05b3e0c2 | 342 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
343 | if ((vcpu->shadow_efer & EFER_LME)) { |
344 | int cs_db, cs_l; | |
345 | ||
346 | if (!is_pae(vcpu)) { | |
347 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
348 | "in long mode while PAE is disabled\n"); | |
349 | inject_gp(vcpu); | |
350 | return; | |
351 | } | |
352 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
353 | if (cs_l) { | |
354 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
355 | "in long mode while CS.L == 1\n"); | |
356 | inject_gp(vcpu); | |
357 | return; | |
358 | ||
359 | } | |
360 | } else | |
361 | #endif | |
362 | if (is_pae(vcpu) && | |
363 | pdptrs_have_reserved_bits_set(vcpu, vcpu->cr3)) { | |
364 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " | |
365 | "reserved bits\n"); | |
366 | inject_gp(vcpu); | |
367 | return; | |
368 | } | |
369 | ||
370 | } | |
371 | ||
372 | kvm_arch_ops->set_cr0(vcpu, cr0); | |
373 | vcpu->cr0 = cr0; | |
374 | ||
375 | spin_lock(&vcpu->kvm->lock); | |
376 | kvm_mmu_reset_context(vcpu); | |
377 | spin_unlock(&vcpu->kvm->lock); | |
378 | return; | |
379 | } | |
380 | EXPORT_SYMBOL_GPL(set_cr0); | |
381 | ||
382 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) | |
383 | { | |
384 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); | |
385 | } | |
386 | EXPORT_SYMBOL_GPL(lmsw); | |
387 | ||
388 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
389 | { | |
390 | if (cr4 & CR4_RESEVED_BITS) { | |
391 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); | |
392 | inject_gp(vcpu); | |
393 | return; | |
394 | } | |
395 | ||
396 | if (kvm_arch_ops->is_long_mode(vcpu)) { | |
397 | if (!(cr4 & CR4_PAE_MASK)) { | |
398 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " | |
399 | "in long mode\n"); | |
400 | inject_gp(vcpu); | |
401 | return; | |
402 | } | |
403 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK) | |
404 | && pdptrs_have_reserved_bits_set(vcpu, vcpu->cr3)) { | |
405 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); | |
406 | inject_gp(vcpu); | |
407 | } | |
408 | ||
409 | if (cr4 & CR4_VMXE_MASK) { | |
410 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); | |
411 | inject_gp(vcpu); | |
412 | return; | |
413 | } | |
414 | kvm_arch_ops->set_cr4(vcpu, cr4); | |
415 | spin_lock(&vcpu->kvm->lock); | |
416 | kvm_mmu_reset_context(vcpu); | |
417 | spin_unlock(&vcpu->kvm->lock); | |
418 | } | |
419 | EXPORT_SYMBOL_GPL(set_cr4); | |
420 | ||
421 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | |
422 | { | |
423 | if (kvm_arch_ops->is_long_mode(vcpu)) { | |
424 | if ( cr3 & CR3_L_MODE_RESEVED_BITS) { | |
425 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
426 | inject_gp(vcpu); | |
427 | return; | |
428 | } | |
429 | } else { | |
430 | if (cr3 & CR3_RESEVED_BITS) { | |
431 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
432 | inject_gp(vcpu); | |
433 | return; | |
434 | } | |
435 | if (is_paging(vcpu) && is_pae(vcpu) && | |
436 | pdptrs_have_reserved_bits_set(vcpu, cr3)) { | |
437 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " | |
438 | "reserved bits\n"); | |
439 | inject_gp(vcpu); | |
440 | return; | |
441 | } | |
442 | } | |
443 | ||
444 | vcpu->cr3 = cr3; | |
445 | spin_lock(&vcpu->kvm->lock); | |
446 | vcpu->mmu.new_cr3(vcpu); | |
447 | spin_unlock(&vcpu->kvm->lock); | |
448 | } | |
449 | EXPORT_SYMBOL_GPL(set_cr3); | |
450 | ||
451 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) | |
452 | { | |
453 | if ( cr8 & CR8_RESEVED_BITS) { | |
454 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); | |
455 | inject_gp(vcpu); | |
456 | return; | |
457 | } | |
458 | vcpu->cr8 = cr8; | |
459 | } | |
460 | EXPORT_SYMBOL_GPL(set_cr8); | |
461 | ||
462 | void fx_init(struct kvm_vcpu *vcpu) | |
463 | { | |
464 | struct __attribute__ ((__packed__)) fx_image_s { | |
465 | u16 control; //fcw | |
466 | u16 status; //fsw | |
467 | u16 tag; // ftw | |
468 | u16 opcode; //fop | |
469 | u64 ip; // fpu ip | |
470 | u64 operand;// fpu dp | |
471 | u32 mxcsr; | |
472 | u32 mxcsr_mask; | |
473 | ||
474 | } *fx_image; | |
475 | ||
476 | fx_save(vcpu->host_fx_image); | |
477 | fpu_init(); | |
478 | fx_save(vcpu->guest_fx_image); | |
479 | fx_restore(vcpu->host_fx_image); | |
480 | ||
481 | fx_image = (struct fx_image_s *)vcpu->guest_fx_image; | |
482 | fx_image->mxcsr = 0x1f80; | |
483 | memset(vcpu->guest_fx_image + sizeof(struct fx_image_s), | |
484 | 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s)); | |
485 | } | |
486 | EXPORT_SYMBOL_GPL(fx_init); | |
487 | ||
488 | /* | |
489 | * Creates some virtual cpus. Good luck creating more than one. | |
490 | */ | |
491 | static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n) | |
492 | { | |
493 | int r; | |
494 | struct kvm_vcpu *vcpu; | |
495 | ||
496 | r = -EINVAL; | |
497 | if (n < 0 || n >= KVM_MAX_VCPUS) | |
498 | goto out; | |
499 | ||
500 | vcpu = &kvm->vcpus[n]; | |
501 | ||
502 | mutex_lock(&vcpu->mutex); | |
503 | ||
504 | if (vcpu->vmcs) { | |
505 | mutex_unlock(&vcpu->mutex); | |
506 | return -EEXIST; | |
507 | } | |
508 | ||
509 | vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf, | |
510 | FX_IMAGE_ALIGN); | |
511 | vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE; | |
512 | ||
513 | vcpu->cpu = -1; /* First load will set up TR */ | |
514 | vcpu->kvm = kvm; | |
515 | r = kvm_arch_ops->vcpu_create(vcpu); | |
516 | if (r < 0) | |
517 | goto out_free_vcpus; | |
518 | ||
519 | kvm_arch_ops->vcpu_load(vcpu); | |
520 | ||
521 | r = kvm_arch_ops->vcpu_setup(vcpu); | |
522 | if (r >= 0) | |
523 | r = kvm_mmu_init(vcpu); | |
524 | ||
525 | vcpu_put(vcpu); | |
526 | ||
527 | if (r < 0) | |
528 | goto out_free_vcpus; | |
529 | ||
530 | return 0; | |
531 | ||
532 | out_free_vcpus: | |
533 | kvm_free_vcpu(vcpu); | |
534 | mutex_unlock(&vcpu->mutex); | |
535 | out: | |
536 | return r; | |
537 | } | |
538 | ||
539 | /* | |
540 | * Allocate some memory and give it an address in the guest physical address | |
541 | * space. | |
542 | * | |
543 | * Discontiguous memory is allowed, mostly for framebuffers. | |
544 | */ | |
545 | static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm, | |
546 | struct kvm_memory_region *mem) | |
547 | { | |
548 | int r; | |
549 | gfn_t base_gfn; | |
550 | unsigned long npages; | |
551 | unsigned long i; | |
552 | struct kvm_memory_slot *memslot; | |
553 | struct kvm_memory_slot old, new; | |
554 | int memory_config_version; | |
555 | ||
556 | r = -EINVAL; | |
557 | /* General sanity checks */ | |
558 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
559 | goto out; | |
560 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
561 | goto out; | |
562 | if (mem->slot >= KVM_MEMORY_SLOTS) | |
563 | goto out; | |
564 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
565 | goto out; | |
566 | ||
567 | memslot = &kvm->memslots[mem->slot]; | |
568 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
569 | npages = mem->memory_size >> PAGE_SHIFT; | |
570 | ||
571 | if (!npages) | |
572 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
573 | ||
574 | raced: | |
575 | spin_lock(&kvm->lock); | |
576 | ||
577 | memory_config_version = kvm->memory_config_version; | |
578 | new = old = *memslot; | |
579 | ||
580 | new.base_gfn = base_gfn; | |
581 | new.npages = npages; | |
582 | new.flags = mem->flags; | |
583 | ||
584 | /* Disallow changing a memory slot's size. */ | |
585 | r = -EINVAL; | |
586 | if (npages && old.npages && npages != old.npages) | |
587 | goto out_unlock; | |
588 | ||
589 | /* Check for overlaps */ | |
590 | r = -EEXIST; | |
591 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
592 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
593 | ||
594 | if (s == memslot) | |
595 | continue; | |
596 | if (!((base_gfn + npages <= s->base_gfn) || | |
597 | (base_gfn >= s->base_gfn + s->npages))) | |
598 | goto out_unlock; | |
599 | } | |
600 | /* | |
601 | * Do memory allocations outside lock. memory_config_version will | |
602 | * detect any races. | |
603 | */ | |
604 | spin_unlock(&kvm->lock); | |
605 | ||
606 | /* Deallocate if slot is being removed */ | |
607 | if (!npages) | |
608 | new.phys_mem = 0; | |
609 | ||
610 | /* Free page dirty bitmap if unneeded */ | |
611 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
612 | new.dirty_bitmap = 0; | |
613 | ||
614 | r = -ENOMEM; | |
615 | ||
616 | /* Allocate if a slot is being created */ | |
617 | if (npages && !new.phys_mem) { | |
618 | new.phys_mem = vmalloc(npages * sizeof(struct page *)); | |
619 | ||
620 | if (!new.phys_mem) | |
621 | goto out_free; | |
622 | ||
623 | memset(new.phys_mem, 0, npages * sizeof(struct page *)); | |
624 | for (i = 0; i < npages; ++i) { | |
625 | new.phys_mem[i] = alloc_page(GFP_HIGHUSER | |
626 | | __GFP_ZERO); | |
627 | if (!new.phys_mem[i]) | |
628 | goto out_free; | |
629 | } | |
630 | } | |
631 | ||
632 | /* Allocate page dirty bitmap if needed */ | |
633 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
634 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
635 | ||
636 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
637 | if (!new.dirty_bitmap) | |
638 | goto out_free; | |
639 | memset(new.dirty_bitmap, 0, dirty_bytes); | |
640 | } | |
641 | ||
642 | spin_lock(&kvm->lock); | |
643 | ||
644 | if (memory_config_version != kvm->memory_config_version) { | |
645 | spin_unlock(&kvm->lock); | |
646 | kvm_free_physmem_slot(&new, &old); | |
647 | goto raced; | |
648 | } | |
649 | ||
650 | r = -EAGAIN; | |
651 | if (kvm->busy) | |
652 | goto out_unlock; | |
653 | ||
654 | if (mem->slot >= kvm->nmemslots) | |
655 | kvm->nmemslots = mem->slot + 1; | |
656 | ||
657 | *memslot = new; | |
658 | ++kvm->memory_config_version; | |
659 | ||
660 | spin_unlock(&kvm->lock); | |
661 | ||
662 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
663 | struct kvm_vcpu *vcpu; | |
664 | ||
665 | vcpu = vcpu_load(kvm, i); | |
666 | if (!vcpu) | |
667 | continue; | |
668 | kvm_mmu_reset_context(vcpu); | |
669 | vcpu_put(vcpu); | |
670 | } | |
671 | ||
672 | kvm_free_physmem_slot(&old, &new); | |
673 | return 0; | |
674 | ||
675 | out_unlock: | |
676 | spin_unlock(&kvm->lock); | |
677 | out_free: | |
678 | kvm_free_physmem_slot(&new, &old); | |
679 | out: | |
680 | return r; | |
681 | } | |
682 | ||
683 | /* | |
684 | * Get (and clear) the dirty memory log for a memory slot. | |
685 | */ | |
686 | static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm, | |
687 | struct kvm_dirty_log *log) | |
688 | { | |
689 | struct kvm_memory_slot *memslot; | |
690 | int r, i; | |
691 | int n; | |
692 | unsigned long any = 0; | |
693 | ||
694 | spin_lock(&kvm->lock); | |
695 | ||
696 | /* | |
697 | * Prevent changes to guest memory configuration even while the lock | |
698 | * is not taken. | |
699 | */ | |
700 | ++kvm->busy; | |
701 | spin_unlock(&kvm->lock); | |
702 | r = -EINVAL; | |
703 | if (log->slot >= KVM_MEMORY_SLOTS) | |
704 | goto out; | |
705 | ||
706 | memslot = &kvm->memslots[log->slot]; | |
707 | r = -ENOENT; | |
708 | if (!memslot->dirty_bitmap) | |
709 | goto out; | |
710 | ||
711 | n = ALIGN(memslot->npages, 8) / 8; | |
712 | ||
713 | for (i = 0; !any && i < n; ++i) | |
714 | any = memslot->dirty_bitmap[i]; | |
715 | ||
716 | r = -EFAULT; | |
717 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
718 | goto out; | |
719 | ||
720 | ||
721 | if (any) { | |
722 | spin_lock(&kvm->lock); | |
723 | kvm_mmu_slot_remove_write_access(kvm, log->slot); | |
724 | spin_unlock(&kvm->lock); | |
725 | memset(memslot->dirty_bitmap, 0, n); | |
726 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
727 | struct kvm_vcpu *vcpu = vcpu_load(kvm, i); | |
728 | ||
729 | if (!vcpu) | |
730 | continue; | |
731 | kvm_arch_ops->tlb_flush(vcpu); | |
732 | vcpu_put(vcpu); | |
733 | } | |
734 | } | |
735 | ||
736 | r = 0; | |
737 | ||
738 | out: | |
739 | spin_lock(&kvm->lock); | |
740 | --kvm->busy; | |
741 | spin_unlock(&kvm->lock); | |
742 | return r; | |
743 | } | |
744 | ||
745 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
746 | { | |
747 | int i; | |
748 | ||
749 | for (i = 0; i < kvm->nmemslots; ++i) { | |
750 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
751 | ||
752 | if (gfn >= memslot->base_gfn | |
753 | && gfn < memslot->base_gfn + memslot->npages) | |
754 | return memslot; | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | EXPORT_SYMBOL_GPL(gfn_to_memslot); | |
759 | ||
760 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) | |
761 | { | |
762 | int i; | |
763 | struct kvm_memory_slot *memslot = 0; | |
764 | unsigned long rel_gfn; | |
765 | ||
766 | for (i = 0; i < kvm->nmemslots; ++i) { | |
767 | memslot = &kvm->memslots[i]; | |
768 | ||
769 | if (gfn >= memslot->base_gfn | |
770 | && gfn < memslot->base_gfn + memslot->npages) { | |
771 | ||
772 | if (!memslot || !memslot->dirty_bitmap) | |
773 | return; | |
774 | ||
775 | rel_gfn = gfn - memslot->base_gfn; | |
776 | ||
777 | /* avoid RMW */ | |
778 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
779 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
780 | return; | |
781 | } | |
782 | } | |
783 | } | |
784 | ||
785 | static int emulator_read_std(unsigned long addr, | |
786 | unsigned long *val, | |
787 | unsigned int bytes, | |
788 | struct x86_emulate_ctxt *ctxt) | |
789 | { | |
790 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
791 | void *data = val; | |
792 | ||
793 | while (bytes) { | |
794 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
795 | unsigned offset = addr & (PAGE_SIZE-1); | |
796 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
797 | unsigned long pfn; | |
798 | struct kvm_memory_slot *memslot; | |
799 | void *page; | |
800 | ||
801 | if (gpa == UNMAPPED_GVA) | |
802 | return X86EMUL_PROPAGATE_FAULT; | |
803 | pfn = gpa >> PAGE_SHIFT; | |
804 | memslot = gfn_to_memslot(vcpu->kvm, pfn); | |
805 | if (!memslot) | |
806 | return X86EMUL_UNHANDLEABLE; | |
807 | page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0); | |
808 | ||
809 | memcpy(data, page + offset, tocopy); | |
810 | ||
811 | kunmap_atomic(page, KM_USER0); | |
812 | ||
813 | bytes -= tocopy; | |
814 | data += tocopy; | |
815 | addr += tocopy; | |
816 | } | |
817 | ||
818 | return X86EMUL_CONTINUE; | |
819 | } | |
820 | ||
821 | static int emulator_write_std(unsigned long addr, | |
822 | unsigned long val, | |
823 | unsigned int bytes, | |
824 | struct x86_emulate_ctxt *ctxt) | |
825 | { | |
826 | printk(KERN_ERR "emulator_write_std: addr %lx n %d\n", | |
827 | addr, bytes); | |
828 | return X86EMUL_UNHANDLEABLE; | |
829 | } | |
830 | ||
831 | static int emulator_read_emulated(unsigned long addr, | |
832 | unsigned long *val, | |
833 | unsigned int bytes, | |
834 | struct x86_emulate_ctxt *ctxt) | |
835 | { | |
836 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
837 | ||
838 | if (vcpu->mmio_read_completed) { | |
839 | memcpy(val, vcpu->mmio_data, bytes); | |
840 | vcpu->mmio_read_completed = 0; | |
841 | return X86EMUL_CONTINUE; | |
842 | } else if (emulator_read_std(addr, val, bytes, ctxt) | |
843 | == X86EMUL_CONTINUE) | |
844 | return X86EMUL_CONTINUE; | |
845 | else { | |
846 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
847 | if (gpa == UNMAPPED_GVA) | |
848 | return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT; | |
849 | vcpu->mmio_needed = 1; | |
850 | vcpu->mmio_phys_addr = gpa; | |
851 | vcpu->mmio_size = bytes; | |
852 | vcpu->mmio_is_write = 0; | |
853 | ||
854 | return X86EMUL_UNHANDLEABLE; | |
855 | } | |
856 | } | |
857 | ||
858 | static int emulator_write_emulated(unsigned long addr, | |
859 | unsigned long val, | |
860 | unsigned int bytes, | |
861 | struct x86_emulate_ctxt *ctxt) | |
862 | { | |
863 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
864 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
865 | ||
866 | if (gpa == UNMAPPED_GVA) | |
867 | return X86EMUL_PROPAGATE_FAULT; | |
868 | ||
869 | vcpu->mmio_needed = 1; | |
870 | vcpu->mmio_phys_addr = gpa; | |
871 | vcpu->mmio_size = bytes; | |
872 | vcpu->mmio_is_write = 1; | |
873 | memcpy(vcpu->mmio_data, &val, bytes); | |
874 | ||
875 | return X86EMUL_CONTINUE; | |
876 | } | |
877 | ||
878 | static int emulator_cmpxchg_emulated(unsigned long addr, | |
879 | unsigned long old, | |
880 | unsigned long new, | |
881 | unsigned int bytes, | |
882 | struct x86_emulate_ctxt *ctxt) | |
883 | { | |
884 | static int reported; | |
885 | ||
886 | if (!reported) { | |
887 | reported = 1; | |
888 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
889 | } | |
890 | return emulator_write_emulated(addr, new, bytes, ctxt); | |
891 | } | |
892 | ||
893 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
894 | { | |
895 | return kvm_arch_ops->get_segment_base(vcpu, seg); | |
896 | } | |
897 | ||
898 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
899 | { | |
900 | spin_lock(&vcpu->kvm->lock); | |
901 | vcpu->mmu.inval_page(vcpu, address); | |
902 | spin_unlock(&vcpu->kvm->lock); | |
903 | kvm_arch_ops->invlpg(vcpu, address); | |
904 | return X86EMUL_CONTINUE; | |
905 | } | |
906 | ||
907 | int emulate_clts(struct kvm_vcpu *vcpu) | |
908 | { | |
909 | unsigned long cr0 = vcpu->cr0; | |
910 | ||
911 | cr0 &= ~CR0_TS_MASK; | |
912 | kvm_arch_ops->set_cr0(vcpu, cr0); | |
913 | return X86EMUL_CONTINUE; | |
914 | } | |
915 | ||
916 | int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest) | |
917 | { | |
918 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
919 | ||
920 | switch (dr) { | |
921 | case 0 ... 3: | |
922 | *dest = kvm_arch_ops->get_dr(vcpu, dr); | |
923 | return X86EMUL_CONTINUE; | |
924 | default: | |
925 | printk(KERN_DEBUG "%s: unexpected dr %u\n", | |
926 | __FUNCTION__, dr); | |
927 | return X86EMUL_UNHANDLEABLE; | |
928 | } | |
929 | } | |
930 | ||
931 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
932 | { | |
933 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
934 | int exception; | |
935 | ||
936 | kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); | |
937 | if (exception) { | |
938 | /* FIXME: better handling */ | |
939 | return X86EMUL_UNHANDLEABLE; | |
940 | } | |
941 | return X86EMUL_CONTINUE; | |
942 | } | |
943 | ||
944 | static void report_emulation_failure(struct x86_emulate_ctxt *ctxt) | |
945 | { | |
946 | static int reported; | |
947 | u8 opcodes[4]; | |
948 | unsigned long rip = ctxt->vcpu->rip; | |
949 | unsigned long rip_linear; | |
950 | ||
951 | rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS); | |
952 | ||
953 | if (reported) | |
954 | return; | |
955 | ||
956 | emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt); | |
957 | ||
958 | printk(KERN_ERR "emulation failed but !mmio_needed?" | |
959 | " rip %lx %02x %02x %02x %02x\n", | |
960 | rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
961 | reported = 1; | |
962 | } | |
963 | ||
964 | struct x86_emulate_ops emulate_ops = { | |
965 | .read_std = emulator_read_std, | |
966 | .write_std = emulator_write_std, | |
967 | .read_emulated = emulator_read_emulated, | |
968 | .write_emulated = emulator_write_emulated, | |
969 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
970 | }; | |
971 | ||
972 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
973 | struct kvm_run *run, | |
974 | unsigned long cr2, | |
975 | u16 error_code) | |
976 | { | |
977 | struct x86_emulate_ctxt emulate_ctxt; | |
978 | int r; | |
979 | int cs_db, cs_l; | |
980 | ||
981 | kvm_arch_ops->cache_regs(vcpu); | |
982 | ||
983 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
984 | ||
985 | emulate_ctxt.vcpu = vcpu; | |
986 | emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu); | |
987 | emulate_ctxt.cr2 = cr2; | |
988 | emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM) | |
989 | ? X86EMUL_MODE_REAL : cs_l | |
990 | ? X86EMUL_MODE_PROT64 : cs_db | |
991 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
992 | ||
993 | if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) { | |
994 | emulate_ctxt.cs_base = 0; | |
995 | emulate_ctxt.ds_base = 0; | |
996 | emulate_ctxt.es_base = 0; | |
997 | emulate_ctxt.ss_base = 0; | |
998 | } else { | |
999 | emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS); | |
1000 | emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS); | |
1001 | emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES); | |
1002 | emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS); | |
1003 | } | |
1004 | ||
1005 | emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS); | |
1006 | emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS); | |
1007 | ||
1008 | vcpu->mmio_is_write = 0; | |
1009 | r = x86_emulate_memop(&emulate_ctxt, &emulate_ops); | |
1010 | ||
1011 | if ((r || vcpu->mmio_is_write) && run) { | |
1012 | run->mmio.phys_addr = vcpu->mmio_phys_addr; | |
1013 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
1014 | run->mmio.len = vcpu->mmio_size; | |
1015 | run->mmio.is_write = vcpu->mmio_is_write; | |
1016 | } | |
1017 | ||
1018 | if (r) { | |
1019 | if (!vcpu->mmio_needed) { | |
1020 | report_emulation_failure(&emulate_ctxt); | |
1021 | return EMULATE_FAIL; | |
1022 | } | |
1023 | return EMULATE_DO_MMIO; | |
1024 | } | |
1025 | ||
1026 | kvm_arch_ops->decache_regs(vcpu); | |
1027 | kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags); | |
1028 | ||
1029 | if (vcpu->mmio_is_write) | |
1030 | return EMULATE_DO_MMIO; | |
1031 | ||
1032 | return EMULATE_DONE; | |
1033 | } | |
1034 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
1035 | ||
1036 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | |
1037 | { | |
1038 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
1039 | } | |
1040 | ||
1041 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1042 | { | |
1043 | struct descriptor_table dt = { limit, base }; | |
1044 | ||
1045 | kvm_arch_ops->set_gdt(vcpu, &dt); | |
1046 | } | |
1047 | ||
1048 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1049 | { | |
1050 | struct descriptor_table dt = { limit, base }; | |
1051 | ||
1052 | kvm_arch_ops->set_idt(vcpu, &dt); | |
1053 | } | |
1054 | ||
1055 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
1056 | unsigned long *rflags) | |
1057 | { | |
1058 | lmsw(vcpu, msw); | |
1059 | *rflags = kvm_arch_ops->get_rflags(vcpu); | |
1060 | } | |
1061 | ||
1062 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
1063 | { | |
1064 | switch (cr) { | |
1065 | case 0: | |
1066 | return vcpu->cr0; | |
1067 | case 2: | |
1068 | return vcpu->cr2; | |
1069 | case 3: | |
1070 | return vcpu->cr3; | |
1071 | case 4: | |
1072 | return vcpu->cr4; | |
1073 | default: | |
1074 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1075 | return 0; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
1080 | unsigned long *rflags) | |
1081 | { | |
1082 | switch (cr) { | |
1083 | case 0: | |
1084 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); | |
1085 | *rflags = kvm_arch_ops->get_rflags(vcpu); | |
1086 | break; | |
1087 | case 2: | |
1088 | vcpu->cr2 = val; | |
1089 | break; | |
1090 | case 3: | |
1091 | set_cr3(vcpu, val); | |
1092 | break; | |
1093 | case 4: | |
1094 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); | |
1095 | break; | |
1096 | default: | |
1097 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | /* | |
1102 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
1103 | * Returns 0 on success, non-0 otherwise. | |
1104 | * Assumes vcpu_load() was already called. | |
1105 | */ | |
1106 | static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
1107 | { | |
1108 | return kvm_arch_ops->get_msr(vcpu, msr_index, pdata); | |
1109 | } | |
1110 | ||
05b3e0c2 | 1111 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1112 | |
1113 | void set_efer(struct kvm_vcpu *vcpu, u64 efer) | |
1114 | { | |
6aa8b732 AK |
1115 | if (efer & EFER_RESERVED_BITS) { |
1116 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", | |
1117 | efer); | |
1118 | inject_gp(vcpu); | |
1119 | return; | |
1120 | } | |
1121 | ||
1122 | if (is_paging(vcpu) | |
1123 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { | |
1124 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); | |
1125 | inject_gp(vcpu); | |
1126 | return; | |
1127 | } | |
1128 | ||
7725f0ba AK |
1129 | kvm_arch_ops->set_efer(vcpu, efer); |
1130 | ||
6aa8b732 AK |
1131 | efer &= ~EFER_LMA; |
1132 | efer |= vcpu->shadow_efer & EFER_LMA; | |
1133 | ||
1134 | vcpu->shadow_efer = efer; | |
6aa8b732 AK |
1135 | } |
1136 | EXPORT_SYMBOL_GPL(set_efer); | |
1137 | ||
1138 | #endif | |
1139 | ||
1140 | /* | |
1141 | * Writes msr value into into the appropriate "register". | |
1142 | * Returns 0 on success, non-0 otherwise. | |
1143 | * Assumes vcpu_load() was already called. | |
1144 | */ | |
1145 | static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
1146 | { | |
1147 | return kvm_arch_ops->set_msr(vcpu, msr_index, data); | |
1148 | } | |
1149 | ||
1150 | void kvm_resched(struct kvm_vcpu *vcpu) | |
1151 | { | |
1152 | vcpu_put(vcpu); | |
1153 | cond_resched(); | |
1154 | /* Cannot fail - no vcpu unplug yet. */ | |
1155 | vcpu_load(vcpu->kvm, vcpu_slot(vcpu)); | |
1156 | } | |
1157 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1158 | ||
1159 | void load_msrs(struct vmx_msr_entry *e, int n) | |
1160 | { | |
1161 | int i; | |
1162 | ||
1163 | for (i = 0; i < n; ++i) | |
1164 | wrmsrl(e[i].index, e[i].data); | |
1165 | } | |
1166 | EXPORT_SYMBOL_GPL(load_msrs); | |
1167 | ||
1168 | void save_msrs(struct vmx_msr_entry *e, int n) | |
1169 | { | |
1170 | int i; | |
1171 | ||
1172 | for (i = 0; i < n; ++i) | |
1173 | rdmsrl(e[i].index, e[i].data); | |
1174 | } | |
1175 | EXPORT_SYMBOL_GPL(save_msrs); | |
1176 | ||
1177 | static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) | |
1178 | { | |
1179 | struct kvm_vcpu *vcpu; | |
1180 | int r; | |
1181 | ||
1182 | if (kvm_run->vcpu < 0 || kvm_run->vcpu >= KVM_MAX_VCPUS) | |
1183 | return -EINVAL; | |
1184 | ||
1185 | vcpu = vcpu_load(kvm, kvm_run->vcpu); | |
1186 | if (!vcpu) | |
1187 | return -ENOENT; | |
1188 | ||
1189 | if (kvm_run->emulated) { | |
1190 | kvm_arch_ops->skip_emulated_instruction(vcpu); | |
1191 | kvm_run->emulated = 0; | |
1192 | } | |
1193 | ||
1194 | if (kvm_run->mmio_completed) { | |
1195 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
1196 | vcpu->mmio_read_completed = 1; | |
1197 | } | |
1198 | ||
1199 | vcpu->mmio_needed = 0; | |
1200 | ||
1201 | r = kvm_arch_ops->run(vcpu, kvm_run); | |
1202 | ||
1203 | vcpu_put(vcpu); | |
1204 | return r; | |
1205 | } | |
1206 | ||
1207 | static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) | |
1208 | { | |
1209 | struct kvm_vcpu *vcpu; | |
1210 | ||
1211 | if (regs->vcpu < 0 || regs->vcpu >= KVM_MAX_VCPUS) | |
1212 | return -EINVAL; | |
1213 | ||
1214 | vcpu = vcpu_load(kvm, regs->vcpu); | |
1215 | if (!vcpu) | |
1216 | return -ENOENT; | |
1217 | ||
1218 | kvm_arch_ops->cache_regs(vcpu); | |
1219 | ||
1220 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
1221 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
1222 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
1223 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
1224 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
1225 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
1226 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
1227 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
05b3e0c2 | 1228 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1229 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
1230 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
1231 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
1232 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
1233 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
1234 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
1235 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
1236 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
1237 | #endif | |
1238 | ||
1239 | regs->rip = vcpu->rip; | |
1240 | regs->rflags = kvm_arch_ops->get_rflags(vcpu); | |
1241 | ||
1242 | /* | |
1243 | * Don't leak debug flags in case they were set for guest debugging | |
1244 | */ | |
1245 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
1246 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
1247 | ||
1248 | vcpu_put(vcpu); | |
1249 | ||
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs) | |
1254 | { | |
1255 | struct kvm_vcpu *vcpu; | |
1256 | ||
1257 | if (regs->vcpu < 0 || regs->vcpu >= KVM_MAX_VCPUS) | |
1258 | return -EINVAL; | |
1259 | ||
1260 | vcpu = vcpu_load(kvm, regs->vcpu); | |
1261 | if (!vcpu) | |
1262 | return -ENOENT; | |
1263 | ||
1264 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
1265 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
1266 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
1267 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
1268 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
1269 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
1270 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
1271 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
05b3e0c2 | 1272 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1273 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
1274 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
1275 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
1276 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
1277 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
1278 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
1279 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
1280 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
1281 | #endif | |
1282 | ||
1283 | vcpu->rip = regs->rip; | |
1284 | kvm_arch_ops->set_rflags(vcpu, regs->rflags); | |
1285 | ||
1286 | kvm_arch_ops->decache_regs(vcpu); | |
1287 | ||
1288 | vcpu_put(vcpu); | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | static void get_segment(struct kvm_vcpu *vcpu, | |
1294 | struct kvm_segment *var, int seg) | |
1295 | { | |
1296 | return kvm_arch_ops->get_segment(vcpu, var, seg); | |
1297 | } | |
1298 | ||
1299 | static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs) | |
1300 | { | |
1301 | struct kvm_vcpu *vcpu; | |
1302 | struct descriptor_table dt; | |
1303 | ||
1304 | if (sregs->vcpu < 0 || sregs->vcpu >= KVM_MAX_VCPUS) | |
1305 | return -EINVAL; | |
1306 | vcpu = vcpu_load(kvm, sregs->vcpu); | |
1307 | if (!vcpu) | |
1308 | return -ENOENT; | |
1309 | ||
1310 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
1311 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
1312 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
1313 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
1314 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
1315 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
1316 | ||
1317 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
1318 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
1319 | ||
1320 | kvm_arch_ops->get_idt(vcpu, &dt); | |
1321 | sregs->idt.limit = dt.limit; | |
1322 | sregs->idt.base = dt.base; | |
1323 | kvm_arch_ops->get_gdt(vcpu, &dt); | |
1324 | sregs->gdt.limit = dt.limit; | |
1325 | sregs->gdt.base = dt.base; | |
1326 | ||
1327 | sregs->cr0 = vcpu->cr0; | |
1328 | sregs->cr2 = vcpu->cr2; | |
1329 | sregs->cr3 = vcpu->cr3; | |
1330 | sregs->cr4 = vcpu->cr4; | |
1331 | sregs->cr8 = vcpu->cr8; | |
1332 | sregs->efer = vcpu->shadow_efer; | |
1333 | sregs->apic_base = vcpu->apic_base; | |
1334 | ||
1335 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, | |
1336 | sizeof sregs->interrupt_bitmap); | |
1337 | ||
1338 | vcpu_put(vcpu); | |
1339 | ||
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | static void set_segment(struct kvm_vcpu *vcpu, | |
1344 | struct kvm_segment *var, int seg) | |
1345 | { | |
1346 | return kvm_arch_ops->set_segment(vcpu, var, seg); | |
1347 | } | |
1348 | ||
1349 | static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs) | |
1350 | { | |
1351 | struct kvm_vcpu *vcpu; | |
1352 | int mmu_reset_needed = 0; | |
1353 | int i; | |
1354 | struct descriptor_table dt; | |
1355 | ||
1356 | if (sregs->vcpu < 0 || sregs->vcpu >= KVM_MAX_VCPUS) | |
1357 | return -EINVAL; | |
1358 | vcpu = vcpu_load(kvm, sregs->vcpu); | |
1359 | if (!vcpu) | |
1360 | return -ENOENT; | |
1361 | ||
1362 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
1363 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
1364 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
1365 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
1366 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
1367 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
1368 | ||
1369 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
1370 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
1371 | ||
1372 | dt.limit = sregs->idt.limit; | |
1373 | dt.base = sregs->idt.base; | |
1374 | kvm_arch_ops->set_idt(vcpu, &dt); | |
1375 | dt.limit = sregs->gdt.limit; | |
1376 | dt.base = sregs->gdt.base; | |
1377 | kvm_arch_ops->set_gdt(vcpu, &dt); | |
1378 | ||
1379 | vcpu->cr2 = sregs->cr2; | |
1380 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
1381 | vcpu->cr3 = sregs->cr3; | |
1382 | ||
1383 | vcpu->cr8 = sregs->cr8; | |
1384 | ||
1385 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
05b3e0c2 | 1386 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1387 | kvm_arch_ops->set_efer(vcpu, sregs->efer); |
1388 | #endif | |
1389 | vcpu->apic_base = sregs->apic_base; | |
1390 | ||
1391 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; | |
1392 | kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0); | |
1393 | ||
1394 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
1395 | kvm_arch_ops->set_cr4(vcpu, sregs->cr4); | |
1396 | ||
1397 | if (mmu_reset_needed) | |
1398 | kvm_mmu_reset_context(vcpu); | |
1399 | ||
1400 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
1401 | sizeof vcpu->irq_pending); | |
1402 | vcpu->irq_summary = 0; | |
1403 | for (i = 0; i < NR_IRQ_WORDS; ++i) | |
1404 | if (vcpu->irq_pending[i]) | |
1405 | __set_bit(i, &vcpu->irq_summary); | |
1406 | ||
1407 | vcpu_put(vcpu); | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | /* | |
1413 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
1414 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
1415 | */ | |
1416 | static u32 msrs_to_save[] = { | |
1417 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
1418 | MSR_K6_STAR, | |
05b3e0c2 | 1419 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1420 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
1421 | #endif | |
1422 | MSR_IA32_TIME_STAMP_COUNTER, | |
1423 | }; | |
1424 | ||
1425 | ||
1426 | /* | |
1427 | * Adapt set_msr() to msr_io()'s calling convention | |
1428 | */ | |
1429 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1430 | { | |
1431 | return set_msr(vcpu, index, *data); | |
1432 | } | |
1433 | ||
1434 | /* | |
1435 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
1436 | * | |
1437 | * @return number of msrs set successfully. | |
1438 | */ | |
1439 | static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, | |
1440 | struct kvm_msr_entry *entries, | |
1441 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
1442 | unsigned index, u64 *data)) | |
1443 | { | |
1444 | struct kvm_vcpu *vcpu; | |
1445 | int i; | |
1446 | ||
1447 | if (msrs->vcpu < 0 || msrs->vcpu >= KVM_MAX_VCPUS) | |
1448 | return -EINVAL; | |
1449 | ||
1450 | vcpu = vcpu_load(kvm, msrs->vcpu); | |
1451 | if (!vcpu) | |
1452 | return -ENOENT; | |
1453 | ||
1454 | for (i = 0; i < msrs->nmsrs; ++i) | |
1455 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
1456 | break; | |
1457 | ||
1458 | vcpu_put(vcpu); | |
1459 | ||
1460 | return i; | |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * Read or write a bunch of msrs. Parameters are user addresses. | |
1465 | * | |
1466 | * @return number of msrs set successfully. | |
1467 | */ | |
1468 | static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, | |
1469 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
1470 | unsigned index, u64 *data), | |
1471 | int writeback) | |
1472 | { | |
1473 | struct kvm_msrs msrs; | |
1474 | struct kvm_msr_entry *entries; | |
1475 | int r, n; | |
1476 | unsigned size; | |
1477 | ||
1478 | r = -EFAULT; | |
1479 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
1480 | goto out; | |
1481 | ||
1482 | r = -E2BIG; | |
1483 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
1484 | goto out; | |
1485 | ||
1486 | r = -ENOMEM; | |
1487 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
1488 | entries = vmalloc(size); | |
1489 | if (!entries) | |
1490 | goto out; | |
1491 | ||
1492 | r = -EFAULT; | |
1493 | if (copy_from_user(entries, user_msrs->entries, size)) | |
1494 | goto out_free; | |
1495 | ||
1496 | r = n = __msr_io(kvm, &msrs, entries, do_msr); | |
1497 | if (r < 0) | |
1498 | goto out_free; | |
1499 | ||
1500 | r = -EFAULT; | |
1501 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
1502 | goto out_free; | |
1503 | ||
1504 | r = n; | |
1505 | ||
1506 | out_free: | |
1507 | vfree(entries); | |
1508 | out: | |
1509 | return r; | |
1510 | } | |
1511 | ||
1512 | /* | |
1513 | * Translate a guest virtual address to a guest physical address. | |
1514 | */ | |
1515 | static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr) | |
1516 | { | |
1517 | unsigned long vaddr = tr->linear_address; | |
1518 | struct kvm_vcpu *vcpu; | |
1519 | gpa_t gpa; | |
1520 | ||
1521 | vcpu = vcpu_load(kvm, tr->vcpu); | |
1522 | if (!vcpu) | |
1523 | return -ENOENT; | |
1524 | spin_lock(&kvm->lock); | |
1525 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); | |
1526 | tr->physical_address = gpa; | |
1527 | tr->valid = gpa != UNMAPPED_GVA; | |
1528 | tr->writeable = 1; | |
1529 | tr->usermode = 0; | |
1530 | spin_unlock(&kvm->lock); | |
1531 | vcpu_put(vcpu); | |
1532 | ||
1533 | return 0; | |
1534 | } | |
1535 | ||
1536 | static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) | |
1537 | { | |
1538 | struct kvm_vcpu *vcpu; | |
1539 | ||
1540 | if (irq->vcpu < 0 || irq->vcpu >= KVM_MAX_VCPUS) | |
1541 | return -EINVAL; | |
1542 | if (irq->irq < 0 || irq->irq >= 256) | |
1543 | return -EINVAL; | |
1544 | vcpu = vcpu_load(kvm, irq->vcpu); | |
1545 | if (!vcpu) | |
1546 | return -ENOENT; | |
1547 | ||
1548 | set_bit(irq->irq, vcpu->irq_pending); | |
1549 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
1550 | ||
1551 | vcpu_put(vcpu); | |
1552 | ||
1553 | return 0; | |
1554 | } | |
1555 | ||
1556 | static int kvm_dev_ioctl_debug_guest(struct kvm *kvm, | |
1557 | struct kvm_debug_guest *dbg) | |
1558 | { | |
1559 | struct kvm_vcpu *vcpu; | |
1560 | int r; | |
1561 | ||
1562 | if (dbg->vcpu < 0 || dbg->vcpu >= KVM_MAX_VCPUS) | |
1563 | return -EINVAL; | |
1564 | vcpu = vcpu_load(kvm, dbg->vcpu); | |
1565 | if (!vcpu) | |
1566 | return -ENOENT; | |
1567 | ||
1568 | r = kvm_arch_ops->set_guest_debug(vcpu, dbg); | |
1569 | ||
1570 | vcpu_put(vcpu); | |
1571 | ||
1572 | return r; | |
1573 | } | |
1574 | ||
1575 | static long kvm_dev_ioctl(struct file *filp, | |
1576 | unsigned int ioctl, unsigned long arg) | |
1577 | { | |
1578 | struct kvm *kvm = filp->private_data; | |
1579 | int r = -EINVAL; | |
1580 | ||
1581 | switch (ioctl) { | |
1582 | case KVM_CREATE_VCPU: { | |
1583 | r = kvm_dev_ioctl_create_vcpu(kvm, arg); | |
1584 | if (r) | |
1585 | goto out; | |
1586 | break; | |
1587 | } | |
1588 | case KVM_RUN: { | |
1589 | struct kvm_run kvm_run; | |
1590 | ||
1591 | r = -EFAULT; | |
1592 | if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run)) | |
1593 | goto out; | |
1594 | r = kvm_dev_ioctl_run(kvm, &kvm_run); | |
1595 | if (r < 0) | |
1596 | goto out; | |
1597 | r = -EFAULT; | |
1598 | if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run)) | |
1599 | goto out; | |
1600 | r = 0; | |
1601 | break; | |
1602 | } | |
1603 | case KVM_GET_REGS: { | |
1604 | struct kvm_regs kvm_regs; | |
1605 | ||
1606 | r = -EFAULT; | |
1607 | if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs)) | |
1608 | goto out; | |
1609 | r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs); | |
1610 | if (r) | |
1611 | goto out; | |
1612 | r = -EFAULT; | |
1613 | if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs)) | |
1614 | goto out; | |
1615 | r = 0; | |
1616 | break; | |
1617 | } | |
1618 | case KVM_SET_REGS: { | |
1619 | struct kvm_regs kvm_regs; | |
1620 | ||
1621 | r = -EFAULT; | |
1622 | if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs)) | |
1623 | goto out; | |
1624 | r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs); | |
1625 | if (r) | |
1626 | goto out; | |
1627 | r = 0; | |
1628 | break; | |
1629 | } | |
1630 | case KVM_GET_SREGS: { | |
1631 | struct kvm_sregs kvm_sregs; | |
1632 | ||
1633 | r = -EFAULT; | |
1634 | if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs)) | |
1635 | goto out; | |
1636 | r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs); | |
1637 | if (r) | |
1638 | goto out; | |
1639 | r = -EFAULT; | |
1640 | if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs)) | |
1641 | goto out; | |
1642 | r = 0; | |
1643 | break; | |
1644 | } | |
1645 | case KVM_SET_SREGS: { | |
1646 | struct kvm_sregs kvm_sregs; | |
1647 | ||
1648 | r = -EFAULT; | |
1649 | if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs)) | |
1650 | goto out; | |
1651 | r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs); | |
1652 | if (r) | |
1653 | goto out; | |
1654 | r = 0; | |
1655 | break; | |
1656 | } | |
1657 | case KVM_TRANSLATE: { | |
1658 | struct kvm_translation tr; | |
1659 | ||
1660 | r = -EFAULT; | |
1661 | if (copy_from_user(&tr, (void *)arg, sizeof tr)) | |
1662 | goto out; | |
1663 | r = kvm_dev_ioctl_translate(kvm, &tr); | |
1664 | if (r) | |
1665 | goto out; | |
1666 | r = -EFAULT; | |
1667 | if (copy_to_user((void *)arg, &tr, sizeof tr)) | |
1668 | goto out; | |
1669 | r = 0; | |
1670 | break; | |
1671 | } | |
1672 | case KVM_INTERRUPT: { | |
1673 | struct kvm_interrupt irq; | |
1674 | ||
1675 | r = -EFAULT; | |
1676 | if (copy_from_user(&irq, (void *)arg, sizeof irq)) | |
1677 | goto out; | |
1678 | r = kvm_dev_ioctl_interrupt(kvm, &irq); | |
1679 | if (r) | |
1680 | goto out; | |
1681 | r = 0; | |
1682 | break; | |
1683 | } | |
1684 | case KVM_DEBUG_GUEST: { | |
1685 | struct kvm_debug_guest dbg; | |
1686 | ||
1687 | r = -EFAULT; | |
1688 | if (copy_from_user(&dbg, (void *)arg, sizeof dbg)) | |
1689 | goto out; | |
1690 | r = kvm_dev_ioctl_debug_guest(kvm, &dbg); | |
1691 | if (r) | |
1692 | goto out; | |
1693 | r = 0; | |
1694 | break; | |
1695 | } | |
1696 | case KVM_SET_MEMORY_REGION: { | |
1697 | struct kvm_memory_region kvm_mem; | |
1698 | ||
1699 | r = -EFAULT; | |
1700 | if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem)) | |
1701 | goto out; | |
1702 | r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem); | |
1703 | if (r) | |
1704 | goto out; | |
1705 | break; | |
1706 | } | |
1707 | case KVM_GET_DIRTY_LOG: { | |
1708 | struct kvm_dirty_log log; | |
1709 | ||
1710 | r = -EFAULT; | |
1711 | if (copy_from_user(&log, (void *)arg, sizeof log)) | |
1712 | goto out; | |
1713 | r = kvm_dev_ioctl_get_dirty_log(kvm, &log); | |
1714 | if (r) | |
1715 | goto out; | |
1716 | break; | |
1717 | } | |
1718 | case KVM_GET_MSRS: | |
1719 | r = msr_io(kvm, (void __user *)arg, get_msr, 1); | |
1720 | break; | |
1721 | case KVM_SET_MSRS: | |
1722 | r = msr_io(kvm, (void __user *)arg, do_set_msr, 0); | |
1723 | break; | |
1724 | case KVM_GET_MSR_INDEX_LIST: { | |
1725 | struct kvm_msr_list __user *user_msr_list = (void __user *)arg; | |
1726 | struct kvm_msr_list msr_list; | |
1727 | unsigned n; | |
1728 | ||
1729 | r = -EFAULT; | |
1730 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
1731 | goto out; | |
1732 | n = msr_list.nmsrs; | |
1733 | msr_list.nmsrs = ARRAY_SIZE(msrs_to_save); | |
1734 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) | |
1735 | goto out; | |
1736 | r = -E2BIG; | |
1737 | if (n < ARRAY_SIZE(msrs_to_save)) | |
1738 | goto out; | |
1739 | r = -EFAULT; | |
1740 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
1741 | sizeof msrs_to_save)) | |
1742 | goto out; | |
1743 | r = 0; | |
1744 | } | |
1745 | default: | |
1746 | ; | |
1747 | } | |
1748 | out: | |
1749 | return r; | |
1750 | } | |
1751 | ||
1752 | static struct page *kvm_dev_nopage(struct vm_area_struct *vma, | |
1753 | unsigned long address, | |
1754 | int *type) | |
1755 | { | |
1756 | struct kvm *kvm = vma->vm_file->private_data; | |
1757 | unsigned long pgoff; | |
1758 | struct kvm_memory_slot *slot; | |
1759 | struct page *page; | |
1760 | ||
1761 | *type = VM_FAULT_MINOR; | |
1762 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | |
1763 | slot = gfn_to_memslot(kvm, pgoff); | |
1764 | if (!slot) | |
1765 | return NOPAGE_SIGBUS; | |
1766 | page = gfn_to_page(slot, pgoff); | |
1767 | if (!page) | |
1768 | return NOPAGE_SIGBUS; | |
1769 | get_page(page); | |
1770 | return page; | |
1771 | } | |
1772 | ||
1773 | static struct vm_operations_struct kvm_dev_vm_ops = { | |
1774 | .nopage = kvm_dev_nopage, | |
1775 | }; | |
1776 | ||
1777 | static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma) | |
1778 | { | |
1779 | vma->vm_ops = &kvm_dev_vm_ops; | |
1780 | return 0; | |
1781 | } | |
1782 | ||
1783 | static struct file_operations kvm_chardev_ops = { | |
1784 | .open = kvm_dev_open, | |
1785 | .release = kvm_dev_release, | |
1786 | .unlocked_ioctl = kvm_dev_ioctl, | |
1787 | .compat_ioctl = kvm_dev_ioctl, | |
1788 | .mmap = kvm_dev_mmap, | |
1789 | }; | |
1790 | ||
1791 | static struct miscdevice kvm_dev = { | |
1792 | MISC_DYNAMIC_MINOR, | |
1793 | "kvm", | |
1794 | &kvm_chardev_ops, | |
1795 | }; | |
1796 | ||
1797 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, | |
1798 | void *v) | |
1799 | { | |
1800 | if (val == SYS_RESTART) { | |
1801 | /* | |
1802 | * Some (well, at least mine) BIOSes hang on reboot if | |
1803 | * in vmx root mode. | |
1804 | */ | |
1805 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
1806 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | |
1807 | } | |
1808 | return NOTIFY_OK; | |
1809 | } | |
1810 | ||
1811 | static struct notifier_block kvm_reboot_notifier = { | |
1812 | .notifier_call = kvm_reboot, | |
1813 | .priority = 0, | |
1814 | }; | |
1815 | ||
1816 | static __init void kvm_init_debug(void) | |
1817 | { | |
1818 | struct kvm_stats_debugfs_item *p; | |
1819 | ||
1820 | debugfs_dir = debugfs_create_dir("kvm", 0); | |
1821 | for (p = debugfs_entries; p->name; ++p) | |
1822 | p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir, | |
1823 | p->data); | |
1824 | } | |
1825 | ||
1826 | static void kvm_exit_debug(void) | |
1827 | { | |
1828 | struct kvm_stats_debugfs_item *p; | |
1829 | ||
1830 | for (p = debugfs_entries; p->name; ++p) | |
1831 | debugfs_remove(p->dentry); | |
1832 | debugfs_remove(debugfs_dir); | |
1833 | } | |
1834 | ||
1835 | hpa_t bad_page_address; | |
1836 | ||
1837 | int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module) | |
1838 | { | |
1839 | int r; | |
1840 | ||
1841 | kvm_arch_ops = ops; | |
1842 | ||
1843 | if (!kvm_arch_ops->cpu_has_kvm_support()) { | |
1844 | printk(KERN_ERR "kvm: no hardware support\n"); | |
1845 | return -EOPNOTSUPP; | |
1846 | } | |
1847 | if (kvm_arch_ops->disabled_by_bios()) { | |
1848 | printk(KERN_ERR "kvm: disabled by bios\n"); | |
1849 | return -EOPNOTSUPP; | |
1850 | } | |
1851 | ||
1852 | r = kvm_arch_ops->hardware_setup(); | |
1853 | if (r < 0) | |
1854 | return r; | |
1855 | ||
1856 | on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1); | |
1857 | register_reboot_notifier(&kvm_reboot_notifier); | |
1858 | ||
1859 | kvm_chardev_ops.owner = module; | |
1860 | ||
1861 | r = misc_register(&kvm_dev); | |
1862 | if (r) { | |
1863 | printk (KERN_ERR "kvm: misc device register failed\n"); | |
1864 | goto out_free; | |
1865 | } | |
1866 | ||
1867 | return r; | |
1868 | ||
1869 | out_free: | |
1870 | unregister_reboot_notifier(&kvm_reboot_notifier); | |
1871 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | |
1872 | kvm_arch_ops->hardware_unsetup(); | |
1873 | return r; | |
1874 | } | |
1875 | ||
1876 | void kvm_exit_arch(void) | |
1877 | { | |
1878 | misc_deregister(&kvm_dev); | |
1879 | ||
1880 | unregister_reboot_notifier(&kvm_reboot_notifier); | |
1881 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | |
1882 | kvm_arch_ops->hardware_unsetup(); | |
1883 | } | |
1884 | ||
1885 | static __init int kvm_init(void) | |
1886 | { | |
1887 | static struct page *bad_page; | |
1888 | int r = 0; | |
1889 | ||
1890 | kvm_init_debug(); | |
1891 | ||
1892 | if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) { | |
1893 | r = -ENOMEM; | |
1894 | goto out; | |
1895 | } | |
1896 | ||
1897 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; | |
1898 | memset(__va(bad_page_address), 0, PAGE_SIZE); | |
1899 | ||
1900 | return r; | |
1901 | ||
1902 | out: | |
1903 | kvm_exit_debug(); | |
1904 | return r; | |
1905 | } | |
1906 | ||
1907 | static __exit void kvm_exit(void) | |
1908 | { | |
1909 | kvm_exit_debug(); | |
1910 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); | |
1911 | } | |
1912 | ||
1913 | module_init(kvm_init) | |
1914 | module_exit(kvm_exit) | |
1915 | ||
1916 | EXPORT_SYMBOL_GPL(kvm_init_arch); | |
1917 | EXPORT_SYMBOL_GPL(kvm_exit_arch); |