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"
24 #include <linux/kvm.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/mman.h>
29 #include <linux/highmem.h>
31 #include <asm/uaccess.h>
34 #define MAX_IO_MSRS 256
35 #define CR0_RESERVED_BITS \
36 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
37 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
38 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
39 #define CR4_RESERVED_BITS \
40 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
41 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
42 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
43 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
45 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
46 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
48 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
49 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
51 struct kvm_x86_ops
*kvm_x86_ops
;
53 struct kvm_stats_debugfs_item debugfs_entries
[] = {
54 { "pf_fixed", VCPU_STAT(pf_fixed
) },
55 { "pf_guest", VCPU_STAT(pf_guest
) },
56 { "tlb_flush", VCPU_STAT(tlb_flush
) },
57 { "invlpg", VCPU_STAT(invlpg
) },
58 { "exits", VCPU_STAT(exits
) },
59 { "io_exits", VCPU_STAT(io_exits
) },
60 { "mmio_exits", VCPU_STAT(mmio_exits
) },
61 { "signal_exits", VCPU_STAT(signal_exits
) },
62 { "irq_window", VCPU_STAT(irq_window_exits
) },
63 { "halt_exits", VCPU_STAT(halt_exits
) },
64 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
65 { "request_irq", VCPU_STAT(request_irq_exits
) },
66 { "irq_exits", VCPU_STAT(irq_exits
) },
67 { "host_state_reload", VCPU_STAT(host_state_reload
) },
68 { "efer_reload", VCPU_STAT(efer_reload
) },
69 { "fpu_reload", VCPU_STAT(fpu_reload
) },
70 { "insn_emulation", VCPU_STAT(insn_emulation
) },
71 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
72 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
73 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
74 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
75 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
76 { "mmu_flooded", VM_STAT(mmu_flooded
) },
77 { "mmu_recycled", VM_STAT(mmu_recycled
) },
78 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
83 unsigned long segment_base(u16 selector
)
85 struct descriptor_table gdt
;
86 struct segment_descriptor
*d
;
87 unsigned long table_base
;
93 asm("sgdt %0" : "=m"(gdt
));
94 table_base
= gdt
.base
;
96 if (selector
& 4) { /* from ldt */
99 asm("sldt %0" : "=g"(ldt_selector
));
100 table_base
= segment_base(ldt_selector
);
102 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
103 v
= d
->base_low
| ((unsigned long)d
->base_mid
<< 16) |
104 ((unsigned long)d
->base_high
<< 24);
106 if (d
->system
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
107 v
|= ((unsigned long) \
108 ((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
112 EXPORT_SYMBOL_GPL(segment_base
);
114 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
116 if (irqchip_in_kernel(vcpu
->kvm
))
117 return vcpu
->arch
.apic_base
;
119 return vcpu
->arch
.apic_base
;
121 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
123 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
125 /* TODO: reserve bits check */
126 if (irqchip_in_kernel(vcpu
->kvm
))
127 kvm_lapic_set_base(vcpu
, data
);
129 vcpu
->arch
.apic_base
= data
;
131 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
133 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
135 WARN_ON(vcpu
->arch
.exception
.pending
);
136 vcpu
->arch
.exception
.pending
= true;
137 vcpu
->arch
.exception
.has_error_code
= false;
138 vcpu
->arch
.exception
.nr
= nr
;
140 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
142 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
145 ++vcpu
->stat
.pf_guest
;
146 if (vcpu
->arch
.exception
.pending
&& vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
147 printk(KERN_DEBUG
"kvm: inject_page_fault:"
148 " double fault 0x%lx\n", addr
);
149 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
150 vcpu
->arch
.exception
.error_code
= 0;
153 vcpu
->arch
.cr2
= addr
;
154 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
157 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
159 WARN_ON(vcpu
->arch
.exception
.pending
);
160 vcpu
->arch
.exception
.pending
= true;
161 vcpu
->arch
.exception
.has_error_code
= true;
162 vcpu
->arch
.exception
.nr
= nr
;
163 vcpu
->arch
.exception
.error_code
= error_code
;
165 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
167 static void __queue_exception(struct kvm_vcpu
*vcpu
)
169 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
170 vcpu
->arch
.exception
.has_error_code
,
171 vcpu
->arch
.exception
.error_code
);
175 * Load the pae pdptrs. Return true is they are all valid.
177 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
179 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
180 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
183 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
185 mutex_lock(&vcpu
->kvm
->lock
);
186 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
187 offset
* sizeof(u64
), sizeof(pdpte
));
192 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
193 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
200 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
202 mutex_unlock(&vcpu
->kvm
->lock
);
207 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
209 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
213 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
216 mutex_lock(&vcpu
->kvm
->lock
);
217 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
220 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
222 mutex_unlock(&vcpu
->kvm
->lock
);
227 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
229 if (cr0
& CR0_RESERVED_BITS
) {
230 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
231 cr0
, vcpu
->arch
.cr0
);
232 kvm_inject_gp(vcpu
, 0);
236 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
237 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
238 kvm_inject_gp(vcpu
, 0);
242 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
243 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
244 "and a clear PE flag\n");
245 kvm_inject_gp(vcpu
, 0);
249 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
251 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
255 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
256 "in long mode while PAE is disabled\n");
257 kvm_inject_gp(vcpu
, 0);
260 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
262 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
263 "in long mode while CS.L == 1\n");
264 kvm_inject_gp(vcpu
, 0);
270 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
271 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
273 kvm_inject_gp(vcpu
, 0);
279 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
280 vcpu
->arch
.cr0
= cr0
;
282 mutex_lock(&vcpu
->kvm
->lock
);
283 kvm_mmu_reset_context(vcpu
);
284 mutex_unlock(&vcpu
->kvm
->lock
);
287 EXPORT_SYMBOL_GPL(set_cr0
);
289 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
291 set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
293 EXPORT_SYMBOL_GPL(lmsw
);
295 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
297 if (cr4
& CR4_RESERVED_BITS
) {
298 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
299 kvm_inject_gp(vcpu
, 0);
303 if (is_long_mode(vcpu
)) {
304 if (!(cr4
& X86_CR4_PAE
)) {
305 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
307 kvm_inject_gp(vcpu
, 0);
310 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
311 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
312 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
313 kvm_inject_gp(vcpu
, 0);
317 if (cr4
& X86_CR4_VMXE
) {
318 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
319 kvm_inject_gp(vcpu
, 0);
322 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
323 vcpu
->arch
.cr4
= cr4
;
324 mutex_lock(&vcpu
->kvm
->lock
);
325 kvm_mmu_reset_context(vcpu
);
326 mutex_unlock(&vcpu
->kvm
->lock
);
328 EXPORT_SYMBOL_GPL(set_cr4
);
330 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
332 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
333 kvm_mmu_flush_tlb(vcpu
);
337 if (is_long_mode(vcpu
)) {
338 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
339 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
340 kvm_inject_gp(vcpu
, 0);
345 if (cr3
& CR3_PAE_RESERVED_BITS
) {
347 "set_cr3: #GP, reserved bits\n");
348 kvm_inject_gp(vcpu
, 0);
351 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
352 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
354 kvm_inject_gp(vcpu
, 0);
359 * We don't check reserved bits in nonpae mode, because
360 * this isn't enforced, and VMware depends on this.
364 mutex_lock(&vcpu
->kvm
->lock
);
366 * Does the new cr3 value map to physical memory? (Note, we
367 * catch an invalid cr3 even in real-mode, because it would
368 * cause trouble later on when we turn on paging anyway.)
370 * A real CPU would silently accept an invalid cr3 and would
371 * attempt to use it - with largely undefined (and often hard
372 * to debug) behavior on the guest side.
374 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
375 kvm_inject_gp(vcpu
, 0);
377 vcpu
->arch
.cr3
= cr3
;
378 vcpu
->arch
.mmu
.new_cr3(vcpu
);
380 mutex_unlock(&vcpu
->kvm
->lock
);
382 EXPORT_SYMBOL_GPL(set_cr3
);
384 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
386 if (cr8
& CR8_RESERVED_BITS
) {
387 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
388 kvm_inject_gp(vcpu
, 0);
391 if (irqchip_in_kernel(vcpu
->kvm
))
392 kvm_lapic_set_tpr(vcpu
, cr8
);
394 vcpu
->arch
.cr8
= cr8
;
396 EXPORT_SYMBOL_GPL(set_cr8
);
398 unsigned long get_cr8(struct kvm_vcpu
*vcpu
)
400 if (irqchip_in_kernel(vcpu
->kvm
))
401 return kvm_lapic_get_cr8(vcpu
);
403 return vcpu
->arch
.cr8
;
405 EXPORT_SYMBOL_GPL(get_cr8
);
408 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
409 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
411 * This list is modified at module load time to reflect the
412 * capabilities of the host cpu.
414 static u32 msrs_to_save
[] = {
415 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
418 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
420 MSR_IA32_TIME_STAMP_COUNTER
,
423 static unsigned num_msrs_to_save
;
425 static u32 emulated_msrs
[] = {
426 MSR_IA32_MISC_ENABLE
,
431 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
433 if (efer
& EFER_RESERVED_BITS
) {
434 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
436 kvm_inject_gp(vcpu
, 0);
441 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
442 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
443 kvm_inject_gp(vcpu
, 0);
447 kvm_x86_ops
->set_efer(vcpu
, efer
);
450 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
452 vcpu
->arch
.shadow_efer
= efer
;
458 * Writes msr value into into the appropriate "register".
459 * Returns 0 on success, non-0 otherwise.
460 * Assumes vcpu_load() was already called.
462 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
464 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
468 * Adapt set_msr() to msr_io()'s calling convention
470 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
472 return kvm_set_msr(vcpu
, index
, *data
);
476 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
481 set_efer(vcpu
, data
);
484 case MSR_IA32_MC0_STATUS
:
485 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
488 case MSR_IA32_MCG_STATUS
:
489 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
492 case MSR_IA32_UCODE_REV
:
493 case MSR_IA32_UCODE_WRITE
:
494 case 0x200 ... 0x2ff: /* MTRRs */
496 case MSR_IA32_APICBASE
:
497 kvm_set_apic_base(vcpu
, data
);
499 case MSR_IA32_MISC_ENABLE
:
500 vcpu
->arch
.ia32_misc_enable_msr
= data
;
503 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x\n", msr
);
508 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
512 * Reads an msr value (of 'msr_index') into 'pdata'.
513 * Returns 0 on success, non-0 otherwise.
514 * Assumes vcpu_load() was already called.
516 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
518 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
521 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
526 case 0xc0010010: /* SYSCFG */
527 case 0xc0010015: /* HWCR */
528 case MSR_IA32_PLATFORM_ID
:
529 case MSR_IA32_P5_MC_ADDR
:
530 case MSR_IA32_P5_MC_TYPE
:
531 case MSR_IA32_MC0_CTL
:
532 case MSR_IA32_MCG_STATUS
:
533 case MSR_IA32_MCG_CAP
:
534 case MSR_IA32_MC0_MISC
:
535 case MSR_IA32_MC0_MISC
+4:
536 case MSR_IA32_MC0_MISC
+8:
537 case MSR_IA32_MC0_MISC
+12:
538 case MSR_IA32_MC0_MISC
+16:
539 case MSR_IA32_UCODE_REV
:
540 case MSR_IA32_PERF_STATUS
:
541 case MSR_IA32_EBL_CR_POWERON
:
544 case 0x200 ... 0x2ff:
547 case 0xcd: /* fsb frequency */
550 case MSR_IA32_APICBASE
:
551 data
= kvm_get_apic_base(vcpu
);
553 case MSR_IA32_MISC_ENABLE
:
554 data
= vcpu
->arch
.ia32_misc_enable_msr
;
558 data
= vcpu
->arch
.shadow_efer
;
562 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
568 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
571 * Read or write a bunch of msrs. All parameters are kernel addresses.
573 * @return number of msrs set successfully.
575 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
576 struct kvm_msr_entry
*entries
,
577 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
578 unsigned index
, u64
*data
))
584 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
585 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
594 * Read or write a bunch of msrs. Parameters are user addresses.
596 * @return number of msrs set successfully.
598 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
599 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
600 unsigned index
, u64
*data
),
603 struct kvm_msrs msrs
;
604 struct kvm_msr_entry
*entries
;
609 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
613 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
617 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
618 entries
= vmalloc(size
);
623 if (copy_from_user(entries
, user_msrs
->entries
, size
))
626 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
631 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
643 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
646 void decache_vcpus_on_cpu(int cpu
)
649 struct kvm_vcpu
*vcpu
;
652 spin_lock(&kvm_lock
);
653 list_for_each_entry(vm
, &vm_list
, vm_list
)
654 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
659 * If the vcpu is locked, then it is running on some
660 * other cpu and therefore it is not cached on the
663 * If it's not locked, check the last cpu it executed
666 if (mutex_trylock(&vcpu
->mutex
)) {
667 if (vcpu
->cpu
== cpu
) {
668 kvm_x86_ops
->vcpu_decache(vcpu
);
671 mutex_unlock(&vcpu
->mutex
);
674 spin_unlock(&kvm_lock
);
677 int kvm_dev_ioctl_check_extension(long ext
)
682 case KVM_CAP_IRQCHIP
:
684 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
685 case KVM_CAP_USER_MEMORY
:
686 case KVM_CAP_SET_TSS_ADDR
:
687 case KVM_CAP_EXT_CPUID
:
698 long kvm_arch_dev_ioctl(struct file
*filp
,
699 unsigned int ioctl
, unsigned long arg
)
701 void __user
*argp
= (void __user
*)arg
;
705 case KVM_GET_MSR_INDEX_LIST
: {
706 struct kvm_msr_list __user
*user_msr_list
= argp
;
707 struct kvm_msr_list msr_list
;
711 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
714 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
715 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
718 if (n
< num_msrs_to_save
)
721 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
722 num_msrs_to_save
* sizeof(u32
)))
724 if (copy_to_user(user_msr_list
->indices
725 + num_msrs_to_save
* sizeof(u32
),
727 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
739 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
741 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
744 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
746 kvm_x86_ops
->vcpu_put(vcpu
);
747 kvm_put_guest_fpu(vcpu
);
750 static int is_efer_nx(void)
754 rdmsrl(MSR_EFER
, efer
);
755 return efer
& EFER_NX
;
758 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
761 struct kvm_cpuid_entry2
*e
, *entry
;
764 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
765 e
= &vcpu
->arch
.cpuid_entries
[i
];
766 if (e
->function
== 0x80000001) {
771 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
772 entry
->edx
&= ~(1 << 20);
773 printk(KERN_INFO
"kvm: guest NX capability removed\n");
777 /* when an old userspace process fills a new kernel module */
778 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
779 struct kvm_cpuid
*cpuid
,
780 struct kvm_cpuid_entry __user
*entries
)
783 struct kvm_cpuid_entry
*cpuid_entries
;
786 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
789 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
793 if (copy_from_user(cpuid_entries
, entries
,
794 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
796 for (i
= 0; i
< cpuid
->nent
; i
++) {
797 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
798 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
799 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
800 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
801 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
802 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
803 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
804 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
805 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
806 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
808 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
809 cpuid_fix_nx_cap(vcpu
);
813 vfree(cpuid_entries
);
818 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
819 struct kvm_cpuid2
*cpuid
,
820 struct kvm_cpuid_entry2 __user
*entries
)
825 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
828 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
829 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
831 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
838 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
839 struct kvm_cpuid2
*cpuid
,
840 struct kvm_cpuid_entry2 __user
*entries
)
845 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
848 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
849 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
854 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
858 static inline u32
bit(int bitno
)
860 return 1 << (bitno
& 31);
863 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
866 entry
->function
= function
;
867 entry
->index
= index
;
868 cpuid_count(entry
->function
, entry
->index
,
869 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
873 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
874 u32 index
, int *nent
, int maxnent
)
876 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
877 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
878 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
879 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
880 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
881 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
882 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
883 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
884 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
885 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
886 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
887 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
888 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
889 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
890 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
891 bit(X86_FEATURE_PGE
) |
892 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
893 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
894 bit(X86_FEATURE_SYSCALL
) |
895 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
897 bit(X86_FEATURE_LM
) |
899 bit(X86_FEATURE_MMXEXT
) |
900 bit(X86_FEATURE_3DNOWEXT
) |
901 bit(X86_FEATURE_3DNOW
);
902 const u32 kvm_supported_word3_x86_features
=
903 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
904 const u32 kvm_supported_word6_x86_features
=
905 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
907 /* all func 2 cpuid_count() should be called on the same cpu */
909 do_cpuid_1_ent(entry
, function
, index
);
914 entry
->eax
= min(entry
->eax
, (u32
)0xb);
917 entry
->edx
&= kvm_supported_word0_x86_features
;
918 entry
->ecx
&= kvm_supported_word3_x86_features
;
920 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
921 * may return different values. This forces us to get_cpu() before
922 * issuing the first command, and also to emulate this annoying behavior
923 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
925 int t
, times
= entry
->eax
& 0xff;
927 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
928 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
929 do_cpuid_1_ent(&entry
[t
], function
, 0);
930 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
935 /* function 4 and 0xb have additional index. */
937 int index
, cache_type
;
939 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
940 /* read more entries until cache_type is zero */
941 for (index
= 1; *nent
< maxnent
; ++index
) {
942 cache_type
= entry
[index
- 1].eax
& 0x1f;
945 do_cpuid_1_ent(&entry
[index
], function
, index
);
946 entry
[index
].flags
|=
947 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
953 int index
, level_type
;
955 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
956 /* read more entries until level_type is zero */
957 for (index
= 1; *nent
< maxnent
; ++index
) {
958 level_type
= entry
[index
- 1].ecx
& 0xff;
961 do_cpuid_1_ent(&entry
[index
], function
, index
);
962 entry
[index
].flags
|=
963 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
969 entry
->eax
= min(entry
->eax
, 0x8000001a);
972 entry
->edx
&= kvm_supported_word1_x86_features
;
973 entry
->ecx
&= kvm_supported_word6_x86_features
;
979 static int kvm_vm_ioctl_get_supported_cpuid(struct kvm
*kvm
,
980 struct kvm_cpuid2
*cpuid
,
981 struct kvm_cpuid_entry2 __user
*entries
)
983 struct kvm_cpuid_entry2
*cpuid_entries
;
984 int limit
, nent
= 0, r
= -E2BIG
;
990 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
994 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
995 limit
= cpuid_entries
[0].eax
;
996 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
997 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1000 if (nent
>= cpuid
->nent
)
1003 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1004 limit
= cpuid_entries
[nent
- 1].eax
;
1005 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1006 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1007 &nent
, cpuid
->nent
);
1009 if (copy_to_user(entries
, cpuid_entries
,
1010 nent
* sizeof(struct kvm_cpuid_entry2
)))
1016 vfree(cpuid_entries
);
1021 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1022 struct kvm_lapic_state
*s
)
1025 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1031 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1032 struct kvm_lapic_state
*s
)
1035 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1036 kvm_apic_post_state_restore(vcpu
);
1042 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1043 struct kvm_interrupt
*irq
)
1045 if (irq
->irq
< 0 || irq
->irq
>= 256)
1047 if (irqchip_in_kernel(vcpu
->kvm
))
1051 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1052 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1059 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1060 unsigned int ioctl
, unsigned long arg
)
1062 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1063 void __user
*argp
= (void __user
*)arg
;
1067 case KVM_GET_LAPIC
: {
1068 struct kvm_lapic_state lapic
;
1070 memset(&lapic
, 0, sizeof lapic
);
1071 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
1075 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
1080 case KVM_SET_LAPIC
: {
1081 struct kvm_lapic_state lapic
;
1084 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
1086 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
1092 case KVM_INTERRUPT
: {
1093 struct kvm_interrupt irq
;
1096 if (copy_from_user(&irq
, argp
, sizeof irq
))
1098 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1104 case KVM_SET_CPUID
: {
1105 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1106 struct kvm_cpuid cpuid
;
1109 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1111 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1116 case KVM_SET_CPUID2
: {
1117 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1118 struct kvm_cpuid2 cpuid
;
1121 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1123 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1124 cpuid_arg
->entries
);
1129 case KVM_GET_CPUID2
: {
1130 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1131 struct kvm_cpuid2 cpuid
;
1134 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1136 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1137 cpuid_arg
->entries
);
1141 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1147 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1150 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1159 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1163 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1165 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1169 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1170 u32 kvm_nr_mmu_pages
)
1172 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1175 mutex_lock(&kvm
->lock
);
1177 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1178 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1180 mutex_unlock(&kvm
->lock
);
1184 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1186 return kvm
->arch
.n_alloc_mmu_pages
;
1189 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1192 struct kvm_mem_alias
*alias
;
1194 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1195 alias
= &kvm
->arch
.aliases
[i
];
1196 if (gfn
>= alias
->base_gfn
1197 && gfn
< alias
->base_gfn
+ alias
->npages
)
1198 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1204 * Set a new alias region. Aliases map a portion of physical memory into
1205 * another portion. This is useful for memory windows, for example the PC
1208 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1209 struct kvm_memory_alias
*alias
)
1212 struct kvm_mem_alias
*p
;
1215 /* General sanity checks */
1216 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1218 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1220 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1222 if (alias
->guest_phys_addr
+ alias
->memory_size
1223 < alias
->guest_phys_addr
)
1225 if (alias
->target_phys_addr
+ alias
->memory_size
1226 < alias
->target_phys_addr
)
1229 mutex_lock(&kvm
->lock
);
1231 p
= &kvm
->arch
.aliases
[alias
->slot
];
1232 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1233 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1234 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1236 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1237 if (kvm
->arch
.aliases
[n
- 1].npages
)
1239 kvm
->arch
.naliases
= n
;
1241 kvm_mmu_zap_all(kvm
);
1243 mutex_unlock(&kvm
->lock
);
1251 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1256 switch (chip
->chip_id
) {
1257 case KVM_IRQCHIP_PIC_MASTER
:
1258 memcpy(&chip
->chip
.pic
,
1259 &pic_irqchip(kvm
)->pics
[0],
1260 sizeof(struct kvm_pic_state
));
1262 case KVM_IRQCHIP_PIC_SLAVE
:
1263 memcpy(&chip
->chip
.pic
,
1264 &pic_irqchip(kvm
)->pics
[1],
1265 sizeof(struct kvm_pic_state
));
1267 case KVM_IRQCHIP_IOAPIC
:
1268 memcpy(&chip
->chip
.ioapic
,
1269 ioapic_irqchip(kvm
),
1270 sizeof(struct kvm_ioapic_state
));
1279 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1284 switch (chip
->chip_id
) {
1285 case KVM_IRQCHIP_PIC_MASTER
:
1286 memcpy(&pic_irqchip(kvm
)->pics
[0],
1288 sizeof(struct kvm_pic_state
));
1290 case KVM_IRQCHIP_PIC_SLAVE
:
1291 memcpy(&pic_irqchip(kvm
)->pics
[1],
1293 sizeof(struct kvm_pic_state
));
1295 case KVM_IRQCHIP_IOAPIC
:
1296 memcpy(ioapic_irqchip(kvm
),
1298 sizeof(struct kvm_ioapic_state
));
1304 kvm_pic_update_irq(pic_irqchip(kvm
));
1309 * Get (and clear) the dirty memory log for a memory slot.
1311 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1312 struct kvm_dirty_log
*log
)
1316 struct kvm_memory_slot
*memslot
;
1319 mutex_lock(&kvm
->lock
);
1321 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1325 /* If nothing is dirty, don't bother messing with page tables. */
1327 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1328 kvm_flush_remote_tlbs(kvm
);
1329 memslot
= &kvm
->memslots
[log
->slot
];
1330 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1331 memset(memslot
->dirty_bitmap
, 0, n
);
1335 mutex_unlock(&kvm
->lock
);
1339 long kvm_arch_vm_ioctl(struct file
*filp
,
1340 unsigned int ioctl
, unsigned long arg
)
1342 struct kvm
*kvm
= filp
->private_data
;
1343 void __user
*argp
= (void __user
*)arg
;
1347 case KVM_SET_TSS_ADDR
:
1348 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1352 case KVM_SET_MEMORY_REGION
: {
1353 struct kvm_memory_region kvm_mem
;
1354 struct kvm_userspace_memory_region kvm_userspace_mem
;
1357 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1359 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1360 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1361 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1362 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1363 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1368 case KVM_SET_NR_MMU_PAGES
:
1369 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1373 case KVM_GET_NR_MMU_PAGES
:
1374 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1376 case KVM_SET_MEMORY_ALIAS
: {
1377 struct kvm_memory_alias alias
;
1380 if (copy_from_user(&alias
, argp
, sizeof alias
))
1382 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1387 case KVM_CREATE_IRQCHIP
:
1389 kvm
->vpic
= kvm_create_pic(kvm
);
1391 r
= kvm_ioapic_init(kvm
);
1400 case KVM_IRQ_LINE
: {
1401 struct kvm_irq_level irq_event
;
1404 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1406 if (irqchip_in_kernel(kvm
)) {
1407 mutex_lock(&kvm
->lock
);
1408 if (irq_event
.irq
< 16)
1409 kvm_pic_set_irq(pic_irqchip(kvm
),
1412 kvm_ioapic_set_irq(kvm
->vioapic
,
1415 mutex_unlock(&kvm
->lock
);
1420 case KVM_GET_IRQCHIP
: {
1421 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1422 struct kvm_irqchip chip
;
1425 if (copy_from_user(&chip
, argp
, sizeof chip
))
1428 if (!irqchip_in_kernel(kvm
))
1430 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1434 if (copy_to_user(argp
, &chip
, sizeof chip
))
1439 case KVM_SET_IRQCHIP
: {
1440 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1441 struct kvm_irqchip chip
;
1444 if (copy_from_user(&chip
, argp
, sizeof chip
))
1447 if (!irqchip_in_kernel(kvm
))
1449 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1455 case KVM_GET_SUPPORTED_CPUID
: {
1456 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1457 struct kvm_cpuid2 cpuid
;
1460 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1462 r
= kvm_vm_ioctl_get_supported_cpuid(kvm
, &cpuid
,
1463 cpuid_arg
->entries
);
1468 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1480 static void kvm_init_msr_list(void)
1485 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1486 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1489 msrs_to_save
[j
] = msrs_to_save
[i
];
1492 num_msrs_to_save
= j
;
1496 * Only apic need an MMIO device hook, so shortcut now..
1498 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1501 struct kvm_io_device
*dev
;
1503 if (vcpu
->arch
.apic
) {
1504 dev
= &vcpu
->arch
.apic
->dev
;
1505 if (dev
->in_range(dev
, addr
))
1512 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1515 struct kvm_io_device
*dev
;
1517 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1519 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1523 int emulator_read_std(unsigned long addr
,
1526 struct kvm_vcpu
*vcpu
)
1531 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1532 unsigned offset
= addr
& (PAGE_SIZE
-1);
1533 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1536 if (gpa
== UNMAPPED_GVA
)
1537 return X86EMUL_PROPAGATE_FAULT
;
1538 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1540 return X86EMUL_UNHANDLEABLE
;
1547 return X86EMUL_CONTINUE
;
1549 EXPORT_SYMBOL_GPL(emulator_read_std
);
1551 static int emulator_read_emulated(unsigned long addr
,
1554 struct kvm_vcpu
*vcpu
)
1556 struct kvm_io_device
*mmio_dev
;
1559 if (vcpu
->mmio_read_completed
) {
1560 memcpy(val
, vcpu
->mmio_data
, bytes
);
1561 vcpu
->mmio_read_completed
= 0;
1562 return X86EMUL_CONTINUE
;
1565 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1567 /* For APIC access vmexit */
1568 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1571 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1572 == X86EMUL_CONTINUE
)
1573 return X86EMUL_CONTINUE
;
1574 if (gpa
== UNMAPPED_GVA
)
1575 return X86EMUL_PROPAGATE_FAULT
;
1579 * Is this MMIO handled locally?
1581 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1583 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1584 return X86EMUL_CONTINUE
;
1587 vcpu
->mmio_needed
= 1;
1588 vcpu
->mmio_phys_addr
= gpa
;
1589 vcpu
->mmio_size
= bytes
;
1590 vcpu
->mmio_is_write
= 0;
1592 return X86EMUL_UNHANDLEABLE
;
1595 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1596 const void *val
, int bytes
)
1600 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1603 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1607 static int emulator_write_emulated_onepage(unsigned long addr
,
1610 struct kvm_vcpu
*vcpu
)
1612 struct kvm_io_device
*mmio_dev
;
1613 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1615 if (gpa
== UNMAPPED_GVA
) {
1616 kvm_inject_page_fault(vcpu
, addr
, 2);
1617 return X86EMUL_PROPAGATE_FAULT
;
1620 /* For APIC access vmexit */
1621 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1624 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1625 return X86EMUL_CONTINUE
;
1629 * Is this MMIO handled locally?
1631 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1633 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1634 return X86EMUL_CONTINUE
;
1637 vcpu
->mmio_needed
= 1;
1638 vcpu
->mmio_phys_addr
= gpa
;
1639 vcpu
->mmio_size
= bytes
;
1640 vcpu
->mmio_is_write
= 1;
1641 memcpy(vcpu
->mmio_data
, val
, bytes
);
1643 return X86EMUL_CONTINUE
;
1646 int emulator_write_emulated(unsigned long addr
,
1649 struct kvm_vcpu
*vcpu
)
1651 /* Crossing a page boundary? */
1652 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1655 now
= -addr
& ~PAGE_MASK
;
1656 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1657 if (rc
!= X86EMUL_CONTINUE
)
1663 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1665 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1667 static int emulator_cmpxchg_emulated(unsigned long addr
,
1671 struct kvm_vcpu
*vcpu
)
1673 static int reported
;
1677 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1679 #ifndef CONFIG_X86_64
1680 /* guests cmpxchg8b have to be emulated atomically */
1682 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1687 if (gpa
== UNMAPPED_GVA
||
1688 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1691 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
1695 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1696 addr
= kmap_atomic(page
, KM_USER0
);
1697 set_64bit((u64
*)(addr
+ offset_in_page(gpa
)), val
);
1698 kunmap_atomic(addr
, KM_USER0
);
1699 kvm_release_page_dirty(page
);
1704 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1707 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1709 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1712 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1714 return X86EMUL_CONTINUE
;
1717 int emulate_clts(struct kvm_vcpu
*vcpu
)
1719 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
1720 return X86EMUL_CONTINUE
;
1723 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1725 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1729 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1730 return X86EMUL_CONTINUE
;
1732 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __FUNCTION__
, dr
);
1733 return X86EMUL_UNHANDLEABLE
;
1737 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1739 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1742 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1744 /* FIXME: better handling */
1745 return X86EMUL_UNHANDLEABLE
;
1747 return X86EMUL_CONTINUE
;
1750 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
1752 static int reported
;
1754 unsigned long rip
= vcpu
->arch
.rip
;
1755 unsigned long rip_linear
;
1757 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
1762 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
1764 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1765 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1768 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
1770 struct x86_emulate_ops emulate_ops
= {
1771 .read_std
= emulator_read_std
,
1772 .read_emulated
= emulator_read_emulated
,
1773 .write_emulated
= emulator_write_emulated
,
1774 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1777 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1778 struct kvm_run
*run
,
1785 vcpu
->arch
.mmio_fault_cr2
= cr2
;
1786 kvm_x86_ops
->cache_regs(vcpu
);
1788 vcpu
->mmio_is_write
= 0;
1789 vcpu
->arch
.pio
.string
= 0;
1793 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1795 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
1796 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
1797 vcpu
->arch
.emulate_ctxt
.mode
=
1798 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1799 ? X86EMUL_MODE_REAL
: cs_l
1800 ? X86EMUL_MODE_PROT64
: cs_db
1801 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
1803 if (vcpu
->arch
.emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
1804 vcpu
->arch
.emulate_ctxt
.cs_base
= 0;
1805 vcpu
->arch
.emulate_ctxt
.ds_base
= 0;
1806 vcpu
->arch
.emulate_ctxt
.es_base
= 0;
1807 vcpu
->arch
.emulate_ctxt
.ss_base
= 0;
1809 vcpu
->arch
.emulate_ctxt
.cs_base
=
1810 get_segment_base(vcpu
, VCPU_SREG_CS
);
1811 vcpu
->arch
.emulate_ctxt
.ds_base
=
1812 get_segment_base(vcpu
, VCPU_SREG_DS
);
1813 vcpu
->arch
.emulate_ctxt
.es_base
=
1814 get_segment_base(vcpu
, VCPU_SREG_ES
);
1815 vcpu
->arch
.emulate_ctxt
.ss_base
=
1816 get_segment_base(vcpu
, VCPU_SREG_SS
);
1819 vcpu
->arch
.emulate_ctxt
.gs_base
=
1820 get_segment_base(vcpu
, VCPU_SREG_GS
);
1821 vcpu
->arch
.emulate_ctxt
.fs_base
=
1822 get_segment_base(vcpu
, VCPU_SREG_FS
);
1824 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
1825 ++vcpu
->stat
.insn_emulation
;
1827 ++vcpu
->stat
.insn_emulation_fail
;
1828 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1829 return EMULATE_DONE
;
1830 return EMULATE_FAIL
;
1834 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
1836 if (vcpu
->arch
.pio
.string
)
1837 return EMULATE_DO_MMIO
;
1839 if ((r
|| vcpu
->mmio_is_write
) && run
) {
1840 run
->exit_reason
= KVM_EXIT_MMIO
;
1841 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
1842 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
1843 run
->mmio
.len
= vcpu
->mmio_size
;
1844 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
1848 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
1849 return EMULATE_DONE
;
1850 if (!vcpu
->mmio_needed
) {
1851 kvm_report_emulation_failure(vcpu
, "mmio");
1852 return EMULATE_FAIL
;
1854 return EMULATE_DO_MMIO
;
1857 kvm_x86_ops
->decache_regs(vcpu
);
1858 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
1860 if (vcpu
->mmio_is_write
) {
1861 vcpu
->mmio_needed
= 0;
1862 return EMULATE_DO_MMIO
;
1865 return EMULATE_DONE
;
1867 EXPORT_SYMBOL_GPL(emulate_instruction
);
1869 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
1873 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
1874 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
1875 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
1876 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
1880 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
1882 void *p
= vcpu
->arch
.pio_data
;
1885 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
1887 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
1890 free_pio_guest_pages(vcpu
);
1893 q
+= vcpu
->arch
.pio
.guest_page_offset
;
1894 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
1895 if (vcpu
->arch
.pio
.in
)
1896 memcpy(q
, p
, bytes
);
1898 memcpy(p
, q
, bytes
);
1899 q
-= vcpu
->arch
.pio
.guest_page_offset
;
1901 free_pio_guest_pages(vcpu
);
1905 int complete_pio(struct kvm_vcpu
*vcpu
)
1907 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
1911 kvm_x86_ops
->cache_regs(vcpu
);
1915 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
1919 r
= pio_copy_data(vcpu
);
1921 kvm_x86_ops
->cache_regs(vcpu
);
1928 delta
*= io
->cur_count
;
1930 * The size of the register should really depend on
1931 * current address size.
1933 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
1939 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
1941 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
1944 kvm_x86_ops
->decache_regs(vcpu
);
1946 io
->count
-= io
->cur_count
;
1952 static void kernel_pio(struct kvm_io_device
*pio_dev
,
1953 struct kvm_vcpu
*vcpu
,
1956 /* TODO: String I/O for in kernel device */
1958 mutex_lock(&vcpu
->kvm
->lock
);
1959 if (vcpu
->arch
.pio
.in
)
1960 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
1961 vcpu
->arch
.pio
.size
,
1964 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
1965 vcpu
->arch
.pio
.size
,
1967 mutex_unlock(&vcpu
->kvm
->lock
);
1970 static void pio_string_write(struct kvm_io_device
*pio_dev
,
1971 struct kvm_vcpu
*vcpu
)
1973 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
1974 void *pd
= vcpu
->arch
.pio_data
;
1977 mutex_lock(&vcpu
->kvm
->lock
);
1978 for (i
= 0; i
< io
->cur_count
; i
++) {
1979 kvm_iodevice_write(pio_dev
, io
->port
,
1984 mutex_unlock(&vcpu
->kvm
->lock
);
1987 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
1990 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
1993 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
1994 int size
, unsigned port
)
1996 struct kvm_io_device
*pio_dev
;
1998 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
1999 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2000 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2001 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2002 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2003 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2004 vcpu
->arch
.pio
.in
= in
;
2005 vcpu
->arch
.pio
.string
= 0;
2006 vcpu
->arch
.pio
.down
= 0;
2007 vcpu
->arch
.pio
.guest_page_offset
= 0;
2008 vcpu
->arch
.pio
.rep
= 0;
2010 kvm_x86_ops
->cache_regs(vcpu
);
2011 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2012 kvm_x86_ops
->decache_regs(vcpu
);
2014 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2016 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2018 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2024 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2026 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2027 int size
, unsigned long count
, int down
,
2028 gva_t address
, int rep
, unsigned port
)
2030 unsigned now
, in_page
;
2034 struct kvm_io_device
*pio_dev
;
2036 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2037 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2038 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2039 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2040 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2041 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2042 vcpu
->arch
.pio
.in
= in
;
2043 vcpu
->arch
.pio
.string
= 1;
2044 vcpu
->arch
.pio
.down
= down
;
2045 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2046 vcpu
->arch
.pio
.rep
= rep
;
2049 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2054 in_page
= PAGE_SIZE
- offset_in_page(address
);
2056 in_page
= offset_in_page(address
) + size
;
2057 now
= min(count
, (unsigned long)in_page
/ size
);
2060 * String I/O straddles page boundary. Pin two guest pages
2061 * so that we satisfy atomicity constraints. Do just one
2062 * transaction to avoid complexity.
2069 * String I/O in reverse. Yuck. Kill the guest, fix later.
2071 pr_unimpl(vcpu
, "guest string pio down\n");
2072 kvm_inject_gp(vcpu
, 0);
2075 vcpu
->run
->io
.count
= now
;
2076 vcpu
->arch
.pio
.cur_count
= now
;
2078 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2079 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2081 for (i
= 0; i
< nr_pages
; ++i
) {
2082 mutex_lock(&vcpu
->kvm
->lock
);
2083 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2084 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2085 mutex_unlock(&vcpu
->kvm
->lock
);
2087 kvm_inject_gp(vcpu
, 0);
2088 free_pio_guest_pages(vcpu
);
2093 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2094 if (!vcpu
->arch
.pio
.in
) {
2095 /* string PIO write */
2096 ret
= pio_copy_data(vcpu
);
2097 if (ret
>= 0 && pio_dev
) {
2098 pio_string_write(pio_dev
, vcpu
);
2100 if (vcpu
->arch
.pio
.count
== 0)
2104 pr_unimpl(vcpu
, "no string pio read support yet, "
2105 "port %x size %d count %ld\n",
2110 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2112 int kvm_arch_init(void *opaque
)
2115 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2117 r
= kvm_mmu_module_init();
2121 kvm_init_msr_list();
2124 printk(KERN_ERR
"kvm: already loaded the other module\n");
2129 if (!ops
->cpu_has_kvm_support()) {
2130 printk(KERN_ERR
"kvm: no hardware support\n");
2134 if (ops
->disabled_by_bios()) {
2135 printk(KERN_ERR
"kvm: disabled by bios\n");
2141 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2145 kvm_mmu_module_exit();
2150 void kvm_arch_exit(void)
2153 kvm_mmu_module_exit();
2156 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2158 ++vcpu
->stat
.halt_exits
;
2159 if (irqchip_in_kernel(vcpu
->kvm
)) {
2160 vcpu
->arch
.mp_state
= VCPU_MP_STATE_HALTED
;
2161 kvm_vcpu_block(vcpu
);
2162 if (vcpu
->arch
.mp_state
!= VCPU_MP_STATE_RUNNABLE
)
2166 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2170 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2172 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2174 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2176 kvm_x86_ops
->cache_regs(vcpu
);
2178 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2179 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2180 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2181 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2182 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2184 if (!is_long_mode(vcpu
)) {
2197 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2198 kvm_x86_ops
->decache_regs(vcpu
);
2201 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2203 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2205 char instruction
[3];
2208 mutex_lock(&vcpu
->kvm
->lock
);
2211 * Blow out the MMU to ensure that no other VCPU has an active mapping
2212 * to ensure that the updated hypercall appears atomically across all
2215 kvm_mmu_zap_all(vcpu
->kvm
);
2217 kvm_x86_ops
->cache_regs(vcpu
);
2218 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2219 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2220 != X86EMUL_CONTINUE
)
2223 mutex_unlock(&vcpu
->kvm
->lock
);
2228 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2230 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2233 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2235 struct descriptor_table dt
= { limit
, base
};
2237 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2240 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2242 struct descriptor_table dt
= { limit
, base
};
2244 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2247 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2248 unsigned long *rflags
)
2251 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2254 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2256 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2259 return vcpu
->arch
.cr0
;
2261 return vcpu
->arch
.cr2
;
2263 return vcpu
->arch
.cr3
;
2265 return vcpu
->arch
.cr4
;
2267 return get_cr8(vcpu
);
2269 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2274 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2275 unsigned long *rflags
)
2279 set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2280 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2283 vcpu
->arch
.cr2
= val
;
2289 set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2292 set_cr8(vcpu
, val
& 0xfUL
);
2295 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2299 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2301 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2302 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2304 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2305 /* when no next entry is found, the current entry[i] is reselected */
2306 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2307 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2308 if (ej
->function
== e
->function
) {
2309 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2313 return 0; /* silence gcc, even though control never reaches here */
2316 /* find an entry with matching function, matching index (if needed), and that
2317 * should be read next (if it's stateful) */
2318 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2319 u32 function
, u32 index
)
2321 if (e
->function
!= function
)
2323 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2325 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2326 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2331 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2334 u32 function
, index
;
2335 struct kvm_cpuid_entry2
*e
, *best
;
2337 kvm_x86_ops
->cache_regs(vcpu
);
2338 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2339 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2340 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2341 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2342 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2343 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2345 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2346 e
= &vcpu
->arch
.cpuid_entries
[i
];
2347 if (is_matching_cpuid_entry(e
, function
, index
)) {
2348 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2349 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2354 * Both basic or both extended?
2356 if (((e
->function
^ function
) & 0x80000000) == 0)
2357 if (!best
|| e
->function
> best
->function
)
2361 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2362 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2363 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2364 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2366 kvm_x86_ops
->decache_regs(vcpu
);
2367 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2369 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2372 * Check if userspace requested an interrupt window, and that the
2373 * interrupt window is open.
2375 * No need to exit to userspace if we already have an interrupt queued.
2377 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2378 struct kvm_run
*kvm_run
)
2380 return (!vcpu
->arch
.irq_summary
&&
2381 kvm_run
->request_interrupt_window
&&
2382 vcpu
->arch
.interrupt_window_open
&&
2383 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2386 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2387 struct kvm_run
*kvm_run
)
2389 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2390 kvm_run
->cr8
= get_cr8(vcpu
);
2391 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2392 if (irqchip_in_kernel(vcpu
->kvm
))
2393 kvm_run
->ready_for_interrupt_injection
= 1;
2395 kvm_run
->ready_for_interrupt_injection
=
2396 (vcpu
->arch
.interrupt_window_open
&&
2397 vcpu
->arch
.irq_summary
== 0);
2400 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2404 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2405 pr_debug("vcpu %d received sipi with vector # %x\n",
2406 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2407 kvm_lapic_reset(vcpu
);
2408 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2411 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
2415 if (vcpu
->guest_debug
.enabled
)
2416 kvm_x86_ops
->guest_debug_pre(vcpu
);
2419 r
= kvm_mmu_reload(vcpu
);
2423 kvm_inject_pending_timer_irqs(vcpu
);
2427 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2428 kvm_load_guest_fpu(vcpu
);
2430 local_irq_disable();
2432 if (signal_pending(current
)) {
2436 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2437 ++vcpu
->stat
.signal_exits
;
2441 if (vcpu
->arch
.exception
.pending
)
2442 __queue_exception(vcpu
);
2443 else if (irqchip_in_kernel(vcpu
->kvm
))
2444 kvm_x86_ops
->inject_pending_irq(vcpu
);
2446 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2448 vcpu
->guest_mode
= 1;
2452 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2453 kvm_x86_ops
->tlb_flush(vcpu
);
2455 kvm_x86_ops
->run(vcpu
, kvm_run
);
2457 vcpu
->guest_mode
= 0;
2463 * We must have an instruction between local_irq_enable() and
2464 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2465 * the interrupt shadow. The stat.exits increment will do nicely.
2466 * But we need to prevent reordering, hence this barrier():
2475 * Profile KVM exit RIPs:
2477 if (unlikely(prof_on
== KVM_PROFILING
)) {
2478 kvm_x86_ops
->cache_regs(vcpu
);
2479 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2482 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2483 vcpu
->arch
.exception
.pending
= false;
2485 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2488 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2490 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2491 ++vcpu
->stat
.request_irq_exits
;
2494 if (!need_resched())
2504 post_kvm_run_save(vcpu
, kvm_run
);
2509 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2516 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2517 kvm_vcpu_block(vcpu
);
2522 if (vcpu
->sigset_active
)
2523 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2525 /* re-sync apic's tpr */
2526 if (!irqchip_in_kernel(vcpu
->kvm
))
2527 set_cr8(vcpu
, kvm_run
->cr8
);
2529 if (vcpu
->arch
.pio
.cur_count
) {
2530 r
= complete_pio(vcpu
);
2534 #if CONFIG_HAS_IOMEM
2535 if (vcpu
->mmio_needed
) {
2536 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2537 vcpu
->mmio_read_completed
= 1;
2538 vcpu
->mmio_needed
= 0;
2539 r
= emulate_instruction(vcpu
, kvm_run
,
2540 vcpu
->arch
.mmio_fault_cr2
, 0, 1);
2541 if (r
== EMULATE_DO_MMIO
) {
2543 * Read-modify-write. Back to userspace.
2550 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2551 kvm_x86_ops
->cache_regs(vcpu
);
2552 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2553 kvm_x86_ops
->decache_regs(vcpu
);
2556 r
= __vcpu_run(vcpu
, kvm_run
);
2559 if (vcpu
->sigset_active
)
2560 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2566 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2570 kvm_x86_ops
->cache_regs(vcpu
);
2572 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2573 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2574 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2575 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2576 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2577 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
2578 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2579 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
2580 #ifdef CONFIG_X86_64
2581 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
2582 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
2583 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
2584 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
2585 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
2586 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
2587 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
2588 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
2591 regs
->rip
= vcpu
->arch
.rip
;
2592 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2595 * Don't leak debug flags in case they were set for guest debugging
2597 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2598 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2605 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2609 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
2610 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2611 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2612 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2613 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2614 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2615 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2616 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2617 #ifdef CONFIG_X86_64
2618 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
2619 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
2620 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
2621 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
2622 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
2623 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
2624 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
2625 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
2628 vcpu
->arch
.rip
= regs
->rip
;
2629 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2631 kvm_x86_ops
->decache_regs(vcpu
);
2638 static void get_segment(struct kvm_vcpu
*vcpu
,
2639 struct kvm_segment
*var
, int seg
)
2641 return kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2644 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2646 struct kvm_segment cs
;
2648 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2652 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
2654 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2655 struct kvm_sregs
*sregs
)
2657 struct descriptor_table dt
;
2662 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2663 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2664 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2665 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2666 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2667 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2669 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2670 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2672 kvm_x86_ops
->get_idt(vcpu
, &dt
);
2673 sregs
->idt
.limit
= dt
.limit
;
2674 sregs
->idt
.base
= dt
.base
;
2675 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
2676 sregs
->gdt
.limit
= dt
.limit
;
2677 sregs
->gdt
.base
= dt
.base
;
2679 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2680 sregs
->cr0
= vcpu
->arch
.cr0
;
2681 sregs
->cr2
= vcpu
->arch
.cr2
;
2682 sregs
->cr3
= vcpu
->arch
.cr3
;
2683 sregs
->cr4
= vcpu
->arch
.cr4
;
2684 sregs
->cr8
= get_cr8(vcpu
);
2685 sregs
->efer
= vcpu
->arch
.shadow_efer
;
2686 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
2688 if (irqchip_in_kernel(vcpu
->kvm
)) {
2689 memset(sregs
->interrupt_bitmap
, 0,
2690 sizeof sregs
->interrupt_bitmap
);
2691 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
2692 if (pending_vec
>= 0)
2693 set_bit(pending_vec
,
2694 (unsigned long *)sregs
->interrupt_bitmap
);
2696 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
2697 sizeof sregs
->interrupt_bitmap
);
2704 static void set_segment(struct kvm_vcpu
*vcpu
,
2705 struct kvm_segment
*var
, int seg
)
2707 return kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
2710 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2711 struct kvm_sregs
*sregs
)
2713 int mmu_reset_needed
= 0;
2714 int i
, pending_vec
, max_bits
;
2715 struct descriptor_table dt
;
2719 dt
.limit
= sregs
->idt
.limit
;
2720 dt
.base
= sregs
->idt
.base
;
2721 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2722 dt
.limit
= sregs
->gdt
.limit
;
2723 dt
.base
= sregs
->gdt
.base
;
2724 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2726 vcpu
->arch
.cr2
= sregs
->cr2
;
2727 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
2728 vcpu
->arch
.cr3
= sregs
->cr3
;
2730 set_cr8(vcpu
, sregs
->cr8
);
2732 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
2733 #ifdef CONFIG_X86_64
2734 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
2736 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
2738 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2740 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
2741 vcpu
->arch
.cr0
= sregs
->cr0
;
2742 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
2744 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
2745 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
2746 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
2747 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
2749 if (mmu_reset_needed
)
2750 kvm_mmu_reset_context(vcpu
);
2752 if (!irqchip_in_kernel(vcpu
->kvm
)) {
2753 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
2754 sizeof vcpu
->arch
.irq_pending
);
2755 vcpu
->arch
.irq_summary
= 0;
2756 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
2757 if (vcpu
->arch
.irq_pending
[i
])
2758 __set_bit(i
, &vcpu
->arch
.irq_summary
);
2760 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
2761 pending_vec
= find_first_bit(
2762 (const unsigned long *)sregs
->interrupt_bitmap
,
2764 /* Only pending external irq is handled here */
2765 if (pending_vec
< max_bits
) {
2766 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
2767 pr_debug("Set back pending irq %d\n",
2772 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2773 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2774 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2775 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2776 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2777 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2779 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2780 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2787 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
2788 struct kvm_debug_guest
*dbg
)
2794 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
2802 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2803 * we have asm/x86/processor.h
2814 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2815 #ifdef CONFIG_X86_64
2816 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2818 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2823 * Translate a guest virtual address to a guest physical address.
2825 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2826 struct kvm_translation
*tr
)
2828 unsigned long vaddr
= tr
->linear_address
;
2832 mutex_lock(&vcpu
->kvm
->lock
);
2833 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
2834 tr
->physical_address
= gpa
;
2835 tr
->valid
= gpa
!= UNMAPPED_GVA
;
2838 mutex_unlock(&vcpu
->kvm
->lock
);
2844 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2846 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
2850 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
2851 fpu
->fcw
= fxsave
->cwd
;
2852 fpu
->fsw
= fxsave
->swd
;
2853 fpu
->ftwx
= fxsave
->twd
;
2854 fpu
->last_opcode
= fxsave
->fop
;
2855 fpu
->last_ip
= fxsave
->rip
;
2856 fpu
->last_dp
= fxsave
->rdp
;
2857 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
2864 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2866 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
2870 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
2871 fxsave
->cwd
= fpu
->fcw
;
2872 fxsave
->swd
= fpu
->fsw
;
2873 fxsave
->twd
= fpu
->ftwx
;
2874 fxsave
->fop
= fpu
->last_opcode
;
2875 fxsave
->rip
= fpu
->last_ip
;
2876 fxsave
->rdp
= fpu
->last_dp
;
2877 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
2884 void fx_init(struct kvm_vcpu
*vcpu
)
2886 unsigned after_mxcsr_mask
;
2888 /* Initialize guest FPU by resetting ours and saving into guest's */
2890 fx_save(&vcpu
->arch
.host_fx_image
);
2892 fx_save(&vcpu
->arch
.guest_fx_image
);
2893 fx_restore(&vcpu
->arch
.host_fx_image
);
2896 vcpu
->arch
.cr0
|= X86_CR0_ET
;
2897 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
2898 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
2899 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
2900 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
2902 EXPORT_SYMBOL_GPL(fx_init
);
2904 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
2906 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
2909 vcpu
->guest_fpu_loaded
= 1;
2910 fx_save(&vcpu
->arch
.host_fx_image
);
2911 fx_restore(&vcpu
->arch
.guest_fx_image
);
2913 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
2915 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
2917 if (!vcpu
->guest_fpu_loaded
)
2920 vcpu
->guest_fpu_loaded
= 0;
2921 fx_save(&vcpu
->arch
.guest_fx_image
);
2922 fx_restore(&vcpu
->arch
.host_fx_image
);
2923 ++vcpu
->stat
.fpu_reload
;
2925 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
2927 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
2929 kvm_x86_ops
->vcpu_free(vcpu
);
2932 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
2935 return kvm_x86_ops
->vcpu_create(kvm
, id
);
2938 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
2942 /* We do fxsave: this must be aligned. */
2943 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
2946 r
= kvm_arch_vcpu_reset(vcpu
);
2948 r
= kvm_mmu_setup(vcpu
);
2955 kvm_x86_ops
->vcpu_free(vcpu
);
2959 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
2962 kvm_mmu_unload(vcpu
);
2965 kvm_x86_ops
->vcpu_free(vcpu
);
2968 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
2970 return kvm_x86_ops
->vcpu_reset(vcpu
);
2973 void kvm_arch_hardware_enable(void *garbage
)
2975 kvm_x86_ops
->hardware_enable(garbage
);
2978 void kvm_arch_hardware_disable(void *garbage
)
2980 kvm_x86_ops
->hardware_disable(garbage
);
2983 int kvm_arch_hardware_setup(void)
2985 return kvm_x86_ops
->hardware_setup();
2988 void kvm_arch_hardware_unsetup(void)
2990 kvm_x86_ops
->hardware_unsetup();
2993 void kvm_arch_check_processor_compat(void *rtn
)
2995 kvm_x86_ops
->check_processor_compatibility(rtn
);
2998 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3004 BUG_ON(vcpu
->kvm
== NULL
);
3007 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3008 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3009 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
3011 vcpu
->arch
.mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
3013 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3018 vcpu
->arch
.pio_data
= page_address(page
);
3020 r
= kvm_mmu_create(vcpu
);
3022 goto fail_free_pio_data
;
3024 if (irqchip_in_kernel(kvm
)) {
3025 r
= kvm_create_lapic(vcpu
);
3027 goto fail_mmu_destroy
;
3033 kvm_mmu_destroy(vcpu
);
3035 free_page((unsigned long)vcpu
->arch
.pio_data
);
3040 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3042 kvm_free_lapic(vcpu
);
3043 kvm_mmu_destroy(vcpu
);
3044 free_page((unsigned long)vcpu
->arch
.pio_data
);
3047 struct kvm
*kvm_arch_create_vm(void)
3049 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3052 return ERR_PTR(-ENOMEM
);
3054 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3059 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3062 kvm_mmu_unload(vcpu
);
3066 static void kvm_free_vcpus(struct kvm
*kvm
)
3071 * Unpin any mmu pages first.
3073 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3075 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3076 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3077 if (kvm
->vcpus
[i
]) {
3078 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3079 kvm
->vcpus
[i
] = NULL
;
3085 void kvm_arch_destroy_vm(struct kvm
*kvm
)
3088 kfree(kvm
->vioapic
);
3089 kvm_free_vcpus(kvm
);
3090 kvm_free_physmem(kvm
);
3094 int kvm_arch_set_memory_region(struct kvm
*kvm
,
3095 struct kvm_userspace_memory_region
*mem
,
3096 struct kvm_memory_slot old
,
3099 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
3100 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
3102 /*To keep backward compatibility with older userspace,
3103 *x86 needs to hanlde !user_alloc case.
3106 if (npages
&& !old
.rmap
) {
3107 down_write(¤t
->mm
->mmap_sem
);
3108 memslot
->userspace_addr
= do_mmap(NULL
, 0,
3110 PROT_READ
| PROT_WRITE
,
3111 MAP_SHARED
| MAP_ANONYMOUS
,
3113 up_write(¤t
->mm
->mmap_sem
);
3115 if (IS_ERR((void *)memslot
->userspace_addr
))
3116 return PTR_ERR((void *)memslot
->userspace_addr
);
3118 if (!old
.user_alloc
&& old
.rmap
) {
3121 down_write(¤t
->mm
->mmap_sem
);
3122 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
3123 old
.npages
* PAGE_SIZE
);
3124 up_write(¤t
->mm
->mmap_sem
);
3127 "kvm_vm_ioctl_set_memory_region: "
3128 "failed to munmap memory\n");
3133 if (!kvm
->arch
.n_requested_mmu_pages
) {
3134 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
3135 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
3138 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
3139 kvm_flush_remote_tlbs(kvm
);
3144 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
3146 return vcpu
->arch
.mp_state
== VCPU_MP_STATE_RUNNABLE
3147 || vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
;