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.
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>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <asm/uaccess.h>
31 #include <linux/reboot.h>
33 #include <linux/debugfs.h>
34 #include <linux/highmem.h>
35 #include <linux/file.h>
38 #include "x86_emulate.h"
39 #include "segment_descriptor.h"
41 MODULE_AUTHOR("Qumranet");
42 MODULE_LICENSE("GPL");
44 struct kvm_arch_ops
*kvm_arch_ops
;
45 struct kvm_stat kvm_stat
;
46 EXPORT_SYMBOL_GPL(kvm_stat
);
48 static struct kvm_stats_debugfs_item
{
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_window", &kvm_stat
.irq_window_exits
},
62 { "halt_exits", &kvm_stat
.halt_exits
},
63 { "request_irq", &kvm_stat
.request_irq_exits
},
64 { "irq_exits", &kvm_stat
.irq_exits
},
68 static struct dentry
*debugfs_dir
;
70 #define MAX_IO_MSRS 256
72 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
73 #define LMSW_GUEST_MASK 0x0eULL
74 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
75 #define CR8_RESEVED_BITS (~0x0fULL)
76 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
79 // LDT or TSS descriptor in the GDT. 16 bytes.
80 struct segment_descriptor_64
{
81 struct segment_descriptor s
;
88 unsigned long segment_base(u16 selector
)
90 struct descriptor_table gdt
;
91 struct segment_descriptor
*d
;
92 unsigned long table_base
;
93 typedef unsigned long ul
;
99 asm ("sgdt %0" : "=m"(gdt
));
100 table_base
= gdt
.base
;
102 if (selector
& 4) { /* from ldt */
105 asm ("sldt %0" : "=g"(ldt_selector
));
106 table_base
= segment_base(ldt_selector
);
108 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
109 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
112 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
113 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
117 EXPORT_SYMBOL_GPL(segment_base
);
119 static inline int valid_vcpu(int n
)
121 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
124 int kvm_read_guest(struct kvm_vcpu
*vcpu
,
129 unsigned char *host_buf
= dest
;
130 unsigned long req_size
= size
;
138 paddr
= gva_to_hpa(vcpu
, addr
);
140 if (is_error_hpa(paddr
))
143 guest_buf
= (hva_t
)kmap_atomic(
144 pfn_to_page(paddr
>> PAGE_SHIFT
),
146 offset
= addr
& ~PAGE_MASK
;
148 now
= min(size
, PAGE_SIZE
- offset
);
149 memcpy(host_buf
, (void*)guest_buf
, now
);
153 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
155 return req_size
- size
;
157 EXPORT_SYMBOL_GPL(kvm_read_guest
);
159 int kvm_write_guest(struct kvm_vcpu
*vcpu
,
164 unsigned char *host_buf
= data
;
165 unsigned long req_size
= size
;
173 paddr
= gva_to_hpa(vcpu
, addr
);
175 if (is_error_hpa(paddr
))
178 guest_buf
= (hva_t
)kmap_atomic(
179 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
180 offset
= addr
& ~PAGE_MASK
;
182 now
= min(size
, PAGE_SIZE
- offset
);
183 memcpy((void*)guest_buf
, host_buf
, now
);
187 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
189 return req_size
- size
;
191 EXPORT_SYMBOL_GPL(kvm_write_guest
);
193 static int vcpu_slot(struct kvm_vcpu
*vcpu
)
195 return vcpu
- vcpu
->kvm
->vcpus
;
199 * Switches to specified vcpu, until a matching vcpu_put()
201 static struct kvm_vcpu
*vcpu_load(struct kvm
*kvm
, int vcpu_slot
)
203 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[vcpu_slot
];
205 mutex_lock(&vcpu
->mutex
);
206 if (unlikely(!vcpu
->vmcs
)) {
207 mutex_unlock(&vcpu
->mutex
);
210 return kvm_arch_ops
->vcpu_load(vcpu
);
213 static void vcpu_put(struct kvm_vcpu
*vcpu
)
215 kvm_arch_ops
->vcpu_put(vcpu
);
216 mutex_unlock(&vcpu
->mutex
);
219 static int kvm_dev_open(struct inode
*inode
, struct file
*filp
)
221 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
227 spin_lock_init(&kvm
->lock
);
228 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
229 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
230 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[i
];
232 mutex_init(&vcpu
->mutex
);
233 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
234 INIT_LIST_HEAD(&vcpu
->free_pages
);
236 filp
->private_data
= kvm
;
241 * Free any memory in @free but not in @dont.
243 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
244 struct kvm_memory_slot
*dont
)
248 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
249 if (free
->phys_mem
) {
250 for (i
= 0; i
< free
->npages
; ++i
)
251 if (free
->phys_mem
[i
])
252 __free_page(free
->phys_mem
[i
]);
253 vfree(free
->phys_mem
);
256 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
257 vfree(free
->dirty_bitmap
);
261 free
->dirty_bitmap
= 0;
264 static void kvm_free_physmem(struct kvm
*kvm
)
268 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
269 kvm_free_physmem_slot(&kvm
->memslots
[i
], 0);
272 static void kvm_free_vcpu(struct kvm_vcpu
*vcpu
)
274 kvm_arch_ops
->vcpu_free(vcpu
);
275 kvm_mmu_destroy(vcpu
);
278 static void kvm_free_vcpus(struct kvm
*kvm
)
282 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
283 kvm_free_vcpu(&kvm
->vcpus
[i
]);
286 static int kvm_dev_release(struct inode
*inode
, struct file
*filp
)
288 struct kvm
*kvm
= filp
->private_data
;
291 kvm_free_physmem(kvm
);
296 static void inject_gp(struct kvm_vcpu
*vcpu
)
298 kvm_arch_ops
->inject_gp(vcpu
, 0);
301 static int pdptrs_have_reserved_bits_set(struct kvm_vcpu
*vcpu
,
304 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
305 unsigned offset
= (cr3
& (PAGE_SIZE
-1)) >> 5;
309 struct kvm_memory_slot
*memslot
;
311 spin_lock(&vcpu
->kvm
->lock
);
312 memslot
= gfn_to_memslot(vcpu
->kvm
, pdpt_gfn
);
313 /* FIXME: !memslot - emulate? 0xff? */
314 pdpt
= kmap_atomic(gfn_to_page(memslot
, pdpt_gfn
), KM_USER0
);
316 for (i
= 0; i
< 4; ++i
) {
317 pdpte
= pdpt
[offset
+ i
];
318 if ((pdpte
& 1) && (pdpte
& 0xfffffff0000001e6ull
))
322 kunmap_atomic(pdpt
, KM_USER0
);
323 spin_unlock(&vcpu
->kvm
->lock
);
328 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
330 if (cr0
& CR0_RESEVED_BITS
) {
331 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
337 if ((cr0
& CR0_NW_MASK
) && !(cr0
& CR0_CD_MASK
)) {
338 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
343 if ((cr0
& CR0_PG_MASK
) && !(cr0
& CR0_PE_MASK
)) {
344 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
345 "and a clear PE flag\n");
350 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
)) {
352 if ((vcpu
->shadow_efer
& EFER_LME
)) {
356 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
357 "in long mode while PAE is disabled\n");
361 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
363 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
364 "in long mode while CS.L == 1\n");
372 pdptrs_have_reserved_bits_set(vcpu
, vcpu
->cr3
)) {
373 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
381 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
384 spin_lock(&vcpu
->kvm
->lock
);
385 kvm_mmu_reset_context(vcpu
);
386 spin_unlock(&vcpu
->kvm
->lock
);
389 EXPORT_SYMBOL_GPL(set_cr0
);
391 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
393 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
394 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
396 EXPORT_SYMBOL_GPL(lmsw
);
398 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
400 if (cr4
& CR4_RESEVED_BITS
) {
401 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
406 if (is_long_mode(vcpu
)) {
407 if (!(cr4
& CR4_PAE_MASK
)) {
408 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
413 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& CR4_PAE_MASK
)
414 && pdptrs_have_reserved_bits_set(vcpu
, vcpu
->cr3
)) {
415 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
419 if (cr4
& CR4_VMXE_MASK
) {
420 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
424 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
425 spin_lock(&vcpu
->kvm
->lock
);
426 kvm_mmu_reset_context(vcpu
);
427 spin_unlock(&vcpu
->kvm
->lock
);
429 EXPORT_SYMBOL_GPL(set_cr4
);
431 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
433 if (is_long_mode(vcpu
)) {
434 if ( cr3
& CR3_L_MODE_RESEVED_BITS
) {
435 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
440 if (cr3
& CR3_RESEVED_BITS
) {
441 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
445 if (is_paging(vcpu
) && is_pae(vcpu
) &&
446 pdptrs_have_reserved_bits_set(vcpu
, cr3
)) {
447 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
455 spin_lock(&vcpu
->kvm
->lock
);
456 vcpu
->mmu
.new_cr3(vcpu
);
457 spin_unlock(&vcpu
->kvm
->lock
);
459 EXPORT_SYMBOL_GPL(set_cr3
);
461 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
463 if ( cr8
& CR8_RESEVED_BITS
) {
464 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
470 EXPORT_SYMBOL_GPL(set_cr8
);
472 void fx_init(struct kvm_vcpu
*vcpu
)
474 struct __attribute__ ((__packed__
)) fx_image_s
{
480 u64 operand
;// fpu dp
486 fx_save(vcpu
->host_fx_image
);
488 fx_save(vcpu
->guest_fx_image
);
489 fx_restore(vcpu
->host_fx_image
);
491 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
492 fx_image
->mxcsr
= 0x1f80;
493 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
494 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
496 EXPORT_SYMBOL_GPL(fx_init
);
499 * Creates some virtual cpus. Good luck creating more than one.
501 static int kvm_dev_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
504 struct kvm_vcpu
*vcpu
;
510 vcpu
= &kvm
->vcpus
[n
];
512 mutex_lock(&vcpu
->mutex
);
515 mutex_unlock(&vcpu
->mutex
);
519 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
521 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
523 vcpu
->cpu
= -1; /* First load will set up TR */
525 r
= kvm_arch_ops
->vcpu_create(vcpu
);
529 r
= kvm_mmu_create(vcpu
);
533 kvm_arch_ops
->vcpu_load(vcpu
);
534 r
= kvm_mmu_setup(vcpu
);
536 r
= kvm_arch_ops
->vcpu_setup(vcpu
);
546 mutex_unlock(&vcpu
->mutex
);
552 * Allocate some memory and give it an address in the guest physical address
555 * Discontiguous memory is allowed, mostly for framebuffers.
557 static int kvm_dev_ioctl_set_memory_region(struct kvm
*kvm
,
558 struct kvm_memory_region
*mem
)
562 unsigned long npages
;
564 struct kvm_memory_slot
*memslot
;
565 struct kvm_memory_slot old
, new;
566 int memory_config_version
;
569 /* General sanity checks */
570 if (mem
->memory_size
& (PAGE_SIZE
- 1))
572 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
574 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
576 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
579 memslot
= &kvm
->memslots
[mem
->slot
];
580 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
581 npages
= mem
->memory_size
>> PAGE_SHIFT
;
584 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
587 spin_lock(&kvm
->lock
);
589 memory_config_version
= kvm
->memory_config_version
;
590 new = old
= *memslot
;
592 new.base_gfn
= base_gfn
;
594 new.flags
= mem
->flags
;
596 /* Disallow changing a memory slot's size. */
598 if (npages
&& old
.npages
&& npages
!= old
.npages
)
601 /* Check for overlaps */
603 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
604 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
608 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
609 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
613 * Do memory allocations outside lock. memory_config_version will
616 spin_unlock(&kvm
->lock
);
618 /* Deallocate if slot is being removed */
622 /* Free page dirty bitmap if unneeded */
623 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
624 new.dirty_bitmap
= 0;
628 /* Allocate if a slot is being created */
629 if (npages
&& !new.phys_mem
) {
630 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
635 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
636 for (i
= 0; i
< npages
; ++i
) {
637 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
639 if (!new.phys_mem
[i
])
644 /* Allocate page dirty bitmap if needed */
645 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
646 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
648 new.dirty_bitmap
= vmalloc(dirty_bytes
);
649 if (!new.dirty_bitmap
)
651 memset(new.dirty_bitmap
, 0, dirty_bytes
);
654 spin_lock(&kvm
->lock
);
656 if (memory_config_version
!= kvm
->memory_config_version
) {
657 spin_unlock(&kvm
->lock
);
658 kvm_free_physmem_slot(&new, &old
);
666 if (mem
->slot
>= kvm
->nmemslots
)
667 kvm
->nmemslots
= mem
->slot
+ 1;
670 ++kvm
->memory_config_version
;
672 spin_unlock(&kvm
->lock
);
674 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
675 struct kvm_vcpu
*vcpu
;
677 vcpu
= vcpu_load(kvm
, i
);
680 kvm_mmu_reset_context(vcpu
);
684 kvm_free_physmem_slot(&old
, &new);
688 spin_unlock(&kvm
->lock
);
690 kvm_free_physmem_slot(&new, &old
);
696 * Get (and clear) the dirty memory log for a memory slot.
698 static int kvm_dev_ioctl_get_dirty_log(struct kvm
*kvm
,
699 struct kvm_dirty_log
*log
)
701 struct kvm_memory_slot
*memslot
;
704 unsigned long any
= 0;
706 spin_lock(&kvm
->lock
);
709 * Prevent changes to guest memory configuration even while the lock
713 spin_unlock(&kvm
->lock
);
715 if (log
->slot
>= KVM_MEMORY_SLOTS
)
718 memslot
= &kvm
->memslots
[log
->slot
];
720 if (!memslot
->dirty_bitmap
)
723 n
= ALIGN(memslot
->npages
, 8) / 8;
725 for (i
= 0; !any
&& i
< n
; ++i
)
726 any
= memslot
->dirty_bitmap
[i
];
729 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
734 spin_lock(&kvm
->lock
);
735 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
736 spin_unlock(&kvm
->lock
);
737 memset(memslot
->dirty_bitmap
, 0, n
);
738 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
739 struct kvm_vcpu
*vcpu
= vcpu_load(kvm
, i
);
743 kvm_arch_ops
->tlb_flush(vcpu
);
751 spin_lock(&kvm
->lock
);
753 spin_unlock(&kvm
->lock
);
757 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
761 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
762 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
764 if (gfn
>= memslot
->base_gfn
765 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
770 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
772 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
775 struct kvm_memory_slot
*memslot
= 0;
776 unsigned long rel_gfn
;
778 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
779 memslot
= &kvm
->memslots
[i
];
781 if (gfn
>= memslot
->base_gfn
782 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
784 if (!memslot
|| !memslot
->dirty_bitmap
)
787 rel_gfn
= gfn
- memslot
->base_gfn
;
790 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
791 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
797 static int emulator_read_std(unsigned long addr
,
800 struct x86_emulate_ctxt
*ctxt
)
802 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
806 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
807 unsigned offset
= addr
& (PAGE_SIZE
-1);
808 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
810 struct kvm_memory_slot
*memslot
;
813 if (gpa
== UNMAPPED_GVA
)
814 return X86EMUL_PROPAGATE_FAULT
;
815 pfn
= gpa
>> PAGE_SHIFT
;
816 memslot
= gfn_to_memslot(vcpu
->kvm
, pfn
);
818 return X86EMUL_UNHANDLEABLE
;
819 page
= kmap_atomic(gfn_to_page(memslot
, pfn
), KM_USER0
);
821 memcpy(data
, page
+ offset
, tocopy
);
823 kunmap_atomic(page
, KM_USER0
);
830 return X86EMUL_CONTINUE
;
833 static int emulator_write_std(unsigned long addr
,
836 struct x86_emulate_ctxt
*ctxt
)
838 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
840 return X86EMUL_UNHANDLEABLE
;
843 static int emulator_read_emulated(unsigned long addr
,
846 struct x86_emulate_ctxt
*ctxt
)
848 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
850 if (vcpu
->mmio_read_completed
) {
851 memcpy(val
, vcpu
->mmio_data
, bytes
);
852 vcpu
->mmio_read_completed
= 0;
853 return X86EMUL_CONTINUE
;
854 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
856 return X86EMUL_CONTINUE
;
858 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
859 if (gpa
== UNMAPPED_GVA
)
860 return vcpu_printf(vcpu
, "not present\n"), X86EMUL_PROPAGATE_FAULT
;
861 vcpu
->mmio_needed
= 1;
862 vcpu
->mmio_phys_addr
= gpa
;
863 vcpu
->mmio_size
= bytes
;
864 vcpu
->mmio_is_write
= 0;
866 return X86EMUL_UNHANDLEABLE
;
870 static int emulator_write_emulated(unsigned long addr
,
873 struct x86_emulate_ctxt
*ctxt
)
875 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
876 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
878 if (gpa
== UNMAPPED_GVA
)
879 return X86EMUL_PROPAGATE_FAULT
;
881 vcpu
->mmio_needed
= 1;
882 vcpu
->mmio_phys_addr
= gpa
;
883 vcpu
->mmio_size
= bytes
;
884 vcpu
->mmio_is_write
= 1;
885 memcpy(vcpu
->mmio_data
, &val
, bytes
);
887 return X86EMUL_CONTINUE
;
890 static int emulator_cmpxchg_emulated(unsigned long addr
,
894 struct x86_emulate_ctxt
*ctxt
)
900 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
902 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
905 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
907 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
910 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
912 spin_lock(&vcpu
->kvm
->lock
);
913 vcpu
->mmu
.inval_page(vcpu
, address
);
914 spin_unlock(&vcpu
->kvm
->lock
);
915 kvm_arch_ops
->invlpg(vcpu
, address
);
916 return X86EMUL_CONTINUE
;
919 int emulate_clts(struct kvm_vcpu
*vcpu
)
923 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
924 cr0
= vcpu
->cr0
& ~CR0_TS_MASK
;
925 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
926 return X86EMUL_CONTINUE
;
929 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
931 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
935 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
936 return X86EMUL_CONTINUE
;
938 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
940 return X86EMUL_UNHANDLEABLE
;
944 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
946 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
949 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
951 /* FIXME: better handling */
952 return X86EMUL_UNHANDLEABLE
;
954 return X86EMUL_CONTINUE
;
957 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
961 unsigned long rip
= ctxt
->vcpu
->rip
;
962 unsigned long rip_linear
;
964 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
969 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
971 printk(KERN_ERR
"emulation failed but !mmio_needed?"
972 " rip %lx %02x %02x %02x %02x\n",
973 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
977 struct x86_emulate_ops emulate_ops
= {
978 .read_std
= emulator_read_std
,
979 .write_std
= emulator_write_std
,
980 .read_emulated
= emulator_read_emulated
,
981 .write_emulated
= emulator_write_emulated
,
982 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
985 int emulate_instruction(struct kvm_vcpu
*vcpu
,
990 struct x86_emulate_ctxt emulate_ctxt
;
994 kvm_arch_ops
->cache_regs(vcpu
);
996 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
998 emulate_ctxt
.vcpu
= vcpu
;
999 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1000 emulate_ctxt
.cr2
= cr2
;
1001 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1002 ? X86EMUL_MODE_REAL
: cs_l
1003 ? X86EMUL_MODE_PROT64
: cs_db
1004 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1006 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1007 emulate_ctxt
.cs_base
= 0;
1008 emulate_ctxt
.ds_base
= 0;
1009 emulate_ctxt
.es_base
= 0;
1010 emulate_ctxt
.ss_base
= 0;
1012 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1013 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1014 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1015 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1018 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1019 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1021 vcpu
->mmio_is_write
= 0;
1022 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1024 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1025 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1026 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1027 run
->mmio
.len
= vcpu
->mmio_size
;
1028 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1032 if (!vcpu
->mmio_needed
) {
1033 report_emulation_failure(&emulate_ctxt
);
1034 return EMULATE_FAIL
;
1036 return EMULATE_DO_MMIO
;
1039 kvm_arch_ops
->decache_regs(vcpu
);
1040 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1042 if (vcpu
->mmio_is_write
)
1043 return EMULATE_DO_MMIO
;
1045 return EMULATE_DONE
;
1047 EXPORT_SYMBOL_GPL(emulate_instruction
);
1049 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1051 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1054 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1056 struct descriptor_table dt
= { limit
, base
};
1058 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1061 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1063 struct descriptor_table dt
= { limit
, base
};
1065 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1068 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1069 unsigned long *rflags
)
1072 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1075 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1077 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1088 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1093 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1094 unsigned long *rflags
)
1098 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1099 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1108 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1111 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1115 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1120 case 0xc0010010: /* SYSCFG */
1121 case 0xc0010015: /* HWCR */
1122 case MSR_IA32_PLATFORM_ID
:
1123 case MSR_IA32_P5_MC_ADDR
:
1124 case MSR_IA32_P5_MC_TYPE
:
1125 case MSR_IA32_MC0_CTL
:
1126 case MSR_IA32_MCG_STATUS
:
1127 case MSR_IA32_MCG_CAP
:
1128 case MSR_IA32_MC0_MISC
:
1129 case MSR_IA32_MC0_MISC
+4:
1130 case MSR_IA32_MC0_MISC
+8:
1131 case MSR_IA32_MC0_MISC
+12:
1132 case MSR_IA32_MC0_MISC
+16:
1133 case MSR_IA32_UCODE_REV
:
1134 case MSR_IA32_PERF_STATUS
:
1135 /* MTRR registers */
1137 case 0x200 ... 0x2ff:
1140 case 0xcd: /* fsb frequency */
1143 case MSR_IA32_APICBASE
:
1144 data
= vcpu
->apic_base
;
1146 #ifdef CONFIG_X86_64
1148 data
= vcpu
->shadow_efer
;
1152 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1158 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1161 * Reads an msr value (of 'msr_index') into 'pdata'.
1162 * Returns 0 on success, non-0 otherwise.
1163 * Assumes vcpu_load() was already called.
1165 static int get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1167 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1170 #ifdef CONFIG_X86_64
1172 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1174 if (efer
& EFER_RESERVED_BITS
) {
1175 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1182 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1183 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1188 kvm_arch_ops
->set_efer(vcpu
, efer
);
1191 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1193 vcpu
->shadow_efer
= efer
;
1198 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1201 #ifdef CONFIG_X86_64
1203 set_efer(vcpu
, data
);
1206 case MSR_IA32_MC0_STATUS
:
1207 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1208 __FUNCTION__
, data
);
1210 case MSR_IA32_UCODE_REV
:
1211 case MSR_IA32_UCODE_WRITE
:
1212 case 0x200 ... 0x2ff: /* MTRRs */
1214 case MSR_IA32_APICBASE
:
1215 vcpu
->apic_base
= data
;
1218 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1223 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1226 * Writes msr value into into the appropriate "register".
1227 * Returns 0 on success, non-0 otherwise.
1228 * Assumes vcpu_load() was already called.
1230 static int set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1232 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1235 void kvm_resched(struct kvm_vcpu
*vcpu
)
1239 /* Cannot fail - no vcpu unplug yet. */
1240 vcpu_load(vcpu
->kvm
, vcpu_slot(vcpu
));
1242 EXPORT_SYMBOL_GPL(kvm_resched
);
1244 void load_msrs(struct vmx_msr_entry
*e
, int n
)
1248 for (i
= 0; i
< n
; ++i
)
1249 wrmsrl(e
[i
].index
, e
[i
].data
);
1251 EXPORT_SYMBOL_GPL(load_msrs
);
1253 void save_msrs(struct vmx_msr_entry
*e
, int n
)
1257 for (i
= 0; i
< n
; ++i
)
1258 rdmsrl(e
[i
].index
, e
[i
].data
);
1260 EXPORT_SYMBOL_GPL(save_msrs
);
1262 static int kvm_dev_ioctl_run(struct kvm
*kvm
, struct kvm_run
*kvm_run
)
1264 struct kvm_vcpu
*vcpu
;
1267 if (!valid_vcpu(kvm_run
->vcpu
))
1270 vcpu
= vcpu_load(kvm
, kvm_run
->vcpu
);
1274 if (kvm_run
->emulated
) {
1275 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1276 kvm_run
->emulated
= 0;
1279 if (kvm_run
->mmio_completed
) {
1280 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1281 vcpu
->mmio_read_completed
= 1;
1284 vcpu
->mmio_needed
= 0;
1286 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1292 static int kvm_dev_ioctl_get_regs(struct kvm
*kvm
, struct kvm_regs
*regs
)
1294 struct kvm_vcpu
*vcpu
;
1296 if (!valid_vcpu(regs
->vcpu
))
1299 vcpu
= vcpu_load(kvm
, regs
->vcpu
);
1303 kvm_arch_ops
->cache_regs(vcpu
);
1305 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
1306 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
1307 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
1308 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
1309 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
1310 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
1311 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
1312 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
1313 #ifdef CONFIG_X86_64
1314 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
1315 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
1316 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
1317 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
1318 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
1319 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
1320 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
1321 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
1324 regs
->rip
= vcpu
->rip
;
1325 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1328 * Don't leak debug flags in case they were set for guest debugging
1330 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
1331 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1338 static int kvm_dev_ioctl_set_regs(struct kvm
*kvm
, struct kvm_regs
*regs
)
1340 struct kvm_vcpu
*vcpu
;
1342 if (!valid_vcpu(regs
->vcpu
))
1345 vcpu
= vcpu_load(kvm
, regs
->vcpu
);
1349 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
1350 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
1351 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
1352 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
1353 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
1354 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
1355 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
1356 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
1357 #ifdef CONFIG_X86_64
1358 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
1359 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
1360 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
1361 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
1362 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
1363 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
1364 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
1365 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
1368 vcpu
->rip
= regs
->rip
;
1369 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
1371 kvm_arch_ops
->decache_regs(vcpu
);
1378 static void get_segment(struct kvm_vcpu
*vcpu
,
1379 struct kvm_segment
*var
, int seg
)
1381 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
1384 static int kvm_dev_ioctl_get_sregs(struct kvm
*kvm
, struct kvm_sregs
*sregs
)
1386 struct kvm_vcpu
*vcpu
;
1387 struct descriptor_table dt
;
1389 if (!valid_vcpu(sregs
->vcpu
))
1391 vcpu
= vcpu_load(kvm
, sregs
->vcpu
);
1395 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
1396 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
1397 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
1398 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
1399 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
1400 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
1402 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
1403 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
1405 kvm_arch_ops
->get_idt(vcpu
, &dt
);
1406 sregs
->idt
.limit
= dt
.limit
;
1407 sregs
->idt
.base
= dt
.base
;
1408 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
1409 sregs
->gdt
.limit
= dt
.limit
;
1410 sregs
->gdt
.base
= dt
.base
;
1412 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1413 sregs
->cr0
= vcpu
->cr0
;
1414 sregs
->cr2
= vcpu
->cr2
;
1415 sregs
->cr3
= vcpu
->cr3
;
1416 sregs
->cr4
= vcpu
->cr4
;
1417 sregs
->cr8
= vcpu
->cr8
;
1418 sregs
->efer
= vcpu
->shadow_efer
;
1419 sregs
->apic_base
= vcpu
->apic_base
;
1421 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
1422 sizeof sregs
->interrupt_bitmap
);
1429 static void set_segment(struct kvm_vcpu
*vcpu
,
1430 struct kvm_segment
*var
, int seg
)
1432 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
1435 static int kvm_dev_ioctl_set_sregs(struct kvm
*kvm
, struct kvm_sregs
*sregs
)
1437 struct kvm_vcpu
*vcpu
;
1438 int mmu_reset_needed
= 0;
1440 struct descriptor_table dt
;
1442 if (!valid_vcpu(sregs
->vcpu
))
1444 vcpu
= vcpu_load(kvm
, sregs
->vcpu
);
1448 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
1449 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
1450 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
1451 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
1452 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
1453 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
1455 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
1456 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
1458 dt
.limit
= sregs
->idt
.limit
;
1459 dt
.base
= sregs
->idt
.base
;
1460 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1461 dt
.limit
= sregs
->gdt
.limit
;
1462 dt
.base
= sregs
->gdt
.base
;
1463 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1465 vcpu
->cr2
= sregs
->cr2
;
1466 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
1467 vcpu
->cr3
= sregs
->cr3
;
1469 vcpu
->cr8
= sregs
->cr8
;
1471 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
1472 #ifdef CONFIG_X86_64
1473 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
1475 vcpu
->apic_base
= sregs
->apic_base
;
1477 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1479 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
1480 kvm_arch_ops
->set_cr0_no_modeswitch(vcpu
, sregs
->cr0
);
1482 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
1483 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
1485 if (mmu_reset_needed
)
1486 kvm_mmu_reset_context(vcpu
);
1488 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
1489 sizeof vcpu
->irq_pending
);
1490 vcpu
->irq_summary
= 0;
1491 for (i
= 0; i
< NR_IRQ_WORDS
; ++i
)
1492 if (vcpu
->irq_pending
[i
])
1493 __set_bit(i
, &vcpu
->irq_summary
);
1501 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1502 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1504 * This list is modified at module load time to reflect the
1505 * capabilities of the host cpu.
1507 static u32 msrs_to_save
[] = {
1508 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
1510 #ifdef CONFIG_X86_64
1511 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
1513 MSR_IA32_TIME_STAMP_COUNTER
,
1516 static unsigned num_msrs_to_save
;
1518 static __init
void kvm_init_msr_list(void)
1523 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1524 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1527 msrs_to_save
[j
] = msrs_to_save
[i
];
1530 num_msrs_to_save
= j
;
1534 * Adapt set_msr() to msr_io()'s calling convention
1536 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
1538 return set_msr(vcpu
, index
, *data
);
1542 * Read or write a bunch of msrs. All parameters are kernel addresses.
1544 * @return number of msrs set successfully.
1546 static int __msr_io(struct kvm
*kvm
, struct kvm_msrs
*msrs
,
1547 struct kvm_msr_entry
*entries
,
1548 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1549 unsigned index
, u64
*data
))
1551 struct kvm_vcpu
*vcpu
;
1554 if (!valid_vcpu(msrs
->vcpu
))
1557 vcpu
= vcpu_load(kvm
, msrs
->vcpu
);
1561 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
1562 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
1571 * Read or write a bunch of msrs. Parameters are user addresses.
1573 * @return number of msrs set successfully.
1575 static int msr_io(struct kvm
*kvm
, struct kvm_msrs __user
*user_msrs
,
1576 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1577 unsigned index
, u64
*data
),
1580 struct kvm_msrs msrs
;
1581 struct kvm_msr_entry
*entries
;
1586 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
1590 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
1594 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
1595 entries
= vmalloc(size
);
1600 if (copy_from_user(entries
, user_msrs
->entries
, size
))
1603 r
= n
= __msr_io(kvm
, &msrs
, entries
, do_msr
);
1608 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
1620 * Translate a guest virtual address to a guest physical address.
1622 static int kvm_dev_ioctl_translate(struct kvm
*kvm
, struct kvm_translation
*tr
)
1624 unsigned long vaddr
= tr
->linear_address
;
1625 struct kvm_vcpu
*vcpu
;
1628 vcpu
= vcpu_load(kvm
, tr
->vcpu
);
1631 spin_lock(&kvm
->lock
);
1632 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
1633 tr
->physical_address
= gpa
;
1634 tr
->valid
= gpa
!= UNMAPPED_GVA
;
1637 spin_unlock(&kvm
->lock
);
1643 static int kvm_dev_ioctl_interrupt(struct kvm
*kvm
, struct kvm_interrupt
*irq
)
1645 struct kvm_vcpu
*vcpu
;
1647 if (!valid_vcpu(irq
->vcpu
))
1649 if (irq
->irq
< 0 || irq
->irq
>= 256)
1651 vcpu
= vcpu_load(kvm
, irq
->vcpu
);
1655 set_bit(irq
->irq
, vcpu
->irq_pending
);
1656 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
1663 static int kvm_dev_ioctl_debug_guest(struct kvm
*kvm
,
1664 struct kvm_debug_guest
*dbg
)
1666 struct kvm_vcpu
*vcpu
;
1669 if (!valid_vcpu(dbg
->vcpu
))
1671 vcpu
= vcpu_load(kvm
, dbg
->vcpu
);
1675 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
1682 static long kvm_dev_ioctl(struct file
*filp
,
1683 unsigned int ioctl
, unsigned long arg
)
1685 struct kvm
*kvm
= filp
->private_data
;
1689 case KVM_GET_API_VERSION
:
1690 r
= KVM_API_VERSION
;
1692 case KVM_CREATE_VCPU
: {
1693 r
= kvm_dev_ioctl_create_vcpu(kvm
, arg
);
1699 struct kvm_run kvm_run
;
1702 if (copy_from_user(&kvm_run
, (void *)arg
, sizeof kvm_run
))
1704 r
= kvm_dev_ioctl_run(kvm
, &kvm_run
);
1705 if (r
< 0 && r
!= -EINTR
)
1707 if (copy_to_user((void *)arg
, &kvm_run
, sizeof kvm_run
)) {
1713 case KVM_GET_REGS
: {
1714 struct kvm_regs kvm_regs
;
1717 if (copy_from_user(&kvm_regs
, (void *)arg
, sizeof kvm_regs
))
1719 r
= kvm_dev_ioctl_get_regs(kvm
, &kvm_regs
);
1723 if (copy_to_user((void *)arg
, &kvm_regs
, sizeof kvm_regs
))
1728 case KVM_SET_REGS
: {
1729 struct kvm_regs kvm_regs
;
1732 if (copy_from_user(&kvm_regs
, (void *)arg
, sizeof kvm_regs
))
1734 r
= kvm_dev_ioctl_set_regs(kvm
, &kvm_regs
);
1740 case KVM_GET_SREGS
: {
1741 struct kvm_sregs kvm_sregs
;
1744 if (copy_from_user(&kvm_sregs
, (void *)arg
, sizeof kvm_sregs
))
1746 r
= kvm_dev_ioctl_get_sregs(kvm
, &kvm_sregs
);
1750 if (copy_to_user((void *)arg
, &kvm_sregs
, sizeof kvm_sregs
))
1755 case KVM_SET_SREGS
: {
1756 struct kvm_sregs kvm_sregs
;
1759 if (copy_from_user(&kvm_sregs
, (void *)arg
, sizeof kvm_sregs
))
1761 r
= kvm_dev_ioctl_set_sregs(kvm
, &kvm_sregs
);
1767 case KVM_TRANSLATE
: {
1768 struct kvm_translation tr
;
1771 if (copy_from_user(&tr
, (void *)arg
, sizeof tr
))
1773 r
= kvm_dev_ioctl_translate(kvm
, &tr
);
1777 if (copy_to_user((void *)arg
, &tr
, sizeof tr
))
1782 case KVM_INTERRUPT
: {
1783 struct kvm_interrupt irq
;
1786 if (copy_from_user(&irq
, (void *)arg
, sizeof irq
))
1788 r
= kvm_dev_ioctl_interrupt(kvm
, &irq
);
1794 case KVM_DEBUG_GUEST
: {
1795 struct kvm_debug_guest dbg
;
1798 if (copy_from_user(&dbg
, (void *)arg
, sizeof dbg
))
1800 r
= kvm_dev_ioctl_debug_guest(kvm
, &dbg
);
1806 case KVM_SET_MEMORY_REGION
: {
1807 struct kvm_memory_region kvm_mem
;
1810 if (copy_from_user(&kvm_mem
, (void *)arg
, sizeof kvm_mem
))
1812 r
= kvm_dev_ioctl_set_memory_region(kvm
, &kvm_mem
);
1817 case KVM_GET_DIRTY_LOG
: {
1818 struct kvm_dirty_log log
;
1821 if (copy_from_user(&log
, (void *)arg
, sizeof log
))
1823 r
= kvm_dev_ioctl_get_dirty_log(kvm
, &log
);
1829 r
= msr_io(kvm
, (void __user
*)arg
, get_msr
, 1);
1832 r
= msr_io(kvm
, (void __user
*)arg
, do_set_msr
, 0);
1834 case KVM_GET_MSR_INDEX_LIST
: {
1835 struct kvm_msr_list __user
*user_msr_list
= (void __user
*)arg
;
1836 struct kvm_msr_list msr_list
;
1840 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
1843 msr_list
.nmsrs
= num_msrs_to_save
;
1844 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
1847 if (n
< num_msrs_to_save
)
1850 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
1851 num_msrs_to_save
* sizeof(u32
)))
1862 static struct page
*kvm_dev_nopage(struct vm_area_struct
*vma
,
1863 unsigned long address
,
1866 struct kvm
*kvm
= vma
->vm_file
->private_data
;
1867 unsigned long pgoff
;
1868 struct kvm_memory_slot
*slot
;
1871 *type
= VM_FAULT_MINOR
;
1872 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1873 slot
= gfn_to_memslot(kvm
, pgoff
);
1875 return NOPAGE_SIGBUS
;
1876 page
= gfn_to_page(slot
, pgoff
);
1878 return NOPAGE_SIGBUS
;
1883 static struct vm_operations_struct kvm_dev_vm_ops
= {
1884 .nopage
= kvm_dev_nopage
,
1887 static int kvm_dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1889 vma
->vm_ops
= &kvm_dev_vm_ops
;
1893 static struct file_operations kvm_chardev_ops
= {
1894 .open
= kvm_dev_open
,
1895 .release
= kvm_dev_release
,
1896 .unlocked_ioctl
= kvm_dev_ioctl
,
1897 .compat_ioctl
= kvm_dev_ioctl
,
1898 .mmap
= kvm_dev_mmap
,
1901 static struct miscdevice kvm_dev
= {
1907 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
1910 if (val
== SYS_RESTART
) {
1912 * Some (well, at least mine) BIOSes hang on reboot if
1915 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
1916 on_each_cpu(kvm_arch_ops
->hardware_disable
, 0, 0, 1);
1921 static struct notifier_block kvm_reboot_notifier
= {
1922 .notifier_call
= kvm_reboot
,
1926 static __init
void kvm_init_debug(void)
1928 struct kvm_stats_debugfs_item
*p
;
1930 debugfs_dir
= debugfs_create_dir("kvm", 0);
1931 for (p
= debugfs_entries
; p
->name
; ++p
)
1932 p
->dentry
= debugfs_create_u32(p
->name
, 0444, debugfs_dir
,
1936 static void kvm_exit_debug(void)
1938 struct kvm_stats_debugfs_item
*p
;
1940 for (p
= debugfs_entries
; p
->name
; ++p
)
1941 debugfs_remove(p
->dentry
);
1942 debugfs_remove(debugfs_dir
);
1945 hpa_t bad_page_address
;
1947 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
1952 printk(KERN_ERR
"kvm: already loaded the other module\n");
1956 if (!ops
->cpu_has_kvm_support()) {
1957 printk(KERN_ERR
"kvm: no hardware support\n");
1960 if (ops
->disabled_by_bios()) {
1961 printk(KERN_ERR
"kvm: disabled by bios\n");
1967 r
= kvm_arch_ops
->hardware_setup();
1971 on_each_cpu(kvm_arch_ops
->hardware_enable
, 0, 0, 1);
1972 register_reboot_notifier(&kvm_reboot_notifier
);
1974 kvm_chardev_ops
.owner
= module
;
1976 r
= misc_register(&kvm_dev
);
1978 printk (KERN_ERR
"kvm: misc device register failed\n");
1985 unregister_reboot_notifier(&kvm_reboot_notifier
);
1986 on_each_cpu(kvm_arch_ops
->hardware_disable
, 0, 0, 1);
1987 kvm_arch_ops
->hardware_unsetup();
1991 void kvm_exit_arch(void)
1993 misc_deregister(&kvm_dev
);
1995 unregister_reboot_notifier(&kvm_reboot_notifier
);
1996 on_each_cpu(kvm_arch_ops
->hardware_disable
, 0, 0, 1);
1997 kvm_arch_ops
->hardware_unsetup();
1998 kvm_arch_ops
= NULL
;
2001 static __init
int kvm_init(void)
2003 static struct page
*bad_page
;
2008 kvm_init_msr_list();
2010 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
2015 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
2016 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
2025 static __exit
void kvm_exit(void)
2028 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
2031 module_init(kvm_init
)
2032 module_exit(kvm_exit
)
2034 EXPORT_SYMBOL_GPL(kvm_init_arch
);
2035 EXPORT_SYMBOL_GPL(kvm_exit_arch
);