2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
19 #include "x86_emulate.h"
20 #include "segment_descriptor.h"
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <linux/reboot.h>
31 #include <linux/debugfs.h>
32 #include <linux/highmem.h>
33 #include <linux/file.h>
34 #include <linux/sysdev.h>
35 #include <linux/cpu.h>
36 #include <linux/sched.h>
37 #include <linux/cpumask.h>
38 #include <linux/smp.h>
39 #include <linux/anon_inodes.h>
41 #include <asm/processor.h>
44 #include <asm/uaccess.h>
47 MODULE_AUTHOR("Qumranet");
48 MODULE_LICENSE("GPL");
50 static DEFINE_SPINLOCK(kvm_lock
);
51 static LIST_HEAD(vm_list
);
53 static cpumask_t cpus_hardware_enabled
;
55 struct kvm_arch_ops
*kvm_arch_ops
;
57 static void hardware_disable(void *ignored
);
59 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
61 static struct kvm_stats_debugfs_item
{
64 struct dentry
*dentry
;
65 } debugfs_entries
[] = {
66 { "pf_fixed", STAT_OFFSET(pf_fixed
) },
67 { "pf_guest", STAT_OFFSET(pf_guest
) },
68 { "tlb_flush", STAT_OFFSET(tlb_flush
) },
69 { "invlpg", STAT_OFFSET(invlpg
) },
70 { "exits", STAT_OFFSET(exits
) },
71 { "io_exits", STAT_OFFSET(io_exits
) },
72 { "mmio_exits", STAT_OFFSET(mmio_exits
) },
73 { "signal_exits", STAT_OFFSET(signal_exits
) },
74 { "irq_window", STAT_OFFSET(irq_window_exits
) },
75 { "halt_exits", STAT_OFFSET(halt_exits
) },
76 { "request_irq", STAT_OFFSET(request_irq_exits
) },
77 { "irq_exits", STAT_OFFSET(irq_exits
) },
78 { "light_exits", STAT_OFFSET(light_exits
) },
79 { "efer_reload", STAT_OFFSET(efer_reload
) },
83 static struct dentry
*debugfs_dir
;
85 #define MAX_IO_MSRS 256
87 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
88 #define LMSW_GUEST_MASK 0x0eULL
89 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
90 #define CR8_RESEVED_BITS (~0x0fULL)
91 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
94 // LDT or TSS descriptor in the GDT. 16 bytes.
95 struct segment_descriptor_64
{
96 struct segment_descriptor s
;
103 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
106 unsigned long segment_base(u16 selector
)
108 struct descriptor_table gdt
;
109 struct segment_descriptor
*d
;
110 unsigned long table_base
;
111 typedef unsigned long ul
;
117 asm ("sgdt %0" : "=m"(gdt
));
118 table_base
= gdt
.base
;
120 if (selector
& 4) { /* from ldt */
123 asm ("sldt %0" : "=g"(ldt_selector
));
124 table_base
= segment_base(ldt_selector
);
126 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
127 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
130 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
131 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
135 EXPORT_SYMBOL_GPL(segment_base
);
137 static inline int valid_vcpu(int n
)
139 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
142 int kvm_read_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
145 unsigned char *host_buf
= dest
;
146 unsigned long req_size
= size
;
154 paddr
= gva_to_hpa(vcpu
, addr
);
156 if (is_error_hpa(paddr
))
159 guest_buf
= (hva_t
)kmap_atomic(
160 pfn_to_page(paddr
>> PAGE_SHIFT
),
162 offset
= addr
& ~PAGE_MASK
;
164 now
= min(size
, PAGE_SIZE
- offset
);
165 memcpy(host_buf
, (void*)guest_buf
, now
);
169 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
171 return req_size
- size
;
173 EXPORT_SYMBOL_GPL(kvm_read_guest
);
175 int kvm_write_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
178 unsigned char *host_buf
= data
;
179 unsigned long req_size
= size
;
188 paddr
= gva_to_hpa(vcpu
, addr
);
190 if (is_error_hpa(paddr
))
193 gfn
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
) >> PAGE_SHIFT
;
194 mark_page_dirty(vcpu
->kvm
, gfn
);
195 guest_buf
= (hva_t
)kmap_atomic(
196 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
197 offset
= addr
& ~PAGE_MASK
;
199 now
= min(size
, PAGE_SIZE
- offset
);
200 memcpy((void*)guest_buf
, host_buf
, now
);
204 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
206 return req_size
- size
;
208 EXPORT_SYMBOL_GPL(kvm_write_guest
);
210 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
212 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
215 vcpu
->guest_fpu_loaded
= 1;
216 fx_save(vcpu
->host_fx_image
);
217 fx_restore(vcpu
->guest_fx_image
);
219 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
221 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
223 if (!vcpu
->guest_fpu_loaded
)
226 vcpu
->guest_fpu_loaded
= 0;
227 fx_save(vcpu
->guest_fx_image
);
228 fx_restore(vcpu
->host_fx_image
);
230 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
233 * Switches to specified vcpu, until a matching vcpu_put()
235 static void vcpu_load(struct kvm_vcpu
*vcpu
)
237 mutex_lock(&vcpu
->mutex
);
238 kvm_arch_ops
->vcpu_load(vcpu
);
241 static void vcpu_put(struct kvm_vcpu
*vcpu
)
243 kvm_arch_ops
->vcpu_put(vcpu
);
244 mutex_unlock(&vcpu
->mutex
);
247 static void ack_flush(void *_completed
)
249 atomic_t
*completed
= _completed
;
251 atomic_inc(completed
);
254 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
258 struct kvm_vcpu
*vcpu
;
261 atomic_set(&completed
, 0);
264 for (i
= 0; i
< kvm
->nvcpus
; ++i
) {
265 vcpu
= &kvm
->vcpus
[i
];
266 if (test_and_set_bit(KVM_TLB_FLUSH
, &vcpu
->requests
))
269 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
270 if (!cpu_isset(cpu
, cpus
)) {
276 /* Uniprocessor kernel does not respect cpus in first_cpu. So
277 * do not go there if we have nothing to do. */
278 if (cpus_empty(cpus
))
282 * We really want smp_call_function_mask() here. But that's not
283 * available, so ipi all cpus in parallel and wait for them
286 for (cpu
= first_cpu(cpus
); cpu
!= NR_CPUS
; cpu
= next_cpu(cpu
, cpus
))
287 smp_call_function_single(cpu
, ack_flush
, &completed
, 1, 0);
288 while (atomic_read(&completed
) != needed
) {
294 static struct kvm
*kvm_create_vm(void)
296 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
300 return ERR_PTR(-ENOMEM
);
302 kvm_io_bus_init(&kvm
->pio_bus
);
303 spin_lock_init(&kvm
->lock
);
304 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
305 kvm_io_bus_init(&kvm
->mmio_bus
);
306 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
307 struct kvm_vcpu
*vcpu
= &kvm
->vcpus
[i
];
309 mutex_init(&vcpu
->mutex
);
312 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
314 spin_lock(&kvm_lock
);
315 list_add(&kvm
->vm_list
, &vm_list
);
316 spin_unlock(&kvm_lock
);
320 static int kvm_dev_open(struct inode
*inode
, struct file
*filp
)
326 * Free any memory in @free but not in @dont.
328 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
329 struct kvm_memory_slot
*dont
)
333 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
334 if (free
->phys_mem
) {
335 for (i
= 0; i
< free
->npages
; ++i
)
336 if (free
->phys_mem
[i
])
337 __free_page(free
->phys_mem
[i
]);
338 vfree(free
->phys_mem
);
341 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
342 vfree(free
->dirty_bitmap
);
344 free
->phys_mem
= NULL
;
346 free
->dirty_bitmap
= NULL
;
349 static void kvm_free_physmem(struct kvm
*kvm
)
353 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
354 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
357 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
361 for (i
= 0; i
< 2; ++i
)
362 if (vcpu
->pio
.guest_pages
[i
]) {
363 __free_page(vcpu
->pio
.guest_pages
[i
]);
364 vcpu
->pio
.guest_pages
[i
] = NULL
;
368 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
374 kvm_mmu_unload(vcpu
);
378 static void kvm_free_vcpu(struct kvm_vcpu
*vcpu
)
384 kvm_mmu_destroy(vcpu
);
386 kvm_arch_ops
->vcpu_free(vcpu
);
387 free_page((unsigned long)vcpu
->run
);
389 free_page((unsigned long)vcpu
->pio_data
);
390 vcpu
->pio_data
= NULL
;
391 free_pio_guest_pages(vcpu
);
394 static void kvm_free_vcpus(struct kvm
*kvm
)
399 * Unpin any mmu pages first.
401 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
402 kvm_unload_vcpu_mmu(&kvm
->vcpus
[i
]);
403 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
404 kvm_free_vcpu(&kvm
->vcpus
[i
]);
407 static int kvm_dev_release(struct inode
*inode
, struct file
*filp
)
412 static void kvm_destroy_vm(struct kvm
*kvm
)
414 spin_lock(&kvm_lock
);
415 list_del(&kvm
->vm_list
);
416 spin_unlock(&kvm_lock
);
417 kvm_io_bus_destroy(&kvm
->pio_bus
);
418 kvm_io_bus_destroy(&kvm
->mmio_bus
);
420 kvm_free_physmem(kvm
);
424 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
426 struct kvm
*kvm
= filp
->private_data
;
432 static void inject_gp(struct kvm_vcpu
*vcpu
)
434 kvm_arch_ops
->inject_gp(vcpu
, 0);
438 * Load the pae pdptrs. Return true is they are all valid.
440 static int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
442 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
443 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
450 spin_lock(&vcpu
->kvm
->lock
);
451 page
= gfn_to_page(vcpu
->kvm
, pdpt_gfn
);
452 /* FIXME: !page - emulate? 0xff? */
453 pdpt
= kmap_atomic(page
, KM_USER0
);
456 for (i
= 0; i
< 4; ++i
) {
457 pdpte
= pdpt
[offset
+ i
];
458 if ((pdpte
& 1) && (pdpte
& 0xfffffff0000001e6ull
)) {
464 for (i
= 0; i
< 4; ++i
)
465 vcpu
->pdptrs
[i
] = pdpt
[offset
+ i
];
468 kunmap_atomic(pdpt
, KM_USER0
);
469 spin_unlock(&vcpu
->kvm
->lock
);
474 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
476 if (cr0
& CR0_RESEVED_BITS
) {
477 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
483 if ((cr0
& CR0_NW_MASK
) && !(cr0
& CR0_CD_MASK
)) {
484 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
489 if ((cr0
& CR0_PG_MASK
) && !(cr0
& CR0_PE_MASK
)) {
490 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
491 "and a clear PE flag\n");
496 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
)) {
498 if ((vcpu
->shadow_efer
& EFER_LME
)) {
502 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
503 "in long mode while PAE is disabled\n");
507 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
509 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
510 "in long mode while CS.L == 1\n");
517 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
518 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
526 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
529 spin_lock(&vcpu
->kvm
->lock
);
530 kvm_mmu_reset_context(vcpu
);
531 spin_unlock(&vcpu
->kvm
->lock
);
534 EXPORT_SYMBOL_GPL(set_cr0
);
536 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
538 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
540 EXPORT_SYMBOL_GPL(lmsw
);
542 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
544 if (cr4
& CR4_RESEVED_BITS
) {
545 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
550 if (is_long_mode(vcpu
)) {
551 if (!(cr4
& CR4_PAE_MASK
)) {
552 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
557 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& CR4_PAE_MASK
)
558 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
559 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
563 if (cr4
& CR4_VMXE_MASK
) {
564 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
568 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
569 spin_lock(&vcpu
->kvm
->lock
);
570 kvm_mmu_reset_context(vcpu
);
571 spin_unlock(&vcpu
->kvm
->lock
);
573 EXPORT_SYMBOL_GPL(set_cr4
);
575 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
577 if (is_long_mode(vcpu
)) {
578 if (cr3
& CR3_L_MODE_RESEVED_BITS
) {
579 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
584 if (cr3
& CR3_RESEVED_BITS
) {
585 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
589 if (is_paging(vcpu
) && is_pae(vcpu
) &&
590 !load_pdptrs(vcpu
, cr3
)) {
591 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
599 spin_lock(&vcpu
->kvm
->lock
);
601 * Does the new cr3 value map to physical memory? (Note, we
602 * catch an invalid cr3 even in real-mode, because it would
603 * cause trouble later on when we turn on paging anyway.)
605 * A real CPU would silently accept an invalid cr3 and would
606 * attempt to use it - with largely undefined (and often hard
607 * to debug) behavior on the guest side.
609 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
612 vcpu
->mmu
.new_cr3(vcpu
);
613 spin_unlock(&vcpu
->kvm
->lock
);
615 EXPORT_SYMBOL_GPL(set_cr3
);
617 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
619 if ( cr8
& CR8_RESEVED_BITS
) {
620 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
626 EXPORT_SYMBOL_GPL(set_cr8
);
628 void fx_init(struct kvm_vcpu
*vcpu
)
630 struct __attribute__ ((__packed__
)) fx_image_s
{
636 u64 operand
;// fpu dp
642 fx_save(vcpu
->host_fx_image
);
644 fx_save(vcpu
->guest_fx_image
);
645 fx_restore(vcpu
->host_fx_image
);
647 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
648 fx_image
->mxcsr
= 0x1f80;
649 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
650 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
652 EXPORT_SYMBOL_GPL(fx_init
);
655 * Allocate some memory and give it an address in the guest physical address
658 * Discontiguous memory is allowed, mostly for framebuffers.
660 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
661 struct kvm_memory_region
*mem
)
665 unsigned long npages
;
667 struct kvm_memory_slot
*memslot
;
668 struct kvm_memory_slot old
, new;
669 int memory_config_version
;
672 /* General sanity checks */
673 if (mem
->memory_size
& (PAGE_SIZE
- 1))
675 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
677 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
679 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
682 memslot
= &kvm
->memslots
[mem
->slot
];
683 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
684 npages
= mem
->memory_size
>> PAGE_SHIFT
;
687 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
690 spin_lock(&kvm
->lock
);
692 memory_config_version
= kvm
->memory_config_version
;
693 new = old
= *memslot
;
695 new.base_gfn
= base_gfn
;
697 new.flags
= mem
->flags
;
699 /* Disallow changing a memory slot's size. */
701 if (npages
&& old
.npages
&& npages
!= old
.npages
)
704 /* Check for overlaps */
706 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
707 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
711 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
712 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
716 * Do memory allocations outside lock. memory_config_version will
719 spin_unlock(&kvm
->lock
);
721 /* Deallocate if slot is being removed */
725 /* Free page dirty bitmap if unneeded */
726 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
727 new.dirty_bitmap
= NULL
;
731 /* Allocate if a slot is being created */
732 if (npages
&& !new.phys_mem
) {
733 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
738 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
739 for (i
= 0; i
< npages
; ++i
) {
740 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
742 if (!new.phys_mem
[i
])
744 set_page_private(new.phys_mem
[i
],0);
748 /* Allocate page dirty bitmap if needed */
749 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
750 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
752 new.dirty_bitmap
= vmalloc(dirty_bytes
);
753 if (!new.dirty_bitmap
)
755 memset(new.dirty_bitmap
, 0, dirty_bytes
);
758 spin_lock(&kvm
->lock
);
760 if (memory_config_version
!= kvm
->memory_config_version
) {
761 spin_unlock(&kvm
->lock
);
762 kvm_free_physmem_slot(&new, &old
);
770 if (mem
->slot
>= kvm
->nmemslots
)
771 kvm
->nmemslots
= mem
->slot
+ 1;
774 ++kvm
->memory_config_version
;
776 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
777 kvm_flush_remote_tlbs(kvm
);
779 spin_unlock(&kvm
->lock
);
781 kvm_free_physmem_slot(&old
, &new);
785 spin_unlock(&kvm
->lock
);
787 kvm_free_physmem_slot(&new, &old
);
793 * Get (and clear) the dirty memory log for a memory slot.
795 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
796 struct kvm_dirty_log
*log
)
798 struct kvm_memory_slot
*memslot
;
801 unsigned long any
= 0;
803 spin_lock(&kvm
->lock
);
806 * Prevent changes to guest memory configuration even while the lock
810 spin_unlock(&kvm
->lock
);
812 if (log
->slot
>= KVM_MEMORY_SLOTS
)
815 memslot
= &kvm
->memslots
[log
->slot
];
817 if (!memslot
->dirty_bitmap
)
820 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
822 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
823 any
= memslot
->dirty_bitmap
[i
];
826 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
829 spin_lock(&kvm
->lock
);
830 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
831 kvm_flush_remote_tlbs(kvm
);
832 memset(memslot
->dirty_bitmap
, 0, n
);
833 spin_unlock(&kvm
->lock
);
838 spin_lock(&kvm
->lock
);
840 spin_unlock(&kvm
->lock
);
845 * Set a new alias region. Aliases map a portion of physical memory into
846 * another portion. This is useful for memory windows, for example the PC
849 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
850 struct kvm_memory_alias
*alias
)
853 struct kvm_mem_alias
*p
;
856 /* General sanity checks */
857 if (alias
->memory_size
& (PAGE_SIZE
- 1))
859 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
861 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
863 if (alias
->guest_phys_addr
+ alias
->memory_size
864 < alias
->guest_phys_addr
)
866 if (alias
->target_phys_addr
+ alias
->memory_size
867 < alias
->target_phys_addr
)
870 spin_lock(&kvm
->lock
);
872 p
= &kvm
->aliases
[alias
->slot
];
873 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
874 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
875 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
877 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
878 if (kvm
->aliases
[n
- 1].npages
)
882 kvm_mmu_zap_all(kvm
);
884 spin_unlock(&kvm
->lock
);
892 static gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
895 struct kvm_mem_alias
*alias
;
897 for (i
= 0; i
< kvm
->naliases
; ++i
) {
898 alias
= &kvm
->aliases
[i
];
899 if (gfn
>= alias
->base_gfn
900 && gfn
< alias
->base_gfn
+ alias
->npages
)
901 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
906 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
910 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
911 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
913 if (gfn
>= memslot
->base_gfn
914 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
920 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
922 gfn
= unalias_gfn(kvm
, gfn
);
923 return __gfn_to_memslot(kvm
, gfn
);
926 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
928 struct kvm_memory_slot
*slot
;
930 gfn
= unalias_gfn(kvm
, gfn
);
931 slot
= __gfn_to_memslot(kvm
, gfn
);
934 return slot
->phys_mem
[gfn
- slot
->base_gfn
];
936 EXPORT_SYMBOL_GPL(gfn_to_page
);
938 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
941 struct kvm_memory_slot
*memslot
;
942 unsigned long rel_gfn
;
944 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
945 memslot
= &kvm
->memslots
[i
];
947 if (gfn
>= memslot
->base_gfn
948 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
950 if (!memslot
->dirty_bitmap
)
953 rel_gfn
= gfn
- memslot
->base_gfn
;
956 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
957 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
963 static int emulator_read_std(unsigned long addr
,
966 struct x86_emulate_ctxt
*ctxt
)
968 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
972 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
973 unsigned offset
= addr
& (PAGE_SIZE
-1);
974 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
979 if (gpa
== UNMAPPED_GVA
)
980 return X86EMUL_PROPAGATE_FAULT
;
981 pfn
= gpa
>> PAGE_SHIFT
;
982 page
= gfn_to_page(vcpu
->kvm
, pfn
);
984 return X86EMUL_UNHANDLEABLE
;
985 page_virt
= kmap_atomic(page
, KM_USER0
);
987 memcpy(data
, page_virt
+ offset
, tocopy
);
989 kunmap_atomic(page_virt
, KM_USER0
);
996 return X86EMUL_CONTINUE
;
999 static int emulator_write_std(unsigned long addr
,
1002 struct x86_emulate_ctxt
*ctxt
)
1004 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
1006 return X86EMUL_UNHANDLEABLE
;
1009 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1013 * Note that its important to have this wrapper function because
1014 * in the very near future we will be checking for MMIOs against
1015 * the LAPIC as well as the general MMIO bus
1017 return kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1020 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1023 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1026 static int emulator_read_emulated(unsigned long addr
,
1029 struct x86_emulate_ctxt
*ctxt
)
1031 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1032 struct kvm_io_device
*mmio_dev
;
1035 if (vcpu
->mmio_read_completed
) {
1036 memcpy(val
, vcpu
->mmio_data
, bytes
);
1037 vcpu
->mmio_read_completed
= 0;
1038 return X86EMUL_CONTINUE
;
1039 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
1040 == X86EMUL_CONTINUE
)
1041 return X86EMUL_CONTINUE
;
1043 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1044 if (gpa
== UNMAPPED_GVA
)
1045 return X86EMUL_PROPAGATE_FAULT
;
1048 * Is this MMIO handled locally?
1050 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1052 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1053 return X86EMUL_CONTINUE
;
1056 vcpu
->mmio_needed
= 1;
1057 vcpu
->mmio_phys_addr
= gpa
;
1058 vcpu
->mmio_size
= bytes
;
1059 vcpu
->mmio_is_write
= 0;
1061 return X86EMUL_UNHANDLEABLE
;
1064 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1065 const void *val
, int bytes
)
1069 unsigned offset
= offset_in_page(gpa
);
1071 if (((gpa
+ bytes
- 1) >> PAGE_SHIFT
) != (gpa
>> PAGE_SHIFT
))
1073 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1076 mark_page_dirty(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1077 virt
= kmap_atomic(page
, KM_USER0
);
1078 kvm_mmu_pte_write(vcpu
, gpa
, virt
+ offset
, val
, bytes
);
1079 memcpy(virt
+ offset_in_page(gpa
), val
, bytes
);
1080 kunmap_atomic(virt
, KM_USER0
);
1084 static int emulator_write_emulated_onepage(unsigned long addr
,
1087 struct x86_emulate_ctxt
*ctxt
)
1089 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1090 struct kvm_io_device
*mmio_dev
;
1091 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1093 if (gpa
== UNMAPPED_GVA
) {
1094 kvm_arch_ops
->inject_page_fault(vcpu
, addr
, 2);
1095 return X86EMUL_PROPAGATE_FAULT
;
1098 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1099 return X86EMUL_CONTINUE
;
1102 * Is this MMIO handled locally?
1104 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1106 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1107 return X86EMUL_CONTINUE
;
1110 vcpu
->mmio_needed
= 1;
1111 vcpu
->mmio_phys_addr
= gpa
;
1112 vcpu
->mmio_size
= bytes
;
1113 vcpu
->mmio_is_write
= 1;
1114 memcpy(vcpu
->mmio_data
, val
, bytes
);
1116 return X86EMUL_CONTINUE
;
1119 static int emulator_write_emulated(unsigned long addr
,
1122 struct x86_emulate_ctxt
*ctxt
)
1124 /* Crossing a page boundary? */
1125 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1128 now
= -addr
& ~PAGE_MASK
;
1129 rc
= emulator_write_emulated_onepage(addr
, val
, now
, ctxt
);
1130 if (rc
!= X86EMUL_CONTINUE
)
1136 return emulator_write_emulated_onepage(addr
, val
, bytes
, ctxt
);
1139 static int emulator_cmpxchg_emulated(unsigned long addr
,
1143 struct x86_emulate_ctxt
*ctxt
)
1145 static int reported
;
1149 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1151 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
1154 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1156 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
1159 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1161 return X86EMUL_CONTINUE
;
1164 int emulate_clts(struct kvm_vcpu
*vcpu
)
1166 kvm_arch_ops
->set_cr0(vcpu
, vcpu
->cr0
& ~X86_CR0_TS
);
1167 return X86EMUL_CONTINUE
;
1170 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1172 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1176 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
1177 return X86EMUL_CONTINUE
;
1179 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
1181 return X86EMUL_UNHANDLEABLE
;
1185 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1187 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1190 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1192 /* FIXME: better handling */
1193 return X86EMUL_UNHANDLEABLE
;
1195 return X86EMUL_CONTINUE
;
1198 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
1200 static int reported
;
1202 unsigned long rip
= ctxt
->vcpu
->rip
;
1203 unsigned long rip_linear
;
1205 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
1210 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
1212 printk(KERN_ERR
"emulation failed but !mmio_needed?"
1213 " rip %lx %02x %02x %02x %02x\n",
1214 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1218 struct x86_emulate_ops emulate_ops
= {
1219 .read_std
= emulator_read_std
,
1220 .write_std
= emulator_write_std
,
1221 .read_emulated
= emulator_read_emulated
,
1222 .write_emulated
= emulator_write_emulated
,
1223 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1226 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1227 struct kvm_run
*run
,
1231 struct x86_emulate_ctxt emulate_ctxt
;
1235 vcpu
->mmio_fault_cr2
= cr2
;
1236 kvm_arch_ops
->cache_regs(vcpu
);
1238 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1240 emulate_ctxt
.vcpu
= vcpu
;
1241 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1242 emulate_ctxt
.cr2
= cr2
;
1243 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1244 ? X86EMUL_MODE_REAL
: cs_l
1245 ? X86EMUL_MODE_PROT64
: cs_db
1246 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1248 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1249 emulate_ctxt
.cs_base
= 0;
1250 emulate_ctxt
.ds_base
= 0;
1251 emulate_ctxt
.es_base
= 0;
1252 emulate_ctxt
.ss_base
= 0;
1254 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1255 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1256 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1257 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1260 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1261 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1263 vcpu
->mmio_is_write
= 0;
1264 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1266 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1267 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1268 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1269 run
->mmio
.len
= vcpu
->mmio_size
;
1270 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1274 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1275 return EMULATE_DONE
;
1276 if (!vcpu
->mmio_needed
) {
1277 report_emulation_failure(&emulate_ctxt
);
1278 return EMULATE_FAIL
;
1280 return EMULATE_DO_MMIO
;
1283 kvm_arch_ops
->decache_regs(vcpu
);
1284 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1286 if (vcpu
->mmio_is_write
) {
1287 vcpu
->mmio_needed
= 0;
1288 return EMULATE_DO_MMIO
;
1291 return EMULATE_DONE
;
1293 EXPORT_SYMBOL_GPL(emulate_instruction
);
1295 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
1297 if (vcpu
->irq_summary
)
1300 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
1301 ++vcpu
->stat
.halt_exits
;
1304 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
1306 int kvm_hypercall(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1308 unsigned long nr
, a0
, a1
, a2
, a3
, a4
, a5
, ret
;
1310 kvm_arch_ops
->cache_regs(vcpu
);
1312 #ifdef CONFIG_X86_64
1313 if (is_long_mode(vcpu
)) {
1314 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1315 a0
= vcpu
->regs
[VCPU_REGS_RDI
];
1316 a1
= vcpu
->regs
[VCPU_REGS_RSI
];
1317 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1318 a3
= vcpu
->regs
[VCPU_REGS_RCX
];
1319 a4
= vcpu
->regs
[VCPU_REGS_R8
];
1320 a5
= vcpu
->regs
[VCPU_REGS_R9
];
1324 nr
= vcpu
->regs
[VCPU_REGS_RBX
] & -1u;
1325 a0
= vcpu
->regs
[VCPU_REGS_RAX
] & -1u;
1326 a1
= vcpu
->regs
[VCPU_REGS_RCX
] & -1u;
1327 a2
= vcpu
->regs
[VCPU_REGS_RDX
] & -1u;
1328 a3
= vcpu
->regs
[VCPU_REGS_RSI
] & -1u;
1329 a4
= vcpu
->regs
[VCPU_REGS_RDI
] & -1u;
1330 a5
= vcpu
->regs
[VCPU_REGS_RBP
] & -1u;
1334 run
->hypercall
.args
[0] = a0
;
1335 run
->hypercall
.args
[1] = a1
;
1336 run
->hypercall
.args
[2] = a2
;
1337 run
->hypercall
.args
[3] = a3
;
1338 run
->hypercall
.args
[4] = a4
;
1339 run
->hypercall
.args
[5] = a5
;
1340 run
->hypercall
.ret
= ret
;
1341 run
->hypercall
.longmode
= is_long_mode(vcpu
);
1342 kvm_arch_ops
->decache_regs(vcpu
);
1345 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1346 kvm_arch_ops
->decache_regs(vcpu
);
1349 EXPORT_SYMBOL_GPL(kvm_hypercall
);
1351 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1353 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1356 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1358 struct descriptor_table dt
= { limit
, base
};
1360 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1363 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1365 struct descriptor_table dt
= { limit
, base
};
1367 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1370 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1371 unsigned long *rflags
)
1374 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1377 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1379 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
1390 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1395 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1396 unsigned long *rflags
)
1400 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1401 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1410 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1413 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1418 * Register the para guest with the host:
1420 static int vcpu_register_para(struct kvm_vcpu
*vcpu
, gpa_t para_state_gpa
)
1422 struct kvm_vcpu_para_state
*para_state
;
1423 hpa_t para_state_hpa
, hypercall_hpa
;
1424 struct page
*para_state_page
;
1425 unsigned char *hypercall
;
1426 gpa_t hypercall_gpa
;
1428 printk(KERN_DEBUG
"kvm: guest trying to enter paravirtual mode\n");
1429 printk(KERN_DEBUG
".... para_state_gpa: %08Lx\n", para_state_gpa
);
1432 * Needs to be page aligned:
1434 if (para_state_gpa
!= PAGE_ALIGN(para_state_gpa
))
1437 para_state_hpa
= gpa_to_hpa(vcpu
, para_state_gpa
);
1438 printk(KERN_DEBUG
".... para_state_hpa: %08Lx\n", para_state_hpa
);
1439 if (is_error_hpa(para_state_hpa
))
1442 mark_page_dirty(vcpu
->kvm
, para_state_gpa
>> PAGE_SHIFT
);
1443 para_state_page
= pfn_to_page(para_state_hpa
>> PAGE_SHIFT
);
1444 para_state
= kmap_atomic(para_state_page
, KM_USER0
);
1446 printk(KERN_DEBUG
".... guest version: %d\n", para_state
->guest_version
);
1447 printk(KERN_DEBUG
".... size: %d\n", para_state
->size
);
1449 para_state
->host_version
= KVM_PARA_API_VERSION
;
1451 * We cannot support guests that try to register themselves
1452 * with a newer API version than the host supports:
1454 if (para_state
->guest_version
> KVM_PARA_API_VERSION
) {
1455 para_state
->ret
= -KVM_EINVAL
;
1456 goto err_kunmap_skip
;
1459 hypercall_gpa
= para_state
->hypercall_gpa
;
1460 hypercall_hpa
= gpa_to_hpa(vcpu
, hypercall_gpa
);
1461 printk(KERN_DEBUG
".... hypercall_hpa: %08Lx\n", hypercall_hpa
);
1462 if (is_error_hpa(hypercall_hpa
)) {
1463 para_state
->ret
= -KVM_EINVAL
;
1464 goto err_kunmap_skip
;
1467 printk(KERN_DEBUG
"kvm: para guest successfully registered.\n");
1468 vcpu
->para_state_page
= para_state_page
;
1469 vcpu
->para_state_gpa
= para_state_gpa
;
1470 vcpu
->hypercall_gpa
= hypercall_gpa
;
1472 mark_page_dirty(vcpu
->kvm
, hypercall_gpa
>> PAGE_SHIFT
);
1473 hypercall
= kmap_atomic(pfn_to_page(hypercall_hpa
>> PAGE_SHIFT
),
1474 KM_USER1
) + (hypercall_hpa
& ~PAGE_MASK
);
1475 kvm_arch_ops
->patch_hypercall(vcpu
, hypercall
);
1476 kunmap_atomic(hypercall
, KM_USER1
);
1478 para_state
->ret
= 0;
1480 kunmap_atomic(para_state
, KM_USER0
);
1486 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1491 case 0xc0010010: /* SYSCFG */
1492 case 0xc0010015: /* HWCR */
1493 case MSR_IA32_PLATFORM_ID
:
1494 case MSR_IA32_P5_MC_ADDR
:
1495 case MSR_IA32_P5_MC_TYPE
:
1496 case MSR_IA32_MC0_CTL
:
1497 case MSR_IA32_MCG_STATUS
:
1498 case MSR_IA32_MCG_CAP
:
1499 case MSR_IA32_MC0_MISC
:
1500 case MSR_IA32_MC0_MISC
+4:
1501 case MSR_IA32_MC0_MISC
+8:
1502 case MSR_IA32_MC0_MISC
+12:
1503 case MSR_IA32_MC0_MISC
+16:
1504 case MSR_IA32_UCODE_REV
:
1505 case MSR_IA32_PERF_STATUS
:
1506 case MSR_IA32_EBL_CR_POWERON
:
1507 /* MTRR registers */
1509 case 0x200 ... 0x2ff:
1512 case 0xcd: /* fsb frequency */
1515 case MSR_IA32_APICBASE
:
1516 data
= vcpu
->apic_base
;
1518 case MSR_IA32_MISC_ENABLE
:
1519 data
= vcpu
->ia32_misc_enable_msr
;
1521 #ifdef CONFIG_X86_64
1523 data
= vcpu
->shadow_efer
;
1527 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1533 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1536 * Reads an msr value (of 'msr_index') into 'pdata'.
1537 * Returns 0 on success, non-0 otherwise.
1538 * Assumes vcpu_load() was already called.
1540 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1542 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1545 #ifdef CONFIG_X86_64
1547 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1549 if (efer
& EFER_RESERVED_BITS
) {
1550 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1557 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1558 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1563 kvm_arch_ops
->set_efer(vcpu
, efer
);
1566 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1568 vcpu
->shadow_efer
= efer
;
1573 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1576 #ifdef CONFIG_X86_64
1578 set_efer(vcpu
, data
);
1581 case MSR_IA32_MC0_STATUS
:
1582 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1583 __FUNCTION__
, data
);
1585 case MSR_IA32_MCG_STATUS
:
1586 printk(KERN_WARNING
"%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1587 __FUNCTION__
, data
);
1589 case MSR_IA32_UCODE_REV
:
1590 case MSR_IA32_UCODE_WRITE
:
1591 case 0x200 ... 0x2ff: /* MTRRs */
1593 case MSR_IA32_APICBASE
:
1594 vcpu
->apic_base
= data
;
1596 case MSR_IA32_MISC_ENABLE
:
1597 vcpu
->ia32_misc_enable_msr
= data
;
1600 * This is the 'probe whether the host is KVM' logic:
1602 case MSR_KVM_API_MAGIC
:
1603 return vcpu_register_para(vcpu
, data
);
1606 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1611 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1614 * Writes msr value into into the appropriate "register".
1615 * Returns 0 on success, non-0 otherwise.
1616 * Assumes vcpu_load() was already called.
1618 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1620 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1623 void kvm_resched(struct kvm_vcpu
*vcpu
)
1625 if (!need_resched())
1631 EXPORT_SYMBOL_GPL(kvm_resched
);
1633 void load_msrs(struct vmx_msr_entry
*e
, int n
)
1637 for (i
= 0; i
< n
; ++i
)
1638 wrmsrl(e
[i
].index
, e
[i
].data
);
1640 EXPORT_SYMBOL_GPL(load_msrs
);
1642 void save_msrs(struct vmx_msr_entry
*e
, int n
)
1646 for (i
= 0; i
< n
; ++i
)
1647 rdmsrl(e
[i
].index
, e
[i
].data
);
1649 EXPORT_SYMBOL_GPL(save_msrs
);
1651 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1655 struct kvm_cpuid_entry
*e
, *best
;
1657 kvm_arch_ops
->cache_regs(vcpu
);
1658 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1659 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1660 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1661 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1662 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1664 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1665 e
= &vcpu
->cpuid_entries
[i
];
1666 if (e
->function
== function
) {
1671 * Both basic or both extended?
1673 if (((e
->function
^ function
) & 0x80000000) == 0)
1674 if (!best
|| e
->function
> best
->function
)
1678 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1679 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1680 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1681 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1683 kvm_arch_ops
->decache_regs(vcpu
);
1684 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1686 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1688 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1690 void *p
= vcpu
->pio_data
;
1693 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1695 kvm_arch_ops
->vcpu_put(vcpu
);
1696 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1699 kvm_arch_ops
->vcpu_load(vcpu
);
1700 free_pio_guest_pages(vcpu
);
1703 q
+= vcpu
->pio
.guest_page_offset
;
1704 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1706 memcpy(q
, p
, bytes
);
1708 memcpy(p
, q
, bytes
);
1709 q
-= vcpu
->pio
.guest_page_offset
;
1711 kvm_arch_ops
->vcpu_load(vcpu
);
1712 free_pio_guest_pages(vcpu
);
1716 static int complete_pio(struct kvm_vcpu
*vcpu
)
1718 struct kvm_pio_request
*io
= &vcpu
->pio
;
1722 kvm_arch_ops
->cache_regs(vcpu
);
1726 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1730 r
= pio_copy_data(vcpu
);
1732 kvm_arch_ops
->cache_regs(vcpu
);
1739 delta
*= io
->cur_count
;
1741 * The size of the register should really depend on
1742 * current address size.
1744 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1750 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1752 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1755 kvm_arch_ops
->decache_regs(vcpu
);
1757 io
->count
-= io
->cur_count
;
1763 void kernel_pio(struct kvm_io_device
*pio_dev
, struct kvm_vcpu
*vcpu
)
1765 /* TODO: String I/O for in kernel device */
1768 kvm_iodevice_read(pio_dev
, vcpu
->pio
.port
,
1772 kvm_iodevice_write(pio_dev
, vcpu
->pio
.port
,
1777 int kvm_setup_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1778 int size
, unsigned long count
, int string
, int down
,
1779 gva_t address
, int rep
, unsigned port
)
1781 unsigned now
, in_page
;
1785 struct kvm_io_device
*pio_dev
;
1787 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1788 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1789 vcpu
->run
->io
.size
= size
;
1790 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1791 vcpu
->run
->io
.count
= count
;
1792 vcpu
->run
->io
.port
= port
;
1793 vcpu
->pio
.count
= count
;
1794 vcpu
->pio
.cur_count
= count
;
1795 vcpu
->pio
.size
= size
;
1797 vcpu
->pio
.port
= port
;
1798 vcpu
->pio
.string
= string
;
1799 vcpu
->pio
.down
= down
;
1800 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1801 vcpu
->pio
.rep
= rep
;
1803 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1805 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1806 kvm_arch_ops
->cache_regs(vcpu
);
1807 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1808 kvm_arch_ops
->decache_regs(vcpu
);
1810 kernel_pio(pio_dev
, vcpu
);
1816 /* TODO: String I/O for in kernel device */
1818 printk(KERN_ERR
"kvm_setup_pio: no string io support\n");
1821 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1825 now
= min(count
, PAGE_SIZE
/ size
);
1828 in_page
= PAGE_SIZE
- offset_in_page(address
);
1830 in_page
= offset_in_page(address
) + size
;
1831 now
= min(count
, (unsigned long)in_page
/ size
);
1834 * String I/O straddles page boundary. Pin two guest pages
1835 * so that we satisfy atomicity constraints. Do just one
1836 * transaction to avoid complexity.
1843 * String I/O in reverse. Yuck. Kill the guest, fix later.
1845 printk(KERN_ERR
"kvm: guest string pio down\n");
1849 vcpu
->run
->io
.count
= now
;
1850 vcpu
->pio
.cur_count
= now
;
1853 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1855 for (i
= 0; i
< nr_pages
; ++i
) {
1856 spin_lock(&vcpu
->kvm
->lock
);
1857 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
1860 vcpu
->pio
.guest_pages
[i
] = page
;
1861 spin_unlock(&vcpu
->kvm
->lock
);
1864 free_pio_guest_pages(vcpu
);
1870 return pio_copy_data(vcpu
);
1873 EXPORT_SYMBOL_GPL(kvm_setup_pio
);
1875 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1882 if (vcpu
->sigset_active
)
1883 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
1885 /* re-sync apic's tpr */
1886 vcpu
->cr8
= kvm_run
->cr8
;
1888 if (vcpu
->pio
.cur_count
) {
1889 r
= complete_pio(vcpu
);
1894 if (vcpu
->mmio_needed
) {
1895 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1896 vcpu
->mmio_read_completed
= 1;
1897 vcpu
->mmio_needed
= 0;
1898 r
= emulate_instruction(vcpu
, kvm_run
,
1899 vcpu
->mmio_fault_cr2
, 0);
1900 if (r
== EMULATE_DO_MMIO
) {
1902 * Read-modify-write. Back to userspace.
1904 kvm_run
->exit_reason
= KVM_EXIT_MMIO
;
1910 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
1911 kvm_arch_ops
->cache_regs(vcpu
);
1912 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
1913 kvm_arch_ops
->decache_regs(vcpu
);
1916 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1919 if (vcpu
->sigset_active
)
1920 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1926 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
1927 struct kvm_regs
*regs
)
1931 kvm_arch_ops
->cache_regs(vcpu
);
1933 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
1934 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
1935 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
1936 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
1937 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
1938 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
1939 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
1940 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
1941 #ifdef CONFIG_X86_64
1942 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
1943 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
1944 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
1945 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
1946 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
1947 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
1948 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
1949 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
1952 regs
->rip
= vcpu
->rip
;
1953 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1956 * Don't leak debug flags in case they were set for guest debugging
1958 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
1959 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1966 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
1967 struct kvm_regs
*regs
)
1971 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
1972 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
1973 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
1974 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
1975 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
1976 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
1977 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
1978 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
1979 #ifdef CONFIG_X86_64
1980 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
1981 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
1982 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
1983 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
1984 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
1985 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
1986 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
1987 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
1990 vcpu
->rip
= regs
->rip
;
1991 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
1993 kvm_arch_ops
->decache_regs(vcpu
);
2000 static void get_segment(struct kvm_vcpu
*vcpu
,
2001 struct kvm_segment
*var
, int seg
)
2003 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
2006 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2007 struct kvm_sregs
*sregs
)
2009 struct descriptor_table dt
;
2013 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2014 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2015 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2016 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2017 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2018 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2020 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2021 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2023 kvm_arch_ops
->get_idt(vcpu
, &dt
);
2024 sregs
->idt
.limit
= dt
.limit
;
2025 sregs
->idt
.base
= dt
.base
;
2026 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
2027 sregs
->gdt
.limit
= dt
.limit
;
2028 sregs
->gdt
.base
= dt
.base
;
2030 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2031 sregs
->cr0
= vcpu
->cr0
;
2032 sregs
->cr2
= vcpu
->cr2
;
2033 sregs
->cr3
= vcpu
->cr3
;
2034 sregs
->cr4
= vcpu
->cr4
;
2035 sregs
->cr8
= vcpu
->cr8
;
2036 sregs
->efer
= vcpu
->shadow_efer
;
2037 sregs
->apic_base
= vcpu
->apic_base
;
2039 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
2040 sizeof sregs
->interrupt_bitmap
);
2047 static void set_segment(struct kvm_vcpu
*vcpu
,
2048 struct kvm_segment
*var
, int seg
)
2050 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
2053 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2054 struct kvm_sregs
*sregs
)
2056 int mmu_reset_needed
= 0;
2058 struct descriptor_table dt
;
2062 dt
.limit
= sregs
->idt
.limit
;
2063 dt
.base
= sregs
->idt
.base
;
2064 kvm_arch_ops
->set_idt(vcpu
, &dt
);
2065 dt
.limit
= sregs
->gdt
.limit
;
2066 dt
.base
= sregs
->gdt
.base
;
2067 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
2069 vcpu
->cr2
= sregs
->cr2
;
2070 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2071 vcpu
->cr3
= sregs
->cr3
;
2073 vcpu
->cr8
= sregs
->cr8
;
2075 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2076 #ifdef CONFIG_X86_64
2077 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
2079 vcpu
->apic_base
= sregs
->apic_base
;
2081 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2083 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2084 kvm_arch_ops
->set_cr0(vcpu
, sregs
->cr0
);
2086 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2087 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
2088 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2089 load_pdptrs(vcpu
, vcpu
->cr3
);
2091 if (mmu_reset_needed
)
2092 kvm_mmu_reset_context(vcpu
);
2094 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2095 sizeof vcpu
->irq_pending
);
2096 vcpu
->irq_summary
= 0;
2097 for (i
= 0; i
< NR_IRQ_WORDS
; ++i
)
2098 if (vcpu
->irq_pending
[i
])
2099 __set_bit(i
, &vcpu
->irq_summary
);
2101 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2102 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2103 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2104 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2105 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2106 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2108 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2109 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2117 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2118 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2120 * This list is modified at module load time to reflect the
2121 * capabilities of the host cpu.
2123 static u32 msrs_to_save
[] = {
2124 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
2126 #ifdef CONFIG_X86_64
2127 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
2129 MSR_IA32_TIME_STAMP_COUNTER
,
2132 static unsigned num_msrs_to_save
;
2134 static u32 emulated_msrs
[] = {
2135 MSR_IA32_MISC_ENABLE
,
2138 static __init
void kvm_init_msr_list(void)
2143 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2144 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2147 msrs_to_save
[j
] = msrs_to_save
[i
];
2150 num_msrs_to_save
= j
;
2154 * Adapt set_msr() to msr_io()'s calling convention
2156 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
2158 return kvm_set_msr(vcpu
, index
, *data
);
2162 * Read or write a bunch of msrs. All parameters are kernel addresses.
2164 * @return number of msrs set successfully.
2166 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
2167 struct kvm_msr_entry
*entries
,
2168 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2169 unsigned index
, u64
*data
))
2175 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
2176 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
2185 * Read or write a bunch of msrs. Parameters are user addresses.
2187 * @return number of msrs set successfully.
2189 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
2190 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2191 unsigned index
, u64
*data
),
2194 struct kvm_msrs msrs
;
2195 struct kvm_msr_entry
*entries
;
2200 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
2204 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
2208 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
2209 entries
= vmalloc(size
);
2214 if (copy_from_user(entries
, user_msrs
->entries
, size
))
2217 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
2222 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
2234 * Translate a guest virtual address to a guest physical address.
2236 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2237 struct kvm_translation
*tr
)
2239 unsigned long vaddr
= tr
->linear_address
;
2243 spin_lock(&vcpu
->kvm
->lock
);
2244 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2245 tr
->physical_address
= gpa
;
2246 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2249 spin_unlock(&vcpu
->kvm
->lock
);
2255 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2256 struct kvm_interrupt
*irq
)
2258 if (irq
->irq
< 0 || irq
->irq
>= 256)
2262 set_bit(irq
->irq
, vcpu
->irq_pending
);
2263 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
2270 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2271 struct kvm_debug_guest
*dbg
)
2277 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
2284 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
2285 unsigned long address
,
2288 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
2289 unsigned long pgoff
;
2292 *type
= VM_FAULT_MINOR
;
2293 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2295 page
= virt_to_page(vcpu
->run
);
2296 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
2297 page
= virt_to_page(vcpu
->pio_data
);
2299 return NOPAGE_SIGBUS
;
2304 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
2305 .nopage
= kvm_vcpu_nopage
,
2308 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2310 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
2314 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
2316 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2318 fput(vcpu
->kvm
->filp
);
2322 static struct file_operations kvm_vcpu_fops
= {
2323 .release
= kvm_vcpu_release
,
2324 .unlocked_ioctl
= kvm_vcpu_ioctl
,
2325 .compat_ioctl
= kvm_vcpu_ioctl
,
2326 .mmap
= kvm_vcpu_mmap
,
2330 * Allocates an inode for the vcpu.
2332 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
2335 struct inode
*inode
;
2338 r
= anon_inode_getfd(&fd
, &inode
, &file
,
2339 "kvm-vcpu", &kvm_vcpu_fops
, vcpu
);
2342 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
2347 * Creates some virtual cpus. Good luck creating more than one.
2349 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
2352 struct kvm_vcpu
*vcpu
;
2359 vcpu
= &kvm
->vcpus
[n
];
2361 mutex_lock(&vcpu
->mutex
);
2364 mutex_unlock(&vcpu
->mutex
);
2368 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2372 vcpu
->run
= page_address(page
);
2374 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2378 vcpu
->pio_data
= page_address(page
);
2380 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
2382 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
2385 r
= kvm_arch_ops
->vcpu_create(vcpu
);
2387 goto out_free_vcpus
;
2389 r
= kvm_mmu_create(vcpu
);
2391 goto out_free_vcpus
;
2393 kvm_arch_ops
->vcpu_load(vcpu
);
2394 r
= kvm_mmu_setup(vcpu
);
2396 r
= kvm_arch_ops
->vcpu_setup(vcpu
);
2400 goto out_free_vcpus
;
2402 r
= create_vcpu_fd(vcpu
);
2404 goto out_free_vcpus
;
2406 spin_lock(&kvm_lock
);
2407 if (n
>= kvm
->nvcpus
)
2408 kvm
->nvcpus
= n
+ 1;
2409 spin_unlock(&kvm_lock
);
2414 kvm_free_vcpu(vcpu
);
2416 free_page((unsigned long)vcpu
->run
);
2419 mutex_unlock(&vcpu
->mutex
);
2424 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
2428 struct kvm_cpuid_entry
*e
, *entry
;
2430 rdmsrl(MSR_EFER
, efer
);
2432 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
2433 e
= &vcpu
->cpuid_entries
[i
];
2434 if (e
->function
== 0x80000001) {
2439 if (entry
&& (entry
->edx
& (1 << 20)) && !(efer
& EFER_NX
)) {
2440 entry
->edx
&= ~(1 << 20);
2441 printk(KERN_INFO
"kvm: guest NX capability removed\n");
2445 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
2446 struct kvm_cpuid
*cpuid
,
2447 struct kvm_cpuid_entry __user
*entries
)
2452 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2455 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
2456 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
2458 vcpu
->cpuid_nent
= cpuid
->nent
;
2459 cpuid_fix_nx_cap(vcpu
);
2466 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
2469 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
2470 vcpu
->sigset_active
= 1;
2471 vcpu
->sigset
= *sigset
;
2473 vcpu
->sigset_active
= 0;
2478 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2479 * we have asm/x86/processor.h
2490 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2491 #ifdef CONFIG_X86_64
2492 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2494 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2498 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2500 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2504 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2505 fpu
->fcw
= fxsave
->cwd
;
2506 fpu
->fsw
= fxsave
->swd
;
2507 fpu
->ftwx
= fxsave
->twd
;
2508 fpu
->last_opcode
= fxsave
->fop
;
2509 fpu
->last_ip
= fxsave
->rip
;
2510 fpu
->last_dp
= fxsave
->rdp
;
2511 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2518 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2520 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2524 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2525 fxsave
->cwd
= fpu
->fcw
;
2526 fxsave
->swd
= fpu
->fsw
;
2527 fxsave
->twd
= fpu
->ftwx
;
2528 fxsave
->fop
= fpu
->last_opcode
;
2529 fxsave
->rip
= fpu
->last_ip
;
2530 fxsave
->rdp
= fpu
->last_dp
;
2531 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2538 static long kvm_vcpu_ioctl(struct file
*filp
,
2539 unsigned int ioctl
, unsigned long arg
)
2541 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2542 void __user
*argp
= (void __user
*)arg
;
2550 r
= kvm_vcpu_ioctl_run(vcpu
, vcpu
->run
);
2552 case KVM_GET_REGS
: {
2553 struct kvm_regs kvm_regs
;
2555 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2556 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2560 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2565 case KVM_SET_REGS
: {
2566 struct kvm_regs kvm_regs
;
2569 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2571 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2577 case KVM_GET_SREGS
: {
2578 struct kvm_sregs kvm_sregs
;
2580 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2581 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2585 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2590 case KVM_SET_SREGS
: {
2591 struct kvm_sregs kvm_sregs
;
2594 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2596 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2602 case KVM_TRANSLATE
: {
2603 struct kvm_translation tr
;
2606 if (copy_from_user(&tr
, argp
, sizeof tr
))
2608 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2612 if (copy_to_user(argp
, &tr
, sizeof tr
))
2617 case KVM_INTERRUPT
: {
2618 struct kvm_interrupt irq
;
2621 if (copy_from_user(&irq
, argp
, sizeof irq
))
2623 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2629 case KVM_DEBUG_GUEST
: {
2630 struct kvm_debug_guest dbg
;
2633 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2635 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2642 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
2645 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2647 case KVM_SET_CPUID
: {
2648 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2649 struct kvm_cpuid cpuid
;
2652 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2654 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2659 case KVM_SET_SIGNAL_MASK
: {
2660 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
2661 struct kvm_signal_mask kvm_sigmask
;
2662 sigset_t sigset
, *p
;
2667 if (copy_from_user(&kvm_sigmask
, argp
,
2668 sizeof kvm_sigmask
))
2671 if (kvm_sigmask
.len
!= sizeof sigset
)
2674 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2679 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2685 memset(&fpu
, 0, sizeof fpu
);
2686 r
= kvm_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
2690 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
2699 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
2701 r
= kvm_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
2714 static long kvm_vm_ioctl(struct file
*filp
,
2715 unsigned int ioctl
, unsigned long arg
)
2717 struct kvm
*kvm
= filp
->private_data
;
2718 void __user
*argp
= (void __user
*)arg
;
2722 case KVM_CREATE_VCPU
:
2723 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2727 case KVM_SET_MEMORY_REGION
: {
2728 struct kvm_memory_region kvm_mem
;
2731 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2733 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
2738 case KVM_GET_DIRTY_LOG
: {
2739 struct kvm_dirty_log log
;
2742 if (copy_from_user(&log
, argp
, sizeof log
))
2744 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2749 case KVM_SET_MEMORY_ALIAS
: {
2750 struct kvm_memory_alias alias
;
2753 if (copy_from_user(&alias
, argp
, sizeof alias
))
2755 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
2767 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
2768 unsigned long address
,
2771 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2772 unsigned long pgoff
;
2775 *type
= VM_FAULT_MINOR
;
2776 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2777 page
= gfn_to_page(kvm
, pgoff
);
2779 return NOPAGE_SIGBUS
;
2784 static struct vm_operations_struct kvm_vm_vm_ops
= {
2785 .nopage
= kvm_vm_nopage
,
2788 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2790 vma
->vm_ops
= &kvm_vm_vm_ops
;
2794 static struct file_operations kvm_vm_fops
= {
2795 .release
= kvm_vm_release
,
2796 .unlocked_ioctl
= kvm_vm_ioctl
,
2797 .compat_ioctl
= kvm_vm_ioctl
,
2798 .mmap
= kvm_vm_mmap
,
2801 static int kvm_dev_ioctl_create_vm(void)
2804 struct inode
*inode
;
2808 kvm
= kvm_create_vm();
2810 return PTR_ERR(kvm
);
2811 r
= anon_inode_getfd(&fd
, &inode
, &file
, "kvm-vm", &kvm_vm_fops
, kvm
);
2813 kvm_destroy_vm(kvm
);
2822 static long kvm_dev_ioctl(struct file
*filp
,
2823 unsigned int ioctl
, unsigned long arg
)
2825 void __user
*argp
= (void __user
*)arg
;
2829 case KVM_GET_API_VERSION
:
2833 r
= KVM_API_VERSION
;
2839 r
= kvm_dev_ioctl_create_vm();
2841 case KVM_GET_MSR_INDEX_LIST
: {
2842 struct kvm_msr_list __user
*user_msr_list
= argp
;
2843 struct kvm_msr_list msr_list
;
2847 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
2850 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
2851 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
2854 if (n
< num_msrs_to_save
)
2857 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
2858 num_msrs_to_save
* sizeof(u32
)))
2860 if (copy_to_user(user_msr_list
->indices
2861 + num_msrs_to_save
* sizeof(u32
),
2863 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
2868 case KVM_CHECK_EXTENSION
:
2870 * No extensions defined at present.
2874 case KVM_GET_VCPU_MMAP_SIZE
:
2887 static struct file_operations kvm_chardev_ops
= {
2888 .open
= kvm_dev_open
,
2889 .release
= kvm_dev_release
,
2890 .unlocked_ioctl
= kvm_dev_ioctl
,
2891 .compat_ioctl
= kvm_dev_ioctl
,
2894 static struct miscdevice kvm_dev
= {
2900 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
2903 if (val
== SYS_RESTART
) {
2905 * Some (well, at least mine) BIOSes hang on reboot if
2908 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
2909 on_each_cpu(hardware_disable
, NULL
, 0, 1);
2914 static struct notifier_block kvm_reboot_notifier
= {
2915 .notifier_call
= kvm_reboot
,
2920 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2923 static void decache_vcpus_on_cpu(int cpu
)
2926 struct kvm_vcpu
*vcpu
;
2929 spin_lock(&kvm_lock
);
2930 list_for_each_entry(vm
, &vm_list
, vm_list
)
2931 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2932 vcpu
= &vm
->vcpus
[i
];
2934 * If the vcpu is locked, then it is running on some
2935 * other cpu and therefore it is not cached on the
2938 * If it's not locked, check the last cpu it executed
2941 if (mutex_trylock(&vcpu
->mutex
)) {
2942 if (vcpu
->cpu
== cpu
) {
2943 kvm_arch_ops
->vcpu_decache(vcpu
);
2946 mutex_unlock(&vcpu
->mutex
);
2949 spin_unlock(&kvm_lock
);
2952 static void hardware_enable(void *junk
)
2954 int cpu
= raw_smp_processor_id();
2956 if (cpu_isset(cpu
, cpus_hardware_enabled
))
2958 cpu_set(cpu
, cpus_hardware_enabled
);
2959 kvm_arch_ops
->hardware_enable(NULL
);
2962 static void hardware_disable(void *junk
)
2964 int cpu
= raw_smp_processor_id();
2966 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
2968 cpu_clear(cpu
, cpus_hardware_enabled
);
2969 decache_vcpus_on_cpu(cpu
);
2970 kvm_arch_ops
->hardware_disable(NULL
);
2973 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
2980 case CPU_DYING_FROZEN
:
2981 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2983 hardware_disable(NULL
);
2985 case CPU_UP_CANCELED
:
2986 case CPU_UP_CANCELED_FROZEN
:
2987 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
2989 smp_call_function_single(cpu
, hardware_disable
, NULL
, 0, 1);
2992 case CPU_ONLINE_FROZEN
:
2993 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
2995 smp_call_function_single(cpu
, hardware_enable
, NULL
, 0, 1);
3001 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
3003 memset(bus
, 0, sizeof(*bus
));
3006 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
3010 for (i
= 0; i
< bus
->dev_count
; i
++) {
3011 struct kvm_io_device
*pos
= bus
->devs
[i
];
3013 kvm_iodevice_destructor(pos
);
3017 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
3021 for (i
= 0; i
< bus
->dev_count
; i
++) {
3022 struct kvm_io_device
*pos
= bus
->devs
[i
];
3024 if (pos
->in_range(pos
, addr
))
3031 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
3033 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
3035 bus
->devs
[bus
->dev_count
++] = dev
;
3038 static struct notifier_block kvm_cpu_notifier
= {
3039 .notifier_call
= kvm_cpu_hotplug
,
3040 .priority
= 20, /* must be > scheduler priority */
3043 static u64
stat_get(void *_offset
)
3045 unsigned offset
= (long)_offset
;
3048 struct kvm_vcpu
*vcpu
;
3051 spin_lock(&kvm_lock
);
3052 list_for_each_entry(kvm
, &vm_list
, vm_list
)
3053 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3054 vcpu
= &kvm
->vcpus
[i
];
3055 total
+= *(u32
*)((void *)vcpu
+ offset
);
3057 spin_unlock(&kvm_lock
);
3061 static void stat_set(void *offset
, u64 val
)
3065 DEFINE_SIMPLE_ATTRIBUTE(stat_fops
, stat_get
, stat_set
, "%llu\n");
3067 static __init
void kvm_init_debug(void)
3069 struct kvm_stats_debugfs_item
*p
;
3071 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
3072 for (p
= debugfs_entries
; p
->name
; ++p
)
3073 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
3074 (void *)(long)p
->offset
,
3078 static void kvm_exit_debug(void)
3080 struct kvm_stats_debugfs_item
*p
;
3082 for (p
= debugfs_entries
; p
->name
; ++p
)
3083 debugfs_remove(p
->dentry
);
3084 debugfs_remove(debugfs_dir
);
3087 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
3089 hardware_disable(NULL
);
3093 static int kvm_resume(struct sys_device
*dev
)
3095 hardware_enable(NULL
);
3099 static struct sysdev_class kvm_sysdev_class
= {
3100 set_kset_name("kvm"),
3101 .suspend
= kvm_suspend
,
3102 .resume
= kvm_resume
,
3105 static struct sys_device kvm_sysdev
= {
3107 .cls
= &kvm_sysdev_class
,
3110 hpa_t bad_page_address
;
3112 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
3117 printk(KERN_ERR
"kvm: already loaded the other module\n");
3121 if (!ops
->cpu_has_kvm_support()) {
3122 printk(KERN_ERR
"kvm: no hardware support\n");
3125 if (ops
->disabled_by_bios()) {
3126 printk(KERN_ERR
"kvm: disabled by bios\n");
3132 r
= kvm_arch_ops
->hardware_setup();
3136 on_each_cpu(hardware_enable
, NULL
, 0, 1);
3137 r
= register_cpu_notifier(&kvm_cpu_notifier
);
3140 register_reboot_notifier(&kvm_reboot_notifier
);
3142 r
= sysdev_class_register(&kvm_sysdev_class
);
3146 r
= sysdev_register(&kvm_sysdev
);
3150 kvm_chardev_ops
.owner
= module
;
3152 r
= misc_register(&kvm_dev
);
3154 printk (KERN_ERR
"kvm: misc device register failed\n");
3161 sysdev_unregister(&kvm_sysdev
);
3163 sysdev_class_unregister(&kvm_sysdev_class
);
3165 unregister_reboot_notifier(&kvm_reboot_notifier
);
3166 unregister_cpu_notifier(&kvm_cpu_notifier
);
3168 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3169 kvm_arch_ops
->hardware_unsetup();
3171 kvm_arch_ops
= NULL
;
3175 void kvm_exit_arch(void)
3177 misc_deregister(&kvm_dev
);
3178 sysdev_unregister(&kvm_sysdev
);
3179 sysdev_class_unregister(&kvm_sysdev_class
);
3180 unregister_reboot_notifier(&kvm_reboot_notifier
);
3181 unregister_cpu_notifier(&kvm_cpu_notifier
);
3182 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3183 kvm_arch_ops
->hardware_unsetup();
3184 kvm_arch_ops
= NULL
;
3187 static __init
int kvm_init(void)
3189 static struct page
*bad_page
;
3192 r
= kvm_mmu_module_init();
3198 kvm_init_msr_list();
3200 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
3205 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
3206 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
3212 kvm_mmu_module_exit();
3217 static __exit
void kvm_exit(void)
3220 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
3221 kvm_mmu_module_exit();
3224 module_init(kvm_init
)
3225 module_exit(kvm_exit
)
3227 EXPORT_SYMBOL_GPL(kvm_init_arch
);
3228 EXPORT_SYMBOL_GPL(kvm_exit_arch
);