2 * Kernel-based Virtual Machine driver for Linux
4 * derived from drivers/kvm/kvm_main.c
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
21 #include <linux/kvm_host.h>
26 #include "kvm_cache_regs.h"
29 #include <linux/clocksource.h>
30 #include <linux/interrupt.h>
31 #include <linux/kvm.h>
33 #include <linux/vmalloc.h>
34 #include <linux/module.h>
35 #include <linux/mman.h>
36 #include <linux/highmem.h>
37 #include <linux/iommu.h>
38 #include <linux/intel-iommu.h>
39 #include <linux/cpufreq.h>
40 #include <linux/user-return-notifier.h>
41 #include <linux/srcu.h>
42 #include <linux/slab.h>
43 #include <linux/perf_event.h>
44 #include <trace/events/kvm.h>
46 #define CREATE_TRACE_POINTS
49 #include <asm/debugreg.h>
50 #include <asm/uaccess.h>
56 #define MAX_IO_MSRS 256
57 #define CR0_RESERVED_BITS \
58 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
59 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
60 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
61 #define CR4_RESERVED_BITS \
62 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
63 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
64 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
65 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
67 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
69 #define KVM_MAX_MCE_BANKS 32
70 #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
73 * - enable syscall per default because its emulated by KVM
74 * - enable LME and LMA per default on 64 bit KVM
77 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffafeULL
;
79 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffffeULL
;
82 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
83 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
85 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
);
86 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
87 struct kvm_cpuid_entry2 __user
*entries
);
89 struct kvm_x86_ops
*kvm_x86_ops
;
90 EXPORT_SYMBOL_GPL(kvm_x86_ops
);
93 module_param_named(ignore_msrs
, ignore_msrs
, bool, S_IRUGO
| S_IWUSR
);
95 #define KVM_NR_SHARED_MSRS 16
97 struct kvm_shared_msrs_global
{
99 u32 msrs
[KVM_NR_SHARED_MSRS
];
102 struct kvm_shared_msrs
{
103 struct user_return_notifier urn
;
105 struct kvm_shared_msr_values
{
108 } values
[KVM_NR_SHARED_MSRS
];
111 static struct kvm_shared_msrs_global __read_mostly shared_msrs_global
;
112 static DEFINE_PER_CPU(struct kvm_shared_msrs
, shared_msrs
);
114 struct kvm_stats_debugfs_item debugfs_entries
[] = {
115 { "pf_fixed", VCPU_STAT(pf_fixed
) },
116 { "pf_guest", VCPU_STAT(pf_guest
) },
117 { "tlb_flush", VCPU_STAT(tlb_flush
) },
118 { "invlpg", VCPU_STAT(invlpg
) },
119 { "exits", VCPU_STAT(exits
) },
120 { "io_exits", VCPU_STAT(io_exits
) },
121 { "mmio_exits", VCPU_STAT(mmio_exits
) },
122 { "signal_exits", VCPU_STAT(signal_exits
) },
123 { "irq_window", VCPU_STAT(irq_window_exits
) },
124 { "nmi_window", VCPU_STAT(nmi_window_exits
) },
125 { "halt_exits", VCPU_STAT(halt_exits
) },
126 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
127 { "hypercalls", VCPU_STAT(hypercalls
) },
128 { "request_irq", VCPU_STAT(request_irq_exits
) },
129 { "irq_exits", VCPU_STAT(irq_exits
) },
130 { "host_state_reload", VCPU_STAT(host_state_reload
) },
131 { "efer_reload", VCPU_STAT(efer_reload
) },
132 { "fpu_reload", VCPU_STAT(fpu_reload
) },
133 { "insn_emulation", VCPU_STAT(insn_emulation
) },
134 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
135 { "irq_injections", VCPU_STAT(irq_injections
) },
136 { "nmi_injections", VCPU_STAT(nmi_injections
) },
137 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
138 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
139 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
140 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
141 { "mmu_flooded", VM_STAT(mmu_flooded
) },
142 { "mmu_recycled", VM_STAT(mmu_recycled
) },
143 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
144 { "mmu_unsync", VM_STAT(mmu_unsync
) },
145 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
146 { "largepages", VM_STAT(lpages
) },
150 static void kvm_on_user_return(struct user_return_notifier
*urn
)
153 struct kvm_shared_msrs
*locals
154 = container_of(urn
, struct kvm_shared_msrs
, urn
);
155 struct kvm_shared_msr_values
*values
;
157 for (slot
= 0; slot
< shared_msrs_global
.nr
; ++slot
) {
158 values
= &locals
->values
[slot
];
159 if (values
->host
!= values
->curr
) {
160 wrmsrl(shared_msrs_global
.msrs
[slot
], values
->host
);
161 values
->curr
= values
->host
;
164 locals
->registered
= false;
165 user_return_notifier_unregister(urn
);
168 static void shared_msr_update(unsigned slot
, u32 msr
)
170 struct kvm_shared_msrs
*smsr
;
173 smsr
= &__get_cpu_var(shared_msrs
);
174 /* only read, and nobody should modify it at this time,
175 * so don't need lock */
176 if (slot
>= shared_msrs_global
.nr
) {
177 printk(KERN_ERR
"kvm: invalid MSR slot!");
180 rdmsrl_safe(msr
, &value
);
181 smsr
->values
[slot
].host
= value
;
182 smsr
->values
[slot
].curr
= value
;
185 void kvm_define_shared_msr(unsigned slot
, u32 msr
)
187 if (slot
>= shared_msrs_global
.nr
)
188 shared_msrs_global
.nr
= slot
+ 1;
189 shared_msrs_global
.msrs
[slot
] = msr
;
190 /* we need ensured the shared_msr_global have been updated */
193 EXPORT_SYMBOL_GPL(kvm_define_shared_msr
);
195 static void kvm_shared_msr_cpu_online(void)
199 for (i
= 0; i
< shared_msrs_global
.nr
; ++i
)
200 shared_msr_update(i
, shared_msrs_global
.msrs
[i
]);
203 void kvm_set_shared_msr(unsigned slot
, u64 value
, u64 mask
)
205 struct kvm_shared_msrs
*smsr
= &__get_cpu_var(shared_msrs
);
207 if (((value
^ smsr
->values
[slot
].curr
) & mask
) == 0)
209 smsr
->values
[slot
].curr
= value
;
210 wrmsrl(shared_msrs_global
.msrs
[slot
], value
);
211 if (!smsr
->registered
) {
212 smsr
->urn
.on_user_return
= kvm_on_user_return
;
213 user_return_notifier_register(&smsr
->urn
);
214 smsr
->registered
= true;
217 EXPORT_SYMBOL_GPL(kvm_set_shared_msr
);
219 static void drop_user_return_notifiers(void *ignore
)
221 struct kvm_shared_msrs
*smsr
= &__get_cpu_var(shared_msrs
);
223 if (smsr
->registered
)
224 kvm_on_user_return(&smsr
->urn
);
227 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
229 if (irqchip_in_kernel(vcpu
->kvm
))
230 return vcpu
->arch
.apic_base
;
232 return vcpu
->arch
.apic_base
;
234 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
236 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
238 /* TODO: reserve bits check */
239 if (irqchip_in_kernel(vcpu
->kvm
))
240 kvm_lapic_set_base(vcpu
, data
);
242 vcpu
->arch
.apic_base
= data
;
244 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
246 #define EXCPT_BENIGN 0
247 #define EXCPT_CONTRIBUTORY 1
250 static int exception_class(int vector
)
260 return EXCPT_CONTRIBUTORY
;
267 static void kvm_multiple_exception(struct kvm_vcpu
*vcpu
,
268 unsigned nr
, bool has_error
, u32 error_code
,
274 if (!vcpu
->arch
.exception
.pending
) {
276 vcpu
->arch
.exception
.pending
= true;
277 vcpu
->arch
.exception
.has_error_code
= has_error
;
278 vcpu
->arch
.exception
.nr
= nr
;
279 vcpu
->arch
.exception
.error_code
= error_code
;
280 vcpu
->arch
.exception
.reinject
= true;
284 /* to check exception */
285 prev_nr
= vcpu
->arch
.exception
.nr
;
286 if (prev_nr
== DF_VECTOR
) {
287 /* triple fault -> shutdown */
288 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
291 class1
= exception_class(prev_nr
);
292 class2
= exception_class(nr
);
293 if ((class1
== EXCPT_CONTRIBUTORY
&& class2
== EXCPT_CONTRIBUTORY
)
294 || (class1
== EXCPT_PF
&& class2
!= EXCPT_BENIGN
)) {
295 /* generate double fault per SDM Table 5-5 */
296 vcpu
->arch
.exception
.pending
= true;
297 vcpu
->arch
.exception
.has_error_code
= true;
298 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
299 vcpu
->arch
.exception
.error_code
= 0;
301 /* replace previous exception with a new one in a hope
302 that instruction re-execution will regenerate lost
307 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
309 kvm_multiple_exception(vcpu
, nr
, false, 0, false);
311 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
313 void kvm_requeue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
315 kvm_multiple_exception(vcpu
, nr
, false, 0, true);
317 EXPORT_SYMBOL_GPL(kvm_requeue_exception
);
319 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
322 ++vcpu
->stat
.pf_guest
;
323 vcpu
->arch
.cr2
= addr
;
324 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
327 void kvm_inject_nmi(struct kvm_vcpu
*vcpu
)
329 vcpu
->arch
.nmi_pending
= 1;
331 EXPORT_SYMBOL_GPL(kvm_inject_nmi
);
333 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
335 kvm_multiple_exception(vcpu
, nr
, true, error_code
, false);
337 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
339 void kvm_requeue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
341 kvm_multiple_exception(vcpu
, nr
, true, error_code
, true);
343 EXPORT_SYMBOL_GPL(kvm_requeue_exception_e
);
346 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
347 * a #GP and return false.
349 bool kvm_require_cpl(struct kvm_vcpu
*vcpu
, int required_cpl
)
351 if (kvm_x86_ops
->get_cpl(vcpu
) <= required_cpl
)
353 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
356 EXPORT_SYMBOL_GPL(kvm_require_cpl
);
359 * Load the pae pdptrs. Return true is they are all valid.
361 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
363 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
364 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
367 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
369 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
370 offset
* sizeof(u64
), sizeof(pdpte
));
375 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
376 if (is_present_gpte(pdpte
[i
]) &&
377 (pdpte
[i
] & vcpu
->arch
.mmu
.rsvd_bits_mask
[0][2])) {
384 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
385 __set_bit(VCPU_EXREG_PDPTR
,
386 (unsigned long *)&vcpu
->arch
.regs_avail
);
387 __set_bit(VCPU_EXREG_PDPTR
,
388 (unsigned long *)&vcpu
->arch
.regs_dirty
);
393 EXPORT_SYMBOL_GPL(load_pdptrs
);
395 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
397 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
401 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
404 if (!test_bit(VCPU_EXREG_PDPTR
,
405 (unsigned long *)&vcpu
->arch
.regs_avail
))
408 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
411 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
417 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
422 if (cr0
& 0xffffffff00000000UL
) {
423 kvm_inject_gp(vcpu
, 0);
428 cr0
&= ~CR0_RESERVED_BITS
;
430 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
431 kvm_inject_gp(vcpu
, 0);
435 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
436 kvm_inject_gp(vcpu
, 0);
440 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
442 if ((vcpu
->arch
.efer
& EFER_LME
)) {
446 kvm_inject_gp(vcpu
, 0);
449 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
451 kvm_inject_gp(vcpu
, 0);
457 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
458 kvm_inject_gp(vcpu
, 0);
464 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
466 kvm_mmu_reset_context(vcpu
);
469 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
471 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
473 kvm_set_cr0(vcpu
, kvm_read_cr0_bits(vcpu
, ~0x0ful
) | (msw
& 0x0f));
475 EXPORT_SYMBOL_GPL(kvm_lmsw
);
477 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
479 unsigned long old_cr4
= kvm_read_cr4(vcpu
);
480 unsigned long pdptr_bits
= X86_CR4_PGE
| X86_CR4_PSE
| X86_CR4_PAE
;
482 if (cr4
& CR4_RESERVED_BITS
) {
483 kvm_inject_gp(vcpu
, 0);
487 if (is_long_mode(vcpu
)) {
488 if (!(cr4
& X86_CR4_PAE
)) {
489 kvm_inject_gp(vcpu
, 0);
492 } else if (is_paging(vcpu
) && (cr4
& X86_CR4_PAE
)
493 && ((cr4
^ old_cr4
) & pdptr_bits
)
494 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
495 kvm_inject_gp(vcpu
, 0);
499 if (cr4
& X86_CR4_VMXE
) {
500 kvm_inject_gp(vcpu
, 0);
503 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
504 vcpu
->arch
.cr4
= cr4
;
505 kvm_mmu_reset_context(vcpu
);
507 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
509 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
511 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
512 kvm_mmu_sync_roots(vcpu
);
513 kvm_mmu_flush_tlb(vcpu
);
517 if (is_long_mode(vcpu
)) {
518 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
519 kvm_inject_gp(vcpu
, 0);
524 if (cr3
& CR3_PAE_RESERVED_BITS
) {
525 kvm_inject_gp(vcpu
, 0);
528 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
529 kvm_inject_gp(vcpu
, 0);
534 * We don't check reserved bits in nonpae mode, because
535 * this isn't enforced, and VMware depends on this.
540 * Does the new cr3 value map to physical memory? (Note, we
541 * catch an invalid cr3 even in real-mode, because it would
542 * cause trouble later on when we turn on paging anyway.)
544 * A real CPU would silently accept an invalid cr3 and would
545 * attempt to use it - with largely undefined (and often hard
546 * to debug) behavior on the guest side.
548 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
549 kvm_inject_gp(vcpu
, 0);
551 vcpu
->arch
.cr3
= cr3
;
552 vcpu
->arch
.mmu
.new_cr3(vcpu
);
555 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
557 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
559 if (cr8
& CR8_RESERVED_BITS
) {
560 kvm_inject_gp(vcpu
, 0);
563 if (irqchip_in_kernel(vcpu
->kvm
))
564 kvm_lapic_set_tpr(vcpu
, cr8
);
566 vcpu
->arch
.cr8
= cr8
;
568 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
570 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
572 if (irqchip_in_kernel(vcpu
->kvm
))
573 return kvm_lapic_get_cr8(vcpu
);
575 return vcpu
->arch
.cr8
;
577 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
579 int kvm_set_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long val
)
583 vcpu
->arch
.db
[dr
] = val
;
584 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
585 vcpu
->arch
.eff_db
[dr
] = val
;
588 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
)) {
589 kvm_queue_exception(vcpu
, UD_VECTOR
);
594 if (val
& 0xffffffff00000000ULL
) {
595 kvm_inject_gp(vcpu
, 0);
598 vcpu
->arch
.dr6
= (val
& DR6_VOLATILE
) | DR6_FIXED_1
;
601 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
)) {
602 kvm_queue_exception(vcpu
, UD_VECTOR
);
607 if (val
& 0xffffffff00000000ULL
) {
608 kvm_inject_gp(vcpu
, 0);
611 vcpu
->arch
.dr7
= (val
& DR7_VOLATILE
) | DR7_FIXED_1
;
612 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
613 kvm_x86_ops
->set_dr7(vcpu
, vcpu
->arch
.dr7
);
614 vcpu
->arch
.switch_db_regs
= (val
& DR7_BP_EN_MASK
);
621 EXPORT_SYMBOL_GPL(kvm_set_dr
);
623 int kvm_get_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long *val
)
627 *val
= vcpu
->arch
.db
[dr
];
630 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
)) {
631 kvm_queue_exception(vcpu
, UD_VECTOR
);
636 *val
= vcpu
->arch
.dr6
;
639 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
)) {
640 kvm_queue_exception(vcpu
, UD_VECTOR
);
645 *val
= vcpu
->arch
.dr7
;
651 EXPORT_SYMBOL_GPL(kvm_get_dr
);
653 static inline u32
bit(int bitno
)
655 return 1 << (bitno
& 31);
659 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
660 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
662 * This list is modified at module load time to reflect the
663 * capabilities of the host cpu. This capabilities test skips MSRs that are
664 * kvm-specific. Those are put in the beginning of the list.
667 #define KVM_SAVE_MSRS_BEGIN 5
668 static u32 msrs_to_save
[] = {
669 MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
670 HV_X64_MSR_GUEST_OS_ID
, HV_X64_MSR_HYPERCALL
,
671 HV_X64_MSR_APIC_ASSIST_PAGE
,
672 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
675 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
677 MSR_IA32_TSC
, MSR_IA32_PERF_STATUS
, MSR_IA32_CR_PAT
, MSR_VM_HSAVE_PA
680 static unsigned num_msrs_to_save
;
682 static u32 emulated_msrs
[] = {
683 MSR_IA32_MISC_ENABLE
,
686 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
688 if (efer
& efer_reserved_bits
) {
689 kvm_inject_gp(vcpu
, 0);
694 && (vcpu
->arch
.efer
& EFER_LME
) != (efer
& EFER_LME
)) {
695 kvm_inject_gp(vcpu
, 0);
699 if (efer
& EFER_FFXSR
) {
700 struct kvm_cpuid_entry2
*feat
;
702 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
703 if (!feat
|| !(feat
->edx
& bit(X86_FEATURE_FXSR_OPT
))) {
704 kvm_inject_gp(vcpu
, 0);
709 if (efer
& EFER_SVME
) {
710 struct kvm_cpuid_entry2
*feat
;
712 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
713 if (!feat
|| !(feat
->ecx
& bit(X86_FEATURE_SVM
))) {
714 kvm_inject_gp(vcpu
, 0);
719 kvm_x86_ops
->set_efer(vcpu
, efer
);
722 efer
|= vcpu
->arch
.efer
& EFER_LMA
;
724 vcpu
->arch
.efer
= efer
;
726 vcpu
->arch
.mmu
.base_role
.nxe
= (efer
& EFER_NX
) && !tdp_enabled
;
727 kvm_mmu_reset_context(vcpu
);
730 void kvm_enable_efer_bits(u64 mask
)
732 efer_reserved_bits
&= ~mask
;
734 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
738 * Writes msr value into into the appropriate "register".
739 * Returns 0 on success, non-0 otherwise.
740 * Assumes vcpu_load() was already called.
742 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
744 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
748 * Adapt set_msr() to msr_io()'s calling convention
750 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
752 return kvm_set_msr(vcpu
, index
, *data
);
755 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
759 struct pvclock_wall_clock wc
;
760 struct timespec boot
;
765 r
= kvm_read_guest(kvm
, wall_clock
, &version
, sizeof(version
));
770 ++version
; /* first time write, random junk */
774 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
777 * The guest calculates current wall clock time by adding
778 * system time (updated by kvm_write_guest_time below) to the
779 * wall clock specified here. guest system time equals host
780 * system time for us, thus we must fill in host boot time here.
784 wc
.sec
= boot
.tv_sec
;
785 wc
.nsec
= boot
.tv_nsec
;
786 wc
.version
= version
;
788 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
791 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
794 static uint32_t div_frac(uint32_t dividend
, uint32_t divisor
)
796 uint32_t quotient
, remainder
;
798 /* Don't try to replace with do_div(), this one calculates
799 * "(dividend << 32) / divisor" */
801 : "=a" (quotient
), "=d" (remainder
)
802 : "0" (0), "1" (dividend
), "r" (divisor
) );
806 static void kvm_set_time_scale(uint32_t tsc_khz
, struct pvclock_vcpu_time_info
*hv_clock
)
808 uint64_t nsecs
= 1000000000LL;
813 tps64
= tsc_khz
* 1000LL;
814 while (tps64
> nsecs
*2) {
819 tps32
= (uint32_t)tps64
;
820 while (tps32
<= (uint32_t)nsecs
) {
825 hv_clock
->tsc_shift
= shift
;
826 hv_clock
->tsc_to_system_mul
= div_frac(nsecs
, tps32
);
828 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
829 __func__
, tsc_khz
, hv_clock
->tsc_shift
,
830 hv_clock
->tsc_to_system_mul
);
833 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz
);
835 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
839 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
841 unsigned long this_tsc_khz
;
843 if ((!vcpu
->time_page
))
846 this_tsc_khz
= get_cpu_var(cpu_tsc_khz
);
847 if (unlikely(vcpu
->hv_clock_tsc_khz
!= this_tsc_khz
)) {
848 kvm_set_time_scale(this_tsc_khz
, &vcpu
->hv_clock
);
849 vcpu
->hv_clock_tsc_khz
= this_tsc_khz
;
851 put_cpu_var(cpu_tsc_khz
);
853 /* Keep irq disabled to prevent changes to the clock */
854 local_irq_save(flags
);
855 kvm_get_msr(v
, MSR_IA32_TSC
, &vcpu
->hv_clock
.tsc_timestamp
);
857 monotonic_to_bootbased(&ts
);
858 local_irq_restore(flags
);
860 /* With all the info we got, fill in the values */
862 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
863 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
) + v
->kvm
->arch
.kvmclock_offset
;
866 * The interface expects us to write an even number signaling that the
867 * update is finished. Since the guest won't see the intermediate
868 * state, we just increase by 2 at the end.
870 vcpu
->hv_clock
.version
+= 2;
872 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
874 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
875 sizeof(vcpu
->hv_clock
));
877 kunmap_atomic(shared_kaddr
, KM_USER0
);
879 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
882 static int kvm_request_guest_time_update(struct kvm_vcpu
*v
)
884 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
886 if (!vcpu
->time_page
)
888 set_bit(KVM_REQ_KVMCLOCK_UPDATE
, &v
->requests
);
892 static bool msr_mtrr_valid(unsigned msr
)
895 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR
- 1:
896 case MSR_MTRRfix64K_00000
:
897 case MSR_MTRRfix16K_80000
:
898 case MSR_MTRRfix16K_A0000
:
899 case MSR_MTRRfix4K_C0000
:
900 case MSR_MTRRfix4K_C8000
:
901 case MSR_MTRRfix4K_D0000
:
902 case MSR_MTRRfix4K_D8000
:
903 case MSR_MTRRfix4K_E0000
:
904 case MSR_MTRRfix4K_E8000
:
905 case MSR_MTRRfix4K_F0000
:
906 case MSR_MTRRfix4K_F8000
:
907 case MSR_MTRRdefType
:
908 case MSR_IA32_CR_PAT
:
916 static bool valid_pat_type(unsigned t
)
918 return t
< 8 && (1 << t
) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
921 static bool valid_mtrr_type(unsigned t
)
923 return t
< 8 && (1 << t
) & 0x73; /* 0, 1, 4, 5, 6 */
926 static bool mtrr_valid(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
930 if (!msr_mtrr_valid(msr
))
933 if (msr
== MSR_IA32_CR_PAT
) {
934 for (i
= 0; i
< 8; i
++)
935 if (!valid_pat_type((data
>> (i
* 8)) & 0xff))
938 } else if (msr
== MSR_MTRRdefType
) {
941 return valid_mtrr_type(data
& 0xff);
942 } else if (msr
>= MSR_MTRRfix64K_00000
&& msr
<= MSR_MTRRfix4K_F8000
) {
943 for (i
= 0; i
< 8 ; i
++)
944 if (!valid_mtrr_type((data
>> (i
* 8)) & 0xff))
950 return valid_mtrr_type(data
& 0xff);
953 static int set_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
955 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
957 if (!mtrr_valid(vcpu
, msr
, data
))
960 if (msr
== MSR_MTRRdefType
) {
961 vcpu
->arch
.mtrr_state
.def_type
= data
;
962 vcpu
->arch
.mtrr_state
.enabled
= (data
& 0xc00) >> 10;
963 } else if (msr
== MSR_MTRRfix64K_00000
)
965 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
966 p
[1 + msr
- MSR_MTRRfix16K_80000
] = data
;
967 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
968 p
[3 + msr
- MSR_MTRRfix4K_C0000
] = data
;
969 else if (msr
== MSR_IA32_CR_PAT
)
970 vcpu
->arch
.pat
= data
;
971 else { /* Variable MTRRs */
972 int idx
, is_mtrr_mask
;
975 idx
= (msr
- 0x200) / 2;
976 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
979 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
982 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
986 kvm_mmu_reset_context(vcpu
);
990 static int set_msr_mce(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
992 u64 mcg_cap
= vcpu
->arch
.mcg_cap
;
993 unsigned bank_num
= mcg_cap
& 0xff;
996 case MSR_IA32_MCG_STATUS
:
997 vcpu
->arch
.mcg_status
= data
;
999 case MSR_IA32_MCG_CTL
:
1000 if (!(mcg_cap
& MCG_CTL_P
))
1002 if (data
!= 0 && data
!= ~(u64
)0)
1004 vcpu
->arch
.mcg_ctl
= data
;
1007 if (msr
>= MSR_IA32_MC0_CTL
&&
1008 msr
< MSR_IA32_MC0_CTL
+ 4 * bank_num
) {
1009 u32 offset
= msr
- MSR_IA32_MC0_CTL
;
1010 /* only 0 or all 1s can be written to IA32_MCi_CTL
1011 * some Linux kernels though clear bit 10 in bank 4 to
1012 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1013 * this to avoid an uncatched #GP in the guest
1015 if ((offset
& 0x3) == 0 &&
1016 data
!= 0 && (data
| (1 << 10)) != ~(u64
)0)
1018 vcpu
->arch
.mce_banks
[offset
] = data
;
1026 static int xen_hvm_config(struct kvm_vcpu
*vcpu
, u64 data
)
1028 struct kvm
*kvm
= vcpu
->kvm
;
1029 int lm
= is_long_mode(vcpu
);
1030 u8
*blob_addr
= lm
? (u8
*)(long)kvm
->arch
.xen_hvm_config
.blob_addr_64
1031 : (u8
*)(long)kvm
->arch
.xen_hvm_config
.blob_addr_32
;
1032 u8 blob_size
= lm
? kvm
->arch
.xen_hvm_config
.blob_size_64
1033 : kvm
->arch
.xen_hvm_config
.blob_size_32
;
1034 u32 page_num
= data
& ~PAGE_MASK
;
1035 u64 page_addr
= data
& PAGE_MASK
;
1040 if (page_num
>= blob_size
)
1043 page
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1047 if (copy_from_user(page
, blob_addr
+ (page_num
* PAGE_SIZE
), PAGE_SIZE
))
1049 if (kvm_write_guest(kvm
, page_addr
, page
, PAGE_SIZE
))
1058 static bool kvm_hv_hypercall_enabled(struct kvm
*kvm
)
1060 return kvm
->arch
.hv_hypercall
& HV_X64_MSR_HYPERCALL_ENABLE
;
1063 static bool kvm_hv_msr_partition_wide(u32 msr
)
1067 case HV_X64_MSR_GUEST_OS_ID
:
1068 case HV_X64_MSR_HYPERCALL
:
1076 static int set_msr_hyperv_pw(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1078 struct kvm
*kvm
= vcpu
->kvm
;
1081 case HV_X64_MSR_GUEST_OS_ID
:
1082 kvm
->arch
.hv_guest_os_id
= data
;
1083 /* setting guest os id to zero disables hypercall page */
1084 if (!kvm
->arch
.hv_guest_os_id
)
1085 kvm
->arch
.hv_hypercall
&= ~HV_X64_MSR_HYPERCALL_ENABLE
;
1087 case HV_X64_MSR_HYPERCALL
: {
1092 /* if guest os id is not set hypercall should remain disabled */
1093 if (!kvm
->arch
.hv_guest_os_id
)
1095 if (!(data
& HV_X64_MSR_HYPERCALL_ENABLE
)) {
1096 kvm
->arch
.hv_hypercall
= data
;
1099 gfn
= data
>> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT
;
1100 addr
= gfn_to_hva(kvm
, gfn
);
1101 if (kvm_is_error_hva(addr
))
1103 kvm_x86_ops
->patch_hypercall(vcpu
, instructions
);
1104 ((unsigned char *)instructions
)[3] = 0xc3; /* ret */
1105 if (copy_to_user((void __user
*)addr
, instructions
, 4))
1107 kvm
->arch
.hv_hypercall
= data
;
1111 pr_unimpl(vcpu
, "HYPER-V unimplemented wrmsr: 0x%x "
1112 "data 0x%llx\n", msr
, data
);
1118 static int set_msr_hyperv(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1121 case HV_X64_MSR_APIC_ASSIST_PAGE
: {
1124 if (!(data
& HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE
)) {
1125 vcpu
->arch
.hv_vapic
= data
;
1128 addr
= gfn_to_hva(vcpu
->kvm
, data
>>
1129 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT
);
1130 if (kvm_is_error_hva(addr
))
1132 if (clear_user((void __user
*)addr
, PAGE_SIZE
))
1134 vcpu
->arch
.hv_vapic
= data
;
1137 case HV_X64_MSR_EOI
:
1138 return kvm_hv_vapic_msr_write(vcpu
, APIC_EOI
, data
);
1139 case HV_X64_MSR_ICR
:
1140 return kvm_hv_vapic_msr_write(vcpu
, APIC_ICR
, data
);
1141 case HV_X64_MSR_TPR
:
1142 return kvm_hv_vapic_msr_write(vcpu
, APIC_TASKPRI
, data
);
1144 pr_unimpl(vcpu
, "HYPER-V unimplemented wrmsr: 0x%x "
1145 "data 0x%llx\n", msr
, data
);
1152 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1156 set_efer(vcpu
, data
);
1159 data
&= ~(u64
)0x40; /* ignore flush filter disable */
1160 data
&= ~(u64
)0x100; /* ignore ignne emulation enable */
1162 pr_unimpl(vcpu
, "unimplemented HWCR wrmsr: 0x%llx\n",
1167 case MSR_FAM10H_MMIO_CONF_BASE
:
1169 pr_unimpl(vcpu
, "unimplemented MMIO_CONF_BASE wrmsr: "
1174 case MSR_AMD64_NB_CFG
:
1176 case MSR_IA32_DEBUGCTLMSR
:
1178 /* We support the non-activated case already */
1180 } else if (data
& ~(DEBUGCTLMSR_LBR
| DEBUGCTLMSR_BTF
)) {
1181 /* Values other than LBR and BTF are vendor-specific,
1182 thus reserved and should throw a #GP */
1185 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1188 case MSR_IA32_UCODE_REV
:
1189 case MSR_IA32_UCODE_WRITE
:
1190 case MSR_VM_HSAVE_PA
:
1191 case MSR_AMD64_PATCH_LOADER
:
1193 case 0x200 ... 0x2ff:
1194 return set_msr_mtrr(vcpu
, msr
, data
);
1195 case MSR_IA32_APICBASE
:
1196 kvm_set_apic_base(vcpu
, data
);
1198 case APIC_BASE_MSR
... APIC_BASE_MSR
+ 0x3ff:
1199 return kvm_x2apic_msr_write(vcpu
, msr
, data
);
1200 case MSR_IA32_MISC_ENABLE
:
1201 vcpu
->arch
.ia32_misc_enable_msr
= data
;
1203 case MSR_KVM_WALL_CLOCK
:
1204 vcpu
->kvm
->arch
.wall_clock
= data
;
1205 kvm_write_wall_clock(vcpu
->kvm
, data
);
1207 case MSR_KVM_SYSTEM_TIME
: {
1208 if (vcpu
->arch
.time_page
) {
1209 kvm_release_page_dirty(vcpu
->arch
.time_page
);
1210 vcpu
->arch
.time_page
= NULL
;
1213 vcpu
->arch
.time
= data
;
1215 /* we verify if the enable bit is set... */
1219 /* ...but clean it before doing the actual write */
1220 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
1222 vcpu
->arch
.time_page
=
1223 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
1225 if (is_error_page(vcpu
->arch
.time_page
)) {
1226 kvm_release_page_clean(vcpu
->arch
.time_page
);
1227 vcpu
->arch
.time_page
= NULL
;
1230 kvm_request_guest_time_update(vcpu
);
1233 case MSR_IA32_MCG_CTL
:
1234 case MSR_IA32_MCG_STATUS
:
1235 case MSR_IA32_MC0_CTL
... MSR_IA32_MC0_CTL
+ 4 * KVM_MAX_MCE_BANKS
- 1:
1236 return set_msr_mce(vcpu
, msr
, data
);
1238 /* Performance counters are not protected by a CPUID bit,
1239 * so we should check all of them in the generic path for the sake of
1240 * cross vendor migration.
1241 * Writing a zero into the event select MSRs disables them,
1242 * which we perfectly emulate ;-). Any other value should be at least
1243 * reported, some guests depend on them.
1245 case MSR_P6_EVNTSEL0
:
1246 case MSR_P6_EVNTSEL1
:
1247 case MSR_K7_EVNTSEL0
:
1248 case MSR_K7_EVNTSEL1
:
1249 case MSR_K7_EVNTSEL2
:
1250 case MSR_K7_EVNTSEL3
:
1252 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: "
1253 "0x%x data 0x%llx\n", msr
, data
);
1255 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1256 * so we ignore writes to make it happy.
1258 case MSR_P6_PERFCTR0
:
1259 case MSR_P6_PERFCTR1
:
1260 case MSR_K7_PERFCTR0
:
1261 case MSR_K7_PERFCTR1
:
1262 case MSR_K7_PERFCTR2
:
1263 case MSR_K7_PERFCTR3
:
1264 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: "
1265 "0x%x data 0x%llx\n", msr
, data
);
1267 case HV_X64_MSR_GUEST_OS_ID
... HV_X64_MSR_SINT15
:
1268 if (kvm_hv_msr_partition_wide(msr
)) {
1270 mutex_lock(&vcpu
->kvm
->lock
);
1271 r
= set_msr_hyperv_pw(vcpu
, msr
, data
);
1272 mutex_unlock(&vcpu
->kvm
->lock
);
1275 return set_msr_hyperv(vcpu
, msr
, data
);
1278 if (msr
&& (msr
== vcpu
->kvm
->arch
.xen_hvm_config
.msr
))
1279 return xen_hvm_config(vcpu
, data
);
1281 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n",
1285 pr_unimpl(vcpu
, "ignored wrmsr: 0x%x data %llx\n",
1292 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1296 * Reads an msr value (of 'msr_index') into 'pdata'.
1297 * Returns 0 on success, non-0 otherwise.
1298 * Assumes vcpu_load() was already called.
1300 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1302 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
1305 static int get_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1307 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
1309 if (!msr_mtrr_valid(msr
))
1312 if (msr
== MSR_MTRRdefType
)
1313 *pdata
= vcpu
->arch
.mtrr_state
.def_type
+
1314 (vcpu
->arch
.mtrr_state
.enabled
<< 10);
1315 else if (msr
== MSR_MTRRfix64K_00000
)
1317 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
1318 *pdata
= p
[1 + msr
- MSR_MTRRfix16K_80000
];
1319 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
1320 *pdata
= p
[3 + msr
- MSR_MTRRfix4K_C0000
];
1321 else if (msr
== MSR_IA32_CR_PAT
)
1322 *pdata
= vcpu
->arch
.pat
;
1323 else { /* Variable MTRRs */
1324 int idx
, is_mtrr_mask
;
1327 idx
= (msr
- 0x200) / 2;
1328 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
1331 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
1334 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
1341 static int get_msr_mce(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1344 u64 mcg_cap
= vcpu
->arch
.mcg_cap
;
1345 unsigned bank_num
= mcg_cap
& 0xff;
1348 case MSR_IA32_P5_MC_ADDR
:
1349 case MSR_IA32_P5_MC_TYPE
:
1352 case MSR_IA32_MCG_CAP
:
1353 data
= vcpu
->arch
.mcg_cap
;
1355 case MSR_IA32_MCG_CTL
:
1356 if (!(mcg_cap
& MCG_CTL_P
))
1358 data
= vcpu
->arch
.mcg_ctl
;
1360 case MSR_IA32_MCG_STATUS
:
1361 data
= vcpu
->arch
.mcg_status
;
1364 if (msr
>= MSR_IA32_MC0_CTL
&&
1365 msr
< MSR_IA32_MC0_CTL
+ 4 * bank_num
) {
1366 u32 offset
= msr
- MSR_IA32_MC0_CTL
;
1367 data
= vcpu
->arch
.mce_banks
[offset
];
1376 static int get_msr_hyperv_pw(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1379 struct kvm
*kvm
= vcpu
->kvm
;
1382 case HV_X64_MSR_GUEST_OS_ID
:
1383 data
= kvm
->arch
.hv_guest_os_id
;
1385 case HV_X64_MSR_HYPERCALL
:
1386 data
= kvm
->arch
.hv_hypercall
;
1389 pr_unimpl(vcpu
, "Hyper-V unhandled rdmsr: 0x%x\n", msr
);
1397 static int get_msr_hyperv(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1402 case HV_X64_MSR_VP_INDEX
: {
1405 kvm_for_each_vcpu(r
, v
, vcpu
->kvm
)
1410 case HV_X64_MSR_EOI
:
1411 return kvm_hv_vapic_msr_read(vcpu
, APIC_EOI
, pdata
);
1412 case HV_X64_MSR_ICR
:
1413 return kvm_hv_vapic_msr_read(vcpu
, APIC_ICR
, pdata
);
1414 case HV_X64_MSR_TPR
:
1415 return kvm_hv_vapic_msr_read(vcpu
, APIC_TASKPRI
, pdata
);
1417 pr_unimpl(vcpu
, "Hyper-V unhandled rdmsr: 0x%x\n", msr
);
1424 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1429 case MSR_IA32_PLATFORM_ID
:
1430 case MSR_IA32_UCODE_REV
:
1431 case MSR_IA32_EBL_CR_POWERON
:
1432 case MSR_IA32_DEBUGCTLMSR
:
1433 case MSR_IA32_LASTBRANCHFROMIP
:
1434 case MSR_IA32_LASTBRANCHTOIP
:
1435 case MSR_IA32_LASTINTFROMIP
:
1436 case MSR_IA32_LASTINTTOIP
:
1439 case MSR_VM_HSAVE_PA
:
1440 case MSR_P6_PERFCTR0
:
1441 case MSR_P6_PERFCTR1
:
1442 case MSR_P6_EVNTSEL0
:
1443 case MSR_P6_EVNTSEL1
:
1444 case MSR_K7_EVNTSEL0
:
1445 case MSR_K7_PERFCTR0
:
1446 case MSR_K8_INT_PENDING_MSG
:
1447 case MSR_AMD64_NB_CFG
:
1448 case MSR_FAM10H_MMIO_CONF_BASE
:
1452 data
= 0x500 | KVM_NR_VAR_MTRR
;
1454 case 0x200 ... 0x2ff:
1455 return get_msr_mtrr(vcpu
, msr
, pdata
);
1456 case 0xcd: /* fsb frequency */
1459 case MSR_IA32_APICBASE
:
1460 data
= kvm_get_apic_base(vcpu
);
1462 case APIC_BASE_MSR
... APIC_BASE_MSR
+ 0x3ff:
1463 return kvm_x2apic_msr_read(vcpu
, msr
, pdata
);
1465 case MSR_IA32_MISC_ENABLE
:
1466 data
= vcpu
->arch
.ia32_misc_enable_msr
;
1468 case MSR_IA32_PERF_STATUS
:
1469 /* TSC increment by tick */
1471 /* CPU multiplier */
1472 data
|= (((uint64_t)4ULL) << 40);
1475 data
= vcpu
->arch
.efer
;
1477 case MSR_KVM_WALL_CLOCK
:
1478 data
= vcpu
->kvm
->arch
.wall_clock
;
1480 case MSR_KVM_SYSTEM_TIME
:
1481 data
= vcpu
->arch
.time
;
1483 case MSR_IA32_P5_MC_ADDR
:
1484 case MSR_IA32_P5_MC_TYPE
:
1485 case MSR_IA32_MCG_CAP
:
1486 case MSR_IA32_MCG_CTL
:
1487 case MSR_IA32_MCG_STATUS
:
1488 case MSR_IA32_MC0_CTL
... MSR_IA32_MC0_CTL
+ 4 * KVM_MAX_MCE_BANKS
- 1:
1489 return get_msr_mce(vcpu
, msr
, pdata
);
1490 case HV_X64_MSR_GUEST_OS_ID
... HV_X64_MSR_SINT15
:
1491 if (kvm_hv_msr_partition_wide(msr
)) {
1493 mutex_lock(&vcpu
->kvm
->lock
);
1494 r
= get_msr_hyperv_pw(vcpu
, msr
, pdata
);
1495 mutex_unlock(&vcpu
->kvm
->lock
);
1498 return get_msr_hyperv(vcpu
, msr
, pdata
);
1502 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
1505 pr_unimpl(vcpu
, "ignored rdmsr: 0x%x\n", msr
);
1513 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1516 * Read or write a bunch of msrs. All parameters are kernel addresses.
1518 * @return number of msrs set successfully.
1520 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
1521 struct kvm_msr_entry
*entries
,
1522 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1523 unsigned index
, u64
*data
))
1529 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1530 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
1531 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
1533 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1541 * Read or write a bunch of msrs. Parameters are user addresses.
1543 * @return number of msrs set successfully.
1545 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
1546 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1547 unsigned index
, u64
*data
),
1550 struct kvm_msrs msrs
;
1551 struct kvm_msr_entry
*entries
;
1556 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
1560 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
1564 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
1565 entries
= vmalloc(size
);
1570 if (copy_from_user(entries
, user_msrs
->entries
, size
))
1573 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
1578 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
1589 int kvm_dev_ioctl_check_extension(long ext
)
1594 case KVM_CAP_IRQCHIP
:
1596 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
1597 case KVM_CAP_SET_TSS_ADDR
:
1598 case KVM_CAP_EXT_CPUID
:
1599 case KVM_CAP_CLOCKSOURCE
:
1601 case KVM_CAP_NOP_IO_DELAY
:
1602 case KVM_CAP_MP_STATE
:
1603 case KVM_CAP_SYNC_MMU
:
1604 case KVM_CAP_REINJECT_CONTROL
:
1605 case KVM_CAP_IRQ_INJECT_STATUS
:
1606 case KVM_CAP_ASSIGN_DEV_IRQ
:
1608 case KVM_CAP_IOEVENTFD
:
1610 case KVM_CAP_PIT_STATE2
:
1611 case KVM_CAP_SET_IDENTITY_MAP_ADDR
:
1612 case KVM_CAP_XEN_HVM
:
1613 case KVM_CAP_ADJUST_CLOCK
:
1614 case KVM_CAP_VCPU_EVENTS
:
1615 case KVM_CAP_HYPERV
:
1616 case KVM_CAP_HYPERV_VAPIC
:
1617 case KVM_CAP_HYPERV_SPIN
:
1618 case KVM_CAP_PCI_SEGMENT
:
1619 case KVM_CAP_DEBUGREGS
:
1620 case KVM_CAP_X86_ROBUST_SINGLESTEP
:
1623 case KVM_CAP_COALESCED_MMIO
:
1624 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
1627 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
1629 case KVM_CAP_NR_VCPUS
:
1632 case KVM_CAP_NR_MEMSLOTS
:
1633 r
= KVM_MEMORY_SLOTS
;
1635 case KVM_CAP_PV_MMU
: /* obsolete */
1642 r
= KVM_MAX_MCE_BANKS
;
1652 long kvm_arch_dev_ioctl(struct file
*filp
,
1653 unsigned int ioctl
, unsigned long arg
)
1655 void __user
*argp
= (void __user
*)arg
;
1659 case KVM_GET_MSR_INDEX_LIST
: {
1660 struct kvm_msr_list __user
*user_msr_list
= argp
;
1661 struct kvm_msr_list msr_list
;
1665 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
1668 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
1669 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
1672 if (n
< msr_list
.nmsrs
)
1675 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
1676 num_msrs_to_save
* sizeof(u32
)))
1678 if (copy_to_user(user_msr_list
->indices
+ num_msrs_to_save
,
1680 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
1685 case KVM_GET_SUPPORTED_CPUID
: {
1686 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1687 struct kvm_cpuid2 cpuid
;
1690 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1692 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
1693 cpuid_arg
->entries
);
1698 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1703 case KVM_X86_GET_MCE_CAP_SUPPORTED
: {
1706 mce_cap
= KVM_MCE_CAP_SUPPORTED
;
1708 if (copy_to_user(argp
, &mce_cap
, sizeof mce_cap
))
1720 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1722 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
1723 if (unlikely(per_cpu(cpu_tsc_khz
, cpu
) == 0)) {
1724 unsigned long khz
= cpufreq_quick_get(cpu
);
1727 per_cpu(cpu_tsc_khz
, cpu
) = khz
;
1729 kvm_request_guest_time_update(vcpu
);
1732 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
1734 kvm_put_guest_fpu(vcpu
);
1735 kvm_x86_ops
->vcpu_put(vcpu
);
1738 static int is_efer_nx(void)
1740 unsigned long long efer
= 0;
1742 rdmsrl_safe(MSR_EFER
, &efer
);
1743 return efer
& EFER_NX
;
1746 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
1749 struct kvm_cpuid_entry2
*e
, *entry
;
1752 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
1753 e
= &vcpu
->arch
.cpuid_entries
[i
];
1754 if (e
->function
== 0x80000001) {
1759 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
1760 entry
->edx
&= ~(1 << 20);
1761 printk(KERN_INFO
"kvm: guest NX capability removed\n");
1765 /* when an old userspace process fills a new kernel module */
1766 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
1767 struct kvm_cpuid
*cpuid
,
1768 struct kvm_cpuid_entry __user
*entries
)
1771 struct kvm_cpuid_entry
*cpuid_entries
;
1774 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1777 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
1781 if (copy_from_user(cpuid_entries
, entries
,
1782 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
1784 for (i
= 0; i
< cpuid
->nent
; i
++) {
1785 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
1786 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
1787 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
1788 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
1789 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
1790 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
1791 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
1792 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
1793 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
1794 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
1796 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1797 cpuid_fix_nx_cap(vcpu
);
1799 kvm_apic_set_version(vcpu
);
1800 kvm_x86_ops
->cpuid_update(vcpu
);
1803 vfree(cpuid_entries
);
1808 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
1809 struct kvm_cpuid2
*cpuid
,
1810 struct kvm_cpuid_entry2 __user
*entries
)
1815 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1818 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
1819 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
1821 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1822 kvm_apic_set_version(vcpu
);
1823 kvm_x86_ops
->cpuid_update(vcpu
);
1830 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
1831 struct kvm_cpuid2
*cpuid
,
1832 struct kvm_cpuid_entry2 __user
*entries
)
1837 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1840 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1841 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1846 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1850 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1853 entry
->function
= function
;
1854 entry
->index
= index
;
1855 cpuid_count(entry
->function
, entry
->index
,
1856 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1860 #define F(x) bit(X86_FEATURE_##x)
1862 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1863 u32 index
, int *nent
, int maxnent
)
1865 unsigned f_nx
= is_efer_nx() ? F(NX
) : 0;
1866 #ifdef CONFIG_X86_64
1867 unsigned f_gbpages
= (kvm_x86_ops
->get_lpage_level() == PT_PDPE_LEVEL
)
1869 unsigned f_lm
= F(LM
);
1871 unsigned f_gbpages
= 0;
1874 unsigned f_rdtscp
= kvm_x86_ops
->rdtscp_supported() ? F(RDTSCP
) : 0;
1877 const u32 kvm_supported_word0_x86_features
=
1878 F(FPU
) | F(VME
) | F(DE
) | F(PSE
) |
1879 F(TSC
) | F(MSR
) | F(PAE
) | F(MCE
) |
1880 F(CX8
) | F(APIC
) | 0 /* Reserved */ | F(SEP
) |
1881 F(MTRR
) | F(PGE
) | F(MCA
) | F(CMOV
) |
1882 F(PAT
) | F(PSE36
) | 0 /* PSN */ | F(CLFLSH
) |
1883 0 /* Reserved, DS, ACPI */ | F(MMX
) |
1884 F(FXSR
) | F(XMM
) | F(XMM2
) | F(SELFSNOOP
) |
1885 0 /* HTT, TM, Reserved, PBE */;
1886 /* cpuid 0x80000001.edx */
1887 const u32 kvm_supported_word1_x86_features
=
1888 F(FPU
) | F(VME
) | F(DE
) | F(PSE
) |
1889 F(TSC
) | F(MSR
) | F(PAE
) | F(MCE
) |
1890 F(CX8
) | F(APIC
) | 0 /* Reserved */ | F(SYSCALL
) |
1891 F(MTRR
) | F(PGE
) | F(MCA
) | F(CMOV
) |
1892 F(PAT
) | F(PSE36
) | 0 /* Reserved */ |
1893 f_nx
| 0 /* Reserved */ | F(MMXEXT
) | F(MMX
) |
1894 F(FXSR
) | F(FXSR_OPT
) | f_gbpages
| f_rdtscp
|
1895 0 /* Reserved */ | f_lm
| F(3DNOWEXT
) | F(3DNOW
);
1897 const u32 kvm_supported_word4_x86_features
=
1898 F(XMM3
) | 0 /* Reserved, DTES64, MONITOR */ |
1899 0 /* DS-CPL, VMX, SMX, EST */ |
1900 0 /* TM2 */ | F(SSSE3
) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1901 0 /* Reserved */ | F(CX16
) | 0 /* xTPR Update, PDCM */ |
1902 0 /* Reserved, DCA */ | F(XMM4_1
) |
1903 F(XMM4_2
) | F(X2APIC
) | F(MOVBE
) | F(POPCNT
) |
1904 0 /* Reserved, XSAVE, OSXSAVE */;
1905 /* cpuid 0x80000001.ecx */
1906 const u32 kvm_supported_word6_x86_features
=
1907 F(LAHF_LM
) | F(CMP_LEGACY
) | F(SVM
) | 0 /* ExtApicSpace */ |
1908 F(CR8_LEGACY
) | F(ABM
) | F(SSE4A
) | F(MISALIGNSSE
) |
1909 F(3DNOWPREFETCH
) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5
) |
1910 0 /* SKINIT */ | 0 /* WDT */;
1912 /* all calls to cpuid_count() should be made on the same cpu */
1914 do_cpuid_1_ent(entry
, function
, index
);
1919 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1922 entry
->edx
&= kvm_supported_word0_x86_features
;
1923 entry
->ecx
&= kvm_supported_word4_x86_features
;
1924 /* we support x2apic emulation even if host does not support
1925 * it since we emulate x2apic in software */
1926 entry
->ecx
|= F(X2APIC
);
1928 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1929 * may return different values. This forces us to get_cpu() before
1930 * issuing the first command, and also to emulate this annoying behavior
1931 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1933 int t
, times
= entry
->eax
& 0xff;
1935 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1936 entry
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
1937 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1938 do_cpuid_1_ent(&entry
[t
], function
, 0);
1939 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1944 /* function 4 and 0xb have additional index. */
1948 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1949 /* read more entries until cache_type is zero */
1950 for (i
= 1; *nent
< maxnent
; ++i
) {
1951 cache_type
= entry
[i
- 1].eax
& 0x1f;
1954 do_cpuid_1_ent(&entry
[i
], function
, i
);
1956 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1964 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1965 /* read more entries until level_type is zero */
1966 for (i
= 1; *nent
< maxnent
; ++i
) {
1967 level_type
= entry
[i
- 1].ecx
& 0xff00;
1970 do_cpuid_1_ent(&entry
[i
], function
, i
);
1972 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1978 entry
->eax
= min(entry
->eax
, 0x8000001a);
1981 entry
->edx
&= kvm_supported_word1_x86_features
;
1982 entry
->ecx
&= kvm_supported_word6_x86_features
;
1986 kvm_x86_ops
->set_supported_cpuid(function
, entry
);
1993 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1994 struct kvm_cpuid_entry2 __user
*entries
)
1996 struct kvm_cpuid_entry2
*cpuid_entries
;
1997 int limit
, nent
= 0, r
= -E2BIG
;
2000 if (cpuid
->nent
< 1)
2002 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2003 cpuid
->nent
= KVM_MAX_CPUID_ENTRIES
;
2005 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
2009 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
2010 limit
= cpuid_entries
[0].eax
;
2011 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
2012 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
2013 &nent
, cpuid
->nent
);
2015 if (nent
>= cpuid
->nent
)
2018 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
2019 limit
= cpuid_entries
[nent
- 1].eax
;
2020 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
2021 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
2022 &nent
, cpuid
->nent
);
2024 if (nent
>= cpuid
->nent
)
2028 if (copy_to_user(entries
, cpuid_entries
,
2029 nent
* sizeof(struct kvm_cpuid_entry2
)))
2035 vfree(cpuid_entries
);
2040 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
2041 struct kvm_lapic_state
*s
)
2044 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
2050 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
2051 struct kvm_lapic_state
*s
)
2054 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
2055 kvm_apic_post_state_restore(vcpu
);
2056 update_cr8_intercept(vcpu
);
2062 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2063 struct kvm_interrupt
*irq
)
2065 if (irq
->irq
< 0 || irq
->irq
>= 256)
2067 if (irqchip_in_kernel(vcpu
->kvm
))
2071 kvm_queue_interrupt(vcpu
, irq
->irq
, false);
2078 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu
*vcpu
)
2081 kvm_inject_nmi(vcpu
);
2087 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
2088 struct kvm_tpr_access_ctl
*tac
)
2092 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
2096 static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu
*vcpu
,
2100 unsigned bank_num
= mcg_cap
& 0xff, bank
;
2103 if (!bank_num
|| bank_num
>= KVM_MAX_MCE_BANKS
)
2105 if (mcg_cap
& ~(KVM_MCE_CAP_SUPPORTED
| 0xff | 0xff0000))
2108 vcpu
->arch
.mcg_cap
= mcg_cap
;
2109 /* Init IA32_MCG_CTL to all 1s */
2110 if (mcg_cap
& MCG_CTL_P
)
2111 vcpu
->arch
.mcg_ctl
= ~(u64
)0;
2112 /* Init IA32_MCi_CTL to all 1s */
2113 for (bank
= 0; bank
< bank_num
; bank
++)
2114 vcpu
->arch
.mce_banks
[bank
*4] = ~(u64
)0;
2119 static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu
*vcpu
,
2120 struct kvm_x86_mce
*mce
)
2122 u64 mcg_cap
= vcpu
->arch
.mcg_cap
;
2123 unsigned bank_num
= mcg_cap
& 0xff;
2124 u64
*banks
= vcpu
->arch
.mce_banks
;
2126 if (mce
->bank
>= bank_num
|| !(mce
->status
& MCI_STATUS_VAL
))
2129 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2130 * reporting is disabled
2132 if ((mce
->status
& MCI_STATUS_UC
) && (mcg_cap
& MCG_CTL_P
) &&
2133 vcpu
->arch
.mcg_ctl
!= ~(u64
)0)
2135 banks
+= 4 * mce
->bank
;
2137 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2138 * reporting is disabled for the bank
2140 if ((mce
->status
& MCI_STATUS_UC
) && banks
[0] != ~(u64
)0)
2142 if (mce
->status
& MCI_STATUS_UC
) {
2143 if ((vcpu
->arch
.mcg_status
& MCG_STATUS_MCIP
) ||
2144 !kvm_read_cr4_bits(vcpu
, X86_CR4_MCE
)) {
2145 printk(KERN_DEBUG
"kvm: set_mce: "
2146 "injects mce exception while "
2147 "previous one is in progress!\n");
2148 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
2151 if (banks
[1] & MCI_STATUS_VAL
)
2152 mce
->status
|= MCI_STATUS_OVER
;
2153 banks
[2] = mce
->addr
;
2154 banks
[3] = mce
->misc
;
2155 vcpu
->arch
.mcg_status
= mce
->mcg_status
;
2156 banks
[1] = mce
->status
;
2157 kvm_queue_exception(vcpu
, MC_VECTOR
);
2158 } else if (!(banks
[1] & MCI_STATUS_VAL
)
2159 || !(banks
[1] & MCI_STATUS_UC
)) {
2160 if (banks
[1] & MCI_STATUS_VAL
)
2161 mce
->status
|= MCI_STATUS_OVER
;
2162 banks
[2] = mce
->addr
;
2163 banks
[3] = mce
->misc
;
2164 banks
[1] = mce
->status
;
2166 banks
[1] |= MCI_STATUS_OVER
;
2170 static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu
*vcpu
,
2171 struct kvm_vcpu_events
*events
)
2175 events
->exception
.injected
=
2176 vcpu
->arch
.exception
.pending
&&
2177 !kvm_exception_is_soft(vcpu
->arch
.exception
.nr
);
2178 events
->exception
.nr
= vcpu
->arch
.exception
.nr
;
2179 events
->exception
.has_error_code
= vcpu
->arch
.exception
.has_error_code
;
2180 events
->exception
.error_code
= vcpu
->arch
.exception
.error_code
;
2182 events
->interrupt
.injected
=
2183 vcpu
->arch
.interrupt
.pending
&& !vcpu
->arch
.interrupt
.soft
;
2184 events
->interrupt
.nr
= vcpu
->arch
.interrupt
.nr
;
2185 events
->interrupt
.soft
= 0;
2186 events
->interrupt
.shadow
=
2187 kvm_x86_ops
->get_interrupt_shadow(vcpu
,
2188 KVM_X86_SHADOW_INT_MOV_SS
| KVM_X86_SHADOW_INT_STI
);
2190 events
->nmi
.injected
= vcpu
->arch
.nmi_injected
;
2191 events
->nmi
.pending
= vcpu
->arch
.nmi_pending
;
2192 events
->nmi
.masked
= kvm_x86_ops
->get_nmi_mask(vcpu
);
2194 events
->sipi_vector
= vcpu
->arch
.sipi_vector
;
2196 events
->flags
= (KVM_VCPUEVENT_VALID_NMI_PENDING
2197 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2198 | KVM_VCPUEVENT_VALID_SHADOW
);
2203 static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu
*vcpu
,
2204 struct kvm_vcpu_events
*events
)
2206 if (events
->flags
& ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2207 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2208 | KVM_VCPUEVENT_VALID_SHADOW
))
2213 vcpu
->arch
.exception
.pending
= events
->exception
.injected
;
2214 vcpu
->arch
.exception
.nr
= events
->exception
.nr
;
2215 vcpu
->arch
.exception
.has_error_code
= events
->exception
.has_error_code
;
2216 vcpu
->arch
.exception
.error_code
= events
->exception
.error_code
;
2218 vcpu
->arch
.interrupt
.pending
= events
->interrupt
.injected
;
2219 vcpu
->arch
.interrupt
.nr
= events
->interrupt
.nr
;
2220 vcpu
->arch
.interrupt
.soft
= events
->interrupt
.soft
;
2221 if (vcpu
->arch
.interrupt
.pending
&& irqchip_in_kernel(vcpu
->kvm
))
2222 kvm_pic_clear_isr_ack(vcpu
->kvm
);
2223 if (events
->flags
& KVM_VCPUEVENT_VALID_SHADOW
)
2224 kvm_x86_ops
->set_interrupt_shadow(vcpu
,
2225 events
->interrupt
.shadow
);
2227 vcpu
->arch
.nmi_injected
= events
->nmi
.injected
;
2228 if (events
->flags
& KVM_VCPUEVENT_VALID_NMI_PENDING
)
2229 vcpu
->arch
.nmi_pending
= events
->nmi
.pending
;
2230 kvm_x86_ops
->set_nmi_mask(vcpu
, events
->nmi
.masked
);
2232 if (events
->flags
& KVM_VCPUEVENT_VALID_SIPI_VECTOR
)
2233 vcpu
->arch
.sipi_vector
= events
->sipi_vector
;
2240 static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu
*vcpu
,
2241 struct kvm_debugregs
*dbgregs
)
2245 memcpy(dbgregs
->db
, vcpu
->arch
.db
, sizeof(vcpu
->arch
.db
));
2246 dbgregs
->dr6
= vcpu
->arch
.dr6
;
2247 dbgregs
->dr7
= vcpu
->arch
.dr7
;
2253 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu
*vcpu
,
2254 struct kvm_debugregs
*dbgregs
)
2261 memcpy(vcpu
->arch
.db
, dbgregs
->db
, sizeof(vcpu
->arch
.db
));
2262 vcpu
->arch
.dr6
= dbgregs
->dr6
;
2263 vcpu
->arch
.dr7
= dbgregs
->dr7
;
2270 long kvm_arch_vcpu_ioctl(struct file
*filp
,
2271 unsigned int ioctl
, unsigned long arg
)
2273 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2274 void __user
*argp
= (void __user
*)arg
;
2276 struct kvm_lapic_state
*lapic
= NULL
;
2279 case KVM_GET_LAPIC
: {
2281 if (!vcpu
->arch
.apic
)
2283 lapic
= kzalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
2288 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, lapic
);
2292 if (copy_to_user(argp
, lapic
, sizeof(struct kvm_lapic_state
)))
2297 case KVM_SET_LAPIC
: {
2299 if (!vcpu
->arch
.apic
)
2301 lapic
= kmalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
2306 if (copy_from_user(lapic
, argp
, sizeof(struct kvm_lapic_state
)))
2308 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, lapic
);
2314 case KVM_INTERRUPT
: {
2315 struct kvm_interrupt irq
;
2318 if (copy_from_user(&irq
, argp
, sizeof irq
))
2320 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2327 r
= kvm_vcpu_ioctl_nmi(vcpu
);
2333 case KVM_SET_CPUID
: {
2334 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2335 struct kvm_cpuid cpuid
;
2338 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2340 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2345 case KVM_SET_CPUID2
: {
2346 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
2347 struct kvm_cpuid2 cpuid
;
2350 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2352 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
2353 cpuid_arg
->entries
);
2358 case KVM_GET_CPUID2
: {
2359 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
2360 struct kvm_cpuid2 cpuid
;
2363 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2365 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
2366 cpuid_arg
->entries
);
2370 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
2376 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
2379 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2381 case KVM_TPR_ACCESS_REPORTING
: {
2382 struct kvm_tpr_access_ctl tac
;
2385 if (copy_from_user(&tac
, argp
, sizeof tac
))
2387 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
2391 if (copy_to_user(argp
, &tac
, sizeof tac
))
2396 case KVM_SET_VAPIC_ADDR
: {
2397 struct kvm_vapic_addr va
;
2400 if (!irqchip_in_kernel(vcpu
->kvm
))
2403 if (copy_from_user(&va
, argp
, sizeof va
))
2406 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
2409 case KVM_X86_SETUP_MCE
: {
2413 if (copy_from_user(&mcg_cap
, argp
, sizeof mcg_cap
))
2415 r
= kvm_vcpu_ioctl_x86_setup_mce(vcpu
, mcg_cap
);
2418 case KVM_X86_SET_MCE
: {
2419 struct kvm_x86_mce mce
;
2422 if (copy_from_user(&mce
, argp
, sizeof mce
))
2424 r
= kvm_vcpu_ioctl_x86_set_mce(vcpu
, &mce
);
2427 case KVM_GET_VCPU_EVENTS
: {
2428 struct kvm_vcpu_events events
;
2430 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu
, &events
);
2433 if (copy_to_user(argp
, &events
, sizeof(struct kvm_vcpu_events
)))
2438 case KVM_SET_VCPU_EVENTS
: {
2439 struct kvm_vcpu_events events
;
2442 if (copy_from_user(&events
, argp
, sizeof(struct kvm_vcpu_events
)))
2445 r
= kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu
, &events
);
2448 case KVM_GET_DEBUGREGS
: {
2449 struct kvm_debugregs dbgregs
;
2451 kvm_vcpu_ioctl_x86_get_debugregs(vcpu
, &dbgregs
);
2454 if (copy_to_user(argp
, &dbgregs
,
2455 sizeof(struct kvm_debugregs
)))
2460 case KVM_SET_DEBUGREGS
: {
2461 struct kvm_debugregs dbgregs
;
2464 if (copy_from_user(&dbgregs
, argp
,
2465 sizeof(struct kvm_debugregs
)))
2468 r
= kvm_vcpu_ioctl_x86_set_debugregs(vcpu
, &dbgregs
);
2479 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
2483 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
2485 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
2489 static int kvm_vm_ioctl_set_identity_map_addr(struct kvm
*kvm
,
2492 kvm
->arch
.ept_identity_map_addr
= ident_addr
;
2496 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
2497 u32 kvm_nr_mmu_pages
)
2499 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
2502 mutex_lock(&kvm
->slots_lock
);
2503 spin_lock(&kvm
->mmu_lock
);
2505 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
2506 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
2508 spin_unlock(&kvm
->mmu_lock
);
2509 mutex_unlock(&kvm
->slots_lock
);
2513 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
2515 return kvm
->arch
.n_alloc_mmu_pages
;
2518 gfn_t
unalias_gfn_instantiation(struct kvm
*kvm
, gfn_t gfn
)
2521 struct kvm_mem_alias
*alias
;
2522 struct kvm_mem_aliases
*aliases
;
2524 aliases
= kvm_aliases(kvm
);
2526 for (i
= 0; i
< aliases
->naliases
; ++i
) {
2527 alias
= &aliases
->aliases
[i
];
2528 if (alias
->flags
& KVM_ALIAS_INVALID
)
2530 if (gfn
>= alias
->base_gfn
2531 && gfn
< alias
->base_gfn
+ alias
->npages
)
2532 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
2537 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
2540 struct kvm_mem_alias
*alias
;
2541 struct kvm_mem_aliases
*aliases
;
2543 aliases
= kvm_aliases(kvm
);
2545 for (i
= 0; i
< aliases
->naliases
; ++i
) {
2546 alias
= &aliases
->aliases
[i
];
2547 if (gfn
>= alias
->base_gfn
2548 && gfn
< alias
->base_gfn
+ alias
->npages
)
2549 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
2555 * Set a new alias region. Aliases map a portion of physical memory into
2556 * another portion. This is useful for memory windows, for example the PC
2559 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
2560 struct kvm_memory_alias
*alias
)
2563 struct kvm_mem_alias
*p
;
2564 struct kvm_mem_aliases
*aliases
, *old_aliases
;
2567 /* General sanity checks */
2568 if (alias
->memory_size
& (PAGE_SIZE
- 1))
2570 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
2572 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
2574 if (alias
->guest_phys_addr
+ alias
->memory_size
2575 < alias
->guest_phys_addr
)
2577 if (alias
->target_phys_addr
+ alias
->memory_size
2578 < alias
->target_phys_addr
)
2582 aliases
= kzalloc(sizeof(struct kvm_mem_aliases
), GFP_KERNEL
);
2586 mutex_lock(&kvm
->slots_lock
);
2588 /* invalidate any gfn reference in case of deletion/shrinking */
2589 memcpy(aliases
, kvm
->arch
.aliases
, sizeof(struct kvm_mem_aliases
));
2590 aliases
->aliases
[alias
->slot
].flags
|= KVM_ALIAS_INVALID
;
2591 old_aliases
= kvm
->arch
.aliases
;
2592 rcu_assign_pointer(kvm
->arch
.aliases
, aliases
);
2593 synchronize_srcu_expedited(&kvm
->srcu
);
2594 kvm_mmu_zap_all(kvm
);
2598 aliases
= kzalloc(sizeof(struct kvm_mem_aliases
), GFP_KERNEL
);
2602 memcpy(aliases
, kvm
->arch
.aliases
, sizeof(struct kvm_mem_aliases
));
2604 p
= &aliases
->aliases
[alias
->slot
];
2605 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
2606 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
2607 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
2608 p
->flags
&= ~(KVM_ALIAS_INVALID
);
2610 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
2611 if (aliases
->aliases
[n
- 1].npages
)
2613 aliases
->naliases
= n
;
2615 old_aliases
= kvm
->arch
.aliases
;
2616 rcu_assign_pointer(kvm
->arch
.aliases
, aliases
);
2617 synchronize_srcu_expedited(&kvm
->srcu
);
2622 mutex_unlock(&kvm
->slots_lock
);
2627 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
2632 switch (chip
->chip_id
) {
2633 case KVM_IRQCHIP_PIC_MASTER
:
2634 memcpy(&chip
->chip
.pic
,
2635 &pic_irqchip(kvm
)->pics
[0],
2636 sizeof(struct kvm_pic_state
));
2638 case KVM_IRQCHIP_PIC_SLAVE
:
2639 memcpy(&chip
->chip
.pic
,
2640 &pic_irqchip(kvm
)->pics
[1],
2641 sizeof(struct kvm_pic_state
));
2643 case KVM_IRQCHIP_IOAPIC
:
2644 r
= kvm_get_ioapic(kvm
, &chip
->chip
.ioapic
);
2653 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
2658 switch (chip
->chip_id
) {
2659 case KVM_IRQCHIP_PIC_MASTER
:
2660 raw_spin_lock(&pic_irqchip(kvm
)->lock
);
2661 memcpy(&pic_irqchip(kvm
)->pics
[0],
2663 sizeof(struct kvm_pic_state
));
2664 raw_spin_unlock(&pic_irqchip(kvm
)->lock
);
2666 case KVM_IRQCHIP_PIC_SLAVE
:
2667 raw_spin_lock(&pic_irqchip(kvm
)->lock
);
2668 memcpy(&pic_irqchip(kvm
)->pics
[1],
2670 sizeof(struct kvm_pic_state
));
2671 raw_spin_unlock(&pic_irqchip(kvm
)->lock
);
2673 case KVM_IRQCHIP_IOAPIC
:
2674 r
= kvm_set_ioapic(kvm
, &chip
->chip
.ioapic
);
2680 kvm_pic_update_irq(pic_irqchip(kvm
));
2684 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
2688 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2689 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
2690 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2694 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
2698 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2699 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
2700 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
, 0);
2701 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2705 static int kvm_vm_ioctl_get_pit2(struct kvm
*kvm
, struct kvm_pit_state2
*ps
)
2709 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2710 memcpy(ps
->channels
, &kvm
->arch
.vpit
->pit_state
.channels
,
2711 sizeof(ps
->channels
));
2712 ps
->flags
= kvm
->arch
.vpit
->pit_state
.flags
;
2713 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2717 static int kvm_vm_ioctl_set_pit2(struct kvm
*kvm
, struct kvm_pit_state2
*ps
)
2719 int r
= 0, start
= 0;
2720 u32 prev_legacy
, cur_legacy
;
2721 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2722 prev_legacy
= kvm
->arch
.vpit
->pit_state
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
2723 cur_legacy
= ps
->flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
2724 if (!prev_legacy
&& cur_legacy
)
2726 memcpy(&kvm
->arch
.vpit
->pit_state
.channels
, &ps
->channels
,
2727 sizeof(kvm
->arch
.vpit
->pit_state
.channels
));
2728 kvm
->arch
.vpit
->pit_state
.flags
= ps
->flags
;
2729 kvm_pit_load_count(kvm
, 0, kvm
->arch
.vpit
->pit_state
.channels
[0].count
, start
);
2730 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2734 static int kvm_vm_ioctl_reinject(struct kvm
*kvm
,
2735 struct kvm_reinject_control
*control
)
2737 if (!kvm
->arch
.vpit
)
2739 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2740 kvm
->arch
.vpit
->pit_state
.pit_timer
.reinject
= control
->pit_reinject
;
2741 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2746 * Get (and clear) the dirty memory log for a memory slot.
2748 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
2749 struct kvm_dirty_log
*log
)
2752 struct kvm_memory_slot
*memslot
;
2754 unsigned long is_dirty
= 0;
2755 unsigned long *dirty_bitmap
= NULL
;
2757 mutex_lock(&kvm
->slots_lock
);
2760 if (log
->slot
>= KVM_MEMORY_SLOTS
)
2763 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
2765 if (!memslot
->dirty_bitmap
)
2768 n
= kvm_dirty_bitmap_bytes(memslot
);
2771 dirty_bitmap
= vmalloc(n
);
2774 memset(dirty_bitmap
, 0, n
);
2776 for (i
= 0; !is_dirty
&& i
< n
/sizeof(long); i
++)
2777 is_dirty
= memslot
->dirty_bitmap
[i
];
2779 /* If nothing is dirty, don't bother messing with page tables. */
2781 struct kvm_memslots
*slots
, *old_slots
;
2783 spin_lock(&kvm
->mmu_lock
);
2784 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
2785 spin_unlock(&kvm
->mmu_lock
);
2787 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
2791 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
2792 slots
->memslots
[log
->slot
].dirty_bitmap
= dirty_bitmap
;
2794 old_slots
= kvm
->memslots
;
2795 rcu_assign_pointer(kvm
->memslots
, slots
);
2796 synchronize_srcu_expedited(&kvm
->srcu
);
2797 dirty_bitmap
= old_slots
->memslots
[log
->slot
].dirty_bitmap
;
2802 if (copy_to_user(log
->dirty_bitmap
, dirty_bitmap
, n
))
2805 vfree(dirty_bitmap
);
2807 mutex_unlock(&kvm
->slots_lock
);
2811 long kvm_arch_vm_ioctl(struct file
*filp
,
2812 unsigned int ioctl
, unsigned long arg
)
2814 struct kvm
*kvm
= filp
->private_data
;
2815 void __user
*argp
= (void __user
*)arg
;
2818 * This union makes it completely explicit to gcc-3.x
2819 * that these two variables' stack usage should be
2820 * combined, not added together.
2823 struct kvm_pit_state ps
;
2824 struct kvm_pit_state2 ps2
;
2825 struct kvm_memory_alias alias
;
2826 struct kvm_pit_config pit_config
;
2830 case KVM_SET_TSS_ADDR
:
2831 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
2835 case KVM_SET_IDENTITY_MAP_ADDR
: {
2839 if (copy_from_user(&ident_addr
, argp
, sizeof ident_addr
))
2841 r
= kvm_vm_ioctl_set_identity_map_addr(kvm
, ident_addr
);
2846 case KVM_SET_MEMORY_REGION
: {
2847 struct kvm_memory_region kvm_mem
;
2848 struct kvm_userspace_memory_region kvm_userspace_mem
;
2851 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2853 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
2854 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
2855 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
2856 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
2857 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2862 case KVM_SET_NR_MMU_PAGES
:
2863 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
2867 case KVM_GET_NR_MMU_PAGES
:
2868 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
2870 case KVM_SET_MEMORY_ALIAS
:
2872 if (copy_from_user(&u
.alias
, argp
, sizeof(struct kvm_memory_alias
)))
2874 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &u
.alias
);
2878 case KVM_CREATE_IRQCHIP
: {
2879 struct kvm_pic
*vpic
;
2881 mutex_lock(&kvm
->lock
);
2884 goto create_irqchip_unlock
;
2886 vpic
= kvm_create_pic(kvm
);
2888 r
= kvm_ioapic_init(kvm
);
2890 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
,
2893 goto create_irqchip_unlock
;
2896 goto create_irqchip_unlock
;
2898 kvm
->arch
.vpic
= vpic
;
2900 r
= kvm_setup_default_irq_routing(kvm
);
2902 mutex_lock(&kvm
->irq_lock
);
2903 kvm_ioapic_destroy(kvm
);
2904 kvm_destroy_pic(kvm
);
2905 mutex_unlock(&kvm
->irq_lock
);
2907 create_irqchip_unlock
:
2908 mutex_unlock(&kvm
->lock
);
2911 case KVM_CREATE_PIT
:
2912 u
.pit_config
.flags
= KVM_PIT_SPEAKER_DUMMY
;
2914 case KVM_CREATE_PIT2
:
2916 if (copy_from_user(&u
.pit_config
, argp
,
2917 sizeof(struct kvm_pit_config
)))
2920 mutex_lock(&kvm
->slots_lock
);
2923 goto create_pit_unlock
;
2925 kvm
->arch
.vpit
= kvm_create_pit(kvm
, u
.pit_config
.flags
);
2929 mutex_unlock(&kvm
->slots_lock
);
2931 case KVM_IRQ_LINE_STATUS
:
2932 case KVM_IRQ_LINE
: {
2933 struct kvm_irq_level irq_event
;
2936 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
2939 if (irqchip_in_kernel(kvm
)) {
2941 status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
2942 irq_event
.irq
, irq_event
.level
);
2943 if (ioctl
== KVM_IRQ_LINE_STATUS
) {
2945 irq_event
.status
= status
;
2946 if (copy_to_user(argp
, &irq_event
,
2954 case KVM_GET_IRQCHIP
: {
2955 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2956 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
2962 if (copy_from_user(chip
, argp
, sizeof *chip
))
2963 goto get_irqchip_out
;
2965 if (!irqchip_in_kernel(kvm
))
2966 goto get_irqchip_out
;
2967 r
= kvm_vm_ioctl_get_irqchip(kvm
, chip
);
2969 goto get_irqchip_out
;
2971 if (copy_to_user(argp
, chip
, sizeof *chip
))
2972 goto get_irqchip_out
;
2980 case KVM_SET_IRQCHIP
: {
2981 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2982 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
2988 if (copy_from_user(chip
, argp
, sizeof *chip
))
2989 goto set_irqchip_out
;
2991 if (!irqchip_in_kernel(kvm
))
2992 goto set_irqchip_out
;
2993 r
= kvm_vm_ioctl_set_irqchip(kvm
, chip
);
2995 goto set_irqchip_out
;
3005 if (copy_from_user(&u
.ps
, argp
, sizeof(struct kvm_pit_state
)))
3008 if (!kvm
->arch
.vpit
)
3010 r
= kvm_vm_ioctl_get_pit(kvm
, &u
.ps
);
3014 if (copy_to_user(argp
, &u
.ps
, sizeof(struct kvm_pit_state
)))
3021 if (copy_from_user(&u
.ps
, argp
, sizeof u
.ps
))
3024 if (!kvm
->arch
.vpit
)
3026 r
= kvm_vm_ioctl_set_pit(kvm
, &u
.ps
);
3032 case KVM_GET_PIT2
: {
3034 if (!kvm
->arch
.vpit
)
3036 r
= kvm_vm_ioctl_get_pit2(kvm
, &u
.ps2
);
3040 if (copy_to_user(argp
, &u
.ps2
, sizeof(u
.ps2
)))
3045 case KVM_SET_PIT2
: {
3047 if (copy_from_user(&u
.ps2
, argp
, sizeof(u
.ps2
)))
3050 if (!kvm
->arch
.vpit
)
3052 r
= kvm_vm_ioctl_set_pit2(kvm
, &u
.ps2
);
3058 case KVM_REINJECT_CONTROL
: {
3059 struct kvm_reinject_control control
;
3061 if (copy_from_user(&control
, argp
, sizeof(control
)))
3063 r
= kvm_vm_ioctl_reinject(kvm
, &control
);
3069 case KVM_XEN_HVM_CONFIG
: {
3071 if (copy_from_user(&kvm
->arch
.xen_hvm_config
, argp
,
3072 sizeof(struct kvm_xen_hvm_config
)))
3075 if (kvm
->arch
.xen_hvm_config
.flags
)
3080 case KVM_SET_CLOCK
: {
3081 struct timespec now
;
3082 struct kvm_clock_data user_ns
;
3087 if (copy_from_user(&user_ns
, argp
, sizeof(user_ns
)))
3096 now_ns
= timespec_to_ns(&now
);
3097 delta
= user_ns
.clock
- now_ns
;
3098 kvm
->arch
.kvmclock_offset
= delta
;
3101 case KVM_GET_CLOCK
: {
3102 struct timespec now
;
3103 struct kvm_clock_data user_ns
;
3107 now_ns
= timespec_to_ns(&now
);
3108 user_ns
.clock
= kvm
->arch
.kvmclock_offset
+ now_ns
;
3112 if (copy_to_user(argp
, &user_ns
, sizeof(user_ns
)))
3125 static void kvm_init_msr_list(void)
3130 /* skip the first msrs in the list. KVM-specific */
3131 for (i
= j
= KVM_SAVE_MSRS_BEGIN
; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
3132 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
3135 msrs_to_save
[j
] = msrs_to_save
[i
];
3138 num_msrs_to_save
= j
;
3141 static int vcpu_mmio_write(struct kvm_vcpu
*vcpu
, gpa_t addr
, int len
,
3144 if (vcpu
->arch
.apic
&&
3145 !kvm_iodevice_write(&vcpu
->arch
.apic
->dev
, addr
, len
, v
))
3148 return kvm_io_bus_write(vcpu
->kvm
, KVM_MMIO_BUS
, addr
, len
, v
);
3151 static int vcpu_mmio_read(struct kvm_vcpu
*vcpu
, gpa_t addr
, int len
, void *v
)
3153 if (vcpu
->arch
.apic
&&
3154 !kvm_iodevice_read(&vcpu
->arch
.apic
->dev
, addr
, len
, v
))
3157 return kvm_io_bus_read(vcpu
->kvm
, KVM_MMIO_BUS
, addr
, len
, v
);
3160 static void kvm_set_segment(struct kvm_vcpu
*vcpu
,
3161 struct kvm_segment
*var
, int seg
)
3163 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3166 void kvm_get_segment(struct kvm_vcpu
*vcpu
,
3167 struct kvm_segment
*var
, int seg
)
3169 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
3172 gpa_t
kvm_mmu_gva_to_gpa_read(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3174 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3175 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, access
, error
);
3178 gpa_t
kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3180 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3181 access
|= PFERR_FETCH_MASK
;
3182 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, access
, error
);
3185 gpa_t
kvm_mmu_gva_to_gpa_write(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3187 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3188 access
|= PFERR_WRITE_MASK
;
3189 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, access
, error
);
3192 /* uses this to access any guest's mapped memory without checking CPL */
3193 gpa_t
kvm_mmu_gva_to_gpa_system(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3195 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, 0, error
);
3198 static int kvm_read_guest_virt_helper(gva_t addr
, void *val
, unsigned int bytes
,
3199 struct kvm_vcpu
*vcpu
, u32 access
,
3203 int r
= X86EMUL_CONTINUE
;
3206 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
, access
, error
);
3207 unsigned offset
= addr
& (PAGE_SIZE
-1);
3208 unsigned toread
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
3211 if (gpa
== UNMAPPED_GVA
) {
3212 r
= X86EMUL_PROPAGATE_FAULT
;
3215 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, toread
);
3217 r
= X86EMUL_UNHANDLEABLE
;
3229 /* used for instruction fetching */
3230 static int kvm_fetch_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
3231 struct kvm_vcpu
*vcpu
, u32
*error
)
3233 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3234 return kvm_read_guest_virt_helper(addr
, val
, bytes
, vcpu
,
3235 access
| PFERR_FETCH_MASK
, error
);
3238 static int kvm_read_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
3239 struct kvm_vcpu
*vcpu
, u32
*error
)
3241 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3242 return kvm_read_guest_virt_helper(addr
, val
, bytes
, vcpu
, access
,
3246 static int kvm_read_guest_virt_system(gva_t addr
, void *val
, unsigned int bytes
,
3247 struct kvm_vcpu
*vcpu
, u32
*error
)
3249 return kvm_read_guest_virt_helper(addr
, val
, bytes
, vcpu
, 0, error
);
3252 static int kvm_write_guest_virt_system(gva_t addr
, void *val
,
3254 struct kvm_vcpu
*vcpu
,
3258 int r
= X86EMUL_CONTINUE
;
3261 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
,
3262 PFERR_WRITE_MASK
, error
);
3263 unsigned offset
= addr
& (PAGE_SIZE
-1);
3264 unsigned towrite
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
3267 if (gpa
== UNMAPPED_GVA
) {
3268 r
= X86EMUL_PROPAGATE_FAULT
;
3271 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, data
, towrite
);
3273 r
= X86EMUL_UNHANDLEABLE
;
3285 static int emulator_read_emulated(unsigned long addr
,
3288 struct kvm_vcpu
*vcpu
)
3293 if (vcpu
->mmio_read_completed
) {
3294 memcpy(val
, vcpu
->mmio_data
, bytes
);
3295 trace_kvm_mmio(KVM_TRACE_MMIO_READ
, bytes
,
3296 vcpu
->mmio_phys_addr
, *(u64
*)val
);
3297 vcpu
->mmio_read_completed
= 0;
3298 return X86EMUL_CONTINUE
;
3301 gpa
= kvm_mmu_gva_to_gpa_read(vcpu
, addr
, &error_code
);
3303 if (gpa
== UNMAPPED_GVA
) {
3304 kvm_inject_page_fault(vcpu
, addr
, error_code
);
3305 return X86EMUL_PROPAGATE_FAULT
;
3308 /* For APIC access vmexit */
3309 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
3312 if (kvm_read_guest_virt(addr
, val
, bytes
, vcpu
, NULL
)
3313 == X86EMUL_CONTINUE
)
3314 return X86EMUL_CONTINUE
;
3318 * Is this MMIO handled locally?
3320 if (!vcpu_mmio_read(vcpu
, gpa
, bytes
, val
)) {
3321 trace_kvm_mmio(KVM_TRACE_MMIO_READ
, bytes
, gpa
, *(u64
*)val
);
3322 return X86EMUL_CONTINUE
;
3325 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED
, bytes
, gpa
, 0);
3327 vcpu
->mmio_needed
= 1;
3328 vcpu
->mmio_phys_addr
= gpa
;
3329 vcpu
->mmio_size
= bytes
;
3330 vcpu
->mmio_is_write
= 0;
3332 return X86EMUL_UNHANDLEABLE
;
3335 int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
3336 const void *val
, int bytes
)
3340 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
3343 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
, 1);
3347 static int emulator_write_emulated_onepage(unsigned long addr
,
3350 struct kvm_vcpu
*vcpu
)
3355 gpa
= kvm_mmu_gva_to_gpa_write(vcpu
, addr
, &error_code
);
3357 if (gpa
== UNMAPPED_GVA
) {
3358 kvm_inject_page_fault(vcpu
, addr
, error_code
);
3359 return X86EMUL_PROPAGATE_FAULT
;
3362 /* For APIC access vmexit */
3363 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
3366 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
3367 return X86EMUL_CONTINUE
;
3370 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE
, bytes
, gpa
, *(u64
*)val
);
3372 * Is this MMIO handled locally?
3374 if (!vcpu_mmio_write(vcpu
, gpa
, bytes
, val
))
3375 return X86EMUL_CONTINUE
;
3377 vcpu
->mmio_needed
= 1;
3378 vcpu
->mmio_phys_addr
= gpa
;
3379 vcpu
->mmio_size
= bytes
;
3380 vcpu
->mmio_is_write
= 1;
3381 memcpy(vcpu
->mmio_data
, val
, bytes
);
3383 return X86EMUL_CONTINUE
;
3386 int emulator_write_emulated(unsigned long addr
,
3389 struct kvm_vcpu
*vcpu
)
3391 /* Crossing a page boundary? */
3392 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
3395 now
= -addr
& ~PAGE_MASK
;
3396 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
3397 if (rc
!= X86EMUL_CONTINUE
)
3403 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
3405 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
3407 #define CMPXCHG_TYPE(t, ptr, old, new) \
3408 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3410 #ifdef CONFIG_X86_64
3411 # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3413 # define CMPXCHG64(ptr, old, new) \
3414 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
3417 static int emulator_cmpxchg_emulated(unsigned long addr
,
3421 struct kvm_vcpu
*vcpu
)
3428 /* guests cmpxchg8b have to be emulated atomically */
3429 if (bytes
> 8 || (bytes
& (bytes
- 1)))
3432 gpa
= kvm_mmu_gva_to_gpa_write(vcpu
, addr
, NULL
);
3434 if (gpa
== UNMAPPED_GVA
||
3435 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
3438 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
3441 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
3443 kaddr
= kmap_atomic(page
, KM_USER0
);
3444 kaddr
+= offset_in_page(gpa
);
3447 exchanged
= CMPXCHG_TYPE(u8
, kaddr
, old
, new);
3450 exchanged
= CMPXCHG_TYPE(u16
, kaddr
, old
, new);
3453 exchanged
= CMPXCHG_TYPE(u32
, kaddr
, old
, new);
3456 exchanged
= CMPXCHG64(kaddr
, old
, new);
3461 kunmap_atomic(kaddr
, KM_USER0
);
3462 kvm_release_page_dirty(page
);
3465 return X86EMUL_CMPXCHG_FAILED
;
3467 kvm_mmu_pte_write(vcpu
, gpa
, new, bytes
, 1);
3469 return X86EMUL_CONTINUE
;
3472 printk_once(KERN_WARNING
"kvm: emulating exchange as write\n");
3474 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
3477 static int kernel_pio(struct kvm_vcpu
*vcpu
, void *pd
)
3479 /* TODO: String I/O for in kernel device */
3482 if (vcpu
->arch
.pio
.in
)
3483 r
= kvm_io_bus_read(vcpu
->kvm
, KVM_PIO_BUS
, vcpu
->arch
.pio
.port
,
3484 vcpu
->arch
.pio
.size
, pd
);
3486 r
= kvm_io_bus_write(vcpu
->kvm
, KVM_PIO_BUS
,
3487 vcpu
->arch
.pio
.port
, vcpu
->arch
.pio
.size
,
3493 static int emulator_pio_in_emulated(int size
, unsigned short port
, void *val
,
3494 unsigned int count
, struct kvm_vcpu
*vcpu
)
3496 if (vcpu
->arch
.pio
.count
)
3499 trace_kvm_pio(1, port
, size
, 1);
3501 vcpu
->arch
.pio
.port
= port
;
3502 vcpu
->arch
.pio
.in
= 1;
3503 vcpu
->arch
.pio
.count
= count
;
3504 vcpu
->arch
.pio
.size
= size
;
3506 if (!kernel_pio(vcpu
, vcpu
->arch
.pio_data
)) {
3508 memcpy(val
, vcpu
->arch
.pio_data
, size
* count
);
3509 vcpu
->arch
.pio
.count
= 0;
3513 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
3514 vcpu
->run
->io
.direction
= KVM_EXIT_IO_IN
;
3515 vcpu
->run
->io
.size
= size
;
3516 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
3517 vcpu
->run
->io
.count
= count
;
3518 vcpu
->run
->io
.port
= port
;
3523 static int emulator_pio_out_emulated(int size
, unsigned short port
,
3524 const void *val
, unsigned int count
,
3525 struct kvm_vcpu
*vcpu
)
3527 trace_kvm_pio(0, port
, size
, 1);
3529 vcpu
->arch
.pio
.port
= port
;
3530 vcpu
->arch
.pio
.in
= 0;
3531 vcpu
->arch
.pio
.count
= count
;
3532 vcpu
->arch
.pio
.size
= size
;
3534 memcpy(vcpu
->arch
.pio_data
, val
, size
* count
);
3536 if (!kernel_pio(vcpu
, vcpu
->arch
.pio_data
)) {
3537 vcpu
->arch
.pio
.count
= 0;
3541 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
3542 vcpu
->run
->io
.direction
= KVM_EXIT_IO_OUT
;
3543 vcpu
->run
->io
.size
= size
;
3544 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
3545 vcpu
->run
->io
.count
= count
;
3546 vcpu
->run
->io
.port
= port
;
3551 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
3553 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
3556 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
3558 kvm_mmu_invlpg(vcpu
, address
);
3559 return X86EMUL_CONTINUE
;
3562 int emulate_clts(struct kvm_vcpu
*vcpu
)
3564 kvm_x86_ops
->set_cr0(vcpu
, kvm_read_cr0_bits(vcpu
, ~X86_CR0_TS
));
3565 kvm_x86_ops
->fpu_activate(vcpu
);
3566 return X86EMUL_CONTINUE
;
3569 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
3571 return kvm_get_dr(ctxt
->vcpu
, dr
, dest
);
3574 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
3576 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
3578 return kvm_set_dr(ctxt
->vcpu
, dr
, value
& mask
);
3581 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
3584 unsigned long rip
= kvm_rip_read(vcpu
);
3585 unsigned long rip_linear
;
3587 if (!printk_ratelimit())
3590 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
3592 kvm_read_guest_virt(rip_linear
, (void *)opcodes
, 4, vcpu
, NULL
);
3594 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3595 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
3597 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
3599 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
3601 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
3604 static unsigned long emulator_get_cr(int cr
, struct kvm_vcpu
*vcpu
)
3606 unsigned long value
;
3610 value
= kvm_read_cr0(vcpu
);
3613 value
= vcpu
->arch
.cr2
;
3616 value
= vcpu
->arch
.cr3
;
3619 value
= kvm_read_cr4(vcpu
);
3622 value
= kvm_get_cr8(vcpu
);
3625 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3632 static void emulator_set_cr(int cr
, unsigned long val
, struct kvm_vcpu
*vcpu
)
3636 kvm_set_cr0(vcpu
, mk_cr_64(kvm_read_cr0(vcpu
), val
));
3639 vcpu
->arch
.cr2
= val
;
3642 kvm_set_cr3(vcpu
, val
);
3645 kvm_set_cr4(vcpu
, mk_cr_64(kvm_read_cr4(vcpu
), val
));
3648 kvm_set_cr8(vcpu
, val
& 0xfUL
);
3651 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3655 static int emulator_get_cpl(struct kvm_vcpu
*vcpu
)
3657 return kvm_x86_ops
->get_cpl(vcpu
);
3660 static void emulator_get_gdt(struct desc_ptr
*dt
, struct kvm_vcpu
*vcpu
)
3662 kvm_x86_ops
->get_gdt(vcpu
, dt
);
3665 static bool emulator_get_cached_descriptor(struct desc_struct
*desc
, int seg
,
3666 struct kvm_vcpu
*vcpu
)
3668 struct kvm_segment var
;
3670 kvm_get_segment(vcpu
, &var
, seg
);
3677 set_desc_limit(desc
, var
.limit
);
3678 set_desc_base(desc
, (unsigned long)var
.base
);
3679 desc
->type
= var
.type
;
3681 desc
->dpl
= var
.dpl
;
3682 desc
->p
= var
.present
;
3683 desc
->avl
= var
.avl
;
3691 static void emulator_set_cached_descriptor(struct desc_struct
*desc
, int seg
,
3692 struct kvm_vcpu
*vcpu
)
3694 struct kvm_segment var
;
3696 /* needed to preserve selector */
3697 kvm_get_segment(vcpu
, &var
, seg
);
3699 var
.base
= get_desc_base(desc
);
3700 var
.limit
= get_desc_limit(desc
);
3702 var
.limit
= (var
.limit
<< 12) | 0xfff;
3703 var
.type
= desc
->type
;
3704 var
.present
= desc
->p
;
3705 var
.dpl
= desc
->dpl
;
3710 var
.avl
= desc
->avl
;
3711 var
.present
= desc
->p
;
3712 var
.unusable
= !var
.present
;
3715 kvm_set_segment(vcpu
, &var
, seg
);
3719 static u16
emulator_get_segment_selector(int seg
, struct kvm_vcpu
*vcpu
)
3721 struct kvm_segment kvm_seg
;
3723 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3724 return kvm_seg
.selector
;
3727 static void emulator_set_segment_selector(u16 sel
, int seg
,
3728 struct kvm_vcpu
*vcpu
)
3730 struct kvm_segment kvm_seg
;
3732 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3733 kvm_seg
.selector
= sel
;
3734 kvm_set_segment(vcpu
, &kvm_seg
, seg
);
3737 static void emulator_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
3739 kvm_x86_ops
->set_rflags(vcpu
, rflags
);
3742 static struct x86_emulate_ops emulate_ops
= {
3743 .read_std
= kvm_read_guest_virt_system
,
3744 .write_std
= kvm_write_guest_virt_system
,
3745 .fetch
= kvm_fetch_guest_virt
,
3746 .read_emulated
= emulator_read_emulated
,
3747 .write_emulated
= emulator_write_emulated
,
3748 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
3749 .pio_in_emulated
= emulator_pio_in_emulated
,
3750 .pio_out_emulated
= emulator_pio_out_emulated
,
3751 .get_cached_descriptor
= emulator_get_cached_descriptor
,
3752 .set_cached_descriptor
= emulator_set_cached_descriptor
,
3753 .get_segment_selector
= emulator_get_segment_selector
,
3754 .set_segment_selector
= emulator_set_segment_selector
,
3755 .get_gdt
= emulator_get_gdt
,
3756 .get_cr
= emulator_get_cr
,
3757 .set_cr
= emulator_set_cr
,
3758 .cpl
= emulator_get_cpl
,
3759 .set_rflags
= emulator_set_rflags
,
3762 static void cache_all_regs(struct kvm_vcpu
*vcpu
)
3764 kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3765 kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3766 kvm_register_read(vcpu
, VCPU_REGS_RIP
);
3767 vcpu
->arch
.regs_dirty
= ~0;
3770 int emulate_instruction(struct kvm_vcpu
*vcpu
,
3776 struct decode_cache
*c
;
3777 struct kvm_run
*run
= vcpu
->run
;
3779 kvm_clear_exception_queue(vcpu
);
3780 vcpu
->arch
.mmio_fault_cr2
= cr2
;
3782 * TODO: fix emulate.c to use guest_read/write_register
3783 * instead of direct ->regs accesses, can save hundred cycles
3784 * on Intel for instructions that don't read/change RSP, for
3787 cache_all_regs(vcpu
);
3789 vcpu
->mmio_is_write
= 0;
3791 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
3793 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
3795 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
3796 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3797 vcpu
->arch
.emulate_ctxt
.eip
= kvm_rip_read(vcpu
);
3798 vcpu
->arch
.emulate_ctxt
.mode
=
3799 (!is_protmode(vcpu
)) ? X86EMUL_MODE_REAL
:
3800 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
3801 ? X86EMUL_MODE_VM86
: cs_l
3802 ? X86EMUL_MODE_PROT64
: cs_db
3803 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
3805 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
3806 trace_kvm_emulate_insn_start(vcpu
);
3808 /* Only allow emulation of specific instructions on #UD
3809 * (namely VMMCALL, sysenter, sysexit, syscall)*/
3810 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
3811 if (emulation_type
& EMULTYPE_TRAP_UD
) {
3813 return EMULATE_FAIL
;
3815 case 0x01: /* VMMCALL */
3816 if (c
->modrm_mod
!= 3 || c
->modrm_rm
!= 1)
3817 return EMULATE_FAIL
;
3819 case 0x34: /* sysenter */
3820 case 0x35: /* sysexit */
3821 if (c
->modrm_mod
!= 0 || c
->modrm_rm
!= 0)
3822 return EMULATE_FAIL
;
3824 case 0x05: /* syscall */
3825 if (c
->modrm_mod
!= 0 || c
->modrm_rm
!= 0)
3826 return EMULATE_FAIL
;
3829 return EMULATE_FAIL
;
3832 if (!(c
->modrm_reg
== 0 || c
->modrm_reg
== 3))
3833 return EMULATE_FAIL
;
3836 ++vcpu
->stat
.insn_emulation
;
3838 ++vcpu
->stat
.insn_emulation_fail
;
3839 trace_kvm_emulate_insn_failed(vcpu
);
3840 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
3841 return EMULATE_DONE
;
3842 return EMULATE_FAIL
;
3846 if (emulation_type
& EMULTYPE_SKIP
) {
3847 kvm_rip_write(vcpu
, vcpu
->arch
.emulate_ctxt
.decode
.eip
);
3848 return EMULATE_DONE
;
3852 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
3853 shadow_mask
= vcpu
->arch
.emulate_ctxt
.interruptibility
;
3856 kvm_x86_ops
->set_interrupt_shadow(vcpu
, shadow_mask
);
3858 if (vcpu
->arch
.pio
.count
) {
3859 if (!vcpu
->arch
.pio
.in
)
3860 vcpu
->arch
.pio
.count
= 0;
3861 return EMULATE_DO_MMIO
;
3864 if (r
|| vcpu
->mmio_is_write
) {
3865 run
->exit_reason
= KVM_EXIT_MMIO
;
3866 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
3867 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
3868 run
->mmio
.len
= vcpu
->mmio_size
;
3869 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
3873 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
3875 if (!vcpu
->mmio_needed
) {
3876 ++vcpu
->stat
.insn_emulation_fail
;
3877 trace_kvm_emulate_insn_failed(vcpu
);
3878 kvm_report_emulation_failure(vcpu
, "mmio");
3879 return EMULATE_FAIL
;
3881 return EMULATE_DO_MMIO
;
3884 if (vcpu
->mmio_is_write
) {
3885 vcpu
->mmio_needed
= 0;
3886 return EMULATE_DO_MMIO
;
3890 if (vcpu
->arch
.exception
.pending
)
3891 vcpu
->arch
.emulate_ctxt
.restart
= false;
3893 if (vcpu
->arch
.emulate_ctxt
.restart
)
3896 return EMULATE_DONE
;
3898 EXPORT_SYMBOL_GPL(emulate_instruction
);
3900 int kvm_fast_pio_out(struct kvm_vcpu
*vcpu
, int size
, unsigned short port
)
3902 unsigned long val
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3903 int ret
= emulator_pio_out_emulated(size
, port
, &val
, 1, vcpu
);
3904 /* do not return to emulator after return from userspace */
3905 vcpu
->arch
.pio
.count
= 0;
3908 EXPORT_SYMBOL_GPL(kvm_fast_pio_out
);
3910 static void bounce_off(void *info
)
3915 static int kvmclock_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
3918 struct cpufreq_freqs
*freq
= data
;
3920 struct kvm_vcpu
*vcpu
;
3921 int i
, send_ipi
= 0;
3923 if (val
== CPUFREQ_PRECHANGE
&& freq
->old
> freq
->new)
3925 if (val
== CPUFREQ_POSTCHANGE
&& freq
->old
< freq
->new)
3927 per_cpu(cpu_tsc_khz
, freq
->cpu
) = freq
->new;
3929 spin_lock(&kvm_lock
);
3930 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
3931 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
3932 if (vcpu
->cpu
!= freq
->cpu
)
3934 if (!kvm_request_guest_time_update(vcpu
))
3936 if (vcpu
->cpu
!= smp_processor_id())
3940 spin_unlock(&kvm_lock
);
3942 if (freq
->old
< freq
->new && send_ipi
) {
3944 * We upscale the frequency. Must make the guest
3945 * doesn't see old kvmclock values while running with
3946 * the new frequency, otherwise we risk the guest sees
3947 * time go backwards.
3949 * In case we update the frequency for another cpu
3950 * (which might be in guest context) send an interrupt
3951 * to kick the cpu out of guest context. Next time
3952 * guest context is entered kvmclock will be updated,
3953 * so the guest will not see stale values.
3955 smp_call_function_single(freq
->cpu
, bounce_off
, NULL
, 1);
3960 static struct notifier_block kvmclock_cpufreq_notifier_block
= {
3961 .notifier_call
= kvmclock_cpufreq_notifier
3964 static void kvm_timer_init(void)
3968 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
)) {
3969 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block
,
3970 CPUFREQ_TRANSITION_NOTIFIER
);
3971 for_each_online_cpu(cpu
) {
3972 unsigned long khz
= cpufreq_get(cpu
);
3975 per_cpu(cpu_tsc_khz
, cpu
) = khz
;
3978 for_each_possible_cpu(cpu
)
3979 per_cpu(cpu_tsc_khz
, cpu
) = tsc_khz
;
3983 static DEFINE_PER_CPU(struct kvm_vcpu
*, current_vcpu
);
3985 static int kvm_is_in_guest(void)
3987 return percpu_read(current_vcpu
) != NULL
;
3990 static int kvm_is_user_mode(void)
3994 if (percpu_read(current_vcpu
))
3995 user_mode
= kvm_x86_ops
->get_cpl(percpu_read(current_vcpu
));
3997 return user_mode
!= 0;
4000 static unsigned long kvm_get_guest_ip(void)
4002 unsigned long ip
= 0;
4004 if (percpu_read(current_vcpu
))
4005 ip
= kvm_rip_read(percpu_read(current_vcpu
));
4010 static struct perf_guest_info_callbacks kvm_guest_cbs
= {
4011 .is_in_guest
= kvm_is_in_guest
,
4012 .is_user_mode
= kvm_is_user_mode
,
4013 .get_guest_ip
= kvm_get_guest_ip
,
4016 void kvm_before_handle_nmi(struct kvm_vcpu
*vcpu
)
4018 percpu_write(current_vcpu
, vcpu
);
4020 EXPORT_SYMBOL_GPL(kvm_before_handle_nmi
);
4022 void kvm_after_handle_nmi(struct kvm_vcpu
*vcpu
)
4024 percpu_write(current_vcpu
, NULL
);
4026 EXPORT_SYMBOL_GPL(kvm_after_handle_nmi
);
4028 int kvm_arch_init(void *opaque
)
4031 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
4034 printk(KERN_ERR
"kvm: already loaded the other module\n");
4039 if (!ops
->cpu_has_kvm_support()) {
4040 printk(KERN_ERR
"kvm: no hardware support\n");
4044 if (ops
->disabled_by_bios()) {
4045 printk(KERN_ERR
"kvm: disabled by bios\n");
4050 r
= kvm_mmu_module_init();
4054 kvm_init_msr_list();
4057 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
4058 kvm_mmu_set_base_ptes(PT_PRESENT_MASK
);
4059 kvm_mmu_set_mask_ptes(PT_USER_MASK
, PT_ACCESSED_MASK
,
4060 PT_DIRTY_MASK
, PT64_NX_MASK
, 0);
4064 perf_register_guest_info_callbacks(&kvm_guest_cbs
);
4072 void kvm_arch_exit(void)
4074 perf_unregister_guest_info_callbacks(&kvm_guest_cbs
);
4076 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
))
4077 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block
,
4078 CPUFREQ_TRANSITION_NOTIFIER
);
4080 kvm_mmu_module_exit();
4083 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
4085 ++vcpu
->stat
.halt_exits
;
4086 if (irqchip_in_kernel(vcpu
->kvm
)) {
4087 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
4090 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
4094 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
4096 static inline gpa_t
hc_gpa(struct kvm_vcpu
*vcpu
, unsigned long a0
,
4099 if (is_long_mode(vcpu
))
4102 return a0
| ((gpa_t
)a1
<< 32);
4105 int kvm_hv_hypercall(struct kvm_vcpu
*vcpu
)
4107 u64 param
, ingpa
, outgpa
, ret
;
4108 uint16_t code
, rep_idx
, rep_cnt
, res
= HV_STATUS_SUCCESS
, rep_done
= 0;
4109 bool fast
, longmode
;
4113 * hypercall generates UD from non zero cpl and real mode
4116 if (kvm_x86_ops
->get_cpl(vcpu
) != 0 || !is_protmode(vcpu
)) {
4117 kvm_queue_exception(vcpu
, UD_VECTOR
);
4121 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
4122 longmode
= is_long_mode(vcpu
) && cs_l
== 1;
4125 param
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RDX
) << 32) |
4126 (kvm_register_read(vcpu
, VCPU_REGS_RAX
) & 0xffffffff);
4127 ingpa
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RBX
) << 32) |
4128 (kvm_register_read(vcpu
, VCPU_REGS_RCX
) & 0xffffffff);
4129 outgpa
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RDI
) << 32) |
4130 (kvm_register_read(vcpu
, VCPU_REGS_RSI
) & 0xffffffff);
4132 #ifdef CONFIG_X86_64
4134 param
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4135 ingpa
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
4136 outgpa
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
4140 code
= param
& 0xffff;
4141 fast
= (param
>> 16) & 0x1;
4142 rep_cnt
= (param
>> 32) & 0xfff;
4143 rep_idx
= (param
>> 48) & 0xfff;
4145 trace_kvm_hv_hypercall(code
, fast
, rep_cnt
, rep_idx
, ingpa
, outgpa
);
4148 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT
:
4149 kvm_vcpu_on_spin(vcpu
);
4152 res
= HV_STATUS_INVALID_HYPERCALL_CODE
;
4156 ret
= res
| (((u64
)rep_done
& 0xfff) << 32);
4158 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
);
4160 kvm_register_write(vcpu
, VCPU_REGS_RDX
, ret
>> 32);
4161 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
& 0xffffffff);
4167 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
4169 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
4172 if (kvm_hv_hypercall_enabled(vcpu
->kvm
))
4173 return kvm_hv_hypercall(vcpu
);
4175 nr
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4176 a0
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
4177 a1
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4178 a2
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
4179 a3
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
4181 trace_kvm_hypercall(nr
, a0
, a1
, a2
, a3
);
4183 if (!is_long_mode(vcpu
)) {
4191 if (kvm_x86_ops
->get_cpl(vcpu
) != 0) {
4197 case KVM_HC_VAPIC_POLL_IRQ
:
4201 r
= kvm_pv_mmu_op(vcpu
, a0
, hc_gpa(vcpu
, a1
, a2
), &ret
);
4208 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
);
4209 ++vcpu
->stat
.hypercalls
;
4212 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
4214 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
4216 char instruction
[3];
4217 unsigned long rip
= kvm_rip_read(vcpu
);
4220 * Blow out the MMU to ensure that no other VCPU has an active mapping
4221 * to ensure that the updated hypercall appears atomically across all
4224 kvm_mmu_zap_all(vcpu
->kvm
);
4226 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
4228 return emulator_write_emulated(rip
, instruction
, 3, vcpu
);
4231 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
4233 struct desc_ptr dt
= { limit
, base
};
4235 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
4238 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
4240 struct desc_ptr dt
= { limit
, base
};
4242 kvm_x86_ops
->set_idt(vcpu
, &dt
);
4245 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
4247 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
4248 int j
, nent
= vcpu
->arch
.cpuid_nent
;
4250 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
4251 /* when no next entry is found, the current entry[i] is reselected */
4252 for (j
= i
+ 1; ; j
= (j
+ 1) % nent
) {
4253 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
4254 if (ej
->function
== e
->function
) {
4255 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
4259 return 0; /* silence gcc, even though control never reaches here */
4262 /* find an entry with matching function, matching index (if needed), and that
4263 * should be read next (if it's stateful) */
4264 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
4265 u32 function
, u32 index
)
4267 if (e
->function
!= function
)
4269 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
4271 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
4272 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
4277 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
4278 u32 function
, u32 index
)
4281 struct kvm_cpuid_entry2
*best
= NULL
;
4283 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
4284 struct kvm_cpuid_entry2
*e
;
4286 e
= &vcpu
->arch
.cpuid_entries
[i
];
4287 if (is_matching_cpuid_entry(e
, function
, index
)) {
4288 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
4289 move_to_next_stateful_cpuid_entry(vcpu
, i
);
4294 * Both basic or both extended?
4296 if (((e
->function
^ function
) & 0x80000000) == 0)
4297 if (!best
|| e
->function
> best
->function
)
4302 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry
);
4304 int cpuid_maxphyaddr(struct kvm_vcpu
*vcpu
)
4306 struct kvm_cpuid_entry2
*best
;
4308 best
= kvm_find_cpuid_entry(vcpu
, 0x80000000, 0);
4309 if (!best
|| best
->eax
< 0x80000008)
4311 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
4313 return best
->eax
& 0xff;
4318 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
4320 u32 function
, index
;
4321 struct kvm_cpuid_entry2
*best
;
4323 function
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4324 index
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4325 kvm_register_write(vcpu
, VCPU_REGS_RAX
, 0);
4326 kvm_register_write(vcpu
, VCPU_REGS_RBX
, 0);
4327 kvm_register_write(vcpu
, VCPU_REGS_RCX
, 0);
4328 kvm_register_write(vcpu
, VCPU_REGS_RDX
, 0);
4329 best
= kvm_find_cpuid_entry(vcpu
, function
, index
);
4331 kvm_register_write(vcpu
, VCPU_REGS_RAX
, best
->eax
);
4332 kvm_register_write(vcpu
, VCPU_REGS_RBX
, best
->ebx
);
4333 kvm_register_write(vcpu
, VCPU_REGS_RCX
, best
->ecx
);
4334 kvm_register_write(vcpu
, VCPU_REGS_RDX
, best
->edx
);
4336 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
4337 trace_kvm_cpuid(function
,
4338 kvm_register_read(vcpu
, VCPU_REGS_RAX
),
4339 kvm_register_read(vcpu
, VCPU_REGS_RBX
),
4340 kvm_register_read(vcpu
, VCPU_REGS_RCX
),
4341 kvm_register_read(vcpu
, VCPU_REGS_RDX
));
4343 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
4346 * Check if userspace requested an interrupt window, and that the
4347 * interrupt window is open.
4349 * No need to exit to userspace if we already have an interrupt queued.
4351 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
)
4353 return (!irqchip_in_kernel(vcpu
->kvm
) && !kvm_cpu_has_interrupt(vcpu
) &&
4354 vcpu
->run
->request_interrupt_window
&&
4355 kvm_arch_interrupt_allowed(vcpu
));
4358 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
)
4360 struct kvm_run
*kvm_run
= vcpu
->run
;
4362 kvm_run
->if_flag
= (kvm_get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
4363 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
4364 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
4365 if (irqchip_in_kernel(vcpu
->kvm
))
4366 kvm_run
->ready_for_interrupt_injection
= 1;
4368 kvm_run
->ready_for_interrupt_injection
=
4369 kvm_arch_interrupt_allowed(vcpu
) &&
4370 !kvm_cpu_has_interrupt(vcpu
) &&
4371 !kvm_event_needs_reinjection(vcpu
);
4374 static void vapic_enter(struct kvm_vcpu
*vcpu
)
4376 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
4379 if (!apic
|| !apic
->vapic_addr
)
4382 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
4384 vcpu
->arch
.apic
->vapic_page
= page
;
4387 static void vapic_exit(struct kvm_vcpu
*vcpu
)
4389 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
4392 if (!apic
|| !apic
->vapic_addr
)
4395 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4396 kvm_release_page_dirty(apic
->vapic_page
);
4397 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
4398 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
4401 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
)
4405 if (!kvm_x86_ops
->update_cr8_intercept
)
4408 if (!vcpu
->arch
.apic
)
4411 if (!vcpu
->arch
.apic
->vapic_addr
)
4412 max_irr
= kvm_lapic_find_highest_irr(vcpu
);
4419 tpr
= kvm_lapic_get_cr8(vcpu
);
4421 kvm_x86_ops
->update_cr8_intercept(vcpu
, tpr
, max_irr
);
4424 static void inject_pending_event(struct kvm_vcpu
*vcpu
)
4426 /* try to reinject previous events if any */
4427 if (vcpu
->arch
.exception
.pending
) {
4428 trace_kvm_inj_exception(vcpu
->arch
.exception
.nr
,
4429 vcpu
->arch
.exception
.has_error_code
,
4430 vcpu
->arch
.exception
.error_code
);
4431 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
4432 vcpu
->arch
.exception
.has_error_code
,
4433 vcpu
->arch
.exception
.error_code
,
4434 vcpu
->arch
.exception
.reinject
);
4438 if (vcpu
->arch
.nmi_injected
) {
4439 kvm_x86_ops
->set_nmi(vcpu
);
4443 if (vcpu
->arch
.interrupt
.pending
) {
4444 kvm_x86_ops
->set_irq(vcpu
);
4448 /* try to inject new event if pending */
4449 if (vcpu
->arch
.nmi_pending
) {
4450 if (kvm_x86_ops
->nmi_allowed(vcpu
)) {
4451 vcpu
->arch
.nmi_pending
= false;
4452 vcpu
->arch
.nmi_injected
= true;
4453 kvm_x86_ops
->set_nmi(vcpu
);
4455 } else if (kvm_cpu_has_interrupt(vcpu
)) {
4456 if (kvm_x86_ops
->interrupt_allowed(vcpu
)) {
4457 kvm_queue_interrupt(vcpu
, kvm_cpu_get_interrupt(vcpu
),
4459 kvm_x86_ops
->set_irq(vcpu
);
4464 static int vcpu_enter_guest(struct kvm_vcpu
*vcpu
)
4467 bool req_int_win
= !irqchip_in_kernel(vcpu
->kvm
) &&
4468 vcpu
->run
->request_interrupt_window
;
4471 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
4472 kvm_mmu_unload(vcpu
);
4474 r
= kvm_mmu_reload(vcpu
);
4478 if (vcpu
->requests
) {
4479 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
4480 __kvm_migrate_timers(vcpu
);
4481 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE
, &vcpu
->requests
))
4482 kvm_write_guest_time(vcpu
);
4483 if (test_and_clear_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
))
4484 kvm_mmu_sync_roots(vcpu
);
4485 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
4486 kvm_x86_ops
->tlb_flush(vcpu
);
4487 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
4489 vcpu
->run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
4493 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
4494 vcpu
->run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
4498 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU
, &vcpu
->requests
)) {
4499 vcpu
->fpu_active
= 0;
4500 kvm_x86_ops
->fpu_deactivate(vcpu
);
4506 kvm_x86_ops
->prepare_guest_switch(vcpu
);
4507 if (vcpu
->fpu_active
)
4508 kvm_load_guest_fpu(vcpu
);
4510 local_irq_disable();
4512 clear_bit(KVM_REQ_KICK
, &vcpu
->requests
);
4513 smp_mb__after_clear_bit();
4515 if (vcpu
->requests
|| need_resched() || signal_pending(current
)) {
4516 set_bit(KVM_REQ_KICK
, &vcpu
->requests
);
4523 inject_pending_event(vcpu
);
4525 /* enable NMI/IRQ window open exits if needed */
4526 if (vcpu
->arch
.nmi_pending
)
4527 kvm_x86_ops
->enable_nmi_window(vcpu
);
4528 else if (kvm_cpu_has_interrupt(vcpu
) || req_int_win
)
4529 kvm_x86_ops
->enable_irq_window(vcpu
);
4531 if (kvm_lapic_enabled(vcpu
)) {
4532 update_cr8_intercept(vcpu
);
4533 kvm_lapic_sync_to_vapic(vcpu
);
4536 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
4540 if (unlikely(vcpu
->arch
.switch_db_regs
)) {
4542 set_debugreg(vcpu
->arch
.eff_db
[0], 0);
4543 set_debugreg(vcpu
->arch
.eff_db
[1], 1);
4544 set_debugreg(vcpu
->arch
.eff_db
[2], 2);
4545 set_debugreg(vcpu
->arch
.eff_db
[3], 3);
4548 trace_kvm_entry(vcpu
->vcpu_id
);
4549 kvm_x86_ops
->run(vcpu
);
4552 * If the guest has used debug registers, at least dr7
4553 * will be disabled while returning to the host.
4554 * If we don't have active breakpoints in the host, we don't
4555 * care about the messed up debug address registers. But if
4556 * we have some of them active, restore the old state.
4558 if (hw_breakpoint_active())
4559 hw_breakpoint_restore();
4561 set_bit(KVM_REQ_KICK
, &vcpu
->requests
);
4567 * We must have an instruction between local_irq_enable() and
4568 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4569 * the interrupt shadow. The stat.exits increment will do nicely.
4570 * But we need to prevent reordering, hence this barrier():
4578 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4581 * Profile KVM exit RIPs:
4583 if (unlikely(prof_on
== KVM_PROFILING
)) {
4584 unsigned long rip
= kvm_rip_read(vcpu
);
4585 profile_hit(KVM_PROFILING
, (void *)rip
);
4589 kvm_lapic_sync_from_vapic(vcpu
);
4591 r
= kvm_x86_ops
->handle_exit(vcpu
);
4597 static int __vcpu_run(struct kvm_vcpu
*vcpu
)
4600 struct kvm
*kvm
= vcpu
->kvm
;
4602 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
)) {
4603 pr_debug("vcpu %d received sipi with vector # %x\n",
4604 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
4605 kvm_lapic_reset(vcpu
);
4606 r
= kvm_arch_vcpu_reset(vcpu
);
4609 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4612 vcpu
->srcu_idx
= srcu_read_lock(&kvm
->srcu
);
4617 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
)
4618 r
= vcpu_enter_guest(vcpu
);
4620 srcu_read_unlock(&kvm
->srcu
, vcpu
->srcu_idx
);
4621 kvm_vcpu_block(vcpu
);
4622 vcpu
->srcu_idx
= srcu_read_lock(&kvm
->srcu
);
4623 if (test_and_clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
))
4625 switch(vcpu
->arch
.mp_state
) {
4626 case KVM_MP_STATE_HALTED
:
4627 vcpu
->arch
.mp_state
=
4628 KVM_MP_STATE_RUNNABLE
;
4629 case KVM_MP_STATE_RUNNABLE
:
4631 case KVM_MP_STATE_SIPI_RECEIVED
:
4642 clear_bit(KVM_REQ_PENDING_TIMER
, &vcpu
->requests
);
4643 if (kvm_cpu_has_pending_timer(vcpu
))
4644 kvm_inject_pending_timer_irqs(vcpu
);
4646 if (dm_request_for_irq_injection(vcpu
)) {
4648 vcpu
->run
->exit_reason
= KVM_EXIT_INTR
;
4649 ++vcpu
->stat
.request_irq_exits
;
4651 if (signal_pending(current
)) {
4653 vcpu
->run
->exit_reason
= KVM_EXIT_INTR
;
4654 ++vcpu
->stat
.signal_exits
;
4656 if (need_resched()) {
4657 srcu_read_unlock(&kvm
->srcu
, vcpu
->srcu_idx
);
4659 vcpu
->srcu_idx
= srcu_read_lock(&kvm
->srcu
);
4663 srcu_read_unlock(&kvm
->srcu
, vcpu
->srcu_idx
);
4670 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
4677 if (vcpu
->sigset_active
)
4678 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
4680 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
4681 kvm_vcpu_block(vcpu
);
4682 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
4687 /* re-sync apic's tpr */
4688 if (!irqchip_in_kernel(vcpu
->kvm
))
4689 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
4691 if (vcpu
->arch
.pio
.count
|| vcpu
->mmio_needed
||
4692 vcpu
->arch
.emulate_ctxt
.restart
) {
4693 if (vcpu
->mmio_needed
) {
4694 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
4695 vcpu
->mmio_read_completed
= 1;
4696 vcpu
->mmio_needed
= 0;
4698 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4699 r
= emulate_instruction(vcpu
, 0, 0, EMULTYPE_NO_DECODE
);
4700 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
4701 if (r
== EMULATE_DO_MMIO
) {
4706 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
)
4707 kvm_register_write(vcpu
, VCPU_REGS_RAX
,
4708 kvm_run
->hypercall
.ret
);
4710 r
= __vcpu_run(vcpu
);
4713 post_kvm_run_save(vcpu
);
4714 if (vcpu
->sigset_active
)
4715 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
4721 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
4725 regs
->rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4726 regs
->rbx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
4727 regs
->rcx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4728 regs
->rdx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
4729 regs
->rsi
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
4730 regs
->rdi
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
4731 regs
->rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
4732 regs
->rbp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
4733 #ifdef CONFIG_X86_64
4734 regs
->r8
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
4735 regs
->r9
= kvm_register_read(vcpu
, VCPU_REGS_R9
);
4736 regs
->r10
= kvm_register_read(vcpu
, VCPU_REGS_R10
);
4737 regs
->r11
= kvm_register_read(vcpu
, VCPU_REGS_R11
);
4738 regs
->r12
= kvm_register_read(vcpu
, VCPU_REGS_R12
);
4739 regs
->r13
= kvm_register_read(vcpu
, VCPU_REGS_R13
);
4740 regs
->r14
= kvm_register_read(vcpu
, VCPU_REGS_R14
);
4741 regs
->r15
= kvm_register_read(vcpu
, VCPU_REGS_R15
);
4744 regs
->rip
= kvm_rip_read(vcpu
);
4745 regs
->rflags
= kvm_get_rflags(vcpu
);
4752 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
4756 kvm_register_write(vcpu
, VCPU_REGS_RAX
, regs
->rax
);
4757 kvm_register_write(vcpu
, VCPU_REGS_RBX
, regs
->rbx
);
4758 kvm_register_write(vcpu
, VCPU_REGS_RCX
, regs
->rcx
);
4759 kvm_register_write(vcpu
, VCPU_REGS_RDX
, regs
->rdx
);
4760 kvm_register_write(vcpu
, VCPU_REGS_RSI
, regs
->rsi
);
4761 kvm_register_write(vcpu
, VCPU_REGS_RDI
, regs
->rdi
);
4762 kvm_register_write(vcpu
, VCPU_REGS_RSP
, regs
->rsp
);
4763 kvm_register_write(vcpu
, VCPU_REGS_RBP
, regs
->rbp
);
4764 #ifdef CONFIG_X86_64
4765 kvm_register_write(vcpu
, VCPU_REGS_R8
, regs
->r8
);
4766 kvm_register_write(vcpu
, VCPU_REGS_R9
, regs
->r9
);
4767 kvm_register_write(vcpu
, VCPU_REGS_R10
, regs
->r10
);
4768 kvm_register_write(vcpu
, VCPU_REGS_R11
, regs
->r11
);
4769 kvm_register_write(vcpu
, VCPU_REGS_R12
, regs
->r12
);
4770 kvm_register_write(vcpu
, VCPU_REGS_R13
, regs
->r13
);
4771 kvm_register_write(vcpu
, VCPU_REGS_R14
, regs
->r14
);
4772 kvm_register_write(vcpu
, VCPU_REGS_R15
, regs
->r15
);
4775 kvm_rip_write(vcpu
, regs
->rip
);
4776 kvm_set_rflags(vcpu
, regs
->rflags
);
4778 vcpu
->arch
.exception
.pending
= false;
4785 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
4787 struct kvm_segment cs
;
4789 kvm_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
4793 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
4795 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
4796 struct kvm_sregs
*sregs
)
4802 kvm_get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
4803 kvm_get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
4804 kvm_get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
4805 kvm_get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
4806 kvm_get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
4807 kvm_get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
4809 kvm_get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
4810 kvm_get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
4812 kvm_x86_ops
->get_idt(vcpu
, &dt
);
4813 sregs
->idt
.limit
= dt
.size
;
4814 sregs
->idt
.base
= dt
.address
;
4815 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
4816 sregs
->gdt
.limit
= dt
.size
;
4817 sregs
->gdt
.base
= dt
.address
;
4819 sregs
->cr0
= kvm_read_cr0(vcpu
);
4820 sregs
->cr2
= vcpu
->arch
.cr2
;
4821 sregs
->cr3
= vcpu
->arch
.cr3
;
4822 sregs
->cr4
= kvm_read_cr4(vcpu
);
4823 sregs
->cr8
= kvm_get_cr8(vcpu
);
4824 sregs
->efer
= vcpu
->arch
.efer
;
4825 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
4827 memset(sregs
->interrupt_bitmap
, 0, sizeof sregs
->interrupt_bitmap
);
4829 if (vcpu
->arch
.interrupt
.pending
&& !vcpu
->arch
.interrupt
.soft
)
4830 set_bit(vcpu
->arch
.interrupt
.nr
,
4831 (unsigned long *)sregs
->interrupt_bitmap
);
4838 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
4839 struct kvm_mp_state
*mp_state
)
4842 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
4847 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
4848 struct kvm_mp_state
*mp_state
)
4851 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
4856 int kvm_task_switch(struct kvm_vcpu
*vcpu
, u16 tss_selector
, int reason
,
4857 bool has_error_code
, u32 error_code
)
4859 int cs_db
, cs_l
, ret
;
4860 cache_all_regs(vcpu
);
4862 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
4864 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
4865 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
4866 vcpu
->arch
.emulate_ctxt
.eip
= kvm_rip_read(vcpu
);
4867 vcpu
->arch
.emulate_ctxt
.mode
=
4868 (!is_protmode(vcpu
)) ? X86EMUL_MODE_REAL
:
4869 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
4870 ? X86EMUL_MODE_VM86
: cs_l
4871 ? X86EMUL_MODE_PROT64
: cs_db
4872 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
4874 ret
= emulator_task_switch(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
,
4875 tss_selector
, reason
, has_error_code
,
4879 return EMULATE_FAIL
;
4881 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
4882 return EMULATE_DONE
;
4884 EXPORT_SYMBOL_GPL(kvm_task_switch
);
4886 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
4887 struct kvm_sregs
*sregs
)
4889 int mmu_reset_needed
= 0;
4890 int pending_vec
, max_bits
;
4895 dt
.size
= sregs
->idt
.limit
;
4896 dt
.address
= sregs
->idt
.base
;
4897 kvm_x86_ops
->set_idt(vcpu
, &dt
);
4898 dt
.size
= sregs
->gdt
.limit
;
4899 dt
.address
= sregs
->gdt
.base
;
4900 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
4902 vcpu
->arch
.cr2
= sregs
->cr2
;
4903 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
4904 vcpu
->arch
.cr3
= sregs
->cr3
;
4906 kvm_set_cr8(vcpu
, sregs
->cr8
);
4908 mmu_reset_needed
|= vcpu
->arch
.efer
!= sregs
->efer
;
4909 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
4910 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
4912 mmu_reset_needed
|= kvm_read_cr0(vcpu
) != sregs
->cr0
;
4913 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
4914 vcpu
->arch
.cr0
= sregs
->cr0
;
4916 mmu_reset_needed
|= kvm_read_cr4(vcpu
) != sregs
->cr4
;
4917 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
4918 if (!is_long_mode(vcpu
) && is_pae(vcpu
)) {
4919 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
4920 mmu_reset_needed
= 1;
4923 if (mmu_reset_needed
)
4924 kvm_mmu_reset_context(vcpu
);
4926 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
4927 pending_vec
= find_first_bit(
4928 (const unsigned long *)sregs
->interrupt_bitmap
, max_bits
);
4929 if (pending_vec
< max_bits
) {
4930 kvm_queue_interrupt(vcpu
, pending_vec
, false);
4931 pr_debug("Set back pending irq %d\n", pending_vec
);
4932 if (irqchip_in_kernel(vcpu
->kvm
))
4933 kvm_pic_clear_isr_ack(vcpu
->kvm
);
4936 kvm_set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
4937 kvm_set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
4938 kvm_set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
4939 kvm_set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
4940 kvm_set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
4941 kvm_set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
4943 kvm_set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
4944 kvm_set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
4946 update_cr8_intercept(vcpu
);
4948 /* Older userspace won't unhalt the vcpu on reset. */
4949 if (kvm_vcpu_is_bsp(vcpu
) && kvm_rip_read(vcpu
) == 0xfff0 &&
4950 sregs
->cs
.selector
== 0xf000 && sregs
->cs
.base
== 0xffff0000 &&
4952 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4959 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
4960 struct kvm_guest_debug
*dbg
)
4962 unsigned long rflags
;
4967 if (dbg
->control
& (KVM_GUESTDBG_INJECT_DB
| KVM_GUESTDBG_INJECT_BP
)) {
4969 if (vcpu
->arch
.exception
.pending
)
4971 if (dbg
->control
& KVM_GUESTDBG_INJECT_DB
)
4972 kvm_queue_exception(vcpu
, DB_VECTOR
);
4974 kvm_queue_exception(vcpu
, BP_VECTOR
);
4978 * Read rflags as long as potentially injected trace flags are still
4981 rflags
= kvm_get_rflags(vcpu
);
4983 vcpu
->guest_debug
= dbg
->control
;
4984 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
))
4985 vcpu
->guest_debug
= 0;
4987 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
4988 for (i
= 0; i
< KVM_NR_DB_REGS
; ++i
)
4989 vcpu
->arch
.eff_db
[i
] = dbg
->arch
.debugreg
[i
];
4990 vcpu
->arch
.switch_db_regs
=
4991 (dbg
->arch
.debugreg
[7] & DR7_BP_EN_MASK
);
4993 for (i
= 0; i
< KVM_NR_DB_REGS
; i
++)
4994 vcpu
->arch
.eff_db
[i
] = vcpu
->arch
.db
[i
];
4995 vcpu
->arch
.switch_db_regs
= (vcpu
->arch
.dr7
& DR7_BP_EN_MASK
);
4998 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
4999 vcpu
->arch
.singlestep_rip
= kvm_rip_read(vcpu
) +
5000 get_segment_base(vcpu
, VCPU_SREG_CS
);
5003 * Trigger an rflags update that will inject or remove the trace
5006 kvm_set_rflags(vcpu
, rflags
);
5008 kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
5019 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5020 * we have asm/x86/processor.h
5031 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5032 #ifdef CONFIG_X86_64
5033 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5035 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5040 * Translate a guest virtual address to a guest physical address.
5042 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
5043 struct kvm_translation
*tr
)
5045 unsigned long vaddr
= tr
->linear_address
;
5050 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
5051 gpa
= kvm_mmu_gva_to_gpa_system(vcpu
, vaddr
, NULL
);
5052 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
5053 tr
->physical_address
= gpa
;
5054 tr
->valid
= gpa
!= UNMAPPED_GVA
;
5062 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
5064 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
5068 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
5069 fpu
->fcw
= fxsave
->cwd
;
5070 fpu
->fsw
= fxsave
->swd
;
5071 fpu
->ftwx
= fxsave
->twd
;
5072 fpu
->last_opcode
= fxsave
->fop
;
5073 fpu
->last_ip
= fxsave
->rip
;
5074 fpu
->last_dp
= fxsave
->rdp
;
5075 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
5082 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
5084 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
5088 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
5089 fxsave
->cwd
= fpu
->fcw
;
5090 fxsave
->swd
= fpu
->fsw
;
5091 fxsave
->twd
= fpu
->ftwx
;
5092 fxsave
->fop
= fpu
->last_opcode
;
5093 fxsave
->rip
= fpu
->last_ip
;
5094 fxsave
->rdp
= fpu
->last_dp
;
5095 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
5102 void fx_init(struct kvm_vcpu
*vcpu
)
5104 unsigned after_mxcsr_mask
;
5107 * Touch the fpu the first time in non atomic context as if
5108 * this is the first fpu instruction the exception handler
5109 * will fire before the instruction returns and it'll have to
5110 * allocate ram with GFP_KERNEL.
5113 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
5115 /* Initialize guest FPU by resetting ours and saving into guest's */
5117 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
5119 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
5120 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
5123 vcpu
->arch
.cr0
|= X86_CR0_ET
;
5124 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
5125 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
5126 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
5127 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
5129 EXPORT_SYMBOL_GPL(fx_init
);
5131 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
5133 if (vcpu
->guest_fpu_loaded
)
5136 vcpu
->guest_fpu_loaded
= 1;
5137 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
5138 kvm_fx_restore(&vcpu
->arch
.guest_fx_image
);
5142 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
5144 if (!vcpu
->guest_fpu_loaded
)
5147 vcpu
->guest_fpu_loaded
= 0;
5148 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
5149 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
5150 ++vcpu
->stat
.fpu_reload
;
5151 set_bit(KVM_REQ_DEACTIVATE_FPU
, &vcpu
->requests
);
5155 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
5157 if (vcpu
->arch
.time_page
) {
5158 kvm_release_page_dirty(vcpu
->arch
.time_page
);
5159 vcpu
->arch
.time_page
= NULL
;
5162 kvm_x86_ops
->vcpu_free(vcpu
);
5165 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
5168 return kvm_x86_ops
->vcpu_create(kvm
, id
);
5171 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
5175 /* We do fxsave: this must be aligned. */
5176 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
5178 vcpu
->arch
.mtrr_state
.have_fixed
= 1;
5180 r
= kvm_arch_vcpu_reset(vcpu
);
5182 r
= kvm_mmu_setup(vcpu
);
5189 kvm_x86_ops
->vcpu_free(vcpu
);
5193 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
5196 kvm_mmu_unload(vcpu
);
5199 kvm_x86_ops
->vcpu_free(vcpu
);
5202 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
5204 vcpu
->arch
.nmi_pending
= false;
5205 vcpu
->arch
.nmi_injected
= false;
5207 vcpu
->arch
.switch_db_regs
= 0;
5208 memset(vcpu
->arch
.db
, 0, sizeof(vcpu
->arch
.db
));
5209 vcpu
->arch
.dr6
= DR6_FIXED_1
;
5210 vcpu
->arch
.dr7
= DR7_FIXED_1
;
5212 return kvm_x86_ops
->vcpu_reset(vcpu
);
5215 int kvm_arch_hardware_enable(void *garbage
)
5218 * Since this may be called from a hotplug notifcation,
5219 * we can't get the CPU frequency directly.
5221 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
)) {
5222 int cpu
= raw_smp_processor_id();
5223 per_cpu(cpu_tsc_khz
, cpu
) = 0;
5226 kvm_shared_msr_cpu_online();
5228 return kvm_x86_ops
->hardware_enable(garbage
);
5231 void kvm_arch_hardware_disable(void *garbage
)
5233 kvm_x86_ops
->hardware_disable(garbage
);
5234 drop_user_return_notifiers(garbage
);
5237 int kvm_arch_hardware_setup(void)
5239 return kvm_x86_ops
->hardware_setup();
5242 void kvm_arch_hardware_unsetup(void)
5244 kvm_x86_ops
->hardware_unsetup();
5247 void kvm_arch_check_processor_compat(void *rtn
)
5249 kvm_x86_ops
->check_processor_compatibility(rtn
);
5252 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
5258 BUG_ON(vcpu
->kvm
== NULL
);
5261 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
5262 if (!irqchip_in_kernel(kvm
) || kvm_vcpu_is_bsp(vcpu
))
5263 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
5265 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
5267 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
5272 vcpu
->arch
.pio_data
= page_address(page
);
5274 r
= kvm_mmu_create(vcpu
);
5276 goto fail_free_pio_data
;
5278 if (irqchip_in_kernel(kvm
)) {
5279 r
= kvm_create_lapic(vcpu
);
5281 goto fail_mmu_destroy
;
5284 vcpu
->arch
.mce_banks
= kzalloc(KVM_MAX_MCE_BANKS
* sizeof(u64
) * 4,
5286 if (!vcpu
->arch
.mce_banks
) {
5288 goto fail_free_lapic
;
5290 vcpu
->arch
.mcg_cap
= KVM_MAX_MCE_BANKS
;
5294 kvm_free_lapic(vcpu
);
5296 kvm_mmu_destroy(vcpu
);
5298 free_page((unsigned long)vcpu
->arch
.pio_data
);
5303 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
5307 kfree(vcpu
->arch
.mce_banks
);
5308 kvm_free_lapic(vcpu
);
5309 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
5310 kvm_mmu_destroy(vcpu
);
5311 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
5312 free_page((unsigned long)vcpu
->arch
.pio_data
);
5315 struct kvm
*kvm_arch_create_vm(void)
5317 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
5320 return ERR_PTR(-ENOMEM
);
5322 kvm
->arch
.aliases
= kzalloc(sizeof(struct kvm_mem_aliases
), GFP_KERNEL
);
5323 if (!kvm
->arch
.aliases
) {
5325 return ERR_PTR(-ENOMEM
);
5328 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
5329 INIT_LIST_HEAD(&kvm
->arch
.assigned_dev_head
);
5331 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5332 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID
, &kvm
->arch
.irq_sources_bitmap
);
5334 rdtscll(kvm
->arch
.vm_init_tsc
);
5339 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
5342 kvm_mmu_unload(vcpu
);
5346 static void kvm_free_vcpus(struct kvm
*kvm
)
5349 struct kvm_vcpu
*vcpu
;
5352 * Unpin any mmu pages first.
5354 kvm_for_each_vcpu(i
, vcpu
, kvm
)
5355 kvm_unload_vcpu_mmu(vcpu
);
5356 kvm_for_each_vcpu(i
, vcpu
, kvm
)
5357 kvm_arch_vcpu_free(vcpu
);
5359 mutex_lock(&kvm
->lock
);
5360 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
5361 kvm
->vcpus
[i
] = NULL
;
5363 atomic_set(&kvm
->online_vcpus
, 0);
5364 mutex_unlock(&kvm
->lock
);
5367 void kvm_arch_sync_events(struct kvm
*kvm
)
5369 kvm_free_all_assigned_devices(kvm
);
5372 void kvm_arch_destroy_vm(struct kvm
*kvm
)
5374 kvm_iommu_unmap_guest(kvm
);
5376 kfree(kvm
->arch
.vpic
);
5377 kfree(kvm
->arch
.vioapic
);
5378 kvm_free_vcpus(kvm
);
5379 kvm_free_physmem(kvm
);
5380 if (kvm
->arch
.apic_access_page
)
5381 put_page(kvm
->arch
.apic_access_page
);
5382 if (kvm
->arch
.ept_identity_pagetable
)
5383 put_page(kvm
->arch
.ept_identity_pagetable
);
5384 cleanup_srcu_struct(&kvm
->srcu
);
5385 kfree(kvm
->arch
.aliases
);
5389 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
5390 struct kvm_memory_slot
*memslot
,
5391 struct kvm_memory_slot old
,
5392 struct kvm_userspace_memory_region
*mem
,
5395 int npages
= memslot
->npages
;
5397 /*To keep backward compatibility with older userspace,
5398 *x86 needs to hanlde !user_alloc case.
5401 if (npages
&& !old
.rmap
) {
5402 unsigned long userspace_addr
;
5404 down_write(¤t
->mm
->mmap_sem
);
5405 userspace_addr
= do_mmap(NULL
, 0,
5407 PROT_READ
| PROT_WRITE
,
5408 MAP_PRIVATE
| MAP_ANONYMOUS
,
5410 up_write(¤t
->mm
->mmap_sem
);
5412 if (IS_ERR((void *)userspace_addr
))
5413 return PTR_ERR((void *)userspace_addr
);
5415 memslot
->userspace_addr
= userspace_addr
;
5423 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
5424 struct kvm_userspace_memory_region
*mem
,
5425 struct kvm_memory_slot old
,
5429 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
5431 if (!user_alloc
&& !old
.user_alloc
&& old
.rmap
&& !npages
) {
5434 down_write(¤t
->mm
->mmap_sem
);
5435 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
5436 old
.npages
* PAGE_SIZE
);
5437 up_write(¤t
->mm
->mmap_sem
);
5440 "kvm_vm_ioctl_set_memory_region: "
5441 "failed to munmap memory\n");
5444 spin_lock(&kvm
->mmu_lock
);
5445 if (!kvm
->arch
.n_requested_mmu_pages
) {
5446 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
5447 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
5450 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
5451 spin_unlock(&kvm
->mmu_lock
);
5454 void kvm_arch_flush_shadow(struct kvm
*kvm
)
5456 kvm_mmu_zap_all(kvm
);
5457 kvm_reload_remote_mmus(kvm
);
5460 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
5462 return vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
5463 || vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
5464 || vcpu
->arch
.nmi_pending
||
5465 (kvm_arch_interrupt_allowed(vcpu
) &&
5466 kvm_cpu_has_interrupt(vcpu
));
5469 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
5472 int cpu
= vcpu
->cpu
;
5474 if (waitqueue_active(&vcpu
->wq
)) {
5475 wake_up_interruptible(&vcpu
->wq
);
5476 ++vcpu
->stat
.halt_wakeup
;
5480 if (cpu
!= me
&& (unsigned)cpu
< nr_cpu_ids
&& cpu_online(cpu
))
5481 if (!test_and_set_bit(KVM_REQ_KICK
, &vcpu
->requests
))
5482 smp_send_reschedule(cpu
);
5486 int kvm_arch_interrupt_allowed(struct kvm_vcpu
*vcpu
)
5488 return kvm_x86_ops
->interrupt_allowed(vcpu
);
5491 bool kvm_is_linear_rip(struct kvm_vcpu
*vcpu
, unsigned long linear_rip
)
5493 unsigned long current_rip
= kvm_rip_read(vcpu
) +
5494 get_segment_base(vcpu
, VCPU_SREG_CS
);
5496 return current_rip
== linear_rip
;
5498 EXPORT_SYMBOL_GPL(kvm_is_linear_rip
);
5500 unsigned long kvm_get_rflags(struct kvm_vcpu
*vcpu
)
5502 unsigned long rflags
;
5504 rflags
= kvm_x86_ops
->get_rflags(vcpu
);
5505 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
5506 rflags
&= ~X86_EFLAGS_TF
;
5509 EXPORT_SYMBOL_GPL(kvm_get_rflags
);
5511 void kvm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
5513 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
&&
5514 kvm_is_linear_rip(vcpu
, vcpu
->arch
.singlestep_rip
))
5515 rflags
|= X86_EFLAGS_TF
;
5516 kvm_x86_ops
->set_rflags(vcpu
, rflags
);
5518 EXPORT_SYMBOL_GPL(kvm_set_rflags
);
5520 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit
);
5521 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq
);
5522 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault
);
5523 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr
);
5524 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr
);
5525 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun
);
5526 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit
);
5527 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject
);
5528 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit
);
5529 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga
);
5530 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit
);
5531 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts
);