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 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
59 static struct kvm_stats_debugfs_item
{
62 struct dentry
*dentry
;
63 } debugfs_entries
[] = {
64 { "pf_fixed", STAT_OFFSET(pf_fixed
) },
65 { "pf_guest", STAT_OFFSET(pf_guest
) },
66 { "tlb_flush", STAT_OFFSET(tlb_flush
) },
67 { "invlpg", STAT_OFFSET(invlpg
) },
68 { "exits", STAT_OFFSET(exits
) },
69 { "io_exits", STAT_OFFSET(io_exits
) },
70 { "mmio_exits", STAT_OFFSET(mmio_exits
) },
71 { "signal_exits", STAT_OFFSET(signal_exits
) },
72 { "irq_window", STAT_OFFSET(irq_window_exits
) },
73 { "halt_exits", STAT_OFFSET(halt_exits
) },
74 { "request_irq", STAT_OFFSET(request_irq_exits
) },
75 { "irq_exits", STAT_OFFSET(irq_exits
) },
76 { "light_exits", STAT_OFFSET(light_exits
) },
77 { "efer_reload", STAT_OFFSET(efer_reload
) },
81 static struct dentry
*debugfs_dir
;
83 #define MAX_IO_MSRS 256
85 #define CR0_RESERVED_BITS \
86 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
87 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
88 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
89 #define CR4_RESERVED_BITS \
90 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
91 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
92 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
93 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
95 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
96 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
99 // LDT or TSS descriptor in the GDT. 16 bytes.
100 struct segment_descriptor_64
{
101 struct segment_descriptor s
;
108 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
111 unsigned long segment_base(u16 selector
)
113 struct descriptor_table gdt
;
114 struct segment_descriptor
*d
;
115 unsigned long table_base
;
116 typedef unsigned long ul
;
122 asm ("sgdt %0" : "=m"(gdt
));
123 table_base
= gdt
.base
;
125 if (selector
& 4) { /* from ldt */
128 asm ("sldt %0" : "=g"(ldt_selector
));
129 table_base
= segment_base(ldt_selector
);
131 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
132 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
135 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
136 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
140 EXPORT_SYMBOL_GPL(segment_base
);
142 static inline int valid_vcpu(int n
)
144 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
147 int kvm_read_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
150 unsigned char *host_buf
= dest
;
151 unsigned long req_size
= size
;
159 paddr
= gva_to_hpa(vcpu
, addr
);
161 if (is_error_hpa(paddr
))
164 guest_buf
= (hva_t
)kmap_atomic(
165 pfn_to_page(paddr
>> PAGE_SHIFT
),
167 offset
= addr
& ~PAGE_MASK
;
169 now
= min(size
, PAGE_SIZE
- offset
);
170 memcpy(host_buf
, (void*)guest_buf
, now
);
174 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
176 return req_size
- size
;
178 EXPORT_SYMBOL_GPL(kvm_read_guest
);
180 int kvm_write_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
183 unsigned char *host_buf
= data
;
184 unsigned long req_size
= size
;
193 paddr
= gva_to_hpa(vcpu
, addr
);
195 if (is_error_hpa(paddr
))
198 gfn
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
) >> PAGE_SHIFT
;
199 mark_page_dirty(vcpu
->kvm
, gfn
);
200 guest_buf
= (hva_t
)kmap_atomic(
201 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
202 offset
= addr
& ~PAGE_MASK
;
204 now
= min(size
, PAGE_SIZE
- offset
);
205 memcpy((void*)guest_buf
, host_buf
, now
);
209 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
211 return req_size
- size
;
213 EXPORT_SYMBOL_GPL(kvm_write_guest
);
215 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
217 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
220 vcpu
->guest_fpu_loaded
= 1;
221 fx_save(vcpu
->host_fx_image
);
222 fx_restore(vcpu
->guest_fx_image
);
224 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
226 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
228 if (!vcpu
->guest_fpu_loaded
)
231 vcpu
->guest_fpu_loaded
= 0;
232 fx_save(vcpu
->guest_fx_image
);
233 fx_restore(vcpu
->host_fx_image
);
235 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
238 * Switches to specified vcpu, until a matching vcpu_put()
240 static void vcpu_load(struct kvm_vcpu
*vcpu
)
242 mutex_lock(&vcpu
->mutex
);
243 kvm_arch_ops
->vcpu_load(vcpu
);
246 static void vcpu_put(struct kvm_vcpu
*vcpu
)
248 kvm_arch_ops
->vcpu_put(vcpu
);
249 mutex_unlock(&vcpu
->mutex
);
252 static void ack_flush(void *_completed
)
254 atomic_t
*completed
= _completed
;
256 atomic_inc(completed
);
259 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
263 struct kvm_vcpu
*vcpu
;
266 atomic_set(&completed
, 0);
269 for (i
= 0; i
< kvm
->nvcpus
; ++i
) {
270 vcpu
= &kvm
->vcpus
[i
];
271 if (test_and_set_bit(KVM_TLB_FLUSH
, &vcpu
->requests
))
274 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
275 if (!cpu_isset(cpu
, 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_RESERVED_BITS
) {
477 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
483 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
484 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
489 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
490 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
491 "and a clear PE flag\n");
496 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
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_RESERVED_BITS
) {
545 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
550 if (is_long_mode(vcpu
)) {
551 if (!(cr4
& X86_CR4_PAE
)) {
552 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
557 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
558 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
559 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
564 if (cr4
& X86_CR4_VMXE
) {
565 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
569 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
570 spin_lock(&vcpu
->kvm
->lock
);
571 kvm_mmu_reset_context(vcpu
);
572 spin_unlock(&vcpu
->kvm
->lock
);
574 EXPORT_SYMBOL_GPL(set_cr4
);
576 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
578 if (is_long_mode(vcpu
)) {
579 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
580 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
586 if (cr3
& CR3_PAE_RESERVED_BITS
) {
588 "set_cr3: #GP, reserved bits\n");
592 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
593 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
599 if (cr3
& CR3_NONPAE_RESERVED_BITS
) {
601 "set_cr3: #GP, reserved bits\n");
609 spin_lock(&vcpu
->kvm
->lock
);
611 * Does the new cr3 value map to physical memory? (Note, we
612 * catch an invalid cr3 even in real-mode, because it would
613 * cause trouble later on when we turn on paging anyway.)
615 * A real CPU would silently accept an invalid cr3 and would
616 * attempt to use it - with largely undefined (and often hard
617 * to debug) behavior on the guest side.
619 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
622 vcpu
->mmu
.new_cr3(vcpu
);
623 spin_unlock(&vcpu
->kvm
->lock
);
625 EXPORT_SYMBOL_GPL(set_cr3
);
627 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
629 if (cr8
& CR8_RESERVED_BITS
) {
630 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
636 EXPORT_SYMBOL_GPL(set_cr8
);
638 void fx_init(struct kvm_vcpu
*vcpu
)
640 struct __attribute__ ((__packed__
)) fx_image_s
{
646 u64 operand
;// fpu dp
652 fx_save(vcpu
->host_fx_image
);
654 fx_save(vcpu
->guest_fx_image
);
655 fx_restore(vcpu
->host_fx_image
);
657 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
658 fx_image
->mxcsr
= 0x1f80;
659 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
660 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
662 EXPORT_SYMBOL_GPL(fx_init
);
665 * Allocate some memory and give it an address in the guest physical address
668 * Discontiguous memory is allowed, mostly for framebuffers.
670 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
671 struct kvm_memory_region
*mem
)
675 unsigned long npages
;
677 struct kvm_memory_slot
*memslot
;
678 struct kvm_memory_slot old
, new;
679 int memory_config_version
;
682 /* General sanity checks */
683 if (mem
->memory_size
& (PAGE_SIZE
- 1))
685 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
687 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
689 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
692 memslot
= &kvm
->memslots
[mem
->slot
];
693 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
694 npages
= mem
->memory_size
>> PAGE_SHIFT
;
697 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
700 spin_lock(&kvm
->lock
);
702 memory_config_version
= kvm
->memory_config_version
;
703 new = old
= *memslot
;
705 new.base_gfn
= base_gfn
;
707 new.flags
= mem
->flags
;
709 /* Disallow changing a memory slot's size. */
711 if (npages
&& old
.npages
&& npages
!= old
.npages
)
714 /* Check for overlaps */
716 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
717 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
721 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
722 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
726 * Do memory allocations outside lock. memory_config_version will
729 spin_unlock(&kvm
->lock
);
731 /* Deallocate if slot is being removed */
735 /* Free page dirty bitmap if unneeded */
736 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
737 new.dirty_bitmap
= NULL
;
741 /* Allocate if a slot is being created */
742 if (npages
&& !new.phys_mem
) {
743 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
748 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
749 for (i
= 0; i
< npages
; ++i
) {
750 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
752 if (!new.phys_mem
[i
])
754 set_page_private(new.phys_mem
[i
],0);
758 /* Allocate page dirty bitmap if needed */
759 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
760 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
762 new.dirty_bitmap
= vmalloc(dirty_bytes
);
763 if (!new.dirty_bitmap
)
765 memset(new.dirty_bitmap
, 0, dirty_bytes
);
768 spin_lock(&kvm
->lock
);
770 if (memory_config_version
!= kvm
->memory_config_version
) {
771 spin_unlock(&kvm
->lock
);
772 kvm_free_physmem_slot(&new, &old
);
780 if (mem
->slot
>= kvm
->nmemslots
)
781 kvm
->nmemslots
= mem
->slot
+ 1;
784 ++kvm
->memory_config_version
;
786 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
787 kvm_flush_remote_tlbs(kvm
);
789 spin_unlock(&kvm
->lock
);
791 kvm_free_physmem_slot(&old
, &new);
795 spin_unlock(&kvm
->lock
);
797 kvm_free_physmem_slot(&new, &old
);
803 * Get (and clear) the dirty memory log for a memory slot.
805 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
806 struct kvm_dirty_log
*log
)
808 struct kvm_memory_slot
*memslot
;
811 unsigned long any
= 0;
813 spin_lock(&kvm
->lock
);
816 * Prevent changes to guest memory configuration even while the lock
820 spin_unlock(&kvm
->lock
);
822 if (log
->slot
>= KVM_MEMORY_SLOTS
)
825 memslot
= &kvm
->memslots
[log
->slot
];
827 if (!memslot
->dirty_bitmap
)
830 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
832 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
833 any
= memslot
->dirty_bitmap
[i
];
836 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
839 spin_lock(&kvm
->lock
);
840 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
841 kvm_flush_remote_tlbs(kvm
);
842 memset(memslot
->dirty_bitmap
, 0, n
);
843 spin_unlock(&kvm
->lock
);
848 spin_lock(&kvm
->lock
);
850 spin_unlock(&kvm
->lock
);
855 * Set a new alias region. Aliases map a portion of physical memory into
856 * another portion. This is useful for memory windows, for example the PC
859 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
860 struct kvm_memory_alias
*alias
)
863 struct kvm_mem_alias
*p
;
866 /* General sanity checks */
867 if (alias
->memory_size
& (PAGE_SIZE
- 1))
869 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
871 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
873 if (alias
->guest_phys_addr
+ alias
->memory_size
874 < alias
->guest_phys_addr
)
876 if (alias
->target_phys_addr
+ alias
->memory_size
877 < alias
->target_phys_addr
)
880 spin_lock(&kvm
->lock
);
882 p
= &kvm
->aliases
[alias
->slot
];
883 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
884 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
885 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
887 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
888 if (kvm
->aliases
[n
- 1].npages
)
892 kvm_mmu_zap_all(kvm
);
894 spin_unlock(&kvm
->lock
);
902 static gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
905 struct kvm_mem_alias
*alias
;
907 for (i
= 0; i
< kvm
->naliases
; ++i
) {
908 alias
= &kvm
->aliases
[i
];
909 if (gfn
>= alias
->base_gfn
910 && gfn
< alias
->base_gfn
+ alias
->npages
)
911 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
916 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
920 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
921 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
923 if (gfn
>= memslot
->base_gfn
924 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
930 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
932 gfn
= unalias_gfn(kvm
, gfn
);
933 return __gfn_to_memslot(kvm
, gfn
);
936 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
938 struct kvm_memory_slot
*slot
;
940 gfn
= unalias_gfn(kvm
, gfn
);
941 slot
= __gfn_to_memslot(kvm
, gfn
);
944 return slot
->phys_mem
[gfn
- slot
->base_gfn
];
946 EXPORT_SYMBOL_GPL(gfn_to_page
);
948 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
951 struct kvm_memory_slot
*memslot
;
952 unsigned long rel_gfn
;
954 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
955 memslot
= &kvm
->memslots
[i
];
957 if (gfn
>= memslot
->base_gfn
958 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
960 if (!memslot
->dirty_bitmap
)
963 rel_gfn
= gfn
- memslot
->base_gfn
;
966 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
967 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
973 static int emulator_read_std(unsigned long addr
,
976 struct x86_emulate_ctxt
*ctxt
)
978 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
982 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
983 unsigned offset
= addr
& (PAGE_SIZE
-1);
984 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
989 if (gpa
== UNMAPPED_GVA
)
990 return X86EMUL_PROPAGATE_FAULT
;
991 pfn
= gpa
>> PAGE_SHIFT
;
992 page
= gfn_to_page(vcpu
->kvm
, pfn
);
994 return X86EMUL_UNHANDLEABLE
;
995 page_virt
= kmap_atomic(page
, KM_USER0
);
997 memcpy(data
, page_virt
+ offset
, tocopy
);
999 kunmap_atomic(page_virt
, KM_USER0
);
1006 return X86EMUL_CONTINUE
;
1009 static int emulator_write_std(unsigned long addr
,
1012 struct x86_emulate_ctxt
*ctxt
)
1014 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
1016 return X86EMUL_UNHANDLEABLE
;
1019 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1023 * Note that its important to have this wrapper function because
1024 * in the very near future we will be checking for MMIOs against
1025 * the LAPIC as well as the general MMIO bus
1027 return kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1030 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1033 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1036 static int emulator_read_emulated(unsigned long addr
,
1039 struct x86_emulate_ctxt
*ctxt
)
1041 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1042 struct kvm_io_device
*mmio_dev
;
1045 if (vcpu
->mmio_read_completed
) {
1046 memcpy(val
, vcpu
->mmio_data
, bytes
);
1047 vcpu
->mmio_read_completed
= 0;
1048 return X86EMUL_CONTINUE
;
1049 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
1050 == X86EMUL_CONTINUE
)
1051 return X86EMUL_CONTINUE
;
1053 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1054 if (gpa
== UNMAPPED_GVA
)
1055 return X86EMUL_PROPAGATE_FAULT
;
1058 * Is this MMIO handled locally?
1060 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1062 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1063 return X86EMUL_CONTINUE
;
1066 vcpu
->mmio_needed
= 1;
1067 vcpu
->mmio_phys_addr
= gpa
;
1068 vcpu
->mmio_size
= bytes
;
1069 vcpu
->mmio_is_write
= 0;
1071 return X86EMUL_UNHANDLEABLE
;
1074 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1075 const void *val
, int bytes
)
1080 if (((gpa
+ bytes
- 1) >> PAGE_SHIFT
) != (gpa
>> PAGE_SHIFT
))
1082 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1085 mark_page_dirty(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1086 virt
= kmap_atomic(page
, KM_USER0
);
1087 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1088 memcpy(virt
+ offset_in_page(gpa
), val
, bytes
);
1089 kunmap_atomic(virt
, KM_USER0
);
1093 static int emulator_write_emulated_onepage(unsigned long addr
,
1096 struct x86_emulate_ctxt
*ctxt
)
1098 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1099 struct kvm_io_device
*mmio_dev
;
1100 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1102 if (gpa
== UNMAPPED_GVA
) {
1103 kvm_arch_ops
->inject_page_fault(vcpu
, addr
, 2);
1104 return X86EMUL_PROPAGATE_FAULT
;
1107 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1108 return X86EMUL_CONTINUE
;
1111 * Is this MMIO handled locally?
1113 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1115 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1116 return X86EMUL_CONTINUE
;
1119 vcpu
->mmio_needed
= 1;
1120 vcpu
->mmio_phys_addr
= gpa
;
1121 vcpu
->mmio_size
= bytes
;
1122 vcpu
->mmio_is_write
= 1;
1123 memcpy(vcpu
->mmio_data
, val
, bytes
);
1125 return X86EMUL_CONTINUE
;
1128 static int emulator_write_emulated(unsigned long addr
,
1131 struct x86_emulate_ctxt
*ctxt
)
1133 /* Crossing a page boundary? */
1134 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1137 now
= -addr
& ~PAGE_MASK
;
1138 rc
= emulator_write_emulated_onepage(addr
, val
, now
, ctxt
);
1139 if (rc
!= X86EMUL_CONTINUE
)
1145 return emulator_write_emulated_onepage(addr
, val
, bytes
, ctxt
);
1148 static int emulator_cmpxchg_emulated(unsigned long addr
,
1152 struct x86_emulate_ctxt
*ctxt
)
1154 static int reported
;
1158 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1160 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
1163 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1165 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
1168 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1170 return X86EMUL_CONTINUE
;
1173 int emulate_clts(struct kvm_vcpu
*vcpu
)
1177 cr0
= vcpu
->cr0
& ~X86_CR0_TS
;
1178 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
1179 return X86EMUL_CONTINUE
;
1182 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1184 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1188 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
1189 return X86EMUL_CONTINUE
;
1191 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
1193 return X86EMUL_UNHANDLEABLE
;
1197 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1199 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1202 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1204 /* FIXME: better handling */
1205 return X86EMUL_UNHANDLEABLE
;
1207 return X86EMUL_CONTINUE
;
1210 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
1212 static int reported
;
1214 unsigned long rip
= ctxt
->vcpu
->rip
;
1215 unsigned long rip_linear
;
1217 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
1222 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
1224 printk(KERN_ERR
"emulation failed but !mmio_needed?"
1225 " rip %lx %02x %02x %02x %02x\n",
1226 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1230 struct x86_emulate_ops emulate_ops
= {
1231 .read_std
= emulator_read_std
,
1232 .write_std
= emulator_write_std
,
1233 .read_emulated
= emulator_read_emulated
,
1234 .write_emulated
= emulator_write_emulated
,
1235 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1238 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1239 struct kvm_run
*run
,
1243 struct x86_emulate_ctxt emulate_ctxt
;
1247 vcpu
->mmio_fault_cr2
= cr2
;
1248 kvm_arch_ops
->cache_regs(vcpu
);
1250 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1252 emulate_ctxt
.vcpu
= vcpu
;
1253 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1254 emulate_ctxt
.cr2
= cr2
;
1255 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1256 ? X86EMUL_MODE_REAL
: cs_l
1257 ? X86EMUL_MODE_PROT64
: cs_db
1258 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1260 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1261 emulate_ctxt
.cs_base
= 0;
1262 emulate_ctxt
.ds_base
= 0;
1263 emulate_ctxt
.es_base
= 0;
1264 emulate_ctxt
.ss_base
= 0;
1266 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1267 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1268 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1269 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1272 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1273 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1275 vcpu
->mmio_is_write
= 0;
1276 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1278 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1279 run
->exit_reason
= KVM_EXIT_MMIO
;
1280 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1281 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1282 run
->mmio
.len
= vcpu
->mmio_size
;
1283 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1287 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1288 return EMULATE_DONE
;
1289 if (!vcpu
->mmio_needed
) {
1290 report_emulation_failure(&emulate_ctxt
);
1291 return EMULATE_FAIL
;
1293 return EMULATE_DO_MMIO
;
1296 kvm_arch_ops
->decache_regs(vcpu
);
1297 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1299 if (vcpu
->mmio_is_write
) {
1300 vcpu
->mmio_needed
= 0;
1301 return EMULATE_DO_MMIO
;
1304 return EMULATE_DONE
;
1306 EXPORT_SYMBOL_GPL(emulate_instruction
);
1308 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
1310 if (vcpu
->irq_summary
)
1313 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
1314 ++vcpu
->stat
.halt_exits
;
1317 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
1319 int kvm_hypercall(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1321 unsigned long nr
, a0
, a1
, a2
, a3
, a4
, a5
, ret
;
1323 kvm_arch_ops
->cache_regs(vcpu
);
1325 #ifdef CONFIG_X86_64
1326 if (is_long_mode(vcpu
)) {
1327 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1328 a0
= vcpu
->regs
[VCPU_REGS_RDI
];
1329 a1
= vcpu
->regs
[VCPU_REGS_RSI
];
1330 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1331 a3
= vcpu
->regs
[VCPU_REGS_RCX
];
1332 a4
= vcpu
->regs
[VCPU_REGS_R8
];
1333 a5
= vcpu
->regs
[VCPU_REGS_R9
];
1337 nr
= vcpu
->regs
[VCPU_REGS_RBX
] & -1u;
1338 a0
= vcpu
->regs
[VCPU_REGS_RAX
] & -1u;
1339 a1
= vcpu
->regs
[VCPU_REGS_RCX
] & -1u;
1340 a2
= vcpu
->regs
[VCPU_REGS_RDX
] & -1u;
1341 a3
= vcpu
->regs
[VCPU_REGS_RSI
] & -1u;
1342 a4
= vcpu
->regs
[VCPU_REGS_RDI
] & -1u;
1343 a5
= vcpu
->regs
[VCPU_REGS_RBP
] & -1u;
1347 run
->hypercall
.args
[0] = a0
;
1348 run
->hypercall
.args
[1] = a1
;
1349 run
->hypercall
.args
[2] = a2
;
1350 run
->hypercall
.args
[3] = a3
;
1351 run
->hypercall
.args
[4] = a4
;
1352 run
->hypercall
.args
[5] = a5
;
1353 run
->hypercall
.ret
= ret
;
1354 run
->hypercall
.longmode
= is_long_mode(vcpu
);
1355 kvm_arch_ops
->decache_regs(vcpu
);
1358 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1359 kvm_arch_ops
->decache_regs(vcpu
);
1362 EXPORT_SYMBOL_GPL(kvm_hypercall
);
1364 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1366 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1369 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1371 struct descriptor_table dt
= { limit
, base
};
1373 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1376 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1378 struct descriptor_table dt
= { limit
, base
};
1380 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1383 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1384 unsigned long *rflags
)
1387 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1390 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1392 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
1403 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1408 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1409 unsigned long *rflags
)
1413 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1414 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1423 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1426 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1431 * Register the para guest with the host:
1433 static int vcpu_register_para(struct kvm_vcpu
*vcpu
, gpa_t para_state_gpa
)
1435 struct kvm_vcpu_para_state
*para_state
;
1436 hpa_t para_state_hpa
, hypercall_hpa
;
1437 struct page
*para_state_page
;
1438 unsigned char *hypercall
;
1439 gpa_t hypercall_gpa
;
1441 printk(KERN_DEBUG
"kvm: guest trying to enter paravirtual mode\n");
1442 printk(KERN_DEBUG
".... para_state_gpa: %08Lx\n", para_state_gpa
);
1445 * Needs to be page aligned:
1447 if (para_state_gpa
!= PAGE_ALIGN(para_state_gpa
))
1450 para_state_hpa
= gpa_to_hpa(vcpu
, para_state_gpa
);
1451 printk(KERN_DEBUG
".... para_state_hpa: %08Lx\n", para_state_hpa
);
1452 if (is_error_hpa(para_state_hpa
))
1455 mark_page_dirty(vcpu
->kvm
, para_state_gpa
>> PAGE_SHIFT
);
1456 para_state_page
= pfn_to_page(para_state_hpa
>> PAGE_SHIFT
);
1457 para_state
= kmap(para_state_page
);
1459 printk(KERN_DEBUG
".... guest version: %d\n", para_state
->guest_version
);
1460 printk(KERN_DEBUG
".... size: %d\n", para_state
->size
);
1462 para_state
->host_version
= KVM_PARA_API_VERSION
;
1464 * We cannot support guests that try to register themselves
1465 * with a newer API version than the host supports:
1467 if (para_state
->guest_version
> KVM_PARA_API_VERSION
) {
1468 para_state
->ret
= -KVM_EINVAL
;
1469 goto err_kunmap_skip
;
1472 hypercall_gpa
= para_state
->hypercall_gpa
;
1473 hypercall_hpa
= gpa_to_hpa(vcpu
, hypercall_gpa
);
1474 printk(KERN_DEBUG
".... hypercall_hpa: %08Lx\n", hypercall_hpa
);
1475 if (is_error_hpa(hypercall_hpa
)) {
1476 para_state
->ret
= -KVM_EINVAL
;
1477 goto err_kunmap_skip
;
1480 printk(KERN_DEBUG
"kvm: para guest successfully registered.\n");
1481 vcpu
->para_state_page
= para_state_page
;
1482 vcpu
->para_state_gpa
= para_state_gpa
;
1483 vcpu
->hypercall_gpa
= hypercall_gpa
;
1485 mark_page_dirty(vcpu
->kvm
, hypercall_gpa
>> PAGE_SHIFT
);
1486 hypercall
= kmap_atomic(pfn_to_page(hypercall_hpa
>> PAGE_SHIFT
),
1487 KM_USER1
) + (hypercall_hpa
& ~PAGE_MASK
);
1488 kvm_arch_ops
->patch_hypercall(vcpu
, hypercall
);
1489 kunmap_atomic(hypercall
, KM_USER1
);
1491 para_state
->ret
= 0;
1493 kunmap(para_state_page
);
1499 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1504 case 0xc0010010: /* SYSCFG */
1505 case 0xc0010015: /* HWCR */
1506 case MSR_IA32_PLATFORM_ID
:
1507 case MSR_IA32_P5_MC_ADDR
:
1508 case MSR_IA32_P5_MC_TYPE
:
1509 case MSR_IA32_MC0_CTL
:
1510 case MSR_IA32_MCG_STATUS
:
1511 case MSR_IA32_MCG_CAP
:
1512 case MSR_IA32_MC0_MISC
:
1513 case MSR_IA32_MC0_MISC
+4:
1514 case MSR_IA32_MC0_MISC
+8:
1515 case MSR_IA32_MC0_MISC
+12:
1516 case MSR_IA32_MC0_MISC
+16:
1517 case MSR_IA32_UCODE_REV
:
1518 case MSR_IA32_PERF_STATUS
:
1519 case MSR_IA32_EBL_CR_POWERON
:
1520 /* MTRR registers */
1522 case 0x200 ... 0x2ff:
1525 case 0xcd: /* fsb frequency */
1528 case MSR_IA32_APICBASE
:
1529 data
= vcpu
->apic_base
;
1531 case MSR_IA32_MISC_ENABLE
:
1532 data
= vcpu
->ia32_misc_enable_msr
;
1534 #ifdef CONFIG_X86_64
1536 data
= vcpu
->shadow_efer
;
1540 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1546 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1549 * Reads an msr value (of 'msr_index') into 'pdata'.
1550 * Returns 0 on success, non-0 otherwise.
1551 * Assumes vcpu_load() was already called.
1553 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1555 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1558 #ifdef CONFIG_X86_64
1560 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1562 if (efer
& EFER_RESERVED_BITS
) {
1563 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1570 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1571 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1576 kvm_arch_ops
->set_efer(vcpu
, efer
);
1579 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1581 vcpu
->shadow_efer
= efer
;
1586 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1589 #ifdef CONFIG_X86_64
1591 set_efer(vcpu
, data
);
1594 case MSR_IA32_MC0_STATUS
:
1595 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1596 __FUNCTION__
, data
);
1598 case MSR_IA32_MCG_STATUS
:
1599 printk(KERN_WARNING
"%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1600 __FUNCTION__
, data
);
1602 case MSR_IA32_UCODE_REV
:
1603 case MSR_IA32_UCODE_WRITE
:
1604 case 0x200 ... 0x2ff: /* MTRRs */
1606 case MSR_IA32_APICBASE
:
1607 vcpu
->apic_base
= data
;
1609 case MSR_IA32_MISC_ENABLE
:
1610 vcpu
->ia32_misc_enable_msr
= data
;
1613 * This is the 'probe whether the host is KVM' logic:
1615 case MSR_KVM_API_MAGIC
:
1616 return vcpu_register_para(vcpu
, data
);
1619 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1624 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1627 * Writes msr value into into the appropriate "register".
1628 * Returns 0 on success, non-0 otherwise.
1629 * Assumes vcpu_load() was already called.
1631 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1633 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1636 void kvm_resched(struct kvm_vcpu
*vcpu
)
1638 if (!need_resched())
1644 EXPORT_SYMBOL_GPL(kvm_resched
);
1646 void load_msrs(struct vmx_msr_entry
*e
, int n
)
1650 for (i
= 0; i
< n
; ++i
)
1651 wrmsrl(e
[i
].index
, e
[i
].data
);
1653 EXPORT_SYMBOL_GPL(load_msrs
);
1655 void save_msrs(struct vmx_msr_entry
*e
, int n
)
1659 for (i
= 0; i
< n
; ++i
)
1660 rdmsrl(e
[i
].index
, e
[i
].data
);
1662 EXPORT_SYMBOL_GPL(save_msrs
);
1664 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1668 struct kvm_cpuid_entry
*e
, *best
;
1670 kvm_arch_ops
->cache_regs(vcpu
);
1671 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1672 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1673 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1674 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1675 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1677 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1678 e
= &vcpu
->cpuid_entries
[i
];
1679 if (e
->function
== function
) {
1684 * Both basic or both extended?
1686 if (((e
->function
^ function
) & 0x80000000) == 0)
1687 if (!best
|| e
->function
> best
->function
)
1691 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1692 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1693 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1694 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1696 kvm_arch_ops
->decache_regs(vcpu
);
1697 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1699 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1701 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1703 void *p
= vcpu
->pio_data
;
1706 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1708 kvm_arch_ops
->vcpu_put(vcpu
);
1709 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1712 kvm_arch_ops
->vcpu_load(vcpu
);
1713 free_pio_guest_pages(vcpu
);
1716 q
+= vcpu
->pio
.guest_page_offset
;
1717 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1719 memcpy(q
, p
, bytes
);
1721 memcpy(p
, q
, bytes
);
1722 q
-= vcpu
->pio
.guest_page_offset
;
1724 kvm_arch_ops
->vcpu_load(vcpu
);
1725 free_pio_guest_pages(vcpu
);
1729 static int complete_pio(struct kvm_vcpu
*vcpu
)
1731 struct kvm_pio_request
*io
= &vcpu
->pio
;
1735 kvm_arch_ops
->cache_regs(vcpu
);
1739 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1743 r
= pio_copy_data(vcpu
);
1745 kvm_arch_ops
->cache_regs(vcpu
);
1752 delta
*= io
->cur_count
;
1754 * The size of the register should really depend on
1755 * current address size.
1757 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1763 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1765 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1768 kvm_arch_ops
->decache_regs(vcpu
);
1770 io
->count
-= io
->cur_count
;
1774 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1778 static void kernel_pio(struct kvm_io_device
*pio_dev
,
1779 struct kvm_vcpu
*vcpu
,
1782 /* TODO: String I/O for in kernel device */
1785 kvm_iodevice_read(pio_dev
, vcpu
->pio
.port
,
1789 kvm_iodevice_write(pio_dev
, vcpu
->pio
.port
,
1794 static void pio_string_write(struct kvm_io_device
*pio_dev
,
1795 struct kvm_vcpu
*vcpu
)
1797 struct kvm_pio_request
*io
= &vcpu
->pio
;
1798 void *pd
= vcpu
->pio_data
;
1801 for (i
= 0; i
< io
->cur_count
; i
++) {
1802 kvm_iodevice_write(pio_dev
, io
->port
,
1809 int kvm_setup_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1810 int size
, unsigned long count
, int string
, int down
,
1811 gva_t address
, int rep
, unsigned port
)
1813 unsigned now
, in_page
;
1817 struct kvm_io_device
*pio_dev
;
1819 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1820 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1821 vcpu
->run
->io
.size
= size
;
1822 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1823 vcpu
->run
->io
.count
= count
;
1824 vcpu
->run
->io
.port
= port
;
1825 vcpu
->pio
.count
= count
;
1826 vcpu
->pio
.cur_count
= count
;
1827 vcpu
->pio
.size
= size
;
1829 vcpu
->pio
.port
= port
;
1830 vcpu
->pio
.string
= string
;
1831 vcpu
->pio
.down
= down
;
1832 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1833 vcpu
->pio
.rep
= rep
;
1835 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1837 kvm_arch_ops
->cache_regs(vcpu
);
1838 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1839 kvm_arch_ops
->decache_regs(vcpu
);
1841 kernel_pio(pio_dev
, vcpu
, vcpu
->pio_data
);
1849 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1853 now
= min(count
, PAGE_SIZE
/ size
);
1856 in_page
= PAGE_SIZE
- offset_in_page(address
);
1858 in_page
= offset_in_page(address
) + size
;
1859 now
= min(count
, (unsigned long)in_page
/ size
);
1862 * String I/O straddles page boundary. Pin two guest pages
1863 * so that we satisfy atomicity constraints. Do just one
1864 * transaction to avoid complexity.
1871 * String I/O in reverse. Yuck. Kill the guest, fix later.
1873 printk(KERN_ERR
"kvm: guest string pio down\n");
1877 vcpu
->run
->io
.count
= now
;
1878 vcpu
->pio
.cur_count
= now
;
1880 for (i
= 0; i
< nr_pages
; ++i
) {
1881 spin_lock(&vcpu
->kvm
->lock
);
1882 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
1885 vcpu
->pio
.guest_pages
[i
] = page
;
1886 spin_unlock(&vcpu
->kvm
->lock
);
1889 free_pio_guest_pages(vcpu
);
1894 if (!vcpu
->pio
.in
) {
1895 /* string PIO write */
1896 ret
= pio_copy_data(vcpu
);
1897 if (ret
>= 0 && pio_dev
) {
1898 pio_string_write(pio_dev
, vcpu
);
1900 if (vcpu
->pio
.count
== 0)
1904 printk(KERN_ERR
"no string pio read support yet, "
1905 "port %x size %d count %ld\n",
1910 EXPORT_SYMBOL_GPL(kvm_setup_pio
);
1912 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1919 if (vcpu
->sigset_active
)
1920 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
1922 /* re-sync apic's tpr */
1923 vcpu
->cr8
= kvm_run
->cr8
;
1925 if (vcpu
->pio
.cur_count
) {
1926 r
= complete_pio(vcpu
);
1931 if (vcpu
->mmio_needed
) {
1932 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1933 vcpu
->mmio_read_completed
= 1;
1934 vcpu
->mmio_needed
= 0;
1935 r
= emulate_instruction(vcpu
, kvm_run
,
1936 vcpu
->mmio_fault_cr2
, 0);
1937 if (r
== EMULATE_DO_MMIO
) {
1939 * Read-modify-write. Back to userspace.
1946 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
1947 kvm_arch_ops
->cache_regs(vcpu
);
1948 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
1949 kvm_arch_ops
->decache_regs(vcpu
);
1952 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1955 if (vcpu
->sigset_active
)
1956 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1962 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
1963 struct kvm_regs
*regs
)
1967 kvm_arch_ops
->cache_regs(vcpu
);
1969 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
1970 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
1971 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
1972 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
1973 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
1974 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
1975 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
1976 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
1977 #ifdef CONFIG_X86_64
1978 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
1979 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
1980 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
1981 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
1982 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
1983 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
1984 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
1985 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
1988 regs
->rip
= vcpu
->rip
;
1989 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1992 * Don't leak debug flags in case they were set for guest debugging
1994 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
1995 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2002 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
2003 struct kvm_regs
*regs
)
2007 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
2008 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2009 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2010 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2011 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2012 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2013 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2014 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2015 #ifdef CONFIG_X86_64
2016 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
2017 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
2018 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
2019 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
2020 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
2021 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
2022 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
2023 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
2026 vcpu
->rip
= regs
->rip
;
2027 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
2029 kvm_arch_ops
->decache_regs(vcpu
);
2036 static void get_segment(struct kvm_vcpu
*vcpu
,
2037 struct kvm_segment
*var
, int seg
)
2039 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
2042 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2043 struct kvm_sregs
*sregs
)
2045 struct descriptor_table dt
;
2049 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2050 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2051 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2052 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2053 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2054 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2056 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2057 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2059 kvm_arch_ops
->get_idt(vcpu
, &dt
);
2060 sregs
->idt
.limit
= dt
.limit
;
2061 sregs
->idt
.base
= dt
.base
;
2062 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
2063 sregs
->gdt
.limit
= dt
.limit
;
2064 sregs
->gdt
.base
= dt
.base
;
2066 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2067 sregs
->cr0
= vcpu
->cr0
;
2068 sregs
->cr2
= vcpu
->cr2
;
2069 sregs
->cr3
= vcpu
->cr3
;
2070 sregs
->cr4
= vcpu
->cr4
;
2071 sregs
->cr8
= vcpu
->cr8
;
2072 sregs
->efer
= vcpu
->shadow_efer
;
2073 sregs
->apic_base
= vcpu
->apic_base
;
2075 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
2076 sizeof sregs
->interrupt_bitmap
);
2083 static void set_segment(struct kvm_vcpu
*vcpu
,
2084 struct kvm_segment
*var
, int seg
)
2086 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
2089 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2090 struct kvm_sregs
*sregs
)
2092 int mmu_reset_needed
= 0;
2094 struct descriptor_table dt
;
2098 dt
.limit
= sregs
->idt
.limit
;
2099 dt
.base
= sregs
->idt
.base
;
2100 kvm_arch_ops
->set_idt(vcpu
, &dt
);
2101 dt
.limit
= sregs
->gdt
.limit
;
2102 dt
.base
= sregs
->gdt
.base
;
2103 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
2105 vcpu
->cr2
= sregs
->cr2
;
2106 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2107 vcpu
->cr3
= sregs
->cr3
;
2109 vcpu
->cr8
= sregs
->cr8
;
2111 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2112 #ifdef CONFIG_X86_64
2113 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
2115 vcpu
->apic_base
= sregs
->apic_base
;
2117 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2119 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2120 kvm_arch_ops
->set_cr0(vcpu
, sregs
->cr0
);
2122 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2123 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
2124 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2125 load_pdptrs(vcpu
, vcpu
->cr3
);
2127 if (mmu_reset_needed
)
2128 kvm_mmu_reset_context(vcpu
);
2130 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2131 sizeof vcpu
->irq_pending
);
2132 vcpu
->irq_summary
= 0;
2133 for (i
= 0; i
< ARRAY_SIZE(vcpu
->irq_pending
); ++i
)
2134 if (vcpu
->irq_pending
[i
])
2135 __set_bit(i
, &vcpu
->irq_summary
);
2137 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2138 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2139 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2140 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2141 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2142 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2144 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2145 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2153 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2154 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2156 * This list is modified at module load time to reflect the
2157 * capabilities of the host cpu.
2159 static u32 msrs_to_save
[] = {
2160 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
2162 #ifdef CONFIG_X86_64
2163 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
2165 MSR_IA32_TIME_STAMP_COUNTER
,
2168 static unsigned num_msrs_to_save
;
2170 static u32 emulated_msrs
[] = {
2171 MSR_IA32_MISC_ENABLE
,
2174 static __init
void kvm_init_msr_list(void)
2179 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2180 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2183 msrs_to_save
[j
] = msrs_to_save
[i
];
2186 num_msrs_to_save
= j
;
2190 * Adapt set_msr() to msr_io()'s calling convention
2192 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
2194 return kvm_set_msr(vcpu
, index
, *data
);
2198 * Read or write a bunch of msrs. All parameters are kernel addresses.
2200 * @return number of msrs set successfully.
2202 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
2203 struct kvm_msr_entry
*entries
,
2204 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2205 unsigned index
, u64
*data
))
2211 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
2212 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
2221 * Read or write a bunch of msrs. Parameters are user addresses.
2223 * @return number of msrs set successfully.
2225 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
2226 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2227 unsigned index
, u64
*data
),
2230 struct kvm_msrs msrs
;
2231 struct kvm_msr_entry
*entries
;
2236 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
2240 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
2244 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
2245 entries
= vmalloc(size
);
2250 if (copy_from_user(entries
, user_msrs
->entries
, size
))
2253 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
2258 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
2270 * Translate a guest virtual address to a guest physical address.
2272 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2273 struct kvm_translation
*tr
)
2275 unsigned long vaddr
= tr
->linear_address
;
2279 spin_lock(&vcpu
->kvm
->lock
);
2280 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2281 tr
->physical_address
= gpa
;
2282 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2285 spin_unlock(&vcpu
->kvm
->lock
);
2291 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2292 struct kvm_interrupt
*irq
)
2294 if (irq
->irq
< 0 || irq
->irq
>= 256)
2298 set_bit(irq
->irq
, vcpu
->irq_pending
);
2299 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
2306 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2307 struct kvm_debug_guest
*dbg
)
2313 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
2320 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
2321 unsigned long address
,
2324 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
2325 unsigned long pgoff
;
2328 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2330 page
= virt_to_page(vcpu
->run
);
2331 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
2332 page
= virt_to_page(vcpu
->pio_data
);
2334 return NOPAGE_SIGBUS
;
2337 *type
= VM_FAULT_MINOR
;
2342 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
2343 .nopage
= kvm_vcpu_nopage
,
2346 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2348 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
2352 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
2354 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2356 fput(vcpu
->kvm
->filp
);
2360 static struct file_operations kvm_vcpu_fops
= {
2361 .release
= kvm_vcpu_release
,
2362 .unlocked_ioctl
= kvm_vcpu_ioctl
,
2363 .compat_ioctl
= kvm_vcpu_ioctl
,
2364 .mmap
= kvm_vcpu_mmap
,
2368 * Allocates an inode for the vcpu.
2370 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
2373 struct inode
*inode
;
2376 r
= anon_inode_getfd(&fd
, &inode
, &file
,
2377 "kvm-vcpu", &kvm_vcpu_fops
, vcpu
);
2380 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
2385 * Creates some virtual cpus. Good luck creating more than one.
2387 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
2390 struct kvm_vcpu
*vcpu
;
2397 vcpu
= &kvm
->vcpus
[n
];
2400 mutex_lock(&vcpu
->mutex
);
2403 mutex_unlock(&vcpu
->mutex
);
2407 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2411 vcpu
->run
= page_address(page
);
2413 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2417 vcpu
->pio_data
= page_address(page
);
2419 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
2421 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
2424 r
= kvm_arch_ops
->vcpu_create(vcpu
);
2426 goto out_free_vcpus
;
2428 r
= kvm_mmu_create(vcpu
);
2430 goto out_free_vcpus
;
2432 kvm_arch_ops
->vcpu_load(vcpu
);
2433 r
= kvm_mmu_setup(vcpu
);
2435 r
= kvm_arch_ops
->vcpu_setup(vcpu
);
2439 goto out_free_vcpus
;
2441 r
= create_vcpu_fd(vcpu
);
2443 goto out_free_vcpus
;
2445 spin_lock(&kvm_lock
);
2446 if (n
>= kvm
->nvcpus
)
2447 kvm
->nvcpus
= n
+ 1;
2448 spin_unlock(&kvm_lock
);
2453 kvm_free_vcpu(vcpu
);
2455 free_page((unsigned long)vcpu
->run
);
2458 mutex_unlock(&vcpu
->mutex
);
2463 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
2467 struct kvm_cpuid_entry
*e
, *entry
;
2469 rdmsrl(MSR_EFER
, efer
);
2471 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
2472 e
= &vcpu
->cpuid_entries
[i
];
2473 if (e
->function
== 0x80000001) {
2478 if (entry
&& (entry
->edx
& (1 << 20)) && !(efer
& EFER_NX
)) {
2479 entry
->edx
&= ~(1 << 20);
2480 printk(KERN_INFO
"kvm: guest NX capability removed\n");
2484 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
2485 struct kvm_cpuid
*cpuid
,
2486 struct kvm_cpuid_entry __user
*entries
)
2491 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2494 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
2495 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
2497 vcpu
->cpuid_nent
= cpuid
->nent
;
2498 cpuid_fix_nx_cap(vcpu
);
2505 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
2508 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
2509 vcpu
->sigset_active
= 1;
2510 vcpu
->sigset
= *sigset
;
2512 vcpu
->sigset_active
= 0;
2517 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2518 * we have asm/x86/processor.h
2529 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2530 #ifdef CONFIG_X86_64
2531 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2533 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2537 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2539 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2543 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2544 fpu
->fcw
= fxsave
->cwd
;
2545 fpu
->fsw
= fxsave
->swd
;
2546 fpu
->ftwx
= fxsave
->twd
;
2547 fpu
->last_opcode
= fxsave
->fop
;
2548 fpu
->last_ip
= fxsave
->rip
;
2549 fpu
->last_dp
= fxsave
->rdp
;
2550 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2557 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2559 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2563 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2564 fxsave
->cwd
= fpu
->fcw
;
2565 fxsave
->swd
= fpu
->fsw
;
2566 fxsave
->twd
= fpu
->ftwx
;
2567 fxsave
->fop
= fpu
->last_opcode
;
2568 fxsave
->rip
= fpu
->last_ip
;
2569 fxsave
->rdp
= fpu
->last_dp
;
2570 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2577 static long kvm_vcpu_ioctl(struct file
*filp
,
2578 unsigned int ioctl
, unsigned long arg
)
2580 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2581 void __user
*argp
= (void __user
*)arg
;
2589 r
= kvm_vcpu_ioctl_run(vcpu
, vcpu
->run
);
2591 case KVM_GET_REGS
: {
2592 struct kvm_regs kvm_regs
;
2594 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2595 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2599 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2604 case KVM_SET_REGS
: {
2605 struct kvm_regs kvm_regs
;
2608 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2610 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2616 case KVM_GET_SREGS
: {
2617 struct kvm_sregs kvm_sregs
;
2619 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2620 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2624 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2629 case KVM_SET_SREGS
: {
2630 struct kvm_sregs kvm_sregs
;
2633 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2635 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2641 case KVM_TRANSLATE
: {
2642 struct kvm_translation tr
;
2645 if (copy_from_user(&tr
, argp
, sizeof tr
))
2647 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2651 if (copy_to_user(argp
, &tr
, sizeof tr
))
2656 case KVM_INTERRUPT
: {
2657 struct kvm_interrupt irq
;
2660 if (copy_from_user(&irq
, argp
, sizeof irq
))
2662 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2668 case KVM_DEBUG_GUEST
: {
2669 struct kvm_debug_guest dbg
;
2672 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2674 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2681 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
2684 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2686 case KVM_SET_CPUID
: {
2687 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2688 struct kvm_cpuid cpuid
;
2691 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2693 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2698 case KVM_SET_SIGNAL_MASK
: {
2699 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
2700 struct kvm_signal_mask kvm_sigmask
;
2701 sigset_t sigset
, *p
;
2706 if (copy_from_user(&kvm_sigmask
, argp
,
2707 sizeof kvm_sigmask
))
2710 if (kvm_sigmask
.len
!= sizeof sigset
)
2713 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2718 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2724 memset(&fpu
, 0, sizeof fpu
);
2725 r
= kvm_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
2729 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
2738 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
2740 r
= kvm_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
2753 static long kvm_vm_ioctl(struct file
*filp
,
2754 unsigned int ioctl
, unsigned long arg
)
2756 struct kvm
*kvm
= filp
->private_data
;
2757 void __user
*argp
= (void __user
*)arg
;
2761 case KVM_CREATE_VCPU
:
2762 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2766 case KVM_SET_MEMORY_REGION
: {
2767 struct kvm_memory_region kvm_mem
;
2770 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2772 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
2777 case KVM_GET_DIRTY_LOG
: {
2778 struct kvm_dirty_log log
;
2781 if (copy_from_user(&log
, argp
, sizeof log
))
2783 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2788 case KVM_SET_MEMORY_ALIAS
: {
2789 struct kvm_memory_alias alias
;
2792 if (copy_from_user(&alias
, argp
, sizeof alias
))
2794 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
2806 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
2807 unsigned long address
,
2810 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2811 unsigned long pgoff
;
2814 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2815 page
= gfn_to_page(kvm
, pgoff
);
2817 return NOPAGE_SIGBUS
;
2820 *type
= VM_FAULT_MINOR
;
2825 static struct vm_operations_struct kvm_vm_vm_ops
= {
2826 .nopage
= kvm_vm_nopage
,
2829 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2831 vma
->vm_ops
= &kvm_vm_vm_ops
;
2835 static struct file_operations kvm_vm_fops
= {
2836 .release
= kvm_vm_release
,
2837 .unlocked_ioctl
= kvm_vm_ioctl
,
2838 .compat_ioctl
= kvm_vm_ioctl
,
2839 .mmap
= kvm_vm_mmap
,
2842 static int kvm_dev_ioctl_create_vm(void)
2845 struct inode
*inode
;
2849 kvm
= kvm_create_vm();
2851 return PTR_ERR(kvm
);
2852 r
= anon_inode_getfd(&fd
, &inode
, &file
, "kvm-vm", &kvm_vm_fops
, kvm
);
2854 kvm_destroy_vm(kvm
);
2863 static long kvm_dev_ioctl(struct file
*filp
,
2864 unsigned int ioctl
, unsigned long arg
)
2866 void __user
*argp
= (void __user
*)arg
;
2870 case KVM_GET_API_VERSION
:
2874 r
= KVM_API_VERSION
;
2880 r
= kvm_dev_ioctl_create_vm();
2882 case KVM_GET_MSR_INDEX_LIST
: {
2883 struct kvm_msr_list __user
*user_msr_list
= argp
;
2884 struct kvm_msr_list msr_list
;
2888 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
2891 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
2892 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
2895 if (n
< num_msrs_to_save
)
2898 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
2899 num_msrs_to_save
* sizeof(u32
)))
2901 if (copy_to_user(user_msr_list
->indices
2902 + num_msrs_to_save
* sizeof(u32
),
2904 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
2909 case KVM_CHECK_EXTENSION
:
2911 * No extensions defined at present.
2915 case KVM_GET_VCPU_MMAP_SIZE
:
2928 static struct file_operations kvm_chardev_ops
= {
2929 .open
= kvm_dev_open
,
2930 .release
= kvm_dev_release
,
2931 .unlocked_ioctl
= kvm_dev_ioctl
,
2932 .compat_ioctl
= kvm_dev_ioctl
,
2935 static struct miscdevice kvm_dev
= {
2942 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2945 static void decache_vcpus_on_cpu(int cpu
)
2948 struct kvm_vcpu
*vcpu
;
2951 spin_lock(&kvm_lock
);
2952 list_for_each_entry(vm
, &vm_list
, vm_list
)
2953 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2954 vcpu
= &vm
->vcpus
[i
];
2956 * If the vcpu is locked, then it is running on some
2957 * other cpu and therefore it is not cached on the
2960 * If it's not locked, check the last cpu it executed
2963 if (mutex_trylock(&vcpu
->mutex
)) {
2964 if (vcpu
->cpu
== cpu
) {
2965 kvm_arch_ops
->vcpu_decache(vcpu
);
2968 mutex_unlock(&vcpu
->mutex
);
2971 spin_unlock(&kvm_lock
);
2974 static void hardware_enable(void *junk
)
2976 int cpu
= raw_smp_processor_id();
2978 if (cpu_isset(cpu
, cpus_hardware_enabled
))
2980 cpu_set(cpu
, cpus_hardware_enabled
);
2981 kvm_arch_ops
->hardware_enable(NULL
);
2984 static void hardware_disable(void *junk
)
2986 int cpu
= raw_smp_processor_id();
2988 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
2990 cpu_clear(cpu
, cpus_hardware_enabled
);
2991 decache_vcpus_on_cpu(cpu
);
2992 kvm_arch_ops
->hardware_disable(NULL
);
2995 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
3002 case CPU_DYING_FROZEN
:
3003 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3005 hardware_disable(NULL
);
3007 case CPU_UP_CANCELED
:
3008 case CPU_UP_CANCELED_FROZEN
:
3009 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3011 smp_call_function_single(cpu
, hardware_disable
, NULL
, 0, 1);
3014 case CPU_ONLINE_FROZEN
:
3015 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
3017 smp_call_function_single(cpu
, hardware_enable
, NULL
, 0, 1);
3023 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
3026 if (val
== SYS_RESTART
) {
3028 * Some (well, at least mine) BIOSes hang on reboot if
3031 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
3032 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3037 static struct notifier_block kvm_reboot_notifier
= {
3038 .notifier_call
= kvm_reboot
,
3042 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
3044 memset(bus
, 0, sizeof(*bus
));
3047 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
3051 for (i
= 0; i
< bus
->dev_count
; i
++) {
3052 struct kvm_io_device
*pos
= bus
->devs
[i
];
3054 kvm_iodevice_destructor(pos
);
3058 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
3062 for (i
= 0; i
< bus
->dev_count
; i
++) {
3063 struct kvm_io_device
*pos
= bus
->devs
[i
];
3065 if (pos
->in_range(pos
, addr
))
3072 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
3074 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
3076 bus
->devs
[bus
->dev_count
++] = dev
;
3079 static struct notifier_block kvm_cpu_notifier
= {
3080 .notifier_call
= kvm_cpu_hotplug
,
3081 .priority
= 20, /* must be > scheduler priority */
3084 static u64
stat_get(void *_offset
)
3086 unsigned offset
= (long)_offset
;
3089 struct kvm_vcpu
*vcpu
;
3092 spin_lock(&kvm_lock
);
3093 list_for_each_entry(kvm
, &vm_list
, vm_list
)
3094 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3095 vcpu
= &kvm
->vcpus
[i
];
3096 total
+= *(u32
*)((void *)vcpu
+ offset
);
3098 spin_unlock(&kvm_lock
);
3102 static void stat_set(void *offset
, u64 val
)
3106 DEFINE_SIMPLE_ATTRIBUTE(stat_fops
, stat_get
, stat_set
, "%llu\n");
3108 static __init
void kvm_init_debug(void)
3110 struct kvm_stats_debugfs_item
*p
;
3112 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
3113 for (p
= debugfs_entries
; p
->name
; ++p
)
3114 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
3115 (void *)(long)p
->offset
,
3119 static void kvm_exit_debug(void)
3121 struct kvm_stats_debugfs_item
*p
;
3123 for (p
= debugfs_entries
; p
->name
; ++p
)
3124 debugfs_remove(p
->dentry
);
3125 debugfs_remove(debugfs_dir
);
3128 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
3130 hardware_disable(NULL
);
3134 static int kvm_resume(struct sys_device
*dev
)
3136 hardware_enable(NULL
);
3140 static struct sysdev_class kvm_sysdev_class
= {
3141 set_kset_name("kvm"),
3142 .suspend
= kvm_suspend
,
3143 .resume
= kvm_resume
,
3146 static struct sys_device kvm_sysdev
= {
3148 .cls
= &kvm_sysdev_class
,
3151 hpa_t bad_page_address
;
3153 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
3158 printk(KERN_ERR
"kvm: already loaded the other module\n");
3162 if (!ops
->cpu_has_kvm_support()) {
3163 printk(KERN_ERR
"kvm: no hardware support\n");
3166 if (ops
->disabled_by_bios()) {
3167 printk(KERN_ERR
"kvm: disabled by bios\n");
3173 r
= kvm_arch_ops
->hardware_setup();
3177 on_each_cpu(hardware_enable
, NULL
, 0, 1);
3178 r
= register_cpu_notifier(&kvm_cpu_notifier
);
3181 register_reboot_notifier(&kvm_reboot_notifier
);
3183 r
= sysdev_class_register(&kvm_sysdev_class
);
3187 r
= sysdev_register(&kvm_sysdev
);
3191 kvm_chardev_ops
.owner
= module
;
3193 r
= misc_register(&kvm_dev
);
3195 printk (KERN_ERR
"kvm: misc device register failed\n");
3202 sysdev_unregister(&kvm_sysdev
);
3204 sysdev_class_unregister(&kvm_sysdev_class
);
3206 unregister_reboot_notifier(&kvm_reboot_notifier
);
3207 unregister_cpu_notifier(&kvm_cpu_notifier
);
3209 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3210 kvm_arch_ops
->hardware_unsetup();
3212 kvm_arch_ops
= NULL
;
3216 void kvm_exit_arch(void)
3218 misc_deregister(&kvm_dev
);
3219 sysdev_unregister(&kvm_sysdev
);
3220 sysdev_class_unregister(&kvm_sysdev_class
);
3221 unregister_reboot_notifier(&kvm_reboot_notifier
);
3222 unregister_cpu_notifier(&kvm_cpu_notifier
);
3223 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3224 kvm_arch_ops
->hardware_unsetup();
3225 kvm_arch_ops
= NULL
;
3228 static __init
int kvm_init(void)
3230 static struct page
*bad_page
;
3233 r
= kvm_mmu_module_init();
3239 kvm_init_msr_list();
3241 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
3246 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
3247 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
3253 kvm_mmu_module_exit();
3258 static __exit
void kvm_exit(void)
3261 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
3262 kvm_mmu_module_exit();
3265 module_init(kvm_init
)
3266 module_exit(kvm_exit
)
3268 EXPORT_SYMBOL_GPL(kvm_init_arch
);
3269 EXPORT_SYMBOL_GPL(kvm_exit_arch
);