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
9 * Copyright 2010 Red Hat, Inc. and/or its affilates.
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Amit Shah <amit.shah@qumranet.com>
15 * Ben-Ami Yassour <benami@il.ibm.com>
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
22 #include <linux/kvm_host.h>
27 #include "kvm_cache_regs.h"
30 #include <linux/clocksource.h>
31 #include <linux/interrupt.h>
32 #include <linux/kvm.h>
34 #include <linux/vmalloc.h>
35 #include <linux/module.h>
36 #include <linux/mman.h>
37 #include <linux/highmem.h>
38 #include <linux/iommu.h>
39 #include <linux/intel-iommu.h>
40 #include <linux/cpufreq.h>
41 #include <linux/user-return-notifier.h>
42 #include <linux/srcu.h>
43 #include <linux/slab.h>
44 #include <linux/perf_event.h>
45 #include <linux/uaccess.h>
46 #include <trace/events/kvm.h>
48 #define CREATE_TRACE_POINTS
51 #include <asm/debugreg.h>
59 #define MAX_IO_MSRS 256
60 #define CR0_RESERVED_BITS \
61 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
62 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
63 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
64 #define CR4_RESERVED_BITS \
65 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
66 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
67 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
69 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
71 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
73 #define KVM_MAX_MCE_BANKS 32
74 #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
77 * - enable syscall per default because its emulated by KVM
78 * - enable LME and LMA per default on 64 bit KVM
81 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffafeULL
;
83 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffffeULL
;
86 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
87 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
89 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
);
90 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
91 struct kvm_cpuid_entry2 __user
*entries
);
93 struct kvm_x86_ops
*kvm_x86_ops
;
94 EXPORT_SYMBOL_GPL(kvm_x86_ops
);
97 module_param_named(ignore_msrs
, ignore_msrs
, bool, S_IRUGO
| S_IWUSR
);
99 #define KVM_NR_SHARED_MSRS 16
101 struct kvm_shared_msrs_global
{
103 u32 msrs
[KVM_NR_SHARED_MSRS
];
106 struct kvm_shared_msrs
{
107 struct user_return_notifier urn
;
109 struct kvm_shared_msr_values
{
112 } values
[KVM_NR_SHARED_MSRS
];
115 static struct kvm_shared_msrs_global __read_mostly shared_msrs_global
;
116 static DEFINE_PER_CPU(struct kvm_shared_msrs
, shared_msrs
);
118 struct kvm_stats_debugfs_item debugfs_entries
[] = {
119 { "pf_fixed", VCPU_STAT(pf_fixed
) },
120 { "pf_guest", VCPU_STAT(pf_guest
) },
121 { "tlb_flush", VCPU_STAT(tlb_flush
) },
122 { "invlpg", VCPU_STAT(invlpg
) },
123 { "exits", VCPU_STAT(exits
) },
124 { "io_exits", VCPU_STAT(io_exits
) },
125 { "mmio_exits", VCPU_STAT(mmio_exits
) },
126 { "signal_exits", VCPU_STAT(signal_exits
) },
127 { "irq_window", VCPU_STAT(irq_window_exits
) },
128 { "nmi_window", VCPU_STAT(nmi_window_exits
) },
129 { "halt_exits", VCPU_STAT(halt_exits
) },
130 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
131 { "hypercalls", VCPU_STAT(hypercalls
) },
132 { "request_irq", VCPU_STAT(request_irq_exits
) },
133 { "irq_exits", VCPU_STAT(irq_exits
) },
134 { "host_state_reload", VCPU_STAT(host_state_reload
) },
135 { "efer_reload", VCPU_STAT(efer_reload
) },
136 { "fpu_reload", VCPU_STAT(fpu_reload
) },
137 { "insn_emulation", VCPU_STAT(insn_emulation
) },
138 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
139 { "irq_injections", VCPU_STAT(irq_injections
) },
140 { "nmi_injections", VCPU_STAT(nmi_injections
) },
141 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
142 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
143 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
144 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
145 { "mmu_flooded", VM_STAT(mmu_flooded
) },
146 { "mmu_recycled", VM_STAT(mmu_recycled
) },
147 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
148 { "mmu_unsync", VM_STAT(mmu_unsync
) },
149 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
150 { "largepages", VM_STAT(lpages
) },
154 u64 __read_mostly host_xcr0
;
156 static inline u32
bit(int bitno
)
158 return 1 << (bitno
& 31);
161 static void kvm_on_user_return(struct user_return_notifier
*urn
)
164 struct kvm_shared_msrs
*locals
165 = container_of(urn
, struct kvm_shared_msrs
, urn
);
166 struct kvm_shared_msr_values
*values
;
168 for (slot
= 0; slot
< shared_msrs_global
.nr
; ++slot
) {
169 values
= &locals
->values
[slot
];
170 if (values
->host
!= values
->curr
) {
171 wrmsrl(shared_msrs_global
.msrs
[slot
], values
->host
);
172 values
->curr
= values
->host
;
175 locals
->registered
= false;
176 user_return_notifier_unregister(urn
);
179 static void shared_msr_update(unsigned slot
, u32 msr
)
181 struct kvm_shared_msrs
*smsr
;
184 smsr
= &__get_cpu_var(shared_msrs
);
185 /* only read, and nobody should modify it at this time,
186 * so don't need lock */
187 if (slot
>= shared_msrs_global
.nr
) {
188 printk(KERN_ERR
"kvm: invalid MSR slot!");
191 rdmsrl_safe(msr
, &value
);
192 smsr
->values
[slot
].host
= value
;
193 smsr
->values
[slot
].curr
= value
;
196 void kvm_define_shared_msr(unsigned slot
, u32 msr
)
198 if (slot
>= shared_msrs_global
.nr
)
199 shared_msrs_global
.nr
= slot
+ 1;
200 shared_msrs_global
.msrs
[slot
] = msr
;
201 /* we need ensured the shared_msr_global have been updated */
204 EXPORT_SYMBOL_GPL(kvm_define_shared_msr
);
206 static void kvm_shared_msr_cpu_online(void)
210 for (i
= 0; i
< shared_msrs_global
.nr
; ++i
)
211 shared_msr_update(i
, shared_msrs_global
.msrs
[i
]);
214 void kvm_set_shared_msr(unsigned slot
, u64 value
, u64 mask
)
216 struct kvm_shared_msrs
*smsr
= &__get_cpu_var(shared_msrs
);
218 if (((value
^ smsr
->values
[slot
].curr
) & mask
) == 0)
220 smsr
->values
[slot
].curr
= value
;
221 wrmsrl(shared_msrs_global
.msrs
[slot
], value
);
222 if (!smsr
->registered
) {
223 smsr
->urn
.on_user_return
= kvm_on_user_return
;
224 user_return_notifier_register(&smsr
->urn
);
225 smsr
->registered
= true;
228 EXPORT_SYMBOL_GPL(kvm_set_shared_msr
);
230 static void drop_user_return_notifiers(void *ignore
)
232 struct kvm_shared_msrs
*smsr
= &__get_cpu_var(shared_msrs
);
234 if (smsr
->registered
)
235 kvm_on_user_return(&smsr
->urn
);
238 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
240 if (irqchip_in_kernel(vcpu
->kvm
))
241 return vcpu
->arch
.apic_base
;
243 return vcpu
->arch
.apic_base
;
245 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
247 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
249 /* TODO: reserve bits check */
250 if (irqchip_in_kernel(vcpu
->kvm
))
251 kvm_lapic_set_base(vcpu
, data
);
253 vcpu
->arch
.apic_base
= data
;
255 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
257 #define EXCPT_BENIGN 0
258 #define EXCPT_CONTRIBUTORY 1
261 static int exception_class(int vector
)
271 return EXCPT_CONTRIBUTORY
;
278 static void kvm_multiple_exception(struct kvm_vcpu
*vcpu
,
279 unsigned nr
, bool has_error
, u32 error_code
,
285 if (!vcpu
->arch
.exception
.pending
) {
287 vcpu
->arch
.exception
.pending
= true;
288 vcpu
->arch
.exception
.has_error_code
= has_error
;
289 vcpu
->arch
.exception
.nr
= nr
;
290 vcpu
->arch
.exception
.error_code
= error_code
;
291 vcpu
->arch
.exception
.reinject
= reinject
;
295 /* to check exception */
296 prev_nr
= vcpu
->arch
.exception
.nr
;
297 if (prev_nr
== DF_VECTOR
) {
298 /* triple fault -> shutdown */
299 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
302 class1
= exception_class(prev_nr
);
303 class2
= exception_class(nr
);
304 if ((class1
== EXCPT_CONTRIBUTORY
&& class2
== EXCPT_CONTRIBUTORY
)
305 || (class1
== EXCPT_PF
&& class2
!= EXCPT_BENIGN
)) {
306 /* generate double fault per SDM Table 5-5 */
307 vcpu
->arch
.exception
.pending
= true;
308 vcpu
->arch
.exception
.has_error_code
= true;
309 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
310 vcpu
->arch
.exception
.error_code
= 0;
312 /* replace previous exception with a new one in a hope
313 that instruction re-execution will regenerate lost
318 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
320 kvm_multiple_exception(vcpu
, nr
, false, 0, false);
322 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
324 void kvm_requeue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
326 kvm_multiple_exception(vcpu
, nr
, false, 0, true);
328 EXPORT_SYMBOL_GPL(kvm_requeue_exception
);
330 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
333 ++vcpu
->stat
.pf_guest
;
334 vcpu
->arch
.cr2
= addr
;
335 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
338 void kvm_inject_nmi(struct kvm_vcpu
*vcpu
)
340 vcpu
->arch
.nmi_pending
= 1;
342 EXPORT_SYMBOL_GPL(kvm_inject_nmi
);
344 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
346 kvm_multiple_exception(vcpu
, nr
, true, error_code
, false);
348 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
350 void kvm_requeue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
352 kvm_multiple_exception(vcpu
, nr
, true, error_code
, true);
354 EXPORT_SYMBOL_GPL(kvm_requeue_exception_e
);
357 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
358 * a #GP and return false.
360 bool kvm_require_cpl(struct kvm_vcpu
*vcpu
, int required_cpl
)
362 if (kvm_x86_ops
->get_cpl(vcpu
) <= required_cpl
)
364 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
367 EXPORT_SYMBOL_GPL(kvm_require_cpl
);
370 * Load the pae pdptrs. Return true is they are all valid.
372 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
374 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
375 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
378 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
380 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
381 offset
* sizeof(u64
), sizeof(pdpte
));
386 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
387 if (is_present_gpte(pdpte
[i
]) &&
388 (pdpte
[i
] & vcpu
->arch
.mmu
.rsvd_bits_mask
[0][2])) {
395 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
396 __set_bit(VCPU_EXREG_PDPTR
,
397 (unsigned long *)&vcpu
->arch
.regs_avail
);
398 __set_bit(VCPU_EXREG_PDPTR
,
399 (unsigned long *)&vcpu
->arch
.regs_dirty
);
404 EXPORT_SYMBOL_GPL(load_pdptrs
);
406 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
408 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
412 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
415 if (!test_bit(VCPU_EXREG_PDPTR
,
416 (unsigned long *)&vcpu
->arch
.regs_avail
))
419 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
422 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
428 static int __kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
430 unsigned long old_cr0
= kvm_read_cr0(vcpu
);
431 unsigned long update_bits
= X86_CR0_PG
| X86_CR0_WP
|
432 X86_CR0_CD
| X86_CR0_NW
;
437 if (cr0
& 0xffffffff00000000UL
)
441 cr0
&= ~CR0_RESERVED_BITS
;
443 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
))
446 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
))
449 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
451 if ((vcpu
->arch
.efer
& EFER_LME
)) {
456 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
461 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
))
465 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
467 if ((cr0
^ old_cr0
) & update_bits
)
468 kvm_mmu_reset_context(vcpu
);
472 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
474 if (__kvm_set_cr0(vcpu
, cr0
))
475 kvm_inject_gp(vcpu
, 0);
477 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
479 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
481 kvm_set_cr0(vcpu
, kvm_read_cr0_bits(vcpu
, ~0x0eul
) | (msw
& 0x0f));
483 EXPORT_SYMBOL_GPL(kvm_lmsw
);
485 int __kvm_set_xcr(struct kvm_vcpu
*vcpu
, u32 index
, u64 xcr
)
489 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
490 if (index
!= XCR_XFEATURE_ENABLED_MASK
)
493 if (kvm_x86_ops
->get_cpl(vcpu
) != 0)
495 if (!(xcr0
& XSTATE_FP
))
497 if ((xcr0
& XSTATE_YMM
) && !(xcr0
& XSTATE_SSE
))
499 if (xcr0
& ~host_xcr0
)
501 vcpu
->arch
.xcr0
= xcr0
;
502 vcpu
->guest_xcr0_loaded
= 0;
506 int kvm_set_xcr(struct kvm_vcpu
*vcpu
, u32 index
, u64 xcr
)
508 if (__kvm_set_xcr(vcpu
, index
, xcr
)) {
509 kvm_inject_gp(vcpu
, 0);
514 EXPORT_SYMBOL_GPL(kvm_set_xcr
);
516 static bool guest_cpuid_has_xsave(struct kvm_vcpu
*vcpu
)
518 struct kvm_cpuid_entry2
*best
;
520 best
= kvm_find_cpuid_entry(vcpu
, 1, 0);
521 return best
&& (best
->ecx
& bit(X86_FEATURE_XSAVE
));
524 static void update_cpuid(struct kvm_vcpu
*vcpu
)
526 struct kvm_cpuid_entry2
*best
;
528 best
= kvm_find_cpuid_entry(vcpu
, 1, 0);
532 /* Update OSXSAVE bit */
533 if (cpu_has_xsave
&& best
->function
== 0x1) {
534 best
->ecx
&= ~(bit(X86_FEATURE_OSXSAVE
));
535 if (kvm_read_cr4_bits(vcpu
, X86_CR4_OSXSAVE
))
536 best
->ecx
|= bit(X86_FEATURE_OSXSAVE
);
540 int __kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
542 unsigned long old_cr4
= kvm_read_cr4(vcpu
);
543 unsigned long pdptr_bits
= X86_CR4_PGE
| X86_CR4_PSE
| X86_CR4_PAE
;
545 if (cr4
& CR4_RESERVED_BITS
)
548 if (!guest_cpuid_has_xsave(vcpu
) && (cr4
& X86_CR4_OSXSAVE
))
551 if (is_long_mode(vcpu
)) {
552 if (!(cr4
& X86_CR4_PAE
))
554 } else if (is_paging(vcpu
) && (cr4
& X86_CR4_PAE
)
555 && ((cr4
^ old_cr4
) & pdptr_bits
)
556 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
))
559 if (cr4
& X86_CR4_VMXE
)
562 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
564 if ((cr4
^ old_cr4
) & pdptr_bits
)
565 kvm_mmu_reset_context(vcpu
);
567 if ((cr4
^ old_cr4
) & X86_CR4_OSXSAVE
)
573 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
575 if (__kvm_set_cr4(vcpu
, cr4
))
576 kvm_inject_gp(vcpu
, 0);
578 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
580 static int __kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
582 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
583 kvm_mmu_sync_roots(vcpu
);
584 kvm_mmu_flush_tlb(vcpu
);
588 if (is_long_mode(vcpu
)) {
589 if (cr3
& CR3_L_MODE_RESERVED_BITS
)
593 if (cr3
& CR3_PAE_RESERVED_BITS
)
595 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
))
599 * We don't check reserved bits in nonpae mode, because
600 * this isn't enforced, and VMware depends on this.
605 * Does the new cr3 value map to physical memory? (Note, we
606 * catch an invalid cr3 even in real-mode, because it would
607 * cause trouble later on when we turn on paging anyway.)
609 * A real CPU would silently accept an invalid cr3 and would
610 * attempt to use it - with largely undefined (and often hard
611 * to debug) behavior on the guest side.
613 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
615 vcpu
->arch
.cr3
= cr3
;
616 vcpu
->arch
.mmu
.new_cr3(vcpu
);
620 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
622 if (__kvm_set_cr3(vcpu
, cr3
))
623 kvm_inject_gp(vcpu
, 0);
625 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
627 int __kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
629 if (cr8
& CR8_RESERVED_BITS
)
631 if (irqchip_in_kernel(vcpu
->kvm
))
632 kvm_lapic_set_tpr(vcpu
, cr8
);
634 vcpu
->arch
.cr8
= cr8
;
638 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
640 if (__kvm_set_cr8(vcpu
, cr8
))
641 kvm_inject_gp(vcpu
, 0);
643 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
645 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
647 if (irqchip_in_kernel(vcpu
->kvm
))
648 return kvm_lapic_get_cr8(vcpu
);
650 return vcpu
->arch
.cr8
;
652 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
654 static int __kvm_set_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long val
)
658 vcpu
->arch
.db
[dr
] = val
;
659 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
660 vcpu
->arch
.eff_db
[dr
] = val
;
663 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
667 if (val
& 0xffffffff00000000ULL
)
669 vcpu
->arch
.dr6
= (val
& DR6_VOLATILE
) | DR6_FIXED_1
;
672 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
676 if (val
& 0xffffffff00000000ULL
)
678 vcpu
->arch
.dr7
= (val
& DR7_VOLATILE
) | DR7_FIXED_1
;
679 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
680 kvm_x86_ops
->set_dr7(vcpu
, vcpu
->arch
.dr7
);
681 vcpu
->arch
.switch_db_regs
= (val
& DR7_BP_EN_MASK
);
689 int kvm_set_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long val
)
693 res
= __kvm_set_dr(vcpu
, dr
, val
);
695 kvm_queue_exception(vcpu
, UD_VECTOR
);
697 kvm_inject_gp(vcpu
, 0);
701 EXPORT_SYMBOL_GPL(kvm_set_dr
);
703 static int _kvm_get_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long *val
)
707 *val
= vcpu
->arch
.db
[dr
];
710 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
714 *val
= vcpu
->arch
.dr6
;
717 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
721 *val
= vcpu
->arch
.dr7
;
728 int kvm_get_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long *val
)
730 if (_kvm_get_dr(vcpu
, dr
, val
)) {
731 kvm_queue_exception(vcpu
, UD_VECTOR
);
736 EXPORT_SYMBOL_GPL(kvm_get_dr
);
739 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
740 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
742 * This list is modified at module load time to reflect the
743 * capabilities of the host cpu. This capabilities test skips MSRs that are
744 * kvm-specific. Those are put in the beginning of the list.
747 #define KVM_SAVE_MSRS_BEGIN 7
748 static u32 msrs_to_save
[] = {
749 MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
750 MSR_KVM_SYSTEM_TIME_NEW
, MSR_KVM_WALL_CLOCK_NEW
,
751 HV_X64_MSR_GUEST_OS_ID
, HV_X64_MSR_HYPERCALL
,
752 HV_X64_MSR_APIC_ASSIST_PAGE
,
753 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
756 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
758 MSR_IA32_TSC
, MSR_IA32_PERF_STATUS
, MSR_IA32_CR_PAT
, MSR_VM_HSAVE_PA
761 static unsigned num_msrs_to_save
;
763 static u32 emulated_msrs
[] = {
764 MSR_IA32_MISC_ENABLE
,
767 static int set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
769 u64 old_efer
= vcpu
->arch
.efer
;
771 if (efer
& efer_reserved_bits
)
775 && (vcpu
->arch
.efer
& EFER_LME
) != (efer
& EFER_LME
))
778 if (efer
& EFER_FFXSR
) {
779 struct kvm_cpuid_entry2
*feat
;
781 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
782 if (!feat
|| !(feat
->edx
& bit(X86_FEATURE_FXSR_OPT
)))
786 if (efer
& EFER_SVME
) {
787 struct kvm_cpuid_entry2
*feat
;
789 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
790 if (!feat
|| !(feat
->ecx
& bit(X86_FEATURE_SVM
)))
795 efer
|= vcpu
->arch
.efer
& EFER_LMA
;
797 kvm_x86_ops
->set_efer(vcpu
, efer
);
799 vcpu
->arch
.mmu
.base_role
.nxe
= (efer
& EFER_NX
) && !tdp_enabled
;
800 kvm_mmu_reset_context(vcpu
);
802 /* Update reserved bits */
803 if ((efer
^ old_efer
) & EFER_NX
)
804 kvm_mmu_reset_context(vcpu
);
809 void kvm_enable_efer_bits(u64 mask
)
811 efer_reserved_bits
&= ~mask
;
813 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
817 * Writes msr value into into the appropriate "register".
818 * Returns 0 on success, non-0 otherwise.
819 * Assumes vcpu_load() was already called.
821 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
823 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
827 * Adapt set_msr() to msr_io()'s calling convention
829 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
831 return kvm_set_msr(vcpu
, index
, *data
);
834 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
838 struct pvclock_wall_clock wc
;
839 struct timespec boot
;
844 r
= kvm_read_guest(kvm
, wall_clock
, &version
, sizeof(version
));
849 ++version
; /* first time write, random junk */
853 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
856 * The guest calculates current wall clock time by adding
857 * system time (updated by kvm_write_guest_time below) to the
858 * wall clock specified here. guest system time equals host
859 * system time for us, thus we must fill in host boot time here.
863 wc
.sec
= boot
.tv_sec
;
864 wc
.nsec
= boot
.tv_nsec
;
865 wc
.version
= version
;
867 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
870 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
873 static uint32_t div_frac(uint32_t dividend
, uint32_t divisor
)
875 uint32_t quotient
, remainder
;
877 /* Don't try to replace with do_div(), this one calculates
878 * "(dividend << 32) / divisor" */
880 : "=a" (quotient
), "=d" (remainder
)
881 : "0" (0), "1" (dividend
), "r" (divisor
) );
885 static void kvm_set_time_scale(uint32_t tsc_khz
, struct pvclock_vcpu_time_info
*hv_clock
)
887 uint64_t nsecs
= 1000000000LL;
892 tps64
= tsc_khz
* 1000LL;
893 while (tps64
> nsecs
*2) {
898 tps32
= (uint32_t)tps64
;
899 while (tps32
<= (uint32_t)nsecs
) {
904 hv_clock
->tsc_shift
= shift
;
905 hv_clock
->tsc_to_system_mul
= div_frac(nsecs
, tps32
);
907 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
908 __func__
, tsc_khz
, hv_clock
->tsc_shift
,
909 hv_clock
->tsc_to_system_mul
);
912 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz
);
914 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
918 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
920 unsigned long this_tsc_khz
;
922 if ((!vcpu
->time_page
))
925 this_tsc_khz
= get_cpu_var(cpu_tsc_khz
);
926 if (unlikely(vcpu
->hv_clock_tsc_khz
!= this_tsc_khz
)) {
927 kvm_set_time_scale(this_tsc_khz
, &vcpu
->hv_clock
);
928 vcpu
->hv_clock_tsc_khz
= this_tsc_khz
;
930 put_cpu_var(cpu_tsc_khz
);
932 /* Keep irq disabled to prevent changes to the clock */
933 local_irq_save(flags
);
934 kvm_get_msr(v
, MSR_IA32_TSC
, &vcpu
->hv_clock
.tsc_timestamp
);
936 monotonic_to_bootbased(&ts
);
937 local_irq_restore(flags
);
939 /* With all the info we got, fill in the values */
941 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
942 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
) + v
->kvm
->arch
.kvmclock_offset
;
944 vcpu
->hv_clock
.flags
= 0;
947 * The interface expects us to write an even number signaling that the
948 * update is finished. Since the guest won't see the intermediate
949 * state, we just increase by 2 at the end.
951 vcpu
->hv_clock
.version
+= 2;
953 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
955 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
956 sizeof(vcpu
->hv_clock
));
958 kunmap_atomic(shared_kaddr
, KM_USER0
);
960 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
963 static int kvm_request_guest_time_update(struct kvm_vcpu
*v
)
965 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
967 if (!vcpu
->time_page
)
969 set_bit(KVM_REQ_KVMCLOCK_UPDATE
, &v
->requests
);
973 static bool msr_mtrr_valid(unsigned msr
)
976 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR
- 1:
977 case MSR_MTRRfix64K_00000
:
978 case MSR_MTRRfix16K_80000
:
979 case MSR_MTRRfix16K_A0000
:
980 case MSR_MTRRfix4K_C0000
:
981 case MSR_MTRRfix4K_C8000
:
982 case MSR_MTRRfix4K_D0000
:
983 case MSR_MTRRfix4K_D8000
:
984 case MSR_MTRRfix4K_E0000
:
985 case MSR_MTRRfix4K_E8000
:
986 case MSR_MTRRfix4K_F0000
:
987 case MSR_MTRRfix4K_F8000
:
988 case MSR_MTRRdefType
:
989 case MSR_IA32_CR_PAT
:
997 static bool valid_pat_type(unsigned t
)
999 return t
< 8 && (1 << t
) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
1002 static bool valid_mtrr_type(unsigned t
)
1004 return t
< 8 && (1 << t
) & 0x73; /* 0, 1, 4, 5, 6 */
1007 static bool mtrr_valid(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1011 if (!msr_mtrr_valid(msr
))
1014 if (msr
== MSR_IA32_CR_PAT
) {
1015 for (i
= 0; i
< 8; i
++)
1016 if (!valid_pat_type((data
>> (i
* 8)) & 0xff))
1019 } else if (msr
== MSR_MTRRdefType
) {
1022 return valid_mtrr_type(data
& 0xff);
1023 } else if (msr
>= MSR_MTRRfix64K_00000
&& msr
<= MSR_MTRRfix4K_F8000
) {
1024 for (i
= 0; i
< 8 ; i
++)
1025 if (!valid_mtrr_type((data
>> (i
* 8)) & 0xff))
1030 /* variable MTRRs */
1031 return valid_mtrr_type(data
& 0xff);
1034 static int set_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1036 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
1038 if (!mtrr_valid(vcpu
, msr
, data
))
1041 if (msr
== MSR_MTRRdefType
) {
1042 vcpu
->arch
.mtrr_state
.def_type
= data
;
1043 vcpu
->arch
.mtrr_state
.enabled
= (data
& 0xc00) >> 10;
1044 } else if (msr
== MSR_MTRRfix64K_00000
)
1046 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
1047 p
[1 + msr
- MSR_MTRRfix16K_80000
] = data
;
1048 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
1049 p
[3 + msr
- MSR_MTRRfix4K_C0000
] = data
;
1050 else if (msr
== MSR_IA32_CR_PAT
)
1051 vcpu
->arch
.pat
= data
;
1052 else { /* Variable MTRRs */
1053 int idx
, is_mtrr_mask
;
1056 idx
= (msr
- 0x200) / 2;
1057 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
1060 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
1063 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
1067 kvm_mmu_reset_context(vcpu
);
1071 static int set_msr_mce(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1073 u64 mcg_cap
= vcpu
->arch
.mcg_cap
;
1074 unsigned bank_num
= mcg_cap
& 0xff;
1077 case MSR_IA32_MCG_STATUS
:
1078 vcpu
->arch
.mcg_status
= data
;
1080 case MSR_IA32_MCG_CTL
:
1081 if (!(mcg_cap
& MCG_CTL_P
))
1083 if (data
!= 0 && data
!= ~(u64
)0)
1085 vcpu
->arch
.mcg_ctl
= data
;
1088 if (msr
>= MSR_IA32_MC0_CTL
&&
1089 msr
< MSR_IA32_MC0_CTL
+ 4 * bank_num
) {
1090 u32 offset
= msr
- MSR_IA32_MC0_CTL
;
1091 /* only 0 or all 1s can be written to IA32_MCi_CTL
1092 * some Linux kernels though clear bit 10 in bank 4 to
1093 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1094 * this to avoid an uncatched #GP in the guest
1096 if ((offset
& 0x3) == 0 &&
1097 data
!= 0 && (data
| (1 << 10)) != ~(u64
)0)
1099 vcpu
->arch
.mce_banks
[offset
] = data
;
1107 static int xen_hvm_config(struct kvm_vcpu
*vcpu
, u64 data
)
1109 struct kvm
*kvm
= vcpu
->kvm
;
1110 int lm
= is_long_mode(vcpu
);
1111 u8
*blob_addr
= lm
? (u8
*)(long)kvm
->arch
.xen_hvm_config
.blob_addr_64
1112 : (u8
*)(long)kvm
->arch
.xen_hvm_config
.blob_addr_32
;
1113 u8 blob_size
= lm
? kvm
->arch
.xen_hvm_config
.blob_size_64
1114 : kvm
->arch
.xen_hvm_config
.blob_size_32
;
1115 u32 page_num
= data
& ~PAGE_MASK
;
1116 u64 page_addr
= data
& PAGE_MASK
;
1121 if (page_num
>= blob_size
)
1124 page
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1128 if (copy_from_user(page
, blob_addr
+ (page_num
* PAGE_SIZE
), PAGE_SIZE
))
1130 if (kvm_write_guest(kvm
, page_addr
, page
, PAGE_SIZE
))
1139 static bool kvm_hv_hypercall_enabled(struct kvm
*kvm
)
1141 return kvm
->arch
.hv_hypercall
& HV_X64_MSR_HYPERCALL_ENABLE
;
1144 static bool kvm_hv_msr_partition_wide(u32 msr
)
1148 case HV_X64_MSR_GUEST_OS_ID
:
1149 case HV_X64_MSR_HYPERCALL
:
1157 static int set_msr_hyperv_pw(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1159 struct kvm
*kvm
= vcpu
->kvm
;
1162 case HV_X64_MSR_GUEST_OS_ID
:
1163 kvm
->arch
.hv_guest_os_id
= data
;
1164 /* setting guest os id to zero disables hypercall page */
1165 if (!kvm
->arch
.hv_guest_os_id
)
1166 kvm
->arch
.hv_hypercall
&= ~HV_X64_MSR_HYPERCALL_ENABLE
;
1168 case HV_X64_MSR_HYPERCALL
: {
1173 /* if guest os id is not set hypercall should remain disabled */
1174 if (!kvm
->arch
.hv_guest_os_id
)
1176 if (!(data
& HV_X64_MSR_HYPERCALL_ENABLE
)) {
1177 kvm
->arch
.hv_hypercall
= data
;
1180 gfn
= data
>> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT
;
1181 addr
= gfn_to_hva(kvm
, gfn
);
1182 if (kvm_is_error_hva(addr
))
1184 kvm_x86_ops
->patch_hypercall(vcpu
, instructions
);
1185 ((unsigned char *)instructions
)[3] = 0xc3; /* ret */
1186 if (copy_to_user((void __user
*)addr
, instructions
, 4))
1188 kvm
->arch
.hv_hypercall
= data
;
1192 pr_unimpl(vcpu
, "HYPER-V unimplemented wrmsr: 0x%x "
1193 "data 0x%llx\n", msr
, data
);
1199 static int set_msr_hyperv(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1202 case HV_X64_MSR_APIC_ASSIST_PAGE
: {
1205 if (!(data
& HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE
)) {
1206 vcpu
->arch
.hv_vapic
= data
;
1209 addr
= gfn_to_hva(vcpu
->kvm
, data
>>
1210 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT
);
1211 if (kvm_is_error_hva(addr
))
1213 if (clear_user((void __user
*)addr
, PAGE_SIZE
))
1215 vcpu
->arch
.hv_vapic
= data
;
1218 case HV_X64_MSR_EOI
:
1219 return kvm_hv_vapic_msr_write(vcpu
, APIC_EOI
, data
);
1220 case HV_X64_MSR_ICR
:
1221 return kvm_hv_vapic_msr_write(vcpu
, APIC_ICR
, data
);
1222 case HV_X64_MSR_TPR
:
1223 return kvm_hv_vapic_msr_write(vcpu
, APIC_TASKPRI
, data
);
1225 pr_unimpl(vcpu
, "HYPER-V unimplemented wrmsr: 0x%x "
1226 "data 0x%llx\n", msr
, data
);
1233 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
1237 return set_efer(vcpu
, data
);
1239 data
&= ~(u64
)0x40; /* ignore flush filter disable */
1240 data
&= ~(u64
)0x100; /* ignore ignne emulation enable */
1242 pr_unimpl(vcpu
, "unimplemented HWCR wrmsr: 0x%llx\n",
1247 case MSR_FAM10H_MMIO_CONF_BASE
:
1249 pr_unimpl(vcpu
, "unimplemented MMIO_CONF_BASE wrmsr: "
1254 case MSR_AMD64_NB_CFG
:
1256 case MSR_IA32_DEBUGCTLMSR
:
1258 /* We support the non-activated case already */
1260 } else if (data
& ~(DEBUGCTLMSR_LBR
| DEBUGCTLMSR_BTF
)) {
1261 /* Values other than LBR and BTF are vendor-specific,
1262 thus reserved and should throw a #GP */
1265 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1268 case MSR_IA32_UCODE_REV
:
1269 case MSR_IA32_UCODE_WRITE
:
1270 case MSR_VM_HSAVE_PA
:
1271 case MSR_AMD64_PATCH_LOADER
:
1273 case 0x200 ... 0x2ff:
1274 return set_msr_mtrr(vcpu
, msr
, data
);
1275 case MSR_IA32_APICBASE
:
1276 kvm_set_apic_base(vcpu
, data
);
1278 case APIC_BASE_MSR
... APIC_BASE_MSR
+ 0x3ff:
1279 return kvm_x2apic_msr_write(vcpu
, msr
, data
);
1280 case MSR_IA32_MISC_ENABLE
:
1281 vcpu
->arch
.ia32_misc_enable_msr
= data
;
1283 case MSR_KVM_WALL_CLOCK_NEW
:
1284 case MSR_KVM_WALL_CLOCK
:
1285 vcpu
->kvm
->arch
.wall_clock
= data
;
1286 kvm_write_wall_clock(vcpu
->kvm
, data
);
1288 case MSR_KVM_SYSTEM_TIME_NEW
:
1289 case MSR_KVM_SYSTEM_TIME
: {
1290 if (vcpu
->arch
.time_page
) {
1291 kvm_release_page_dirty(vcpu
->arch
.time_page
);
1292 vcpu
->arch
.time_page
= NULL
;
1295 vcpu
->arch
.time
= data
;
1297 /* we verify if the enable bit is set... */
1301 /* ...but clean it before doing the actual write */
1302 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
1304 vcpu
->arch
.time_page
=
1305 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
1307 if (is_error_page(vcpu
->arch
.time_page
)) {
1308 kvm_release_page_clean(vcpu
->arch
.time_page
);
1309 vcpu
->arch
.time_page
= NULL
;
1312 kvm_request_guest_time_update(vcpu
);
1315 case MSR_IA32_MCG_CTL
:
1316 case MSR_IA32_MCG_STATUS
:
1317 case MSR_IA32_MC0_CTL
... MSR_IA32_MC0_CTL
+ 4 * KVM_MAX_MCE_BANKS
- 1:
1318 return set_msr_mce(vcpu
, msr
, data
);
1320 /* Performance counters are not protected by a CPUID bit,
1321 * so we should check all of them in the generic path for the sake of
1322 * cross vendor migration.
1323 * Writing a zero into the event select MSRs disables them,
1324 * which we perfectly emulate ;-). Any other value should be at least
1325 * reported, some guests depend on them.
1327 case MSR_P6_EVNTSEL0
:
1328 case MSR_P6_EVNTSEL1
:
1329 case MSR_K7_EVNTSEL0
:
1330 case MSR_K7_EVNTSEL1
:
1331 case MSR_K7_EVNTSEL2
:
1332 case MSR_K7_EVNTSEL3
:
1334 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: "
1335 "0x%x data 0x%llx\n", msr
, data
);
1337 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1338 * so we ignore writes to make it happy.
1340 case MSR_P6_PERFCTR0
:
1341 case MSR_P6_PERFCTR1
:
1342 case MSR_K7_PERFCTR0
:
1343 case MSR_K7_PERFCTR1
:
1344 case MSR_K7_PERFCTR2
:
1345 case MSR_K7_PERFCTR3
:
1346 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: "
1347 "0x%x data 0x%llx\n", msr
, data
);
1349 case HV_X64_MSR_GUEST_OS_ID
... HV_X64_MSR_SINT15
:
1350 if (kvm_hv_msr_partition_wide(msr
)) {
1352 mutex_lock(&vcpu
->kvm
->lock
);
1353 r
= set_msr_hyperv_pw(vcpu
, msr
, data
);
1354 mutex_unlock(&vcpu
->kvm
->lock
);
1357 return set_msr_hyperv(vcpu
, msr
, data
);
1360 if (msr
&& (msr
== vcpu
->kvm
->arch
.xen_hvm_config
.msr
))
1361 return xen_hvm_config(vcpu
, data
);
1363 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n",
1367 pr_unimpl(vcpu
, "ignored wrmsr: 0x%x data %llx\n",
1374 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
1378 * Reads an msr value (of 'msr_index') into 'pdata'.
1379 * Returns 0 on success, non-0 otherwise.
1380 * Assumes vcpu_load() was already called.
1382 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
1384 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
1387 static int get_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1389 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
1391 if (!msr_mtrr_valid(msr
))
1394 if (msr
== MSR_MTRRdefType
)
1395 *pdata
= vcpu
->arch
.mtrr_state
.def_type
+
1396 (vcpu
->arch
.mtrr_state
.enabled
<< 10);
1397 else if (msr
== MSR_MTRRfix64K_00000
)
1399 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
1400 *pdata
= p
[1 + msr
- MSR_MTRRfix16K_80000
];
1401 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
1402 *pdata
= p
[3 + msr
- MSR_MTRRfix4K_C0000
];
1403 else if (msr
== MSR_IA32_CR_PAT
)
1404 *pdata
= vcpu
->arch
.pat
;
1405 else { /* Variable MTRRs */
1406 int idx
, is_mtrr_mask
;
1409 idx
= (msr
- 0x200) / 2;
1410 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
1413 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
1416 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
1423 static int get_msr_mce(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1426 u64 mcg_cap
= vcpu
->arch
.mcg_cap
;
1427 unsigned bank_num
= mcg_cap
& 0xff;
1430 case MSR_IA32_P5_MC_ADDR
:
1431 case MSR_IA32_P5_MC_TYPE
:
1434 case MSR_IA32_MCG_CAP
:
1435 data
= vcpu
->arch
.mcg_cap
;
1437 case MSR_IA32_MCG_CTL
:
1438 if (!(mcg_cap
& MCG_CTL_P
))
1440 data
= vcpu
->arch
.mcg_ctl
;
1442 case MSR_IA32_MCG_STATUS
:
1443 data
= vcpu
->arch
.mcg_status
;
1446 if (msr
>= MSR_IA32_MC0_CTL
&&
1447 msr
< MSR_IA32_MC0_CTL
+ 4 * bank_num
) {
1448 u32 offset
= msr
- MSR_IA32_MC0_CTL
;
1449 data
= vcpu
->arch
.mce_banks
[offset
];
1458 static int get_msr_hyperv_pw(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1461 struct kvm
*kvm
= vcpu
->kvm
;
1464 case HV_X64_MSR_GUEST_OS_ID
:
1465 data
= kvm
->arch
.hv_guest_os_id
;
1467 case HV_X64_MSR_HYPERCALL
:
1468 data
= kvm
->arch
.hv_hypercall
;
1471 pr_unimpl(vcpu
, "Hyper-V unhandled rdmsr: 0x%x\n", msr
);
1479 static int get_msr_hyperv(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1484 case HV_X64_MSR_VP_INDEX
: {
1487 kvm_for_each_vcpu(r
, v
, vcpu
->kvm
)
1492 case HV_X64_MSR_EOI
:
1493 return kvm_hv_vapic_msr_read(vcpu
, APIC_EOI
, pdata
);
1494 case HV_X64_MSR_ICR
:
1495 return kvm_hv_vapic_msr_read(vcpu
, APIC_ICR
, pdata
);
1496 case HV_X64_MSR_TPR
:
1497 return kvm_hv_vapic_msr_read(vcpu
, APIC_TASKPRI
, pdata
);
1499 pr_unimpl(vcpu
, "Hyper-V unhandled rdmsr: 0x%x\n", msr
);
1506 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1511 case MSR_IA32_PLATFORM_ID
:
1512 case MSR_IA32_UCODE_REV
:
1513 case MSR_IA32_EBL_CR_POWERON
:
1514 case MSR_IA32_DEBUGCTLMSR
:
1515 case MSR_IA32_LASTBRANCHFROMIP
:
1516 case MSR_IA32_LASTBRANCHTOIP
:
1517 case MSR_IA32_LASTINTFROMIP
:
1518 case MSR_IA32_LASTINTTOIP
:
1521 case MSR_VM_HSAVE_PA
:
1522 case MSR_P6_PERFCTR0
:
1523 case MSR_P6_PERFCTR1
:
1524 case MSR_P6_EVNTSEL0
:
1525 case MSR_P6_EVNTSEL1
:
1526 case MSR_K7_EVNTSEL0
:
1527 case MSR_K7_PERFCTR0
:
1528 case MSR_K8_INT_PENDING_MSG
:
1529 case MSR_AMD64_NB_CFG
:
1530 case MSR_FAM10H_MMIO_CONF_BASE
:
1534 data
= 0x500 | KVM_NR_VAR_MTRR
;
1536 case 0x200 ... 0x2ff:
1537 return get_msr_mtrr(vcpu
, msr
, pdata
);
1538 case 0xcd: /* fsb frequency */
1541 case MSR_IA32_APICBASE
:
1542 data
= kvm_get_apic_base(vcpu
);
1544 case APIC_BASE_MSR
... APIC_BASE_MSR
+ 0x3ff:
1545 return kvm_x2apic_msr_read(vcpu
, msr
, pdata
);
1547 case MSR_IA32_MISC_ENABLE
:
1548 data
= vcpu
->arch
.ia32_misc_enable_msr
;
1550 case MSR_IA32_PERF_STATUS
:
1551 /* TSC increment by tick */
1553 /* CPU multiplier */
1554 data
|= (((uint64_t)4ULL) << 40);
1557 data
= vcpu
->arch
.efer
;
1559 case MSR_KVM_WALL_CLOCK
:
1560 case MSR_KVM_WALL_CLOCK_NEW
:
1561 data
= vcpu
->kvm
->arch
.wall_clock
;
1563 case MSR_KVM_SYSTEM_TIME
:
1564 case MSR_KVM_SYSTEM_TIME_NEW
:
1565 data
= vcpu
->arch
.time
;
1567 case MSR_IA32_P5_MC_ADDR
:
1568 case MSR_IA32_P5_MC_TYPE
:
1569 case MSR_IA32_MCG_CAP
:
1570 case MSR_IA32_MCG_CTL
:
1571 case MSR_IA32_MCG_STATUS
:
1572 case MSR_IA32_MC0_CTL
... MSR_IA32_MC0_CTL
+ 4 * KVM_MAX_MCE_BANKS
- 1:
1573 return get_msr_mce(vcpu
, msr
, pdata
);
1574 case HV_X64_MSR_GUEST_OS_ID
... HV_X64_MSR_SINT15
:
1575 if (kvm_hv_msr_partition_wide(msr
)) {
1577 mutex_lock(&vcpu
->kvm
->lock
);
1578 r
= get_msr_hyperv_pw(vcpu
, msr
, pdata
);
1579 mutex_unlock(&vcpu
->kvm
->lock
);
1582 return get_msr_hyperv(vcpu
, msr
, pdata
);
1586 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
1589 pr_unimpl(vcpu
, "ignored rdmsr: 0x%x\n", msr
);
1597 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
1600 * Read or write a bunch of msrs. All parameters are kernel addresses.
1602 * @return number of msrs set successfully.
1604 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
1605 struct kvm_msr_entry
*entries
,
1606 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1607 unsigned index
, u64
*data
))
1611 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1612 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
1613 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
1615 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1621 * Read or write a bunch of msrs. Parameters are user addresses.
1623 * @return number of msrs set successfully.
1625 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
1626 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1627 unsigned index
, u64
*data
),
1630 struct kvm_msrs msrs
;
1631 struct kvm_msr_entry
*entries
;
1636 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
1640 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
1644 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
1645 entries
= kmalloc(size
, GFP_KERNEL
);
1650 if (copy_from_user(entries
, user_msrs
->entries
, size
))
1653 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
1658 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
1669 int kvm_dev_ioctl_check_extension(long ext
)
1674 case KVM_CAP_IRQCHIP
:
1676 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
1677 case KVM_CAP_SET_TSS_ADDR
:
1678 case KVM_CAP_EXT_CPUID
:
1679 case KVM_CAP_CLOCKSOURCE
:
1681 case KVM_CAP_NOP_IO_DELAY
:
1682 case KVM_CAP_MP_STATE
:
1683 case KVM_CAP_SYNC_MMU
:
1684 case KVM_CAP_REINJECT_CONTROL
:
1685 case KVM_CAP_IRQ_INJECT_STATUS
:
1686 case KVM_CAP_ASSIGN_DEV_IRQ
:
1688 case KVM_CAP_IOEVENTFD
:
1690 case KVM_CAP_PIT_STATE2
:
1691 case KVM_CAP_SET_IDENTITY_MAP_ADDR
:
1692 case KVM_CAP_XEN_HVM
:
1693 case KVM_CAP_ADJUST_CLOCK
:
1694 case KVM_CAP_VCPU_EVENTS
:
1695 case KVM_CAP_HYPERV
:
1696 case KVM_CAP_HYPERV_VAPIC
:
1697 case KVM_CAP_HYPERV_SPIN
:
1698 case KVM_CAP_PCI_SEGMENT
:
1699 case KVM_CAP_DEBUGREGS
:
1700 case KVM_CAP_X86_ROBUST_SINGLESTEP
:
1703 case KVM_CAP_COALESCED_MMIO
:
1704 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
1707 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
1709 case KVM_CAP_NR_VCPUS
:
1712 case KVM_CAP_NR_MEMSLOTS
:
1713 r
= KVM_MEMORY_SLOTS
;
1715 case KVM_CAP_PV_MMU
: /* obsolete */
1722 r
= KVM_MAX_MCE_BANKS
;
1732 long kvm_arch_dev_ioctl(struct file
*filp
,
1733 unsigned int ioctl
, unsigned long arg
)
1735 void __user
*argp
= (void __user
*)arg
;
1739 case KVM_GET_MSR_INDEX_LIST
: {
1740 struct kvm_msr_list __user
*user_msr_list
= argp
;
1741 struct kvm_msr_list msr_list
;
1745 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
1748 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
1749 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
1752 if (n
< msr_list
.nmsrs
)
1755 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
1756 num_msrs_to_save
* sizeof(u32
)))
1758 if (copy_to_user(user_msr_list
->indices
+ num_msrs_to_save
,
1760 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
1765 case KVM_GET_SUPPORTED_CPUID
: {
1766 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1767 struct kvm_cpuid2 cpuid
;
1770 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1772 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
1773 cpuid_arg
->entries
);
1778 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1783 case KVM_X86_GET_MCE_CAP_SUPPORTED
: {
1786 mce_cap
= KVM_MCE_CAP_SUPPORTED
;
1788 if (copy_to_user(argp
, &mce_cap
, sizeof mce_cap
))
1800 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1802 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
1803 if (unlikely(per_cpu(cpu_tsc_khz
, cpu
) == 0)) {
1804 unsigned long khz
= cpufreq_quick_get(cpu
);
1807 per_cpu(cpu_tsc_khz
, cpu
) = khz
;
1809 kvm_request_guest_time_update(vcpu
);
1812 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
1814 kvm_x86_ops
->vcpu_put(vcpu
);
1815 kvm_put_guest_fpu(vcpu
);
1818 static int is_efer_nx(void)
1820 unsigned long long efer
= 0;
1822 rdmsrl_safe(MSR_EFER
, &efer
);
1823 return efer
& EFER_NX
;
1826 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
1829 struct kvm_cpuid_entry2
*e
, *entry
;
1832 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
1833 e
= &vcpu
->arch
.cpuid_entries
[i
];
1834 if (e
->function
== 0x80000001) {
1839 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
1840 entry
->edx
&= ~(1 << 20);
1841 printk(KERN_INFO
"kvm: guest NX capability removed\n");
1845 /* when an old userspace process fills a new kernel module */
1846 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
1847 struct kvm_cpuid
*cpuid
,
1848 struct kvm_cpuid_entry __user
*entries
)
1851 struct kvm_cpuid_entry
*cpuid_entries
;
1854 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1857 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
1861 if (copy_from_user(cpuid_entries
, entries
,
1862 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
1864 for (i
= 0; i
< cpuid
->nent
; i
++) {
1865 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
1866 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
1867 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
1868 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
1869 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
1870 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
1871 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
1872 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
1873 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
1874 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
1876 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1877 cpuid_fix_nx_cap(vcpu
);
1879 kvm_apic_set_version(vcpu
);
1880 kvm_x86_ops
->cpuid_update(vcpu
);
1884 vfree(cpuid_entries
);
1889 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
1890 struct kvm_cpuid2
*cpuid
,
1891 struct kvm_cpuid_entry2 __user
*entries
)
1896 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1899 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
1900 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
1902 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1903 kvm_apic_set_version(vcpu
);
1904 kvm_x86_ops
->cpuid_update(vcpu
);
1912 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
1913 struct kvm_cpuid2
*cpuid
,
1914 struct kvm_cpuid_entry2 __user
*entries
)
1919 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1922 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1923 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1928 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1932 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1935 entry
->function
= function
;
1936 entry
->index
= index
;
1937 cpuid_count(entry
->function
, entry
->index
,
1938 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1942 #define F(x) bit(X86_FEATURE_##x)
1944 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1945 u32 index
, int *nent
, int maxnent
)
1947 unsigned f_nx
= is_efer_nx() ? F(NX
) : 0;
1948 #ifdef CONFIG_X86_64
1949 unsigned f_gbpages
= (kvm_x86_ops
->get_lpage_level() == PT_PDPE_LEVEL
)
1951 unsigned f_lm
= F(LM
);
1953 unsigned f_gbpages
= 0;
1956 unsigned f_rdtscp
= kvm_x86_ops
->rdtscp_supported() ? F(RDTSCP
) : 0;
1959 const u32 kvm_supported_word0_x86_features
=
1960 F(FPU
) | F(VME
) | F(DE
) | F(PSE
) |
1961 F(TSC
) | F(MSR
) | F(PAE
) | F(MCE
) |
1962 F(CX8
) | F(APIC
) | 0 /* Reserved */ | F(SEP
) |
1963 F(MTRR
) | F(PGE
) | F(MCA
) | F(CMOV
) |
1964 F(PAT
) | F(PSE36
) | 0 /* PSN */ | F(CLFLSH
) |
1965 0 /* Reserved, DS, ACPI */ | F(MMX
) |
1966 F(FXSR
) | F(XMM
) | F(XMM2
) | F(SELFSNOOP
) |
1967 0 /* HTT, TM, Reserved, PBE */;
1968 /* cpuid 0x80000001.edx */
1969 const u32 kvm_supported_word1_x86_features
=
1970 F(FPU
) | F(VME
) | F(DE
) | F(PSE
) |
1971 F(TSC
) | F(MSR
) | F(PAE
) | F(MCE
) |
1972 F(CX8
) | F(APIC
) | 0 /* Reserved */ | F(SYSCALL
) |
1973 F(MTRR
) | F(PGE
) | F(MCA
) | F(CMOV
) |
1974 F(PAT
) | F(PSE36
) | 0 /* Reserved */ |
1975 f_nx
| 0 /* Reserved */ | F(MMXEXT
) | F(MMX
) |
1976 F(FXSR
) | F(FXSR_OPT
) | f_gbpages
| f_rdtscp
|
1977 0 /* Reserved */ | f_lm
| F(3DNOWEXT
) | F(3DNOW
);
1979 const u32 kvm_supported_word4_x86_features
=
1980 F(XMM3
) | 0 /* Reserved, DTES64, MONITOR */ |
1981 0 /* DS-CPL, VMX, SMX, EST */ |
1982 0 /* TM2 */ | F(SSSE3
) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1983 0 /* Reserved */ | F(CX16
) | 0 /* xTPR Update, PDCM */ |
1984 0 /* Reserved, DCA */ | F(XMM4_1
) |
1985 F(XMM4_2
) | F(X2APIC
) | F(MOVBE
) | F(POPCNT
) |
1986 0 /* Reserved, AES */ | F(XSAVE
) | 0 /* OSXSAVE */;
1987 /* cpuid 0x80000001.ecx */
1988 const u32 kvm_supported_word6_x86_features
=
1989 F(LAHF_LM
) | F(CMP_LEGACY
) | F(SVM
) | 0 /* ExtApicSpace */ |
1990 F(CR8_LEGACY
) | F(ABM
) | F(SSE4A
) | F(MISALIGNSSE
) |
1991 F(3DNOWPREFETCH
) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5
) |
1992 0 /* SKINIT */ | 0 /* WDT */;
1994 /* all calls to cpuid_count() should be made on the same cpu */
1996 do_cpuid_1_ent(entry
, function
, index
);
2001 entry
->eax
= min(entry
->eax
, (u32
)0xd);
2004 entry
->edx
&= kvm_supported_word0_x86_features
;
2005 entry
->ecx
&= kvm_supported_word4_x86_features
;
2006 /* we support x2apic emulation even if host does not support
2007 * it since we emulate x2apic in software */
2008 entry
->ecx
|= F(X2APIC
);
2010 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
2011 * may return different values. This forces us to get_cpu() before
2012 * issuing the first command, and also to emulate this annoying behavior
2013 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
2015 int t
, times
= entry
->eax
& 0xff;
2017 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
2018 entry
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2019 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
2020 do_cpuid_1_ent(&entry
[t
], function
, 0);
2021 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
2026 /* function 4 and 0xb have additional index. */
2030 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2031 /* read more entries until cache_type is zero */
2032 for (i
= 1; *nent
< maxnent
; ++i
) {
2033 cache_type
= entry
[i
- 1].eax
& 0x1f;
2036 do_cpuid_1_ent(&entry
[i
], function
, i
);
2038 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2046 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2047 /* read more entries until level_type is zero */
2048 for (i
= 1; *nent
< maxnent
; ++i
) {
2049 level_type
= entry
[i
- 1].ecx
& 0xff00;
2052 do_cpuid_1_ent(&entry
[i
], function
, i
);
2054 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2062 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2063 for (i
= 1; *nent
< maxnent
; ++i
) {
2064 if (entry
[i
- 1].eax
== 0 && i
!= 2)
2066 do_cpuid_1_ent(&entry
[i
], function
, i
);
2068 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2073 case KVM_CPUID_SIGNATURE
: {
2074 char signature
[12] = "KVMKVMKVM\0\0";
2075 u32
*sigptr
= (u32
*)signature
;
2077 entry
->ebx
= sigptr
[0];
2078 entry
->ecx
= sigptr
[1];
2079 entry
->edx
= sigptr
[2];
2082 case KVM_CPUID_FEATURES
:
2083 entry
->eax
= (1 << KVM_FEATURE_CLOCKSOURCE
) |
2084 (1 << KVM_FEATURE_NOP_IO_DELAY
) |
2085 (1 << KVM_FEATURE_CLOCKSOURCE2
) |
2086 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
);
2092 entry
->eax
= min(entry
->eax
, 0x8000001a);
2095 entry
->edx
&= kvm_supported_word1_x86_features
;
2096 entry
->ecx
&= kvm_supported_word6_x86_features
;
2100 kvm_x86_ops
->set_supported_cpuid(function
, entry
);
2107 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
2108 struct kvm_cpuid_entry2 __user
*entries
)
2110 struct kvm_cpuid_entry2
*cpuid_entries
;
2111 int limit
, nent
= 0, r
= -E2BIG
;
2114 if (cpuid
->nent
< 1)
2116 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
2117 cpuid
->nent
= KVM_MAX_CPUID_ENTRIES
;
2119 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
2123 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
2124 limit
= cpuid_entries
[0].eax
;
2125 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
2126 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
2127 &nent
, cpuid
->nent
);
2129 if (nent
>= cpuid
->nent
)
2132 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
2133 limit
= cpuid_entries
[nent
- 1].eax
;
2134 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
2135 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
2136 &nent
, cpuid
->nent
);
2141 if (nent
>= cpuid
->nent
)
2144 do_cpuid_ent(&cpuid_entries
[nent
], KVM_CPUID_SIGNATURE
, 0, &nent
,
2148 if (nent
>= cpuid
->nent
)
2151 do_cpuid_ent(&cpuid_entries
[nent
], KVM_CPUID_FEATURES
, 0, &nent
,
2155 if (nent
>= cpuid
->nent
)
2159 if (copy_to_user(entries
, cpuid_entries
,
2160 nent
* sizeof(struct kvm_cpuid_entry2
)))
2166 vfree(cpuid_entries
);
2171 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
2172 struct kvm_lapic_state
*s
)
2174 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
2179 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
2180 struct kvm_lapic_state
*s
)
2182 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
2183 kvm_apic_post_state_restore(vcpu
);
2184 update_cr8_intercept(vcpu
);
2189 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
2190 struct kvm_interrupt
*irq
)
2192 if (irq
->irq
< 0 || irq
->irq
>= 256)
2194 if (irqchip_in_kernel(vcpu
->kvm
))
2197 kvm_queue_interrupt(vcpu
, irq
->irq
, false);
2202 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu
*vcpu
)
2204 kvm_inject_nmi(vcpu
);
2209 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
2210 struct kvm_tpr_access_ctl
*tac
)
2214 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
2218 static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu
*vcpu
,
2222 unsigned bank_num
= mcg_cap
& 0xff, bank
;
2225 if (!bank_num
|| bank_num
>= KVM_MAX_MCE_BANKS
)
2227 if (mcg_cap
& ~(KVM_MCE_CAP_SUPPORTED
| 0xff | 0xff0000))
2230 vcpu
->arch
.mcg_cap
= mcg_cap
;
2231 /* Init IA32_MCG_CTL to all 1s */
2232 if (mcg_cap
& MCG_CTL_P
)
2233 vcpu
->arch
.mcg_ctl
= ~(u64
)0;
2234 /* Init IA32_MCi_CTL to all 1s */
2235 for (bank
= 0; bank
< bank_num
; bank
++)
2236 vcpu
->arch
.mce_banks
[bank
*4] = ~(u64
)0;
2241 static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu
*vcpu
,
2242 struct kvm_x86_mce
*mce
)
2244 u64 mcg_cap
= vcpu
->arch
.mcg_cap
;
2245 unsigned bank_num
= mcg_cap
& 0xff;
2246 u64
*banks
= vcpu
->arch
.mce_banks
;
2248 if (mce
->bank
>= bank_num
|| !(mce
->status
& MCI_STATUS_VAL
))
2251 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2252 * reporting is disabled
2254 if ((mce
->status
& MCI_STATUS_UC
) && (mcg_cap
& MCG_CTL_P
) &&
2255 vcpu
->arch
.mcg_ctl
!= ~(u64
)0)
2257 banks
+= 4 * mce
->bank
;
2259 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2260 * reporting is disabled for the bank
2262 if ((mce
->status
& MCI_STATUS_UC
) && banks
[0] != ~(u64
)0)
2264 if (mce
->status
& MCI_STATUS_UC
) {
2265 if ((vcpu
->arch
.mcg_status
& MCG_STATUS_MCIP
) ||
2266 !kvm_read_cr4_bits(vcpu
, X86_CR4_MCE
)) {
2267 printk(KERN_DEBUG
"kvm: set_mce: "
2268 "injects mce exception while "
2269 "previous one is in progress!\n");
2270 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
2273 if (banks
[1] & MCI_STATUS_VAL
)
2274 mce
->status
|= MCI_STATUS_OVER
;
2275 banks
[2] = mce
->addr
;
2276 banks
[3] = mce
->misc
;
2277 vcpu
->arch
.mcg_status
= mce
->mcg_status
;
2278 banks
[1] = mce
->status
;
2279 kvm_queue_exception(vcpu
, MC_VECTOR
);
2280 } else if (!(banks
[1] & MCI_STATUS_VAL
)
2281 || !(banks
[1] & MCI_STATUS_UC
)) {
2282 if (banks
[1] & MCI_STATUS_VAL
)
2283 mce
->status
|= MCI_STATUS_OVER
;
2284 banks
[2] = mce
->addr
;
2285 banks
[3] = mce
->misc
;
2286 banks
[1] = mce
->status
;
2288 banks
[1] |= MCI_STATUS_OVER
;
2292 static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu
*vcpu
,
2293 struct kvm_vcpu_events
*events
)
2295 events
->exception
.injected
=
2296 vcpu
->arch
.exception
.pending
&&
2297 !kvm_exception_is_soft(vcpu
->arch
.exception
.nr
);
2298 events
->exception
.nr
= vcpu
->arch
.exception
.nr
;
2299 events
->exception
.has_error_code
= vcpu
->arch
.exception
.has_error_code
;
2300 events
->exception
.error_code
= vcpu
->arch
.exception
.error_code
;
2302 events
->interrupt
.injected
=
2303 vcpu
->arch
.interrupt
.pending
&& !vcpu
->arch
.interrupt
.soft
;
2304 events
->interrupt
.nr
= vcpu
->arch
.interrupt
.nr
;
2305 events
->interrupt
.soft
= 0;
2306 events
->interrupt
.shadow
=
2307 kvm_x86_ops
->get_interrupt_shadow(vcpu
,
2308 KVM_X86_SHADOW_INT_MOV_SS
| KVM_X86_SHADOW_INT_STI
);
2310 events
->nmi
.injected
= vcpu
->arch
.nmi_injected
;
2311 events
->nmi
.pending
= vcpu
->arch
.nmi_pending
;
2312 events
->nmi
.masked
= kvm_x86_ops
->get_nmi_mask(vcpu
);
2314 events
->sipi_vector
= vcpu
->arch
.sipi_vector
;
2316 events
->flags
= (KVM_VCPUEVENT_VALID_NMI_PENDING
2317 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2318 | KVM_VCPUEVENT_VALID_SHADOW
);
2321 static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu
*vcpu
,
2322 struct kvm_vcpu_events
*events
)
2324 if (events
->flags
& ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2325 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2326 | KVM_VCPUEVENT_VALID_SHADOW
))
2329 vcpu
->arch
.exception
.pending
= events
->exception
.injected
;
2330 vcpu
->arch
.exception
.nr
= events
->exception
.nr
;
2331 vcpu
->arch
.exception
.has_error_code
= events
->exception
.has_error_code
;
2332 vcpu
->arch
.exception
.error_code
= events
->exception
.error_code
;
2334 vcpu
->arch
.interrupt
.pending
= events
->interrupt
.injected
;
2335 vcpu
->arch
.interrupt
.nr
= events
->interrupt
.nr
;
2336 vcpu
->arch
.interrupt
.soft
= events
->interrupt
.soft
;
2337 if (vcpu
->arch
.interrupt
.pending
&& irqchip_in_kernel(vcpu
->kvm
))
2338 kvm_pic_clear_isr_ack(vcpu
->kvm
);
2339 if (events
->flags
& KVM_VCPUEVENT_VALID_SHADOW
)
2340 kvm_x86_ops
->set_interrupt_shadow(vcpu
,
2341 events
->interrupt
.shadow
);
2343 vcpu
->arch
.nmi_injected
= events
->nmi
.injected
;
2344 if (events
->flags
& KVM_VCPUEVENT_VALID_NMI_PENDING
)
2345 vcpu
->arch
.nmi_pending
= events
->nmi
.pending
;
2346 kvm_x86_ops
->set_nmi_mask(vcpu
, events
->nmi
.masked
);
2348 if (events
->flags
& KVM_VCPUEVENT_VALID_SIPI_VECTOR
)
2349 vcpu
->arch
.sipi_vector
= events
->sipi_vector
;
2354 static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu
*vcpu
,
2355 struct kvm_debugregs
*dbgregs
)
2357 memcpy(dbgregs
->db
, vcpu
->arch
.db
, sizeof(vcpu
->arch
.db
));
2358 dbgregs
->dr6
= vcpu
->arch
.dr6
;
2359 dbgregs
->dr7
= vcpu
->arch
.dr7
;
2363 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu
*vcpu
,
2364 struct kvm_debugregs
*dbgregs
)
2369 memcpy(vcpu
->arch
.db
, dbgregs
->db
, sizeof(vcpu
->arch
.db
));
2370 vcpu
->arch
.dr6
= dbgregs
->dr6
;
2371 vcpu
->arch
.dr7
= dbgregs
->dr7
;
2376 long kvm_arch_vcpu_ioctl(struct file
*filp
,
2377 unsigned int ioctl
, unsigned long arg
)
2379 struct kvm_vcpu
*vcpu
= filp
->private_data
;
2380 void __user
*argp
= (void __user
*)arg
;
2382 struct kvm_lapic_state
*lapic
= NULL
;
2385 case KVM_GET_LAPIC
: {
2387 if (!vcpu
->arch
.apic
)
2389 lapic
= kzalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
2394 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, lapic
);
2398 if (copy_to_user(argp
, lapic
, sizeof(struct kvm_lapic_state
)))
2403 case KVM_SET_LAPIC
: {
2405 if (!vcpu
->arch
.apic
)
2407 lapic
= kmalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
2412 if (copy_from_user(lapic
, argp
, sizeof(struct kvm_lapic_state
)))
2414 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, lapic
);
2420 case KVM_INTERRUPT
: {
2421 struct kvm_interrupt irq
;
2424 if (copy_from_user(&irq
, argp
, sizeof irq
))
2426 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
2433 r
= kvm_vcpu_ioctl_nmi(vcpu
);
2439 case KVM_SET_CPUID
: {
2440 struct kvm_cpuid __user
*cpuid_arg
= argp
;
2441 struct kvm_cpuid cpuid
;
2444 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2446 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
2451 case KVM_SET_CPUID2
: {
2452 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
2453 struct kvm_cpuid2 cpuid
;
2456 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2458 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
2459 cpuid_arg
->entries
);
2464 case KVM_GET_CPUID2
: {
2465 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
2466 struct kvm_cpuid2 cpuid
;
2469 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
2471 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
2472 cpuid_arg
->entries
);
2476 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
2482 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
2485 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
2487 case KVM_TPR_ACCESS_REPORTING
: {
2488 struct kvm_tpr_access_ctl tac
;
2491 if (copy_from_user(&tac
, argp
, sizeof tac
))
2493 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
2497 if (copy_to_user(argp
, &tac
, sizeof tac
))
2502 case KVM_SET_VAPIC_ADDR
: {
2503 struct kvm_vapic_addr va
;
2506 if (!irqchip_in_kernel(vcpu
->kvm
))
2509 if (copy_from_user(&va
, argp
, sizeof va
))
2512 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
2515 case KVM_X86_SETUP_MCE
: {
2519 if (copy_from_user(&mcg_cap
, argp
, sizeof mcg_cap
))
2521 r
= kvm_vcpu_ioctl_x86_setup_mce(vcpu
, mcg_cap
);
2524 case KVM_X86_SET_MCE
: {
2525 struct kvm_x86_mce mce
;
2528 if (copy_from_user(&mce
, argp
, sizeof mce
))
2530 r
= kvm_vcpu_ioctl_x86_set_mce(vcpu
, &mce
);
2533 case KVM_GET_VCPU_EVENTS
: {
2534 struct kvm_vcpu_events events
;
2536 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu
, &events
);
2539 if (copy_to_user(argp
, &events
, sizeof(struct kvm_vcpu_events
)))
2544 case KVM_SET_VCPU_EVENTS
: {
2545 struct kvm_vcpu_events events
;
2548 if (copy_from_user(&events
, argp
, sizeof(struct kvm_vcpu_events
)))
2551 r
= kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu
, &events
);
2554 case KVM_GET_DEBUGREGS
: {
2555 struct kvm_debugregs dbgregs
;
2557 kvm_vcpu_ioctl_x86_get_debugregs(vcpu
, &dbgregs
);
2560 if (copy_to_user(argp
, &dbgregs
,
2561 sizeof(struct kvm_debugregs
)))
2566 case KVM_SET_DEBUGREGS
: {
2567 struct kvm_debugregs dbgregs
;
2570 if (copy_from_user(&dbgregs
, argp
,
2571 sizeof(struct kvm_debugregs
)))
2574 r
= kvm_vcpu_ioctl_x86_set_debugregs(vcpu
, &dbgregs
);
2585 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
2589 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
2591 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
2595 static int kvm_vm_ioctl_set_identity_map_addr(struct kvm
*kvm
,
2598 kvm
->arch
.ept_identity_map_addr
= ident_addr
;
2602 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
2603 u32 kvm_nr_mmu_pages
)
2605 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
2608 mutex_lock(&kvm
->slots_lock
);
2609 spin_lock(&kvm
->mmu_lock
);
2611 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
2612 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
2614 spin_unlock(&kvm
->mmu_lock
);
2615 mutex_unlock(&kvm
->slots_lock
);
2619 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
2621 return kvm
->arch
.n_alloc_mmu_pages
;
2624 gfn_t
unalias_gfn_instantiation(struct kvm
*kvm
, gfn_t gfn
)
2627 struct kvm_mem_alias
*alias
;
2628 struct kvm_mem_aliases
*aliases
;
2630 aliases
= kvm_aliases(kvm
);
2632 for (i
= 0; i
< aliases
->naliases
; ++i
) {
2633 alias
= &aliases
->aliases
[i
];
2634 if (alias
->flags
& KVM_ALIAS_INVALID
)
2636 if (gfn
>= alias
->base_gfn
2637 && gfn
< alias
->base_gfn
+ alias
->npages
)
2638 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
2643 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
2646 struct kvm_mem_alias
*alias
;
2647 struct kvm_mem_aliases
*aliases
;
2649 aliases
= kvm_aliases(kvm
);
2651 for (i
= 0; i
< aliases
->naliases
; ++i
) {
2652 alias
= &aliases
->aliases
[i
];
2653 if (gfn
>= alias
->base_gfn
2654 && gfn
< alias
->base_gfn
+ alias
->npages
)
2655 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
2661 * Set a new alias region. Aliases map a portion of physical memory into
2662 * another portion. This is useful for memory windows, for example the PC
2665 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
2666 struct kvm_memory_alias
*alias
)
2669 struct kvm_mem_alias
*p
;
2670 struct kvm_mem_aliases
*aliases
, *old_aliases
;
2673 /* General sanity checks */
2674 if (alias
->memory_size
& (PAGE_SIZE
- 1))
2676 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
2678 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
2680 if (alias
->guest_phys_addr
+ alias
->memory_size
2681 < alias
->guest_phys_addr
)
2683 if (alias
->target_phys_addr
+ alias
->memory_size
2684 < alias
->target_phys_addr
)
2688 aliases
= kzalloc(sizeof(struct kvm_mem_aliases
), GFP_KERNEL
);
2692 mutex_lock(&kvm
->slots_lock
);
2694 /* invalidate any gfn reference in case of deletion/shrinking */
2695 memcpy(aliases
, kvm
->arch
.aliases
, sizeof(struct kvm_mem_aliases
));
2696 aliases
->aliases
[alias
->slot
].flags
|= KVM_ALIAS_INVALID
;
2697 old_aliases
= kvm
->arch
.aliases
;
2698 rcu_assign_pointer(kvm
->arch
.aliases
, aliases
);
2699 synchronize_srcu_expedited(&kvm
->srcu
);
2700 kvm_mmu_zap_all(kvm
);
2704 aliases
= kzalloc(sizeof(struct kvm_mem_aliases
), GFP_KERNEL
);
2708 memcpy(aliases
, kvm
->arch
.aliases
, sizeof(struct kvm_mem_aliases
));
2710 p
= &aliases
->aliases
[alias
->slot
];
2711 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
2712 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
2713 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
2714 p
->flags
&= ~(KVM_ALIAS_INVALID
);
2716 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
2717 if (aliases
->aliases
[n
- 1].npages
)
2719 aliases
->naliases
= n
;
2721 old_aliases
= kvm
->arch
.aliases
;
2722 rcu_assign_pointer(kvm
->arch
.aliases
, aliases
);
2723 synchronize_srcu_expedited(&kvm
->srcu
);
2728 mutex_unlock(&kvm
->slots_lock
);
2733 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
2738 switch (chip
->chip_id
) {
2739 case KVM_IRQCHIP_PIC_MASTER
:
2740 memcpy(&chip
->chip
.pic
,
2741 &pic_irqchip(kvm
)->pics
[0],
2742 sizeof(struct kvm_pic_state
));
2744 case KVM_IRQCHIP_PIC_SLAVE
:
2745 memcpy(&chip
->chip
.pic
,
2746 &pic_irqchip(kvm
)->pics
[1],
2747 sizeof(struct kvm_pic_state
));
2749 case KVM_IRQCHIP_IOAPIC
:
2750 r
= kvm_get_ioapic(kvm
, &chip
->chip
.ioapic
);
2759 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
2764 switch (chip
->chip_id
) {
2765 case KVM_IRQCHIP_PIC_MASTER
:
2766 raw_spin_lock(&pic_irqchip(kvm
)->lock
);
2767 memcpy(&pic_irqchip(kvm
)->pics
[0],
2769 sizeof(struct kvm_pic_state
));
2770 raw_spin_unlock(&pic_irqchip(kvm
)->lock
);
2772 case KVM_IRQCHIP_PIC_SLAVE
:
2773 raw_spin_lock(&pic_irqchip(kvm
)->lock
);
2774 memcpy(&pic_irqchip(kvm
)->pics
[1],
2776 sizeof(struct kvm_pic_state
));
2777 raw_spin_unlock(&pic_irqchip(kvm
)->lock
);
2779 case KVM_IRQCHIP_IOAPIC
:
2780 r
= kvm_set_ioapic(kvm
, &chip
->chip
.ioapic
);
2786 kvm_pic_update_irq(pic_irqchip(kvm
));
2790 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
2794 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2795 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
2796 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2800 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
2804 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2805 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
2806 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
, 0);
2807 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2811 static int kvm_vm_ioctl_get_pit2(struct kvm
*kvm
, struct kvm_pit_state2
*ps
)
2815 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2816 memcpy(ps
->channels
, &kvm
->arch
.vpit
->pit_state
.channels
,
2817 sizeof(ps
->channels
));
2818 ps
->flags
= kvm
->arch
.vpit
->pit_state
.flags
;
2819 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2823 static int kvm_vm_ioctl_set_pit2(struct kvm
*kvm
, struct kvm_pit_state2
*ps
)
2825 int r
= 0, start
= 0;
2826 u32 prev_legacy
, cur_legacy
;
2827 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2828 prev_legacy
= kvm
->arch
.vpit
->pit_state
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
2829 cur_legacy
= ps
->flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
2830 if (!prev_legacy
&& cur_legacy
)
2832 memcpy(&kvm
->arch
.vpit
->pit_state
.channels
, &ps
->channels
,
2833 sizeof(kvm
->arch
.vpit
->pit_state
.channels
));
2834 kvm
->arch
.vpit
->pit_state
.flags
= ps
->flags
;
2835 kvm_pit_load_count(kvm
, 0, kvm
->arch
.vpit
->pit_state
.channels
[0].count
, start
);
2836 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2840 static int kvm_vm_ioctl_reinject(struct kvm
*kvm
,
2841 struct kvm_reinject_control
*control
)
2843 if (!kvm
->arch
.vpit
)
2845 mutex_lock(&kvm
->arch
.vpit
->pit_state
.lock
);
2846 kvm
->arch
.vpit
->pit_state
.pit_timer
.reinject
= control
->pit_reinject
;
2847 mutex_unlock(&kvm
->arch
.vpit
->pit_state
.lock
);
2852 * Get (and clear) the dirty memory log for a memory slot.
2854 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
2855 struct kvm_dirty_log
*log
)
2858 struct kvm_memory_slot
*memslot
;
2860 unsigned long is_dirty
= 0;
2862 mutex_lock(&kvm
->slots_lock
);
2865 if (log
->slot
>= KVM_MEMORY_SLOTS
)
2868 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
2870 if (!memslot
->dirty_bitmap
)
2873 n
= kvm_dirty_bitmap_bytes(memslot
);
2875 for (i
= 0; !is_dirty
&& i
< n
/sizeof(long); i
++)
2876 is_dirty
= memslot
->dirty_bitmap
[i
];
2878 /* If nothing is dirty, don't bother messing with page tables. */
2880 struct kvm_memslots
*slots
, *old_slots
;
2881 unsigned long *dirty_bitmap
;
2883 spin_lock(&kvm
->mmu_lock
);
2884 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
2885 spin_unlock(&kvm
->mmu_lock
);
2888 dirty_bitmap
= vmalloc(n
);
2891 memset(dirty_bitmap
, 0, n
);
2894 slots
= kzalloc(sizeof(struct kvm_memslots
), GFP_KERNEL
);
2896 vfree(dirty_bitmap
);
2899 memcpy(slots
, kvm
->memslots
, sizeof(struct kvm_memslots
));
2900 slots
->memslots
[log
->slot
].dirty_bitmap
= dirty_bitmap
;
2902 old_slots
= kvm
->memslots
;
2903 rcu_assign_pointer(kvm
->memslots
, slots
);
2904 synchronize_srcu_expedited(&kvm
->srcu
);
2905 dirty_bitmap
= old_slots
->memslots
[log
->slot
].dirty_bitmap
;
2909 if (copy_to_user(log
->dirty_bitmap
, dirty_bitmap
, n
)) {
2910 vfree(dirty_bitmap
);
2913 vfree(dirty_bitmap
);
2916 if (clear_user(log
->dirty_bitmap
, n
))
2922 mutex_unlock(&kvm
->slots_lock
);
2926 long kvm_arch_vm_ioctl(struct file
*filp
,
2927 unsigned int ioctl
, unsigned long arg
)
2929 struct kvm
*kvm
= filp
->private_data
;
2930 void __user
*argp
= (void __user
*)arg
;
2933 * This union makes it completely explicit to gcc-3.x
2934 * that these two variables' stack usage should be
2935 * combined, not added together.
2938 struct kvm_pit_state ps
;
2939 struct kvm_pit_state2 ps2
;
2940 struct kvm_memory_alias alias
;
2941 struct kvm_pit_config pit_config
;
2945 case KVM_SET_TSS_ADDR
:
2946 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
2950 case KVM_SET_IDENTITY_MAP_ADDR
: {
2954 if (copy_from_user(&ident_addr
, argp
, sizeof ident_addr
))
2956 r
= kvm_vm_ioctl_set_identity_map_addr(kvm
, ident_addr
);
2961 case KVM_SET_MEMORY_REGION
: {
2962 struct kvm_memory_region kvm_mem
;
2963 struct kvm_userspace_memory_region kvm_userspace_mem
;
2966 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
2968 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
2969 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
2970 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
2971 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
2972 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2977 case KVM_SET_NR_MMU_PAGES
:
2978 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
2982 case KVM_GET_NR_MMU_PAGES
:
2983 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
2985 case KVM_SET_MEMORY_ALIAS
:
2987 if (copy_from_user(&u
.alias
, argp
, sizeof(struct kvm_memory_alias
)))
2989 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &u
.alias
);
2993 case KVM_CREATE_IRQCHIP
: {
2994 struct kvm_pic
*vpic
;
2996 mutex_lock(&kvm
->lock
);
2999 goto create_irqchip_unlock
;
3001 vpic
= kvm_create_pic(kvm
);
3003 r
= kvm_ioapic_init(kvm
);
3005 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
,
3008 goto create_irqchip_unlock
;
3011 goto create_irqchip_unlock
;
3013 kvm
->arch
.vpic
= vpic
;
3015 r
= kvm_setup_default_irq_routing(kvm
);
3017 mutex_lock(&kvm
->irq_lock
);
3018 kvm_ioapic_destroy(kvm
);
3019 kvm_destroy_pic(kvm
);
3020 mutex_unlock(&kvm
->irq_lock
);
3022 create_irqchip_unlock
:
3023 mutex_unlock(&kvm
->lock
);
3026 case KVM_CREATE_PIT
:
3027 u
.pit_config
.flags
= KVM_PIT_SPEAKER_DUMMY
;
3029 case KVM_CREATE_PIT2
:
3031 if (copy_from_user(&u
.pit_config
, argp
,
3032 sizeof(struct kvm_pit_config
)))
3035 mutex_lock(&kvm
->slots_lock
);
3038 goto create_pit_unlock
;
3040 kvm
->arch
.vpit
= kvm_create_pit(kvm
, u
.pit_config
.flags
);
3044 mutex_unlock(&kvm
->slots_lock
);
3046 case KVM_IRQ_LINE_STATUS
:
3047 case KVM_IRQ_LINE
: {
3048 struct kvm_irq_level irq_event
;
3051 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
3054 if (irqchip_in_kernel(kvm
)) {
3056 status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
3057 irq_event
.irq
, irq_event
.level
);
3058 if (ioctl
== KVM_IRQ_LINE_STATUS
) {
3060 irq_event
.status
= status
;
3061 if (copy_to_user(argp
, &irq_event
,
3069 case KVM_GET_IRQCHIP
: {
3070 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3071 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
3077 if (copy_from_user(chip
, argp
, sizeof *chip
))
3078 goto get_irqchip_out
;
3080 if (!irqchip_in_kernel(kvm
))
3081 goto get_irqchip_out
;
3082 r
= kvm_vm_ioctl_get_irqchip(kvm
, chip
);
3084 goto get_irqchip_out
;
3086 if (copy_to_user(argp
, chip
, sizeof *chip
))
3087 goto get_irqchip_out
;
3095 case KVM_SET_IRQCHIP
: {
3096 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3097 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
3103 if (copy_from_user(chip
, argp
, sizeof *chip
))
3104 goto set_irqchip_out
;
3106 if (!irqchip_in_kernel(kvm
))
3107 goto set_irqchip_out
;
3108 r
= kvm_vm_ioctl_set_irqchip(kvm
, chip
);
3110 goto set_irqchip_out
;
3120 if (copy_from_user(&u
.ps
, argp
, sizeof(struct kvm_pit_state
)))
3123 if (!kvm
->arch
.vpit
)
3125 r
= kvm_vm_ioctl_get_pit(kvm
, &u
.ps
);
3129 if (copy_to_user(argp
, &u
.ps
, sizeof(struct kvm_pit_state
)))
3136 if (copy_from_user(&u
.ps
, argp
, sizeof u
.ps
))
3139 if (!kvm
->arch
.vpit
)
3141 r
= kvm_vm_ioctl_set_pit(kvm
, &u
.ps
);
3147 case KVM_GET_PIT2
: {
3149 if (!kvm
->arch
.vpit
)
3151 r
= kvm_vm_ioctl_get_pit2(kvm
, &u
.ps2
);
3155 if (copy_to_user(argp
, &u
.ps2
, sizeof(u
.ps2
)))
3160 case KVM_SET_PIT2
: {
3162 if (copy_from_user(&u
.ps2
, argp
, sizeof(u
.ps2
)))
3165 if (!kvm
->arch
.vpit
)
3167 r
= kvm_vm_ioctl_set_pit2(kvm
, &u
.ps2
);
3173 case KVM_REINJECT_CONTROL
: {
3174 struct kvm_reinject_control control
;
3176 if (copy_from_user(&control
, argp
, sizeof(control
)))
3178 r
= kvm_vm_ioctl_reinject(kvm
, &control
);
3184 case KVM_XEN_HVM_CONFIG
: {
3186 if (copy_from_user(&kvm
->arch
.xen_hvm_config
, argp
,
3187 sizeof(struct kvm_xen_hvm_config
)))
3190 if (kvm
->arch
.xen_hvm_config
.flags
)
3195 case KVM_SET_CLOCK
: {
3196 struct timespec now
;
3197 struct kvm_clock_data user_ns
;
3202 if (copy_from_user(&user_ns
, argp
, sizeof(user_ns
)))
3211 now_ns
= timespec_to_ns(&now
);
3212 delta
= user_ns
.clock
- now_ns
;
3213 kvm
->arch
.kvmclock_offset
= delta
;
3216 case KVM_GET_CLOCK
: {
3217 struct timespec now
;
3218 struct kvm_clock_data user_ns
;
3222 now_ns
= timespec_to_ns(&now
);
3223 user_ns
.clock
= kvm
->arch
.kvmclock_offset
+ now_ns
;
3227 if (copy_to_user(argp
, &user_ns
, sizeof(user_ns
)))
3240 static void kvm_init_msr_list(void)
3245 /* skip the first msrs in the list. KVM-specific */
3246 for (i
= j
= KVM_SAVE_MSRS_BEGIN
; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
3247 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
3250 msrs_to_save
[j
] = msrs_to_save
[i
];
3253 num_msrs_to_save
= j
;
3256 static int vcpu_mmio_write(struct kvm_vcpu
*vcpu
, gpa_t addr
, int len
,
3259 if (vcpu
->arch
.apic
&&
3260 !kvm_iodevice_write(&vcpu
->arch
.apic
->dev
, addr
, len
, v
))
3263 return kvm_io_bus_write(vcpu
->kvm
, KVM_MMIO_BUS
, addr
, len
, v
);
3266 static int vcpu_mmio_read(struct kvm_vcpu
*vcpu
, gpa_t addr
, int len
, void *v
)
3268 if (vcpu
->arch
.apic
&&
3269 !kvm_iodevice_read(&vcpu
->arch
.apic
->dev
, addr
, len
, v
))
3272 return kvm_io_bus_read(vcpu
->kvm
, KVM_MMIO_BUS
, addr
, len
, v
);
3275 static void kvm_set_segment(struct kvm_vcpu
*vcpu
,
3276 struct kvm_segment
*var
, int seg
)
3278 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3281 void kvm_get_segment(struct kvm_vcpu
*vcpu
,
3282 struct kvm_segment
*var
, int seg
)
3284 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
3287 gpa_t
kvm_mmu_gva_to_gpa_read(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3289 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3290 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, access
, error
);
3293 gpa_t
kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3295 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3296 access
|= PFERR_FETCH_MASK
;
3297 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, access
, error
);
3300 gpa_t
kvm_mmu_gva_to_gpa_write(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3302 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3303 access
|= PFERR_WRITE_MASK
;
3304 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, access
, error
);
3307 /* uses this to access any guest's mapped memory without checking CPL */
3308 gpa_t
kvm_mmu_gva_to_gpa_system(struct kvm_vcpu
*vcpu
, gva_t gva
, u32
*error
)
3310 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
, 0, error
);
3313 static int kvm_read_guest_virt_helper(gva_t addr
, void *val
, unsigned int bytes
,
3314 struct kvm_vcpu
*vcpu
, u32 access
,
3318 int r
= X86EMUL_CONTINUE
;
3321 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
, access
, error
);
3322 unsigned offset
= addr
& (PAGE_SIZE
-1);
3323 unsigned toread
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
3326 if (gpa
== UNMAPPED_GVA
) {
3327 r
= X86EMUL_PROPAGATE_FAULT
;
3330 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, toread
);
3332 r
= X86EMUL_IO_NEEDED
;
3344 /* used for instruction fetching */
3345 static int kvm_fetch_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
3346 struct kvm_vcpu
*vcpu
, u32
*error
)
3348 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3349 return kvm_read_guest_virt_helper(addr
, val
, bytes
, vcpu
,
3350 access
| PFERR_FETCH_MASK
, error
);
3353 static int kvm_read_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
3354 struct kvm_vcpu
*vcpu
, u32
*error
)
3356 u32 access
= (kvm_x86_ops
->get_cpl(vcpu
) == 3) ? PFERR_USER_MASK
: 0;
3357 return kvm_read_guest_virt_helper(addr
, val
, bytes
, vcpu
, access
,
3361 static int kvm_read_guest_virt_system(gva_t addr
, void *val
, unsigned int bytes
,
3362 struct kvm_vcpu
*vcpu
, u32
*error
)
3364 return kvm_read_guest_virt_helper(addr
, val
, bytes
, vcpu
, 0, error
);
3367 static int kvm_write_guest_virt_system(gva_t addr
, void *val
,
3369 struct kvm_vcpu
*vcpu
,
3373 int r
= X86EMUL_CONTINUE
;
3376 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
,
3377 PFERR_WRITE_MASK
, error
);
3378 unsigned offset
= addr
& (PAGE_SIZE
-1);
3379 unsigned towrite
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
3382 if (gpa
== UNMAPPED_GVA
) {
3383 r
= X86EMUL_PROPAGATE_FAULT
;
3386 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, data
, towrite
);
3388 r
= X86EMUL_IO_NEEDED
;
3400 static int emulator_read_emulated(unsigned long addr
,
3403 unsigned int *error_code
,
3404 struct kvm_vcpu
*vcpu
)
3408 if (vcpu
->mmio_read_completed
) {
3409 memcpy(val
, vcpu
->mmio_data
, bytes
);
3410 trace_kvm_mmio(KVM_TRACE_MMIO_READ
, bytes
,
3411 vcpu
->mmio_phys_addr
, *(u64
*)val
);
3412 vcpu
->mmio_read_completed
= 0;
3413 return X86EMUL_CONTINUE
;
3416 gpa
= kvm_mmu_gva_to_gpa_read(vcpu
, addr
, error_code
);
3418 if (gpa
== UNMAPPED_GVA
)
3419 return X86EMUL_PROPAGATE_FAULT
;
3421 /* For APIC access vmexit */
3422 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
3425 if (kvm_read_guest_virt(addr
, val
, bytes
, vcpu
, NULL
)
3426 == X86EMUL_CONTINUE
)
3427 return X86EMUL_CONTINUE
;
3431 * Is this MMIO handled locally?
3433 if (!vcpu_mmio_read(vcpu
, gpa
, bytes
, val
)) {
3434 trace_kvm_mmio(KVM_TRACE_MMIO_READ
, bytes
, gpa
, *(u64
*)val
);
3435 return X86EMUL_CONTINUE
;
3438 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED
, bytes
, gpa
, 0);
3440 vcpu
->mmio_needed
= 1;
3441 vcpu
->run
->exit_reason
= KVM_EXIT_MMIO
;
3442 vcpu
->run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
= gpa
;
3443 vcpu
->run
->mmio
.len
= vcpu
->mmio_size
= bytes
;
3444 vcpu
->run
->mmio
.is_write
= vcpu
->mmio_is_write
= 0;
3446 return X86EMUL_IO_NEEDED
;
3449 int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
3450 const void *val
, int bytes
)
3454 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
3457 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
, 1);
3461 static int emulator_write_emulated_onepage(unsigned long addr
,
3464 unsigned int *error_code
,
3465 struct kvm_vcpu
*vcpu
)
3469 gpa
= kvm_mmu_gva_to_gpa_write(vcpu
, addr
, error_code
);
3471 if (gpa
== UNMAPPED_GVA
)
3472 return X86EMUL_PROPAGATE_FAULT
;
3474 /* For APIC access vmexit */
3475 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
3478 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
3479 return X86EMUL_CONTINUE
;
3482 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE
, bytes
, gpa
, *(u64
*)val
);
3484 * Is this MMIO handled locally?
3486 if (!vcpu_mmio_write(vcpu
, gpa
, bytes
, val
))
3487 return X86EMUL_CONTINUE
;
3489 vcpu
->mmio_needed
= 1;
3490 vcpu
->run
->exit_reason
= KVM_EXIT_MMIO
;
3491 vcpu
->run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
= gpa
;
3492 vcpu
->run
->mmio
.len
= vcpu
->mmio_size
= bytes
;
3493 vcpu
->run
->mmio
.is_write
= vcpu
->mmio_is_write
= 1;
3494 memcpy(vcpu
->run
->mmio
.data
, val
, bytes
);
3496 return X86EMUL_CONTINUE
;
3499 int emulator_write_emulated(unsigned long addr
,
3502 unsigned int *error_code
,
3503 struct kvm_vcpu
*vcpu
)
3505 /* Crossing a page boundary? */
3506 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
3509 now
= -addr
& ~PAGE_MASK
;
3510 rc
= emulator_write_emulated_onepage(addr
, val
, now
, error_code
,
3512 if (rc
!= X86EMUL_CONTINUE
)
3518 return emulator_write_emulated_onepage(addr
, val
, bytes
, error_code
,
3522 #define CMPXCHG_TYPE(t, ptr, old, new) \
3523 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3525 #ifdef CONFIG_X86_64
3526 # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3528 # define CMPXCHG64(ptr, old, new) \
3529 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
3532 static int emulator_cmpxchg_emulated(unsigned long addr
,
3536 unsigned int *error_code
,
3537 struct kvm_vcpu
*vcpu
)
3544 /* guests cmpxchg8b have to be emulated atomically */
3545 if (bytes
> 8 || (bytes
& (bytes
- 1)))
3548 gpa
= kvm_mmu_gva_to_gpa_write(vcpu
, addr
, NULL
);
3550 if (gpa
== UNMAPPED_GVA
||
3551 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
3554 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
3557 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
3559 kaddr
= kmap_atomic(page
, KM_USER0
);
3560 kaddr
+= offset_in_page(gpa
);
3563 exchanged
= CMPXCHG_TYPE(u8
, kaddr
, old
, new);
3566 exchanged
= CMPXCHG_TYPE(u16
, kaddr
, old
, new);
3569 exchanged
= CMPXCHG_TYPE(u32
, kaddr
, old
, new);
3572 exchanged
= CMPXCHG64(kaddr
, old
, new);
3577 kunmap_atomic(kaddr
, KM_USER0
);
3578 kvm_release_page_dirty(page
);
3581 return X86EMUL_CMPXCHG_FAILED
;
3583 kvm_mmu_pte_write(vcpu
, gpa
, new, bytes
, 1);
3585 return X86EMUL_CONTINUE
;
3588 printk_once(KERN_WARNING
"kvm: emulating exchange as write\n");
3590 return emulator_write_emulated(addr
, new, bytes
, error_code
, vcpu
);
3593 static int kernel_pio(struct kvm_vcpu
*vcpu
, void *pd
)
3595 /* TODO: String I/O for in kernel device */
3598 if (vcpu
->arch
.pio
.in
)
3599 r
= kvm_io_bus_read(vcpu
->kvm
, KVM_PIO_BUS
, vcpu
->arch
.pio
.port
,
3600 vcpu
->arch
.pio
.size
, pd
);
3602 r
= kvm_io_bus_write(vcpu
->kvm
, KVM_PIO_BUS
,
3603 vcpu
->arch
.pio
.port
, vcpu
->arch
.pio
.size
,
3609 static int emulator_pio_in_emulated(int size
, unsigned short port
, void *val
,
3610 unsigned int count
, struct kvm_vcpu
*vcpu
)
3612 if (vcpu
->arch
.pio
.count
)
3615 trace_kvm_pio(1, port
, size
, 1);
3617 vcpu
->arch
.pio
.port
= port
;
3618 vcpu
->arch
.pio
.in
= 1;
3619 vcpu
->arch
.pio
.count
= count
;
3620 vcpu
->arch
.pio
.size
= size
;
3622 if (!kernel_pio(vcpu
, vcpu
->arch
.pio_data
)) {
3624 memcpy(val
, vcpu
->arch
.pio_data
, size
* count
);
3625 vcpu
->arch
.pio
.count
= 0;
3629 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
3630 vcpu
->run
->io
.direction
= KVM_EXIT_IO_IN
;
3631 vcpu
->run
->io
.size
= size
;
3632 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
3633 vcpu
->run
->io
.count
= count
;
3634 vcpu
->run
->io
.port
= port
;
3639 static int emulator_pio_out_emulated(int size
, unsigned short port
,
3640 const void *val
, unsigned int count
,
3641 struct kvm_vcpu
*vcpu
)
3643 trace_kvm_pio(0, port
, size
, 1);
3645 vcpu
->arch
.pio
.port
= port
;
3646 vcpu
->arch
.pio
.in
= 0;
3647 vcpu
->arch
.pio
.count
= count
;
3648 vcpu
->arch
.pio
.size
= size
;
3650 memcpy(vcpu
->arch
.pio_data
, val
, size
* count
);
3652 if (!kernel_pio(vcpu
, vcpu
->arch
.pio_data
)) {
3653 vcpu
->arch
.pio
.count
= 0;
3657 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
3658 vcpu
->run
->io
.direction
= KVM_EXIT_IO_OUT
;
3659 vcpu
->run
->io
.size
= size
;
3660 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
3661 vcpu
->run
->io
.count
= count
;
3662 vcpu
->run
->io
.port
= port
;
3667 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
3669 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
3672 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
3674 kvm_mmu_invlpg(vcpu
, address
);
3675 return X86EMUL_CONTINUE
;
3678 int emulate_clts(struct kvm_vcpu
*vcpu
)
3680 kvm_x86_ops
->set_cr0(vcpu
, kvm_read_cr0_bits(vcpu
, ~X86_CR0_TS
));
3681 kvm_x86_ops
->fpu_activate(vcpu
);
3682 return X86EMUL_CONTINUE
;
3685 int emulator_get_dr(int dr
, unsigned long *dest
, struct kvm_vcpu
*vcpu
)
3687 return _kvm_get_dr(vcpu
, dr
, dest
);
3690 int emulator_set_dr(int dr
, unsigned long value
, struct kvm_vcpu
*vcpu
)
3693 return __kvm_set_dr(vcpu
, dr
, value
);
3696 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
3698 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
3701 static unsigned long emulator_get_cr(int cr
, struct kvm_vcpu
*vcpu
)
3703 unsigned long value
;
3707 value
= kvm_read_cr0(vcpu
);
3710 value
= vcpu
->arch
.cr2
;
3713 value
= vcpu
->arch
.cr3
;
3716 value
= kvm_read_cr4(vcpu
);
3719 value
= kvm_get_cr8(vcpu
);
3722 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3729 static int emulator_set_cr(int cr
, unsigned long val
, struct kvm_vcpu
*vcpu
)
3735 res
= __kvm_set_cr0(vcpu
, mk_cr_64(kvm_read_cr0(vcpu
), val
));
3738 vcpu
->arch
.cr2
= val
;
3741 res
= __kvm_set_cr3(vcpu
, val
);
3744 res
= __kvm_set_cr4(vcpu
, mk_cr_64(kvm_read_cr4(vcpu
), val
));
3747 res
= __kvm_set_cr8(vcpu
, val
& 0xfUL
);
3750 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3757 static int emulator_get_cpl(struct kvm_vcpu
*vcpu
)
3759 return kvm_x86_ops
->get_cpl(vcpu
);
3762 static void emulator_get_gdt(struct desc_ptr
*dt
, struct kvm_vcpu
*vcpu
)
3764 kvm_x86_ops
->get_gdt(vcpu
, dt
);
3767 static unsigned long emulator_get_cached_segment_base(int seg
,
3768 struct kvm_vcpu
*vcpu
)
3770 return get_segment_base(vcpu
, seg
);
3773 static bool emulator_get_cached_descriptor(struct desc_struct
*desc
, int seg
,
3774 struct kvm_vcpu
*vcpu
)
3776 struct kvm_segment var
;
3778 kvm_get_segment(vcpu
, &var
, seg
);
3785 set_desc_limit(desc
, var
.limit
);
3786 set_desc_base(desc
, (unsigned long)var
.base
);
3787 desc
->type
= var
.type
;
3789 desc
->dpl
= var
.dpl
;
3790 desc
->p
= var
.present
;
3791 desc
->avl
= var
.avl
;
3799 static void emulator_set_cached_descriptor(struct desc_struct
*desc
, int seg
,
3800 struct kvm_vcpu
*vcpu
)
3802 struct kvm_segment var
;
3804 /* needed to preserve selector */
3805 kvm_get_segment(vcpu
, &var
, seg
);
3807 var
.base
= get_desc_base(desc
);
3808 var
.limit
= get_desc_limit(desc
);
3810 var
.limit
= (var
.limit
<< 12) | 0xfff;
3811 var
.type
= desc
->type
;
3812 var
.present
= desc
->p
;
3813 var
.dpl
= desc
->dpl
;
3818 var
.avl
= desc
->avl
;
3819 var
.present
= desc
->p
;
3820 var
.unusable
= !var
.present
;
3823 kvm_set_segment(vcpu
, &var
, seg
);
3827 static u16
emulator_get_segment_selector(int seg
, struct kvm_vcpu
*vcpu
)
3829 struct kvm_segment kvm_seg
;
3831 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3832 return kvm_seg
.selector
;
3835 static void emulator_set_segment_selector(u16 sel
, int seg
,
3836 struct kvm_vcpu
*vcpu
)
3838 struct kvm_segment kvm_seg
;
3840 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3841 kvm_seg
.selector
= sel
;
3842 kvm_set_segment(vcpu
, &kvm_seg
, seg
);
3845 static struct x86_emulate_ops emulate_ops
= {
3846 .read_std
= kvm_read_guest_virt_system
,
3847 .write_std
= kvm_write_guest_virt_system
,
3848 .fetch
= kvm_fetch_guest_virt
,
3849 .read_emulated
= emulator_read_emulated
,
3850 .write_emulated
= emulator_write_emulated
,
3851 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
3852 .pio_in_emulated
= emulator_pio_in_emulated
,
3853 .pio_out_emulated
= emulator_pio_out_emulated
,
3854 .get_cached_descriptor
= emulator_get_cached_descriptor
,
3855 .set_cached_descriptor
= emulator_set_cached_descriptor
,
3856 .get_segment_selector
= emulator_get_segment_selector
,
3857 .set_segment_selector
= emulator_set_segment_selector
,
3858 .get_cached_segment_base
= emulator_get_cached_segment_base
,
3859 .get_gdt
= emulator_get_gdt
,
3860 .get_cr
= emulator_get_cr
,
3861 .set_cr
= emulator_set_cr
,
3862 .cpl
= emulator_get_cpl
,
3863 .get_dr
= emulator_get_dr
,
3864 .set_dr
= emulator_set_dr
,
3865 .set_msr
= kvm_set_msr
,
3866 .get_msr
= kvm_get_msr
,
3869 static void cache_all_regs(struct kvm_vcpu
*vcpu
)
3871 kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3872 kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3873 kvm_register_read(vcpu
, VCPU_REGS_RIP
);
3874 vcpu
->arch
.regs_dirty
= ~0;
3877 static void toggle_interruptibility(struct kvm_vcpu
*vcpu
, u32 mask
)
3879 u32 int_shadow
= kvm_x86_ops
->get_interrupt_shadow(vcpu
, mask
);
3881 * an sti; sti; sequence only disable interrupts for the first
3882 * instruction. So, if the last instruction, be it emulated or
3883 * not, left the system with the INT_STI flag enabled, it
3884 * means that the last instruction is an sti. We should not
3885 * leave the flag on in this case. The same goes for mov ss
3887 if (!(int_shadow
& mask
))
3888 kvm_x86_ops
->set_interrupt_shadow(vcpu
, mask
);
3891 static void inject_emulated_exception(struct kvm_vcpu
*vcpu
)
3893 struct x86_emulate_ctxt
*ctxt
= &vcpu
->arch
.emulate_ctxt
;
3894 if (ctxt
->exception
== PF_VECTOR
)
3895 kvm_inject_page_fault(vcpu
, ctxt
->cr2
, ctxt
->error_code
);
3896 else if (ctxt
->error_code_valid
)
3897 kvm_queue_exception_e(vcpu
, ctxt
->exception
, ctxt
->error_code
);
3899 kvm_queue_exception(vcpu
, ctxt
->exception
);
3902 static int handle_emulation_failure(struct kvm_vcpu
*vcpu
)
3904 ++vcpu
->stat
.insn_emulation_fail
;
3905 trace_kvm_emulate_insn_failed(vcpu
);
3906 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
3907 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
3908 vcpu
->run
->internal
.ndata
= 0;
3909 kvm_queue_exception(vcpu
, UD_VECTOR
);
3910 return EMULATE_FAIL
;
3913 int emulate_instruction(struct kvm_vcpu
*vcpu
,
3919 struct decode_cache
*c
= &vcpu
->arch
.emulate_ctxt
.decode
;
3921 kvm_clear_exception_queue(vcpu
);
3922 vcpu
->arch
.mmio_fault_cr2
= cr2
;
3924 * TODO: fix emulate.c to use guest_read/write_register
3925 * instead of direct ->regs accesses, can save hundred cycles
3926 * on Intel for instructions that don't read/change RSP, for
3929 cache_all_regs(vcpu
);
3931 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
3933 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
3935 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
3936 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3937 vcpu
->arch
.emulate_ctxt
.eip
= kvm_rip_read(vcpu
);
3938 vcpu
->arch
.emulate_ctxt
.mode
=
3939 (!is_protmode(vcpu
)) ? X86EMUL_MODE_REAL
:
3940 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
3941 ? X86EMUL_MODE_VM86
: cs_l
3942 ? X86EMUL_MODE_PROT64
: cs_db
3943 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
3944 memset(c
, 0, sizeof(struct decode_cache
));
3945 memcpy(c
->regs
, vcpu
->arch
.regs
, sizeof c
->regs
);
3946 vcpu
->arch
.emulate_ctxt
.interruptibility
= 0;
3947 vcpu
->arch
.emulate_ctxt
.exception
= -1;
3949 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
3950 trace_kvm_emulate_insn_start(vcpu
);
3952 /* Only allow emulation of specific instructions on #UD
3953 * (namely VMMCALL, sysenter, sysexit, syscall)*/
3954 if (emulation_type
& EMULTYPE_TRAP_UD
) {
3956 return EMULATE_FAIL
;
3958 case 0x01: /* VMMCALL */
3959 if (c
->modrm_mod
!= 3 || c
->modrm_rm
!= 1)
3960 return EMULATE_FAIL
;
3962 case 0x34: /* sysenter */
3963 case 0x35: /* sysexit */
3964 if (c
->modrm_mod
!= 0 || c
->modrm_rm
!= 0)
3965 return EMULATE_FAIL
;
3967 case 0x05: /* syscall */
3968 if (c
->modrm_mod
!= 0 || c
->modrm_rm
!= 0)
3969 return EMULATE_FAIL
;
3972 return EMULATE_FAIL
;
3975 if (!(c
->modrm_reg
== 0 || c
->modrm_reg
== 3))
3976 return EMULATE_FAIL
;
3979 ++vcpu
->stat
.insn_emulation
;
3981 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
3982 return EMULATE_DONE
;
3983 if (emulation_type
& EMULTYPE_SKIP
)
3984 return EMULATE_FAIL
;
3985 return handle_emulation_failure(vcpu
);
3989 if (emulation_type
& EMULTYPE_SKIP
) {
3990 kvm_rip_write(vcpu
, vcpu
->arch
.emulate_ctxt
.decode
.eip
);
3991 return EMULATE_DONE
;
3994 /* this is needed for vmware backdor interface to work since it
3995 changes registers values during IO operation */
3996 memcpy(c
->regs
, vcpu
->arch
.regs
, sizeof c
->regs
);
3999 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
4001 if (r
) { /* emulation failed */
4003 * if emulation was due to access to shadowed page table
4004 * and it failed try to unshadow page and re-entetr the
4005 * guest to let CPU execute the instruction.
4007 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
4008 return EMULATE_DONE
;
4010 return handle_emulation_failure(vcpu
);
4013 toggle_interruptibility(vcpu
, vcpu
->arch
.emulate_ctxt
.interruptibility
);
4014 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
4015 memcpy(vcpu
->arch
.regs
, c
->regs
, sizeof c
->regs
);
4016 kvm_rip_write(vcpu
, vcpu
->arch
.emulate_ctxt
.eip
);
4018 if (vcpu
->arch
.emulate_ctxt
.exception
>= 0) {
4019 inject_emulated_exception(vcpu
);
4020 return EMULATE_DONE
;
4023 if (vcpu
->arch
.pio
.count
) {
4024 if (!vcpu
->arch
.pio
.in
)
4025 vcpu
->arch
.pio
.count
= 0;
4026 return EMULATE_DO_MMIO
;
4029 if (vcpu
->mmio_needed
) {
4030 if (vcpu
->mmio_is_write
)
4031 vcpu
->mmio_needed
= 0;
4032 return EMULATE_DO_MMIO
;
4035 if (vcpu
->arch
.emulate_ctxt
.restart
)
4038 return EMULATE_DONE
;
4040 EXPORT_SYMBOL_GPL(emulate_instruction
);
4042 int kvm_fast_pio_out(struct kvm_vcpu
*vcpu
, int size
, unsigned short port
)
4044 unsigned long val
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4045 int ret
= emulator_pio_out_emulated(size
, port
, &val
, 1, vcpu
);
4046 /* do not return to emulator after return from userspace */
4047 vcpu
->arch
.pio
.count
= 0;
4050 EXPORT_SYMBOL_GPL(kvm_fast_pio_out
);
4052 static void bounce_off(void *info
)
4057 static int kvmclock_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
4060 struct cpufreq_freqs
*freq
= data
;
4062 struct kvm_vcpu
*vcpu
;
4063 int i
, send_ipi
= 0;
4065 if (val
== CPUFREQ_PRECHANGE
&& freq
->old
> freq
->new)
4067 if (val
== CPUFREQ_POSTCHANGE
&& freq
->old
< freq
->new)
4069 per_cpu(cpu_tsc_khz
, freq
->cpu
) = freq
->new;
4071 spin_lock(&kvm_lock
);
4072 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
4073 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
4074 if (vcpu
->cpu
!= freq
->cpu
)
4076 if (!kvm_request_guest_time_update(vcpu
))
4078 if (vcpu
->cpu
!= smp_processor_id())
4082 spin_unlock(&kvm_lock
);
4084 if (freq
->old
< freq
->new && send_ipi
) {
4086 * We upscale the frequency. Must make the guest
4087 * doesn't see old kvmclock values while running with
4088 * the new frequency, otherwise we risk the guest sees
4089 * time go backwards.
4091 * In case we update the frequency for another cpu
4092 * (which might be in guest context) send an interrupt
4093 * to kick the cpu out of guest context. Next time
4094 * guest context is entered kvmclock will be updated,
4095 * so the guest will not see stale values.
4097 smp_call_function_single(freq
->cpu
, bounce_off
, NULL
, 1);
4102 static struct notifier_block kvmclock_cpufreq_notifier_block
= {
4103 .notifier_call
= kvmclock_cpufreq_notifier
4106 static void kvm_timer_init(void)
4110 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
)) {
4111 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block
,
4112 CPUFREQ_TRANSITION_NOTIFIER
);
4113 for_each_online_cpu(cpu
) {
4114 unsigned long khz
= cpufreq_get(cpu
);
4117 per_cpu(cpu_tsc_khz
, cpu
) = khz
;
4120 for_each_possible_cpu(cpu
)
4121 per_cpu(cpu_tsc_khz
, cpu
) = tsc_khz
;
4125 static DEFINE_PER_CPU(struct kvm_vcpu
*, current_vcpu
);
4127 static int kvm_is_in_guest(void)
4129 return percpu_read(current_vcpu
) != NULL
;
4132 static int kvm_is_user_mode(void)
4136 if (percpu_read(current_vcpu
))
4137 user_mode
= kvm_x86_ops
->get_cpl(percpu_read(current_vcpu
));
4139 return user_mode
!= 0;
4142 static unsigned long kvm_get_guest_ip(void)
4144 unsigned long ip
= 0;
4146 if (percpu_read(current_vcpu
))
4147 ip
= kvm_rip_read(percpu_read(current_vcpu
));
4152 static struct perf_guest_info_callbacks kvm_guest_cbs
= {
4153 .is_in_guest
= kvm_is_in_guest
,
4154 .is_user_mode
= kvm_is_user_mode
,
4155 .get_guest_ip
= kvm_get_guest_ip
,
4158 void kvm_before_handle_nmi(struct kvm_vcpu
*vcpu
)
4160 percpu_write(current_vcpu
, vcpu
);
4162 EXPORT_SYMBOL_GPL(kvm_before_handle_nmi
);
4164 void kvm_after_handle_nmi(struct kvm_vcpu
*vcpu
)
4166 percpu_write(current_vcpu
, NULL
);
4168 EXPORT_SYMBOL_GPL(kvm_after_handle_nmi
);
4170 int kvm_arch_init(void *opaque
)
4173 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
4176 printk(KERN_ERR
"kvm: already loaded the other module\n");
4181 if (!ops
->cpu_has_kvm_support()) {
4182 printk(KERN_ERR
"kvm: no hardware support\n");
4186 if (ops
->disabled_by_bios()) {
4187 printk(KERN_ERR
"kvm: disabled by bios\n");
4192 r
= kvm_mmu_module_init();
4196 kvm_init_msr_list();
4199 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
4200 kvm_mmu_set_base_ptes(PT_PRESENT_MASK
);
4201 kvm_mmu_set_mask_ptes(PT_USER_MASK
, PT_ACCESSED_MASK
,
4202 PT_DIRTY_MASK
, PT64_NX_MASK
, 0);
4206 perf_register_guest_info_callbacks(&kvm_guest_cbs
);
4209 host_xcr0
= xgetbv(XCR_XFEATURE_ENABLED_MASK
);
4217 void kvm_arch_exit(void)
4219 perf_unregister_guest_info_callbacks(&kvm_guest_cbs
);
4221 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
))
4222 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block
,
4223 CPUFREQ_TRANSITION_NOTIFIER
);
4225 kvm_mmu_module_exit();
4228 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
4230 ++vcpu
->stat
.halt_exits
;
4231 if (irqchip_in_kernel(vcpu
->kvm
)) {
4232 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
4235 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
4239 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
4241 static inline gpa_t
hc_gpa(struct kvm_vcpu
*vcpu
, unsigned long a0
,
4244 if (is_long_mode(vcpu
))
4247 return a0
| ((gpa_t
)a1
<< 32);
4250 int kvm_hv_hypercall(struct kvm_vcpu
*vcpu
)
4252 u64 param
, ingpa
, outgpa
, ret
;
4253 uint16_t code
, rep_idx
, rep_cnt
, res
= HV_STATUS_SUCCESS
, rep_done
= 0;
4254 bool fast
, longmode
;
4258 * hypercall generates UD from non zero cpl and real mode
4261 if (kvm_x86_ops
->get_cpl(vcpu
) != 0 || !is_protmode(vcpu
)) {
4262 kvm_queue_exception(vcpu
, UD_VECTOR
);
4266 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
4267 longmode
= is_long_mode(vcpu
) && cs_l
== 1;
4270 param
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RDX
) << 32) |
4271 (kvm_register_read(vcpu
, VCPU_REGS_RAX
) & 0xffffffff);
4272 ingpa
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RBX
) << 32) |
4273 (kvm_register_read(vcpu
, VCPU_REGS_RCX
) & 0xffffffff);
4274 outgpa
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RDI
) << 32) |
4275 (kvm_register_read(vcpu
, VCPU_REGS_RSI
) & 0xffffffff);
4277 #ifdef CONFIG_X86_64
4279 param
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4280 ingpa
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
4281 outgpa
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
4285 code
= param
& 0xffff;
4286 fast
= (param
>> 16) & 0x1;
4287 rep_cnt
= (param
>> 32) & 0xfff;
4288 rep_idx
= (param
>> 48) & 0xfff;
4290 trace_kvm_hv_hypercall(code
, fast
, rep_cnt
, rep_idx
, ingpa
, outgpa
);
4293 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT
:
4294 kvm_vcpu_on_spin(vcpu
);
4297 res
= HV_STATUS_INVALID_HYPERCALL_CODE
;
4301 ret
= res
| (((u64
)rep_done
& 0xfff) << 32);
4303 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
);
4305 kvm_register_write(vcpu
, VCPU_REGS_RDX
, ret
>> 32);
4306 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
& 0xffffffff);
4312 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
4314 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
4317 if (kvm_hv_hypercall_enabled(vcpu
->kvm
))
4318 return kvm_hv_hypercall(vcpu
);
4320 nr
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4321 a0
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
4322 a1
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4323 a2
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
4324 a3
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
4326 trace_kvm_hypercall(nr
, a0
, a1
, a2
, a3
);
4328 if (!is_long_mode(vcpu
)) {
4336 if (kvm_x86_ops
->get_cpl(vcpu
) != 0) {
4342 case KVM_HC_VAPIC_POLL_IRQ
:
4346 r
= kvm_pv_mmu_op(vcpu
, a0
, hc_gpa(vcpu
, a1
, a2
), &ret
);
4353 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
);
4354 ++vcpu
->stat
.hypercalls
;
4357 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
4359 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
4361 char instruction
[3];
4362 unsigned long rip
= kvm_rip_read(vcpu
);
4365 * Blow out the MMU to ensure that no other VCPU has an active mapping
4366 * to ensure that the updated hypercall appears atomically across all
4369 kvm_mmu_zap_all(vcpu
->kvm
);
4371 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
4373 return emulator_write_emulated(rip
, instruction
, 3, NULL
, vcpu
);
4376 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
4378 struct desc_ptr dt
= { limit
, base
};
4380 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
4383 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
4385 struct desc_ptr dt
= { limit
, base
};
4387 kvm_x86_ops
->set_idt(vcpu
, &dt
);
4390 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
4392 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
4393 int j
, nent
= vcpu
->arch
.cpuid_nent
;
4395 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
4396 /* when no next entry is found, the current entry[i] is reselected */
4397 for (j
= i
+ 1; ; j
= (j
+ 1) % nent
) {
4398 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
4399 if (ej
->function
== e
->function
) {
4400 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
4404 return 0; /* silence gcc, even though control never reaches here */
4407 /* find an entry with matching function, matching index (if needed), and that
4408 * should be read next (if it's stateful) */
4409 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
4410 u32 function
, u32 index
)
4412 if (e
->function
!= function
)
4414 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
4416 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
4417 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
4422 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
4423 u32 function
, u32 index
)
4426 struct kvm_cpuid_entry2
*best
= NULL
;
4428 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
4429 struct kvm_cpuid_entry2
*e
;
4431 e
= &vcpu
->arch
.cpuid_entries
[i
];
4432 if (is_matching_cpuid_entry(e
, function
, index
)) {
4433 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
4434 move_to_next_stateful_cpuid_entry(vcpu
, i
);
4439 * Both basic or both extended?
4441 if (((e
->function
^ function
) & 0x80000000) == 0)
4442 if (!best
|| e
->function
> best
->function
)
4447 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry
);
4449 int cpuid_maxphyaddr(struct kvm_vcpu
*vcpu
)
4451 struct kvm_cpuid_entry2
*best
;
4453 best
= kvm_find_cpuid_entry(vcpu
, 0x80000000, 0);
4454 if (!best
|| best
->eax
< 0x80000008)
4456 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
4458 return best
->eax
& 0xff;
4463 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
4465 u32 function
, index
;
4466 struct kvm_cpuid_entry2
*best
;
4468 function
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4469 index
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4470 kvm_register_write(vcpu
, VCPU_REGS_RAX
, 0);
4471 kvm_register_write(vcpu
, VCPU_REGS_RBX
, 0);
4472 kvm_register_write(vcpu
, VCPU_REGS_RCX
, 0);
4473 kvm_register_write(vcpu
, VCPU_REGS_RDX
, 0);
4474 best
= kvm_find_cpuid_entry(vcpu
, function
, index
);
4476 kvm_register_write(vcpu
, VCPU_REGS_RAX
, best
->eax
);
4477 kvm_register_write(vcpu
, VCPU_REGS_RBX
, best
->ebx
);
4478 kvm_register_write(vcpu
, VCPU_REGS_RCX
, best
->ecx
);
4479 kvm_register_write(vcpu
, VCPU_REGS_RDX
, best
->edx
);
4481 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
4482 trace_kvm_cpuid(function
,
4483 kvm_register_read(vcpu
, VCPU_REGS_RAX
),
4484 kvm_register_read(vcpu
, VCPU_REGS_RBX
),
4485 kvm_register_read(vcpu
, VCPU_REGS_RCX
),
4486 kvm_register_read(vcpu
, VCPU_REGS_RDX
));
4488 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
4491 * Check if userspace requested an interrupt window, and that the
4492 * interrupt window is open.
4494 * No need to exit to userspace if we already have an interrupt queued.
4496 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
)
4498 return (!irqchip_in_kernel(vcpu
->kvm
) && !kvm_cpu_has_interrupt(vcpu
) &&
4499 vcpu
->run
->request_interrupt_window
&&
4500 kvm_arch_interrupt_allowed(vcpu
));
4503 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
)
4505 struct kvm_run
*kvm_run
= vcpu
->run
;
4507 kvm_run
->if_flag
= (kvm_get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
4508 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
4509 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
4510 if (irqchip_in_kernel(vcpu
->kvm
))
4511 kvm_run
->ready_for_interrupt_injection
= 1;
4513 kvm_run
->ready_for_interrupt_injection
=
4514 kvm_arch_interrupt_allowed(vcpu
) &&
4515 !kvm_cpu_has_interrupt(vcpu
) &&
4516 !kvm_event_needs_reinjection(vcpu
);
4519 static void vapic_enter(struct kvm_vcpu
*vcpu
)
4521 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
4524 if (!apic
|| !apic
->vapic_addr
)
4527 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
4529 vcpu
->arch
.apic
->vapic_page
= page
;
4532 static void vapic_exit(struct kvm_vcpu
*vcpu
)
4534 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
4537 if (!apic
|| !apic
->vapic_addr
)
4540 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4541 kvm_release_page_dirty(apic
->vapic_page
);
4542 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
4543 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
4546 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
)
4550 if (!kvm_x86_ops
->update_cr8_intercept
)
4553 if (!vcpu
->arch
.apic
)
4556 if (!vcpu
->arch
.apic
->vapic_addr
)
4557 max_irr
= kvm_lapic_find_highest_irr(vcpu
);
4564 tpr
= kvm_lapic_get_cr8(vcpu
);
4566 kvm_x86_ops
->update_cr8_intercept(vcpu
, tpr
, max_irr
);
4569 static void inject_pending_event(struct kvm_vcpu
*vcpu
)
4571 /* try to reinject previous events if any */
4572 if (vcpu
->arch
.exception
.pending
) {
4573 trace_kvm_inj_exception(vcpu
->arch
.exception
.nr
,
4574 vcpu
->arch
.exception
.has_error_code
,
4575 vcpu
->arch
.exception
.error_code
);
4576 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
4577 vcpu
->arch
.exception
.has_error_code
,
4578 vcpu
->arch
.exception
.error_code
,
4579 vcpu
->arch
.exception
.reinject
);
4583 if (vcpu
->arch
.nmi_injected
) {
4584 kvm_x86_ops
->set_nmi(vcpu
);
4588 if (vcpu
->arch
.interrupt
.pending
) {
4589 kvm_x86_ops
->set_irq(vcpu
);
4593 /* try to inject new event if pending */
4594 if (vcpu
->arch
.nmi_pending
) {
4595 if (kvm_x86_ops
->nmi_allowed(vcpu
)) {
4596 vcpu
->arch
.nmi_pending
= false;
4597 vcpu
->arch
.nmi_injected
= true;
4598 kvm_x86_ops
->set_nmi(vcpu
);
4600 } else if (kvm_cpu_has_interrupt(vcpu
)) {
4601 if (kvm_x86_ops
->interrupt_allowed(vcpu
)) {
4602 kvm_queue_interrupt(vcpu
, kvm_cpu_get_interrupt(vcpu
),
4604 kvm_x86_ops
->set_irq(vcpu
);
4609 static void kvm_load_guest_xcr0(struct kvm_vcpu
*vcpu
)
4611 if (kvm_read_cr4_bits(vcpu
, X86_CR4_OSXSAVE
) &&
4612 !vcpu
->guest_xcr0_loaded
) {
4613 /* kvm_set_xcr() also depends on this */
4614 xsetbv(XCR_XFEATURE_ENABLED_MASK
, vcpu
->arch
.xcr0
);
4615 vcpu
->guest_xcr0_loaded
= 1;
4619 static void kvm_put_guest_xcr0(struct kvm_vcpu
*vcpu
)
4621 if (vcpu
->guest_xcr0_loaded
) {
4622 if (vcpu
->arch
.xcr0
!= host_xcr0
)
4623 xsetbv(XCR_XFEATURE_ENABLED_MASK
, host_xcr0
);
4624 vcpu
->guest_xcr0_loaded
= 0;
4628 static int vcpu_enter_guest(struct kvm_vcpu
*vcpu
)
4631 bool req_int_win
= !irqchip_in_kernel(vcpu
->kvm
) &&
4632 vcpu
->run
->request_interrupt_window
;
4635 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
4636 kvm_mmu_unload(vcpu
);
4638 r
= kvm_mmu_reload(vcpu
);
4642 if (vcpu
->requests
) {
4643 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
4644 __kvm_migrate_timers(vcpu
);
4645 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE
, &vcpu
->requests
))
4646 kvm_write_guest_time(vcpu
);
4647 if (test_and_clear_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
))
4648 kvm_mmu_sync_roots(vcpu
);
4649 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
4650 kvm_x86_ops
->tlb_flush(vcpu
);
4651 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
4653 vcpu
->run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
4657 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
4658 vcpu
->run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
4662 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU
, &vcpu
->requests
)) {
4663 vcpu
->fpu_active
= 0;
4664 kvm_x86_ops
->fpu_deactivate(vcpu
);
4670 kvm_x86_ops
->prepare_guest_switch(vcpu
);
4671 if (vcpu
->fpu_active
)
4672 kvm_load_guest_fpu(vcpu
);
4673 kvm_load_guest_xcr0(vcpu
);
4675 atomic_set(&vcpu
->guest_mode
, 1);
4678 local_irq_disable();
4680 if (!atomic_read(&vcpu
->guest_mode
) || vcpu
->requests
4681 || need_resched() || signal_pending(current
)) {
4682 atomic_set(&vcpu
->guest_mode
, 0);
4690 inject_pending_event(vcpu
);
4692 /* enable NMI/IRQ window open exits if needed */
4693 if (vcpu
->arch
.nmi_pending
)
4694 kvm_x86_ops
->enable_nmi_window(vcpu
);
4695 else if (kvm_cpu_has_interrupt(vcpu
) || req_int_win
)
4696 kvm_x86_ops
->enable_irq_window(vcpu
);
4698 if (kvm_lapic_enabled(vcpu
)) {
4699 update_cr8_intercept(vcpu
);
4700 kvm_lapic_sync_to_vapic(vcpu
);
4703 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
4707 if (unlikely(vcpu
->arch
.switch_db_regs
)) {
4709 set_debugreg(vcpu
->arch
.eff_db
[0], 0);
4710 set_debugreg(vcpu
->arch
.eff_db
[1], 1);
4711 set_debugreg(vcpu
->arch
.eff_db
[2], 2);
4712 set_debugreg(vcpu
->arch
.eff_db
[3], 3);
4715 trace_kvm_entry(vcpu
->vcpu_id
);
4716 kvm_x86_ops
->run(vcpu
);
4719 * If the guest has used debug registers, at least dr7
4720 * will be disabled while returning to the host.
4721 * If we don't have active breakpoints in the host, we don't
4722 * care about the messed up debug address registers. But if
4723 * we have some of them active, restore the old state.
4725 if (hw_breakpoint_active())
4726 hw_breakpoint_restore();
4728 atomic_set(&vcpu
->guest_mode
, 0);
4735 * We must have an instruction between local_irq_enable() and
4736 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4737 * the interrupt shadow. The stat.exits increment will do nicely.
4738 * But we need to prevent reordering, hence this barrier():
4746 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4749 * Profile KVM exit RIPs:
4751 if (unlikely(prof_on
== KVM_PROFILING
)) {
4752 unsigned long rip
= kvm_rip_read(vcpu
);
4753 profile_hit(KVM_PROFILING
, (void *)rip
);
4757 kvm_lapic_sync_from_vapic(vcpu
);
4759 r
= kvm_x86_ops
->handle_exit(vcpu
);
4765 static int __vcpu_run(struct kvm_vcpu
*vcpu
)
4768 struct kvm
*kvm
= vcpu
->kvm
;
4770 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
)) {
4771 pr_debug("vcpu %d received sipi with vector # %x\n",
4772 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
4773 kvm_lapic_reset(vcpu
);
4774 r
= kvm_arch_vcpu_reset(vcpu
);
4777 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4780 vcpu
->srcu_idx
= srcu_read_lock(&kvm
->srcu
);
4785 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
)
4786 r
= vcpu_enter_guest(vcpu
);
4788 srcu_read_unlock(&kvm
->srcu
, vcpu
->srcu_idx
);
4789 kvm_vcpu_block(vcpu
);
4790 vcpu
->srcu_idx
= srcu_read_lock(&kvm
->srcu
);
4791 if (test_and_clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
))
4793 switch(vcpu
->arch
.mp_state
) {
4794 case KVM_MP_STATE_HALTED
:
4795 vcpu
->arch
.mp_state
=
4796 KVM_MP_STATE_RUNNABLE
;
4797 case KVM_MP_STATE_RUNNABLE
:
4799 case KVM_MP_STATE_SIPI_RECEIVED
:
4810 clear_bit(KVM_REQ_PENDING_TIMER
, &vcpu
->requests
);
4811 if (kvm_cpu_has_pending_timer(vcpu
))
4812 kvm_inject_pending_timer_irqs(vcpu
);
4814 if (dm_request_for_irq_injection(vcpu
)) {
4816 vcpu
->run
->exit_reason
= KVM_EXIT_INTR
;
4817 ++vcpu
->stat
.request_irq_exits
;
4819 if (signal_pending(current
)) {
4821 vcpu
->run
->exit_reason
= KVM_EXIT_INTR
;
4822 ++vcpu
->stat
.signal_exits
;
4824 if (need_resched()) {
4825 srcu_read_unlock(&kvm
->srcu
, vcpu
->srcu_idx
);
4827 vcpu
->srcu_idx
= srcu_read_lock(&kvm
->srcu
);
4831 srcu_read_unlock(&kvm
->srcu
, vcpu
->srcu_idx
);
4838 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
4843 if (vcpu
->sigset_active
)
4844 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
4846 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
4847 kvm_vcpu_block(vcpu
);
4848 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
4853 /* re-sync apic's tpr */
4854 if (!irqchip_in_kernel(vcpu
->kvm
))
4855 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
4857 if (vcpu
->arch
.pio
.count
|| vcpu
->mmio_needed
||
4858 vcpu
->arch
.emulate_ctxt
.restart
) {
4859 if (vcpu
->mmio_needed
) {
4860 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
4861 vcpu
->mmio_read_completed
= 1;
4862 vcpu
->mmio_needed
= 0;
4864 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4865 r
= emulate_instruction(vcpu
, 0, 0, EMULTYPE_NO_DECODE
);
4866 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
4867 if (r
!= EMULATE_DONE
) {
4872 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
)
4873 kvm_register_write(vcpu
, VCPU_REGS_RAX
,
4874 kvm_run
->hypercall
.ret
);
4876 r
= __vcpu_run(vcpu
);
4879 post_kvm_run_save(vcpu
);
4880 if (vcpu
->sigset_active
)
4881 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
4886 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
4888 regs
->rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
4889 regs
->rbx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
4890 regs
->rcx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
4891 regs
->rdx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
4892 regs
->rsi
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
4893 regs
->rdi
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
4894 regs
->rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
4895 regs
->rbp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
4896 #ifdef CONFIG_X86_64
4897 regs
->r8
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
4898 regs
->r9
= kvm_register_read(vcpu
, VCPU_REGS_R9
);
4899 regs
->r10
= kvm_register_read(vcpu
, VCPU_REGS_R10
);
4900 regs
->r11
= kvm_register_read(vcpu
, VCPU_REGS_R11
);
4901 regs
->r12
= kvm_register_read(vcpu
, VCPU_REGS_R12
);
4902 regs
->r13
= kvm_register_read(vcpu
, VCPU_REGS_R13
);
4903 regs
->r14
= kvm_register_read(vcpu
, VCPU_REGS_R14
);
4904 regs
->r15
= kvm_register_read(vcpu
, VCPU_REGS_R15
);
4907 regs
->rip
= kvm_rip_read(vcpu
);
4908 regs
->rflags
= kvm_get_rflags(vcpu
);
4913 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
4915 kvm_register_write(vcpu
, VCPU_REGS_RAX
, regs
->rax
);
4916 kvm_register_write(vcpu
, VCPU_REGS_RBX
, regs
->rbx
);
4917 kvm_register_write(vcpu
, VCPU_REGS_RCX
, regs
->rcx
);
4918 kvm_register_write(vcpu
, VCPU_REGS_RDX
, regs
->rdx
);
4919 kvm_register_write(vcpu
, VCPU_REGS_RSI
, regs
->rsi
);
4920 kvm_register_write(vcpu
, VCPU_REGS_RDI
, regs
->rdi
);
4921 kvm_register_write(vcpu
, VCPU_REGS_RSP
, regs
->rsp
);
4922 kvm_register_write(vcpu
, VCPU_REGS_RBP
, regs
->rbp
);
4923 #ifdef CONFIG_X86_64
4924 kvm_register_write(vcpu
, VCPU_REGS_R8
, regs
->r8
);
4925 kvm_register_write(vcpu
, VCPU_REGS_R9
, regs
->r9
);
4926 kvm_register_write(vcpu
, VCPU_REGS_R10
, regs
->r10
);
4927 kvm_register_write(vcpu
, VCPU_REGS_R11
, regs
->r11
);
4928 kvm_register_write(vcpu
, VCPU_REGS_R12
, regs
->r12
);
4929 kvm_register_write(vcpu
, VCPU_REGS_R13
, regs
->r13
);
4930 kvm_register_write(vcpu
, VCPU_REGS_R14
, regs
->r14
);
4931 kvm_register_write(vcpu
, VCPU_REGS_R15
, regs
->r15
);
4934 kvm_rip_write(vcpu
, regs
->rip
);
4935 kvm_set_rflags(vcpu
, regs
->rflags
);
4937 vcpu
->arch
.exception
.pending
= false;
4942 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
4944 struct kvm_segment cs
;
4946 kvm_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
4950 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
4952 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
4953 struct kvm_sregs
*sregs
)
4957 kvm_get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
4958 kvm_get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
4959 kvm_get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
4960 kvm_get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
4961 kvm_get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
4962 kvm_get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
4964 kvm_get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
4965 kvm_get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
4967 kvm_x86_ops
->get_idt(vcpu
, &dt
);
4968 sregs
->idt
.limit
= dt
.size
;
4969 sregs
->idt
.base
= dt
.address
;
4970 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
4971 sregs
->gdt
.limit
= dt
.size
;
4972 sregs
->gdt
.base
= dt
.address
;
4974 sregs
->cr0
= kvm_read_cr0(vcpu
);
4975 sregs
->cr2
= vcpu
->arch
.cr2
;
4976 sregs
->cr3
= vcpu
->arch
.cr3
;
4977 sregs
->cr4
= kvm_read_cr4(vcpu
);
4978 sregs
->cr8
= kvm_get_cr8(vcpu
);
4979 sregs
->efer
= vcpu
->arch
.efer
;
4980 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
4982 memset(sregs
->interrupt_bitmap
, 0, sizeof sregs
->interrupt_bitmap
);
4984 if (vcpu
->arch
.interrupt
.pending
&& !vcpu
->arch
.interrupt
.soft
)
4985 set_bit(vcpu
->arch
.interrupt
.nr
,
4986 (unsigned long *)sregs
->interrupt_bitmap
);
4991 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
4992 struct kvm_mp_state
*mp_state
)
4994 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
4998 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
4999 struct kvm_mp_state
*mp_state
)
5001 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
5005 int kvm_task_switch(struct kvm_vcpu
*vcpu
, u16 tss_selector
, int reason
,
5006 bool has_error_code
, u32 error_code
)
5008 struct decode_cache
*c
= &vcpu
->arch
.emulate_ctxt
.decode
;
5009 int cs_db
, cs_l
, ret
;
5010 cache_all_regs(vcpu
);
5012 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
5014 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
5015 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
5016 vcpu
->arch
.emulate_ctxt
.eip
= kvm_rip_read(vcpu
);
5017 vcpu
->arch
.emulate_ctxt
.mode
=
5018 (!is_protmode(vcpu
)) ? X86EMUL_MODE_REAL
:
5019 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
5020 ? X86EMUL_MODE_VM86
: cs_l
5021 ? X86EMUL_MODE_PROT64
: cs_db
5022 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
5023 memset(c
, 0, sizeof(struct decode_cache
));
5024 memcpy(c
->regs
, vcpu
->arch
.regs
, sizeof c
->regs
);
5026 ret
= emulator_task_switch(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
,
5027 tss_selector
, reason
, has_error_code
,
5031 return EMULATE_FAIL
;
5033 memcpy(vcpu
->arch
.regs
, c
->regs
, sizeof c
->regs
);
5034 kvm_rip_write(vcpu
, vcpu
->arch
.emulate_ctxt
.eip
);
5035 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
5036 return EMULATE_DONE
;
5038 EXPORT_SYMBOL_GPL(kvm_task_switch
);
5040 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
5041 struct kvm_sregs
*sregs
)
5043 int mmu_reset_needed
= 0;
5044 int pending_vec
, max_bits
;
5047 dt
.size
= sregs
->idt
.limit
;
5048 dt
.address
= sregs
->idt
.base
;
5049 kvm_x86_ops
->set_idt(vcpu
, &dt
);
5050 dt
.size
= sregs
->gdt
.limit
;
5051 dt
.address
= sregs
->gdt
.base
;
5052 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
5054 vcpu
->arch
.cr2
= sregs
->cr2
;
5055 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
5056 vcpu
->arch
.cr3
= sregs
->cr3
;
5058 kvm_set_cr8(vcpu
, sregs
->cr8
);
5060 mmu_reset_needed
|= vcpu
->arch
.efer
!= sregs
->efer
;
5061 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
5062 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
5064 mmu_reset_needed
|= kvm_read_cr0(vcpu
) != sregs
->cr0
;
5065 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
5066 vcpu
->arch
.cr0
= sregs
->cr0
;
5068 mmu_reset_needed
|= kvm_read_cr4(vcpu
) != sregs
->cr4
;
5069 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
5070 if (!is_long_mode(vcpu
) && is_pae(vcpu
)) {
5071 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
5072 mmu_reset_needed
= 1;
5075 if (mmu_reset_needed
)
5076 kvm_mmu_reset_context(vcpu
);
5078 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
5079 pending_vec
= find_first_bit(
5080 (const unsigned long *)sregs
->interrupt_bitmap
, max_bits
);
5081 if (pending_vec
< max_bits
) {
5082 kvm_queue_interrupt(vcpu
, pending_vec
, false);
5083 pr_debug("Set back pending irq %d\n", pending_vec
);
5084 if (irqchip_in_kernel(vcpu
->kvm
))
5085 kvm_pic_clear_isr_ack(vcpu
->kvm
);
5088 kvm_set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
5089 kvm_set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
5090 kvm_set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
5091 kvm_set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
5092 kvm_set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
5093 kvm_set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
5095 kvm_set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
5096 kvm_set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
5098 update_cr8_intercept(vcpu
);
5100 /* Older userspace won't unhalt the vcpu on reset. */
5101 if (kvm_vcpu_is_bsp(vcpu
) && kvm_rip_read(vcpu
) == 0xfff0 &&
5102 sregs
->cs
.selector
== 0xf000 && sregs
->cs
.base
== 0xffff0000 &&
5104 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
5109 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
5110 struct kvm_guest_debug
*dbg
)
5112 unsigned long rflags
;
5115 if (dbg
->control
& (KVM_GUESTDBG_INJECT_DB
| KVM_GUESTDBG_INJECT_BP
)) {
5117 if (vcpu
->arch
.exception
.pending
)
5119 if (dbg
->control
& KVM_GUESTDBG_INJECT_DB
)
5120 kvm_queue_exception(vcpu
, DB_VECTOR
);
5122 kvm_queue_exception(vcpu
, BP_VECTOR
);
5126 * Read rflags as long as potentially injected trace flags are still
5129 rflags
= kvm_get_rflags(vcpu
);
5131 vcpu
->guest_debug
= dbg
->control
;
5132 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
))
5133 vcpu
->guest_debug
= 0;
5135 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
5136 for (i
= 0; i
< KVM_NR_DB_REGS
; ++i
)
5137 vcpu
->arch
.eff_db
[i
] = dbg
->arch
.debugreg
[i
];
5138 vcpu
->arch
.switch_db_regs
=
5139 (dbg
->arch
.debugreg
[7] & DR7_BP_EN_MASK
);
5141 for (i
= 0; i
< KVM_NR_DB_REGS
; i
++)
5142 vcpu
->arch
.eff_db
[i
] = vcpu
->arch
.db
[i
];
5143 vcpu
->arch
.switch_db_regs
= (vcpu
->arch
.dr7
& DR7_BP_EN_MASK
);
5146 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
5147 vcpu
->arch
.singlestep_rip
= kvm_rip_read(vcpu
) +
5148 get_segment_base(vcpu
, VCPU_SREG_CS
);
5151 * Trigger an rflags update that will inject or remove the trace
5154 kvm_set_rflags(vcpu
, rflags
);
5156 kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
5166 * Translate a guest virtual address to a guest physical address.
5168 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
5169 struct kvm_translation
*tr
)
5171 unsigned long vaddr
= tr
->linear_address
;
5175 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
5176 gpa
= kvm_mmu_gva_to_gpa_system(vcpu
, vaddr
, NULL
);
5177 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
5178 tr
->physical_address
= gpa
;
5179 tr
->valid
= gpa
!= UNMAPPED_GVA
;
5186 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
5188 struct i387_fxsave_struct
*fxsave
=
5189 &vcpu
->arch
.guest_fpu
.state
->fxsave
;
5191 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
5192 fpu
->fcw
= fxsave
->cwd
;
5193 fpu
->fsw
= fxsave
->swd
;
5194 fpu
->ftwx
= fxsave
->twd
;
5195 fpu
->last_opcode
= fxsave
->fop
;
5196 fpu
->last_ip
= fxsave
->rip
;
5197 fpu
->last_dp
= fxsave
->rdp
;
5198 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
5203 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
5205 struct i387_fxsave_struct
*fxsave
=
5206 &vcpu
->arch
.guest_fpu
.state
->fxsave
;
5208 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
5209 fxsave
->cwd
= fpu
->fcw
;
5210 fxsave
->swd
= fpu
->fsw
;
5211 fxsave
->twd
= fpu
->ftwx
;
5212 fxsave
->fop
= fpu
->last_opcode
;
5213 fxsave
->rip
= fpu
->last_ip
;
5214 fxsave
->rdp
= fpu
->last_dp
;
5215 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
5220 int fx_init(struct kvm_vcpu
*vcpu
)
5224 err
= fpu_alloc(&vcpu
->arch
.guest_fpu
);
5228 fpu_finit(&vcpu
->arch
.guest_fpu
);
5231 * Ensure guest xcr0 is valid for loading
5233 vcpu
->arch
.xcr0
= XSTATE_FP
;
5235 vcpu
->arch
.cr0
|= X86_CR0_ET
;
5239 EXPORT_SYMBOL_GPL(fx_init
);
5241 static void fx_free(struct kvm_vcpu
*vcpu
)
5243 fpu_free(&vcpu
->arch
.guest_fpu
);
5246 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
5248 if (vcpu
->guest_fpu_loaded
)
5252 * Restore all possible states in the guest,
5253 * and assume host would use all available bits.
5254 * Guest xcr0 would be loaded later.
5256 kvm_put_guest_xcr0(vcpu
);
5257 vcpu
->guest_fpu_loaded
= 1;
5258 unlazy_fpu(current
);
5259 fpu_restore_checking(&vcpu
->arch
.guest_fpu
);
5263 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
5265 kvm_put_guest_xcr0(vcpu
);
5267 if (!vcpu
->guest_fpu_loaded
)
5270 vcpu
->guest_fpu_loaded
= 0;
5271 fpu_save_init(&vcpu
->arch
.guest_fpu
);
5272 ++vcpu
->stat
.fpu_reload
;
5273 set_bit(KVM_REQ_DEACTIVATE_FPU
, &vcpu
->requests
);
5277 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
5279 if (vcpu
->arch
.time_page
) {
5280 kvm_release_page_dirty(vcpu
->arch
.time_page
);
5281 vcpu
->arch
.time_page
= NULL
;
5285 kvm_x86_ops
->vcpu_free(vcpu
);
5288 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
5291 return kvm_x86_ops
->vcpu_create(kvm
, id
);
5294 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
5298 vcpu
->arch
.mtrr_state
.have_fixed
= 1;
5300 r
= kvm_arch_vcpu_reset(vcpu
);
5302 r
= kvm_mmu_setup(vcpu
);
5309 kvm_x86_ops
->vcpu_free(vcpu
);
5313 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
5316 kvm_mmu_unload(vcpu
);
5320 kvm_x86_ops
->vcpu_free(vcpu
);
5323 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
5325 vcpu
->arch
.nmi_pending
= false;
5326 vcpu
->arch
.nmi_injected
= false;
5328 vcpu
->arch
.switch_db_regs
= 0;
5329 memset(vcpu
->arch
.db
, 0, sizeof(vcpu
->arch
.db
));
5330 vcpu
->arch
.dr6
= DR6_FIXED_1
;
5331 vcpu
->arch
.dr7
= DR7_FIXED_1
;
5333 return kvm_x86_ops
->vcpu_reset(vcpu
);
5336 int kvm_arch_hardware_enable(void *garbage
)
5339 * Since this may be called from a hotplug notifcation,
5340 * we can't get the CPU frequency directly.
5342 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
)) {
5343 int cpu
= raw_smp_processor_id();
5344 per_cpu(cpu_tsc_khz
, cpu
) = 0;
5347 kvm_shared_msr_cpu_online();
5349 return kvm_x86_ops
->hardware_enable(garbage
);
5352 void kvm_arch_hardware_disable(void *garbage
)
5354 kvm_x86_ops
->hardware_disable(garbage
);
5355 drop_user_return_notifiers(garbage
);
5358 int kvm_arch_hardware_setup(void)
5360 return kvm_x86_ops
->hardware_setup();
5363 void kvm_arch_hardware_unsetup(void)
5365 kvm_x86_ops
->hardware_unsetup();
5368 void kvm_arch_check_processor_compat(void *rtn
)
5370 kvm_x86_ops
->check_processor_compatibility(rtn
);
5373 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
5379 BUG_ON(vcpu
->kvm
== NULL
);
5382 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
5383 if (!irqchip_in_kernel(kvm
) || kvm_vcpu_is_bsp(vcpu
))
5384 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
5386 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
5388 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
5393 vcpu
->arch
.pio_data
= page_address(page
);
5395 r
= kvm_mmu_create(vcpu
);
5397 goto fail_free_pio_data
;
5399 if (irqchip_in_kernel(kvm
)) {
5400 r
= kvm_create_lapic(vcpu
);
5402 goto fail_mmu_destroy
;
5405 vcpu
->arch
.mce_banks
= kzalloc(KVM_MAX_MCE_BANKS
* sizeof(u64
) * 4,
5407 if (!vcpu
->arch
.mce_banks
) {
5409 goto fail_free_lapic
;
5411 vcpu
->arch
.mcg_cap
= KVM_MAX_MCE_BANKS
;
5415 kvm_free_lapic(vcpu
);
5417 kvm_mmu_destroy(vcpu
);
5419 free_page((unsigned long)vcpu
->arch
.pio_data
);
5424 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
5428 kfree(vcpu
->arch
.mce_banks
);
5429 kvm_free_lapic(vcpu
);
5430 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
5431 kvm_mmu_destroy(vcpu
);
5432 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
5433 free_page((unsigned long)vcpu
->arch
.pio_data
);
5436 struct kvm
*kvm_arch_create_vm(void)
5438 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
5441 return ERR_PTR(-ENOMEM
);
5443 kvm
->arch
.aliases
= kzalloc(sizeof(struct kvm_mem_aliases
), GFP_KERNEL
);
5444 if (!kvm
->arch
.aliases
) {
5446 return ERR_PTR(-ENOMEM
);
5449 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
5450 INIT_LIST_HEAD(&kvm
->arch
.assigned_dev_head
);
5452 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5453 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID
, &kvm
->arch
.irq_sources_bitmap
);
5455 rdtscll(kvm
->arch
.vm_init_tsc
);
5460 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
5463 kvm_mmu_unload(vcpu
);
5467 static void kvm_free_vcpus(struct kvm
*kvm
)
5470 struct kvm_vcpu
*vcpu
;
5473 * Unpin any mmu pages first.
5475 kvm_for_each_vcpu(i
, vcpu
, kvm
)
5476 kvm_unload_vcpu_mmu(vcpu
);
5477 kvm_for_each_vcpu(i
, vcpu
, kvm
)
5478 kvm_arch_vcpu_free(vcpu
);
5480 mutex_lock(&kvm
->lock
);
5481 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
5482 kvm
->vcpus
[i
] = NULL
;
5484 atomic_set(&kvm
->online_vcpus
, 0);
5485 mutex_unlock(&kvm
->lock
);
5488 void kvm_arch_sync_events(struct kvm
*kvm
)
5490 kvm_free_all_assigned_devices(kvm
);
5493 void kvm_arch_destroy_vm(struct kvm
*kvm
)
5495 kvm_iommu_unmap_guest(kvm
);
5497 kfree(kvm
->arch
.vpic
);
5498 kfree(kvm
->arch
.vioapic
);
5499 kvm_free_vcpus(kvm
);
5500 kvm_free_physmem(kvm
);
5501 if (kvm
->arch
.apic_access_page
)
5502 put_page(kvm
->arch
.apic_access_page
);
5503 if (kvm
->arch
.ept_identity_pagetable
)
5504 put_page(kvm
->arch
.ept_identity_pagetable
);
5505 cleanup_srcu_struct(&kvm
->srcu
);
5506 kfree(kvm
->arch
.aliases
);
5510 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
5511 struct kvm_memory_slot
*memslot
,
5512 struct kvm_memory_slot old
,
5513 struct kvm_userspace_memory_region
*mem
,
5516 int npages
= memslot
->npages
;
5518 /*To keep backward compatibility with older userspace,
5519 *x86 needs to hanlde !user_alloc case.
5522 if (npages
&& !old
.rmap
) {
5523 unsigned long userspace_addr
;
5525 down_write(¤t
->mm
->mmap_sem
);
5526 userspace_addr
= do_mmap(NULL
, 0,
5528 PROT_READ
| PROT_WRITE
,
5529 MAP_PRIVATE
| MAP_ANONYMOUS
,
5531 up_write(¤t
->mm
->mmap_sem
);
5533 if (IS_ERR((void *)userspace_addr
))
5534 return PTR_ERR((void *)userspace_addr
);
5536 memslot
->userspace_addr
= userspace_addr
;
5544 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
5545 struct kvm_userspace_memory_region
*mem
,
5546 struct kvm_memory_slot old
,
5550 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
5552 if (!user_alloc
&& !old
.user_alloc
&& old
.rmap
&& !npages
) {
5555 down_write(¤t
->mm
->mmap_sem
);
5556 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
5557 old
.npages
* PAGE_SIZE
);
5558 up_write(¤t
->mm
->mmap_sem
);
5561 "kvm_vm_ioctl_set_memory_region: "
5562 "failed to munmap memory\n");
5565 spin_lock(&kvm
->mmu_lock
);
5566 if (!kvm
->arch
.n_requested_mmu_pages
) {
5567 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
5568 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
5571 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
5572 spin_unlock(&kvm
->mmu_lock
);
5575 void kvm_arch_flush_shadow(struct kvm
*kvm
)
5577 kvm_mmu_zap_all(kvm
);
5578 kvm_reload_remote_mmus(kvm
);
5581 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
5583 return vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
5584 || vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
5585 || vcpu
->arch
.nmi_pending
||
5586 (kvm_arch_interrupt_allowed(vcpu
) &&
5587 kvm_cpu_has_interrupt(vcpu
));
5590 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
5593 int cpu
= vcpu
->cpu
;
5595 if (waitqueue_active(&vcpu
->wq
)) {
5596 wake_up_interruptible(&vcpu
->wq
);
5597 ++vcpu
->stat
.halt_wakeup
;
5601 if (cpu
!= me
&& (unsigned)cpu
< nr_cpu_ids
&& cpu_online(cpu
))
5602 if (atomic_xchg(&vcpu
->guest_mode
, 0))
5603 smp_send_reschedule(cpu
);
5607 int kvm_arch_interrupt_allowed(struct kvm_vcpu
*vcpu
)
5609 return kvm_x86_ops
->interrupt_allowed(vcpu
);
5612 bool kvm_is_linear_rip(struct kvm_vcpu
*vcpu
, unsigned long linear_rip
)
5614 unsigned long current_rip
= kvm_rip_read(vcpu
) +
5615 get_segment_base(vcpu
, VCPU_SREG_CS
);
5617 return current_rip
== linear_rip
;
5619 EXPORT_SYMBOL_GPL(kvm_is_linear_rip
);
5621 unsigned long kvm_get_rflags(struct kvm_vcpu
*vcpu
)
5623 unsigned long rflags
;
5625 rflags
= kvm_x86_ops
->get_rflags(vcpu
);
5626 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
5627 rflags
&= ~X86_EFLAGS_TF
;
5630 EXPORT_SYMBOL_GPL(kvm_get_rflags
);
5632 void kvm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
5634 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
&&
5635 kvm_is_linear_rip(vcpu
, vcpu
->arch
.singlestep_rip
))
5636 rflags
|= X86_EFLAGS_TF
;
5637 kvm_x86_ops
->set_rflags(vcpu
, rflags
);
5639 EXPORT_SYMBOL_GPL(kvm_set_rflags
);
5641 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit
);
5642 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq
);
5643 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault
);
5644 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr
);
5645 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr
);
5646 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun
);
5647 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit
);
5648 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject
);
5649 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit
);
5650 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga
);
5651 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit
);
5652 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts
);