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 __read_mostly
struct preempt_ops kvm_preempt_ops
;
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_RESERVED_BITS \
88 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
89 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
90 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
91 #define CR4_RESERVED_BITS \
92 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
93 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
94 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
95 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
97 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
98 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
101 // LDT or TSS descriptor in the GDT. 16 bytes.
102 struct segment_descriptor_64
{
103 struct segment_descriptor s
;
110 static long kvm_vcpu_ioctl(struct file
*file
, unsigned int ioctl
,
113 unsigned long segment_base(u16 selector
)
115 struct descriptor_table gdt
;
116 struct segment_descriptor
*d
;
117 unsigned long table_base
;
118 typedef unsigned long ul
;
124 asm ("sgdt %0" : "=m"(gdt
));
125 table_base
= gdt
.base
;
127 if (selector
& 4) { /* from ldt */
130 asm ("sldt %0" : "=g"(ldt_selector
));
131 table_base
= segment_base(ldt_selector
);
133 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
134 v
= d
->base_low
| ((ul
)d
->base_mid
<< 16) | ((ul
)d
->base_high
<< 24);
137 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
138 v
|= ((ul
)((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
142 EXPORT_SYMBOL_GPL(segment_base
);
144 static inline int valid_vcpu(int n
)
146 return likely(n
>= 0 && n
< KVM_MAX_VCPUS
);
149 int kvm_read_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
152 unsigned char *host_buf
= dest
;
153 unsigned long req_size
= size
;
161 paddr
= gva_to_hpa(vcpu
, addr
);
163 if (is_error_hpa(paddr
))
166 guest_buf
= (hva_t
)kmap_atomic(
167 pfn_to_page(paddr
>> PAGE_SHIFT
),
169 offset
= addr
& ~PAGE_MASK
;
171 now
= min(size
, PAGE_SIZE
- offset
);
172 memcpy(host_buf
, (void*)guest_buf
, now
);
176 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
178 return req_size
- size
;
180 EXPORT_SYMBOL_GPL(kvm_read_guest
);
182 int kvm_write_guest(struct kvm_vcpu
*vcpu
, gva_t addr
, unsigned long size
,
185 unsigned char *host_buf
= data
;
186 unsigned long req_size
= size
;
195 paddr
= gva_to_hpa(vcpu
, addr
);
197 if (is_error_hpa(paddr
))
200 gfn
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
) >> PAGE_SHIFT
;
201 mark_page_dirty(vcpu
->kvm
, gfn
);
202 guest_buf
= (hva_t
)kmap_atomic(
203 pfn_to_page(paddr
>> PAGE_SHIFT
), KM_USER0
);
204 offset
= addr
& ~PAGE_MASK
;
206 now
= min(size
, PAGE_SIZE
- offset
);
207 memcpy((void*)guest_buf
, host_buf
, now
);
211 kunmap_atomic((void *)(guest_buf
& PAGE_MASK
), KM_USER0
);
213 return req_size
- size
;
215 EXPORT_SYMBOL_GPL(kvm_write_guest
);
217 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
219 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
222 vcpu
->guest_fpu_loaded
= 1;
223 fx_save(vcpu
->host_fx_image
);
224 fx_restore(vcpu
->guest_fx_image
);
226 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
228 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
230 if (!vcpu
->guest_fpu_loaded
)
233 vcpu
->guest_fpu_loaded
= 0;
234 fx_save(vcpu
->guest_fx_image
);
235 fx_restore(vcpu
->host_fx_image
);
237 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
240 * Switches to specified vcpu, until a matching vcpu_put()
242 static void vcpu_load(struct kvm_vcpu
*vcpu
)
246 mutex_lock(&vcpu
->mutex
);
248 preempt_notifier_register(&vcpu
->preempt_notifier
);
249 kvm_arch_ops
->vcpu_load(vcpu
, cpu
);
253 static void vcpu_put(struct kvm_vcpu
*vcpu
)
256 kvm_arch_ops
->vcpu_put(vcpu
);
257 preempt_notifier_unregister(&vcpu
->preempt_notifier
);
259 mutex_unlock(&vcpu
->mutex
);
262 static void ack_flush(void *_completed
)
264 atomic_t
*completed
= _completed
;
266 atomic_inc(completed
);
269 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
273 struct kvm_vcpu
*vcpu
;
276 atomic_set(&completed
, 0);
279 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
280 vcpu
= kvm
->vcpus
[i
];
283 if (test_and_set_bit(KVM_TLB_FLUSH
, &vcpu
->requests
))
286 if (cpu
!= -1 && cpu
!= raw_smp_processor_id())
287 if (!cpu_isset(cpu
, cpus
)) {
294 * We really want smp_call_function_mask() here. But that's not
295 * available, so ipi all cpus in parallel and wait for them
298 for (cpu
= first_cpu(cpus
); cpu
!= NR_CPUS
; cpu
= next_cpu(cpu
, cpus
))
299 smp_call_function_single(cpu
, ack_flush
, &completed
, 1, 0);
300 while (atomic_read(&completed
) != needed
) {
306 int kvm_vcpu_init(struct kvm_vcpu
*vcpu
, struct kvm
*kvm
, unsigned id
)
311 mutex_init(&vcpu
->mutex
);
313 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
317 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
322 vcpu
->run
= page_address(page
);
324 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
329 vcpu
->pio_data
= page_address(page
);
331 vcpu
->host_fx_image
= (char*)ALIGN((hva_t
)vcpu
->fx_buf
,
333 vcpu
->guest_fx_image
= vcpu
->host_fx_image
+ FX_IMAGE_SIZE
;
335 r
= kvm_mmu_create(vcpu
);
337 goto fail_free_pio_data
;
342 free_page((unsigned long)vcpu
->pio_data
);
344 free_page((unsigned long)vcpu
->run
);
348 EXPORT_SYMBOL_GPL(kvm_vcpu_init
);
350 void kvm_vcpu_uninit(struct kvm_vcpu
*vcpu
)
352 kvm_mmu_destroy(vcpu
);
353 free_page((unsigned long)vcpu
->pio_data
);
354 free_page((unsigned long)vcpu
->run
);
356 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit
);
358 static struct kvm
*kvm_create_vm(void)
360 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
363 return ERR_PTR(-ENOMEM
);
365 kvm_io_bus_init(&kvm
->pio_bus
);
366 mutex_init(&kvm
->lock
);
367 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
368 kvm_io_bus_init(&kvm
->mmio_bus
);
369 spin_lock(&kvm_lock
);
370 list_add(&kvm
->vm_list
, &vm_list
);
371 spin_unlock(&kvm_lock
);
375 static int kvm_dev_open(struct inode
*inode
, struct file
*filp
)
381 * Free any memory in @free but not in @dont.
383 static void kvm_free_physmem_slot(struct kvm_memory_slot
*free
,
384 struct kvm_memory_slot
*dont
)
388 if (!dont
|| free
->phys_mem
!= dont
->phys_mem
)
389 if (free
->phys_mem
) {
390 for (i
= 0; i
< free
->npages
; ++i
)
391 if (free
->phys_mem
[i
])
392 __free_page(free
->phys_mem
[i
]);
393 vfree(free
->phys_mem
);
396 if (!dont
|| free
->dirty_bitmap
!= dont
->dirty_bitmap
)
397 vfree(free
->dirty_bitmap
);
399 free
->phys_mem
= NULL
;
401 free
->dirty_bitmap
= NULL
;
404 static void kvm_free_physmem(struct kvm
*kvm
)
408 for (i
= 0; i
< kvm
->nmemslots
; ++i
)
409 kvm_free_physmem_slot(&kvm
->memslots
[i
], NULL
);
412 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
416 for (i
= 0; i
< ARRAY_SIZE(vcpu
->pio
.guest_pages
); ++i
)
417 if (vcpu
->pio
.guest_pages
[i
]) {
418 __free_page(vcpu
->pio
.guest_pages
[i
]);
419 vcpu
->pio
.guest_pages
[i
] = NULL
;
423 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
426 kvm_mmu_unload(vcpu
);
430 static void kvm_free_vcpus(struct kvm
*kvm
)
435 * Unpin any mmu pages first.
437 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
439 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
440 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
442 kvm_arch_ops
->vcpu_free(kvm
->vcpus
[i
]);
443 kvm
->vcpus
[i
] = NULL
;
449 static int kvm_dev_release(struct inode
*inode
, struct file
*filp
)
454 static void kvm_destroy_vm(struct kvm
*kvm
)
456 spin_lock(&kvm_lock
);
457 list_del(&kvm
->vm_list
);
458 spin_unlock(&kvm_lock
);
459 kvm_io_bus_destroy(&kvm
->pio_bus
);
460 kvm_io_bus_destroy(&kvm
->mmio_bus
);
462 kvm_free_physmem(kvm
);
466 static int kvm_vm_release(struct inode
*inode
, struct file
*filp
)
468 struct kvm
*kvm
= filp
->private_data
;
474 static void inject_gp(struct kvm_vcpu
*vcpu
)
476 kvm_arch_ops
->inject_gp(vcpu
, 0);
480 * Load the pae pdptrs. Return true is they are all valid.
482 static int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
484 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
485 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
490 u64 pdpte
[ARRAY_SIZE(vcpu
->pdptrs
)];
492 mutex_lock(&vcpu
->kvm
->lock
);
493 page
= gfn_to_page(vcpu
->kvm
, pdpt_gfn
);
499 pdpt
= kmap_atomic(page
, KM_USER0
);
500 memcpy(pdpte
, pdpt
+offset
, sizeof(pdpte
));
501 kunmap_atomic(pdpt
, KM_USER0
);
503 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
504 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
511 memcpy(vcpu
->pdptrs
, pdpte
, sizeof(vcpu
->pdptrs
));
513 mutex_unlock(&vcpu
->kvm
->lock
);
518 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
520 if (cr0
& CR0_RESERVED_BITS
) {
521 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
527 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
528 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
533 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
534 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
535 "and a clear PE flag\n");
540 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
542 if ((vcpu
->shadow_efer
& EFER_LME
)) {
546 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
547 "in long mode while PAE is disabled\n");
551 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
553 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
554 "in long mode while CS.L == 1\n");
561 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
562 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
570 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
573 mutex_lock(&vcpu
->kvm
->lock
);
574 kvm_mmu_reset_context(vcpu
);
575 mutex_unlock(&vcpu
->kvm
->lock
);
578 EXPORT_SYMBOL_GPL(set_cr0
);
580 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
582 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
584 EXPORT_SYMBOL_GPL(lmsw
);
586 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
588 if (cr4
& CR4_RESERVED_BITS
) {
589 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
594 if (is_long_mode(vcpu
)) {
595 if (!(cr4
& X86_CR4_PAE
)) {
596 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
601 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
602 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
603 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
608 if (cr4
& X86_CR4_VMXE
) {
609 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
613 kvm_arch_ops
->set_cr4(vcpu
, cr4
);
614 mutex_lock(&vcpu
->kvm
->lock
);
615 kvm_mmu_reset_context(vcpu
);
616 mutex_unlock(&vcpu
->kvm
->lock
);
618 EXPORT_SYMBOL_GPL(set_cr4
);
620 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
622 if (is_long_mode(vcpu
)) {
623 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
624 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
630 if (cr3
& CR3_PAE_RESERVED_BITS
) {
632 "set_cr3: #GP, reserved bits\n");
636 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
637 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
643 if (cr3
& CR3_NONPAE_RESERVED_BITS
) {
645 "set_cr3: #GP, reserved bits\n");
653 mutex_lock(&vcpu
->kvm
->lock
);
655 * Does the new cr3 value map to physical memory? (Note, we
656 * catch an invalid cr3 even in real-mode, because it would
657 * cause trouble later on when we turn on paging anyway.)
659 * A real CPU would silently accept an invalid cr3 and would
660 * attempt to use it - with largely undefined (and often hard
661 * to debug) behavior on the guest side.
663 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
666 vcpu
->mmu
.new_cr3(vcpu
);
667 mutex_unlock(&vcpu
->kvm
->lock
);
669 EXPORT_SYMBOL_GPL(set_cr3
);
671 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
673 if (cr8
& CR8_RESERVED_BITS
) {
674 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
680 EXPORT_SYMBOL_GPL(set_cr8
);
682 void fx_init(struct kvm_vcpu
*vcpu
)
684 struct __attribute__ ((__packed__
)) fx_image_s
{
690 u64 operand
;// fpu dp
696 /* Initialize guest FPU by resetting ours and saving into guest's */
698 fx_save(vcpu
->host_fx_image
);
700 fx_save(vcpu
->guest_fx_image
);
701 fx_restore(vcpu
->host_fx_image
);
704 fx_image
= (struct fx_image_s
*)vcpu
->guest_fx_image
;
705 fx_image
->mxcsr
= 0x1f80;
706 memset(vcpu
->guest_fx_image
+ sizeof(struct fx_image_s
),
707 0, FX_IMAGE_SIZE
- sizeof(struct fx_image_s
));
709 EXPORT_SYMBOL_GPL(fx_init
);
712 * Allocate some memory and give it an address in the guest physical address
715 * Discontiguous memory is allowed, mostly for framebuffers.
717 static int kvm_vm_ioctl_set_memory_region(struct kvm
*kvm
,
718 struct kvm_memory_region
*mem
)
722 unsigned long npages
;
724 struct kvm_memory_slot
*memslot
;
725 struct kvm_memory_slot old
, new;
726 int memory_config_version
;
729 /* General sanity checks */
730 if (mem
->memory_size
& (PAGE_SIZE
- 1))
732 if (mem
->guest_phys_addr
& (PAGE_SIZE
- 1))
734 if (mem
->slot
>= KVM_MEMORY_SLOTS
)
736 if (mem
->guest_phys_addr
+ mem
->memory_size
< mem
->guest_phys_addr
)
739 memslot
= &kvm
->memslots
[mem
->slot
];
740 base_gfn
= mem
->guest_phys_addr
>> PAGE_SHIFT
;
741 npages
= mem
->memory_size
>> PAGE_SHIFT
;
744 mem
->flags
&= ~KVM_MEM_LOG_DIRTY_PAGES
;
747 mutex_lock(&kvm
->lock
);
749 memory_config_version
= kvm
->memory_config_version
;
750 new = old
= *memslot
;
752 new.base_gfn
= base_gfn
;
754 new.flags
= mem
->flags
;
756 /* Disallow changing a memory slot's size. */
758 if (npages
&& old
.npages
&& npages
!= old
.npages
)
761 /* Check for overlaps */
763 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
764 struct kvm_memory_slot
*s
= &kvm
->memslots
[i
];
768 if (!((base_gfn
+ npages
<= s
->base_gfn
) ||
769 (base_gfn
>= s
->base_gfn
+ s
->npages
)))
773 * Do memory allocations outside lock. memory_config_version will
776 mutex_unlock(&kvm
->lock
);
778 /* Deallocate if slot is being removed */
782 /* Free page dirty bitmap if unneeded */
783 if (!(new.flags
& KVM_MEM_LOG_DIRTY_PAGES
))
784 new.dirty_bitmap
= NULL
;
788 /* Allocate if a slot is being created */
789 if (npages
&& !new.phys_mem
) {
790 new.phys_mem
= vmalloc(npages
* sizeof(struct page
*));
795 memset(new.phys_mem
, 0, npages
* sizeof(struct page
*));
796 for (i
= 0; i
< npages
; ++i
) {
797 new.phys_mem
[i
] = alloc_page(GFP_HIGHUSER
799 if (!new.phys_mem
[i
])
801 set_page_private(new.phys_mem
[i
],0);
805 /* Allocate page dirty bitmap if needed */
806 if ((new.flags
& KVM_MEM_LOG_DIRTY_PAGES
) && !new.dirty_bitmap
) {
807 unsigned dirty_bytes
= ALIGN(npages
, BITS_PER_LONG
) / 8;
809 new.dirty_bitmap
= vmalloc(dirty_bytes
);
810 if (!new.dirty_bitmap
)
812 memset(new.dirty_bitmap
, 0, dirty_bytes
);
815 mutex_lock(&kvm
->lock
);
817 if (memory_config_version
!= kvm
->memory_config_version
) {
818 mutex_unlock(&kvm
->lock
);
819 kvm_free_physmem_slot(&new, &old
);
827 if (mem
->slot
>= kvm
->nmemslots
)
828 kvm
->nmemslots
= mem
->slot
+ 1;
831 ++kvm
->memory_config_version
;
833 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
834 kvm_flush_remote_tlbs(kvm
);
836 mutex_unlock(&kvm
->lock
);
838 kvm_free_physmem_slot(&old
, &new);
842 mutex_unlock(&kvm
->lock
);
844 kvm_free_physmem_slot(&new, &old
);
850 * Get (and clear) the dirty memory log for a memory slot.
852 static int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
853 struct kvm_dirty_log
*log
)
855 struct kvm_memory_slot
*memslot
;
858 unsigned long any
= 0;
860 mutex_lock(&kvm
->lock
);
863 * Prevent changes to guest memory configuration even while the lock
867 mutex_unlock(&kvm
->lock
);
869 if (log
->slot
>= KVM_MEMORY_SLOTS
)
872 memslot
= &kvm
->memslots
[log
->slot
];
874 if (!memslot
->dirty_bitmap
)
877 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
879 for (i
= 0; !any
&& i
< n
/sizeof(long); ++i
)
880 any
= memslot
->dirty_bitmap
[i
];
883 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
886 mutex_lock(&kvm
->lock
);
887 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
888 kvm_flush_remote_tlbs(kvm
);
889 memset(memslot
->dirty_bitmap
, 0, n
);
890 mutex_unlock(&kvm
->lock
);
895 mutex_lock(&kvm
->lock
);
897 mutex_unlock(&kvm
->lock
);
902 * Set a new alias region. Aliases map a portion of physical memory into
903 * another portion. This is useful for memory windows, for example the PC
906 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
907 struct kvm_memory_alias
*alias
)
910 struct kvm_mem_alias
*p
;
913 /* General sanity checks */
914 if (alias
->memory_size
& (PAGE_SIZE
- 1))
916 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
918 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
920 if (alias
->guest_phys_addr
+ alias
->memory_size
921 < alias
->guest_phys_addr
)
923 if (alias
->target_phys_addr
+ alias
->memory_size
924 < alias
->target_phys_addr
)
927 mutex_lock(&kvm
->lock
);
929 p
= &kvm
->aliases
[alias
->slot
];
930 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
931 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
932 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
934 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
935 if (kvm
->aliases
[n
- 1].npages
)
939 kvm_mmu_zap_all(kvm
);
941 mutex_unlock(&kvm
->lock
);
949 static gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
952 struct kvm_mem_alias
*alias
;
954 for (i
= 0; i
< kvm
->naliases
; ++i
) {
955 alias
= &kvm
->aliases
[i
];
956 if (gfn
>= alias
->base_gfn
957 && gfn
< alias
->base_gfn
+ alias
->npages
)
958 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
963 static struct kvm_memory_slot
*__gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
967 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
968 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
970 if (gfn
>= memslot
->base_gfn
971 && gfn
< memslot
->base_gfn
+ memslot
->npages
)
977 struct kvm_memory_slot
*gfn_to_memslot(struct kvm
*kvm
, gfn_t gfn
)
979 gfn
= unalias_gfn(kvm
, gfn
);
980 return __gfn_to_memslot(kvm
, gfn
);
983 struct page
*gfn_to_page(struct kvm
*kvm
, gfn_t gfn
)
985 struct kvm_memory_slot
*slot
;
987 gfn
= unalias_gfn(kvm
, gfn
);
988 slot
= __gfn_to_memslot(kvm
, gfn
);
991 return slot
->phys_mem
[gfn
- slot
->base_gfn
];
993 EXPORT_SYMBOL_GPL(gfn_to_page
);
995 void mark_page_dirty(struct kvm
*kvm
, gfn_t gfn
)
998 struct kvm_memory_slot
*memslot
;
999 unsigned long rel_gfn
;
1001 for (i
= 0; i
< kvm
->nmemslots
; ++i
) {
1002 memslot
= &kvm
->memslots
[i
];
1004 if (gfn
>= memslot
->base_gfn
1005 && gfn
< memslot
->base_gfn
+ memslot
->npages
) {
1007 if (!memslot
->dirty_bitmap
)
1010 rel_gfn
= gfn
- memslot
->base_gfn
;
1013 if (!test_bit(rel_gfn
, memslot
->dirty_bitmap
))
1014 set_bit(rel_gfn
, memslot
->dirty_bitmap
);
1020 static int emulator_read_std(unsigned long addr
,
1023 struct x86_emulate_ctxt
*ctxt
)
1025 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1029 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1030 unsigned offset
= addr
& (PAGE_SIZE
-1);
1031 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1036 if (gpa
== UNMAPPED_GVA
)
1037 return X86EMUL_PROPAGATE_FAULT
;
1038 pfn
= gpa
>> PAGE_SHIFT
;
1039 page
= gfn_to_page(vcpu
->kvm
, pfn
);
1041 return X86EMUL_UNHANDLEABLE
;
1042 page_virt
= kmap_atomic(page
, KM_USER0
);
1044 memcpy(data
, page_virt
+ offset
, tocopy
);
1046 kunmap_atomic(page_virt
, KM_USER0
);
1053 return X86EMUL_CONTINUE
;
1056 static int emulator_write_std(unsigned long addr
,
1059 struct x86_emulate_ctxt
*ctxt
)
1061 printk(KERN_ERR
"emulator_write_std: addr %lx n %d\n",
1063 return X86EMUL_UNHANDLEABLE
;
1066 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1070 * Note that its important to have this wrapper function because
1071 * in the very near future we will be checking for MMIOs against
1072 * the LAPIC as well as the general MMIO bus
1074 return kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1077 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1080 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1083 static int emulator_read_emulated(unsigned long addr
,
1086 struct x86_emulate_ctxt
*ctxt
)
1088 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1089 struct kvm_io_device
*mmio_dev
;
1092 if (vcpu
->mmio_read_completed
) {
1093 memcpy(val
, vcpu
->mmio_data
, bytes
);
1094 vcpu
->mmio_read_completed
= 0;
1095 return X86EMUL_CONTINUE
;
1096 } else if (emulator_read_std(addr
, val
, bytes
, ctxt
)
1097 == X86EMUL_CONTINUE
)
1098 return X86EMUL_CONTINUE
;
1100 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1101 if (gpa
== UNMAPPED_GVA
)
1102 return X86EMUL_PROPAGATE_FAULT
;
1105 * Is this MMIO handled locally?
1107 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1109 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1110 return X86EMUL_CONTINUE
;
1113 vcpu
->mmio_needed
= 1;
1114 vcpu
->mmio_phys_addr
= gpa
;
1115 vcpu
->mmio_size
= bytes
;
1116 vcpu
->mmio_is_write
= 0;
1118 return X86EMUL_UNHANDLEABLE
;
1121 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1122 const void *val
, int bytes
)
1127 if (((gpa
+ bytes
- 1) >> PAGE_SHIFT
) != (gpa
>> PAGE_SHIFT
))
1129 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1132 mark_page_dirty(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1133 virt
= kmap_atomic(page
, KM_USER0
);
1134 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1135 memcpy(virt
+ offset_in_page(gpa
), val
, bytes
);
1136 kunmap_atomic(virt
, KM_USER0
);
1140 static int emulator_write_emulated_onepage(unsigned long addr
,
1143 struct x86_emulate_ctxt
*ctxt
)
1145 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1146 struct kvm_io_device
*mmio_dev
;
1147 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1149 if (gpa
== UNMAPPED_GVA
) {
1150 kvm_arch_ops
->inject_page_fault(vcpu
, addr
, 2);
1151 return X86EMUL_PROPAGATE_FAULT
;
1154 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1155 return X86EMUL_CONTINUE
;
1158 * Is this MMIO handled locally?
1160 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1162 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1163 return X86EMUL_CONTINUE
;
1166 vcpu
->mmio_needed
= 1;
1167 vcpu
->mmio_phys_addr
= gpa
;
1168 vcpu
->mmio_size
= bytes
;
1169 vcpu
->mmio_is_write
= 1;
1170 memcpy(vcpu
->mmio_data
, val
, bytes
);
1172 return X86EMUL_CONTINUE
;
1175 static int emulator_write_emulated(unsigned long addr
,
1178 struct x86_emulate_ctxt
*ctxt
)
1180 /* Crossing a page boundary? */
1181 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1184 now
= -addr
& ~PAGE_MASK
;
1185 rc
= emulator_write_emulated_onepage(addr
, val
, now
, ctxt
);
1186 if (rc
!= X86EMUL_CONTINUE
)
1192 return emulator_write_emulated_onepage(addr
, val
, bytes
, ctxt
);
1195 static int emulator_cmpxchg_emulated(unsigned long addr
,
1199 struct x86_emulate_ctxt
*ctxt
)
1201 static int reported
;
1205 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1207 return emulator_write_emulated(addr
, new, bytes
, ctxt
);
1210 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1212 return kvm_arch_ops
->get_segment_base(vcpu
, seg
);
1215 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1217 return X86EMUL_CONTINUE
;
1220 int emulate_clts(struct kvm_vcpu
*vcpu
)
1224 cr0
= vcpu
->cr0
& ~X86_CR0_TS
;
1225 kvm_arch_ops
->set_cr0(vcpu
, cr0
);
1226 return X86EMUL_CONTINUE
;
1229 int emulator_get_dr(struct x86_emulate_ctxt
* ctxt
, int dr
, unsigned long *dest
)
1231 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1235 *dest
= kvm_arch_ops
->get_dr(vcpu
, dr
);
1236 return X86EMUL_CONTINUE
;
1238 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
1240 return X86EMUL_UNHANDLEABLE
;
1244 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1246 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1249 kvm_arch_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1251 /* FIXME: better handling */
1252 return X86EMUL_UNHANDLEABLE
;
1254 return X86EMUL_CONTINUE
;
1257 static void report_emulation_failure(struct x86_emulate_ctxt
*ctxt
)
1259 static int reported
;
1261 unsigned long rip
= ctxt
->vcpu
->rip
;
1262 unsigned long rip_linear
;
1264 rip_linear
= rip
+ get_segment_base(ctxt
->vcpu
, VCPU_SREG_CS
);
1269 emulator_read_std(rip_linear
, (void *)opcodes
, 4, ctxt
);
1271 printk(KERN_ERR
"emulation failed but !mmio_needed?"
1272 " rip %lx %02x %02x %02x %02x\n",
1273 rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1277 struct x86_emulate_ops emulate_ops
= {
1278 .read_std
= emulator_read_std
,
1279 .write_std
= emulator_write_std
,
1280 .read_emulated
= emulator_read_emulated
,
1281 .write_emulated
= emulator_write_emulated
,
1282 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1285 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1286 struct kvm_run
*run
,
1290 struct x86_emulate_ctxt emulate_ctxt
;
1294 vcpu
->mmio_fault_cr2
= cr2
;
1295 kvm_arch_ops
->cache_regs(vcpu
);
1297 kvm_arch_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1299 emulate_ctxt
.vcpu
= vcpu
;
1300 emulate_ctxt
.eflags
= kvm_arch_ops
->get_rflags(vcpu
);
1301 emulate_ctxt
.cr2
= cr2
;
1302 emulate_ctxt
.mode
= (emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1303 ? X86EMUL_MODE_REAL
: cs_l
1304 ? X86EMUL_MODE_PROT64
: cs_db
1305 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1307 if (emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1308 emulate_ctxt
.cs_base
= 0;
1309 emulate_ctxt
.ds_base
= 0;
1310 emulate_ctxt
.es_base
= 0;
1311 emulate_ctxt
.ss_base
= 0;
1313 emulate_ctxt
.cs_base
= get_segment_base(vcpu
, VCPU_SREG_CS
);
1314 emulate_ctxt
.ds_base
= get_segment_base(vcpu
, VCPU_SREG_DS
);
1315 emulate_ctxt
.es_base
= get_segment_base(vcpu
, VCPU_SREG_ES
);
1316 emulate_ctxt
.ss_base
= get_segment_base(vcpu
, VCPU_SREG_SS
);
1319 emulate_ctxt
.gs_base
= get_segment_base(vcpu
, VCPU_SREG_GS
);
1320 emulate_ctxt
.fs_base
= get_segment_base(vcpu
, VCPU_SREG_FS
);
1322 vcpu
->mmio_is_write
= 0;
1323 r
= x86_emulate_memop(&emulate_ctxt
, &emulate_ops
);
1325 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1326 run
->exit_reason
= KVM_EXIT_MMIO
;
1327 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1328 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1329 run
->mmio
.len
= vcpu
->mmio_size
;
1330 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1334 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1335 return EMULATE_DONE
;
1336 if (!vcpu
->mmio_needed
) {
1337 report_emulation_failure(&emulate_ctxt
);
1338 return EMULATE_FAIL
;
1340 return EMULATE_DO_MMIO
;
1343 kvm_arch_ops
->decache_regs(vcpu
);
1344 kvm_arch_ops
->set_rflags(vcpu
, emulate_ctxt
.eflags
);
1346 if (vcpu
->mmio_is_write
) {
1347 vcpu
->mmio_needed
= 0;
1348 return EMULATE_DO_MMIO
;
1351 return EMULATE_DONE
;
1353 EXPORT_SYMBOL_GPL(emulate_instruction
);
1355 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
1357 if (vcpu
->irq_summary
)
1360 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
1361 ++vcpu
->stat
.halt_exits
;
1364 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
1366 int kvm_hypercall(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1368 unsigned long nr
, a0
, a1
, a2
, a3
, a4
, a5
, ret
;
1370 kvm_arch_ops
->cache_regs(vcpu
);
1372 #ifdef CONFIG_X86_64
1373 if (is_long_mode(vcpu
)) {
1374 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1375 a0
= vcpu
->regs
[VCPU_REGS_RDI
];
1376 a1
= vcpu
->regs
[VCPU_REGS_RSI
];
1377 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1378 a3
= vcpu
->regs
[VCPU_REGS_RCX
];
1379 a4
= vcpu
->regs
[VCPU_REGS_R8
];
1380 a5
= vcpu
->regs
[VCPU_REGS_R9
];
1384 nr
= vcpu
->regs
[VCPU_REGS_RBX
] & -1u;
1385 a0
= vcpu
->regs
[VCPU_REGS_RAX
] & -1u;
1386 a1
= vcpu
->regs
[VCPU_REGS_RCX
] & -1u;
1387 a2
= vcpu
->regs
[VCPU_REGS_RDX
] & -1u;
1388 a3
= vcpu
->regs
[VCPU_REGS_RSI
] & -1u;
1389 a4
= vcpu
->regs
[VCPU_REGS_RDI
] & -1u;
1390 a5
= vcpu
->regs
[VCPU_REGS_RBP
] & -1u;
1394 run
->hypercall
.nr
= nr
;
1395 run
->hypercall
.args
[0] = a0
;
1396 run
->hypercall
.args
[1] = a1
;
1397 run
->hypercall
.args
[2] = a2
;
1398 run
->hypercall
.args
[3] = a3
;
1399 run
->hypercall
.args
[4] = a4
;
1400 run
->hypercall
.args
[5] = a5
;
1401 run
->hypercall
.ret
= ret
;
1402 run
->hypercall
.longmode
= is_long_mode(vcpu
);
1403 kvm_arch_ops
->decache_regs(vcpu
);
1406 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1407 kvm_arch_ops
->decache_regs(vcpu
);
1410 EXPORT_SYMBOL_GPL(kvm_hypercall
);
1412 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1414 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1417 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1419 struct descriptor_table dt
= { limit
, base
};
1421 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
1424 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1426 struct descriptor_table dt
= { limit
, base
};
1428 kvm_arch_ops
->set_idt(vcpu
, &dt
);
1431 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1432 unsigned long *rflags
)
1435 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1438 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1440 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
1451 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1456 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1457 unsigned long *rflags
)
1461 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1462 *rflags
= kvm_arch_ops
->get_rflags(vcpu
);
1471 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1474 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1479 * Register the para guest with the host:
1481 static int vcpu_register_para(struct kvm_vcpu
*vcpu
, gpa_t para_state_gpa
)
1483 struct kvm_vcpu_para_state
*para_state
;
1484 hpa_t para_state_hpa
, hypercall_hpa
;
1485 struct page
*para_state_page
;
1486 unsigned char *hypercall
;
1487 gpa_t hypercall_gpa
;
1489 printk(KERN_DEBUG
"kvm: guest trying to enter paravirtual mode\n");
1490 printk(KERN_DEBUG
".... para_state_gpa: %08Lx\n", para_state_gpa
);
1493 * Needs to be page aligned:
1495 if (para_state_gpa
!= PAGE_ALIGN(para_state_gpa
))
1498 para_state_hpa
= gpa_to_hpa(vcpu
, para_state_gpa
);
1499 printk(KERN_DEBUG
".... para_state_hpa: %08Lx\n", para_state_hpa
);
1500 if (is_error_hpa(para_state_hpa
))
1503 mark_page_dirty(vcpu
->kvm
, para_state_gpa
>> PAGE_SHIFT
);
1504 para_state_page
= pfn_to_page(para_state_hpa
>> PAGE_SHIFT
);
1505 para_state
= kmap(para_state_page
);
1507 printk(KERN_DEBUG
".... guest version: %d\n", para_state
->guest_version
);
1508 printk(KERN_DEBUG
".... size: %d\n", para_state
->size
);
1510 para_state
->host_version
= KVM_PARA_API_VERSION
;
1512 * We cannot support guests that try to register themselves
1513 * with a newer API version than the host supports:
1515 if (para_state
->guest_version
> KVM_PARA_API_VERSION
) {
1516 para_state
->ret
= -KVM_EINVAL
;
1517 goto err_kunmap_skip
;
1520 hypercall_gpa
= para_state
->hypercall_gpa
;
1521 hypercall_hpa
= gpa_to_hpa(vcpu
, hypercall_gpa
);
1522 printk(KERN_DEBUG
".... hypercall_hpa: %08Lx\n", hypercall_hpa
);
1523 if (is_error_hpa(hypercall_hpa
)) {
1524 para_state
->ret
= -KVM_EINVAL
;
1525 goto err_kunmap_skip
;
1528 printk(KERN_DEBUG
"kvm: para guest successfully registered.\n");
1529 vcpu
->para_state_page
= para_state_page
;
1530 vcpu
->para_state_gpa
= para_state_gpa
;
1531 vcpu
->hypercall_gpa
= hypercall_gpa
;
1533 mark_page_dirty(vcpu
->kvm
, hypercall_gpa
>> PAGE_SHIFT
);
1534 hypercall
= kmap_atomic(pfn_to_page(hypercall_hpa
>> PAGE_SHIFT
),
1535 KM_USER1
) + (hypercall_hpa
& ~PAGE_MASK
);
1536 kvm_arch_ops
->patch_hypercall(vcpu
, hypercall
);
1537 kunmap_atomic(hypercall
, KM_USER1
);
1539 para_state
->ret
= 0;
1541 kunmap(para_state_page
);
1547 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1552 case 0xc0010010: /* SYSCFG */
1553 case 0xc0010015: /* HWCR */
1554 case MSR_IA32_PLATFORM_ID
:
1555 case MSR_IA32_P5_MC_ADDR
:
1556 case MSR_IA32_P5_MC_TYPE
:
1557 case MSR_IA32_MC0_CTL
:
1558 case MSR_IA32_MCG_STATUS
:
1559 case MSR_IA32_MCG_CAP
:
1560 case MSR_IA32_MC0_MISC
:
1561 case MSR_IA32_MC0_MISC
+4:
1562 case MSR_IA32_MC0_MISC
+8:
1563 case MSR_IA32_MC0_MISC
+12:
1564 case MSR_IA32_MC0_MISC
+16:
1565 case MSR_IA32_UCODE_REV
:
1566 case MSR_IA32_PERF_STATUS
:
1567 case MSR_IA32_EBL_CR_POWERON
:
1568 /* MTRR registers */
1570 case 0x200 ... 0x2ff:
1573 case 0xcd: /* fsb frequency */
1576 case MSR_IA32_APICBASE
:
1577 data
= vcpu
->apic_base
;
1579 case MSR_IA32_MISC_ENABLE
:
1580 data
= vcpu
->ia32_misc_enable_msr
;
1582 #ifdef CONFIG_X86_64
1584 data
= vcpu
->shadow_efer
;
1588 printk(KERN_ERR
"kvm: unhandled rdmsr: 0x%x\n", msr
);
1594 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1597 * Reads an msr value (of 'msr_index') into 'pdata'.
1598 * Returns 0 on success, non-0 otherwise.
1599 * Assumes vcpu_load() was already called.
1601 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1603 return kvm_arch_ops
->get_msr(vcpu
, msr_index
, pdata
);
1606 #ifdef CONFIG_X86_64
1608 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1610 if (efer
& EFER_RESERVED_BITS
) {
1611 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
1618 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
1619 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
1624 kvm_arch_ops
->set_efer(vcpu
, efer
);
1627 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
1629 vcpu
->shadow_efer
= efer
;
1634 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1637 #ifdef CONFIG_X86_64
1639 set_efer(vcpu
, data
);
1642 case MSR_IA32_MC0_STATUS
:
1643 printk(KERN_WARNING
"%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1644 __FUNCTION__
, data
);
1646 case MSR_IA32_MCG_STATUS
:
1647 printk(KERN_WARNING
"%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1648 __FUNCTION__
, data
);
1650 case MSR_IA32_UCODE_REV
:
1651 case MSR_IA32_UCODE_WRITE
:
1652 case 0x200 ... 0x2ff: /* MTRRs */
1654 case MSR_IA32_APICBASE
:
1655 vcpu
->apic_base
= data
;
1657 case MSR_IA32_MISC_ENABLE
:
1658 vcpu
->ia32_misc_enable_msr
= data
;
1661 * This is the 'probe whether the host is KVM' logic:
1663 case MSR_KVM_API_MAGIC
:
1664 return vcpu_register_para(vcpu
, data
);
1667 printk(KERN_ERR
"kvm: unhandled wrmsr: 0x%x\n", msr
);
1672 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1675 * Writes msr value into into the appropriate "register".
1676 * Returns 0 on success, non-0 otherwise.
1677 * Assumes vcpu_load() was already called.
1679 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1681 return kvm_arch_ops
->set_msr(vcpu
, msr_index
, data
);
1684 void kvm_resched(struct kvm_vcpu
*vcpu
)
1686 if (!need_resched())
1690 EXPORT_SYMBOL_GPL(kvm_resched
);
1692 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1696 struct kvm_cpuid_entry
*e
, *best
;
1698 kvm_arch_ops
->cache_regs(vcpu
);
1699 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1700 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1701 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1702 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1703 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1705 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1706 e
= &vcpu
->cpuid_entries
[i
];
1707 if (e
->function
== function
) {
1712 * Both basic or both extended?
1714 if (((e
->function
^ function
) & 0x80000000) == 0)
1715 if (!best
|| e
->function
> best
->function
)
1719 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1720 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1721 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1722 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1724 kvm_arch_ops
->decache_regs(vcpu
);
1725 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1727 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1729 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1731 void *p
= vcpu
->pio_data
;
1734 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1736 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1739 free_pio_guest_pages(vcpu
);
1742 q
+= vcpu
->pio
.guest_page_offset
;
1743 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1745 memcpy(q
, p
, bytes
);
1747 memcpy(p
, q
, bytes
);
1748 q
-= vcpu
->pio
.guest_page_offset
;
1750 free_pio_guest_pages(vcpu
);
1754 static int complete_pio(struct kvm_vcpu
*vcpu
)
1756 struct kvm_pio_request
*io
= &vcpu
->pio
;
1760 kvm_arch_ops
->cache_regs(vcpu
);
1764 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1768 r
= pio_copy_data(vcpu
);
1770 kvm_arch_ops
->cache_regs(vcpu
);
1777 delta
*= io
->cur_count
;
1779 * The size of the register should really depend on
1780 * current address size.
1782 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1788 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1790 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1793 kvm_arch_ops
->decache_regs(vcpu
);
1795 io
->count
-= io
->cur_count
;
1799 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1803 static void kernel_pio(struct kvm_io_device
*pio_dev
,
1804 struct kvm_vcpu
*vcpu
,
1807 /* TODO: String I/O for in kernel device */
1810 kvm_iodevice_read(pio_dev
, vcpu
->pio
.port
,
1814 kvm_iodevice_write(pio_dev
, vcpu
->pio
.port
,
1819 static void pio_string_write(struct kvm_io_device
*pio_dev
,
1820 struct kvm_vcpu
*vcpu
)
1822 struct kvm_pio_request
*io
= &vcpu
->pio
;
1823 void *pd
= vcpu
->pio_data
;
1826 for (i
= 0; i
< io
->cur_count
; i
++) {
1827 kvm_iodevice_write(pio_dev
, io
->port
,
1834 int kvm_setup_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1835 int size
, unsigned long count
, int string
, int down
,
1836 gva_t address
, int rep
, unsigned port
)
1838 unsigned now
, in_page
;
1842 struct kvm_io_device
*pio_dev
;
1844 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1845 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1846 vcpu
->run
->io
.size
= size
;
1847 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1848 vcpu
->run
->io
.count
= count
;
1849 vcpu
->run
->io
.port
= port
;
1850 vcpu
->pio
.count
= count
;
1851 vcpu
->pio
.cur_count
= count
;
1852 vcpu
->pio
.size
= size
;
1854 vcpu
->pio
.port
= port
;
1855 vcpu
->pio
.string
= string
;
1856 vcpu
->pio
.down
= down
;
1857 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1858 vcpu
->pio
.rep
= rep
;
1860 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1862 kvm_arch_ops
->cache_regs(vcpu
);
1863 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1864 kvm_arch_ops
->decache_regs(vcpu
);
1866 kernel_pio(pio_dev
, vcpu
, vcpu
->pio_data
);
1874 kvm_arch_ops
->skip_emulated_instruction(vcpu
);
1878 now
= min(count
, PAGE_SIZE
/ size
);
1881 in_page
= PAGE_SIZE
- offset_in_page(address
);
1883 in_page
= offset_in_page(address
) + size
;
1884 now
= min(count
, (unsigned long)in_page
/ size
);
1887 * String I/O straddles page boundary. Pin two guest pages
1888 * so that we satisfy atomicity constraints. Do just one
1889 * transaction to avoid complexity.
1896 * String I/O in reverse. Yuck. Kill the guest, fix later.
1898 printk(KERN_ERR
"kvm: guest string pio down\n");
1902 vcpu
->run
->io
.count
= now
;
1903 vcpu
->pio
.cur_count
= now
;
1905 for (i
= 0; i
< nr_pages
; ++i
) {
1906 mutex_lock(&vcpu
->kvm
->lock
);
1907 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
1910 vcpu
->pio
.guest_pages
[i
] = page
;
1911 mutex_unlock(&vcpu
->kvm
->lock
);
1914 free_pio_guest_pages(vcpu
);
1919 if (!vcpu
->pio
.in
) {
1920 /* string PIO write */
1921 ret
= pio_copy_data(vcpu
);
1922 if (ret
>= 0 && pio_dev
) {
1923 pio_string_write(pio_dev
, vcpu
);
1925 if (vcpu
->pio
.count
== 0)
1929 printk(KERN_ERR
"no string pio read support yet, "
1930 "port %x size %d count %ld\n",
1935 EXPORT_SYMBOL_GPL(kvm_setup_pio
);
1937 static int kvm_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1944 if (vcpu
->sigset_active
)
1945 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
1947 /* re-sync apic's tpr */
1948 vcpu
->cr8
= kvm_run
->cr8
;
1950 if (vcpu
->pio
.cur_count
) {
1951 r
= complete_pio(vcpu
);
1956 if (vcpu
->mmio_needed
) {
1957 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
1958 vcpu
->mmio_read_completed
= 1;
1959 vcpu
->mmio_needed
= 0;
1960 r
= emulate_instruction(vcpu
, kvm_run
,
1961 vcpu
->mmio_fault_cr2
, 0);
1962 if (r
== EMULATE_DO_MMIO
) {
1964 * Read-modify-write. Back to userspace.
1971 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
1972 kvm_arch_ops
->cache_regs(vcpu
);
1973 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
1974 kvm_arch_ops
->decache_regs(vcpu
);
1977 r
= kvm_arch_ops
->run(vcpu
, kvm_run
);
1980 if (vcpu
->sigset_active
)
1981 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1987 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
,
1988 struct kvm_regs
*regs
)
1992 kvm_arch_ops
->cache_regs(vcpu
);
1994 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
1995 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
1996 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
1997 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
1998 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
1999 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
2000 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
2001 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
2002 #ifdef CONFIG_X86_64
2003 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
2004 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
2005 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
2006 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
2007 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
2008 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
2009 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
2010 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
2013 regs
->rip
= vcpu
->rip
;
2014 regs
->rflags
= kvm_arch_ops
->get_rflags(vcpu
);
2017 * Don't leak debug flags in case they were set for guest debugging
2019 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2020 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2027 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
,
2028 struct kvm_regs
*regs
)
2032 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
2033 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2034 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2035 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2036 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2037 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2038 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2039 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2040 #ifdef CONFIG_X86_64
2041 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
2042 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
2043 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
2044 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
2045 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
2046 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
2047 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
2048 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
2051 vcpu
->rip
= regs
->rip
;
2052 kvm_arch_ops
->set_rflags(vcpu
, regs
->rflags
);
2054 kvm_arch_ops
->decache_regs(vcpu
);
2061 static void get_segment(struct kvm_vcpu
*vcpu
,
2062 struct kvm_segment
*var
, int seg
)
2064 return kvm_arch_ops
->get_segment(vcpu
, var
, seg
);
2067 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2068 struct kvm_sregs
*sregs
)
2070 struct descriptor_table dt
;
2074 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2075 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2076 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2077 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2078 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2079 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2081 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2082 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2084 kvm_arch_ops
->get_idt(vcpu
, &dt
);
2085 sregs
->idt
.limit
= dt
.limit
;
2086 sregs
->idt
.base
= dt
.base
;
2087 kvm_arch_ops
->get_gdt(vcpu
, &dt
);
2088 sregs
->gdt
.limit
= dt
.limit
;
2089 sregs
->gdt
.base
= dt
.base
;
2091 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2092 sregs
->cr0
= vcpu
->cr0
;
2093 sregs
->cr2
= vcpu
->cr2
;
2094 sregs
->cr3
= vcpu
->cr3
;
2095 sregs
->cr4
= vcpu
->cr4
;
2096 sregs
->cr8
= vcpu
->cr8
;
2097 sregs
->efer
= vcpu
->shadow_efer
;
2098 sregs
->apic_base
= vcpu
->apic_base
;
2100 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
2101 sizeof sregs
->interrupt_bitmap
);
2108 static void set_segment(struct kvm_vcpu
*vcpu
,
2109 struct kvm_segment
*var
, int seg
)
2111 return kvm_arch_ops
->set_segment(vcpu
, var
, seg
);
2114 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2115 struct kvm_sregs
*sregs
)
2117 int mmu_reset_needed
= 0;
2119 struct descriptor_table dt
;
2123 dt
.limit
= sregs
->idt
.limit
;
2124 dt
.base
= sregs
->idt
.base
;
2125 kvm_arch_ops
->set_idt(vcpu
, &dt
);
2126 dt
.limit
= sregs
->gdt
.limit
;
2127 dt
.base
= sregs
->gdt
.base
;
2128 kvm_arch_ops
->set_gdt(vcpu
, &dt
);
2130 vcpu
->cr2
= sregs
->cr2
;
2131 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2132 vcpu
->cr3
= sregs
->cr3
;
2134 vcpu
->cr8
= sregs
->cr8
;
2136 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2137 #ifdef CONFIG_X86_64
2138 kvm_arch_ops
->set_efer(vcpu
, sregs
->efer
);
2140 vcpu
->apic_base
= sregs
->apic_base
;
2142 kvm_arch_ops
->decache_cr4_guest_bits(vcpu
);
2144 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2145 kvm_arch_ops
->set_cr0(vcpu
, sregs
->cr0
);
2147 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2148 kvm_arch_ops
->set_cr4(vcpu
, sregs
->cr4
);
2149 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2150 load_pdptrs(vcpu
, vcpu
->cr3
);
2152 if (mmu_reset_needed
)
2153 kvm_mmu_reset_context(vcpu
);
2155 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2156 sizeof vcpu
->irq_pending
);
2157 vcpu
->irq_summary
= 0;
2158 for (i
= 0; i
< ARRAY_SIZE(vcpu
->irq_pending
); ++i
)
2159 if (vcpu
->irq_pending
[i
])
2160 __set_bit(i
, &vcpu
->irq_summary
);
2162 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2163 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2164 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2165 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2166 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2167 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2169 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2170 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2178 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2179 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2181 * This list is modified at module load time to reflect the
2182 * capabilities of the host cpu.
2184 static u32 msrs_to_save
[] = {
2185 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
2187 #ifdef CONFIG_X86_64
2188 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
2190 MSR_IA32_TIME_STAMP_COUNTER
,
2193 static unsigned num_msrs_to_save
;
2195 static u32 emulated_msrs
[] = {
2196 MSR_IA32_MISC_ENABLE
,
2199 static __init
void kvm_init_msr_list(void)
2204 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2205 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2208 msrs_to_save
[j
] = msrs_to_save
[i
];
2211 num_msrs_to_save
= j
;
2215 * Adapt set_msr() to msr_io()'s calling convention
2217 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
2219 return kvm_set_msr(vcpu
, index
, *data
);
2223 * Read or write a bunch of msrs. All parameters are kernel addresses.
2225 * @return number of msrs set successfully.
2227 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
2228 struct kvm_msr_entry
*entries
,
2229 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2230 unsigned index
, u64
*data
))
2236 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
2237 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
2246 * Read or write a bunch of msrs. Parameters are user addresses.
2248 * @return number of msrs set successfully.
2250 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
2251 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
2252 unsigned index
, u64
*data
),
2255 struct kvm_msrs msrs
;
2256 struct kvm_msr_entry
*entries
;
2261 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
2265 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
2269 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
2270 entries
= vmalloc(size
);
2275 if (copy_from_user(entries
, user_msrs
->entries
, size
))
2278 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
2283 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
2295 * Translate a guest virtual address to a guest physical address.
2297 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2298 struct kvm_translation
*tr
)
2300 unsigned long vaddr
= tr
->linear_address
;
2304 mutex_lock(&vcpu
->kvm
->lock
);
2305 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2306 tr
->physical_address
= gpa
;
2307 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2310 mutex_unlock(&vcpu
->kvm
->lock
);
2316 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2317 struct kvm_interrupt
*irq
)
2319 if (irq
->irq
< 0 || irq
->irq
>= 256)
2323 set_bit(irq
->irq
, vcpu
->irq_pending
);
2324 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
2331 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2332 struct kvm_debug_guest
*dbg
)
2338 r
= kvm_arch_ops
->set_guest_debug(vcpu
, dbg
);
2345 static struct page
*kvm_vcpu_nopage(struct vm_area_struct
*vma
,
2346 unsigned long address
,
2349 struct kvm_vcpu
*vcpu
= vma
->vm_file
->private_data
;
2350 unsigned long pgoff
;
2353 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2355 page
= virt_to_page(vcpu
->run
);
2356 else if (pgoff
== KVM_PIO_PAGE_OFFSET
)
2357 page
= virt_to_page(vcpu
->pio_data
);
2359 return NOPAGE_SIGBUS
;
2362 *type
= VM_FAULT_MINOR
;
2367 static struct vm_operations_struct kvm_vcpu_vm_ops
= {
2368 .nopage
= kvm_vcpu_nopage
,
2371 static int kvm_vcpu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2373 vma
->vm_ops
= &kvm_vcpu_vm_ops
;
2377 static int kvm_vcpu_release(struct inode
*inode
, struct file
*filp
)
2379 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2381 fput(vcpu
->kvm
->filp
);
2385 static struct file_operations kvm_vcpu_fops
= {
2386 .release
= kvm_vcpu_release
,
2387 .unlocked_ioctl
= kvm_vcpu_ioctl
,
2388 .compat_ioctl
= kvm_vcpu_ioctl
,
2389 .mmap
= kvm_vcpu_mmap
,
2393 * Allocates an inode for the vcpu.
2395 static int create_vcpu_fd(struct kvm_vcpu
*vcpu
)
2398 struct inode
*inode
;
2401 r
= anon_inode_getfd(&fd
, &inode
, &file
,
2402 "kvm-vcpu", &kvm_vcpu_fops
, vcpu
);
2405 atomic_inc(&vcpu
->kvm
->filp
->f_count
);
2410 * Creates some virtual cpus. Good luck creating more than one.
2412 static int kvm_vm_ioctl_create_vcpu(struct kvm
*kvm
, int n
)
2415 struct kvm_vcpu
*vcpu
;
2420 vcpu
= kvm_arch_ops
->vcpu_create(kvm
, n
);
2422 return PTR_ERR(vcpu
);
2424 preempt_notifier_init(&vcpu
->preempt_notifier
, &kvm_preempt_ops
);
2427 r
= kvm_mmu_setup(vcpu
);
2432 mutex_lock(&kvm
->lock
);
2433 if (kvm
->vcpus
[n
]) {
2435 mutex_unlock(&kvm
->lock
);
2438 kvm
->vcpus
[n
] = vcpu
;
2439 mutex_unlock(&kvm
->lock
);
2441 /* Now it's all set up, let userspace reach it */
2442 r
= create_vcpu_fd(vcpu
);
2448 mutex_lock(&kvm
->lock
);
2449 kvm
->vcpus
[n
] = NULL
;
2450 mutex_unlock(&kvm
->lock
);
2454 kvm_mmu_unload(vcpu
);
2458 kvm_arch_ops
->vcpu_free(vcpu
);
2462 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
2466 struct kvm_cpuid_entry
*e
, *entry
;
2468 rdmsrl(MSR_EFER
, efer
);
2470 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
2471 e
= &vcpu
->cpuid_entries
[i
];
2472 if (e
->function
== 0x80000001) {
2477 if (entry
&& (entry
->edx
& (1 << 20)) && !(efer
& EFER_NX
)) {
2478 entry
->edx
&= ~(1 << 20);
2479 printk(KERN_INFO
"kvm: guest NX capability removed\n");
2483 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
2484 struct kvm_cpuid
*cpuid
,
2485 struct kvm_cpuid_entry __user
*entries
)
2490 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2493 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
2494 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
2496 vcpu
->cpuid_nent
= cpuid
->nent
;
2497 cpuid_fix_nx_cap(vcpu
);
2504 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu
*vcpu
, sigset_t
*sigset
)
2507 sigdelsetmask(sigset
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
2508 vcpu
->sigset_active
= 1;
2509 vcpu
->sigset
= *sigset
;
2511 vcpu
->sigset_active
= 0;
2516 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2517 * we have asm/x86/processor.h
2528 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2529 #ifdef CONFIG_X86_64
2530 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2532 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2536 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2538 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2542 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2543 fpu
->fcw
= fxsave
->cwd
;
2544 fpu
->fsw
= fxsave
->swd
;
2545 fpu
->ftwx
= fxsave
->twd
;
2546 fpu
->last_opcode
= fxsave
->fop
;
2547 fpu
->last_ip
= fxsave
->rip
;
2548 fpu
->last_dp
= fxsave
->rdp
;
2549 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2556 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2558 struct fxsave
*fxsave
= (struct fxsave
*)vcpu
->guest_fx_image
;
2562 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2563 fxsave
->cwd
= fpu
->fcw
;
2564 fxsave
->swd
= fpu
->fsw
;
2565 fxsave
->twd
= fpu
->ftwx
;
2566 fxsave
->fop
= fpu
->last_opcode
;
2567 fxsave
->rip
= fpu
->last_ip
;
2568 fxsave
->rdp
= fpu
->last_dp
;
2569 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2576 static long kvm_vcpu_ioctl(struct file
*filp
,
2577 unsigned int ioctl
, unsigned long arg
)
2579 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2580 void __user
*argp
= (void __user
*)arg
;
2588 r
= kvm_vcpu_ioctl_run(vcpu
, vcpu
->run
);
2590 case KVM_GET_REGS
: {
2591 struct kvm_regs kvm_regs
;
2593 memset(&kvm_regs
, 0, sizeof kvm_regs
);
2594 r
= kvm_vcpu_ioctl_get_regs(vcpu
, &kvm_regs
);
2598 if (copy_to_user(argp
, &kvm_regs
, sizeof kvm_regs
))
2603 case KVM_SET_REGS
: {
2604 struct kvm_regs kvm_regs
;
2607 if (copy_from_user(&kvm_regs
, argp
, sizeof kvm_regs
))
2609 r
= kvm_vcpu_ioctl_set_regs(vcpu
, &kvm_regs
);
2615 case KVM_GET_SREGS
: {
2616 struct kvm_sregs kvm_sregs
;
2618 memset(&kvm_sregs
, 0, sizeof kvm_sregs
);
2619 r
= kvm_vcpu_ioctl_get_sregs(vcpu
, &kvm_sregs
);
2623 if (copy_to_user(argp
, &kvm_sregs
, sizeof kvm_sregs
))
2628 case KVM_SET_SREGS
: {
2629 struct kvm_sregs kvm_sregs
;
2632 if (copy_from_user(&kvm_sregs
, argp
, sizeof kvm_sregs
))
2634 r
= kvm_vcpu_ioctl_set_sregs(vcpu
, &kvm_sregs
);
2640 case KVM_TRANSLATE
: {
2641 struct kvm_translation tr
;
2644 if (copy_from_user(&tr
, argp
, sizeof tr
))
2646 r
= kvm_vcpu_ioctl_translate(vcpu
, &tr
);
2650 if (copy_to_user(argp
, &tr
, sizeof tr
))
2655 case KVM_INTERRUPT
: {
2656 struct kvm_interrupt irq
;
2659 if (copy_from_user(&irq
, argp
, sizeof irq
))
2661 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2667 case KVM_DEBUG_GUEST
: {
2668 struct kvm_debug_guest dbg
;
2671 if (copy_from_user(&dbg
, argp
, sizeof dbg
))
2673 r
= kvm_vcpu_ioctl_debug_guest(vcpu
, &dbg
);
2680 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
2683 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2685 case KVM_SET_CPUID
: {
2686 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2687 struct kvm_cpuid cpuid
;
2690 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2692 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2697 case KVM_SET_SIGNAL_MASK
: {
2698 struct kvm_signal_mask __user
*sigmask_arg
= argp
;
2699 struct kvm_signal_mask kvm_sigmask
;
2700 sigset_t sigset
, *p
;
2705 if (copy_from_user(&kvm_sigmask
, argp
,
2706 sizeof kvm_sigmask
))
2709 if (kvm_sigmask
.len
!= sizeof sigset
)
2712 if (copy_from_user(&sigset
, sigmask_arg
->sigset
,
2717 r
= kvm_vcpu_ioctl_set_sigmask(vcpu
, &sigset
);
2723 memset(&fpu
, 0, sizeof fpu
);
2724 r
= kvm_vcpu_ioctl_get_fpu(vcpu
, &fpu
);
2728 if (copy_to_user(argp
, &fpu
, sizeof fpu
))
2737 if (copy_from_user(&fpu
, argp
, sizeof fpu
))
2739 r
= kvm_vcpu_ioctl_set_fpu(vcpu
, &fpu
);
2752 static long kvm_vm_ioctl(struct file
*filp
,
2753 unsigned int ioctl
, unsigned long arg
)
2755 struct kvm
*kvm
= filp
->private_data
;
2756 void __user
*argp
= (void __user
*)arg
;
2760 case KVM_CREATE_VCPU
:
2761 r
= kvm_vm_ioctl_create_vcpu(kvm
, arg
);
2765 case KVM_SET_MEMORY_REGION
: {
2766 struct kvm_memory_region kvm_mem
;
2769 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2771 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_mem
);
2776 case KVM_GET_DIRTY_LOG
: {
2777 struct kvm_dirty_log log
;
2780 if (copy_from_user(&log
, argp
, sizeof log
))
2782 r
= kvm_vm_ioctl_get_dirty_log(kvm
, &log
);
2787 case KVM_SET_MEMORY_ALIAS
: {
2788 struct kvm_memory_alias alias
;
2791 if (copy_from_user(&alias
, argp
, sizeof alias
))
2793 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
2805 static struct page
*kvm_vm_nopage(struct vm_area_struct
*vma
,
2806 unsigned long address
,
2809 struct kvm
*kvm
= vma
->vm_file
->private_data
;
2810 unsigned long pgoff
;
2813 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2814 page
= gfn_to_page(kvm
, pgoff
);
2816 return NOPAGE_SIGBUS
;
2819 *type
= VM_FAULT_MINOR
;
2824 static struct vm_operations_struct kvm_vm_vm_ops
= {
2825 .nopage
= kvm_vm_nopage
,
2828 static int kvm_vm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2830 vma
->vm_ops
= &kvm_vm_vm_ops
;
2834 static struct file_operations kvm_vm_fops
= {
2835 .release
= kvm_vm_release
,
2836 .unlocked_ioctl
= kvm_vm_ioctl
,
2837 .compat_ioctl
= kvm_vm_ioctl
,
2838 .mmap
= kvm_vm_mmap
,
2841 static int kvm_dev_ioctl_create_vm(void)
2844 struct inode
*inode
;
2848 kvm
= kvm_create_vm();
2850 return PTR_ERR(kvm
);
2851 r
= anon_inode_getfd(&fd
, &inode
, &file
, "kvm-vm", &kvm_vm_fops
, kvm
);
2853 kvm_destroy_vm(kvm
);
2862 static long kvm_dev_ioctl(struct file
*filp
,
2863 unsigned int ioctl
, unsigned long arg
)
2865 void __user
*argp
= (void __user
*)arg
;
2869 case KVM_GET_API_VERSION
:
2873 r
= KVM_API_VERSION
;
2879 r
= kvm_dev_ioctl_create_vm();
2881 case KVM_GET_MSR_INDEX_LIST
: {
2882 struct kvm_msr_list __user
*user_msr_list
= argp
;
2883 struct kvm_msr_list msr_list
;
2887 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
2890 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
2891 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
2894 if (n
< num_msrs_to_save
)
2897 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
2898 num_msrs_to_save
* sizeof(u32
)))
2900 if (copy_to_user(user_msr_list
->indices
2901 + num_msrs_to_save
* sizeof(u32
),
2903 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
2908 case KVM_CHECK_EXTENSION
:
2910 * No extensions defined at present.
2914 case KVM_GET_VCPU_MMAP_SIZE
:
2927 static struct file_operations kvm_chardev_ops
= {
2928 .open
= kvm_dev_open
,
2929 .release
= kvm_dev_release
,
2930 .unlocked_ioctl
= kvm_dev_ioctl
,
2931 .compat_ioctl
= kvm_dev_ioctl
,
2934 static struct miscdevice kvm_dev
= {
2941 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2944 static void decache_vcpus_on_cpu(int cpu
)
2947 struct kvm_vcpu
*vcpu
;
2950 spin_lock(&kvm_lock
);
2951 list_for_each_entry(vm
, &vm_list
, vm_list
)
2952 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2953 vcpu
= vm
->vcpus
[i
];
2957 * If the vcpu is locked, then it is running on some
2958 * other cpu and therefore it is not cached on the
2961 * If it's not locked, check the last cpu it executed
2964 if (mutex_trylock(&vcpu
->mutex
)) {
2965 if (vcpu
->cpu
== cpu
) {
2966 kvm_arch_ops
->vcpu_decache(vcpu
);
2969 mutex_unlock(&vcpu
->mutex
);
2972 spin_unlock(&kvm_lock
);
2975 static void hardware_enable(void *junk
)
2977 int cpu
= raw_smp_processor_id();
2979 if (cpu_isset(cpu
, cpus_hardware_enabled
))
2981 cpu_set(cpu
, cpus_hardware_enabled
);
2982 kvm_arch_ops
->hardware_enable(NULL
);
2985 static void hardware_disable(void *junk
)
2987 int cpu
= raw_smp_processor_id();
2989 if (!cpu_isset(cpu
, cpus_hardware_enabled
))
2991 cpu_clear(cpu
, cpus_hardware_enabled
);
2992 decache_vcpus_on_cpu(cpu
);
2993 kvm_arch_ops
->hardware_disable(NULL
);
2996 static int kvm_cpu_hotplug(struct notifier_block
*notifier
, unsigned long val
,
3003 case CPU_DYING_FROZEN
:
3004 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3006 hardware_disable(NULL
);
3008 case CPU_UP_CANCELED
:
3009 case CPU_UP_CANCELED_FROZEN
:
3010 printk(KERN_INFO
"kvm: disabling virtualization on CPU%d\n",
3012 smp_call_function_single(cpu
, hardware_disable
, NULL
, 0, 1);
3015 case CPU_ONLINE_FROZEN
:
3016 printk(KERN_INFO
"kvm: enabling virtualization on CPU%d\n",
3018 smp_call_function_single(cpu
, hardware_enable
, NULL
, 0, 1);
3024 static int kvm_reboot(struct notifier_block
*notifier
, unsigned long val
,
3027 if (val
== SYS_RESTART
) {
3029 * Some (well, at least mine) BIOSes hang on reboot if
3032 printk(KERN_INFO
"kvm: exiting hardware virtualization\n");
3033 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3038 static struct notifier_block kvm_reboot_notifier
= {
3039 .notifier_call
= kvm_reboot
,
3043 void kvm_io_bus_init(struct kvm_io_bus
*bus
)
3045 memset(bus
, 0, sizeof(*bus
));
3048 void kvm_io_bus_destroy(struct kvm_io_bus
*bus
)
3052 for (i
= 0; i
< bus
->dev_count
; i
++) {
3053 struct kvm_io_device
*pos
= bus
->devs
[i
];
3055 kvm_iodevice_destructor(pos
);
3059 struct kvm_io_device
*kvm_io_bus_find_dev(struct kvm_io_bus
*bus
, gpa_t addr
)
3063 for (i
= 0; i
< bus
->dev_count
; i
++) {
3064 struct kvm_io_device
*pos
= bus
->devs
[i
];
3066 if (pos
->in_range(pos
, addr
))
3073 void kvm_io_bus_register_dev(struct kvm_io_bus
*bus
, struct kvm_io_device
*dev
)
3075 BUG_ON(bus
->dev_count
> (NR_IOBUS_DEVS
-1));
3077 bus
->devs
[bus
->dev_count
++] = dev
;
3080 static struct notifier_block kvm_cpu_notifier
= {
3081 .notifier_call
= kvm_cpu_hotplug
,
3082 .priority
= 20, /* must be > scheduler priority */
3085 static u64
stat_get(void *_offset
)
3087 unsigned offset
= (long)_offset
;
3090 struct kvm_vcpu
*vcpu
;
3093 spin_lock(&kvm_lock
);
3094 list_for_each_entry(kvm
, &vm_list
, vm_list
)
3095 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3096 vcpu
= kvm
->vcpus
[i
];
3098 total
+= *(u32
*)((void *)vcpu
+ offset
);
3100 spin_unlock(&kvm_lock
);
3104 static void stat_set(void *offset
, u64 val
)
3108 DEFINE_SIMPLE_ATTRIBUTE(stat_fops
, stat_get
, stat_set
, "%llu\n");
3110 static __init
void kvm_init_debug(void)
3112 struct kvm_stats_debugfs_item
*p
;
3114 debugfs_dir
= debugfs_create_dir("kvm", NULL
);
3115 for (p
= debugfs_entries
; p
->name
; ++p
)
3116 p
->dentry
= debugfs_create_file(p
->name
, 0444, debugfs_dir
,
3117 (void *)(long)p
->offset
,
3121 static void kvm_exit_debug(void)
3123 struct kvm_stats_debugfs_item
*p
;
3125 for (p
= debugfs_entries
; p
->name
; ++p
)
3126 debugfs_remove(p
->dentry
);
3127 debugfs_remove(debugfs_dir
);
3130 static int kvm_suspend(struct sys_device
*dev
, pm_message_t state
)
3132 hardware_disable(NULL
);
3136 static int kvm_resume(struct sys_device
*dev
)
3138 hardware_enable(NULL
);
3142 static struct sysdev_class kvm_sysdev_class
= {
3143 set_kset_name("kvm"),
3144 .suspend
= kvm_suspend
,
3145 .resume
= kvm_resume
,
3148 static struct sys_device kvm_sysdev
= {
3150 .cls
= &kvm_sysdev_class
,
3153 hpa_t bad_page_address
;
3156 struct kvm_vcpu
*preempt_notifier_to_vcpu(struct preempt_notifier
*pn
)
3158 return container_of(pn
, struct kvm_vcpu
, preempt_notifier
);
3161 static void kvm_sched_in(struct preempt_notifier
*pn
, int cpu
)
3163 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
3165 kvm_arch_ops
->vcpu_load(vcpu
, cpu
);
3168 static void kvm_sched_out(struct preempt_notifier
*pn
,
3169 struct task_struct
*next
)
3171 struct kvm_vcpu
*vcpu
= preempt_notifier_to_vcpu(pn
);
3173 kvm_arch_ops
->vcpu_put(vcpu
);
3176 int kvm_init_arch(struct kvm_arch_ops
*ops
, struct module
*module
)
3181 printk(KERN_ERR
"kvm: already loaded the other module\n");
3185 if (!ops
->cpu_has_kvm_support()) {
3186 printk(KERN_ERR
"kvm: no hardware support\n");
3189 if (ops
->disabled_by_bios()) {
3190 printk(KERN_ERR
"kvm: disabled by bios\n");
3196 r
= kvm_arch_ops
->hardware_setup();
3200 on_each_cpu(hardware_enable
, NULL
, 0, 1);
3201 r
= register_cpu_notifier(&kvm_cpu_notifier
);
3204 register_reboot_notifier(&kvm_reboot_notifier
);
3206 r
= sysdev_class_register(&kvm_sysdev_class
);
3210 r
= sysdev_register(&kvm_sysdev
);
3214 kvm_chardev_ops
.owner
= module
;
3216 r
= misc_register(&kvm_dev
);
3218 printk (KERN_ERR
"kvm: misc device register failed\n");
3222 kvm_preempt_ops
.sched_in
= kvm_sched_in
;
3223 kvm_preempt_ops
.sched_out
= kvm_sched_out
;
3228 sysdev_unregister(&kvm_sysdev
);
3230 sysdev_class_unregister(&kvm_sysdev_class
);
3232 unregister_reboot_notifier(&kvm_reboot_notifier
);
3233 unregister_cpu_notifier(&kvm_cpu_notifier
);
3235 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3236 kvm_arch_ops
->hardware_unsetup();
3238 kvm_arch_ops
= NULL
;
3242 void kvm_exit_arch(void)
3244 misc_deregister(&kvm_dev
);
3245 sysdev_unregister(&kvm_sysdev
);
3246 sysdev_class_unregister(&kvm_sysdev_class
);
3247 unregister_reboot_notifier(&kvm_reboot_notifier
);
3248 unregister_cpu_notifier(&kvm_cpu_notifier
);
3249 on_each_cpu(hardware_disable
, NULL
, 0, 1);
3250 kvm_arch_ops
->hardware_unsetup();
3251 kvm_arch_ops
= NULL
;
3254 static __init
int kvm_init(void)
3256 static struct page
*bad_page
;
3259 r
= kvm_mmu_module_init();
3265 kvm_init_msr_list();
3267 if ((bad_page
= alloc_page(GFP_KERNEL
)) == NULL
) {
3272 bad_page_address
= page_to_pfn(bad_page
) << PAGE_SHIFT
;
3273 memset(__va(bad_page_address
), 0, PAGE_SIZE
);
3279 kvm_mmu_module_exit();
3284 static __exit
void kvm_exit(void)
3287 __free_page(pfn_to_page(bad_page_address
>> PAGE_SHIFT
));
3288 kvm_mmu_module_exit();
3291 module_init(kvm_init
)
3292 module_exit(kvm_exit
)
3294 EXPORT_SYMBOL_GPL(kvm_init_arch
);
3295 EXPORT_SYMBOL_GPL(kvm_exit_arch
);