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>
44 #include "x86_emulate.h"
45 #include "segment_descriptor.h"
47 MODULE_AUTHOR("Qumranet");
48 MODULE_LICENSE("GPL");
50 static DEFINE_SPINLOCK(kvm_lock
);
51 static LIST_HEAD(vm_list
);
53 struct kvm_arch_ops
*kvm_arch_ops
;
54 struct kvm_stat kvm_stat
;
55 EXPORT_SYMBOL_GPL(kvm_stat
);
57 static struct kvm_stats_debugfs_item
{
60 struct dentry
*dentry
;
61 } debugfs_entries
[] = {
62 { "pf_fixed", &kvm_stat
.pf_fixed
},
63 { "pf_guest", &kvm_stat
.pf_guest
},
64 { "tlb_flush", &kvm_stat
.tlb_flush
},
65 { "invlpg", &kvm_stat
.invlpg
},
66 { "exits", &kvm_stat
.exits
},
67 { "io_exits", &kvm_stat
.io_exits
},
68 { "mmio_exits", &kvm_stat
.mmio_exits
},
69 { "signal_exits", &kvm_stat
.signal_exits
},
70 { "irq_window", &kvm_stat
.irq_window_exits
},
71 { "halt_exits", &kvm_stat
.halt_exits
},
72 { "request_irq", &kvm_stat
.request_irq_exits
},
73 { "irq_exits", &kvm_stat
.irq_exits
},
77 static struct dentry
*debugfs_dir
;
79 struct vfsmount
*kvmfs_mnt
;
81 #define MAX_IO_MSRS 256
83 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
84 #define LMSW_GUEST_MASK 0x0eULL
85 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
86 #define CR8_RESEVED_BITS (~0x0fULL)
87 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
90 // LDT or TSS descriptor in the GDT. 16 bytes.
91 struct segment_descriptor_64
{
92 struct segment_descriptor s
;
99 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
102 static struct inode
*kvmfs_inode(struct file_operations
*fops
)
105 struct inode
*inode
= new_inode(kvmfs_mnt
->mnt_sb
);
113 * Mark the inode dirty from the very beginning,
114 * that way it will never be moved to the dirty
115 * list because mark_inode_dirty() will think
116 * that it already _is_ on the dirty list.
118 inode
->i_state
= I_DIRTY
;
119 inode
->i_mode
= S_IRUSR
| S_IWUSR
;
120 inode
->i_uid
= current
->fsuid
;
121 inode
->i_gid
= current
->fsgid
;
122 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
126 return ERR_PTR(error
);
129 static struct file
*kvmfs_file(struct inode
*inode
, void *private_data
)
131 struct file
*file
= get_empty_filp();
134 return ERR_PTR(-ENFILE
);
136 file
->f_path
.mnt
= mntget(kvmfs_mnt
);
137 file
->f_path
.dentry
= d_alloc_anon(inode
);
138 if (!file
->f_path
.dentry
)
139 return ERR_PTR(-ENOMEM
);
140 file
->f_mapping
= inode
->i_mapping
;
143 file
->f_flags
= O_RDWR
;
144 file
->f_op
= inode
->i_fop
;
145 file
->f_mode
= FMODE_READ
| FMODE_WRITE
;
147 file
->private_data
= private_data
;
151 unsigned long segment_base(u16 selector
)
153 struct descriptor_table gdt
;
154 struct segment_descriptor
*d
;
155 unsigned long table_base
;
156 typedef unsigned long ul
;
162 asm ("sgdt %0" : "=m"(gdt
));
163 table_base
= gdt
.base
;
165 if (selector
& 4) { /* from ldt */
168 asm ("sldt %0" : "=g"(ldt_selector
));
169 table_base
= segment_base(ldt_selector
);
171 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
172 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
175 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
176 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
180 EXPORT_SYMBOL_GPL(segment_base
);
182 static inline int valid_vcpu(int n
)
184 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
187 int kvm_read_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
190 unsigned char *host_buf
= dest
;
191 unsigned long req_size
= size
;
199 paddr
= gva_to_hpa(vcpu
, addr
);
201 if (is_error_hpa(paddr
))
204 guest_buf
= (hva_t
)kmap_atomic(
205 pfn_to_page(paddr
>> PAGE_SHIFT
),
207 offset
= addr
& ~PAGE_MASK
;
209 now
= min(size
, PAGE_SIZE
- offset
);
210 memcpy(host_buf
, (void*)guest_buf
, now
);
214 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
216 return req_size
- size
;
218 EXPORT_SYMBOL_GPL(kvm_read_guest
);
220 int kvm_write_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
223 unsigned char *host_buf
= data
;
224 unsigned long req_size
= size
;
233 paddr
= gva_to_hpa(vcpu
, addr
);
235 if (is_error_hpa(paddr
))
238 gfn
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
) >> PAGE_SHIFT
;
239 mark_page_dirty(vcpu
->kvm
, gfn
);
240 guest_buf
= (hva_t
)kmap_atomic(
241 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
242 offset
= addr
& ~PAGE_MASK
;
244 now
= min(size
, PAGE_SIZE
- offset
);
245 memcpy((void*)guest_buf
, host_buf
, now
);
249 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
251 return req_size
- size
;
253 EXPORT_SYMBOL_GPL(kvm_write_guest
);
256 * Switches to specified vcpu, until a matching vcpu_put()
258 static void vcpu_load(struct kvm_vcpu
*vcpu
)
260 mutex_lock(&vcpu
->mutex
);
261 kvm_arch_ops
->vcpu_load(vcpu
);
265 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
266 * if the slot is not populated.
268 static struct kvm_vcpu
*vcpu_load_slot(struct kvm
*kvm
, int slot
)
270 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[slot
];
272 mutex_lock(&vcpu
->mutex
);
274 mutex_unlock(&vcpu
->mutex
);
277 kvm_arch_ops
->vcpu_load(vcpu
);
281 static void vcpu_put(struct kvm_vcpu
*vcpu
)
283 kvm_arch_ops
->vcpu_put(vcpu
);
284 mutex_unlock(&vcpu
->mutex
);
287 static struct kvm
*kvm_create_vm(void)
289 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
293 return ERR_PTR(-ENOMEM
);
295 spin_lock_init(&kvm
->lock
);
296 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
297 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
298 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[i
];
300 mutex_init(&vcpu
->mutex
);
303 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
304 INIT_LIST_HEAD(&vcpu
->free_pages
);
305 spin_lock(&kvm_lock
);
306 list_add(&kvm
->vm_list
, &vm_list
);
307 spin_unlock(&kvm_lock
);
312 static int kvm_dev_open(struct inode
*inode
, struct file
*filp
)
318 * Free any memory in @free but not in @dont.
320 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
321 struct kvm_memory_slot
*dont
)
325 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
326 if (free
->phys_mem
) {
327 for (i
= 0; i
< free
->npages
; ++i
)
328 if (free
->phys_mem
[i
])
329 __free_page(free
->phys_mem
[i
]);
330 vfree(free
->phys_mem
);
333 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
334 vfree(free
->dirty_bitmap
);
336 free
->phys_mem
= NULL
;
338 free
->dirty_bitmap
= NULL
;
341 static void kvm_free_physmem(struct kvm
*kvm
)
345 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
346 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
349 static void kvm_free_vcpu(struct kvm_vcpu
*vcpu
)
355 kvm_mmu_destroy(vcpu
);
357 kvm_arch_ops
->vcpu_free(vcpu
);
360 static void kvm_free_vcpus(struct kvm
*kvm
)
364 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
365 kvm_free_vcpu(&kvm
->vcpus
[i
]);
368 static int kvm_dev_release(struct inode
*inode
, struct file
*filp
)
373 static void kvm_destroy_vm(struct kvm
*kvm
)
375 spin_lock(&kvm_lock
);
376 list_del(&kvm
->vm_list
);
377 spin_unlock(&kvm_lock
);
379 kvm_free_physmem(kvm
);
383 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
385 struct kvm
*kvm
= filp
->private_data
;
391 static void inject_gp(struct kvm_vcpu
*vcpu
)
393 kvm_arch_ops
->inject_gp(vcpu
, 0);
397 * Load the pae pdptrs. Return true is they are all valid.
399 static int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
401 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
402 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
407 struct kvm_memory_slot
*memslot
;
409 spin_lock(&vcpu
->kvm
->lock
);
410 memslot
= gfn_to_memslot(vcpu
->kvm
, pdpt_gfn
);
411 /* FIXME: !memslot - emulate? 0xff? */
412 pdpt
= kmap_atomic(gfn_to_page(memslot
, pdpt_gfn
), KM_USER0
);
415 for (i
= 0; i
< 4; ++i
) {
416 pdpte
= pdpt
[offset
+ i
];
417 if ((pdpte
& 1) && (pdpte
& 0xfffffff0000001e6ull
)) {
423 for (i
= 0; i
< 4; ++i
)
424 vcpu
->pdptrs
[i
] = pdpt
[offset
+ i
];
427 kunmap_atomic(pdpt
, KM_USER0
);
428 spin_unlock(&vcpu
->kvm
->lock
);
433 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
435 if (cr0
& CR0_RESEVED_BITS
) {
436 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
442 if ((cr0
& CR0_NW_MASK
) && !(cr0
& CR0_CD_MASK
)) {
443 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
448 if ((cr0
& CR0_PG_MASK
) && !(cr0
& CR0_PE_MASK
)) {
449 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
450 "and a clear PE flag\n");
455 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
)) {
457 if ((vcpu
->shadow_efer
& EFER_LME
)) {
461 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
462 "in long mode while PAE is disabled\n");
466 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
468 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
469 "in long mode while CS.L == 1\n");
476 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
477 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
485 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
488 spin_lock(&vcpu
->kvm
->lock
);
489 kvm_mmu_reset_context(vcpu
);
490 spin_unlock(&vcpu
->kvm
->lock
);
493 EXPORT_SYMBOL_GPL(set_cr0
);
495 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
497 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
498 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
500 EXPORT_SYMBOL_GPL(lmsw
);
502 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
504 if (cr4
& CR4_RESEVED_BITS
) {
505 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
510 if (is_long_mode(vcpu
)) {
511 if (!(cr4
& CR4_PAE_MASK
)) {
512 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
517 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& CR4_PAE_MASK
)
518 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
519 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
523 if (cr4
& CR4_VMXE_MASK
) {
524 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
528 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
529 spin_lock(&vcpu
->kvm
->lock
);
530 kvm_mmu_reset_context(vcpu
);
531 spin_unlock(&vcpu
->kvm
->lock
);
533 EXPORT_SYMBOL_GPL(set_cr4
);
535 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
537 if (is_long_mode(vcpu
)) {
538 if (cr3
& CR3_L_MODE_RESEVED_BITS
) {
539 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
544 if (cr3
& CR3_RESEVED_BITS
) {
545 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
549 if (is_paging(vcpu
) && is_pae(vcpu
) &&
550 !load_pdptrs(vcpu
, cr3
)) {
551 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
559 spin_lock(&vcpu
->kvm
->lock
);
561 * Does the new cr3 value map to physical memory? (Note, we
562 * catch an invalid cr3 even in real-mode, because it would
563 * cause trouble later on when we turn on paging anyway.)
565 * A real CPU would silently accept an invalid cr3 and would
566 * attempt to use it - with largely undefined (and often hard
567 * to debug) behavior on the guest side.
569 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
572 vcpu
->mmu
.new_cr3(vcpu
);
573 spin_unlock(&vcpu
->kvm
->lock
);
575 EXPORT_SYMBOL_GPL(set_cr3
);
577 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
579 if ( cr8
& CR8_RESEVED_BITS
) {
580 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
586 EXPORT_SYMBOL_GPL(set_cr8
);
588 void fx_init(struct kvm_vcpu
*vcpu
)
590 struct __attribute__ ((__packed__
)) fx_image_s
{
596 u64 operand
;// fpu dp
602 fx_save(vcpu
->host_fx_image
);
604 fx_save(vcpu
->guest_fx_image
);
605 fx_restore(vcpu
->host_fx_image
);
607 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
608 fx_image
->mxcsr
= 0x1f80;
609 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
610 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
612 EXPORT_SYMBOL_GPL(fx_init
);
614 static void do_remove_write_access(struct kvm_vcpu
*vcpu
, int slot
)
616 spin_lock(&vcpu
->kvm
->lock
);
617 kvm_mmu_slot_remove_write_access(vcpu
, slot
);
618 spin_unlock(&vcpu
->kvm
->lock
);
622 * Allocate some memory and give it an address in the guest physical address
625 * Discontiguous memory is allowed, mostly for framebuffers.
627 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
628 struct kvm_memory_region
*mem
)
632 unsigned long npages
;
634 struct kvm_memory_slot
*memslot
;
635 struct kvm_memory_slot old
, new;
636 int memory_config_version
;
639 /* General sanity checks */
640 if (mem
->memory_size
& (PAGE_SIZE
- 1))
642 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
644 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
646 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
649 memslot
= &kvm
->memslots
[mem
->slot
];
650 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
651 npages
= mem
->memory_size
>> PAGE_SHIFT
;
654 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
657 spin_lock(&kvm
->lock
);
659 memory_config_version
= kvm
->memory_config_version
;
660 new = old
= *memslot
;
662 new.base_gfn
= base_gfn
;
664 new.flags
= mem
->flags
;
666 /* Disallow changing a memory slot's size. */
668 if (npages
&& old
.npages
&& npages
!= old
.npages
)
671 /* Check for overlaps */
673 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
674 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
678 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
679 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
683 * Do memory allocations outside lock. memory_config_version will
686 spin_unlock(&kvm
->lock
);
688 /* Deallocate if slot is being removed */
692 /* Free page dirty bitmap if unneeded */
693 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
694 new.dirty_bitmap
= NULL
;
698 /* Allocate if a slot is being created */
699 if (npages
&& !new.phys_mem
) {
700 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
705 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
706 for (i
= 0; i
< npages
; ++i
) {
707 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
709 if (!new.phys_mem
[i
])
711 set_page_private(new.phys_mem
[i
],0);
715 /* Allocate page dirty bitmap if needed */
716 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
717 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
719 new.dirty_bitmap
= vmalloc(dirty_bytes
);
720 if (!new.dirty_bitmap
)
722 memset(new.dirty_bitmap
, 0, dirty_bytes
);
725 spin_lock(&kvm
->lock
);
727 if (memory_config_version
!= kvm
->memory_config_version
) {
728 spin_unlock(&kvm
->lock
);
729 kvm_free_physmem_slot(&new, &old
);
737 if (mem
->slot
>= kvm
->nmemslots
)
738 kvm
->nmemslots
= mem
->slot
+ 1;
741 ++kvm
->memory_config_version
;
743 spin_unlock(&kvm
->lock
);
745 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
746 struct kvm_vcpu
*vcpu
;
748 vcpu
= vcpu_load_slot(kvm
, i
);
751 if (new.flags
& KVM_MEM_LOG_DIRTY_PAGES
)
752 do_remove_write_access(vcpu
, mem
->slot
);
753 kvm_mmu_reset_context(vcpu
);
757 kvm_free_physmem_slot(&old
, &new);
761 spin_unlock(&kvm
->lock
);
763 kvm_free_physmem_slot(&new, &old
);
769 * Get (and clear) the dirty memory log for a memory slot.
771 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
772 struct kvm_dirty_log
*log
)
774 struct kvm_memory_slot
*memslot
;
778 unsigned long any
= 0;
780 spin_lock(&kvm
->lock
);
783 * Prevent changes to guest memory configuration even while the lock
787 spin_unlock(&kvm
->lock
);
789 if (log
->slot
>= KVM_MEMORY_SLOTS
)
792 memslot
= &kvm
->memslots
[log
->slot
];
794 if (!memslot
->dirty_bitmap
)
797 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
799 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
800 any
= memslot
->dirty_bitmap
[i
];
803 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
808 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
809 struct kvm_vcpu
*vcpu
;
811 vcpu
= vcpu_load_slot(kvm
, i
);
815 do_remove_write_access(vcpu
, log
->slot
);
816 memset(memslot
->dirty_bitmap
, 0, n
);
819 kvm_arch_ops
->tlb_flush(vcpu
);
827 spin_lock(&kvm
->lock
);
829 spin_unlock(&kvm
->lock
);
833 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
837 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
838 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
840 if (gfn
>= memslot
->base_gfn
841 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
846 EXPORT_SYMBOL_GPL(gfn_to_memslot
);
848 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
851 struct kvm_memory_slot
*memslot
= NULL
;
852 unsigned long rel_gfn
;
854 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
855 memslot
= &kvm
->memslots
[i
];
857 if (gfn
>= memslot
->base_gfn
858 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
860 if (!memslot
|| !memslot
->dirty_bitmap
)
863 rel_gfn
= gfn
- memslot
->base_gfn
;
866 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
867 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
873 static int emulator_read_std(unsigned long addr
,
876 struct x86_emulate_ctxt
*ctxt
)
878 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
882 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
883 unsigned offset
= addr
& (PAGE_SIZE
-1);
884 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
886 struct kvm_memory_slot
*memslot
;
889 if (gpa
== UNMAPPED_GVA
)
890 return X86EMUL_PROPAGATE_FAULT
;
891 pfn
= gpa
>> PAGE_SHIFT
;
892 memslot
= gfn_to_memslot(vcpu
->kvm
, pfn
);
894 return X86EMUL_UNHANDLEABLE
;
895 page
= kmap_atomic(gfn_to_page(memslot
, pfn
), KM_USER0
);
897 memcpy(data
, page
+ offset
, tocopy
);
899 kunmap_atomic(page
, KM_USER0
);
906 return X86EMUL_CONTINUE
;
909 static int emulator_write_std(unsigned long addr
,
912 struct x86_emulate_ctxt
*ctxt
)
914 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
916 return X86EMUL_UNHANDLEABLE
;
919 static int emulator_read_emulated(unsigned long addr
,
922 struct x86_emulate_ctxt
*ctxt
)
924 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
926 if (vcpu
->mmio_read_completed
) {
927 memcpy(val
, vcpu
->mmio_data
, bytes
);
928 vcpu
->mmio_read_completed
= 0;
929 return X86EMUL_CONTINUE
;
930 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
932 return X86EMUL_CONTINUE
;
934 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
936 if (gpa
== UNMAPPED_GVA
)
937 return X86EMUL_PROPAGATE_FAULT
;
938 vcpu
->mmio_needed
= 1;
939 vcpu
->mmio_phys_addr
= gpa
;
940 vcpu
->mmio_size
= bytes
;
941 vcpu
->mmio_is_write
= 0;
943 return X86EMUL_UNHANDLEABLE
;
947 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
948 unsigned long val
, int bytes
)
950 struct kvm_memory_slot
*m
;
954 if (((gpa
+ bytes
- 1) >> PAGE_SHIFT
) != (gpa
>> PAGE_SHIFT
))
956 m
= gfn_to_memslot(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
959 page
= gfn_to_page(m
, gpa
>> PAGE_SHIFT
);
960 kvm_mmu_pre_write(vcpu
, gpa
, bytes
);
961 mark_page_dirty(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
962 virt
= kmap_atomic(page
, KM_USER0
);
963 memcpy(virt
+ offset_in_page(gpa
), &val
, bytes
);
964 kunmap_atomic(virt
, KM_USER0
);
965 kvm_mmu_post_write(vcpu
, gpa
, bytes
);
969 static int emulator_write_emulated(unsigned long addr
,
972 struct x86_emulate_ctxt
*ctxt
)
974 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
975 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
977 if (gpa
== UNMAPPED_GVA
)
978 return X86EMUL_PROPAGATE_FAULT
;
980 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
981 return X86EMUL_CONTINUE
;
983 vcpu
->mmio_needed
= 1;
984 vcpu
->mmio_phys_addr
= gpa
;
985 vcpu
->mmio_size
= bytes
;
986 vcpu
->mmio_is_write
= 1;
987 memcpy(vcpu
->mmio_data
, &val
, bytes
);
989 return X86EMUL_CONTINUE
;
992 static int emulator_cmpxchg_emulated(unsigned long addr
,
996 struct x86_emulate_ctxt
*ctxt
)
1002 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1004 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
1007 #ifdef CONFIG_X86_32
1009 static int emulator_cmpxchg8b_emulated(unsigned long addr
,
1010 unsigned long old_lo
,
1011 unsigned long old_hi
,
1012 unsigned long new_lo
,
1013 unsigned long new_hi
,
1014 struct x86_emulate_ctxt
*ctxt
)
1016 static int reported
;
1021 printk(KERN_WARNING
"kvm: emulating exchange8b as write\n");
1023 r
= emulator_write_emulated(addr
, new_lo
, 4, ctxt
);
1024 if (r
!= X86EMUL_CONTINUE
)
1026 return emulator_write_emulated(addr
+4, new_hi
, 4, ctxt
);
1031 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1033 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
1036 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1038 return X86EMUL_CONTINUE
;
1041 int emulate_clts(struct kvm_vcpu
*vcpu
)
1045 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1046 cr0
= vcpu
->cr0
& ~CR0_TS_MASK
;
1047 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
1048 return X86EMUL_CONTINUE
;
1051 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1053 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1057 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
1058 return X86EMUL_CONTINUE
;
1060 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
1062 return X86EMUL_UNHANDLEABLE
;
1066 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1068 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1071 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1073 /* FIXME: better handling */
1074 return X86EMUL_UNHANDLEABLE
;
1076 return X86EMUL_CONTINUE
;
1079 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
1081 static int reported
;
1083 unsigned long rip
= ctxt
->vcpu
->rip
;
1084 unsigned long rip_linear
;
1086 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
1091 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
1093 printk(KERN_ERR
"emulation failed but !mmio_needed?"
1094 " rip %lx %02x %02x %02x %02x\n",
1095 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1099 struct x86_emulate_ops emulate_ops
= {
1100 .read_std
= emulator_read_std
,
1101 .write_std
= emulator_write_std
,
1102 .read_emulated
= emulator_read_emulated
,
1103 .write_emulated
= emulator_write_emulated
,
1104 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1105 #ifdef CONFIG_X86_32
1106 .cmpxchg8b_emulated
= emulator_cmpxchg8b_emulated
,
1110 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1111 struct kvm_run
*run
,
1115 struct x86_emulate_ctxt emulate_ctxt
;
1119 kvm_arch_ops
->cache_regs(vcpu
);
1121 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1123 emulate_ctxt
.vcpu
= vcpu
;
1124 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1125 emulate_ctxt
.cr2
= cr2
;
1126 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1127 ? X86EMUL_MODE_REAL
: cs_l
1128 ? X86EMUL_MODE_PROT64
: cs_db
1129 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1131 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1132 emulate_ctxt
.cs_base
= 0;
1133 emulate_ctxt
.ds_base
= 0;
1134 emulate_ctxt
.es_base
= 0;
1135 emulate_ctxt
.ss_base
= 0;
1137 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1138 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1139 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1140 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1143 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1144 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1146 vcpu
->mmio_is_write
= 0;
1147 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1149 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1150 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1151 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1152 run
->mmio
.len
= vcpu
->mmio_size
;
1153 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1157 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1158 return EMULATE_DONE
;
1159 if (!vcpu
->mmio_needed
) {
1160 report_emulation_failure(&emulate_ctxt
);
1161 return EMULATE_FAIL
;
1163 return EMULATE_DO_MMIO
;
1166 kvm_arch_ops
->decache_regs(vcpu
);
1167 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1169 if (vcpu
->mmio_is_write
)
1170 return EMULATE_DO_MMIO
;
1172 return EMULATE_DONE
;
1174 EXPORT_SYMBOL_GPL(emulate_instruction
);
1176 int kvm_hypercall(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1178 unsigned long nr
, a0
, a1
, a2
, a3
, a4
, a5
, ret
;
1180 kvm_arch_ops
->decache_regs(vcpu
);
1182 #ifdef CONFIG_X86_64
1183 if (is_long_mode(vcpu
)) {
1184 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1185 a0
= vcpu
->regs
[VCPU_REGS_RDI
];
1186 a1
= vcpu
->regs
[VCPU_REGS_RSI
];
1187 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1188 a3
= vcpu
->regs
[VCPU_REGS_RCX
];
1189 a4
= vcpu
->regs
[VCPU_REGS_R8
];
1190 a5
= vcpu
->regs
[VCPU_REGS_R9
];
1194 nr
= vcpu
->regs
[VCPU_REGS_RBX
] & -1u;
1195 a0
= vcpu
->regs
[VCPU_REGS_RAX
] & -1u;
1196 a1
= vcpu
->regs
[VCPU_REGS_RCX
] & -1u;
1197 a2
= vcpu
->regs
[VCPU_REGS_RDX
] & -1u;
1198 a3
= vcpu
->regs
[VCPU_REGS_RSI
] & -1u;
1199 a4
= vcpu
->regs
[VCPU_REGS_RDI
] & -1u;
1200 a5
= vcpu
->regs
[VCPU_REGS_RBP
] & -1u;
1206 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1207 kvm_arch_ops
->cache_regs(vcpu
);
1210 EXPORT_SYMBOL_GPL(kvm_hypercall
);
1212 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1214 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1217 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1219 struct descriptor_table dt
= { limit
, base
};
1221 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1224 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1226 struct descriptor_table dt
= { limit
, base
};
1228 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1231 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1232 unsigned long *rflags
)
1235 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1238 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1240 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1251 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1256 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1257 unsigned long *rflags
)
1261 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1262 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1271 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1274 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1279 * Register the para guest with the host:
1281 static int vcpu_register_para(struct kvm_vcpu
*vcpu
, gpa_t para_state_gpa
)
1283 struct kvm_vcpu_para_state
*para_state
;
1284 hpa_t para_state_hpa
, hypercall_hpa
;
1285 struct page
*para_state_page
;
1286 unsigned char *hypercall
;
1287 gpa_t hypercall_gpa
;
1289 printk(KERN_DEBUG
"kvm: guest trying to enter paravirtual mode\n");
1290 printk(KERN_DEBUG
".... para_state_gpa: %08Lx\n", para_state_gpa
);
1293 * Needs to be page aligned:
1295 if (para_state_gpa
!= PAGE_ALIGN(para_state_gpa
))
1298 para_state_hpa
= gpa_to_hpa(vcpu
, para_state_gpa
);
1299 printk(KERN_DEBUG
".... para_state_hpa: %08Lx\n", para_state_hpa
);
1300 if (is_error_hpa(para_state_hpa
))
1303 mark_page_dirty(vcpu
->kvm
, para_state_gpa
>> PAGE_SHIFT
);
1304 para_state_page
= pfn_to_page(para_state_hpa
>> PAGE_SHIFT
);
1305 para_state
= kmap_atomic(para_state_page
, KM_USER0
);
1307 printk(KERN_DEBUG
".... guest version: %d\n", para_state
->guest_version
);
1308 printk(KERN_DEBUG
".... size: %d\n", para_state
->size
);
1310 para_state
->host_version
= KVM_PARA_API_VERSION
;
1312 * We cannot support guests that try to register themselves
1313 * with a newer API version than the host supports:
1315 if (para_state
->guest_version
> KVM_PARA_API_VERSION
) {
1316 para_state
->ret
= -KVM_EINVAL
;
1317 goto err_kunmap_skip
;
1320 hypercall_gpa
= para_state
->hypercall_gpa
;
1321 hypercall_hpa
= gpa_to_hpa(vcpu
, hypercall_gpa
);
1322 printk(KERN_DEBUG
".... hypercall_hpa: %08Lx\n", hypercall_hpa
);
1323 if (is_error_hpa(hypercall_hpa
)) {
1324 para_state
->ret
= -KVM_EINVAL
;
1325 goto err_kunmap_skip
;
1328 printk(KERN_DEBUG
"kvm: para guest successfully registered.\n");
1329 vcpu
->para_state_page
= para_state_page
;
1330 vcpu
->para_state_gpa
= para_state_gpa
;
1331 vcpu
->hypercall_gpa
= hypercall_gpa
;
1333 mark_page_dirty(vcpu
->kvm
, hypercall_gpa
>> PAGE_SHIFT
);
1334 hypercall
= kmap_atomic(pfn_to_page(hypercall_hpa
>> PAGE_SHIFT
),
1335 KM_USER1
) + (hypercall_hpa
& ~PAGE_MASK
);
1336 kvm_arch_ops
->patch_hypercall(vcpu
, hypercall
);
1337 kunmap_atomic(hypercall
, KM_USER1
);
1339 para_state
->ret
= 0;
1341 kunmap_atomic(para_state
, KM_USER0
);
1347 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1352 case 0xc0010010: /* SYSCFG */
1353 case 0xc0010015: /* HWCR */
1354 case MSR_IA32_PLATFORM_ID
:
1355 case MSR_IA32_P5_MC_ADDR
:
1356 case MSR_IA32_P5_MC_TYPE
:
1357 case MSR_IA32_MC0_CTL
:
1358 case MSR_IA32_MCG_STATUS
:
1359 case MSR_IA32_MCG_CAP
:
1360 case MSR_IA32_MC0_MISC
:
1361 case MSR_IA32_MC0_MISC
+4:
1362 case MSR_IA32_MC0_MISC
+8:
1363 case MSR_IA32_MC0_MISC
+12:
1364 case MSR_IA32_MC0_MISC
+16:
1365 case MSR_IA32_UCODE_REV
:
1366 case MSR_IA32_PERF_STATUS
:
1367 /* MTRR registers */
1369 case 0x200 ... 0x2ff:
1372 case 0xcd: /* fsb frequency */
1375 case MSR_IA32_APICBASE
:
1376 data
= vcpu
->apic_base
;
1378 case MSR_IA32_MISC_ENABLE
:
1379 data
= vcpu
->ia32_misc_enable_msr
;
1381 #ifdef CONFIG_X86_64
1383 data
= vcpu
->shadow_efer
;
1387 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1393 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1396 * Reads an msr value (of 'msr_index') into 'pdata'.
1397 * Returns 0 on success, non-0 otherwise.
1398 * Assumes vcpu_load() was already called.
1400 static int get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1402 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1405 #ifdef CONFIG_X86_64
1407 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1409 if (efer
& EFER_RESERVED_BITS
) {
1410 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1417 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1418 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1423 kvm_arch_ops
->set_efer(vcpu
, efer
);
1426 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1428 vcpu
->shadow_efer
= efer
;
1433 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1436 #ifdef CONFIG_X86_64
1438 set_efer(vcpu
, data
);
1441 case MSR_IA32_MC0_STATUS
:
1442 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1443 __FUNCTION__
, data
);
1445 case MSR_IA32_UCODE_REV
:
1446 case MSR_IA32_UCODE_WRITE
:
1447 case 0x200 ... 0x2ff: /* MTRRs */
1449 case MSR_IA32_APICBASE
:
1450 vcpu
->apic_base
= data
;
1452 case MSR_IA32_MISC_ENABLE
:
1453 vcpu
->ia32_misc_enable_msr
= data
;
1456 * This is the 'probe whether the host is KVM' logic:
1458 case MSR_KVM_API_MAGIC
:
1459 return vcpu_register_para(vcpu
, data
);
1462 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1467 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1470 * Writes msr value into into the appropriate "register".
1471 * Returns 0 on success, non-0 otherwise.
1472 * Assumes vcpu_load() was already called.
1474 static int set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1476 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1479 void kvm_resched(struct kvm_vcpu
*vcpu
)
1485 EXPORT_SYMBOL_GPL(kvm_resched
);
1487 void load_msrs(struct vmx_msr_entry
*e
, int n
)
1491 for (i
= 0; i
< n
; ++i
)
1492 wrmsrl(e
[i
].index
, e
[i
].data
);
1494 EXPORT_SYMBOL_GPL(load_msrs
);
1496 void save_msrs(struct vmx_msr_entry
*e
, int n
)
1500 for (i
= 0; i
< n
; ++i
)
1501 rdmsrl(e
[i
].index
, e
[i
].data
);
1503 EXPORT_SYMBOL_GPL(save_msrs
);
1505 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1511 /* re-sync apic's tpr */
1512 vcpu
->cr8
= kvm_run
->cr8
;
1514 if (kvm_run
->emulated
) {
1515 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1516 kvm_run
->emulated
= 0;
1519 if (kvm_run
->mmio_completed
) {
1520 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1521 vcpu
->mmio_read_completed
= 1;
1524 vcpu
->mmio_needed
= 0;
1526 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1532 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
1533 struct kvm_regs
*regs
)
1537 kvm_arch_ops
->cache_regs(vcpu
);
1539 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
1540 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
1541 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
1542 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
1543 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
1544 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
1545 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
1546 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
1547 #ifdef CONFIG_X86_64
1548 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
1549 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
1550 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
1551 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
1552 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
1553 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
1554 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
1555 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
1558 regs
->rip
= vcpu
->rip
;
1559 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1562 * Don't leak debug flags in case they were set for guest debugging
1564 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
1565 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1572 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
1573 struct kvm_regs
*regs
)
1577 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
1578 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
1579 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
1580 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
1581 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
1582 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
1583 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
1584 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
1585 #ifdef CONFIG_X86_64
1586 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
1587 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
1588 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
1589 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
1590 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
1591 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
1592 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
1593 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
1596 vcpu
->rip
= regs
->rip
;
1597 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
1599 kvm_arch_ops
->decache_regs(vcpu
);
1606 static void get_segment(struct kvm_vcpu
*vcpu
,
1607 struct kvm_segment
*var
, int seg
)
1609 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
1612 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1613 struct kvm_sregs
*sregs
)
1615 struct descriptor_table dt
;
1619 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
1620 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
1621 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
1622 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
1623 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
1624 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
1626 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
1627 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
1629 kvm_arch_ops
->get_idt(vcpu
, &dt
);
1630 sregs
->idt
.limit
= dt
.limit
;
1631 sregs
->idt
.base
= dt
.base
;
1632 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
1633 sregs
->gdt
.limit
= dt
.limit
;
1634 sregs
->gdt
.base
= dt
.base
;
1636 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1637 sregs
->cr0
= vcpu
->cr0
;
1638 sregs
->cr2
= vcpu
->cr2
;
1639 sregs
->cr3
= vcpu
->cr3
;
1640 sregs
->cr4
= vcpu
->cr4
;
1641 sregs
->cr8
= vcpu
->cr8
;
1642 sregs
->efer
= vcpu
->shadow_efer
;
1643 sregs
->apic_base
= vcpu
->apic_base
;
1645 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
1646 sizeof sregs
->interrupt_bitmap
);
1653 static void set_segment(struct kvm_vcpu
*vcpu
,
1654 struct kvm_segment
*var
, int seg
)
1656 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
1659 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
1660 struct kvm_sregs
*sregs
)
1662 int mmu_reset_needed
= 0;
1664 struct descriptor_table dt
;
1668 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
1669 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
1670 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
1671 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
1672 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
1673 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
1675 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
1676 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
1678 dt
.limit
= sregs
->idt
.limit
;
1679 dt
.base
= sregs
->idt
.base
;
1680 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1681 dt
.limit
= sregs
->gdt
.limit
;
1682 dt
.base
= sregs
->gdt
.base
;
1683 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1685 vcpu
->cr2
= sregs
->cr2
;
1686 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
1687 vcpu
->cr3
= sregs
->cr3
;
1689 vcpu
->cr8
= sregs
->cr8
;
1691 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
1692 #ifdef CONFIG_X86_64
1693 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
1695 vcpu
->apic_base
= sregs
->apic_base
;
1697 kvm_arch_ops
->decache_cr0_cr4_guest_bits(vcpu
);
1699 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
1700 kvm_arch_ops
->set_cr0_no_modeswitch(vcpu
, sregs
->cr0
);
1702 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
1703 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
1704 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
1705 load_pdptrs(vcpu
, vcpu
->cr3
);
1707 if (mmu_reset_needed
)
1708 kvm_mmu_reset_context(vcpu
);
1710 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
1711 sizeof vcpu
->irq_pending
);
1712 vcpu
->irq_summary
= 0;
1713 for (i
= 0; i
< NR_IRQ_WORDS
; ++i
)
1714 if (vcpu
->irq_pending
[i
])
1715 __set_bit(i
, &vcpu
->irq_summary
);
1723 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1724 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1726 * This list is modified at module load time to reflect the
1727 * capabilities of the host cpu.
1729 static u32 msrs_to_save
[] = {
1730 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
1732 #ifdef CONFIG_X86_64
1733 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
1735 MSR_IA32_TIME_STAMP_COUNTER
,
1738 static unsigned num_msrs_to_save
;
1740 static u32 emulated_msrs
[] = {
1741 MSR_IA32_MISC_ENABLE
,
1744 static __init
void kvm_init_msr_list(void)
1749 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1750 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1753 msrs_to_save
[j
] = msrs_to_save
[i
];
1756 num_msrs_to_save
= j
;
1760 * Adapt set_msr() to msr_io()'s calling convention
1762 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
1764 return set_msr(vcpu
, index
, *data
);
1768 * Read or write a bunch of msrs. All parameters are kernel addresses.
1770 * @return number of msrs set successfully.
1772 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
1773 struct kvm_msr_entry
*entries
,
1774 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1775 unsigned index
, u64
*data
))
1781 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
1782 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
1791 * Read or write a bunch of msrs. Parameters are user addresses.
1793 * @return number of msrs set successfully.
1795 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
1796 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1797 unsigned index
, u64
*data
),
1800 struct kvm_msrs msrs
;
1801 struct kvm_msr_entry
*entries
;
1806 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
1810 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
1814 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
1815 entries
= vmalloc(size
);
1820 if (copy_from_user(entries
, user_msrs
->entries
, size
))
1823 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
1828 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
1840 * Translate a guest virtual address to a guest physical address.
1842 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1843 struct kvm_translation
*tr
)
1845 unsigned long vaddr
= tr
->linear_address
;
1849 spin_lock(&vcpu
->kvm
->lock
);
1850 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
1851 tr
->physical_address
= gpa
;
1852 tr
->valid
= gpa
!= UNMAPPED_GVA
;
1855 spin_unlock(&vcpu
->kvm
->lock
);
1861 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1862 struct kvm_interrupt
*irq
)
1864 if (irq
->irq
< 0 || irq
->irq
>= 256)
1868 set_bit(irq
->irq
, vcpu
->irq_pending
);
1869 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
1876 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
1877 struct kvm_debug_guest
*dbg
)
1883 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
1890 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
1892 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1894 fput(vcpu
->kvm
->filp
);
1898 static struct file_operations kvm_vcpu_fops
= {
1899 .release
= kvm_vcpu_release
,
1900 .unlocked_ioctl
= kvm_vcpu_ioctl
,
1901 .compat_ioctl
= kvm_vcpu_ioctl
,
1905 * Allocates an inode for the vcpu.
1907 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
1910 struct inode
*inode
;
1913 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
1914 inode
= kvmfs_inode(&kvm_vcpu_fops
);
1915 if (IS_ERR(inode
)) {
1920 file
= kvmfs_file(inode
, vcpu
);
1926 r
= get_unused_fd();
1930 fd_install(fd
, file
);
1939 fput(vcpu
->kvm
->filp
);
1944 * Creates some virtual cpus. Good luck creating more than one.
1946 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
1949 struct kvm_vcpu
*vcpu
;
1955 vcpu
= &kvm
->vcpus
[n
];
1957 mutex_lock(&vcpu
->mutex
);
1960 mutex_unlock(&vcpu
->mutex
);
1964 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
1966 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
1968 r
= kvm_arch_ops
->vcpu_create(vcpu
);
1970 goto out_free_vcpus
;
1972 r
= kvm_mmu_create(vcpu
);
1974 goto out_free_vcpus
;
1976 kvm_arch_ops
->vcpu_load(vcpu
);
1977 r
= kvm_mmu_setup(vcpu
);
1979 r
= kvm_arch_ops
->vcpu_setup(vcpu
);
1983 goto out_free_vcpus
;
1985 r
= create_vcpu_fd(vcpu
);
1987 goto out_free_vcpus
;
1992 kvm_free_vcpu(vcpu
);
1993 mutex_unlock(&vcpu
->mutex
);
1998 static long kvm_vcpu_ioctl(struct file
*filp
,
1999 unsigned int ioctl
, unsigned long arg
)
2001 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2002 void __user
*argp
= (void __user
*)arg
;
2007 struct kvm_run kvm_run
;
2010 if (copy_from_user(&kvm_run
, argp
, sizeof kvm_run
))
2012 r
= kvm_vcpu_ioctl_run(vcpu
, &kvm_run
);
2013 if (r
< 0 && r
!= -EINTR
)
2015 if (copy_to_user(argp
, &kvm_run
, sizeof kvm_run
)) {
2021 case KVM_GET_REGS
: {
2022 struct kvm_regs kvm_regs
;
2024 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2025 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2029 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2034 case KVM_SET_REGS
: {
2035 struct kvm_regs kvm_regs
;
2038 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2040 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2046 case KVM_GET_SREGS
: {
2047 struct kvm_sregs kvm_sregs
;
2049 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2050 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2054 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2059 case KVM_SET_SREGS
: {
2060 struct kvm_sregs kvm_sregs
;
2063 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2065 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2071 case KVM_TRANSLATE
: {
2072 struct kvm_translation tr
;
2075 if (copy_from_user(&tr
, argp
, sizeof tr
))
2077 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2081 if (copy_to_user(argp
, &tr
, sizeof tr
))
2086 case KVM_INTERRUPT
: {
2087 struct kvm_interrupt irq
;
2090 if (copy_from_user(&irq
, argp
, sizeof irq
))
2092 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2098 case KVM_DEBUG_GUEST
: {
2099 struct kvm_debug_guest dbg
;
2102 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2104 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2111 r
= msr_io(vcpu
, argp
, get_msr
, 1);
2114 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2123 static long kvm_vm_ioctl(struct file
*filp
,
2124 unsigned int ioctl
, unsigned long arg
)
2126 struct kvm
*kvm
= filp
->private_data
;
2127 void __user
*argp
= (void __user
*)arg
;
2131 case KVM_CREATE_VCPU
:
2132 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2136 case KVM_SET_MEMORY_REGION
: {
2137 struct kvm_memory_region kvm_mem
;
2140 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2142 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
2147 case KVM_GET_DIRTY_LOG
: {
2148 struct kvm_dirty_log log
;
2151 if (copy_from_user(&log
, argp
, sizeof log
))
2153 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2165 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
2166 unsigned long address
,
2169 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2170 unsigned long pgoff
;
2171 struct kvm_memory_slot
*slot
;
2174 *type
= VM_FAULT_MINOR
;
2175 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2176 slot
= gfn_to_memslot(kvm
, pgoff
);
2178 return NOPAGE_SIGBUS
;
2179 page
= gfn_to_page(slot
, pgoff
);
2181 return NOPAGE_SIGBUS
;
2186 static struct vm_operations_struct kvm_vm_vm_ops
= {
2187 .nopage
= kvm_vm_nopage
,
2190 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2192 vma
->vm_ops
= &kvm_vm_vm_ops
;
2196 static struct file_operations kvm_vm_fops
= {
2197 .release
= kvm_vm_release
,
2198 .unlocked_ioctl
= kvm_vm_ioctl
,
2199 .compat_ioctl
= kvm_vm_ioctl
,
2200 .mmap
= kvm_vm_mmap
,
2203 static int kvm_dev_ioctl_create_vm(void)
2206 struct inode
*inode
;
2210 inode
= kvmfs_inode(&kvm_vm_fops
);
2211 if (IS_ERR(inode
)) {
2216 kvm
= kvm_create_vm();
2222 file
= kvmfs_file(inode
, kvm
);
2229 r
= get_unused_fd();
2233 fd_install(fd
, file
);
2240 kvm_destroy_vm(kvm
);
2247 static long kvm_dev_ioctl(struct file
*filp
,
2248 unsigned int ioctl
, unsigned long arg
)
2250 void __user
*argp
= (void __user
*)arg
;
2254 case KVM_GET_API_VERSION
:
2255 r
= KVM_API_VERSION
;
2258 r
= kvm_dev_ioctl_create_vm();
2260 case KVM_GET_MSR_INDEX_LIST
: {
2261 struct kvm_msr_list __user
*user_msr_list
= argp
;
2262 struct kvm_msr_list msr_list
;
2266 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
2269 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
2270 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
2273 if (n
< num_msrs_to_save
)
2276 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
2277 num_msrs_to_save
* sizeof(u32
)))
2279 if (copy_to_user(user_msr_list
->indices
2280 + num_msrs_to_save
* sizeof(u32
),
2282 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
2294 static struct file_operations kvm_chardev_ops
= {
2295 .open
= kvm_dev_open
,
2296 .release
= kvm_dev_release
,
2297 .unlocked_ioctl
= kvm_dev_ioctl
,
2298 .compat_ioctl
= kvm_dev_ioctl
,
2301 static struct miscdevice kvm_dev
= {
2307 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2310 if (val
== SYS_RESTART
) {
2312 * Some (well, at least mine) BIOSes hang on reboot if
2315 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2316 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
2321 static struct notifier_block kvm_reboot_notifier
= {
2322 .notifier_call
= kvm_reboot
,
2327 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2330 static void decache_vcpus_on_cpu(int cpu
)
2333 struct kvm_vcpu
*vcpu
;
2336 spin_lock(&kvm_lock
);
2337 list_for_each_entry(vm
, &vm_list
, vm_list
)
2338 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2339 vcpu
= &vm
->vcpus
[i
];
2341 * If the vcpu is locked, then it is running on some
2342 * other cpu and therefore it is not cached on the
2345 * If it's not locked, check the last cpu it executed
2348 if (mutex_trylock(&vcpu
->mutex
)) {
2349 if (vcpu
->cpu
== cpu
) {
2350 kvm_arch_ops
->vcpu_decache(vcpu
);
2353 mutex_unlock(&vcpu
->mutex
);
2356 spin_unlock(&kvm_lock
);
2359 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2365 case CPU_DOWN_PREPARE
:
2366 case CPU_UP_CANCELED
:
2367 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2369 decache_vcpus_on_cpu(cpu
);
2370 smp_call_function_single(cpu
, kvm_arch_ops
->hardware_disable
,
2374 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2376 smp_call_function_single(cpu
, kvm_arch_ops
->hardware_enable
,
2383 static struct notifier_block kvm_cpu_notifier
= {
2384 .notifier_call
= kvm_cpu_hotplug
,
2385 .priority
= 20, /* must be > scheduler priority */
2388 static __init
void kvm_init_debug(void)
2390 struct kvm_stats_debugfs_item
*p
;
2392 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
2393 for (p
= debugfs_entries
; p
->name
; ++p
)
2394 p
->dentry
= debugfs_create_u32(p
->name
, 0444, debugfs_dir
,
2398 static void kvm_exit_debug(void)
2400 struct kvm_stats_debugfs_item
*p
;
2402 for (p
= debugfs_entries
; p
->name
; ++p
)
2403 debugfs_remove(p
->dentry
);
2404 debugfs_remove(debugfs_dir
);
2407 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
2409 decache_vcpus_on_cpu(raw_smp_processor_id());
2410 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
2414 static int kvm_resume(struct sys_device
*dev
)
2416 on_each_cpu(kvm_arch_ops
->hardware_enable
, NULL
, 0, 1);
2420 static struct sysdev_class kvm_sysdev_class
= {
2421 set_kset_name("kvm"),
2422 .suspend
= kvm_suspend
,
2423 .resume
= kvm_resume
,
2426 static struct sys_device kvm_sysdev
= {
2428 .cls
= &kvm_sysdev_class
,
2431 hpa_t bad_page_address
;
2433 static int kvmfs_get_sb(struct file_system_type
*fs_type
, int flags
,
2434 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
2436 return get_sb_pseudo(fs_type
, "kvm:", NULL
, KVMFS_SUPER_MAGIC
, mnt
);
2439 static struct file_system_type kvm_fs_type
= {
2441 .get_sb
= kvmfs_get_sb
,
2442 .kill_sb
= kill_anon_super
,
2445 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
2450 printk(KERN_ERR
"kvm: already loaded the other module\n");
2454 if (!ops
->cpu_has_kvm_support()) {
2455 printk(KERN_ERR
"kvm: no hardware support\n");
2458 if (ops
->disabled_by_bios()) {
2459 printk(KERN_ERR
"kvm: disabled by bios\n");
2465 r
= kvm_arch_ops
->hardware_setup();
2469 on_each_cpu(kvm_arch_ops
->hardware_enable
, NULL
, 0, 1);
2470 r
= register_cpu_notifier(&kvm_cpu_notifier
);
2473 register_reboot_notifier(&kvm_reboot_notifier
);
2475 r
= sysdev_class_register(&kvm_sysdev_class
);
2479 r
= sysdev_register(&kvm_sysdev
);
2483 kvm_chardev_ops
.owner
= module
;
2485 r
= misc_register(&kvm_dev
);
2487 printk (KERN_ERR
"kvm: misc device register failed\n");
2494 sysdev_unregister(&kvm_sysdev
);
2496 sysdev_class_unregister(&kvm_sysdev_class
);
2498 unregister_reboot_notifier(&kvm_reboot_notifier
);
2499 unregister_cpu_notifier(&kvm_cpu_notifier
);
2501 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
2502 kvm_arch_ops
->hardware_unsetup();
2504 kvm_arch_ops
= NULL
;
2508 void kvm_exit_arch(void)
2510 misc_deregister(&kvm_dev
);
2511 sysdev_unregister(&kvm_sysdev
);
2512 sysdev_class_unregister(&kvm_sysdev_class
);
2513 unregister_reboot_notifier(&kvm_reboot_notifier
);
2514 unregister_cpu_notifier(&kvm_cpu_notifier
);
2515 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
2516 kvm_arch_ops
->hardware_unsetup();
2517 kvm_arch_ops
= NULL
;
2520 static __init
int kvm_init(void)
2522 static struct page
*bad_page
;
2525 r
= register_filesystem(&kvm_fs_type
);
2529 kvmfs_mnt
= kern_mount(&kvm_fs_type
);
2530 r
= PTR_ERR(kvmfs_mnt
);
2531 if (IS_ERR(kvmfs_mnt
))
2535 kvm_init_msr_list();
2537 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
2542 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
2543 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
2551 unregister_filesystem(&kvm_fs_type
);
2556 static __exit
void kvm_exit(void)
2559 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
2561 unregister_filesystem(&kvm_fs_type
);
2564 module_init(kvm_init
)
2565 module_exit(kvm_exit
)
2567 EXPORT_SYMBOL_GPL(kvm_init_arch
);
2568 EXPORT_SYMBOL_GPL(kvm_exit_arch
);