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 <trace/events/kvm.h>
47 #define CREATE_TRACE_POINTS
50 #include <asm/debugreg.h>
51 #include <asm/uaccess.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 \
68 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
70 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
72 #define KVM_MAX_MCE_BANKS 32
73 #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
76 * - enable syscall per default because its emulated by KVM
77 * - enable LME and LMA per default on 64 bit KVM
80 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
82 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
85 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
86 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
88 static void update_cr8_intercept(struct kvm_vcpu *vcpu);
89 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
90 struct kvm_cpuid_entry2 __user *entries);
92 struct kvm_x86_ops *kvm_x86_ops;
93 EXPORT_SYMBOL_GPL(kvm_x86_ops);
96 module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
98 #define KVM_NR_SHARED_MSRS 16
100 struct kvm_shared_msrs_global {
102 u32 msrs[KVM_NR_SHARED_MSRS];
105 struct kvm_shared_msrs {
106 struct user_return_notifier urn;
108 struct kvm_shared_msr_values {
111 } values[KVM_NR_SHARED_MSRS];
114 static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
115 static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
117 struct kvm_stats_debugfs_item debugfs_entries[] = {
118 { "pf_fixed", VCPU_STAT(pf_fixed) },
119 { "pf_guest", VCPU_STAT(pf_guest) },
120 { "tlb_flush", VCPU_STAT(tlb_flush) },
121 { "invlpg", VCPU_STAT(invlpg) },
122 { "exits", VCPU_STAT(exits) },
123 { "io_exits", VCPU_STAT(io_exits) },
124 { "mmio_exits", VCPU_STAT(mmio_exits) },
125 { "signal_exits", VCPU_STAT(signal_exits) },
126 { "irq_window", VCPU_STAT(irq_window_exits) },
127 { "nmi_window", VCPU_STAT(nmi_window_exits) },
128 { "halt_exits", VCPU_STAT(halt_exits) },
129 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
130 { "hypercalls", VCPU_STAT(hypercalls) },
131 { "request_irq", VCPU_STAT(request_irq_exits) },
132 { "irq_exits", VCPU_STAT(irq_exits) },
133 { "host_state_reload", VCPU_STAT(host_state_reload) },
134 { "efer_reload", VCPU_STAT(efer_reload) },
135 { "fpu_reload", VCPU_STAT(fpu_reload) },
136 { "insn_emulation", VCPU_STAT(insn_emulation) },
137 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
138 { "irq_injections", VCPU_STAT(irq_injections) },
139 { "nmi_injections", VCPU_STAT(nmi_injections) },
140 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
141 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
142 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
143 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
144 { "mmu_flooded", VM_STAT(mmu_flooded) },
145 { "mmu_recycled", VM_STAT(mmu_recycled) },
146 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
147 { "mmu_unsync", VM_STAT(mmu_unsync) },
148 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
149 { "largepages", VM_STAT(lpages) },
153 static void kvm_on_user_return(struct user_return_notifier *urn)
156 struct kvm_shared_msrs *locals
157 = container_of(urn, struct kvm_shared_msrs, urn);
158 struct kvm_shared_msr_values *values;
160 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
161 values = &locals->values[slot];
162 if (values->host != values->curr) {
163 wrmsrl(shared_msrs_global.msrs[slot], values->host);
164 values->curr = values->host;
167 locals->registered = false;
168 user_return_notifier_unregister(urn);
171 static void shared_msr_update(unsigned slot, u32 msr)
173 struct kvm_shared_msrs *smsr;
176 smsr = &__get_cpu_var(shared_msrs);
177 /* only read, and nobody should modify it at this time,
178 * so don't need lock */
179 if (slot >= shared_msrs_global.nr) {
180 printk(KERN_ERR "kvm: invalid MSR slot!");
183 rdmsrl_safe(msr, &value);
184 smsr->values[slot].host = value;
185 smsr->values[slot].curr = value;
188 void kvm_define_shared_msr(unsigned slot, u32 msr)
190 if (slot >= shared_msrs_global.nr)
191 shared_msrs_global.nr = slot + 1;
192 shared_msrs_global.msrs[slot] = msr;
193 /* we need ensured the shared_msr_global have been updated */
196 EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
198 static void kvm_shared_msr_cpu_online(void)
202 for (i = 0; i < shared_msrs_global.nr; ++i)
203 shared_msr_update(i, shared_msrs_global.msrs[i]);
206 void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
208 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
210 if (((value ^ smsr->values[slot].curr) & mask) == 0)
212 smsr->values[slot].curr = value;
213 wrmsrl(shared_msrs_global.msrs[slot], value);
214 if (!smsr->registered) {
215 smsr->urn.on_user_return = kvm_on_user_return;
216 user_return_notifier_register(&smsr->urn);
217 smsr->registered = true;
220 EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
222 static void drop_user_return_notifiers(void *ignore)
224 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
226 if (smsr->registered)
227 kvm_on_user_return(&smsr->urn);
230 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
232 if (irqchip_in_kernel(vcpu->kvm))
233 return vcpu->arch.apic_base;
235 return vcpu->arch.apic_base;
237 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
239 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
241 /* TODO: reserve bits check */
242 if (irqchip_in_kernel(vcpu->kvm))
243 kvm_lapic_set_base(vcpu, data);
245 vcpu->arch.apic_base = data;
247 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
249 #define EXCPT_BENIGN 0
250 #define EXCPT_CONTRIBUTORY 1
253 static int exception_class(int vector)
263 return EXCPT_CONTRIBUTORY;
270 static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
271 unsigned nr, bool has_error, u32 error_code,
277 if (!vcpu->arch.exception.pending) {
279 vcpu->arch.exception.pending = true;
280 vcpu->arch.exception.has_error_code = has_error;
281 vcpu->arch.exception.nr = nr;
282 vcpu->arch.exception.error_code = error_code;
283 vcpu->arch.exception.reinject = reinject;
287 /* to check exception */
288 prev_nr = vcpu->arch.exception.nr;
289 if (prev_nr == DF_VECTOR) {
290 /* triple fault -> shutdown */
291 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
294 class1 = exception_class(prev_nr);
295 class2 = exception_class(nr);
296 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
297 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
298 /* generate double fault per SDM Table 5-5 */
299 vcpu->arch.exception.pending = true;
300 vcpu->arch.exception.has_error_code = true;
301 vcpu->arch.exception.nr = DF_VECTOR;
302 vcpu->arch.exception.error_code = 0;
304 /* replace previous exception with a new one in a hope
305 that instruction re-execution will regenerate lost
310 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
312 kvm_multiple_exception(vcpu, nr, false, 0, false);
314 EXPORT_SYMBOL_GPL(kvm_queue_exception);
316 void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
318 kvm_multiple_exception(vcpu, nr, false, 0, true);
320 EXPORT_SYMBOL_GPL(kvm_requeue_exception);
322 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
325 ++vcpu->stat.pf_guest;
326 vcpu->arch.cr2 = addr;
327 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
330 void kvm_inject_nmi(struct kvm_vcpu *vcpu)
332 vcpu->arch.nmi_pending = 1;
334 EXPORT_SYMBOL_GPL(kvm_inject_nmi);
336 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
338 kvm_multiple_exception(vcpu, nr, true, error_code, false);
340 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
342 void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
344 kvm_multiple_exception(vcpu, nr, true, error_code, true);
346 EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
349 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
350 * a #GP and return false.
352 bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
354 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
356 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
359 EXPORT_SYMBOL_GPL(kvm_require_cpl);
362 * Load the pae pdptrs. Return true is they are all valid.
364 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
366 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
367 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
370 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
372 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
373 offset * sizeof(u64), sizeof(pdpte));
378 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
379 if (is_present_gpte(pdpte[i]) &&
380 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
387 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
388 __set_bit(VCPU_EXREG_PDPTR,
389 (unsigned long *)&vcpu->arch.regs_avail);
390 __set_bit(VCPU_EXREG_PDPTR,
391 (unsigned long *)&vcpu->arch.regs_dirty);
396 EXPORT_SYMBOL_GPL(load_pdptrs);
398 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
400 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
404 if (is_long_mode(vcpu) || !is_pae(vcpu))
407 if (!test_bit(VCPU_EXREG_PDPTR,
408 (unsigned long *)&vcpu->arch.regs_avail))
411 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
414 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
420 static int __kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
422 unsigned long old_cr0 = kvm_read_cr0(vcpu);
423 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
424 X86_CR0_CD | X86_CR0_NW;
429 if (cr0 & 0xffffffff00000000UL)
433 cr0 &= ~CR0_RESERVED_BITS;
435 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
438 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
441 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
443 if ((vcpu->arch.efer & EFER_LME)) {
448 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
453 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3))
457 kvm_x86_ops->set_cr0(vcpu, cr0);
459 if ((cr0 ^ old_cr0) & update_bits)
460 kvm_mmu_reset_context(vcpu);
464 void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
466 if (__kvm_set_cr0(vcpu, cr0))
467 kvm_inject_gp(vcpu, 0);
469 EXPORT_SYMBOL_GPL(kvm_set_cr0);
471 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
473 kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
475 EXPORT_SYMBOL_GPL(kvm_lmsw);
477 int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
479 unsigned long old_cr4 = kvm_read_cr4(vcpu);
480 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
482 if (cr4 & CR4_RESERVED_BITS)
485 if (is_long_mode(vcpu)) {
486 if (!(cr4 & X86_CR4_PAE))
488 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
489 && ((cr4 ^ old_cr4) & pdptr_bits)
490 && !load_pdptrs(vcpu, vcpu->arch.cr3))
493 if (cr4 & X86_CR4_VMXE)
496 kvm_x86_ops->set_cr4(vcpu, cr4);
498 if ((cr4 ^ old_cr4) & pdptr_bits)
499 kvm_mmu_reset_context(vcpu);
504 void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
506 if (__kvm_set_cr4(vcpu, cr4))
507 kvm_inject_gp(vcpu, 0);
509 EXPORT_SYMBOL_GPL(kvm_set_cr4);
511 static int __kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
513 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
514 kvm_mmu_sync_roots(vcpu);
515 kvm_mmu_flush_tlb(vcpu);
519 if (is_long_mode(vcpu)) {
520 if (cr3 & CR3_L_MODE_RESERVED_BITS)
524 if (cr3 & CR3_PAE_RESERVED_BITS)
526 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3))
530 * We don't check reserved bits in nonpae mode, because
531 * this isn't enforced, and VMware depends on this.
536 * Does the new cr3 value map to physical memory? (Note, we
537 * catch an invalid cr3 even in real-mode, because it would
538 * cause trouble later on when we turn on paging anyway.)
540 * A real CPU would silently accept an invalid cr3 and would
541 * attempt to use it - with largely undefined (and often hard
542 * to debug) behavior on the guest side.
544 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
546 vcpu->arch.cr3 = cr3;
547 vcpu->arch.mmu.new_cr3(vcpu);
551 void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
553 if (__kvm_set_cr3(vcpu, cr3))
554 kvm_inject_gp(vcpu, 0);
556 EXPORT_SYMBOL_GPL(kvm_set_cr3);
558 int __kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
560 if (cr8 & CR8_RESERVED_BITS)
562 if (irqchip_in_kernel(vcpu->kvm))
563 kvm_lapic_set_tpr(vcpu, cr8);
565 vcpu->arch.cr8 = cr8;
569 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
571 if (__kvm_set_cr8(vcpu, cr8))
572 kvm_inject_gp(vcpu, 0);
574 EXPORT_SYMBOL_GPL(kvm_set_cr8);
576 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
578 if (irqchip_in_kernel(vcpu->kvm))
579 return kvm_lapic_get_cr8(vcpu);
581 return vcpu->arch.cr8;
583 EXPORT_SYMBOL_GPL(kvm_get_cr8);
585 static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
589 vcpu->arch.db[dr] = val;
590 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
591 vcpu->arch.eff_db[dr] = val;
594 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
598 if (val & 0xffffffff00000000ULL)
600 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
603 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
607 if (val & 0xffffffff00000000ULL)
609 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
610 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
611 kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7);
612 vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK);
620 int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
624 res = __kvm_set_dr(vcpu, dr, val);
626 kvm_queue_exception(vcpu, UD_VECTOR);
628 kvm_inject_gp(vcpu, 0);
632 EXPORT_SYMBOL_GPL(kvm_set_dr);
634 static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
638 *val = vcpu->arch.db[dr];
641 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
645 *val = vcpu->arch.dr6;
648 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
652 *val = vcpu->arch.dr7;
659 int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
661 if (_kvm_get_dr(vcpu, dr, val)) {
662 kvm_queue_exception(vcpu, UD_VECTOR);
667 EXPORT_SYMBOL_GPL(kvm_get_dr);
669 static inline u32 bit(int bitno)
671 return 1 << (bitno & 31);
675 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
676 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
678 * This list is modified at module load time to reflect the
679 * capabilities of the host cpu. This capabilities test skips MSRs that are
680 * kvm-specific. Those are put in the beginning of the list.
683 #define KVM_SAVE_MSRS_BEGIN 7
684 static u32 msrs_to_save[] = {
685 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
686 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
687 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
688 HV_X64_MSR_APIC_ASSIST_PAGE,
689 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
692 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
694 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
697 static unsigned num_msrs_to_save;
699 static u32 emulated_msrs[] = {
700 MSR_IA32_MISC_ENABLE,
703 static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
705 u64 old_efer = vcpu->arch.efer;
707 if (efer & efer_reserved_bits)
711 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
714 if (efer & EFER_FFXSR) {
715 struct kvm_cpuid_entry2 *feat;
717 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
718 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
722 if (efer & EFER_SVME) {
723 struct kvm_cpuid_entry2 *feat;
725 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
726 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
731 efer |= vcpu->arch.efer & EFER_LMA;
733 kvm_x86_ops->set_efer(vcpu, efer);
735 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
736 kvm_mmu_reset_context(vcpu);
738 /* Update reserved bits */
739 if ((efer ^ old_efer) & EFER_NX)
740 kvm_mmu_reset_context(vcpu);
745 void kvm_enable_efer_bits(u64 mask)
747 efer_reserved_bits &= ~mask;
749 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
753 * Writes msr value into into the appropriate "register".
754 * Returns 0 on success, non-0 otherwise.
755 * Assumes vcpu_load() was already called.
757 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
759 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
763 * Adapt set_msr() to msr_io()'s calling convention
765 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
767 return kvm_set_msr(vcpu, index, *data);
770 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
774 struct pvclock_wall_clock wc;
775 struct timespec boot;
780 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
785 ++version; /* first time write, random junk */
789 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
792 * The guest calculates current wall clock time by adding
793 * system time (updated by kvm_write_guest_time below) to the
794 * wall clock specified here. guest system time equals host
795 * system time for us, thus we must fill in host boot time here.
799 wc.sec = boot.tv_sec;
800 wc.nsec = boot.tv_nsec;
801 wc.version = version;
803 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
806 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
809 static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
811 uint32_t quotient, remainder;
813 /* Don't try to replace with do_div(), this one calculates
814 * "(dividend << 32) / divisor" */
816 : "=a" (quotient), "=d" (remainder)
817 : "0" (0), "1" (dividend), "r" (divisor) );
821 static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
823 uint64_t nsecs = 1000000000LL;
828 tps64 = tsc_khz * 1000LL;
829 while (tps64 > nsecs*2) {
834 tps32 = (uint32_t)tps64;
835 while (tps32 <= (uint32_t)nsecs) {
840 hv_clock->tsc_shift = shift;
841 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
843 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
844 __func__, tsc_khz, hv_clock->tsc_shift,
845 hv_clock->tsc_to_system_mul);
848 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
850 static void kvm_write_guest_time(struct kvm_vcpu *v)
854 struct kvm_vcpu_arch *vcpu = &v->arch;
856 unsigned long this_tsc_khz;
858 if ((!vcpu->time_page))
861 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
862 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
863 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
864 vcpu->hv_clock_tsc_khz = this_tsc_khz;
866 put_cpu_var(cpu_tsc_khz);
868 /* Keep irq disabled to prevent changes to the clock */
869 local_irq_save(flags);
870 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
872 monotonic_to_bootbased(&ts);
873 local_irq_restore(flags);
875 /* With all the info we got, fill in the values */
877 vcpu->hv_clock.system_time = ts.tv_nsec +
878 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
880 vcpu->hv_clock.flags = 0;
883 * The interface expects us to write an even number signaling that the
884 * update is finished. Since the guest won't see the intermediate
885 * state, we just increase by 2 at the end.
887 vcpu->hv_clock.version += 2;
889 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
891 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
892 sizeof(vcpu->hv_clock));
894 kunmap_atomic(shared_kaddr, KM_USER0);
896 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
899 static int kvm_request_guest_time_update(struct kvm_vcpu *v)
901 struct kvm_vcpu_arch *vcpu = &v->arch;
903 if (!vcpu->time_page)
905 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
909 static bool msr_mtrr_valid(unsigned msr)
912 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
913 case MSR_MTRRfix64K_00000:
914 case MSR_MTRRfix16K_80000:
915 case MSR_MTRRfix16K_A0000:
916 case MSR_MTRRfix4K_C0000:
917 case MSR_MTRRfix4K_C8000:
918 case MSR_MTRRfix4K_D0000:
919 case MSR_MTRRfix4K_D8000:
920 case MSR_MTRRfix4K_E0000:
921 case MSR_MTRRfix4K_E8000:
922 case MSR_MTRRfix4K_F0000:
923 case MSR_MTRRfix4K_F8000:
924 case MSR_MTRRdefType:
925 case MSR_IA32_CR_PAT:
933 static bool valid_pat_type(unsigned t)
935 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
938 static bool valid_mtrr_type(unsigned t)
940 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
943 static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
947 if (!msr_mtrr_valid(msr))
950 if (msr == MSR_IA32_CR_PAT) {
951 for (i = 0; i < 8; i++)
952 if (!valid_pat_type((data >> (i * 8)) & 0xff))
955 } else if (msr == MSR_MTRRdefType) {
958 return valid_mtrr_type(data & 0xff);
959 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
960 for (i = 0; i < 8 ; i++)
961 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
967 return valid_mtrr_type(data & 0xff);
970 static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
972 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
974 if (!mtrr_valid(vcpu, msr, data))
977 if (msr == MSR_MTRRdefType) {
978 vcpu->arch.mtrr_state.def_type = data;
979 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
980 } else if (msr == MSR_MTRRfix64K_00000)
982 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
983 p[1 + msr - MSR_MTRRfix16K_80000] = data;
984 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
985 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
986 else if (msr == MSR_IA32_CR_PAT)
987 vcpu->arch.pat = data;
988 else { /* Variable MTRRs */
989 int idx, is_mtrr_mask;
992 idx = (msr - 0x200) / 2;
993 is_mtrr_mask = msr - 0x200 - 2 * idx;
996 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
999 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1003 kvm_mmu_reset_context(vcpu);
1007 static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1009 u64 mcg_cap = vcpu->arch.mcg_cap;
1010 unsigned bank_num = mcg_cap & 0xff;
1013 case MSR_IA32_MCG_STATUS:
1014 vcpu->arch.mcg_status = data;
1016 case MSR_IA32_MCG_CTL:
1017 if (!(mcg_cap & MCG_CTL_P))
1019 if (data != 0 && data != ~(u64)0)
1021 vcpu->arch.mcg_ctl = data;
1024 if (msr >= MSR_IA32_MC0_CTL &&
1025 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1026 u32 offset = msr - MSR_IA32_MC0_CTL;
1027 /* only 0 or all 1s can be written to IA32_MCi_CTL
1028 * some Linux kernels though clear bit 10 in bank 4 to
1029 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1030 * this to avoid an uncatched #GP in the guest
1032 if ((offset & 0x3) == 0 &&
1033 data != 0 && (data | (1 << 10)) != ~(u64)0)
1035 vcpu->arch.mce_banks[offset] = data;
1043 static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1045 struct kvm *kvm = vcpu->kvm;
1046 int lm = is_long_mode(vcpu);
1047 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1048 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1049 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1050 : kvm->arch.xen_hvm_config.blob_size_32;
1051 u32 page_num = data & ~PAGE_MASK;
1052 u64 page_addr = data & PAGE_MASK;
1057 if (page_num >= blob_size)
1060 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
1064 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
1066 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1075 static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1077 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1080 static bool kvm_hv_msr_partition_wide(u32 msr)
1084 case HV_X64_MSR_GUEST_OS_ID:
1085 case HV_X64_MSR_HYPERCALL:
1093 static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1095 struct kvm *kvm = vcpu->kvm;
1098 case HV_X64_MSR_GUEST_OS_ID:
1099 kvm->arch.hv_guest_os_id = data;
1100 /* setting guest os id to zero disables hypercall page */
1101 if (!kvm->arch.hv_guest_os_id)
1102 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1104 case HV_X64_MSR_HYPERCALL: {
1109 /* if guest os id is not set hypercall should remain disabled */
1110 if (!kvm->arch.hv_guest_os_id)
1112 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1113 kvm->arch.hv_hypercall = data;
1116 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1117 addr = gfn_to_hva(kvm, gfn);
1118 if (kvm_is_error_hva(addr))
1120 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1121 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1122 if (copy_to_user((void __user *)addr, instructions, 4))
1124 kvm->arch.hv_hypercall = data;
1128 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1129 "data 0x%llx\n", msr, data);
1135 static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1138 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1141 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1142 vcpu->arch.hv_vapic = data;
1145 addr = gfn_to_hva(vcpu->kvm, data >>
1146 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1147 if (kvm_is_error_hva(addr))
1149 if (clear_user((void __user *)addr, PAGE_SIZE))
1151 vcpu->arch.hv_vapic = data;
1154 case HV_X64_MSR_EOI:
1155 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1156 case HV_X64_MSR_ICR:
1157 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1158 case HV_X64_MSR_TPR:
1159 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1161 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1162 "data 0x%llx\n", msr, data);
1169 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1173 return set_efer(vcpu, data);
1175 data &= ~(u64)0x40; /* ignore flush filter disable */
1176 data &= ~(u64)0x100; /* ignore ignne emulation enable */
1178 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1183 case MSR_FAM10H_MMIO_CONF_BASE:
1185 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1190 case MSR_AMD64_NB_CFG:
1192 case MSR_IA32_DEBUGCTLMSR:
1194 /* We support the non-activated case already */
1196 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1197 /* Values other than LBR and BTF are vendor-specific,
1198 thus reserved and should throw a #GP */
1201 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1204 case MSR_IA32_UCODE_REV:
1205 case MSR_IA32_UCODE_WRITE:
1206 case MSR_VM_HSAVE_PA:
1207 case MSR_AMD64_PATCH_LOADER:
1209 case 0x200 ... 0x2ff:
1210 return set_msr_mtrr(vcpu, msr, data);
1211 case MSR_IA32_APICBASE:
1212 kvm_set_apic_base(vcpu, data);
1214 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1215 return kvm_x2apic_msr_write(vcpu, msr, data);
1216 case MSR_IA32_MISC_ENABLE:
1217 vcpu->arch.ia32_misc_enable_msr = data;
1219 case MSR_KVM_WALL_CLOCK_NEW:
1220 case MSR_KVM_WALL_CLOCK:
1221 vcpu->kvm->arch.wall_clock = data;
1222 kvm_write_wall_clock(vcpu->kvm, data);
1224 case MSR_KVM_SYSTEM_TIME_NEW:
1225 case MSR_KVM_SYSTEM_TIME: {
1226 if (vcpu->arch.time_page) {
1227 kvm_release_page_dirty(vcpu->arch.time_page);
1228 vcpu->arch.time_page = NULL;
1231 vcpu->arch.time = data;
1233 /* we verify if the enable bit is set... */
1237 /* ...but clean it before doing the actual write */
1238 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1240 vcpu->arch.time_page =
1241 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
1243 if (is_error_page(vcpu->arch.time_page)) {
1244 kvm_release_page_clean(vcpu->arch.time_page);
1245 vcpu->arch.time_page = NULL;
1248 kvm_request_guest_time_update(vcpu);
1251 case MSR_IA32_MCG_CTL:
1252 case MSR_IA32_MCG_STATUS:
1253 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1254 return set_msr_mce(vcpu, msr, data);
1256 /* Performance counters are not protected by a CPUID bit,
1257 * so we should check all of them in the generic path for the sake of
1258 * cross vendor migration.
1259 * Writing a zero into the event select MSRs disables them,
1260 * which we perfectly emulate ;-). Any other value should be at least
1261 * reported, some guests depend on them.
1263 case MSR_P6_EVNTSEL0:
1264 case MSR_P6_EVNTSEL1:
1265 case MSR_K7_EVNTSEL0:
1266 case MSR_K7_EVNTSEL1:
1267 case MSR_K7_EVNTSEL2:
1268 case MSR_K7_EVNTSEL3:
1270 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1271 "0x%x data 0x%llx\n", msr, data);
1273 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1274 * so we ignore writes to make it happy.
1276 case MSR_P6_PERFCTR0:
1277 case MSR_P6_PERFCTR1:
1278 case MSR_K7_PERFCTR0:
1279 case MSR_K7_PERFCTR1:
1280 case MSR_K7_PERFCTR2:
1281 case MSR_K7_PERFCTR3:
1282 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1283 "0x%x data 0x%llx\n", msr, data);
1285 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1286 if (kvm_hv_msr_partition_wide(msr)) {
1288 mutex_lock(&vcpu->kvm->lock);
1289 r = set_msr_hyperv_pw(vcpu, msr, data);
1290 mutex_unlock(&vcpu->kvm->lock);
1293 return set_msr_hyperv(vcpu, msr, data);
1296 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1297 return xen_hvm_config(vcpu, data);
1299 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1303 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1310 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1314 * Reads an msr value (of 'msr_index') into 'pdata'.
1315 * Returns 0 on success, non-0 otherwise.
1316 * Assumes vcpu_load() was already called.
1318 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1320 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1323 static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1325 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1327 if (!msr_mtrr_valid(msr))
1330 if (msr == MSR_MTRRdefType)
1331 *pdata = vcpu->arch.mtrr_state.def_type +
1332 (vcpu->arch.mtrr_state.enabled << 10);
1333 else if (msr == MSR_MTRRfix64K_00000)
1335 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1336 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1337 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1338 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1339 else if (msr == MSR_IA32_CR_PAT)
1340 *pdata = vcpu->arch.pat;
1341 else { /* Variable MTRRs */
1342 int idx, is_mtrr_mask;
1345 idx = (msr - 0x200) / 2;
1346 is_mtrr_mask = msr - 0x200 - 2 * idx;
1349 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1352 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1359 static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1362 u64 mcg_cap = vcpu->arch.mcg_cap;
1363 unsigned bank_num = mcg_cap & 0xff;
1366 case MSR_IA32_P5_MC_ADDR:
1367 case MSR_IA32_P5_MC_TYPE:
1370 case MSR_IA32_MCG_CAP:
1371 data = vcpu->arch.mcg_cap;
1373 case MSR_IA32_MCG_CTL:
1374 if (!(mcg_cap & MCG_CTL_P))
1376 data = vcpu->arch.mcg_ctl;
1378 case MSR_IA32_MCG_STATUS:
1379 data = vcpu->arch.mcg_status;
1382 if (msr >= MSR_IA32_MC0_CTL &&
1383 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1384 u32 offset = msr - MSR_IA32_MC0_CTL;
1385 data = vcpu->arch.mce_banks[offset];
1394 static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1397 struct kvm *kvm = vcpu->kvm;
1400 case HV_X64_MSR_GUEST_OS_ID:
1401 data = kvm->arch.hv_guest_os_id;
1403 case HV_X64_MSR_HYPERCALL:
1404 data = kvm->arch.hv_hypercall;
1407 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1415 static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1420 case HV_X64_MSR_VP_INDEX: {
1423 kvm_for_each_vcpu(r, v, vcpu->kvm)
1428 case HV_X64_MSR_EOI:
1429 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1430 case HV_X64_MSR_ICR:
1431 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1432 case HV_X64_MSR_TPR:
1433 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1435 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1442 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1447 case MSR_IA32_PLATFORM_ID:
1448 case MSR_IA32_UCODE_REV:
1449 case MSR_IA32_EBL_CR_POWERON:
1450 case MSR_IA32_DEBUGCTLMSR:
1451 case MSR_IA32_LASTBRANCHFROMIP:
1452 case MSR_IA32_LASTBRANCHTOIP:
1453 case MSR_IA32_LASTINTFROMIP:
1454 case MSR_IA32_LASTINTTOIP:
1457 case MSR_VM_HSAVE_PA:
1458 case MSR_P6_PERFCTR0:
1459 case MSR_P6_PERFCTR1:
1460 case MSR_P6_EVNTSEL0:
1461 case MSR_P6_EVNTSEL1:
1462 case MSR_K7_EVNTSEL0:
1463 case MSR_K7_PERFCTR0:
1464 case MSR_K8_INT_PENDING_MSG:
1465 case MSR_AMD64_NB_CFG:
1466 case MSR_FAM10H_MMIO_CONF_BASE:
1470 data = 0x500 | KVM_NR_VAR_MTRR;
1472 case 0x200 ... 0x2ff:
1473 return get_msr_mtrr(vcpu, msr, pdata);
1474 case 0xcd: /* fsb frequency */
1477 case MSR_IA32_APICBASE:
1478 data = kvm_get_apic_base(vcpu);
1480 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1481 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1483 case MSR_IA32_MISC_ENABLE:
1484 data = vcpu->arch.ia32_misc_enable_msr;
1486 case MSR_IA32_PERF_STATUS:
1487 /* TSC increment by tick */
1489 /* CPU multiplier */
1490 data |= (((uint64_t)4ULL) << 40);
1493 data = vcpu->arch.efer;
1495 case MSR_KVM_WALL_CLOCK:
1496 case MSR_KVM_WALL_CLOCK_NEW:
1497 data = vcpu->kvm->arch.wall_clock;
1499 case MSR_KVM_SYSTEM_TIME:
1500 case MSR_KVM_SYSTEM_TIME_NEW:
1501 data = vcpu->arch.time;
1503 case MSR_IA32_P5_MC_ADDR:
1504 case MSR_IA32_P5_MC_TYPE:
1505 case MSR_IA32_MCG_CAP:
1506 case MSR_IA32_MCG_CTL:
1507 case MSR_IA32_MCG_STATUS:
1508 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1509 return get_msr_mce(vcpu, msr, pdata);
1510 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1511 if (kvm_hv_msr_partition_wide(msr)) {
1513 mutex_lock(&vcpu->kvm->lock);
1514 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1515 mutex_unlock(&vcpu->kvm->lock);
1518 return get_msr_hyperv(vcpu, msr, pdata);
1522 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1525 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1533 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1536 * Read or write a bunch of msrs. All parameters are kernel addresses.
1538 * @return number of msrs set successfully.
1540 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1541 struct kvm_msr_entry *entries,
1542 int (*do_msr)(struct kvm_vcpu *vcpu,
1543 unsigned index, u64 *data))
1547 idx = srcu_read_lock(&vcpu->kvm->srcu);
1548 for (i = 0; i < msrs->nmsrs; ++i)
1549 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1551 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1557 * Read or write a bunch of msrs. Parameters are user addresses.
1559 * @return number of msrs set successfully.
1561 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1562 int (*do_msr)(struct kvm_vcpu *vcpu,
1563 unsigned index, u64 *data),
1566 struct kvm_msrs msrs;
1567 struct kvm_msr_entry *entries;
1572 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1576 if (msrs.nmsrs >= MAX_IO_MSRS)
1580 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1581 entries = kmalloc(size, GFP_KERNEL);
1586 if (copy_from_user(entries, user_msrs->entries, size))
1589 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1594 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1605 int kvm_dev_ioctl_check_extension(long ext)
1610 case KVM_CAP_IRQCHIP:
1612 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
1613 case KVM_CAP_SET_TSS_ADDR:
1614 case KVM_CAP_EXT_CPUID:
1615 case KVM_CAP_CLOCKSOURCE:
1617 case KVM_CAP_NOP_IO_DELAY:
1618 case KVM_CAP_MP_STATE:
1619 case KVM_CAP_SYNC_MMU:
1620 case KVM_CAP_REINJECT_CONTROL:
1621 case KVM_CAP_IRQ_INJECT_STATUS:
1622 case KVM_CAP_ASSIGN_DEV_IRQ:
1624 case KVM_CAP_IOEVENTFD:
1626 case KVM_CAP_PIT_STATE2:
1627 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
1628 case KVM_CAP_XEN_HVM:
1629 case KVM_CAP_ADJUST_CLOCK:
1630 case KVM_CAP_VCPU_EVENTS:
1631 case KVM_CAP_HYPERV:
1632 case KVM_CAP_HYPERV_VAPIC:
1633 case KVM_CAP_HYPERV_SPIN:
1634 case KVM_CAP_PCI_SEGMENT:
1635 case KVM_CAP_DEBUGREGS:
1636 case KVM_CAP_X86_ROBUST_SINGLESTEP:
1639 case KVM_CAP_COALESCED_MMIO:
1640 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1643 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1645 case KVM_CAP_NR_VCPUS:
1648 case KVM_CAP_NR_MEMSLOTS:
1649 r = KVM_MEMORY_SLOTS;
1651 case KVM_CAP_PV_MMU: /* obsolete */
1658 r = KVM_MAX_MCE_BANKS;
1668 long kvm_arch_dev_ioctl(struct file *filp,
1669 unsigned int ioctl, unsigned long arg)
1671 void __user *argp = (void __user *)arg;
1675 case KVM_GET_MSR_INDEX_LIST: {
1676 struct kvm_msr_list __user *user_msr_list = argp;
1677 struct kvm_msr_list msr_list;
1681 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1684 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1685 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1688 if (n < msr_list.nmsrs)
1691 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1692 num_msrs_to_save * sizeof(u32)))
1694 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
1696 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1701 case KVM_GET_SUPPORTED_CPUID: {
1702 struct kvm_cpuid2 __user *cpuid_arg = argp;
1703 struct kvm_cpuid2 cpuid;
1706 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1708 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
1709 cpuid_arg->entries);
1714 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1719 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1722 mce_cap = KVM_MCE_CAP_SUPPORTED;
1724 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1736 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1738 kvm_x86_ops->vcpu_load(vcpu, cpu);
1739 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1740 unsigned long khz = cpufreq_quick_get(cpu);
1743 per_cpu(cpu_tsc_khz, cpu) = khz;
1745 kvm_request_guest_time_update(vcpu);
1748 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1750 kvm_x86_ops->vcpu_put(vcpu);
1751 kvm_put_guest_fpu(vcpu);
1754 static int is_efer_nx(void)
1756 unsigned long long efer = 0;
1758 rdmsrl_safe(MSR_EFER, &efer);
1759 return efer & EFER_NX;
1762 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1765 struct kvm_cpuid_entry2 *e, *entry;
1768 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1769 e = &vcpu->arch.cpuid_entries[i];
1770 if (e->function == 0x80000001) {
1775 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
1776 entry->edx &= ~(1 << 20);
1777 printk(KERN_INFO "kvm: guest NX capability removed\n");
1781 /* when an old userspace process fills a new kernel module */
1782 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1783 struct kvm_cpuid *cpuid,
1784 struct kvm_cpuid_entry __user *entries)
1787 struct kvm_cpuid_entry *cpuid_entries;
1790 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1793 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1797 if (copy_from_user(cpuid_entries, entries,
1798 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1800 for (i = 0; i < cpuid->nent; i++) {
1801 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1802 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1803 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1804 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1805 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1806 vcpu->arch.cpuid_entries[i].index = 0;
1807 vcpu->arch.cpuid_entries[i].flags = 0;
1808 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1809 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1810 vcpu->arch.cpuid_entries[i].padding[2] = 0;
1812 vcpu->arch.cpuid_nent = cpuid->nent;
1813 cpuid_fix_nx_cap(vcpu);
1815 kvm_apic_set_version(vcpu);
1816 kvm_x86_ops->cpuid_update(vcpu);
1819 vfree(cpuid_entries);
1824 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1825 struct kvm_cpuid2 *cpuid,
1826 struct kvm_cpuid_entry2 __user *entries)
1831 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1834 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
1835 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
1837 vcpu->arch.cpuid_nent = cpuid->nent;
1838 kvm_apic_set_version(vcpu);
1839 kvm_x86_ops->cpuid_update(vcpu);
1846 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1847 struct kvm_cpuid2 *cpuid,
1848 struct kvm_cpuid_entry2 __user *entries)
1853 if (cpuid->nent < vcpu->arch.cpuid_nent)
1856 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1857 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1862 cpuid->nent = vcpu->arch.cpuid_nent;
1866 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1869 entry->function = function;
1870 entry->index = index;
1871 cpuid_count(entry->function, entry->index,
1872 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1876 #define F(x) bit(X86_FEATURE_##x)
1878 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1879 u32 index, int *nent, int maxnent)
1881 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
1882 #ifdef CONFIG_X86_64
1883 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1885 unsigned f_lm = F(LM);
1887 unsigned f_gbpages = 0;
1890 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
1893 const u32 kvm_supported_word0_x86_features =
1894 F(FPU) | F(VME) | F(DE) | F(PSE) |
1895 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1896 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1897 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1898 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1899 0 /* Reserved, DS, ACPI */ | F(MMX) |
1900 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1901 0 /* HTT, TM, Reserved, PBE */;
1902 /* cpuid 0x80000001.edx */
1903 const u32 kvm_supported_word1_x86_features =
1904 F(FPU) | F(VME) | F(DE) | F(PSE) |
1905 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1906 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1907 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1908 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1909 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
1910 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
1911 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1913 const u32 kvm_supported_word4_x86_features =
1914 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1915 0 /* DS-CPL, VMX, SMX, EST */ |
1916 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1917 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1918 0 /* Reserved, DCA */ | F(XMM4_1) |
1919 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
1920 0 /* Reserved, XSAVE, OSXSAVE */;
1921 /* cpuid 0x80000001.ecx */
1922 const u32 kvm_supported_word6_x86_features =
1923 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1924 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1925 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1926 0 /* SKINIT */ | 0 /* WDT */;
1928 /* all calls to cpuid_count() should be made on the same cpu */
1930 do_cpuid_1_ent(entry, function, index);
1935 entry->eax = min(entry->eax, (u32)0xb);
1938 entry->edx &= kvm_supported_word0_x86_features;
1939 entry->ecx &= kvm_supported_word4_x86_features;
1940 /* we support x2apic emulation even if host does not support
1941 * it since we emulate x2apic in software */
1942 entry->ecx |= F(X2APIC);
1944 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1945 * may return different values. This forces us to get_cpu() before
1946 * issuing the first command, and also to emulate this annoying behavior
1947 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1949 int t, times = entry->eax & 0xff;
1951 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1952 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
1953 for (t = 1; t < times && *nent < maxnent; ++t) {
1954 do_cpuid_1_ent(&entry[t], function, 0);
1955 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1960 /* function 4 and 0xb have additional index. */
1964 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1965 /* read more entries until cache_type is zero */
1966 for (i = 1; *nent < maxnent; ++i) {
1967 cache_type = entry[i - 1].eax & 0x1f;
1970 do_cpuid_1_ent(&entry[i], function, i);
1972 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1980 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1981 /* read more entries until level_type is zero */
1982 for (i = 1; *nent < maxnent; ++i) {
1983 level_type = entry[i - 1].ecx & 0xff00;
1986 do_cpuid_1_ent(&entry[i], function, i);
1988 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1993 case KVM_CPUID_SIGNATURE: {
1994 char signature[12] = "KVMKVMKVM\0\0";
1995 u32 *sigptr = (u32 *)signature;
1997 entry->ebx = sigptr[0];
1998 entry->ecx = sigptr[1];
1999 entry->edx = sigptr[2];
2002 case KVM_CPUID_FEATURES:
2003 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
2004 (1 << KVM_FEATURE_NOP_IO_DELAY) |
2005 (1 << KVM_FEATURE_CLOCKSOURCE2) |
2006 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
2012 entry->eax = min(entry->eax, 0x8000001a);
2015 entry->edx &= kvm_supported_word1_x86_features;
2016 entry->ecx &= kvm_supported_word6_x86_features;
2020 kvm_x86_ops->set_supported_cpuid(function, entry);
2027 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
2028 struct kvm_cpuid_entry2 __user *entries)
2030 struct kvm_cpuid_entry2 *cpuid_entries;
2031 int limit, nent = 0, r = -E2BIG;
2034 if (cpuid->nent < 1)
2036 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2037 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
2039 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
2043 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
2044 limit = cpuid_entries[0].eax;
2045 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
2046 do_cpuid_ent(&cpuid_entries[nent], func, 0,
2047 &nent, cpuid->nent);
2049 if (nent >= cpuid->nent)
2052 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
2053 limit = cpuid_entries[nent - 1].eax;
2054 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
2055 do_cpuid_ent(&cpuid_entries[nent], func, 0,
2056 &nent, cpuid->nent);
2061 if (nent >= cpuid->nent)
2064 do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent,
2068 if (nent >= cpuid->nent)
2071 do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent,
2075 if (nent >= cpuid->nent)
2079 if (copy_to_user(entries, cpuid_entries,
2080 nent * sizeof(struct kvm_cpuid_entry2)))
2086 vfree(cpuid_entries);
2091 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2092 struct kvm_lapic_state *s)
2094 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
2099 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2100 struct kvm_lapic_state *s)
2102 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
2103 kvm_apic_post_state_restore(vcpu);
2104 update_cr8_intercept(vcpu);
2109 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2110 struct kvm_interrupt *irq)
2112 if (irq->irq < 0 || irq->irq >= 256)
2114 if (irqchip_in_kernel(vcpu->kvm))
2117 kvm_queue_interrupt(vcpu, irq->irq, false);
2122 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2124 kvm_inject_nmi(vcpu);
2129 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2130 struct kvm_tpr_access_ctl *tac)
2134 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2138 static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2142 unsigned bank_num = mcg_cap & 0xff, bank;
2145 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
2147 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2150 vcpu->arch.mcg_cap = mcg_cap;
2151 /* Init IA32_MCG_CTL to all 1s */
2152 if (mcg_cap & MCG_CTL_P)
2153 vcpu->arch.mcg_ctl = ~(u64)0;
2154 /* Init IA32_MCi_CTL to all 1s */
2155 for (bank = 0; bank < bank_num; bank++)
2156 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2161 static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2162 struct kvm_x86_mce *mce)
2164 u64 mcg_cap = vcpu->arch.mcg_cap;
2165 unsigned bank_num = mcg_cap & 0xff;
2166 u64 *banks = vcpu->arch.mce_banks;
2168 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2171 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2172 * reporting is disabled
2174 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2175 vcpu->arch.mcg_ctl != ~(u64)0)
2177 banks += 4 * mce->bank;
2179 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2180 * reporting is disabled for the bank
2182 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2184 if (mce->status & MCI_STATUS_UC) {
2185 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
2186 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
2187 printk(KERN_DEBUG "kvm: set_mce: "
2188 "injects mce exception while "
2189 "previous one is in progress!\n");
2190 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2193 if (banks[1] & MCI_STATUS_VAL)
2194 mce->status |= MCI_STATUS_OVER;
2195 banks[2] = mce->addr;
2196 banks[3] = mce->misc;
2197 vcpu->arch.mcg_status = mce->mcg_status;
2198 banks[1] = mce->status;
2199 kvm_queue_exception(vcpu, MC_VECTOR);
2200 } else if (!(banks[1] & MCI_STATUS_VAL)
2201 || !(banks[1] & MCI_STATUS_UC)) {
2202 if (banks[1] & MCI_STATUS_VAL)
2203 mce->status |= MCI_STATUS_OVER;
2204 banks[2] = mce->addr;
2205 banks[3] = mce->misc;
2206 banks[1] = mce->status;
2208 banks[1] |= MCI_STATUS_OVER;
2212 static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2213 struct kvm_vcpu_events *events)
2215 events->exception.injected =
2216 vcpu->arch.exception.pending &&
2217 !kvm_exception_is_soft(vcpu->arch.exception.nr);
2218 events->exception.nr = vcpu->arch.exception.nr;
2219 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2220 events->exception.error_code = vcpu->arch.exception.error_code;
2222 events->interrupt.injected =
2223 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
2224 events->interrupt.nr = vcpu->arch.interrupt.nr;
2225 events->interrupt.soft = 0;
2226 events->interrupt.shadow =
2227 kvm_x86_ops->get_interrupt_shadow(vcpu,
2228 KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
2230 events->nmi.injected = vcpu->arch.nmi_injected;
2231 events->nmi.pending = vcpu->arch.nmi_pending;
2232 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2234 events->sipi_vector = vcpu->arch.sipi_vector;
2236 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
2237 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2238 | KVM_VCPUEVENT_VALID_SHADOW);
2241 static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2242 struct kvm_vcpu_events *events)
2244 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2245 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2246 | KVM_VCPUEVENT_VALID_SHADOW))
2249 vcpu->arch.exception.pending = events->exception.injected;
2250 vcpu->arch.exception.nr = events->exception.nr;
2251 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2252 vcpu->arch.exception.error_code = events->exception.error_code;
2254 vcpu->arch.interrupt.pending = events->interrupt.injected;
2255 vcpu->arch.interrupt.nr = events->interrupt.nr;
2256 vcpu->arch.interrupt.soft = events->interrupt.soft;
2257 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2258 kvm_pic_clear_isr_ack(vcpu->kvm);
2259 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2260 kvm_x86_ops->set_interrupt_shadow(vcpu,
2261 events->interrupt.shadow);
2263 vcpu->arch.nmi_injected = events->nmi.injected;
2264 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2265 vcpu->arch.nmi_pending = events->nmi.pending;
2266 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2268 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2269 vcpu->arch.sipi_vector = events->sipi_vector;
2274 static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2275 struct kvm_debugregs *dbgregs)
2277 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
2278 dbgregs->dr6 = vcpu->arch.dr6;
2279 dbgregs->dr7 = vcpu->arch.dr7;
2283 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2284 struct kvm_debugregs *dbgregs)
2289 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
2290 vcpu->arch.dr6 = dbgregs->dr6;
2291 vcpu->arch.dr7 = dbgregs->dr7;
2296 long kvm_arch_vcpu_ioctl(struct file *filp,
2297 unsigned int ioctl, unsigned long arg)
2299 struct kvm_vcpu *vcpu = filp->private_data;
2300 void __user *argp = (void __user *)arg;
2302 struct kvm_lapic_state *lapic = NULL;
2305 case KVM_GET_LAPIC: {
2307 if (!vcpu->arch.apic)
2309 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2314 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
2318 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
2323 case KVM_SET_LAPIC: {
2325 if (!vcpu->arch.apic)
2327 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2332 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2334 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
2340 case KVM_INTERRUPT: {
2341 struct kvm_interrupt irq;
2344 if (copy_from_user(&irq, argp, sizeof irq))
2346 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2353 r = kvm_vcpu_ioctl_nmi(vcpu);
2359 case KVM_SET_CPUID: {
2360 struct kvm_cpuid __user *cpuid_arg = argp;
2361 struct kvm_cpuid cpuid;
2364 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2366 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2371 case KVM_SET_CPUID2: {
2372 struct kvm_cpuid2 __user *cpuid_arg = argp;
2373 struct kvm_cpuid2 cpuid;
2376 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2378 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
2379 cpuid_arg->entries);
2384 case KVM_GET_CPUID2: {
2385 struct kvm_cpuid2 __user *cpuid_arg = argp;
2386 struct kvm_cpuid2 cpuid;
2389 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2391 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
2392 cpuid_arg->entries);
2396 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2402 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2405 r = msr_io(vcpu, argp, do_set_msr, 0);
2407 case KVM_TPR_ACCESS_REPORTING: {
2408 struct kvm_tpr_access_ctl tac;
2411 if (copy_from_user(&tac, argp, sizeof tac))
2413 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2417 if (copy_to_user(argp, &tac, sizeof tac))
2422 case KVM_SET_VAPIC_ADDR: {
2423 struct kvm_vapic_addr va;
2426 if (!irqchip_in_kernel(vcpu->kvm))
2429 if (copy_from_user(&va, argp, sizeof va))
2432 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2435 case KVM_X86_SETUP_MCE: {
2439 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2441 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2444 case KVM_X86_SET_MCE: {
2445 struct kvm_x86_mce mce;
2448 if (copy_from_user(&mce, argp, sizeof mce))
2450 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2453 case KVM_GET_VCPU_EVENTS: {
2454 struct kvm_vcpu_events events;
2456 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2459 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2464 case KVM_SET_VCPU_EVENTS: {
2465 struct kvm_vcpu_events events;
2468 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2471 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2474 case KVM_GET_DEBUGREGS: {
2475 struct kvm_debugregs dbgregs;
2477 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2480 if (copy_to_user(argp, &dbgregs,
2481 sizeof(struct kvm_debugregs)))
2486 case KVM_SET_DEBUGREGS: {
2487 struct kvm_debugregs dbgregs;
2490 if (copy_from_user(&dbgregs, argp,
2491 sizeof(struct kvm_debugregs)))
2494 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2505 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2509 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2511 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2515 static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2518 kvm->arch.ept_identity_map_addr = ident_addr;
2522 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2523 u32 kvm_nr_mmu_pages)
2525 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2528 mutex_lock(&kvm->slots_lock);
2529 spin_lock(&kvm->mmu_lock);
2531 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
2532 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
2534 spin_unlock(&kvm->mmu_lock);
2535 mutex_unlock(&kvm->slots_lock);
2539 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2541 return kvm->arch.n_alloc_mmu_pages;
2544 gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2547 struct kvm_mem_alias *alias;
2548 struct kvm_mem_aliases *aliases;
2550 aliases = kvm_aliases(kvm);
2552 for (i = 0; i < aliases->naliases; ++i) {
2553 alias = &aliases->aliases[i];
2554 if (alias->flags & KVM_ALIAS_INVALID)
2556 if (gfn >= alias->base_gfn
2557 && gfn < alias->base_gfn + alias->npages)
2558 return alias->target_gfn + gfn - alias->base_gfn;
2563 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2566 struct kvm_mem_alias *alias;
2567 struct kvm_mem_aliases *aliases;
2569 aliases = kvm_aliases(kvm);
2571 for (i = 0; i < aliases->naliases; ++i) {
2572 alias = &aliases->aliases[i];
2573 if (gfn >= alias->base_gfn
2574 && gfn < alias->base_gfn + alias->npages)
2575 return alias->target_gfn + gfn - alias->base_gfn;
2581 * Set a new alias region. Aliases map a portion of physical memory into
2582 * another portion. This is useful for memory windows, for example the PC
2585 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2586 struct kvm_memory_alias *alias)
2589 struct kvm_mem_alias *p;
2590 struct kvm_mem_aliases *aliases, *old_aliases;
2593 /* General sanity checks */
2594 if (alias->memory_size & (PAGE_SIZE - 1))
2596 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2598 if (alias->slot >= KVM_ALIAS_SLOTS)
2600 if (alias->guest_phys_addr + alias->memory_size
2601 < alias->guest_phys_addr)
2603 if (alias->target_phys_addr + alias->memory_size
2604 < alias->target_phys_addr)
2608 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2612 mutex_lock(&kvm->slots_lock);
2614 /* invalidate any gfn reference in case of deletion/shrinking */
2615 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2616 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2617 old_aliases = kvm->arch.aliases;
2618 rcu_assign_pointer(kvm->arch.aliases, aliases);
2619 synchronize_srcu_expedited(&kvm->srcu);
2620 kvm_mmu_zap_all(kvm);
2624 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2628 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2630 p = &aliases->aliases[alias->slot];
2631 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2632 p->npages = alias->memory_size >> PAGE_SHIFT;
2633 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
2634 p->flags &= ~(KVM_ALIAS_INVALID);
2636 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
2637 if (aliases->aliases[n - 1].npages)
2639 aliases->naliases = n;
2641 old_aliases = kvm->arch.aliases;
2642 rcu_assign_pointer(kvm->arch.aliases, aliases);
2643 synchronize_srcu_expedited(&kvm->srcu);
2648 mutex_unlock(&kvm->slots_lock);
2653 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2658 switch (chip->chip_id) {
2659 case KVM_IRQCHIP_PIC_MASTER:
2660 memcpy(&chip->chip.pic,
2661 &pic_irqchip(kvm)->pics[0],
2662 sizeof(struct kvm_pic_state));
2664 case KVM_IRQCHIP_PIC_SLAVE:
2665 memcpy(&chip->chip.pic,
2666 &pic_irqchip(kvm)->pics[1],
2667 sizeof(struct kvm_pic_state));
2669 case KVM_IRQCHIP_IOAPIC:
2670 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
2679 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2684 switch (chip->chip_id) {
2685 case KVM_IRQCHIP_PIC_MASTER:
2686 raw_spin_lock(&pic_irqchip(kvm)->lock);
2687 memcpy(&pic_irqchip(kvm)->pics[0],
2689 sizeof(struct kvm_pic_state));
2690 raw_spin_unlock(&pic_irqchip(kvm)->lock);
2692 case KVM_IRQCHIP_PIC_SLAVE:
2693 raw_spin_lock(&pic_irqchip(kvm)->lock);
2694 memcpy(&pic_irqchip(kvm)->pics[1],
2696 sizeof(struct kvm_pic_state));
2697 raw_spin_unlock(&pic_irqchip(kvm)->lock);
2699 case KVM_IRQCHIP_IOAPIC:
2700 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
2706 kvm_pic_update_irq(pic_irqchip(kvm));
2710 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2714 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2715 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
2716 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2720 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2724 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2725 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
2726 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2727 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2731 static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2735 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2736 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2737 sizeof(ps->channels));
2738 ps->flags = kvm->arch.vpit->pit_state.flags;
2739 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2743 static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2745 int r = 0, start = 0;
2746 u32 prev_legacy, cur_legacy;
2747 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2748 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2749 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2750 if (!prev_legacy && cur_legacy)
2752 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2753 sizeof(kvm->arch.vpit->pit_state.channels));
2754 kvm->arch.vpit->pit_state.flags = ps->flags;
2755 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
2756 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2760 static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2761 struct kvm_reinject_control *control)
2763 if (!kvm->arch.vpit)
2765 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2766 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
2767 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2772 * Get (and clear) the dirty memory log for a memory slot.
2774 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2775 struct kvm_dirty_log *log)
2778 struct kvm_memory_slot *memslot;
2780 unsigned long is_dirty = 0;
2782 mutex_lock(&kvm->slots_lock);
2785 if (log->slot >= KVM_MEMORY_SLOTS)
2788 memslot = &kvm->memslots->memslots[log->slot];
2790 if (!memslot->dirty_bitmap)
2793 n = kvm_dirty_bitmap_bytes(memslot);
2795 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2796 is_dirty = memslot->dirty_bitmap[i];
2798 /* If nothing is dirty, don't bother messing with page tables. */
2800 struct kvm_memslots *slots, *old_slots;
2801 unsigned long *dirty_bitmap;
2803 spin_lock(&kvm->mmu_lock);
2804 kvm_mmu_slot_remove_write_access(kvm, log->slot);
2805 spin_unlock(&kvm->mmu_lock);
2808 dirty_bitmap = vmalloc(n);
2811 memset(dirty_bitmap, 0, n);
2814 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2816 vfree(dirty_bitmap);
2819 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2820 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2822 old_slots = kvm->memslots;
2823 rcu_assign_pointer(kvm->memslots, slots);
2824 synchronize_srcu_expedited(&kvm->srcu);
2825 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2829 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) {
2830 vfree(dirty_bitmap);
2833 vfree(dirty_bitmap);
2836 if (clear_user(log->dirty_bitmap, n))
2842 mutex_unlock(&kvm->slots_lock);
2846 long kvm_arch_vm_ioctl(struct file *filp,
2847 unsigned int ioctl, unsigned long arg)
2849 struct kvm *kvm = filp->private_data;
2850 void __user *argp = (void __user *)arg;
2853 * This union makes it completely explicit to gcc-3.x
2854 * that these two variables' stack usage should be
2855 * combined, not added together.
2858 struct kvm_pit_state ps;
2859 struct kvm_pit_state2 ps2;
2860 struct kvm_memory_alias alias;
2861 struct kvm_pit_config pit_config;
2865 case KVM_SET_TSS_ADDR:
2866 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2870 case KVM_SET_IDENTITY_MAP_ADDR: {
2874 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2876 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2881 case KVM_SET_MEMORY_REGION: {
2882 struct kvm_memory_region kvm_mem;
2883 struct kvm_userspace_memory_region kvm_userspace_mem;
2886 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2888 kvm_userspace_mem.slot = kvm_mem.slot;
2889 kvm_userspace_mem.flags = kvm_mem.flags;
2890 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2891 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2892 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2897 case KVM_SET_NR_MMU_PAGES:
2898 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2902 case KVM_GET_NR_MMU_PAGES:
2903 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2905 case KVM_SET_MEMORY_ALIAS:
2907 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
2909 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
2913 case KVM_CREATE_IRQCHIP: {
2914 struct kvm_pic *vpic;
2916 mutex_lock(&kvm->lock);
2919 goto create_irqchip_unlock;
2921 vpic = kvm_create_pic(kvm);
2923 r = kvm_ioapic_init(kvm);
2925 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
2928 goto create_irqchip_unlock;
2931 goto create_irqchip_unlock;
2933 kvm->arch.vpic = vpic;
2935 r = kvm_setup_default_irq_routing(kvm);
2937 mutex_lock(&kvm->irq_lock);
2938 kvm_ioapic_destroy(kvm);
2939 kvm_destroy_pic(kvm);
2940 mutex_unlock(&kvm->irq_lock);
2942 create_irqchip_unlock:
2943 mutex_unlock(&kvm->lock);
2946 case KVM_CREATE_PIT:
2947 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2949 case KVM_CREATE_PIT2:
2951 if (copy_from_user(&u.pit_config, argp,
2952 sizeof(struct kvm_pit_config)))
2955 mutex_lock(&kvm->slots_lock);
2958 goto create_pit_unlock;
2960 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
2964 mutex_unlock(&kvm->slots_lock);
2966 case KVM_IRQ_LINE_STATUS:
2967 case KVM_IRQ_LINE: {
2968 struct kvm_irq_level irq_event;
2971 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2974 if (irqchip_in_kernel(kvm)) {
2976 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2977 irq_event.irq, irq_event.level);
2978 if (ioctl == KVM_IRQ_LINE_STATUS) {
2980 irq_event.status = status;
2981 if (copy_to_user(argp, &irq_event,
2989 case KVM_GET_IRQCHIP: {
2990 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2991 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
2997 if (copy_from_user(chip, argp, sizeof *chip))
2998 goto get_irqchip_out;
3000 if (!irqchip_in_kernel(kvm))
3001 goto get_irqchip_out;
3002 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3004 goto get_irqchip_out;
3006 if (copy_to_user(argp, chip, sizeof *chip))
3007 goto get_irqchip_out;
3015 case KVM_SET_IRQCHIP: {
3016 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3017 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
3023 if (copy_from_user(chip, argp, sizeof *chip))
3024 goto set_irqchip_out;
3026 if (!irqchip_in_kernel(kvm))
3027 goto set_irqchip_out;
3028 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3030 goto set_irqchip_out;
3040 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
3043 if (!kvm->arch.vpit)
3045 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
3049 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
3056 if (copy_from_user(&u.ps, argp, sizeof u.ps))
3059 if (!kvm->arch.vpit)
3061 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
3067 case KVM_GET_PIT2: {
3069 if (!kvm->arch.vpit)
3071 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3075 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3080 case KVM_SET_PIT2: {
3082 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3085 if (!kvm->arch.vpit)
3087 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
3093 case KVM_REINJECT_CONTROL: {
3094 struct kvm_reinject_control control;
3096 if (copy_from_user(&control, argp, sizeof(control)))
3098 r = kvm_vm_ioctl_reinject(kvm, &control);
3104 case KVM_XEN_HVM_CONFIG: {
3106 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3107 sizeof(struct kvm_xen_hvm_config)))
3110 if (kvm->arch.xen_hvm_config.flags)
3115 case KVM_SET_CLOCK: {
3116 struct timespec now;
3117 struct kvm_clock_data user_ns;
3122 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3131 now_ns = timespec_to_ns(&now);
3132 delta = user_ns.clock - now_ns;
3133 kvm->arch.kvmclock_offset = delta;
3136 case KVM_GET_CLOCK: {
3137 struct timespec now;
3138 struct kvm_clock_data user_ns;
3142 now_ns = timespec_to_ns(&now);
3143 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
3147 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3160 static void kvm_init_msr_list(void)
3165 /* skip the first msrs in the list. KVM-specific */
3166 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
3167 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3170 msrs_to_save[j] = msrs_to_save[i];
3173 num_msrs_to_save = j;
3176 static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3179 if (vcpu->arch.apic &&
3180 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3183 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
3186 static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
3188 if (vcpu->arch.apic &&
3189 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3192 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
3195 static void kvm_set_segment(struct kvm_vcpu *vcpu,
3196 struct kvm_segment *var, int seg)
3198 kvm_x86_ops->set_segment(vcpu, var, seg);
3201 void kvm_get_segment(struct kvm_vcpu *vcpu,
3202 struct kvm_segment *var, int seg)
3204 kvm_x86_ops->get_segment(vcpu, var, seg);
3207 gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3209 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3210 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3213 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3215 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3216 access |= PFERR_FETCH_MASK;
3217 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3220 gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3222 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3223 access |= PFERR_WRITE_MASK;
3224 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3227 /* uses this to access any guest's mapped memory without checking CPL */
3228 gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3230 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3233 static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3234 struct kvm_vcpu *vcpu, u32 access,
3238 int r = X86EMUL_CONTINUE;
3241 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
3242 unsigned offset = addr & (PAGE_SIZE-1);
3243 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
3246 if (gpa == UNMAPPED_GVA) {
3247 r = X86EMUL_PROPAGATE_FAULT;
3250 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
3252 r = X86EMUL_IO_NEEDED;
3264 /* used for instruction fetching */
3265 static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3266 struct kvm_vcpu *vcpu, u32 *error)
3268 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3269 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3270 access | PFERR_FETCH_MASK, error);
3273 static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3274 struct kvm_vcpu *vcpu, u32 *error)
3276 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3277 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3281 static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3282 struct kvm_vcpu *vcpu, u32 *error)
3284 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3287 static int kvm_write_guest_virt_system(gva_t addr, void *val,
3289 struct kvm_vcpu *vcpu,
3293 int r = X86EMUL_CONTINUE;
3296 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr,
3297 PFERR_WRITE_MASK, error);
3298 unsigned offset = addr & (PAGE_SIZE-1);
3299 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3302 if (gpa == UNMAPPED_GVA) {
3303 r = X86EMUL_PROPAGATE_FAULT;
3306 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3308 r = X86EMUL_IO_NEEDED;
3320 static int emulator_read_emulated(unsigned long addr,
3323 unsigned int *error_code,
3324 struct kvm_vcpu *vcpu)
3328 if (vcpu->mmio_read_completed) {
3329 memcpy(val, vcpu->mmio_data, bytes);
3330 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3331 vcpu->mmio_phys_addr, *(u64 *)val);
3332 vcpu->mmio_read_completed = 0;
3333 return X86EMUL_CONTINUE;
3336 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, error_code);
3338 if (gpa == UNMAPPED_GVA)
3339 return X86EMUL_PROPAGATE_FAULT;
3341 /* For APIC access vmexit */
3342 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3345 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
3346 == X86EMUL_CONTINUE)
3347 return X86EMUL_CONTINUE;
3351 * Is this MMIO handled locally?
3353 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3354 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
3355 return X86EMUL_CONTINUE;
3358 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
3360 vcpu->mmio_needed = 1;
3361 vcpu->run->exit_reason = KVM_EXIT_MMIO;
3362 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
3363 vcpu->run->mmio.len = vcpu->mmio_size = bytes;
3364 vcpu->run->mmio.is_write = vcpu->mmio_is_write = 0;
3366 return X86EMUL_IO_NEEDED;
3369 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
3370 const void *val, int bytes)
3374 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3377 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
3381 static int emulator_write_emulated_onepage(unsigned long addr,
3384 unsigned int *error_code,
3385 struct kvm_vcpu *vcpu)
3389 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error_code);
3391 if (gpa == UNMAPPED_GVA)
3392 return X86EMUL_PROPAGATE_FAULT;
3394 /* For APIC access vmexit */
3395 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3398 if (emulator_write_phys(vcpu, gpa, val, bytes))
3399 return X86EMUL_CONTINUE;
3402 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
3404 * Is this MMIO handled locally?
3406 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
3407 return X86EMUL_CONTINUE;
3409 vcpu->mmio_needed = 1;
3410 vcpu->run->exit_reason = KVM_EXIT_MMIO;
3411 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
3412 vcpu->run->mmio.len = vcpu->mmio_size = bytes;
3413 vcpu->run->mmio.is_write = vcpu->mmio_is_write = 1;
3414 memcpy(vcpu->run->mmio.data, val, bytes);
3416 return X86EMUL_CONTINUE;
3419 int emulator_write_emulated(unsigned long addr,
3422 unsigned int *error_code,
3423 struct kvm_vcpu *vcpu)
3425 /* Crossing a page boundary? */
3426 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3429 now = -addr & ~PAGE_MASK;
3430 rc = emulator_write_emulated_onepage(addr, val, now, error_code,
3432 if (rc != X86EMUL_CONTINUE)
3438 return emulator_write_emulated_onepage(addr, val, bytes, error_code,
3442 #define CMPXCHG_TYPE(t, ptr, old, new) \
3443 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3445 #ifdef CONFIG_X86_64
3446 # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3448 # define CMPXCHG64(ptr, old, new) \
3449 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
3452 static int emulator_cmpxchg_emulated(unsigned long addr,
3456 unsigned int *error_code,
3457 struct kvm_vcpu *vcpu)
3464 /* guests cmpxchg8b have to be emulated atomically */
3465 if (bytes > 8 || (bytes & (bytes - 1)))
3468 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
3470 if (gpa == UNMAPPED_GVA ||
3471 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3474 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3477 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
3479 kaddr = kmap_atomic(page, KM_USER0);
3480 kaddr += offset_in_page(gpa);
3483 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
3486 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
3489 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
3492 exchanged = CMPXCHG64(kaddr, old, new);
3497 kunmap_atomic(kaddr, KM_USER0);
3498 kvm_release_page_dirty(page);
3501 return X86EMUL_CMPXCHG_FAILED;
3503 kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1);
3505 return X86EMUL_CONTINUE;
3508 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
3510 return emulator_write_emulated(addr, new, bytes, error_code, vcpu);
3513 static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
3515 /* TODO: String I/O for in kernel device */
3518 if (vcpu->arch.pio.in)
3519 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
3520 vcpu->arch.pio.size, pd);
3522 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3523 vcpu->arch.pio.port, vcpu->arch.pio.size,
3529 static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
3530 unsigned int count, struct kvm_vcpu *vcpu)
3532 if (vcpu->arch.pio.count)
3535 trace_kvm_pio(1, port, size, 1);
3537 vcpu->arch.pio.port = port;
3538 vcpu->arch.pio.in = 1;
3539 vcpu->arch.pio.count = count;
3540 vcpu->arch.pio.size = size;
3542 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3544 memcpy(val, vcpu->arch.pio_data, size * count);
3545 vcpu->arch.pio.count = 0;
3549 vcpu->run->exit_reason = KVM_EXIT_IO;
3550 vcpu->run->io.direction = KVM_EXIT_IO_IN;
3551 vcpu->run->io.size = size;
3552 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3553 vcpu->run->io.count = count;
3554 vcpu->run->io.port = port;
3559 static int emulator_pio_out_emulated(int size, unsigned short port,
3560 const void *val, unsigned int count,
3561 struct kvm_vcpu *vcpu)
3563 trace_kvm_pio(0, port, size, 1);
3565 vcpu->arch.pio.port = port;
3566 vcpu->arch.pio.in = 0;
3567 vcpu->arch.pio.count = count;
3568 vcpu->arch.pio.size = size;
3570 memcpy(vcpu->arch.pio_data, val, size * count);
3572 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3573 vcpu->arch.pio.count = 0;
3577 vcpu->run->exit_reason = KVM_EXIT_IO;
3578 vcpu->run->io.direction = KVM_EXIT_IO_OUT;
3579 vcpu->run->io.size = size;
3580 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3581 vcpu->run->io.count = count;
3582 vcpu->run->io.port = port;
3587 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3589 return kvm_x86_ops->get_segment_base(vcpu, seg);
3592 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3594 kvm_mmu_invlpg(vcpu, address);
3595 return X86EMUL_CONTINUE;
3598 int emulate_clts(struct kvm_vcpu *vcpu)
3600 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
3601 kvm_x86_ops->fpu_activate(vcpu);
3602 return X86EMUL_CONTINUE;
3605 int emulator_get_dr(int dr, unsigned long *dest, struct kvm_vcpu *vcpu)
3607 return _kvm_get_dr(vcpu, dr, dest);
3610 int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu)
3613 return __kvm_set_dr(vcpu, dr, value);
3616 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3618 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3621 static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
3623 unsigned long value;
3627 value = kvm_read_cr0(vcpu);
3630 value = vcpu->arch.cr2;
3633 value = vcpu->arch.cr3;
3636 value = kvm_read_cr4(vcpu);
3639 value = kvm_get_cr8(vcpu);
3642 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3649 static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
3655 res = __kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
3658 vcpu->arch.cr2 = val;
3661 res = __kvm_set_cr3(vcpu, val);
3664 res = __kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
3667 res = __kvm_set_cr8(vcpu, val & 0xfUL);
3670 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3677 static int emulator_get_cpl(struct kvm_vcpu *vcpu)
3679 return kvm_x86_ops->get_cpl(vcpu);
3682 static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
3684 kvm_x86_ops->get_gdt(vcpu, dt);
3687 static unsigned long emulator_get_cached_segment_base(int seg,
3688 struct kvm_vcpu *vcpu)
3690 return get_segment_base(vcpu, seg);
3693 static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg,
3694 struct kvm_vcpu *vcpu)
3696 struct kvm_segment var;
3698 kvm_get_segment(vcpu, &var, seg);
3705 set_desc_limit(desc, var.limit);
3706 set_desc_base(desc, (unsigned long)var.base);
3707 desc->type = var.type;
3709 desc->dpl = var.dpl;
3710 desc->p = var.present;
3711 desc->avl = var.avl;
3719 static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg,
3720 struct kvm_vcpu *vcpu)
3722 struct kvm_segment var;
3724 /* needed to preserve selector */
3725 kvm_get_segment(vcpu, &var, seg);
3727 var.base = get_desc_base(desc);
3728 var.limit = get_desc_limit(desc);
3730 var.limit = (var.limit << 12) | 0xfff;
3731 var.type = desc->type;
3732 var.present = desc->p;
3733 var.dpl = desc->dpl;
3738 var.avl = desc->avl;
3739 var.present = desc->p;
3740 var.unusable = !var.present;
3743 kvm_set_segment(vcpu, &var, seg);
3747 static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
3749 struct kvm_segment kvm_seg;
3751 kvm_get_segment(vcpu, &kvm_seg, seg);
3752 return kvm_seg.selector;
3755 static void emulator_set_segment_selector(u16 sel, int seg,
3756 struct kvm_vcpu *vcpu)
3758 struct kvm_segment kvm_seg;
3760 kvm_get_segment(vcpu, &kvm_seg, seg);
3761 kvm_seg.selector = sel;
3762 kvm_set_segment(vcpu, &kvm_seg, seg);
3765 static struct x86_emulate_ops emulate_ops = {
3766 .read_std = kvm_read_guest_virt_system,
3767 .write_std = kvm_write_guest_virt_system,
3768 .fetch = kvm_fetch_guest_virt,
3769 .read_emulated = emulator_read_emulated,
3770 .write_emulated = emulator_write_emulated,
3771 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3772 .pio_in_emulated = emulator_pio_in_emulated,
3773 .pio_out_emulated = emulator_pio_out_emulated,
3774 .get_cached_descriptor = emulator_get_cached_descriptor,
3775 .set_cached_descriptor = emulator_set_cached_descriptor,
3776 .get_segment_selector = emulator_get_segment_selector,
3777 .set_segment_selector = emulator_set_segment_selector,
3778 .get_cached_segment_base = emulator_get_cached_segment_base,
3779 .get_gdt = emulator_get_gdt,
3780 .get_cr = emulator_get_cr,
3781 .set_cr = emulator_set_cr,
3782 .cpl = emulator_get_cpl,
3783 .get_dr = emulator_get_dr,
3784 .set_dr = emulator_set_dr,
3785 .set_msr = kvm_set_msr,
3786 .get_msr = kvm_get_msr,
3789 static void cache_all_regs(struct kvm_vcpu *vcpu)
3791 kvm_register_read(vcpu, VCPU_REGS_RAX);
3792 kvm_register_read(vcpu, VCPU_REGS_RSP);
3793 kvm_register_read(vcpu, VCPU_REGS_RIP);
3794 vcpu->arch.regs_dirty = ~0;
3797 static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
3799 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask);
3801 * an sti; sti; sequence only disable interrupts for the first
3802 * instruction. So, if the last instruction, be it emulated or
3803 * not, left the system with the INT_STI flag enabled, it
3804 * means that the last instruction is an sti. We should not
3805 * leave the flag on in this case. The same goes for mov ss
3807 if (!(int_shadow & mask))
3808 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
3811 static void inject_emulated_exception(struct kvm_vcpu *vcpu)
3813 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
3814 if (ctxt->exception == PF_VECTOR)
3815 kvm_inject_page_fault(vcpu, ctxt->cr2, ctxt->error_code);
3816 else if (ctxt->error_code_valid)
3817 kvm_queue_exception_e(vcpu, ctxt->exception, ctxt->error_code);
3819 kvm_queue_exception(vcpu, ctxt->exception);
3822 static int handle_emulation_failure(struct kvm_vcpu *vcpu)
3824 ++vcpu->stat.insn_emulation_fail;
3825 trace_kvm_emulate_insn_failed(vcpu);
3826 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3827 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3828 vcpu->run->internal.ndata = 0;
3829 kvm_queue_exception(vcpu, UD_VECTOR);
3830 return EMULATE_FAIL;
3833 int emulate_instruction(struct kvm_vcpu *vcpu,
3839 struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
3841 kvm_clear_exception_queue(vcpu);
3842 vcpu->arch.mmio_fault_cr2 = cr2;
3844 * TODO: fix emulate.c to use guest_read/write_register
3845 * instead of direct ->regs accesses, can save hundred cycles
3846 * on Intel for instructions that don't read/change RSP, for
3849 cache_all_regs(vcpu);
3851 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
3853 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3855 vcpu->arch.emulate_ctxt.vcpu = vcpu;
3856 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
3857 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
3858 vcpu->arch.emulate_ctxt.mode =
3859 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
3860 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
3861 ? X86EMUL_MODE_VM86 : cs_l
3862 ? X86EMUL_MODE_PROT64 : cs_db
3863 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3864 memset(c, 0, sizeof(struct decode_cache));
3865 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
3866 vcpu->arch.emulate_ctxt.interruptibility = 0;
3867 vcpu->arch.emulate_ctxt.exception = -1;
3869 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
3870 trace_kvm_emulate_insn_start(vcpu);
3872 /* Only allow emulation of specific instructions on #UD
3873 * (namely VMMCALL, sysenter, sysexit, syscall)*/
3874 if (emulation_type & EMULTYPE_TRAP_UD) {
3876 return EMULATE_FAIL;
3878 case 0x01: /* VMMCALL */
3879 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3880 return EMULATE_FAIL;
3882 case 0x34: /* sysenter */
3883 case 0x35: /* sysexit */
3884 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3885 return EMULATE_FAIL;
3887 case 0x05: /* syscall */
3888 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3889 return EMULATE_FAIL;
3892 return EMULATE_FAIL;
3895 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3896 return EMULATE_FAIL;
3899 ++vcpu->stat.insn_emulation;
3901 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3902 return EMULATE_DONE;
3903 if (emulation_type & EMULTYPE_SKIP)
3904 return EMULATE_FAIL;
3905 return handle_emulation_failure(vcpu);
3909 if (emulation_type & EMULTYPE_SKIP) {
3910 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3911 return EMULATE_DONE;
3914 /* this is needed for vmware backdor interface to work since it
3915 changes registers values during IO operation */
3916 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
3919 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
3921 if (r) { /* emulation failed */
3923 * if emulation was due to access to shadowed page table
3924 * and it failed try to unshadow page and re-entetr the
3925 * guest to let CPU execute the instruction.
3927 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3928 return EMULATE_DONE;
3930 return handle_emulation_failure(vcpu);
3933 toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility);
3934 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
3935 memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
3936 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
3938 if (vcpu->arch.emulate_ctxt.exception >= 0) {
3939 inject_emulated_exception(vcpu);
3940 return EMULATE_DONE;
3943 if (vcpu->arch.pio.count) {
3944 if (!vcpu->arch.pio.in)
3945 vcpu->arch.pio.count = 0;
3946 return EMULATE_DO_MMIO;
3949 if (vcpu->mmio_needed) {
3950 if (vcpu->mmio_is_write)
3951 vcpu->mmio_needed = 0;
3952 return EMULATE_DO_MMIO;
3955 if (vcpu->arch.emulate_ctxt.restart)
3958 return EMULATE_DONE;
3960 EXPORT_SYMBOL_GPL(emulate_instruction);
3962 int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
3964 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3965 int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
3966 /* do not return to emulator after return from userspace */
3967 vcpu->arch.pio.count = 0;
3970 EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
3972 static void bounce_off(void *info)
3977 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3980 struct cpufreq_freqs *freq = data;
3982 struct kvm_vcpu *vcpu;
3983 int i, send_ipi = 0;
3985 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3987 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3989 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
3991 spin_lock(&kvm_lock);
3992 list_for_each_entry(kvm, &vm_list, vm_list) {
3993 kvm_for_each_vcpu(i, vcpu, kvm) {
3994 if (vcpu->cpu != freq->cpu)
3996 if (!kvm_request_guest_time_update(vcpu))
3998 if (vcpu->cpu != smp_processor_id())
4002 spin_unlock(&kvm_lock);
4004 if (freq->old < freq->new && send_ipi) {
4006 * We upscale the frequency. Must make the guest
4007 * doesn't see old kvmclock values while running with
4008 * the new frequency, otherwise we risk the guest sees
4009 * time go backwards.
4011 * In case we update the frequency for another cpu
4012 * (which might be in guest context) send an interrupt
4013 * to kick the cpu out of guest context. Next time
4014 * guest context is entered kvmclock will be updated,
4015 * so the guest will not see stale values.
4017 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
4022 static struct notifier_block kvmclock_cpufreq_notifier_block = {
4023 .notifier_call = kvmclock_cpufreq_notifier
4026 static void kvm_timer_init(void)
4030 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
4031 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
4032 CPUFREQ_TRANSITION_NOTIFIER);
4033 for_each_online_cpu(cpu) {
4034 unsigned long khz = cpufreq_get(cpu);
4037 per_cpu(cpu_tsc_khz, cpu) = khz;
4040 for_each_possible_cpu(cpu)
4041 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
4045 static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
4047 static int kvm_is_in_guest(void)
4049 return percpu_read(current_vcpu) != NULL;
4052 static int kvm_is_user_mode(void)
4056 if (percpu_read(current_vcpu))
4057 user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
4059 return user_mode != 0;
4062 static unsigned long kvm_get_guest_ip(void)
4064 unsigned long ip = 0;
4066 if (percpu_read(current_vcpu))
4067 ip = kvm_rip_read(percpu_read(current_vcpu));
4072 static struct perf_guest_info_callbacks kvm_guest_cbs = {
4073 .is_in_guest = kvm_is_in_guest,
4074 .is_user_mode = kvm_is_user_mode,
4075 .get_guest_ip = kvm_get_guest_ip,
4078 void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
4080 percpu_write(current_vcpu, vcpu);
4082 EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
4084 void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
4086 percpu_write(current_vcpu, NULL);
4088 EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
4090 int kvm_arch_init(void *opaque)
4093 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
4096 printk(KERN_ERR "kvm: already loaded the other module\n");
4101 if (!ops->cpu_has_kvm_support()) {
4102 printk(KERN_ERR "kvm: no hardware support\n");
4106 if (ops->disabled_by_bios()) {
4107 printk(KERN_ERR "kvm: disabled by bios\n");
4112 r = kvm_mmu_module_init();
4116 kvm_init_msr_list();
4119 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
4120 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
4121 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
4122 PT_DIRTY_MASK, PT64_NX_MASK, 0);
4126 perf_register_guest_info_callbacks(&kvm_guest_cbs);
4134 void kvm_arch_exit(void)
4136 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
4138 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
4139 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
4140 CPUFREQ_TRANSITION_NOTIFIER);
4142 kvm_mmu_module_exit();
4145 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
4147 ++vcpu->stat.halt_exits;
4148 if (irqchip_in_kernel(vcpu->kvm)) {
4149 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
4152 vcpu->run->exit_reason = KVM_EXIT_HLT;
4156 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
4158 static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
4161 if (is_long_mode(vcpu))
4164 return a0 | ((gpa_t)a1 << 32);
4167 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
4169 u64 param, ingpa, outgpa, ret;
4170 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
4171 bool fast, longmode;
4175 * hypercall generates UD from non zero cpl and real mode
4178 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
4179 kvm_queue_exception(vcpu, UD_VECTOR);
4183 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4184 longmode = is_long_mode(vcpu) && cs_l == 1;
4187 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
4188 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
4189 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
4190 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
4191 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
4192 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
4194 #ifdef CONFIG_X86_64
4196 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
4197 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
4198 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
4202 code = param & 0xffff;
4203 fast = (param >> 16) & 0x1;
4204 rep_cnt = (param >> 32) & 0xfff;
4205 rep_idx = (param >> 48) & 0xfff;
4207 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
4210 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
4211 kvm_vcpu_on_spin(vcpu);
4214 res = HV_STATUS_INVALID_HYPERCALL_CODE;
4218 ret = res | (((u64)rep_done & 0xfff) << 32);
4220 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
4222 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
4223 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
4229 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
4231 unsigned long nr, a0, a1, a2, a3, ret;
4234 if (kvm_hv_hypercall_enabled(vcpu->kvm))
4235 return kvm_hv_hypercall(vcpu);
4237 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
4238 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
4239 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
4240 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
4241 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
4243 trace_kvm_hypercall(nr, a0, a1, a2, a3);
4245 if (!is_long_mode(vcpu)) {
4253 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
4259 case KVM_HC_VAPIC_POLL_IRQ:
4263 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
4270 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
4271 ++vcpu->stat.hypercalls;
4274 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
4276 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
4278 char instruction[3];
4279 unsigned long rip = kvm_rip_read(vcpu);
4282 * Blow out the MMU to ensure that no other VCPU has an active mapping
4283 * to ensure that the updated hypercall appears atomically across all
4286 kvm_mmu_zap_all(vcpu->kvm);
4288 kvm_x86_ops->patch_hypercall(vcpu, instruction);
4290 return emulator_write_emulated(rip, instruction, 3, NULL, vcpu);
4293 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4295 struct desc_ptr dt = { limit, base };
4297 kvm_x86_ops->set_gdt(vcpu, &dt);
4300 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4302 struct desc_ptr dt = { limit, base };
4304 kvm_x86_ops->set_idt(vcpu, &dt);
4307 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4309 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4310 int j, nent = vcpu->arch.cpuid_nent;
4312 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4313 /* when no next entry is found, the current entry[i] is reselected */
4314 for (j = i + 1; ; j = (j + 1) % nent) {
4315 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
4316 if (ej->function == e->function) {
4317 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4321 return 0; /* silence gcc, even though control never reaches here */
4324 /* find an entry with matching function, matching index (if needed), and that
4325 * should be read next (if it's stateful) */
4326 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4327 u32 function, u32 index)
4329 if (e->function != function)
4331 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4333 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
4334 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
4339 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4340 u32 function, u32 index)
4343 struct kvm_cpuid_entry2 *best = NULL;
4345 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
4346 struct kvm_cpuid_entry2 *e;
4348 e = &vcpu->arch.cpuid_entries[i];
4349 if (is_matching_cpuid_entry(e, function, index)) {
4350 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4351 move_to_next_stateful_cpuid_entry(vcpu, i);
4356 * Both basic or both extended?
4358 if (((e->function ^ function) & 0x80000000) == 0)
4359 if (!best || e->function > best->function)
4364 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
4366 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4368 struct kvm_cpuid_entry2 *best;
4370 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
4371 if (!best || best->eax < 0x80000008)
4373 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4375 return best->eax & 0xff;
4380 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4382 u32 function, index;
4383 struct kvm_cpuid_entry2 *best;
4385 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4386 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4387 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4388 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4389 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4390 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4391 best = kvm_find_cpuid_entry(vcpu, function, index);
4393 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4394 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4395 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4396 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
4398 kvm_x86_ops->skip_emulated_instruction(vcpu);
4399 trace_kvm_cpuid(function,
4400 kvm_register_read(vcpu, VCPU_REGS_RAX),
4401 kvm_register_read(vcpu, VCPU_REGS_RBX),
4402 kvm_register_read(vcpu, VCPU_REGS_RCX),
4403 kvm_register_read(vcpu, VCPU_REGS_RDX));
4405 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
4408 * Check if userspace requested an interrupt window, and that the
4409 * interrupt window is open.
4411 * No need to exit to userspace if we already have an interrupt queued.
4413 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
4415 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
4416 vcpu->run->request_interrupt_window &&
4417 kvm_arch_interrupt_allowed(vcpu));
4420 static void post_kvm_run_save(struct kvm_vcpu *vcpu)
4422 struct kvm_run *kvm_run = vcpu->run;
4424 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
4425 kvm_run->cr8 = kvm_get_cr8(vcpu);
4426 kvm_run->apic_base = kvm_get_apic_base(vcpu);
4427 if (irqchip_in_kernel(vcpu->kvm))
4428 kvm_run->ready_for_interrupt_injection = 1;
4430 kvm_run->ready_for_interrupt_injection =
4431 kvm_arch_interrupt_allowed(vcpu) &&
4432 !kvm_cpu_has_interrupt(vcpu) &&
4433 !kvm_event_needs_reinjection(vcpu);
4436 static void vapic_enter(struct kvm_vcpu *vcpu)
4438 struct kvm_lapic *apic = vcpu->arch.apic;
4441 if (!apic || !apic->vapic_addr)
4444 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
4446 vcpu->arch.apic->vapic_page = page;
4449 static void vapic_exit(struct kvm_vcpu *vcpu)
4451 struct kvm_lapic *apic = vcpu->arch.apic;
4454 if (!apic || !apic->vapic_addr)
4457 idx = srcu_read_lock(&vcpu->kvm->srcu);
4458 kvm_release_page_dirty(apic->vapic_page);
4459 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
4460 srcu_read_unlock(&vcpu->kvm->srcu, idx);
4463 static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4467 if (!kvm_x86_ops->update_cr8_intercept)
4470 if (!vcpu->arch.apic)
4473 if (!vcpu->arch.apic->vapic_addr)
4474 max_irr = kvm_lapic_find_highest_irr(vcpu);
4481 tpr = kvm_lapic_get_cr8(vcpu);
4483 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4486 static void inject_pending_event(struct kvm_vcpu *vcpu)
4488 /* try to reinject previous events if any */
4489 if (vcpu->arch.exception.pending) {
4490 trace_kvm_inj_exception(vcpu->arch.exception.nr,
4491 vcpu->arch.exception.has_error_code,
4492 vcpu->arch.exception.error_code);
4493 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4494 vcpu->arch.exception.has_error_code,
4495 vcpu->arch.exception.error_code,
4496 vcpu->arch.exception.reinject);
4500 if (vcpu->arch.nmi_injected) {
4501 kvm_x86_ops->set_nmi(vcpu);
4505 if (vcpu->arch.interrupt.pending) {
4506 kvm_x86_ops->set_irq(vcpu);
4510 /* try to inject new event if pending */
4511 if (vcpu->arch.nmi_pending) {
4512 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4513 vcpu->arch.nmi_pending = false;
4514 vcpu->arch.nmi_injected = true;
4515 kvm_x86_ops->set_nmi(vcpu);
4517 } else if (kvm_cpu_has_interrupt(vcpu)) {
4518 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
4519 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4521 kvm_x86_ops->set_irq(vcpu);
4526 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
4529 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
4530 vcpu->run->request_interrupt_window;
4533 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4534 kvm_mmu_unload(vcpu);
4536 r = kvm_mmu_reload(vcpu);
4540 if (vcpu->requests) {
4541 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
4542 __kvm_migrate_timers(vcpu);
4543 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4544 kvm_write_guest_time(vcpu);
4545 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4546 kvm_mmu_sync_roots(vcpu);
4547 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4548 kvm_x86_ops->tlb_flush(vcpu);
4549 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4551 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
4555 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
4556 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
4560 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4561 vcpu->fpu_active = 0;
4562 kvm_x86_ops->fpu_deactivate(vcpu);
4568 kvm_x86_ops->prepare_guest_switch(vcpu);
4569 if (vcpu->fpu_active)
4570 kvm_load_guest_fpu(vcpu);
4572 atomic_set(&vcpu->guest_mode, 1);
4575 local_irq_disable();
4577 if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
4578 || need_resched() || signal_pending(current)) {
4579 atomic_set(&vcpu->guest_mode, 0);
4587 inject_pending_event(vcpu);
4589 /* enable NMI/IRQ window open exits if needed */
4590 if (vcpu->arch.nmi_pending)
4591 kvm_x86_ops->enable_nmi_window(vcpu);
4592 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4593 kvm_x86_ops->enable_irq_window(vcpu);
4595 if (kvm_lapic_enabled(vcpu)) {
4596 update_cr8_intercept(vcpu);
4597 kvm_lapic_sync_to_vapic(vcpu);
4600 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4604 if (unlikely(vcpu->arch.switch_db_regs)) {
4606 set_debugreg(vcpu->arch.eff_db[0], 0);
4607 set_debugreg(vcpu->arch.eff_db[1], 1);
4608 set_debugreg(vcpu->arch.eff_db[2], 2);
4609 set_debugreg(vcpu->arch.eff_db[3], 3);
4612 trace_kvm_entry(vcpu->vcpu_id);
4613 kvm_x86_ops->run(vcpu);
4616 * If the guest has used debug registers, at least dr7
4617 * will be disabled while returning to the host.
4618 * If we don't have active breakpoints in the host, we don't
4619 * care about the messed up debug address registers. But if
4620 * we have some of them active, restore the old state.
4622 if (hw_breakpoint_active())
4623 hw_breakpoint_restore();
4625 atomic_set(&vcpu->guest_mode, 0);
4632 * We must have an instruction between local_irq_enable() and
4633 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4634 * the interrupt shadow. The stat.exits increment will do nicely.
4635 * But we need to prevent reordering, hence this barrier():
4643 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4646 * Profile KVM exit RIPs:
4648 if (unlikely(prof_on == KVM_PROFILING)) {
4649 unsigned long rip = kvm_rip_read(vcpu);
4650 profile_hit(KVM_PROFILING, (void *)rip);
4654 kvm_lapic_sync_from_vapic(vcpu);
4656 r = kvm_x86_ops->handle_exit(vcpu);
4662 static int __vcpu_run(struct kvm_vcpu *vcpu)
4665 struct kvm *kvm = vcpu->kvm;
4667 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
4668 pr_debug("vcpu %d received sipi with vector # %x\n",
4669 vcpu->vcpu_id, vcpu->arch.sipi_vector);
4670 kvm_lapic_reset(vcpu);
4671 r = kvm_arch_vcpu_reset(vcpu);
4674 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4677 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
4682 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
4683 r = vcpu_enter_guest(vcpu);
4685 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
4686 kvm_vcpu_block(vcpu);
4687 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
4688 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
4690 switch(vcpu->arch.mp_state) {
4691 case KVM_MP_STATE_HALTED:
4692 vcpu->arch.mp_state =
4693 KVM_MP_STATE_RUNNABLE;
4694 case KVM_MP_STATE_RUNNABLE:
4696 case KVM_MP_STATE_SIPI_RECEIVED:
4707 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4708 if (kvm_cpu_has_pending_timer(vcpu))
4709 kvm_inject_pending_timer_irqs(vcpu);
4711 if (dm_request_for_irq_injection(vcpu)) {
4713 vcpu->run->exit_reason = KVM_EXIT_INTR;
4714 ++vcpu->stat.request_irq_exits;
4716 if (signal_pending(current)) {
4718 vcpu->run->exit_reason = KVM_EXIT_INTR;
4719 ++vcpu->stat.signal_exits;
4721 if (need_resched()) {
4722 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
4724 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
4728 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
4735 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4740 if (vcpu->sigset_active)
4741 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4743 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4744 kvm_vcpu_block(vcpu);
4745 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
4750 /* re-sync apic's tpr */
4751 if (!irqchip_in_kernel(vcpu->kvm))
4752 kvm_set_cr8(vcpu, kvm_run->cr8);
4754 if (vcpu->arch.pio.count || vcpu->mmio_needed ||
4755 vcpu->arch.emulate_ctxt.restart) {
4756 if (vcpu->mmio_needed) {
4757 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4758 vcpu->mmio_read_completed = 1;
4759 vcpu->mmio_needed = 0;
4761 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4762 r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE);
4763 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4764 if (r != EMULATE_DONE) {
4769 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4770 kvm_register_write(vcpu, VCPU_REGS_RAX,
4771 kvm_run->hypercall.ret);
4773 r = __vcpu_run(vcpu);
4776 post_kvm_run_save(vcpu);
4777 if (vcpu->sigset_active)
4778 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4783 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4785 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4786 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4787 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4788 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4789 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4790 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4791 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4792 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4793 #ifdef CONFIG_X86_64
4794 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4795 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4796 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4797 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4798 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4799 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4800 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4801 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
4804 regs->rip = kvm_rip_read(vcpu);
4805 regs->rflags = kvm_get_rflags(vcpu);
4810 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4812 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4813 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4814 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4815 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4816 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4817 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4818 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4819 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
4820 #ifdef CONFIG_X86_64
4821 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4822 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4823 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4824 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4825 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4826 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4827 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4828 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
4831 kvm_rip_write(vcpu, regs->rip);
4832 kvm_set_rflags(vcpu, regs->rflags);
4834 vcpu->arch.exception.pending = false;
4839 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4841 struct kvm_segment cs;
4843 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
4847 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4849 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4850 struct kvm_sregs *sregs)
4854 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4855 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4856 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4857 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4858 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4859 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
4861 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4862 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4864 kvm_x86_ops->get_idt(vcpu, &dt);
4865 sregs->idt.limit = dt.size;
4866 sregs->idt.base = dt.address;
4867 kvm_x86_ops->get_gdt(vcpu, &dt);
4868 sregs->gdt.limit = dt.size;
4869 sregs->gdt.base = dt.address;
4871 sregs->cr0 = kvm_read_cr0(vcpu);
4872 sregs->cr2 = vcpu->arch.cr2;
4873 sregs->cr3 = vcpu->arch.cr3;
4874 sregs->cr4 = kvm_read_cr4(vcpu);
4875 sregs->cr8 = kvm_get_cr8(vcpu);
4876 sregs->efer = vcpu->arch.efer;
4877 sregs->apic_base = kvm_get_apic_base(vcpu);
4879 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
4881 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
4882 set_bit(vcpu->arch.interrupt.nr,
4883 (unsigned long *)sregs->interrupt_bitmap);
4888 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4889 struct kvm_mp_state *mp_state)
4891 mp_state->mp_state = vcpu->arch.mp_state;
4895 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4896 struct kvm_mp_state *mp_state)
4898 vcpu->arch.mp_state = mp_state->mp_state;
4902 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
4903 bool has_error_code, u32 error_code)
4905 struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
4906 int cs_db, cs_l, ret;
4907 cache_all_regs(vcpu);
4909 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4911 vcpu->arch.emulate_ctxt.vcpu = vcpu;
4912 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
4913 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
4914 vcpu->arch.emulate_ctxt.mode =
4915 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
4916 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
4917 ? X86EMUL_MODE_VM86 : cs_l
4918 ? X86EMUL_MODE_PROT64 : cs_db
4919 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
4920 memset(c, 0, sizeof(struct decode_cache));
4921 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
4923 ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, &emulate_ops,
4924 tss_selector, reason, has_error_code,
4928 return EMULATE_FAIL;
4930 memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
4931 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
4932 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
4933 return EMULATE_DONE;
4935 EXPORT_SYMBOL_GPL(kvm_task_switch);
4937 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4938 struct kvm_sregs *sregs)
4940 int mmu_reset_needed = 0;
4941 int pending_vec, max_bits;
4944 dt.size = sregs->idt.limit;
4945 dt.address = sregs->idt.base;
4946 kvm_x86_ops->set_idt(vcpu, &dt);
4947 dt.size = sregs->gdt.limit;
4948 dt.address = sregs->gdt.base;
4949 kvm_x86_ops->set_gdt(vcpu, &dt);
4951 vcpu->arch.cr2 = sregs->cr2;
4952 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
4953 vcpu->arch.cr3 = sregs->cr3;
4955 kvm_set_cr8(vcpu, sregs->cr8);
4957 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
4958 kvm_x86_ops->set_efer(vcpu, sregs->efer);
4959 kvm_set_apic_base(vcpu, sregs->apic_base);
4961 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
4962 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
4963 vcpu->arch.cr0 = sregs->cr0;
4965 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
4966 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
4967 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
4968 load_pdptrs(vcpu, vcpu->arch.cr3);
4969 mmu_reset_needed = 1;
4972 if (mmu_reset_needed)
4973 kvm_mmu_reset_context(vcpu);
4975 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4976 pending_vec = find_first_bit(
4977 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4978 if (pending_vec < max_bits) {
4979 kvm_queue_interrupt(vcpu, pending_vec, false);
4980 pr_debug("Set back pending irq %d\n", pending_vec);
4981 if (irqchip_in_kernel(vcpu->kvm))
4982 kvm_pic_clear_isr_ack(vcpu->kvm);
4985 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4986 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4987 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4988 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4989 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4990 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
4992 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4993 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4995 update_cr8_intercept(vcpu);
4997 /* Older userspace won't unhalt the vcpu on reset. */
4998 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
4999 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
5001 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5006 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5007 struct kvm_guest_debug *dbg)
5009 unsigned long rflags;
5012 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5014 if (vcpu->arch.exception.pending)
5016 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5017 kvm_queue_exception(vcpu, DB_VECTOR);
5019 kvm_queue_exception(vcpu, BP_VECTOR);
5023 * Read rflags as long as potentially injected trace flags are still
5026 rflags = kvm_get_rflags(vcpu);
5028 vcpu->guest_debug = dbg->control;
5029 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5030 vcpu->guest_debug = 0;
5032 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
5033 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5034 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5035 vcpu->arch.switch_db_regs =
5036 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5038 for (i = 0; i < KVM_NR_DB_REGS; i++)
5039 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5040 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5043 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5044 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
5045 get_segment_base(vcpu, VCPU_SREG_CS);
5048 * Trigger an rflags update that will inject or remove the trace
5051 kvm_set_rflags(vcpu, rflags);
5053 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5063 * Translate a guest virtual address to a guest physical address.
5065 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5066 struct kvm_translation *tr)
5068 unsigned long vaddr = tr->linear_address;
5072 idx = srcu_read_lock(&vcpu->kvm->srcu);
5073 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
5074 srcu_read_unlock(&vcpu->kvm->srcu, idx);
5075 tr->physical_address = gpa;
5076 tr->valid = gpa != UNMAPPED_GVA;
5083 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5085 struct i387_fxsave_struct *fxsave =
5086 &vcpu->arch.guest_fpu.state->fxsave;
5088 memcpy(fpu->fpr, fxsave->st_space, 128);
5089 fpu->fcw = fxsave->cwd;
5090 fpu->fsw = fxsave->swd;
5091 fpu->ftwx = fxsave->twd;
5092 fpu->last_opcode = fxsave->fop;
5093 fpu->last_ip = fxsave->rip;
5094 fpu->last_dp = fxsave->rdp;
5095 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5100 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5102 struct i387_fxsave_struct *fxsave =
5103 &vcpu->arch.guest_fpu.state->fxsave;
5105 memcpy(fxsave->st_space, fpu->fpr, 128);
5106 fxsave->cwd = fpu->fcw;
5107 fxsave->swd = fpu->fsw;
5108 fxsave->twd = fpu->ftwx;
5109 fxsave->fop = fpu->last_opcode;
5110 fxsave->rip = fpu->last_ip;
5111 fxsave->rdp = fpu->last_dp;
5112 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5117 void fx_init(struct kvm_vcpu *vcpu)
5119 fpu_alloc(&vcpu->arch.guest_fpu);
5120 fpu_finit(&vcpu->arch.guest_fpu);
5122 vcpu->arch.cr0 |= X86_CR0_ET;
5124 EXPORT_SYMBOL_GPL(fx_init);
5126 static void fx_free(struct kvm_vcpu *vcpu)
5128 fpu_free(&vcpu->arch.guest_fpu);
5131 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5133 if (vcpu->guest_fpu_loaded)
5136 vcpu->guest_fpu_loaded = 1;
5137 unlazy_fpu(current);
5138 fpu_restore_checking(&vcpu->arch.guest_fpu);
5142 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5144 if (!vcpu->guest_fpu_loaded)
5147 vcpu->guest_fpu_loaded = 0;
5148 fpu_save_init(&vcpu->arch.guest_fpu);
5149 ++vcpu->stat.fpu_reload;
5150 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
5154 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5156 if (vcpu->arch.time_page) {
5157 kvm_release_page_dirty(vcpu->arch.time_page);
5158 vcpu->arch.time_page = NULL;
5162 kvm_x86_ops->vcpu_free(vcpu);
5165 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5168 return kvm_x86_ops->vcpu_create(kvm, id);
5171 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5175 vcpu->arch.mtrr_state.have_fixed = 1;
5177 r = kvm_arch_vcpu_reset(vcpu);
5179 r = kvm_mmu_setup(vcpu);
5186 kvm_x86_ops->vcpu_free(vcpu);
5190 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
5193 kvm_mmu_unload(vcpu);
5197 kvm_x86_ops->vcpu_free(vcpu);
5200 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5202 vcpu->arch.nmi_pending = false;
5203 vcpu->arch.nmi_injected = false;
5205 vcpu->arch.switch_db_regs = 0;
5206 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5207 vcpu->arch.dr6 = DR6_FIXED_1;
5208 vcpu->arch.dr7 = DR7_FIXED_1;
5210 return kvm_x86_ops->vcpu_reset(vcpu);
5213 int kvm_arch_hardware_enable(void *garbage)
5216 * Since this may be called from a hotplug notifcation,
5217 * we can't get the CPU frequency directly.
5219 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5220 int cpu = raw_smp_processor_id();
5221 per_cpu(cpu_tsc_khz, cpu) = 0;
5224 kvm_shared_msr_cpu_online();
5226 return kvm_x86_ops->hardware_enable(garbage);
5229 void kvm_arch_hardware_disable(void *garbage)
5231 kvm_x86_ops->hardware_disable(garbage);
5232 drop_user_return_notifiers(garbage);
5235 int kvm_arch_hardware_setup(void)
5237 return kvm_x86_ops->hardware_setup();
5240 void kvm_arch_hardware_unsetup(void)
5242 kvm_x86_ops->hardware_unsetup();
5245 void kvm_arch_check_processor_compat(void *rtn)
5247 kvm_x86_ops->check_processor_compatibility(rtn);
5250 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5256 BUG_ON(vcpu->kvm == NULL);
5259 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
5260 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
5261 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5263 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
5265 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5270 vcpu->arch.pio_data = page_address(page);
5272 r = kvm_mmu_create(vcpu);
5274 goto fail_free_pio_data;
5276 if (irqchip_in_kernel(kvm)) {
5277 r = kvm_create_lapic(vcpu);
5279 goto fail_mmu_destroy;
5282 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5284 if (!vcpu->arch.mce_banks) {
5286 goto fail_free_lapic;
5288 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5292 kvm_free_lapic(vcpu);
5294 kvm_mmu_destroy(vcpu);
5296 free_page((unsigned long)vcpu->arch.pio_data);
5301 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5305 kfree(vcpu->arch.mce_banks);
5306 kvm_free_lapic(vcpu);
5307 idx = srcu_read_lock(&vcpu->kvm->srcu);
5308 kvm_mmu_destroy(vcpu);
5309 srcu_read_unlock(&vcpu->kvm->srcu, idx);
5310 free_page((unsigned long)vcpu->arch.pio_data);
5313 struct kvm *kvm_arch_create_vm(void)
5315 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5318 return ERR_PTR(-ENOMEM);
5320 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5321 if (!kvm->arch.aliases) {
5323 return ERR_PTR(-ENOMEM);
5326 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
5327 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
5329 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5330 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5332 rdtscll(kvm->arch.vm_init_tsc);
5337 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5340 kvm_mmu_unload(vcpu);
5344 static void kvm_free_vcpus(struct kvm *kvm)
5347 struct kvm_vcpu *vcpu;
5350 * Unpin any mmu pages first.
5352 kvm_for_each_vcpu(i, vcpu, kvm)
5353 kvm_unload_vcpu_mmu(vcpu);
5354 kvm_for_each_vcpu(i, vcpu, kvm)
5355 kvm_arch_vcpu_free(vcpu);
5357 mutex_lock(&kvm->lock);
5358 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5359 kvm->vcpus[i] = NULL;
5361 atomic_set(&kvm->online_vcpus, 0);
5362 mutex_unlock(&kvm->lock);
5365 void kvm_arch_sync_events(struct kvm *kvm)
5367 kvm_free_all_assigned_devices(kvm);
5370 void kvm_arch_destroy_vm(struct kvm *kvm)
5372 kvm_iommu_unmap_guest(kvm);
5374 kfree(kvm->arch.vpic);
5375 kfree(kvm->arch.vioapic);
5376 kvm_free_vcpus(kvm);
5377 kvm_free_physmem(kvm);
5378 if (kvm->arch.apic_access_page)
5379 put_page(kvm->arch.apic_access_page);
5380 if (kvm->arch.ept_identity_pagetable)
5381 put_page(kvm->arch.ept_identity_pagetable);
5382 cleanup_srcu_struct(&kvm->srcu);
5383 kfree(kvm->arch.aliases);
5387 int kvm_arch_prepare_memory_region(struct kvm *kvm,
5388 struct kvm_memory_slot *memslot,
5389 struct kvm_memory_slot old,
5390 struct kvm_userspace_memory_region *mem,
5393 int npages = memslot->npages;
5395 /*To keep backward compatibility with older userspace,
5396 *x86 needs to hanlde !user_alloc case.
5399 if (npages && !old.rmap) {
5400 unsigned long userspace_addr;
5402 down_write(¤t->mm->mmap_sem);
5403 userspace_addr = do_mmap(NULL, 0,
5405 PROT_READ | PROT_WRITE,
5406 MAP_PRIVATE | MAP_ANONYMOUS,
5408 up_write(¤t->mm->mmap_sem);
5410 if (IS_ERR((void *)userspace_addr))
5411 return PTR_ERR((void *)userspace_addr);
5413 memslot->userspace_addr = userspace_addr;
5421 void kvm_arch_commit_memory_region(struct kvm *kvm,
5422 struct kvm_userspace_memory_region *mem,
5423 struct kvm_memory_slot old,
5427 int npages = mem->memory_size >> PAGE_SHIFT;
5429 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5432 down_write(¤t->mm->mmap_sem);
5433 ret = do_munmap(current->mm, old.userspace_addr,
5434 old.npages * PAGE_SIZE);
5435 up_write(¤t->mm->mmap_sem);
5438 "kvm_vm_ioctl_set_memory_region: "
5439 "failed to munmap memory\n");
5442 spin_lock(&kvm->mmu_lock);
5443 if (!kvm->arch.n_requested_mmu_pages) {
5444 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5445 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5448 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
5449 spin_unlock(&kvm->mmu_lock);
5452 void kvm_arch_flush_shadow(struct kvm *kvm)
5454 kvm_mmu_zap_all(kvm);
5455 kvm_reload_remote_mmus(kvm);
5458 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5460 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
5461 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5462 || vcpu->arch.nmi_pending ||
5463 (kvm_arch_interrupt_allowed(vcpu) &&
5464 kvm_cpu_has_interrupt(vcpu));
5467 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5470 int cpu = vcpu->cpu;
5472 if (waitqueue_active(&vcpu->wq)) {
5473 wake_up_interruptible(&vcpu->wq);
5474 ++vcpu->stat.halt_wakeup;
5478 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5479 if (atomic_xchg(&vcpu->guest_mode, 0))
5480 smp_send_reschedule(cpu);
5484 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5486 return kvm_x86_ops->interrupt_allowed(vcpu);
5489 bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
5491 unsigned long current_rip = kvm_rip_read(vcpu) +
5492 get_segment_base(vcpu, VCPU_SREG_CS);
5494 return current_rip == linear_rip;
5496 EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
5498 unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5500 unsigned long rflags;
5502 rflags = kvm_x86_ops->get_rflags(vcpu);
5503 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5504 rflags &= ~X86_EFLAGS_TF;
5507 EXPORT_SYMBOL_GPL(kvm_get_rflags);
5509 void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5511 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5512 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
5513 rflags |= X86_EFLAGS_TF;
5514 kvm_x86_ops->set_rflags(vcpu, rflags);
5516 EXPORT_SYMBOL_GPL(kvm_set_rflags);
5518 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5519 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5520 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5521 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5522 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
5523 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
5524 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
5525 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
5526 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
5527 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
5528 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
5529 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);