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 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
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 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);
471 EXPORT_SYMBOL_GPL(kvm_set_cr0);
473 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
475 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
477 EXPORT_SYMBOL_GPL(kvm_lmsw);
479 int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
483 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
484 if (index != XCR_XFEATURE_ENABLED_MASK)
487 if (kvm_x86_ops->get_cpl(vcpu) != 0)
489 if (!(xcr0 & XSTATE_FP))
491 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
493 if (xcr0 & ~host_xcr0)
495 vcpu->arch.xcr0 = xcr0;
496 vcpu->guest_xcr0_loaded = 0;
500 int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
502 if (__kvm_set_xcr(vcpu, index, xcr)) {
503 kvm_inject_gp(vcpu, 0);
508 EXPORT_SYMBOL_GPL(kvm_set_xcr);
510 static bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu)
512 struct kvm_cpuid_entry2 *best;
514 best = kvm_find_cpuid_entry(vcpu, 1, 0);
515 return best && (best->ecx & bit(X86_FEATURE_XSAVE));
518 static void update_cpuid(struct kvm_vcpu *vcpu)
520 struct kvm_cpuid_entry2 *best;
522 best = kvm_find_cpuid_entry(vcpu, 1, 0);
526 /* Update OSXSAVE bit */
527 if (cpu_has_xsave && best->function == 0x1) {
528 best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
529 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
530 best->ecx |= bit(X86_FEATURE_OSXSAVE);
534 int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
536 unsigned long old_cr4 = kvm_read_cr4(vcpu);
537 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
539 if (cr4 & CR4_RESERVED_BITS)
542 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
545 if (is_long_mode(vcpu)) {
546 if (!(cr4 & X86_CR4_PAE))
548 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
549 && ((cr4 ^ old_cr4) & pdptr_bits)
550 && !load_pdptrs(vcpu, vcpu->arch.cr3))
553 if (cr4 & X86_CR4_VMXE)
556 kvm_x86_ops->set_cr4(vcpu, cr4);
558 if ((cr4 ^ old_cr4) & pdptr_bits)
559 kvm_mmu_reset_context(vcpu);
561 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
566 EXPORT_SYMBOL_GPL(kvm_set_cr4);
568 int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
570 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
571 kvm_mmu_sync_roots(vcpu);
572 kvm_mmu_flush_tlb(vcpu);
576 if (is_long_mode(vcpu)) {
577 if (cr3 & CR3_L_MODE_RESERVED_BITS)
581 if (cr3 & CR3_PAE_RESERVED_BITS)
583 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3))
587 * We don't check reserved bits in nonpae mode, because
588 * this isn't enforced, and VMware depends on this.
593 * Does the new cr3 value map to physical memory? (Note, we
594 * catch an invalid cr3 even in real-mode, because it would
595 * cause trouble later on when we turn on paging anyway.)
597 * A real CPU would silently accept an invalid cr3 and would
598 * attempt to use it - with largely undefined (and often hard
599 * to debug) behavior on the guest side.
601 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
603 vcpu->arch.cr3 = cr3;
604 vcpu->arch.mmu.new_cr3(vcpu);
607 EXPORT_SYMBOL_GPL(kvm_set_cr3);
609 int __kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
611 if (cr8 & CR8_RESERVED_BITS)
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_tpr(vcpu, cr8);
616 vcpu->arch.cr8 = cr8;
620 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
622 if (__kvm_set_cr8(vcpu, cr8))
623 kvm_inject_gp(vcpu, 0);
625 EXPORT_SYMBOL_GPL(kvm_set_cr8);
627 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
629 if (irqchip_in_kernel(vcpu->kvm))
630 return kvm_lapic_get_cr8(vcpu);
632 return vcpu->arch.cr8;
634 EXPORT_SYMBOL_GPL(kvm_get_cr8);
636 static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
640 vcpu->arch.db[dr] = val;
641 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
642 vcpu->arch.eff_db[dr] = val;
645 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
649 if (val & 0xffffffff00000000ULL)
651 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
654 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
658 if (val & 0xffffffff00000000ULL)
660 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
661 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
662 kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7);
663 vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK);
671 int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
675 res = __kvm_set_dr(vcpu, dr, val);
677 kvm_queue_exception(vcpu, UD_VECTOR);
679 kvm_inject_gp(vcpu, 0);
683 EXPORT_SYMBOL_GPL(kvm_set_dr);
685 static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
689 *val = vcpu->arch.db[dr];
692 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
696 *val = vcpu->arch.dr6;
699 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
703 *val = vcpu->arch.dr7;
710 int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
712 if (_kvm_get_dr(vcpu, dr, val)) {
713 kvm_queue_exception(vcpu, UD_VECTOR);
718 EXPORT_SYMBOL_GPL(kvm_get_dr);
721 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
722 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
724 * This list is modified at module load time to reflect the
725 * capabilities of the host cpu. This capabilities test skips MSRs that are
726 * kvm-specific. Those are put in the beginning of the list.
729 #define KVM_SAVE_MSRS_BEGIN 7
730 static u32 msrs_to_save[] = {
731 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
732 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
733 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
734 HV_X64_MSR_APIC_ASSIST_PAGE,
735 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
738 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
740 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
743 static unsigned num_msrs_to_save;
745 static u32 emulated_msrs[] = {
746 MSR_IA32_MISC_ENABLE,
751 static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
753 u64 old_efer = vcpu->arch.efer;
755 if (efer & efer_reserved_bits)
759 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
762 if (efer & EFER_FFXSR) {
763 struct kvm_cpuid_entry2 *feat;
765 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
766 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
770 if (efer & EFER_SVME) {
771 struct kvm_cpuid_entry2 *feat;
773 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
774 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
779 efer |= vcpu->arch.efer & EFER_LMA;
781 kvm_x86_ops->set_efer(vcpu, efer);
783 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
784 kvm_mmu_reset_context(vcpu);
786 /* Update reserved bits */
787 if ((efer ^ old_efer) & EFER_NX)
788 kvm_mmu_reset_context(vcpu);
793 void kvm_enable_efer_bits(u64 mask)
795 efer_reserved_bits &= ~mask;
797 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
801 * Writes msr value into into the appropriate "register".
802 * Returns 0 on success, non-0 otherwise.
803 * Assumes vcpu_load() was already called.
805 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
807 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
811 * Adapt set_msr() to msr_io()'s calling convention
813 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
815 return kvm_set_msr(vcpu, index, *data);
818 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
822 struct pvclock_wall_clock wc;
823 struct timespec boot;
828 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
833 ++version; /* first time write, random junk */
837 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
840 * The guest calculates current wall clock time by adding
841 * system time (updated by kvm_write_guest_time below) to the
842 * wall clock specified here. guest system time equals host
843 * system time for us, thus we must fill in host boot time here.
847 wc.sec = boot.tv_sec;
848 wc.nsec = boot.tv_nsec;
849 wc.version = version;
851 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
854 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
857 static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
859 uint32_t quotient, remainder;
861 /* Don't try to replace with do_div(), this one calculates
862 * "(dividend << 32) / divisor" */
864 : "=a" (quotient), "=d" (remainder)
865 : "0" (0), "1" (dividend), "r" (divisor) );
869 static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
871 uint64_t nsecs = 1000000000LL;
876 tps64 = tsc_khz * 1000LL;
877 while (tps64 > nsecs*2) {
882 tps32 = (uint32_t)tps64;
883 while (tps32 <= (uint32_t)nsecs) {
888 hv_clock->tsc_shift = shift;
889 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
891 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
892 __func__, tsc_khz, hv_clock->tsc_shift,
893 hv_clock->tsc_to_system_mul);
896 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
898 static void kvm_write_guest_time(struct kvm_vcpu *v)
902 struct kvm_vcpu_arch *vcpu = &v->arch;
904 unsigned long this_tsc_khz;
906 if ((!vcpu->time_page))
909 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
910 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
911 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
912 vcpu->hv_clock_tsc_khz = this_tsc_khz;
914 put_cpu_var(cpu_tsc_khz);
916 /* Keep irq disabled to prevent changes to the clock */
917 local_irq_save(flags);
918 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
920 monotonic_to_bootbased(&ts);
921 local_irq_restore(flags);
923 /* With all the info we got, fill in the values */
925 vcpu->hv_clock.system_time = ts.tv_nsec +
926 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
928 vcpu->hv_clock.flags = 0;
931 * The interface expects us to write an even number signaling that the
932 * update is finished. Since the guest won't see the intermediate
933 * state, we just increase by 2 at the end.
935 vcpu->hv_clock.version += 2;
937 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
939 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
940 sizeof(vcpu->hv_clock));
942 kunmap_atomic(shared_kaddr, KM_USER0);
944 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
947 static int kvm_request_guest_time_update(struct kvm_vcpu *v)
949 struct kvm_vcpu_arch *vcpu = &v->arch;
951 if (!vcpu->time_page)
953 kvm_make_request(KVM_REQ_KVMCLOCK_UPDATE, v);
957 static bool msr_mtrr_valid(unsigned msr)
960 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
961 case MSR_MTRRfix64K_00000:
962 case MSR_MTRRfix16K_80000:
963 case MSR_MTRRfix16K_A0000:
964 case MSR_MTRRfix4K_C0000:
965 case MSR_MTRRfix4K_C8000:
966 case MSR_MTRRfix4K_D0000:
967 case MSR_MTRRfix4K_D8000:
968 case MSR_MTRRfix4K_E0000:
969 case MSR_MTRRfix4K_E8000:
970 case MSR_MTRRfix4K_F0000:
971 case MSR_MTRRfix4K_F8000:
972 case MSR_MTRRdefType:
973 case MSR_IA32_CR_PAT:
981 static bool valid_pat_type(unsigned t)
983 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
986 static bool valid_mtrr_type(unsigned t)
988 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
991 static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
995 if (!msr_mtrr_valid(msr))
998 if (msr == MSR_IA32_CR_PAT) {
999 for (i = 0; i < 8; i++)
1000 if (!valid_pat_type((data >> (i * 8)) & 0xff))
1003 } else if (msr == MSR_MTRRdefType) {
1006 return valid_mtrr_type(data & 0xff);
1007 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
1008 for (i = 0; i < 8 ; i++)
1009 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
1014 /* variable MTRRs */
1015 return valid_mtrr_type(data & 0xff);
1018 static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1020 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1022 if (!mtrr_valid(vcpu, msr, data))
1025 if (msr == MSR_MTRRdefType) {
1026 vcpu->arch.mtrr_state.def_type = data;
1027 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
1028 } else if (msr == MSR_MTRRfix64K_00000)
1030 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1031 p[1 + msr - MSR_MTRRfix16K_80000] = data;
1032 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1033 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
1034 else if (msr == MSR_IA32_CR_PAT)
1035 vcpu->arch.pat = data;
1036 else { /* Variable MTRRs */
1037 int idx, is_mtrr_mask;
1040 idx = (msr - 0x200) / 2;
1041 is_mtrr_mask = msr - 0x200 - 2 * idx;
1044 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1047 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1051 kvm_mmu_reset_context(vcpu);
1055 static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1057 u64 mcg_cap = vcpu->arch.mcg_cap;
1058 unsigned bank_num = mcg_cap & 0xff;
1061 case MSR_IA32_MCG_STATUS:
1062 vcpu->arch.mcg_status = data;
1064 case MSR_IA32_MCG_CTL:
1065 if (!(mcg_cap & MCG_CTL_P))
1067 if (data != 0 && data != ~(u64)0)
1069 vcpu->arch.mcg_ctl = data;
1072 if (msr >= MSR_IA32_MC0_CTL &&
1073 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1074 u32 offset = msr - MSR_IA32_MC0_CTL;
1075 /* only 0 or all 1s can be written to IA32_MCi_CTL
1076 * some Linux kernels though clear bit 10 in bank 4 to
1077 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1078 * this to avoid an uncatched #GP in the guest
1080 if ((offset & 0x3) == 0 &&
1081 data != 0 && (data | (1 << 10)) != ~(u64)0)
1083 vcpu->arch.mce_banks[offset] = data;
1091 static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1093 struct kvm *kvm = vcpu->kvm;
1094 int lm = is_long_mode(vcpu);
1095 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1096 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1097 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1098 : kvm->arch.xen_hvm_config.blob_size_32;
1099 u32 page_num = data & ~PAGE_MASK;
1100 u64 page_addr = data & PAGE_MASK;
1105 if (page_num >= blob_size)
1108 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
1112 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
1114 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1123 static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1125 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1128 static bool kvm_hv_msr_partition_wide(u32 msr)
1132 case HV_X64_MSR_GUEST_OS_ID:
1133 case HV_X64_MSR_HYPERCALL:
1141 static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1143 struct kvm *kvm = vcpu->kvm;
1146 case HV_X64_MSR_GUEST_OS_ID:
1147 kvm->arch.hv_guest_os_id = data;
1148 /* setting guest os id to zero disables hypercall page */
1149 if (!kvm->arch.hv_guest_os_id)
1150 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1152 case HV_X64_MSR_HYPERCALL: {
1157 /* if guest os id is not set hypercall should remain disabled */
1158 if (!kvm->arch.hv_guest_os_id)
1160 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1161 kvm->arch.hv_hypercall = data;
1164 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1165 addr = gfn_to_hva(kvm, gfn);
1166 if (kvm_is_error_hva(addr))
1168 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1169 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1170 if (copy_to_user((void __user *)addr, instructions, 4))
1172 kvm->arch.hv_hypercall = data;
1176 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1177 "data 0x%llx\n", msr, data);
1183 static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1186 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1189 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1190 vcpu->arch.hv_vapic = data;
1193 addr = gfn_to_hva(vcpu->kvm, data >>
1194 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1195 if (kvm_is_error_hva(addr))
1197 if (clear_user((void __user *)addr, PAGE_SIZE))
1199 vcpu->arch.hv_vapic = data;
1202 case HV_X64_MSR_EOI:
1203 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1204 case HV_X64_MSR_ICR:
1205 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1206 case HV_X64_MSR_TPR:
1207 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1209 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1210 "data 0x%llx\n", msr, data);
1217 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1221 return set_efer(vcpu, data);
1223 data &= ~(u64)0x40; /* ignore flush filter disable */
1224 data &= ~(u64)0x100; /* ignore ignne emulation enable */
1226 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1231 case MSR_FAM10H_MMIO_CONF_BASE:
1233 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1238 case MSR_AMD64_NB_CFG:
1240 case MSR_IA32_DEBUGCTLMSR:
1242 /* We support the non-activated case already */
1244 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1245 /* Values other than LBR and BTF are vendor-specific,
1246 thus reserved and should throw a #GP */
1249 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1252 case MSR_IA32_UCODE_REV:
1253 case MSR_IA32_UCODE_WRITE:
1254 case MSR_VM_HSAVE_PA:
1255 case MSR_AMD64_PATCH_LOADER:
1257 case 0x200 ... 0x2ff:
1258 return set_msr_mtrr(vcpu, msr, data);
1259 case MSR_IA32_APICBASE:
1260 kvm_set_apic_base(vcpu, data);
1262 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1263 return kvm_x2apic_msr_write(vcpu, msr, data);
1264 case MSR_IA32_MISC_ENABLE:
1265 vcpu->arch.ia32_misc_enable_msr = data;
1267 case MSR_KVM_WALL_CLOCK_NEW:
1268 case MSR_KVM_WALL_CLOCK:
1269 vcpu->kvm->arch.wall_clock = data;
1270 kvm_write_wall_clock(vcpu->kvm, data);
1272 case MSR_KVM_SYSTEM_TIME_NEW:
1273 case MSR_KVM_SYSTEM_TIME: {
1274 if (vcpu->arch.time_page) {
1275 kvm_release_page_dirty(vcpu->arch.time_page);
1276 vcpu->arch.time_page = NULL;
1279 vcpu->arch.time = data;
1281 /* we verify if the enable bit is set... */
1285 /* ...but clean it before doing the actual write */
1286 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1288 vcpu->arch.time_page =
1289 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
1291 if (is_error_page(vcpu->arch.time_page)) {
1292 kvm_release_page_clean(vcpu->arch.time_page);
1293 vcpu->arch.time_page = NULL;
1296 kvm_request_guest_time_update(vcpu);
1299 case MSR_IA32_MCG_CTL:
1300 case MSR_IA32_MCG_STATUS:
1301 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1302 return set_msr_mce(vcpu, msr, data);
1304 /* Performance counters are not protected by a CPUID bit,
1305 * so we should check all of them in the generic path for the sake of
1306 * cross vendor migration.
1307 * Writing a zero into the event select MSRs disables them,
1308 * which we perfectly emulate ;-). Any other value should be at least
1309 * reported, some guests depend on them.
1311 case MSR_P6_EVNTSEL0:
1312 case MSR_P6_EVNTSEL1:
1313 case MSR_K7_EVNTSEL0:
1314 case MSR_K7_EVNTSEL1:
1315 case MSR_K7_EVNTSEL2:
1316 case MSR_K7_EVNTSEL3:
1318 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1319 "0x%x data 0x%llx\n", msr, data);
1321 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1322 * so we ignore writes to make it happy.
1324 case MSR_P6_PERFCTR0:
1325 case MSR_P6_PERFCTR1:
1326 case MSR_K7_PERFCTR0:
1327 case MSR_K7_PERFCTR1:
1328 case MSR_K7_PERFCTR2:
1329 case MSR_K7_PERFCTR3:
1330 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1331 "0x%x data 0x%llx\n", msr, data);
1333 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1334 if (kvm_hv_msr_partition_wide(msr)) {
1336 mutex_lock(&vcpu->kvm->lock);
1337 r = set_msr_hyperv_pw(vcpu, msr, data);
1338 mutex_unlock(&vcpu->kvm->lock);
1341 return set_msr_hyperv(vcpu, msr, data);
1344 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1345 return xen_hvm_config(vcpu, data);
1347 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1351 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1358 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1362 * Reads an msr value (of 'msr_index') into 'pdata'.
1363 * Returns 0 on success, non-0 otherwise.
1364 * Assumes vcpu_load() was already called.
1366 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1368 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1371 static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1373 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1375 if (!msr_mtrr_valid(msr))
1378 if (msr == MSR_MTRRdefType)
1379 *pdata = vcpu->arch.mtrr_state.def_type +
1380 (vcpu->arch.mtrr_state.enabled << 10);
1381 else if (msr == MSR_MTRRfix64K_00000)
1383 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1384 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1385 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1386 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1387 else if (msr == MSR_IA32_CR_PAT)
1388 *pdata = vcpu->arch.pat;
1389 else { /* Variable MTRRs */
1390 int idx, is_mtrr_mask;
1393 idx = (msr - 0x200) / 2;
1394 is_mtrr_mask = msr - 0x200 - 2 * idx;
1397 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1400 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1407 static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1410 u64 mcg_cap = vcpu->arch.mcg_cap;
1411 unsigned bank_num = mcg_cap & 0xff;
1414 case MSR_IA32_P5_MC_ADDR:
1415 case MSR_IA32_P5_MC_TYPE:
1418 case MSR_IA32_MCG_CAP:
1419 data = vcpu->arch.mcg_cap;
1421 case MSR_IA32_MCG_CTL:
1422 if (!(mcg_cap & MCG_CTL_P))
1424 data = vcpu->arch.mcg_ctl;
1426 case MSR_IA32_MCG_STATUS:
1427 data = vcpu->arch.mcg_status;
1430 if (msr >= MSR_IA32_MC0_CTL &&
1431 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1432 u32 offset = msr - MSR_IA32_MC0_CTL;
1433 data = vcpu->arch.mce_banks[offset];
1442 static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1445 struct kvm *kvm = vcpu->kvm;
1448 case HV_X64_MSR_GUEST_OS_ID:
1449 data = kvm->arch.hv_guest_os_id;
1451 case HV_X64_MSR_HYPERCALL:
1452 data = kvm->arch.hv_hypercall;
1455 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1463 static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1468 case HV_X64_MSR_VP_INDEX: {
1471 kvm_for_each_vcpu(r, v, vcpu->kvm)
1476 case HV_X64_MSR_EOI:
1477 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1478 case HV_X64_MSR_ICR:
1479 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1480 case HV_X64_MSR_TPR:
1481 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1483 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1490 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1495 case MSR_IA32_PLATFORM_ID:
1496 case MSR_IA32_UCODE_REV:
1497 case MSR_IA32_EBL_CR_POWERON:
1498 case MSR_IA32_DEBUGCTLMSR:
1499 case MSR_IA32_LASTBRANCHFROMIP:
1500 case MSR_IA32_LASTBRANCHTOIP:
1501 case MSR_IA32_LASTINTFROMIP:
1502 case MSR_IA32_LASTINTTOIP:
1505 case MSR_VM_HSAVE_PA:
1506 case MSR_P6_PERFCTR0:
1507 case MSR_P6_PERFCTR1:
1508 case MSR_P6_EVNTSEL0:
1509 case MSR_P6_EVNTSEL1:
1510 case MSR_K7_EVNTSEL0:
1511 case MSR_K7_PERFCTR0:
1512 case MSR_K8_INT_PENDING_MSG:
1513 case MSR_AMD64_NB_CFG:
1514 case MSR_FAM10H_MMIO_CONF_BASE:
1518 data = 0x500 | KVM_NR_VAR_MTRR;
1520 case 0x200 ... 0x2ff:
1521 return get_msr_mtrr(vcpu, msr, pdata);
1522 case 0xcd: /* fsb frequency */
1525 case MSR_IA32_APICBASE:
1526 data = kvm_get_apic_base(vcpu);
1528 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1529 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1531 case MSR_IA32_MISC_ENABLE:
1532 data = vcpu->arch.ia32_misc_enable_msr;
1534 case MSR_IA32_PERF_STATUS:
1535 /* TSC increment by tick */
1537 /* CPU multiplier */
1538 data |= (((uint64_t)4ULL) << 40);
1541 data = vcpu->arch.efer;
1543 case MSR_KVM_WALL_CLOCK:
1544 case MSR_KVM_WALL_CLOCK_NEW:
1545 data = vcpu->kvm->arch.wall_clock;
1547 case MSR_KVM_SYSTEM_TIME:
1548 case MSR_KVM_SYSTEM_TIME_NEW:
1549 data = vcpu->arch.time;
1551 case MSR_IA32_P5_MC_ADDR:
1552 case MSR_IA32_P5_MC_TYPE:
1553 case MSR_IA32_MCG_CAP:
1554 case MSR_IA32_MCG_CTL:
1555 case MSR_IA32_MCG_STATUS:
1556 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1557 return get_msr_mce(vcpu, msr, pdata);
1558 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1559 if (kvm_hv_msr_partition_wide(msr)) {
1561 mutex_lock(&vcpu->kvm->lock);
1562 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1563 mutex_unlock(&vcpu->kvm->lock);
1566 return get_msr_hyperv(vcpu, msr, pdata);
1570 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1573 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1581 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1584 * Read or write a bunch of msrs. All parameters are kernel addresses.
1586 * @return number of msrs set successfully.
1588 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1589 struct kvm_msr_entry *entries,
1590 int (*do_msr)(struct kvm_vcpu *vcpu,
1591 unsigned index, u64 *data))
1595 idx = srcu_read_lock(&vcpu->kvm->srcu);
1596 for (i = 0; i < msrs->nmsrs; ++i)
1597 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1599 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1605 * Read or write a bunch of msrs. Parameters are user addresses.
1607 * @return number of msrs set successfully.
1609 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1610 int (*do_msr)(struct kvm_vcpu *vcpu,
1611 unsigned index, u64 *data),
1614 struct kvm_msrs msrs;
1615 struct kvm_msr_entry *entries;
1620 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1624 if (msrs.nmsrs >= MAX_IO_MSRS)
1628 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1629 entries = kmalloc(size, GFP_KERNEL);
1634 if (copy_from_user(entries, user_msrs->entries, size))
1637 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1642 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1653 int kvm_dev_ioctl_check_extension(long ext)
1658 case KVM_CAP_IRQCHIP:
1660 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
1661 case KVM_CAP_SET_TSS_ADDR:
1662 case KVM_CAP_EXT_CPUID:
1663 case KVM_CAP_CLOCKSOURCE:
1665 case KVM_CAP_NOP_IO_DELAY:
1666 case KVM_CAP_MP_STATE:
1667 case KVM_CAP_SYNC_MMU:
1668 case KVM_CAP_REINJECT_CONTROL:
1669 case KVM_CAP_IRQ_INJECT_STATUS:
1670 case KVM_CAP_ASSIGN_DEV_IRQ:
1672 case KVM_CAP_IOEVENTFD:
1674 case KVM_CAP_PIT_STATE2:
1675 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
1676 case KVM_CAP_XEN_HVM:
1677 case KVM_CAP_ADJUST_CLOCK:
1678 case KVM_CAP_VCPU_EVENTS:
1679 case KVM_CAP_HYPERV:
1680 case KVM_CAP_HYPERV_VAPIC:
1681 case KVM_CAP_HYPERV_SPIN:
1682 case KVM_CAP_PCI_SEGMENT:
1683 case KVM_CAP_DEBUGREGS:
1684 case KVM_CAP_X86_ROBUST_SINGLESTEP:
1688 case KVM_CAP_COALESCED_MMIO:
1689 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1692 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1694 case KVM_CAP_NR_VCPUS:
1697 case KVM_CAP_NR_MEMSLOTS:
1698 r = KVM_MEMORY_SLOTS;
1700 case KVM_CAP_PV_MMU: /* obsolete */
1707 r = KVM_MAX_MCE_BANKS;
1720 long kvm_arch_dev_ioctl(struct file *filp,
1721 unsigned int ioctl, unsigned long arg)
1723 void __user *argp = (void __user *)arg;
1727 case KVM_GET_MSR_INDEX_LIST: {
1728 struct kvm_msr_list __user *user_msr_list = argp;
1729 struct kvm_msr_list msr_list;
1733 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1736 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1737 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1740 if (n < msr_list.nmsrs)
1743 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1744 num_msrs_to_save * sizeof(u32)))
1746 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
1748 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1753 case KVM_GET_SUPPORTED_CPUID: {
1754 struct kvm_cpuid2 __user *cpuid_arg = argp;
1755 struct kvm_cpuid2 cpuid;
1758 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1760 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
1761 cpuid_arg->entries);
1766 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1771 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1774 mce_cap = KVM_MCE_CAP_SUPPORTED;
1776 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1788 static void wbinvd_ipi(void *garbage)
1793 static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
1795 return vcpu->kvm->arch.iommu_domain &&
1796 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
1799 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1801 /* Address WBINVD may be executed by guest */
1802 if (need_emulate_wbinvd(vcpu)) {
1803 if (kvm_x86_ops->has_wbinvd_exit())
1804 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
1805 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
1806 smp_call_function_single(vcpu->cpu,
1807 wbinvd_ipi, NULL, 1);
1810 kvm_x86_ops->vcpu_load(vcpu, cpu);
1811 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1812 unsigned long khz = cpufreq_quick_get(cpu);
1815 per_cpu(cpu_tsc_khz, cpu) = khz;
1817 kvm_request_guest_time_update(vcpu);
1820 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1822 kvm_x86_ops->vcpu_put(vcpu);
1823 kvm_put_guest_fpu(vcpu);
1826 static int is_efer_nx(void)
1828 unsigned long long efer = 0;
1830 rdmsrl_safe(MSR_EFER, &efer);
1831 return efer & EFER_NX;
1834 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1837 struct kvm_cpuid_entry2 *e, *entry;
1840 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1841 e = &vcpu->arch.cpuid_entries[i];
1842 if (e->function == 0x80000001) {
1847 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
1848 entry->edx &= ~(1 << 20);
1849 printk(KERN_INFO "kvm: guest NX capability removed\n");
1853 /* when an old userspace process fills a new kernel module */
1854 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1855 struct kvm_cpuid *cpuid,
1856 struct kvm_cpuid_entry __user *entries)
1859 struct kvm_cpuid_entry *cpuid_entries;
1862 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1865 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1869 if (copy_from_user(cpuid_entries, entries,
1870 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1872 for (i = 0; i < cpuid->nent; i++) {
1873 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1874 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1875 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1876 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1877 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1878 vcpu->arch.cpuid_entries[i].index = 0;
1879 vcpu->arch.cpuid_entries[i].flags = 0;
1880 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1881 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1882 vcpu->arch.cpuid_entries[i].padding[2] = 0;
1884 vcpu->arch.cpuid_nent = cpuid->nent;
1885 cpuid_fix_nx_cap(vcpu);
1887 kvm_apic_set_version(vcpu);
1888 kvm_x86_ops->cpuid_update(vcpu);
1892 vfree(cpuid_entries);
1897 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1898 struct kvm_cpuid2 *cpuid,
1899 struct kvm_cpuid_entry2 __user *entries)
1904 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1907 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
1908 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
1910 vcpu->arch.cpuid_nent = cpuid->nent;
1911 kvm_apic_set_version(vcpu);
1912 kvm_x86_ops->cpuid_update(vcpu);
1920 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1921 struct kvm_cpuid2 *cpuid,
1922 struct kvm_cpuid_entry2 __user *entries)
1927 if (cpuid->nent < vcpu->arch.cpuid_nent)
1930 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1931 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1936 cpuid->nent = vcpu->arch.cpuid_nent;
1940 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1943 entry->function = function;
1944 entry->index = index;
1945 cpuid_count(entry->function, entry->index,
1946 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1950 #define F(x) bit(X86_FEATURE_##x)
1952 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1953 u32 index, int *nent, int maxnent)
1955 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
1956 #ifdef CONFIG_X86_64
1957 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1959 unsigned f_lm = F(LM);
1961 unsigned f_gbpages = 0;
1964 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
1967 const u32 kvm_supported_word0_x86_features =
1968 F(FPU) | F(VME) | F(DE) | F(PSE) |
1969 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1970 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1971 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1972 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1973 0 /* Reserved, DS, ACPI */ | F(MMX) |
1974 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1975 0 /* HTT, TM, Reserved, PBE */;
1976 /* cpuid 0x80000001.edx */
1977 const u32 kvm_supported_word1_x86_features =
1978 F(FPU) | F(VME) | F(DE) | F(PSE) |
1979 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1980 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1981 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1982 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1983 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
1984 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
1985 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1987 const u32 kvm_supported_word4_x86_features =
1988 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
1989 0 /* DS-CPL, VMX, SMX, EST */ |
1990 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1991 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1992 0 /* Reserved, DCA */ | F(XMM4_1) |
1993 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
1994 0 /* Reserved, AES */ | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX);
1995 /* cpuid 0x80000001.ecx */
1996 const u32 kvm_supported_word6_x86_features =
1997 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1998 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1999 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
2000 0 /* SKINIT */ | 0 /* WDT */;
2002 /* all calls to cpuid_count() should be made on the same cpu */
2004 do_cpuid_1_ent(entry, function, index);
2009 entry->eax = min(entry->eax, (u32)0xd);
2012 entry->edx &= kvm_supported_word0_x86_features;
2013 entry->ecx &= kvm_supported_word4_x86_features;
2014 /* we support x2apic emulation even if host does not support
2015 * it since we emulate x2apic in software */
2016 entry->ecx |= F(X2APIC);
2018 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
2019 * may return different values. This forces us to get_cpu() before
2020 * issuing the first command, and also to emulate this annoying behavior
2021 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
2023 int t, times = entry->eax & 0xff;
2025 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
2026 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2027 for (t = 1; t < times && *nent < maxnent; ++t) {
2028 do_cpuid_1_ent(&entry[t], function, 0);
2029 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
2034 /* function 4 and 0xb have additional index. */
2038 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2039 /* read more entries until cache_type is zero */
2040 for (i = 1; *nent < maxnent; ++i) {
2041 cache_type = entry[i - 1].eax & 0x1f;
2044 do_cpuid_1_ent(&entry[i], function, i);
2046 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2054 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2055 /* read more entries until level_type is zero */
2056 for (i = 1; *nent < maxnent; ++i) {
2057 level_type = entry[i - 1].ecx & 0xff00;
2060 do_cpuid_1_ent(&entry[i], function, i);
2062 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2070 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2071 for (i = 1; *nent < maxnent; ++i) {
2072 if (entry[i - 1].eax == 0 && i != 2)
2074 do_cpuid_1_ent(&entry[i], function, i);
2076 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2081 case KVM_CPUID_SIGNATURE: {
2082 char signature[12] = "KVMKVMKVM\0\0";
2083 u32 *sigptr = (u32 *)signature;
2085 entry->ebx = sigptr[0];
2086 entry->ecx = sigptr[1];
2087 entry->edx = sigptr[2];
2090 case KVM_CPUID_FEATURES:
2091 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
2092 (1 << KVM_FEATURE_NOP_IO_DELAY) |
2093 (1 << KVM_FEATURE_CLOCKSOURCE2) |
2094 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
2100 entry->eax = min(entry->eax, 0x8000001a);
2103 entry->edx &= kvm_supported_word1_x86_features;
2104 entry->ecx &= kvm_supported_word6_x86_features;
2108 kvm_x86_ops->set_supported_cpuid(function, entry);
2115 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
2116 struct kvm_cpuid_entry2 __user *entries)
2118 struct kvm_cpuid_entry2 *cpuid_entries;
2119 int limit, nent = 0, r = -E2BIG;
2122 if (cpuid->nent < 1)
2124 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2125 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
2127 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
2131 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
2132 limit = cpuid_entries[0].eax;
2133 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
2134 do_cpuid_ent(&cpuid_entries[nent], func, 0,
2135 &nent, cpuid->nent);
2137 if (nent >= cpuid->nent)
2140 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
2141 limit = cpuid_entries[nent - 1].eax;
2142 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
2143 do_cpuid_ent(&cpuid_entries[nent], func, 0,
2144 &nent, cpuid->nent);
2149 if (nent >= cpuid->nent)
2152 do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent,
2156 if (nent >= cpuid->nent)
2159 do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent,
2163 if (nent >= cpuid->nent)
2167 if (copy_to_user(entries, cpuid_entries,
2168 nent * sizeof(struct kvm_cpuid_entry2)))
2174 vfree(cpuid_entries);
2179 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2180 struct kvm_lapic_state *s)
2182 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
2187 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2188 struct kvm_lapic_state *s)
2190 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
2191 kvm_apic_post_state_restore(vcpu);
2192 update_cr8_intercept(vcpu);
2197 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2198 struct kvm_interrupt *irq)
2200 if (irq->irq < 0 || irq->irq >= 256)
2202 if (irqchip_in_kernel(vcpu->kvm))
2205 kvm_queue_interrupt(vcpu, irq->irq, false);
2210 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2212 kvm_inject_nmi(vcpu);
2217 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2218 struct kvm_tpr_access_ctl *tac)
2222 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2226 static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2230 unsigned bank_num = mcg_cap & 0xff, bank;
2233 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
2235 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2238 vcpu->arch.mcg_cap = mcg_cap;
2239 /* Init IA32_MCG_CTL to all 1s */
2240 if (mcg_cap & MCG_CTL_P)
2241 vcpu->arch.mcg_ctl = ~(u64)0;
2242 /* Init IA32_MCi_CTL to all 1s */
2243 for (bank = 0; bank < bank_num; bank++)
2244 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2249 static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2250 struct kvm_x86_mce *mce)
2252 u64 mcg_cap = vcpu->arch.mcg_cap;
2253 unsigned bank_num = mcg_cap & 0xff;
2254 u64 *banks = vcpu->arch.mce_banks;
2256 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2259 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2260 * reporting is disabled
2262 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2263 vcpu->arch.mcg_ctl != ~(u64)0)
2265 banks += 4 * mce->bank;
2267 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2268 * reporting is disabled for the bank
2270 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2272 if (mce->status & MCI_STATUS_UC) {
2273 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
2274 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
2275 printk(KERN_DEBUG "kvm: set_mce: "
2276 "injects mce exception while "
2277 "previous one is in progress!\n");
2278 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2281 if (banks[1] & MCI_STATUS_VAL)
2282 mce->status |= MCI_STATUS_OVER;
2283 banks[2] = mce->addr;
2284 banks[3] = mce->misc;
2285 vcpu->arch.mcg_status = mce->mcg_status;
2286 banks[1] = mce->status;
2287 kvm_queue_exception(vcpu, MC_VECTOR);
2288 } else if (!(banks[1] & MCI_STATUS_VAL)
2289 || !(banks[1] & MCI_STATUS_UC)) {
2290 if (banks[1] & MCI_STATUS_VAL)
2291 mce->status |= MCI_STATUS_OVER;
2292 banks[2] = mce->addr;
2293 banks[3] = mce->misc;
2294 banks[1] = mce->status;
2296 banks[1] |= MCI_STATUS_OVER;
2300 static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2301 struct kvm_vcpu_events *events)
2303 events->exception.injected =
2304 vcpu->arch.exception.pending &&
2305 !kvm_exception_is_soft(vcpu->arch.exception.nr);
2306 events->exception.nr = vcpu->arch.exception.nr;
2307 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2308 events->exception.error_code = vcpu->arch.exception.error_code;
2310 events->interrupt.injected =
2311 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
2312 events->interrupt.nr = vcpu->arch.interrupt.nr;
2313 events->interrupt.soft = 0;
2314 events->interrupt.shadow =
2315 kvm_x86_ops->get_interrupt_shadow(vcpu,
2316 KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
2318 events->nmi.injected = vcpu->arch.nmi_injected;
2319 events->nmi.pending = vcpu->arch.nmi_pending;
2320 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2322 events->sipi_vector = vcpu->arch.sipi_vector;
2324 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
2325 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2326 | KVM_VCPUEVENT_VALID_SHADOW);
2329 static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2330 struct kvm_vcpu_events *events)
2332 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2333 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2334 | KVM_VCPUEVENT_VALID_SHADOW))
2337 vcpu->arch.exception.pending = events->exception.injected;
2338 vcpu->arch.exception.nr = events->exception.nr;
2339 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2340 vcpu->arch.exception.error_code = events->exception.error_code;
2342 vcpu->arch.interrupt.pending = events->interrupt.injected;
2343 vcpu->arch.interrupt.nr = events->interrupt.nr;
2344 vcpu->arch.interrupt.soft = events->interrupt.soft;
2345 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2346 kvm_pic_clear_isr_ack(vcpu->kvm);
2347 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2348 kvm_x86_ops->set_interrupt_shadow(vcpu,
2349 events->interrupt.shadow);
2351 vcpu->arch.nmi_injected = events->nmi.injected;
2352 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2353 vcpu->arch.nmi_pending = events->nmi.pending;
2354 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2356 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2357 vcpu->arch.sipi_vector = events->sipi_vector;
2362 static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2363 struct kvm_debugregs *dbgregs)
2365 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
2366 dbgregs->dr6 = vcpu->arch.dr6;
2367 dbgregs->dr7 = vcpu->arch.dr7;
2371 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2372 struct kvm_debugregs *dbgregs)
2377 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
2378 vcpu->arch.dr6 = dbgregs->dr6;
2379 vcpu->arch.dr7 = dbgregs->dr7;
2384 static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
2385 struct kvm_xsave *guest_xsave)
2388 memcpy(guest_xsave->region,
2389 &vcpu->arch.guest_fpu.state->xsave,
2392 memcpy(guest_xsave->region,
2393 &vcpu->arch.guest_fpu.state->fxsave,
2394 sizeof(struct i387_fxsave_struct));
2395 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
2400 static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
2401 struct kvm_xsave *guest_xsave)
2404 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
2407 memcpy(&vcpu->arch.guest_fpu.state->xsave,
2408 guest_xsave->region, xstate_size);
2410 if (xstate_bv & ~XSTATE_FPSSE)
2412 memcpy(&vcpu->arch.guest_fpu.state->fxsave,
2413 guest_xsave->region, sizeof(struct i387_fxsave_struct));
2418 static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
2419 struct kvm_xcrs *guest_xcrs)
2421 if (!cpu_has_xsave) {
2422 guest_xcrs->nr_xcrs = 0;
2426 guest_xcrs->nr_xcrs = 1;
2427 guest_xcrs->flags = 0;
2428 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
2429 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
2432 static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
2433 struct kvm_xcrs *guest_xcrs)
2440 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
2443 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
2444 /* Only support XCR0 currently */
2445 if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) {
2446 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
2447 guest_xcrs->xcrs[0].value);
2455 long kvm_arch_vcpu_ioctl(struct file *filp,
2456 unsigned int ioctl, unsigned long arg)
2458 struct kvm_vcpu *vcpu = filp->private_data;
2459 void __user *argp = (void __user *)arg;
2462 struct kvm_lapic_state *lapic;
2463 struct kvm_xsave *xsave;
2464 struct kvm_xcrs *xcrs;
2470 case KVM_GET_LAPIC: {
2472 if (!vcpu->arch.apic)
2474 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2479 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
2483 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
2488 case KVM_SET_LAPIC: {
2490 if (!vcpu->arch.apic)
2492 u.lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2497 if (copy_from_user(u.lapic, argp, sizeof(struct kvm_lapic_state)))
2499 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
2505 case KVM_INTERRUPT: {
2506 struct kvm_interrupt irq;
2509 if (copy_from_user(&irq, argp, sizeof irq))
2511 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2518 r = kvm_vcpu_ioctl_nmi(vcpu);
2524 case KVM_SET_CPUID: {
2525 struct kvm_cpuid __user *cpuid_arg = argp;
2526 struct kvm_cpuid cpuid;
2529 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2531 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2536 case KVM_SET_CPUID2: {
2537 struct kvm_cpuid2 __user *cpuid_arg = argp;
2538 struct kvm_cpuid2 cpuid;
2541 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2543 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
2544 cpuid_arg->entries);
2549 case KVM_GET_CPUID2: {
2550 struct kvm_cpuid2 __user *cpuid_arg = argp;
2551 struct kvm_cpuid2 cpuid;
2554 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2556 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
2557 cpuid_arg->entries);
2561 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2567 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2570 r = msr_io(vcpu, argp, do_set_msr, 0);
2572 case KVM_TPR_ACCESS_REPORTING: {
2573 struct kvm_tpr_access_ctl tac;
2576 if (copy_from_user(&tac, argp, sizeof tac))
2578 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2582 if (copy_to_user(argp, &tac, sizeof tac))
2587 case KVM_SET_VAPIC_ADDR: {
2588 struct kvm_vapic_addr va;
2591 if (!irqchip_in_kernel(vcpu->kvm))
2594 if (copy_from_user(&va, argp, sizeof va))
2597 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2600 case KVM_X86_SETUP_MCE: {
2604 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2606 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2609 case KVM_X86_SET_MCE: {
2610 struct kvm_x86_mce mce;
2613 if (copy_from_user(&mce, argp, sizeof mce))
2615 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2618 case KVM_GET_VCPU_EVENTS: {
2619 struct kvm_vcpu_events events;
2621 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2624 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2629 case KVM_SET_VCPU_EVENTS: {
2630 struct kvm_vcpu_events events;
2633 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2636 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2639 case KVM_GET_DEBUGREGS: {
2640 struct kvm_debugregs dbgregs;
2642 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2645 if (copy_to_user(argp, &dbgregs,
2646 sizeof(struct kvm_debugregs)))
2651 case KVM_SET_DEBUGREGS: {
2652 struct kvm_debugregs dbgregs;
2655 if (copy_from_user(&dbgregs, argp,
2656 sizeof(struct kvm_debugregs)))
2659 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2662 case KVM_GET_XSAVE: {
2663 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
2668 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
2671 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
2676 case KVM_SET_XSAVE: {
2677 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
2683 if (copy_from_user(u.xsave, argp, sizeof(struct kvm_xsave)))
2686 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
2689 case KVM_GET_XCRS: {
2690 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
2695 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
2698 if (copy_to_user(argp, u.xcrs,
2699 sizeof(struct kvm_xcrs)))
2704 case KVM_SET_XCRS: {
2705 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
2711 if (copy_from_user(u.xcrs, argp,
2712 sizeof(struct kvm_xcrs)))
2715 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
2726 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2730 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2732 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2736 static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2739 kvm->arch.ept_identity_map_addr = ident_addr;
2743 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2744 u32 kvm_nr_mmu_pages)
2746 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2749 mutex_lock(&kvm->slots_lock);
2750 spin_lock(&kvm->mmu_lock);
2752 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
2753 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
2755 spin_unlock(&kvm->mmu_lock);
2756 mutex_unlock(&kvm->slots_lock);
2760 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2762 return kvm->arch.n_alloc_mmu_pages;
2765 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2770 switch (chip->chip_id) {
2771 case KVM_IRQCHIP_PIC_MASTER:
2772 memcpy(&chip->chip.pic,
2773 &pic_irqchip(kvm)->pics[0],
2774 sizeof(struct kvm_pic_state));
2776 case KVM_IRQCHIP_PIC_SLAVE:
2777 memcpy(&chip->chip.pic,
2778 &pic_irqchip(kvm)->pics[1],
2779 sizeof(struct kvm_pic_state));
2781 case KVM_IRQCHIP_IOAPIC:
2782 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
2791 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2796 switch (chip->chip_id) {
2797 case KVM_IRQCHIP_PIC_MASTER:
2798 raw_spin_lock(&pic_irqchip(kvm)->lock);
2799 memcpy(&pic_irqchip(kvm)->pics[0],
2801 sizeof(struct kvm_pic_state));
2802 raw_spin_unlock(&pic_irqchip(kvm)->lock);
2804 case KVM_IRQCHIP_PIC_SLAVE:
2805 raw_spin_lock(&pic_irqchip(kvm)->lock);
2806 memcpy(&pic_irqchip(kvm)->pics[1],
2808 sizeof(struct kvm_pic_state));
2809 raw_spin_unlock(&pic_irqchip(kvm)->lock);
2811 case KVM_IRQCHIP_IOAPIC:
2812 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
2818 kvm_pic_update_irq(pic_irqchip(kvm));
2822 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2826 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2827 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
2828 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2832 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2836 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2837 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
2838 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2839 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2843 static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2847 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2848 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2849 sizeof(ps->channels));
2850 ps->flags = kvm->arch.vpit->pit_state.flags;
2851 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2855 static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2857 int r = 0, start = 0;
2858 u32 prev_legacy, cur_legacy;
2859 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2860 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2861 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2862 if (!prev_legacy && cur_legacy)
2864 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2865 sizeof(kvm->arch.vpit->pit_state.channels));
2866 kvm->arch.vpit->pit_state.flags = ps->flags;
2867 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
2868 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2872 static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2873 struct kvm_reinject_control *control)
2875 if (!kvm->arch.vpit)
2877 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2878 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
2879 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2884 * Get (and clear) the dirty memory log for a memory slot.
2886 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2887 struct kvm_dirty_log *log)
2890 struct kvm_memory_slot *memslot;
2892 unsigned long is_dirty = 0;
2894 mutex_lock(&kvm->slots_lock);
2897 if (log->slot >= KVM_MEMORY_SLOTS)
2900 memslot = &kvm->memslots->memslots[log->slot];
2902 if (!memslot->dirty_bitmap)
2905 n = kvm_dirty_bitmap_bytes(memslot);
2907 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2908 is_dirty = memslot->dirty_bitmap[i];
2910 /* If nothing is dirty, don't bother messing with page tables. */
2912 struct kvm_memslots *slots, *old_slots;
2913 unsigned long *dirty_bitmap;
2915 spin_lock(&kvm->mmu_lock);
2916 kvm_mmu_slot_remove_write_access(kvm, log->slot);
2917 spin_unlock(&kvm->mmu_lock);
2920 dirty_bitmap = vmalloc(n);
2923 memset(dirty_bitmap, 0, n);
2926 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2928 vfree(dirty_bitmap);
2931 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2932 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2934 old_slots = kvm->memslots;
2935 rcu_assign_pointer(kvm->memslots, slots);
2936 synchronize_srcu_expedited(&kvm->srcu);
2937 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2941 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) {
2942 vfree(dirty_bitmap);
2945 vfree(dirty_bitmap);
2948 if (clear_user(log->dirty_bitmap, n))
2954 mutex_unlock(&kvm->slots_lock);
2958 long kvm_arch_vm_ioctl(struct file *filp,
2959 unsigned int ioctl, unsigned long arg)
2961 struct kvm *kvm = filp->private_data;
2962 void __user *argp = (void __user *)arg;
2965 * This union makes it completely explicit to gcc-3.x
2966 * that these two variables' stack usage should be
2967 * combined, not added together.
2970 struct kvm_pit_state ps;
2971 struct kvm_pit_state2 ps2;
2972 struct kvm_pit_config pit_config;
2976 case KVM_SET_TSS_ADDR:
2977 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2981 case KVM_SET_IDENTITY_MAP_ADDR: {
2985 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2987 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2992 case KVM_SET_NR_MMU_PAGES:
2993 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2997 case KVM_GET_NR_MMU_PAGES:
2998 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3000 case KVM_CREATE_IRQCHIP: {
3001 struct kvm_pic *vpic;
3003 mutex_lock(&kvm->lock);
3006 goto create_irqchip_unlock;
3008 vpic = kvm_create_pic(kvm);
3010 r = kvm_ioapic_init(kvm);
3012 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3015 goto create_irqchip_unlock;
3018 goto create_irqchip_unlock;
3020 kvm->arch.vpic = vpic;
3022 r = kvm_setup_default_irq_routing(kvm);
3024 mutex_lock(&kvm->irq_lock);
3025 kvm_ioapic_destroy(kvm);
3026 kvm_destroy_pic(kvm);
3027 mutex_unlock(&kvm->irq_lock);
3029 create_irqchip_unlock:
3030 mutex_unlock(&kvm->lock);
3033 case KVM_CREATE_PIT:
3034 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3036 case KVM_CREATE_PIT2:
3038 if (copy_from_user(&u.pit_config, argp,
3039 sizeof(struct kvm_pit_config)))
3042 mutex_lock(&kvm->slots_lock);
3045 goto create_pit_unlock;
3047 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
3051 mutex_unlock(&kvm->slots_lock);
3053 case KVM_IRQ_LINE_STATUS:
3054 case KVM_IRQ_LINE: {
3055 struct kvm_irq_level irq_event;
3058 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3061 if (irqchip_in_kernel(kvm)) {
3063 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
3064 irq_event.irq, irq_event.level);
3065 if (ioctl == KVM_IRQ_LINE_STATUS) {
3067 irq_event.status = status;
3068 if (copy_to_user(argp, &irq_event,
3076 case KVM_GET_IRQCHIP: {
3077 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3078 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
3084 if (copy_from_user(chip, argp, sizeof *chip))
3085 goto get_irqchip_out;
3087 if (!irqchip_in_kernel(kvm))
3088 goto get_irqchip_out;
3089 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3091 goto get_irqchip_out;
3093 if (copy_to_user(argp, chip, sizeof *chip))
3094 goto get_irqchip_out;
3102 case KVM_SET_IRQCHIP: {
3103 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3104 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
3110 if (copy_from_user(chip, argp, sizeof *chip))
3111 goto set_irqchip_out;
3113 if (!irqchip_in_kernel(kvm))
3114 goto set_irqchip_out;
3115 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3117 goto set_irqchip_out;
3127 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
3130 if (!kvm->arch.vpit)
3132 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
3136 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
3143 if (copy_from_user(&u.ps, argp, sizeof u.ps))
3146 if (!kvm->arch.vpit)
3148 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
3154 case KVM_GET_PIT2: {
3156 if (!kvm->arch.vpit)
3158 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3162 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3167 case KVM_SET_PIT2: {
3169 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3172 if (!kvm->arch.vpit)
3174 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
3180 case KVM_REINJECT_CONTROL: {
3181 struct kvm_reinject_control control;
3183 if (copy_from_user(&control, argp, sizeof(control)))
3185 r = kvm_vm_ioctl_reinject(kvm, &control);
3191 case KVM_XEN_HVM_CONFIG: {
3193 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3194 sizeof(struct kvm_xen_hvm_config)))
3197 if (kvm->arch.xen_hvm_config.flags)
3202 case KVM_SET_CLOCK: {
3203 struct timespec now;
3204 struct kvm_clock_data user_ns;
3209 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3218 now_ns = timespec_to_ns(&now);
3219 delta = user_ns.clock - now_ns;
3220 kvm->arch.kvmclock_offset = delta;
3223 case KVM_GET_CLOCK: {
3224 struct timespec now;
3225 struct kvm_clock_data user_ns;
3229 now_ns = timespec_to_ns(&now);
3230 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
3234 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3247 static void kvm_init_msr_list(void)
3252 /* skip the first msrs in the list. KVM-specific */
3253 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
3254 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3257 msrs_to_save[j] = msrs_to_save[i];
3260 num_msrs_to_save = j;
3263 static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3266 if (vcpu->arch.apic &&
3267 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3270 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
3273 static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
3275 if (vcpu->arch.apic &&
3276 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3279 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
3282 static void kvm_set_segment(struct kvm_vcpu *vcpu,
3283 struct kvm_segment *var, int seg)
3285 kvm_x86_ops->set_segment(vcpu, var, seg);
3288 void kvm_get_segment(struct kvm_vcpu *vcpu,
3289 struct kvm_segment *var, int seg)
3291 kvm_x86_ops->get_segment(vcpu, var, seg);
3294 gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3296 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3297 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3300 gpa_t kvm_mmu_gva_to_gpa_fetch(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_FETCH_MASK;
3304 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3307 gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3309 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3310 access |= PFERR_WRITE_MASK;
3311 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3314 /* uses this to access any guest's mapped memory without checking CPL */
3315 gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3317 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3320 static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3321 struct kvm_vcpu *vcpu, u32 access,
3325 int r = X86EMUL_CONTINUE;
3328 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
3329 unsigned offset = addr & (PAGE_SIZE-1);
3330 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
3333 if (gpa == UNMAPPED_GVA) {
3334 r = X86EMUL_PROPAGATE_FAULT;
3337 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
3339 r = X86EMUL_IO_NEEDED;
3351 /* used for instruction fetching */
3352 static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3353 struct kvm_vcpu *vcpu, u32 *error)
3355 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3356 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3357 access | PFERR_FETCH_MASK, error);
3360 static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3361 struct kvm_vcpu *vcpu, u32 *error)
3363 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3364 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3368 static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3369 struct kvm_vcpu *vcpu, u32 *error)
3371 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3374 static int kvm_write_guest_virt_system(gva_t addr, void *val,
3376 struct kvm_vcpu *vcpu,
3380 int r = X86EMUL_CONTINUE;
3383 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr,
3384 PFERR_WRITE_MASK, error);
3385 unsigned offset = addr & (PAGE_SIZE-1);
3386 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3389 if (gpa == UNMAPPED_GVA) {
3390 r = X86EMUL_PROPAGATE_FAULT;
3393 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3395 r = X86EMUL_IO_NEEDED;
3407 static int emulator_read_emulated(unsigned long addr,
3410 unsigned int *error_code,
3411 struct kvm_vcpu *vcpu)
3415 if (vcpu->mmio_read_completed) {
3416 memcpy(val, vcpu->mmio_data, bytes);
3417 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3418 vcpu->mmio_phys_addr, *(u64 *)val);
3419 vcpu->mmio_read_completed = 0;
3420 return X86EMUL_CONTINUE;
3423 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, error_code);
3425 if (gpa == UNMAPPED_GVA)
3426 return X86EMUL_PROPAGATE_FAULT;
3428 /* For APIC access vmexit */
3429 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3432 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
3433 == X86EMUL_CONTINUE)
3434 return X86EMUL_CONTINUE;
3438 * Is this MMIO handled locally?
3440 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3441 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
3442 return X86EMUL_CONTINUE;
3445 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
3447 vcpu->mmio_needed = 1;
3448 vcpu->run->exit_reason = KVM_EXIT_MMIO;
3449 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
3450 vcpu->run->mmio.len = vcpu->mmio_size = bytes;
3451 vcpu->run->mmio.is_write = vcpu->mmio_is_write = 0;
3453 return X86EMUL_IO_NEEDED;
3456 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
3457 const void *val, int bytes)
3461 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3464 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
3468 static int emulator_write_emulated_onepage(unsigned long addr,
3471 unsigned int *error_code,
3472 struct kvm_vcpu *vcpu)
3476 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error_code);
3478 if (gpa == UNMAPPED_GVA)
3479 return X86EMUL_PROPAGATE_FAULT;
3481 /* For APIC access vmexit */
3482 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3485 if (emulator_write_phys(vcpu, gpa, val, bytes))
3486 return X86EMUL_CONTINUE;
3489 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
3491 * Is this MMIO handled locally?
3493 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
3494 return X86EMUL_CONTINUE;
3496 vcpu->mmio_needed = 1;
3497 vcpu->run->exit_reason = KVM_EXIT_MMIO;
3498 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
3499 vcpu->run->mmio.len = vcpu->mmio_size = bytes;
3500 vcpu->run->mmio.is_write = vcpu->mmio_is_write = 1;
3501 memcpy(vcpu->run->mmio.data, val, bytes);
3503 return X86EMUL_CONTINUE;
3506 int emulator_write_emulated(unsigned long addr,
3509 unsigned int *error_code,
3510 struct kvm_vcpu *vcpu)
3512 /* Crossing a page boundary? */
3513 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3516 now = -addr & ~PAGE_MASK;
3517 rc = emulator_write_emulated_onepage(addr, val, now, error_code,
3519 if (rc != X86EMUL_CONTINUE)
3525 return emulator_write_emulated_onepage(addr, val, bytes, error_code,
3529 #define CMPXCHG_TYPE(t, ptr, old, new) \
3530 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3532 #ifdef CONFIG_X86_64
3533 # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3535 # define CMPXCHG64(ptr, old, new) \
3536 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
3539 static int emulator_cmpxchg_emulated(unsigned long addr,
3543 unsigned int *error_code,
3544 struct kvm_vcpu *vcpu)
3551 /* guests cmpxchg8b have to be emulated atomically */
3552 if (bytes > 8 || (bytes & (bytes - 1)))
3555 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
3557 if (gpa == UNMAPPED_GVA ||
3558 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3561 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3564 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
3565 if (is_error_page(page)) {
3566 kvm_release_page_clean(page);
3570 kaddr = kmap_atomic(page, KM_USER0);
3571 kaddr += offset_in_page(gpa);
3574 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
3577 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
3580 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
3583 exchanged = CMPXCHG64(kaddr, old, new);
3588 kunmap_atomic(kaddr, KM_USER0);
3589 kvm_release_page_dirty(page);
3592 return X86EMUL_CMPXCHG_FAILED;
3594 kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1);
3596 return X86EMUL_CONTINUE;
3599 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
3601 return emulator_write_emulated(addr, new, bytes, error_code, vcpu);
3604 static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
3606 /* TODO: String I/O for in kernel device */
3609 if (vcpu->arch.pio.in)
3610 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
3611 vcpu->arch.pio.size, pd);
3613 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3614 vcpu->arch.pio.port, vcpu->arch.pio.size,
3620 static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
3621 unsigned int count, struct kvm_vcpu *vcpu)
3623 if (vcpu->arch.pio.count)
3626 trace_kvm_pio(1, port, size, 1);
3628 vcpu->arch.pio.port = port;
3629 vcpu->arch.pio.in = 1;
3630 vcpu->arch.pio.count = count;
3631 vcpu->arch.pio.size = size;
3633 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3635 memcpy(val, vcpu->arch.pio_data, size * count);
3636 vcpu->arch.pio.count = 0;
3640 vcpu->run->exit_reason = KVM_EXIT_IO;
3641 vcpu->run->io.direction = KVM_EXIT_IO_IN;
3642 vcpu->run->io.size = size;
3643 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3644 vcpu->run->io.count = count;
3645 vcpu->run->io.port = port;
3650 static int emulator_pio_out_emulated(int size, unsigned short port,
3651 const void *val, unsigned int count,
3652 struct kvm_vcpu *vcpu)
3654 trace_kvm_pio(0, port, size, 1);
3656 vcpu->arch.pio.port = port;
3657 vcpu->arch.pio.in = 0;
3658 vcpu->arch.pio.count = count;
3659 vcpu->arch.pio.size = size;
3661 memcpy(vcpu->arch.pio_data, val, size * count);
3663 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3664 vcpu->arch.pio.count = 0;
3668 vcpu->run->exit_reason = KVM_EXIT_IO;
3669 vcpu->run->io.direction = KVM_EXIT_IO_OUT;
3670 vcpu->run->io.size = size;
3671 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3672 vcpu->run->io.count = count;
3673 vcpu->run->io.port = port;
3678 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3680 return kvm_x86_ops->get_segment_base(vcpu, seg);
3683 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3685 kvm_mmu_invlpg(vcpu, address);
3686 return X86EMUL_CONTINUE;
3689 int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
3691 if (!need_emulate_wbinvd(vcpu))
3692 return X86EMUL_CONTINUE;
3694 if (kvm_x86_ops->has_wbinvd_exit()) {
3695 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
3696 wbinvd_ipi, NULL, 1);
3697 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
3700 return X86EMUL_CONTINUE;
3702 EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
3704 int emulate_clts(struct kvm_vcpu *vcpu)
3706 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
3707 kvm_x86_ops->fpu_activate(vcpu);
3708 return X86EMUL_CONTINUE;
3711 int emulator_get_dr(int dr, unsigned long *dest, struct kvm_vcpu *vcpu)
3713 return _kvm_get_dr(vcpu, dr, dest);
3716 int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu)
3719 return __kvm_set_dr(vcpu, dr, value);
3722 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3724 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3727 static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
3729 unsigned long value;
3733 value = kvm_read_cr0(vcpu);
3736 value = vcpu->arch.cr2;
3739 value = vcpu->arch.cr3;
3742 value = kvm_read_cr4(vcpu);
3745 value = kvm_get_cr8(vcpu);
3748 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3755 static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
3761 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
3764 vcpu->arch.cr2 = val;
3767 res = kvm_set_cr3(vcpu, val);
3770 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
3773 res = __kvm_set_cr8(vcpu, val & 0xfUL);
3776 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3783 static int emulator_get_cpl(struct kvm_vcpu *vcpu)
3785 return kvm_x86_ops->get_cpl(vcpu);
3788 static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
3790 kvm_x86_ops->get_gdt(vcpu, dt);
3793 static void emulator_get_idt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
3795 kvm_x86_ops->get_idt(vcpu, dt);
3798 static unsigned long emulator_get_cached_segment_base(int seg,
3799 struct kvm_vcpu *vcpu)
3801 return get_segment_base(vcpu, seg);
3804 static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg,
3805 struct kvm_vcpu *vcpu)
3807 struct kvm_segment var;
3809 kvm_get_segment(vcpu, &var, seg);
3816 set_desc_limit(desc, var.limit);
3817 set_desc_base(desc, (unsigned long)var.base);
3818 desc->type = var.type;
3820 desc->dpl = var.dpl;
3821 desc->p = var.present;
3822 desc->avl = var.avl;
3830 static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg,
3831 struct kvm_vcpu *vcpu)
3833 struct kvm_segment var;
3835 /* needed to preserve selector */
3836 kvm_get_segment(vcpu, &var, seg);
3838 var.base = get_desc_base(desc);
3839 var.limit = get_desc_limit(desc);
3841 var.limit = (var.limit << 12) | 0xfff;
3842 var.type = desc->type;
3843 var.present = desc->p;
3844 var.dpl = desc->dpl;
3849 var.avl = desc->avl;
3850 var.present = desc->p;
3851 var.unusable = !var.present;
3854 kvm_set_segment(vcpu, &var, seg);
3858 static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
3860 struct kvm_segment kvm_seg;
3862 kvm_get_segment(vcpu, &kvm_seg, seg);
3863 return kvm_seg.selector;
3866 static void emulator_set_segment_selector(u16 sel, int seg,
3867 struct kvm_vcpu *vcpu)
3869 struct kvm_segment kvm_seg;
3871 kvm_get_segment(vcpu, &kvm_seg, seg);
3872 kvm_seg.selector = sel;
3873 kvm_set_segment(vcpu, &kvm_seg, seg);
3876 static struct x86_emulate_ops emulate_ops = {
3877 .read_std = kvm_read_guest_virt_system,
3878 .write_std = kvm_write_guest_virt_system,
3879 .fetch = kvm_fetch_guest_virt,
3880 .read_emulated = emulator_read_emulated,
3881 .write_emulated = emulator_write_emulated,
3882 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3883 .pio_in_emulated = emulator_pio_in_emulated,
3884 .pio_out_emulated = emulator_pio_out_emulated,
3885 .get_cached_descriptor = emulator_get_cached_descriptor,
3886 .set_cached_descriptor = emulator_set_cached_descriptor,
3887 .get_segment_selector = emulator_get_segment_selector,
3888 .set_segment_selector = emulator_set_segment_selector,
3889 .get_cached_segment_base = emulator_get_cached_segment_base,
3890 .get_gdt = emulator_get_gdt,
3891 .get_idt = emulator_get_idt,
3892 .get_cr = emulator_get_cr,
3893 .set_cr = emulator_set_cr,
3894 .cpl = emulator_get_cpl,
3895 .get_dr = emulator_get_dr,
3896 .set_dr = emulator_set_dr,
3897 .set_msr = kvm_set_msr,
3898 .get_msr = kvm_get_msr,
3901 static void cache_all_regs(struct kvm_vcpu *vcpu)
3903 kvm_register_read(vcpu, VCPU_REGS_RAX);
3904 kvm_register_read(vcpu, VCPU_REGS_RSP);
3905 kvm_register_read(vcpu, VCPU_REGS_RIP);
3906 vcpu->arch.regs_dirty = ~0;
3909 static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
3911 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask);
3913 * an sti; sti; sequence only disable interrupts for the first
3914 * instruction. So, if the last instruction, be it emulated or
3915 * not, left the system with the INT_STI flag enabled, it
3916 * means that the last instruction is an sti. We should not
3917 * leave the flag on in this case. The same goes for mov ss
3919 if (!(int_shadow & mask))
3920 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
3923 static void inject_emulated_exception(struct kvm_vcpu *vcpu)
3925 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
3926 if (ctxt->exception == PF_VECTOR)
3927 kvm_inject_page_fault(vcpu, ctxt->cr2, ctxt->error_code);
3928 else if (ctxt->error_code_valid)
3929 kvm_queue_exception_e(vcpu, ctxt->exception, ctxt->error_code);
3931 kvm_queue_exception(vcpu, ctxt->exception);
3934 static int handle_emulation_failure(struct kvm_vcpu *vcpu)
3936 ++vcpu->stat.insn_emulation_fail;
3937 trace_kvm_emulate_insn_failed(vcpu);
3938 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3939 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3940 vcpu->run->internal.ndata = 0;
3941 kvm_queue_exception(vcpu, UD_VECTOR);
3942 return EMULATE_FAIL;
3945 static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
3953 * if emulation was due to access to shadowed page table
3954 * and it failed try to unshadow page and re-entetr the
3955 * guest to let CPU execute the instruction.
3957 if (kvm_mmu_unprotect_page_virt(vcpu, gva))
3960 gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);
3962 if (gpa == UNMAPPED_GVA)
3963 return true; /* let cpu generate fault */
3965 if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT)))
3971 int emulate_instruction(struct kvm_vcpu *vcpu,
3977 struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
3979 kvm_clear_exception_queue(vcpu);
3980 vcpu->arch.mmio_fault_cr2 = cr2;
3982 * TODO: fix emulate.c to use guest_read/write_register
3983 * instead of direct ->regs accesses, can save hundred cycles
3984 * on Intel for instructions that don't read/change RSP, for
3987 cache_all_regs(vcpu);
3989 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
3991 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3993 vcpu->arch.emulate_ctxt.vcpu = vcpu;
3994 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
3995 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
3996 vcpu->arch.emulate_ctxt.mode =
3997 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
3998 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
3999 ? X86EMUL_MODE_VM86 : cs_l
4000 ? X86EMUL_MODE_PROT64 : cs_db
4001 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
4002 memset(c, 0, sizeof(struct decode_cache));
4003 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
4004 vcpu->arch.emulate_ctxt.interruptibility = 0;
4005 vcpu->arch.emulate_ctxt.exception = -1;
4006 vcpu->arch.emulate_ctxt.perm_ok = false;
4008 r = x86_decode_insn(&vcpu->arch.emulate_ctxt);
4009 trace_kvm_emulate_insn_start(vcpu);
4011 /* Only allow emulation of specific instructions on #UD
4012 * (namely VMMCALL, sysenter, sysexit, syscall)*/
4013 if (emulation_type & EMULTYPE_TRAP_UD) {
4015 return EMULATE_FAIL;
4017 case 0x01: /* VMMCALL */
4018 if (c->modrm_mod != 3 || c->modrm_rm != 1)
4019 return EMULATE_FAIL;
4021 case 0x34: /* sysenter */
4022 case 0x35: /* sysexit */
4023 if (c->modrm_mod != 0 || c->modrm_rm != 0)
4024 return EMULATE_FAIL;
4026 case 0x05: /* syscall */
4027 if (c->modrm_mod != 0 || c->modrm_rm != 0)
4028 return EMULATE_FAIL;
4031 return EMULATE_FAIL;
4034 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
4035 return EMULATE_FAIL;
4038 ++vcpu->stat.insn_emulation;
4040 if (reexecute_instruction(vcpu, cr2))
4041 return EMULATE_DONE;
4042 if (emulation_type & EMULTYPE_SKIP)
4043 return EMULATE_FAIL;
4044 return handle_emulation_failure(vcpu);
4048 if (emulation_type & EMULTYPE_SKIP) {
4049 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
4050 return EMULATE_DONE;
4053 /* this is needed for vmware backdor interface to work since it
4054 changes registers values during IO operation */
4055 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
4058 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);
4060 if (r) { /* emulation failed */
4061 if (reexecute_instruction(vcpu, cr2))
4062 return EMULATE_DONE;
4064 return handle_emulation_failure(vcpu);
4069 if (vcpu->arch.emulate_ctxt.exception >= 0)
4070 inject_emulated_exception(vcpu);
4071 else if (vcpu->arch.pio.count) {
4072 if (!vcpu->arch.pio.in)
4073 vcpu->arch.pio.count = 0;
4074 r = EMULATE_DO_MMIO;
4075 } else if (vcpu->mmio_needed) {
4076 if (vcpu->mmio_is_write)
4077 vcpu->mmio_needed = 0;
4078 r = EMULATE_DO_MMIO;
4079 } else if (vcpu->arch.emulate_ctxt.restart)
4082 toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility);
4083 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
4084 memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
4085 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
4089 EXPORT_SYMBOL_GPL(emulate_instruction);
4091 int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
4093 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
4094 int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
4095 /* do not return to emulator after return from userspace */
4096 vcpu->arch.pio.count = 0;
4099 EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
4101 static void bounce_off(void *info)
4106 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
4109 struct cpufreq_freqs *freq = data;
4111 struct kvm_vcpu *vcpu;
4112 int i, send_ipi = 0;
4114 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
4116 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
4118 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
4120 spin_lock(&kvm_lock);
4121 list_for_each_entry(kvm, &vm_list, vm_list) {
4122 kvm_for_each_vcpu(i, vcpu, kvm) {
4123 if (vcpu->cpu != freq->cpu)
4125 if (!kvm_request_guest_time_update(vcpu))
4127 if (vcpu->cpu != smp_processor_id())
4131 spin_unlock(&kvm_lock);
4133 if (freq->old < freq->new && send_ipi) {
4135 * We upscale the frequency. Must make the guest
4136 * doesn't see old kvmclock values while running with
4137 * the new frequency, otherwise we risk the guest sees
4138 * time go backwards.
4140 * In case we update the frequency for another cpu
4141 * (which might be in guest context) send an interrupt
4142 * to kick the cpu out of guest context. Next time
4143 * guest context is entered kvmclock will be updated,
4144 * so the guest will not see stale values.
4146 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
4151 static struct notifier_block kvmclock_cpufreq_notifier_block = {
4152 .notifier_call = kvmclock_cpufreq_notifier
4155 static void kvm_timer_init(void)
4159 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
4160 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
4161 CPUFREQ_TRANSITION_NOTIFIER);
4162 for_each_online_cpu(cpu) {
4163 unsigned long khz = cpufreq_get(cpu);
4166 per_cpu(cpu_tsc_khz, cpu) = khz;
4169 for_each_possible_cpu(cpu)
4170 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
4174 static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
4176 static int kvm_is_in_guest(void)
4178 return percpu_read(current_vcpu) != NULL;
4181 static int kvm_is_user_mode(void)
4185 if (percpu_read(current_vcpu))
4186 user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
4188 return user_mode != 0;
4191 static unsigned long kvm_get_guest_ip(void)
4193 unsigned long ip = 0;
4195 if (percpu_read(current_vcpu))
4196 ip = kvm_rip_read(percpu_read(current_vcpu));
4201 static struct perf_guest_info_callbacks kvm_guest_cbs = {
4202 .is_in_guest = kvm_is_in_guest,
4203 .is_user_mode = kvm_is_user_mode,
4204 .get_guest_ip = kvm_get_guest_ip,
4207 void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
4209 percpu_write(current_vcpu, vcpu);
4211 EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
4213 void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
4215 percpu_write(current_vcpu, NULL);
4217 EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
4219 int kvm_arch_init(void *opaque)
4222 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
4225 printk(KERN_ERR "kvm: already loaded the other module\n");
4230 if (!ops->cpu_has_kvm_support()) {
4231 printk(KERN_ERR "kvm: no hardware support\n");
4235 if (ops->disabled_by_bios()) {
4236 printk(KERN_ERR "kvm: disabled by bios\n");
4241 r = kvm_mmu_module_init();
4245 kvm_init_msr_list();
4248 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
4249 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
4250 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
4251 PT_DIRTY_MASK, PT64_NX_MASK, 0);
4255 perf_register_guest_info_callbacks(&kvm_guest_cbs);
4258 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
4266 void kvm_arch_exit(void)
4268 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
4270 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
4271 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
4272 CPUFREQ_TRANSITION_NOTIFIER);
4274 kvm_mmu_module_exit();
4277 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
4279 ++vcpu->stat.halt_exits;
4280 if (irqchip_in_kernel(vcpu->kvm)) {
4281 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
4284 vcpu->run->exit_reason = KVM_EXIT_HLT;
4288 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
4290 static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
4293 if (is_long_mode(vcpu))
4296 return a0 | ((gpa_t)a1 << 32);
4299 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
4301 u64 param, ingpa, outgpa, ret;
4302 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
4303 bool fast, longmode;
4307 * hypercall generates UD from non zero cpl and real mode
4310 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
4311 kvm_queue_exception(vcpu, UD_VECTOR);
4315 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4316 longmode = is_long_mode(vcpu) && cs_l == 1;
4319 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
4320 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
4321 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
4322 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
4323 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
4324 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
4326 #ifdef CONFIG_X86_64
4328 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
4329 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
4330 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
4334 code = param & 0xffff;
4335 fast = (param >> 16) & 0x1;
4336 rep_cnt = (param >> 32) & 0xfff;
4337 rep_idx = (param >> 48) & 0xfff;
4339 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
4342 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
4343 kvm_vcpu_on_spin(vcpu);
4346 res = HV_STATUS_INVALID_HYPERCALL_CODE;
4350 ret = res | (((u64)rep_done & 0xfff) << 32);
4352 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
4354 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
4355 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
4361 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
4363 unsigned long nr, a0, a1, a2, a3, ret;
4366 if (kvm_hv_hypercall_enabled(vcpu->kvm))
4367 return kvm_hv_hypercall(vcpu);
4369 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
4370 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
4371 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
4372 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
4373 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
4375 trace_kvm_hypercall(nr, a0, a1, a2, a3);
4377 if (!is_long_mode(vcpu)) {
4385 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
4391 case KVM_HC_VAPIC_POLL_IRQ:
4395 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
4402 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
4403 ++vcpu->stat.hypercalls;
4406 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
4408 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
4410 char instruction[3];
4411 unsigned long rip = kvm_rip_read(vcpu);
4414 * Blow out the MMU to ensure that no other VCPU has an active mapping
4415 * to ensure that the updated hypercall appears atomically across all
4418 kvm_mmu_zap_all(vcpu->kvm);
4420 kvm_x86_ops->patch_hypercall(vcpu, instruction);
4422 return emulator_write_emulated(rip, instruction, 3, NULL, vcpu);
4425 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4427 struct desc_ptr dt = { limit, base };
4429 kvm_x86_ops->set_gdt(vcpu, &dt);
4432 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4434 struct desc_ptr dt = { limit, base };
4436 kvm_x86_ops->set_idt(vcpu, &dt);
4439 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4441 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4442 int j, nent = vcpu->arch.cpuid_nent;
4444 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4445 /* when no next entry is found, the current entry[i] is reselected */
4446 for (j = i + 1; ; j = (j + 1) % nent) {
4447 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
4448 if (ej->function == e->function) {
4449 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4453 return 0; /* silence gcc, even though control never reaches here */
4456 /* find an entry with matching function, matching index (if needed), and that
4457 * should be read next (if it's stateful) */
4458 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4459 u32 function, u32 index)
4461 if (e->function != function)
4463 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4465 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
4466 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
4471 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4472 u32 function, u32 index)
4475 struct kvm_cpuid_entry2 *best = NULL;
4477 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
4478 struct kvm_cpuid_entry2 *e;
4480 e = &vcpu->arch.cpuid_entries[i];
4481 if (is_matching_cpuid_entry(e, function, index)) {
4482 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4483 move_to_next_stateful_cpuid_entry(vcpu, i);
4488 * Both basic or both extended?
4490 if (((e->function ^ function) & 0x80000000) == 0)
4491 if (!best || e->function > best->function)
4496 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
4498 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4500 struct kvm_cpuid_entry2 *best;
4502 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
4503 if (!best || best->eax < 0x80000008)
4505 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4507 return best->eax & 0xff;
4512 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4514 u32 function, index;
4515 struct kvm_cpuid_entry2 *best;
4517 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4518 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4519 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4520 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4521 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4522 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4523 best = kvm_find_cpuid_entry(vcpu, function, index);
4525 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4526 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4527 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4528 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
4530 kvm_x86_ops->skip_emulated_instruction(vcpu);
4531 trace_kvm_cpuid(function,
4532 kvm_register_read(vcpu, VCPU_REGS_RAX),
4533 kvm_register_read(vcpu, VCPU_REGS_RBX),
4534 kvm_register_read(vcpu, VCPU_REGS_RCX),
4535 kvm_register_read(vcpu, VCPU_REGS_RDX));
4537 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
4540 * Check if userspace requested an interrupt window, and that the
4541 * interrupt window is open.
4543 * No need to exit to userspace if we already have an interrupt queued.
4545 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
4547 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
4548 vcpu->run->request_interrupt_window &&
4549 kvm_arch_interrupt_allowed(vcpu));
4552 static void post_kvm_run_save(struct kvm_vcpu *vcpu)
4554 struct kvm_run *kvm_run = vcpu->run;
4556 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
4557 kvm_run->cr8 = kvm_get_cr8(vcpu);
4558 kvm_run->apic_base = kvm_get_apic_base(vcpu);
4559 if (irqchip_in_kernel(vcpu->kvm))
4560 kvm_run->ready_for_interrupt_injection = 1;
4562 kvm_run->ready_for_interrupt_injection =
4563 kvm_arch_interrupt_allowed(vcpu) &&
4564 !kvm_cpu_has_interrupt(vcpu) &&
4565 !kvm_event_needs_reinjection(vcpu);
4568 static void vapic_enter(struct kvm_vcpu *vcpu)
4570 struct kvm_lapic *apic = vcpu->arch.apic;
4573 if (!apic || !apic->vapic_addr)
4576 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
4578 vcpu->arch.apic->vapic_page = page;
4581 static void vapic_exit(struct kvm_vcpu *vcpu)
4583 struct kvm_lapic *apic = vcpu->arch.apic;
4586 if (!apic || !apic->vapic_addr)
4589 idx = srcu_read_lock(&vcpu->kvm->srcu);
4590 kvm_release_page_dirty(apic->vapic_page);
4591 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
4592 srcu_read_unlock(&vcpu->kvm->srcu, idx);
4595 static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4599 if (!kvm_x86_ops->update_cr8_intercept)
4602 if (!vcpu->arch.apic)
4605 if (!vcpu->arch.apic->vapic_addr)
4606 max_irr = kvm_lapic_find_highest_irr(vcpu);
4613 tpr = kvm_lapic_get_cr8(vcpu);
4615 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4618 static void inject_pending_event(struct kvm_vcpu *vcpu)
4620 /* try to reinject previous events if any */
4621 if (vcpu->arch.exception.pending) {
4622 trace_kvm_inj_exception(vcpu->arch.exception.nr,
4623 vcpu->arch.exception.has_error_code,
4624 vcpu->arch.exception.error_code);
4625 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4626 vcpu->arch.exception.has_error_code,
4627 vcpu->arch.exception.error_code,
4628 vcpu->arch.exception.reinject);
4632 if (vcpu->arch.nmi_injected) {
4633 kvm_x86_ops->set_nmi(vcpu);
4637 if (vcpu->arch.interrupt.pending) {
4638 kvm_x86_ops->set_irq(vcpu);
4642 /* try to inject new event if pending */
4643 if (vcpu->arch.nmi_pending) {
4644 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4645 vcpu->arch.nmi_pending = false;
4646 vcpu->arch.nmi_injected = true;
4647 kvm_x86_ops->set_nmi(vcpu);
4649 } else if (kvm_cpu_has_interrupt(vcpu)) {
4650 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
4651 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4653 kvm_x86_ops->set_irq(vcpu);
4658 static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
4660 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
4661 !vcpu->guest_xcr0_loaded) {
4662 /* kvm_set_xcr() also depends on this */
4663 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
4664 vcpu->guest_xcr0_loaded = 1;
4668 static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
4670 if (vcpu->guest_xcr0_loaded) {
4671 if (vcpu->arch.xcr0 != host_xcr0)
4672 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
4673 vcpu->guest_xcr0_loaded = 0;
4677 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
4680 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
4681 vcpu->run->request_interrupt_window;
4683 if (vcpu->requests) {
4684 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
4685 kvm_mmu_unload(vcpu);
4686 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
4687 __kvm_migrate_timers(vcpu);
4688 if (kvm_check_request(KVM_REQ_KVMCLOCK_UPDATE, vcpu))
4689 kvm_write_guest_time(vcpu);
4690 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
4691 kvm_mmu_sync_roots(vcpu);
4692 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
4693 kvm_x86_ops->tlb_flush(vcpu);
4694 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
4695 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
4699 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
4700 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
4704 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
4705 vcpu->fpu_active = 0;
4706 kvm_x86_ops->fpu_deactivate(vcpu);
4710 r = kvm_mmu_reload(vcpu);
4716 kvm_x86_ops->prepare_guest_switch(vcpu);
4717 if (vcpu->fpu_active)
4718 kvm_load_guest_fpu(vcpu);
4719 kvm_load_guest_xcr0(vcpu);
4721 atomic_set(&vcpu->guest_mode, 1);
4724 local_irq_disable();
4726 if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
4727 || need_resched() || signal_pending(current)) {
4728 atomic_set(&vcpu->guest_mode, 0);
4736 inject_pending_event(vcpu);
4738 /* enable NMI/IRQ window open exits if needed */
4739 if (vcpu->arch.nmi_pending)
4740 kvm_x86_ops->enable_nmi_window(vcpu);
4741 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4742 kvm_x86_ops->enable_irq_window(vcpu);
4744 if (kvm_lapic_enabled(vcpu)) {
4745 update_cr8_intercept(vcpu);
4746 kvm_lapic_sync_to_vapic(vcpu);
4749 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4753 if (unlikely(vcpu->arch.switch_db_regs)) {
4755 set_debugreg(vcpu->arch.eff_db[0], 0);
4756 set_debugreg(vcpu->arch.eff_db[1], 1);
4757 set_debugreg(vcpu->arch.eff_db[2], 2);
4758 set_debugreg(vcpu->arch.eff_db[3], 3);
4761 trace_kvm_entry(vcpu->vcpu_id);
4762 kvm_x86_ops->run(vcpu);
4765 * If the guest has used debug registers, at least dr7
4766 * will be disabled while returning to the host.
4767 * If we don't have active breakpoints in the host, we don't
4768 * care about the messed up debug address registers. But if
4769 * we have some of them active, restore the old state.
4771 if (hw_breakpoint_active())
4772 hw_breakpoint_restore();
4774 atomic_set(&vcpu->guest_mode, 0);
4781 * We must have an instruction between local_irq_enable() and
4782 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4783 * the interrupt shadow. The stat.exits increment will do nicely.
4784 * But we need to prevent reordering, hence this barrier():
4792 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4795 * Profile KVM exit RIPs:
4797 if (unlikely(prof_on == KVM_PROFILING)) {
4798 unsigned long rip = kvm_rip_read(vcpu);
4799 profile_hit(KVM_PROFILING, (void *)rip);
4803 kvm_lapic_sync_from_vapic(vcpu);
4805 r = kvm_x86_ops->handle_exit(vcpu);
4811 static int __vcpu_run(struct kvm_vcpu *vcpu)
4814 struct kvm *kvm = vcpu->kvm;
4816 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
4817 pr_debug("vcpu %d received sipi with vector # %x\n",
4818 vcpu->vcpu_id, vcpu->arch.sipi_vector);
4819 kvm_lapic_reset(vcpu);
4820 r = kvm_arch_vcpu_reset(vcpu);
4823 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4826 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
4831 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
4832 r = vcpu_enter_guest(vcpu);
4834 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
4835 kvm_vcpu_block(vcpu);
4836 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
4837 if (kvm_check_request(KVM_REQ_UNHALT, vcpu))
4839 switch(vcpu->arch.mp_state) {
4840 case KVM_MP_STATE_HALTED:
4841 vcpu->arch.mp_state =
4842 KVM_MP_STATE_RUNNABLE;
4843 case KVM_MP_STATE_RUNNABLE:
4845 case KVM_MP_STATE_SIPI_RECEIVED:
4856 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4857 if (kvm_cpu_has_pending_timer(vcpu))
4858 kvm_inject_pending_timer_irqs(vcpu);
4860 if (dm_request_for_irq_injection(vcpu)) {
4862 vcpu->run->exit_reason = KVM_EXIT_INTR;
4863 ++vcpu->stat.request_irq_exits;
4865 if (signal_pending(current)) {
4867 vcpu->run->exit_reason = KVM_EXIT_INTR;
4868 ++vcpu->stat.signal_exits;
4870 if (need_resched()) {
4871 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
4873 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
4877 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
4884 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4889 if (vcpu->sigset_active)
4890 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4892 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4893 kvm_vcpu_block(vcpu);
4894 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
4899 /* re-sync apic's tpr */
4900 if (!irqchip_in_kernel(vcpu->kvm))
4901 kvm_set_cr8(vcpu, kvm_run->cr8);
4903 if (vcpu->arch.pio.count || vcpu->mmio_needed ||
4904 vcpu->arch.emulate_ctxt.restart) {
4905 if (vcpu->mmio_needed) {
4906 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4907 vcpu->mmio_read_completed = 1;
4908 vcpu->mmio_needed = 0;
4910 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4911 r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE);
4912 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4913 if (r != EMULATE_DONE) {
4918 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4919 kvm_register_write(vcpu, VCPU_REGS_RAX,
4920 kvm_run->hypercall.ret);
4922 r = __vcpu_run(vcpu);
4925 post_kvm_run_save(vcpu);
4926 if (vcpu->sigset_active)
4927 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4932 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4934 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4935 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4936 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4937 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4938 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4939 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4940 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4941 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4942 #ifdef CONFIG_X86_64
4943 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4944 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4945 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4946 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4947 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4948 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4949 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4950 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
4953 regs->rip = kvm_rip_read(vcpu);
4954 regs->rflags = kvm_get_rflags(vcpu);
4959 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4961 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4962 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4963 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4964 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4965 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4966 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4967 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4968 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
4969 #ifdef CONFIG_X86_64
4970 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4971 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4972 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4973 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4974 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4975 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4976 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4977 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
4980 kvm_rip_write(vcpu, regs->rip);
4981 kvm_set_rflags(vcpu, regs->rflags);
4983 vcpu->arch.exception.pending = false;
4988 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4990 struct kvm_segment cs;
4992 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
4996 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4998 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4999 struct kvm_sregs *sregs)
5003 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5004 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5005 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5006 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5007 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5008 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
5010 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5011 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
5013 kvm_x86_ops->get_idt(vcpu, &dt);
5014 sregs->idt.limit = dt.size;
5015 sregs->idt.base = dt.address;
5016 kvm_x86_ops->get_gdt(vcpu, &dt);
5017 sregs->gdt.limit = dt.size;
5018 sregs->gdt.base = dt.address;
5020 sregs->cr0 = kvm_read_cr0(vcpu);
5021 sregs->cr2 = vcpu->arch.cr2;
5022 sregs->cr3 = vcpu->arch.cr3;
5023 sregs->cr4 = kvm_read_cr4(vcpu);
5024 sregs->cr8 = kvm_get_cr8(vcpu);
5025 sregs->efer = vcpu->arch.efer;
5026 sregs->apic_base = kvm_get_apic_base(vcpu);
5028 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
5030 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
5031 set_bit(vcpu->arch.interrupt.nr,
5032 (unsigned long *)sregs->interrupt_bitmap);
5037 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
5038 struct kvm_mp_state *mp_state)
5040 mp_state->mp_state = vcpu->arch.mp_state;
5044 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
5045 struct kvm_mp_state *mp_state)
5047 vcpu->arch.mp_state = mp_state->mp_state;
5051 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
5052 bool has_error_code, u32 error_code)
5054 struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
5055 int cs_db, cs_l, ret;
5056 cache_all_regs(vcpu);
5058 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
5060 vcpu->arch.emulate_ctxt.vcpu = vcpu;
5061 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
5062 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
5063 vcpu->arch.emulate_ctxt.mode =
5064 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
5065 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
5066 ? X86EMUL_MODE_VM86 : cs_l
5067 ? X86EMUL_MODE_PROT64 : cs_db
5068 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
5069 memset(c, 0, sizeof(struct decode_cache));
5070 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
5072 ret = emulator_task_switch(&vcpu->arch.emulate_ctxt,
5073 tss_selector, reason, has_error_code,
5077 return EMULATE_FAIL;
5079 memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
5080 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
5081 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
5082 return EMULATE_DONE;
5084 EXPORT_SYMBOL_GPL(kvm_task_switch);
5086 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
5087 struct kvm_sregs *sregs)
5089 int mmu_reset_needed = 0;
5090 int pending_vec, max_bits;
5093 dt.size = sregs->idt.limit;
5094 dt.address = sregs->idt.base;
5095 kvm_x86_ops->set_idt(vcpu, &dt);
5096 dt.size = sregs->gdt.limit;
5097 dt.address = sregs->gdt.base;
5098 kvm_x86_ops->set_gdt(vcpu, &dt);
5100 vcpu->arch.cr2 = sregs->cr2;
5101 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
5102 vcpu->arch.cr3 = sregs->cr3;
5104 kvm_set_cr8(vcpu, sregs->cr8);
5106 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
5107 kvm_x86_ops->set_efer(vcpu, sregs->efer);
5108 kvm_set_apic_base(vcpu, sregs->apic_base);
5110 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
5111 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
5112 vcpu->arch.cr0 = sregs->cr0;
5114 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
5115 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
5116 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
5117 load_pdptrs(vcpu, vcpu->arch.cr3);
5118 mmu_reset_needed = 1;
5121 if (mmu_reset_needed)
5122 kvm_mmu_reset_context(vcpu);
5124 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
5125 pending_vec = find_first_bit(
5126 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
5127 if (pending_vec < max_bits) {
5128 kvm_queue_interrupt(vcpu, pending_vec, false);
5129 pr_debug("Set back pending irq %d\n", pending_vec);
5130 if (irqchip_in_kernel(vcpu->kvm))
5131 kvm_pic_clear_isr_ack(vcpu->kvm);
5134 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5135 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5136 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5137 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5138 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5139 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
5141 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5142 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
5144 update_cr8_intercept(vcpu);
5146 /* Older userspace won't unhalt the vcpu on reset. */
5147 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
5148 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
5150 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5155 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5156 struct kvm_guest_debug *dbg)
5158 unsigned long rflags;
5161 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5163 if (vcpu->arch.exception.pending)
5165 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5166 kvm_queue_exception(vcpu, DB_VECTOR);
5168 kvm_queue_exception(vcpu, BP_VECTOR);
5172 * Read rflags as long as potentially injected trace flags are still
5175 rflags = kvm_get_rflags(vcpu);
5177 vcpu->guest_debug = dbg->control;
5178 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5179 vcpu->guest_debug = 0;
5181 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
5182 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5183 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5184 vcpu->arch.switch_db_regs =
5185 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5187 for (i = 0; i < KVM_NR_DB_REGS; i++)
5188 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5189 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5192 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5193 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
5194 get_segment_base(vcpu, VCPU_SREG_CS);
5197 * Trigger an rflags update that will inject or remove the trace
5200 kvm_set_rflags(vcpu, rflags);
5202 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5212 * Translate a guest virtual address to a guest physical address.
5214 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5215 struct kvm_translation *tr)
5217 unsigned long vaddr = tr->linear_address;
5221 idx = srcu_read_lock(&vcpu->kvm->srcu);
5222 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
5223 srcu_read_unlock(&vcpu->kvm->srcu, idx);
5224 tr->physical_address = gpa;
5225 tr->valid = gpa != UNMAPPED_GVA;
5232 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5234 struct i387_fxsave_struct *fxsave =
5235 &vcpu->arch.guest_fpu.state->fxsave;
5237 memcpy(fpu->fpr, fxsave->st_space, 128);
5238 fpu->fcw = fxsave->cwd;
5239 fpu->fsw = fxsave->swd;
5240 fpu->ftwx = fxsave->twd;
5241 fpu->last_opcode = fxsave->fop;
5242 fpu->last_ip = fxsave->rip;
5243 fpu->last_dp = fxsave->rdp;
5244 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5249 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5251 struct i387_fxsave_struct *fxsave =
5252 &vcpu->arch.guest_fpu.state->fxsave;
5254 memcpy(fxsave->st_space, fpu->fpr, 128);
5255 fxsave->cwd = fpu->fcw;
5256 fxsave->swd = fpu->fsw;
5257 fxsave->twd = fpu->ftwx;
5258 fxsave->fop = fpu->last_opcode;
5259 fxsave->rip = fpu->last_ip;
5260 fxsave->rdp = fpu->last_dp;
5261 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5266 int fx_init(struct kvm_vcpu *vcpu)
5270 err = fpu_alloc(&vcpu->arch.guest_fpu);
5274 fpu_finit(&vcpu->arch.guest_fpu);
5277 * Ensure guest xcr0 is valid for loading
5279 vcpu->arch.xcr0 = XSTATE_FP;
5281 vcpu->arch.cr0 |= X86_CR0_ET;
5285 EXPORT_SYMBOL_GPL(fx_init);
5287 static void fx_free(struct kvm_vcpu *vcpu)
5289 fpu_free(&vcpu->arch.guest_fpu);
5292 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5294 if (vcpu->guest_fpu_loaded)
5298 * Restore all possible states in the guest,
5299 * and assume host would use all available bits.
5300 * Guest xcr0 would be loaded later.
5302 kvm_put_guest_xcr0(vcpu);
5303 vcpu->guest_fpu_loaded = 1;
5304 unlazy_fpu(current);
5305 fpu_restore_checking(&vcpu->arch.guest_fpu);
5309 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5311 kvm_put_guest_xcr0(vcpu);
5313 if (!vcpu->guest_fpu_loaded)
5316 vcpu->guest_fpu_loaded = 0;
5317 fpu_save_init(&vcpu->arch.guest_fpu);
5318 ++vcpu->stat.fpu_reload;
5319 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
5323 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5325 if (vcpu->arch.time_page) {
5326 kvm_release_page_dirty(vcpu->arch.time_page);
5327 vcpu->arch.time_page = NULL;
5330 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
5332 kvm_x86_ops->vcpu_free(vcpu);
5335 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5338 return kvm_x86_ops->vcpu_create(kvm, id);
5341 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5345 vcpu->arch.mtrr_state.have_fixed = 1;
5347 r = kvm_arch_vcpu_reset(vcpu);
5349 r = kvm_mmu_setup(vcpu);
5356 kvm_x86_ops->vcpu_free(vcpu);
5360 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
5363 kvm_mmu_unload(vcpu);
5367 kvm_x86_ops->vcpu_free(vcpu);
5370 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5372 vcpu->arch.nmi_pending = false;
5373 vcpu->arch.nmi_injected = false;
5375 vcpu->arch.switch_db_regs = 0;
5376 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5377 vcpu->arch.dr6 = DR6_FIXED_1;
5378 vcpu->arch.dr7 = DR7_FIXED_1;
5380 return kvm_x86_ops->vcpu_reset(vcpu);
5383 int kvm_arch_hardware_enable(void *garbage)
5386 * Since this may be called from a hotplug notifcation,
5387 * we can't get the CPU frequency directly.
5389 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5390 int cpu = raw_smp_processor_id();
5391 per_cpu(cpu_tsc_khz, cpu) = 0;
5394 kvm_shared_msr_cpu_online();
5396 return kvm_x86_ops->hardware_enable(garbage);
5399 void kvm_arch_hardware_disable(void *garbage)
5401 kvm_x86_ops->hardware_disable(garbage);
5402 drop_user_return_notifiers(garbage);
5405 int kvm_arch_hardware_setup(void)
5407 return kvm_x86_ops->hardware_setup();
5410 void kvm_arch_hardware_unsetup(void)
5412 kvm_x86_ops->hardware_unsetup();
5415 void kvm_arch_check_processor_compat(void *rtn)
5417 kvm_x86_ops->check_processor_compatibility(rtn);
5420 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5426 BUG_ON(vcpu->kvm == NULL);
5429 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
5430 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
5431 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
5432 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5434 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
5436 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5441 vcpu->arch.pio_data = page_address(page);
5443 r = kvm_mmu_create(vcpu);
5445 goto fail_free_pio_data;
5447 if (irqchip_in_kernel(kvm)) {
5448 r = kvm_create_lapic(vcpu);
5450 goto fail_mmu_destroy;
5453 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5455 if (!vcpu->arch.mce_banks) {
5457 goto fail_free_lapic;
5459 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5461 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL))
5462 goto fail_free_mce_banks;
5465 fail_free_mce_banks:
5466 kfree(vcpu->arch.mce_banks);
5468 kvm_free_lapic(vcpu);
5470 kvm_mmu_destroy(vcpu);
5472 free_page((unsigned long)vcpu->arch.pio_data);
5477 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5481 kfree(vcpu->arch.mce_banks);
5482 kvm_free_lapic(vcpu);
5483 idx = srcu_read_lock(&vcpu->kvm->srcu);
5484 kvm_mmu_destroy(vcpu);
5485 srcu_read_unlock(&vcpu->kvm->srcu, idx);
5486 free_page((unsigned long)vcpu->arch.pio_data);
5489 struct kvm *kvm_arch_create_vm(void)
5491 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5494 return ERR_PTR(-ENOMEM);
5496 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
5497 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
5499 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5500 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5502 rdtscll(kvm->arch.vm_init_tsc);
5507 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5510 kvm_mmu_unload(vcpu);
5514 static void kvm_free_vcpus(struct kvm *kvm)
5517 struct kvm_vcpu *vcpu;
5520 * Unpin any mmu pages first.
5522 kvm_for_each_vcpu(i, vcpu, kvm)
5523 kvm_unload_vcpu_mmu(vcpu);
5524 kvm_for_each_vcpu(i, vcpu, kvm)
5525 kvm_arch_vcpu_free(vcpu);
5527 mutex_lock(&kvm->lock);
5528 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5529 kvm->vcpus[i] = NULL;
5531 atomic_set(&kvm->online_vcpus, 0);
5532 mutex_unlock(&kvm->lock);
5535 void kvm_arch_sync_events(struct kvm *kvm)
5537 kvm_free_all_assigned_devices(kvm);
5541 void kvm_arch_destroy_vm(struct kvm *kvm)
5543 kvm_iommu_unmap_guest(kvm);
5544 kfree(kvm->arch.vpic);
5545 kfree(kvm->arch.vioapic);
5546 kvm_free_vcpus(kvm);
5547 kvm_free_physmem(kvm);
5548 if (kvm->arch.apic_access_page)
5549 put_page(kvm->arch.apic_access_page);
5550 if (kvm->arch.ept_identity_pagetable)
5551 put_page(kvm->arch.ept_identity_pagetable);
5552 cleanup_srcu_struct(&kvm->srcu);
5556 int kvm_arch_prepare_memory_region(struct kvm *kvm,
5557 struct kvm_memory_slot *memslot,
5558 struct kvm_memory_slot old,
5559 struct kvm_userspace_memory_region *mem,
5562 int npages = memslot->npages;
5563 int map_flags = MAP_PRIVATE | MAP_ANONYMOUS;
5565 /* Prevent internal slot pages from being moved by fork()/COW. */
5566 if (memslot->id >= KVM_MEMORY_SLOTS)
5567 map_flags = MAP_SHARED | MAP_ANONYMOUS;
5569 /*To keep backward compatibility with older userspace,
5570 *x86 needs to hanlde !user_alloc case.
5573 if (npages && !old.rmap) {
5574 unsigned long userspace_addr;
5576 down_write(¤t->mm->mmap_sem);
5577 userspace_addr = do_mmap(NULL, 0,
5579 PROT_READ | PROT_WRITE,
5582 up_write(¤t->mm->mmap_sem);
5584 if (IS_ERR((void *)userspace_addr))
5585 return PTR_ERR((void *)userspace_addr);
5587 memslot->userspace_addr = userspace_addr;
5595 void kvm_arch_commit_memory_region(struct kvm *kvm,
5596 struct kvm_userspace_memory_region *mem,
5597 struct kvm_memory_slot old,
5601 int npages = mem->memory_size >> PAGE_SHIFT;
5603 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5606 down_write(¤t->mm->mmap_sem);
5607 ret = do_munmap(current->mm, old.userspace_addr,
5608 old.npages * PAGE_SIZE);
5609 up_write(¤t->mm->mmap_sem);
5612 "kvm_vm_ioctl_set_memory_region: "
5613 "failed to munmap memory\n");
5616 spin_lock(&kvm->mmu_lock);
5617 if (!kvm->arch.n_requested_mmu_pages) {
5618 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5619 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5622 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
5623 spin_unlock(&kvm->mmu_lock);
5626 void kvm_arch_flush_shadow(struct kvm *kvm)
5628 kvm_mmu_zap_all(kvm);
5629 kvm_reload_remote_mmus(kvm);
5632 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5634 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
5635 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5636 || vcpu->arch.nmi_pending ||
5637 (kvm_arch_interrupt_allowed(vcpu) &&
5638 kvm_cpu_has_interrupt(vcpu));
5641 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5644 int cpu = vcpu->cpu;
5646 if (waitqueue_active(&vcpu->wq)) {
5647 wake_up_interruptible(&vcpu->wq);
5648 ++vcpu->stat.halt_wakeup;
5652 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5653 if (atomic_xchg(&vcpu->guest_mode, 0))
5654 smp_send_reschedule(cpu);
5658 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5660 return kvm_x86_ops->interrupt_allowed(vcpu);
5663 bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
5665 unsigned long current_rip = kvm_rip_read(vcpu) +
5666 get_segment_base(vcpu, VCPU_SREG_CS);
5668 return current_rip == linear_rip;
5670 EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
5672 unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5674 unsigned long rflags;
5676 rflags = kvm_x86_ops->get_rflags(vcpu);
5677 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5678 rflags &= ~X86_EFLAGS_TF;
5681 EXPORT_SYMBOL_GPL(kvm_get_rflags);
5683 void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5685 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5686 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
5687 rflags |= X86_EFLAGS_TF;
5688 kvm_x86_ops->set_rflags(vcpu, rflags);
5690 EXPORT_SYMBOL_GPL(kvm_set_rflags);
5692 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5693 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5694 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5695 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5696 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
5697 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
5698 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
5699 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
5700 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
5701 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
5702 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
5703 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);