2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
19 #include "x86_emulate.h"
20 #include "segment_descriptor.h"
23 #include <linux/kvm.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/percpu.h>
27 #include <linux/gfp.h>
29 #include <linux/miscdevice.h>
30 #include <linux/vmalloc.h>
31 #include <linux/reboot.h>
32 #include <linux/debugfs.h>
33 #include <linux/highmem.h>
34 #include <linux/file.h>
35 #include <linux/sysdev.h>
36 #include <linux/cpu.h>
37 #include <linux/sched.h>
38 #include <linux/cpumask.h>
39 #include <linux/smp.h>
40 #include <linux/anon_inodes.h>
41 #include <linux/profile.h>
42 #include <linux/kvm_para.h>
44 #include <asm/processor.h>
47 #include <asm/uaccess.h>
50 MODULE_AUTHOR("Qumranet");
51 MODULE_LICENSE("GPL");
53 static DEFINE_SPINLOCK(kvm_lock
);
54 static LIST_HEAD(vm_list
);
56 static cpumask_t cpus_hardware_enabled
;
58 struct kvm_x86_ops
*kvm_x86_ops
;
59 struct kmem_cache
*kvm_vcpu_cache
;
60 EXPORT_SYMBOL_GPL(kvm_vcpu_cache
);
62 static __read_mostly
struct preempt_ops kvm_preempt_ops
;
64 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
66 static struct kvm_stats_debugfs_item
{
69 struct dentry
*dentry
;
70 } debugfs_entries
[] = {
71 { "pf_fixed", STAT_OFFSET(pf_fixed
) },
72 { "pf_guest", STAT_OFFSET(pf_guest
) },
73 { "tlb_flush", STAT_OFFSET(tlb_flush
) },
74 { "invlpg", STAT_OFFSET(invlpg
) },
75 { "exits", STAT_OFFSET(exits
) },
76 { "io_exits", STAT_OFFSET(io_exits
) },
77 { "mmio_exits", STAT_OFFSET(mmio_exits
) },
78 { "signal_exits", STAT_OFFSET(signal_exits
) },
79 { "irq_window", STAT_OFFSET(irq_window_exits
) },
80 { "halt_exits", STAT_OFFSET(halt_exits
) },
81 { "halt_wakeup", STAT_OFFSET(halt_wakeup
) },
82 { "request_irq", STAT_OFFSET(request_irq_exits
) },
83 { "irq_exits", STAT_OFFSET(irq_exits
) },
84 { "light_exits", STAT_OFFSET(light_exits
) },
85 { "efer_reload", STAT_OFFSET(efer_reload
) },
89 static struct dentry
*debugfs_dir
;
91 #define MAX_IO_MSRS 256
93 #define CR0_RESERVED_BITS \
94 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
95 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
96 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
97 #define CR4_RESERVED_BITS \
98 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
99 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
100 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
101 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
104 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
107 // LDT or TSS descriptor in the GDT. 16 bytes.
108 struct segment_descriptor_64
{
109 struct segment_descriptor s
;
116 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
119 unsigned long segment_base(u16 selector
)
121 struct descriptor_table gdt
;
122 struct segment_descriptor
*d
;
123 unsigned long table_base
;
124 typedef unsigned long ul
;
130 asm ("sgdt %0" : "=m"(gdt
));
131 table_base
= gdt
.base
;
133 if (selector
& 4) { /* from ldt */
136 asm ("sldt %0" : "=g"(ldt_selector
));
137 table_base
= segment_base(ldt_selector
);
139 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
140 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
143 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
144 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
148 EXPORT_SYMBOL_GPL(segment_base
);
150 static inline int valid_vcpu(int n
)
152 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
155 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
157 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
160 vcpu
->guest_fpu_loaded
= 1;
161 fx_save(&vcpu
->host_fx_image
);
162 fx_restore(&vcpu
->guest_fx_image
);
164 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
166 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
168 if (!vcpu
->guest_fpu_loaded
)
171 vcpu
->guest_fpu_loaded
= 0;
172 fx_save(&vcpu
->guest_fx_image
);
173 fx_restore(&vcpu
->host_fx_image
);
175 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
178 * Switches to specified vcpu, until a matching vcpu_put()
180 static void vcpu_load(struct kvm_vcpu
*vcpu
)
184 mutex_lock(&vcpu
->mutex
);
186 preempt_notifier_register(&vcpu
->preempt_notifier
);
187 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
191 static void vcpu_put(struct kvm_vcpu
*vcpu
)
194 kvm_x86_ops
->vcpu_put(vcpu
);
195 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
197 mutex_unlock(&vcpu
->mutex
);
200 static void ack_flush(void *_completed
)
204 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
208 struct kvm_vcpu
*vcpu
;
211 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
212 vcpu
= kvm
->vcpus
[i
];
215 if (test_and_set_bit(KVM_TLB_FLUSH
, &vcpu
->requests
))
218 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
221 smp_call_function_mask(cpus
, ack_flush
, NULL
, 1);
224 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
229 mutex_init(&vcpu
->mutex
);
231 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
234 if (!irqchip_in_kernel(kvm
) || id
== 0)
235 vcpu
->mp_state
= VCPU_MP_STATE_RUNNABLE
;
237 vcpu
->mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
238 init_waitqueue_head(&vcpu
->wq
);
240 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
245 vcpu
->run
= page_address(page
);
247 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
252 vcpu
->pio_data
= page_address(page
);
254 r
= kvm_mmu_create(vcpu
);
256 goto fail_free_pio_data
;
258 if (irqchip_in_kernel(kvm
)) {
259 r
= kvm_create_lapic(vcpu
);
261 goto fail_mmu_destroy
;
267 kvm_mmu_destroy(vcpu
);
269 free_page((unsigned long)vcpu
->pio_data
);
271 free_page((unsigned long)vcpu
->run
);
275 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
277 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
279 kvm_free_lapic(vcpu
);
280 kvm_mmu_destroy(vcpu
);
281 free_page((unsigned long)vcpu
->pio_data
);
282 free_page((unsigned long)vcpu
->run
);
284 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
286 static struct kvm
*kvm_create_vm(void)
288 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
291 return ERR_PTR(-ENOMEM
);
293 kvm_io_bus_init(&kvm
->pio_bus
);
294 mutex_init(&kvm
->lock
);
295 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
296 kvm_io_bus_init(&kvm
->mmio_bus
);
297 spin_lock(&kvm_lock
);
298 list_add(&kvm
->vm_list
, &vm_list
);
299 spin_unlock(&kvm_lock
);
304 * Free any memory in @free but not in @dont.
306 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
307 struct kvm_memory_slot
*dont
)
311 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
312 if (free
->phys_mem
) {
313 for (i
= 0; i
< free
->npages
; ++i
)
314 if (free
->phys_mem
[i
])
315 __free_page(free
->phys_mem
[i
]);
316 vfree(free
->phys_mem
);
318 if (!dont
|| free
->rmap
!= dont
->rmap
)
321 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
322 vfree(free
->dirty_bitmap
);
324 free
->phys_mem
= NULL
;
326 free
->dirty_bitmap
= NULL
;
329 static void kvm_free_physmem(struct kvm
*kvm
)
333 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
334 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
337 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
341 for (i
= 0; i
< ARRAY_SIZE(vcpu
->pio
.guest_pages
); ++i
)
342 if (vcpu
->pio
.guest_pages
[i
]) {
343 __free_page(vcpu
->pio
.guest_pages
[i
]);
344 vcpu
->pio
.guest_pages
[i
] = NULL
;
348 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
351 kvm_mmu_unload(vcpu
);
355 static void kvm_free_vcpus(struct kvm
*kvm
)
360 * Unpin any mmu pages first.
362 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
364 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
365 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
367 kvm_x86_ops
->vcpu_free(kvm
->vcpus
[i
]);
368 kvm
->vcpus
[i
] = NULL
;
374 static void kvm_destroy_vm(struct kvm
*kvm
)
376 spin_lock(&kvm_lock
);
377 list_del(&kvm
->vm_list
);
378 spin_unlock(&kvm_lock
);
379 kvm_io_bus_destroy(&kvm
->pio_bus
);
380 kvm_io_bus_destroy(&kvm
->mmio_bus
);
384 kvm_free_physmem(kvm
);
388 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
390 struct kvm
*kvm
= filp
->private_data
;
396 static void inject_gp(struct kvm_vcpu
*vcpu
)
398 kvm_x86_ops
->inject_gp(vcpu
, 0);
402 * Load the pae pdptrs. Return true is they are all valid.
404 static int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
406 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
407 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
410 u64 pdpte
[ARRAY_SIZE(vcpu
->pdptrs
)];
412 mutex_lock(&vcpu
->kvm
->lock
);
413 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
414 offset
* sizeof(u64
), sizeof(pdpte
));
419 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
420 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
427 memcpy(vcpu
->pdptrs
, pdpte
, sizeof(vcpu
->pdptrs
));
429 mutex_unlock(&vcpu
->kvm
->lock
);
434 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
436 if (cr0
& CR0_RESERVED_BITS
) {
437 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
443 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
444 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
449 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
450 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
451 "and a clear PE flag\n");
456 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
458 if ((vcpu
->shadow_efer
& EFER_LME
)) {
462 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
463 "in long mode while PAE is disabled\n");
467 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
469 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
470 "in long mode while CS.L == 1\n");
477 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
478 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
486 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
489 mutex_lock(&vcpu
->kvm
->lock
);
490 kvm_mmu_reset_context(vcpu
);
491 mutex_unlock(&vcpu
->kvm
->lock
);
494 EXPORT_SYMBOL_GPL(set_cr0
);
496 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
498 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
500 EXPORT_SYMBOL_GPL(lmsw
);
502 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
504 if (cr4
& CR4_RESERVED_BITS
) {
505 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
510 if (is_long_mode(vcpu
)) {
511 if (!(cr4
& X86_CR4_PAE
)) {
512 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
517 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
518 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
519 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
524 if (cr4
& X86_CR4_VMXE
) {
525 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
529 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
531 mutex_lock(&vcpu
->kvm
->lock
);
532 kvm_mmu_reset_context(vcpu
);
533 mutex_unlock(&vcpu
->kvm
->lock
);
535 EXPORT_SYMBOL_GPL(set_cr4
);
537 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
539 if (is_long_mode(vcpu
)) {
540 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
541 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
547 if (cr3
& CR3_PAE_RESERVED_BITS
) {
549 "set_cr3: #GP, reserved bits\n");
553 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
554 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
561 * We don't check reserved bits in nonpae mode, because
562 * this isn't enforced, and VMware depends on this.
566 mutex_lock(&vcpu
->kvm
->lock
);
568 * Does the new cr3 value map to physical memory? (Note, we
569 * catch an invalid cr3 even in real-mode, because it would
570 * cause trouble later on when we turn on paging anyway.)
572 * A real CPU would silently accept an invalid cr3 and would
573 * attempt to use it - with largely undefined (and often hard
574 * to debug) behavior on the guest side.
576 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
580 vcpu
->mmu
.new_cr3(vcpu
);
582 mutex_unlock(&vcpu
->kvm
->lock
);
584 EXPORT_SYMBOL_GPL(set_cr3
);
586 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
588 if (cr8
& CR8_RESERVED_BITS
) {
589 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
593 if (irqchip_in_kernel(vcpu
->kvm
))
594 kvm_lapic_set_tpr(vcpu
, cr8
);
598 EXPORT_SYMBOL_GPL(set_cr8
);
600 unsigned long get_cr8(struct kvm_vcpu
*vcpu
)
602 if (irqchip_in_kernel(vcpu
->kvm
))
603 return kvm_lapic_get_cr8(vcpu
);
607 EXPORT_SYMBOL_GPL(get_cr8
);
609 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
611 if (irqchip_in_kernel(vcpu
->kvm
))
612 return vcpu
->apic_base
;
614 return vcpu
->apic_base
;
616 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
618 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
620 /* TODO: reserve bits check */
621 if (irqchip_in_kernel(vcpu
->kvm
))
622 kvm_lapic_set_base(vcpu
, data
);
624 vcpu
->apic_base
= data
;
626 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
628 void fx_init(struct kvm_vcpu
*vcpu
)
630 unsigned after_mxcsr_mask
;
632 /* Initialize guest FPU by resetting ours and saving into guest's */
634 fx_save(&vcpu
->host_fx_image
);
636 fx_save(&vcpu
->guest_fx_image
);
637 fx_restore(&vcpu
->host_fx_image
);
640 vcpu
->cr0
|= X86_CR0_ET
;
641 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
642 vcpu
->guest_fx_image
.mxcsr
= 0x1f80;
643 memset((void *)&vcpu
->guest_fx_image
+ after_mxcsr_mask
,
644 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
646 EXPORT_SYMBOL_GPL(fx_init
);
649 * Allocate some memory and give it an address in the guest physical address
652 * Discontiguous memory is allowed, mostly for framebuffers.
654 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
655 struct kvm_memory_region
*mem
)
659 unsigned long npages
;
661 struct kvm_memory_slot
*memslot
;
662 struct kvm_memory_slot old
, new;
665 /* General sanity checks */
666 if (mem
->memory_size
& (PAGE_SIZE
- 1))
668 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
670 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
672 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
675 memslot
= &kvm
->memslots
[mem
->slot
];
676 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
677 npages
= mem
->memory_size
>> PAGE_SHIFT
;
680 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
682 mutex_lock(&kvm
->lock
);
684 new = old
= *memslot
;
686 new.base_gfn
= base_gfn
;
688 new.flags
= mem
->flags
;
690 /* Disallow changing a memory slot's size. */
692 if (npages
&& old
.npages
&& npages
!= old
.npages
)
695 /* Check for overlaps */
697 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
698 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
702 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
703 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
707 /* Deallocate if slot is being removed */
711 /* Free page dirty bitmap if unneeded */
712 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
713 new.dirty_bitmap
= NULL
;
717 /* Allocate if a slot is being created */
718 if (npages
&& !new.phys_mem
) {
719 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
724 new.rmap
= vmalloc(npages
* sizeof(struct page
*));
729 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
730 memset(new.rmap
, 0, npages
* sizeof(*new.rmap
));
731 for (i
= 0; i
< npages
; ++i
) {
732 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
734 if (!new.phys_mem
[i
])
739 /* Allocate page dirty bitmap if needed */
740 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
741 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
743 new.dirty_bitmap
= vmalloc(dirty_bytes
);
744 if (!new.dirty_bitmap
)
746 memset(new.dirty_bitmap
, 0, dirty_bytes
);
749 if (mem
->slot
>= kvm
->nmemslots
)
750 kvm
->nmemslots
= mem
->slot
+ 1;
752 if (!kvm
->n_requested_mmu_pages
) {
753 unsigned int n_pages
;
756 n_pages
= npages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
757 kvm_mmu_change_mmu_pages(kvm
, kvm
->n_alloc_mmu_pages
+
760 unsigned int nr_mmu_pages
;
762 n_pages
= old
.npages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
763 nr_mmu_pages
= kvm
->n_alloc_mmu_pages
- n_pages
;
764 nr_mmu_pages
= max(nr_mmu_pages
,
765 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
766 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
772 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
773 kvm_flush_remote_tlbs(kvm
);
775 mutex_unlock(&kvm
->lock
);
777 kvm_free_physmem_slot(&old
, &new);
781 mutex_unlock(&kvm
->lock
);
782 kvm_free_physmem_slot(&new, &old
);
787 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
788 u32 kvm_nr_mmu_pages
)
790 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
793 mutex_lock(&kvm
->lock
);
795 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
796 kvm
->n_requested_mmu_pages
= kvm_nr_mmu_pages
;
798 mutex_unlock(&kvm
->lock
);
802 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
804 return kvm
->n_alloc_mmu_pages
;
808 * Get (and clear) the dirty memory log for a memory slot.
810 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
811 struct kvm_dirty_log
*log
)
813 struct kvm_memory_slot
*memslot
;
816 unsigned long any
= 0;
818 mutex_lock(&kvm
->lock
);
821 if (log
->slot
>= KVM_MEMORY_SLOTS
)
824 memslot
= &kvm
->memslots
[log
->slot
];
826 if (!memslot
->dirty_bitmap
)
829 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
831 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
832 any
= memslot
->dirty_bitmap
[i
];
835 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
838 /* If nothing is dirty, don't bother messing with page tables. */
840 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
841 kvm_flush_remote_tlbs(kvm
);
842 memset(memslot
->dirty_bitmap
, 0, n
);
848 mutex_unlock(&kvm
->lock
);
853 * Set a new alias region. Aliases map a portion of physical memory into
854 * another portion. This is useful for memory windows, for example the PC
857 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
858 struct kvm_memory_alias
*alias
)
861 struct kvm_mem_alias
*p
;
864 /* General sanity checks */
865 if (alias
->memory_size
& (PAGE_SIZE
- 1))
867 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
869 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
871 if (alias
->guest_phys_addr
+ alias
->memory_size
872 < alias
->guest_phys_addr
)
874 if (alias
->target_phys_addr
+ alias
->memory_size
875 < alias
->target_phys_addr
)
878 mutex_lock(&kvm
->lock
);
880 p
= &kvm
->aliases
[alias
->slot
];
881 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
882 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
883 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
885 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
886 if (kvm
->aliases
[n
- 1].npages
)
890 kvm_mmu_zap_all(kvm
);
892 mutex_unlock(&kvm
->lock
);
900 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
905 switch (chip
->chip_id
) {
906 case KVM_IRQCHIP_PIC_MASTER
:
907 memcpy (&chip
->chip
.pic
,
908 &pic_irqchip(kvm
)->pics
[0],
909 sizeof(struct kvm_pic_state
));
911 case KVM_IRQCHIP_PIC_SLAVE
:
912 memcpy (&chip
->chip
.pic
,
913 &pic_irqchip(kvm
)->pics
[1],
914 sizeof(struct kvm_pic_state
));
916 case KVM_IRQCHIP_IOAPIC
:
917 memcpy (&chip
->chip
.ioapic
,
919 sizeof(struct kvm_ioapic_state
));
928 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
933 switch (chip
->chip_id
) {
934 case KVM_IRQCHIP_PIC_MASTER
:
935 memcpy (&pic_irqchip(kvm
)->pics
[0],
937 sizeof(struct kvm_pic_state
));
939 case KVM_IRQCHIP_PIC_SLAVE
:
940 memcpy (&pic_irqchip(kvm
)->pics
[1],
942 sizeof(struct kvm_pic_state
));
944 case KVM_IRQCHIP_IOAPIC
:
945 memcpy (ioapic_irqchip(kvm
),
947 sizeof(struct kvm_ioapic_state
));
953 kvm_pic_update_irq(pic_irqchip(kvm
));
957 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
960 struct kvm_mem_alias
*alias
;
962 for (i
= 0; i
< kvm
->naliases
; ++i
) {
963 alias
= &kvm
->aliases
[i
];
964 if (gfn
>= alias
->base_gfn
965 && gfn
< alias
->base_gfn
+ alias
->npages
)
966 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
971 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
975 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
976 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
978 if (gfn
>= memslot
->base_gfn
979 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
985 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
987 gfn
= unalias_gfn(kvm
, gfn
);
988 return __gfn_to_memslot(kvm
, gfn
);
991 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
993 struct kvm_memory_slot
*slot
;
995 gfn
= unalias_gfn(kvm
, gfn
);
996 slot
= __gfn_to_memslot(kvm
, gfn
);
999 return slot
->phys_mem
[gfn
- slot
->base_gfn
];
1001 EXPORT_SYMBOL_GPL(gfn_to_page
);
1003 static int next_segment(unsigned long len
, int offset
)
1005 if (len
> PAGE_SIZE
- offset
)
1006 return PAGE_SIZE
- offset
;
1011 int kvm_read_guest_page(struct kvm
*kvm
, gfn_t gfn
, void *data
, int offset
,
1017 page
= gfn_to_page(kvm
, gfn
);
1020 page_virt
= kmap_atomic(page
, KM_USER0
);
1022 memcpy(data
, page_virt
+ offset
, len
);
1024 kunmap_atomic(page_virt
, KM_USER0
);
1027 EXPORT_SYMBOL_GPL(kvm_read_guest_page
);
1029 int kvm_read_guest(struct kvm
*kvm
, gpa_t gpa
, void *data
, unsigned long len
)
1031 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1033 int offset
= offset_in_page(gpa
);
1036 while ((seg
= next_segment(len
, offset
)) != 0) {
1037 ret
= kvm_read_guest_page(kvm
, gfn
, data
, offset
, seg
);
1047 EXPORT_SYMBOL_GPL(kvm_read_guest
);
1049 int kvm_write_guest_page(struct kvm
*kvm
, gfn_t gfn
, const void *data
,
1050 int offset
, int len
)
1055 page
= gfn_to_page(kvm
, gfn
);
1058 page_virt
= kmap_atomic(page
, KM_USER0
);
1060 memcpy(page_virt
+ offset
, data
, len
);
1062 kunmap_atomic(page_virt
, KM_USER0
);
1063 mark_page_dirty(kvm
, gfn
);
1066 EXPORT_SYMBOL_GPL(kvm_write_guest_page
);
1068 int kvm_write_guest(struct kvm
*kvm
, gpa_t gpa
, const void *data
,
1071 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1073 int offset
= offset_in_page(gpa
);
1076 while ((seg
= next_segment(len
, offset
)) != 0) {
1077 ret
= kvm_write_guest_page(kvm
, gfn
, data
, offset
, seg
);
1088 int kvm_clear_guest_page(struct kvm
*kvm
, gfn_t gfn
, int offset
, int len
)
1093 page
= gfn_to_page(kvm
, gfn
);
1096 page_virt
= kmap_atomic(page
, KM_USER0
);
1098 memset(page_virt
+ offset
, 0, len
);
1100 kunmap_atomic(page_virt
, KM_USER0
);
1103 EXPORT_SYMBOL_GPL(kvm_clear_guest_page
);
1105 int kvm_clear_guest(struct kvm
*kvm
, gpa_t gpa
, unsigned long len
)
1107 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1109 int offset
= offset_in_page(gpa
);
1112 while ((seg
= next_segment(len
, offset
)) != 0) {
1113 ret
= kvm_clear_guest_page(kvm
, gfn
, offset
, seg
);
1122 EXPORT_SYMBOL_GPL(kvm_clear_guest
);
1124 /* WARNING: Does not work on aliased pages. */
1125 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1127 struct kvm_memory_slot
*memslot
;
1129 memslot
= __gfn_to_memslot(kvm
, gfn
);
1130 if (memslot
&& memslot
->dirty_bitmap
) {
1131 unsigned long rel_gfn
= gfn
- memslot
->base_gfn
;
1134 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1135 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1139 int emulator_read_std(unsigned long addr
,
1142 struct kvm_vcpu
*vcpu
)
1147 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1148 unsigned offset
= addr
& (PAGE_SIZE
-1);
1149 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1152 if (gpa
== UNMAPPED_GVA
)
1153 return X86EMUL_PROPAGATE_FAULT
;
1154 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1156 return X86EMUL_UNHANDLEABLE
;
1163 return X86EMUL_CONTINUE
;
1165 EXPORT_SYMBOL_GPL(emulator_read_std
);
1167 static int emulator_write_std(unsigned long addr
,
1170 struct kvm_vcpu
*vcpu
)
1172 pr_unimpl(vcpu
, "emulator_write_std: addr %lx n %d\n", addr
, bytes
);
1173 return X86EMUL_UNHANDLEABLE
;
1177 * Only apic need an MMIO device hook, so shortcut now..
1179 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1182 struct kvm_io_device
*dev
;
1185 dev
= &vcpu
->apic
->dev
;
1186 if (dev
->in_range(dev
, addr
))
1192 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1195 struct kvm_io_device
*dev
;
1197 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1199 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1203 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1206 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1209 static int emulator_read_emulated(unsigned long addr
,
1212 struct kvm_vcpu
*vcpu
)
1214 struct kvm_io_device
*mmio_dev
;
1217 if (vcpu
->mmio_read_completed
) {
1218 memcpy(val
, vcpu
->mmio_data
, bytes
);
1219 vcpu
->mmio_read_completed
= 0;
1220 return X86EMUL_CONTINUE
;
1221 } else if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1222 == X86EMUL_CONTINUE
)
1223 return X86EMUL_CONTINUE
;
1225 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1226 if (gpa
== UNMAPPED_GVA
)
1227 return X86EMUL_PROPAGATE_FAULT
;
1230 * Is this MMIO handled locally?
1232 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1234 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1235 return X86EMUL_CONTINUE
;
1238 vcpu
->mmio_needed
= 1;
1239 vcpu
->mmio_phys_addr
= gpa
;
1240 vcpu
->mmio_size
= bytes
;
1241 vcpu
->mmio_is_write
= 0;
1243 return X86EMUL_UNHANDLEABLE
;
1246 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1247 const void *val
, int bytes
)
1251 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1254 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1258 static int emulator_write_emulated_onepage(unsigned long addr
,
1261 struct kvm_vcpu
*vcpu
)
1263 struct kvm_io_device
*mmio_dev
;
1264 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1266 if (gpa
== UNMAPPED_GVA
) {
1267 kvm_x86_ops
->inject_page_fault(vcpu
, addr
, 2);
1268 return X86EMUL_PROPAGATE_FAULT
;
1271 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1272 return X86EMUL_CONTINUE
;
1275 * Is this MMIO handled locally?
1277 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1279 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1280 return X86EMUL_CONTINUE
;
1283 vcpu
->mmio_needed
= 1;
1284 vcpu
->mmio_phys_addr
= gpa
;
1285 vcpu
->mmio_size
= bytes
;
1286 vcpu
->mmio_is_write
= 1;
1287 memcpy(vcpu
->mmio_data
, val
, bytes
);
1289 return X86EMUL_CONTINUE
;
1292 int emulator_write_emulated(unsigned long addr
,
1295 struct kvm_vcpu
*vcpu
)
1297 /* Crossing a page boundary? */
1298 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1301 now
= -addr
& ~PAGE_MASK
;
1302 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1303 if (rc
!= X86EMUL_CONTINUE
)
1309 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1311 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1313 static int emulator_cmpxchg_emulated(unsigned long addr
,
1317 struct kvm_vcpu
*vcpu
)
1319 static int reported
;
1323 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1325 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1328 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1330 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1333 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1335 return X86EMUL_CONTINUE
;
1338 int emulate_clts(struct kvm_vcpu
*vcpu
)
1340 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->cr0
& ~X86_CR0_TS
);
1341 return X86EMUL_CONTINUE
;
1344 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1346 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1350 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1351 return X86EMUL_CONTINUE
;
1353 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __FUNCTION__
, dr
);
1354 return X86EMUL_UNHANDLEABLE
;
1358 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1360 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1363 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1365 /* FIXME: better handling */
1366 return X86EMUL_UNHANDLEABLE
;
1368 return X86EMUL_CONTINUE
;
1371 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
1373 static int reported
;
1375 unsigned long rip
= vcpu
->rip
;
1376 unsigned long rip_linear
;
1378 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
1383 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
1385 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1386 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1389 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
1391 struct x86_emulate_ops emulate_ops
= {
1392 .read_std
= emulator_read_std
,
1393 .write_std
= emulator_write_std
,
1394 .read_emulated
= emulator_read_emulated
,
1395 .write_emulated
= emulator_write_emulated
,
1396 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1399 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1400 struct kvm_run
*run
,
1407 vcpu
->mmio_fault_cr2
= cr2
;
1408 kvm_x86_ops
->cache_regs(vcpu
);
1410 vcpu
->mmio_is_write
= 0;
1411 vcpu
->pio
.string
= 0;
1415 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1417 vcpu
->emulate_ctxt
.vcpu
= vcpu
;
1418 vcpu
->emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
1419 vcpu
->emulate_ctxt
.cr2
= cr2
;
1420 vcpu
->emulate_ctxt
.mode
=
1421 (vcpu
->emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1422 ? X86EMUL_MODE_REAL
: cs_l
1423 ? X86EMUL_MODE_PROT64
: cs_db
1424 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1426 if (vcpu
->emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1427 vcpu
->emulate_ctxt
.cs_base
= 0;
1428 vcpu
->emulate_ctxt
.ds_base
= 0;
1429 vcpu
->emulate_ctxt
.es_base
= 0;
1430 vcpu
->emulate_ctxt
.ss_base
= 0;
1432 vcpu
->emulate_ctxt
.cs_base
=
1433 get_segment_base(vcpu
, VCPU_SREG_CS
);
1434 vcpu
->emulate_ctxt
.ds_base
=
1435 get_segment_base(vcpu
, VCPU_SREG_DS
);
1436 vcpu
->emulate_ctxt
.es_base
=
1437 get_segment_base(vcpu
, VCPU_SREG_ES
);
1438 vcpu
->emulate_ctxt
.ss_base
=
1439 get_segment_base(vcpu
, VCPU_SREG_SS
);
1442 vcpu
->emulate_ctxt
.gs_base
=
1443 get_segment_base(vcpu
, VCPU_SREG_GS
);
1444 vcpu
->emulate_ctxt
.fs_base
=
1445 get_segment_base(vcpu
, VCPU_SREG_FS
);
1447 r
= x86_decode_insn(&vcpu
->emulate_ctxt
, &emulate_ops
);
1449 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1450 return EMULATE_DONE
;
1451 return EMULATE_FAIL
;
1455 r
= x86_emulate_insn(&vcpu
->emulate_ctxt
, &emulate_ops
);
1457 if (vcpu
->pio
.string
)
1458 return EMULATE_DO_MMIO
;
1460 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1461 run
->exit_reason
= KVM_EXIT_MMIO
;
1462 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1463 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1464 run
->mmio
.len
= vcpu
->mmio_size
;
1465 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1469 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1470 return EMULATE_DONE
;
1471 if (!vcpu
->mmio_needed
) {
1472 kvm_report_emulation_failure(vcpu
, "mmio");
1473 return EMULATE_FAIL
;
1475 return EMULATE_DO_MMIO
;
1478 kvm_x86_ops
->decache_regs(vcpu
);
1479 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->emulate_ctxt
.eflags
);
1481 if (vcpu
->mmio_is_write
) {
1482 vcpu
->mmio_needed
= 0;
1483 return EMULATE_DO_MMIO
;
1486 return EMULATE_DONE
;
1488 EXPORT_SYMBOL_GPL(emulate_instruction
);
1491 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1493 static void kvm_vcpu_block(struct kvm_vcpu
*vcpu
)
1495 DECLARE_WAITQUEUE(wait
, current
);
1497 add_wait_queue(&vcpu
->wq
, &wait
);
1500 * We will block until either an interrupt or a signal wakes us up
1502 while (!kvm_cpu_has_interrupt(vcpu
)
1503 && !signal_pending(current
)
1504 && vcpu
->mp_state
!= VCPU_MP_STATE_RUNNABLE
1505 && vcpu
->mp_state
!= VCPU_MP_STATE_SIPI_RECEIVED
) {
1506 set_current_state(TASK_INTERRUPTIBLE
);
1512 __set_current_state(TASK_RUNNING
);
1513 remove_wait_queue(&vcpu
->wq
, &wait
);
1516 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
1518 ++vcpu
->stat
.halt_exits
;
1519 if (irqchip_in_kernel(vcpu
->kvm
)) {
1520 vcpu
->mp_state
= VCPU_MP_STATE_HALTED
;
1521 kvm_vcpu_block(vcpu
);
1522 if (vcpu
->mp_state
!= VCPU_MP_STATE_RUNNABLE
)
1526 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
1530 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
1532 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
1534 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
1536 kvm_x86_ops
->cache_regs(vcpu
);
1538 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1539 a0
= vcpu
->regs
[VCPU_REGS_RBX
];
1540 a1
= vcpu
->regs
[VCPU_REGS_RCX
];
1541 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1542 a3
= vcpu
->regs
[VCPU_REGS_RSI
];
1544 if (!is_long_mode(vcpu
)) {
1557 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1558 kvm_x86_ops
->decache_regs(vcpu
);
1561 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
1563 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
1565 char instruction
[3];
1568 mutex_lock(&vcpu
->kvm
->lock
);
1571 * Blow out the MMU to ensure that no other VCPU has an active mapping
1572 * to ensure that the updated hypercall appears atomically across all
1575 kvm_mmu_zap_all(vcpu
->kvm
);
1577 kvm_x86_ops
->cache_regs(vcpu
);
1578 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
1579 if (emulator_write_emulated(vcpu
->rip
, instruction
, 3, vcpu
)
1580 != X86EMUL_CONTINUE
)
1583 mutex_unlock(&vcpu
->kvm
->lock
);
1588 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1590 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1593 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1595 struct descriptor_table dt
= { limit
, base
};
1597 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
1600 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1602 struct descriptor_table dt
= { limit
, base
};
1604 kvm_x86_ops
->set_idt(vcpu
, &dt
);
1607 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1608 unsigned long *rflags
)
1611 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
1614 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1616 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
1627 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1632 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1633 unsigned long *rflags
)
1637 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1638 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
1647 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1650 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1654 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1659 case 0xc0010010: /* SYSCFG */
1660 case 0xc0010015: /* HWCR */
1661 case MSR_IA32_PLATFORM_ID
:
1662 case MSR_IA32_P5_MC_ADDR
:
1663 case MSR_IA32_P5_MC_TYPE
:
1664 case MSR_IA32_MC0_CTL
:
1665 case MSR_IA32_MCG_STATUS
:
1666 case MSR_IA32_MCG_CAP
:
1667 case MSR_IA32_MC0_MISC
:
1668 case MSR_IA32_MC0_MISC
+4:
1669 case MSR_IA32_MC0_MISC
+8:
1670 case MSR_IA32_MC0_MISC
+12:
1671 case MSR_IA32_MC0_MISC
+16:
1672 case MSR_IA32_UCODE_REV
:
1673 case MSR_IA32_PERF_STATUS
:
1674 case MSR_IA32_EBL_CR_POWERON
:
1675 /* MTRR registers */
1677 case 0x200 ... 0x2ff:
1680 case 0xcd: /* fsb frequency */
1683 case MSR_IA32_APICBASE
:
1684 data
= kvm_get_apic_base(vcpu
);
1686 case MSR_IA32_MISC_ENABLE
:
1687 data
= vcpu
->ia32_misc_enable_msr
;
1689 #ifdef CONFIG_X86_64
1691 data
= vcpu
->shadow_efer
;
1695 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
1701 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1704 * Reads an msr value (of 'msr_index') into 'pdata'.
1705 * Returns 0 on success, non-0 otherwise.
1706 * Assumes vcpu_load() was already called.
1708 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1710 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
1713 #ifdef CONFIG_X86_64
1715 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1717 if (efer
& EFER_RESERVED_BITS
) {
1718 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1725 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1726 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1731 kvm_x86_ops
->set_efer(vcpu
, efer
);
1734 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1736 vcpu
->shadow_efer
= efer
;
1741 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1744 #ifdef CONFIG_X86_64
1746 set_efer(vcpu
, data
);
1749 case MSR_IA32_MC0_STATUS
:
1750 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1751 __FUNCTION__
, data
);
1753 case MSR_IA32_MCG_STATUS
:
1754 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1755 __FUNCTION__
, data
);
1757 case MSR_IA32_UCODE_REV
:
1758 case MSR_IA32_UCODE_WRITE
:
1759 case 0x200 ... 0x2ff: /* MTRRs */
1761 case MSR_IA32_APICBASE
:
1762 kvm_set_apic_base(vcpu
, data
);
1764 case MSR_IA32_MISC_ENABLE
:
1765 vcpu
->ia32_misc_enable_msr
= data
;
1768 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x\n", msr
);
1773 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1776 * Writes msr value into into the appropriate "register".
1777 * Returns 0 on success, non-0 otherwise.
1778 * Assumes vcpu_load() was already called.
1780 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1782 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
1785 void kvm_resched(struct kvm_vcpu
*vcpu
)
1787 if (!need_resched())
1791 EXPORT_SYMBOL_GPL(kvm_resched
);
1793 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1797 struct kvm_cpuid_entry
*e
, *best
;
1799 kvm_x86_ops
->cache_regs(vcpu
);
1800 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1801 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1802 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1803 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1804 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1806 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1807 e
= &vcpu
->cpuid_entries
[i
];
1808 if (e
->function
== function
) {
1813 * Both basic or both extended?
1815 if (((e
->function
^ function
) & 0x80000000) == 0)
1816 if (!best
|| e
->function
> best
->function
)
1820 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1821 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1822 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1823 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1825 kvm_x86_ops
->decache_regs(vcpu
);
1826 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1828 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1830 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1832 void *p
= vcpu
->pio_data
;
1835 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1837 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1840 free_pio_guest_pages(vcpu
);
1843 q
+= vcpu
->pio
.guest_page_offset
;
1844 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1846 memcpy(q
, p
, bytes
);
1848 memcpy(p
, q
, bytes
);
1849 q
-= vcpu
->pio
.guest_page_offset
;
1851 free_pio_guest_pages(vcpu
);
1855 static int complete_pio(struct kvm_vcpu
*vcpu
)
1857 struct kvm_pio_request
*io
= &vcpu
->pio
;
1861 kvm_x86_ops
->cache_regs(vcpu
);
1865 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1869 r
= pio_copy_data(vcpu
);
1871 kvm_x86_ops
->cache_regs(vcpu
);
1878 delta
*= io
->cur_count
;
1880 * The size of the register should really depend on
1881 * current address size.
1883 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1889 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1891 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1894 kvm_x86_ops
->decache_regs(vcpu
);
1896 io
->count
-= io
->cur_count
;
1902 static void kernel_pio(struct kvm_io_device
*pio_dev
,
1903 struct kvm_vcpu
*vcpu
,
1906 /* TODO: String I/O for in kernel device */
1908 mutex_lock(&vcpu
->kvm
->lock
);
1910 kvm_iodevice_read(pio_dev
, vcpu
->pio
.port
,
1914 kvm_iodevice_write(pio_dev
, vcpu
->pio
.port
,
1917 mutex_unlock(&vcpu
->kvm
->lock
);
1920 static void pio_string_write(struct kvm_io_device
*pio_dev
,
1921 struct kvm_vcpu
*vcpu
)
1923 struct kvm_pio_request
*io
= &vcpu
->pio
;
1924 void *pd
= vcpu
->pio_data
;
1927 mutex_lock(&vcpu
->kvm
->lock
);
1928 for (i
= 0; i
< io
->cur_count
; i
++) {
1929 kvm_iodevice_write(pio_dev
, io
->port
,
1934 mutex_unlock(&vcpu
->kvm
->lock
);
1937 int kvm_emulate_pio (struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1938 int size
, unsigned port
)
1940 struct kvm_io_device
*pio_dev
;
1942 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1943 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1944 vcpu
->run
->io
.size
= vcpu
->pio
.size
= size
;
1945 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1946 vcpu
->run
->io
.count
= vcpu
->pio
.count
= vcpu
->pio
.cur_count
= 1;
1947 vcpu
->run
->io
.port
= vcpu
->pio
.port
= port
;
1949 vcpu
->pio
.string
= 0;
1951 vcpu
->pio
.guest_page_offset
= 0;
1954 kvm_x86_ops
->cache_regs(vcpu
);
1955 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1956 kvm_x86_ops
->decache_regs(vcpu
);
1958 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1960 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1962 kernel_pio(pio_dev
, vcpu
, vcpu
->pio_data
);
1968 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
1970 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1971 int size
, unsigned long count
, int down
,
1972 gva_t address
, int rep
, unsigned port
)
1974 unsigned now
, in_page
;
1978 struct kvm_io_device
*pio_dev
;
1980 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1981 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1982 vcpu
->run
->io
.size
= vcpu
->pio
.size
= size
;
1983 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1984 vcpu
->run
->io
.count
= vcpu
->pio
.count
= vcpu
->pio
.cur_count
= count
;
1985 vcpu
->run
->io
.port
= vcpu
->pio
.port
= port
;
1987 vcpu
->pio
.string
= 1;
1988 vcpu
->pio
.down
= down
;
1989 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1990 vcpu
->pio
.rep
= rep
;
1993 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1998 in_page
= PAGE_SIZE
- offset_in_page(address
);
2000 in_page
= offset_in_page(address
) + size
;
2001 now
= min(count
, (unsigned long)in_page
/ size
);
2004 * String I/O straddles page boundary. Pin two guest pages
2005 * so that we satisfy atomicity constraints. Do just one
2006 * transaction to avoid complexity.
2013 * String I/O in reverse. Yuck. Kill the guest, fix later.
2015 pr_unimpl(vcpu
, "guest string pio down\n");
2019 vcpu
->run
->io
.count
= now
;
2020 vcpu
->pio
.cur_count
= now
;
2022 if (vcpu
->pio
.cur_count
== vcpu
->pio
.count
)
2023 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2025 for (i
= 0; i
< nr_pages
; ++i
) {
2026 mutex_lock(&vcpu
->kvm
->lock
);
2027 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2030 vcpu
->pio
.guest_pages
[i
] = page
;
2031 mutex_unlock(&vcpu
->kvm
->lock
);
2034 free_pio_guest_pages(vcpu
);
2039 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2040 if (!vcpu
->pio
.in
) {
2041 /* string PIO write */
2042 ret
= pio_copy_data(vcpu
);
2043 if (ret
>= 0 && pio_dev
) {
2044 pio_string_write(pio_dev
, vcpu
);
2046 if (vcpu
->pio
.count
== 0)
2050 pr_unimpl(vcpu
, "no string pio read support yet, "
2051 "port %x size %d count %ld\n",
2056 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2059 * Check if userspace requested an interrupt window, and that the
2060 * interrupt window is open.
2062 * No need to exit to userspace if we already have an interrupt queued.
2064 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2065 struct kvm_run
*kvm_run
)
2067 return (!vcpu
->irq_summary
&&
2068 kvm_run
->request_interrupt_window
&&
2069 vcpu
->interrupt_window_open
&&
2070 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2073 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2074 struct kvm_run
*kvm_run
)
2076 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2077 kvm_run
->cr8
= get_cr8(vcpu
);
2078 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2079 if (irqchip_in_kernel(vcpu
->kvm
))
2080 kvm_run
->ready_for_interrupt_injection
= 1;
2082 kvm_run
->ready_for_interrupt_injection
=
2083 (vcpu
->interrupt_window_open
&&
2084 vcpu
->irq_summary
== 0);
2087 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2091 if (unlikely(vcpu
->mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2092 printk("vcpu %d received sipi with vector # %x\n",
2093 vcpu
->vcpu_id
, vcpu
->sipi_vector
);
2094 kvm_lapic_reset(vcpu
);
2095 kvm_x86_ops
->vcpu_reset(vcpu
);
2096 vcpu
->mp_state
= VCPU_MP_STATE_RUNNABLE
;
2100 if (vcpu
->guest_debug
.enabled
)
2101 kvm_x86_ops
->guest_debug_pre(vcpu
);
2104 r
= kvm_mmu_reload(vcpu
);
2110 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2111 kvm_load_guest_fpu(vcpu
);
2113 local_irq_disable();
2115 if (signal_pending(current
)) {
2119 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2120 ++vcpu
->stat
.signal_exits
;
2124 if (irqchip_in_kernel(vcpu
->kvm
))
2125 kvm_x86_ops
->inject_pending_irq(vcpu
);
2126 else if (!vcpu
->mmio_read_completed
)
2127 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2129 vcpu
->guest_mode
= 1;
2133 if (test_and_clear_bit(KVM_TLB_FLUSH
, &vcpu
->requests
))
2134 kvm_x86_ops
->tlb_flush(vcpu
);
2136 kvm_x86_ops
->run(vcpu
, kvm_run
);
2138 vcpu
->guest_mode
= 0;
2144 * We must have an instruction between local_irq_enable() and
2145 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2146 * the interrupt shadow. The stat.exits increment will do nicely.
2147 * But we need to prevent reordering, hence this barrier():
2156 * Profile KVM exit RIPs:
2158 if (unlikely(prof_on
== KVM_PROFILING
)) {
2159 kvm_x86_ops
->cache_regs(vcpu
);
2160 profile_hit(KVM_PROFILING
, (void *)vcpu
->rip
);
2163 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2166 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2168 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2169 ++vcpu
->stat
.request_irq_exits
;
2172 if (!need_resched()) {
2173 ++vcpu
->stat
.light_exits
;
2184 post_kvm_run_save(vcpu
, kvm_run
);
2190 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2197 if (unlikely(vcpu
->mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2198 kvm_vcpu_block(vcpu
);
2203 if (vcpu
->sigset_active
)
2204 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2206 /* re-sync apic's tpr */
2207 if (!irqchip_in_kernel(vcpu
->kvm
))
2208 set_cr8(vcpu
, kvm_run
->cr8
);
2210 if (vcpu
->pio
.cur_count
) {
2211 r
= complete_pio(vcpu
);
2216 if (vcpu
->mmio_needed
) {
2217 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2218 vcpu
->mmio_read_completed
= 1;
2219 vcpu
->mmio_needed
= 0;
2220 r
= emulate_instruction(vcpu
, kvm_run
,
2221 vcpu
->mmio_fault_cr2
, 0, 1);
2222 if (r
== EMULATE_DO_MMIO
) {
2224 * Read-modify-write. Back to userspace.
2231 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2232 kvm_x86_ops
->cache_regs(vcpu
);
2233 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2234 kvm_x86_ops
->decache_regs(vcpu
);
2237 r
= __vcpu_run(vcpu
, kvm_run
);
2240 if (vcpu
->sigset_active
)
2241 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2247 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
2248 struct kvm_regs
*regs
)
2252 kvm_x86_ops
->cache_regs(vcpu
);
2254 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
2255 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
2256 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
2257 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
2258 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
2259 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
2260 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
2261 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
2262 #ifdef CONFIG_X86_64
2263 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
2264 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
2265 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
2266 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
2267 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
2268 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
2269 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
2270 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
2273 regs
->rip
= vcpu
->rip
;
2274 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2277 * Don't leak debug flags in case they were set for guest debugging
2279 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2280 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2287 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
2288 struct kvm_regs
*regs
)
2292 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
2293 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2294 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2295 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2296 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2297 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2298 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2299 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2300 #ifdef CONFIG_X86_64
2301 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
2302 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
2303 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
2304 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
2305 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
2306 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
2307 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
2308 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
2311 vcpu
->rip
= regs
->rip
;
2312 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2314 kvm_x86_ops
->decache_regs(vcpu
);
2321 static void get_segment(struct kvm_vcpu
*vcpu
,
2322 struct kvm_segment
*var
, int seg
)
2324 return kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2327 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2328 struct kvm_sregs
*sregs
)
2330 struct descriptor_table dt
;
2335 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2336 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2337 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2338 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2339 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2340 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2342 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2343 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2345 kvm_x86_ops
->get_idt(vcpu
, &dt
);
2346 sregs
->idt
.limit
= dt
.limit
;
2347 sregs
->idt
.base
= dt
.base
;
2348 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
2349 sregs
->gdt
.limit
= dt
.limit
;
2350 sregs
->gdt
.base
= dt
.base
;
2352 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2353 sregs
->cr0
= vcpu
->cr0
;
2354 sregs
->cr2
= vcpu
->cr2
;
2355 sregs
->cr3
= vcpu
->cr3
;
2356 sregs
->cr4
= vcpu
->cr4
;
2357 sregs
->cr8
= get_cr8(vcpu
);
2358 sregs
->efer
= vcpu
->shadow_efer
;
2359 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
2361 if (irqchip_in_kernel(vcpu
->kvm
)) {
2362 memset(sregs
->interrupt_bitmap
, 0,
2363 sizeof sregs
->interrupt_bitmap
);
2364 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
2365 if (pending_vec
>= 0)
2366 set_bit(pending_vec
, (unsigned long *)sregs
->interrupt_bitmap
);
2368 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
2369 sizeof sregs
->interrupt_bitmap
);
2376 static void set_segment(struct kvm_vcpu
*vcpu
,
2377 struct kvm_segment
*var
, int seg
)
2379 return kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
2382 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2383 struct kvm_sregs
*sregs
)
2385 int mmu_reset_needed
= 0;
2386 int i
, pending_vec
, max_bits
;
2387 struct descriptor_table dt
;
2391 dt
.limit
= sregs
->idt
.limit
;
2392 dt
.base
= sregs
->idt
.base
;
2393 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2394 dt
.limit
= sregs
->gdt
.limit
;
2395 dt
.base
= sregs
->gdt
.base
;
2396 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2398 vcpu
->cr2
= sregs
->cr2
;
2399 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2400 vcpu
->cr3
= sregs
->cr3
;
2402 set_cr8(vcpu
, sregs
->cr8
);
2404 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2405 #ifdef CONFIG_X86_64
2406 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
2408 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
2410 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2412 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2413 vcpu
->cr0
= sregs
->cr0
;
2414 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
2416 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2417 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
2418 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2419 load_pdptrs(vcpu
, vcpu
->cr3
);
2421 if (mmu_reset_needed
)
2422 kvm_mmu_reset_context(vcpu
);
2424 if (!irqchip_in_kernel(vcpu
->kvm
)) {
2425 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2426 sizeof vcpu
->irq_pending
);
2427 vcpu
->irq_summary
= 0;
2428 for (i
= 0; i
< ARRAY_SIZE(vcpu
->irq_pending
); ++i
)
2429 if (vcpu
->irq_pending
[i
])
2430 __set_bit(i
, &vcpu
->irq_summary
);
2432 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
2433 pending_vec
= find_first_bit(
2434 (const unsigned long *)sregs
->interrupt_bitmap
,
2436 /* Only pending external irq is handled here */
2437 if (pending_vec
< max_bits
) {
2438 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
2439 printk("Set back pending irq %d\n", pending_vec
);
2443 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2444 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2445 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2446 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2447 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2448 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2450 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2451 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2458 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2460 struct kvm_segment cs
;
2462 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2466 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
2469 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2470 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2472 * This list is modified at module load time to reflect the
2473 * capabilities of the host cpu.
2475 static u32 msrs_to_save
[] = {
2476 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
2478 #ifdef CONFIG_X86_64
2479 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
2481 MSR_IA32_TIME_STAMP_COUNTER
,
2484 static unsigned num_msrs_to_save
;
2486 static u32 emulated_msrs
[] = {
2487 MSR_IA32_MISC_ENABLE
,
2490 static __init
void kvm_init_msr_list(void)
2495 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2496 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2499 msrs_to_save
[j
] = msrs_to_save
[i
];
2502 num_msrs_to_save
= j
;
2506 * Adapt set_msr() to msr_io()'s calling convention
2508 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
2510 return kvm_set_msr(vcpu
, index
, *data
);
2514 * Read or write a bunch of msrs. All parameters are kernel addresses.
2516 * @return number of msrs set successfully.
2518 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
2519 struct kvm_msr_entry
*entries
,
2520 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2521 unsigned index
, u64
*data
))
2527 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
2528 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
2537 * Read or write a bunch of msrs. Parameters are user addresses.
2539 * @return number of msrs set successfully.
2541 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
2542 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2543 unsigned index
, u64
*data
),
2546 struct kvm_msrs msrs
;
2547 struct kvm_msr_entry
*entries
;
2552 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
2556 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
2560 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
2561 entries
= vmalloc(size
);
2566 if (copy_from_user(entries
, user_msrs
->entries
, size
))
2569 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
2574 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
2586 * Translate a guest virtual address to a guest physical address.
2588 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2589 struct kvm_translation
*tr
)
2591 unsigned long vaddr
= tr
->linear_address
;
2595 mutex_lock(&vcpu
->kvm
->lock
);
2596 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2597 tr
->physical_address
= gpa
;
2598 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2601 mutex_unlock(&vcpu
->kvm
->lock
);
2607 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2608 struct kvm_interrupt
*irq
)
2610 if (irq
->irq
< 0 || irq
->irq
>= 256)
2612 if (irqchip_in_kernel(vcpu
->kvm
))
2616 set_bit(irq
->irq
, vcpu
->irq_pending
);
2617 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
2624 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2625 struct kvm_debug_guest
*dbg
)
2631 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
2638 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
2639 unsigned long address
,
2642 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
2643 unsigned long pgoff
;
2646 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2648 page
= virt_to_page(vcpu
->run
);
2649 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
2650 page
= virt_to_page(vcpu
->pio_data
);
2652 return NOPAGE_SIGBUS
;
2655 *type
= VM_FAULT_MINOR
;
2660 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
2661 .nopage
= kvm_vcpu_nopage
,
2664 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2666 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
2670 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
2672 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2674 fput(vcpu
->kvm
->filp
);
2678 static struct file_operations kvm_vcpu_fops
= {
2679 .release
= kvm_vcpu_release
,
2680 .unlocked_ioctl
= kvm_vcpu_ioctl
,
2681 .compat_ioctl
= kvm_vcpu_ioctl
,
2682 .mmap
= kvm_vcpu_mmap
,
2686 * Allocates an inode for the vcpu.
2688 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
2691 struct inode
*inode
;
2694 r
= anon_inode_getfd(&fd
, &inode
, &file
,
2695 "kvm-vcpu", &kvm_vcpu_fops
, vcpu
);
2698 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
2703 * Creates some virtual cpus. Good luck creating more than one.
2705 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
2708 struct kvm_vcpu
*vcpu
;
2713 vcpu
= kvm_x86_ops
->vcpu_create(kvm
, n
);
2715 return PTR_ERR(vcpu
);
2717 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
2719 /* We do fxsave: this must be aligned. */
2720 BUG_ON((unsigned long)&vcpu
->host_fx_image
& 0xF);
2723 r
= kvm_mmu_setup(vcpu
);
2728 mutex_lock(&kvm
->lock
);
2729 if (kvm
->vcpus
[n
]) {
2731 mutex_unlock(&kvm
->lock
);
2734 kvm
->vcpus
[n
] = vcpu
;
2735 mutex_unlock(&kvm
->lock
);
2737 /* Now it's all set up, let userspace reach it */
2738 r
= create_vcpu_fd(vcpu
);
2744 mutex_lock(&kvm
->lock
);
2745 kvm
->vcpus
[n
] = NULL
;
2746 mutex_unlock(&kvm
->lock
);
2750 kvm_mmu_unload(vcpu
);
2754 kvm_x86_ops
->vcpu_free(vcpu
);
2758 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
2762 struct kvm_cpuid_entry
*e
, *entry
;
2764 rdmsrl(MSR_EFER
, efer
);
2766 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
2767 e
= &vcpu
->cpuid_entries
[i
];
2768 if (e
->function
== 0x80000001) {
2773 if (entry
&& (entry
->edx
& (1 << 20)) && !(efer
& EFER_NX
)) {
2774 entry
->edx
&= ~(1 << 20);
2775 printk(KERN_INFO
"kvm: guest NX capability removed\n");
2779 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
2780 struct kvm_cpuid
*cpuid
,
2781 struct kvm_cpuid_entry __user
*entries
)
2786 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2789 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
2790 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
2792 vcpu
->cpuid_nent
= cpuid
->nent
;
2793 cpuid_fix_nx_cap(vcpu
);
2800 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
2803 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
2804 vcpu
->sigset_active
= 1;
2805 vcpu
->sigset
= *sigset
;
2807 vcpu
->sigset_active
= 0;
2812 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2813 * we have asm/x86/processor.h
2824 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2825 #ifdef CONFIG_X86_64
2826 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2828 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2832 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2834 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->guest_fx_image
;
2838 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2839 fpu
->fcw
= fxsave
->cwd
;
2840 fpu
->fsw
= fxsave
->swd
;
2841 fpu
->ftwx
= fxsave
->twd
;
2842 fpu
->last_opcode
= fxsave
->fop
;
2843 fpu
->last_ip
= fxsave
->rip
;
2844 fpu
->last_dp
= fxsave
->rdp
;
2845 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2852 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2854 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->guest_fx_image
;
2858 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2859 fxsave
->cwd
= fpu
->fcw
;
2860 fxsave
->swd
= fpu
->fsw
;
2861 fxsave
->twd
= fpu
->ftwx
;
2862 fxsave
->fop
= fpu
->last_opcode
;
2863 fxsave
->rip
= fpu
->last_ip
;
2864 fxsave
->rdp
= fpu
->last_dp
;
2865 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2872 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
2873 struct kvm_lapic_state
*s
)
2876 memcpy(s
->regs
, vcpu
->apic
->regs
, sizeof *s
);
2882 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
2883 struct kvm_lapic_state
*s
)
2886 memcpy(vcpu
->apic
->regs
, s
->regs
, sizeof *s
);
2887 kvm_apic_post_state_restore(vcpu
);
2893 static long kvm_vcpu_ioctl(struct file
*filp
,
2894 unsigned int ioctl
, unsigned long arg
)
2896 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2897 void __user
*argp
= (void __user
*)arg
;
2905 r
= kvm_vcpu_ioctl_run(vcpu
, vcpu
->run
);
2907 case KVM_GET_REGS
: {
2908 struct kvm_regs kvm_regs
;
2910 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2911 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2915 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2920 case KVM_SET_REGS
: {
2921 struct kvm_regs kvm_regs
;
2924 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2926 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2932 case KVM_GET_SREGS
: {
2933 struct kvm_sregs kvm_sregs
;
2935 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2936 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2940 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2945 case KVM_SET_SREGS
: {
2946 struct kvm_sregs kvm_sregs
;
2949 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2951 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2957 case KVM_TRANSLATE
: {
2958 struct kvm_translation tr
;
2961 if (copy_from_user(&tr
, argp
, sizeof tr
))
2963 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2967 if (copy_to_user(argp
, &tr
, sizeof tr
))
2972 case KVM_INTERRUPT
: {
2973 struct kvm_interrupt irq
;
2976 if (copy_from_user(&irq
, argp
, sizeof irq
))
2978 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2984 case KVM_DEBUG_GUEST
: {
2985 struct kvm_debug_guest dbg
;
2988 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2990 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2997 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
3000 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
3002 case KVM_SET_CPUID
: {
3003 struct kvm_cpuid __user
*cpuid_arg
= argp
;
3004 struct kvm_cpuid cpuid
;
3007 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
3009 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
3014 case KVM_SET_SIGNAL_MASK
: {
3015 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
3016 struct kvm_signal_mask kvm_sigmask
;
3017 sigset_t sigset
, *p
;
3022 if (copy_from_user(&kvm_sigmask
, argp
,
3023 sizeof kvm_sigmask
))
3026 if (kvm_sigmask
.len
!= sizeof sigset
)
3029 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
3034 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
3040 memset(&fpu
, 0, sizeof fpu
);
3041 r
= kvm_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
3045 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
3054 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
3056 r
= kvm_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
3062 case KVM_GET_LAPIC
: {
3063 struct kvm_lapic_state lapic
;
3065 memset(&lapic
, 0, sizeof lapic
);
3066 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
3070 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
3075 case KVM_SET_LAPIC
: {
3076 struct kvm_lapic_state lapic
;
3079 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
3081 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
3094 static long kvm_vm_ioctl(struct file
*filp
,
3095 unsigned int ioctl
, unsigned long arg
)
3097 struct kvm
*kvm
= filp
->private_data
;
3098 void __user
*argp
= (void __user
*)arg
;
3102 case KVM_CREATE_VCPU
:
3103 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
3107 case KVM_SET_MEMORY_REGION
: {
3108 struct kvm_memory_region kvm_mem
;
3111 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
3113 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
3118 case KVM_SET_NR_MMU_PAGES
:
3119 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
3123 case KVM_GET_NR_MMU_PAGES
:
3124 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
3126 case KVM_GET_DIRTY_LOG
: {
3127 struct kvm_dirty_log log
;
3130 if (copy_from_user(&log
, argp
, sizeof log
))
3132 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
3137 case KVM_SET_MEMORY_ALIAS
: {
3138 struct kvm_memory_alias alias
;
3141 if (copy_from_user(&alias
, argp
, sizeof alias
))
3143 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
3148 case KVM_CREATE_IRQCHIP
:
3150 kvm
->vpic
= kvm_create_pic(kvm
);
3152 r
= kvm_ioapic_init(kvm
);
3162 case KVM_IRQ_LINE
: {
3163 struct kvm_irq_level irq_event
;
3166 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
3168 if (irqchip_in_kernel(kvm
)) {
3169 mutex_lock(&kvm
->lock
);
3170 if (irq_event
.irq
< 16)
3171 kvm_pic_set_irq(pic_irqchip(kvm
),
3174 kvm_ioapic_set_irq(kvm
->vioapic
,
3177 mutex_unlock(&kvm
->lock
);
3182 case KVM_GET_IRQCHIP
: {
3183 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3184 struct kvm_irqchip chip
;
3187 if (copy_from_user(&chip
, argp
, sizeof chip
))
3190 if (!irqchip_in_kernel(kvm
))
3192 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
3196 if (copy_to_user(argp
, &chip
, sizeof chip
))
3201 case KVM_SET_IRQCHIP
: {
3202 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3203 struct kvm_irqchip chip
;
3206 if (copy_from_user(&chip
, argp
, sizeof chip
))
3209 if (!irqchip_in_kernel(kvm
))
3211 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
3224 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
3225 unsigned long address
,
3228 struct kvm
*kvm
= vma
->vm_file
->private_data
;
3229 unsigned long pgoff
;
3232 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
3233 page
= gfn_to_page(kvm
, pgoff
);
3235 return NOPAGE_SIGBUS
;
3238 *type
= VM_FAULT_MINOR
;
3243 static struct vm_operations_struct kvm_vm_vm_ops
= {
3244 .nopage
= kvm_vm_nopage
,
3247 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3249 vma
->vm_ops
= &kvm_vm_vm_ops
;
3253 static struct file_operations kvm_vm_fops
= {
3254 .release
= kvm_vm_release
,
3255 .unlocked_ioctl
= kvm_vm_ioctl
,
3256 .compat_ioctl
= kvm_vm_ioctl
,
3257 .mmap
= kvm_vm_mmap
,
3260 static int kvm_dev_ioctl_create_vm(void)
3263 struct inode
*inode
;
3267 kvm
= kvm_create_vm();
3269 return PTR_ERR(kvm
);
3270 r
= anon_inode_getfd(&fd
, &inode
, &file
, "kvm-vm", &kvm_vm_fops
, kvm
);
3272 kvm_destroy_vm(kvm
);
3281 static long kvm_dev_ioctl(struct file
*filp
,
3282 unsigned int ioctl
, unsigned long arg
)
3284 void __user
*argp
= (void __user
*)arg
;
3288 case KVM_GET_API_VERSION
:
3292 r
= KVM_API_VERSION
;
3298 r
= kvm_dev_ioctl_create_vm();
3300 case KVM_GET_MSR_INDEX_LIST
: {
3301 struct kvm_msr_list __user
*user_msr_list
= argp
;
3302 struct kvm_msr_list msr_list
;
3306 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
3309 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
3310 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
3313 if (n
< num_msrs_to_save
)
3316 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
3317 num_msrs_to_save
* sizeof(u32
)))
3319 if (copy_to_user(user_msr_list
->indices
3320 + num_msrs_to_save
* sizeof(u32
),
3322 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
3327 case KVM_CHECK_EXTENSION
: {
3328 int ext
= (long)argp
;
3331 case KVM_CAP_IRQCHIP
:
3333 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
3342 case KVM_GET_VCPU_MMAP_SIZE
:
3355 static struct file_operations kvm_chardev_ops
= {
3356 .unlocked_ioctl
= kvm_dev_ioctl
,
3357 .compat_ioctl
= kvm_dev_ioctl
,
3360 static struct miscdevice kvm_dev
= {
3367 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3370 static void decache_vcpus_on_cpu(int cpu
)
3373 struct kvm_vcpu
*vcpu
;
3376 spin_lock(&kvm_lock
);
3377 list_for_each_entry(vm
, &vm_list
, vm_list
)
3378 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3379 vcpu
= vm
->vcpus
[i
];
3383 * If the vcpu is locked, then it is running on some
3384 * other cpu and therefore it is not cached on the
3387 * If it's not locked, check the last cpu it executed
3390 if (mutex_trylock(&vcpu
->mutex
)) {
3391 if (vcpu
->cpu
== cpu
) {
3392 kvm_x86_ops
->vcpu_decache(vcpu
);
3395 mutex_unlock(&vcpu
->mutex
);
3398 spin_unlock(&kvm_lock
);
3401 static void hardware_enable(void *junk
)
3403 int cpu
= raw_smp_processor_id();
3405 if (cpu_isset(cpu
, cpus_hardware_enabled
))
3407 cpu_set(cpu
, cpus_hardware_enabled
);
3408 kvm_x86_ops
->hardware_enable(NULL
);
3411 static void hardware_disable(void *junk
)
3413 int cpu
= raw_smp_processor_id();
3415 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
3417 cpu_clear(cpu
, cpus_hardware_enabled
);
3418 decache_vcpus_on_cpu(cpu
);
3419 kvm_x86_ops
->hardware_disable(NULL
);
3422 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
3429 case CPU_DYING_FROZEN
:
3430 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3432 hardware_disable(NULL
);
3434 case CPU_UP_CANCELED
:
3435 case CPU_UP_CANCELED_FROZEN
:
3436 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3438 smp_call_function_single(cpu
, hardware_disable
, NULL
, 0, 1);
3441 case CPU_ONLINE_FROZEN
:
3442 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
3444 smp_call_function_single(cpu
, hardware_enable
, NULL
, 0, 1);
3450 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
3453 if (val
== SYS_RESTART
) {
3455 * Some (well, at least mine) BIOSes hang on reboot if
3458 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
3459 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3464 static struct notifier_block kvm_reboot_notifier
= {
3465 .notifier_call
= kvm_reboot
,
3469 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
3471 memset(bus
, 0, sizeof(*bus
));
3474 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
3478 for (i
= 0; i
< bus
->dev_count
; i
++) {
3479 struct kvm_io_device
*pos
= bus
->devs
[i
];
3481 kvm_iodevice_destructor(pos
);
3485 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
3489 for (i
= 0; i
< bus
->dev_count
; i
++) {
3490 struct kvm_io_device
*pos
= bus
->devs
[i
];
3492 if (pos
->in_range(pos
, addr
))
3499 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
3501 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
3503 bus
->devs
[bus
->dev_count
++] = dev
;
3506 static struct notifier_block kvm_cpu_notifier
= {
3507 .notifier_call
= kvm_cpu_hotplug
,
3508 .priority
= 20, /* must be > scheduler priority */
3511 static u64
stat_get(void *_offset
)
3513 unsigned offset
= (long)_offset
;
3516 struct kvm_vcpu
*vcpu
;
3519 spin_lock(&kvm_lock
);
3520 list_for_each_entry(kvm
, &vm_list
, vm_list
)
3521 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3522 vcpu
= kvm
->vcpus
[i
];
3524 total
+= *(u32
*)((void *)vcpu
+ offset
);
3526 spin_unlock(&kvm_lock
);
3530 DEFINE_SIMPLE_ATTRIBUTE(stat_fops
, stat_get
, NULL
, "%llu\n");
3532 static __init
void kvm_init_debug(void)
3534 struct kvm_stats_debugfs_item
*p
;
3536 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
3537 for (p
= debugfs_entries
; p
->name
; ++p
)
3538 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
3539 (void *)(long)p
->offset
,
3543 static void kvm_exit_debug(void)
3545 struct kvm_stats_debugfs_item
*p
;
3547 for (p
= debugfs_entries
; p
->name
; ++p
)
3548 debugfs_remove(p
->dentry
);
3549 debugfs_remove(debugfs_dir
);
3552 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
3554 hardware_disable(NULL
);
3558 static int kvm_resume(struct sys_device
*dev
)
3560 hardware_enable(NULL
);
3564 static struct sysdev_class kvm_sysdev_class
= {
3566 .suspend
= kvm_suspend
,
3567 .resume
= kvm_resume
,
3570 static struct sys_device kvm_sysdev
= {
3572 .cls
= &kvm_sysdev_class
,
3575 hpa_t bad_page_address
;
3578 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
3580 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
3583 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
3585 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
3587 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
3590 static void kvm_sched_out(struct preempt_notifier
*pn
,
3591 struct task_struct
*next
)
3593 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
3595 kvm_x86_ops
->vcpu_put(vcpu
);
3598 int kvm_init_x86(struct kvm_x86_ops
*ops
, unsigned int vcpu_size
,
3599 struct module
*module
)
3605 printk(KERN_ERR
"kvm: already loaded the other module\n");
3609 if (!ops
->cpu_has_kvm_support()) {
3610 printk(KERN_ERR
"kvm: no hardware support\n");
3613 if (ops
->disabled_by_bios()) {
3614 printk(KERN_ERR
"kvm: disabled by bios\n");
3620 r
= kvm_x86_ops
->hardware_setup();
3624 for_each_online_cpu(cpu
) {
3625 smp_call_function_single(cpu
,
3626 kvm_x86_ops
->check_processor_compatibility
,
3632 on_each_cpu(hardware_enable
, NULL
, 0, 1);
3633 r
= register_cpu_notifier(&kvm_cpu_notifier
);
3636 register_reboot_notifier(&kvm_reboot_notifier
);
3638 r
= sysdev_class_register(&kvm_sysdev_class
);
3642 r
= sysdev_register(&kvm_sysdev
);
3646 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3647 kvm_vcpu_cache
= kmem_cache_create("kvm_vcpu", vcpu_size
,
3648 __alignof__(struct kvm_vcpu
), 0, 0);
3649 if (!kvm_vcpu_cache
) {
3654 kvm_chardev_ops
.owner
= module
;
3656 r
= misc_register(&kvm_dev
);
3658 printk (KERN_ERR
"kvm: misc device register failed\n");
3662 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
3663 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
3665 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3670 kmem_cache_destroy(kvm_vcpu_cache
);
3672 sysdev_unregister(&kvm_sysdev
);
3674 sysdev_class_unregister(&kvm_sysdev_class
);
3676 unregister_reboot_notifier(&kvm_reboot_notifier
);
3677 unregister_cpu_notifier(&kvm_cpu_notifier
);
3679 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3681 kvm_x86_ops
->hardware_unsetup();
3687 void kvm_exit_x86(void)
3689 misc_deregister(&kvm_dev
);
3690 kmem_cache_destroy(kvm_vcpu_cache
);
3691 sysdev_unregister(&kvm_sysdev
);
3692 sysdev_class_unregister(&kvm_sysdev_class
);
3693 unregister_reboot_notifier(&kvm_reboot_notifier
);
3694 unregister_cpu_notifier(&kvm_cpu_notifier
);
3695 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3696 kvm_x86_ops
->hardware_unsetup();
3700 static __init
int kvm_init(void)
3702 static struct page
*bad_page
;
3705 r
= kvm_mmu_module_init();
3711 kvm_init_msr_list();
3713 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
3718 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
3719 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
3725 kvm_mmu_module_exit();
3730 static __exit
void kvm_exit(void)
3733 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
3734 kvm_mmu_module_exit();
3737 module_init(kvm_init
)
3738 module_exit(kvm_exit
)
3740 EXPORT_SYMBOL_GPL(kvm_init_x86
);
3741 EXPORT_SYMBOL_GPL(kvm_exit_x86
);