2 * Kernel-based Virtual Machine driver for Linux
4 * derived from drivers/kvm/kvm_main.c
6 * Copyright (C) 2006 Qumranet, Inc.
9 * Avi Kivity <avi@qumranet.com>
10 * Yaniv Kamay <yaniv@qumranet.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
19 #include "x86_emulate.h"
20 #include "segment_descriptor.h"
23 #include <linux/kvm.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/mman.h>
29 #include <asm/uaccess.h>
32 #define MAX_IO_MSRS 256
33 #define CR0_RESERVED_BITS \
34 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
35 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
36 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
37 #define CR4_RESERVED_BITS \
38 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
39 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
40 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
41 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
43 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
44 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
46 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
49 struct kvm_x86_ops
*kvm_x86_ops
;
51 struct kvm_stats_debugfs_item debugfs_entries
[] = {
52 { "pf_fixed", VCPU_STAT(pf_fixed
) },
53 { "pf_guest", VCPU_STAT(pf_guest
) },
54 { "tlb_flush", VCPU_STAT(tlb_flush
) },
55 { "invlpg", VCPU_STAT(invlpg
) },
56 { "exits", VCPU_STAT(exits
) },
57 { "io_exits", VCPU_STAT(io_exits
) },
58 { "mmio_exits", VCPU_STAT(mmio_exits
) },
59 { "signal_exits", VCPU_STAT(signal_exits
) },
60 { "irq_window", VCPU_STAT(irq_window_exits
) },
61 { "halt_exits", VCPU_STAT(halt_exits
) },
62 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
63 { "request_irq", VCPU_STAT(request_irq_exits
) },
64 { "irq_exits", VCPU_STAT(irq_exits
) },
65 { "host_state_reload", VCPU_STAT(host_state_reload
) },
66 { "efer_reload", VCPU_STAT(efer_reload
) },
67 { "fpu_reload", VCPU_STAT(fpu_reload
) },
68 { "insn_emulation", VCPU_STAT(insn_emulation
) },
69 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
70 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
71 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
72 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
73 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
74 { "mmu_flooded", VM_STAT(mmu_flooded
) },
75 { "mmu_recycled", VM_STAT(mmu_recycled
) },
80 unsigned long segment_base(u16 selector
)
82 struct descriptor_table gdt
;
83 struct segment_descriptor
*d
;
84 unsigned long table_base
;
90 asm("sgdt %0" : "=m"(gdt
));
91 table_base
= gdt
.base
;
93 if (selector
& 4) { /* from ldt */
96 asm("sldt %0" : "=g"(ldt_selector
));
97 table_base
= segment_base(ldt_selector
);
99 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
100 v
= d
->base_low
| ((unsigned long)d
->base_mid
<< 16) |
101 ((unsigned long)d
->base_high
<< 24);
103 if (d
->system
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
104 v
|= ((unsigned long) \
105 ((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
109 EXPORT_SYMBOL_GPL(segment_base
);
111 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
113 if (irqchip_in_kernel(vcpu
->kvm
))
114 return vcpu
->apic_base
;
116 return vcpu
->apic_base
;
118 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
120 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
122 /* TODO: reserve bits check */
123 if (irqchip_in_kernel(vcpu
->kvm
))
124 kvm_lapic_set_base(vcpu
, data
);
126 vcpu
->apic_base
= data
;
128 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
130 static void inject_gp(struct kvm_vcpu
*vcpu
)
132 kvm_x86_ops
->inject_gp(vcpu
, 0);
136 * Load the pae pdptrs. Return true is they are all valid.
138 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
140 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
141 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
144 u64 pdpte
[ARRAY_SIZE(vcpu
->pdptrs
)];
146 mutex_lock(&vcpu
->kvm
->lock
);
147 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
148 offset
* sizeof(u64
), sizeof(pdpte
));
153 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
154 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
161 memcpy(vcpu
->pdptrs
, pdpte
, sizeof(vcpu
->pdptrs
));
163 mutex_unlock(&vcpu
->kvm
->lock
);
168 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
170 if (cr0
& CR0_RESERVED_BITS
) {
171 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
177 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
178 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
183 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
184 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
185 "and a clear PE flag\n");
190 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
192 if ((vcpu
->shadow_efer
& EFER_LME
)) {
196 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
197 "in long mode while PAE is disabled\n");
201 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
203 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
204 "in long mode while CS.L == 1\n");
211 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
212 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
220 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
223 mutex_lock(&vcpu
->kvm
->lock
);
224 kvm_mmu_reset_context(vcpu
);
225 mutex_unlock(&vcpu
->kvm
->lock
);
228 EXPORT_SYMBOL_GPL(set_cr0
);
230 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
232 set_cr0(vcpu
, (vcpu
->cr0
& ~0x0ful
) | (msw
& 0x0f));
234 EXPORT_SYMBOL_GPL(lmsw
);
236 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
238 if (cr4
& CR4_RESERVED_BITS
) {
239 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
244 if (is_long_mode(vcpu
)) {
245 if (!(cr4
& X86_CR4_PAE
)) {
246 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
251 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
252 && !load_pdptrs(vcpu
, vcpu
->cr3
)) {
253 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
258 if (cr4
& X86_CR4_VMXE
) {
259 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
263 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
265 mutex_lock(&vcpu
->kvm
->lock
);
266 kvm_mmu_reset_context(vcpu
);
267 mutex_unlock(&vcpu
->kvm
->lock
);
269 EXPORT_SYMBOL_GPL(set_cr4
);
271 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
273 if (is_long_mode(vcpu
)) {
274 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
275 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
281 if (cr3
& CR3_PAE_RESERVED_BITS
) {
283 "set_cr3: #GP, reserved bits\n");
287 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
288 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
295 * We don't check reserved bits in nonpae mode, because
296 * this isn't enforced, and VMware depends on this.
300 mutex_lock(&vcpu
->kvm
->lock
);
302 * Does the new cr3 value map to physical memory? (Note, we
303 * catch an invalid cr3 even in real-mode, because it would
304 * cause trouble later on when we turn on paging anyway.)
306 * A real CPU would silently accept an invalid cr3 and would
307 * attempt to use it - with largely undefined (and often hard
308 * to debug) behavior on the guest side.
310 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
314 vcpu
->mmu
.new_cr3(vcpu
);
316 mutex_unlock(&vcpu
->kvm
->lock
);
318 EXPORT_SYMBOL_GPL(set_cr3
);
320 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
322 if (cr8
& CR8_RESERVED_BITS
) {
323 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
327 if (irqchip_in_kernel(vcpu
->kvm
))
328 kvm_lapic_set_tpr(vcpu
, cr8
);
332 EXPORT_SYMBOL_GPL(set_cr8
);
334 unsigned long get_cr8(struct kvm_vcpu
*vcpu
)
336 if (irqchip_in_kernel(vcpu
->kvm
))
337 return kvm_lapic_get_cr8(vcpu
);
341 EXPORT_SYMBOL_GPL(get_cr8
);
344 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
345 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
347 * This list is modified at module load time to reflect the
348 * capabilities of the host cpu.
350 static u32 msrs_to_save
[] = {
351 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
354 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
356 MSR_IA32_TIME_STAMP_COUNTER
,
359 static unsigned num_msrs_to_save
;
361 static u32 emulated_msrs
[] = {
362 MSR_IA32_MISC_ENABLE
,
367 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
369 if (efer
& EFER_RESERVED_BITS
) {
370 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
377 && (vcpu
->shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
378 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
383 kvm_x86_ops
->set_efer(vcpu
, efer
);
386 efer
|= vcpu
->shadow_efer
& EFER_LMA
;
388 vcpu
->shadow_efer
= efer
;
394 * Writes msr value into into the appropriate "register".
395 * Returns 0 on success, non-0 otherwise.
396 * Assumes vcpu_load() was already called.
398 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
400 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
404 * Adapt set_msr() to msr_io()'s calling convention
406 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
408 return kvm_set_msr(vcpu
, index
, *data
);
412 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
417 set_efer(vcpu
, data
);
420 case MSR_IA32_MC0_STATUS
:
421 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
424 case MSR_IA32_MCG_STATUS
:
425 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
428 case MSR_IA32_UCODE_REV
:
429 case MSR_IA32_UCODE_WRITE
:
430 case 0x200 ... 0x2ff: /* MTRRs */
432 case MSR_IA32_APICBASE
:
433 kvm_set_apic_base(vcpu
, data
);
435 case MSR_IA32_MISC_ENABLE
:
436 vcpu
->ia32_misc_enable_msr
= data
;
439 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x\n", msr
);
444 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
448 * Reads an msr value (of 'msr_index') into 'pdata'.
449 * Returns 0 on success, non-0 otherwise.
450 * Assumes vcpu_load() was already called.
452 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
454 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
457 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
462 case 0xc0010010: /* SYSCFG */
463 case 0xc0010015: /* HWCR */
464 case MSR_IA32_PLATFORM_ID
:
465 case MSR_IA32_P5_MC_ADDR
:
466 case MSR_IA32_P5_MC_TYPE
:
467 case MSR_IA32_MC0_CTL
:
468 case MSR_IA32_MCG_STATUS
:
469 case MSR_IA32_MCG_CAP
:
470 case MSR_IA32_MC0_MISC
:
471 case MSR_IA32_MC0_MISC
+4:
472 case MSR_IA32_MC0_MISC
+8:
473 case MSR_IA32_MC0_MISC
+12:
474 case MSR_IA32_MC0_MISC
+16:
475 case MSR_IA32_UCODE_REV
:
476 case MSR_IA32_PERF_STATUS
:
477 case MSR_IA32_EBL_CR_POWERON
:
480 case 0x200 ... 0x2ff:
483 case 0xcd: /* fsb frequency */
486 case MSR_IA32_APICBASE
:
487 data
= kvm_get_apic_base(vcpu
);
489 case MSR_IA32_MISC_ENABLE
:
490 data
= vcpu
->ia32_misc_enable_msr
;
494 data
= vcpu
->shadow_efer
;
498 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
504 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
507 * Read or write a bunch of msrs. All parameters are kernel addresses.
509 * @return number of msrs set successfully.
511 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
512 struct kvm_msr_entry
*entries
,
513 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
514 unsigned index
, u64
*data
))
520 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
521 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
530 * Read or write a bunch of msrs. Parameters are user addresses.
532 * @return number of msrs set successfully.
534 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
535 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
536 unsigned index
, u64
*data
),
539 struct kvm_msrs msrs
;
540 struct kvm_msr_entry
*entries
;
545 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
549 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
553 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
554 entries
= vmalloc(size
);
559 if (copy_from_user(entries
, user_msrs
->entries
, size
))
562 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
567 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
579 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
582 void decache_vcpus_on_cpu(int cpu
)
585 struct kvm_vcpu
*vcpu
;
588 spin_lock(&kvm_lock
);
589 list_for_each_entry(vm
, &vm_list
, vm_list
)
590 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
595 * If the vcpu is locked, then it is running on some
596 * other cpu and therefore it is not cached on the
599 * If it's not locked, check the last cpu it executed
602 if (mutex_trylock(&vcpu
->mutex
)) {
603 if (vcpu
->cpu
== cpu
) {
604 kvm_x86_ops
->vcpu_decache(vcpu
);
607 mutex_unlock(&vcpu
->mutex
);
610 spin_unlock(&kvm_lock
);
613 int kvm_dev_ioctl_check_extension(long ext
)
618 case KVM_CAP_IRQCHIP
:
620 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
621 case KVM_CAP_USER_MEMORY
:
622 case KVM_CAP_SET_TSS_ADDR
:
633 long kvm_arch_dev_ioctl(struct file
*filp
,
634 unsigned int ioctl
, unsigned long arg
)
636 void __user
*argp
= (void __user
*)arg
;
640 case KVM_GET_MSR_INDEX_LIST
: {
641 struct kvm_msr_list __user
*user_msr_list
= argp
;
642 struct kvm_msr_list msr_list
;
646 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
649 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
650 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
653 if (n
< num_msrs_to_save
)
656 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
657 num_msrs_to_save
* sizeof(u32
)))
659 if (copy_to_user(user_msr_list
->indices
660 + num_msrs_to_save
* sizeof(u32
),
662 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
674 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
676 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
679 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
681 kvm_x86_ops
->vcpu_put(vcpu
);
682 kvm_put_guest_fpu(vcpu
);
685 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
689 struct kvm_cpuid_entry
*e
, *entry
;
691 rdmsrl(MSR_EFER
, efer
);
693 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
694 e
= &vcpu
->cpuid_entries
[i
];
695 if (e
->function
== 0x80000001) {
700 if (entry
&& (entry
->edx
& (1 << 20)) && !(efer
& EFER_NX
)) {
701 entry
->edx
&= ~(1 << 20);
702 printk(KERN_INFO
"kvm: guest NX capability removed\n");
706 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
707 struct kvm_cpuid
*cpuid
,
708 struct kvm_cpuid_entry __user
*entries
)
713 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
716 if (copy_from_user(&vcpu
->cpuid_entries
, entries
,
717 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
719 vcpu
->cpuid_nent
= cpuid
->nent
;
720 cpuid_fix_nx_cap(vcpu
);
727 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
728 struct kvm_lapic_state
*s
)
731 memcpy(s
->regs
, vcpu
->apic
->regs
, sizeof *s
);
737 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
738 struct kvm_lapic_state
*s
)
741 memcpy(vcpu
->apic
->regs
, s
->regs
, sizeof *s
);
742 kvm_apic_post_state_restore(vcpu
);
748 long kvm_arch_vcpu_ioctl(struct file
*filp
,
749 unsigned int ioctl
, unsigned long arg
)
751 struct kvm_vcpu
*vcpu
= filp
->private_data
;
752 void __user
*argp
= (void __user
*)arg
;
756 case KVM_GET_LAPIC
: {
757 struct kvm_lapic_state lapic
;
759 memset(&lapic
, 0, sizeof lapic
);
760 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
764 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
769 case KVM_SET_LAPIC
: {
770 struct kvm_lapic_state lapic
;
773 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
775 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
781 case KVM_SET_CPUID
: {
782 struct kvm_cpuid __user
*cpuid_arg
= argp
;
783 struct kvm_cpuid cpuid
;
786 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
788 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
794 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
797 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
806 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
810 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
812 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
816 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
817 u32 kvm_nr_mmu_pages
)
819 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
822 mutex_lock(&kvm
->lock
);
824 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
825 kvm
->n_requested_mmu_pages
= kvm_nr_mmu_pages
;
827 mutex_unlock(&kvm
->lock
);
831 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
833 return kvm
->n_alloc_mmu_pages
;
837 * Set a new alias region. Aliases map a portion of physical memory into
838 * another portion. This is useful for memory windows, for example the PC
841 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
842 struct kvm_memory_alias
*alias
)
845 struct kvm_mem_alias
*p
;
848 /* General sanity checks */
849 if (alias
->memory_size
& (PAGE_SIZE
- 1))
851 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
853 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
855 if (alias
->guest_phys_addr
+ alias
->memory_size
856 < alias
->guest_phys_addr
)
858 if (alias
->target_phys_addr
+ alias
->memory_size
859 < alias
->target_phys_addr
)
862 mutex_lock(&kvm
->lock
);
864 p
= &kvm
->aliases
[alias
->slot
];
865 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
866 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
867 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
869 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
870 if (kvm
->aliases
[n
- 1].npages
)
874 kvm_mmu_zap_all(kvm
);
876 mutex_unlock(&kvm
->lock
);
884 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
889 switch (chip
->chip_id
) {
890 case KVM_IRQCHIP_PIC_MASTER
:
891 memcpy(&chip
->chip
.pic
,
892 &pic_irqchip(kvm
)->pics
[0],
893 sizeof(struct kvm_pic_state
));
895 case KVM_IRQCHIP_PIC_SLAVE
:
896 memcpy(&chip
->chip
.pic
,
897 &pic_irqchip(kvm
)->pics
[1],
898 sizeof(struct kvm_pic_state
));
900 case KVM_IRQCHIP_IOAPIC
:
901 memcpy(&chip
->chip
.ioapic
,
903 sizeof(struct kvm_ioapic_state
));
912 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
917 switch (chip
->chip_id
) {
918 case KVM_IRQCHIP_PIC_MASTER
:
919 memcpy(&pic_irqchip(kvm
)->pics
[0],
921 sizeof(struct kvm_pic_state
));
923 case KVM_IRQCHIP_PIC_SLAVE
:
924 memcpy(&pic_irqchip(kvm
)->pics
[1],
926 sizeof(struct kvm_pic_state
));
928 case KVM_IRQCHIP_IOAPIC
:
929 memcpy(ioapic_irqchip(kvm
),
931 sizeof(struct kvm_ioapic_state
));
937 kvm_pic_update_irq(pic_irqchip(kvm
));
942 * Get (and clear) the dirty memory log for a memory slot.
944 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
945 struct kvm_dirty_log
*log
)
949 struct kvm_memory_slot
*memslot
;
952 mutex_lock(&kvm
->lock
);
954 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
958 /* If nothing is dirty, don't bother messing with page tables. */
960 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
961 kvm_flush_remote_tlbs(kvm
);
962 memslot
= &kvm
->memslots
[log
->slot
];
963 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
964 memset(memslot
->dirty_bitmap
, 0, n
);
968 mutex_unlock(&kvm
->lock
);
972 long kvm_arch_vm_ioctl(struct file
*filp
,
973 unsigned int ioctl
, unsigned long arg
)
975 struct kvm
*kvm
= filp
->private_data
;
976 void __user
*argp
= (void __user
*)arg
;
980 case KVM_SET_TSS_ADDR
:
981 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
985 case KVM_SET_MEMORY_REGION
: {
986 struct kvm_memory_region kvm_mem
;
987 struct kvm_userspace_memory_region kvm_userspace_mem
;
990 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
992 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
993 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
994 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
995 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
996 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1001 case KVM_SET_NR_MMU_PAGES
:
1002 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1006 case KVM_GET_NR_MMU_PAGES
:
1007 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1009 case KVM_SET_MEMORY_ALIAS
: {
1010 struct kvm_memory_alias alias
;
1013 if (copy_from_user(&alias
, argp
, sizeof alias
))
1015 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1020 case KVM_CREATE_IRQCHIP
:
1022 kvm
->vpic
= kvm_create_pic(kvm
);
1024 r
= kvm_ioapic_init(kvm
);
1033 case KVM_IRQ_LINE
: {
1034 struct kvm_irq_level irq_event
;
1037 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1039 if (irqchip_in_kernel(kvm
)) {
1040 mutex_lock(&kvm
->lock
);
1041 if (irq_event
.irq
< 16)
1042 kvm_pic_set_irq(pic_irqchip(kvm
),
1045 kvm_ioapic_set_irq(kvm
->vioapic
,
1048 mutex_unlock(&kvm
->lock
);
1053 case KVM_GET_IRQCHIP
: {
1054 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1055 struct kvm_irqchip chip
;
1058 if (copy_from_user(&chip
, argp
, sizeof chip
))
1061 if (!irqchip_in_kernel(kvm
))
1063 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1067 if (copy_to_user(argp
, &chip
, sizeof chip
))
1072 case KVM_SET_IRQCHIP
: {
1073 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1074 struct kvm_irqchip chip
;
1077 if (copy_from_user(&chip
, argp
, sizeof chip
))
1080 if (!irqchip_in_kernel(kvm
))
1082 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1095 static void kvm_init_msr_list(void)
1100 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1101 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1104 msrs_to_save
[j
] = msrs_to_save
[i
];
1107 num_msrs_to_save
= j
;
1111 * Only apic need an MMIO device hook, so shortcut now..
1113 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1116 struct kvm_io_device
*dev
;
1119 dev
= &vcpu
->apic
->dev
;
1120 if (dev
->in_range(dev
, addr
))
1127 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1130 struct kvm_io_device
*dev
;
1132 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1134 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1138 int emulator_read_std(unsigned long addr
,
1141 struct kvm_vcpu
*vcpu
)
1146 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1147 unsigned offset
= addr
& (PAGE_SIZE
-1);
1148 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1151 if (gpa
== UNMAPPED_GVA
)
1152 return X86EMUL_PROPAGATE_FAULT
;
1153 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1155 return X86EMUL_UNHANDLEABLE
;
1162 return X86EMUL_CONTINUE
;
1164 EXPORT_SYMBOL_GPL(emulator_read_std
);
1166 static int emulator_read_emulated(unsigned long addr
,
1169 struct kvm_vcpu
*vcpu
)
1171 struct kvm_io_device
*mmio_dev
;
1174 if (vcpu
->mmio_read_completed
) {
1175 memcpy(val
, vcpu
->mmio_data
, bytes
);
1176 vcpu
->mmio_read_completed
= 0;
1177 return X86EMUL_CONTINUE
;
1180 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1182 /* For APIC access vmexit */
1183 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1186 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1187 == X86EMUL_CONTINUE
)
1188 return X86EMUL_CONTINUE
;
1189 if (gpa
== UNMAPPED_GVA
)
1190 return X86EMUL_PROPAGATE_FAULT
;
1194 * Is this MMIO handled locally?
1196 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1198 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1199 return X86EMUL_CONTINUE
;
1202 vcpu
->mmio_needed
= 1;
1203 vcpu
->mmio_phys_addr
= gpa
;
1204 vcpu
->mmio_size
= bytes
;
1205 vcpu
->mmio_is_write
= 0;
1207 return X86EMUL_UNHANDLEABLE
;
1210 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1211 const void *val
, int bytes
)
1215 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1218 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1222 static int emulator_write_emulated_onepage(unsigned long addr
,
1225 struct kvm_vcpu
*vcpu
)
1227 struct kvm_io_device
*mmio_dev
;
1228 gpa_t gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, addr
);
1230 if (gpa
== UNMAPPED_GVA
) {
1231 kvm_x86_ops
->inject_page_fault(vcpu
, addr
, 2);
1232 return X86EMUL_PROPAGATE_FAULT
;
1235 /* For APIC access vmexit */
1236 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1239 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1240 return X86EMUL_CONTINUE
;
1244 * Is this MMIO handled locally?
1246 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1248 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1249 return X86EMUL_CONTINUE
;
1252 vcpu
->mmio_needed
= 1;
1253 vcpu
->mmio_phys_addr
= gpa
;
1254 vcpu
->mmio_size
= bytes
;
1255 vcpu
->mmio_is_write
= 1;
1256 memcpy(vcpu
->mmio_data
, val
, bytes
);
1258 return X86EMUL_CONTINUE
;
1261 int emulator_write_emulated(unsigned long addr
,
1264 struct kvm_vcpu
*vcpu
)
1266 /* Crossing a page boundary? */
1267 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1270 now
= -addr
& ~PAGE_MASK
;
1271 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1272 if (rc
!= X86EMUL_CONTINUE
)
1278 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1280 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1282 static int emulator_cmpxchg_emulated(unsigned long addr
,
1286 struct kvm_vcpu
*vcpu
)
1288 static int reported
;
1292 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1294 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1297 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1299 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1302 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1304 return X86EMUL_CONTINUE
;
1307 int emulate_clts(struct kvm_vcpu
*vcpu
)
1309 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->cr0
& ~X86_CR0_TS
);
1310 return X86EMUL_CONTINUE
;
1313 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1315 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1319 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1320 return X86EMUL_CONTINUE
;
1322 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __FUNCTION__
, dr
);
1323 return X86EMUL_UNHANDLEABLE
;
1327 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1329 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1332 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1334 /* FIXME: better handling */
1335 return X86EMUL_UNHANDLEABLE
;
1337 return X86EMUL_CONTINUE
;
1340 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
1342 static int reported
;
1344 unsigned long rip
= vcpu
->rip
;
1345 unsigned long rip_linear
;
1347 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
1352 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
1354 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1355 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1358 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
1360 struct x86_emulate_ops emulate_ops
= {
1361 .read_std
= emulator_read_std
,
1362 .read_emulated
= emulator_read_emulated
,
1363 .write_emulated
= emulator_write_emulated
,
1364 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1367 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1368 struct kvm_run
*run
,
1375 vcpu
->mmio_fault_cr2
= cr2
;
1376 kvm_x86_ops
->cache_regs(vcpu
);
1378 vcpu
->mmio_is_write
= 0;
1379 vcpu
->pio
.string
= 0;
1383 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1385 vcpu
->emulate_ctxt
.vcpu
= vcpu
;
1386 vcpu
->emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
1387 vcpu
->emulate_ctxt
.cr2
= cr2
;
1388 vcpu
->emulate_ctxt
.mode
=
1389 (vcpu
->emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1390 ? X86EMUL_MODE_REAL
: cs_l
1391 ? X86EMUL_MODE_PROT64
: cs_db
1392 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1394 if (vcpu
->emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1395 vcpu
->emulate_ctxt
.cs_base
= 0;
1396 vcpu
->emulate_ctxt
.ds_base
= 0;
1397 vcpu
->emulate_ctxt
.es_base
= 0;
1398 vcpu
->emulate_ctxt
.ss_base
= 0;
1400 vcpu
->emulate_ctxt
.cs_base
=
1401 get_segment_base(vcpu
, VCPU_SREG_CS
);
1402 vcpu
->emulate_ctxt
.ds_base
=
1403 get_segment_base(vcpu
, VCPU_SREG_DS
);
1404 vcpu
->emulate_ctxt
.es_base
=
1405 get_segment_base(vcpu
, VCPU_SREG_ES
);
1406 vcpu
->emulate_ctxt
.ss_base
=
1407 get_segment_base(vcpu
, VCPU_SREG_SS
);
1410 vcpu
->emulate_ctxt
.gs_base
=
1411 get_segment_base(vcpu
, VCPU_SREG_GS
);
1412 vcpu
->emulate_ctxt
.fs_base
=
1413 get_segment_base(vcpu
, VCPU_SREG_FS
);
1415 r
= x86_decode_insn(&vcpu
->emulate_ctxt
, &emulate_ops
);
1416 ++vcpu
->stat
.insn_emulation
;
1418 ++vcpu
->stat
.insn_emulation_fail
;
1419 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1420 return EMULATE_DONE
;
1421 return EMULATE_FAIL
;
1425 r
= x86_emulate_insn(&vcpu
->emulate_ctxt
, &emulate_ops
);
1427 if (vcpu
->pio
.string
)
1428 return EMULATE_DO_MMIO
;
1430 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1431 run
->exit_reason
= KVM_EXIT_MMIO
;
1432 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1433 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1434 run
->mmio
.len
= vcpu
->mmio_size
;
1435 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1439 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1440 return EMULATE_DONE
;
1441 if (!vcpu
->mmio_needed
) {
1442 kvm_report_emulation_failure(vcpu
, "mmio");
1443 return EMULATE_FAIL
;
1445 return EMULATE_DO_MMIO
;
1448 kvm_x86_ops
->decache_regs(vcpu
);
1449 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->emulate_ctxt
.eflags
);
1451 if (vcpu
->mmio_is_write
) {
1452 vcpu
->mmio_needed
= 0;
1453 return EMULATE_DO_MMIO
;
1456 return EMULATE_DONE
;
1458 EXPORT_SYMBOL_GPL(emulate_instruction
);
1460 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
1464 for (i
= 0; i
< ARRAY_SIZE(vcpu
->pio
.guest_pages
); ++i
)
1465 if (vcpu
->pio
.guest_pages
[i
]) {
1466 kvm_release_page_dirty(vcpu
->pio
.guest_pages
[i
]);
1467 vcpu
->pio
.guest_pages
[i
] = NULL
;
1471 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1473 void *p
= vcpu
->pio_data
;
1476 int nr_pages
= vcpu
->pio
.guest_pages
[1] ? 2 : 1;
1478 q
= vmap(vcpu
->pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1481 free_pio_guest_pages(vcpu
);
1484 q
+= vcpu
->pio
.guest_page_offset
;
1485 bytes
= vcpu
->pio
.size
* vcpu
->pio
.cur_count
;
1487 memcpy(q
, p
, bytes
);
1489 memcpy(p
, q
, bytes
);
1490 q
-= vcpu
->pio
.guest_page_offset
;
1492 free_pio_guest_pages(vcpu
);
1496 int complete_pio(struct kvm_vcpu
*vcpu
)
1498 struct kvm_pio_request
*io
= &vcpu
->pio
;
1502 kvm_x86_ops
->cache_regs(vcpu
);
1506 memcpy(&vcpu
->regs
[VCPU_REGS_RAX
], vcpu
->pio_data
,
1510 r
= pio_copy_data(vcpu
);
1512 kvm_x86_ops
->cache_regs(vcpu
);
1519 delta
*= io
->cur_count
;
1521 * The size of the register should really depend on
1522 * current address size.
1524 vcpu
->regs
[VCPU_REGS_RCX
] -= delta
;
1530 vcpu
->regs
[VCPU_REGS_RDI
] += delta
;
1532 vcpu
->regs
[VCPU_REGS_RSI
] += delta
;
1535 kvm_x86_ops
->decache_regs(vcpu
);
1537 io
->count
-= io
->cur_count
;
1543 static void kernel_pio(struct kvm_io_device
*pio_dev
,
1544 struct kvm_vcpu
*vcpu
,
1547 /* TODO: String I/O for in kernel device */
1549 mutex_lock(&vcpu
->kvm
->lock
);
1551 kvm_iodevice_read(pio_dev
, vcpu
->pio
.port
,
1555 kvm_iodevice_write(pio_dev
, vcpu
->pio
.port
,
1558 mutex_unlock(&vcpu
->kvm
->lock
);
1561 static void pio_string_write(struct kvm_io_device
*pio_dev
,
1562 struct kvm_vcpu
*vcpu
)
1564 struct kvm_pio_request
*io
= &vcpu
->pio
;
1565 void *pd
= vcpu
->pio_data
;
1568 mutex_lock(&vcpu
->kvm
->lock
);
1569 for (i
= 0; i
< io
->cur_count
; i
++) {
1570 kvm_iodevice_write(pio_dev
, io
->port
,
1575 mutex_unlock(&vcpu
->kvm
->lock
);
1578 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1581 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1584 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1585 int size
, unsigned port
)
1587 struct kvm_io_device
*pio_dev
;
1589 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1590 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1591 vcpu
->run
->io
.size
= vcpu
->pio
.size
= size
;
1592 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1593 vcpu
->run
->io
.count
= vcpu
->pio
.count
= vcpu
->pio
.cur_count
= 1;
1594 vcpu
->run
->io
.port
= vcpu
->pio
.port
= port
;
1596 vcpu
->pio
.string
= 0;
1598 vcpu
->pio
.guest_page_offset
= 0;
1601 kvm_x86_ops
->cache_regs(vcpu
);
1602 memcpy(vcpu
->pio_data
, &vcpu
->regs
[VCPU_REGS_RAX
], 4);
1603 kvm_x86_ops
->decache_regs(vcpu
);
1605 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1607 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1609 kernel_pio(pio_dev
, vcpu
, vcpu
->pio_data
);
1615 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
1617 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1618 int size
, unsigned long count
, int down
,
1619 gva_t address
, int rep
, unsigned port
)
1621 unsigned now
, in_page
;
1625 struct kvm_io_device
*pio_dev
;
1627 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1628 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1629 vcpu
->run
->io
.size
= vcpu
->pio
.size
= size
;
1630 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
1631 vcpu
->run
->io
.count
= vcpu
->pio
.count
= vcpu
->pio
.cur_count
= count
;
1632 vcpu
->run
->io
.port
= vcpu
->pio
.port
= port
;
1634 vcpu
->pio
.string
= 1;
1635 vcpu
->pio
.down
= down
;
1636 vcpu
->pio
.guest_page_offset
= offset_in_page(address
);
1637 vcpu
->pio
.rep
= rep
;
1640 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1645 in_page
= PAGE_SIZE
- offset_in_page(address
);
1647 in_page
= offset_in_page(address
) + size
;
1648 now
= min(count
, (unsigned long)in_page
/ size
);
1651 * String I/O straddles page boundary. Pin two guest pages
1652 * so that we satisfy atomicity constraints. Do just one
1653 * transaction to avoid complexity.
1660 * String I/O in reverse. Yuck. Kill the guest, fix later.
1662 pr_unimpl(vcpu
, "guest string pio down\n");
1666 vcpu
->run
->io
.count
= now
;
1667 vcpu
->pio
.cur_count
= now
;
1669 if (vcpu
->pio
.cur_count
== vcpu
->pio
.count
)
1670 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1672 for (i
= 0; i
< nr_pages
; ++i
) {
1673 mutex_lock(&vcpu
->kvm
->lock
);
1674 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
1675 vcpu
->pio
.guest_pages
[i
] = page
;
1676 mutex_unlock(&vcpu
->kvm
->lock
);
1679 free_pio_guest_pages(vcpu
);
1684 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
1685 if (!vcpu
->pio
.in
) {
1686 /* string PIO write */
1687 ret
= pio_copy_data(vcpu
);
1688 if (ret
>= 0 && pio_dev
) {
1689 pio_string_write(pio_dev
, vcpu
);
1691 if (vcpu
->pio
.count
== 0)
1695 pr_unimpl(vcpu
, "no string pio read support yet, "
1696 "port %x size %d count %ld\n",
1701 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
1703 int kvm_arch_init(void *opaque
)
1706 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
1708 r
= kvm_mmu_module_init();
1712 kvm_init_msr_list();
1715 printk(KERN_ERR
"kvm: already loaded the other module\n");
1720 if (!ops
->cpu_has_kvm_support()) {
1721 printk(KERN_ERR
"kvm: no hardware support\n");
1725 if (ops
->disabled_by_bios()) {
1726 printk(KERN_ERR
"kvm: disabled by bios\n");
1732 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
1736 kvm_mmu_module_exit();
1741 void kvm_arch_exit(void)
1744 kvm_mmu_module_exit();
1747 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
1749 ++vcpu
->stat
.halt_exits
;
1750 if (irqchip_in_kernel(vcpu
->kvm
)) {
1751 vcpu
->mp_state
= VCPU_MP_STATE_HALTED
;
1752 kvm_vcpu_block(vcpu
);
1753 if (vcpu
->mp_state
!= VCPU_MP_STATE_RUNNABLE
)
1757 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
1761 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
1763 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
1765 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
1767 kvm_x86_ops
->cache_regs(vcpu
);
1769 nr
= vcpu
->regs
[VCPU_REGS_RAX
];
1770 a0
= vcpu
->regs
[VCPU_REGS_RBX
];
1771 a1
= vcpu
->regs
[VCPU_REGS_RCX
];
1772 a2
= vcpu
->regs
[VCPU_REGS_RDX
];
1773 a3
= vcpu
->regs
[VCPU_REGS_RSI
];
1775 if (!is_long_mode(vcpu
)) {
1788 vcpu
->regs
[VCPU_REGS_RAX
] = ret
;
1789 kvm_x86_ops
->decache_regs(vcpu
);
1792 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
1794 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
1796 char instruction
[3];
1799 mutex_lock(&vcpu
->kvm
->lock
);
1802 * Blow out the MMU to ensure that no other VCPU has an active mapping
1803 * to ensure that the updated hypercall appears atomically across all
1806 kvm_mmu_zap_all(vcpu
->kvm
);
1808 kvm_x86_ops
->cache_regs(vcpu
);
1809 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
1810 if (emulator_write_emulated(vcpu
->rip
, instruction
, 3, vcpu
)
1811 != X86EMUL_CONTINUE
)
1814 mutex_unlock(&vcpu
->kvm
->lock
);
1819 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
1821 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
1824 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1826 struct descriptor_table dt
= { limit
, base
};
1828 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
1831 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
1833 struct descriptor_table dt
= { limit
, base
};
1835 kvm_x86_ops
->set_idt(vcpu
, &dt
);
1838 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
1839 unsigned long *rflags
)
1842 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
1845 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
1847 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
1858 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1863 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
1864 unsigned long *rflags
)
1868 set_cr0(vcpu
, mk_cr_64(vcpu
->cr0
, val
));
1869 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
1878 set_cr4(vcpu
, mk_cr_64(vcpu
->cr4
, val
));
1881 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
1885 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
1889 struct kvm_cpuid_entry
*e
, *best
;
1891 kvm_x86_ops
->cache_regs(vcpu
);
1892 function
= vcpu
->regs
[VCPU_REGS_RAX
];
1893 vcpu
->regs
[VCPU_REGS_RAX
] = 0;
1894 vcpu
->regs
[VCPU_REGS_RBX
] = 0;
1895 vcpu
->regs
[VCPU_REGS_RCX
] = 0;
1896 vcpu
->regs
[VCPU_REGS_RDX
] = 0;
1898 for (i
= 0; i
< vcpu
->cpuid_nent
; ++i
) {
1899 e
= &vcpu
->cpuid_entries
[i
];
1900 if (e
->function
== function
) {
1905 * Both basic or both extended?
1907 if (((e
->function
^ function
) & 0x80000000) == 0)
1908 if (!best
|| e
->function
> best
->function
)
1912 vcpu
->regs
[VCPU_REGS_RAX
] = best
->eax
;
1913 vcpu
->regs
[VCPU_REGS_RBX
] = best
->ebx
;
1914 vcpu
->regs
[VCPU_REGS_RCX
] = best
->ecx
;
1915 vcpu
->regs
[VCPU_REGS_RDX
] = best
->edx
;
1917 kvm_x86_ops
->decache_regs(vcpu
);
1918 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
1920 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
1923 * Check if userspace requested an interrupt window, and that the
1924 * interrupt window is open.
1926 * No need to exit to userspace if we already have an interrupt queued.
1928 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
1929 struct kvm_run
*kvm_run
)
1931 return (!vcpu
->irq_summary
&&
1932 kvm_run
->request_interrupt_window
&&
1933 vcpu
->interrupt_window_open
&&
1934 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
1937 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
1938 struct kvm_run
*kvm_run
)
1940 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
1941 kvm_run
->cr8
= get_cr8(vcpu
);
1942 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
1943 if (irqchip_in_kernel(vcpu
->kvm
))
1944 kvm_run
->ready_for_interrupt_injection
= 1;
1946 kvm_run
->ready_for_interrupt_injection
=
1947 (vcpu
->interrupt_window_open
&&
1948 vcpu
->irq_summary
== 0);
1951 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1955 if (unlikely(vcpu
->mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
1956 pr_debug("vcpu %d received sipi with vector # %x\n",
1957 vcpu
->vcpu_id
, vcpu
->sipi_vector
);
1958 kvm_lapic_reset(vcpu
);
1959 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
1962 vcpu
->mp_state
= VCPU_MP_STATE_RUNNABLE
;
1966 if (vcpu
->guest_debug
.enabled
)
1967 kvm_x86_ops
->guest_debug_pre(vcpu
);
1970 r
= kvm_mmu_reload(vcpu
);
1974 kvm_inject_pending_timer_irqs(vcpu
);
1978 kvm_x86_ops
->prepare_guest_switch(vcpu
);
1979 kvm_load_guest_fpu(vcpu
);
1981 local_irq_disable();
1983 if (signal_pending(current
)) {
1987 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1988 ++vcpu
->stat
.signal_exits
;
1992 if (irqchip_in_kernel(vcpu
->kvm
))
1993 kvm_x86_ops
->inject_pending_irq(vcpu
);
1994 else if (!vcpu
->mmio_read_completed
)
1995 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
1997 vcpu
->guest_mode
= 1;
2001 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2002 kvm_x86_ops
->tlb_flush(vcpu
);
2004 kvm_x86_ops
->run(vcpu
, kvm_run
);
2006 vcpu
->guest_mode
= 0;
2012 * We must have an instruction between local_irq_enable() and
2013 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2014 * the interrupt shadow. The stat.exits increment will do nicely.
2015 * But we need to prevent reordering, hence this barrier():
2024 * Profile KVM exit RIPs:
2026 if (unlikely(prof_on
== KVM_PROFILING
)) {
2027 kvm_x86_ops
->cache_regs(vcpu
);
2028 profile_hit(KVM_PROFILING
, (void *)vcpu
->rip
);
2031 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2034 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2036 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2037 ++vcpu
->stat
.request_irq_exits
;
2040 if (!need_resched())
2050 post_kvm_run_save(vcpu
, kvm_run
);
2055 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2062 if (unlikely(vcpu
->mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2063 kvm_vcpu_block(vcpu
);
2068 if (vcpu
->sigset_active
)
2069 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2071 /* re-sync apic's tpr */
2072 if (!irqchip_in_kernel(vcpu
->kvm
))
2073 set_cr8(vcpu
, kvm_run
->cr8
);
2075 if (vcpu
->pio
.cur_count
) {
2076 r
= complete_pio(vcpu
);
2080 #if CONFIG_HAS_IOMEM
2081 if (vcpu
->mmio_needed
) {
2082 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2083 vcpu
->mmio_read_completed
= 1;
2084 vcpu
->mmio_needed
= 0;
2085 r
= emulate_instruction(vcpu
, kvm_run
,
2086 vcpu
->mmio_fault_cr2
, 0, 1);
2087 if (r
== EMULATE_DO_MMIO
) {
2089 * Read-modify-write. Back to userspace.
2096 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2097 kvm_x86_ops
->cache_regs(vcpu
);
2098 vcpu
->regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2099 kvm_x86_ops
->decache_regs(vcpu
);
2102 r
= __vcpu_run(vcpu
, kvm_run
);
2105 if (vcpu
->sigset_active
)
2106 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2112 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2116 kvm_x86_ops
->cache_regs(vcpu
);
2118 regs
->rax
= vcpu
->regs
[VCPU_REGS_RAX
];
2119 regs
->rbx
= vcpu
->regs
[VCPU_REGS_RBX
];
2120 regs
->rcx
= vcpu
->regs
[VCPU_REGS_RCX
];
2121 regs
->rdx
= vcpu
->regs
[VCPU_REGS_RDX
];
2122 regs
->rsi
= vcpu
->regs
[VCPU_REGS_RSI
];
2123 regs
->rdi
= vcpu
->regs
[VCPU_REGS_RDI
];
2124 regs
->rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
2125 regs
->rbp
= vcpu
->regs
[VCPU_REGS_RBP
];
2126 #ifdef CONFIG_X86_64
2127 regs
->r8
= vcpu
->regs
[VCPU_REGS_R8
];
2128 regs
->r9
= vcpu
->regs
[VCPU_REGS_R9
];
2129 regs
->r10
= vcpu
->regs
[VCPU_REGS_R10
];
2130 regs
->r11
= vcpu
->regs
[VCPU_REGS_R11
];
2131 regs
->r12
= vcpu
->regs
[VCPU_REGS_R12
];
2132 regs
->r13
= vcpu
->regs
[VCPU_REGS_R13
];
2133 regs
->r14
= vcpu
->regs
[VCPU_REGS_R14
];
2134 regs
->r15
= vcpu
->regs
[VCPU_REGS_R15
];
2137 regs
->rip
= vcpu
->rip
;
2138 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2141 * Don't leak debug flags in case they were set for guest debugging
2143 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2144 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2151 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2155 vcpu
->regs
[VCPU_REGS_RAX
] = regs
->rax
;
2156 vcpu
->regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2157 vcpu
->regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2158 vcpu
->regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2159 vcpu
->regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2160 vcpu
->regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2161 vcpu
->regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2162 vcpu
->regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2163 #ifdef CONFIG_X86_64
2164 vcpu
->regs
[VCPU_REGS_R8
] = regs
->r8
;
2165 vcpu
->regs
[VCPU_REGS_R9
] = regs
->r9
;
2166 vcpu
->regs
[VCPU_REGS_R10
] = regs
->r10
;
2167 vcpu
->regs
[VCPU_REGS_R11
] = regs
->r11
;
2168 vcpu
->regs
[VCPU_REGS_R12
] = regs
->r12
;
2169 vcpu
->regs
[VCPU_REGS_R13
] = regs
->r13
;
2170 vcpu
->regs
[VCPU_REGS_R14
] = regs
->r14
;
2171 vcpu
->regs
[VCPU_REGS_R15
] = regs
->r15
;
2174 vcpu
->rip
= regs
->rip
;
2175 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2177 kvm_x86_ops
->decache_regs(vcpu
);
2184 static void get_segment(struct kvm_vcpu
*vcpu
,
2185 struct kvm_segment
*var
, int seg
)
2187 return kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2190 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2192 struct kvm_segment cs
;
2194 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2198 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
2200 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2201 struct kvm_sregs
*sregs
)
2203 struct descriptor_table dt
;
2208 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2209 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2210 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2211 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2212 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2213 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2215 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2216 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2218 kvm_x86_ops
->get_idt(vcpu
, &dt
);
2219 sregs
->idt
.limit
= dt
.limit
;
2220 sregs
->idt
.base
= dt
.base
;
2221 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
2222 sregs
->gdt
.limit
= dt
.limit
;
2223 sregs
->gdt
.base
= dt
.base
;
2225 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2226 sregs
->cr0
= vcpu
->cr0
;
2227 sregs
->cr2
= vcpu
->cr2
;
2228 sregs
->cr3
= vcpu
->cr3
;
2229 sregs
->cr4
= vcpu
->cr4
;
2230 sregs
->cr8
= get_cr8(vcpu
);
2231 sregs
->efer
= vcpu
->shadow_efer
;
2232 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
2234 if (irqchip_in_kernel(vcpu
->kvm
)) {
2235 memset(sregs
->interrupt_bitmap
, 0,
2236 sizeof sregs
->interrupt_bitmap
);
2237 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
2238 if (pending_vec
>= 0)
2239 set_bit(pending_vec
,
2240 (unsigned long *)sregs
->interrupt_bitmap
);
2242 memcpy(sregs
->interrupt_bitmap
, vcpu
->irq_pending
,
2243 sizeof sregs
->interrupt_bitmap
);
2250 static void set_segment(struct kvm_vcpu
*vcpu
,
2251 struct kvm_segment
*var
, int seg
)
2253 return kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
2256 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2257 struct kvm_sregs
*sregs
)
2259 int mmu_reset_needed
= 0;
2260 int i
, pending_vec
, max_bits
;
2261 struct descriptor_table dt
;
2265 dt
.limit
= sregs
->idt
.limit
;
2266 dt
.base
= sregs
->idt
.base
;
2267 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2268 dt
.limit
= sregs
->gdt
.limit
;
2269 dt
.base
= sregs
->gdt
.base
;
2270 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2272 vcpu
->cr2
= sregs
->cr2
;
2273 mmu_reset_needed
|= vcpu
->cr3
!= sregs
->cr3
;
2274 vcpu
->cr3
= sregs
->cr3
;
2276 set_cr8(vcpu
, sregs
->cr8
);
2278 mmu_reset_needed
|= vcpu
->shadow_efer
!= sregs
->efer
;
2279 #ifdef CONFIG_X86_64
2280 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
2282 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
2284 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2286 mmu_reset_needed
|= vcpu
->cr0
!= sregs
->cr0
;
2287 vcpu
->cr0
= sregs
->cr0
;
2288 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
2290 mmu_reset_needed
|= vcpu
->cr4
!= sregs
->cr4
;
2291 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
2292 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2293 load_pdptrs(vcpu
, vcpu
->cr3
);
2295 if (mmu_reset_needed
)
2296 kvm_mmu_reset_context(vcpu
);
2298 if (!irqchip_in_kernel(vcpu
->kvm
)) {
2299 memcpy(vcpu
->irq_pending
, sregs
->interrupt_bitmap
,
2300 sizeof vcpu
->irq_pending
);
2301 vcpu
->irq_summary
= 0;
2302 for (i
= 0; i
< ARRAY_SIZE(vcpu
->irq_pending
); ++i
)
2303 if (vcpu
->irq_pending
[i
])
2304 __set_bit(i
, &vcpu
->irq_summary
);
2306 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
2307 pending_vec
= find_first_bit(
2308 (const unsigned long *)sregs
->interrupt_bitmap
,
2310 /* Only pending external irq is handled here */
2311 if (pending_vec
< max_bits
) {
2312 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
2313 pr_debug("Set back pending irq %d\n",
2318 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2319 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2320 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2321 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2322 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2323 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2325 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2326 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2333 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2334 struct kvm_debug_guest
*dbg
)
2340 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
2348 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2349 * we have asm/x86/processor.h
2360 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2361 #ifdef CONFIG_X86_64
2362 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2364 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2369 * Translate a guest virtual address to a guest physical address.
2371 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2372 struct kvm_translation
*tr
)
2374 unsigned long vaddr
= tr
->linear_address
;
2378 mutex_lock(&vcpu
->kvm
->lock
);
2379 gpa
= vcpu
->mmu
.gva_to_gpa(vcpu
, vaddr
);
2380 tr
->physical_address
= gpa
;
2381 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2384 mutex_unlock(&vcpu
->kvm
->lock
);
2390 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2392 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->guest_fx_image
;
2396 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2397 fpu
->fcw
= fxsave
->cwd
;
2398 fpu
->fsw
= fxsave
->swd
;
2399 fpu
->ftwx
= fxsave
->twd
;
2400 fpu
->last_opcode
= fxsave
->fop
;
2401 fpu
->last_ip
= fxsave
->rip
;
2402 fpu
->last_dp
= fxsave
->rdp
;
2403 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2410 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2412 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->guest_fx_image
;
2416 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2417 fxsave
->cwd
= fpu
->fcw
;
2418 fxsave
->swd
= fpu
->fsw
;
2419 fxsave
->twd
= fpu
->ftwx
;
2420 fxsave
->fop
= fpu
->last_opcode
;
2421 fxsave
->rip
= fpu
->last_ip
;
2422 fxsave
->rdp
= fpu
->last_dp
;
2423 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2430 void fx_init(struct kvm_vcpu
*vcpu
)
2432 unsigned after_mxcsr_mask
;
2434 /* Initialize guest FPU by resetting ours and saving into guest's */
2436 fx_save(&vcpu
->host_fx_image
);
2438 fx_save(&vcpu
->guest_fx_image
);
2439 fx_restore(&vcpu
->host_fx_image
);
2442 vcpu
->cr0
|= X86_CR0_ET
;
2443 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
2444 vcpu
->guest_fx_image
.mxcsr
= 0x1f80;
2445 memset((void *)&vcpu
->guest_fx_image
+ after_mxcsr_mask
,
2446 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
2448 EXPORT_SYMBOL_GPL(fx_init
);
2450 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
2452 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
2455 vcpu
->guest_fpu_loaded
= 1;
2456 fx_save(&vcpu
->host_fx_image
);
2457 fx_restore(&vcpu
->guest_fx_image
);
2459 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
2461 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
2463 if (!vcpu
->guest_fpu_loaded
)
2466 vcpu
->guest_fpu_loaded
= 0;
2467 fx_save(&vcpu
->guest_fx_image
);
2468 fx_restore(&vcpu
->host_fx_image
);
2469 ++vcpu
->stat
.fpu_reload
;
2471 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
2473 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
2475 kvm_x86_ops
->vcpu_free(vcpu
);
2478 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
2481 return kvm_x86_ops
->vcpu_create(kvm
, id
);
2484 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
2488 /* We do fxsave: this must be aligned. */
2489 BUG_ON((unsigned long)&vcpu
->host_fx_image
& 0xF);
2492 r
= kvm_arch_vcpu_reset(vcpu
);
2494 r
= kvm_mmu_setup(vcpu
);
2501 kvm_x86_ops
->vcpu_free(vcpu
);
2505 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
2508 kvm_mmu_unload(vcpu
);
2511 kvm_x86_ops
->vcpu_free(vcpu
);
2514 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
2516 return kvm_x86_ops
->vcpu_reset(vcpu
);
2519 void kvm_arch_hardware_enable(void *garbage
)
2521 kvm_x86_ops
->hardware_enable(garbage
);
2524 void kvm_arch_hardware_disable(void *garbage
)
2526 kvm_x86_ops
->hardware_disable(garbage
);
2529 int kvm_arch_hardware_setup(void)
2531 return kvm_x86_ops
->hardware_setup();
2534 void kvm_arch_hardware_unsetup(void)
2536 kvm_x86_ops
->hardware_unsetup();
2539 void kvm_arch_check_processor_compat(void *rtn
)
2541 kvm_x86_ops
->check_processor_compatibility(rtn
);
2544 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
2550 BUG_ON(vcpu
->kvm
== NULL
);
2553 vcpu
->mmu
.root_hpa
= INVALID_PAGE
;
2554 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
2555 vcpu
->mp_state
= VCPU_MP_STATE_RUNNABLE
;
2557 vcpu
->mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
2559 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
2564 vcpu
->pio_data
= page_address(page
);
2566 r
= kvm_mmu_create(vcpu
);
2568 goto fail_free_pio_data
;
2570 if (irqchip_in_kernel(kvm
)) {
2571 r
= kvm_create_lapic(vcpu
);
2573 goto fail_mmu_destroy
;
2579 kvm_mmu_destroy(vcpu
);
2581 free_page((unsigned long)vcpu
->pio_data
);
2586 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
2588 kvm_free_lapic(vcpu
);
2589 kvm_mmu_destroy(vcpu
);
2590 free_page((unsigned long)vcpu
->pio_data
);
2593 struct kvm
*kvm_arch_create_vm(void)
2595 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
2598 return ERR_PTR(-ENOMEM
);
2600 INIT_LIST_HEAD(&kvm
->active_mmu_pages
);
2605 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
2608 kvm_mmu_unload(vcpu
);
2612 static void kvm_free_vcpus(struct kvm
*kvm
)
2617 * Unpin any mmu pages first.
2619 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
2621 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
2622 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2623 if (kvm
->vcpus
[i
]) {
2624 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
2625 kvm
->vcpus
[i
] = NULL
;
2631 void kvm_arch_destroy_vm(struct kvm
*kvm
)
2634 kfree(kvm
->vioapic
);
2635 kvm_free_vcpus(kvm
);
2636 kvm_free_physmem(kvm
);
2640 int kvm_arch_set_memory_region(struct kvm
*kvm
,
2641 struct kvm_userspace_memory_region
*mem
,
2642 struct kvm_memory_slot old
,
2645 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
2646 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
2648 /*To keep backward compatibility with older userspace,
2649 *x86 needs to hanlde !user_alloc case.
2652 if (npages
&& !old
.rmap
) {
2653 down_write(¤t
->mm
->mmap_sem
);
2654 memslot
->userspace_addr
= do_mmap(NULL
, 0,
2656 PROT_READ
| PROT_WRITE
,
2657 MAP_SHARED
| MAP_ANONYMOUS
,
2659 up_write(¤t
->mm
->mmap_sem
);
2661 if (IS_ERR((void *)memslot
->userspace_addr
))
2662 return PTR_ERR((void *)memslot
->userspace_addr
);
2664 if (!old
.user_alloc
&& old
.rmap
) {
2667 down_write(¤t
->mm
->mmap_sem
);
2668 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
2669 old
.npages
* PAGE_SIZE
);
2670 up_write(¤t
->mm
->mmap_sem
);
2673 "kvm_vm_ioctl_set_memory_region: "
2674 "failed to munmap memory\n");
2679 if (!kvm
->n_requested_mmu_pages
) {
2680 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
2681 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
2684 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
2685 kvm_flush_remote_tlbs(kvm
);