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>
44 #include <linux/cpumask.h>
45 #include <linux/smp.h>
47 #include "x86_emulate.h"
48 #include "segment_descriptor.h"
50 MODULE_AUTHOR("Qumranet");
51 MODULE_LICENSE("GPL");
53 static DEFINE_SPINLOCK(kvm_lock
);
54 static LIST_HEAD(vm_list
);
56 struct kvm_arch_ops
*kvm_arch_ops
;
58 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
60 static struct kvm_stats_debugfs_item
{
63 struct dentry
*dentry
;
64 } debugfs_entries
[] = {
65 { "pf_fixed", STAT_OFFSET(pf_fixed
) },
66 { "pf_guest", STAT_OFFSET(pf_guest
) },
67 { "tlb_flush", STAT_OFFSET(tlb_flush
) },
68 { "invlpg", STAT_OFFSET(invlpg
) },
69 { "exits", STAT_OFFSET(exits
) },
70 { "io_exits", STAT_OFFSET(io_exits
) },
71 { "mmio_exits", STAT_OFFSET(mmio_exits
) },
72 { "signal_exits", STAT_OFFSET(signal_exits
) },
73 { "irq_window", STAT_OFFSET(irq_window_exits
) },
74 { "halt_exits", STAT_OFFSET(halt_exits
) },
75 { "request_irq", STAT_OFFSET(request_irq_exits
) },
76 { "irq_exits", STAT_OFFSET(irq_exits
) },
77 { "light_exits", STAT_OFFSET(light_exits
) },
78 { "efer_reload", STAT_OFFSET(efer_reload
) },
82 static struct dentry
*debugfs_dir
;
84 struct vfsmount
*kvmfs_mnt
;
86 #define MAX_IO_MSRS 256
88 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
89 #define LMSW_GUEST_MASK 0x0eULL
90 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
91 #define CR8_RESEVED_BITS (~0x0fULL)
92 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
95 // LDT or TSS descriptor in the GDT. 16 bytes.
96 struct segment_descriptor_64
{
97 struct segment_descriptor s
;
104 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
107 static struct inode
*kvmfs_inode(struct file_operations
*fops
)
110 struct inode
*inode
= new_inode(kvmfs_mnt
->mnt_sb
);
118 * Mark the inode dirty from the very beginning,
119 * that way it will never be moved to the dirty
120 * list because mark_inode_dirty() will think
121 * that it already _is_ on the dirty list.
123 inode
->i_state
= I_DIRTY
;
124 inode
->i_mode
= S_IRUSR
| S_IWUSR
;
125 inode
->i_uid
= current
->fsuid
;
126 inode
->i_gid
= current
->fsgid
;
127 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
131 return ERR_PTR(error
);
134 static struct file
*kvmfs_file(struct inode
*inode
, void *private_data
)
136 struct file
*file
= get_empty_filp();
139 return ERR_PTR(-ENFILE
);
141 file
->f_path
.mnt
= mntget(kvmfs_mnt
);
142 file
->f_path
.dentry
= d_alloc_anon(inode
);
143 if (!file
->f_path
.dentry
)
144 return ERR_PTR(-ENOMEM
);
145 file
->f_mapping
= inode
->i_mapping
;
148 file
->f_flags
= O_RDWR
;
149 file
->f_op
= inode
->i_fop
;
150 file
->f_mode
= FMODE_READ
| FMODE_WRITE
;
152 file
->private_data
= private_data
;
156 unsigned long segment_base(u16 selector
)
158 struct descriptor_table gdt
;
159 struct segment_descriptor
*d
;
160 unsigned long table_base
;
161 typedef unsigned long ul
;
167 asm ("sgdt %0" : "=m"(gdt
));
168 table_base
= gdt
.base
;
170 if (selector
& 4) { /* from ldt */
173 asm ("sldt %0" : "=g"(ldt_selector
));
174 table_base
= segment_base(ldt_selector
);
176 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
177 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
180 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
181 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
185 EXPORT_SYMBOL_GPL(segment_base
);
187 static inline int valid_vcpu(int n
)
189 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
192 int kvm_read_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
195 unsigned char *host_buf
= dest
;
196 unsigned long req_size
= size
;
204 paddr
= gva_to_hpa(vcpu
, addr
);
206 if (is_error_hpa(paddr
))
209 guest_buf
= (hva_t
)kmap_atomic(
210 pfn_to_page(paddr
>> PAGE_SHIFT
),
212 offset
= addr
& ~PAGE_MASK
;
214 now
= min(size
, PAGE_SIZE
- offset
);
215 memcpy(host_buf
, (void*)guest_buf
, now
);
219 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
221 return req_size
- size
;
223 EXPORT_SYMBOL_GPL(kvm_read_guest
);
225 int kvm_write_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
228 unsigned char *host_buf
= data
;
229 unsigned long req_size
= size
;
238 paddr
= gva_to_hpa(vcpu
, addr
);
240 if (is_error_hpa(paddr
))
243 gfn
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
) >> PAGE_SHIFT
;
244 mark_page_dirty(vcpu
->kvm
, gfn
);
245 guest_buf
= (hva_t
)kmap_atomic(
246 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
247 offset
= addr
& ~PAGE_MASK
;
249 now
= min(size
, PAGE_SIZE
- offset
);
250 memcpy((void*)guest_buf
, host_buf
, now
);
254 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
256 return req_size
- size
;
258 EXPORT_SYMBOL_GPL(kvm_write_guest
);
260 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
262 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
265 vcpu
->guest_fpu_loaded
= 1;
266 fx_save(vcpu
->host_fx_image
);
267 fx_restore(vcpu
->guest_fx_image
);
269 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
271 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
273 if (!vcpu
->guest_fpu_loaded
)
276 vcpu
->guest_fpu_loaded
= 0;
277 fx_save(vcpu
->guest_fx_image
);
278 fx_restore(vcpu
->host_fx_image
);
280 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
283 * Switches to specified vcpu, until a matching vcpu_put()
285 static void vcpu_load(struct kvm_vcpu
*vcpu
)
287 mutex_lock(&vcpu
->mutex
);
288 kvm_arch_ops
->vcpu_load(vcpu
);
292 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
293 * if the slot is not populated.
295 static struct kvm_vcpu
*vcpu_load_slot(struct kvm
*kvm
, int slot
)
297 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[slot
];
299 mutex_lock(&vcpu
->mutex
);
301 mutex_unlock(&vcpu
->mutex
);
304 kvm_arch_ops
->vcpu_load(vcpu
);
308 static void vcpu_put(struct kvm_vcpu
*vcpu
)
310 kvm_arch_ops
->vcpu_put(vcpu
);
311 mutex_unlock(&vcpu
->mutex
);
314 static void ack_flush(void *_completed
)
316 atomic_t
*completed
= _completed
;
318 atomic_inc(completed
);
321 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
325 struct kvm_vcpu
*vcpu
;
328 atomic_set(&completed
, 0);
331 for (i
= 0; i
< kvm
->nvcpus
; ++i
) {
332 vcpu
= &kvm
->vcpus
[i
];
333 if (test_and_set_bit(KVM_TLB_FLUSH
, &vcpu
->requests
))
336 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
337 if (!cpu_isset(cpu
, cpus
)) {
344 * We really want smp_call_function_mask() here. But that's not
345 * available, so ipi all cpus in parallel and wait for them
348 for (cpu
= first_cpu(cpus
); cpu
!= NR_CPUS
; cpu
= next_cpu(cpu
, cpus
))
349 smp_call_function_single(cpu
, ack_flush
, &completed
, 1, 0);
350 while (atomic_read(&completed
) != needed
) {
356 static struct kvm
*kvm_create_vm(void)
358 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
362 return ERR_PTR(-ENOMEM
);
364 kvm_io_bus_init(&kvm
->pio_bus
);
365 spin_lock_init(&kvm
->lock
);
366 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
367 spin_lock(&kvm_lock
);
368 list_add(&kvm
->vm_list
, &vm_list
);
369 spin_unlock(&kvm_lock
);
370 kvm_io_bus_init(&kvm
->mmio_bus
);
371 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
372 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[i
];
374 mutex_init(&vcpu
->mutex
);
377 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
382 static int kvm_dev_open(struct inode
*inode
, struct file
*filp
)
388 * Free any memory in @free but not in @dont.
390 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
391 struct kvm_memory_slot
*dont
)
395 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
396 if (free
->phys_mem
) {
397 for (i
= 0; i
< free
->npages
; ++i
)
398 if (free
->phys_mem
[i
])
399 __free_page(free
->phys_mem
[i
]);
400 vfree(free
->phys_mem
);
403 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
404 vfree(free
->dirty_bitmap
);
406 free
->phys_mem
= NULL
;
408 free
->dirty_bitmap
= NULL
;
411 static void kvm_free_physmem(struct kvm
*kvm
)
415 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
416 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
419 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
423 for (i
= 0; i
< 2; ++i
)
424 if (vcpu
->pio
.guest_pages
[i
]) {
425 __free_page(vcpu
->pio
.guest_pages
[i
]);
426 vcpu
->pio
.guest_pages
[i
] = NULL
;
430 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
436 kvm_mmu_unload(vcpu
);
440 static void kvm_free_vcpu(struct kvm_vcpu
*vcpu
)
446 kvm_mmu_destroy(vcpu
);
448 kvm_arch_ops
->vcpu_free(vcpu
);
449 free_page((unsigned long)vcpu
->run
);
451 free_page((unsigned long)vcpu
->pio_data
);
452 vcpu
->pio_data
= NULL
;
453 free_pio_guest_pages(vcpu
);
456 static void kvm_free_vcpus(struct kvm
*kvm
)
461 * Unpin any mmu pages first.
463 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
464 kvm_unload_vcpu_mmu(&kvm
->vcpus
[i
]);
465 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
466 kvm_free_vcpu(&kvm
->vcpus
[i
]);
469 static int kvm_dev_release(struct inode
*inode
, struct file
*filp
)
474 static void kvm_destroy_vm(struct kvm
*kvm
)
476 spin_lock(&kvm_lock
);
477 list_del(&kvm
->vm_list
);
478 spin_unlock(&kvm_lock
);
479 kvm_io_bus_destroy(&kvm
->pio_bus
);
480 kvm_io_bus_destroy(&kvm
->mmio_bus
);
482 kvm_free_physmem(kvm
);
486 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
488 struct kvm
*kvm
= filp
->private_data
;
494 static void inject_gp(struct kvm_vcpu
*vcpu
)
496 kvm_arch_ops
->inject_gp(vcpu
, 0);
500 * Load the pae pdptrs. Return true is they are all valid.
502 static int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
504 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
505 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
512 spin_lock(&vcpu
->kvm
->lock
);
513 page
= gfn_to_page(vcpu
->kvm
, pdpt_gfn
);
514 /* FIXME: !page - emulate? 0xff? */
515 pdpt
= kmap_atomic(page
, KM_USER0
);
518 for (i
= 0; i
< 4; ++i
) {
519 pdpte
= pdpt
[offset
+ i
];
520 if ((pdpte
& 1) && (pdpte
& 0xfffffff0000001e6ull
)) {
526 for (i
= 0; i
< 4; ++i
)
527 vcpu
->pdptrs
[i
] = pdpt
[offset
+ i
];
530 kunmap_atomic(pdpt
, KM_USER0
);
531 spin_unlock(&vcpu
->kvm
->lock
);
536 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
538 if (cr0
& CR0_RESEVED_BITS
) {
539 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
545 if ((cr0
& CR0_NW_MASK
) && !(cr0
& CR0_CD_MASK
)) {
546 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
551 if ((cr0
& CR0_PG_MASK
) && !(cr0
& CR0_PE_MASK
)) {
552 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
553 "and a clear PE flag\n");
558 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
)) {
560 if ((vcpu
->shadow_efer
& EFER_LME
)) {
564 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
565 "in long mode while PAE is disabled\n");
569 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
571 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
572 "in long mode while CS.L == 1\n");
579 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
580 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
588 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
591 spin_lock(&vcpu
->kvm
->lock
);
592 kvm_mmu_reset_context(vcpu
);
593 spin_unlock(&vcpu
->kvm
->lock
);
596 EXPORT_SYMBOL_GPL(set_cr0
);
598 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
600 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
602 EXPORT_SYMBOL_GPL(lmsw
);
604 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
606 if (cr4
& CR4_RESEVED_BITS
) {
607 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
612 if (is_long_mode(vcpu
)) {
613 if (!(cr4
& CR4_PAE_MASK
)) {
614 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
619 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& CR4_PAE_MASK
)
620 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
621 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
625 if (cr4
& CR4_VMXE_MASK
) {
626 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
630 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
631 spin_lock(&vcpu
->kvm
->lock
);
632 kvm_mmu_reset_context(vcpu
);
633 spin_unlock(&vcpu
->kvm
->lock
);
635 EXPORT_SYMBOL_GPL(set_cr4
);
637 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
639 if (is_long_mode(vcpu
)) {
640 if (cr3
& CR3_L_MODE_RESEVED_BITS
) {
641 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
646 if (cr3
& CR3_RESEVED_BITS
) {
647 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
651 if (is_paging(vcpu
) && is_pae(vcpu
) &&
652 !load_pdptrs(vcpu
, cr3
)) {
653 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
661 spin_lock(&vcpu
->kvm
->lock
);
663 * Does the new cr3 value map to physical memory? (Note, we
664 * catch an invalid cr3 even in real-mode, because it would
665 * cause trouble later on when we turn on paging anyway.)
667 * A real CPU would silently accept an invalid cr3 and would
668 * attempt to use it - with largely undefined (and often hard
669 * to debug) behavior on the guest side.
671 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
674 vcpu
->mmu
.new_cr3(vcpu
);
675 spin_unlock(&vcpu
->kvm
->lock
);
677 EXPORT_SYMBOL_GPL(set_cr3
);
679 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
681 if ( cr8
& CR8_RESEVED_BITS
) {
682 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
688 EXPORT_SYMBOL_GPL(set_cr8
);
690 void fx_init(struct kvm_vcpu
*vcpu
)
692 struct __attribute__ ((__packed__
)) fx_image_s
{
698 u64 operand
;// fpu dp
704 fx_save(vcpu
->host_fx_image
);
706 fx_save(vcpu
->guest_fx_image
);
707 fx_restore(vcpu
->host_fx_image
);
709 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
710 fx_image
->mxcsr
= 0x1f80;
711 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
712 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
714 EXPORT_SYMBOL_GPL(fx_init
);
716 static void do_remove_write_access(struct kvm_vcpu
*vcpu
, int slot
)
718 spin_lock(&vcpu
->kvm
->lock
);
719 kvm_mmu_slot_remove_write_access(vcpu
, slot
);
720 spin_unlock(&vcpu
->kvm
->lock
);
724 * Allocate some memory and give it an address in the guest physical address
727 * Discontiguous memory is allowed, mostly for framebuffers.
729 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
730 struct kvm_memory_region
*mem
)
734 unsigned long npages
;
736 struct kvm_memory_slot
*memslot
;
737 struct kvm_memory_slot old
, new;
738 int memory_config_version
;
741 /* General sanity checks */
742 if (mem
->memory_size
& (PAGE_SIZE
- 1))
744 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
746 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
748 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
751 memslot
= &kvm
->memslots
[mem
->slot
];
752 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
753 npages
= mem
->memory_size
>> PAGE_SHIFT
;
756 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
759 spin_lock(&kvm
->lock
);
761 memory_config_version
= kvm
->memory_config_version
;
762 new = old
= *memslot
;
764 new.base_gfn
= base_gfn
;
766 new.flags
= mem
->flags
;
768 /* Disallow changing a memory slot's size. */
770 if (npages
&& old
.npages
&& npages
!= old
.npages
)
773 /* Check for overlaps */
775 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
776 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
780 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
781 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
785 * Do memory allocations outside lock. memory_config_version will
788 spin_unlock(&kvm
->lock
);
790 /* Deallocate if slot is being removed */
794 /* Free page dirty bitmap if unneeded */
795 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
796 new.dirty_bitmap
= NULL
;
800 /* Allocate if a slot is being created */
801 if (npages
&& !new.phys_mem
) {
802 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
807 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
808 for (i
= 0; i
< npages
; ++i
) {
809 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
811 if (!new.phys_mem
[i
])
813 set_page_private(new.phys_mem
[i
],0);
817 /* Allocate page dirty bitmap if needed */
818 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
819 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
821 new.dirty_bitmap
= vmalloc(dirty_bytes
);
822 if (!new.dirty_bitmap
)
824 memset(new.dirty_bitmap
, 0, dirty_bytes
);
827 spin_lock(&kvm
->lock
);
829 if (memory_config_version
!= kvm
->memory_config_version
) {
830 spin_unlock(&kvm
->lock
);
831 kvm_free_physmem_slot(&new, &old
);
839 if (mem
->slot
>= kvm
->nmemslots
)
840 kvm
->nmemslots
= mem
->slot
+ 1;
843 ++kvm
->memory_config_version
;
845 spin_unlock(&kvm
->lock
);
847 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
848 struct kvm_vcpu
*vcpu
;
850 vcpu
= vcpu_load_slot(kvm
, i
);
853 if (new.flags
& KVM_MEM_LOG_DIRTY_PAGES
)
854 do_remove_write_access(vcpu
, mem
->slot
);
855 kvm_mmu_reset_context(vcpu
);
859 kvm_free_physmem_slot(&old
, &new);
863 spin_unlock(&kvm
->lock
);
865 kvm_free_physmem_slot(&new, &old
);
871 * Get (and clear) the dirty memory log for a memory slot.
873 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
874 struct kvm_dirty_log
*log
)
876 struct kvm_memory_slot
*memslot
;
880 unsigned long any
= 0;
882 spin_lock(&kvm
->lock
);
885 * Prevent changes to guest memory configuration even while the lock
889 spin_unlock(&kvm
->lock
);
891 if (log
->slot
>= KVM_MEMORY_SLOTS
)
894 memslot
= &kvm
->memslots
[log
->slot
];
896 if (!memslot
->dirty_bitmap
)
899 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
901 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
902 any
= memslot
->dirty_bitmap
[i
];
905 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
910 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
911 struct kvm_vcpu
*vcpu
;
913 vcpu
= vcpu_load_slot(kvm
, i
);
917 do_remove_write_access(vcpu
, log
->slot
);
918 memset(memslot
->dirty_bitmap
, 0, n
);
921 kvm_arch_ops
->tlb_flush(vcpu
);
929 spin_lock(&kvm
->lock
);
931 spin_unlock(&kvm
->lock
);
936 * Set a new alias region. Aliases map a portion of physical memory into
937 * another portion. This is useful for memory windows, for example the PC
940 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
941 struct kvm_memory_alias
*alias
)
944 struct kvm_mem_alias
*p
;
947 /* General sanity checks */
948 if (alias
->memory_size
& (PAGE_SIZE
- 1))
950 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
952 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
954 if (alias
->guest_phys_addr
+ alias
->memory_size
955 < alias
->guest_phys_addr
)
957 if (alias
->target_phys_addr
+ alias
->memory_size
958 < alias
->target_phys_addr
)
961 spin_lock(&kvm
->lock
);
963 p
= &kvm
->aliases
[alias
->slot
];
964 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
965 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
966 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
968 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
969 if (kvm
->aliases
[n
- 1].npages
)
973 spin_unlock(&kvm
->lock
);
975 vcpu_load(&kvm
->vcpus
[0]);
976 spin_lock(&kvm
->lock
);
977 kvm_mmu_zap_all(&kvm
->vcpus
[0]);
978 spin_unlock(&kvm
->lock
);
979 vcpu_put(&kvm
->vcpus
[0]);
987 static gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
990 struct kvm_mem_alias
*alias
;
992 for (i
= 0; i
< kvm
->naliases
; ++i
) {
993 alias
= &kvm
->aliases
[i
];
994 if (gfn
>= alias
->base_gfn
995 && gfn
< alias
->base_gfn
+ alias
->npages
)
996 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1001 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1005 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1006 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
1008 if (gfn
>= memslot
->base_gfn
1009 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
1015 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
1017 gfn
= unalias_gfn(kvm
, gfn
);
1018 return __gfn_to_memslot(kvm
, gfn
);
1021 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
1023 struct kvm_memory_slot
*slot
;
1025 gfn
= unalias_gfn(kvm
, gfn
);
1026 slot
= __gfn_to_memslot(kvm
, gfn
);
1029 return slot
->phys_mem
[gfn
- slot
->base_gfn
];
1031 EXPORT_SYMBOL_GPL(gfn_to_page
);
1033 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
1036 struct kvm_memory_slot
*memslot
;
1037 unsigned long rel_gfn
;
1039 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1040 memslot
= &kvm
->memslots
[i
];
1042 if (gfn
>= memslot
->base_gfn
1043 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
1045 if (!memslot
->dirty_bitmap
)
1048 rel_gfn
= gfn
- memslot
->base_gfn
;
1051 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1052 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1058 static int emulator_read_std(unsigned long addr
,
1061 struct x86_emulate_ctxt
*ctxt
)
1063 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1067 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1068 unsigned offset
= addr
& (PAGE_SIZE
-1);
1069 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1074 if (gpa
== UNMAPPED_GVA
)
1075 return X86EMUL_PROPAGATE_FAULT
;
1076 pfn
= gpa
>> PAGE_SHIFT
;
1077 page
= gfn_to_page(vcpu
->kvm
, pfn
);
1079 return X86EMUL_UNHANDLEABLE
;
1080 page_virt
= kmap_atomic(page
, KM_USER0
);
1082 memcpy(data
, page_virt
+ offset
, tocopy
);
1084 kunmap_atomic(page_virt
, KM_USER0
);
1091 return X86EMUL_CONTINUE
;
1094 static int emulator_write_std(unsigned long addr
,
1097 struct x86_emulate_ctxt
*ctxt
)
1099 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
1101 return X86EMUL_UNHANDLEABLE
;
1104 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1108 * Note that its important to have this wrapper function because
1109 * in the very near future we will be checking for MMIOs against
1110 * the LAPIC as well as the general MMIO bus
1112 return kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1115 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1118 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1121 static int emulator_read_emulated(unsigned long addr
,
1124 struct x86_emulate_ctxt
*ctxt
)
1126 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1127 struct kvm_io_device
*mmio_dev
;
1130 if (vcpu
->mmio_read_completed
) {
1131 memcpy(val
, vcpu
->mmio_data
, bytes
);
1132 vcpu
->mmio_read_completed
= 0;
1133 return X86EMUL_CONTINUE
;
1134 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
1135 == X86EMUL_CONTINUE
)
1136 return X86EMUL_CONTINUE
;
1138 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1139 if (gpa
== UNMAPPED_GVA
)
1140 return X86EMUL_PROPAGATE_FAULT
;
1143 * Is this MMIO handled locally?
1145 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1147 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1148 return X86EMUL_CONTINUE
;
1151 vcpu
->mmio_needed
= 1;
1152 vcpu
->mmio_phys_addr
= gpa
;
1153 vcpu
->mmio_size
= bytes
;
1154 vcpu
->mmio_is_write
= 0;
1156 return X86EMUL_UNHANDLEABLE
;
1159 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1160 const void *val
, int bytes
)
1164 unsigned offset
= offset_in_page(gpa
);
1166 if (((gpa
+ bytes
- 1) >> PAGE_SHIFT
) != (gpa
>> PAGE_SHIFT
))
1168 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1171 mark_page_dirty(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1172 virt
= kmap_atomic(page
, KM_USER0
);
1173 kvm_mmu_pte_write(vcpu
, gpa
, virt
+ offset
, val
, bytes
);
1174 memcpy(virt
+ offset_in_page(gpa
), val
, bytes
);
1175 kunmap_atomic(virt
, KM_USER0
);
1179 static int emulator_write_emulated(unsigned long addr
,
1182 struct x86_emulate_ctxt
*ctxt
)
1184 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1185 struct kvm_io_device
*mmio_dev
;
1186 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1188 if (gpa
== UNMAPPED_GVA
) {
1189 kvm_arch_ops
->inject_page_fault(vcpu
, addr
, 2);
1190 return X86EMUL_PROPAGATE_FAULT
;
1193 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1194 return X86EMUL_CONTINUE
;
1197 * Is this MMIO handled locally?
1199 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1201 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1202 return X86EMUL_CONTINUE
;
1205 vcpu
->mmio_needed
= 1;
1206 vcpu
->mmio_phys_addr
= gpa
;
1207 vcpu
->mmio_size
= bytes
;
1208 vcpu
->mmio_is_write
= 1;
1209 memcpy(vcpu
->mmio_data
, val
, bytes
);
1211 return X86EMUL_CONTINUE
;
1214 static int emulator_cmpxchg_emulated(unsigned long addr
,
1218 struct x86_emulate_ctxt
*ctxt
)
1220 static int reported
;
1224 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1226 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
1229 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1231 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
1234 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1236 return X86EMUL_CONTINUE
;
1239 int emulate_clts(struct kvm_vcpu
*vcpu
)
1243 cr0
= vcpu
->cr0
& ~CR0_TS_MASK
;
1244 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
1245 return X86EMUL_CONTINUE
;
1248 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1250 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1254 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
1255 return X86EMUL_CONTINUE
;
1257 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
1259 return X86EMUL_UNHANDLEABLE
;
1263 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1265 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1268 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1270 /* FIXME: better handling */
1271 return X86EMUL_UNHANDLEABLE
;
1273 return X86EMUL_CONTINUE
;
1276 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
1278 static int reported
;
1280 unsigned long rip
= ctxt
->vcpu
->rip
;
1281 unsigned long rip_linear
;
1283 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
1288 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
1290 printk(KERN_ERR
"emulation failed but !mmio_needed?"
1291 " rip %lx %02x %02x %02x %02x\n",
1292 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1296 struct x86_emulate_ops emulate_ops
= {
1297 .read_std
= emulator_read_std
,
1298 .write_std
= emulator_write_std
,
1299 .read_emulated
= emulator_read_emulated
,
1300 .write_emulated
= emulator_write_emulated
,
1301 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1304 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1305 struct kvm_run
*run
,
1309 struct x86_emulate_ctxt emulate_ctxt
;
1313 vcpu
->mmio_fault_cr2
= cr2
;
1314 kvm_arch_ops
->cache_regs(vcpu
);
1316 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1318 emulate_ctxt
.vcpu
= vcpu
;
1319 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1320 emulate_ctxt
.cr2
= cr2
;
1321 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1322 ? X86EMUL_MODE_REAL
: cs_l
1323 ? X86EMUL_MODE_PROT64
: cs_db
1324 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1326 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1327 emulate_ctxt
.cs_base
= 0;
1328 emulate_ctxt
.ds_base
= 0;
1329 emulate_ctxt
.es_base
= 0;
1330 emulate_ctxt
.ss_base
= 0;
1332 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1333 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1334 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1335 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1338 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1339 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1341 vcpu
->mmio_is_write
= 0;
1342 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1344 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1345 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1346 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1347 run
->mmio
.len
= vcpu
->mmio_size
;
1348 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1352 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1353 return EMULATE_DONE
;
1354 if (!vcpu
->mmio_needed
) {
1355 report_emulation_failure(&emulate_ctxt
);
1356 return EMULATE_FAIL
;
1358 return EMULATE_DO_MMIO
;
1361 kvm_arch_ops
->decache_regs(vcpu
);
1362 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1364 if (vcpu
->mmio_is_write
) {
1365 vcpu
->mmio_needed
= 0;
1366 return EMULATE_DO_MMIO
;
1369 return EMULATE_DONE
;
1371 EXPORT_SYMBOL_GPL(emulate_instruction
);
1373 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
1375 if (vcpu
->irq_summary
)
1378 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
1379 ++vcpu
->stat
.halt_exits
;
1382 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
1384 int kvm_hypercall(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1386 unsigned long nr
, a0
, a1
, a2
, a3
, a4
, a5
, ret
;
1388 kvm_arch_ops
->cache_regs(vcpu
);
1390 #ifdef CONFIG_X86_64
1391 if (is_long_mode(vcpu
)) {
1392 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1393 a0
= vcpu
->regs
[VCPU_REGS_RDI
];
1394 a1
= vcpu
->regs
[VCPU_REGS_RSI
];
1395 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1396 a3
= vcpu
->regs
[VCPU_REGS_RCX
];
1397 a4
= vcpu
->regs
[VCPU_REGS_R8
];
1398 a5
= vcpu
->regs
[VCPU_REGS_R9
];
1402 nr
= vcpu
->regs
[VCPU_REGS_RBX
] & -1u;
1403 a0
= vcpu
->regs
[VCPU_REGS_RAX
] & -1u;
1404 a1
= vcpu
->regs
[VCPU_REGS_RCX
] & -1u;
1405 a2
= vcpu
->regs
[VCPU_REGS_RDX
] & -1u;
1406 a3
= vcpu
->regs
[VCPU_REGS_RSI
] & -1u;
1407 a4
= vcpu
->regs
[VCPU_REGS_RDI
] & -1u;
1408 a5
= vcpu
->regs
[VCPU_REGS_RBP
] & -1u;
1412 run
->hypercall
.args
[0] = a0
;
1413 run
->hypercall
.args
[1] = a1
;
1414 run
->hypercall
.args
[2] = a2
;
1415 run
->hypercall
.args
[3] = a3
;
1416 run
->hypercall
.args
[4] = a4
;
1417 run
->hypercall
.args
[5] = a5
;
1418 run
->hypercall
.ret
= ret
;
1419 run
->hypercall
.longmode
= is_long_mode(vcpu
);
1420 kvm_arch_ops
->decache_regs(vcpu
);
1423 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1424 kvm_arch_ops
->decache_regs(vcpu
);
1427 EXPORT_SYMBOL_GPL(kvm_hypercall
);
1429 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1431 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1434 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1436 struct descriptor_table dt
= { limit
, base
};
1438 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1441 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1443 struct descriptor_table dt
= { limit
, base
};
1445 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1448 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1449 unsigned long *rflags
)
1452 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1455 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1457 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
1468 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1473 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1474 unsigned long *rflags
)
1478 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1479 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1488 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1491 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1496 * Register the para guest with the host:
1498 static int vcpu_register_para(struct kvm_vcpu
*vcpu
, gpa_t para_state_gpa
)
1500 struct kvm_vcpu_para_state
*para_state
;
1501 hpa_t para_state_hpa
, hypercall_hpa
;
1502 struct page
*para_state_page
;
1503 unsigned char *hypercall
;
1504 gpa_t hypercall_gpa
;
1506 printk(KERN_DEBUG
"kvm: guest trying to enter paravirtual mode\n");
1507 printk(KERN_DEBUG
".... para_state_gpa: %08Lx\n", para_state_gpa
);
1510 * Needs to be page aligned:
1512 if (para_state_gpa
!= PAGE_ALIGN(para_state_gpa
))
1515 para_state_hpa
= gpa_to_hpa(vcpu
, para_state_gpa
);
1516 printk(KERN_DEBUG
".... para_state_hpa: %08Lx\n", para_state_hpa
);
1517 if (is_error_hpa(para_state_hpa
))
1520 mark_page_dirty(vcpu
->kvm
, para_state_gpa
>> PAGE_SHIFT
);
1521 para_state_page
= pfn_to_page(para_state_hpa
>> PAGE_SHIFT
);
1522 para_state
= kmap_atomic(para_state_page
, KM_USER0
);
1524 printk(KERN_DEBUG
".... guest version: %d\n", para_state
->guest_version
);
1525 printk(KERN_DEBUG
".... size: %d\n", para_state
->size
);
1527 para_state
->host_version
= KVM_PARA_API_VERSION
;
1529 * We cannot support guests that try to register themselves
1530 * with a newer API version than the host supports:
1532 if (para_state
->guest_version
> KVM_PARA_API_VERSION
) {
1533 para_state
->ret
= -KVM_EINVAL
;
1534 goto err_kunmap_skip
;
1537 hypercall_gpa
= para_state
->hypercall_gpa
;
1538 hypercall_hpa
= gpa_to_hpa(vcpu
, hypercall_gpa
);
1539 printk(KERN_DEBUG
".... hypercall_hpa: %08Lx\n", hypercall_hpa
);
1540 if (is_error_hpa(hypercall_hpa
)) {
1541 para_state
->ret
= -KVM_EINVAL
;
1542 goto err_kunmap_skip
;
1545 printk(KERN_DEBUG
"kvm: para guest successfully registered.\n");
1546 vcpu
->para_state_page
= para_state_page
;
1547 vcpu
->para_state_gpa
= para_state_gpa
;
1548 vcpu
->hypercall_gpa
= hypercall_gpa
;
1550 mark_page_dirty(vcpu
->kvm
, hypercall_gpa
>> PAGE_SHIFT
);
1551 hypercall
= kmap_atomic(pfn_to_page(hypercall_hpa
>> PAGE_SHIFT
),
1552 KM_USER1
) + (hypercall_hpa
& ~PAGE_MASK
);
1553 kvm_arch_ops
->patch_hypercall(vcpu
, hypercall
);
1554 kunmap_atomic(hypercall
, KM_USER1
);
1556 para_state
->ret
= 0;
1558 kunmap_atomic(para_state
, KM_USER0
);
1564 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1569 case 0xc0010010: /* SYSCFG */
1570 case 0xc0010015: /* HWCR */
1571 case MSR_IA32_PLATFORM_ID
:
1572 case MSR_IA32_P5_MC_ADDR
:
1573 case MSR_IA32_P5_MC_TYPE
:
1574 case MSR_IA32_MC0_CTL
:
1575 case MSR_IA32_MCG_STATUS
:
1576 case MSR_IA32_MCG_CAP
:
1577 case MSR_IA32_MC0_MISC
:
1578 case MSR_IA32_MC0_MISC
+4:
1579 case MSR_IA32_MC0_MISC
+8:
1580 case MSR_IA32_MC0_MISC
+12:
1581 case MSR_IA32_MC0_MISC
+16:
1582 case MSR_IA32_UCODE_REV
:
1583 case MSR_IA32_PERF_STATUS
:
1584 case MSR_IA32_EBL_CR_POWERON
:
1585 /* MTRR registers */
1587 case 0x200 ... 0x2ff:
1590 case 0xcd: /* fsb frequency */
1593 case MSR_IA32_APICBASE
:
1594 data
= vcpu
->apic_base
;
1596 case MSR_IA32_MISC_ENABLE
:
1597 data
= vcpu
->ia32_misc_enable_msr
;
1599 #ifdef CONFIG_X86_64
1601 data
= vcpu
->shadow_efer
;
1605 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1611 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1614 * Reads an msr value (of 'msr_index') into 'pdata'.
1615 * Returns 0 on success, non-0 otherwise.
1616 * Assumes vcpu_load() was already called.
1618 static int get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1620 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1623 #ifdef CONFIG_X86_64
1625 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1627 if (efer
& EFER_RESERVED_BITS
) {
1628 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1635 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1636 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1641 kvm_arch_ops
->set_efer(vcpu
, efer
);
1644 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1646 vcpu
->shadow_efer
= efer
;
1651 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1654 #ifdef CONFIG_X86_64
1656 set_efer(vcpu
, data
);
1659 case MSR_IA32_MC0_STATUS
:
1660 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1661 __FUNCTION__
, data
);
1663 case MSR_IA32_MCG_STATUS
:
1664 printk(KERN_WARNING
"%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1665 __FUNCTION__
, data
);
1667 case MSR_IA32_UCODE_REV
:
1668 case MSR_IA32_UCODE_WRITE
:
1669 case 0x200 ... 0x2ff: /* MTRRs */
1671 case MSR_IA32_APICBASE
:
1672 vcpu
->apic_base
= data
;
1674 case MSR_IA32_MISC_ENABLE
:
1675 vcpu
->ia32_misc_enable_msr
= data
;
1678 * This is the 'probe whether the host is KVM' logic:
1680 case MSR_KVM_API_MAGIC
:
1681 return vcpu_register_para(vcpu
, data
);
1684 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1689 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1692 * Writes msr value into into the appropriate "register".
1693 * Returns 0 on success, non-0 otherwise.
1694 * Assumes vcpu_load() was already called.
1696 static int set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1698 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1701 void kvm_resched(struct kvm_vcpu
*vcpu
)
1703 if (!need_resched())
1709 EXPORT_SYMBOL_GPL(kvm_resched
);
1711 void load_msrs(struct vmx_msr_entry
*e
, int n
)
1715 for (i
= 0; i
< n
; ++i
)
1716 wrmsrl(e
[i
].index
, e
[i
].data
);
1718 EXPORT_SYMBOL_GPL(load_msrs
);
1720 void save_msrs(struct vmx_msr_entry
*e
, int n
)
1724 for (i
= 0; i
< n
; ++i
)
1725 rdmsrl(e
[i
].index
, e
[i
].data
);
1727 EXPORT_SYMBOL_GPL(save_msrs
);
1729 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1733 struct kvm_cpuid_entry
*e
, *best
;
1735 kvm_arch_ops
->cache_regs(vcpu
);
1736 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1737 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1738 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1739 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1740 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1742 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1743 e
= &vcpu
->cpuid_entries
[i
];
1744 if (e
->function
== function
) {
1749 * Both basic or both extended?
1751 if (((e
->function
^ function
) & 0x80000000) == 0)
1752 if (!best
|| e
->function
> best
->function
)
1756 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1757 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1758 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1759 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1761 kvm_arch_ops
->decache_regs(vcpu
);
1762 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1764 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1766 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1768 void *p
= vcpu
->pio_data
;
1771 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1773 kvm_arch_ops
->vcpu_put(vcpu
);
1774 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1777 kvm_arch_ops
->vcpu_load(vcpu
);
1778 free_pio_guest_pages(vcpu
);
1781 q
+= vcpu
->pio
.guest_page_offset
;
1782 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1784 memcpy(q
, p
, bytes
);
1786 memcpy(p
, q
, bytes
);
1787 q
-= vcpu
->pio
.guest_page_offset
;
1789 kvm_arch_ops
->vcpu_load(vcpu
);
1790 free_pio_guest_pages(vcpu
);
1794 static int complete_pio(struct kvm_vcpu
*vcpu
)
1796 struct kvm_pio_request
*io
= &vcpu
->pio
;
1800 kvm_arch_ops
->cache_regs(vcpu
);
1804 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1808 r
= pio_copy_data(vcpu
);
1810 kvm_arch_ops
->cache_regs(vcpu
);
1817 delta
*= io
->cur_count
;
1819 * The size of the register should really depend on
1820 * current address size.
1822 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1828 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1830 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1833 kvm_arch_ops
->decache_regs(vcpu
);
1835 io
->count
-= io
->cur_count
;
1839 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1843 void kernel_pio(struct kvm_io_device
*pio_dev
, struct kvm_vcpu
*vcpu
)
1845 /* TODO: String I/O for in kernel device */
1848 kvm_iodevice_read(pio_dev
, vcpu
->pio
.port
,
1852 kvm_iodevice_write(pio_dev
, vcpu
->pio
.port
,
1857 int kvm_setup_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1858 int size
, unsigned long count
, int string
, int down
,
1859 gva_t address
, int rep
, unsigned port
)
1861 unsigned now
, in_page
;
1865 struct kvm_io_device
*pio_dev
;
1867 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1868 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1869 vcpu
->run
->io
.size
= size
;
1870 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1871 vcpu
->run
->io
.count
= count
;
1872 vcpu
->run
->io
.port
= port
;
1873 vcpu
->pio
.count
= count
;
1874 vcpu
->pio
.cur_count
= count
;
1875 vcpu
->pio
.size
= size
;
1877 vcpu
->pio
.port
= port
;
1878 vcpu
->pio
.string
= string
;
1879 vcpu
->pio
.down
= down
;
1880 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1881 vcpu
->pio
.rep
= rep
;
1883 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1885 kvm_arch_ops
->cache_regs(vcpu
);
1886 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1887 kvm_arch_ops
->decache_regs(vcpu
);
1889 kernel_pio(pio_dev
, vcpu
);
1895 /* TODO: String I/O for in kernel device */
1897 printk(KERN_ERR
"kvm_setup_pio: no string io support\n");
1900 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1904 now
= min(count
, PAGE_SIZE
/ size
);
1907 in_page
= PAGE_SIZE
- offset_in_page(address
);
1909 in_page
= offset_in_page(address
) + size
;
1910 now
= min(count
, (unsigned long)in_page
/ size
);
1913 * String I/O straddles page boundary. Pin two guest pages
1914 * so that we satisfy atomicity constraints. Do just one
1915 * transaction to avoid complexity.
1922 * String I/O in reverse. Yuck. Kill the guest, fix later.
1924 printk(KERN_ERR
"kvm: guest string pio down\n");
1928 vcpu
->run
->io
.count
= now
;
1929 vcpu
->pio
.cur_count
= now
;
1931 for (i
= 0; i
< nr_pages
; ++i
) {
1932 spin_lock(&vcpu
->kvm
->lock
);
1933 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
1936 vcpu
->pio
.guest_pages
[i
] = page
;
1937 spin_unlock(&vcpu
->kvm
->lock
);
1940 free_pio_guest_pages(vcpu
);
1946 return pio_copy_data(vcpu
);
1949 EXPORT_SYMBOL_GPL(kvm_setup_pio
);
1951 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1958 if (vcpu
->sigset_active
)
1959 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
1961 /* re-sync apic's tpr */
1962 vcpu
->cr8
= kvm_run
->cr8
;
1964 if (vcpu
->pio
.cur_count
) {
1965 r
= complete_pio(vcpu
);
1970 if (vcpu
->mmio_needed
) {
1971 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1972 vcpu
->mmio_read_completed
= 1;
1973 vcpu
->mmio_needed
= 0;
1974 r
= emulate_instruction(vcpu
, kvm_run
,
1975 vcpu
->mmio_fault_cr2
, 0);
1976 if (r
== EMULATE_DO_MMIO
) {
1978 * Read-modify-write. Back to userspace.
1980 kvm_run
->exit_reason
= KVM_EXIT_MMIO
;
1986 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
1987 kvm_arch_ops
->cache_regs(vcpu
);
1988 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
1989 kvm_arch_ops
->decache_regs(vcpu
);
1992 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1995 if (vcpu
->sigset_active
)
1996 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2002 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
2003 struct kvm_regs
*regs
)
2007 kvm_arch_ops
->cache_regs(vcpu
);
2009 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
2010 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
2011 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
2012 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
2013 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
2014 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
2015 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
2016 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
2017 #ifdef CONFIG_X86_64
2018 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
2019 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
2020 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
2021 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
2022 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
2023 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
2024 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
2025 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
2028 regs
->rip
= vcpu
->rip
;
2029 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
2032 * Don't leak debug flags in case they were set for guest debugging
2034 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2035 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2042 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
2043 struct kvm_regs
*regs
)
2047 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
2048 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2049 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2050 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2051 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2052 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2053 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2054 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2055 #ifdef CONFIG_X86_64
2056 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
2057 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
2058 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
2059 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
2060 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
2061 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
2062 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
2063 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
2066 vcpu
->rip
= regs
->rip
;
2067 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
2069 kvm_arch_ops
->decache_regs(vcpu
);
2076 static void get_segment(struct kvm_vcpu
*vcpu
,
2077 struct kvm_segment
*var
, int seg
)
2079 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
2082 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2083 struct kvm_sregs
*sregs
)
2085 struct descriptor_table dt
;
2089 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2090 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2091 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2092 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2093 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2094 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2096 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2097 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2099 kvm_arch_ops
->get_idt(vcpu
, &dt
);
2100 sregs
->idt
.limit
= dt
.limit
;
2101 sregs
->idt
.base
= dt
.base
;
2102 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
2103 sregs
->gdt
.limit
= dt
.limit
;
2104 sregs
->gdt
.base
= dt
.base
;
2106 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2107 sregs
->cr0
= vcpu
->cr0
;
2108 sregs
->cr2
= vcpu
->cr2
;
2109 sregs
->cr3
= vcpu
->cr3
;
2110 sregs
->cr4
= vcpu
->cr4
;
2111 sregs
->cr8
= vcpu
->cr8
;
2112 sregs
->efer
= vcpu
->shadow_efer
;
2113 sregs
->apic_base
= vcpu
->apic_base
;
2115 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
2116 sizeof sregs
->interrupt_bitmap
);
2123 static void set_segment(struct kvm_vcpu
*vcpu
,
2124 struct kvm_segment
*var
, int seg
)
2126 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
2129 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2130 struct kvm_sregs
*sregs
)
2132 int mmu_reset_needed
= 0;
2134 struct descriptor_table dt
;
2138 dt
.limit
= sregs
->idt
.limit
;
2139 dt
.base
= sregs
->idt
.base
;
2140 kvm_arch_ops
->set_idt(vcpu
, &dt
);
2141 dt
.limit
= sregs
->gdt
.limit
;
2142 dt
.base
= sregs
->gdt
.base
;
2143 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
2145 vcpu
->cr2
= sregs
->cr2
;
2146 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2147 vcpu
->cr3
= sregs
->cr3
;
2149 vcpu
->cr8
= sregs
->cr8
;
2151 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2152 #ifdef CONFIG_X86_64
2153 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
2155 vcpu
->apic_base
= sregs
->apic_base
;
2157 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2159 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2160 kvm_arch_ops
->set_cr0(vcpu
, sregs
->cr0
);
2162 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2163 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
2164 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2165 load_pdptrs(vcpu
, vcpu
->cr3
);
2167 if (mmu_reset_needed
)
2168 kvm_mmu_reset_context(vcpu
);
2170 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2171 sizeof vcpu
->irq_pending
);
2172 vcpu
->irq_summary
= 0;
2173 for (i
= 0; i
< NR_IRQ_WORDS
; ++i
)
2174 if (vcpu
->irq_pending
[i
])
2175 __set_bit(i
, &vcpu
->irq_summary
);
2177 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2178 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2179 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2180 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2181 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2182 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2184 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2185 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2193 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2194 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2196 * This list is modified at module load time to reflect the
2197 * capabilities of the host cpu.
2199 static u32 msrs_to_save
[] = {
2200 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
2202 #ifdef CONFIG_X86_64
2203 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
2205 MSR_IA32_TIME_STAMP_COUNTER
,
2208 static unsigned num_msrs_to_save
;
2210 static u32 emulated_msrs
[] = {
2211 MSR_IA32_MISC_ENABLE
,
2214 static __init
void kvm_init_msr_list(void)
2219 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2220 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2223 msrs_to_save
[j
] = msrs_to_save
[i
];
2226 num_msrs_to_save
= j
;
2230 * Adapt set_msr() to msr_io()'s calling convention
2232 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
2234 return set_msr(vcpu
, index
, *data
);
2238 * Read or write a bunch of msrs. All parameters are kernel addresses.
2240 * @return number of msrs set successfully.
2242 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
2243 struct kvm_msr_entry
*entries
,
2244 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2245 unsigned index
, u64
*data
))
2251 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
2252 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
2261 * Read or write a bunch of msrs. Parameters are user addresses.
2263 * @return number of msrs set successfully.
2265 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
2266 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2267 unsigned index
, u64
*data
),
2270 struct kvm_msrs msrs
;
2271 struct kvm_msr_entry
*entries
;
2276 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
2280 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
2284 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
2285 entries
= vmalloc(size
);
2290 if (copy_from_user(entries
, user_msrs
->entries
, size
))
2293 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
2298 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
2310 * Translate a guest virtual address to a guest physical address.
2312 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2313 struct kvm_translation
*tr
)
2315 unsigned long vaddr
= tr
->linear_address
;
2319 spin_lock(&vcpu
->kvm
->lock
);
2320 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2321 tr
->physical_address
= gpa
;
2322 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2325 spin_unlock(&vcpu
->kvm
->lock
);
2331 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2332 struct kvm_interrupt
*irq
)
2334 if (irq
->irq
< 0 || irq
->irq
>= 256)
2338 set_bit(irq
->irq
, vcpu
->irq_pending
);
2339 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
2346 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2347 struct kvm_debug_guest
*dbg
)
2353 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
2360 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
2361 unsigned long address
,
2364 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
2365 unsigned long pgoff
;
2368 *type
= VM_FAULT_MINOR
;
2369 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2371 page
= virt_to_page(vcpu
->run
);
2372 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
2373 page
= virt_to_page(vcpu
->pio_data
);
2375 return NOPAGE_SIGBUS
;
2380 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
2381 .nopage
= kvm_vcpu_nopage
,
2384 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2386 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
2390 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
2392 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2394 fput(vcpu
->kvm
->filp
);
2398 static struct file_operations kvm_vcpu_fops
= {
2399 .release
= kvm_vcpu_release
,
2400 .unlocked_ioctl
= kvm_vcpu_ioctl
,
2401 .compat_ioctl
= kvm_vcpu_ioctl
,
2402 .mmap
= kvm_vcpu_mmap
,
2406 * Allocates an inode for the vcpu.
2408 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
2411 struct inode
*inode
;
2414 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
2415 inode
= kvmfs_inode(&kvm_vcpu_fops
);
2416 if (IS_ERR(inode
)) {
2421 file
= kvmfs_file(inode
, vcpu
);
2427 r
= get_unused_fd();
2431 fd_install(fd
, file
);
2440 fput(vcpu
->kvm
->filp
);
2445 * Creates some virtual cpus. Good luck creating more than one.
2447 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
2450 struct kvm_vcpu
*vcpu
;
2457 vcpu
= &kvm
->vcpus
[n
];
2459 mutex_lock(&vcpu
->mutex
);
2462 mutex_unlock(&vcpu
->mutex
);
2466 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2470 vcpu
->run
= page_address(page
);
2472 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2476 vcpu
->pio_data
= page_address(page
);
2478 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
2480 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
2483 r
= kvm_arch_ops
->vcpu_create(vcpu
);
2485 goto out_free_vcpus
;
2487 r
= kvm_mmu_create(vcpu
);
2489 goto out_free_vcpus
;
2491 kvm_arch_ops
->vcpu_load(vcpu
);
2492 r
= kvm_mmu_setup(vcpu
);
2494 r
= kvm_arch_ops
->vcpu_setup(vcpu
);
2498 goto out_free_vcpus
;
2500 r
= create_vcpu_fd(vcpu
);
2502 goto out_free_vcpus
;
2504 spin_lock(&kvm_lock
);
2505 if (n
>= kvm
->nvcpus
)
2506 kvm
->nvcpus
= n
+ 1;
2507 spin_unlock(&kvm_lock
);
2512 kvm_free_vcpu(vcpu
);
2514 free_page((unsigned long)vcpu
->run
);
2517 mutex_unlock(&vcpu
->mutex
);
2522 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
2526 struct kvm_cpuid_entry
*e
, *entry
;
2528 rdmsrl(MSR_EFER
, efer
);
2530 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
2531 e
= &vcpu
->cpuid_entries
[i
];
2532 if (e
->function
== 0x80000001) {
2537 if (entry
&& (entry
->edx
& EFER_NX
) && !(efer
& EFER_NX
)) {
2538 entry
->edx
&= ~(1 << 20);
2539 printk(KERN_INFO
": guest NX capability removed\n");
2543 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
2544 struct kvm_cpuid
*cpuid
,
2545 struct kvm_cpuid_entry __user
*entries
)
2550 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2553 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
2554 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
2556 vcpu
->cpuid_nent
= cpuid
->nent
;
2557 cpuid_fix_nx_cap(vcpu
);
2564 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
2567 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
2568 vcpu
->sigset_active
= 1;
2569 vcpu
->sigset
= *sigset
;
2571 vcpu
->sigset_active
= 0;
2576 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2577 * we have asm/x86/processor.h
2588 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2589 #ifdef CONFIG_X86_64
2590 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2592 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2596 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2598 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2602 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2603 fpu
->fcw
= fxsave
->cwd
;
2604 fpu
->fsw
= fxsave
->swd
;
2605 fpu
->ftwx
= fxsave
->twd
;
2606 fpu
->last_opcode
= fxsave
->fop
;
2607 fpu
->last_ip
= fxsave
->rip
;
2608 fpu
->last_dp
= fxsave
->rdp
;
2609 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2616 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2618 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2622 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2623 fxsave
->cwd
= fpu
->fcw
;
2624 fxsave
->swd
= fpu
->fsw
;
2625 fxsave
->twd
= fpu
->ftwx
;
2626 fxsave
->fop
= fpu
->last_opcode
;
2627 fxsave
->rip
= fpu
->last_ip
;
2628 fxsave
->rdp
= fpu
->last_dp
;
2629 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2636 static long kvm_vcpu_ioctl(struct file
*filp
,
2637 unsigned int ioctl
, unsigned long arg
)
2639 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2640 void __user
*argp
= (void __user
*)arg
;
2648 r
= kvm_vcpu_ioctl_run(vcpu
, vcpu
->run
);
2650 case KVM_GET_REGS
: {
2651 struct kvm_regs kvm_regs
;
2653 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2654 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2658 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2663 case KVM_SET_REGS
: {
2664 struct kvm_regs kvm_regs
;
2667 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2669 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2675 case KVM_GET_SREGS
: {
2676 struct kvm_sregs kvm_sregs
;
2678 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2679 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2683 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2688 case KVM_SET_SREGS
: {
2689 struct kvm_sregs kvm_sregs
;
2692 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2694 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2700 case KVM_TRANSLATE
: {
2701 struct kvm_translation tr
;
2704 if (copy_from_user(&tr
, argp
, sizeof tr
))
2706 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2710 if (copy_to_user(argp
, &tr
, sizeof tr
))
2715 case KVM_INTERRUPT
: {
2716 struct kvm_interrupt irq
;
2719 if (copy_from_user(&irq
, argp
, sizeof irq
))
2721 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2727 case KVM_DEBUG_GUEST
: {
2728 struct kvm_debug_guest dbg
;
2731 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2733 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2740 r
= msr_io(vcpu
, argp
, get_msr
, 1);
2743 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2745 case KVM_SET_CPUID
: {
2746 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2747 struct kvm_cpuid cpuid
;
2750 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2752 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2757 case KVM_SET_SIGNAL_MASK
: {
2758 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
2759 struct kvm_signal_mask kvm_sigmask
;
2760 sigset_t sigset
, *p
;
2765 if (copy_from_user(&kvm_sigmask
, argp
,
2766 sizeof kvm_sigmask
))
2769 if (kvm_sigmask
.len
!= sizeof sigset
)
2772 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2777 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2783 memset(&fpu
, 0, sizeof fpu
);
2784 r
= kvm_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
2788 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
2797 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
2799 r
= kvm_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
2812 static long kvm_vm_ioctl(struct file
*filp
,
2813 unsigned int ioctl
, unsigned long arg
)
2815 struct kvm
*kvm
= filp
->private_data
;
2816 void __user
*argp
= (void __user
*)arg
;
2820 case KVM_CREATE_VCPU
:
2821 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2825 case KVM_SET_MEMORY_REGION
: {
2826 struct kvm_memory_region kvm_mem
;
2829 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2831 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
2836 case KVM_GET_DIRTY_LOG
: {
2837 struct kvm_dirty_log log
;
2840 if (copy_from_user(&log
, argp
, sizeof log
))
2842 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2847 case KVM_SET_MEMORY_ALIAS
: {
2848 struct kvm_memory_alias alias
;
2851 if (copy_from_user(&alias
, argp
, sizeof alias
))
2853 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
2865 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
2866 unsigned long address
,
2869 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2870 unsigned long pgoff
;
2873 *type
= VM_FAULT_MINOR
;
2874 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2875 page
= gfn_to_page(kvm
, pgoff
);
2877 return NOPAGE_SIGBUS
;
2882 static struct vm_operations_struct kvm_vm_vm_ops
= {
2883 .nopage
= kvm_vm_nopage
,
2886 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2888 vma
->vm_ops
= &kvm_vm_vm_ops
;
2892 static struct file_operations kvm_vm_fops
= {
2893 .release
= kvm_vm_release
,
2894 .unlocked_ioctl
= kvm_vm_ioctl
,
2895 .compat_ioctl
= kvm_vm_ioctl
,
2896 .mmap
= kvm_vm_mmap
,
2899 static int kvm_dev_ioctl_create_vm(void)
2902 struct inode
*inode
;
2906 inode
= kvmfs_inode(&kvm_vm_fops
);
2907 if (IS_ERR(inode
)) {
2912 kvm
= kvm_create_vm();
2918 file
= kvmfs_file(inode
, kvm
);
2925 r
= get_unused_fd();
2929 fd_install(fd
, file
);
2936 kvm_destroy_vm(kvm
);
2943 static long kvm_dev_ioctl(struct file
*filp
,
2944 unsigned int ioctl
, unsigned long arg
)
2946 void __user
*argp
= (void __user
*)arg
;
2950 case KVM_GET_API_VERSION
:
2954 r
= KVM_API_VERSION
;
2960 r
= kvm_dev_ioctl_create_vm();
2962 case KVM_GET_MSR_INDEX_LIST
: {
2963 struct kvm_msr_list __user
*user_msr_list
= argp
;
2964 struct kvm_msr_list msr_list
;
2968 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
2971 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
2972 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
2975 if (n
< num_msrs_to_save
)
2978 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
2979 num_msrs_to_save
* sizeof(u32
)))
2981 if (copy_to_user(user_msr_list
->indices
2982 + num_msrs_to_save
* sizeof(u32
),
2984 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
2989 case KVM_CHECK_EXTENSION
:
2991 * No extensions defined at present.
2995 case KVM_GET_VCPU_MMAP_SIZE
:
3008 static struct file_operations kvm_chardev_ops
= {
3009 .open
= kvm_dev_open
,
3010 .release
= kvm_dev_release
,
3011 .unlocked_ioctl
= kvm_dev_ioctl
,
3012 .compat_ioctl
= kvm_dev_ioctl
,
3015 static struct miscdevice kvm_dev
= {
3021 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
3024 if (val
== SYS_RESTART
) {
3026 * Some (well, at least mine) BIOSes hang on reboot if
3029 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
3030 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
3035 static struct notifier_block kvm_reboot_notifier
= {
3036 .notifier_call
= kvm_reboot
,
3041 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3044 static void decache_vcpus_on_cpu(int cpu
)
3047 struct kvm_vcpu
*vcpu
;
3050 spin_lock(&kvm_lock
);
3051 list_for_each_entry(vm
, &vm_list
, vm_list
)
3052 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3053 vcpu
= &vm
->vcpus
[i
];
3055 * If the vcpu is locked, then it is running on some
3056 * other cpu and therefore it is not cached on the
3059 * If it's not locked, check the last cpu it executed
3062 if (mutex_trylock(&vcpu
->mutex
)) {
3063 if (vcpu
->cpu
== cpu
) {
3064 kvm_arch_ops
->vcpu_decache(vcpu
);
3067 mutex_unlock(&vcpu
->mutex
);
3070 spin_unlock(&kvm_lock
);
3073 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
3079 case CPU_DOWN_PREPARE
:
3080 case CPU_DOWN_PREPARE_FROZEN
:
3081 case CPU_UP_CANCELED
:
3082 case CPU_UP_CANCELED_FROZEN
:
3083 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3085 decache_vcpus_on_cpu(cpu
);
3086 smp_call_function_single(cpu
, kvm_arch_ops
->hardware_disable
,
3090 case CPU_ONLINE_FROZEN
:
3091 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
3093 smp_call_function_single(cpu
, kvm_arch_ops
->hardware_enable
,
3100 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
3102 memset(bus
, 0, sizeof(*bus
));
3105 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
3109 for (i
= 0; i
< bus
->dev_count
; i
++) {
3110 struct kvm_io_device
*pos
= bus
->devs
[i
];
3112 kvm_iodevice_destructor(pos
);
3116 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
3120 for (i
= 0; i
< bus
->dev_count
; i
++) {
3121 struct kvm_io_device
*pos
= bus
->devs
[i
];
3123 if (pos
->in_range(pos
, addr
))
3130 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
3132 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
3134 bus
->devs
[bus
->dev_count
++] = dev
;
3137 static struct notifier_block kvm_cpu_notifier
= {
3138 .notifier_call
= kvm_cpu_hotplug
,
3139 .priority
= 20, /* must be > scheduler priority */
3142 static u64
stat_get(void *_offset
)
3144 unsigned offset
= (long)_offset
;
3147 struct kvm_vcpu
*vcpu
;
3150 spin_lock(&kvm_lock
);
3151 list_for_each_entry(kvm
, &vm_list
, vm_list
)
3152 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3153 vcpu
= &kvm
->vcpus
[i
];
3154 total
+= *(u32
*)((void *)vcpu
+ offset
);
3156 spin_unlock(&kvm_lock
);
3160 static void stat_set(void *offset
, u64 val
)
3164 DEFINE_SIMPLE_ATTRIBUTE(stat_fops
, stat_get
, stat_set
, "%llu\n");
3166 static __init
void kvm_init_debug(void)
3168 struct kvm_stats_debugfs_item
*p
;
3170 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
3171 for (p
= debugfs_entries
; p
->name
; ++p
)
3172 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
3173 (void *)(long)p
->offset
,
3177 static void kvm_exit_debug(void)
3179 struct kvm_stats_debugfs_item
*p
;
3181 for (p
= debugfs_entries
; p
->name
; ++p
)
3182 debugfs_remove(p
->dentry
);
3183 debugfs_remove(debugfs_dir
);
3186 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
3188 decache_vcpus_on_cpu(raw_smp_processor_id());
3189 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
3193 static int kvm_resume(struct sys_device
*dev
)
3195 on_each_cpu(kvm_arch_ops
->hardware_enable
, NULL
, 0, 1);
3199 static struct sysdev_class kvm_sysdev_class
= {
3200 set_kset_name("kvm"),
3201 .suspend
= kvm_suspend
,
3202 .resume
= kvm_resume
,
3205 static struct sys_device kvm_sysdev
= {
3207 .cls
= &kvm_sysdev_class
,
3210 hpa_t bad_page_address
;
3212 static int kvmfs_get_sb(struct file_system_type
*fs_type
, int flags
,
3213 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
3215 return get_sb_pseudo(fs_type
, "kvm:", NULL
, KVMFS_SUPER_MAGIC
, mnt
);
3218 static struct file_system_type kvm_fs_type
= {
3220 .get_sb
= kvmfs_get_sb
,
3221 .kill_sb
= kill_anon_super
,
3224 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
3229 printk(KERN_ERR
"kvm: already loaded the other module\n");
3233 if (!ops
->cpu_has_kvm_support()) {
3234 printk(KERN_ERR
"kvm: no hardware support\n");
3237 if (ops
->disabled_by_bios()) {
3238 printk(KERN_ERR
"kvm: disabled by bios\n");
3244 r
= kvm_arch_ops
->hardware_setup();
3248 on_each_cpu(kvm_arch_ops
->hardware_enable
, NULL
, 0, 1);
3249 r
= register_cpu_notifier(&kvm_cpu_notifier
);
3252 register_reboot_notifier(&kvm_reboot_notifier
);
3254 r
= sysdev_class_register(&kvm_sysdev_class
);
3258 r
= sysdev_register(&kvm_sysdev
);
3262 kvm_chardev_ops
.owner
= module
;
3264 r
= misc_register(&kvm_dev
);
3266 printk (KERN_ERR
"kvm: misc device register failed\n");
3273 sysdev_unregister(&kvm_sysdev
);
3275 sysdev_class_unregister(&kvm_sysdev_class
);
3277 unregister_reboot_notifier(&kvm_reboot_notifier
);
3278 unregister_cpu_notifier(&kvm_cpu_notifier
);
3280 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
3281 kvm_arch_ops
->hardware_unsetup();
3283 kvm_arch_ops
= NULL
;
3287 void kvm_exit_arch(void)
3289 misc_deregister(&kvm_dev
);
3290 sysdev_unregister(&kvm_sysdev
);
3291 sysdev_class_unregister(&kvm_sysdev_class
);
3292 unregister_reboot_notifier(&kvm_reboot_notifier
);
3293 unregister_cpu_notifier(&kvm_cpu_notifier
);
3294 on_each_cpu(kvm_arch_ops
->hardware_disable
, NULL
, 0, 1);
3295 kvm_arch_ops
->hardware_unsetup();
3296 kvm_arch_ops
= NULL
;
3299 static __init
int kvm_init(void)
3301 static struct page
*bad_page
;
3304 r
= kvm_mmu_module_init();
3308 r
= register_filesystem(&kvm_fs_type
);
3312 kvmfs_mnt
= kern_mount(&kvm_fs_type
);
3313 r
= PTR_ERR(kvmfs_mnt
);
3314 if (IS_ERR(kvmfs_mnt
))
3318 kvm_init_msr_list();
3320 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
3325 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
3326 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
3334 unregister_filesystem(&kvm_fs_type
);
3336 kvm_mmu_module_exit();
3341 static __exit
void kvm_exit(void)
3344 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
3346 unregister_filesystem(&kvm_fs_type
);
3347 kvm_mmu_module_exit();
3350 module_init(kvm_init
)
3351 module_exit(kvm_exit
)
3353 EXPORT_SYMBOL_GPL(kvm_init_arch
);
3354 EXPORT_SYMBOL_GPL(kvm_exit_arch
);