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 <linux/magic.h>
24 #include <asm/processor.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
29 #include <linux/miscdevice.h>
30 #include <linux/vmalloc.h>
31 #include <asm/uaccess.h>
32 #include <linux/reboot.h>
34 #include <linux/debugfs.h>
35 #include <linux/highmem.h>
36 #include <linux/file.h>
38 #include <linux/sysdev.h>
39 #include <linux/cpu.h>
40 #include <linux/file.h>
42 #include <linux/mount.h>
43 #include <linux/sched.h>
45 #include "x86_emulate.h"
46 #include "segment_descriptor.h"
48 MODULE_AUTHOR("Qumranet");
49 MODULE_LICENSE("GPL");
51 static DEFINE_SPINLOCK(kvm_lock
);
52 static LIST_HEAD(vm_list
);
54 struct kvm_arch_ops
*kvm_arch_ops
;
56 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
58 static struct kvm_stats_debugfs_item
{
61 struct dentry
*dentry
;
62 } debugfs_entries
[] = {
63 { "pf_fixed", STAT_OFFSET(pf_fixed
) },
64 { "pf_guest", STAT_OFFSET(pf_guest
) },
65 { "tlb_flush", STAT_OFFSET(tlb_flush
) },
66 { "invlpg", STAT_OFFSET(invlpg
) },
67 { "exits", STAT_OFFSET(exits
) },
68 { "io_exits", STAT_OFFSET(io_exits
) },
69 { "mmio_exits", STAT_OFFSET(mmio_exits
) },
70 { "signal_exits", STAT_OFFSET(signal_exits
) },
71 { "irq_window", STAT_OFFSET(irq_window_exits
) },
72 { "halt_exits", STAT_OFFSET(halt_exits
) },
73 { "request_irq", STAT_OFFSET(request_irq_exits
) },
74 { "irq_exits", STAT_OFFSET(irq_exits
) },
78 static struct dentry
*debugfs_dir
;
80 struct vfsmount
*kvmfs_mnt
;
82 #define MAX_IO_MSRS 256
84 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
85 #define LMSW_GUEST_MASK 0x0eULL
86 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
87 #define CR8_RESEVED_BITS (~0x0fULL)
88 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
91 // LDT or TSS descriptor in the GDT. 16 bytes.
92 struct segment_descriptor_64
{
93 struct segment_descriptor s
;
100 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
103 static struct inode
*kvmfs_inode(struct file_operations
*fops
)
106 struct inode
*inode
= new_inode(kvmfs_mnt
->mnt_sb
);
114 * Mark the inode dirty from the very beginning,
115 * that way it will never be moved to the dirty
116 * list because mark_inode_dirty() will think
117 * that it already _is_ on the dirty list.
119 inode
->i_state
= I_DIRTY
;
120 inode
->i_mode
= S_IRUSR
| S_IWUSR
;
121 inode
->i_uid
= current
->fsuid
;
122 inode
->i_gid
= current
->fsgid
;
123 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
127 return ERR_PTR(error
);
130 static struct file
*kvmfs_file(struct inode
*inode
, void *private_data
)
132 struct file
*file
= get_empty_filp();
135 return ERR_PTR(-ENFILE
);
137 file
->f_path
.mnt
= mntget(kvmfs_mnt
);
138 file
->f_path
.dentry
= d_alloc_anon(inode
);
139 if (!file
->f_path
.dentry
)
140 return ERR_PTR(-ENOMEM
);
141 file
->f_mapping
= inode
->i_mapping
;
144 file
->f_flags
= O_RDWR
;
145 file
->f_op
= inode
->i_fop
;
146 file
->f_mode
= FMODE_READ
| FMODE_WRITE
;
148 file
->private_data
= private_data
;
152 unsigned long segment_base(u16 selector
)
154 struct descriptor_table gdt
;
155 struct segment_descriptor
*d
;
156 unsigned long table_base
;
157 typedef unsigned long ul
;
163 asm ("sgdt %0" : "=m"(gdt
));
164 table_base
= gdt
.base
;
166 if (selector
& 4) { /* from ldt */
169 asm ("sldt %0" : "=g"(ldt_selector
));
170 table_base
= segment_base(ldt_selector
);
172 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
173 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
176 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
177 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
181 EXPORT_SYMBOL_GPL(segment_base
);
183 static inline int valid_vcpu(int n
)
185 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
188 int kvm_read_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
191 unsigned char *host_buf
= dest
;
192 unsigned long req_size
= size
;
200 paddr
= gva_to_hpa(vcpu
, addr
);
202 if (is_error_hpa(paddr
))
205 guest_buf
= (hva_t
)kmap_atomic(
206 pfn_to_page(paddr
>> PAGE_SHIFT
),
208 offset
= addr
& ~PAGE_MASK
;
210 now
= min(size
, PAGE_SIZE
- offset
);
211 memcpy(host_buf
, (void*)guest_buf
, now
);
215 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
217 return req_size
- size
;
219 EXPORT_SYMBOL_GPL(kvm_read_guest
);
221 int kvm_write_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
224 unsigned char *host_buf
= data
;
225 unsigned long req_size
= size
;
234 paddr
= gva_to_hpa(vcpu
, addr
);
236 if (is_error_hpa(paddr
))
239 gfn
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
) >> PAGE_SHIFT
;
240 mark_page_dirty(vcpu
->kvm
, gfn
);
241 guest_buf
= (hva_t
)kmap_atomic(
242 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
243 offset
= addr
& ~PAGE_MASK
;
245 now
= min(size
, PAGE_SIZE
- offset
);
246 memcpy((void*)guest_buf
, host_buf
, now
);
250 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
252 return req_size
- size
;
254 EXPORT_SYMBOL_GPL(kvm_write_guest
);
256 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
258 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
261 vcpu
->guest_fpu_loaded
= 1;
262 fx_save(vcpu
->host_fx_image
);
263 fx_restore(vcpu
->guest_fx_image
);
265 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
267 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
269 if (!vcpu
->guest_fpu_loaded
)
272 vcpu
->guest_fpu_loaded
= 0;
273 fx_save(vcpu
->guest_fx_image
);
274 fx_restore(vcpu
->host_fx_image
);
276 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
279 * Switches to specified vcpu, until a matching vcpu_put()
281 static void vcpu_load(struct kvm_vcpu
*vcpu
)
283 mutex_lock(&vcpu
->mutex
);
284 kvm_arch_ops
->vcpu_load(vcpu
);
288 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
289 * if the slot is not populated.
291 static struct kvm_vcpu
*vcpu_load_slot(struct kvm
*kvm
, int slot
)
293 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[slot
];
295 mutex_lock(&vcpu
->mutex
);
297 mutex_unlock(&vcpu
->mutex
);
300 kvm_arch_ops
->vcpu_load(vcpu
);
304 static void vcpu_put(struct kvm_vcpu
*vcpu
)
306 kvm_arch_ops
->vcpu_put(vcpu
);
307 mutex_unlock(&vcpu
->mutex
);
310 static struct kvm
*kvm_create_vm(void)
312 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
316 return ERR_PTR(-ENOMEM
);
318 spin_lock_init(&kvm
->lock
);
319 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
320 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
321 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[i
];
323 mutex_init(&vcpu
->mutex
);
326 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
327 INIT_LIST_HEAD(&vcpu
->free_pages
);
328 spin_lock(&kvm_lock
);
329 list_add(&kvm
->vm_list
, &vm_list
);
330 spin_unlock(&kvm_lock
);
335 static int kvm_dev_open(struct inode
*inode
, struct file
*filp
)
341 * Free any memory in @free but not in @dont.
343 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
344 struct kvm_memory_slot
*dont
)
348 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
349 if (free
->phys_mem
) {
350 for (i
= 0; i
< free
->npages
; ++i
)
351 if (free
->phys_mem
[i
])
352 __free_page(free
->phys_mem
[i
]);
353 vfree(free
->phys_mem
);
356 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
357 vfree(free
->dirty_bitmap
);
359 free
->phys_mem
= NULL
;
361 free
->dirty_bitmap
= NULL
;
364 static void kvm_free_physmem(struct kvm
*kvm
)
368 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
369 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
372 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
376 for (i
= 0; i
< 2; ++i
)
377 if (vcpu
->pio
.guest_pages
[i
]) {
378 __free_page(vcpu
->pio
.guest_pages
[i
]);
379 vcpu
->pio
.guest_pages
[i
] = NULL
;
383 static void kvm_free_vcpu(struct kvm_vcpu
*vcpu
)
389 kvm_mmu_destroy(vcpu
);
391 kvm_arch_ops
->vcpu_free(vcpu
);
392 free_page((unsigned long)vcpu
->run
);
394 free_page((unsigned long)vcpu
->pio_data
);
395 vcpu
->pio_data
= NULL
;
396 free_pio_guest_pages(vcpu
);
399 static void kvm_free_vcpus(struct kvm
*kvm
)
403 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
404 kvm_free_vcpu(&kvm
->vcpus
[i
]);
407 static int kvm_dev_release(struct inode
*inode
, struct file
*filp
)
412 static void kvm_destroy_vm(struct kvm
*kvm
)
414 spin_lock(&kvm_lock
);
415 list_del(&kvm
->vm_list
);
416 spin_unlock(&kvm_lock
);
418 kvm_free_physmem(kvm
);
422 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
424 struct kvm
*kvm
= filp
->private_data
;
430 static void inject_gp(struct kvm_vcpu
*vcpu
)
432 kvm_arch_ops
->inject_gp(vcpu
, 0);
436 * Load the pae pdptrs. Return true is they are all valid.
438 static int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
440 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
441 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
448 spin_lock(&vcpu
->kvm
->lock
);
449 page
= gfn_to_page(vcpu
->kvm
, pdpt_gfn
);
450 /* FIXME: !page - emulate? 0xff? */
451 pdpt
= kmap_atomic(page
, KM_USER0
);
454 for (i
= 0; i
< 4; ++i
) {
455 pdpte
= pdpt
[offset
+ i
];
456 if ((pdpte
& 1) && (pdpte
& 0xfffffff0000001e6ull
)) {
462 for (i
= 0; i
< 4; ++i
)
463 vcpu
->pdptrs
[i
] = pdpt
[offset
+ i
];
466 kunmap_atomic(pdpt
, KM_USER0
);
467 spin_unlock(&vcpu
->kvm
->lock
);
472 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
474 if (cr0
& CR0_RESEVED_BITS
) {
475 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
481 if ((cr0
& CR0_NW_MASK
) && !(cr0
& CR0_CD_MASK
)) {
482 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
487 if ((cr0
& CR0_PG_MASK
) && !(cr0
& CR0_PE_MASK
)) {
488 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
489 "and a clear PE flag\n");
494 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
)) {
496 if ((vcpu
->shadow_efer
& EFER_LME
)) {
500 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
501 "in long mode while PAE is disabled\n");
505 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
507 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
508 "in long mode while CS.L == 1\n");
515 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
516 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
524 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
527 spin_lock(&vcpu
->kvm
->lock
);
528 kvm_mmu_reset_context(vcpu
);
529 spin_unlock(&vcpu
->kvm
->lock
);
532 EXPORT_SYMBOL_GPL(set_cr0
);
534 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
536 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
538 EXPORT_SYMBOL_GPL(lmsw
);
540 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
542 if (cr4
& CR4_RESEVED_BITS
) {
543 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
548 if (is_long_mode(vcpu
)) {
549 if (!(cr4
& CR4_PAE_MASK
)) {
550 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
555 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& CR4_PAE_MASK
)
556 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
557 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
561 if (cr4
& CR4_VMXE_MASK
) {
562 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
566 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
567 spin_lock(&vcpu
->kvm
->lock
);
568 kvm_mmu_reset_context(vcpu
);
569 spin_unlock(&vcpu
->kvm
->lock
);
571 EXPORT_SYMBOL_GPL(set_cr4
);
573 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
575 if (is_long_mode(vcpu
)) {
576 if (cr3
& CR3_L_MODE_RESEVED_BITS
) {
577 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
582 if (cr3
& CR3_RESEVED_BITS
) {
583 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
587 if (is_paging(vcpu
) && is_pae(vcpu
) &&
588 !load_pdptrs(vcpu
, cr3
)) {
589 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
597 spin_lock(&vcpu
->kvm
->lock
);
599 * Does the new cr3 value map to physical memory? (Note, we
600 * catch an invalid cr3 even in real-mode, because it would
601 * cause trouble later on when we turn on paging anyway.)
603 * A real CPU would silently accept an invalid cr3 and would
604 * attempt to use it - with largely undefined (and often hard
605 * to debug) behavior on the guest side.
607 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
610 vcpu
->mmu
.new_cr3(vcpu
);
611 spin_unlock(&vcpu
->kvm
->lock
);
613 EXPORT_SYMBOL_GPL(set_cr3
);
615 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
617 if ( cr8
& CR8_RESEVED_BITS
) {
618 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
624 EXPORT_SYMBOL_GPL(set_cr8
);
626 void fx_init(struct kvm_vcpu
*vcpu
)
628 struct __attribute__ ((__packed__
)) fx_image_s
{
634 u64 operand
;// fpu dp
640 fx_save(vcpu
->host_fx_image
);
642 fx_save(vcpu
->guest_fx_image
);
643 fx_restore(vcpu
->host_fx_image
);
645 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
646 fx_image
->mxcsr
= 0x1f80;
647 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
648 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
650 EXPORT_SYMBOL_GPL(fx_init
);
652 static void do_remove_write_access(struct kvm_vcpu
*vcpu
, int slot
)
654 spin_lock(&vcpu
->kvm
->lock
);
655 kvm_mmu_slot_remove_write_access(vcpu
, slot
);
656 spin_unlock(&vcpu
->kvm
->lock
);
660 * Allocate some memory and give it an address in the guest physical address
663 * Discontiguous memory is allowed, mostly for framebuffers.
665 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
666 struct kvm_memory_region
*mem
)
670 unsigned long npages
;
672 struct kvm_memory_slot
*memslot
;
673 struct kvm_memory_slot old
, new;
674 int memory_config_version
;
677 /* General sanity checks */
678 if (mem
->memory_size
& (PAGE_SIZE
- 1))
680 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
682 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
684 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
687 memslot
= &kvm
->memslots
[mem
->slot
];
688 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
689 npages
= mem
->memory_size
>> PAGE_SHIFT
;
692 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
695 spin_lock(&kvm
->lock
);
697 memory_config_version
= kvm
->memory_config_version
;
698 new = old
= *memslot
;
700 new.base_gfn
= base_gfn
;
702 new.flags
= mem
->flags
;
704 /* Disallow changing a memory slot's size. */
706 if (npages
&& old
.npages
&& npages
!= old
.npages
)
709 /* Check for overlaps */
711 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
712 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
716 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
717 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
721 * Do memory allocations outside lock. memory_config_version will
724 spin_unlock(&kvm
->lock
);
726 /* Deallocate if slot is being removed */
730 /* Free page dirty bitmap if unneeded */
731 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
732 new.dirty_bitmap
= NULL
;
736 /* Allocate if a slot is being created */
737 if (npages
&& !new.phys_mem
) {
738 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
743 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
744 for (i
= 0; i
< npages
; ++i
) {
745 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
747 if (!new.phys_mem
[i
])
749 set_page_private(new.phys_mem
[i
],0);
753 /* Allocate page dirty bitmap if needed */
754 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
755 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
757 new.dirty_bitmap
= vmalloc(dirty_bytes
);
758 if (!new.dirty_bitmap
)
760 memset(new.dirty_bitmap
, 0, dirty_bytes
);
763 spin_lock(&kvm
->lock
);
765 if (memory_config_version
!= kvm
->memory_config_version
) {
766 spin_unlock(&kvm
->lock
);
767 kvm_free_physmem_slot(&new, &old
);
775 if (mem
->slot
>= kvm
->nmemslots
)
776 kvm
->nmemslots
= mem
->slot
+ 1;
779 ++kvm
->memory_config_version
;
781 spin_unlock(&kvm
->lock
);
783 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
784 struct kvm_vcpu
*vcpu
;
786 vcpu
= vcpu_load_slot(kvm
, i
);
789 if (new.flags
& KVM_MEM_LOG_DIRTY_PAGES
)
790 do_remove_write_access(vcpu
, mem
->slot
);
791 kvm_mmu_reset_context(vcpu
);
795 kvm_free_physmem_slot(&old
, &new);
799 spin_unlock(&kvm
->lock
);
801 kvm_free_physmem_slot(&new, &old
);
807 * Get (and clear) the dirty memory log for a memory slot.
809 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
810 struct kvm_dirty_log
*log
)
812 struct kvm_memory_slot
*memslot
;
816 unsigned long any
= 0;
818 spin_lock(&kvm
->lock
);
821 * Prevent changes to guest memory configuration even while the lock
825 spin_unlock(&kvm
->lock
);
827 if (log
->slot
>= KVM_MEMORY_SLOTS
)
830 memslot
= &kvm
->memslots
[log
->slot
];
832 if (!memslot
->dirty_bitmap
)
835 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
837 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
838 any
= memslot
->dirty_bitmap
[i
];
841 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
846 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
847 struct kvm_vcpu
*vcpu
;
849 vcpu
= vcpu_load_slot(kvm
, i
);
853 do_remove_write_access(vcpu
, log
->slot
);
854 memset(memslot
->dirty_bitmap
, 0, n
);
857 kvm_arch_ops
->tlb_flush(vcpu
);
865 spin_lock(&kvm
->lock
);
867 spin_unlock(&kvm
->lock
);
872 * Set a new alias region. Aliases map a portion of physical memory into
873 * another portion. This is useful for memory windows, for example the PC
876 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
877 struct kvm_memory_alias
*alias
)
880 struct kvm_mem_alias
*p
;
883 /* General sanity checks */
884 if (alias
->memory_size
& (PAGE_SIZE
- 1))
886 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
888 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
890 if (alias
->guest_phys_addr
+ alias
->memory_size
891 < alias
->guest_phys_addr
)
893 if (alias
->target_phys_addr
+ alias
->memory_size
894 < alias
->target_phys_addr
)
897 spin_lock(&kvm
->lock
);
899 p
= &kvm
->aliases
[alias
->slot
];
900 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
901 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
902 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
904 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
905 if (kvm
->aliases
[n
- 1].npages
)
909 spin_unlock(&kvm
->lock
);
911 vcpu_load(&kvm
->vcpus
[0]);
912 spin_lock(&kvm
->lock
);
913 kvm_mmu_zap_all(&kvm
->vcpus
[0]);
914 spin_unlock(&kvm
->lock
);
915 vcpu_put(&kvm
->vcpus
[0]);
923 static gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
926 struct kvm_mem_alias
*alias
;
928 for (i
= 0; i
< kvm
->naliases
; ++i
) {
929 alias
= &kvm
->aliases
[i
];
930 if (gfn
>= alias
->base_gfn
931 && gfn
< alias
->base_gfn
+ alias
->npages
)
932 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
937 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
941 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
942 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
944 if (gfn
>= memslot
->base_gfn
945 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
951 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
953 gfn
= unalias_gfn(kvm
, gfn
);
954 return __gfn_to_memslot(kvm
, gfn
);
957 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
959 struct kvm_memory_slot
*slot
;
961 gfn
= unalias_gfn(kvm
, gfn
);
962 slot
= __gfn_to_memslot(kvm
, gfn
);
965 return slot
->phys_mem
[gfn
- slot
->base_gfn
];
967 EXPORT_SYMBOL_GPL(gfn_to_page
);
969 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
972 struct kvm_memory_slot
*memslot
= NULL
;
973 unsigned long rel_gfn
;
975 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
976 memslot
= &kvm
->memslots
[i
];
978 if (gfn
>= memslot
->base_gfn
979 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
981 if (!memslot
|| !memslot
->dirty_bitmap
)
984 rel_gfn
= gfn
- memslot
->base_gfn
;
987 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
988 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
994 static int emulator_read_std(unsigned long addr
,
997 struct x86_emulate_ctxt
*ctxt
)
999 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1003 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1004 unsigned offset
= addr
& (PAGE_SIZE
-1);
1005 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1010 if (gpa
== UNMAPPED_GVA
)
1011 return X86EMUL_PROPAGATE_FAULT
;
1012 pfn
= gpa
>> PAGE_SHIFT
;
1013 page
= gfn_to_page(vcpu
->kvm
, pfn
);
1015 return X86EMUL_UNHANDLEABLE
;
1016 page_virt
= kmap_atomic(page
, KM_USER0
);
1018 memcpy(data
, page_virt
+ offset
, tocopy
);
1020 kunmap_atomic(page_virt
, KM_USER0
);
1027 return X86EMUL_CONTINUE
;
1030 static int emulator_write_std(unsigned long addr
,
1033 struct x86_emulate_ctxt
*ctxt
)
1035 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
1037 return X86EMUL_UNHANDLEABLE
;
1040 static int emulator_read_emulated(unsigned long addr
,
1043 struct x86_emulate_ctxt
*ctxt
)
1045 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1047 if (vcpu
->mmio_read_completed
) {
1048 memcpy(val
, vcpu
->mmio_data
, bytes
);
1049 vcpu
->mmio_read_completed
= 0;
1050 return X86EMUL_CONTINUE
;
1051 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
1052 == X86EMUL_CONTINUE
)
1053 return X86EMUL_CONTINUE
;
1055 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1057 if (gpa
== UNMAPPED_GVA
)
1058 return X86EMUL_PROPAGATE_FAULT
;
1059 vcpu
->mmio_needed
= 1;
1060 vcpu
->mmio_phys_addr
= gpa
;
1061 vcpu
->mmio_size
= bytes
;
1062 vcpu
->mmio_is_write
= 0;
1064 return X86EMUL_UNHANDLEABLE
;
1068 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1069 const void *val
, int bytes
)
1074 if (((gpa
+ bytes
- 1) >> PAGE_SHIFT
) != (gpa
>> PAGE_SHIFT
))
1076 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1079 kvm_mmu_pre_write(vcpu
, gpa
, bytes
);
1080 mark_page_dirty(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1081 virt
= kmap_atomic(page
, KM_USER0
);
1082 memcpy(virt
+ offset_in_page(gpa
), val
, bytes
);
1083 kunmap_atomic(virt
, KM_USER0
);
1084 kvm_mmu_post_write(vcpu
, gpa
, bytes
);
1088 static int emulator_write_emulated(unsigned long addr
,
1091 struct x86_emulate_ctxt
*ctxt
)
1093 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1094 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1096 if (gpa
== UNMAPPED_GVA
) {
1097 kvm_arch_ops
->inject_page_fault(vcpu
, addr
, 2);
1098 return X86EMUL_PROPAGATE_FAULT
;
1101 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1102 return X86EMUL_CONTINUE
;
1104 vcpu
->mmio_needed
= 1;
1105 vcpu
->mmio_phys_addr
= gpa
;
1106 vcpu
->mmio_size
= bytes
;
1107 vcpu
->mmio_is_write
= 1;
1108 memcpy(vcpu
->mmio_data
, val
, bytes
);
1110 return X86EMUL_CONTINUE
;
1113 static int emulator_cmpxchg_emulated(unsigned long addr
,
1117 struct x86_emulate_ctxt
*ctxt
)
1119 static int reported
;
1123 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1125 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
1128 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1130 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
1133 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1135 return X86EMUL_CONTINUE
;
1138 int emulate_clts(struct kvm_vcpu
*vcpu
)
1142 cr0
= vcpu
->cr0
& ~CR0_TS_MASK
;
1143 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
1144 return X86EMUL_CONTINUE
;
1147 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1149 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1153 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
1154 return X86EMUL_CONTINUE
;
1156 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
1158 return X86EMUL_UNHANDLEABLE
;
1162 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1164 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1167 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1169 /* FIXME: better handling */
1170 return X86EMUL_UNHANDLEABLE
;
1172 return X86EMUL_CONTINUE
;
1175 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
1177 static int reported
;
1179 unsigned long rip
= ctxt
->vcpu
->rip
;
1180 unsigned long rip_linear
;
1182 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
1187 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
1189 printk(KERN_ERR
"emulation failed but !mmio_needed?"
1190 " rip %lx %02x %02x %02x %02x\n",
1191 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1195 struct x86_emulate_ops emulate_ops
= {
1196 .read_std
= emulator_read_std
,
1197 .write_std
= emulator_write_std
,
1198 .read_emulated
= emulator_read_emulated
,
1199 .write_emulated
= emulator_write_emulated
,
1200 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1203 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1204 struct kvm_run
*run
,
1208 struct x86_emulate_ctxt emulate_ctxt
;
1212 vcpu
->mmio_fault_cr2
= cr2
;
1213 kvm_arch_ops
->cache_regs(vcpu
);
1215 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1217 emulate_ctxt
.vcpu
= vcpu
;
1218 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1219 emulate_ctxt
.cr2
= cr2
;
1220 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1221 ? X86EMUL_MODE_REAL
: cs_l
1222 ? X86EMUL_MODE_PROT64
: cs_db
1223 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1225 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1226 emulate_ctxt
.cs_base
= 0;
1227 emulate_ctxt
.ds_base
= 0;
1228 emulate_ctxt
.es_base
= 0;
1229 emulate_ctxt
.ss_base
= 0;
1231 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1232 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1233 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1234 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1237 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1238 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1240 vcpu
->mmio_is_write
= 0;
1241 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1243 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1244 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1245 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1246 run
->mmio
.len
= vcpu
->mmio_size
;
1247 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1251 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1252 return EMULATE_DONE
;
1253 if (!vcpu
->mmio_needed
) {
1254 report_emulation_failure(&emulate_ctxt
);
1255 return EMULATE_FAIL
;
1257 return EMULATE_DO_MMIO
;
1260 kvm_arch_ops
->decache_regs(vcpu
);
1261 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1263 if (vcpu
->mmio_is_write
) {
1264 vcpu
->mmio_needed
= 0;
1265 return EMULATE_DO_MMIO
;
1268 return EMULATE_DONE
;
1270 EXPORT_SYMBOL_GPL(emulate_instruction
);
1272 int kvm_hypercall(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1274 unsigned long nr
, a0
, a1
, a2
, a3
, a4
, a5
, ret
;
1276 kvm_arch_ops
->cache_regs(vcpu
);
1278 #ifdef CONFIG_X86_64
1279 if (is_long_mode(vcpu
)) {
1280 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1281 a0
= vcpu
->regs
[VCPU_REGS_RDI
];
1282 a1
= vcpu
->regs
[VCPU_REGS_RSI
];
1283 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1284 a3
= vcpu
->regs
[VCPU_REGS_RCX
];
1285 a4
= vcpu
->regs
[VCPU_REGS_R8
];
1286 a5
= vcpu
->regs
[VCPU_REGS_R9
];
1290 nr
= vcpu
->regs
[VCPU_REGS_RBX
] & -1u;
1291 a0
= vcpu
->regs
[VCPU_REGS_RAX
] & -1u;
1292 a1
= vcpu
->regs
[VCPU_REGS_RCX
] & -1u;
1293 a2
= vcpu
->regs
[VCPU_REGS_RDX
] & -1u;
1294 a3
= vcpu
->regs
[VCPU_REGS_RSI
] & -1u;
1295 a4
= vcpu
->regs
[VCPU_REGS_RDI
] & -1u;
1296 a5
= vcpu
->regs
[VCPU_REGS_RBP
] & -1u;
1300 run
->hypercall
.args
[0] = a0
;
1301 run
->hypercall
.args
[1] = a1
;
1302 run
->hypercall
.args
[2] = a2
;
1303 run
->hypercall
.args
[3] = a3
;
1304 run
->hypercall
.args
[4] = a4
;
1305 run
->hypercall
.args
[5] = a5
;
1306 run
->hypercall
.ret
= ret
;
1307 run
->hypercall
.longmode
= is_long_mode(vcpu
);
1308 kvm_arch_ops
->decache_regs(vcpu
);
1311 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1312 kvm_arch_ops
->decache_regs(vcpu
);
1315 EXPORT_SYMBOL_GPL(kvm_hypercall
);
1317 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1319 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1322 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1324 struct descriptor_table dt
= { limit
, base
};
1326 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1329 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1331 struct descriptor_table dt
= { limit
, base
};
1333 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1336 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1337 unsigned long *rflags
)
1340 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1343 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1345 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
1356 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1361 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1362 unsigned long *rflags
)
1366 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1367 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1376 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1379 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1384 * Register the para guest with the host:
1386 static int vcpu_register_para(struct kvm_vcpu
*vcpu
, gpa_t para_state_gpa
)
1388 struct kvm_vcpu_para_state
*para_state
;
1389 hpa_t para_state_hpa
, hypercall_hpa
;
1390 struct page
*para_state_page
;
1391 unsigned char *hypercall
;
1392 gpa_t hypercall_gpa
;
1394 printk(KERN_DEBUG
"kvm: guest trying to enter paravirtual mode\n");
1395 printk(KERN_DEBUG
".... para_state_gpa: %08Lx\n", para_state_gpa
);
1398 * Needs to be page aligned:
1400 if (para_state_gpa
!= PAGE_ALIGN(para_state_gpa
))
1403 para_state_hpa
= gpa_to_hpa(vcpu
, para_state_gpa
);
1404 printk(KERN_DEBUG
".... para_state_hpa: %08Lx\n", para_state_hpa
);
1405 if (is_error_hpa(para_state_hpa
))
1408 mark_page_dirty(vcpu
->kvm
, para_state_gpa
>> PAGE_SHIFT
);
1409 para_state_page
= pfn_to_page(para_state_hpa
>> PAGE_SHIFT
);
1410 para_state
= kmap_atomic(para_state_page
, KM_USER0
);
1412 printk(KERN_DEBUG
".... guest version: %d\n", para_state
->guest_version
);
1413 printk(KERN_DEBUG
".... size: %d\n", para_state
->size
);
1415 para_state
->host_version
= KVM_PARA_API_VERSION
;
1417 * We cannot support guests that try to register themselves
1418 * with a newer API version than the host supports:
1420 if (para_state
->guest_version
> KVM_PARA_API_VERSION
) {
1421 para_state
->ret
= -KVM_EINVAL
;
1422 goto err_kunmap_skip
;
1425 hypercall_gpa
= para_state
->hypercall_gpa
;
1426 hypercall_hpa
= gpa_to_hpa(vcpu
, hypercall_gpa
);
1427 printk(KERN_DEBUG
".... hypercall_hpa: %08Lx\n", hypercall_hpa
);
1428 if (is_error_hpa(hypercall_hpa
)) {
1429 para_state
->ret
= -KVM_EINVAL
;
1430 goto err_kunmap_skip
;
1433 printk(KERN_DEBUG
"kvm: para guest successfully registered.\n");
1434 vcpu
->para_state_page
= para_state_page
;
1435 vcpu
->para_state_gpa
= para_state_gpa
;
1436 vcpu
->hypercall_gpa
= hypercall_gpa
;
1438 mark_page_dirty(vcpu
->kvm
, hypercall_gpa
>> PAGE_SHIFT
);
1439 hypercall
= kmap_atomic(pfn_to_page(hypercall_hpa
>> PAGE_SHIFT
),
1440 KM_USER1
) + (hypercall_hpa
& ~PAGE_MASK
);
1441 kvm_arch_ops
->patch_hypercall(vcpu
, hypercall
);
1442 kunmap_atomic(hypercall
, KM_USER1
);
1444 para_state
->ret
= 0;
1446 kunmap_atomic(para_state
, KM_USER0
);
1452 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1457 case 0xc0010010: /* SYSCFG */
1458 case 0xc0010015: /* HWCR */
1459 case MSR_IA32_PLATFORM_ID
:
1460 case MSR_IA32_P5_MC_ADDR
:
1461 case MSR_IA32_P5_MC_TYPE
:
1462 case MSR_IA32_MC0_CTL
:
1463 case MSR_IA32_MCG_STATUS
:
1464 case MSR_IA32_MCG_CAP
:
1465 case MSR_IA32_MC0_MISC
:
1466 case MSR_IA32_MC0_MISC
+4:
1467 case MSR_IA32_MC0_MISC
+8:
1468 case MSR_IA32_MC0_MISC
+12:
1469 case MSR_IA32_MC0_MISC
+16:
1470 case MSR_IA32_UCODE_REV
:
1471 case MSR_IA32_PERF_STATUS
:
1472 /* MTRR registers */
1474 case 0x200 ... 0x2ff:
1477 case 0xcd: /* fsb frequency */
1480 case MSR_IA32_APICBASE
:
1481 data
= vcpu
->apic_base
;
1483 case MSR_IA32_MISC_ENABLE
:
1484 data
= vcpu
->ia32_misc_enable_msr
;
1486 #ifdef CONFIG_X86_64
1488 data
= vcpu
->shadow_efer
;
1492 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1498 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1501 * Reads an msr value (of 'msr_index') into 'pdata'.
1502 * Returns 0 on success, non-0 otherwise.
1503 * Assumes vcpu_load() was already called.
1505 static int get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1507 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1510 #ifdef CONFIG_X86_64
1512 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1514 if (efer
& EFER_RESERVED_BITS
) {
1515 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1522 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1523 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1528 kvm_arch_ops
->set_efer(vcpu
, efer
);
1531 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1533 vcpu
->shadow_efer
= efer
;
1538 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1541 #ifdef CONFIG_X86_64
1543 set_efer(vcpu
, data
);
1546 case MSR_IA32_MC0_STATUS
:
1547 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1548 __FUNCTION__
, data
);
1550 case MSR_IA32_MCG_STATUS
:
1551 printk(KERN_WARNING
"%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1552 __FUNCTION__
, data
);
1554 case MSR_IA32_UCODE_REV
:
1555 case MSR_IA32_UCODE_WRITE
:
1556 case 0x200 ... 0x2ff: /* MTRRs */
1558 case MSR_IA32_APICBASE
:
1559 vcpu
->apic_base
= data
;
1561 case MSR_IA32_MISC_ENABLE
:
1562 vcpu
->ia32_misc_enable_msr
= data
;
1565 * This is the 'probe whether the host is KVM' logic:
1567 case MSR_KVM_API_MAGIC
:
1568 return vcpu_register_para(vcpu
, data
);
1571 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1576 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1579 * Writes msr value into into the appropriate "register".
1580 * Returns 0 on success, non-0 otherwise.
1581 * Assumes vcpu_load() was already called.
1583 static int set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1585 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1588 void kvm_resched(struct kvm_vcpu
*vcpu
)
1590 if (!need_resched())
1596 EXPORT_SYMBOL_GPL(kvm_resched
);
1598 void load_msrs(struct vmx_msr_entry
*e
, int n
)
1602 for (i
= 0; i
< n
; ++i
)
1603 wrmsrl(e
[i
].index
, e
[i
].data
);
1605 EXPORT_SYMBOL_GPL(load_msrs
);
1607 void save_msrs(struct vmx_msr_entry
*e
, int n
)
1611 for (i
= 0; i
< n
; ++i
)
1612 rdmsrl(e
[i
].index
, e
[i
].data
);
1614 EXPORT_SYMBOL_GPL(save_msrs
);
1616 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1620 struct kvm_cpuid_entry
*e
, *best
;
1622 kvm_arch_ops
->cache_regs(vcpu
);
1623 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1624 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1625 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1626 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1627 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1629 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1630 e
= &vcpu
->cpuid_entries
[i
];
1631 if (e
->function
== function
) {
1636 * Both basic or both extended?
1638 if (((e
->function
^ function
) & 0x80000000) == 0)
1639 if (!best
|| e
->function
> best
->function
)
1643 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1644 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1645 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1646 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1648 kvm_arch_ops
->decache_regs(vcpu
);
1649 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1651 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1653 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1655 void *p
= vcpu
->pio_data
;
1658 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1660 kvm_arch_ops
->vcpu_put(vcpu
);
1661 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1664 kvm_arch_ops
->vcpu_load(vcpu
);
1665 free_pio_guest_pages(vcpu
);
1668 q
+= vcpu
->pio
.guest_page_offset
;
1669 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1671 memcpy(q
, p
, bytes
);
1673 memcpy(p
, q
, bytes
);
1674 q
-= vcpu
->pio
.guest_page_offset
;
1676 kvm_arch_ops
->vcpu_load(vcpu
);
1677 free_pio_guest_pages(vcpu
);
1681 static int complete_pio(struct kvm_vcpu
*vcpu
)
1683 struct kvm_pio_request
*io
= &vcpu
->pio
;
1687 kvm_arch_ops
->cache_regs(vcpu
);
1691 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1695 r
= pio_copy_data(vcpu
);
1697 kvm_arch_ops
->cache_regs(vcpu
);
1704 delta
*= io
->cur_count
;
1706 * The size of the register should really depend on
1707 * current address size.
1709 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1715 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1717 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1720 kvm_arch_ops
->decache_regs(vcpu
);
1722 io
->count
-= io
->cur_count
;
1726 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1730 int kvm_setup_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1731 int size
, unsigned long count
, int string
, int down
,
1732 gva_t address
, int rep
, unsigned port
)
1734 unsigned now
, in_page
;
1739 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1740 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1741 vcpu
->run
->io
.size
= size
;
1742 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1743 vcpu
->run
->io
.count
= count
;
1744 vcpu
->run
->io
.port
= port
;
1745 vcpu
->pio
.count
= count
;
1746 vcpu
->pio
.cur_count
= count
;
1747 vcpu
->pio
.size
= size
;
1749 vcpu
->pio
.string
= string
;
1750 vcpu
->pio
.down
= down
;
1751 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1752 vcpu
->pio
.rep
= rep
;
1755 kvm_arch_ops
->cache_regs(vcpu
);
1756 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1757 kvm_arch_ops
->decache_regs(vcpu
);
1762 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1766 now
= min(count
, PAGE_SIZE
/ size
);
1769 in_page
= PAGE_SIZE
- offset_in_page(address
);
1771 in_page
= offset_in_page(address
) + size
;
1772 now
= min(count
, (unsigned long)in_page
/ size
);
1775 * String I/O straddles page boundary. Pin two guest pages
1776 * so that we satisfy atomicity constraints. Do just one
1777 * transaction to avoid complexity.
1784 * String I/O in reverse. Yuck. Kill the guest, fix later.
1786 printk(KERN_ERR
"kvm: guest string pio down\n");
1790 vcpu
->run
->io
.count
= now
;
1791 vcpu
->pio
.cur_count
= now
;
1793 for (i
= 0; i
< nr_pages
; ++i
) {
1794 spin_lock(&vcpu
->kvm
->lock
);
1795 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
1798 vcpu
->pio
.guest_pages
[i
] = page
;
1799 spin_unlock(&vcpu
->kvm
->lock
);
1802 free_pio_guest_pages(vcpu
);
1808 return pio_copy_data(vcpu
);
1811 EXPORT_SYMBOL_GPL(kvm_setup_pio
);
1813 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1820 if (vcpu
->sigset_active
)
1821 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
1823 /* re-sync apic's tpr */
1824 vcpu
->cr8
= kvm_run
->cr8
;
1826 if (vcpu
->pio
.cur_count
) {
1827 r
= complete_pio(vcpu
);
1832 if (vcpu
->mmio_needed
) {
1833 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1834 vcpu
->mmio_read_completed
= 1;
1835 vcpu
->mmio_needed
= 0;
1836 r
= emulate_instruction(vcpu
, kvm_run
,
1837 vcpu
->mmio_fault_cr2
, 0);
1838 if (r
== EMULATE_DO_MMIO
) {
1840 * Read-modify-write. Back to userspace.
1842 kvm_run
->exit_reason
= KVM_EXIT_MMIO
;
1848 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
1849 kvm_arch_ops
->cache_regs(vcpu
);
1850 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
1851 kvm_arch_ops
->decache_regs(vcpu
);
1854 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1857 if (vcpu
->sigset_active
)
1858 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1864 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
1865 struct kvm_regs
*regs
)
1869 kvm_arch_ops
->cache_regs(vcpu
);
1871 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
1872 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
1873 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
1874 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
1875 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
1876 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
1877 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
1878 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
1879 #ifdef CONFIG_X86_64
1880 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
1881 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
1882 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
1883 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
1884 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
1885 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
1886 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
1887 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
1890 regs
->rip
= vcpu
->rip
;
1891 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1894 * Don't leak debug flags in case they were set for guest debugging
1896 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
1897 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1904 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
1905 struct kvm_regs
*regs
)
1909 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
1910 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
1911 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
1912 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
1913 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
1914 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
1915 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
1916 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
1917 #ifdef CONFIG_X86_64
1918 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
1919 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
1920 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
1921 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
1922 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
1923 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
1924 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
1925 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
1928 vcpu
->rip
= regs
->rip
;
1929 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
1931 kvm_arch_ops
->decache_regs(vcpu
);
1938 static void get_segment(struct kvm_vcpu
*vcpu
,
1939 struct kvm_segment
*var
, int seg
)
1941 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
1944 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1945 struct kvm_sregs
*sregs
)
1947 struct descriptor_table dt
;
1951 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
1952 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
1953 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
1954 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
1955 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
1956 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
1958 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
1959 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
1961 kvm_arch_ops
->get_idt(vcpu
, &dt
);
1962 sregs
->idt
.limit
= dt
.limit
;
1963 sregs
->idt
.base
= dt
.base
;
1964 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
1965 sregs
->gdt
.limit
= dt
.limit
;
1966 sregs
->gdt
.base
= dt
.base
;
1968 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
1969 sregs
->cr0
= vcpu
->cr0
;
1970 sregs
->cr2
= vcpu
->cr2
;
1971 sregs
->cr3
= vcpu
->cr3
;
1972 sregs
->cr4
= vcpu
->cr4
;
1973 sregs
->cr8
= vcpu
->cr8
;
1974 sregs
->efer
= vcpu
->shadow_efer
;
1975 sregs
->apic_base
= vcpu
->apic_base
;
1977 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
1978 sizeof sregs
->interrupt_bitmap
);
1985 static void set_segment(struct kvm_vcpu
*vcpu
,
1986 struct kvm_segment
*var
, int seg
)
1988 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
1991 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
1992 struct kvm_sregs
*sregs
)
1994 int mmu_reset_needed
= 0;
1996 struct descriptor_table dt
;
2000 dt
.limit
= sregs
->idt
.limit
;
2001 dt
.base
= sregs
->idt
.base
;
2002 kvm_arch_ops
->set_idt(vcpu
, &dt
);
2003 dt
.limit
= sregs
->gdt
.limit
;
2004 dt
.base
= sregs
->gdt
.base
;
2005 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
2007 vcpu
->cr2
= sregs
->cr2
;
2008 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2009 vcpu
->cr3
= sregs
->cr3
;
2011 vcpu
->cr8
= sregs
->cr8
;
2013 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2014 #ifdef CONFIG_X86_64
2015 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
2017 vcpu
->apic_base
= sregs
->apic_base
;
2019 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2021 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2022 kvm_arch_ops
->set_cr0(vcpu
, sregs
->cr0
);
2024 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2025 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
2026 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2027 load_pdptrs(vcpu
, vcpu
->cr3
);
2029 if (mmu_reset_needed
)
2030 kvm_mmu_reset_context(vcpu
);
2032 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2033 sizeof vcpu
->irq_pending
);
2034 vcpu
->irq_summary
= 0;
2035 for (i
= 0; i
< NR_IRQ_WORDS
; ++i
)
2036 if (vcpu
->irq_pending
[i
])
2037 __set_bit(i
, &vcpu
->irq_summary
);
2039 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2040 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2041 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2042 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2043 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2044 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2046 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2047 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2055 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2056 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2058 * This list is modified at module load time to reflect the
2059 * capabilities of the host cpu.
2061 static u32 msrs_to_save
[] = {
2062 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
2064 #ifdef CONFIG_X86_64
2065 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
2067 MSR_IA32_TIME_STAMP_COUNTER
,
2070 static unsigned num_msrs_to_save
;
2072 static u32 emulated_msrs
[] = {
2073 MSR_IA32_MISC_ENABLE
,
2076 static __init
void kvm_init_msr_list(void)
2081 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2082 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2085 msrs_to_save
[j
] = msrs_to_save
[i
];
2088 num_msrs_to_save
= j
;
2092 * Adapt set_msr() to msr_io()'s calling convention
2094 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
2096 return set_msr(vcpu
, index
, *data
);
2100 * Read or write a bunch of msrs. All parameters are kernel addresses.
2102 * @return number of msrs set successfully.
2104 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
2105 struct kvm_msr_entry
*entries
,
2106 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2107 unsigned index
, u64
*data
))
2113 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
2114 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
2123 * Read or write a bunch of msrs. Parameters are user addresses.
2125 * @return number of msrs set successfully.
2127 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
2128 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2129 unsigned index
, u64
*data
),
2132 struct kvm_msrs msrs
;
2133 struct kvm_msr_entry
*entries
;
2138 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
2142 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
2146 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
2147 entries
= vmalloc(size
);
2152 if (copy_from_user(entries
, user_msrs
->entries
, size
))
2155 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
2160 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
2172 * Translate a guest virtual address to a guest physical address.
2174 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2175 struct kvm_translation
*tr
)
2177 unsigned long vaddr
= tr
->linear_address
;
2181 spin_lock(&vcpu
->kvm
->lock
);
2182 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2183 tr
->physical_address
= gpa
;
2184 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2187 spin_unlock(&vcpu
->kvm
->lock
);
2193 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2194 struct kvm_interrupt
*irq
)
2196 if (irq
->irq
< 0 || irq
->irq
>= 256)
2200 set_bit(irq
->irq
, vcpu
->irq_pending
);
2201 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
2208 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2209 struct kvm_debug_guest
*dbg
)
2215 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
2222 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
2223 unsigned long address
,
2226 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
2227 unsigned long pgoff
;
2230 *type
= VM_FAULT_MINOR
;
2231 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2233 page
= virt_to_page(vcpu
->run
);
2234 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
2235 page
= virt_to_page(vcpu
->pio_data
);
2237 return NOPAGE_SIGBUS
;
2242 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
2243 .nopage
= kvm_vcpu_nopage
,
2246 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2248 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
2252 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
2254 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2256 fput(vcpu
->kvm
->filp
);
2260 static struct file_operations kvm_vcpu_fops
= {
2261 .release
= kvm_vcpu_release
,
2262 .unlocked_ioctl
= kvm_vcpu_ioctl
,
2263 .compat_ioctl
= kvm_vcpu_ioctl
,
2264 .mmap
= kvm_vcpu_mmap
,
2268 * Allocates an inode for the vcpu.
2270 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
2273 struct inode
*inode
;
2276 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
2277 inode
= kvmfs_inode(&kvm_vcpu_fops
);
2278 if (IS_ERR(inode
)) {
2283 file
= kvmfs_file(inode
, vcpu
);
2289 r
= get_unused_fd();
2293 fd_install(fd
, file
);
2302 fput(vcpu
->kvm
->filp
);
2307 * Creates some virtual cpus. Good luck creating more than one.
2309 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
2312 struct kvm_vcpu
*vcpu
;
2319 vcpu
= &kvm
->vcpus
[n
];
2321 mutex_lock(&vcpu
->mutex
);
2324 mutex_unlock(&vcpu
->mutex
);
2328 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2332 vcpu
->run
= page_address(page
);
2334 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2338 vcpu
->pio_data
= page_address(page
);
2340 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
2342 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
2345 r
= kvm_arch_ops
->vcpu_create(vcpu
);
2347 goto out_free_vcpus
;
2349 r
= kvm_mmu_create(vcpu
);
2351 goto out_free_vcpus
;
2353 kvm_arch_ops
->vcpu_load(vcpu
);
2354 r
= kvm_mmu_setup(vcpu
);
2356 r
= kvm_arch_ops
->vcpu_setup(vcpu
);
2360 goto out_free_vcpus
;
2362 r
= create_vcpu_fd(vcpu
);
2364 goto out_free_vcpus
;
2369 kvm_free_vcpu(vcpu
);
2371 free_page((unsigned long)vcpu
->run
);
2374 mutex_unlock(&vcpu
->mutex
);
2379 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
2380 struct kvm_cpuid
*cpuid
,
2381 struct kvm_cpuid_entry __user
*entries
)
2386 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2389 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
2390 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
2392 vcpu
->cpuid_nent
= cpuid
->nent
;
2399 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
2402 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
2403 vcpu
->sigset_active
= 1;
2404 vcpu
->sigset
= *sigset
;
2406 vcpu
->sigset_active
= 0;
2411 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2412 * we have asm/x86/processor.h
2423 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2424 #ifdef CONFIG_X86_64
2425 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2427 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2431 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2433 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2437 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2438 fpu
->fcw
= fxsave
->cwd
;
2439 fpu
->fsw
= fxsave
->swd
;
2440 fpu
->ftwx
= fxsave
->twd
;
2441 fpu
->last_opcode
= fxsave
->fop
;
2442 fpu
->last_ip
= fxsave
->rip
;
2443 fpu
->last_dp
= fxsave
->rdp
;
2444 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2451 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2453 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2457 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2458 fxsave
->cwd
= fpu
->fcw
;
2459 fxsave
->swd
= fpu
->fsw
;
2460 fxsave
->twd
= fpu
->ftwx
;
2461 fxsave
->fop
= fpu
->last_opcode
;
2462 fxsave
->rip
= fpu
->last_ip
;
2463 fxsave
->rdp
= fpu
->last_dp
;
2464 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2471 static long kvm_vcpu_ioctl(struct file
*filp
,
2472 unsigned int ioctl
, unsigned long arg
)
2474 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2475 void __user
*argp
= (void __user
*)arg
;
2483 r
= kvm_vcpu_ioctl_run(vcpu
, vcpu
->run
);
2485 case KVM_GET_REGS
: {
2486 struct kvm_regs kvm_regs
;
2488 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2489 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2493 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2498 case KVM_SET_REGS
: {
2499 struct kvm_regs kvm_regs
;
2502 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2504 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2510 case KVM_GET_SREGS
: {
2511 struct kvm_sregs kvm_sregs
;
2513 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2514 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2518 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2523 case KVM_SET_SREGS
: {
2524 struct kvm_sregs kvm_sregs
;
2527 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2529 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2535 case KVM_TRANSLATE
: {
2536 struct kvm_translation tr
;
2539 if (copy_from_user(&tr
, argp
, sizeof tr
))
2541 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2545 if (copy_to_user(argp
, &tr
, sizeof tr
))
2550 case KVM_INTERRUPT
: {
2551 struct kvm_interrupt irq
;
2554 if (copy_from_user(&irq
, argp
, sizeof irq
))
2556 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2562 case KVM_DEBUG_GUEST
: {
2563 struct kvm_debug_guest dbg
;
2566 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2568 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2575 r
= msr_io(vcpu
, argp
, get_msr
, 1);
2578 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2580 case KVM_SET_CPUID
: {
2581 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2582 struct kvm_cpuid cpuid
;
2585 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2587 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2592 case KVM_SET_SIGNAL_MASK
: {
2593 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
2594 struct kvm_signal_mask kvm_sigmask
;
2595 sigset_t sigset
, *p
;
2600 if (copy_from_user(&kvm_sigmask
, argp
,
2601 sizeof kvm_sigmask
))
2604 if (kvm_sigmask
.len
!= sizeof sigset
)
2607 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2612 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2618 memset(&fpu
, 0, sizeof fpu
);
2619 r
= kvm_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
2623 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
2632 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
2634 r
= kvm_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
2647 static long kvm_vm_ioctl(struct file
*filp
,
2648 unsigned int ioctl
, unsigned long arg
)
2650 struct kvm
*kvm
= filp
->private_data
;
2651 void __user
*argp
= (void __user
*)arg
;
2655 case KVM_CREATE_VCPU
:
2656 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2660 case KVM_SET_MEMORY_REGION
: {
2661 struct kvm_memory_region kvm_mem
;
2664 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2666 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
2671 case KVM_GET_DIRTY_LOG
: {
2672 struct kvm_dirty_log log
;
2675 if (copy_from_user(&log
, argp
, sizeof log
))
2677 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2682 case KVM_SET_MEMORY_ALIAS
: {
2683 struct kvm_memory_alias alias
;
2686 if (copy_from_user(&alias
, argp
, sizeof alias
))
2688 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
2700 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
2701 unsigned long address
,
2704 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2705 unsigned long pgoff
;
2708 *type
= VM_FAULT_MINOR
;
2709 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2710 page
= gfn_to_page(kvm
, pgoff
);
2712 return NOPAGE_SIGBUS
;
2717 static struct vm_operations_struct kvm_vm_vm_ops
= {
2718 .nopage
= kvm_vm_nopage
,
2721 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2723 vma
->vm_ops
= &kvm_vm_vm_ops
;
2727 static struct file_operations kvm_vm_fops
= {
2728 .release
= kvm_vm_release
,
2729 .unlocked_ioctl
= kvm_vm_ioctl
,
2730 .compat_ioctl
= kvm_vm_ioctl
,
2731 .mmap
= kvm_vm_mmap
,
2734 static int kvm_dev_ioctl_create_vm(void)
2737 struct inode
*inode
;
2741 inode
= kvmfs_inode(&kvm_vm_fops
);
2742 if (IS_ERR(inode
)) {
2747 kvm
= kvm_create_vm();
2753 file
= kvmfs_file(inode
, kvm
);
2760 r
= get_unused_fd();
2764 fd_install(fd
, file
);
2771 kvm_destroy_vm(kvm
);
2778 static long kvm_dev_ioctl(struct file
*filp
,
2779 unsigned int ioctl
, unsigned long arg
)
2781 void __user
*argp
= (void __user
*)arg
;
2785 case KVM_GET_API_VERSION
:
2789 r
= KVM_API_VERSION
;
2795 r
= kvm_dev_ioctl_create_vm();
2797 case KVM_GET_MSR_INDEX_LIST
: {
2798 struct kvm_msr_list __user
*user_msr_list
= argp
;
2799 struct kvm_msr_list msr_list
;
2803 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
2806 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
2807 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
2810 if (n
< num_msrs_to_save
)
2813 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
2814 num_msrs_to_save
* sizeof(u32
)))
2816 if (copy_to_user(user_msr_list
->indices
2817 + num_msrs_to_save
* sizeof(u32
),
2819 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
2824 case KVM_CHECK_EXTENSION
:
2826 * No extensions defined at present.
2830 case KVM_GET_VCPU_MMAP_SIZE
:
2843 static struct file_operations kvm_chardev_ops
= {
2844 .open
= kvm_dev_open
,
2845 .release
= kvm_dev_release
,
2846 .unlocked_ioctl
= kvm_dev_ioctl
,
2847 .compat_ioctl
= kvm_dev_ioctl
,
2850 static struct miscdevice kvm_dev
= {
2856 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2859 if (val
== SYS_RESTART
) {
2861 * Some (well, at least mine) BIOSes hang on reboot if
2864 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2865 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
2870 static struct notifier_block kvm_reboot_notifier
= {
2871 .notifier_call
= kvm_reboot
,
2876 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2879 static void decache_vcpus_on_cpu(int cpu
)
2882 struct kvm_vcpu
*vcpu
;
2885 spin_lock(&kvm_lock
);
2886 list_for_each_entry(vm
, &vm_list
, vm_list
)
2887 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2888 vcpu
= &vm
->vcpus
[i
];
2890 * If the vcpu is locked, then it is running on some
2891 * other cpu and therefore it is not cached on the
2894 * If it's not locked, check the last cpu it executed
2897 if (mutex_trylock(&vcpu
->mutex
)) {
2898 if (vcpu
->cpu
== cpu
) {
2899 kvm_arch_ops
->vcpu_decache(vcpu
);
2902 mutex_unlock(&vcpu
->mutex
);
2905 spin_unlock(&kvm_lock
);
2908 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2914 case CPU_DOWN_PREPARE
:
2915 case CPU_DOWN_PREPARE_FROZEN
:
2916 case CPU_UP_CANCELED
:
2917 case CPU_UP_CANCELED_FROZEN
:
2918 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2920 decache_vcpus_on_cpu(cpu
);
2921 smp_call_function_single(cpu
, kvm_arch_ops
->hardware_disable
,
2925 case CPU_ONLINE_FROZEN
:
2926 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2928 smp_call_function_single(cpu
, kvm_arch_ops
->hardware_enable
,
2935 static struct notifier_block kvm_cpu_notifier
= {
2936 .notifier_call
= kvm_cpu_hotplug
,
2937 .priority
= 20, /* must be > scheduler priority */
2940 static u64
stat_get(void *_offset
)
2942 unsigned offset
= (long)_offset
;
2945 struct kvm_vcpu
*vcpu
;
2948 spin_lock(&kvm_lock
);
2949 list_for_each_entry(kvm
, &vm_list
, vm_list
)
2950 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2951 vcpu
= &kvm
->vcpus
[i
];
2952 total
+= *(u32
*)((void *)vcpu
+ offset
);
2954 spin_unlock(&kvm_lock
);
2958 static void stat_set(void *offset
, u64 val
)
2962 DEFINE_SIMPLE_ATTRIBUTE(stat_fops
, stat_get
, stat_set
, "%llu\n");
2964 static __init
void kvm_init_debug(void)
2966 struct kvm_stats_debugfs_item
*p
;
2968 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2969 for (p
= debugfs_entries
; p
->name
; ++p
)
2970 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
2971 (void *)(long)p
->offset
,
2975 static void kvm_exit_debug(void)
2977 struct kvm_stats_debugfs_item
*p
;
2979 for (p
= debugfs_entries
; p
->name
; ++p
)
2980 debugfs_remove(p
->dentry
);
2981 debugfs_remove(debugfs_dir
);
2984 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2986 decache_vcpus_on_cpu(raw_smp_processor_id());
2987 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
2991 static int kvm_resume(struct sys_device
*dev
)
2993 on_each_cpu(kvm_arch_ops
->hardware_enable
, NULL
, 0, 1);
2997 static struct sysdev_class kvm_sysdev_class
= {
2998 set_kset_name("kvm"),
2999 .suspend
= kvm_suspend
,
3000 .resume
= kvm_resume
,
3003 static struct sys_device kvm_sysdev
= {
3005 .cls
= &kvm_sysdev_class
,
3008 hpa_t bad_page_address
;
3010 static int kvmfs_get_sb(struct file_system_type
*fs_type
, int flags
,
3011 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
3013 return get_sb_pseudo(fs_type
, "kvm:", NULL
, KVMFS_SUPER_MAGIC
, mnt
);
3016 static struct file_system_type kvm_fs_type
= {
3018 .get_sb
= kvmfs_get_sb
,
3019 .kill_sb
= kill_anon_super
,
3022 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
3027 printk(KERN_ERR
"kvm: already loaded the other module\n");
3031 if (!ops
->cpu_has_kvm_support()) {
3032 printk(KERN_ERR
"kvm: no hardware support\n");
3035 if (ops
->disabled_by_bios()) {
3036 printk(KERN_ERR
"kvm: disabled by bios\n");
3042 r
= kvm_arch_ops
->hardware_setup();
3046 on_each_cpu(kvm_arch_ops
->hardware_enable
, NULL
, 0, 1);
3047 r
= register_cpu_notifier(&kvm_cpu_notifier
);
3050 register_reboot_notifier(&kvm_reboot_notifier
);
3052 r
= sysdev_class_register(&kvm_sysdev_class
);
3056 r
= sysdev_register(&kvm_sysdev
);
3060 kvm_chardev_ops
.owner
= module
;
3062 r
= misc_register(&kvm_dev
);
3064 printk (KERN_ERR
"kvm: misc device register failed\n");
3071 sysdev_unregister(&kvm_sysdev
);
3073 sysdev_class_unregister(&kvm_sysdev_class
);
3075 unregister_reboot_notifier(&kvm_reboot_notifier
);
3076 unregister_cpu_notifier(&kvm_cpu_notifier
);
3078 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
3079 kvm_arch_ops
->hardware_unsetup();
3081 kvm_arch_ops
= NULL
;
3085 void kvm_exit_arch(void)
3087 misc_deregister(&kvm_dev
);
3088 sysdev_unregister(&kvm_sysdev
);
3089 sysdev_class_unregister(&kvm_sysdev_class
);
3090 unregister_reboot_notifier(&kvm_reboot_notifier
);
3091 unregister_cpu_notifier(&kvm_cpu_notifier
);
3092 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
3093 kvm_arch_ops
->hardware_unsetup();
3094 kvm_arch_ops
= NULL
;
3097 static __init
int kvm_init(void)
3099 static struct page
*bad_page
;
3102 r
= kvm_mmu_module_init();
3106 r
= register_filesystem(&kvm_fs_type
);
3110 kvmfs_mnt
= kern_mount(&kvm_fs_type
);
3111 r
= PTR_ERR(kvmfs_mnt
);
3112 if (IS_ERR(kvmfs_mnt
))
3116 kvm_init_msr_list();
3118 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
3123 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
3124 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
3132 unregister_filesystem(&kvm_fs_type
);
3134 kvm_mmu_module_exit();
3139 static __exit
void kvm_exit(void)
3142 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
3144 unregister_filesystem(&kvm_fs_type
);
3145 kvm_mmu_module_exit();
3148 module_init(kvm_init
)
3149 module_exit(kvm_exit
)
3151 EXPORT_SYMBOL_GPL(kvm_init_arch
);
3152 EXPORT_SYMBOL_GPL(kvm_exit_arch
);